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 (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
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/sysrc.subr
37 f_include $BSDCFG_SHARE/media/tcpip.subr
38 f_include $BSDCFG_SHARE/networking/common.subr
39 f_include $BSDCFG_SHARE/networking/ipaddr.subr
40 f_include $BSDCFG_SHARE/networking/media.subr
41 f_include $BSDCFG_SHARE/networking/netmask.subr
42 f_include $BSDCFG_SHARE/networking/resolv.subr
43 f_include $BSDCFG_SHARE/networking/routing.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
60 # Display a list of network devices with descriptions.
62 f_dialog_menu_netdev()
65 # Display a message to let the user know we're working...
66 # (message will remain until we throw up the next dialog)
68 f_dialog_info "$msg_probing_network_interfaces"
71 # Get list of usable network interfaces
73 local d='[[:digit:]]+:'
74 local iflist="`echo "$(ifconfig -l):" | sed -E -e "
75 # Convert all spaces to colons
78 # Prune unsavory interfaces
84 # Convert all colons back into spaces
89 # Optionally kick interfaces in the head to get them to accurately
90 # track the carrier status in realtime (required on FreeBSD).
92 if [ "$DIALOG_MENU_NETDEV_KICK_INTERFACES" ]; then
93 DIALOG_MENU_NETDEV_KICK_INTERFACES=
96 for ifn in $iflist; do
97 f_quietly ifconfig $ifn up
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
107 # Mark any "active" interfaces with an asterisk (*)
108 # to the right of the device name.
111 for ifn in $iflist; do
112 active=$( ifconfig $ifn | awk \
114 ( $1 == "status:" ) \
116 if ( $2 == "active" ) { print 1; exit }
119 printf "'%s%s' '%s'\n" \
120 $ifn "${active:+*}" "$( f_device_desc $ifn )"
123 if [ ! "$interfaces" ]; then
124 f_dialog_msgbox "$msg_no_network_interfaces"
128 local hline="$hline_arrows_tab_enter"
131 # Ask user to select an interface
134 prompt="$msg_select_network_interface"
135 size=$( eval f_dialog_menu_size \
137 \"\$DIALOG_BACKTITLE\" \
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 \
150 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
153 setvar DIALOG_MENU_$$ "$dialog_menu"
157 # f_dialog_menu_netdev_edit $interface $ipaddr $netmask $options $dhcp
159 # Allow a user to edit network interface settings. Current values are not
160 # probed but rather taken from the positional arguments.
162 f_dialog_menu_netdev_edit()
164 local interface="$1" ipaddr="$2" netmask="$3" options="$4" dhcp="$5"
165 local prompt menu_list size
168 # Create a duplicate set of variables for change-tracking...
170 local ipaddr_orig="$2" \
175 local hline="$hline_arrows_tab_enter"
176 prompt=$( printf "$msg_network_configuration" "$interface" )
179 # Loop forever until the user has finished configuring the different
180 # components of the network interface.
182 # To apply the settings, we need to know each of the following:
184 # - Network subnet mask
185 # - Additional ifconfig(8) options
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
192 local dhcp_status="$msg_disabled"
193 [ "$dhcp" ] && dhcp_status="$msg_enabled"
196 # Display configuration-edit menu
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'
205 size=$( eval f_dialog_menu_size \
207 \"\$DIALOG_BACKTITLE\" \
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\" \
219 --help-label \"\$msg_help\" \
220 ${USE_XDIALOG:+--help \"\"} \
221 --menu \"\$prompt\" $size \
223 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
227 setvar DIALOG_MENU_$$ "$dialog_menu"
228 local tag="$( f_dialog_menutag )"
230 if [ $retval -eq 2 ]; then
231 # The Help button was pressed
232 f_show_help "$TCP_HELPFILE"
234 elif [ $retval -ne $SUCCESS ]; then
235 # "Cancel" was chosen (-1) or ESC was pressed (255)
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.
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).
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"
263 if [ "$dhcp_status" = "$msg_enabled" ]; then
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
272 f_quietly ifconfig $interface delete
273 f_quietly dhclient $interface
275 f_xdialog_info "$msg"
278 f_quietly ifconfig $interface delete
279 f_quietly dhclient $interface
283 trap 'interrupt' SIGINT
284 if [ $retval -eq $SUCCESS ]; then
286 ipaddr=$( f_ifconfig_inet $interface )
287 netmask=$( f_ifconfig_netmask $interface )
290 # Fixup search/domain in resolv.conf(5)
291 hostname=$( f_sysrc_get \
292 'hostname:-$(hostname)' )
293 f_dialog_resolv_conf_update "$hostname"
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=;;
307 # Save only if the user changed at least one feature of the interface
309 if [ "$ipaddr" != "$ipaddr_orig" -o \
310 "$netmask" != "$netmask_orig" -o \
311 "$options" != "$options_orig" -o \
312 "$dhcp" != "$dhcp_orig" ]
314 f_show_info "$msg_saving_network_interface" "$interface"
318 f_sysrc_delete defaultrouter
321 value="inet $ipaddr netmask $netmask"
322 value="$value${options:+ }$options"
325 f_sysrc_set ifconfig_$interface "$value"
329 # Re/Apply the settings if desired
331 if [ ! "$dhcp" ]; then
332 if f_yesno "$msg_bring_interface_up" "$interface"
334 f_show_info "$msg_bring_interface_up" "$interface"
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"
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).
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 ] || \
361 if cmp -s "$RESOLV_CONF" "$RESOLV_CONF.$$"; then
362 f_quietly rm -f "$RESOLV_CONF.$$"
364 f_quietly mv -f "$RESOLV_CONF.$$" "$RESOLV_CONF"
372 ############################################################ MAIN
374 f_dprintf "%s: Successfully loaded." networking/device.subr
376 fi # ! $_NETWORKING_DEVICE_SUBR