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