xref: /freebsd/libexec/rc/rc.d/defaultroute (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
1#!/bin/sh
2#
3# Wait for the default route to be up if DHCP is in use
4#
5# $FreeBSD$
6#
7
8# PROVIDE: defaultroute
9# REQUIRE: devd netif stf
10# KEYWORD: nojailvnet
11
12. /etc/rc.subr
13. /etc/network.subr
14
15name="defaultroute"
16desc="Setup default router"
17start_cmd="defaultroute_start"
18stop_cmd=":"
19
20# Does any interface have a carrier?
21defaultroute_carrier()
22{
23	local carrier nocarrier
24
25	carrier=1
26	for _if in ${dhcp_interfaces}; do
27		output=`/sbin/ifconfig ${_if}`
28		nocarrier=`expr "${output}" : '.*[[:blank:]]status: \(no carrier\)'`
29		[ -z "${nocarrier}" ] && carrier=0
30	done
31	return ${carrier}
32}
33
34defaultroute_start()
35{
36	local nl waited
37
38	afexists inet || return 0
39
40	# Return without waiting if we don't have dhcp interfaces or
41	# if none of the dhcp interfaces is plugged in.
42	dhcp_interfaces=`list_net_interfaces dhcp`
43	[ -z "${dhcp_interfaces}" ] && return
44
45	# Wait for a default route
46	waited=0
47	while [ ${waited} -lt ${defaultroute_delay} ]; do
48		defif=`get_default_if -inet`
49		if [ -n "${defif}" ]; then
50			if [ ${waited} -ne 0 ]; then
51				echo -n "($defif)"
52				nl=1
53			fi
54			break
55		fi
56		if [ ${waited} -eq 0 ]; then
57			echo -n "Waiting ${defaultroute_delay}s for the default route interface: "
58		else
59			echo -n .
60		fi
61		if [ ${waited} -eq ${defaultroute_carrier_delay} ] && ! defaultroute_carrier; then
62			echo -n "(no carrier)"
63			break
64		fi
65		nl=1
66		sleep 1
67		waited=$(($waited + 1))
68	done
69
70	[ -n "$nl" ] && echo
71}
72
73load_rc_config $name
74run_rc_command "$1"
75