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