00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include "olsrd_dyn_gw.h"
00048 #include "olsr.h"
00049 #include "defs.h"
00050 #include "ipcalc.h"
00051 #include "olsr_timer.h"
00052 #include "olsr_socket.h"
00053 #include "olsr_memcookie.h"
00054 #include "olsr_ip_prefix_list.h"
00055 #include "olsr_logging.h"
00056
00057 #include <errno.h>
00058 #include <stdio.h>
00059 #include <stdlib.h>
00060 #include <net/route.h>
00061 #include <arpa/inet.h>
00062 #ifdef linux
00063 #include <linux/in_route.h>
00064 #endif
00065 #ifndef WIN32
00066 #include <pthread.h>
00067 #else
00068 #define WIN32_LEAN_AND_MEAN
00069 #include <windows.h>
00070 #undef interface
00071
00072 typedef HANDLE pthread_mutex_t;
00073 typedef HANDLE pthread_t;
00074
00075 int pthread_create(HANDLE * Hand, void *Attr, void *(*Func) (void *), void *Arg);
00076 int pthread_kill(HANDLE Hand, int Sig);
00077 int pthread_mutex_init(HANDLE * Hand, void *Attr);
00078 int pthread_mutex_lock(HANDLE * Hand);
00079 int pthread_mutex_unlock(HANDLE * Hand);
00080
00081 struct ThreadPara {
00082 void *(*Func) (void *);
00083 void *Arg;
00084 };
00085 #endif
00086
00087
00088
00089 static int check_interval = 5;
00090
00091
00092 struct ping_list {
00093 char *ping_address;
00094 struct ping_list *next;
00095 };
00096
00097 static struct ping_list *add_to_ping_list(const char *, struct ping_list *);
00098
00099 struct hna_list {
00100 union olsr_ip_addr hna_net;
00101 uint8_t hna_prefixlen;
00102 struct ping_list *ping_hosts;
00103 int hna_added;
00104 int probe_ok;
00105 struct hna_list *next;
00106 };
00107
00108 static struct hna_list *add_to_hna_list(struct hna_list *, union olsr_ip_addr *hna_net, uint8_t hna_prefixlen);
00109
00110 struct hna_list *the_hna_list = NULL;
00111
00112 static void
00113 looped_checks(void *) __attribute__ ((noreturn));
00114
00115 static int
00116 check_gw(union olsr_ip_addr *, uint8_t, struct ping_list *);
00117
00118 static int
00119 ping_is_possible(struct ping_list *);
00120
00121
00122 static void
00123 olsr_event_doing_hna(void *);
00124
00128 static int
00129 set_plugin_ping(const char *value, void *data __attribute__ ((unused)), set_plugin_parameter_addon addon __attribute__ ((unused)))
00130 {
00131 union olsr_ip_addr foo_addr;
00132
00133 if (inet_pton(olsr_cnf->ip_version, value, &foo_addr) <= 0) {
00134 OLSR_WARN(LOG_PLUGINS, "Illegal IP address \"%s\"", value);
00135 return 1;
00136 }
00137
00138 if (the_hna_list == NULL) {
00139 union olsr_ip_addr temp_net;
00140 union olsr_ip_addr temp_netmask;
00141 temp_net.v4.s_addr = INET_NET;
00142 temp_netmask.v4.s_addr = INET_PREFIX;
00143 the_hna_list = add_to_hna_list(the_hna_list, &temp_net, olsr_netmask_to_prefix(&temp_netmask));
00144 if (the_hna_list == NULL) {
00145 return 1;
00146 }
00147 }
00148 the_hna_list->ping_hosts = add_to_ping_list(value, the_hna_list->ping_hosts);
00149 return 0;
00150 }
00151
00152 static int
00153 set_plugin_hna(const char *value, void *data __attribute__ ((unused)), set_plugin_parameter_addon addon __attribute__ ((unused)))
00154 {
00155 union olsr_ip_addr temp_net;
00156 union olsr_ip_addr temp_netmask;
00157 char s_netaddr[128];
00158 char s_mask[128];
00159
00160
00161 int i = sscanf(value, "%127s %127s", s_netaddr, s_mask);
00162 if (i != 2) {
00163 OLSR_WARN(LOG_PLUGINS, "Cannot get IP address and netmask from \"%s\"", value);
00164 return 1;
00165 }
00166
00167 if (inet_pton(olsr_cnf->ip_version, s_netaddr, &temp_net) <= 0) {
00168 OLSR_WARN(LOG_PLUGINS, "Illegal IP address \"%s\"", s_netaddr);
00169 return 1;
00170 }
00171
00172 if (inet_pton(olsr_cnf->ip_version, s_mask, &temp_netmask) <= 0) {
00173 OLSR_WARN(LOG_PLUGINS, "Illegal netmask \"%s\"", s_netaddr);
00174 return 1;
00175 }
00176
00177
00178 the_hna_list = add_to_hna_list(the_hna_list, &temp_net, olsr_netmask_to_prefix(&temp_netmask));
00179 if (the_hna_list == NULL) {
00180 return 1;
00181 }
00182 return 0;
00183 }
00184
00185 static const struct olsrd_plugin_parameters plugin_parameters[] = {
00186 {.name = "interval",.set_plugin_parameter = &set_plugin_int,.data = &check_interval},
00187 {.name = "ping",.set_plugin_parameter = &set_plugin_ping,.data = NULL},
00188 {.name = "hna",.set_plugin_parameter = &set_plugin_hna,.data = NULL},
00189 };
00190
00191 void
00192 olsrd_get_plugin_parameters(const struct olsrd_plugin_parameters **params, int *size)
00193 {
00194 *params = plugin_parameters;
00195 *size = ARRAYSIZE(plugin_parameters);
00196 }
00197
00198 static struct olsr_timer_info *doing_hna_timer_info;
00199
00208 int
00209 olsrd_plugin_init(void)
00210 {
00211 pthread_t ping_thread;
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 pthread_create(&ping_thread, NULL, (void *(*)(void *))looped_checks, NULL);
00227
00228
00229 doing_hna_timer_info = olsr_timer_add("DynGW: Doing HNS", &olsr_event_doing_hna, true);
00230
00231
00232 olsr_timer_start(3 * MSEC_PER_SEC, 0, NULL, doing_hna_timer_info);
00233
00234 return 1;
00235 }
00236
00237
00242 static void
00243 olsr_event_doing_hna(void *foo __attribute__ ((unused)))
00244 {
00245 struct hna_list *li;
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 for (li = the_hna_list; li; li = li->next) {
00260 if ((li->probe_ok == 1) && (li->hna_added == 0)) {
00261 OLSR_DEBUG(LOG_PLUGINS, "Adding OLSR local HNA entry\n");
00262 ip_prefix_list_add(&olsr_cnf->hna_entries, &li->hna_net, li->hna_prefixlen);
00263 li->hna_added = 1;
00264 } else if ((li->probe_ok == 0) && (li->hna_added == 1)) {
00265 while (ip_prefix_list_remove(&olsr_cnf->hna_entries, &li->hna_net, li->hna_prefixlen, olsr_cnf->ip_version)) {
00266 OLSR_DEBUG(LOG_PLUGINS, "Removing OLSR local HNA entry\n");
00267 }
00268 li->hna_added = 0;
00269 }
00270 }
00271 }
00272
00273
00274
00280 static void
00281 looped_checks(void *foo __attribute__ ((unused)))
00282 {
00283 for (;;) {
00284 struct hna_list *li;
00285 struct timespec remainder_spec;
00286
00287 struct timespec sleeptime_spec = { check_interval, 0L };
00288
00289 for (li = the_hna_list; li; li = li->next) {
00290
00291 li->probe_ok = check_gw(&li->hna_net, li->hna_prefixlen, li->ping_hosts);
00292
00293 }
00294
00295 while (nanosleep(&sleeptime_spec, &remainder_spec) < 0)
00296 sleeptime_spec = remainder_spec;
00297 }
00298
00299 }
00300
00301
00302
00303 static int
00304 check_gw(union olsr_ip_addr *net, uint8_t prefixlen, struct ping_list *the_ping_list)
00305 {
00306 char buf[1024], iface[16];
00307 uint32_t gate_addr, dest_addr, netmask;
00308 unsigned int iflags;
00309 int metric, refcnt, use;
00310 int retval = 0;
00311 union olsr_ip_addr mask;
00312
00313 FILE *fp = fopen(PROCENTRY_ROUTE, "r");
00314 if (!fp) {
00315 OLSR_WARN(LOG_PLUGINS, "Cannot read proc file %s: %s\n", PROCENTRY_ROUTE, strerror(errno));
00316 return -1;
00317 }
00318
00319 olsr_prefix_to_netmask(&mask, prefixlen);
00320
00321
00322
00323
00324 while (fgets(buf, sizeof(buf), fp)) {
00325 int num = sscanf(buf, "%15s %128X %128X %X %d %d %d %128X \n",
00326 iface, &dest_addr, &gate_addr,
00327 &iflags, &refcnt, &use, &metric, &netmask);
00328 if (num < 8)
00329 continue;
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341 if ((iflags & RTF_UP) && (metric == 0) && (netmask == mask.v4.s_addr) && (dest_addr == net->v4.s_addr)) {
00342 if (((mask.v4.s_addr == INET_PREFIX) && (net->v4.s_addr == INET_NET)) && (!(iflags & RTF_GATEWAY))) {
00343 fclose(fp);
00344 return retval;
00345 }
00346
00347 if (the_ping_list != NULL) {
00348
00349 if (ping_is_possible(the_ping_list)) {
00350 OLSR_DEBUG(LOG_PLUGINS, "HNA[%08x/%08x](ping is possible) VIA %s detected in routing table.\n", dest_addr, netmask,
00351 iface);
00352 retval = 1;
00353 }
00354 } else {
00355 OLSR_DEBUG(LOG_PLUGINS, "HNA[%08x/%08x] VIA %s detected in routing table.\n", dest_addr, netmask, iface);
00356 retval = 1;
00357 }
00358 }
00359 }
00360
00361 fclose(fp);
00362 if (retval == 0) {
00363
00364 OLSR_WARN(LOG_PLUGINS, "HNA[%08x/%08x] is invalid\n", (unsigned int)net->v4.s_addr, (unsigned int)mask.v4.s_addr);
00365 }
00366 return retval;
00367 }
00368
00369 static int
00370 ping_is_possible(struct ping_list *the_ping_list)
00371 {
00372 struct ping_list *list;
00373 for (list = the_ping_list; list; list = list->next) {
00374 char ping_command[50];
00375 snprintf(ping_command, sizeof(ping_command), "ping -c 1 -q %s", list->ping_address);
00376 if (system(ping_command) == 0) {
00377 OLSR_DEBUG(LOG_PLUGINS, "\nDo ping on %s ... ok\n", list->ping_address);
00378 return 1;
00379 }
00380 OLSR_DEBUG(LOG_PLUGINS, "\nDo ping on %s ... failed\n", list->ping_address);
00381 }
00382 return 0;
00383 }
00384
00385
00386 static struct ping_list *
00387 add_to_ping_list(const char *ping_address, struct ping_list *the_ping_list)
00388 {
00389 struct ping_list *new = olsr_malloc(sizeof(struct ping_list), "ping list");
00390 new->ping_address = olsr_strdup(ping_address);
00391 new->next = the_ping_list;
00392 return new;
00393 }
00394
00395
00396
00397 static struct hna_list *
00398 add_to_hna_list(struct hna_list *listold_root, union olsr_ip_addr *hna_net, uint8_t hna_prefixlen)
00399 {
00400 struct hna_list *new = olsr_malloc(sizeof(struct hna_list), "hna list");
00401
00402
00403 new->hna_net.v4 = hna_net->v4;
00404 new->hna_prefixlen = hna_prefixlen;
00405 new->hna_added = 0;
00406 new->probe_ok = 0;
00407 new->ping_hosts = NULL;
00408 new->next = listold_root;
00409 return new;
00410 }
00411
00412 #ifdef WIN32
00413
00414
00415
00416
00417 static unsigned long __stdcall
00418 ThreadWrapper(void *Para)
00419 {
00420 struct ThreadPara *Cast;
00421 void *(*Func) (void *);
00422 void *Arg;
00423
00424 Cast = (struct ThreadPara *)Para;
00425
00426 Func = Cast->Func;
00427 Arg = Cast->Arg;
00428
00429 HeapFree(GetProcessHeap(), 0, Para);
00430
00431 Func(Arg);
00432
00433 return 0;
00434 }
00435
00436 int
00437 pthread_create(HANDLE * Hand, void *Attr __attribute__ ((unused)), void *(*Func) (void *), void *Arg)
00438 {
00439 struct ThreadPara *Para;
00440 unsigned long ThreadId;
00441
00442 Para = HeapAlloc(GetProcessHeap(), 0, sizeof(struct ThreadPara));
00443
00444 if (Para == NULL)
00445 return -1;
00446
00447 Para->Func = Func;
00448 Para->Arg = Arg;
00449
00450 *Hand = CreateThread(NULL, 0, ThreadWrapper, Para, 0, &ThreadId);
00451
00452 if (*Hand == NULL)
00453 return -1;
00454
00455 return 0;
00456 }
00457
00458 int
00459 pthread_kill(HANDLE Hand, int Sig __attribute__ ((unused)))
00460 {
00461 if (!TerminateThread(Hand, 0))
00462 return -1;
00463
00464 return 0;
00465 }
00466
00467 int
00468 pthread_mutex_init(HANDLE * Hand, void *Attr __attribute__ ((unused)))
00469 {
00470 *Hand = CreateMutex(NULL, FALSE, NULL);
00471
00472 if (*Hand == NULL)
00473 return -1;
00474
00475 return 0;
00476 }
00477
00478 int
00479 pthread_mutex_lock(HANDLE * Hand)
00480 {
00481 if (WaitForSingleObject(*Hand, INFINITE) == WAIT_FAILED)
00482 return -1;
00483
00484 return 0;
00485 }
00486
00487 int
00488 pthread_mutex_unlock(HANDLE * Hand)
00489 {
00490 if (!ReleaseMutex(*Hand))
00491 return -1;
00492
00493 return 0;
00494 }
00495
00496 #endif
00497
00498
00499
00500
00501
00502
00503