device.subr revision 260675
1if [ ! "$_NETWORKING_DEVICE_SUBR" ]; then _NETWORKING_DEVICE_SUBR=1 2# 3# Copyright (c) 2006-2013 Devin Teske 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: 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. 14# 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 25# SUCH DAMAGE. 26# 27# $FreeBSD: stable/10/usr.sbin/bsdconfig/networking/share/device.subr 260675 2014-01-15 07:36:34Z dteske $ 28# 29############################################################ INCLUDES 30 31BSDCFG_SHARE="/usr/share/bsdconfig" 32. $BSDCFG_SHARE/common.subr || exit 1 33f_dprintf "%s: loading includes..." networking/device.subr 34f_include $BSDCFG_SHARE/device.subr 35f_include $BSDCFG_SHARE/dialog.subr 36f_include $BSDCFG_SHARE/media/tcpip.subr 37f_include $BSDCFG_SHARE/networking/common.subr 38f_include $BSDCFG_SHARE/networking/ipaddr.subr 39f_include $BSDCFG_SHARE/networking/media.subr 40f_include $BSDCFG_SHARE/networking/netmask.subr 41f_include $BSDCFG_SHARE/networking/resolv.subr 42f_include $BSDCFG_SHARE/networking/routing.subr 43f_include $BSDCFG_SHARE/sysrc.subr 44 45BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking" 46f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr 47 48############################################################ GLOBALS 49 50# 51# Settings used while interacting with various dialog(1) menus 52# 53: ${DIALOG_MENU_NETDEV_KICK_INTERFACES=1} 54: ${DIALOG_MENU_NETDEV_SLEEP_AFTER_KICK=3} 55 56############################################################ FUNCTIONS 57 58# f_dialog_menu_netdev [$default] 59# 60# Display a list of network devices with descriptions. Optionally, if present 61# and non-NULL, initially highlight $default interface. 62# 63f_dialog_menu_netdev() 64{ 65 local menu_list # Calculated below 66 local defaultitem="${1%\*}" # Trim trailing asterisk if present 67 68 # 69 # Display a message to let the user know we're working... 70 # (message will remain until we throw up the next dialog) 71 # 72 f_dialog_info "$msg_probing_network_interfaces" 73 74 # 75 # Get list of usable network interfaces 76 # 77 local if iflist= # Calculated below 78 for if in $( ifconfig -l ); do 79 # Skip unsavory interfaces 80 case "$if" in 81 lo[0-9]*|ppp[0-9]*|sl[0-9]*|faith[0-9]*) continue ;; 82 esac 83 iflist="$iflist $if" 84 done 85 iflist="${iflist# }" 86 87 # 88 # Optionally kick interfaces in the head to get them to accurately 89 # track the carrier status in realtime (required on FreeBSD). 90 # 91 if [ "$DIALOG_MENU_NETDEV_KICK_INTERFACES" ]; then 92 DIALOG_MENU_NETDEV_KICK_INTERFACES= 93 94 local ifn 95 for ifn in $iflist; do 96 f_quietly ifconfig $ifn up 97 done 98 99 if [ "$DIALOG_MENU_NETDEV_SLEEP_AFTER_KICK" ]; then 100 # interfaces need time to update carrier status 101 sleep $DIALOG_MENU_NETDEV_SLEEP_AFTER_KICK 102 fi 103 fi 104 105 # 106 # Mark any "active" interfaces with an asterisk (*) 107 # to the right of the device name. 108 # 109 menu_list=$( 110 for ifn in $iflist; do 111 active=$( ifconfig $ifn 2> /dev/null | awk ' 112 ($1 == "status:") { 113 if ($2 == "active") { print 1; exit } 114 }' ) 115 printf "'%s%s' '%s'\n" \ 116 $ifn "${active:+*}" "$( f_device_desc $ifn )" 117 done 118 ) 119 if [ ! "$menu_list" ]; then 120 f_show_msg "$msg_no_network_interfaces" 121 return $DIALOG_CANCEL 122 fi 123 124 # 125 # Maybe the default item was marked as active 126 # 127 if [ "$defaultitem" ]; then 128 ifconfig "$defaultitem" 2> /dev/null | 129 awk '($1 == "status:" && $2 == "active"){exit 1}' || 130 defaultitem="$defaultitem*" 131 fi 132 133 local hline="$hline_arrows_tab_enter" 134 135 # 136 # Ask user to select an interface 137 # 138 local prompt="$msg_select_network_interface" 139 local height width rows 140 eval f_dialog_menu_size height width rows \ 141 \"\$DIALOG_TITLE\" \ 142 \"\$DIALOG_BACKTITLE\" \ 143 \"\$prompt\" \ 144 \"\$hline\" \ 145 $menu_list 146 local menu_choice 147 menu_choice=$( eval $DIALOG \ 148 --title \"\$DIALOG_TITLE\" \ 149 --backtitle \"\$DIALOG_BACKTITLE\" \ 150 --hline \"\$hline\" \ 151 --ok-label \"\$msg_ok\" \ 152 --cancel-label \"\$msg_cancel\" \ 153 --default-item \"\$defaultitem\" \ 154 --menu \"\$prompt\" \ 155 $height $width $rows \ 156 $menu_list \ 157 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 158 ) 159 local retval=$? 160 f_dialog_menutag_store -s "$menu_choice" 161 return $retval 162} 163 164# f_dialog_menu_netdev_edit $interface $ipaddr $netmask $options $dhcp 165# 166# Allow a user to edit network interface settings. Current values are not 167# probed but rather taken from the positional arguments. 168# 169f_dialog_menu_netdev_edit() 170{ 171 local interface="$1" ipaddr="$2" netmask="$3" options="$4" dhcp="$5" 172 local prompt menu_list height width rows 173 174 # 175 # Create a duplicate set of variables for change-tracking... 176 # 177 local ipaddr_orig="$2" \ 178 netmask_orig="$3" \ 179 options_orig="$4" \ 180 dhcp_orig="$5" 181 182 local hline="$hline_arrows_tab_enter" 183 prompt=$( printf "$msg_network_configuration" "$interface" ) 184 185 # 186 # Loop forever until the user has finished configuring the different 187 # components of the network interface. 188 # 189 # To apply the settings, we need to know each of the following: 190 # - IP Address 191 # - Network subnet mask 192 # - Additional ifconfig(8) options 193 # 194 # It is only when we have all of the above values that we can make the 195 # changes effective because all three options must be specified at-once 196 # to ifconfig(8). 197 # 198 local defaultitem= 199 while :; do 200 local dhcp_status="$msg_disabled" 201 [ "$dhcp" ] && dhcp_status="$msg_enabled" 202 203 # 204 # Display configuration-edit menu 205 # 206 menu_list=" 207 'X $msg_save_exit' '$msg_return_to_previous_menu' 208 '2 $msg_dhcp' '$dhcp_status' 209 '3 $msg_ipaddr4' '$ipaddr' 210 '4 $msg_netmask' '$netmask' 211 '5 $msg_options' '$options' 212 " 213 eval f_dialog_menu_size height width rows \ 214 \"\$DIALOG_TITLE\" \ 215 \"\$DIALOG_BACKTITLE\" \ 216 \"\$prompt\" \ 217 \"\$hline\" \ 218 $menu_list 219 local tag 220 tag=$( eval $DIALOG \ 221 --title \"\$DIALOG_TITLE\" \ 222 --backtitle \"\$DIALOG_BACKTITLE\" \ 223 --hline \"\$hline\" \ 224 --ok-label \"\$msg_ok\" \ 225 --cancel-label \"\$msg_cancel\" \ 226 --help-button \ 227 --help-label \"\$msg_help\" \ 228 ${USE_XDIALOG:+--help \"\"} \ 229 --default-item \"\$defaultitem\" \ 230 --menu \"\$prompt\" \ 231 $height $width $rows \ 232 $menu_list \ 233 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 234 ) 235 local retval=$? 236 f_dialog_data_sanitize tag 237 238 if [ $retval -eq $DIALOG_HELP ]; then 239 f_show_help "$TCP_HELPFILE" 240 continue 241 elif [ $retval -ne $DIALOG_OK ]; then 242 return $retval 243 else 244 # Only update default-item on success 245 defaultitem="$tag" 246 fi 247 248 # 249 # Call the below ``modifier functions'' whose job it is to take 250 # input from the user and assign the newly-acquired values back 251 # to the ipaddr, netmask, and options variables for us to re- 252 # read and display in the summary dialog. 253 # 254 case "$tag" in 255 X\ *) break ;; 256 2\ *) # 257 # Proceed cautiously (confirm with the user) if/when NFS- 258 # mounts are active. If the network on which these mounts 259 # are made is changed parts of the system may hang. 260 # 261 if f_nfs_mounted && ! f_jailed; then 262 local setting="$( printf "$msg_current_dhcp_status" \ 263 "$interface" "$dhcp_status" )" 264 f_noyes "$msg_nfs_mounts_may_cause_hang" "$setting" || 265 continue 266 fi 267 268 # 269 # Toggle DHCP status 270 # 271 if [ "$dhcp_status" = "$msg_enabled" ]; then 272 dhcp= 273 else 274 trap - SIGINT 275 ( # Execute within sub-shell to allow/catch Ctrl-C 276 trap 'exit $FAILURE' SIGINT 277 msg=$( printf "$msg_scanning_for_dhcp" "$interface" ) 278 if [ "$USE_XDIALOG" ]; then 279 ( 280 f_quietly ifconfig "$interface" delete 281 f_quietly dhclient "$interface" 282 ) | 283 f_xdialog_info "$msg" 284 else 285 f_dialog_info "$msg" 286 f_quietly ifconfig "$interface" delete 287 f_quietly dhclient "$interface" 288 fi 289 ) 290 retval=$? 291 trap 'interrupt' SIGINT 292 if [ $retval -eq $DIALOG_OK ]; then 293 dhcp=1 294 f_ifconfig_inet "$interface" ipaddr 295 f_ifconfig_inet6 "$interface" ipaddr6 296 f_ifconfig_netmask "$interface" netmask 297 options= 298 299 # Fixup search/domain in resolv.conf(5) 300 hostname=$( f_sysrc_get \ 301 'hostname:-$(hostname)' ) 302 f_dialog_resolv_conf_update "$hostname" 303 fi 304 fi 305 ;; 306 3\ *) f_dialog_input_ipaddr "$interface" "$ipaddr" 307 [ $? -eq $DIALOG_OK ] && dhcp= ;; 308 4\ *) f_dialog_input_netmask "$interface" "$netmask" 309 [ $? -eq $DIALOG_OK -a "$_netmask" ] && dhcp= ;; 310 5\ *) f_dialog_menu_media_options "$interface" "$options" 311 [ $? -eq $DIALOG_OK ] && dhcp= ;; 312 esac 313 done 314 315 # 316 # Save only if the user changed at least one feature of the interface 317 # 318 if [ "$ipaddr" != "$ipaddr_orig" -o \ 319 "$netmask" != "$netmask_orig" -o \ 320 "$options" != "$options_orig" -o \ 321 "$dhcp" != "$dhcp_orig" ] 322 then 323 f_show_info "$msg_saving_network_interface" "$interface" 324 325 local value= 326 if [ "$dhcp" ]; then 327 f_sysrc_delete defaultrouter 328 value=DHCP 329 else 330 value="inet $ipaddr netmask $netmask" 331 value="$value${options:+ }$options" 332 fi 333 334 f_sysrc_set ifconfig_$interface "$value" 335 fi 336 337 # 338 # Re/Apply the settings if desired 339 # 340 if [ ! "$dhcp" ]; then 341 if f_yesno "$msg_bring_interface_up" "$interface" 342 then 343 f_show_info "$msg_bring_interface_up" "$interface" 344 345 local dr="$( f_sysrc_get defaultrouter )" err 346 if [ "$dr" = "NO" -o ! "$dr" ]; then 347 dr=$( f_route_get_default ) 348 [ "$dr" ] && f_sysrc_set defaultrouter "$dr" 349 fi 350 # 351 # Make a backup of resolv.conf(5) before using 352 # ifconfig(8) and then restore it afterward. This 353 # allows preservation of nameservers acquired via 354 # DHCP on FreeBSD-8.x (normally lost as ifconfig(8) 355 # usage causes dhclient(8) to exit which scrubs 356 # resolv.conf(5) by-default upon termination). 357 # 358 f_quietly cp -fp "$RESOLV_CONF" "$RESOLV_CONF.$$" 359 err=$( ifconfig $interface inet $ipaddr \ 360 netmask $netmask $options 2>&1 ) 361 if [ $? -eq $SUCCESS ]; then 362 if [ "$dr" -a "$dr" != "NO" ]; then 363 err=$( route add default "$dr" 2>&1 ) 364 [ $? -eq $SUCCESS ] || \ 365 dialog_msgbox "$err" 366 fi 367 else 368 dialog_msgbox "$err" 369 fi 370 if cmp -s "$RESOLV_CONF" "$RESOLV_CONF.$$"; then 371 f_quietly rm -f "$RESOLV_CONF.$$" 372 else 373 f_quietly mv -f "$RESOLV_CONF.$$" "$RESOLV_CONF" 374 fi 375 fi 376 fi 377 378 return $DIALOG_OK 379} 380 381############################################################ MAIN 382 383f_dprintf "%s: Successfully loaded." networking/device.subr 384 385fi # ! $_NETWORKING_DEVICE_SUBR 386