1247280Sdteskeif [ ! "$_MEDIA_NETWORK_SUBR" ]; then _MEDIA_NETWORK_SUBR=1 2247280Sdteske# 3247280Sdteske# Copyright (c) 2012-2013 Devin Teske 4252980Sdteske# All rights reserved. 5247280Sdteske# 6247280Sdteske# Redistribution and use in source and binary forms, with or without 7247280Sdteske# modification, are permitted provided that the following conditions 8247280Sdteske# are met: 9247280Sdteske# 1. Redistributions of source code must retain the above copyright 10247280Sdteske# notice, this list of conditions and the following disclaimer. 11247280Sdteske# 2. Redistributions in binary form must reproduce the above copyright 12247280Sdteske# notice, this list of conditions and the following disclaimer in the 13247280Sdteske# documentation and/or other materials provided with the distribution. 14247280Sdteske# 15247280Sdteske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16252987Sdteske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17247280Sdteske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18247280Sdteske# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19247280Sdteske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20252987Sdteske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21247280Sdteske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22247280Sdteske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23247280Sdteske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24247280Sdteske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25247280Sdteske# SUCH DAMAGE. 26247280Sdteske# 27247280Sdteske# $FreeBSD: releng/10.3/usr.sbin/bsdconfig/share/media/network.subr 252987 2013-07-07 18:51:44Z dteske $ 28247280Sdteske# 29247280Sdteske############################################################ INCLUDES 30247280Sdteske 31247280SdteskeBSDCFG_SHARE="/usr/share/bsdconfig" 32247280Sdteske. $BSDCFG_SHARE/common.subr || exit 1 33247280Sdteskef_dprintf "%s: loading includes..." media/network.subr 34247280Sdteskef_include $BSDCFG_SHARE/dialog.subr 35247280Sdteskef_include $BSDCFG_SHARE/media/tcpip.subr 36247280Sdteske 37247280SdteskeBSDCFG_LIBE="/usr/libexec/bsdconfig" 38247280Sdteskef_include_lang $BSDCFG_LIBE/include/messages.subr 39247280Sdteske 40247280Sdteske############################################################ GLOBALS 41247280Sdteske 42247280SdteskeNETWORK_INITIALIZED= 43247280Sdteske 44247280Sdteske############################################################ FUNCTIONS 45247280Sdteske 46247280Sdteske# f_media_init_network $device 47247280Sdteske# 48247280Sdteske# Initialize a network device (such as `fxp0', `em0', etc.). Returns success if 49247280Sdteske# able to successfully initialize the device. If not running as init (basically 50247280Sdteske# from the FreeBSD install media) then assume that the network has already been 51247280Sdteske# initialized and returns success. 52247280Sdteske# 53247280Sdteske# The variables (from variable.subr) used to initialize the network are as 54247280Sdteske# follows (all of which are configured either automatically or manaully): 55247280Sdteske# 56247280Sdteske# VAR_IFCONFIG + device_name (e.g., `ifconfig_em0') 57247280Sdteske# Automatically populated but can be overridden in a script. This 58247280Sdteske# defines the ifconfig(8) properties specific to a chosen network 59247280Sdteske# interface device. Optional if VAR_IPV6ADDR is set. 60247280Sdteske# VAR_IPV6ADDR [Optional] 61247280Sdteske# If not running as init (and setting up RTSOL connections for 62247280Sdteske# the interface), then must be set manually. If set, used as the 63247280Sdteske# IPv6 configuration for the given network interface device. 64247280Sdteske# VAR_GATEWAY [Optional] 65247280Sdteske# If not running as init (and setting up a static connection for 66247280Sdteske# the interface) then must be set (usually via rc.conf(5), but 67247280Sdteske# can be set manually to override). If unset, the user is warned 68247280Sdteske# but not prevented from proceeding (as most connections need a 69247280Sdteske# default route but not everyone). 70247280Sdteske# 71247280Sdteskef_media_init_network() 72247280Sdteske{ 73247280Sdteske local dev="$1" 74247280Sdteske 75247280Sdteske f_dprintf "Init routine called for network device \`%s'." "$dev" 76247280Sdteske if [ "$NETWORK_INITIALIZED" ]; then 77247280Sdteske f_dprintf "Network already initialized." 78247280Sdteske return $SUCCESS 79247280Sdteske elif ! f_running_as_init; then 80247280Sdteske f_dprintf "Not running as init -- calling the deed done." 81247280Sdteske NETWORK_INITIALIZED=1 82247280Sdteske return $SUCCESS 83247280Sdteske fi 84247280Sdteske 85247280Sdteske if [ ! -e "$RESOLV_CONF" ]; then 86247280Sdteske if ! f_config_resolv; then 87247280Sdteske f_show_msg "$msg_cant_seem_to_write_out_resolv_conf" \ 88247280Sdteske "$RESOLV_CONF" 89247280Sdteske return $FAILURE 90247280Sdteske fi 91247280Sdteske fi 92247280Sdteske 93247280Sdteske local cp 94247280Sdteske if f_getvar $VAR_IFCONFIG$dev cp; then 95247280Sdteske # 96247280Sdteske # If this interface isn't a DHCP one, bring it up. 97247280Sdteske # If it is, then it's already up. 98247280Sdteske # 99247280Sdteske case "$cp" in 100247280Sdteske *DHCP*) 101247280Sdteske f_dprintf "A DHCP interface. Should already be up." 102247280Sdteske ;; 103247280Sdteske *) 104247280Sdteske f_dprintf "Not a DHCP interface." 105247280Sdteske if ! f_quietly ifconfig "$dev" $cp; then 106247280Sdteske f_show_msg "$msg_unable_to_configure_device" \ 107247280Sdteske "$dev" 108247280Sdteske return $FAILURE 109247280Sdteske fi 110247280Sdteske local rp 111247280Sdteske f_getvar $VAR_GATEWAY rp 112247280Sdteske if [ ! "$rp" ]; then 113252795Sdteske f_show_msg "$msg_no_gateway_has_been_set" 114247280Sdteske else 115247280Sdteske # 116247280Sdteske # Explicitly flush all routes to get back to a 117247280Sdteske # known sane state. We don't need to check this 118247280Sdteske # exit code because if anything fails it will 119247280Sdteske # show up in the route add below. 120247280Sdteske # 121247280Sdteske f_quietly route -n flush 122247280Sdteske f_dprintf "Adding default route to %s." "$rp" 123247280Sdteske if ! f_quietly route -n add default "$rp"; then 124252795Sdteske f_show_msg \ 125247280Sdteske "$msg_failed_to_add_default_route" 126247280Sdteske return $FAILURE 127247280Sdteske fi 128247280Sdteske fi 129247280Sdteske esac 130247280Sdteske elif ! { f_getvar $VAR_IPV6ADDR cp && [ "$cp" ]; }; then 131247280Sdteske f_show_msg "$msg_device_is_not_configured" "$dev" 132247280Sdteske return $FAILURE 133247280Sdteske fi 134247280Sdteske 135247280Sdteske f_dprintf "Network initialized successfully." 136247280Sdteske NETWORK_INITIALIZED=1 137247280Sdteske return $SUCCESS 138247280Sdteske} 139247280Sdteske 140247280Sdteske# f_media_shutdown_network $device 141247280Sdteske# 142247280Sdteske# Shuts down the configured network device (e.g., `fxp0', `em0', etc.) and 143247280Sdteske# deletes the default route (if configured). Returns failure if the device 144247280Sdteske# passed has not been configured. If not running as init (basically from the 145247280Sdteske# FreeBSD install media) then does nothing and returns success. 146247280Sdteske# 147247280Sdteskef_media_shutdown_network() 148247280Sdteske{ 149247280Sdteske local dev="$1" cp 150247280Sdteske 151247280Sdteske f_dprintf "Shutdown called for network device %s" "$dev" 152247280Sdteske if [ ! "$NETWORK_INITIALIZED" ]; then 153247280Sdteske f_dprintf "Network not initialized -- nothing to do." 154247280Sdteske return $SUCCESS 155247280Sdteske fi 156247280Sdteske 157247280Sdteske unset NETWORK_INITIALIZED 158247280Sdteske unset $VAR_NETWORK_DEVICE 159247280Sdteske 160247280Sdteske if ! f_running_as_init; then 161247280Sdteske f_dprintf "Not running as init -- calling the deed done." 162247280Sdteske return $SUCCESS 163247280Sdteske fi 164247280Sdteske 165247280Sdteske f_getvar $VAR_IFCONFIG$dev cp || return $FAILURE 166247280Sdteske f_dprintf "ifconfig %s down" "$dev" 167247280Sdteske f_quietly ifconfig $dev down || 168247280Sdteske f_show_msg "$msg_unable_to_down_the_interface_properly" "$dev" 169247280Sdteske 170247280Sdteske if f_getvar $VAR_GATEWAY cp; then 171247280Sdteske f_dprintf "Deleting default route." 172247280Sdteske f_quietly route -n delete default 173247280Sdteske fi 174247280Sdteske 175247280Sdteske return $SUCCESS 176247280Sdteske} 177247280Sdteske 178247280Sdteske############################################################ MAIN 179247280Sdteske 180247280Sdteskef_dprintf "%s: Successfully loaded." media/network.subr 181247280Sdteske 182247280Sdteskefi # ! $_MEDIA_NETWORK_SUBR 183