]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.radvd
bird: Write IPv6 router advertisement configuration
[people/ms/network.git] / src / functions / functions.radvd
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 RADVD_CONFIGFILE="/etc/radvd.conf"
23
24 radvd_update() {
25 # (Re-)write the configuration file
26 if radvd_write_config; then
27 # Reload the radvd service if it is already running
28 if service_is_active radvd; then
29 service_reload radvd
30 return ${EXIT_OK}
31 fi
32
33 # Start the radvd service
34 service_start radvd
35 fi
36 }
37
38 radvd_clear_config() {
39 log DEBUG "Clearing radv daemon configuration file"
40
41 config_header "radv daemon configuration file" > ${RADVD_CONFIGFILE}
42
43 return ${EXIT_OK}
44 }
45
46 radvd_write_config() {
47 radvd_clear_config
48
49 # Write the configuration for all zones.
50 local zone
51
52 # The return value determine if radvd is started or not
53 local return_value=${EXIT_FALSE}
54
55 for zone in $(zones_get_local); do
56 if __radvd_config_interface ${zone}; then
57 # We return TRUE when __radvd_config_interface returns True
58 return_value=${EXIT_TRUE}
59 fi
60 done >> ${RADVD_CONFIGFILE}
61
62 return ${return_value}
63 }
64
65 # This function return ${EXIT_FALSE} if no radvd config was written and ${EXIT_TRUE} in all other cases
66 __radvd_config_interface() {
67 local zone=${1}
68 assert isset zone
69
70 log DEBUG "Writing radvd configuration for ${zone}."
71
72 # If the interface does not provide any routing information,
73 # we can skip this whole stuff.
74 if ! db_exists "${zone}/ipv6"; then
75 return ${EXIT_FALSE}
76 fi
77
78 # Skip if zone is not active.
79 local active="$(db_get "${zone}/ipv6/active")"
80 [ "${active}" = "0" ] && return ${EXIT_FALSE}
81
82 # Skip if there is no prefix or prefix is link-local.
83 local addr="$(db_get "${zone}/ipv6/local-ip-address")"
84 if [ -z "${addr}" ] || [ "${addr:0:5}" = "fe80:" ]; then
85 return ${EXIT_FALSE}
86 fi
87
88 # Check if the subnet is configured by the DHCP server.
89 local dhcpd="false"
90 local prefix="$(ipv6_get_network "${addr}")"
91 if isset prefix && dhcpd_subnet_match ipv6 "${prefix}"; then
92 dhcpd="true"
93 fi
94
95 print "interface ${zone} {"
96 print " AdvSendAdvert on;"
97 print " MinRtrAdvInterval 3;"
98 print " MaxRtrAdvInterval 10;"
99 print " IgnoreIfMissing on;"
100
101 if enabled dhcpd; then
102 print " AdvManagedFlag on;"
103 print " AdvOtherConfigFlag on;"
104 fi
105
106 print
107 print " prefix ::/64 {"
108 print " AdvOnLink on;"
109
110 if enabled dhcpd; then
111 print " AdvRouterAddr off;"
112 print " AdvAutonomous off;"
113 else
114 print " AdvRouterAddr on;"
115 print " AdvAutonomous on;"
116 fi
117
118 print " };"
119 print
120
121 # Add the DNS configuration.
122 __radvd_config_dns ${zone}
123
124 print "};"
125 print
126
127 return ${EXIT_TRUE}
128 }
129
130 __radvd_config_dns() {
131 local zone=${1}
132
133 # Do nothing, when this option is not enabled.
134 enabled DNS_ADVERTISE_SERVERS || return ${EXIT_OK}
135
136 # XXX it is kind of difficult to announce our local
137 # resolver.
138
139 local server servers
140 for server in $(dns_server_list_sorted); do
141 # Filter out non IPv6 addresses.
142 ipv6_is_valid ${server} || continue
143
144 servers="${servers} ${server}"
145 done
146
147 # Remove whitespaces.
148 servers=$(echo ${servers})
149
150 # If there are no servers to announce, we stop right here.
151 if ! isset servers; then
152 log DEBUG "No servers to announce."
153 return ${EXIT_OK}
154 fi
155
156 print " RDNSS ${servers} {"
157 print " # Use the defaults here."
158 print " };"
159 print
160 }