aboutsummaryrefslogtreecommitdiff
path: root/init.d/network.in
blob: f03d08f0f57af6a830cf1250ba4f5cd289b74361 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!@PREFIX@/sbin/runscript
# Copyright (c) 2009 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.

# This script was inspired by the equivalent rc.d network from NetBSD.

description="Configures network interfaces."
__nl="
"

depend()
{
	need localmount
	after bootmisc
	provide net
	keyword -jail -prefix -vserver
}

uniqify()
{
	local result= i=
	for i; do
		case " $result " in
		*" $i "*);;
		*) result="$result $i";;
		esac
	done
	echo "${result# *}"
}

reverse()
{
	local result= i=
	for i; do
		result="$i $result"
	done
	echo "${result# *}"
}

sys_interfaces()
{
	case "$RC_UNAME" in
	Linux)
		local w= rest= i= cmd=$1
		while read w rest; do
			i=${w%%:*}
			[ "$i" != "$w" ] || continue
			if [ "$cmd" = u ]; then
				ifconfig "$i" | grep -q "[ ]*UP" || continue
			fi
			printf "%s " "$i"
		done </proc/net/dev
		;;
	*)
		ifconfig -l$1
		;;
	esac
}

tentative()
{
	local inet= address= rest=

	case "$RC_UNAME" in
	Linux)
		[ -x /sbin/ip ] || return 1
		[ -n "$(ip -f inet6 addr show tentative)" ]
		;;
	*)
		local inet= address= rest=
		LC_ALL=C ifconfig -a | while read inet address rest; do
	 		case "${inet}" in
			inet6)
				case "${rest}" in
				*" "tentative*) return 2;;
				esac
				;;
			esac
		done
		[ $? = 2 ]
		;;
	esac
}


auto_interfaces()
{
	local ifs= c= f=

	case "$RC_UNAME" in
	NetBSD)
		for c in $(ifconfig -C 2>/dev/null); do
			for f in /etc/ifconfig.${c}[0-9]*; do
				[ -f "$f" ] && printf "%s" "$f{##*.} "
			done
		done
		;;
	*)
		for f in /etc/ifconfig.*; do
			[ -f "$f" ] && printf "%s" "${f##*.} "
		done
		for f in /etc/ip.*; do
			[ -f "$f" ] && printf "%s" "${f##*.} "
		done
		;;
	esac
	echo
}

interfaces()
{
	uniqify $(sys_interfaces "$@") $interfaces $(auto_interfaces)
}

dumpargs()
{
	local f="$1"

	shift
	case "$@" in
	'')		[ -f "$f" ] && cat "$f";;
	*"$__nl"*)	echo "$@";;
	*)
		(
		 	set -o noglob
			IFS=';'; set -- $@
			IFS="$__nl"; echo "$*"
		);;
	esac
}

intup=false
runip()
{
	local int="$1" err=
	shift

	# Ensure we have a valid broadcast address
	case "$@" in
	*" broadcast "*|*" brd "*) ;;
	*:*) ;; # Ignore IPv6
	*) set -- "$@" brd +;;
	esac

	err=$(LC_ALL=C ip address add "$@" dev "$int" 2>&1)
	if [ -z "$err" ]; then
		# ip does not bring up the interface when adding addresses
		if ! $intup; then
			ip link set "$int" up
			intup=true
		fi
		return 0
	fi
	if [ "$err" = "RTNETLINK answers: File exists" ]; then
		ip address del "$@" dev "$int" 2>/dev/null
	fi
	# Localise the error
	ip address add "$@" dev "$int"
}

routeflush()
{
	if [ "$RC_UNAME" = Linux ]; then
		if [ -x /sbin/ip ]; then
			ip route flush scope global
			ip route delete default 2>/dev/null
		else
			# Sadly we also delete some link routes, but
			# this cannot be helped
			local dest= gate= net= flags= rest=
			route -n | while read dest gate net flags rest; do
				[ -z "$net" ] && continue
				case "$dest" in
				[0-9]*)	;;
				*)	continue;;
				esac
				local xtra= netmask="netmask $net"
				case "$flags" in
				U)	continue;;
				*H*)	flags=-host; netmask=;;
				*!*)	flags=-net; xtra=reject;;
				*)	flags=-net;;
				esac
				route del $flags $dest $netmask $xtra
			done
			# Erase any default dev eth0 routes
			route del default 2>/dev/null
		fi
	else
		route -qn flush
	fi
}

runargs()
{
	dumpargs "$@" | while read -r args; do
		case "$args" in
		''|"#"*)	;;
		*)
				(
				 	eval vebegin "${args#*!}"
					eval "${args#*!}"
					veend $?
				);;
		esac
	done
}

start()
{
	local cr=0 r= int= intv= cmd= args= upcmd=

	if [ -z "$domainname" -a -s /etc/defaultdomain ]; then
		domainname=$(cat /etc/defaultdomain)
	fi
	if [ -n "$domainname" ]; then
		ebegin "Setting NIS domainname: $domainname"
		domainname "$domainname"
		eend $?
	fi

	einfo "Starting network"
	routeflush
	if [ "$RC_UNAME" = "Linux" ]; then
		ifconfig lo 127.0.0.1 netmask 255.0.0.0 || cr=1 
		route add -net 127.0.0.0 netmask 255.0.0.0 \
			gw 127.0.0.1 2>/dev/null
		route add -net 127.0.0.0 netmask 255.0.0.0 \
			gw 127.0.0.1 reject 2>/dev/null
	else
		ifconfig lo0 127.0.0.1 netmask 255.0.0.0 || cr=1
		route -q add -inet 127.0.0.0 -netmask 255.0.0.0 \
			127.0.0.1 -reject || cr=1
	fi
	eindent
	for int in $(interfaces); do
		local func= cf=
		intv=$(shell_var "$int")
		eval upcmd=\$ifup_$intv
		for func in ip ifconfig; do
			eval cmd=\$${func}_$intv
			if [ -n "$cmd" -o -f /etc/"$func.$int" ]; then
				cf=/etc/"$func.$int"
				break
			fi
		done
		[ -n "$cf" -o -n "$upcmd" -o \
			-f /etc/ifup."$int" -o -f "$cf" ] || continue
		veinfo "$int"
		case "$func" in
		ip)	func=runip; intup=false;;
		esac
		eindent
		runargs /etc/ifup."$int" "$upcmd"
		r=0
		dumpargs "$cf" "$cmd" | while read -r args; do
			case "$args" in
			''|"#"*)	;;
			"!"*)
					(
					 	eval vebegin "${args#*!}"
						eval "${args#*!}"
						veend $?
					);;
			*)
					(
					 	set -o noglob
						eval set -- "$args"
						vebegin "$@"
						$func "$int" "$@"
						veend $?
					);;
			esac
		done
		eoutdent
	done
	eoutdent
	eend $cr

	# Wait for any inet6 tentative addresses
	r=5
	while [ $r -gt 0 ]; do
		tentative || break
		[ $r = 5 ] && vebegin "Waiting for tentative addresses"
		sleep 1
		r=$(($r - 1))
	done
	if [ $r != 5 ]; then
		[ $r != 0 ]
		veend $?
	fi
	
	if [ -n "$defaultroute" ]; then
		ebegin "Setting default route $defaultroute"
		route add default $defaultroute
		eend $?
	elif [ -n "$defaultiproute" ]; then
		ebegin "Setting default route $defaultiproute"
		ip route add default $defaultiproute
		eend $?
	fi

	if [ -n "$defaultroute6" ]; then
		ebegin "Setting default route $defaultroute6"
		if [ "$RC_UNAME" = Linux ]; then
			routecmd="route -A inet6 add"
		else
			routecmd="route -inet6 add"
		fi
		$routecmd default $defaultroute6
		eend $?
	elif [ -n "$defaultiproute6" ]; then
		ebegin "Setting default route $defaultiproute6"
		ip -f inet6 route add default $defaultiproute6
		eend $?
	fi

	return 0
}

stop()
{
	# Don't stop the network at shutdown.
 	# We don't use the noshutdown keyword so that we are started again
	# correctly if we go back to multiuser.
	yesno ${shutdown_network:-YES} && yesno $RC_GOINGDOWN && return 0

	local int= intv= cmd= downcmd= r=
	einfo "Stopping network"
	routeflush
	eindent
	for int in $(reverse $(interfaces u)); do
		intv=$(shell_var "$int")
		eval downcmd=\$ifdown_$intv
		eval cmd=\$ip_$intv
		[ -z "$cmd" ] && eval cmd=\$ifconfig_$intv
		if [ -n "$cmd" -o -f /etc/ip."$int" -o \
			-f /etc/ifconfig."$int" -o \
			-n "$downcmd" -o -f /etc/ifdown."$int" ];
		then
			veinfo "$int"
			runargs /etc/ifdown."$int" "$downcmd"
			if [ -x /sbin/ip ]; then
				# We need to do this, otherwise we may
				# fail to add things correctly on restart
				ip address flush dev "$int" 2>/dev/null
			fi
			ifconfig "$int" down 2>/dev/null
			ifconfig "$int" destroy 2>/dev/null
		fi
	done
	eoutdent
	eend 0
}