1#!/bin/sh
2
3#
4# Setup address label from /etc/gai.conf
5#
6# Written by YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>, 2010.
7#
8
9IP=ip
10DEFAULT_GAICONF=/etc/gai.conf
11verbose=
12debug=
13
14function run ()
15{
16	if [ x"$verbose" != x"" ]; then
17		echo "$@"
18	fi
19	if [ x"$debug" = x"" ]; then
20		"$@"
21	fi
22}
23
24function do_load_config ()
25{
26	file=$1; shift
27	flush=1
28	cat $file | while read command prefix label; do
29		if [ x"$command" = x"#label" ]; then
30			if [ ${flush} = 1 ]; then
31				run ${IP} -6 addrlabel flush
32				flush=0
33			fi
34			run ${IP} -6 addrlabel add prefix $prefix label $label
35		fi
36	done
37}
38
39function do_list_config ()
40{
41	${IP} -6 addrlabel list | while read p pfx l lbl; do
42		echo label ${pfx} ${lbl}
43	done
44}
45
46function help ()
47{
48	echo "Usage: $0 [-v] {--list | --config [ ${DEFAULT_GAICONF} ] | --default}"
49	exit 1
50}
51
52TEMP=`getopt -o c::dlv -l config::,default,list,verbose -n gaiconf -- "$@"`
53
54if [ $? != 0 ]; then
55	echo "Terminating..." >&2
56	exit 1
57fi
58
59TEMPFILE=`mktemp`
60
61eval set -- "$TEMP"
62
63while true ; do
64	case "$1" in
65		-c|--config)
66			if [ x"$cmd" != x"" ]; then
67				help
68			fi
69			case "$2" in
70			"")	gai_conf="${DEFAULT_GAICONF}"
71				shift 2
72				;;
73			*)	gai_conf="$2"
74				shift 2
75			esac
76			cmd=config
77			;;
78		-d|--default)
79			if [ x"$cmd" != x"" ]; then
80				help
81			fi
82			gai_conf=${TEMPFILE}
83			cmd=config
84			;;
85		-l|--list)
86			if [ x"$cmd" != x"" ]; then
87				help
88			fi
89			cmd=list
90			shift
91			;;
92		-v)
93			verbose=1
94			shift
95			;;
96		--)
97			shift;
98			break
99			;;
100		*)
101			echo "Internal error!" >&2
102			exit 1
103			;;
104	esac
105done
106
107case "$cmd" in
108	config)
109		if [ x"$gai_conf" = x"${TEMPFILE}" ]; then
110			sed -e 's/^[[:space:]]*//' <<END_OF_DEFAULT >${TEMPFILE}
111				label ::1/128       0
112				label ::/0          1
113				label 2002::/16     2
114				label ::/96         3
115				label ::ffff:0:0/96 4
116				label fec0::/10     5
117				label fc00::/7      6
118				label 2001:0::/32   7
119END_OF_DEFAULT
120		fi
121		do_load_config "$gai_conf"
122		;;
123	list)
124		do_list_config
125		;;
126	*)
127		help
128		;;
129esac
130
131rm -f "${TEMPFILE}"
132
133exit 0
134
135