18971c
From 17985b2575c10b488e7af2aeef160b41a97b2358 Mon Sep 17 00:00:00 2001
18971c
From: Harald Hoyer <harald@redhat.com>
18971c
Date: Mon, 14 Dec 2015 13:10:05 +0100
18971c
Subject: [PATCH] network: fix carrier detection
18971c
18971c
rename iface_has_link() to iface_has_carrier() to clarify usage
18971c
18971c
Only assign static "wildcard interface" settings, if the interface has a
18971c
carrier.
18971c
18971c
If the interface name was specified with a name, do not do carrier
18971c
checking for static configurations.
18971c
18971c
(cherry picked from commit df95b1003c8e7564da73de92403013763eb028fe)
18971c
---
18971c
 modules.d/40network/ifup.sh    | 42 ++++++++++++++++--------------
18971c
 modules.d/40network/net-lib.sh | 47 +++++++++++++++++++++-------------
18971c
 2 files changed, 52 insertions(+), 37 deletions(-)
18971c
18971c
diff --git a/modules.d/40network/ifup.sh b/modules.d/40network/ifup.sh
18971c
index 7c06cd86..30551987 100755
18971c
--- a/modules.d/40network/ifup.sh
18971c
+++ b/modules.d/40network/ifup.sh
18971c
@@ -105,7 +105,7 @@ do_dhcp() {
18971c
 
18971c
     [ -e /tmp/dhclient.$netif.pid ] && return 0
18971c
 
18971c
-    if ! iface_has_link $netif; then
18971c
+    if ! iface_has_carrier $netif; then
18971c
         warn "No carrier detected on interface $netif"
18971c
         return 1
18971c
     fi
18971c
@@ -155,7 +155,10 @@ do_ipv6auto() {
18971c
 do_static() {
18971c
     strstr $ip '*:*:*' && load_ipv6
18971c
 
18971c
-    if ! linkup $netif; then
18971c
+    if [ -z "$dev" ] && ! iface_has_carrier "$netif"; then
18971c
+        warn "No carrier detected on interface $netif"
18971c
+        return 1
18971c
+    elif ! linkup "$netif"; then
18971c
         warn "Could not bring interface $netif up!"
18971c
         return 1
18971c
     fi
18971c
@@ -398,27 +401,28 @@ for p in $(getargs ip=); do
18971c
     done
18971c
     ret=$?
18971c
 
18971c
-    > /tmp/net.${netif}.up
18971c
+    if [ $ret -eq 0 ]; then
18971c
+        > /tmp/net.${netif}.up
18971c
 
18971c
-    if [ -e /sys/class/net/${netif}/address ]; then
18971c
-        > /tmp/net.$(cat /sys/class/net/${netif}/address).up
18971c
-    fi
18971c
+        if [ -e /sys/class/net/${netif}/address ]; then
18971c
+            > /tmp/net.$(cat /sys/class/net/${netif}/address).up
18971c
+        fi
18971c
 
18971c
-    case $autoconf in
18971c
-        dhcp|on|any|dhcp6)
18971c
+        case $autoconf in
18971c
+            dhcp|on|any|dhcp6)
18971c
             ;;
18971c
-        *)
18971c
-            if [ $ret -eq 0 ]; then
18971c
-                setup_net $netif
18971c
-                source_hook initqueue/online $netif
18971c
-                if [ -z "$manualup" ]; then
18971c
-                    /sbin/netroot $netif
18971c
+            *)
18971c
+                if [ $ret -eq 0 ]; then
18971c
+                    setup_net $netif
18971c
+                    source_hook initqueue/online $netif
18971c
+                    if [ -z "$manualup" ]; then
18971c
+                        /sbin/netroot $netif
18971c
+                    fi
18971c
                 fi
18971c
-            fi
18971c
-            ;;
18971c
-    esac
18971c
-
18971c
-    exit 0
18971c
+                ;;
18971c
+        esac
18971c
+        exit $ret
18971c
+    fi
18971c
 done
18971c
 
18971c
 # netif isn't the top stack? Then we should exit here.
18971c
diff --git a/modules.d/40network/net-lib.sh b/modules.d/40network/net-lib.sh
18971c
index 995d32f6..07ea5709 100755
18971c
--- a/modules.d/40network/net-lib.sh
18971c
+++ b/modules.d/40network/net-lib.sh
18971c
@@ -531,22 +531,20 @@ wait_for_if_up() {
18971c
 
18971c
     while [ $cnt -lt $timeout ]; do
18971c
         li=$(ip -o link show up dev $1)
18971c
-        if ! strstr "$li" "NO-CARRIER"; then
18971c
-            if [ -n "$li" ]; then
18971c
-                case "$li" in
18971c
-                    *\
18971c
-                        return 0;;
18971c
-                    *\<*,UP\>*)
18971c
-                        return 0;;
18971c
-                    *\<*,UP,*\>*)
18971c
-                        return 0;;
18971c
-                esac
18971c
-            fi
18971c
-            if strstr "$li" "LOWER_UP" \
18971c
-                    && strstr "$li" "state UNKNOWN" \
18971c
-                    && ! strstr "$li" "DORMANT"; then
18971c
-                return 0
18971c
-            fi
18971c
+        if [ -n "$li" ]; then
18971c
+            case "$li" in
18971c
+                *\
18971c
+                    return 0;;
18971c
+                *\<*,UP\>*)
18971c
+                    return 0;;
18971c
+                *\<*,UP,*\>*)
18971c
+                    return 0;;
18971c
+            esac
18971c
+        fi
18971c
+        if strstr "$li" "LOWER_UP" \
18971c
+                && strstr "$li" "state UNKNOWN" \
18971c
+                && ! strstr "$li" "DORMANT"; then
18971c
+            return 0
18971c
         fi
18971c
         sleep 0.1
18971c
         cnt=$(($cnt+1))
18971c
@@ -614,7 +612,7 @@ hostname() {
18971c
 	cat /proc/sys/kernel/hostname
18971c
 }
18971c
 
18971c
-iface_has_link() {
18971c
+iface_has_carrier() {
18971c
     local cnt=0
18971c
     local interface="$1" flags=""
18971c
     [ -n "$interface" ] || return 2
18971c
@@ -625,14 +623,27 @@ iface_has_link() {
18971c
     timeout=$(($timeout*10))
18971c
 
18971c
     linkup "$1"
18971c
+
18971c
+    li=$(ip -o link show up dev $1)
18971c
+    strstr "$li" "NO-CARRIER" && _no_carrier_flag=1
18971c
+
18971c
     while [ $cnt -lt $timeout ]; do
18971c
-        [ "$(cat $interface/carrier)" = 1 ] && return 0
18971c
+        if [ -n "$_no_carrier_flag" ]; then
18971c
+            # NO-CARRIER flag was cleared
18971c
+            strstr "$li" "NO-CARRIER" || return 0
18971c
+        fi
18971c
+        # double check the syscfs carrier flag
18971c
+        [ -e "$interface/carrier" ] && [ "$(cat $interface/carrier)" = 1 ] && return 0
18971c
         sleep 0.1
18971c
         cnt=$(($cnt+1))
18971c
     done
18971c
     return 1
18971c
 }
18971c
 
18971c
+iface_has_link() {
18971c
+    iface_has_carrier "$@"
18971c
+}
18971c
+
18971c
 find_iface_with_link() {
18971c
     local iface_path="" iface=""
18971c
     for iface_path in /sys/class/net/*; do