]>
git.ipfire.org Git - people/stevee/network.git/blob - functions.routing
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
22 function routing_has_default
() {
23 ip route |
grep -q "^default"
26 function routing_default_update
() {
29 local zones
=$
(zones_get_nonlocal
)
30 if [ -z "${zones}" ]; then
31 zones
=$
(zones_get_local
)
40 for proto
in ${IP_SUPPORTED_PROTOCOLS}; do
44 cmd
="ip $([ "${proto}" = "ipv6
" ] && echo "-6") route"
46 for zone
in ${zones}; do
47 # Skip if zone is not up
48 routing_db_exists
${zone} ${proto} ||
continue
50 if [ "$(routing_db_get ${zone} ${proto} active)" = "1" ]; then
51 gateway
=$
(routing_db_get
${zone} ${proto} remote-ip-address
)
53 assert device_exists
${zone}
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}"
60 # On other devices, we will use the gateway if we got one.
61 elif isset gateway
; then
62 routes
="${routes} nexthop via ${gateway}"
64 # If none of the cases above apply, we cannot go on.
70 weight
=$
(routing_db_get
${zone} ${proto} weight
)
71 if isinteger
${weight}; then
72 routes
="${routes} weight ${weight}"
75 log DEBUG
"Ignoring zone '${zone}' which is not active."
79 # Remove too much spaces.
80 routes
=$
(echo ${routes})
82 # Remove all default routes.
83 while ${cmd} |
grep -q "^default"; do
87 if [ -z "${routes}" ]; then
88 log INFO
"Removed default route for ${proto}."
92 log INFO
"Setting default route for ${proto}: ${routes}"
94 cmd
${cmd} add default
${routes}
99 # Apply radvd configuration.
106 function routing_table_exists
() {
109 grep -q "${zone}$" < /etc
/iproute
2/rt_tables
112 function routing_table_create
() {
115 if routing_table_exists
${zone}; then
119 log INFO
"Creating routing table for zone '${zone}'"
121 local id
=$
(( ${zone#${ZONE_NONLOCAL}} + 1 ))
123 echo "${id} ${zone}" >> /etc
/iproute
2/rt_tables
126 function routing_table_remove
() {
127 : # XXX do we need this?
130 function routing_db_path
() {
136 assert isoneof proto
${IP_SUPPORTED_PROTOCOLS}
138 echo "${ROUTING_DB_DIR}/${zone}/${proto}"
141 function routing_db_exists
() {
142 [ -d "$(routing_db_path $@)" ]
145 function routing_db_create
() {
146 routing_db_exists $@
&& return ${EXIT_OK}
148 mkdir
-p $
(routing_db_path $@
)
151 function routing_db_remove
() {
152 rm -rf $
(routing_db_path $@
)
155 function routing_db_set
() {
163 log INFO
"Updating database (${zone} - ${proto}): ${parameter} = ${value}"
165 routing_db_create
${zone} ${proto}
167 echo "${value}" > $(routing_db_path ${zone} ${proto})/${parameter}
170 function routing_db_get() {
176 cat $(routing_db_path ${zone} ${proto})/${parameter} 2>/dev/null
179 function routing_db_from_ppp() {
186 # Save ppp configuration
187 routing_db_set ${zone} ${proto} type "ppp
"
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}
197 routing_db_set ${zone} ${proto} dns ${PPP_DNS1} ${PPP_DNS2}
199 routing_db_set ${zone} ${proto} remote-address ${PPP_MACREMOTE,,}
202 function routing_update() {
206 # Nothing to do for local zones.
207 if zone_is_local ${zone}; then
216 if [ "${proto}" = "ipv6
" ]; then
217 ip_cmd="${ip_cmd} -6"
220 # Create routing table if not exists
221 routing_table_create ${table}
223 log DEBUG "Flushing routing table
${table}"
224 cmd ${ip_cmd} route flush table ${table}
226 # Exit here if there is no routing information.
227 if ! routing_db_exists ${zone} ${proto}; then
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)
236 local net_address=$(ipv4_get_netaddress ${local_ip_address})
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}
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}"
248 routing_cmd="${routing_cmd} dev
${zone}"
252 cmd ${ip_cmd} rule add from ${local_ip_address} lookup ${table}