netconfig_ipv6 revision 222482
1#!/bin/sh
2#-
3# Copyright (c) 2011 Nathan Whitehorn
4# All rights reserved.
5# Copyright (c) 2011 The FreeBSD Foundation
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: head/usr.sbin/bsdinstall/scripts/netconfig_ipv6 222482 2011-05-30 08:40:59Z bz $
33
34#
35# TODO: 
36# - Add -R /sbin/resolvconf to rtsol once support is in tree.
37# - Add DHCPv6 support once FreeBSD ships with it.
38# 
39
40: ${DIALOG_OK=0}
41: ${DIALOG_CANCEL=1}
42: ${DIALOG_HELP=2}
43: ${DIALOG_EXTRA=3}
44: ${DIALOG_ITEM_HELP=4}
45: ${DIALOG_ESC=255}
46
47INTERFACE=$1
48case "${INTERFACE}" in
49"")	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
50	    --msgbox 'No interface specified for IPv6 configuration.' 0 0
51	exit 1
52	;;
53esac
54
55AGAIN=""
56while : ; do
57	MSG="Would you like to try stateless address autoconfiguration (SLAAC)${AGAIN}?"
58	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
59	    --yesno "${MSG}" 0 0
60	if [ $? -eq $DIALOG_OK ]; then
61		if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
62			dialog --backtitle 'FreeBSD Installer' \
63			    --infobox "Sending Router Solicitation ..." 0 0
64			ifconfig ${INTERFACE} inet6 accept_rtadv up
65			rtsol -F $INTERFACE 2>> $BSDINSTALL_LOG
66			if [ $? -ne 0 ]; then
67				dialog --backtitle 'FreeBSD Installer' --msgbox "SLAAC failed." 0 0
68				AGAIN=" again"
69				continue
70			fi
71		fi
72		echo ifconfig_${INTERFACE}_ipv6=\"inet6 accept_rtadv\" >> $BSDINSTALL_TMPETC/rc.conf.net
73		exit 0
74	else
75		break
76	fi
77done
78
79ROUTER6=`netstat -Wrn -f inet6 | awk '/default/ {printf("%s\n", $2);}'`
80ADDRS=`ifconfig ${INTERFACE} inet6 | \
81awk  -v dfr="${ROUTER6}" '
82BEGIN {
83	n=0;
84}
85{
86	if (/inet6/) {
87		if (match($2, "^fe80:")) { next; };
88		# For the moment ignore all but the first address; it might confuse the user.
89		if (n > 0) { next; };
90		n++;
91		printf "\"IPv6 Address\" %d 0 \"%s/%s\" %d 16 50 0 0 ", n, $2, $4, n;
92	}
93}
94END {
95	if (n == 0) {
96		n++;
97		printf "\"IPv6 Address\" %d 0 \"\" %d 16 50 0 0 ", n, n;
98	}
99	n++;
100	# Nasty trick adding a (hidden, same y) read-only field as a marker
101	# to separate interface address(es) from the default router.
102	printf "\"Default Router\" %d 0 \"%s\" %d 16 50 0 2 ", n, "DefaultRouter", n;
103	printf "\"Default Router\" %d 0 \"%s\" %d 16 50 0 0 ", n, dfr, n;
104}'`
105
106exec 3>&1
107IF_CONFIG=$(echo ${ADDRS} | xargs dialog --backtitle 'FreeBSD Installer' \
108	--title 'Network Configuration' \
109	--mixedform 'Static IPv6 Network Interface Configuration' 0 0 0 \
1102>&1 1>&3)
111if [ $? -eq $DIALOG_CANCEL ]; then exit 1; fi
112exec 3>&-
113
114echo ${IF_CONFIG} | tr ' ' '\n' | \
115awk -v iface="${INTERFACE}" '
116BEGIN {
117	dfr=0;
118	count=0;
119}
120{
121	if (/^[[:space:]]+$/) {
122		next;
123	}
124	if (/DefaultRouter/) {
125		dfr=1;
126		next;
127	}
128	if (dfr == 1) {
129		printf("ipv6_defaultrouter=\"%s\"\n", $1);
130		next;
131	}
132	if (count > 0) {
133		# Ignore all but the first IP address for now.
134		next;
135	}
136	count++;
137	if (!match($1, "/")) {
138		sub("$", "/64", $1);
139	}
140	printf("ifconfig_%s_ipv6=\"inet6 %s\"\n", iface, $1);
141}' >> $BSDINSTALL_TMPETC/rc.conf.net
142
143if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
144	. $BSDINSTALL_TMPETC/rc.conf.net
145	ifconfig ${INTERFACE} `eval echo \\\$ifconfig_${INTERFACE}_ipv6`
146	route delete default
147	route add default ${ipv6_defaultrouter}
148fi
149
150