xref: /freebsd/usr.sbin/bsdinstall/scripts/netconfig (revision fed1ca4b719c56c930f2259d80663cd34be812bb)
1#!/bin/sh
2#-
3# Copyright (c) 2011 Nathan Whitehorn
4# All rights reserved.
5# Copyright (c) 2011 The FreeBSD Foundation
6# All rights reserved.
7#
8# Portions of this software were developed by Bjoern Zeeb
9# under sponsorship from the FreeBSD Foundation.
10#
11# Redistribution and use in source and binary forms, with or without
12# modification, are permitted provided that the following conditions
13# are met:
14# 1. Redistributions of source code must retain the above copyright
15#    notice, this list of conditions and the following disclaimer.
16# 2. Redistributions in binary form must reproduce the above copyright
17#    notice, this list of conditions and the following disclaimer in the
18#    documentation and/or other materials provided with the distribution.
19#
20# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30# SUCH DAMAGE.
31#
32# $FreeBSD$
33
34INTERFACES=""
35DIALOG_TAGS=""
36
37: ${DIALOG_OK=0}
38: ${DIALOG_CANCEL=1}
39: ${DIALOG_HELP=2}
40: ${DIALOG_EXTRA=3}
41: ${DIALOG_ITEM_HELP=4}
42: ${DIALOG_ESC=255}
43
44for IF in `ifconfig -l`; do
45	test "$IF" = "lo0" && continue
46	(ifconfig -g wlan | egrep -wq $IF) && continue
47	INTERFACES="$INTERFACES $IF"
48done
49
50INTERFACES="$INTERFACES $(sysctl -in net.wlan.devices)"
51is_wireless_if() {
52	for IF in $(sysctl -in net.wlan.devices); do
53		if [ $IF = $1 ]; then
54			return 0
55		fi
56	done
57	return 1
58}
59
60for IF in $INTERFACES; do
61	DESC=`sysctl -n dev.$(echo $IF | sed -E 's/([[:alpha:]]*)([[:digit:]]*)/\1.\2/g').%desc`
62	DIALOG_TAGS="$DIALOG_TAGS $IF \"$DESC\""
63done
64
65if [ -z "$INTERFACES" ]; then
66	dialog --backtitle 'FreeBSD Installer' \
67	    --title 'Network Configuration Error' \
68	    --msgbox 'No network interfaces present to configure.' 0 0
69	exit 1
70fi
71
72exec 3>&1
73INTERFACE=`echo $DIALOG_TAGS | xargs dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' --menu 'Please select a network interface to configure:' 0 0 0 2>&1 1>&3`
74if [ $? -eq $DIALOG_CANCEL ]; then exit 1; fi
75exec 3>&-
76
77: > $BSDINSTALL_TMPETC/._rc.conf.net
78
79IFCONFIG_PREFIX=""
80if is_wireless_if $INTERFACE; then
81	NEXT_WLAN_IFACE=wlan0	# XXX
82	echo wlans_$INTERFACE=\"$NEXT_WLAN_IFACE\" >> $BSDINSTALL_TMPETC/._rc.conf.net
83	IFCONFIG_PREFIX="WPA "
84	if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
85		ifconfig $NEXT_WLAN_IFACE create wlandev $INTERFACE
86		ifconfig $NEXT_WLAN_IFACE up
87	fi
88	bsdinstall wlanconfig $NEXT_WLAN_IFACE || exec $0
89	INTERFACE="$NEXT_WLAN_IFACE"
90fi
91
92IPV6_AVAIL=0
93IPV4_AVAIL=0
94sysctl -N kern.features.inet6 > /dev/null 2>&1
95case $? in
960)	IPV6_AVAIL=1 ;;
97esac
98sysctl -N kern.features.inet > /dev/null 2>&1
99case $? in
1000)	IPV4_AVAIL=1 ;;
101esac
102
103if [ ${IPV4_AVAIL} -eq 1 ]; then
104	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
105	    --yesno 'Would you like to configure IPv4 for this interface?' 0 0
106	if [ $? -eq $DIALOG_OK ]; then
107		bsdinstall netconfig_ipv4 ${INTERFACE} "${IFCONFIG_PREFIX}" || \
108		exec $0
109	else
110		IPV4_AVAIL=0
111	fi
112fi
113# In case wlanconfig left an option and we do not support IPv4 we need to write
114# it out on its own.  We cannot write it out with IPv6 as that suffix.
115if [ ${IPV4_AVAIL} -eq 0 -a -n ${IFCONFIG_PREFIX} ]; then
116	echo ifconfig_${INTERFACE}=\"${IFCONFIG_PREFIX}\" >> $BSDINSTALL_TMPETC/._rc.conf.net
117fi
118if [ ${IPV6_AVAIL} -eq 1 ]; then
119	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
120	    --yesno 'Would you like to configure IPv6 for this interface?' 0 0
121	if [ $? -eq $DIALOG_OK ]; then
122		bsdinstall netconfig_ipv6 ${INTERFACE} || exec $0
123	else
124		IPV6_AVAIL=0
125	fi
126fi
127
128SEARCH=""
129IP4_1=""
130IP4_2=""
131IP6_1=""
132IP6_2=""
133while read key value; do
134	case "${key}" in
135	search)		SEARCH="${value}" ;;
136	nameserver)	# is more trick as we have to distinguish v4 and v6
137		case "${value}" in
138		[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*)
139			if [ -z "${IP4_1}" ] ; then
140				IP4_1="${value}"
141			elif [ -z "${IP4_2}" ]; then
142				IP4_2="${value}"
143			fi
144			;;
145		[0-9A-Fa-f:]*:*)
146			if [ -z "${IP6_1}" ] ; then
147				IP6_1="${value}"
148			elif [ -z "${IP6_2}" ]; then
149				IP6_2="${value}"
150			fi
151			;;
152		esac
153		;;
154	# ignore others
155	esac
156done < ${BSDINSTALL_TMPETC}/resolv.conf
157
158RESOLV=""
159if [ ${IPV6_AVAIL} -eq 1 -a ${IPV4_AVAIL} -eq 1 ];  then
160	RESOLV="
161	    'Search' 1 0 \"${SEARCH}\" 1 16 50 0 0
162	    'Nameserver' 2 0 \"Nameserver\" 2 16 50 0 2
163	    'IPv6 DNS #1' 2 0 \"${IP6_1}\" 2 16 50 0 0
164	    'IPv6 DNS #2' 3 0 \"${IP6_2}\" 3 16 50 0 0
165	    'IPv4 DNS #1' 4 0 \"${IP4_1}\" 4 16 16 0 0
166	    'IPv4 DNS #2' 5 0 \"${IP4_2}\" 5 16 16 0 0"
167elif [ ${IPV6_AVAIL} -eq 1 ]; then
168	RESOLV="
169	    'Search' 1 0 \"${SEARCH}\" 1 16 50 0 0
170	    'Nameserver' 2 0 \"Nameserver\" 2 16 50 0 2
171	    'IPv6 DNS #1' 2 0 \"${IP6_1}\" 2 16 50 0 0
172	    'IPv6 DNS #2' 3 0 \"${IP6_2}\" 3 16 50 0 0"
173elif [ ${IPV4_AVAIL} -eq 1 ]; then
174	RESOLV="
175	    'Search' 1 0 \"${SEARCH}\" 1 16 50 0 0
176	    'Nameserver' 2 0 \"Nameserver\" 2 16 50 0 2
177	    'IPv4 DNS #1' 2 0 \"${IP4_1}\" 2 16 16 0 0
178	    'IPv4 DNS #2' 3 0 \"${IP4_2}\" 3 16 16 0 0"
179else
180	exit 0
181fi
182
183exec 3>&1
184RESOLV=$(echo "${RESOLV}" | xargs dialog --backtitle 'FreeBSD Installer' \
185	--title 'Network Configuration' \
186	--mixedform 'Resolver Configuration' 0 0 0 \
1872>&1 1>&3)
188if [ $? -eq $DIALOG_CANCEL ]; then exec $0; fi
189exec 3>&-
190
191echo ${RESOLV} | tr ' ' '\n' | \
192awk '
193BEGIN {
194	search=-1;
195}
196{
197	if (/^[[:space:]]+$/) {
198		next;
199	}
200	if (/^Nameserver$/) {
201		printf "\n";
202		search=0;
203		next;
204	}
205	if (search == -1) {
206		printf "search ";
207		search=1;
208	}
209	if (search > 0) {
210		printf "%s%s", (search > 1) ? " " : "", $1;
211		search++;
212		next;
213	}
214	printf "nameserver %s\n", $1;
215}' > ${BSDINSTALL_TMPETC}/resolv.conf
216
217mv $BSDINSTALL_TMPETC/._rc.conf.net $BSDINSTALL_TMPETC/rc.conf.net
218