1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008 Alina Friedrichsen <x-alina@gmx.net>
5 RADVD_INTERFACE_STRING_OPTIONS='MaxRtrAdvInterval MinRtrAdvInterval MinDelayBetweenRAs AdvLinkMTU AdvReachableTime AdvRetransTimer AdvCurHopLimit AdvDefaultLifetime AdvDefaultPreference HomeAgentLifetime HomeAgentPreference'
6 RADVD_INTERFACE_BOOLEAN_OPTIONS='IgnoreIfMissing AdvSendAdvert UnicastOnly AdvManagedFlag AdvOtherConfigFlag AdvSourceLLAddress AdvHomeAgentFlag AdvHomeAgentInfo AdvMobRtrSupportFlag AdvIntervalOpt'
8 RADVD_PREFIX_STRING_OPTIONS='AdvValidLifetime AdvPreferredLifetime'
9 RADVD_PREFIX_BOOLEAN_OPTIONS='AdvOnLink AdvAutonomous AdvRouterAddr'
11 RADVD_ROUTE_STRING_OPTIONS='AdvRouteLifetime AdvRoutePreference'
13 RADVD_RDNSS_STRING_OPTIONS='AdvRDNSSLifetime'
15 RADVD_DNSSL_STRING_OPTIONS='AdvDNSSLLifetime'
19 [ -z "$varname" -o "$varname" != "${varname%%[!A-Za-z0-9_]*}" ] && return 1
25 [ -z "$ifname" -o "$ifname" != "${ifname%%[!A-Za-z0-9.:_-]*}" ] && return 1
31 [ -z "$ip6addr" -o "$ip6addr" != "${ip6addr%%[!A-Fa-f0-9.:]*}" ] && return 1
35 validate_ip6prefix() {
37 [ -z "$ip6prefix" -o "$ip6prefix" != "${ip6prefix%%[!A-Fa-f0-9./:]*}" ] && return 1
41 validate_radvd_string() {
43 [ -z "$radvd_string" -o "$radvd_string" != "${radvd_string%%[!a-z0-9.:_-]*}" ] && return 1
49 validate_varname "$interface" || return 1
54 config_get cfgt "$interface" TYPE
55 [ "$cfgt" != "interface" ] && return 1
56 config_get ifname "$interface" ifname
57 validate_ifname "$ifname" || return 1
58 printf '%s\n' "$ifname"
71 host) iproute2_scope=host ifconfig_scope=Host;;
72 link) iproute2_scope=link ifconfig_scope=Link;;
73 site) iproute2_scope=site ifconfig_scope=Site;;
74 global) iproute2_scope=global ifconfig_scope=Global;;
75 "") get_ip6addr "$ifname" global || get_ip6addr "$ifname" site; return;;
79 ip6addr=$(LANG=C ip -f inet6 -- addr show dev "$ifname" 2> /dev/null | sed -n -e 's/^ *inet6 \([A-Fa-f0-9.:]*[/][0-9]*\) scope '"$iproute2_scope"' $/\1/p' | head -n 1)
80 if [ -z "$ip6addr" ]; then
81 ip6addr=$(LANG=C ifconfig "$ifname" 2> /dev/null | sed -n -e 's/^ *inet6 addr: \([A-Fa-f0-9.:]*[/][0-9]*\) Scope:'"$ifconfig_scope"'$/\1/p' | tail -n 1)
82 [ -z "$ip6addr" ] && return 1
85 printf '%s\n' "$ip6addr"
90 radvd_find_config_file() {
92 validate_varname "$cfg" || return 0
94 config_get_bool ignore "$cfg" ignore 0
95 [ "$ignore" -ne 0 ] && return 0
96 config_get RADVD_CONFIG_FILE "$cfg" config_file
101 radvd_add_interface() {
103 validate_varname "$cfg" || return 0
110 config_get_bool ignore "$cfg" ignore 0
111 [ "$ignore" -ne 0 ] && return 0
113 config_get interfaces "$cfg" interface
114 for interface in $interfaces; do
115 validate_varname "$interface" || continue
117 for list_interface in $RADVD_INTERFACES; do
118 [ "$interface" = "$list_interface" ] && exist=1
120 [ "$exist" -eq 0 ] && RADVD_INTERFACES="$RADVD_INTERFACES $interface"
126 radvd_write_interface() {
128 validate_varname "$cfg" || return 0
135 config_get_bool ignore "$cfg" ignore 0
136 [ "$ignore" -ne 0 ] && return 0
138 config_get interfaces "$cfg" interface
140 for interface in $interfaces; do
141 [ "$INTERFACE" = "$interface" ] && exist=1
143 [ "$exist" -eq 0 ] && return 0
145 for name in $RADVD_INTERFACE_STRING_OPTIONS; do
146 config_get value "$cfg" "$name"
147 validate_radvd_string "$value" || continue
148 printf '\t%s %s;\n' "$name" "$value"
151 for name in $RADVD_INTERFACE_BOOLEAN_OPTIONS; do
152 config_get value "$cfg" "$name"
153 [ -z "$value" ] && continue
154 config_get_bool value "$cfg" "$name" 0
155 if [ "$value" -ne 0 ]; then
156 printf '\t%s on;\n' "$name"
158 printf '\t%s off;\n' "$name"
162 config_get clients "$cfg" client
163 if [ -n "$clients" ]; then
164 printf '\n\tclients\n\t{\n'
166 for client in $clients; do
167 validate_ip6addr "$client" || continue
168 printf '\t\t%s;\n' "$client"
177 radvd_write_prefix() {
179 validate_varname "$cfg" || return 0
189 config_get_bool ignore "$cfg" ignore 0
190 [ "$ignore" -ne 0 ] && return 0
192 config_get interfaces "$cfg" interface
194 for interface in $interfaces; do
195 [ "$INTERFACE" = "$interface" ] && exist=1
197 [ "$exist" -eq 0 ] && return 0
199 config_get prefixes "$cfg" prefix "::/64"
200 for prefix in $prefixes; do
201 validate_ip6prefix "$prefix" || continue
202 printf '\n\tprefix %s\n\t{\n' "$prefix"
204 for name in $RADVD_PREFIX_STRING_OPTIONS; do
205 config_get value "$cfg" "$name"
206 validate_radvd_string "$value" || continue
207 printf '\t\t%s %s;\n' "$name" "$value"
210 for name in $RADVD_PREFIX_BOOLEAN_OPTIONS; do
211 config_get value "$cfg" "$name"
212 [ -z "$value" ] && continue
213 config_get_bool value "$cfg" "$name" 0
214 if [ "$value" -ne 0 ]; then
215 printf '\t\t%s on;\n' "$name"
217 printf '\t\t%s off;\n' "$name"
221 config_get value "$cfg" Base6to4Interface
222 if [ -n "$value" ]; then
223 if ifname=$(get_ifname "$value"); then
224 printf '\t\t%s %s;\n' "Base6to4Interface" "$ifname"
234 radvd_write_route() {
236 validate_varname "$cfg" || return 0
245 config_get_bool ignore "$cfg" ignore 0
246 [ "$ignore" -ne 0 ] && return 0
248 config_get interfaces "$cfg" interface
250 for interface in $interfaces; do
251 [ "$INTERFACE" = "$interface" ] && exist=1
253 [ "$exist" -eq 0 ] && return 0
255 config_get prefixes "$cfg" prefix
256 for prefix in $prefixes; do
257 validate_ip6prefix "$prefix" || continue
258 printf '\n\troute %s\n\t{\n' "$prefix"
260 for name in $RADVD_ROUTE_STRING_OPTIONS; do
261 config_get value "$cfg" "$name"
262 validate_radvd_string "$value" || continue
263 printf '\t\t%s %s;\n' "$name" "$value"
272 radvd_write_rdnss() {
274 validate_varname "$cfg" || return 0
285 config_get_bool ignore "$cfg" ignore 0
286 [ "$ignore" -ne 0 ] && return 0
288 config_get interfaces "$cfg" interface
290 for interface in $interfaces; do
291 [ "$INTERFACE" = "$interface" ] && exist=1
293 [ "$exist" -eq 0 ] && return 0
295 config_get addrs "$cfg" addr
297 for addr in $addrs; do
298 [ "$i" -ge 3 ] && break
299 validate_ip6addr "$addr" || continue
300 addr_list="$addr_list $addr"
304 if [ -z "$addr_list" ]; then
305 addr=$(get_ip6addr "$IFNAME" link) || return 0
306 addr_list=" ${addr%%[/]*}"
309 printf '\n\tRDNSS%s\n\t{\n' "$addr_list"
311 for name in $RADVD_RDNSS_STRING_OPTIONS; do
312 config_get value "$cfg" "$name"
313 validate_radvd_string "$value" || continue
314 printf '\t\t%s %s;\n' "$name" "$value"
322 radvd_write_dnssl() {
324 validate_varname "$cfg" || return 0
334 config_get_bool ignore "$cfg" ignore 0
335 [ "$ignore" -ne 0 ] && return 0
337 config_get interfaces "$cfg" interface
339 for interface in $interfaces; do
340 [ "$INTERFACE" = "$interface" ] && exist=1
342 [ "$exist" -eq 0 ] && return 0
344 config_get suffixes "$cfg" suffix
345 for suffix in $suffixes; do
346 validate_radvd_string "$suffix" || continue
347 suffix_list="$suffix_list $suffix"
350 printf '\n\tDNSSL%s\n\t{\n' "$suffix_list"
352 for name in $RADVD_DNSSL_STRING_OPTIONS; do
353 config_get value "$cfg" "$name"
354 validate_radvd_string "$value" || continue
355 printf '\t\t%s %s;\n' "$name" "$value"
363 radvd_write_config() {
367 config_foreach radvd_add_interface interface
368 config_foreach radvd_add_interface prefix
369 config_foreach radvd_add_interface route
370 config_foreach radvd_add_interface RDNSS
371 config_foreach radvd_add_interface DNSSL
373 for INTERFACE in $RADVD_INTERFACES; do
374 IFNAME=$(get_ifname "$INTERFACE") || continue
375 printf 'interface %s\n{\n' "$IFNAME"
376 config_foreach radvd_write_interface interface
377 config_foreach radvd_write_prefix prefix
378 config_foreach radvd_write_route route
379 config_foreach radvd_write_rdnss rdnss
380 config_foreach radvd_write_dnssl dnssl
391 config_foreach radvd_find_config_file radvd
393 if [ -z "$RADVD_CONFIG_FILE" ]; then
394 mkdir -p -- /var/etc/
395 radvd_write_config > /var/etc/radvd.conf
396 if [ -s "/var/etc/radvd.conf" ]; then
397 RADVD_CONFIG_FILE=/var/etc/radvd.conf
401 [ -z "$RADVD_CONFIG_FILE" ] && return 1
403 sysctl -w net.ipv6.conf.all.forwarding=1 > /dev/null 2> /dev/null
405 radvd -C "$RADVD_CONFIG_FILE" -m stderr_syslog -p /var/run/radvd.pid