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