3 * The olsr.org Optimized Link-State Routing daemon(olsrd)
4 * Copyright (c) 2004-2009, the olsr.org team - see HISTORY file
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of olsr.org, olsrd nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * Visit http://www.olsr.org for more information.
36 * If you find this software useful feel free to make a donation
37 * to the project. For more information see the website or contact
38 * the copyright holders.
43 #include "net/route.h"
45 #include "kernel_routes.h"
48 #include "routing_table.h"
49 #include "olsr_logging.h"
51 #define WIN32_LEAN_AND_MEAN
57 char *StrError(unsigned int ErrNo);
60 * Insert a route in the kernel routing table
61 * @param destination the route to add
62 * @return negative on error
65 olsr_kernel_add_route(const struct rt_entry *rt, int ip_version)
68 union olsr_ip_addr mask;
70 struct interface *iface = rt->rt_best->rtp_nexthop.interface;
72 if (AF_INET != ip_version) {
80 OLSR_DEBUG(LOG_ROUTING, "KERN: Adding %s\n", olsr_rt_to_string(rt));
82 memset(&Row, 0, sizeof(MIB_IPFORWARDROW));
84 Row.dwForwardDest = rt->rt_dst.prefix.v4.s_addr;
86 if (!olsr_prefix_to_netmask(&mask, rt->rt_dst.prefix_len)) {
89 Row.dwForwardMask = mask.v4.s_addr;
92 Row.dwForwardPolicy = 0;
93 Row.dwForwardNextHop = rt->rt_best->rtp_nexthop.gateway.v4.s_addr;
94 Row.dwForwardIfIndex = iface->if_index;
95 // MIB_IPROUTE_TYPE_DIRECT and MIB_IPROUTE_TYPE_INDIRECT
96 Row.dwForwardType = (rt->rt_dst.prefix.v4.s_addr == rt->rt_best->rtp_nexthop.gateway.v4.s_addr) ? 3 : 4;
97 Row.dwForwardProto = 3; // MIB_IPPROTO_NETMGMT
98 Row.dwForwardAge = INFINITE;
99 Row.dwForwardNextHopAS = 0;
100 Row.dwForwardMetric1 = olsr_fib_metric(&rt->rt_best->rtp_metric);
101 Row.dwForwardMetric2 = -1;
102 Row.dwForwardMetric3 = -1;
103 Row.dwForwardMetric4 = -1;
104 Row.dwForwardMetric5 = -1;
106 Res = SetIpForwardEntry(&Row);
108 if (Res != NO_ERROR) {
109 if (Res != ERROR_NOT_FOUND)
110 OLSR_WARN(LOG_ROUTING, "SetIpForwardEntry() = %08lx, %s", Res, StrError(Res));
112 Res = CreateIpForwardEntry(&Row);
115 if (Res != NO_ERROR) {
116 OLSR_WARN(LOG_ROUTING, "CreateIpForwardEntry() = %08lx, %s", Res, StrError(Res));
118 // XXX - report error in a different way
130 * Remove a route from the kernel
131 * @param destination the route to remove
132 * @return negative on error
135 olsr_kernel_del_route(const struct rt_entry *rt, int ip_version)
137 MIB_IPFORWARDROW Row;
138 union olsr_ip_addr mask;
140 struct interface *iface = rt->rt_nexthop.interface;
142 if (AF_INET != ip_version) {
149 OLSR_DEBUG(LOG_NETWORKING, "KERN: Deleting %s\n", olsr_rt_to_string(rt));
151 memset(&Row, 0, sizeof(Row));
153 Row.dwForwardDest = rt->rt_dst.prefix.v4.s_addr;
155 if (!olsr_prefix_to_netmask(&mask, rt->rt_dst.prefix_len)) {
158 Row.dwForwardMask = mask.v4.s_addr;
159 Row.dwForwardPolicy = 0;
160 Row.dwForwardNextHop = rt->rt_nexthop.gateway.v4.s_addr;
161 Row.dwForwardIfIndex = iface->if_index;
162 // MIB_IPROUTE_TYPE_DIRECT and MIB_IPROUTE_TYPE_INDIRECT
163 Row.dwForwardType = (rt->rt_dst.prefix.v4.s_addr == rt->rt_nexthop.gateway.v4.s_addr) ? 3 : 4;
164 Row.dwForwardProto = 3; // MIB_IPPROTO_NETMGMT
165 Row.dwForwardAge = INFINITE;
166 Row.dwForwardNextHopAS = 0;
167 Row.dwForwardMetric1 = olsr_fib_metric(&rt->rt_metric);
168 Row.dwForwardMetric2 = -1;
169 Row.dwForwardMetric3 = -1;
170 Row.dwForwardMetric4 = -1;
171 Row.dwForwardMetric5 = -1;
173 Res = DeleteIpForwardEntry(&Row);
175 if (Res != NO_ERROR) {
176 OLSR_WARN(LOG_NETWORKING, "DeleteIpForwardEntry() = %08lx, %s", Res, StrError(Res));
178 // XXX - report error in a different way
190 * indent-tabs-mode: nil