Skip to content

Commit

Permalink
Merge pull request #2372 from freifunk-gluon/uci-regen
Browse files Browse the repository at this point in the history
Regenerate network and system UCI configs on every reconfigure, switch to role-based interface configuration
  • Loading branch information
blocktrron authored Mar 20, 2022
2 parents f75bb7c + 210cacd commit a3203b2
Show file tree
Hide file tree
Showing 33 changed files with 353 additions and 201 deletions.
15 changes: 8 additions & 7 deletions docs/dev/wan.rst → docs/dev/uplink.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
WAN support
===========
Uplink support
==============

As the WAN port of a node will be connected to a user's private network, it
is essential that the node only uses the WAN when it is absolutely necessary.
Expand All @@ -11,11 +11,12 @@ There are two cases in which the WAN port is used:
After the VPN connection has been established, the node should be able to reach
the mesh's DNS servers and use these for all other name resolution.

If the device does not feature a WAN port, the LAN port is configured as WAN port.
In case such a device has multiple LAN ports, all these can be used as WAN.
Devices, which feature a "hybrid" port (labelled as WAN/LAN), this port is used as WAN.

This behavior can be reversed using the ``single_as_lan`` site.conf option.
If a device has only a single Ethernet port (or group of ports), it will be
used as an uplink port even when it is not labelled as "WAN" by default. This
behavior can be controlled using the ``interfaces.single.default_roles``
site.conf option. It is also possible to alter the interface assignment after
installation by modifying ``/etc/config/gluon`` and running
``gluon-reconfigure``.

Routing tables
~~~~~~~~~~~~~~
Expand Down
4 changes: 1 addition & 3 deletions docs/features/multidomain.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ site.conf only variables
- authorized_keys
- default_domain
- poe_passthrough
- mesh_on_wan
- mesh_on_lan
- single_as_lan
- interfaces.*.default_roles
- setup_mode.skip
- autoupdater.branch
- mesh_vpn.enabled
Expand Down
4 changes: 2 additions & 2 deletions docs/features/private-wlan.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Private WLAN
============

It is possible to set up a private WLAN that bridges the WAN port and is separated from the mesh network.
Please note that you should not enable ``mesh_on_wan`` simultaneously.
It is possible to set up a private WLAN that bridges the uplink port and is separated from the mesh network.
Please note that you should not enable Wired Mesh on the uplink port at the same time.

The private WLAN is encrypted using WPA2 by default. On devices with enough flash and a supported radio,
WPA3 or WPA2/WPA3 mixed-mode can be used instead of WPA2. For this to work, the ``wireless-encryption-wpa3``
Expand Down
4 changes: 2 additions & 2 deletions docs/features/wired-mesh.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ Configuration
Both Mesh-on-WAN and Mesh-on-LAN can be configured on the "Network" page
of the *Advanced settings* (if the package ``gluon-web-network`` is installed).

It is also possible to enable Mesh-on-WAN and Mesh-on-LAN by default by
adding ``mesh_on_wan = true`` and ``mesh_on_lan = true`` to ``site.conf``.
It is also possible to enable Mesh-on-WAN and Mesh-on-LAN by default by adding
the ``mesh`` role to the ``interfaces.*.default_roles`` options in site.conf.

Commandline
===========
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Several Freifunk communities in Germany use Gluon as the foundation of their Fre
dev/hardware
dev/packages
dev/upgrade
dev/wan
dev/uplink
dev/mac_addresses
dev/site_library
dev/build
Expand Down
46 changes: 39 additions & 7 deletions docs/user/site.rst
Original file line number Diff line number Diff line change
Expand Up @@ -399,17 +399,49 @@ mesh_vpn
},
}

mesh_on_wan \: optional
Enables the mesh on the WAN port (``true`` or ``false``).
interfaces \: optional
Default setup for Ethernet ports.
::

mesh_on_wan = true,
interfaces = {
lan = {
default_roles = { 'client', 'mesh' },
},
wan = {
default_roles = { 'uplink', 'mesh' },
},
single = {
default_roles = { 'uplink', 'mesh' },
},
},

mesh_on_lan \: optional
Enables the mesh on the LAN port (``true`` or ``false``).
::
For devices that have two distinct Ethernet ports or port groups (often
labelled WAN and LAN), the ``lan`` and ``wan`` sections are used. When there
is only one port (group), ``single`` is used instead.

Available interface roles:

- ``client``: Port allows regular clients to connect to the mesh
- ``uplink``: Port is used to establish Mesh VPN connections
- ``mesh``: Wired meshing to another Gluon or Gluon-compatible node

The ``client`` role requires exclusive control over an interface. When
the ``client`` role is assigned to an interface at the same time as other
roles (like ``'client', 'mesh'`` in the above example), the other roles take
precedence (enabling ``mesh``, but not ``client`` in the example).

Such a default configuration still fulfills a purpose (and is in fact the
recommended way to enable "Mesh-on-LAN" by default): The "LAN interface
meshing" checkbox in the advanced network settings will only add or remove
the ``mesh`` role, so the ``client`` role must already be in the configuration
to make the LAN port a regular client interface when the checkbox is disabled.

All interface settings are optional. If unset, the following defaults are
used:

mesh_on_lan = true,
- ``lan``: ``{ 'client' }``
- ``wan``: ``{ 'uplink' }``
- ``single``: Same as ``wan``

poe_passthrough \: optional
Enable PoE passthrough by default on hardware with such a feature.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,9 @@ local util = require 'gluon.util'
local uci = require('simple-uci').cursor()


local interfaces = uci:get('network', 'client', 'ifname') or {}

if type(interfaces) == 'string' then
local ifname = interfaces
interfaces = {}
for iface in ifname:gmatch('%S+') do
util.add_to_set(interfaces, iface)
end
end

if sysconfig.lan_ifname and uci:get_bool('network', 'mesh_lan', 'disabled') then
for lanif in sysconfig.lan_ifname:gmatch('%S+') do
util.add_to_set(interfaces, lanif)
end
end

local interfaces = util.get_role_interfaces(uci, 'client', true)
util.add_to_set(interfaces, 'local-port')


uci:delete('network', 'client')
uci:section('network', 'interface', 'client', {
type = 'bridge',
ifname = interfaces,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ local uci = require('simple-uci').cursor()
local next_node = site.next_node({})


uci:delete('network', 'local_node_dev')
uci:section('network', 'device', 'local_node_dev', {
type = 'veth',
name = 'local-node',
Expand All @@ -31,7 +30,6 @@ if next_node.ip6 then
ip6 = next_node.ip6 .. '/128'
end

uci:delete('network', 'local_node')
uci:section('network', 'interface', 'local_node', {
ifname = 'local-node',
proto = 'static',
Expand Down
11 changes: 8 additions & 3 deletions package/gluon-core/check_site.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ need_string_match(in_domain({'next_node', 'ip4'}), '^%d+.%d+.%d+.%d+$', false)

need_boolean(in_domain({'mesh', 'vxlan'}), false)

need_boolean(in_site({'mesh_on_wan'}), false)
need_boolean(in_site({'mesh_on_lan'}), false)
need_boolean(in_site({'single_as_lan'}), false)
local interfaces_roles = {'client', 'uplink', 'mesh'}
for _, config in ipairs({'wan', 'lan', 'single'}) do
need_array_of(in_site({'interfaces', config, 'default_roles'}), interfaces_roles, false)
end

obsolete({'mesh_on_wan'}, 'Use interfaces.wan.default_roles.')
obsolete({'mesh_on_lan'}, 'Use interfaces.lan.default_roles.')
obsolete({'single_as_lan'}, 'Use interfaces.single.default_roles.')
23 changes: 23 additions & 0 deletions package/gluon-core/files/lib/gluon/upgrade/001-reset-uci
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh

NETWORK_CFG='/etc/config/network'
NETWORK_SAVED="${NETWORK_CFG}_gluon-old"

SYSTEM_CFG='/etc/config/system'
SYSTEM_SAVED="${SYSTEM_CFG}_gluon-old"

# Make sure everything is saved before we move away the config files
uci commit

# Save old configs (unless there is already a saved config,
# which means that the previous upgrade was interrupted)
if [ -s "$NETWORK_CFG" ] && ! [ -s "$NETWORK_SAVED" ]; then
mv -f "$NETWORK_CFG" "$NETWORK_SAVED"
fi
if [ -s "$SYSTEM_CFG" ] && ! [ -s "$SYSTEM_SAVED" ]; then
mv -f "$SYSTEM_CFG" "$SYSTEM_SAVED"
fi

# Generate a new network config
rm -f /etc/board.json "$NETWORK_CFG" "$SYSTEM_CFG"
config_generate
3 changes: 3 additions & 0 deletions package/gluon-core/files/lib/gluon/upgrade/998-commit
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ uci -q batch <<-EOF
delete gluon.core.reconfigure
commit
EOF

# New config is saved, we can delete the old one
rm -f /etc/config/*_gluon-old
31 changes: 31 additions & 0 deletions package/gluon-core/luasrc/lib/gluon/upgrade/002-migrate-system
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/lua

local uci = require('simple-uci').cursor()

-- Migrate system section
local system = uci:get_all('system_gluon-old', '@system[0]')
if system then
uci:tset('system', '@system[0]', system)
end

-- Migrate ntp section
local ntp = uci:get_all('system_gluon-old', 'ntp')
if ntp then
uci:tset('system', 'ntp', ntp)
end

-- Migrate gpio_switch sections
--
-- Only the value is copied from the old config, so updates to names and
-- pins are preserved
uci:foreach('system', 'gpio_switch', function(s)
local name = s['.name']
local value = uci:get('system_gluon-old', name, 'value')
if value then
uci:set('system', name, 'value', value)
end
end)

-- No other sections are migrated, so updated LED and RSSI configs can take effect

uci:save('system')
24 changes: 6 additions & 18 deletions package/gluon-core/luasrc/lib/gluon/upgrade/020-interfaces
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
#!/usr/bin/lua

local sysconfig = require 'gluon.sysconfig'

-- Are we already set up?
if sysconfig.lan_ifname or sysconfig.wan_ifname then
os.exit(0)
end


local platform = require 'gluon.platform'
local site = require 'gluon.site'
local sysconfig = require 'gluon.sysconfig'

local json = require 'jsonc'
local uci = require('simple-uci').cursor()
Expand Down Expand Up @@ -69,19 +61,15 @@ end
if wan_ifname and lan_ifname then
sysconfig.wan_ifname = wan_ifname
sysconfig.lan_ifname = lan_ifname
sysconfig.single_ifname = nil
else
local single_ifname = lan_ifname or wan_ifname
if site.single_as_lan(false) then
sysconfig.lan_ifname = single_ifname
else
sysconfig.wan_ifname = single_ifname
end
sysconfig.wan_ifname = nil
sysconfig.lan_ifname = nil
sysconfig.single_ifname = lan_ifname or wan_ifname
end


uci:delete('network', 'lan')
uci:delete('network', 'wan')

uci:delete_all('network', 'device')
uci:delete_all('network', 'interface')

uci:save('network')
66 changes: 66 additions & 0 deletions package/gluon-core/luasrc/lib/gluon/upgrade/021-interface-roles
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/lua

local site = require 'gluon.site'
local sysconfig = require 'gluon.sysconfig'
local uci = require('simple-uci').cursor()
local util = require 'gluon.util'

-- Defaults from site.conf
local roles = {
lan = site.interfaces.lan.roles({'client'}),
wan = site.interfaces.wan.roles({'uplink'}),
}
roles.single = site.interfaces.single.roles(roles.wan)

-- Migration of Mesh-on-WAN/LAN setting from Gluon 2021.1 and older (to be removed in 2024)
--
-- Wired meshing is enabled for single interfaces if either of the settings
-- was previously enabled
local mesh_lan_disabled = uci:get('network_gluon-old', 'mesh_lan', 'disabled')
local mesh_wan_disabled = uci:get('network_gluon-old', 'mesh_wan', 'disabled')
if mesh_wan_disabled == '0' then
util.add_to_set(roles.wan, 'mesh')
util.add_to_set(roles.single, 'mesh')
elseif mesh_wan_disabled == '1' then
util.remove_from_set(roles.wan, 'mesh')
util.remove_from_set(roles.single, 'mesh')
end
if mesh_lan_disabled == '0' then
util.add_to_set(roles.lan, 'mesh')
util.add_to_set(roles.single, 'mesh')
elseif mesh_lan_disabled == '1' then
util.remove_from_set(roles.lan, 'mesh')
util.remove_from_set(roles.single, 'mesh')
end

-- Migration of single to WAN/LAN or vice-versa (an interface was added or removed)
-- We identify the WAN with the single interface in this case
--
-- These settings only take effect when the section that is the target of the
-- migration does not exist yet.
if uci:get('gluon', 'iface_wan') then
roles.single = uci:get_list('gluon', 'iface_wan', 'role')
end
if uci:get('gluon', 'iface_single') then
roles.wan = uci:get_list('gluon', 'iface_single', 'role')
end

-- Non-existing interfaces are nil, so they will not be added to the table
local interfaces = {
lan = sysconfig.lan_ifname,
wan = sysconfig.wan_ifname,
single = sysconfig.single_ifname,
}

for iface in pairs(interfaces) do
local section_name = 'iface_' .. iface
if not uci:get('gluon', section_name) then
uci:section('gluon', 'interface', section_name, {
-- / prefix refers to sysconfig ifnames
name = '/' .. iface,
role = roles[iface],
})
end
end

uci:save('gluon')
Loading

0 comments on commit a3203b2

Please sign in to comment.