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