device.subr revision 260676
1if [ ! "$_NETWORKING_DEVICE_SUBR" ]; then _NETWORKING_DEVICE_SUBR=1
2#
3# Copyright (c) 2006-2013 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: stable/10/usr.sbin/bsdconfig/networking/share/device.subr 260676 2014-01-15 07:42:31Z dteske $
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/networking/common.subr
38f_include $BSDCFG_SHARE/networking/ipaddr.subr
39f_include $BSDCFG_SHARE/networking/media.subr
40f_include $BSDCFG_SHARE/networking/netmask.subr
41f_include $BSDCFG_SHARE/networking/resolv.subr
42f_include $BSDCFG_SHARE/networking/routing.subr
43f_include $BSDCFG_SHARE/sysrc.subr
44
45BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
46f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
47
48############################################################ GLOBALS
49
50#
51# Settings used while interacting with various dialog(1) menus
52#
53: ${DIALOG_MENU_NETDEV_KICK_INTERFACES=1}
54: ${DIALOG_MENU_NETDEV_SLEEP_AFTER_KICK=3}
55
56############################################################ FUNCTIONS
57
58# f_dialog_menu_netdev [$default]
59#
60# Display a list of network devices with descriptions. Optionally, if present
61# and non-NULL, initially highlight $default interface.
62#
63f_dialog_menu_netdev()
64{
65	local menu_list # Calculated below
66	local defaultitem="${1%\*}" # Trim trailing asterisk if present
67
68	#
69	# Display a message to let the user know we're working...
70	# (message will remain until we throw up the next dialog)
71	#
72	f_dialog_info "$msg_probing_network_interfaces"
73
74	#
75	# Get list of usable network interfaces
76	#
77	local devs if iflist= # Calculated below
78	f_device_rescan_network
79	f_device_find "" $DEVICE_TYPE_NETWORK devs
80	for if in $devs; do
81		# Skip unsavory interfaces
82		case "$if" in
83		lo[0-9]*|ppp[0-9]*|sl[0-9]*|faith[0-9]*) continue ;;
84		esac
85		iflist="$iflist $if"
86	done
87	iflist="${iflist# }"
88
89	#
90	# Optionally kick interfaces in the head to get them to accurately
91	# track the carrier status in realtime (required on FreeBSD).
92	#
93	if [ "$DIALOG_MENU_NETDEV_KICK_INTERFACES" ]; then
94		DIALOG_MENU_NETDEV_KICK_INTERFACES=
95
96		for if in $iflist; do
97			f_quietly ifconfig $if up
98		done
99
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
103		fi
104	fi
105
106	#
107	# Mark any "active" interfaces with an asterisk (*)
108	# to the right of the device name.
109	#
110	menu_list=$(
111		for if in $iflist; do
112			f_device_desc $if $DEVICE_TYPE_NETWORK desc
113			f_shell_escape "$desc" desc
114			if f_device_is_active $if; then
115				printf "'%s\*' '%s'\n" $if "$desc"
116			else
117				printf "'%s' '%s'\n" $if "$desc"
118			fi
119		done
120	)
121	if [ ! "$menu_list" ]; then
122		f_show_msg "$msg_no_network_interfaces"
123		return $DIALOG_CANCEL
124	fi
125
126	# Maybe the default item was marked as active
127	f_device_is_active "$defaultitem" && defaultitem="$defaultitem*"
128
129	#
130	# Ask user to select an interface
131	#
132	local prompt="$msg_select_network_interface"
133	local hline="$hline_arrows_tab_enter"
134	local height width rows
135	eval f_dialog_menu_size height width rows \
136	                        \"\$DIALOG_TITLE\"     \
137	                        \"\$DIALOG_BACKTITLE\" \
138	                        \"\$prompt\"           \
139	                        \"\$hline\"            \
140	                        $menu_list
141	local menu_choice
142	menu_choice=$( eval $DIALOG \
143		--title \"\$DIALOG_TITLE\"         \
144		--backtitle \"\$DIALOG_BACKTITLE\" \
145		--hline \"\$hline\"                \
146		--ok-label \"\$msg_ok\"            \
147		--cancel-label \"\$msg_cancel\"    \
148		--default-item \"\$defaultitem\"   \
149		--menu \"\$prompt\"                \
150		$height $width $rows               \
151		$menu_list                         \
152		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
153	)
154	local retval=$?
155	f_dialog_menutag_store -s "$menu_choice"
156	return $retval
157}
158
159# f_dialog_menu_netdev_edit $interface $ipaddr $netmask $options $dhcp
160#
161# Allow a user to edit network interface settings. Current values are not
162# probed but rather taken from the positional arguments.
163#
164f_dialog_menu_netdev_edit()
165{
166	local interface="$1" ipaddr="$2" netmask="$3" options="$4" dhcp="$5"
167	local prompt menu_list height width rows
168
169	#
170	# Create a duplicate set of variables for change-tracking...
171	#
172	local ipaddr_orig="$2"  \
173	      netmask_orig="$3" \
174	      options_orig="$4" \
175	      dhcp_orig="$5"
176
177	local hline="$hline_arrows_tab_enter"
178	prompt=$( printf "$msg_network_configuration" "$interface" )
179
180	#
181	# Loop forever until the user has finished configuring the different
182	# components of the network interface.
183	#
184	# To apply the settings, we need to know each of the following:
185	# 	- IP Address
186	# 	- Network subnet mask
187	# 	- Additional ifconfig(8) options
188	#
189	# It is only when we have all of the above values that we can make the
190	# changes effective because all three options must be specified at-once
191	# to ifconfig(8).
192	#
193	local defaultitem=
194	while :; do
195		local dhcp_status="$msg_disabled"
196		[ "$dhcp" ] && dhcp_status="$msg_enabled"
197
198		#
199		# Display configuration-edit menu
200		#
201		menu_list="
202			'X $msg_save_exit' '$msg_return_to_previous_menu'
203			'2 $msg_dhcp'      '$dhcp_status'
204			'3 $msg_ipaddr4'   '$ipaddr'
205			'4 $msg_netmask'   '$netmask'
206			'5 $msg_options'   '$options'
207		"
208		eval f_dialog_menu_size height width rows \
209		                        \"\$DIALOG_TITLE\"     \
210		                        \"\$DIALOG_BACKTITLE\" \
211		                        \"\$prompt\"           \
212		                        \"\$hline\"            \
213		                        $menu_list
214		local tag
215		tag=$( eval $DIALOG \
216			--title \"\$DIALOG_TITLE\"         \
217			--backtitle \"\$DIALOG_BACKTITLE\" \
218			--hline \"\$hline\"                \
219			--ok-label \"\$msg_ok\"            \
220			--cancel-label \"\$msg_cancel\"    \
221			--help-button                      \
222			--help-label \"\$msg_help\"        \
223			${USE_XDIALOG:+--help \"\"}        \
224			--default-item \"\$defaultitem\"   \
225			--menu \"\$prompt\"                \
226			$height $width $rows               \
227			$menu_list                         \
228			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
229		)
230		local retval=$?
231		f_dialog_data_sanitize tag
232
233		if [ $retval -eq $DIALOG_HELP ]; then
234			f_show_help "$TCP_HELPFILE"
235			continue
236		elif [ $retval -ne $DIALOG_OK ]; then
237			return $retval
238		else
239			# Only update default-item on success
240			defaultitem="$tag"
241		fi
242
243		#
244		# Call the below ``modifier functions'' whose job it is to take
245		# input from the user and assign the newly-acquired values back
246		# to the ipaddr, netmask, and options variables for us to re-
247		# read and display in the summary dialog.
248		#
249		case "$tag" in
250		X\ *) break ;;
251		2\ *) #
252		      # Proceed cautiously (confirm with the user) if/when NFS-
253		      # mounts are active. If the network on which these mounts
254		      # are made is changed parts of the system may hang.
255		      #
256		      if f_nfs_mounted && ! f_jailed; then
257		      	local setting="$( printf "$msg_current_dhcp_status" \
258		      	                         "$interface" "$dhcp_status" )"
259			f_noyes "$msg_nfs_mounts_may_cause_hang" "$setting" ||
260		      		continue
261		      fi
262
263		      #
264		      # Toggle DHCP status
265		      #
266		      if [ "$dhcp_status" = "$msg_enabled" ]; then
267		      	dhcp=
268		      else
269		      	trap - SIGINT
270		      	( # Execute within sub-shell to allow/catch Ctrl-C
271		      	  trap 'exit $FAILURE' SIGINT
272		      	  msg=$( printf "$msg_scanning_for_dhcp" "$interface" )
273		      	  if [ "$USE_XDIALOG" ]; then
274		      	  	(
275		      	  	  f_quietly ifconfig "$interface" delete
276		      	  	  f_quietly dhclient "$interface"
277		      	  	) |
278		      	  	  f_xdialog_info "$msg"
279		      	  else
280		      	  	f_dialog_info "$msg"
281		      	  	f_quietly ifconfig "$interface" delete
282		      	  	f_quietly dhclient "$interface"
283		      	  fi
284		      	)
285		      	retval=$?
286		      	trap 'interrupt' SIGINT
287		      	if [ $retval -eq $DIALOG_OK ]; then
288		      		dhcp=1
289		      		f_ifconfig_inet "$interface" ipaddr
290				f_ifconfig_inet6 "$interface" ipaddr6
291		      		f_ifconfig_netmask "$interface" netmask
292		      		options=
293
294		      		# Fixup search/domain in resolv.conf(5)
295		      		hostname=$( f_sysrc_get \
296				            	'hostname:-$(hostname)' )
297		      		f_dialog_resolv_conf_update "$hostname"
298		      	fi
299		      fi
300		      ;;
301		3\ *) f_dialog_input_ipaddr "$interface" "$ipaddr"
302		      [ $? -eq $DIALOG_OK ] && dhcp= ;;
303		4\ *) f_dialog_input_netmask "$interface" "$netmask"
304		      [ $? -eq $DIALOG_OK -a "$_netmask" ] && dhcp= ;;
305		5\ *) f_dialog_menu_media_options "$interface" "$options"
306		      [ $? -eq $DIALOG_OK ] && dhcp= ;;
307		esac
308	done
309
310	#
311	# Save only if the user changed at least one feature of the interface
312	#
313	if [ "$ipaddr"  != "$ipaddr_orig"  -o \
314	     "$netmask" != "$netmask_orig" -o \
315	     "$options" != "$options_orig" -o \
316	     "$dhcp"    != "$dhcp_orig" ]
317	then
318		f_show_info "$msg_saving_network_interface" "$interface"
319
320		local value=
321		if [ "$dhcp" ]; then
322			f_sysrc_delete defaultrouter
323			value=DHCP
324		else
325			value="inet $ipaddr netmask $netmask"
326			value="$value${options:+ }$options"
327		fi
328
329		f_sysrc_set ifconfig_$interface "$value"
330	fi
331
332	#
333	# Re/Apply the settings if desired
334	#
335	if [ ! "$dhcp" ]; then
336		if f_yesno "$msg_bring_interface_up" "$interface"
337		then
338			f_show_info "$msg_bring_interface_up" "$interface"
339
340			local dr="$( f_sysrc_get defaultrouter )" err
341			if [ "$dr" = "NO" -o ! "$dr" ]; then
342				dr=$( f_route_get_default )
343				[ "$dr" ] && f_sysrc_set defaultrouter "$dr"
344			fi
345			#
346			# Make a backup of resolv.conf(5) before using
347			# ifconfig(8) and then restore it afterward. This
348			# allows preservation of nameservers acquired via
349			# DHCP on FreeBSD-8.x (normally lost as ifconfig(8)
350			# usage causes dhclient(8) to exit which scrubs
351			# resolv.conf(5) by-default upon termination).
352			#
353			f_quietly cp -fp "$RESOLV_CONF" "$RESOLV_CONF.$$"
354			err=$( ifconfig $interface inet $ipaddr \
355			       	netmask $netmask $options 2>&1 )
356			if [ $? -eq $SUCCESS ]; then
357				if [ "$dr" -a "$dr" != "NO" ]; then
358					err=$( route add default "$dr" 2>&1 )
359					[ $? -eq $SUCCESS ] || \
360						dialog_msgbox "$err"
361				fi
362			else
363				dialog_msgbox "$err"
364			fi
365			if cmp -s "$RESOLV_CONF" "$RESOLV_CONF.$$"; then
366				f_quietly rm -f "$RESOLV_CONF.$$"
367			else
368				f_quietly mv -f "$RESOLV_CONF.$$" "$RESOLV_CONF"
369			fi
370		fi
371	fi
372
373	return $DIALOG_OK
374}
375
376############################################################ MAIN
377
378f_dprintf "%s: Successfully loaded." networking/device.subr
379
380fi # ! $_NETWORKING_DEVICE_SUBR
381