]> git.ipfire.org Git - people/stevee/network.git/blob - functions.routing
DNS: Add RDNSS functionality.
[people/stevee/network.git] / functions.routing
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 function routing_has_default() {
23 ip route | grep -q "^default"
24 }
25
26 function routing_default_update() {
27 local routes
28
29 local zones=$(zones_get_nonlocal)
30 if [ -z "${zones}" ]; then
31 zones=$(zones_get_local)
32 fi
33
34 local gateway
35 local proto
36 local weight
37 local zone
38 local cmd
39
40 for proto in ${IP_SUPPORTED_PROTOCOLS}; do
41 # Clear routes
42 routes=""
43
44 cmd="ip $([ "${proto}" = "ipv6" ] && echo "-6") route"
45
46 for zone in ${zones}; do
47 # Skip if zone is not up
48 routing_db_exists ${zone} ${proto} || continue
49
50 if [ "$(routing_db_get ${zone} ${proto} active)" = "1" ]; then
51 gateway=$(routing_db_get ${zone} ${proto} remote-ip-address)
52
53 assert device_exists ${zone}
54
55 # If we have got a Point-to-Point device, we will directly send all
56 # packets into the pipe.
57 if device_is_ptp ${zone}; then
58 routes="${routes} dev ${zone}"
59
60 # On other devices, we will use the gateway if we got one.
61 elif isset gateway; then
62 routes="${routes} nexthop via ${gateway}"
63
64 # If none of the cases above apply, we cannot go on.
65 else
66 continue
67 fi
68
69 # Apply weight.
70 weight=$(routing_db_get ${zone} ${proto} weight)
71 if isinteger ${weight}; then
72 routes="${routes} weight ${weight}"
73 fi
74 else
75 log DEBUG "Ignoring zone '${zone}' which is not active."
76 fi
77 done
78
79 # Remove too much spaces.
80 routes=$(echo ${routes})
81
82 # Remove all default routes.
83 while ${cmd} | grep -q "^default"; do
84 ${cmd} del default
85 done
86
87 if [ -z "${routes}" ]; then
88 log INFO "Removed default route for ${proto}."
89 return ${EXIT_OK}
90 fi
91
92 log INFO "Setting default route for ${proto}: ${routes}"
93
94 cmd ${cmd} add default ${routes}
95 assert [ $? -eq 0 ]
96
97 case "${proto}" in
98 ipv6)
99 # Apply radvd configuration.
100 radvd_update
101 ;;
102 esac
103 done
104 }
105
106 function routing_table_exists() {
107 local zone=${1}
108
109 grep -q "${zone}$" < /etc/iproute2/rt_tables
110 }
111
112 function routing_table_create() {
113 local zone=${1}
114
115 if routing_table_exists ${zone}; then
116 return ${EXIT_OK}
117 fi
118
119 log INFO "Creating routing table for zone '${zone}'"
120
121 local id=$(( ${zone#${ZONE_NONLOCAL}} + 1 ))
122
123 echo "${id} ${zone}" >> /etc/iproute2/rt_tables
124 }
125
126 function routing_table_remove() {
127 : # XXX do we need this?
128 }
129
130 function routing_db_path() {
131 local zone=${1}
132 local proto=${2}
133
134 assert isset zone
135 assert isset proto
136 assert isoneof proto ${IP_SUPPORTED_PROTOCOLS}
137
138 echo "${ROUTING_DB_DIR}/${zone}/${proto}"
139 }
140
141 function routing_db_exists() {
142 [ -d "$(routing_db_path $@)" ]
143 }
144
145 function routing_db_create() {
146 routing_db_exists $@ && return ${EXIT_OK}
147
148 mkdir -p $(routing_db_path $@)
149 }
150
151 function routing_db_remove() {
152 rm -rf $(routing_db_path $@)
153 }
154
155 function routing_db_set() {
156 local zone=${1}
157 local proto=${2}
158 local parameter=${3}
159 shift 3
160
161 local value="$@"
162
163 log INFO "Updating database (${zone} - ${proto}): ${parameter} = ${value}"
164
165 routing_db_create ${zone} ${proto}
166
167 echo "${value}" > $(routing_db_path ${zone} ${proto})/${parameter}
168 }
169
170 function routing_db_get() {
171 local zone=${1}
172 local proto=${2}
173 local parameter=${3}
174 shift 3
175
176 cat $(routing_db_path ${zone} ${proto})/${parameter} 2>/dev/null
177 }
178
179 function routing_db_from_ppp() {
180 local zone=${1}
181 local proto=${2}
182
183 assert isset zone
184 assert isset proto
185
186 # Save ppp configuration
187 routing_db_set ${zone} ${proto} type "ppp"
188
189 if [ "${proto}" = "ipv6" ]; then
190 routing_db_set ${zone} ${proto} local-ip-address ${PPP_LLLOCAL}
191 routing_db_set ${zone} ${proto} remote-ip-address ${PPP_LLREMOTE}
192 elif [ "${proto}" = "ipv4" ]; then
193 routing_db_set ${zone} ${proto} local-ip-address ${PPP_IPLOCAL}
194 routing_db_set ${zone} ${proto} remote-ip-address ${PPP_IPREMOTE}
195 fi
196
197 routing_db_set ${zone} ${proto} dns ${PPP_DNS1} ${PPP_DNS2}
198
199 routing_db_set ${zone} ${proto} remote-address ${PPP_MACREMOTE,,}
200 }
201
202 function routing_update() {
203 local zone=${1}
204 assert isset zone
205
206 # Nothing to do for local zones.
207 if zone_is_local ${zone}; then
208 return ${EXIT_OK}
209 fi
210
211 local proto=${2}
212 local table=${zone}
213 assert isset proto
214
215 local ip_cmd="ip"
216 if [ "${proto}" = "ipv6" ]; then
217 ip_cmd="${ip_cmd} -6"
218 fi
219
220 # Create routing table if not exists
221 routing_table_create ${table}
222
223 log DEBUG "Flushing routing table ${table}"
224 cmd ${ip_cmd} route flush table ${table}
225
226 # Exit here if there is no routing information.
227 if ! routing_db_exists ${zone} ${proto}; then
228 return ${EXIT_OK}
229 fi
230
231 local local_ip_address=$(routing_db_get ${zone} ${proto} local-ip-address)
232 local remote_ip_address=$(routing_db_get ${zone} ${proto} remote-ip-address)
233
234 case "${proto}" in
235 ipv4)
236 local net_address=$(ipv4_get_netaddress ${local_ip_address})
237
238 log DEBUG "Adding route for subnet ${local_ip_address} to table ${table}"
239 cmd ${ip_cmd} route add table ${table} ${net_address} dev ${zone}
240 ;;
241 esac
242
243 log DEBUG "Adding default route for table ${table}"
244 local routing_cmd="${ip_cmd} route add table ${table} default"
245 if isset remote_ip_address; then
246 routing_cmd="${routing_cmd} via ${remote_ip_address}"
247 else
248 routing_cmd="${routing_cmd} dev ${zone}"
249 fi
250 cmd ${routing_cmd}
251
252 cmd ${ip_cmd} rule add from ${local_ip_address} lookup ${table}
253 }