xref: /freebsd/usr.sbin/bsdconfig/networking/share/device.subr (revision f6a3b357e9be4c6423c85eff9a847163a0d307c8)
1if [ ! "$_NETWORKING_DEVICE_SUBR" ]; then _NETWORKING_DEVICE_SUBR=1
2#
3# Copyright (c) 2006-2016 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 (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
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/media/tcpip.subr
37f_include $BSDCFG_SHARE/media/wlan.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
44f_include $BSDCFG_SHARE/strings.subr
45f_include $BSDCFG_SHARE/sysrc.subr
46
47BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
48f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
49
50############################################################ GLOBALS
51
52#
53# Settings used while interacting with various dialog(1) menus
54#
55: ${DIALOG_MENU_NETDEV_KICK_INTERFACES=1}
56: ${DIALOG_MENU_NETDEV_SLEEP_AFTER_KICK=3}
57
58############################################################ FUNCTIONS
59
60# f_dialog_menu_netdev [$default]
61#
62# Display a list of network devices with descriptions. Optionally, if present
63# and non-NULL, initially highlight $default interface.
64#
65f_dialog_menu_netdev()
66{
67	local menu_list # Calculated below
68	local defaultitem="${1%\*}" # Trim trailing asterisk if present
69
70	#
71	# Display a message to let the user know we're working...
72	# (message will remain until we throw up the next dialog)
73	#
74	f_dialog_info "$msg_probing_network_interfaces"
75
76	#
77	# Get list of usable network interfaces
78	#
79	local dev devs if iflist= # Calculated below
80	f_device_rescan_network
81	f_device_find "" $DEVICE_TYPE_NETWORK devs
82	for dev in $devs; do
83		f_struct "$dev" get name if || continue
84		# Skip unsavory interfaces
85		case "$if" in
86		lo[0-9]*|ppp[0-9]*|sl[0-9]*) continue ;;
87		esac
88		iflist="$iflist $if"
89	done
90	iflist="${iflist# }"
91
92	#
93	# Optionally kick interfaces in the head to get them to accurately
94	# track the carrier status in realtime (required on FreeBSD).
95	#
96	if [ "$DIALOG_MENU_NETDEV_KICK_INTERFACES" ]; then
97		DIALOG_MENU_NETDEV_KICK_INTERFACES=
98
99		for if in $iflist; do
100			f_quietly ifconfig $if up
101		done
102
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
106		fi
107	fi
108
109	#
110	# Mark any "active" interfaces with an asterisk (*)
111	# to the right of the device name.
112	#
113	menu_list=$(
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"
119			else
120				printf "'%s' '%s'\n" $if "$desc"
121			fi
122		done
123	)
124	if [ ! "$menu_list" ]; then
125		f_show_msg "$msg_no_network_interfaces"
126		return $DIALOG_CANCEL
127	fi
128
129	# Maybe the default item was marked as active
130	f_device_is_active "$defaultitem" && defaultitem="$defaultitem*"
131
132	#
133	# Ask user to select an interface
134	#
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 \
139	                        \"\$DIALOG_TITLE\"     \
140	                        \"\$DIALOG_BACKTITLE\" \
141	                        \"\$prompt\"           \
142	                        \"\$hline\"            \
143	                        $menu_list
144	local menu_choice
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               \
154		$menu_list                         \
155		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
156	)
157	local retval=$?
158	f_dialog_menutag_store -s "$menu_choice"
159	return $retval
160}
161
162# f_dialog_menu_netdev_edit $interface $ipaddr $netmask $options $dhcp
163#
164# Allow a user to edit network interface settings. Current values are not
165# probed but rather taken from the positional arguments.
166#
167f_dialog_menu_netdev_edit()
168{
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
172
173	#
174	# Create a duplicate set of variables for change-tracking...
175	#
176	local ipaddr_orig="$2"  \
177	      netmask_orig="$3" \
178	      options_orig="$4" \
179	      dhcp_orig="$5"
180
181	local hline="$hline_arrows_tab_enter"
182	f_sprintf prompt "$msg_network_configuration" "$interface"
183
184	#
185	# Loop forever until the user has finished configuring the different
186	# components of the network interface.
187	#
188	# To apply the settings, we need to know each of the following:
189	# 	- IP Address
190	# 	- Network subnet mask
191	# 	- Additional ifconfig(8) options
192	#
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
195	# to ifconfig(8).
196	#
197	local defaultitem=
198	local wlans wlan_status
199	while :; do
200		local dhcp_status="$msg_disabled"
201		[ "$dhcp" ] && dhcp_status="$msg_enabled"
202
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"
208		fi
209
210		#
211		# Display configuration-edit menu
212		#
213		menu_list="
214			'X $msg_save_exit' '$msg_return_to_previous_menu'
215		" # END-QUOTE
216		f_device_is_wireless "$interface" && menu_list="$menu_list
217			'W $msg_wireless_networks' '$wlan_status'
218			'1 $msg_wlans'             '$wlans'
219		" # END-QUOTE
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'
225		" # END-QUOTE
226		eval f_dialog_menu_size height width rows \
227		                        \"\$DIALOG_TITLE\"     \
228		                        \"\$DIALOG_BACKTITLE\" \
229		                        \"\$prompt\"           \
230		                        \"\$hline\"            \
231		                        $menu_list
232		local tag
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\"    \
239			--help-button                      \
240			--help-label \"\$msg_help\"        \
241			${USE_XDIALOG:+--help \"\"}        \
242			--default-item \"\$defaultitem\"   \
243			--menu \"\$prompt\"                \
244			$height $width $rows               \
245			$menu_list                         \
246			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
247		)
248		local retval=$?
249		f_dialog_data_sanitize tag
250
251		if [ $retval -eq $DIALOG_HELP ]; then
252			f_show_help "$TCP_HELPFILE"
253			continue
254		elif [ $retval -ne $DIALOG_OK ]; then
255			return $retval
256		else
257			# Only update default-item on success
258			defaultitem="$tag"
259		fi
260
261		#
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.
266		#
267		case "$tag" in
268		X\ *) break ;;
269		W\ *) f_dialog_menu_wireless_edit ;;
270		1\ *) f_dialog_menu_wlandev_edit \
271		      	"$interface" "${wlans%%[$IFS]*}" ;;
272		2\ *) #
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.
276		      #
277		      if f_nfs_mounted && ! f_jailed; then
278			local setting
279			f_sprintf setting "$msg_current_dhcp_status" \
280		      	                  "$interface" "$dhcp_status"
281			f_noyes "$msg_nfs_mounts_may_cause_hang" "$setting" ||
282		      		continue
283		      fi
284
285		      #
286		      # Toggle DHCP status
287		      #
288		      if [ "$dhcp_status" = "$msg_enabled" ]; then
289		      	dhcp=
290		      else
291		      	trap - SIGINT
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
296		      	  	(
297		      	  	  f_quietly ifconfig "$interface" delete
298		      	  	  f_quietly dhclient "$interface"
299		      	  	) |
300		      	  	  f_xdialog_info "$msg"
301		      	  else
302		      	  	f_dialog_info "$msg"
303		      	  	f_quietly ifconfig "$interface" delete
304		      	  	f_quietly dhclient "$interface"
305		      	  fi
306		      	)
307		      	retval=$?
308		      	trap 'interrupt' SIGINT
309		      	if [ $retval -eq $DIALOG_OK ]; then
310		      		dhcp=1
311		      		f_ifconfig_inet "$interface" ipaddr
312				f_ifconfig_inet6 "$interface" ipaddr6
313		      		f_ifconfig_netmask "$interface" netmask
314		      		options=
315
316		      		# Fixup search/domain in resolv.conf(5)
317		      		hostname=$( f_sysrc_get \
318				            	'hostname:-$(hostname)' )
319		      		f_dialog_resolv_conf_update "$hostname"
320		      	fi
321		      fi
322		      ;;
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= ;;
329		esac
330	done
331
332	#
333	# Save only if the user changed at least one feature of the interface
334	#
335	if [ "$ipaddr"  != "$ipaddr_orig"  -o \
336	     "$netmask" != "$netmask_orig" -o \
337	     "$options" != "$options_orig" -o \
338	     "$dhcp"    != "$dhcp_orig" ]
339	then
340		f_show_info "$msg_saving_network_interface" "$interface"
341
342		local value=
343		if [ "$dhcp" ]; then
344			f_eval_catch $funcname f_sysrc_delete \
345				'f_sysrc_delete defaultrouter'
346			value=DHCP
347		else
348			value="inet $ipaddr netmask $netmask"
349			value="$value${options:+ }$options"
350		fi
351
352		f_eval_catch $funcname f_sysrc_set \
353			'f_sysrc_set "ifconfig_%s" "%s"' "$interface" "$value"
354	fi
355
356	#
357	# Re/Apply the settings if desired
358	#
359	if [ ! "$dhcp" ]; then
360		if f_yesno "$msg_bring_interface_up" "$interface"
361		then
362			f_show_info "$msg_bring_interface_up" "$interface"
363
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"
370			fi
371			#
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).
378			#
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"
383			then
384				[ "$dr" -a "$dr" != "NO" ] &&
385					f_eval_catch $funcname route \
386						'route add default "%s"' "$dr"
387			fi
388			if cmp -s "$RESOLV_CONF" "$RESOLV_CONF.$$"; then
389				f_quietly rm -f "$RESOLV_CONF.$$"
390			else
391				f_quietly mv -f "$RESOLV_CONF.$$" "$RESOLV_CONF"
392			fi
393		fi
394	fi
395
396	return $DIALOG_OK
397}
398
399############################################################ MAIN
400
401f_dprintf "%s: Successfully loaded." networking/device.subr
402
403fi # ! $_NETWORKING_DEVICE_SUBR
404