This commit is contained in:
Matthias Schiffer 2016-08-29 07:09:57 +00:00 committed by GitHub
commit 11e8fd3e0d
22 changed files with 387 additions and 326 deletions

View File

@ -1,16 +1,16 @@
#!/usr/bin/lua
local sysconfig = require 'gluon.sysconfig'
local lutil = require 'luci.util'
local uci = require('luci.model.uci').cursor()
if not uci:get('network', 'client') then
uci:section('network', 'interface', 'client',
{
type = 'bridge',
}
)
end
uci:section('network', 'interface', 'client',
{
type = 'bridge',
}
)
local ifname = uci:get('network', 'client', 'ifname')
@ -21,6 +21,13 @@ if type(ifname) == 'string' then
end
end
if sysconfig.lan_ifname and not ifname and not uci:get_bool('network', 'mesh_lan', 'auto') then
for _, lanif in ipairs(lutil.split(sysconfig.lan_ifname, ' ')) do
uci:add_to_set('network', 'client', 'ifname', lanif)
end
end
uci:set('network', 'client', 'macaddr', sysconfig.primary_mac)
uci:save('network')

View File

@ -0,0 +1,76 @@
local util = require 'gluon.util'
local json = require 'luci.jsonc'
local ltn12 = require 'luci.ltn12'
local lutil = require 'luci.util'
local assert = assert
local io = io
local ipairs = ipairs
local mesh_interfaces = '/lib/gluon/core/mesh_interfaces.json'
local interface_lock = '/var/lock/gluon_core_mesh_interfaces'
local function _interfaces()
local file = io.open(mesh_interfaces)
if not file then
return {}
end
local decoder = json.new()
ltn12.pump.all(ltn12.source.file(file), decoder:sink())
file:close()
return assert(decoder:get())
end
local function _update(ifs)
for _, v in pairs(ifs) do
v[{}] = true
end
ifs[{}] = true
file = assert(io.open(mesh_interfaces, 'w'))
file:write(json.stringify(ifs))
file:close()
end
local function _register_interface(name, info)
local ifs = _interfaces()
if not ifs[name] then
ifs[name] = info or {}
_update(ifs)
end
end
local function _unregister_interface(name)
local ifs = _interfaces()
if ifs[name] then
ifs[name] = nil
_update(ifs)
end
end
module 'gluon.mesh'
function interfaces()
return util.locked(interface_lock, _interfaces)
end
-- info may hold additional information about a mesh interface, the following values are defined:
--
-- transitive (boolean): defines if the interface provides transitive connectivity (default: false)
-- fixed_mtu (boolean): defines if the interface's MTU may not be changed (default: false)
--
-- Note: if the interface is already registered, the info is not updated
function register_interface(name, info)
util.locked(interface_lock, _register_interface, name, info)
end
function unregister_interface(name)
util.locked(interface_lock, _unregister_interface, name)
end

View File

@ -1,10 +1,12 @@
#!/usr/bin/lua
local mesh = require 'gluon.mesh'
local util = require 'gluon.util'
local uci = require('luci.model.uci').cursor()
local site = require 'gluon.site_config'
local sysconfig = require 'gluon.sysconfig'
local uci = require('luci.model.uci').cursor()
-- Initial
if not sysconfig.gluon_version then
uci:delete_all('wireless', 'wifi-iface')
@ -18,30 +20,189 @@ local function get_channel(radio, config)
end
end
local function configure_radio(radio, index, config)
if config then
local channel = get_channel(radio, config)
uci:delete('wireless', radio, 'disabled')
uci:set('wireless', radio, 'channel', channel)
uci:set('wireless', radio, 'htmode', 'HT20')
uci:set('wireless', radio, 'country', site.regdom)
if config.supported_rates then
uci:set_list('wireless', radio, 'supported_rates', config.supported_rates)
else
uci:delete('wireless', radio, 'supported_rates')
end
if config.basic_rate then
uci:set_list('wireless', radio, 'basic_rate', config.basic_rate)
else
uci:delete('wireless', radio, 'basic_rate')
end
local function is_disabled(name)
if uci:get('wireless', name) then
return uci:get_bool('wireless', name, 'disabled')
end
end
-- Returns the first argument that is not nil; don't call without any non-nil arguments!
local function first_non_nil(first, ...)
if first ~= nil then
return first
else
return first_non_nil(...)
end
end
local function configure_ibss(config, radio, index, suffix, disabled)
local name = 'ibss_' .. radio
local ifname = 'ibss' .. suffix
uci:delete('network', name)
uci:delete('network', name .. '_vlan')
uci:delete('wireless', name)
if not config then
return
end
local macaddr = util.get_wlan_mac(radio, index, 3)
if not macaddr then
return
end
if config.vlan then
uci:section('network', 'interface', name,
{
proto = 'none',
}
)
uci:section('network', 'interface', name .. '_vlan',
{
ifname = '@' .. name .. '.' .. config.vlan,
proto = 'none',
auto = 1,
}
)
mesh.register_interface(ifname .. '.' .. config.vlan)
else
uci:section('network', 'interface', name,
{
proto = 'none',
auto = 1,
}
)
mesh.register_interface(ifname)
end
uci:section('wireless', 'wifi-iface', name,
{
device = radio,
network = name,
mode = 'adhoc',
ssid = config.ssid,
bssid = config.bssid,
macaddr = macaddr,
mcast_rate = config.mcast_rate,
ifname = ifname,
disabled = disabled and 1 or 0,
}
)
end
local function configure_mesh(config, radio, index, suffix, disabled)
local name = 'mesh_' .. radio
local ifname = 'mesh' .. suffix
local macfilter = uci:get('wireless', name, 'macfilter')
local maclist = uci:get('wireless', name, 'maclist')
uci:delete('network', name)
uci:delete('wireless', name)
if not config then
return
end
local macaddr = util.get_wlan_mac(radio, index, 2)
if not macaddr then
return
end
mesh.register_interface(ifname)
uci:section('network', 'interface', name,
{
proto = 'none',
auto = 1,
}
)
uci:section('wireless', 'wifi-iface', name,
{
device = radio,
network = name,
mode = 'mesh',
mesh_id = config.id,
mesh_fwding = 0,
macaddr = macaddr,
mcast_rate = config.mcast_rate,
ifname = ifname,
disabled = disabled and 1 or 0,
macfilter = macfilter,
maclist = maclist,
}
)
end
local function configure_radio(radio, index, config)
if not config then
return
end
local suffix = radio:match('^radio(%d+)$')
if not suffix then
return
end
local channel = get_channel(radio, config)
uci:delete('wireless', radio, 'disabled')
uci:set('wireless', radio, 'channel', channel)
uci:set('wireless', radio, 'htmode', 'HT20')
uci:set('wireless', radio, 'country', site.regdom)
if config.supported_rates then
uci:set_list('wireless', radio, 'supported_rates', config.supported_rates)
else
uci:delete('wireless', radio, 'supported_rates')
end
if config.basic_rate then
uci:set_list('wireless', radio, 'basic_rate', config.basic_rate)
else
uci:delete('wireless', radio, 'basic_rate')
end
local ibss_disabled = is_disabled('ibss_' .. radio)
local mesh_disabled = is_disabled('mesh_' .. radio)
configure_ibss(config.ibss, radio, index, suffix,
first_non_nil(
ibss_disabled,
mesh_disabled,
(config.ibss or {}).disabled, -- will be nil if config.ibss or config.ibss.disabled is unset
false
)
)
configure_mesh(config.mesh, radio, index, suffix,
first_non_nil(
mesh_disabled,
ibss_disabled,
(config.mesh or {}).disabled, -- will be nil if config.mesh or config.mesh.disabled is unset
false
)
)
end
util.iterate_radios(configure_radio)
if uci:get('system', 'rssid_wlan0') then
if uci:get('wireless', 'mesh_radio0') then
uci:set('system', 'rssid_wlan0', 'dev', 'mesh0')
else
uci:set('system', 'rssid_wlan0', 'dev', 'ibss0')
end
uci:save('system')
end
uci:save('wireless')
uci:save('network')

View File

@ -0,0 +1,17 @@
#!/usr/bin/lua
local network = require 'gluon.network'
local site = require 'gluon.site_config'
local uci = require('luci.model.uci').cursor()
if not uci:get('network', 'mesh_wan') then
uci:section('network', 'interface', 'mesh_wan', {
ifname = 'br-wan',
proto = 'none',
auto = site.mesh_on_wan and 1 or 0,
})
end
c:save('network')
network.update_mesh_on_wan()

View File

@ -1,7 +1,7 @@
#!/usr/bin/lua
local mesh = require 'gluon.mesh'
local site = require 'gluon.site_config'
local util = require 'gluon.util'
local sysconfig = require 'gluon.sysconfig'
local uci = require('luci.model.uci').cursor()
@ -15,10 +15,7 @@ uci:section('network', 'interface', 'mesh_lan', {
ifname = sysconfig.lan_ifname,
type = 'bridge',
igmp_snooping = 0,
proto = 'batadv',
mesh = 'bat0',
mesh_no_rebroadcast = '1',
macaddr = util.get_mac(3),
proto = 'none',
})
if uci:get('network', 'mesh_lan', 'auto') == nil then
@ -41,3 +38,5 @@ if uci:get('network', 'mesh_lan', 'auto') == nil then
end
uci:save('network')
mesh.register_interface('br-mesh_lan', {transitive = true})

View File

@ -0,0 +1,14 @@
-- High-level network config functions
local mesh = require 'gluon.mesh'
local uci = require('luci.model.uci').cursor()
module 'gluon.network'
function update_mesh_on_wan()
if uci:get_bool('network', 'mesh_wan', 'auto') then
mesh.register_interface('br-wan', {transitive = true, fixed_mtu = true})
else
mesh.unregister_interface('br-wan')
end
end

View File

@ -23,12 +23,15 @@ local function escape_args(ret, arg0, ...)
end
local error = error
local io = io
local ipairs = ipairs
local os = os
local pcall = pcall
local string = string
local tonumber = tonumber
local ipairs = ipairs
local table = table
local unpack = unpack
local nixio = require 'nixio'
local hash = require 'hash'
@ -70,6 +73,19 @@ function unlock(file)
exec('lock', '-u', file)
end
function locked(file, f, ...)
lock(file)
local ret = {pcall(f, ...)}
unlock(file)
if ret[1] then
table.remove(ret, 1)
return unpack(ret)
else
error(ret[2])
end
end
function node_id()
return string.gsub(sysconfig.primary_mac, ':', '')
end

View File

@ -18,7 +18,7 @@ define Package/gluon-luci-portconfig
SECTION:=gluon
CATEGORY:=Gluon
TITLE:=Luci module for advanced ethernet port configuration
DEPENDS:=+gluon-luci-admin +gluon-mesh-batman-adv
DEPENDS:=+gluon-luci-admin +gluon-client-bridge
endef
define Build/Prepare

View File

@ -14,6 +14,7 @@ $Id$
local uci = luci.model.uci.cursor()
local lutil = require 'luci.util'
local network = require 'gluon.network'
local sysconfig = require 'gluon.sysconfig'
local wan = uci:get_all("network", "wan")
@ -150,6 +151,8 @@ function f.handle(self, state, data)
uci:commit('system')
end
network.update_mesh_on_wan()
if dns then
if #data.dns > 0 then
uci:set("gluon-wan-dnsmasq", dns, "server", data.dns)

View File

@ -24,12 +24,10 @@ define Build/Configure
endef
define Build/Compile
$(call GluonSrcDiet,./luasrc,$(PKG_BUILD_DIR)/luadest/)
endef
define Package/gluon-mesh-batman-adv-14/install
$(CP) ./files/* $(1)/
$(CP) $(PKG_BUILD_DIR)/luadest/* $(1)/
endef
$(eval $(call BuildPackage,gluon-mesh-batman-adv-14))

View File

@ -0,0 +1,6 @@
[ "$ACTION" = 'add' ] || exit 0
# Exit if we're in setup mode
[ $(lua -e 'uci_state=require("luci.model.uci").cursor_state(); print(uci_state:get_first("gluon-setup-mode", "setup_mode", "running", "0"))') = 0 ] || exit 0
exec /lib/gluon/mesh-batman-adv-core/enable_mesh_interface "$INTERFACE" 1528

View File

@ -1,47 +0,0 @@
#!/usr/bin/lua
local site = require 'gluon.site_config'
local uci = require('luci.model.uci').cursor()
local function configure_mtu(radio, config, mtu)
if config.ibss then
local network = 'ibss_' .. radio
if config.ibss.vlan then
uci:set('network', network, 'mtu', mtu + 4)
uci:set('network', network .. '_vlan', 'mtu', mtu)
else
uci:set('network', network, 'mtu', mtu)
end
end
if config.mesh then
uci:set('network', 'mesh_' .. radio, 'mtu', mtu)
end
end
local radios = {}
uci:foreach('wireless', 'wifi-device',
function(s)
table.insert(radios, s['.name'])
end
)
local mtu = 1528
for _, radio in ipairs(radios) do
local hwmode = uci:get('wireless', radio, 'hwmode')
if hwmode == '11g' or hwmode == '11ng' then
configure_mtu(radio, site.wifi24, mtu)
elseif hwmode == '11a' or hwmode == '11na' then
configure_mtu(radio, site.wifi5, mtu)
end
end
uci:save('network')

View File

@ -24,12 +24,10 @@ define Build/Configure
endef
define Build/Compile
$(call GluonSrcDiet,./luasrc,$(PKG_BUILD_DIR)/luadest/)
endef
define Package/gluon-mesh-batman-adv-15/install
$(CP) ./files/* $(1)/
$(CP) $(PKG_BUILD_DIR)/luadest/* $(1)/
endef
$(eval $(call BuildPackage,gluon-mesh-batman-adv-15))

View File

@ -0,0 +1,6 @@
[ "$ACTION" = 'add' ] || exit 0
# Exit if we're in setup mode
[ $(lua -e 'uci_state=require("luci.model.uci").cursor_state(); print(uci_state:get_first("gluon-setup-mode", "setup_mode", "running", "0"))') = 0 ] || exit 0
exec /lib/gluon/mesh-batman-adv-core/enable_mesh_interface "$INTERFACE" 1532

View File

@ -1,47 +0,0 @@
#!/usr/bin/lua
local site = require 'gluon.site_config'
local uci = require('luci.model.uci').cursor()
local function configure_mtu(radio, config, mtu)
if config.ibss then
local network = 'ibss_' .. radio
if config.ibss.vlan then
uci:set('network', network, 'mtu', mtu + 4)
uci:set('network', network .. '_vlan', 'mtu', mtu)
else
uci:set('network', network, 'mtu', mtu)
end
end
if config.mesh then
uci:set('network', 'mesh_' .. radio, 'mtu', mtu)
end
end
local radios = {}
uci:foreach('wireless', 'wifi-device',
function(s)
table.insert(radios, s['.name'])
end
)
local mtu = 1532
for _, radio in ipairs(radios) do
local hwmode = uci:get('wireless', radio, 'hwmode')
if hwmode == '11g' or hwmode == '11ng' then
configure_mtu(radio, site.wifi24, mtu)
elseif hwmode == '11a' or hwmode == '11na' then
configure_mtu(radio, site.wifi5, mtu)
end
end
uci:save('network')

View File

@ -0,0 +1,38 @@
#!/usr/bin/lua
local mesh = require 'gluon.mesh'
local util = require 'gluon.util'
local fs = require 'nixio.fs'
local ifname = arg[1]
local mtu = tonumber(arg[2])
local interfaces = mesh.interfaces()
local interface = interfaces[ifname]
if not interface then
os.exit(0)
end
if not interface.fixed_mtu then
-- If we have a VLAN on a wireless interface, we need to adjust the MTU of the real interface...
local lower = fs.glob('/sys/class/net/' .. ifname .. '/lower_*')()
if lower then
lower = lower:match('/lower_([^/]+)$')
if fs.access('/sys/class/net/' .. lower .. '/wireless') then
util.exec('ip', 'link', 'set', 'dev', lower, 'mtu', tostring(mtu+4))
end
end
util.exec('ip', 'link', 'set', 'dev', ifname, 'mtu', tostring(mtu))
end
local file = assert(io.open('/sys/class/net/' .. ifname .. '/batman_adv/mesh_iface', 'w'))
file:write('bat0')
file:close()
file = assert(io.open('/sys/class/net/' .. ifname .. '/batman_adv/no_rebroadcast', 'w'))
file:write(interface.transitive and '1' or '0')
file:close()

View File

@ -1,147 +0,0 @@
#!/usr/bin/lua
local site = require 'gluon.site_config'
local util = require 'gluon.util'
local uci = require('luci.model.uci').cursor()
local function is_disabled(name)
if uci:get('wireless', name) then
return uci:get_bool('wireless', name, 'disabled')
end
end
-- Returns the first argument that is not nil; don't call without any non-nil arguments!
local function first_non_nil(first, ...)
if first ~= nil then
return first
else
return first_non_nil(...)
end
end
local function configure_ibss(config, radio, index, suffix, disabled)
local name = 'ibss_' .. radio
uci:delete('network', name)
uci:delete('network', name .. '_vlan')
uci:delete('wireless', name)
if not config then
return
end
local macaddr = util.get_wlan_mac(radio, index, 3)
if not macaddr then
return
end
if config.vlan then
uci:section('network', 'interface', name,
{
proto = 'none',
}
)
uci:section('network', 'interface', name .. '_vlan',
{
ifname = '@' .. name .. '.' .. config.vlan,
proto = 'batadv',
mesh = 'bat0',
}
)
else
uci:section('network', 'interface', name,
{
proto = 'batadv',
mesh = 'bat0',
}
)
end
uci:section('wireless', 'wifi-iface', name,
{
device = radio,
network = name,
mode = 'adhoc',
ssid = config.ssid,
bssid = config.bssid,
macaddr = macaddr,
mcast_rate = config.mcast_rate,
ifname = suffix and 'ibss' .. suffix,
disabled = disabled and 1 or 0,
}
)
end
local function configure_mesh(config, radio, index, suffix, disabled)
local name = 'mesh_' .. radio
local macfilter = uci:get('wireless', name, 'macfilter')
local maclist = uci:get('wireless', name, 'maclist')
uci:delete('network', name)
uci:delete('wireless', name)
if not config then
return
end
local macaddr = util.get_wlan_mac(radio, index, 2)
if not macaddr then
return
end
uci:section('network', 'interface', name,
{
proto = 'batadv',
mesh = 'bat0',
}
)
uci:section('wireless', 'wifi-iface', name,
{
device = radio,
network = name,
mode = 'mesh',
mesh_id = config.id,
mesh_fwding = 0,
macaddr = macaddr,
mcast_rate = config.mcast_rate,
ifname = suffix and 'mesh' .. suffix,
disabled = disabled and 1 or 0,
macfilter = macfilter,
maclist = maclist,
}
)
end
local function configure_radio(radio, index, config)
local suffix = radio:match('^radio(%d+)$')
local ibss_disabled = is_disabled('ibss_' .. radio)
local mesh_disabled = is_disabled('mesh_' .. radio)
configure_ibss(config.ibss, radio, index, suffix,
first_non_nil(
ibss_disabled,
mesh_disabled,
(config.ibss or {}).disabled, -- will be nil if config.ibss or config.ibss.disabled is unset
false
)
)
configure_mesh(config.mesh, radio, index, suffix,
first_non_nil(
mesh_disabled,
ibss_disabled,
(config.mesh or {}).disabled, -- will be nil if config.mesh or config.mesh.disabled is unset
false
)
)
end
util.iterate_radios(configure_radio)
uci:save('wireless')
uci:save('network')

View File

@ -1,18 +0,0 @@
#!/usr/bin/lua
local site = require 'gluon.site_config'
local uci = require 'luci.model.uci'
local c = uci.cursor()
if not c:get('network', 'mesh_wan') then
c:section('network', 'interface', 'mesh_wan',
{ ifname = 'br-wan'
, proto = 'batadv'
, mesh = 'bat0'
, mesh_no_rebroadcast = '1'
, auto = site.mesh_on_wan and 1 or 0
})
end
c:save('network')

View File

@ -5,7 +5,6 @@ local sysctl = require 'gluon.sysctl'
local site = require 'gluon.site_config'
local uci = require('luci.model.uci').cursor()
local lutil = require 'luci.util'
local gw_sel_class
@ -25,15 +24,8 @@ uci:section('batman-adv', 'mesh', 'bat0',
)
uci:save('batman-adv')
if not uci:get('network', 'client', 'ifname') then
uci:add_to_set('network', 'client', 'ifname', 'bat0')
if sysconfig.lan_ifname and not site.mesh_on_lan then
for _, lanif in ipairs(lutil.split(sysconfig.lan_ifname, ' ')) do
uci:add_to_set('network', 'client', 'ifname', lanif)
end
end
end
uci:add_to_set('network', 'client', 'ifname', 'bat0')
uci:set('network', 'client', 'proto', 'dhcpv6')
uci:set('network', 'client', 'reqprefix', 'no')

View File

@ -1,13 +0,0 @@
#!/usr/bin/lua
local uci = require('luci.model.uci').cursor()
if uci:get('system', 'rssid_wlan0') then
if uci:get('wireless', 'mesh_radio0') then
uci:set('system', 'rssid_wlan0', 'dev', 'mesh0')
else
uci:set('system', 'rssid_wlan0', 'dev', 'ibss0')
end
uci:save('system')
end

View File

@ -3,7 +3,7 @@
local util = require 'gluon.util'
local uci = require('luci.model.uci').cursor()
-- fix up duplicate mac addresses (for mesh-on-WAN)
-- fix up duplicate mac addresses (for meshing)
uci:set('network', 'wan', 'macaddr', util.get_mac(2))
uci:set('network', 'mesh_lan', 'macaddr', util.get_mac(3))
uci:save('network')

View File

@ -1,5 +1,6 @@
#!/usr/bin/lua
local mesh = require 'gluon.mesh'
local site = require 'gluon.site_config'
local users = require 'gluon.users'
local util = require 'gluon.util'
@ -64,6 +65,8 @@ uci:section('fastd', 'fastd', 'mesh_vpn',
)
uci:delete('fastd', 'mesh_vpn', 'user')
mesh.register_interface('mesh-vpn', {fixed_mtu = true, transitive = true})
local add_groups
@ -124,9 +127,8 @@ uci:save('fastd')
uci:section('network', 'interface', 'mesh_vpn',
{
ifname = 'mesh-vpn',
proto = 'batadv',
mesh = 'bat0',
mesh_no_rebroadcast = 1,
proto = 'none',
auto = 1,
macaddr = util.get_mac(1),
}
)