xref: /freebsd/usr.sbin/bsdconfig/share/media/network.subr (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
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#
28############################################################ INCLUDES
29
30BSDCFG_SHARE="/usr/share/bsdconfig"
31. $BSDCFG_SHARE/common.subr || exit 1
32f_dprintf "%s: loading includes..." media/network.subr
33f_include $BSDCFG_SHARE/dialog.subr
34f_include $BSDCFG_SHARE/media/tcpip.subr
35
36BSDCFG_LIBE="/usr/libexec/bsdconfig"
37f_include_lang $BSDCFG_LIBE/include/messages.subr
38
39############################################################ GLOBALS
40
41NETWORK_INITIALIZED=
42
43############################################################ FUNCTIONS
44
45# f_media_init_network $device
46#
47# Initialize a network device (such as `fxp0', `em0', etc.). Returns success if
48# able to successfully initialize the device. If not running as init (basically
49# from the FreeBSD install media) then assume that the network has already been
50# initialized and returns success.
51#
52# The variables (from variable.subr) used to initialize the network are as
53# follows (all of which are configured either automatically or manually):
54#
55# 	VAR_IFCONFIG + device_name (e.g., `ifconfig_em0')
56# 		Automatically populated but can be overridden in a script. This
57# 		defines the ifconfig(8) properties specific to a chosen network
58# 		interface device. Optional if VAR_IPV6ADDR is set.
59# 	VAR_IPV6ADDR [Optional]
60# 		If not running as init (and setting up RTSOL connections for
61# 		the interface), then must be set manually. If set, used as the
62# 		IPv6 configuration for the given network interface device.
63# 	VAR_GATEWAY [Optional]
64# 		If not running as init (and setting up a static connection for
65# 		the interface) then must be set (usually via rc.conf(5), but
66# 		can be set manually to override). If unset, the user is warned
67# 		but not prevented from proceeding (as most connections need a
68# 		default route but not everyone).
69#
70f_media_init_network()
71{
72	local dev="$1"
73
74	f_dprintf "Init routine called for network device \`%s'." "$dev"
75	if [ "$NETWORK_INITIALIZED" ]; then
76		f_dprintf "Network already initialized."
77		return $SUCCESS
78	elif ! f_running_as_init; then
79		f_dprintf "Not running as init -- calling the deed done."
80		NETWORK_INITIALIZED=1
81		return $SUCCESS
82	fi
83
84	if [ ! -e "$RESOLV_CONF" ]; then
85		if ! f_config_resolv; then
86			f_show_msg "$msg_cant_seem_to_write_out_resolv_conf" \
87			           "$RESOLV_CONF"
88			return $FAILURE
89		fi
90	fi
91
92	local cp
93	if f_getvar $VAR_IFCONFIG$dev cp; then
94		#
95		# If this interface isn't a DHCP one, bring it up.
96		# If it is, then it's already up.
97		#
98		case "$cp" in
99		*DHCP*)
100			f_dprintf "A DHCP interface.  Should already be up."
101			;;
102		*)
103			f_dprintf "Not a DHCP interface."
104			if ! f_quietly ifconfig "$dev" $cp; then
105				f_show_msg "$msg_unable_to_configure_device" \
106				           "$dev"
107				return $FAILURE
108			fi
109			local rp
110			f_getvar $VAR_GATEWAY rp
111			if [ ! "$rp" ]; then
112				f_show_msg "$msg_no_gateway_has_been_set"
113			else
114				#
115				# Explicitly flush all routes to get back to a
116				# known sane state. We don't need to check this
117				# exit code because if anything fails it will
118				# show up in the route add below.
119				#
120				f_quietly route -n flush
121				f_dprintf "Adding default route to %s." "$rp"
122				if ! f_quietly route -n add default "$rp"; then
123					f_show_msg \
124					    "$msg_failed_to_add_default_route"
125					return $FAILURE
126				fi
127			fi
128		esac
129	elif ! { f_getvar $VAR_IPV6ADDR cp && [ "$cp" ]; }; then
130		f_show_msg "$msg_device_is_not_configured" "$dev"
131		return $FAILURE
132	fi
133
134	f_dprintf "Network initialized successfully."
135	NETWORK_INITIALIZED=1
136	return $SUCCESS
137}
138
139# f_media_shutdown_network $device
140#
141# Shuts down the configured network device (e.g., `fxp0', `em0', etc.) and
142# deletes the default route (if configured). Returns failure if the device
143# passed has not been configured. If not running as init (basically from the
144# FreeBSD install media) then does nothing and returns success.
145#
146f_media_shutdown_network()
147{
148	local dev="$1" cp
149
150	f_dprintf "Shutdown called for network device %s" "$dev"
151	if [ ! "$NETWORK_INITIALIZED" ]; then
152		f_dprintf "Network not initialized -- nothing to do."
153		return $SUCCESS
154	fi
155
156	unset NETWORK_INITIALIZED
157	unset $VAR_NETWORK_DEVICE
158
159	if ! f_running_as_init; then
160		f_dprintf "Not running as init -- calling the deed done."
161		return $SUCCESS
162	fi
163
164	f_getvar $VAR_IFCONFIG$dev cp || return $FAILURE
165	f_dprintf "ifconfig %s down" "$dev"
166	f_quietly ifconfig $dev down ||
167		f_show_msg "$msg_unable_to_down_the_interface_properly" "$dev"
168
169	if f_getvar $VAR_GATEWAY cp; then
170		f_dprintf "Deleting default route."
171		f_quietly route -n delete default
172	fi
173
174	return $SUCCESS
175}
176
177############################################################ MAIN
178
179f_dprintf "%s: Successfully loaded." media/network.subr
180
181fi # ! $_MEDIA_NETWORK_SUBR
182