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

Daniel Dickinson openwrt at daniel.thecshore.com
Wed Jan 20 04:17:18 EST 2016


On 20/01/16 02:10 AM, Bastian Bittorf wrote:
> * openwrt at daniel.thecshore.com <openwrt at daniel.thecshore.com> [20.01.2016 07:21]:
>> @@ -5,30 +5,34 @@ set_classless_routes() {
>>   	local max=128
>>   	local type
>
> thanks for that, i have it also on my todo-list.
> please remove also the 'local type' here.

Missed that.

>
>>   	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 prefix="$(
>> +		eval "$(ipcalc.sh 0.0.0.0 ${subnet:-255.255.255.0})"
>> +		echo -n $PREFIX
>
> dont use '-n'

Why not?  It prevents echo from emitting an unwanted newline.

>
>> +	)"
>> +		
>> +	echo "udhcpc: ip address add $ip/${prefix:-24} ${broadcast:-+} dev $interface"
>> +	ip address add $ip/${prefix:-24} ${broadcast:-+} dev $interface"
>
> please dont double-fallback. It's ok to have it once default to '255.255.255.0',
> so just use $prefix

The second fallback is in case the interpolation fails.

>
> maybe we can even have a function in /lib/functions.sh for that:
>
> !#/bin/sh
> mask2cidr()
> {
> 	local x=${1##*255.}
> 	local allones=$(( (${#1} - ${#x}) * 2 ))
> 	local tbl='0^^^128^192^224^240^248^252^254^'
>
> 	x=${tbl%%${x%%.*}*}
> 	export CIDR=$(( allones + (${#x}/4) ))
> }
>
> mask2cidr 255.255.255.224
> echo $CIDR
>
>
>>
>>   	[ -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";"}
>
> the code leaves the default-gateway if already set and removes all other
> default routes. i dont like the awk-approach, maybe something like:

I wasn't planning on reworking the udhcpc script beyond making it work 
with ip vs ifconfig/route.  You're talking about changing more unrelated 
things, which really should go in a separate patch.

>
> root at box:~ ip route list exact '0.0.0.0/0'
> default via 217.0.116.253 dev pppoe-wan  proto static
> default via 10.63.21.98 dev eth0.1  metric 7
>
> #!/bin/sh
>
> replace_default_gw()
> {
> 	ip route list exact '0.0.0.0/0' | while read LINE; do
> 		set -- $LINE
> 		[ "$3" = "$valid_gw" ] || ip route del default via $3
> 	done
> }
>
> the rest looks good! there are still a lot of other users
> for route/ifconfig, but thats a good start!
>

Actually according to grep, only openvpn after this in base (there are < 
10 others in packages feed as well, and I am planning on creating a 
minimalist busybox package (calling the binary e.g. net-tools) to supply 
ifconfig/route for those packages that aren't converted yet, or for 
third party use of ifconfig/route (in the packages feeds; doesn't belong 
in base).

Regards,

Daniel
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list