1 if [ ! "$_NETWORKING_DEVICE_SUBR" ]; then _NETWORKING_DEVICE_SUBR=1
3 # Copyright (c) 2006-2015 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/strings.subr
44 f_include $BSDCFG_SHARE/sysrc.subr
46 BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
47 f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
49 ############################################################ GLOBALS
52 # Settings used while interacting with various dialog(1) menus
54 : ${DIALOG_MENU_NETDEV_KICK_INTERFACES=1}
55 : ${DIALOG_MENU_NETDEV_SLEEP_AFTER_KICK=3}
57 ############################################################ FUNCTIONS
59 # f_dialog_menu_netdev [$default]
61 # Display a list of network devices with descriptions. Optionally, if present
62 # and non-NULL, initially highlight $default interface.
64 f_dialog_menu_netdev()
66 local menu_list # Calculated below
67 local defaultitem="${1%\*}" # Trim trailing asterisk if present
70 # Display a message to let the user know we're working...
71 # (message will remain until we throw up the next dialog)
73 f_dialog_info "$msg_probing_network_interfaces"
76 # Get list of usable network interfaces
78 local dev devs if iflist= # Calculated below
79 f_device_rescan_network
80 f_device_find "" $DEVICE_TYPE_NETWORK devs
82 f_struct "$dev" get name if || continue
83 # Skip unsavory interfaces
85 lo[0-9]*|ppp[0-9]*|sl[0-9]*|faith[0-9]*) continue ;;
92 # Optionally kick interfaces in the head to get them to accurately
93 # track the carrier status in realtime (required on FreeBSD).
95 if [ "$DIALOG_MENU_NETDEV_KICK_INTERFACES" ]; then
96 DIALOG_MENU_NETDEV_KICK_INTERFACES=
99 f_quietly ifconfig $if up
102 if [ "$DIALOG_MENU_NETDEV_SLEEP_AFTER_KICK" ]; then
103 # interfaces need time to update carrier status
104 sleep $DIALOG_MENU_NETDEV_SLEEP_AFTER_KICK
109 # Mark any "active" interfaces with an asterisk (*)
110 # to the right of the device name.
113 for if in $iflist; do
114 f_device_desc $if $DEVICE_TYPE_NETWORK desc
115 f_shell_escape "$desc" desc
116 if f_device_is_active $if; then
117 printf "'%s\*' '%s'\n" $if "$desc"
119 printf "'%s' '%s'\n" $if "$desc"
123 if [ ! "$menu_list" ]; then
124 f_show_msg "$msg_no_network_interfaces"
125 return $DIALOG_CANCEL
128 # Maybe the default item was marked as active
129 f_device_is_active "$defaultitem" && defaultitem="$defaultitem*"
132 # Ask user to select an interface
134 local prompt="$msg_select_network_interface"
135 local hline="$hline_arrows_tab_enter"
136 local height width rows
137 eval f_dialog_menu_size height width rows \
139 \"\$DIALOG_BACKTITLE\" \
144 menu_choice=$( eval $DIALOG \
145 --title \"\$DIALOG_TITLE\" \
146 --backtitle \"\$DIALOG_BACKTITLE\" \
147 --hline \"\$hline\" \
148 --ok-label \"\$msg_ok\" \
149 --cancel-label \"\$msg_cancel\" \
150 --default-item \"\$defaultitem\" \
151 --menu \"\$prompt\" \
152 $height $width $rows \
154 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
157 f_dialog_menutag_store -s "$menu_choice"
161 # f_dialog_menu_netdev_edit $interface $ipaddr $netmask $options $dhcp
163 # Allow a user to edit network interface settings. Current values are not
164 # probed but rather taken from the positional arguments.
166 f_dialog_menu_netdev_edit()
168 local funcname=f_dialog_menu_netdev_edit
169 local interface="$1" ipaddr="$2" netmask="$3" options="$4" dhcp="$5"
170 local prompt menu_list height width rows
173 # Create a duplicate set of variables for change-tracking...
175 local ipaddr_orig="$2" \
180 local hline="$hline_arrows_tab_enter"
181 f_sprintf prompt "$msg_network_configuration" "$interface"
184 # Loop forever until the user has finished configuring the different
185 # components of the network interface.
187 # To apply the settings, we need to know each of the following:
189 # - Network subnet mask
190 # - Additional ifconfig(8) options
192 # It is only when we have all of the above values that we can make the
193 # changes effective because all three options must be specified at-once
198 local dhcp_status="$msg_disabled"
199 [ "$dhcp" ] && dhcp_status="$msg_enabled"
202 # Display configuration-edit menu
205 'X $msg_save_exit' '$msg_return_to_previous_menu'
206 '2 $msg_dhcp' '$dhcp_status'
207 '3 $msg_ipaddr4' '$ipaddr'
208 '4 $msg_netmask' '$netmask'
209 '5 $msg_options' '$options'
211 eval f_dialog_menu_size height width rows \
213 \"\$DIALOG_BACKTITLE\" \
218 tag=$( eval $DIALOG \
219 --title \"\$DIALOG_TITLE\" \
220 --backtitle \"\$DIALOG_BACKTITLE\" \
221 --hline \"\$hline\" \
222 --ok-label \"\$msg_ok\" \
223 --cancel-label \"\$msg_cancel\" \
225 --help-label \"\$msg_help\" \
226 ${USE_XDIALOG:+--help \"\"} \
227 --default-item \"\$defaultitem\" \
228 --menu \"\$prompt\" \
229 $height $width $rows \
231 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
234 f_dialog_data_sanitize tag
236 if [ $retval -eq $DIALOG_HELP ]; then
237 f_show_help "$TCP_HELPFILE"
239 elif [ $retval -ne $DIALOG_OK ]; then
242 # Only update default-item on success
247 # Call the below ``modifier functions'' whose job it is to take
248 # input from the user and assign the newly-acquired values back
249 # to the ipaddr, netmask, and options variables for us to re-
250 # read and display in the summary dialog.
255 # Proceed cautiously (confirm with the user) if/when NFS-
256 # mounts are active. If the network on which these mounts
257 # are made is changed parts of the system may hang.
259 if f_nfs_mounted && ! f_jailed; then
261 f_sprintf setting "$msg_current_dhcp_status" \
262 "$interface" "$dhcp_status"
263 f_noyes "$msg_nfs_mounts_may_cause_hang" "$setting" ||
270 if [ "$dhcp_status" = "$msg_enabled" ]; then
274 ( # Execute within sub-shell to allow/catch Ctrl-C
275 trap 'exit $FAILURE' SIGINT
276 f_sprintf msg "$msg_scanning_for_dhcp" "$interface"
277 if [ "$USE_XDIALOG" ]; then
279 f_quietly ifconfig "$interface" delete
280 f_quietly dhclient "$interface"
282 f_xdialog_info "$msg"
285 f_quietly ifconfig "$interface" delete
286 f_quietly dhclient "$interface"
290 trap 'interrupt' SIGINT
291 if [ $retval -eq $DIALOG_OK ]; then
293 f_ifconfig_inet "$interface" ipaddr
294 f_ifconfig_inet6 "$interface" ipaddr6
295 f_ifconfig_netmask "$interface" netmask
298 # Fixup search/domain in resolv.conf(5)
299 hostname=$( f_sysrc_get \
300 'hostname:-$(hostname)' )
301 f_dialog_resolv_conf_update "$hostname"
305 3\ *) f_dialog_input_ipaddr "$interface" "$ipaddr"
306 [ $? -eq $DIALOG_OK ] && dhcp= ;;
307 4\ *) f_dialog_input_netmask "$interface" "$netmask"
308 [ $? -eq $DIALOG_OK -a "$_netmask" ] && dhcp= ;;
309 5\ *) f_dialog_menu_media_options "$interface" "$options"
310 [ $? -eq $DIALOG_OK ] && dhcp= ;;
315 # Save only if the user changed at least one feature of the interface
317 if [ "$ipaddr" != "$ipaddr_orig" -o \
318 "$netmask" != "$netmask_orig" -o \
319 "$options" != "$options_orig" -o \
320 "$dhcp" != "$dhcp_orig" ]
322 f_show_info "$msg_saving_network_interface" "$interface"
326 f_eval_catch $funcname f_sysrc_delete \
327 'f_sysrc_delete defaultrouter'
330 value="inet $ipaddr netmask $netmask"
331 value="$value${options:+ }$options"
334 f_eval_catch $funcname f_sysrc_set \
335 'f_sysrc_set "ifconfig_%s" "%s"' "$interface" "$value"
339 # Re/Apply the settings if desired
341 if [ ! "$dhcp" ]; then
342 if f_yesno "$msg_bring_interface_up" "$interface"
344 f_show_info "$msg_bring_interface_up" "$interface"
346 local dr="$( f_sysrc_get defaultrouter )"
347 if [ "$dr" = "NO" -o ! "$dr" ]; then
348 f_route_get_default dr
349 [ "$dr" ] && f_eval_catch \
350 $funcname f_sysrc_set \
351 'f_sysrc_set defaultrouter "%s"' "$dr"
354 # Make a backup of resolv.conf(5) before using
355 # ifconfig(8) and then restore it afterward. This
356 # allows preservation of nameservers acquired via
357 # DHCP on FreeBSD-8.x (normally lost as ifconfig(8)
358 # usage causes dhclient(8) to exit which scrubs
359 # resolv.conf(5) by-default upon termination).
361 f_quietly cp -fp "$RESOLV_CONF" "$RESOLV_CONF.$$"
362 if f_eval_catch $funcname ifconfig \
363 'ifconfig "%s" inet "%s" netmask "%s" %s' \
364 "$interface" "$ipaddr" "$netmask" "$options"
366 [ "$dr" -a "$dr" != "NO" ] &&
367 f_eval_catch $funcname route \
368 'route add default "%s"' "$dr"
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