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