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 #include "common/avl.h"
00043 #include "common/string.h"
00044
00045 #include "defs.h"
00046 #include "avl_olsr_comp.h"
00047 #include "interfaces.h"
00048 #include "olsr_timer.h"
00049 #include "olsr_socket.h"
00050 #include "olsr.h"
00051 #include "parser.h"
00052 #include "net_olsr.h"
00053 #include "ipcalc.h"
00054 #include "olsr_logging.h"
00055 #include "os_net.h"
00056
00057 #include <signal.h>
00058 #include <unistd.h>
00059 #include <assert.h>
00060
00061 #define BUFSPACE (127*1024)
00062
00063 const char *INTERFACE_MODE_NAMES[] = {
00064 "mesh",
00065 "ether"
00066 };
00067
00068
00069 struct list_entity interface_head;
00070
00071
00072 struct avl_tree interface_lost_tree;
00073
00074
00075 struct ifchgf {
00076 ifchg_cb_func function;
00077 struct ifchgf *next;
00078 };
00079
00080 static struct ifchgf *ifchgf_list = NULL;
00081
00082
00083 static struct olsr_memcookie_info *interface_mem_cookie = NULL;
00084 static struct olsr_memcookie_info *interface_lost_mem_cookie = NULL;
00085
00086 static struct olsr_timer_info *interface_poll_timerinfo = NULL;
00087 static struct olsr_timer_info *hello_gen_timerinfo = NULL;
00088
00089 static void check_interface_updates(void *);
00090
00097 bool
00098 init_interfaces(void)
00099 {
00100 struct olsr_if_config *tmp_if;
00101
00102
00103 list_init_head(&interface_head);
00104 avl_init(&interface_lost_tree, avl_comp_default, false, NULL);
00105
00106
00107
00108
00109 interface_mem_cookie = olsr_memcookie_add("Interface", sizeof(struct interface));
00110
00111 interface_lost_mem_cookie = olsr_memcookie_add("Interface lost", sizeof(struct interface_lost));
00112
00113 interface_poll_timerinfo = olsr_timer_add("Interface Polling", &check_interface_updates, true);
00114 hello_gen_timerinfo = olsr_timer_add("Hello Generation", &generate_hello, true);
00115
00116 OLSR_INFO(LOG_INTERFACE, "\n ---- Interface configuration ---- \n\n");
00117
00118
00119 for (tmp_if = olsr_cnf->if_configs; tmp_if != NULL; tmp_if = tmp_if->next) {
00120 add_interface(tmp_if);
00121 }
00122
00123
00124 olsr_timer_start(olsr_cnf->nic_chgs_pollrate, 5,
00125 NULL, interface_poll_timerinfo);
00126
00127 return (!list_is_empty(&interface_head));
00128 }
00129
00130 static void remove_lost_interface_ip(struct interface_lost *lost) {
00131 #if !defined(REMOVE_LOG_DEBUG)
00132 struct ipaddr_str buf;
00133 #endif
00134
00135 OLSR_DEBUG(LOG_INTERFACE, "Remove %s from lost interface list\n",
00136 olsr_ip_to_string(&buf, &lost->ip));
00137 avl_delete(&interface_lost_tree, &lost->node);
00138 olsr_memcookie_free(interface_lost_mem_cookie, lost);
00139 }
00140
00141 static void add_lost_interface_ip(union olsr_ip_addr *ip, uint32_t hello_timeout) {
00142 struct interface_lost *lost;
00143 #if !defined(REMOVE_LOG_DEBUG)
00144 struct ipaddr_str buf;
00145 #endif
00146
00147 lost = olsr_memcookie_malloc(interface_lost_mem_cookie);
00148 lost->node.key = &lost->ip;
00149 lost->ip = *ip;
00150 lost->valid_until = olsr_clock_getAbsolute(hello_timeout * 2);
00151 avl_insert(&interface_lost_tree, &lost->node);
00152
00153 OLSR_DEBUG(LOG_INTERFACE, "Added %s to lost interface list for %d ms\n",
00154 olsr_ip_to_string(&buf, ip), hello_timeout*2);
00155 }
00156
00157 static struct interface_lost *get_lost_interface_ip(union olsr_ip_addr *ip) {
00158 struct interface_lost *lost;
00159 assert(ip);
00160 lost = avl_find_element(&interface_lost_tree, ip, lost, node);
00161 return lost;
00162 }
00163
00164 bool
00165 is_lost_interface_ip(union olsr_ip_addr *ip) {
00166 assert(ip);
00167 return get_lost_interface_ip(ip) != NULL;
00168 }
00169
00170 void destroy_interfaces(void) {
00171 struct interface *iface, *iface_iterator;
00172 struct interface_lost *lost, *lost_iterator;
00173
00174 OLSR_FOR_ALL_INTERFACES(iface, iface_iterator) {
00175 remove_interface(iface);
00176 }
00177
00178 OLSR_FOR_ALL_LOSTIF_ENTRIES(lost, lost_iterator) {
00179 remove_lost_interface_ip(lost);
00180 }
00181 }
00182
00183 struct interface *
00184 add_interface(struct olsr_if_config *iface) {
00185 struct interface *ifp;
00186 int sock_rcv, sock_send;
00187
00188 ifp = olsr_memcookie_malloc(interface_mem_cookie);
00189 ifp->int_name = iface->name;
00190
00191 if ((os_init_interface(ifp, iface))) {
00192 olsr_memcookie_free(interface_mem_cookie, ifp);
00193 return NULL;
00194 }
00195
00196 sock_rcv = os_getsocket46(olsr_cnf->ip_version, ifp->int_name, olsr_cnf->olsr_port, BUFSPACE, NULL);
00197 sock_send = os_getsocket46(olsr_cnf->ip_version, ifp->int_name, olsr_cnf->olsr_port, BUFSPACE, &ifp->int_multicast);
00198 if (sock_rcv < 0 || sock_send < 0) {
00199 OLSR_ERROR(LOG_INTERFACE, "Could not initialize socket... exiting!\n\n");
00200 olsr_exit(EXIT_FAILURE);
00201 }
00202
00203 set_buffer_timer(ifp);
00204
00205
00206 ifp->olsr_socket = olsr_socket_add(sock_rcv, &olsr_input, NULL, OLSR_SOCKET_READ);
00207
00208 ifp->send_socket_fd = sock_send;
00209
00210 os_socket_set_olsr_options(ifp, ifp->olsr_socket->fd, &ifp->int_multicast);
00211 os_socket_set_olsr_options(ifp, sock_send, NULL);
00212
00213
00214
00215
00216 ifp->olsr_seqnum = random() & 0xFFFF;
00217
00218
00219
00220
00221 if (olsr_ipcmp(&all_zero, &olsr_cnf->router_id) == 0) {
00222 #if !defined(REMOVE_LOG_INFO)
00223 struct ipaddr_str buf;
00224 #endif
00225 olsr_cnf->router_id = ifp->ip_addr;
00226 OLSR_INFO(LOG_INTERFACE, "New main address: %s\n", olsr_ip_to_string(&buf, &olsr_cnf->router_id));
00227
00228
00229 olsr_change_myself_tc();
00230 }
00231
00232
00233 net_add_buffer(ifp);
00234
00235
00236
00237
00238 ifp->hello_gen_timer =
00239 olsr_timer_start(iface->cnf->hello_params.emission_interval,
00240 HELLO_JITTER, ifp, hello_gen_timerinfo);
00241 ifp->hello_interval = iface->cnf->hello_params.emission_interval;
00242 ifp->hello_validity = iface->cnf->hello_params.validity_time;
00243
00244 ifp->mode = iface->cnf->mode;
00245
00246
00247
00248
00249 run_ifchg_cbs(ifp, IFCHG_IF_ADD);
00250
00251
00252
00253
00254 lock_interface(ifp);
00255
00256
00257
00258
00259 iface->interf = ifp;
00260 ifp->if_cfg = iface;
00261 lock_interface(iface->interf);
00262
00263
00264 list_add_before(&interface_head, &ifp->int_node);
00265
00266 return ifp;
00267 }
00268
00272 static void
00273 check_interface_updates(void *foo __attribute__ ((unused)))
00274 {
00275 struct olsr_if_config *tmp_if;
00276 struct interface_lost *lost, *iterator;
00277
00278 OLSR_DEBUG(LOG_INTERFACE, "Checking for updates in the interface set\n");
00279
00280 for (tmp_if = olsr_cnf->if_configs; tmp_if != NULL; tmp_if = tmp_if->next) {
00281
00282 if (!tmp_if->cnf->autodetect_chg) {
00283
00284 OLSR_DEBUG(LOG_INTERFACE, "Not checking interface %s\n", tmp_if->name);
00285 continue;
00286 }
00287
00288 if (tmp_if->interf) {
00289 chk_if_changed(tmp_if);
00290 } else {
00291 if (add_interface(tmp_if)) {
00292 lost = get_lost_interface_ip(&tmp_if->interf->ip_addr);
00293 if (lost) {
00294 remove_lost_interface_ip(lost);
00295 }
00296 }
00297 }
00298 }
00299
00300
00301 OLSR_FOR_ALL_LOSTIF_ENTRIES(lost, iterator) {
00302 if (olsr_clock_isPast(lost->valid_until)) {
00303 remove_lost_interface_ip(lost);
00304 }
00305 }
00306 }
00307
00311 void
00312 remove_interface(struct interface *ifp)
00313 {
00314 if (!ifp) {
00315 return;
00316 }
00317
00318 OLSR_INFO(LOG_INTERFACE, "Removing interface %s\n", ifp->int_name);
00319
00320 ifp->if_cfg->interf = NULL;
00321
00322 os_cleanup_interface(ifp);
00323
00324 olsr_delete_link_entry_by_if(ifp);
00325
00326
00327
00328
00329 run_ifchg_cbs(ifp, IFCHG_IF_REMOVE);
00330
00331
00332 list_remove(&ifp->int_node);
00333
00334
00335 net_remove_buffer(ifp);
00336
00337
00338
00339
00340 olsr_timer_stop(ifp->hello_gen_timer);
00341 ifp->hello_gen_timer = NULL;
00342
00343
00344
00345
00346 olsr_timer_stop(ifp->buffer_hold_timer);
00347 ifp->buffer_hold_timer = NULL;
00348
00349
00350
00351
00352 add_lost_interface_ip(&ifp->ip_addr, ifp->hello_validity);
00353
00354
00355
00356
00357 unlock_interface(ifp);
00358
00359
00360 os_close(ifp->olsr_socket->fd);
00361 os_close(ifp->send_socket_fd);
00362
00363 olsr_socket_remove(ifp->olsr_socket);
00364
00365
00366 ifp->int_name = NULL;
00367 unlock_interface(ifp);
00368
00369 if (list_is_empty(&interface_head) && !olsr_cnf->allow_no_interfaces) {
00370 OLSR_ERROR(LOG_INTERFACE, "No more active interfaces - exiting.\n");
00371 olsr_exit(EXIT_FAILURE);
00372 }
00373 }
00374
00375 void
00376 run_ifchg_cbs(struct interface *ifp, int flag)
00377 {
00378 struct ifchgf *tmp;
00379 for (tmp = ifchgf_list; tmp != NULL; tmp = tmp->next) {
00380 tmp->function(ifp, flag);
00381 }
00382 }
00383
00391 struct interface *
00392 if_ifwithaddr(const union olsr_ip_addr *addr)
00393 {
00394 struct interface *ifp, *iterator;
00395 if (!addr) {
00396 return NULL;
00397 }
00398
00399 OLSR_FOR_ALL_INTERFACES(ifp, iterator) {
00400 if (olsr_ipcmp(&ifp->ip_addr, addr) == 0) {
00401 return ifp;
00402 }
00403 }
00404 return NULL;
00405 }
00406
00414 struct interface *
00415 if_ifwithsock(int fd)
00416 {
00417 struct interface *ifp, *iterator;
00418
00419 OLSR_FOR_ALL_INTERFACES(ifp, iterator) {
00420 if (ifp->olsr_socket->fd == fd) {
00421 return ifp;
00422 }
00423 if (ifp->send_socket_fd == fd) {
00424 return ifp;
00425 }
00426 }
00427
00428 return NULL;
00429 }
00430
00431
00439 struct interface *
00440 if_ifwithname(const char *if_name)
00441 {
00442 struct interface *ifp, *iterator;
00443
00444 OLSR_FOR_ALL_INTERFACES(ifp, iterator) {
00445
00446 if (strcmp(ifp->int_name, if_name) == 0) {
00447 return ifp;
00448 }
00449 }
00450
00451 return NULL;
00452 }
00453
00461 struct interface *
00462 if_ifwithindex(const int if_index)
00463 {
00464 struct interface *ifp, *iterator;
00465 OLSR_FOR_ALL_INTERFACES(ifp, iterator) {
00466 if (ifp->if_index == if_index) {
00467 return ifp;
00468 }
00469 }
00470
00471 return NULL;
00472 }
00473
00480 const char *
00481 if_ifwithindex_name(const int if_index)
00482 {
00483 const struct interface *const ifp = if_ifwithindex(if_index);
00484 return ifp == NULL ? "void" : ifp->int_name;
00485 }
00486
00490 void
00491 lock_interface(struct interface *ifp)
00492 {
00493 assert(ifp);
00494
00495 ifp->refcount++;
00496 }
00497
00501 void
00502 unlock_interface(struct interface *ifp)
00503 {
00504
00505 assert(ifp->refcount);
00506
00507 if (--ifp->refcount) {
00508 return;
00509 }
00510
00511
00512 assert(!list_node_added(&ifp->int_node));
00513
00514
00515 free(ifp->int_name);
00516 olsr_memcookie_free(interface_mem_cookie, ifp);
00517 }
00518
00519
00524 void
00525 add_ifchgf(ifchg_cb_func f)
00526 {
00527 struct ifchgf *tmp = olsr_malloc(sizeof(struct ifchgf), "Add ifchgfunction");
00528
00529 tmp->function = f;
00530 tmp->next = ifchgf_list;
00531 ifchgf_list = tmp;
00532 }
00533
00534 #if 0
00535
00536
00537
00538
00539 int
00540 del_ifchgf(ifchg_cb_func f)
00541 {
00542 struct ifchgf *tmp, *prev;
00543
00544 for (tmp = ifchgf_list, prev = NULL; tmp != NULL; prev = tmp, tmp = tmp->next) {
00545 if (tmp->function == f) {
00546
00547 if (prev == NULL) {
00548 ifchgf_list = tmp->next;
00549 } else {
00550 prev->next = tmp->next;
00551 }
00552 free(tmp);
00553 return 1;
00554 }
00555 }
00556 return 0;
00557 }
00558 #endif
00559
00560
00561
00562
00563
00564
00565