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