17323adacSDevin Teskeif [ ! "$_MEDIA_NETWORK_SUBR" ]; then _MEDIA_NETWORK_SUBR=1 27323adacSDevin Teske# 37323adacSDevin Teske# Copyright (c) 2012-2013 Devin Teske 4f8ea072aSDevin Teske# All rights reserved. 57323adacSDevin Teske# 67323adacSDevin Teske# Redistribution and use in source and binary forms, with or without 77323adacSDevin Teske# modification, are permitted provided that the following conditions 87323adacSDevin Teske# are met: 97323adacSDevin Teske# 1. Redistributions of source code must retain the above copyright 107323adacSDevin Teske# notice, this list of conditions and the following disclaimer. 117323adacSDevin Teske# 2. Redistributions in binary form must reproduce the above copyright 127323adacSDevin Teske# notice, this list of conditions and the following disclaimer in the 137323adacSDevin Teske# documentation and/or other materials provided with the distribution. 147323adacSDevin Teske# 157323adacSDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16*8e37a7c8SDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 177323adacSDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 187323adacSDevin Teske# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 197323adacSDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20*8e37a7c8SDevin Teske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 217323adacSDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 227323adacSDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 237323adacSDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 247323adacSDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 257323adacSDevin Teske# SUCH DAMAGE. 267323adacSDevin Teske# 277323adacSDevin Teske# $FreeBSD$ 287323adacSDevin Teske# 297323adacSDevin Teske############################################################ INCLUDES 307323adacSDevin Teske 317323adacSDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig" 327323adacSDevin Teske. $BSDCFG_SHARE/common.subr || exit 1 337323adacSDevin Teskef_dprintf "%s: loading includes..." media/network.subr 347323adacSDevin Teskef_include $BSDCFG_SHARE/dialog.subr 357323adacSDevin Teskef_include $BSDCFG_SHARE/media/tcpip.subr 367323adacSDevin Teske 377323adacSDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig" 387323adacSDevin Teskef_include_lang $BSDCFG_LIBE/include/messages.subr 397323adacSDevin Teske 407323adacSDevin Teske############################################################ GLOBALS 417323adacSDevin Teske 427323adacSDevin TeskeNETWORK_INITIALIZED= 437323adacSDevin Teske 447323adacSDevin Teske############################################################ FUNCTIONS 457323adacSDevin Teske 467323adacSDevin Teske# f_media_init_network $device 477323adacSDevin Teske# 487323adacSDevin Teske# Initialize a network device (such as `fxp0', `em0', etc.). Returns success if 497323adacSDevin Teske# able to successfully initialize the device. If not running as init (basically 507323adacSDevin Teske# from the FreeBSD install media) then assume that the network has already been 517323adacSDevin Teske# initialized and returns success. 527323adacSDevin Teske# 537323adacSDevin Teske# The variables (from variable.subr) used to initialize the network are as 547323adacSDevin Teske# follows (all of which are configured either automatically or manaully): 557323adacSDevin Teske# 567323adacSDevin Teske# VAR_IFCONFIG + device_name (e.g., `ifconfig_em0') 577323adacSDevin Teske# Automatically populated but can be overridden in a script. This 587323adacSDevin Teske# defines the ifconfig(8) properties specific to a chosen network 597323adacSDevin Teske# interface device. Optional if VAR_IPV6ADDR is set. 607323adacSDevin Teske# VAR_IPV6ADDR [Optional] 617323adacSDevin Teske# If not running as init (and setting up RTSOL connections for 627323adacSDevin Teske# the interface), then must be set manually. If set, used as the 637323adacSDevin Teske# IPv6 configuration for the given network interface device. 647323adacSDevin Teske# VAR_GATEWAY [Optional] 657323adacSDevin Teske# If not running as init (and setting up a static connection for 667323adacSDevin Teske# the interface) then must be set (usually via rc.conf(5), but 677323adacSDevin Teske# can be set manually to override). If unset, the user is warned 687323adacSDevin Teske# but not prevented from proceeding (as most connections need a 697323adacSDevin Teske# default route but not everyone). 707323adacSDevin Teske# 717323adacSDevin Teskef_media_init_network() 727323adacSDevin Teske{ 737323adacSDevin Teske local dev="$1" 747323adacSDevin Teske 757323adacSDevin Teske f_dprintf "Init routine called for network device \`%s'." "$dev" 767323adacSDevin Teske if [ "$NETWORK_INITIALIZED" ]; then 777323adacSDevin Teske f_dprintf "Network already initialized." 787323adacSDevin Teske return $SUCCESS 797323adacSDevin Teske elif ! f_running_as_init; then 807323adacSDevin Teske f_dprintf "Not running as init -- calling the deed done." 817323adacSDevin Teske NETWORK_INITIALIZED=1 827323adacSDevin Teske return $SUCCESS 837323adacSDevin Teske fi 847323adacSDevin Teske 857323adacSDevin Teske if [ ! -e "$RESOLV_CONF" ]; then 867323adacSDevin Teske if ! f_config_resolv; then 877323adacSDevin Teske f_show_msg "$msg_cant_seem_to_write_out_resolv_conf" \ 887323adacSDevin Teske "$RESOLV_CONF" 897323adacSDevin Teske return $FAILURE 907323adacSDevin Teske fi 917323adacSDevin Teske fi 927323adacSDevin Teske 937323adacSDevin Teske local cp 947323adacSDevin Teske if f_getvar $VAR_IFCONFIG$dev cp; then 957323adacSDevin Teske # 967323adacSDevin Teske # If this interface isn't a DHCP one, bring it up. 977323adacSDevin Teske # If it is, then it's already up. 987323adacSDevin Teske # 997323adacSDevin Teske case "$cp" in 1007323adacSDevin Teske *DHCP*) 1017323adacSDevin Teske f_dprintf "A DHCP interface. Should already be up." 1027323adacSDevin Teske ;; 1037323adacSDevin Teske *) 1047323adacSDevin Teske f_dprintf "Not a DHCP interface." 1057323adacSDevin Teske if ! f_quietly ifconfig "$dev" $cp; then 1067323adacSDevin Teske f_show_msg "$msg_unable_to_configure_device" \ 1077323adacSDevin Teske "$dev" 1087323adacSDevin Teske return $FAILURE 1097323adacSDevin Teske fi 1107323adacSDevin Teske local rp 1117323adacSDevin Teske f_getvar $VAR_GATEWAY rp 1127323adacSDevin Teske if [ ! "$rp" ]; then 1137079fc4eSDevin Teske f_show_msg "$msg_no_gateway_has_been_set" 1147323adacSDevin Teske else 1157323adacSDevin Teske # 1167323adacSDevin Teske # Explicitly flush all routes to get back to a 1177323adacSDevin Teske # known sane state. We don't need to check this 1187323adacSDevin Teske # exit code because if anything fails it will 1197323adacSDevin Teske # show up in the route add below. 1207323adacSDevin Teske # 1217323adacSDevin Teske f_quietly route -n flush 1227323adacSDevin Teske f_dprintf "Adding default route to %s." "$rp" 1237323adacSDevin Teske if ! f_quietly route -n add default "$rp"; then 1247079fc4eSDevin Teske f_show_msg \ 1257323adacSDevin Teske "$msg_failed_to_add_default_route" 1267323adacSDevin Teske return $FAILURE 1277323adacSDevin Teske fi 1287323adacSDevin Teske fi 1297323adacSDevin Teske esac 1307323adacSDevin Teske elif ! { f_getvar $VAR_IPV6ADDR cp && [ "$cp" ]; }; then 1317323adacSDevin Teske f_show_msg "$msg_device_is_not_configured" "$dev" 1327323adacSDevin Teske return $FAILURE 1337323adacSDevin Teske fi 1347323adacSDevin Teske 1357323adacSDevin Teske f_dprintf "Network initialized successfully." 1367323adacSDevin Teske NETWORK_INITIALIZED=1 1377323adacSDevin Teske return $SUCCESS 1387323adacSDevin Teske} 1397323adacSDevin Teske 1407323adacSDevin Teske# f_media_shutdown_network $device 1417323adacSDevin Teske# 1427323adacSDevin Teske# Shuts down the configured network device (e.g., `fxp0', `em0', etc.) and 1437323adacSDevin Teske# deletes the default route (if configured). Returns failure if the device 1447323adacSDevin Teske# passed has not been configured. If not running as init (basically from the 1457323adacSDevin Teske# FreeBSD install media) then does nothing and returns success. 1467323adacSDevin Teske# 1477323adacSDevin Teskef_media_shutdown_network() 1487323adacSDevin Teske{ 1497323adacSDevin Teske local dev="$1" cp 1507323adacSDevin Teske 1517323adacSDevin Teske f_dprintf "Shutdown called for network device %s" "$dev" 1527323adacSDevin Teske if [ ! "$NETWORK_INITIALIZED" ]; then 1537323adacSDevin Teske f_dprintf "Network not initialized -- nothing to do." 1547323adacSDevin Teske return $SUCCESS 1557323adacSDevin Teske fi 1567323adacSDevin Teske 1577323adacSDevin Teske unset NETWORK_INITIALIZED 1587323adacSDevin Teske unset $VAR_NETWORK_DEVICE 1597323adacSDevin Teske 1607323adacSDevin Teske if ! f_running_as_init; then 1617323adacSDevin Teske f_dprintf "Not running as init -- calling the deed done." 1627323adacSDevin Teske return $SUCCESS 1637323adacSDevin Teske fi 1647323adacSDevin Teske 1657323adacSDevin Teske f_getvar $VAR_IFCONFIG$dev cp || return $FAILURE 1667323adacSDevin Teske f_dprintf "ifconfig %s down" "$dev" 1677323adacSDevin Teske f_quietly ifconfig $dev down || 1687323adacSDevin Teske f_show_msg "$msg_unable_to_down_the_interface_properly" "$dev" 1697323adacSDevin Teske 1707323adacSDevin Teske if f_getvar $VAR_GATEWAY cp; then 1717323adacSDevin Teske f_dprintf "Deleting default route." 1727323adacSDevin Teske f_quietly route -n delete default 1737323adacSDevin Teske fi 1747323adacSDevin Teske 1757323adacSDevin Teske return $SUCCESS 1767323adacSDevin Teske} 1777323adacSDevin Teske 1787323adacSDevin Teske############################################################ MAIN 1797323adacSDevin Teske 1807323adacSDevin Teskef_dprintf "%s: Successfully loaded." media/network.subr 1817323adacSDevin Teske 1827323adacSDevin Teskefi # ! $_MEDIA_NETWORK_SUBR 183