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