1 if [ ! "$_MEDIA_NETWORK_SUBR" ]; then _MEDIA_NETWORK_SUBR=1
3 # Copyright (c) 2012-2013 Devin Teske
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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
29 ############################################################ INCLUDES
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." media/network.subr
34 f_include $BSDCFG_SHARE/dialog.subr
35 f_include $BSDCFG_SHARE/media/tcpip.subr
37 BSDCFG_LIBE="/usr/libexec/bsdconfig"
38 f_include_lang $BSDCFG_LIBE/include/messages.subr
40 ############################################################ GLOBALS
44 ############################################################ FUNCTIONS
46 # f_media_init_network $device
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.
53 # The variables (from variable.subr) used to initialize the network are as
54 # follows (all of which are configured either automatically or manaully):
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).
71 f_media_init_network()
75 f_dprintf "Init routine called for network device \`%s'." "$dev"
76 if [ "$NETWORK_INITIALIZED" ]; then
77 f_dprintf "Network already initialized."
79 elif ! f_running_as_init; then
80 f_dprintf "Not running as init -- calling the deed done."
85 if [ ! -e "$RESOLV_CONF" ]; then
86 if ! f_config_resolv; then
87 f_show_msg "$msg_cant_seem_to_write_out_resolv_conf" \
94 if f_getvar $VAR_IFCONFIG$dev cp; then
96 # If this interface isn't a DHCP one, bring it up.
97 # If it is, then it's already up.
101 f_dprintf "A DHCP interface. Should already be up."
104 f_dprintf "Not a DHCP interface."
105 if ! f_quietly ifconfig "$dev" $cp; then
106 f_show_msg "$msg_unable_to_configure_device" \
111 f_getvar $VAR_GATEWAY rp
113 f_show_msg "$msg_no_gateway_has_been_set"
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.
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
125 "$msg_failed_to_add_default_route"
130 elif ! { f_getvar $VAR_IPV6ADDR cp && [ "$cp" ]; }; then
131 f_show_msg "$msg_device_is_not_configured" "$dev"
135 f_dprintf "Network initialized successfully."
136 NETWORK_INITIALIZED=1
140 # f_media_shutdown_network $device
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.
147 f_media_shutdown_network()
151 f_dprintf "Shutdown called for network device %s" "$dev"
152 if [ ! "$NETWORK_INITIALIZED" ]; then
153 f_dprintf "Network not initialized -- nothing to do."
157 unset NETWORK_INITIALIZED
158 unset $VAR_NETWORK_DEVICE
160 if ! f_running_as_init; then
161 f_dprintf "Not running as init -- calling the deed done."
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"
170 if f_getvar $VAR_GATEWAY cp; then
171 f_dprintf "Deleting default route."
172 f_quietly route -n delete default
178 ############################################################ MAIN
180 f_dprintf "%s: Successfully loaded." media/network.subr
182 fi # ! $_MEDIA_NETWORK_SUBR