Submitted By: Ken Moffat <ken at linuxfromscratch dot org>
Date: 2011-11-23
Initial Package Version: 4.2.2
Upstream Status: ignored
Origin: Peter Marschall, modified by Ken Moffat
Description: Fixes dhclient-script to work with iproute2

originally from Peter Marschall,
https://lists.isc.org/pipermail/dhcp-users/2011-January/012539.html

 Extended to handle other 'route' invocations in 4.2.2, and output to
/dev/null removed.  This replaces the earlier dhcp patch by Jim Gifford,
Bruce Dubbs, and DJ Lucas which no longer applies.  Reinstated one
output to /dev/null because the script tries to add an interface during
reboot.

--- dhcp-4.2.2/client/scripts/linux.orig	2011-05-18 21:01:54.000000000 +0100
+++ dhcp-4.2.2/client/scripts/linux	2011-09-06 01:04:13.000000000 +0100
@@ -3,6 +3,8 @@
 # Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
 # No guarantees about this. I'm a novice at the details of Linux
 # networking.
+# Conversion to use ip for ipv4 (instead of ifconfig, route) by Peter Marschall
+# Extended for 4.2.2, and ip output to /dev/null removed, by Ken Moffat
 
 # Notes:
 
@@ -98,17 +100,11 @@
 if [ x$old_broadcast_address != x ]; then
   old_broadcast_arg="broadcast $old_broadcast_address"
 fi
-if [ x$new_subnet_mask != x ]; then
-  new_subnet_arg="netmask $new_subnet_mask"
+if [ -n "$new_subnet_mask" ]; then
+    new_mask="/$new_subnet_mask"
 fi
-if [ x$old_subnet_mask != x ]; then
-  old_subnet_arg="netmask $old_subnet_mask"
-fi
-if [ x$alias_subnet_mask != x ]; then
-  alias_subnet_arg="netmask $alias_subnet_mask"
-fi
-if [ x$new_interface_mtu != x ]; then
-  mtu_arg="mtu $new_interface_mtu"
+if [ -n "$alias_subnet_mask" ]; then
+    alias_mask="/$alias_subnet_mask"
 fi
 if [ x$IF_METRIC != x ]; then
   metric_arg="metric $IF_METRIC"
@@ -122,9 +118,9 @@
 if [ x$reason = xPREINIT ]; then
   if [ x$alias_ip_address != x ]; then
     # Bring down alias interface. Its routes will disappear too.
-    ifconfig $interface:0- inet 0
+    ${ip} -4 addr flush dev ${interface} label ${interface}:0
   fi
-  ifconfig $interface 0 up
+  ${ip} link set dev ${interface} up
 
   # We need to give the kernel some time to get the interface up.
   sleep 1
@@ -151,25 +147,32 @@
   if [ x$old_ip_address != x ] && [ x$alias_ip_address != x ] && \
 		[ x$alias_ip_address != x$old_ip_address ]; then
     # Possible new alias. Remove old alias.
-    ifconfig $interface:0- inet 0
+    ${ip} -4 addr flush dev ${interface} label ${interface}:0
   fi
   if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]; then
     # IP address changed. Bringing down the interface will delete all routes,
     # and clear the ARP cache.
-    ifconfig $interface inet 0 down
+    ${ip} -4 addr flush dev ${interface} label ${interface}
+  fi
+
 
   fi
   if [ x$old_ip_address = x ] || [ x$old_ip_address != x$new_ip_address ] || \
      [ x$reason = xBOUND ] || [ x$reason = xREBOOT ]; then
 
-    ifconfig $interface inet $new_ip_address $new_subnet_arg \
-					$new_broadcast_arg $mtu_arg
+    ${ip} -4 addr add ${new_ip_address}${new_mask} ${new_broadcast_arg} \
+                dev ${interface} label ${interface} 2>/dev/null
+    if [ -n "$new_interface_mtu" ]; then
+      # set MTU
+      ${ip} link set dev ${interface} mtu ${new_interface_mtu}
+    fi
     # Add a network route to the computed network address.
     for router in $new_routers; do
       if [ "x$new_subnet_mask" = "x255.255.255.255" ] ; then
-	route add -host $router dev $interface
+         ${ip} -4 route add ${router} dev $interface
       fi
-      route add default gw $router $metric_arg dev $interface
+      ${ip} -4 route add default via ${router} dev ${interface} \
+                    ${metric_arg}
     done
   else
     # we haven't changed the address, have we changed other options           
@@ -177,21 +180,24 @@
     if [ x$new_routers != x ] && [ x$new_routers != x$old_routers ] ; then
       # if we've changed routers delete the old and add the new.
       for router in $old_routers; do
-        route del default gw $router
+	${ip} -4 route del via $router
       done
       for router in $new_routers; do
         if [ "x$new_subnet_mask" = "x255.255.255.255" ] ; then
-	  route add -host $router dev $interface
+	  ${ip} -4 route add $router dev $interface
 	fi
 	route add default gw $router $metric_arg dev $interface
+	${ip} -4 route add default via $router dev $interface \
+                      $metric_arg
       done
     fi
   fi
   if [ x$new_ip_address != x$alias_ip_address ] && [ x$alias_ip_address != x ];
    then
-    ifconfig $interface:0- inet 0
-    ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
-    route add -host $alias_ip_address $interface:0
+    ${ip} -4 addr flush dev ${interface} label ${interface}:0
+    ${ip} -4 addr add ${alias_ip_address}${alias_mask} \
+        dev ${interface} label ${interface}:0
+    ${ip} -4 route add ${alias_ip_address} dev ${interface}
   fi
   make_resolv_conf
   exit_with_hooks 0
@@ -201,42 +207,49 @@
    || [ x$reason = xSTOP ]; then
   if [ x$alias_ip_address != x ]; then
     # Turn off alias interface.
-    ifconfig $interface:0- inet 0
+    ${ip} -4 addr flush dev ${interface} label ${interface}:0
   fi
   if [ x$old_ip_address != x ]; then
     # Shut down interface, which will delete routes and clear arp cache.
-    ifconfig $interface inet 0 down
+    ${ip} -4 addr flush dev ${interface} label ${interface}
   fi
   if [ x$alias_ip_address != x ]; then
-    ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
-    route add -host $alias_ip_address $interface:0
+    ${ip} -4 addr add ${alias_ip_address}${alias_network_arg} \
+        dev ${interface} label ${interface}:0
+    ${ip} -4 route add ${alias_ip_address} dev ${interface}
   fi
   exit_with_hooks 0
 fi
 
 if [ x$reason = xTIMEOUT ]; then
   if [ x$alias_ip_address != x ]; then
-    ifconfig $interface:0- inet 0
+    ${ip} -4 addr flush dev ${interface} label ${interface}:0
+  fi
+  ${ip} -4 addr add ${new_ip_address}${new_mask} ${new_broadcast_arg} \
+            dev ${interface} label ${interface}
+  if [ -n "$new_interface_mtu" ]; then
+    # set MTU
+    ip link set dev ${interface} mtu ${new_interface_mtu}
   fi
-  ifconfig $interface inet $new_ip_address $new_subnet_arg \
-					$new_broadcast_arg $mtu_arg
   set $new_routers
   if ping -q -c 1 $1; then
     if [ x$new_ip_address != x$alias_ip_address ] && \
 			[ x$alias_ip_address != x ]; then
-      ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
-      route add -host $alias_ip_address dev $interface:0
+      ${ip} -4 addr add ${alias_ip_address}${alias_mask} \
+            dev ${interface} label ${interface}:0
+      ${ip} -4 route add ${alias_ip_address} dev ${interface}
     fi
     for router in $new_routers; do
       if [ "x$new_subnet_mask" = "x255.255.255.255" ] ; then
-	route add -host $router dev $interface
+        ${ip} -4 route add ${router} dev $interface
       fi
-      route add default gw $router $metric_arg dev $interface
+      ${ip} -4 route add default via ${router} dev ${interface} \
+                    ${metric_arg}
     done
     make_resolv_conf
     exit_with_hooks 0
   fi
-  ifconfig $interface inet 0 down
+  ${ip} -4 addr flush dev ${interface}
   exit_with_hooks 1
 fi
 
