xref: /plan9/rc/bin/ipv6on (revision 588d0145e19f8596f2f4442d05dd8a9eda147983)
1#!/bin/rc
2# ipv6on [netdir ndbfile] - configure an interface for ipv6.
3#	use ipv6 address from ndb if present,
4#	else do stateless address autoconfiguration (SLAAC).
5if (! ~ $#* 0 2 3) {
6	echo usage: $0 '[netdir ndbfile [obs-gw-v4-name]]' >[1=2]
7	exit usage
8}
9rfork e
10fn ipq {			# attr val rattr
11	ndb/ipquery $* | sed 's/^[a-z0-9]+=//'
12}
13
14if (~ $#* 0) {
15	netdir=/net
16	ndbf=/lib/ndb/local
17}
18if not {
19	netdir=$1
20	ndbf=$2
21}
22if (~ $netdir /net) {
23	xsfx=()
24	xdir=()
25}
26if not {
27	xsfx=(-x `{echo $netdir | sed 's;^/net;;'})
28	xdir=(-x $netdir)
29}
30
31fn nonnil {		# variable
32	if (~ $#$1 0) {
33		echo ipv6on: no ip for $1 >[1=2]
34		exit no-ip
35	}
36	if (! ~ $#$1 1) {
37		echo ipv6on: multiple ips for $1 >[1=2]
38		exit multiple-ips
39	}
40}
41
42#
43# configure v6 for link-local addresses (fe80::) & multicast (ff02::).
44# accept router advertisements, which will contain a default route.
45#
46if (! ip/ipconfig -6 $xdir ether $netdir/ether?)
47	exit 'ipconfig -6 failed'
48ip/ipconfig $xdir ether $netdir/ether? ra6 recvra 1
49
50# lookup any v6 address for me in ndb, else use slaac
51myeth=`{cat $netdir/ether?/addr}
52mev6=`{ndb/query -f $ndbf ether $myeth ipv6}
53if (~ $#mev6 0 || ~ $mev6 '')
54	mev6=`{ndb/query -f $ndbf sys $sysname ipv6}
55if (~ $#mev6 0 || ~ $mev6 '') {
56	# slaac: could perhaps get prefix from above RA
57	ipnet=`{ipq ether $myeth ipnet}
58	if (~ $#ipnet 0 || ~ $ipnet '')
59		ipnet=`{ipq sys $sysname ipnet}
60	nonnil ipnet
61	v6pfx=`{ipq ipnet $ipnet ipv6pfx}
62	nonnil v6pfx
63	mev6=`{ip/linklocal $myeth | sed 's/^fe80:/'$v6pfx'/'}
64}
65nonnil mev6
66
67#
68# configure my global v6 addresses
69#
70v6mask=`{ipq ip $mev6 ipmask}
71ip/ipconfig $xdir ether $netdir/ether?	add $mev6 $v6mask
72ip/ipconfig $xdir loopback /dev/null	add $mev6 /128
73