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 168e37a7c8SDevin 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 208e37a7c8SDevin 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# 287323adacSDevin Teske############################################################ INCLUDES 297323adacSDevin Teske 307323adacSDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig" 317323adacSDevin Teske. $BSDCFG_SHARE/common.subr || exit 1 327323adacSDevin Teskef_dprintf "%s: loading includes..." media/network.subr 337323adacSDevin Teskef_include $BSDCFG_SHARE/dialog.subr 347323adacSDevin Teskef_include $BSDCFG_SHARE/media/tcpip.subr 357323adacSDevin Teske 367323adacSDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig" 377323adacSDevin Teskef_include_lang $BSDCFG_LIBE/include/messages.subr 387323adacSDevin Teske 397323adacSDevin Teske############################################################ GLOBALS 407323adacSDevin Teske 417323adacSDevin TeskeNETWORK_INITIALIZED= 427323adacSDevin Teske 437323adacSDevin Teske############################################################ FUNCTIONS 447323adacSDevin Teske 457323adacSDevin Teske# f_media_init_network $device 467323adacSDevin Teske# 477323adacSDevin Teske# Initialize a network device (such as `fxp0', `em0', etc.). Returns success if 487323adacSDevin Teske# able to successfully initialize the device. If not running as init (basically 497323adacSDevin Teske# from the FreeBSD install media) then assume that the network has already been 507323adacSDevin Teske# initialized and returns success. 517323adacSDevin Teske# 527323adacSDevin Teske# The variables (from variable.subr) used to initialize the network are as 53*5aa0576bSEd Maste# follows (all of which are configured either automatically or manually): 547323adacSDevin Teske# 557323adacSDevin Teske# VAR_IFCONFIG + device_name (e.g., `ifconfig_em0') 567323adacSDevin Teske# Automatically populated but can be overridden in a script. This 577323adacSDevin Teske# defines the ifconfig(8) properties specific to a chosen network 587323adacSDevin Teske# interface device. Optional if VAR_IPV6ADDR is set. 597323adacSDevin Teske# VAR_IPV6ADDR [Optional] 607323adacSDevin Teske# If not running as init (and setting up RTSOL connections for 617323adacSDevin Teske# the interface), then must be set manually. If set, used as the 627323adacSDevin Teske# IPv6 configuration for the given network interface device. 637323adacSDevin Teske# VAR_GATEWAY [Optional] 647323adacSDevin Teske# If not running as init (and setting up a static connection for 657323adacSDevin Teske# the interface) then must be set (usually via rc.conf(5), but 667323adacSDevin Teske# can be set manually to override). If unset, the user is warned 677323adacSDevin Teske# but not prevented from proceeding (as most connections need a 687323adacSDevin Teske# default route but not everyone). 697323adacSDevin Teske# 707323adacSDevin Teskef_media_init_network() 717323adacSDevin Teske{ 727323adacSDevin Teske local dev="$1" 737323adacSDevin Teske 747323adacSDevin Teske f_dprintf "Init routine called for network device \`%s'." "$dev" 757323adacSDevin Teske if [ "$NETWORK_INITIALIZED" ]; then 767323adacSDevin Teske f_dprintf "Network already initialized." 777323adacSDevin Teske return $SUCCESS 787323adacSDevin Teske elif ! f_running_as_init; then 797323adacSDevin Teske f_dprintf "Not running as init -- calling the deed done." 807323adacSDevin Teske NETWORK_INITIALIZED=1 817323adacSDevin Teske return $SUCCESS 827323adacSDevin Teske fi 837323adacSDevin Teske 847323adacSDevin Teske if [ ! -e "$RESOLV_CONF" ]; then 857323adacSDevin Teske if ! f_config_resolv; then 867323adacSDevin Teske f_show_msg "$msg_cant_seem_to_write_out_resolv_conf" \ 877323adacSDevin Teske "$RESOLV_CONF" 887323adacSDevin Teske return $FAILURE 897323adacSDevin Teske fi 907323adacSDevin Teske fi 917323adacSDevin Teske 927323adacSDevin Teske local cp 937323adacSDevin Teske if f_getvar $VAR_IFCONFIG$dev cp; then 947323adacSDevin Teske # 957323adacSDevin Teske # If this interface isn't a DHCP one, bring it up. 967323adacSDevin Teske # If it is, then it's already up. 977323adacSDevin Teske # 987323adacSDevin Teske case "$cp" in 997323adacSDevin Teske *DHCP*) 1007323adacSDevin Teske f_dprintf "A DHCP interface. Should already be up." 1017323adacSDevin Teske ;; 1027323adacSDevin Teske *) 1037323adacSDevin Teske f_dprintf "Not a DHCP interface." 1047323adacSDevin Teske if ! f_quietly ifconfig "$dev" $cp; then 1057323adacSDevin Teske f_show_msg "$msg_unable_to_configure_device" \ 1067323adacSDevin Teske "$dev" 1077323adacSDevin Teske return $FAILURE 1087323adacSDevin Teske fi 1097323adacSDevin Teske local rp 1107323adacSDevin Teske f_getvar $VAR_GATEWAY rp 1117323adacSDevin Teske if [ ! "$rp" ]; then 1127079fc4eSDevin Teske f_show_msg "$msg_no_gateway_has_been_set" 1137323adacSDevin Teske else 1147323adacSDevin Teske # 1157323adacSDevin Teske # Explicitly flush all routes to get back to a 1167323adacSDevin Teske # known sane state. We don't need to check this 1177323adacSDevin Teske # exit code because if anything fails it will 1187323adacSDevin Teske # show up in the route add below. 1197323adacSDevin Teske # 1207323adacSDevin Teske f_quietly route -n flush 1217323adacSDevin Teske f_dprintf "Adding default route to %s." "$rp" 1227323adacSDevin Teske if ! f_quietly route -n add default "$rp"; then 1237079fc4eSDevin Teske f_show_msg \ 1247323adacSDevin Teske "$msg_failed_to_add_default_route" 1257323adacSDevin Teske return $FAILURE 1267323adacSDevin Teske fi 1277323adacSDevin Teske fi 1287323adacSDevin Teske esac 1297323adacSDevin Teske elif ! { f_getvar $VAR_IPV6ADDR cp && [ "$cp" ]; }; then 1307323adacSDevin Teske f_show_msg "$msg_device_is_not_configured" "$dev" 1317323adacSDevin Teske return $FAILURE 1327323adacSDevin Teske fi 1337323adacSDevin Teske 1347323adacSDevin Teske f_dprintf "Network initialized successfully." 1357323adacSDevin Teske NETWORK_INITIALIZED=1 1367323adacSDevin Teske return $SUCCESS 1377323adacSDevin Teske} 1387323adacSDevin Teske 1397323adacSDevin Teske# f_media_shutdown_network $device 1407323adacSDevin Teske# 1417323adacSDevin Teske# Shuts down the configured network device (e.g., `fxp0', `em0', etc.) and 1427323adacSDevin Teske# deletes the default route (if configured). Returns failure if the device 1437323adacSDevin Teske# passed has not been configured. If not running as init (basically from the 1447323adacSDevin Teske# FreeBSD install media) then does nothing and returns success. 1457323adacSDevin Teske# 1467323adacSDevin Teskef_media_shutdown_network() 1477323adacSDevin Teske{ 1487323adacSDevin Teske local dev="$1" cp 1497323adacSDevin Teske 1507323adacSDevin Teske f_dprintf "Shutdown called for network device %s" "$dev" 1517323adacSDevin Teske if [ ! "$NETWORK_INITIALIZED" ]; then 1527323adacSDevin Teske f_dprintf "Network not initialized -- nothing to do." 1537323adacSDevin Teske return $SUCCESS 1547323adacSDevin Teske fi 1557323adacSDevin Teske 1567323adacSDevin Teske unset NETWORK_INITIALIZED 1577323adacSDevin Teske unset $VAR_NETWORK_DEVICE 1587323adacSDevin Teske 1597323adacSDevin Teske if ! f_running_as_init; then 1607323adacSDevin Teske f_dprintf "Not running as init -- calling the deed done." 1617323adacSDevin Teske return $SUCCESS 1627323adacSDevin Teske fi 1637323adacSDevin Teske 1647323adacSDevin Teske f_getvar $VAR_IFCONFIG$dev cp || return $FAILURE 1657323adacSDevin Teske f_dprintf "ifconfig %s down" "$dev" 1667323adacSDevin Teske f_quietly ifconfig $dev down || 1677323adacSDevin Teske f_show_msg "$msg_unable_to_down_the_interface_properly" "$dev" 1687323adacSDevin Teske 1697323adacSDevin Teske if f_getvar $VAR_GATEWAY cp; then 1707323adacSDevin Teske f_dprintf "Deleting default route." 1717323adacSDevin Teske f_quietly route -n delete default 1727323adacSDevin Teske fi 1737323adacSDevin Teske 1747323adacSDevin Teske return $SUCCESS 1757323adacSDevin Teske} 1767323adacSDevin Teske 1777323adacSDevin Teske############################################################ MAIN 1787323adacSDevin Teske 1797323adacSDevin Teskef_dprintf "%s: Successfully loaded." media/network.subr 1807323adacSDevin Teske 1817323adacSDevin Teskefi # ! $_MEDIA_NETWORK_SUBR 182