3 * Copyright (c) 2006, Sven-Ola Tuecke <sven-ola-aet-gmx.de>
4 * Copyright (c) 2004, Andreas Tonnesen(andreto@olsr.org)
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.
41 #include "olsrd_dyn_gw_plain.h"
42 #include "olsr_types.h"
44 #include "scheduler.h"
46 #include "builddata.h"
51 #include <net/route.h>
57 #define PLUGIN_NAME "OLSRD dyn_gw_plain plugin"
58 #define PLUGIN_INTERFACE_VERSION 5
60 static int has_inet_gateway;
62 static void my_init(void) __attribute__ ((constructor));
63 static void my_fini(void) __attribute__ ((destructor));
71 /* Print plugin info to stdout */
72 olsr_printf(0, "%s (%s)\n", PLUGIN_NAME, git_descriptor);
84 * Plugin interface version
85 * Used by main olsrd to check plugin interface version
88 olsrd_plugin_interface_version(void)
90 return PLUGIN_INTERFACE_VERSION;
93 static const struct olsrd_plugin_parameters plugin_parameters[] = {
97 olsrd_get_plugin_parameters(const struct olsrd_plugin_parameters **params, int *size)
99 *params = plugin_parameters;
100 *size = sizeof(plugin_parameters) / sizeof(*plugin_parameters);
105 * Called after all parameters are passed
108 olsrd_plugin_init(void)
110 gw_net.v4.s_addr = INET_NET;
111 gw_netmask.v4.s_addr = INET_PREFIX;
113 has_inet_gateway = 0;
115 /* Remove all local Inet HNA entries */
116 while (ip_prefix_list_remove(&olsr_cnf->hna_entries, &gw_net, olsr_netmask_to_prefix(&gw_netmask))) {
117 olsr_printf(DEBUGLEV, "HNA Internet gateway deleted\n");
120 /* Register the GW check */
121 olsr_start_timer(3 * MSEC_PER_SEC, 0, OLSR_TIMER_PERIODIC, &olsr_event, NULL, 0);
127 check_gw(union olsr_ip_addr *net, union olsr_ip_addr *mask)
129 char buff[1024], iface[17];
130 uint32_t gate_addr, dest_addr, netmask;
132 int num, metric, refcnt, use;
135 FILE *fp = fopen(PROCENTRY_ROUTE, "r");
138 perror(PROCENTRY_ROUTE);
139 olsr_printf(DEBUGLEV, "INET (IPv4) not configured in this system.\n");
146 olsr_printf(DEBUGLEV, "Genmask Destination Gateway "
147 "Flags Metric Ref Use Iface\n");
149 while (fgets(buff, 1023, fp)) {
151 sscanf(buff, "%16s %128X %128X %X %d %d %d %128X \n", iface, &dest_addr, &gate_addr, &iflags, &refcnt, &use, &metric,
159 olsr_printf(DEBUGLEV, "%-15s ", olsr_ip_to_string((union olsr_ip_addr *)&netmask));
161 olsr_printf(DEBUGLEV, "%-15s ", olsr_ip_to_string((union olsr_ip_addr *)&dest_addr));
163 olsr_printf(DEBUGLEV, "%-15s %-6d %-2d %7d %s\n",
164 olsr_ip_to_string((union olsr_ip_addr *)&gate_addr),
165 metric, refcnt, use, iface);
168 if ( //(iflags & RTF_GATEWAY) &&
169 (iflags & RTF_UP) && (metric == 0) && (netmask == mask->v4.s_addr) && (dest_addr == net->v4.s_addr)) {
170 olsr_printf(DEBUGLEV, "INTERNET GATEWAY VIA %s detected in routing table.\n", iface);
179 olsr_printf(DEBUGLEV, "No Internet GWs detected...\n");
186 * Scheduled event to update the hna table,
187 * called from olsrd main thread to keep the hna table thread-safe
190 olsr_event(void *foo __attribute__ ((unused)))
192 int res = check_gw(&gw_net, &gw_netmask);
193 if (1 == res && 0 == has_inet_gateway) {
194 olsr_printf(DEBUGLEV, "Adding OLSR local HNA entry for Internet\n");
195 ip_prefix_list_add(&olsr_cnf->hna_entries, &gw_net, olsr_netmask_to_prefix(&gw_netmask));
196 has_inet_gateway = 1;
197 } else if (0 == res && 1 == has_inet_gateway) {
198 /* Remove all local Inet HNA entries */
199 while (ip_prefix_list_remove(&olsr_cnf->hna_entries, &gw_net, olsr_netmask_to_prefix(&gw_netmask))) {
200 olsr_printf(DEBUGLEV, "Removing OLSR local HNA entry for Internet\n");
202 has_inet_gateway = 0;
209 * indent-tabs-mode: nil