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