]> git.ipfire.org Git - network.git/blame - functions.routing
Fixed some issues with pppoe dial-in.
[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
MT
34 local gateway
35 local weight
b816e04b 36 local zone
ff8ec5ef 37
b816e04b 38 local proto="ipv4"
ff8ec5ef 39
b816e04b 40 for zone in ${zones}; do
ff8ec5ef 41 # Skip if zone is not up
b816e04b 42 routing_db_exists ${zone} ${proto} || continue
ff8ec5ef 43
b816e04b
MT
44 if [ "$(routing_db_get ${zone} ${proto} active)" = "1" ]; then
45 gateway=$(routing_db_get ${zone} ${proto} remote-ip-address)
46 weight=$(routing_db_get ${zone} ${proto} weight)
ff8ec5ef
MT
47
48 routes="${routes} nexthop via ${gateway}"
b816e04b 49
ff8ec5ef
MT
50 if [ -n "${weight}" ]; then
51 routes="${routes} weight ${weight}"
52 fi
53 else
54 log DEBUG "Ignoring zone '${zone}' which is not active."
55 fi
56 done
57
58 if [ -z "${routes}" ]; then
b816e04b
MT
59 log INFO "Removing default route."
60
ff8ec5ef
MT
61 if routing_has_default; then
62 ip route del default
63 fi
64 return ${EXIT_OK}
65 fi
66
b816e04b
MT
67 # Remove too much spaces.
68 routes=$(echo ${routes})
69
70 log INFO "Setting default route: ${routes}"
71
ff8ec5ef
MT
72 ip route replace default ${routes}
73}
74
75function routing_table_exists() {
76 local zone=${1}
77
78 grep -q "${zone}$" < /etc/iproute2/rt_tables
79}
80
81function routing_table_create() {
82 local zone=${1}
83
ff8ec5ef
MT
84 if routing_table_exists ${zone}; then
85 return ${EXIT_OK}
86 fi
87
88 log INFO "Creating routing table for zone '${zone}'"
89
90 local id=$(( ${zone#red} + 1 ))
91
92 echo "${id} ${zone}" >> /etc/iproute2/rt_tables
93}
94
95function routing_table_remove() {
96 : # XXX do we need this?
97}
b816e04b
MT
98
99function routing_db_path() {
100 local zone=${1}
101 local proto=${2}
102
103 assert isset zone
104 assert isset proto
105 assert isoneof proto ${IP_SUPPORTED_PROTOCOLS}
106
107 echo "${ROUTING_DB_DIR}/${zone}/${proto}"
108}
109
110function routing_db_exists() {
111 [ -d "$(routing_db_path $@)" ]
112}
113
114function routing_db_create() {
115 routing_db_exists $@ && return ${EXIT_OK}
116
117 mkdir -p $(routing_db_path $@)
118}
119
120function routing_db_remove() {
121 rm -rf $(routing_db_path $@)
122}
123
124function routing_db_set() {
125 local zone=${1}
126 local proto=${2}
127 local parameter=${3}
128 shift 3
129
130 local value="$@"
131
132 log INFO "Updating database (${zone} - ${proto}): ${parameter} = ${value}"
133
134 routing_db_create ${zone} ${proto}
135
136 echo "${value}" > $(routing_db_path ${zone} ${proto})/${parameter}
137}
138
139function routing_db_get() {
140 local zone=${1}
141 local proto=${2}
142 local parameter=${3}
143 shift 3
144
145 cat $(routing_db_path ${zone} ${proto})/${parameter} 2>/dev/null
146}
147
148function routing_db_from_ppp() {
149 local zone=${1}
150 local proto=${2}
151
2c973348
MT
152 assert isset zone
153 assert isset proto
154
b816e04b
MT
155 # Save ppp configuration
156 routing_db_set ${zone} ${proto} type "ppp"
157 routing_db_set ${zone} ${proto} local-ip-address ${PPP_IPLOCAL}
158 routing_db_set ${zone} ${proto} remote-ip-address ${PPP_IPREMOTE}
159
160 routing_db_set ${zone} ${proto} dns ${PPP_DNS1} ${PPP_DNS2}
161
162 routing_db_set ${zone} ${proto} remote-address ${PPP_MACREMOTE,,}
163}
164
165function routing_update() {
166 local zone=${1}
2c973348 167 assert isset zone
b816e04b
MT
168
169 # Nothing to do for local zones.
170 if zone_is_local ${zone}; then
171 return ${EXIT_OK}
172 fi
173
174 local proto=${2}
175 local table=${zone}
2c973348 176 assert isset proto
b816e04b
MT
177
178 # Create routing table if not exists
179 routing_table_create ${table}
180
181 log DEBUG "Flushing routing table ${table}"
182 cmd ip route flush table ${table}
183
184 local local_ip_address=$(routing_db_get ${zone} ${proto} local-ip-address)
185
186 # XXX does not work.
187 #log DEBUG "Adding route for subnet ${local_ip_address} to table ${table}"
188 #cmd ip route add table ${table} ${local_ip_address} dev ${zone}
189
190 local remote_ip_address=$(routing_db_get ${zone} ${proto} remote-ip-address)
191
192 if isset remote_ip_address; then
193 log DEBUG "Adding default route for table ${table}"
194
195 cmd ip route add table ${table} default nexthop via ${remote_ip_address}
196 fi
197
198 cmd ip rule add from ${local_ip_address} lookup ${table}
199}