]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.bird
950bb787a27442409d535e5f934766f7a6a6cd09
[people/ms/network.git] / src / functions / functions.bird
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2018 IPFire Network Development Team #
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 BIRD_CONF="/etc/bird.conf"
23
24 bird_start() {
25 service_start "bird.service"
26 }
27
28 bird_stop() {
29 service_stop "bird.service"
30 }
31
32 bird_reload() {
33 service_reload "bird.service"
34 }
35
36 bird_generate_config() {
37 log DEBUG "Write BIRD configuration file"
38
39 # Write header
40 config_header "bird" > ${BIRD_CONF}
41
42 # Write some basic settings
43 local proto
44 (
45 print "# Log everything to syslog"
46 print "log syslog all;"
47 print
48
49 print "# Turn on internal watchdog"
50 print "watchdog warning 5s;"
51 print "watchdog timeout 30s;"
52 print
53
54 print "# Define default route tables"
55 print "ipv6 table master6;"
56 print "ipv4 table master4;"
57
58 print "# Enable device configuration"
59 print "protocol device {}"
60 print
61
62 print "# Export all routes to kernel"
63 for proto in ${IP_SUPPORTED_PROTOCOLS}; do
64 print "protocol kernel {"
65 print " ${proto} {"
66 print " table ${proto/ipv/master};"
67 print " export all;"
68 print " };"
69 print " learn;"
70 print "}"
71 print
72 done
73 ) >> ${BIRD_CONF}
74
75 # Static routes
76 for proto in ${IP_SUPPORTED_PROTOCOLS}; do
77 print "protocol static {"
78 print " ${proto};"
79 print
80
81 # Read routes for this protocol from configuration
82 __bird_static_routes "${proto}"
83
84 print "}"
85 print
86 done >> ${BIRD_CONF}
87
88 # Write IPv6 Router Advertisement configuration
89 __bird_ipv6_radv >> ${BIRD_CONF}
90 }
91
92 __bird_static_routes() {
93 local proto="${1}"
94 assert isset proto
95
96 local ${NETWORK_CONFIG_ROUTES_PARAMS}
97 local line
98 while read line; do
99 route_parse_line "${line}"
100 [ $? -eq ${EXIT_OK} ] || continue
101
102 local type
103 local arg
104 for arg in unreachable prohibit blackhole; do
105 if enabled "${arg}"; then
106 type="${arg}"
107 break
108 fi
109 done
110
111 # Skip all routes of another protocol
112 local _proto="$(ip_detect_protocol "${network}")"
113 if [ "${proto}" != "${_proto}" ]; then
114 continue
115 fi
116
117 case "${type}" in
118 unreachable|prohibit|blackhole)
119 print " route ${network} ${type};"
120 ;;
121
122 *)
123 print " route ${network} via ${gateway};"
124 ;;
125 esac
126 done < ${NETWORK_CONFIG_ROUTES}
127 }
128
129 __bird_ipv6_radv() {
130 print "protocol radv {"
131
132 local zone
133 for zone in $(zones_get_local); do
134 log DEBUG "Writing bird radv configuration for ${zone}"
135
136 # Skip if there is no prefix or prefix is link-local.
137 local addr="$(db_get "${zone}/ipv6/local-ip-address")"
138 if [ -z "${addr}" ] || [ "${addr:0:5}" = "fe80:" ]; then
139 continue
140 fi
141
142 # Check if the subnet is configured by the DHCP server.
143 local dhcp="false"
144 local prefix="$(ipv6_get_network "${addr}")"
145 if isset prefix && dhcpd_subnet_match ipv6 "${prefix}"; then
146 dhcp="true"
147 fi
148
149 print " interface \"${zone}\" {"
150 # Failover to other routers within 10s
151 print " max ra interval 10;"
152
153 # Tell clients we are running DHCP
154 if enabled dhcp; then
155 print " managed yes;"
156 print " other config yes;"
157 fi
158
159 if device_exists "${zone}"; then
160 # Announce link MTU
161 local mtu="$(device_get_mtu "${zone}")"
162 print " link mtu ${mtu};"
163 fi
164
165 print # empty line
166
167 # Announce all prefixes
168 print " prefix ::/0 {"
169
170 if enabled dhcp; then
171 print " autonomous off;"
172 fi
173
174 print " };"
175 print " };\n"
176 done
177
178 # Advertise any DNS servers
179 if enabled DNS_ADVERTISE_SERVERS; then
180 # Get a list of all IPv6 name servers
181 local servers=()
182 local server
183 for server in $(dns_server_list_sorted); do
184 # Skip any non-IPv6 servers
185 ipv6_is_valid "${server}" || continue
186
187 servers+=( "${server}" )
188 done
189
190 if isset servers; then
191 print " rdnss {"
192
193 local server
194 for server in ${servers}; do
195 print " ns ${server};"
196 done
197
198 print " };"
199 fi
200 fi
201
202 # DNS Search Domain
203 print " dnssl {"
204
205 local domain
206 for domain in $(dns_get_search_domains); do
207 print " domain \"${domain}\";"
208 done
209
210 print " };"
211
212 print "}\n"
213 }