xref: /freebsd/usr.sbin/bsdinstall/scripts/netconfig_ipv6 (revision bc4a673f89b9c5d4eb0be86cf4206a6b09fd2ca3)
17986af23SBjoern A. Zeeb#!/bin/sh
27986af23SBjoern A. Zeeb#-
37986af23SBjoern A. Zeeb# Copyright (c) 2011 Nathan Whitehorn
47986af23SBjoern A. Zeeb# Copyright (c) 2011 The FreeBSD Foundation
5*bc4a673fSDevin Teske# Copyright (c) 2013 Devin Teske
67986af23SBjoern A. Zeeb# All rights reserved.
77986af23SBjoern A. Zeeb#
87986af23SBjoern A. Zeeb# Portions of this software were developed by Bjoern Zeeb
97986af23SBjoern A. Zeeb# under sponsorship from the FreeBSD Foundation.
107986af23SBjoern A. Zeeb#
117986af23SBjoern A. Zeeb# Redistribution and use in source and binary forms, with or without
127986af23SBjoern A. Zeeb# modification, are permitted provided that the following conditions
137986af23SBjoern A. Zeeb# are met:
147986af23SBjoern A. Zeeb# 1. Redistributions of source code must retain the above copyright
157986af23SBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer.
167986af23SBjoern A. Zeeb# 2. Redistributions in binary form must reproduce the above copyright
177986af23SBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer in the
187986af23SBjoern A. Zeeb#    documentation and/or other materials provided with the distribution.
197986af23SBjoern A. Zeeb#
207986af23SBjoern A. Zeeb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
217986af23SBjoern A. Zeeb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
227986af23SBjoern A. Zeeb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
237986af23SBjoern A. Zeeb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
247986af23SBjoern A. Zeeb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
257986af23SBjoern A. Zeeb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
267986af23SBjoern A. Zeeb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
277986af23SBjoern A. Zeeb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
287986af23SBjoern A. Zeeb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
297986af23SBjoern A. Zeeb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
307986af23SBjoern A. Zeeb# SUCH DAMAGE.
317986af23SBjoern A. Zeeb#
327986af23SBjoern A. Zeeb# $FreeBSD$
33*bc4a673fSDevin Teske#
34*bc4a673fSDevin Teske############################################################ INCLUDES
35*bc4a673fSDevin Teske
36*bc4a673fSDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
37*bc4a673fSDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
38*bc4a673fSDevin Teskef_dprintf "%s: loading includes..." "$0"
39*bc4a673fSDevin Teskef_include $BSDCFG_SHARE/dialog.subr
40*bc4a673fSDevin Teske
41*bc4a673fSDevin Teske############################################################ MAIN
427986af23SBjoern A. Zeeb
437986af23SBjoern A. Zeeb#
447986af23SBjoern A. Zeeb# TODO:
457986af23SBjoern A. Zeeb# - Add DHCPv6 support once FreeBSD ships with it.
467986af23SBjoern A. Zeeb#
477986af23SBjoern A. Zeeb
487986af23SBjoern A. ZeebINTERFACE=$1
497986af23SBjoern A. Zeebcase "${INTERFACE}" in
507986af23SBjoern A. Zeeb"")	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
517986af23SBjoern A. Zeeb	    --msgbox 'No interface specified for IPv6 configuration.' 0 0
527986af23SBjoern A. Zeeb	exit 1
537986af23SBjoern A. Zeeb	;;
547986af23SBjoern A. Zeebesac
557986af23SBjoern A. Zeeb
567986af23SBjoern A. ZeebAGAIN=""
577986af23SBjoern A. Zeebwhile : ; do
587986af23SBjoern A. Zeeb	MSG="Would you like to try stateless address autoconfiguration (SLAAC)${AGAIN}?"
597986af23SBjoern A. Zeeb	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
607986af23SBjoern A. Zeeb	    --yesno "${MSG}" 0 0
617986af23SBjoern A. Zeeb	if [ $? -eq $DIALOG_OK ]; then
627986af23SBjoern A. Zeeb		if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
637986af23SBjoern A. Zeeb			dialog --backtitle 'FreeBSD Installer' \
647986af23SBjoern A. Zeeb			    --infobox "Sending Router Solicitation ..." 0 0
65f07f97beSBjoern A. Zeeb			ifconfig ${INTERFACE} inet6 -ifdisabled accept_rtadv up
66*bc4a673fSDevin Teske			err=$( rtsol -F $INTERFACE 2>&1 )
677986af23SBjoern A. Zeeb			if [ $? -ne 0 ]; then
68*bc4a673fSDevin Teske				f_dprintf "%s" "$err"
697986af23SBjoern A. Zeeb				dialog --backtitle 'FreeBSD Installer' --msgbox "SLAAC failed." 0 0
707986af23SBjoern A. Zeeb				AGAIN=" again"
717986af23SBjoern A. Zeeb				continue
727986af23SBjoern A. Zeeb			fi
737986af23SBjoern A. Zeeb		fi
74d63d020eSBjoern A. Zeeb		echo ifconfig_${INTERFACE}_ipv6=\"inet6 accept_rtadv\" >> $BSDINSTALL_TMPETC/._rc.conf.net
757986af23SBjoern A. Zeeb		exit 0
767986af23SBjoern A. Zeeb	else
777986af23SBjoern A. Zeeb		break
787986af23SBjoern A. Zeeb	fi
797986af23SBjoern A. Zeebdone
807986af23SBjoern A. Zeeb
817986af23SBjoern A. ZeebROUTER6=`netstat -Wrn -f inet6 | awk '/default/ {printf("%s\n", $2);}'`
827986af23SBjoern A. ZeebADDRS=`ifconfig ${INTERFACE} inet6 | \
837986af23SBjoern A. Zeebawk  -v dfr="${ROUTER6}" '
847986af23SBjoern A. ZeebBEGIN {
857986af23SBjoern A. Zeeb	n=0;
867986af23SBjoern A. Zeeb}
877986af23SBjoern A. Zeeb{
887986af23SBjoern A. Zeeb	if (/inet6/) {
897986af23SBjoern A. Zeeb		if (match($2, "^fe80:")) { next; };
907986af23SBjoern A. Zeeb		# For the moment ignore all but the first address; it might confuse the user.
917986af23SBjoern A. Zeeb		if (n > 0) { next; };
927986af23SBjoern A. Zeeb		n++;
937986af23SBjoern A. Zeeb		printf "\"IPv6 Address\" %d 0 \"%s/%s\" %d 16 50 0 0 ", n, $2, $4, n;
947986af23SBjoern A. Zeeb	}
957986af23SBjoern A. Zeeb}
967986af23SBjoern A. ZeebEND {
977986af23SBjoern A. Zeeb	if (n == 0) {
987986af23SBjoern A. Zeeb		n++;
997986af23SBjoern A. Zeeb		printf "\"IPv6 Address\" %d 0 \"\" %d 16 50 0 0 ", n, n;
1007986af23SBjoern A. Zeeb	}
1017986af23SBjoern A. Zeeb	n++;
1027986af23SBjoern A. Zeeb	# Nasty trick adding a (hidden, same y) read-only field as a marker
1037986af23SBjoern A. Zeeb	# to separate interface address(es) from the default router.
1047986af23SBjoern A. Zeeb	printf "\"Default Router\" %d 0 \"%s\" %d 16 50 0 2 ", n, "DefaultRouter", n;
1057986af23SBjoern A. Zeeb	printf "\"Default Router\" %d 0 \"%s\" %d 16 50 0 0 ", n, dfr, n;
1067986af23SBjoern A. Zeeb}'`
1077986af23SBjoern A. Zeeb
1087986af23SBjoern A. Zeebexec 3>&1
1097986af23SBjoern A. ZeebIF_CONFIG=$(echo ${ADDRS} | xargs dialog --backtitle 'FreeBSD Installer' \
1107986af23SBjoern A. Zeeb	--title 'Network Configuration' \
1117986af23SBjoern A. Zeeb	--mixedform 'Static IPv6 Network Interface Configuration' 0 0 0 \
1127986af23SBjoern A. Zeeb2>&1 1>&3)
1137986af23SBjoern A. Zeebif [ $? -eq $DIALOG_CANCEL ]; then exit 1; fi
1147986af23SBjoern A. Zeebexec 3>&-
1157986af23SBjoern A. Zeeb
1167986af23SBjoern A. Zeebecho ${IF_CONFIG} | tr ' ' '\n' | \
1177986af23SBjoern A. Zeebawk -v iface="${INTERFACE}" '
1187986af23SBjoern A. ZeebBEGIN {
1197986af23SBjoern A. Zeeb	dfr=0;
1207986af23SBjoern A. Zeeb	count=0;
1217986af23SBjoern A. Zeeb}
1227986af23SBjoern A. Zeeb{
1237986af23SBjoern A. Zeeb	if (/^[[:space:]]+$/) {
1247986af23SBjoern A. Zeeb		next;
1257986af23SBjoern A. Zeeb	}
1267986af23SBjoern A. Zeeb	if (/DefaultRouter/) {
1277986af23SBjoern A. Zeeb		dfr=1;
1287986af23SBjoern A. Zeeb		next;
1297986af23SBjoern A. Zeeb	}
1307986af23SBjoern A. Zeeb	if (dfr == 1) {
1317986af23SBjoern A. Zeeb		printf("ipv6_defaultrouter=\"%s\"\n", $1);
1327986af23SBjoern A. Zeeb		next;
1337986af23SBjoern A. Zeeb	}
1347986af23SBjoern A. Zeeb	if (count > 0) {
1357986af23SBjoern A. Zeeb		# Ignore all but the first IP address for now.
1367986af23SBjoern A. Zeeb		next;
1377986af23SBjoern A. Zeeb	}
1387986af23SBjoern A. Zeeb	count++;
1397986af23SBjoern A. Zeeb	if (!match($1, "/")) {
1407986af23SBjoern A. Zeeb		sub("$", "/64", $1);
1417986af23SBjoern A. Zeeb	}
1427986af23SBjoern A. Zeeb	printf("ifconfig_%s_ipv6=\"inet6 %s\"\n", iface, $1);
143d63d020eSBjoern A. Zeeb}' >> $BSDINSTALL_TMPETC/._rc.conf.net
1447986af23SBjoern A. Zeeb
1457986af23SBjoern A. Zeebif [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
146d63d020eSBjoern A. Zeeb	. $BSDINSTALL_TMPETC/._rc.conf.net
1477986af23SBjoern A. Zeeb	ifconfig ${INTERFACE} `eval echo \\\$ifconfig_${INTERFACE}_ipv6`
1485b046b47SBjoern A. Zeeb	if [ -n "${ipv6_defaultrouter}" ]; then
1495b046b47SBjoern A. Zeeb		route delete -inet6 default
1505b046b47SBjoern A. Zeeb		route add -inet6 default ${ipv6_defaultrouter}
1515b046b47SBjoern A. Zeeb	fi
1527986af23SBjoern A. Zeebfi
1537986af23SBjoern A. Zeeb
154*bc4a673fSDevin Teske################################################################################
155*bc4a673fSDevin Teske# END
156*bc4a673fSDevin Teske################################################################################
157