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 routing_has_default
() {
23 ip route |
grep -q "^default"
26 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 db_exists
"${zone}/${proto}" ||
continue
50 if [ "$(db_get "${zone}/${proto}/active
")" = "1" ]; then
51 gateway
="$(db_get "${zone}/${proto}/remote-ip-address
")"
53 # Go on if the device is not there anymore.
54 device_exists
${zone} ||
continue
56 # On other devices, we will use the gateway if we got one.
57 if isset gateway
; then
58 routes
="${routes} nexthop via ${gateway}"
60 # If we have got a Point-to-Point device, we will directly send all
61 # packets into the pipe.
62 elif device_is_ptp
${zone}; then
63 routes
="${routes} dev ${zone}"
65 # If none of the cases above apply, we cannot go on.
71 weight
="$(db_get "${zone}/${proto}/weight
")"
72 if isinteger
${weight}; then
73 routes
="${routes} weight ${weight}"
76 log DEBUG
"Ignoring zone '${zone}' which is not active."
80 # Remove too much spaces.
81 routes
=$
(echo ${routes})
83 # Reload radvd configuration
84 [[ "${proto}" = "ipv6" ]] && radvd_update
86 # Remove all default routes.
87 if [ -z "${routes}" ]; then
88 cmd
${cmd} del default
89 log INFO
"Removed default route for ${proto}."
93 log INFO
"Setting default route for ${proto}: ${routes}"
95 cmd
${cmd} replace default
${routes}
98 triggers_execute_all
"online"
102 routing_db_from_ppp
() {
109 # Save ppp configuration
110 db_set
"${zone}/${proto}/type" "ppp"
112 if [ "${proto}" = "ipv6" ]; then
113 db_set
"${zone}/${proto}/local-ip-address" "${PPP_LLLOCAL}"
114 db_set
"${zone}/${proto}/remote-ip-address" "${PPP_LLREMOTE}"
115 elif [ "${proto}" = "ipv4" ]; then
116 db_set
"${zone}/${proto}/local-ip-address" "${PPP_IPLOCAL}"
117 db_set
"${zone}/${proto}/remote-ip-address" "${PPP_IPREMOTE}"
120 # Save the transmitted DNS servers
121 if isset PPP_DNS1 || isset PPP_DNS2
; then
122 db_set
"${zone}/${proto}/domain-name-servers" "${PPP_DNS1} ${PPP_DNS2}"
124 db_set
"${zone}/${proto}/domain-name-servers"
127 # Save the MAC address of the remote DSLAM
128 if isset PPP_MACREMOTE
; then
129 db_set
"${zone}/remote-address" "${PPP_MACREMOTE,,}"
137 # Nothing to do for local zones.
138 if zone_is_local
${zone}; then
147 if [ "${proto}" = "ipv6" ]; then
148 ip_cmd
="${ip_cmd} -6"
151 # Create routing table if not exists
152 route_table_create
${table}
154 log DEBUG
"Flushing routing table ${table}"
155 cmd
${ip_cmd} route flush table
${table}
157 # Exit here if there is no routing information.
158 if ! db_exists
"${zone}/${proto}"; then
162 local local_ip_address
="$(db_get "${zone}/${proto}/local-ip-address
")"
163 local remote_ip_address
="$(db_get "${zone}/${proto}/remote-ip-address
")"
167 local network
=$
(ipv4_get_network
"${local_ip_address}")
169 log DEBUG
"Adding route for subnet ${local_ip_address} to table ${table}"
170 cmd
${ip_cmd} route add table "${table}" "${network}" dev "${zone}"
174 log DEBUG "Adding default route
for table
${table}"
175 local routing_cmd="${ip_cmd} route add table
${table} default
"
176 if isset remote_ip_address; then
177 routing_cmd="${routing_cmd} via
${remote_ip_address}"
179 routing_cmd="${routing_cmd} dev
${zone}"
183 cmd ${ip_cmd} rule add from ${local_ip_address} lookup ${table}
185 # Apply all static routes