1251881Speterif [ ! "$_MEDIA_NETWORK_SUBR" ]; then _MEDIA_NETWORK_SUBR=1
2251881Speter#
3251881Speter# Copyright (c) 2012-2013 Devin Teske
4251881Speter# All rights reserved.
5251881Speter#
6251881Speter# Redistribution and use in source and binary forms, with or without
7251881Speter# modification, are permitted provided that the following conditions
8251881Speter# are met:
9251881Speter# 1. Redistributions of source code must retain the above copyright
10251881Speter#    notice, this list of conditions and the following disclaimer.
11251881Speter# 2. Redistributions in binary form must reproduce the above copyright
12251881Speter#    notice, this list of conditions and the following disclaimer in the
13251881Speter#    documentation and/or other materials provided with the distribution.
14251881Speter#
15251881Speter# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16251881Speter# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17251881Speter# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18251881Speter# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19251881Speter# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20251881Speter# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21251881Speter# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22251881Speter# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23251881Speter# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24251881Speter# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25251881Speter# SUCH DAMAGE.
26251881Speter#
27251881Speter# $FreeBSD$
28251881Speter#
29251881Speter############################################################ INCLUDES
30251881Speter
31251881SpeterBSDCFG_SHARE="/usr/share/bsdconfig"
32251881Speter. $BSDCFG_SHARE/common.subr || exit 1
33251881Speterf_dprintf "%s: loading includes..." media/network.subr
34251881Speterf_include $BSDCFG_SHARE/dialog.subr
35251881Speterf_include $BSDCFG_SHARE/media/tcpip.subr
36251881Speter
37251881SpeterBSDCFG_LIBE="/usr/libexec/bsdconfig"
38251881Speterf_include_lang $BSDCFG_LIBE/include/messages.subr
39251881Speter
40251881Speter############################################################ GLOBALS
41251881Speter
42251881SpeterNETWORK_INITIALIZED=
43251881Speter
44251881Speter############################################################ FUNCTIONS
45251881Speter
46251881Speter# f_media_init_network $device
47251881Speter#
48251881Speter# Initialize a network device (such as `fxp0', `em0', etc.). Returns success if
49251881Speter# able to successfully initialize the device. If not running as init (basically
50251881Speter# from the FreeBSD install media) then assume that the network has already been
51251881Speter# initialized and returns success.
52251881Speter#
53251881Speter# The variables (from variable.subr) used to initialize the network are as
54251881Speter# follows (all of which are configured either automatically or manaully):
55251881Speter#
56251881Speter# 	VAR_IFCONFIG + device_name (e.g., `ifconfig_em0')
57251881Speter# 		Automatically populated but can be overridden in a script. This
58251881Speter# 		defines the ifconfig(8) properties specific to a chosen network
59251881Speter# 		interface device. Optional if VAR_IPV6ADDR is set.
60251881Speter# 	VAR_IPV6ADDR [Optional]
61251881Speter# 		If not running as init (and setting up RTSOL connections for
62251881Speter# 		the interface), then must be set manually. If set, used as the
63251881Speter# 		IPv6 configuration for the given network interface device.
64251881Speter# 	VAR_GATEWAY [Optional]
65251881Speter# 		If not running as init (and setting up a static connection for
66251881Speter# 		the interface) then must be set (usually via rc.conf(5), but
67251881Speter# 		can be set manually to override). If unset, the user is warned
68251881Speter# 		but not prevented from proceeding (as most connections need a
69251881Speter# 		default route but not everyone).
70251881Speter#
71251881Speterf_media_init_network()
72251881Speter{
73251881Speter	local dev="$1"
74251881Speter
75251881Speter	f_dprintf "Init routine called for network device \`%s'." "$dev"
76251881Speter	if [ "$NETWORK_INITIALIZED" ]; then
77251881Speter		f_dprintf "Network already initialized."
78251881Speter		return $SUCCESS
79251881Speter	elif ! f_running_as_init; then
80251881Speter		f_dprintf "Not running as init -- calling the deed done."
81251881Speter		NETWORK_INITIALIZED=1
82251881Speter		return $SUCCESS
83251881Speter	fi
84251881Speter
85251881Speter	if [ ! -e "$RESOLV_CONF" ]; then
86251881Speter		if ! f_config_resolv; then
87251881Speter			f_show_msg "$msg_cant_seem_to_write_out_resolv_conf" \
88251881Speter			           "$RESOLV_CONF"
89251881Speter			return $FAILURE
90251881Speter		fi
91251881Speter	fi
92251881Speter
93251881Speter	local cp
94251881Speter	if f_getvar $VAR_IFCONFIG$dev cp; then
95251881Speter		#
96251881Speter		# If this interface isn't a DHCP one, bring it up.
97251881Speter		# If it is, then it's already up.
98251881Speter		#
99299742Sdim		case "$cp" in
100251881Speter		*DHCP*)
101251881Speter			f_dprintf "A DHCP interface.  Should already be up."
102251881Speter			;;
103251881Speter		*)
104251881Speter			f_dprintf "Not a DHCP interface."
105251881Speter			if ! f_quietly ifconfig "$dev" $cp; then
106251881Speter				f_show_msg "$msg_unable_to_configure_device" \
107251881Speter				           "$dev"
108299742Sdim				return $FAILURE
109251881Speter			fi
110251881Speter			local rp
111251881Speter			f_getvar $VAR_GATEWAY rp
112251881Speter			if [ ! "$rp" ]; then
113251881Speter				f_show_msg "$msg_no_gateway_has_been_set"
114251881Speter			else
115299742Sdim				#
116299742Sdim				# Explicitly flush all routes to get back to a
117299742Sdim				# known sane state. We don't need to check this
118299742Sdim				# exit code because if anything fails it will
119299742Sdim				# show up in the route add below.
120299742Sdim				#
121299742Sdim				f_quietly route -n flush
122299742Sdim				f_dprintf "Adding default route to %s." "$rp"
123299742Sdim				if ! f_quietly route -n add default "$rp"; then
124299742Sdim					f_show_msg \
125299742Sdim					    "$msg_failed_to_add_default_route"
126299742Sdim					return $FAILURE
127299742Sdim				fi
128299742Sdim			fi
129299742Sdim		esac
130299742Sdim	elif ! { f_getvar $VAR_IPV6ADDR cp && [ "$cp" ]; }; then
131251881Speter		f_show_msg "$msg_device_is_not_configured" "$dev"
132299742Sdim		return $FAILURE
133299742Sdim	fi
134299742Sdim
135299742Sdim	f_dprintf "Network initialized successfully."
136299742Sdim	NETWORK_INITIALIZED=1
137299742Sdim	return $SUCCESS
138299742Sdim}
139299742Sdim
140299742Sdim# f_media_shutdown_network $device
141299742Sdim#
142299742Sdim# Shuts down the configured network device (e.g., `fxp0', `em0', etc.) and
143299742Sdim# deletes the default route (if configured). Returns failure if the device
144299742Sdim# passed has not been configured. If not running as init (basically from the
145299742Sdim# FreeBSD install media) then does nothing and returns success.
146299742Sdim#
147299742Sdimf_media_shutdown_network()
148299742Sdim{
149299742Sdim	local dev="$1" cp
150299742Sdim
151299742Sdim	f_dprintf "Shutdown called for network device %s" "$dev"
152251881Speter	if [ ! "$NETWORK_INITIALIZED" ]; then
153251881Speter		f_dprintf "Network not initialized -- nothing to do."
154251881Speter		return $SUCCESS
155251881Speter	fi
156251881Speter
157251881Speter	unset NETWORK_INITIALIZED
158251881Speter	unset $VAR_NETWORK_DEVICE
159251881Speter
160299742Sdim	if ! f_running_as_init; then
161299742Sdim		f_dprintf "Not running as init -- calling the deed done."
162299742Sdim		return $SUCCESS
163299742Sdim	fi
164299742Sdim
165251881Speter	f_getvar $VAR_IFCONFIG$dev cp || return $FAILURE
166251881Speter	f_dprintf "ifconfig %s down" "$dev"
167251881Speter	f_quietly ifconfig $dev down ||
168251881Speter		f_show_msg "$msg_unable_to_down_the_interface_properly" "$dev"
169251881Speter
170251881Speter	if f_getvar $VAR_GATEWAY cp; then
171251881Speter		f_dprintf "Deleting default route."
172251881Speter		f_quietly route -n delete default
173251881Speter	fi
174251881Speter
175251881Speter	return $SUCCESS
176251881Speter}
177251881Speter
178251881Speter############################################################ MAIN
179251881Speter
180251881Speterf_dprintf "%s: Successfully loaded." media/network.subr
181251881Speter
182251881Speterfi # ! $_MEDIA_NETWORK_SUBR
183251881Speter