2022-03-08 20:22:07 -07:00
|
|
|
#! /usr/bin/lua
|
|
|
|
--[[
|
|
|
|
|
2024-05-29 01:45:25 -06:00
|
|
|
Part of AREDN® -- Used for creating Amateur Radio Emergency Data Networks
|
2023-04-28 12:01:58 -06:00
|
|
|
Copyright (C) 2021 Tim Wilkinson
|
|
|
|
Original Perl Copyright (C) 2015 Conrad Lara
|
|
|
|
See Contributors file for additional contributors
|
2022-03-08 20:22:07 -07:00
|
|
|
|
2023-04-28 12:01:58 -06:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation version 3 of the License.
|
2022-03-08 20:22:07 -07:00
|
|
|
|
2023-04-28 12:01:58 -06:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2022-03-08 20:22:07 -07:00
|
|
|
|
2023-04-28 12:01:58 -06:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2022-03-08 20:22:07 -07:00
|
|
|
|
2023-04-28 12:01:58 -06:00
|
|
|
Additional Terms:
|
2022-03-08 20:22:07 -07:00
|
|
|
|
2024-05-29 01:45:25 -06:00
|
|
|
Additional use restrictions exist on the AREDN® trademark and logo.
|
2023-04-28 12:01:58 -06:00
|
|
|
See AREDNLicense.txt for more info.
|
2022-03-08 20:22:07 -07:00
|
|
|
|
2024-05-29 01:45:25 -06:00
|
|
|
Attributions to the AREDN® Project must be retained in the source code.
|
2023-04-28 12:01:58 -06:00
|
|
|
If importing this code into a new or existing project attribution
|
2024-05-29 01:45:25 -06:00
|
|
|
to the AREDN® project must be added to the source code.
|
2022-03-08 20:22:07 -07:00
|
|
|
|
2023-04-28 12:01:58 -06:00
|
|
|
You must not misrepresent the origin of the material contained within.
|
2022-03-08 20:22:07 -07:00
|
|
|
|
2023-04-28 12:01:58 -06:00
|
|
|
Modified versions must be modified to attribute to the original source
|
|
|
|
and be marked in reasonable ways as differentiate it from the original
|
|
|
|
version
|
2022-03-08 20:22:07 -07:00
|
|
|
|
|
|
|
--]]
|
|
|
|
|
|
|
|
-- do the initial setup of the essential nvram variables
|
|
|
|
-- node - node name
|
|
|
|
-- mac2 - last 3 bytes of wifi mac address in the form ddd.ddd.ddd
|
|
|
|
-- wifi_mac - full mac address of wireless card in the form hh:hh:hh:hh:hh:hh
|
|
|
|
--
|
|
|
|
-- intended to run every boot but it should only actually do anything
|
|
|
|
-- on the first boot
|
|
|
|
|
|
|
|
require("aredn.utils")
|
|
|
|
require("iwinfo")
|
|
|
|
require("aredn.hardware")
|
2024-04-01 23:15:45 -06:00
|
|
|
require('aredn.info')
|
2022-03-08 20:22:07 -07:00
|
|
|
|
2024-04-01 23:15:45 -06:00
|
|
|
local wifi_mac = aredn.info.get_nvram("wifimac")
|
|
|
|
local mac2 = aredn.info.get_nvram("mac2")
|
|
|
|
local node = aredn.info.get_nvram("node")
|
|
|
|
local dtdmac = aredn.info.get_nvram("dtdmac")
|
2022-03-08 20:22:07 -07:00
|
|
|
|
|
|
|
local hardware_mac
|
|
|
|
if wifi_mac == "" or mac2 == "" then
|
2023-06-25 20:10:07 -06:00
|
|
|
if aredn.hardware.has_wifi() then
|
2023-04-28 12:01:58 -06:00
|
|
|
local phy
|
|
|
|
for i = 1,5
|
|
|
|
do
|
2023-06-25 20:10:07 -06:00
|
|
|
for _, wlan in ipairs({ "wlan0", "wlan1" })
|
|
|
|
do
|
|
|
|
local f = io.popen("iwinfo " .. wlan .. " info")
|
|
|
|
if f then
|
|
|
|
for line in f:lines()
|
|
|
|
do
|
|
|
|
phy = line:match("PHY name:%s*([a-z0-4]+)")
|
|
|
|
if phy then
|
|
|
|
break
|
|
|
|
end
|
2023-04-28 12:01:58 -06:00
|
|
|
end
|
2023-06-25 20:10:07 -06:00
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
if phy then
|
|
|
|
break
|
2023-04-28 12:01:58 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if phy then
|
2023-06-25 20:10:07 -06:00
|
|
|
for line in io.lines("/sys/class/ieee80211/" .. phy .. "/macaddress")
|
|
|
|
do
|
|
|
|
local m = line:match("(%w%w:%w%w:%w%w:%w%w:%w%w:%w%w)")
|
|
|
|
if m then
|
|
|
|
hardware_mac = m
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2023-04-28 12:01:58 -06:00
|
|
|
break
|
|
|
|
end
|
|
|
|
sleep(5)
|
|
|
|
end
|
2022-03-08 20:22:07 -07:00
|
|
|
end
|
|
|
|
if not hardware_mac then
|
2023-04-28 12:01:58 -06:00
|
|
|
if not aredn.hardware.has_wifi() then
|
|
|
|
-- Non wifi device, create a random mac address instead
|
|
|
|
hardware_mac = capture([[hexdump -n5 -e'/5 "02" 5/1 ":%02X"' /dev/random]]):match("^(%S+)")
|
|
|
|
-- Disable wifi
|
|
|
|
os.execute("sed -i -e 's/wifi_enable = 1/wifi_enable = 0/' /etc/config.mesh/_setup")
|
|
|
|
else
|
|
|
|
io.stderr:write("ERROR: hardware mac not found\n")
|
|
|
|
os.exit(-1)
|
|
|
|
end
|
2022-03-08 20:22:07 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if wifi_mac == "" then
|
2024-04-01 23:15:45 -06:00
|
|
|
aredn.info.set_nvram("wifimac", hardware_mac)
|
2022-03-08 20:22:07 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
if mac2 == "" then
|
|
|
|
local a, b, c = hardware_mac:match("%w%w:%w%w:%w%w:(%w%w):(%w%w):(%w%w)")
|
|
|
|
mac2 = string.format("%d.%d.%d", tonumber(a, 16), tonumber(b, 16), tonumber(c, 16))
|
2024-04-01 23:15:45 -06:00
|
|
|
aredn.info.set_nvram("mac2", mac2)
|
2022-03-08 20:22:07 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
if node == "" then
|
2024-04-01 23:15:45 -06:00
|
|
|
aredn.info.set_nvram("node", "NOCALL-" .. mac2:gsub("%.", "-"))
|
2022-03-08 20:22:07 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
if dtdmac == "" then
|
|
|
|
local a, b, c = aredn.hardware.get_interface_mac(aredn.hardware.get_iface_name("lan")):match("%w%w:%w%w:%w%w:(%w%w):(%w%w):(%w%w)")
|
2024-03-26 23:56:21 -06:00
|
|
|
dtdmac = string.format("%d.%d.%d", tonumber(a, 16), tonumber(b, 16), tonumber(c, 16))
|
|
|
|
if dtdmac == mac2 then
|
|
|
|
a = tonumber(a, 16) + 1
|
|
|
|
if a > 255 then
|
|
|
|
a = 0
|
|
|
|
end
|
|
|
|
dtdmac = string.format("%d.%d.%d", a, tonumber(b, 16), tonumber(c, 16))
|
|
|
|
end
|
2024-04-01 23:15:45 -06:00
|
|
|
aredn.info.set_nvram("dtdmac", dtdmac)
|
2022-03-08 20:22:07 -07:00
|
|
|
end
|