Network: improved ip selection for interface

This commit is contained in:
bergware
2025-05-08 16:01:37 +02:00
parent cb42fcf436
commit a4bcc9e3b0
4 changed files with 87 additions and 60 deletions
+48 -36
View File
@@ -7,7 +7,7 @@
# @(#)/etc/rc.d/rc.inet1 10.2 Sun Jul 24 12:45:56 PDT 2005 (pjv)
# LimeTech - modified for Unraid OS
# Bergware - modified for Unraid OS, February 2025
# Bergware - modified for Unraid OS, May 2025
# Adapted by Bergware for use in Unraid OS - April 2016
# - improved interface configuration
@@ -81,6 +81,9 @@
# - added metric value to interface IP assignment
# - fixed DNS entries get removed when configuring interface other then eth0
# Adapted by Bergware for use in Unraid OS - May 2025
# - improved metric value to interface IP assignment
###########
# LOGGING #
###########
@@ -122,7 +125,7 @@ done
# LOOPBACK FUNCTIONS #
######################
# function to bring up loopback interface
# bring up loopback interface
lo_up(){
if [[ -e $SYSTEM/lo ]]; then
if ! ip -4 addr show lo | grep -qw 'inet'; then
@@ -137,7 +140,7 @@ lo_up(){
fi
}
# function to take down loopback interface
# take down loopback interface
lo_down(){
if [[ -e $SYSTEM/lo ]]; then
run ip link set lo down
@@ -150,12 +153,17 @@ lo_down(){
# INTERFACE FUNCTIONS #
#######################
# function to get link mtu size
# return interface index
index(){
cat $SYSTEM/$1/ifindex 2>/dev/null
}
# get link mtu size
get_mtu(){
ip link show $1 | grep -Po 'mtu \K\d+'
}
# function to set/reset link mtu size
# set/reset link mtu size
set_mtu(){
if [[ -n ${MTU[$i]} ]]; then
# set MTU to specified value
@@ -166,8 +174,8 @@ set_mtu(){
fi
}
# function to wait for carrier of interface
carrier_up(){
# wait for carrier of interface
carrier(){
local n
for n in {1..10}; do
[[ $(cat $SYSTEM/$1/carrier 2>/dev/null) == 1 ]] && return 0 || sleep 1
@@ -175,7 +183,7 @@ carrier_up(){
return 1
}
# function to create bond interface
# create bond interface
bond_up(){
[[ -d /proc/net/bonding ]] || modprobe bonding mode=${BONDING_MODE[$i]} miimon=${BONDING_MIIMON[$i]}
run ip link add name ${BONDNAME[$i]} type bond mode ${BONDING_MODE[$i]} miimon ${BONDING_MIIMON[$i]}
@@ -194,7 +202,7 @@ bond_up(){
[[ -n $PRIMARY ]] && run ip link set name ${BONDNAME[$i]} type bond primary $PRIMARY
}
# function to delete bond interface
# delete bond interface
bond_down(){
if [[ -e $SYSTEM/${BONDNAME[$i]} ]]; then
# loop thru attached interfaces in bond
@@ -208,7 +216,7 @@ bond_down(){
fi
}
# function to create bridge interface
# create bridge interface
br_up(){
for ((j=0;j<${VLANS[$i]:-1};j++)); do
[[ $j -eq 0 ]] && BRIDGE=${BRNAME[$i]} || BRIDGE=${BRNAME[$i]}.${VLANID[$i,$j]}
@@ -235,7 +243,7 @@ br_up(){
done
}
# function to delete bridge interface
# delete bridge interface
br_down(){
for ((j=0;j<${VLANS[$i]:-1};j++)); do
# loop thru main bridge and bridge VLAN interfaces
@@ -253,7 +261,7 @@ br_down(){
done
}
# function to create VLAN interfaces
# create VLAN interfaces
vlan_up(){
for PORT in ${BRNICS[$i]:-${IFNAME[$i]}}; do
for ((j=1;j<${VLANS[$i]};j++)); do
@@ -265,7 +273,7 @@ vlan_up(){
done
}
# function to delete VLAN interfaces
# delete VLAN interfaces
vlan_down(){
for PORT in ${BRNICS[$i]:-${IFNAME[$i]}}; do
for VLAN in $(ls --indicator-style=none $SYSTEM | grep -Po "$PORT\.\d+"); do
@@ -275,7 +283,7 @@ vlan_down(){
done
}
# function to create macvtap interfaces
# create macvtap interfaces
macvtap_up(){
PARENT=${IFNAME[$i]}
[[ -n ${BONDNICS[$i]} ]] && PARENT=${BONDNAME[$i]}
@@ -283,16 +291,18 @@ macvtap_up(){
MAC=$(echo $(hostname)-$VTAP | md5sum | sed -r 's/^(..)(..)(..)(..)(..).*$/02:\1:\2:\3:\4:\5/')
run ip link add link $PARENT name $VTAP address $MAC type macvtap mode bridge
set_mtu $VTAP
echo 1 >$CONF6/$VTAP/disable_ipv6
run ip link set $VTAP up
for ((j=1;j<${VLANS[$i]:-0};j++)); do
VLAN=${VLANID[$i,$j]}
run ip link add link $PARENT.$VLAN name $VTAP.$VLAN address $MAC type macvtap mode bridge
set_mtu $VTAP.$VLAN
echo 1 >$CONF6/$VTAP.$VLAN/disable_ipv6
run ip link set $VTAP.$VLAN up
done
}
# function to delete macvtap interfaces
# delete macvtap interfaces
macvtap_down(){
PARENT=${IFNAME[$i]}
[[ -n ${BONDNICS[$i]} ]] && PARENT=${BONDNAME[$i]}
@@ -308,26 +318,26 @@ macvtap_down(){
run ip link del $VTAP
}
# function to enable/disable ipv6 protocol per interface
# enable/disable ipv6 protocol per interface
ipv6_up(){
[[ -d $CONF6/${IFACE/$1/$2} ]] && echo $4 >$CONF6/${IFACE/$1/$2}/disable_ipv6
[[ -d $CONF6/${IFACE/$1/$3} ]] && echo $4 >$CONF6/${IFACE/$1/$3}/disable_ipv6
}
# function to enable/disable ipv6 assignment per interface
# enable/disable ipv6 assignment per interface
ipv6_ra(){
echo $2 >$CONF6/$1/accept_ra
echo $2 >$CONF6/$1/accept_ra_defrtr
echo $3 >$CONF6/$1/autoconf
}
# function to enable/disable ipv6 assignment per interface
# enable/disable ipv6 assignment per interface
ipv6_conf(){
[[ -d $CONF6/${IFACE/$1/$2} ]] && ipv6_ra ${IFACE/$1/$2} $4 $5
[[ -d $CONF6/${IFACE/$1/$3} ]] && ipv6_ra ${IFACE/$1/$3} $4 $5
}
# function to enable/disable ipv6 assignment per interface
# enable/disable ipv6 assignment per interface
ipv6_addr(){
[[ -d $CONF6/$IFACE ]] && ipv6_ra $IFACE $1 $2
[[ -d $CONF6/$VHOST ]] && ipv6_ra $VHOST $1 $2
@@ -342,7 +352,7 @@ ipv6_addr(){
sleep 1
}
# function to assign IP address
# assign IP address
ipaddr_up(){
if [[ -z $RENEW ]]; then
# disable IPv6 per interface when IPv4 only
@@ -371,7 +381,7 @@ ipaddr_up(){
[[ $IP == ipv4 ]] && DHCP_OPTIONS="$DHCP_OPTIONS -4"
[[ $IP == ipv6 ]] && DHCP_OPTIONS="$DHCP_OPTIONS -6"
[[ $IP != ipv4 && -n $PRIV6 && -d $CONF6/$IFACE ]] && echo $PRIV6 >$CONF6/$IFACE/use_tempaddr
if carrier_up $IFACE; then
if carrier $IFACE; then
# interface is UP
log "interface $IFACE is UP, polling up to 60 sec for DHCP $IP server"
if ! run timeout 60 dhcpcd -w $DHCP_OPTIONS $IFACE; then
@@ -385,21 +395,23 @@ ipaddr_up(){
fi
elif [[ $DHCP == no ]]; then
# bring up interface using static IP address
if carrier_up $IFACE; then STATE="UP"; else STATE="DOWN"; fi
if carrier $IFACE; then STATE="UP"; else STATE="DOWN"; fi
log "interface $IFACE is $STATE, setting static $IP address"
ipv6_addr 0 1
INDEX=$(index $IFACE)
INDEX=$((1000 + ${INDEX:-$(($(index * | sort -n | tail -1) + 1))}))
if [[ $IP != ipv6 ]]; then
[[ $j -eq 0 ]] && ADDR=${IPADDR[$i]} || ADDR=${IPADDR[$i,$j]}
if [[ -n $ADDR ]]; then
[[ $j -eq 0 ]] && MASK=${NETMASK[$i]} || MASK=${NETMASK[$i,$j]}
[[ -n $MASK ]] && run ip -4 addr add $(unzero $ADDR)/$MASK dev $IFACE metric ${DHCP_METRIC:-1}
[[ -n $MASK ]] && run ip -4 addr add $(unzero $ADDR)/$MASK metric $INDEX dev $IFACE
fi
fi
if [[ $IP != ipv4 ]]; then
[[ $j -eq 0 ]] && ADDR6=${IPADDR6[$i]} || ADDR6=${IPADDR6[$i,$j]}
if [[ -n $ADDR6 ]]; then
[[ $j -eq 0 ]] && MASK6=${NETMASK6[$i]} || MASK6=${NETMASK6[$i,$j]}
[[ -n $MASK6 ]] && run ip -6 addr add $(unzero6 $ADDR6)/$MASK6 dev $IFACE metric ${DHCP_METRIC:-1}
[[ -n $MASK6 ]] && run ip -6 addr add $(unzero6 $ADDR6)/$MASK6 metric $INDEX dev $IFACE
[[ -n $PRIV6 && -d $CONF6/$IFACE ]] && echo 0 >$CONF6/$IFACE/use_tempaddr
fi
fi
@@ -410,7 +422,7 @@ ipaddr_up(){
fi
}
# function to release IP addresses and routes
# release IP addresses and routes
ipaddr_conf(){
if [[ -e $SYSTEM/${IFACE/$1/$2} ]]; then
run ip -$4 addr flush dev ${IFACE/$1/$2}
@@ -422,7 +434,7 @@ ipaddr_conf(){
fi
}
# function to release IP addresses and routes
# release IP addresses and routes
ipaddr_flush(){
run ip -$1 addr flush dev $IFACE
run ip -$1 route flush dev $IFACE
@@ -436,7 +448,7 @@ ipaddr_flush(){
fi
}
# function to release IP addresses and routes
# release IP addresses and routes
ipaddr_down(){
if [[ $DHCP == yes ]]; then
DHCP_OPTIONS="-q -k"
@@ -453,7 +465,7 @@ ipaddr_down(){
fi
}
# function to bring up network interface
# bring up network interface
if_up(){
# set index of INTERFACE in array
i=0
@@ -555,7 +567,7 @@ if_up(){
done
}
# function to take down network interface
# take down network interface
if_down(){
# set index of INTERFACE in array
i=0
@@ -612,7 +624,7 @@ if_down(){
# GATEWAY FUNCTIONS #
#####################
# function to add default gateway per interface
# add default gateway per interface
gateway_up(){
for GW in ${GATEWAY[@]}; do
[[ -z $GW ]] && continue
@@ -640,7 +652,7 @@ gateway_up(){
done
}
# function to delete default gateway per interface
# delete default gateway per interface
gateway_down(){
for GW in ${GATEWAY[@]}; do
[[ -z $GW ]] && continue
@@ -664,7 +676,7 @@ gateway_down(){
done
}
# function to start network
# start network
start(){
lo_up
for INTERFACE in ${IFNAME[@]}; do
@@ -673,7 +685,7 @@ start(){
gateway_up
}
# function to stop network
# stop network
stop(){
gateway_down
for INTERFACE in ${IFNAME[@]}; do
@@ -682,7 +694,7 @@ stop(){
lo_down
}
# function to show network status
# show network status
status(){
echo "INTERFACE STATE INFORMATION"
echo "========================================================================"
@@ -693,7 +705,7 @@ status(){
# STATIC ROUTE FUNCTIONS #
##########################
# function to add static route
# add static route
route_up(){
[[ -n $3 ]] && METRIC="metric $3" || METRIC=
if [[ $2 == default ]]; then
@@ -711,7 +723,7 @@ route_up(){
fi
}
# function to delete static route
# delete static route
route_down(){
[[ -n $3 ]] && METRIC="metric $3" || METRIC=
if [[ $2 == default ]]; then
+4 -4
View File
@@ -5,7 +5,7 @@
# Library used by nfsd, ntpd, rpc, samba, nginx, sshd, avahidaemon, show_interfaces
#
# Bergware - created for Unraid OS, December 2023
# Bergware - updated January 2025
# Bergware - updated May 2025
WIREGUARD="/etc/wireguard"
NETWORK_INI="/var/local/emhttp/network.ini"
@@ -56,8 +56,8 @@ good(){
show(){
case $# in
1) ip -br addr show scope global -temporary -deprecated to $1 2>/dev/null | awk '{gsub("@.+","",$1);print $1;exit}' ;;
2) ip -br addr show scope global -temporary -deprecated $1 $2 2>/dev/null | awk '{$1=$2="";print;exit}' | sed -r 's/ metric [0-9]+//g' ;;
1) ip -br addr show scope global primary -deprecated to $1 2>/dev/null | awk '{gsub("@.+","",$1);print $1;exit}' ;;
2) ip -br addr show scope global primary -deprecated $1 $2 2>/dev/null | awk '{$1=$2="";print;exit}' | sed -r 's/ metric [0-9]+//g' ;;
esac
}
@@ -160,7 +160,7 @@ check(){
fi
[[ $(ipv $ADDR) == 4 ]] && IPV4=yes || IPV6=yes
done
done <<< $(ip -br addr show scope global -temporary -deprecated | awk '$1~"^(br|bond|eth|wlan|wg)[0-9]+(.[0-9]+)?" && $3!="" {gsub("@.+","",$1);$2="";print}' | sed -r 's/ metric [0-9]+//g' | sort)
done <<< $(ip -br addr show scope global primary -deprecated | awk '$1~"^(br|bond|eth|wlan|wg)[0-9]+(.[0-9]+)?" && $3!="" {gsub("@.+","",$1);$2="";print}' | sed -r 's/ metric [0-9]+//g' | sort)
# add loopback interface
if [[ "smb nfs" =~ "$CALLER" ]]; then
[[ $IPV4 == yes ]] && BIND+=(127.0.0.1)
+5 -5
View File
@@ -6,7 +6,7 @@
# Written for Slackware Linux by Cherife Li <cherife-#-dotimes.com>.
# LimeTech - modified for Unraid OS
# Bergware - modified for Unraid OS, October 2023
# Bergware - modified for Unraid OS, May 2025
# reference:
# LANNAME 'tower'
@@ -537,12 +537,12 @@ build_ssl(){
# fetch LAN IP address (read management interface eth0)
[[ -e $SYSTEM/bond0 ]] && DEV=bond0 || DEV=eth0
[[ -e $SYSTEM/br0 ]] && DEV=br0
LANIP=$(ip -4 -br addr show $DEV scope global | sed -r 's/\/[0-9]+//g' | awk '{print $3;exit}')
LANIP6=$(ip -6 -br addr show $DEV scope global -temporary -deprecated | sed -r 's/\/[0-9]+//g' | awk '{print $3;exit}')
LANIP=$(ip -4 -br addr show scope global primary dev $DEV | awk '{print $3;exit}' | sed -r 's/\/[0-9]+//')
LANIP6=$(ip -6 -br addr show scope global primary -deprecated dev $DEV | awk '{print $3;exit}' | sed -r 's/\/[0-9]+//')
# try wireless connection if no IP address on interface eth0
[[ -z $LANIP && -e $SYSTEM/wlan0 ]] && LANIP=$(ip -4 -br addr show wlan0 scope global | sed -r 's/\/[0-9]+//g' | awk '{print $3;exit}')
[[ -z $LANIP6 && -e $SYSTEM/wlan0 ]] && LANIP6=$(ip -6 -br addr show wlan0 scope global -temporary -deprecated | sed -r 's/\/[0-9]+//g' | awk '{print $3;exit}')
[[ -z $LANIP && -e $SYSTEM/wlan0 ]] && LANIP=$(ip -4 -br addr show scope global primary dev wlan0 | awk '{print $3;exit}' | sed -r 's/\/[0-9]+//')
[[ -z $LANIP6 && -e $SYSTEM/wlan0 ]] && LANIP6=$(ip -6 -br addr show scope global primary -deprecated dev wlan0 | awk '{print $3;exit}' | sed -r 's/\/[0-9]+//')
# regenerate self-signed cert if local TLD changes */
SELFCERTPATH=$SSL/certs/${LANNAME}_unraid_bundle.pem
+30 -15
View File
@@ -12,6 +12,7 @@ INI="/var/local/emhttp/wireless.ini"
CFG="/boot/config/wireless.cfg"
OPENSSL="/usr/local/emhttp/webGui/scripts/open_ssl"
STARTWIFI="/usr/local/emhttp/webGui/scripts/wireless"
SERVICES="/usr/local/emhttp/webGui/scripts/update_services"
WPA="/etc/wpa_supplicant.conf"
# system network references
@@ -28,6 +29,11 @@ CONF6="/proc/sys/net/ipv6/conf"
[[ -r $INI ]] && . $INI
PORT=${PORT:-wlan0}
# return interface index
index(){
cat $SYSTEM/$1/ifindex 2>/dev/null
}
# translate security to informational text
trans(){
case "$1" in
@@ -126,13 +132,15 @@ ipaddr_up(){
if carrier $PORT; then STATE="UP"; else STATE="DOWN"; fi
log "interface $PORT is $STATE, setting static $IP address"
ipv6_addr $PORT 0 1
INDEX=$(index $PORT)
INDEX=$((3000 + ${INDEX:-$(($(index * | sort -n | tail -1) + 1))}))
if [[ $IP == ipv4 ]]; then
[[ -n $IP4 && -n $MASK4 ]] && run ip -4 addr add $(unzero $IP4)/$MASK4 dev $PORT metric 3004
[[ -n $GATEWAY4 ]] && run ip -4 route add default via $GATEWAY4 dev $PORT metric 3004
[[ -n $IP4 && -n $MASK4 ]] && run ip -4 addr add $(unzero $IP4)/$MASK4 metric $INDEX dev $PORT
[[ -n $GATEWAY4 ]] && run ip -4 route add default via $GATEWAY4 metric $INDEX dev $PORT
fi
if [[ $IP == ipv6 ]]; then
[[ -n $IP6 && -n $MASK6 ]] && run ip -6 addr add $(unzero6 $IP6)/$MASK6 dev $PORT metric 3004
[[ -n $GATEWAY6 ]] && run ip -6 route add default via $GATEWAY6 dev $PORT metric 3004
[[ -n $IP6 && -n $MASK6 ]] && run ip -6 addr add $(unzero6 $IP6)/$MASK6 metric $INDEX dev $PORT
[[ -n $GATEWAY6 ]] && run ip -6 route add default via $GATEWAY6 metric $INDEX dev $PORT
fi
fi
if [[ $DNS == yes ]]; then
@@ -148,17 +156,13 @@ ipaddr_up(){
ipaddr_down(){
if [[ $DHCP == yes ]]; then
# release DHCP assigned address and default route
OPTIONS="-q -k"
OPTIONS="-q -k -$1"
[[ $DNS == yes ]] && OPTIONS="$OPTIONS -C resolv.conf"
[[ $IP == ipv4 ]] && OPTIONS="$OPTIONS -4"
[[ $IP == ipv6 ]] && OPTIONS="$OPTIONS -6"
run dhcpcd $OPTIONS $PORT
elif [[ $DHCP == no ]]; then
# release static assigned address and default route
[[ $IP == ipv4 ]] && run ip -4 addr flush dev $PORT
[[ $IP == ipv4 ]] && run ip -4 route flush default dev $PORT
[[ $IP == ipv6 ]] && run ip -6 addr flush dev $PORT
[[ $IP == ipv6 ]] && run ip -6 route flush default dev $PORT
run ip -$1 addr flush dev $PORT
run ip -$1 route flush default dev $PORT
fi
}
@@ -253,19 +257,25 @@ wifi_stop(){
log "$DAEMON... No Wifi present."
return
fi
IP=ipv4
IPV4=$(ip -4 -br addr show scope global primary dev $PORT | awk '{print $3;exit}')
INDEX=$((3000 - 1 + $(index $PORT)))
DHCP=$DHCP4
DNS=$DNS4
ipaddr_down
SRV4=$DNS
SRV6=
ipaddr_down 4
if [[ -n $DHCP6 ]]; then
IP=ipv6
DHCP=$DHCP6
DNS=$DNS6
ipaddr_down
SRV6=$DNS
ipaddr_down 6
fi
run ip addr del $IPV4 metric $INDEX dev $PORT
run pkill wpa_supplicant
run iw dev $PORT disconnect
run rm -f $INI
# restart services when static assignments
[[ $SRV4 == no && (-z $SRV6 || $SRV6 == no) ]] && $SERVICE 5
if ! wifi_running; then REPLY="Stopped"; else REPLY="Failed"; fi
log "$DAEMON... $REPLY."
}
@@ -309,6 +319,8 @@ wifi_join(){
IP=ipv4
DHCP=$DHCP4
DNS=$DNS4
SRV4=$DNS
SRV6=
ipaddr_up
# IPv6 address assignment (if enabled)
if [[ -n $DHCP6 ]]; then
@@ -316,10 +328,13 @@ wifi_join(){
IP=ipv6
DHCP=$DHCP6
DNS=$DNS6
SRV6=$DNS
ipaddr_up
else
echo 1 >$CONF6/$PORT/disable_ipv6
fi
# restart services when static assignments
[[ $SRV4 == no && (-z $SRV6 || $SRV6 == no) ]] && $SERVICE 5
if wifi_running; then
if [[ -z $CC ]]; then
CC=($(iw reg get | grep -Po '^country \K..'))