1222468Sbz#!/bin/sh
2222468Sbz#-
3222468Sbz# Copyright (c) 2011 Nathan Whitehorn
4222468Sbz# Copyright (c) 2011 The FreeBSD Foundation
5287690Sdteske# Copyright (c) 2013-2015 Devin Teske
6222468Sbz# All rights reserved.
7222468Sbz#
8222468Sbz# Portions of this software were developed by Bjoern Zeeb
9222468Sbz# under sponsorship from the FreeBSD Foundation.
10222468Sbz#
11222468Sbz# Redistribution and use in source and binary forms, with or without
12222468Sbz# modification, are permitted provided that the following conditions
13222468Sbz# are met:
14222468Sbz# 1. Redistributions of source code must retain the above copyright
15222468Sbz#    notice, this list of conditions and the following disclaimer.
16222468Sbz# 2. Redistributions in binary form must reproduce the above copyright
17222468Sbz#    notice, this list of conditions and the following disclaimer in the
18222468Sbz#    documentation and/or other materials provided with the distribution.
19222468Sbz#
20222468Sbz# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21222468Sbz# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22222468Sbz# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23222468Sbz# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24222468Sbz# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25222468Sbz# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26222468Sbz# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27222468Sbz# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28222468Sbz# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29222468Sbz# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30222468Sbz# SUCH DAMAGE.
31222468Sbz#
32222468Sbz# $FreeBSD$
33257842Sdteske#
34257842Sdteske############################################################ INCLUDES
35222468Sbz
36257842SdteskeBSDCFG_SHARE="/usr/share/bsdconfig"
37257842Sdteske. $BSDCFG_SHARE/common.subr || exit 1
38257842Sdteskef_dprintf "%s: loading includes..." "$0"
39257842Sdteskef_include $BSDCFG_SHARE/dialog.subr
40257842Sdteske
41257842Sdteske############################################################ MAIN
42257842Sdteske
43222468Sbz#
44222468Sbz# TODO: 
45222468Sbz# - Add DHCPv6 support once FreeBSD ships with it.
46222468Sbz# 
47222468Sbz
48222468SbzINTERFACE=$1
49222468Sbzcase "${INTERFACE}" in
50222468Sbz"")	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
51222468Sbz	    --msgbox 'No interface specified for IPv6 configuration.' 0 0
52222468Sbz	exit 1
53222468Sbz	;;
54222468Sbzesac
55222468Sbz
56222468SbzAGAIN=""
57222468Sbzwhile : ; do
58222468Sbz	MSG="Would you like to try stateless address autoconfiguration (SLAAC)${AGAIN}?"
59222468Sbz	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
60222468Sbz	    --yesno "${MSG}" 0 0
61222468Sbz	if [ $? -eq $DIALOG_OK ]; then
62222468Sbz		if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
63222468Sbz			dialog --backtitle 'FreeBSD Installer' \
64222468Sbz			    --infobox "Sending Router Solicitation ..." 0 0
65222502Sbz			ifconfig ${INTERFACE} inet6 -ifdisabled accept_rtadv up
66257842Sdteske			err=$( rtsol -F $INTERFACE 2>&1 )
67222468Sbz			if [ $? -ne 0 ]; then
68257842Sdteske				f_dprintf "%s" "$err"
69222468Sbz				dialog --backtitle 'FreeBSD Installer' --msgbox "SLAAC failed." 0 0
70222468Sbz				AGAIN=" again"
71222468Sbz				continue
72222468Sbz			fi
73222468Sbz		fi
74222619Sbz		echo ifconfig_${INTERFACE}_ipv6=\"inet6 accept_rtadv\" >> $BSDINSTALL_TMPETC/._rc.conf.net
75222468Sbz		exit 0
76222468Sbz	else
77222468Sbz		break
78222468Sbz	fi
79222468Sbzdone
80222468Sbz
81222468SbzROUTER6=`netstat -Wrn -f inet6 | awk '/default/ {printf("%s\n", $2);}'`
82222468SbzADDRS=`ifconfig ${INTERFACE} inet6 | \
83222468Sbzawk  -v dfr="${ROUTER6}" '
84222468SbzBEGIN {
85222468Sbz	n=0;
86222468Sbz}
87222468Sbz{
88222468Sbz	if (/inet6/) {
89222468Sbz		if (match($2, "^fe80:")) { next; };
90222468Sbz		# For the moment ignore all but the first address; it might confuse the user.
91222468Sbz		if (n > 0) { next; };
92222468Sbz		n++;
93222468Sbz		printf "\"IPv6 Address\" %d 0 \"%s/%s\" %d 16 50 0 0 ", n, $2, $4, n;
94222468Sbz	}
95222468Sbz}
96222468SbzEND {
97222468Sbz	if (n == 0) {
98222468Sbz		n++;
99222468Sbz		printf "\"IPv6 Address\" %d 0 \"\" %d 16 50 0 0 ", n, n;
100222468Sbz	}
101222468Sbz	n++;
102222468Sbz	# Nasty trick adding a (hidden, same y) read-only field as a marker
103222468Sbz	# to separate interface address(es) from the default router.
104222468Sbz	printf "\"Default Router\" %d 0 \"%s\" %d 16 50 0 2 ", n, "DefaultRouter", n;
105222468Sbz	printf "\"Default Router\" %d 0 \"%s\" %d 16 50 0 0 ", n, dfr, n;
106222468Sbz}'`
107222468Sbz
108222468Sbzexec 3>&1
109222468SbzIF_CONFIG=$(echo ${ADDRS} | xargs dialog --backtitle 'FreeBSD Installer' \
110222468Sbz	--title 'Network Configuration' \
111222468Sbz	--mixedform 'Static IPv6 Network Interface Configuration' 0 0 0 \
112222468Sbz2>&1 1>&3)
113222468Sbzif [ $? -eq $DIALOG_CANCEL ]; then exit 1; fi
114222468Sbzexec 3>&-
115222468Sbz
116222468Sbzecho ${IF_CONFIG} | tr ' ' '\n' | \
117222468Sbzawk -v iface="${INTERFACE}" '
118222468SbzBEGIN {
119222468Sbz	dfr=0;
120222468Sbz	count=0;
121222468Sbz}
122222468Sbz{
123222468Sbz	if (/^[[:space:]]+$/) {
124222468Sbz		next;
125222468Sbz	}
126222468Sbz	if (/DefaultRouter/) {
127222468Sbz		dfr=1;
128222468Sbz		next;
129222468Sbz	}
130222468Sbz	if (dfr == 1) {
131222468Sbz		printf("ipv6_defaultrouter=\"%s\"\n", $1);
132222468Sbz		next;
133222468Sbz	}
134222468Sbz	if (count > 0) {
135222468Sbz		# Ignore all but the first IP address for now.
136222468Sbz		next;
137222468Sbz	}
138222468Sbz	count++;
139222468Sbz	if (!match($1, "/")) {
140222468Sbz		sub("$", "/64", $1);
141222468Sbz	}
142222468Sbz	printf("ifconfig_%s_ipv6=\"inet6 %s\"\n", iface, $1);
143222619Sbz}' >> $BSDINSTALL_TMPETC/._rc.conf.net
144287690Sdteskeretval=$?
145222468Sbz
146287690Sdteskeif [ "$BSDINSTALL_CONFIGCURRENT" ]; then
147222619Sbz	. $BSDINSTALL_TMPETC/._rc.conf.net
148222468Sbz	ifconfig ${INTERFACE} `eval echo \\\$ifconfig_${INTERFACE}_ipv6`
149287690Sdteske	if [ "$ipv6_defaultrouter" ]; then
150225429Sbz		route delete -inet6 default
151225429Sbz		route add -inet6 default ${ipv6_defaultrouter}
152287690Sdteske		retval=$?
153225429Sbz	fi
154222468Sbzfi
155222468Sbz
156287690Sdteskeexit $retval
157287690Sdteske
158257842Sdteske################################################################################
159257842Sdteske# END
160257842Sdteske################################################################################
161