network.subr revision 252795
1262566Sdesif [ ! "$_MEDIA_NETWORK_SUBR" ]; then _MEDIA_NETWORK_SUBR=1 2197670Sdes# 3197670Sdes# Copyright (c) 2012-2013 Devin Teske 4197670Sdes# All Rights Reserved. 5197670Sdes# 6197670Sdes# Redistribution and use in source and binary forms, with or without 7197670Sdes# modification, are permitted provided that the following conditions 8197670Sdes# are met: 9197670Sdes# 1. Redistributions of source code must retain the above copyright 10197670Sdes# notice, this list of conditions and the following disclaimer. 11197670Sdes# 2. Redistributions in binary form must reproduce the above copyright 12197670Sdes# notice, this list of conditions and the following disclaimer in the 13197670Sdes# documentation and/or other materials provided with the distribution. 14197670Sdes# 15197670Sdes# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16197670Sdes# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE 17197670Sdes# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18197670Sdes# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19197670Sdes# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20197670Sdes# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21197670Sdes# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22197670Sdes# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23197670Sdes# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24197670Sdes# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25197670Sdes# SUCH DAMAGE. 26197670Sdes# 27197670Sdes# $FreeBSD: head/usr.sbin/bsdconfig/share/media/network.subr 252795 2013-07-05 16:00:01Z dteske $ 28197670Sdes# 29197670Sdes############################################################ INCLUDES 30197670Sdes 31197670SdesBSDCFG_SHARE="/usr/share/bsdconfig" 32197670Sdes. $BSDCFG_SHARE/common.subr || exit 1 33197670Sdesf_dprintf "%s: loading includes..." media/network.subr 34197670Sdesf_include $BSDCFG_SHARE/dialog.subr 35197670Sdesf_include $BSDCFG_SHARE/media/tcpip.subr 36197670Sdes 37197670SdesBSDCFG_LIBE="/usr/libexec/bsdconfig" 38197670Sdesf_include_lang $BSDCFG_LIBE/include/messages.subr 39262566Sdes 40197670Sdes############################################################ GLOBALS 41197670Sdes 42197670SdesNETWORK_INITIALIZED= 43197670Sdes 44197670Sdes############################################################ FUNCTIONS 45197670Sdes 46197670Sdes# f_media_init_network $device 47197670Sdes# 48197670Sdes# Initialize a network device (such as `fxp0', `em0', etc.). Returns success if 49197670Sdes# able to successfully initialize the device. If not running as init (basically 50197670Sdes# from the FreeBSD install media) then assume that the network has already been 51197670Sdes# initialized and returns success. 52197670Sdes# 53262566Sdes# The variables (from variable.subr) used to initialize the network are as 54197670Sdes# follows (all of which are configured either automatically or manaully): 55197670Sdes# 56204917Sdes# VAR_IFCONFIG + device_name (e.g., `ifconfig_em0') 57204917Sdes# Automatically populated but can be overridden in a script. This 58197670Sdes# defines the ifconfig(8) properties specific to a chosen network 59197670Sdes# interface device. Optional if VAR_IPV6ADDR is set. 60197670Sdes# VAR_IPV6ADDR [Optional] 61197670Sdes# If not running as init (and setting up RTSOL connections for 62197670Sdes# the interface), then must be set manually. If set, used as the 63197670Sdes# IPv6 configuration for the given network interface device. 64197670Sdes# VAR_GATEWAY [Optional] 65262566Sdes# If not running as init (and setting up a static connection for 66197670Sdes# the interface) then must be set (usually via rc.conf(5), but 67197670Sdes# can be set manually to override). If unset, the user is warned 68204917Sdes# but not prevented from proceeding (as most connections need a 69204917Sdes# default route but not everyone). 70197670Sdes# 71197670Sdesf_media_init_network() 72197670Sdes{ 73197670Sdes local dev="$1" 74197670Sdes 75197670Sdes f_dprintf "Init routine called for network device \`%s'." "$dev" 76197670Sdes if [ "$NETWORK_INITIALIZED" ]; then 77197670Sdes f_dprintf "Network already initialized." 78197670Sdes return $SUCCESS 79240075Sdes elif ! f_running_as_init; then 80240075Sdes f_dprintf "Not running as init -- calling the deed done." 81197670Sdes NETWORK_INITIALIZED=1 82197670Sdes return $SUCCESS 83197670Sdes fi 84197670Sdes 85197670Sdes if [ ! -e "$RESOLV_CONF" ]; then 86197670Sdes if ! f_config_resolv; then 87197670Sdes f_show_msg "$msg_cant_seem_to_write_out_resolv_conf" \ 88197670Sdes "$RESOLV_CONF" 89197670Sdes return $FAILURE 90197670Sdes fi 91197670Sdes fi 92197670Sdes 93197670Sdes local cp 94197670Sdes if f_getvar $VAR_IFCONFIG$dev cp; then 95197670Sdes # 96197670Sdes # If this interface isn't a DHCP one, bring it up. 97197670Sdes # If it is, then it's already up. 98197670Sdes # 99197670Sdes case "$cp" in 100197670Sdes *DHCP*) 101197670Sdes f_dprintf "A DHCP interface. Should already be up." 102197670Sdes ;; 103197670Sdes *) 104197670Sdes f_dprintf "Not a DHCP interface." 105197670Sdes if ! f_quietly ifconfig "$dev" $cp; then 106197670Sdes f_show_msg "$msg_unable_to_configure_device" \ 107197670Sdes "$dev" 108197670Sdes return $FAILURE 109197670Sdes fi 110197670Sdes local rp 111197670Sdes f_getvar $VAR_GATEWAY rp 112197670Sdes if [ ! "$rp" ]; then 113197670Sdes f_show_msg "$msg_no_gateway_has_been_set" 114197670Sdes else 115197670Sdes # 116197670Sdes # Explicitly flush all routes to get back to a 117197670Sdes # known sane state. We don't need to check this 118197670Sdes # exit code because if anything fails it will 119197670Sdes # show up in the route add below. 120197670Sdes # 121197670Sdes f_quietly route -n flush 122197670Sdes f_dprintf "Adding default route to %s." "$rp" 123197670Sdes if ! f_quietly route -n add default "$rp"; then 124197670Sdes f_show_msg \ 125197670Sdes "$msg_failed_to_add_default_route" 126197670Sdes return $FAILURE 127197670Sdes fi 128197670Sdes fi 129197670Sdes esac 130197670Sdes elif ! { f_getvar $VAR_IPV6ADDR cp && [ "$cp" ]; }; then 131197670Sdes f_show_msg "$msg_device_is_not_configured" "$dev" 132197670Sdes return $FAILURE 133197670Sdes fi 134197670Sdes 135197670Sdes f_dprintf "Network initialized successfully." 136197670Sdes NETWORK_INITIALIZED=1 137197670Sdes return $SUCCESS 138197670Sdes} 139197670Sdes 140197670Sdes# f_media_shutdown_network $device 141197670Sdes# 142197670Sdes# Shuts down the configured network device (e.g., `fxp0', `em0', etc.) and 143197670Sdes# deletes the default route (if configured). Returns failure if the device 144197670Sdes# passed has not been configured. If not running as init (basically from the 145197670Sdes# FreeBSD install media) then does nothing and returns success. 146197670Sdes# 147197670Sdesf_media_shutdown_network() 148197670Sdes{ 149197670Sdes local dev="$1" cp 150197670Sdes 151204917Sdes f_dprintf "Shutdown called for network device %s" "$dev" 152204917Sdes if [ ! "$NETWORK_INITIALIZED" ]; then 153204917Sdes f_dprintf "Network not initialized -- nothing to do." 154204917Sdes return $SUCCESS 155204917Sdes fi 156204917Sdes 157204917Sdes unset NETWORK_INITIALIZED 158204917Sdes unset $VAR_NETWORK_DEVICE 159204917Sdes 160204917Sdes if ! f_running_as_init; then 161197670Sdes f_dprintf "Not running as init -- calling the deed done." 162197670Sdes return $SUCCESS 163197670Sdes fi 164197670Sdes 165197670Sdes f_getvar $VAR_IFCONFIG$dev cp || return $FAILURE 166197670Sdes f_dprintf "ifconfig %s down" "$dev" 167197670Sdes f_quietly ifconfig $dev down || 168197670Sdes f_show_msg "$msg_unable_to_down_the_interface_properly" "$dev" 169197670Sdes 170197670Sdes if f_getvar $VAR_GATEWAY cp; then 171197670Sdes f_dprintf "Deleting default route." 172204917Sdes f_quietly route -n delete default 173204917Sdes fi 174204917Sdes 175204917Sdes return $SUCCESS 176204917Sdes} 177204917Sdes 178204917Sdes############################################################ MAIN 179204917Sdes 180204917Sdesf_dprintf "%s: Successfully loaded." media/network.subr 181197670Sdes 182197670Sdesfi # ! $_MEDIA_NETWORK_SUBR 183197670Sdes