Skip to content

Commit 7929208

Browse files
authored
Merge pull request #1236 from ilario/lime-proto-wan-vlan3
lime-proto-wan add support for VLAN on WAN
2 parents 0ee2fdd + 188995c commit 7929208

File tree

3 files changed

+60
-29
lines changed

3 files changed

+60
-29
lines changed

packages/lime-docs/files/www/docs/lime-example.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,12 @@ config net wirelessclientbackbone
307307

308308
## configure the ethernet port eth1.5 as WAN with static ipv4 and ipv6 addresses
309309

310-
config net port5 # Do not put any "." in the section name
310+
config net lm_hwd_openwrt_wan # Do not put any "." in the section name. Specifying lm_hwd_openwrt_wan will overwrite the configuration automatically generated by lime-hwd-openwrt-wan
311+
option autogenerated 'false' # This tells lime-hwd-openwrt-wan to not destroy and rewrite this section
311312
option linux_name 'eth1.5' # Put here the actual name of the interface
312313
# list protocols 'wan' # Use 'wan' instead of 'static' to get Internet connectivity via DHCP
314+
# If a VLAN is needed on the WAN interface for connecting to the gateway, it can be specified with wan:ID, like "wan:20" or "wan:20:8021q"
315+
313316
list protocols 'static' # Set up a static IP (both IPv4 and IPv6 supported)
314317
option static_ipv4 '192.168.1.2/24'
315318
option static_gateway_ipv4 '192.168.1.1' # In static protocol, specifying an IP for the gateway is optional. Skip this line if no default route should be added on this interface.

packages/lime-proto-wan/files/usr/lib/lua/lime/proto/wan.lua

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
--! SPDX-License-Identifier: AGPL-3.0-only
99

1010
local libuci = require("uci")
11+
local network = require("lime.network")
12+
local utils = require("lime.utils")
1113

1214
wan = {}
1315

@@ -25,7 +27,27 @@ end
2527

2628
function wan.setup_interface(ifname, args)
2729
local uci = libuci:cursor()
28-
uci:set("network", "wan", "device", ifname)
30+
local vlanId = tostring(args[2] or "0")
31+
32+
if vlanId ~= "0" then
33+
local vlanProto = args[3] or "8021q"
34+
local nameSuffix = args[4] or "_wan"
35+
36+
local owrtDeviceName = network.sanitizeIfaceName(ifname.."_dev")
37+
38+
--! Do not use . as separator as this will make netifd create an 802.1q interface anyway
39+
--! and sanitize ifname because it can contain dots as well (i.e. switch ports)
40+
local linuxName = ifname:gsub("[^%w-]", "-")..network.protoVlanSeparator..vlanId
41+
42+
network.createDevice(owrtDeviceName, ifname, linuxName, vlanProto, { vid=vlanId })
43+
44+
utils.log("lime.proto.wan.setup_interface(%s with VLAN ID %s, ...)", ifname, vlanId)
45+
uci:set("network", "wan", "device", linuxName)
46+
else
47+
utils.log("lime.proto.wan.setup_interface(%s, ...)", ifname)
48+
uci:set("network", "wan", "device", ifname)
49+
end
50+
2951
uci:save("network")
3052

3153
--! Accepting link local traffic also on WAN should not cause hazards.

packages/lime-system/files/usr/lib/lua/lime/network.lua

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,28 @@ function network.sanitizeIfaceName(ifName)
405405
return network.limeIfNamePrefix..ifName:gsub("[^%w_]", "_")
406406
end
407407

408+
function network.createDevice(owrtDeviceName, baseIfname, linuxName, devType, args)
409+
--! baseIfname can be a linux interface name like eth0 or an openwrt
410+
--! interface name like @lan of the base interface;
411+
--! linuxName is the linux name of the new interface;
412+
--! args optional additional arguments for device like
413+
--! { macaddr="aa:aa:aa:aa:aa:aa", mode="vepa" };
414+
415+
args = args or {}
416+
417+
local uci = config.get_uci_cursor()
418+
419+
uci:set("network", owrtDeviceName, "device")
420+
uci:set("network", owrtDeviceName, "type", devType)
421+
uci:set("network", owrtDeviceName, "name", linuxName)
422+
uci:set("network", owrtDeviceName, "ifname", baseIfname)
423+
for k,v in pairs(args) do
424+
uci:set("network", owrtDeviceName, k, v)
425+
end
426+
427+
uci:save("network")
428+
end
429+
408430
--! Creates a network Interface with static protocol
409431
--! ipAddr can be IPv4 or IPv6
410432
--! the function can be called twice to set both IPv4 and IPv6
@@ -449,7 +471,7 @@ function network.createVlanIface(linuxBaseIfname, vid, openwrtNameSuffix, vlanPr
449471
--! because only alphanumeric and underscores are allowed
450472
local owrtInterfaceName = network.sanitizeIfaceName(linuxBaseIfname)
451473
local owrtDeviceName = owrtInterfaceName
452-
local linux802adIfName = linuxBaseIfname
474+
local linuxVlanIfName = linuxBaseIfname
453475

454476
local uci = config.get_uci_cursor()
455477

@@ -467,14 +489,9 @@ function network.createVlanIface(linuxBaseIfname, vid, openwrtNameSuffix, vlanPr
467489

468490
--! Do not use . as separator as this will make netifd create an 802.1q interface anyway
469491
--! and sanitize linuxBaseIfName because it can contain dots as well (i.e. switch ports)
470-
linux802adIfName = linux802adIfName:gsub("[^%w-]", "-")..network.protoVlanSeparator..vlanId
471-
472-
uci:set("network", owrtDeviceName, "device")
473-
uci:set("network", owrtDeviceName, "type", vlanProtocol)
474-
uci:set("network", owrtDeviceName, "name", linux802adIfName)
475-
--! This is ifname also on current OpenWrt
476-
uci:set("network", owrtDeviceName, "ifname", linuxBaseIfname)
477-
uci:set("network", owrtDeviceName, "vid", vlanId)
492+
linuxVlanIfName = linuxVlanIfName:gsub("[^%w-]", "-")..network.protoVlanSeparator..vlanId
493+
494+
network.createDevice(owrtDeviceName, linuxBaseIfname, linuxVlanIfName, vlanProtocol, { vid=vlanId })
478495
end
479496

480497
uci:set("network", owrtInterfaceName, "interface")
@@ -488,13 +505,13 @@ function network.createVlanIface(linuxBaseIfname, vid, openwrtNameSuffix, vlanPr
488505
--! In case of wifi interface not using vlan (vid == 0) avoid to set
489506
--! ifname in network because it is already set in wireless, because
490507
--! setting ifname on both places cause a netifd race condition
491-
if vid ~= 0 or not linux802adIfName:match("^wlan") then
492-
uci:set("network", owrtInterfaceName, "device", linux802adIfName)
508+
if vid ~= 0 or not linuxVlanIfName:match("^wlan") then
509+
uci:set("network", owrtInterfaceName, "device", linuxVlanIfName)
493510
end
494511

495512
uci:save("network")
496513

497-
return owrtInterfaceName, linux802adIfName, owrtDeviceName
514+
return owrtInterfaceName, linuxVlanIfName, owrtDeviceName
498515
end
499516

500517
function network.createMacvlanIface(baseIfname, linuxName, argsDev, argsIf)
@@ -511,25 +528,14 @@ function network.createMacvlanIface(baseIfname, linuxName, argsDev, argsIf)
511528
--! lime.proto which want to use macvlan so this function should depend
512529
--! on its own on kmod-macvlan as needed.
513530

514-
argsDev = argsDev or {}
515531
argsIf = argsIf or {}
516532

517-
local owrtDeviceName = network.limeIfNamePrefix..baseIfname.."_"..linuxName.."_dev"
518-
local owrtInterfaceName = network.limeIfNamePrefix..baseIfname.."_"..linuxName.."_if"
519-
--! sanitize uci sections name
520-
owrtDeviceName = owrtDeviceName:gsub("[^%w_]", "_")
521-
owrtInterfaceName = owrtInterfaceName:gsub("[^%w_]", "_")
533+
local owrtDeviceName = network.sanitizeIfaceName(baseIfname.."_"..linuxName.."_dev")
534+
local owrtInterfaceName = network.sanitizeIfaceName(baseIfname.."_"..linuxName.."_if")
522535

523-
local uci = config.get_uci_cursor()
536+
network.createDevice(owrtDeviceName, baseIfname, linuxName, "macvlan", argsDev)
524537

525-
uci:set("network", owrtDeviceName, "device")
526-
uci:set("network", owrtDeviceName, "type", "macvlan")
527-
uci:set("network", owrtDeviceName, "name", linuxName)
528-
--! This is ifname also on current OpenWrt
529-
uci:set("network", owrtDeviceName, "ifname", baseIfname)
530-
for k,v in pairs(argsDev) do
531-
uci:set("network", owrtDeviceName, k, v)
532-
end
538+
local uci = config.get_uci_cursor()
533539

534540
uci:set("network", owrtInterfaceName, "interface")
535541
uci:set("network", owrtInterfaceName, "proto", "none")

0 commit comments

Comments
 (0)