xref: /freebsd/usr.sbin/bsdconfig/share/media/network.subr (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1if [ ! "$_MEDIA_NETWORK_SUBR" ]; then _MEDIA_NETWORK_SUBR=1
2#
3# Copyright (c) 2012-2013 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD$
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." media/network.subr
34f_include $BSDCFG_SHARE/dialog.subr
35f_include $BSDCFG_SHARE/media/tcpip.subr
36
37BSDCFG_LIBE="/usr/libexec/bsdconfig"
38f_include_lang $BSDCFG_LIBE/include/messages.subr
39
40############################################################ GLOBALS
41
42NETWORK_INITIALIZED=
43
44############################################################ FUNCTIONS
45
46# f_media_init_network $device
47#
48# Initialize a network device (such as `fxp0', `em0', etc.). Returns success if
49# able to successfully initialize the device. If not running as init (basically
50# from the FreeBSD install media) then assume that the network has already been
51# initialized and returns success.
52#
53# The variables (from variable.subr) used to initialize the network are as
54# follows (all of which are configured either automatically or manually):
55#
56# 	VAR_IFCONFIG + device_name (e.g., `ifconfig_em0')
57# 		Automatically populated but can be overridden in a script. This
58# 		defines the ifconfig(8) properties specific to a chosen network
59# 		interface device. Optional if VAR_IPV6ADDR is set.
60# 	VAR_IPV6ADDR [Optional]
61# 		If not running as init (and setting up RTSOL connections for
62# 		the interface), then must be set manually. If set, used as the
63# 		IPv6 configuration for the given network interface device.
64# 	VAR_GATEWAY [Optional]
65# 		If not running as init (and setting up a static connection for
66# 		the interface) then must be set (usually via rc.conf(5), but
67# 		can be set manually to override). If unset, the user is warned
68# 		but not prevented from proceeding (as most connections need a
69# 		default route but not everyone).
70#
71f_media_init_network()
72{
73	local dev="$1"
74
75	f_dprintf "Init routine called for network device \`%s'." "$dev"
76	if [ "$NETWORK_INITIALIZED" ]; then
77		f_dprintf "Network already initialized."
78		return $SUCCESS
79	elif ! f_running_as_init; then
80		f_dprintf "Not running as init -- calling the deed done."
81		NETWORK_INITIALIZED=1
82		return $SUCCESS
83	fi
84
85	if [ ! -e "$RESOLV_CONF" ]; then
86		if ! f_config_resolv; then
87			f_show_msg "$msg_cant_seem_to_write_out_resolv_conf" \
88			           "$RESOLV_CONF"
89			return $FAILURE
90		fi
91	fi
92
93	local cp
94	if f_getvar $VAR_IFCONFIG$dev cp; then
95		#
96		# If this interface isn't a DHCP one, bring it up.
97		# If it is, then it's already up.
98		#
99		case "$cp" in
100		*DHCP*)
101			f_dprintf "A DHCP interface.  Should already be up."
102			;;
103		*)
104			f_dprintf "Not a DHCP interface."
105			if ! f_quietly ifconfig "$dev" $cp; then
106				f_show_msg "$msg_unable_to_configure_device" \
107				           "$dev"
108				return $FAILURE
109			fi
110			local rp
111			f_getvar $VAR_GATEWAY rp
112			if [ ! "$rp" ]; then
113				f_show_msg "$msg_no_gateway_has_been_set"
114			else
115				#
116				# Explicitly flush all routes to get back to a
117				# known sane state. We don't need to check this
118				# exit code because if anything fails it will
119				# show up in the route add below.
120				#
121				f_quietly route -n flush
122				f_dprintf "Adding default route to %s." "$rp"
123				if ! f_quietly route -n add default "$rp"; then
124					f_show_msg \
125					    "$msg_failed_to_add_default_route"
126					return $FAILURE
127				fi
128			fi
129		esac
130	elif ! { f_getvar $VAR_IPV6ADDR cp && [ "$cp" ]; }; then
131		f_show_msg "$msg_device_is_not_configured" "$dev"
132		return $FAILURE
133	fi
134
135	f_dprintf "Network initialized successfully."
136	NETWORK_INITIALIZED=1
137	return $SUCCESS
138}
139
140# f_media_shutdown_network $device
141#
142# Shuts down the configured network device (e.g., `fxp0', `em0', etc.) and
143# deletes the default route (if configured). Returns failure if the device
144# passed has not been configured. If not running as init (basically from the
145# FreeBSD install media) then does nothing and returns success.
146#
147f_media_shutdown_network()
148{
149	local dev="$1" cp
150
151	f_dprintf "Shutdown called for network device %s" "$dev"
152	if [ ! "$NETWORK_INITIALIZED" ]; then
153		f_dprintf "Network not initialized -- nothing to do."
154		return $SUCCESS
155	fi
156
157	unset NETWORK_INITIALIZED
158	unset $VAR_NETWORK_DEVICE
159
160	if ! f_running_as_init; then
161		f_dprintf "Not running as init -- calling the deed done."
162		return $SUCCESS
163	fi
164
165	f_getvar $VAR_IFCONFIG$dev cp || return $FAILURE
166	f_dprintf "ifconfig %s down" "$dev"
167	f_quietly ifconfig $dev down ||
168		f_show_msg "$msg_unable_to_down_the_interface_properly" "$dev"
169
170	if f_getvar $VAR_GATEWAY cp; then
171		f_dprintf "Deleting default route."
172		f_quietly route -n delete default
173	fi
174
175	return $SUCCESS
176}
177
178############################################################ MAIN
179
180f_dprintf "%s: Successfully loaded." media/network.subr
181
182fi # ! $_MEDIA_NETWORK_SUBR
183