netmode: update macvlan handling

This commit is contained in:
Mohd Husaam Mehdi
2025-12-17 20:59:22 +05:30
committed by Vivek Dutta
parent b352b83155
commit bfd65787d2

View File

@@ -204,6 +204,79 @@ validate_interface_type() {
return 0
}
disable_macoffset_for_iface() {
local iface="$1"
local file="/rom/etc/macoffset"
[ -z "$iface" ] && {
logger -t "netmode-advanced" "No interface name provided"
return 1
}
[ ! -f "$file" ] && {
logger -t "netmode-advanced" "macoffset file not found: $file"
return 1
}
logger -t "netmode-advanced" "Disabling macoffset for interface: $iface"
# Create a temp file
local tmp="$(mktemp)"
awk -v iface="$iface" '
BEGIN {
in_config = 0
}
# Force autofill to false
/"autofill"[[:space:]]*:/ {
sub(/true/, "false")
print
next
}
# Detect start of config array
/"config"[[:space:]]*:[[:space:]]*\[/ {
in_config = 1
print
next
}
# Detect end of config array
in_config && /\]/ {
in_config = 0
print
next
}
# Inside config array: filter out matching interfaces
in_config {
# Strip quotes and commas for matching
line = $0
gsub(/[",]/, "", line)
# Drop entries containing network.<iface>
if (line ~ ("network\\." iface "( |$)")) {
next
}
print
next
}
# Default: print unchanged
{
print
}
' "$file" > "$tmp"
# Replace original file
cp "$tmp" "$file"
rm -f "$tmp"
logger -t "netmode-advanced" "macoffset updated: interface '$iface' removed and autofill disabled"
}
# Get base MAC address from environment
get_base_mac() {
local base_mac=$(fw_printenv -n ethaddr 2>/dev/null)
@@ -700,9 +773,7 @@ create_macvlan_device() {
local base_if="$1"
local mac_addr="$2"
local if_name="$3"
# Resolve MAC address macro if needed
local resolved_mac=$(resolve_mac_address "$mac_addr")
local resolved_mac="$4"
local macvlan_name="${if_name}_macvlan"
local sec_name=$(echo "$macvlan_name" | tr '.-' '__')
@@ -909,6 +980,7 @@ create_routed_interface() {
local disabled="$7"
local purpose="$8"
local defaultroute="0"
local resolved_mac=""
if [ "$purpose" = "inet" ]; then
defaultroute="1"
@@ -929,7 +1001,9 @@ create_routed_interface() {
;;
macvlan)
# Create MACVLAN device
device_name=$(create_macvlan_device "$base_device" "$mac_addr" "$if_name")
# Resolve MAC address macro if needed
resolved_mac=$(resolve_mac_address "$mac_addr")
device_name=$(create_macvlan_device "$base_device" "$resolved_mac" "$if_name")
;;
none)
# Direct on base device
@@ -948,6 +1022,14 @@ create_routed_interface() {
uci -q set "network.${if_name}.device=${device_name}"
uci -q set "network.${if_name}.defaultroute=${defaultroute}"
if [ "$vlan_type" = "macvlan" ]; then
# so that macoffset does not overwrite this mac address
disable_macoffset_for_iface "${if_name}"
if [ -n "$resolved_mac" ]; then
uci -q set "network.${if_name}.macaddr=${resolved_mac}"
fi
fi
# Add MAC address if specified for VLAN interface
if [ "$vlan_type" = "vlan" -a -n "$mac_addr" ]; then
uci -q set "network.${if_name}.macaddr=${mac_addr}"