1 if [ ! "$_NETWORKING_DEVICE_SUBR" ]; then _NETWORKING_DEVICE_SUBR=1
3 # Copyright (c) 2006-2016 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/media/wlan.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
44 f_include $BSDCFG_SHARE/strings.subr
45 f_include $BSDCFG_SHARE/sysrc.subr
47 BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
48 f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
50 ############################################################ GLOBALS
53 # Settings used while interacting with various dialog(1) menus
55 : ${DIALOG_MENU_NETDEV_KICK_INTERFACES=1}
56 : ${DIALOG_MENU_NETDEV_SLEEP_AFTER_KICK=3}
58 ############################################################ FUNCTIONS
60 # f_dialog_menu_netdev [$default]
62 # Display a list of network devices with descriptions. Optionally, if present
63 # and non-NULL, initially highlight $default interface.
65 f_dialog_menu_netdev()
67 local menu_list # Calculated below
68 local defaultitem="${1%\*}" # Trim trailing asterisk if present
71 # Display a message to let the user know we're working...
72 # (message will remain until we throw up the next dialog)
74 f_dialog_info "$msg_probing_network_interfaces"
77 # Get list of usable network interfaces
79 local dev devs if iflist= # Calculated below
80 f_device_rescan_network
81 f_device_find "" $DEVICE_TYPE_NETWORK devs
83 f_struct "$dev" get name if || continue
84 # Skip unsavory interfaces
86 lo[0-9]*|ppp[0-9]*|sl[0-9]*) continue ;;
93 # Optionally kick interfaces in the head to get them to accurately
94 # track the carrier status in realtime (required on FreeBSD).
96 if [ "$DIALOG_MENU_NETDEV_KICK_INTERFACES" ]; then
97 DIALOG_MENU_NETDEV_KICK_INTERFACES=
100 f_quietly ifconfig $if up
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
110 # Mark any "active" interfaces with an asterisk (*)
111 # to the right of the device name.
114 for if in $iflist; do
115 f_device_desc $if $DEVICE_TYPE_NETWORK desc
116 f_shell_escape "$desc" desc
117 if f_device_is_active $if; then
118 printf "'%s\*' '%s'\n" $if "$desc"
120 printf "'%s' '%s'\n" $if "$desc"
124 if [ ! "$menu_list" ]; then
125 f_show_msg "$msg_no_network_interfaces"
126 return $DIALOG_CANCEL
129 # Maybe the default item was marked as active
130 f_device_is_active "$defaultitem" && defaultitem="$defaultitem*"
133 # Ask user to select an interface
135 local prompt="$msg_select_network_interface"
136 local hline="$hline_arrows_tab_enter"
137 local height width rows
138 eval f_dialog_menu_size height width rows \
140 \"\$DIALOG_BACKTITLE\" \
145 menu_choice=$( eval $DIALOG \
146 --title \"\$DIALOG_TITLE\" \
147 --backtitle \"\$DIALOG_BACKTITLE\" \
148 --hline \"\$hline\" \
149 --ok-label \"\$msg_ok\" \
150 --cancel-label \"\$msg_cancel\" \
151 --default-item \"\$defaultitem\" \
152 --menu \"\$prompt\" \
153 $height $width $rows \
155 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
158 f_dialog_menutag_store -s "$menu_choice"
162 # f_dialog_menu_netdev_edit $interface $ipaddr $netmask $options $dhcp
164 # Allow a user to edit network interface settings. Current values are not
165 # probed but rather taken from the positional arguments.
167 f_dialog_menu_netdev_edit()
169 local funcname=f_dialog_menu_netdev_edit
170 local interface="$1" ipaddr="$2" netmask="$3" options="$4" dhcp="$5"
171 local prompt menu_list height width rows
174 # Create a duplicate set of variables for change-tracking...
176 local ipaddr_orig="$2" \
181 local hline="$hline_arrows_tab_enter"
182 f_sprintf prompt "$msg_network_configuration" "$interface"
185 # Loop forever until the user has finished configuring the different
186 # components of the network interface.
188 # To apply the settings, we need to know each of the following:
190 # - Network subnet mask
191 # - Additional ifconfig(8) options
193 # It is only when we have all of the above values that we can make the
194 # changes effective because all three options must be specified at-once
198 local wlans wlan_status
200 local dhcp_status="$msg_disabled"
201 [ "$dhcp" ] && dhcp_status="$msg_enabled"
203 if f_device_is_wireless "$interface"; then
204 wlans=$( f_sysrc_get "wlans_$interface" )
205 wlan_status="$msg_unconfigured"
206 [ -e "$( f_sysrc_get wpa_supplicant_conf_file )" ] &&
207 wlan_status="$msg_configured"
211 # Display configuration-edit menu
214 'X $msg_save_exit' '$msg_return_to_previous_menu'
216 f_device_is_wireless "$interface" && menu_list="$menu_list
217 'W $msg_wireless_networks' '$wlan_status'
218 '1 $msg_wlans' '$wlans'
220 menu_list="$menu_list
221 '2 $msg_dhcp' '$dhcp_status'
222 '3 $msg_ipaddr4' '$ipaddr'
223 '4 $msg_netmask' '$netmask'
224 '5 $msg_options' '$options'
226 eval f_dialog_menu_size height width rows \
228 \"\$DIALOG_BACKTITLE\" \
233 tag=$( eval $DIALOG \
234 --title \"\$DIALOG_TITLE\" \
235 --backtitle \"\$DIALOG_BACKTITLE\" \
236 --hline \"\$hline\" \
237 --ok-label \"\$msg_ok\" \
238 --cancel-label \"\$msg_cancel\" \
240 --help-label \"\$msg_help\" \
241 ${USE_XDIALOG:+--help \"\"} \
242 --default-item \"\$defaultitem\" \
243 --menu \"\$prompt\" \
244 $height $width $rows \
246 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
249 f_dialog_data_sanitize tag
251 if [ $retval -eq $DIALOG_HELP ]; then
252 f_show_help "$TCP_HELPFILE"
254 elif [ $retval -ne $DIALOG_OK ]; then
257 # Only update default-item on success
262 # Call the below ``modifier functions'' whose job it is to take
263 # input from the user and assign the newly-acquired values back
264 # to the ipaddr, netmask, and options variables for us to re-
265 # read and display in the summary dialog.
269 W\ *) f_dialog_menu_wireless_edit ;;
270 1\ *) f_dialog_menu_wlandev_edit \
271 "$interface" "${wlans%%[$IFS]*}" ;;
273 # Proceed cautiously (confirm with the user) if/when NFS-
274 # mounts are active. If the network on which these mounts
275 # are made is changed parts of the system may hang.
277 if f_nfs_mounted && ! f_jailed; then
279 f_sprintf setting "$msg_current_dhcp_status" \
280 "$interface" "$dhcp_status"
281 f_noyes "$msg_nfs_mounts_may_cause_hang" "$setting" ||
288 if [ "$dhcp_status" = "$msg_enabled" ]; then
292 ( # Execute within sub-shell to allow/catch Ctrl-C
293 trap 'exit $FAILURE' SIGINT
294 f_sprintf msg "$msg_scanning_for_dhcp" "$interface"
295 if [ "$USE_XDIALOG" ]; then
297 f_quietly ifconfig "$interface" delete
298 f_quietly dhclient "$interface"
300 f_xdialog_info "$msg"
303 f_quietly ifconfig "$interface" delete
304 f_quietly dhclient "$interface"
308 trap 'interrupt' SIGINT
309 if [ $retval -eq $DIALOG_OK ]; then
311 f_ifconfig_inet "$interface" ipaddr
312 f_ifconfig_inet6 "$interface" ipaddr6
313 f_ifconfig_netmask "$interface" netmask
316 # Fixup search/domain in resolv.conf(5)
317 hostname=$( f_sysrc_get \
318 'hostname:-$(hostname)' )
319 f_dialog_resolv_conf_update "$hostname"
323 3\ *) f_dialog_input_ipaddr "$interface" "$ipaddr"
324 [ $? -eq $DIALOG_OK ] && dhcp= ;;
325 4\ *) f_dialog_input_netmask "$interface" "$netmask"
326 [ $? -eq $DIALOG_OK -a "$_netmask" ] && dhcp= ;;
327 5\ *) f_dialog_menu_media_options "$interface" "$options"
328 [ $? -eq $DIALOG_OK ] && dhcp= ;;
333 # Save only if the user changed at least one feature of the interface
335 if [ "$ipaddr" != "$ipaddr_orig" -o \
336 "$netmask" != "$netmask_orig" -o \
337 "$options" != "$options_orig" -o \
338 "$dhcp" != "$dhcp_orig" ]
340 f_show_info "$msg_saving_network_interface" "$interface"
344 f_eval_catch $funcname f_sysrc_delete \
345 'f_sysrc_delete defaultrouter'
348 value="inet $ipaddr netmask $netmask"
349 value="$value${options:+ }$options"
352 f_eval_catch $funcname f_sysrc_set \
353 'f_sysrc_set "ifconfig_%s" "%s"' "$interface" "$value"
357 # Re/Apply the settings if desired
359 if [ ! "$dhcp" ]; then
360 if f_yesno "$msg_bring_interface_up" "$interface"
362 f_show_info "$msg_bring_interface_up" "$interface"
364 local dr="$( f_sysrc_get defaultrouter )"
365 if [ "$dr" = "NO" -o ! "$dr" ]; then
366 f_route_get_default dr
367 [ "$dr" ] && f_eval_catch \
368 $funcname f_sysrc_set \
369 'f_sysrc_set defaultrouter "%s"' "$dr"
372 # Make a backup of resolv.conf(5) before using
373 # ifconfig(8) and then restore it afterward. This
374 # allows preservation of nameservers acquired via
375 # DHCP on FreeBSD-8.x (normally lost as ifconfig(8)
376 # usage causes dhclient(8) to exit which scrubs
377 # resolv.conf(5) by-default upon termination).
379 f_quietly cp -fp "$RESOLV_CONF" "$RESOLV_CONF.$$"
380 if f_eval_catch $funcname ifconfig \
381 'ifconfig "%s" inet "%s" netmask "%s" %s' \
382 "$interface" "$ipaddr" "$netmask" "$options"
384 [ "$dr" -a "$dr" != "NO" ] &&
385 f_eval_catch $funcname route \
386 'route add default "%s"' "$dr"
388 if cmp -s "$RESOLV_CONF" "$RESOLV_CONF.$$"; then
389 f_quietly rm -f "$RESOLV_CONF.$$"
391 f_quietly mv -f "$RESOLV_CONF.$$" "$RESOLV_CONF"
399 ############################################################ MAIN
401 f_dprintf "%s: Successfully loaded." networking/device.subr
403 fi # ! $_NETWORKING_DEVICE_SUBR