1 if [ ! "$_NETWORKING_DEVICE_SUBR" ]; then _NETWORKING_DEVICE_SUBR=1
3 # Copyright (c) 2006-2013 Devin Teske
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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
29 ############################################################ INCLUDES
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." networking/device.subr
34 f_include $BSDCFG_SHARE/device.subr
35 f_include $BSDCFG_SHARE/dialog.subr
36 f_include $BSDCFG_SHARE/media/tcpip.subr
37 f_include $BSDCFG_SHARE/networking/common.subr
38 f_include $BSDCFG_SHARE/networking/ipaddr.subr
39 f_include $BSDCFG_SHARE/networking/media.subr
40 f_include $BSDCFG_SHARE/networking/netmask.subr
41 f_include $BSDCFG_SHARE/networking/resolv.subr
42 f_include $BSDCFG_SHARE/networking/routing.subr
43 f_include $BSDCFG_SHARE/sysrc.subr
45 BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
46 f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
48 ############################################################ GLOBALS
51 # Settings used while interacting with various dialog(1) menus
53 : ${DIALOG_MENU_NETDEV_KICK_INTERFACES=1}
54 : ${DIALOG_MENU_NETDEV_SLEEP_AFTER_KICK=3}
56 ############################################################ FUNCTIONS
58 # f_dialog_menu_netdev [$default]
60 # Display a list of network devices with descriptions. Optionally, if present
61 # and non-NULL, initially highlight $default interface.
63 f_dialog_menu_netdev()
65 local menu_list # Calculated below
66 local defaultitem="${1%\*}" # Trim trailing asterisk if present
69 # Display a message to let the user know we're working...
70 # (message will remain until we throw up the next dialog)
72 f_dialog_info "$msg_probing_network_interfaces"
75 # Get list of usable network interfaces
77 local if iflist= # Calculated below
78 for if in $( ifconfig -l ); do
79 # Skip unsavory interfaces
81 lo[0-9]*|ppp[0-9]*|sl[0-9]*|faith[0-9]*) continue ;;
88 # Optionally kick interfaces in the head to get them to accurately
89 # track the carrier status in realtime (required on FreeBSD).
91 if [ "$DIALOG_MENU_NETDEV_KICK_INTERFACES" ]; then
92 DIALOG_MENU_NETDEV_KICK_INTERFACES=
95 for ifn in $iflist; do
96 f_quietly ifconfig $ifn up
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
106 # Mark any "active" interfaces with an asterisk (*)
107 # to the right of the device name.
110 for ifn in $iflist; do
111 active=$( ifconfig $ifn 2> /dev/null | awk '
113 if ($2 == "active") { print 1; exit }
115 printf "'%s%s' '%s'\n" \
116 $ifn "${active:+*}" "$( f_device_desc $ifn )"
119 if [ ! "$menu_list" ]; then
120 f_show_msg "$msg_no_network_interfaces"
121 return $DIALOG_CANCEL
125 # Maybe the default item was marked as active
127 if [ "$defaultitem" ]; then
128 ifconfig "$defaultitem" 2> /dev/null |
129 awk '($1 == "status:" && $2 == "active"){exit 1}' ||
130 defaultitem="$defaultitem*"
133 local hline="$hline_arrows_tab_enter"
136 # Ask user to select an interface
138 local prompt="$msg_select_network_interface"
139 local height width rows
140 eval f_dialog_menu_size height width rows \
142 \"\$DIALOG_BACKTITLE\" \
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 \
157 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
160 f_dialog_menutag_store -s "$menu_choice"
164 # f_dialog_menu_netdev_edit $interface $ipaddr $netmask $options $dhcp
166 # Allow a user to edit network interface settings. Current values are not
167 # probed but rather taken from the positional arguments.
169 f_dialog_menu_netdev_edit()
171 local interface="$1" ipaddr="$2" netmask="$3" options="$4" dhcp="$5"
172 local prompt menu_list height width rows
175 # Create a duplicate set of variables for change-tracking...
177 local ipaddr_orig="$2" \
182 local hline="$hline_arrows_tab_enter"
183 prompt=$( printf "$msg_network_configuration" "$interface" )
186 # Loop forever until the user has finished configuring the different
187 # components of the network interface.
189 # To apply the settings, we need to know each of the following:
191 # - Network subnet mask
192 # - Additional ifconfig(8) options
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
200 local dhcp_status="$msg_disabled"
201 [ "$dhcp" ] && dhcp_status="$msg_enabled"
204 # Display configuration-edit menu
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'
213 eval f_dialog_menu_size height width rows \
215 \"\$DIALOG_BACKTITLE\" \
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\" \
227 --help-label \"\$msg_help\" \
228 ${USE_XDIALOG:+--help \"\"} \
229 --default-item \"\$defaultitem\" \
230 --menu \"\$prompt\" \
231 $height $width $rows \
233 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
236 f_dialog_data_sanitize tag
238 if [ $retval -eq $DIALOG_HELP ]; then
239 f_show_help "$TCP_HELPFILE"
241 elif [ $retval -ne $DIALOG_OK ]; then
244 # Only update default-item on success
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.
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.
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" ||
271 if [ "$dhcp_status" = "$msg_enabled" ]; then
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
280 f_quietly ifconfig "$interface" delete
281 f_quietly dhclient "$interface"
283 f_xdialog_info "$msg"
286 f_quietly ifconfig "$interface" delete
287 f_quietly dhclient "$interface"
291 trap 'interrupt' SIGINT
292 if [ $retval -eq $DIALOG_OK ]; then
294 f_ifconfig_inet "$interface" ipaddr
295 f_ifconfig_inet6 "$interface" ipaddr6
296 f_ifconfig_netmask "$interface" netmask
299 # Fixup search/domain in resolv.conf(5)
300 hostname=$( f_sysrc_get \
301 'hostname:-$(hostname)' )
302 f_dialog_resolv_conf_update "$hostname"
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= ;;
316 # Save only if the user changed at least one feature of the interface
318 if [ "$ipaddr" != "$ipaddr_orig" -o \
319 "$netmask" != "$netmask_orig" -o \
320 "$options" != "$options_orig" -o \
321 "$dhcp" != "$dhcp_orig" ]
323 f_show_info "$msg_saving_network_interface" "$interface"
327 f_sysrc_delete defaultrouter
330 value="inet $ipaddr netmask $netmask"
331 value="$value${options:+ }$options"
334 f_sysrc_set ifconfig_$interface "$value"
338 # Re/Apply the settings if desired
340 if [ ! "$dhcp" ]; then
341 if f_yesno "$msg_bring_interface_up" "$interface"
343 f_show_info "$msg_bring_interface_up" "$interface"
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"
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).
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 ] || \
370 if cmp -s "$RESOLV_CONF" "$RESOLV_CONF.$$"; then
371 f_quietly rm -f "$RESOLV_CONF.$$"
373 f_quietly mv -f "$RESOLV_CONF.$$" "$RESOLV_CONF"
381 ############################################################ MAIN
383 f_dprintf "%s: Successfully loaded." networking/device.subr
385 fi # ! $_NETWORKING_DEVICE_SUBR