netconfig_ipv6 revision 225429
1222468Sbz#!/bin/sh
2222468Sbz#-
3222468Sbz# Copyright (c) 2011 Nathan Whitehorn
4222468Sbz# All rights reserved.
5222468Sbz# Copyright (c) 2011 The FreeBSD Foundation
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: head/usr.sbin/bsdinstall/scripts/netconfig_ipv6 225429 2011-09-07 00:45:15Z bz $
33222468Sbz
34222468Sbz#
35222468Sbz# TODO: 
36222468Sbz# - Add DHCPv6 support once FreeBSD ships with it.
37222468Sbz# 
38222468Sbz
39222468Sbz: ${DIALOG_OK=0}
40222468Sbz: ${DIALOG_CANCEL=1}
41222468Sbz: ${DIALOG_HELP=2}
42222468Sbz: ${DIALOG_EXTRA=3}
43222468Sbz: ${DIALOG_ITEM_HELP=4}
44222468Sbz: ${DIALOG_ESC=255}
45222468Sbz
46222468SbzINTERFACE=$1
47222468Sbzcase "${INTERFACE}" in
48222468Sbz"")	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
49222468Sbz	    --msgbox 'No interface specified for IPv6 configuration.' 0 0
50222468Sbz	exit 1
51222468Sbz	;;
52222468Sbzesac
53222468Sbz
54222468SbzAGAIN=""
55222468Sbzwhile : ; do
56222468Sbz	MSG="Would you like to try stateless address autoconfiguration (SLAAC)${AGAIN}?"
57222468Sbz	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
58222468Sbz	    --yesno "${MSG}" 0 0
59222468Sbz	if [ $? -eq $DIALOG_OK ]; then
60222468Sbz		if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
61222468Sbz			dialog --backtitle 'FreeBSD Installer' \
62222468Sbz			    --infobox "Sending Router Solicitation ..." 0 0
63222502Sbz			ifconfig ${INTERFACE} inet6 -ifdisabled accept_rtadv up
64222468Sbz			rtsol -F $INTERFACE 2>> $BSDINSTALL_LOG
65222468Sbz			if [ $? -ne 0 ]; then
66222468Sbz				dialog --backtitle 'FreeBSD Installer' --msgbox "SLAAC failed." 0 0
67222468Sbz				AGAIN=" again"
68222468Sbz				continue
69222468Sbz			fi
70222468Sbz		fi
71222619Sbz		echo ifconfig_${INTERFACE}_ipv6=\"inet6 accept_rtadv\" >> $BSDINSTALL_TMPETC/._rc.conf.net
72222468Sbz		exit 0
73222468Sbz	else
74222468Sbz		break
75222468Sbz	fi
76222468Sbzdone
77222468Sbz
78222468SbzROUTER6=`netstat -Wrn -f inet6 | awk '/default/ {printf("%s\n", $2);}'`
79222468SbzADDRS=`ifconfig ${INTERFACE} inet6 | \
80222468Sbzawk  -v dfr="${ROUTER6}" '
81222468SbzBEGIN {
82222468Sbz	n=0;
83222468Sbz}
84222468Sbz{
85222468Sbz	if (/inet6/) {
86222468Sbz		if (match($2, "^fe80:")) { next; };
87222468Sbz		# For the moment ignore all but the first address; it might confuse the user.
88222468Sbz		if (n > 0) { next; };
89222468Sbz		n++;
90222468Sbz		printf "\"IPv6 Address\" %d 0 \"%s/%s\" %d 16 50 0 0 ", n, $2, $4, n;
91222468Sbz	}
92222468Sbz}
93222468SbzEND {
94222468Sbz	if (n == 0) {
95222468Sbz		n++;
96222468Sbz		printf "\"IPv6 Address\" %d 0 \"\" %d 16 50 0 0 ", n, n;
97222468Sbz	}
98222468Sbz	n++;
99222468Sbz	# Nasty trick adding a (hidden, same y) read-only field as a marker
100222468Sbz	# to separate interface address(es) from the default router.
101222468Sbz	printf "\"Default Router\" %d 0 \"%s\" %d 16 50 0 2 ", n, "DefaultRouter", n;
102222468Sbz	printf "\"Default Router\" %d 0 \"%s\" %d 16 50 0 0 ", n, dfr, n;
103222468Sbz}'`
104222468Sbz
105222468Sbzexec 3>&1
106222468SbzIF_CONFIG=$(echo ${ADDRS} | xargs dialog --backtitle 'FreeBSD Installer' \
107222468Sbz	--title 'Network Configuration' \
108222468Sbz	--mixedform 'Static IPv6 Network Interface Configuration' 0 0 0 \
109222468Sbz2>&1 1>&3)
110222468Sbzif [ $? -eq $DIALOG_CANCEL ]; then exit 1; fi
111222468Sbzexec 3>&-
112222468Sbz
113222468Sbzecho ${IF_CONFIG} | tr ' ' '\n' | \
114222468Sbzawk -v iface="${INTERFACE}" '
115222468SbzBEGIN {
116222468Sbz	dfr=0;
117222468Sbz	count=0;
118222468Sbz}
119222468Sbz{
120222468Sbz	if (/^[[:space:]]+$/) {
121222468Sbz		next;
122222468Sbz	}
123222468Sbz	if (/DefaultRouter/) {
124222468Sbz		dfr=1;
125222468Sbz		next;
126222468Sbz	}
127222468Sbz	if (dfr == 1) {
128222468Sbz		printf("ipv6_defaultrouter=\"%s\"\n", $1);
129222468Sbz		next;
130222468Sbz	}
131222468Sbz	if (count > 0) {
132222468Sbz		# Ignore all but the first IP address for now.
133222468Sbz		next;
134222468Sbz	}
135222468Sbz	count++;
136222468Sbz	if (!match($1, "/")) {
137222468Sbz		sub("$", "/64", $1);
138222468Sbz	}
139222468Sbz	printf("ifconfig_%s_ipv6=\"inet6 %s\"\n", iface, $1);
140222619Sbz}' >> $BSDINSTALL_TMPETC/._rc.conf.net
141222468Sbz
142222468Sbzif [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
143222619Sbz	. $BSDINSTALL_TMPETC/._rc.conf.net
144222468Sbz	ifconfig ${INTERFACE} `eval echo \\\$ifconfig_${INTERFACE}_ipv6`
145225429Sbz	if [ -n "${ipv6_defaultrouter}" ]; then
146225429Sbz		route delete -inet6 default
147225429Sbz		route add -inet6 default ${ipv6_defaultrouter}
148225429Sbz	fi
149222468Sbzfi
150222468Sbz
151