aboutsummaryrefslogtreecommitdiffstats
path: root/main/busybox-initscripts/default.script
blob: 2a8267f22123550caade5db843f87ccc0b3ad05d (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/sh

# script for udhcpc
# Copyright (c) 2008 Natanael Copa <natanael.copa@gmail.com>

UDHCPC="/etc/udhcpc"
UDHCPC_CONF="$UDHCPC/udhcpc.conf"

RESOLV_CONF="/etc/resolv.conf"
[ -f $UDHCPC_CONF ] && . $UDHCPC_CONF

export broadcast
export dns
export domain
export interface
export ip
export mask
export metric
export router
export subnet

export PATH=/usr/bin:/bin:/usr/sbin:/sbin

run_scripts() {
	local dir=$1
	if [ -d $dir ]; then
		for i in $dir/*; do
			[ -f $i ] && $i
		done
	fi
}

deconfig() {
	ip addr flush dev $interface
}

is_wifi() {
	test -e /sys/class/net/$interface/phy80211
}

if_index() {
	if [ -e  /sys/class/net/$interface/ifindex ]; then
		cat /sys/class/net/$interface/ifindex
	else
		ip link show dev $interface | head -n1 | cut -d: -f1
	fi
}

calc_metric() {
	local base=
	if is_wifi; then
		base=300
	else
		base=200
	fi
	echo $(( $base + $(if_index) ))
}

routes() {
	[ -z "$router" ] && return
	for i in $NO_GATEWAY; do
		[ "$i" = "$interface" ] && return
	done
	local gw= num=
	while ip route del default via dev $interface 2>/dev/null; do
		:
	done
	num=0
	for gw in $router; do
		ip route add 0.0.0.0/0 via $gw dev $interface \
			metric $(( $num + ${IF_METRIC:-$(calc_metric)} ))
		num=$(( $num + 1 ))
	done
}

resolvconf() {
	local i
	[ -n "$IF_PEER_DNS" ] && [ "$IF_PEER_DNS" != "yes" ] && return
	if [ "$RESOLV_CONF" = "no" ] || [ "$RESOLV_CONF" = "NO" ] \
			|| [ -z "$RESOLV_CONF" ]; then
		return
	fi
	for i in $NO_DNS; do
		[ "$i" = "$interface" ] && return
	done
	echo -n > "$RESOLV_CONF.$$"
	if [ -n "$search" ]; then
		echo "search $search" >> "$RESOLV_CONF.$$"
	elif [ -n "$domain" ]; then
		echo "search $domain" >> "$RESOLV_CONF.$$"
	fi
	for i in $dns; do
		echo "nameserver $i" >> "$RESOLV_CONF.$$"
	done
	mv "$RESOLV_CONF.$$" "$RESOLV_CONF"
}

bound() {
	ip addr add $ip/$mask ${broadcast:+broadcast $broadcast} dev $interface
	ip link set dev $interface up
	routes
	resolvconf
}

renew() {
	if ! ip addr show dev $interface | grep $ip/$mask; then
		ip addr flush dev $interface
		ip addr add $ip/$mask ${broadcast:+broadcast $broadcast} dev $interface
	fi

	local i
	for i in $router; do
		if ! ip route show | grep ^default | grep $i; then
			routes
			break
		fi
	done

	if ! grep "^search $domain"; then
		resolvconf
		return
	fi
	for i in $dns; do
		if ! grep "^nameserver $i"; then
			resolvconf
			return
		fi
	done
}

case "$1" in
	deconfig|renew|bound)
		run_scripts $UDHCPC/pre-$1
		$1
		run_scripts $UDHCPC/post-$1
		;;
	leasefail)
		echo "udhcpc failed to get a DHCP lease" >&2
		;;
	nak)
		echo "udhcpc received DHCP NAK" >&2
		;;
	*)
		echo "Error: this script should be called from udhcpc" >&2
		exit 1
		;;
esac
exit 0