1#!/bin/sh
2#-
3# Copyright (c) 2011 Nathan Whitehorn
4# Copyright (c) 2011 The FreeBSD Foundation
5# Copyright (c) 2013-2015 Devin Teske
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#
33############################################################ INCLUDES
34
35BSDCFG_SHARE="/usr/share/bsdconfig"
36. $BSDCFG_SHARE/common.subr || exit 1
37f_dprintf "%s: loading includes..." "$0"
38f_include $BSDCFG_SHARE/dialog.subr
39
40############################################################ MAIN
41
42: ${BSDDIALOG_OK=0}
43: ${BSDDIALOG_CANCEL=1}
44
45#
46# TODO:
47# - Add DHCPv6 support once FreeBSD ships with it.
48#
49
50INTERFACE=$1
51AUTO="${2:-}"
52case "${INTERFACE}" in
53"")	bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' \
54	    --msgbox 'No interface specified for IPv6 configuration.' 0 0
55	exit 1
56	;;
57esac
58case "$AUTO" in
59""|auto)
60	;;
61*)
62	bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' \
63	    --msgbox "Bad auto option '$AUTO'." 0 0
64	exit 1
65	;;
66esac
67
68AGAIN=""
69while : ; do
70	if [ -n "$AUTO" ]; then
71		SLAAC=1
72	else
73		MSG="Would you like to try stateless address autoconfiguration (SLAAC)${AGAIN}?"
74		bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' \
75		    --yesno "${MSG}" 0 0
76		if [ $? -eq $BSDDIALOG_OK ]; then
77			SLAAC=1
78		else
79			SLAAC=0
80		fi
81	fi
82	if [ $SLAAC -eq 1 ]; then
83		if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
84			bsddialog --backtitle "$OSNAME Installer" \
85			    --infobox "Sending Router Solicitation ..." 0 0
86			ifconfig ${INTERFACE} inet6 -ifdisabled accept_rtadv up
87			err=$( rtsol -F $INTERFACE 2>&1 )
88			if [ $? -ne 0 ]; then
89				f_dprintf "%s" "$err"
90				if [ -n "$AUTO" ]; then
91					exit 1
92				fi
93				bsddialog --backtitle "$OSNAME Installer" --msgbox "SLAAC failed." 0 0
94				AGAIN=" again"
95				continue
96			fi
97		fi
98		sysrc -f $BSDINSTALL_TMPETC/._rc.conf.net ifconfig_${INTERFACE}_ipv6="inet6 accept_rtadv"
99		exit 0
100	else
101		break
102	fi
103done
104
105ROUTER6=`netstat -Wrn -f inet6 | awk '/default/ {printf("%s\n", $2);}'`
106ADDRS=`ifconfig ${INTERFACE} inet6 | \
107awk  -v dfr="${ROUTER6}" '
108BEGIN {
109	n=0;
110}
111{
112	if (/inet6/) {
113		if (match($2, "^fe80:")) { next; };
114		# For the moment ignore all but the first address; it might confuse the user.
115		if (n > 0) { next; };
116		n++;
117		printf "\"IPv6 Address\" %d 1 \"%s/%s\" %d 16 50 50 0 ", n, $2, $4, n;
118	}
119}
120END {
121	if (n == 0) {
122		n++;
123		printf "\"IPv6 Address\" %d 1 \"\" %d 16 50 50 0 ", n, n;
124	}
125	n++;
126	# Nasty trick adding a (hidden, same y) read-only field as a marker
127	# to separate interface address(es) from the default router.
128	printf "\"Default Router\" %d 1 \"%s\" %d 1 14 14 2 ", n, "DefaultRouter", n;
129	printf "\"Default Router\" %d 1 \"%s\" %d 16 50 50 0 ", n, dfr, n;
130}'`
131
132exec 5>&1
133IF_CONFIG=$(echo ${ADDRS} | xargs -o bsddialog --backtitle "$OSNAME Installer" \
134	--title 'Network Configuration' \
135	--mixedform 'Static IPv6 Network Interface Configuration' 0 0 0 \
1362>&1 1>&5)
137if [ $? -eq $BSDDIALOG_CANCEL ]; then exit 1; fi
138exec 5>&-
139
140echo ${IF_CONFIG} | tr ' ' '\n' | \
141awk -v iface="${INTERFACE}" '
142BEGIN {
143	dfr=0;
144	count=0;
145}
146{
147	if (/^[[:space:]]+$/) {
148		next;
149	}
150	if (/DefaultRouter/) {
151		dfr=1;
152		next;
153	}
154	if (dfr == 1) {
155		printf("ipv6_defaultrouter=\"%s\"\n", $1);
156		next;
157	}
158	if (count > 0) {
159		# Ignore all but the first IP address for now.
160		next;
161	}
162	count++;
163	if (!match($1, "/")) {
164		sub("$", "/64", $1);
165	}
166	printf("ifconfig_%s_ipv6=\"inet6 %s\"\n", iface, $1);
167}' >> $BSDINSTALL_TMPETC/._rc.conf.net
168retval=$?
169
170if [ "$BSDINSTALL_CONFIGCURRENT" ]; then
171	. $BSDINSTALL_TMPETC/._rc.conf.net
172	ifconfig ${INTERFACE} `eval echo \\\$ifconfig_${INTERFACE}_ipv6`
173	if [ "$ipv6_defaultrouter" ]; then
174		route delete -inet6 default
175		route add -inet6 default ${ipv6_defaultrouter}
176		retval=$?
177	fi
178fi
179
180exit $retval
181
182################################################################################
183# END
184################################################################################
185