diff mbox

[OpenWrt-Devel] package/config/netifd: Replace ifconfig/route with ip command

Message ID 1453294297-21725-1-git-send-email-openwrt@daniel.thecshore.com
State Changes Requested
Headers show

Commit Message

Daniel Dickinson Jan. 20, 2016, 12:51 p.m. UTC
From: Daniel Dickinson <openwrt@daniel.thecshore.com>

v2: Also update previously missed deconfig use of ifconfig
  : Replace ipcalc.sh callout with pure shell mask2cidr
  : Remove unused local variable

ip from busybox is now standard and it would be good to
eventually drop the ancient and 10+ year deprecated
upstream commands ifconfig and route, so eliminate
one of the last consumers of ifconfig and route in
the base system.

Signed-off-by: Daniel Dickinson <openwrt@daniel.thecshore.com>
---
 .../netifd/files/usr/share/udhcpc/default.script   | 34 +++++++++++++++-------
 1 file changed, 24 insertions(+), 10 deletions(-)

Comments

Bastian Bittorf Jan. 20, 2016, 1:08 p.m. UTC | #1
* openwrt@daniel.thecshore.com <openwrt@daniel.thecshore.com> [20.01.2016 14:01]:
> +# From Bastian Bittorf <bittorf@bluebottle.com>
> +# Included in this file to avoid dependencies
> +mask2cidr()

please drop the comment, thank you.

> +{
> +	local x=${1##*255.}
> +	local allones=$(( (${#1} - ${#x}) * 2 ))
> +	local tbl='0^^^128^192^224^240^248^252^254^'
> +
> +	x=${tbl%%${x%%.*}*}
> +	CIDR=$(( allones + (${#x}/4) ))
> +}
> +
>  set_classless_routes() {
>  	local max=128
> -	local type
>  	while [ -n "$1" -a -n "$2" -a $max -gt 0 ]; do
> -		[ ${1##*/} -eq 32 ] && type=host || type=net
>  		echo "udhcpc: adding route for $type $1 via $2"
> -		route add -$type "$1" gw "$2" dev "$interface"
> +		ip route add "$1" via "$2" dev "$interface"
>  		max=$(($max-1))

please use max=$((max-1))
the '$' is unneeded (just style).

>  setup_interface() {
> -	echo "udhcpc: ifconfig $interface $ip netmask ${subnet:-255.255.255.0} broadcast ${broadcast:-+}"
> -	ifconfig $interface $ip netmask ${subnet:-255.255.255.0} broadcast ${broadcast:-+}
> +	local CIDR
> +
> +	mask2cidr ${subnet:-255.255.255.0}
> +
> +	echo "udhcpc: ip address add $ip/${CIDR} ${broadcast:-+} dev $interface"
> +	ip address add $ip/${prefix:-24} ${broadcast:-+} dev $interface"

a mistake: "add $ip/${prefix:-24} " -> "add $ip/$CIDR" like in the 'echo'

> @@ -41,7 +55,7 @@ setup_interface() {
>  applied=
>  case "$1" in
>  	deconfig)
> -		ifconfig "$interface" 0.0.0.0
> +		ip -f inet addr flush dev "$interface"

during scripting i like to use '-family' which makes
it clearer for the unpractised reader. (so dont abbrev. 8-)

thank you!

bye, bastian
diff mbox

Patch

diff --git a/package/network/config/netifd/files/usr/share/udhcpc/default.script b/package/network/config/netifd/files/usr/share/udhcpc/default.script
index ac765a6..78cfe36 100755
--- a/package/network/config/netifd/files/usr/share/udhcpc/default.script
+++ b/package/network/config/netifd/files/usr/share/udhcpc/default.script
@@ -1,34 +1,48 @@ 
 #!/bin/sh
 [ -z "$1" ] && echo "Error: should be run by udhcpc" && exit 1
 
+# From Bastian Bittorf <bittorf@bluebottle.com>
+# Included in this file to avoid dependencies
+mask2cidr()
+{
+	local x=${1##*255.}
+	local allones=$(( (${#1} - ${#x}) * 2 ))
+	local tbl='0^^^128^192^224^240^248^252^254^'
+
+	x=${tbl%%${x%%.*}*}
+	CIDR=$(( allones + (${#x}/4) ))
+}
+
 set_classless_routes() {
 	local max=128
-	local type
 	while [ -n "$1" -a -n "$2" -a $max -gt 0 ]; do
-		[ ${1##*/} -eq 32 ] && type=host || type=net
 		echo "udhcpc: adding route for $type $1 via $2"
-		route add -$type "$1" gw "$2" dev "$interface"
+		ip route add "$1" via "$2" dev "$interface"
 		max=$(($max-1))
 		shift 2
 	done
 }
 
 setup_interface() {
-	echo "udhcpc: ifconfig $interface $ip netmask ${subnet:-255.255.255.0} broadcast ${broadcast:-+}"
-	ifconfig $interface $ip netmask ${subnet:-255.255.255.0} broadcast ${broadcast:-+}
+	local CIDR
+
+	mask2cidr ${subnet:-255.255.255.0}
+
+	echo "udhcpc: ip address add $ip/${CIDR} ${broadcast:-+} dev $interface"
+	ip address add $ip/${prefix:-24} ${broadcast:-+} dev $interface"
 
 	[ -n "$router" ] && [ "$router" != "0.0.0.0" ] && [ "$router" != "255.255.255.255" ] && {
 		echo "udhcpc: setting default routers: $router"
 
 		local valid_gw=""
 		for i in $router ; do
-			route add default gw $i dev $interface
+			ip route add default via $i dev $interface
 			valid_gw="${valid_gw:+$valid_gw|}$i"
 		done
 		
-		eval $(route -n | awk '
-			/^0.0.0.0\W{9}('$valid_gw')\W/ {next}
-			/^0.0.0.0/ {print "route del -net "$1" gw "$2";"}
+		eval $(ip route | awk '
+			/^default\Wvia\W('$valid_gw')/ {next}
+			/^default/ {print "ip route del "$1" via "$3";"}
 		')
 	}
 
@@ -41,7 +55,7 @@  setup_interface() {
 applied=
 case "$1" in
 	deconfig)
-		ifconfig "$interface" 0.0.0.0
+		ip -f inet addr flush dev "$interface"
 	;;
 	renew)
 		setup_interface update