aboutsummaryrefslogtreecommitdiffstats
path: root/package/netifd/files/usr/share/udhcpc/default.script
blob: ac765a6363b09cbf0bd8b0f529455ff42c77412d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/sh
[ -z "$1" ] && echo "Error: should be run by udhcpc" && exit 1

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"
		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:-+}

	[ -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
			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";"}
		')
	}

	# CIDR STATIC ROUTES (rfc3442)
	[ -n "$staticroutes" ] && set_classless_routes $staticroutes
	[ -n "$msstaticroutes" ] && set_classless_routes $msstaticroutes
}


applied=
case "$1" in
	deconfig)
		ifconfig "$interface" 0.0.0.0
	;;
	renew)
		setup_interface update
	;;
	bound)
		setup_interface ifup
	;;
esac

# user rules
[ -f /etc/udhcpc.user ] && . /etc/udhcpc.user

exit 0