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 #include <stddef.h>
00045 #include <sys/types.h>
00046 #include <string.h>
00047 #include <stdarg.h>
00048 #include <errno.h>
00049 #include <assert.h>
00050 #include <linux/if_ether.h>
00051 #include <linux/if_packet.h>
00052 #include <signal.h>
00053 #include <netinet/ip.h>
00054 #include <netinet/udp.h>
00055 #include <unistd.h>
00056 #include <stdlib.h>
00057
00058 #include <netinet/in.h>
00059 #include <netinet/ip6.h>
00060
00061 #include <fcntl.h>
00062
00063
00064 #include "plugin_util.h"
00065 #include "defs.h"
00066 #include "ipcalc.h"
00067 #include "olsr.h"
00068 #include "mid_set.h"
00069 #include "link_set.h"
00070 #include "net_olsr.h"
00071 #include "olsr_logging.h"
00072
00073
00074 #include "obamp.h"
00075 #include "common/list.h"
00076
00077
00078 #define OLSR_FOR_ALL_OBAMPNODE_ENTRIES(n, iterator) list_for_each_element_safe(&ListOfObampNodes, n, list, iterator)
00079 #define OLSR_FOR_ALL_OBAMPSNIFF_ENTRIES(s, iterator) list_for_each_element_safe(&ListOfObampSniffingIf, s, list, iterator)
00080
00081 struct ObampNodeState *myState;
00082
00083
00084
00085
00086
00087
00088 struct list_entity ListOfObampNodes;
00089
00090
00091 struct list_entity ListOfObampSniffingIf;
00092
00093
00094 int sdudp = -1;
00095
00096
00097
00098
00099
00100 static struct ObampNode *
00101 select_tree_anchor(void)
00102 {
00103
00104 struct ObampNode *tmp, *iterator;
00105 struct ObampNode *best;
00106 struct rt_entry *rt;
00107
00108 unsigned int mincost = 15;
00109
00110 best = NULL;
00111
00112 if (list_is_empty(&ListOfObampNodes)) {
00113 OLSR_DEBUG(LOG_PLUGINS, "List empty can't create Overlay Mesh");
00114 return NULL;
00115 }
00116
00117
00118 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
00119 if (tmp->status == 1) {
00120 rt = olsr_lookup_routing_table(&tmp->neighbor_ip_addr);
00121
00122 if (rt == NULL) {
00123 OLSR_DEBUG(LOG_PLUGINS, "No route present to this anchor");
00124 continue;
00125 }
00126
00127 if ((rt->rt_best->rtp_metric.cost / 65536) < mincost)
00128 best = tmp;
00129 }
00130 }
00131 return best;
00132 }
00133
00134
00135 static int
00136 SendOBAMPData(struct in_addr *addr, unsigned char *ipPacket, int nBytes)
00137 {
00138
00139 struct sockaddr_in si_other;
00140 struct OBAMP_data_message4 data_msg;
00141
00142 memset(&data_msg, 0, sizeof(data_msg));
00143 data_msg.MessageID = OBAMP_DATA;
00144 data_msg.router_id = (u_int32_t) myState->myipaddr.v4.s_addr;
00145 data_msg.last_hop = (u_int32_t) myState->myipaddr.v4.s_addr;
00146
00147 data_msg.CoreAddress = (u_int32_t) myState->CoreAddress.v4.s_addr;
00148
00149 data_msg.SequenceNumber = myState->DataSequenceNumber;
00150 myState->DataSequenceNumber++;
00151
00152 if (nBytes >= 1471) {
00153 OLSR_DEBUG(LOG_PLUGINS, "PACKET DROPPED: %d bytes are too much",nBytes);
00154 return 1;
00155 }
00156
00157 memcpy(data_msg.data, ipPacket, nBytes);
00158
00159 data_msg.datalen = nBytes;
00160
00161 memset((char *)&si_other, 0, sizeof(si_other));
00162 si_other.sin_family = AF_INET;
00163 si_other.sin_port = htons(OBAMP_SIGNALLING_PORT);
00164 si_other.sin_addr = *addr;
00165
00166
00167 sendto(sdudp, &data_msg, 17+data_msg.datalen, 0, (struct sockaddr *)&si_other, sizeof(si_other));
00168 return 0;
00169
00170 }
00171
00172 static int
00173 CreateCaptureSocket(const char *ifName)
00174 {
00175 int ifIndex = if_nametoindex(ifName);
00176 struct packet_mreq mreq;
00177 struct ifreq req;
00178 struct sockaddr_ll bindTo;
00179 int skfd = 0;
00180
00181
00182 if (olsr_cnf->ip_version == AF_INET) {
00183 skfd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
00184 } else {
00185 skfd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
00186 }
00187 if (skfd < 0) {
00188 OLSR_DEBUG(LOG_PLUGINS, "socket(PF_PACKET) error");
00189 return -1;
00190 }
00191
00192
00193 memset(&mreq, 0, sizeof(struct packet_mreq));
00194 mreq.mr_ifindex = ifIndex;
00195 mreq.mr_type = PACKET_MR_PROMISC;
00196 if (setsockopt(skfd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
00197 OLSR_DEBUG(LOG_PLUGINS, "setsockopt(PACKET_MR_PROMISC) error");
00198 close(skfd);
00199 return -1;
00200 }
00201
00202
00203 memset(&req, 0, sizeof(struct ifreq));
00204 strncpy(req.ifr_name, ifName, IFNAMSIZ - 1);
00205 req.ifr_name[IFNAMSIZ - 1] = '\0';
00206 if (ioctl(skfd, SIOCGIFHWADDR, &req) < 0) {
00207 OLSR_DEBUG(LOG_PLUGINS, "error retrieving MAC address");
00208 close(skfd);
00209 return -1;
00210 }
00211
00212
00213 memset(&bindTo, 0, sizeof(bindTo));
00214 bindTo.sll_family = AF_PACKET;
00215 if (olsr_cnf->ip_version == AF_INET) {
00216 bindTo.sll_protocol = htons(ETH_P_IP);
00217 } else {
00218 bindTo.sll_protocol = htons(ETH_P_IPV6);
00219 }
00220 bindTo.sll_ifindex = ifIndex;
00221 memcpy(bindTo.sll_addr, req.ifr_hwaddr.sa_data, IFHWADDRLEN);
00222 bindTo.sll_halen = IFHWADDRLEN;
00223
00224 if (bind(skfd, (struct sockaddr *)&bindTo, sizeof(bindTo)) < 0) {
00225 OLSR_DEBUG(LOG_PLUGINS, "bind() error");
00226 close(skfd);
00227 return -1;
00228 }
00229
00230
00231 if (fcntl(skfd, F_SETFL, fcntl(skfd, F_GETFL, 0) & ~O_NONBLOCK) < 0) {
00232 OLSR_DEBUG(LOG_PLUGINS, "fcntl() error");
00233 close(skfd);
00234 return -1;
00235 }
00236
00237 if (NULL == olsr_socket_add(skfd, &EncapFlowInObamp, NULL, OLSR_SOCKET_READ)) {
00238 OLSR_DEBUG(LOG_PLUGINS, "Could not register socket with scheduler");
00239 close(skfd);
00240 return -1;
00241 }
00242
00243 return skfd;
00244 }
00245
00246
00247 static int
00248 CreateObampSniffingInterfaces(void)
00249 {
00250
00251 struct ObampSniffingIf *tmp, *iterator;
00252 OLSR_DEBUG(LOG_PLUGINS, "CreateObampSniffingInterfaces");
00253
00254 if (list_is_empty(&ListOfObampSniffingIf)) {
00255 OLSR_DEBUG(LOG_PLUGINS, "List of sniffin interfaces was empty");
00256 return 0;
00257 }
00258
00259 OLSR_DEBUG(LOG_PLUGINS, "adding interfaces");
00260
00261 OLSR_FOR_ALL_OBAMPSNIFF_ENTRIES(tmp, iterator) {
00262 tmp->skd = CreateCaptureSocket(tmp->ifName);
00263 }
00264 return 0;
00265 }
00266
00267
00268 static int
00269 IsMulticast(union olsr_ip_addr *ipAddress)
00270 {
00271 assert(ipAddress != NULL);
00272
00273 return (ntohl(ipAddress->v4.s_addr) & 0xF0000000) == 0xE0000000;
00274 }
00275
00276
00277
00278 static void
00279 activate_tree_link(struct OBAMP_tree_link_ack *ack)
00280 {
00281 #if !defined(REMOVE_LOG_DEBUG)
00282 struct ipaddr_str buf;
00283 #endif
00284 struct ObampNode *tmp, *iterator;
00285
00286 if (memcmp(&myState->CoreAddress.v4, &ack->CoreAddress.v4, sizeof(struct in_addr)) != 0) {
00287 OLSR_DEBUG(LOG_PLUGINS, "Discarding message with no coherent core address");
00288 return;
00289 }
00290
00291 if (ack->SequenceNumber != myState->tree_req_sn - 1) {
00292
00293 OLSR_DEBUG(LOG_PLUGINS, "ACK DISCARDED WRONG SEQ NUMBER");
00294 return;
00295 } else {
00296
00297 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
00298 if (tmp->neighbor_ip_addr.v4.s_addr == ack->router_id.v4.s_addr) {
00299
00300 tmp->isTree = 1;
00301 myState->TreeHeartBeat = TREE_HEARTBEAT;
00302 OLSR_DEBUG(LOG_PLUGINS, "Tree link to %s activated", ip4_to_string(&buf, tmp->neighbor_ip_addr.v4));
00303 return;
00304
00305 }
00306 }
00307 }
00308
00309 }
00310
00311
00312
00313
00314
00315
00316
00317 static void
00318 obamp_hello(struct in_addr *addr)
00319 {
00320
00321 struct OBAMP_hello hello;
00322 struct sockaddr_in si_other;
00323
00324 memset(&hello, 0, sizeof(hello));
00325 hello.MessageID = OBAMP_HELLO;
00326
00327 hello.router_id = myState->myipaddr;
00328 hello.CoreAddress = myState->CoreAddress;
00329
00330
00331
00332
00333
00334 memset((char *)&si_other, 0, sizeof(si_other));
00335 si_other.sin_family = AF_INET;
00336 si_other.sin_port = htons(OBAMP_SIGNALLING_PORT);
00337 si_other.sin_addr = *addr;
00338 sendto(sdudp, &hello, sizeof(struct OBAMP_hello), 0, (struct sockaddr *)&si_other, sizeof(si_other));
00339 }
00340
00341
00342
00343 static void
00344 send_destroy_tree_link(struct in_addr *addr)
00345 {
00346 struct OBAMP_tree_destroy msg;
00347 struct sockaddr_in si_other;
00348 #if !defined(REMOVE_LOG_DEBUG)
00349 struct ipaddr_str buf;
00350 #endif
00351
00352
00353 OLSR_DEBUG(LOG_PLUGINS, "Sending tree destroy to: %s", ip4_to_string(&buf, *addr));
00354
00355 memset(&msg, 0, sizeof(msg));
00356 msg.MessageID = OBAMP_TREE_DESTROY;
00357
00358 msg.router_id = myState->myipaddr;
00359 msg.CoreAddress = myState->CoreAddress;
00360
00361 memset((char *)&si_other, 0, sizeof(si_other));
00362 si_other.sin_family = AF_INET;
00363 si_other.sin_port = htons(OBAMP_SIGNALLING_PORT);
00364 si_other.sin_addr = *addr;
00365 sendto(sdudp, &msg, sizeof(struct OBAMP_tree_destroy), 0, (struct sockaddr *)&si_other, sizeof(si_other));
00366 }
00367
00368
00369
00370 static void
00371 tree_link_req(struct in_addr *addr)
00372 {
00373 if (myState->TreeRequestDelay == 0) {
00374 struct OBAMP_tree_link_req req;
00375 struct sockaddr_in si_other;
00376 #if !defined(REMOVE_LOG_DEBUG)
00377 struct ipaddr_str buf;
00378 #endif
00379
00380
00381 OLSR_DEBUG(LOG_PLUGINS, "Sending tree request to: %s", ip4_to_string(&buf, *addr));
00382
00383 memset(&req, 0, sizeof(req));
00384 req.MessageID = OBAMP_TREE_REQ;
00385
00386 req.router_id = myState->myipaddr;
00387 req.CoreAddress = myState->CoreAddress;
00388
00389 req.SequenceNumber = myState->tree_req_sn;
00390 myState->tree_req_sn++;
00391
00392 memset((char *)&si_other, 0, sizeof(si_other));
00393 si_other.sin_family = AF_INET;
00394 si_other.sin_port = htons(OBAMP_SIGNALLING_PORT);
00395 si_other.sin_addr = *addr;
00396 myState->TreeRequestDelay=_TREE_REQUEST_DELAY_;
00397 sendto(sdudp, &req, sizeof(struct OBAMP_tree_link_req), 0, (struct sockaddr *)&si_other, sizeof(si_other));
00398 }
00399 else OLSR_DEBUG(LOG_PLUGINS,"Do not send Tree Link Request because there is another one running");
00400 }
00401
00402 static void
00403 tree_link_ack(struct OBAMP_tree_link_req *req)
00404 {
00405
00406 struct sockaddr_in si_other;
00407 struct in_addr addr;
00408
00409 struct ObampNode *tmp, *iterator;
00410
00411 #if !defined(REMOVE_LOG_DEBUG)
00412 struct ipaddr_str buf;
00413 #endif
00414
00415 struct OBAMP_tree_link_ack ack;
00416
00417
00418 if (memcmp(&myState->CoreAddress.v4, &req->CoreAddress.v4, sizeof(struct in_addr)) != 0) {
00419 OLSR_DEBUG(LOG_PLUGINS, "Discarding message with no coherent core address");
00420 return;
00421 }
00422
00423
00424
00425 memset(&ack, 0, sizeof(ack));
00426 ack.MessageID = OBAMP_TREE_ACK;
00427
00428 ack.router_id = myState->myipaddr;
00429 ack.CoreAddress = myState->CoreAddress;
00430
00431 ack.SequenceNumber = req->SequenceNumber;
00432
00433 addr = req->router_id.v4;
00434
00435 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
00436 if (tmp->neighbor_ip_addr.v4.s_addr == req->router_id.v4.s_addr) {
00437
00438 tmp->isTree = 1;
00439 OLSR_DEBUG(LOG_PLUGINS, "Tree link to %s activated", ip4_to_string(&buf, tmp->neighbor_ip_addr.v4));
00440 break;
00441 }
00442 }
00443
00444 memset((char *)&si_other, 0, sizeof(si_other));
00445 si_other.sin_family = AF_INET;
00446 si_other.sin_port = htons(OBAMP_SIGNALLING_PORT);
00447 si_other.sin_addr = addr;
00448 sendto(sdudp, &ack, sizeof(struct OBAMP_tree_link_req), 0, (struct sockaddr *)&si_other, sizeof(si_other));
00449 }
00450
00451
00452 static void
00453 init_overlay_neighbor(struct ObampNode *n)
00454 {
00455
00456 n->Texpire = _Texpire_;
00457 n->isMesh = 0;
00458 n->wasMesh = 0;
00459 n->MeshLock = 0;
00460 n->isTree = 0;
00461 n->outerTreeLink = 0;
00462 n->DataSeqNumber = 0;
00463 }
00464
00465 static void
00466 tree_create_forward_to(struct in_addr *addr, struct OBAMP_tree_create *mytc)
00467 {
00468
00469 struct sockaddr_in si_other;
00470 struct OBAMP_tree_create temptc;
00471
00472 memset(&temptc, 0, sizeof(temptc));
00473 memcpy(&temptc, mytc, sizeof(struct OBAMP_tree_create));
00474
00475
00476 if (memcmp(&myState->CoreAddress.v4, &temptc.CoreAddress.v4, sizeof(struct in_addr)) != 0) {
00477 OLSR_DEBUG(LOG_PLUGINS, "Discarding message with no coherent core address");
00478 return;
00479 }
00480
00481 temptc.router_id = myState->myipaddr;
00482
00483 memset((char *)&si_other, 0, sizeof(si_other));
00484 si_other.sin_family = AF_INET;
00485 si_other.sin_port = htons(OBAMP_SIGNALLING_PORT);
00486 si_other.sin_addr = *addr;
00487
00488 sendto(sdudp, &temptc, sizeof(struct OBAMP_tree_create), 0, (struct sockaddr *)&si_other, sizeof(si_other));
00489 }
00490
00491 static void
00492 tree_create_gen(struct in_addr *addr)
00493 {
00494 struct OBAMP_tree_create mytc;
00495 struct sockaddr_in si_other;
00496
00497 OLSR_DEBUG(LOG_PLUGINS, "Calling tree_create_gen\n");
00498
00499 memset(&mytc, 0, sizeof(mytc));
00500 mytc.MessageID = OBAMP_TREECREATE;
00501 mytc.router_id = myState->myipaddr;
00502 mytc.CoreAddress = myState->CoreAddress;
00503 myState->TreeCreateSequenceNumber++;
00504 mytc.SequenceNumber = myState->TreeCreateSequenceNumber;
00505
00506 memset((char *)&si_other, 0, sizeof(si_other));
00507 si_other.sin_family = AF_INET;
00508 si_other.sin_port = htons(OBAMP_SIGNALLING_PORT);
00509 si_other.sin_addr = *addr;
00510 sendto(sdudp, &mytc, sizeof(struct OBAMP_tree_create), 0, (struct sockaddr *)&si_other, sizeof(si_other));
00511 }
00512
00513
00514 static void
00515 printObampNodesList(void)
00516 {
00517
00518 int i = 1;
00519 #if !defined(REMOVE_LOG_DEBUG)
00520 struct ipaddr_str buf;
00521 #endif
00522 struct ObampNode *tmp, *iterator;
00523
00524 if (list_is_empty(&ListOfObampNodes)) {
00525 return;
00526 }
00527
00528
00529 OLSR_DEBUG(LOG_PLUGINS, "--------------------NODE STATUS---------");
00530 OLSR_DEBUG(LOG_PLUGINS, "---Current Core: %s", ip4_to_string(&buf, myState->CoreAddress.v4));
00531 OLSR_DEBUG(LOG_PLUGINS, "---Current Parent: %s", ip4_to_string(&buf, myState->ParentId.v4));
00532
00533
00534 OLSR_DEBUG(LOG_PLUGINS, "Number \t IP \t\t IsMesh \t IsTree \t MeshLock \t outerTreeLink");
00535
00536 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
00537 OLSR_DEBUG(LOG_PLUGINS, "%d \t\t %s \t %d \t\t %d \t\t %d \t\t %d", i, ip4_to_string(&buf, tmp->neighbor_ip_addr.v4),
00538 tmp->isMesh, tmp->isTree, tmp->MeshLock, tmp->outerTreeLink);
00539
00540 i++;
00541 }
00542 OLSR_DEBUG(LOG_PLUGINS, "----------------------------------------");
00543 }
00544
00545 static int
00546 DoIHaveATreeLink(void)
00547 {
00548
00549
00550 struct ObampNode *tmp, *iterator;
00551
00552 if (list_is_empty(&ListOfObampNodes)) {
00553 return 0;
00554 }
00555
00556 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
00557 if (tmp->isTree == 1) {
00558 return 1;
00559 }
00560 }
00561 return 0;
00562 }
00563
00564
00565 void
00566 unsolicited_tree_destroy(void *x)
00567 {
00568
00569
00570 struct ObampNode *tmp, *iterator;
00571
00572 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
00573 if (tmp->isTree == 0) {
00574 send_destroy_tree_link(&tmp->neighbor_ip_addr.v4);
00575 }
00576 }
00577 x = NULL;
00578 }
00579
00580 static int
00581 DoIHaveAMeshLink(void)
00582 {
00583
00584
00585 struct ObampNode *tmp, *iterator;
00586
00587 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
00588 if (tmp->isMesh == 1) {
00589 return 1;
00590 }
00591 }
00592 return 0;
00593 }
00594
00595 static void
00596 reset_tree_links(void)
00597 {
00598 struct ObampNode *tmp, *iterator;
00599
00600 OLSR_DEBUG(LOG_PLUGINS,"Reset Tree Links Now");
00601
00602 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
00603 tmp->isTree = 0;
00604 tmp->outerTreeLink = 0;
00605 }
00606
00607 memset(&myState->ParentId.v4, 0, sizeof(myState->ParentId.v4));
00608 memset(&myState->OldParentId.v4, 1, sizeof(myState->OldParentId.v4));
00609 myState->TreeCreateSequenceNumber=0;
00610 };
00611
00612
00613
00614 static void
00615 deactivate_tree_link(struct OBAMP_tree_destroy *destroy_msg)
00616 {
00617 #if !defined(REMOVE_LOG_DEBUG)
00618 struct ipaddr_str buf;
00619 #endif
00620 struct ObampNode *tmp, *iterator;
00621
00622 if (memcmp(&myState->CoreAddress.v4, &destroy_msg->CoreAddress.v4, sizeof(struct in_addr)) != 0) {
00623 OLSR_DEBUG(LOG_PLUGINS, "Discarding message with no coherent core address");
00624 return;
00625 }
00626
00627 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
00628 if (tmp->neighbor_ip_addr.v4.s_addr == destroy_msg->router_id.v4.s_addr) {
00629 tmp->isTree = 0;
00630 OLSR_DEBUG(LOG_PLUGINS, "Tree link to %s deactivated", ip4_to_string(&buf, tmp->neighbor_ip_addr.v4));
00631
00632 if (memcmp(&tmp->neighbor_ip_addr.v4, &myState->ParentId.v4, sizeof(struct in_addr)) == 0) {
00633 OLSR_DEBUG(LOG_PLUGINS,"RESET TREE LINKS: I lost tree link with my PARENT");
00634 reset_tree_links();
00635 }
00636 return;
00637 }
00638 }
00639 }
00640
00641
00642 static void
00643 CoreElection(void)
00644 {
00645 #if !defined(REMOVE_LOG_DEBUG)
00646 struct ipaddr_str buf;
00647 #endif
00648 struct ObampNode *tmp, *iterator;
00649 u_int32_t smallestIP = 0xFFFFFFFF;
00650
00651
00652 memcpy(&myState->myipaddr.v4, &olsr_cnf->router_id, olsr_cnf->ipsize);
00653
00654 OLSR_DEBUG(LOG_PLUGINS, "SETUP: my IP - %s", ip4_to_string(&buf, myState->myipaddr.v4));
00655
00656
00657 if (list_is_empty(&ListOfObampNodes)) {
00658 OLSR_DEBUG(LOG_PLUGINS, "CoreElection: I'm alone I'm the core");
00659 myState->CoreAddress = myState->myipaddr;
00660 myState->iamcore = 1;
00661 myState->TreeCreateSequenceNumber = 0;
00662 OLSR_DEBUG(LOG_PLUGINS,"Calling Reset Tree Links");
00663 reset_tree_links();
00664 return;
00665 }
00666
00667 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
00668 if (tmp->neighbor_ip_addr.v4.s_addr < smallestIP) {
00669 smallestIP = tmp->neighbor_ip_addr.v4.s_addr;
00670 OLSR_DEBUG(LOG_PLUGINS, "CoreElection: current smallest IP is - %s", ip4_to_string(&buf, tmp->neighbor_ip_addr.v4));
00671 };
00672 }
00673
00674
00675 if (myState->myipaddr.v4.s_addr < smallestIP) {
00676 if (myState->myipaddr.v4.s_addr == myState->CoreAddress.v4.s_addr) {
00677 return;
00678 }
00679
00680
00681 myState->CoreAddress = myState->myipaddr;
00682 myState->iamcore = 1;
00683 myState->TreeCreateSequenceNumber = 0;
00684 OLSR_DEBUG(LOG_PLUGINS,"Calling Reset Tree Links");
00685 reset_tree_links();
00686 OLSR_DEBUG(LOG_PLUGINS, "I'm the core");
00687 } else {
00688 if (myState->CoreAddress.v4.s_addr == smallestIP) {
00689 return;
00690 }
00691
00692
00693 myState->iamcore = 0;
00694 myState->CoreAddress.v4.s_addr = smallestIP;
00695 OLSR_DEBUG(LOG_PLUGINS,"Calling Reset Tree Links");
00696 reset_tree_links();
00697 OLSR_DEBUG(LOG_PLUGINS, "CoreElection: current Core is - %s", ip4_to_string(&buf, tmp->neighbor_ip_addr.v4));
00698 }
00699 }
00700
00701
00702 static int
00703 UdpServer(void)
00704 {
00705 struct sockaddr_in addr;
00706
00707 if ((sdudp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
00708 OLSR_DEBUG(LOG_PLUGINS, "Socket UDP error");
00709 return -1;
00710 }
00711
00712 memset((void *)&addr, 0, sizeof(addr));
00713 addr.sin_family = AF_INET;
00714 addr.sin_port = htons(OBAMP_SIGNALLING_PORT);
00715 addr.sin_addr.s_addr = htonl(INADDR_ANY);
00716
00717 if (bind(sdudp, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
00718 OLSR_DEBUG(LOG_PLUGINS, "Socket UDP BIND error");
00719 return (-1);
00720 }
00721
00722 olsr_socket_add(sdudp, &ObampSignalling, NULL, OLSR_SOCKET_READ);
00723
00724 return 0;
00725
00726 }
00727
00728 static void
00729 decap_data(char *buffer)
00730 {
00731 struct ObampSniffingIf *tmp, *iterator;
00732 unsigned char *ipPacket;
00733 int stripped_len;
00734 struct ip *ipHeader;
00735 struct ip6_hdr *ip6Header;
00736 struct OBAMP_data_message4 *msg;
00737 int nBytesWritten;
00738 struct sockaddr_ll dest;
00739 msg = (struct OBAMP_data_message4 *)buffer;
00740
00741 ipPacket = msg->data;
00742
00743 ipHeader = (struct ip *)ipPacket;
00744 ip6Header = (struct ip6_hdr *)ipPacket;
00745
00746 OLSR_FOR_ALL_OBAMPSNIFF_ENTRIES(tmp, iterator) {
00747 memset(&dest, 0, sizeof(dest));
00748 dest.sll_family = AF_PACKET;
00749 if ((ipPacket[0] & 0xf0) == 0x40) {
00750 dest.sll_protocol = htons(ETH_P_IP);
00751 stripped_len = ntohs(ipHeader->ip_len);
00752 }
00753 if ((ipPacket[0] & 0xf0) == 0x60) {
00754 dest.sll_protocol = htons(ETH_P_IPV6);
00755 stripped_len = 40 + ntohs(ip6Header->ip6_plen);
00756 }
00757
00758
00759 dest.sll_ifindex = if_nametoindex(tmp->ifName);
00760 dest.sll_halen = IFHWADDRLEN;
00761
00762
00763
00764
00765
00766
00767
00768 memset(dest.sll_addr, 0xFF, IFHWADDRLEN);
00769
00770 nBytesWritten = sendto(tmp->skd, ipPacket, stripped_len, 0, (struct sockaddr *)&dest, sizeof(dest));
00771 if (nBytesWritten != stripped_len) {
00772 OLSR_DEBUG(LOG_PLUGINS, "sendto() error forwarding unpacked encapsulated pkt on \"%s\"", tmp->ifName);
00773 } else {
00774 OLSR_DEBUG(LOG_PLUGINS, "OBAMP: --> unpacked and forwarded on \"%s\"\n", tmp->ifName);
00775 }
00776 }
00777 }
00778
00779
00780
00781 static int
00782 CheckDataFromTreeLink(char *buffer)
00783 {
00784
00785 struct ObampNode *tmp, *iterator;
00786 struct OBAMP_data_message4 *data_msg;
00787
00788 data_msg = (struct OBAMP_data_message4 *)buffer;
00789
00790
00791 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
00792
00793 if (memcmp(&tmp->neighbor_ip_addr.v4, &data_msg->last_hop, sizeof(struct in_addr)) == 0) {
00794 if (tmp->isTree == 1) {
00795 return 1;
00796 }
00797 OLSR_DEBUG(LOG_PLUGINS, "DISCARDING DATA PACKET SENT BY NOT TREE NEIGHBOR");
00798 send_destroy_tree_link(&tmp->neighbor_ip_addr.v4);
00799 return 0;
00800 }
00801 }
00802 return 0;
00803 }
00804
00805
00806 static int
00807 CheckDupData(char *buffer)
00808 {
00809
00810 struct ObampNode *tmp, *iterator;
00811 struct OBAMP_data_message4 *data_msg;
00812
00813 data_msg = (struct OBAMP_data_message4 *)buffer;
00814
00815
00816 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
00817 if (memcmp(&tmp->neighbor_ip_addr.v4, &data_msg->router_id, sizeof(struct in_addr)) == 0) {
00818 OLSR_DEBUG(LOG_PLUGINS, "Processing Data Packet %d - Last seen was %d",data_msg->SequenceNumber, tmp->DataSeqNumber);
00819
00820 if (tmp->DataSeqNumber == 0) {
00821 tmp->DataSeqNumber = data_msg->SequenceNumber;
00822 return 1;
00823 }
00824
00825 if ( ((data_msg->SequenceNumber > tmp->DataSeqNumber) && ((data_msg->SequenceNumber - tmp->DataSeqNumber ) <= 127)) || ((tmp->DataSeqNumber > data_msg->SequenceNumber) && ((tmp->DataSeqNumber - data_msg->SequenceNumber) > 127 )))
00826 {
00827 tmp->DataSeqNumber = data_msg->SequenceNumber;
00828 return 1;
00829 }
00830 OLSR_DEBUG(LOG_PLUGINS, "DISCARDING DUP PACKET");
00831 return 0;
00832 }
00833 }
00834 return 1;
00835 }
00836
00837 static void
00838 forward_obamp_data(char *buffer)
00839 {
00840
00841 #if !defined(REMOVE_LOG_DEBUG)
00842 struct ipaddr_str buf;
00843 struct ipaddr_str buf2;
00844 #endif
00845 struct ObampNode *tmp, *iterator;
00846 struct OBAMP_data_message4 *data_msg;
00847 struct sockaddr_in si_other;
00848 struct in_addr temporary;
00849
00850
00851 data_msg = (struct OBAMP_data_message4 *)buffer;
00852 temporary.s_addr = data_msg->last_hop;
00853 data_msg->last_hop = myState->myipaddr.v4.s_addr;
00854
00855
00856 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
00857 if (tmp->isTree == 1 && memcmp(&tmp->neighbor_ip_addr.v4, &temporary.s_addr, 4) != 0) {
00858 OLSR_DEBUG(LOG_PLUGINS, "FORWARDING OBAMP DATA TO node %s because come from %s", ip4_to_string(&buf, tmp->neighbor_ip_addr.v4), ip4_to_string(&buf2,temporary));
00859
00860
00861 memset((char *)&si_other, 0, sizeof(si_other));
00862 si_other.sin_family = AF_INET;
00863 si_other.sin_port = htons(OBAMP_SIGNALLING_PORT);
00864 si_other.sin_addr = tmp->neighbor_ip_addr.v4;
00865
00866
00867 sendto(sdudp, data_msg, 17+data_msg->datalen, 0, (struct sockaddr *)&si_other, sizeof(si_other));
00868 }
00869 }
00870 }
00871
00872
00873 static void
00874 manage_hello(char *packet)
00875 {
00876
00877 struct OBAMP_hello *hello;
00878
00879 struct ObampNode *tmp, *iterator;
00880
00881 hello = (struct OBAMP_hello *)packet;
00882
00883
00884 if (memcmp(&myState->CoreAddress.v4, &hello->CoreAddress.v4, sizeof(struct in_addr)) != 0) {
00885 OLSR_DEBUG(LOG_PLUGINS, "Discarding message with no coherent core address");
00886 return;
00887 }
00888
00889 if (!list_is_empty(&ListOfObampNodes)) {
00890 OLSR_DEBUG(LOG_PLUGINS, "Very strange, list cannot be empty here !");
00891 return;
00892 }
00893
00894
00895 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
00896 if (memcmp(&tmp->neighbor_ip_addr.v4, &hello->router_id.v4, sizeof(struct in_addr)) == 0) {
00897 tmp->isMesh = 1;
00898 tmp->MeshLock = _MESH_LOCK_;
00899 }
00900 }
00901 }
00902
00903 static void
00904 manage_tree_create(char *packet)
00905 {
00906
00907 struct OBAMP_tree_create *msg;
00908
00909 #if !defined(REMOVE_LOG_DEBUG)
00910 struct ipaddr_str buf;
00911 struct ipaddr_str buf2;
00912 #endif
00913
00914 struct ObampNode *tmp, *iterator;
00915
00916 OLSR_DEBUG(LOG_PLUGINS,"manage_tree_create");
00917 msg = (struct OBAMP_tree_create *)packet;
00918
00919 if (msg->MessageID != OBAMP_TREECREATE) {
00920 OLSR_DEBUG(LOG_PLUGINS, "BIG PROBLEM, I'M IN THIS FUNCTION BUT MESSAGE IS NOT TREE CREATE");
00921 return;
00922 }
00923
00924
00925 if (memcmp(&myState->CoreAddress.v4, &msg->CoreAddress.v4, sizeof(struct in_addr)) != 0) {
00926 OLSR_DEBUG(LOG_PLUGINS, "Discarding message with no coherent core address");
00927 return;
00928 }
00929 if (myState->iamcore == 1) {
00930 return;
00931 }
00932
00933
00934 if ( (((msg->SequenceNumber > myState->TreeCreateSequenceNumber) && ((msg->SequenceNumber - myState->TreeCreateSequenceNumber ) <= 127))
00935 || ((myState->TreeCreateSequenceNumber > msg->SequenceNumber) && ((myState->TreeCreateSequenceNumber - msg->SequenceNumber) > 127 ))
00936 ) || myState->TreeCreateSequenceNumber == 0 ) {
00937
00938 OLSR_DEBUG(LOG_PLUGINS, "myState->TreeCreateSequenceNumber < msg->SequenceNumber --- %d < %d",myState->TreeCreateSequenceNumber,msg->SequenceNumber);
00939 myState->TreeCreateSequenceNumber = msg->SequenceNumber;
00940
00941
00942
00943
00944
00945
00946 if (memcmp(&msg->router_id.v4, &myState->ParentId.v4, sizeof(struct in_addr)) != 0)
00947 {
00948 OLSR_DEBUG(LOG_PLUGINS, "Receiving a tree message from a link that is not parent");
00949 if (DoIHaveATreeLink() == 0 ) {
00950 OLSR_DEBUG(LOG_PLUGINS,"Calling Reset Tree Links");
00951 reset_tree_links();
00952 myState->ParentId.v4 = msg->router_id.v4;
00953 myState->OldParentId.v4 = myState->ParentId.v4;
00954 tree_link_req(&msg->router_id.v4);
00955 }
00956 else {
00957
00958 }
00959 }
00960 else {
00961 myState->TreeHeartBeat = TREE_HEARTBEAT;
00962
00963
00964 if (list_is_empty(&ListOfObampNodes)) {
00965 OLSR_DEBUG(LOG_PLUGINS, "Very strange, list cannot be empty here !");
00966 }
00967
00968
00969 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
00970 if (tmp->isMesh == 1 && memcmp(&tmp->neighbor_ip_addr.v4, &msg->router_id.v4, sizeof(struct in_addr)) != 0) {
00971
00972 if (DoIHaveATreeLink() == 1 ) {
00973 tree_create_forward_to(&tmp->neighbor_ip_addr.v4, msg);
00974 OLSR_DEBUG(LOG_PLUGINS, "FORWARDING TREE CREATE ORIGINATOR %s TO node %s ", ip4_to_string(&buf2, msg->router_id.v4),
00975 ip4_to_string(&buf, tmp->neighbor_ip_addr.v4));
00976 }
00977 }
00978 }
00979 }
00980 }
00981 else {
00982 OLSR_DEBUG(LOG_PLUGINS, "myState->TreeCreateSequenceNumber < msg->SequenceNumber --- %d < %d",myState->TreeCreateSequenceNumber,msg->SequenceNumber);
00983 OLSR_DEBUG(LOG_PLUGINS, "DISCARDING DUP TREE CREATE");
00984 }
00985 }
00986
00987 void
00988 ObampSignalling(int skfd, void *data __attribute__ ((unused)), unsigned int flags __attribute__ ((unused)))
00989 {
00990
00991 char buffer[1500];
00992 char text_buffer[300];
00993 struct sockaddr_in addr;
00994 int n = 0;
00995 socklen_t len;
00996 u_int8_t MessageID;
00997
00998 memset(&addr, 0, sizeof(struct sockaddr_in));
00999 len = sizeof(addr);
01000
01001 if (skfd > 0) {
01002
01003
01004
01005 n = recvfrom(skfd, buffer, 1500, 0, (struct sockaddr *)&addr, &len);
01006
01007 if (n < 0) {
01008 OLSR_DEBUG(LOG_PLUGINS, "recvfrom error");
01009 }
01010
01011 inet_ntop(AF_INET, &addr.sin_addr, text_buffer, sizeof(text_buffer));
01012
01013
01014 MessageID = buffer[0];
01015
01016 switch (MessageID) {
01017
01018 case OBAMP_DATA:
01019 OLSR_DEBUG(LOG_PLUGINS, "OBAMP Received OBAMP_DATA from host %s, port %d\n", text_buffer, ntohs(addr.sin_port));
01020
01021 if (CheckDupData(buffer) && CheckDataFromTreeLink(buffer) ) {
01022 forward_obamp_data(buffer);
01023 decap_data(buffer);
01024 }
01025
01026 break;
01027
01028 case OBAMP_HELLO:
01029 OLSR_DEBUG(LOG_PLUGINS, "OBAMP Received OBAMP_HELLO from host %s, port %d\n", text_buffer, ntohs(addr.sin_port));
01030 manage_hello(buffer);
01031 break;
01032
01033 case OBAMP_TREECREATE:
01034
01035 OLSR_DEBUG(LOG_PLUGINS, "OBAMP Received OBAMP_TREECREATE from host %s, port %d\n", text_buffer, ntohs(addr.sin_port));
01036 manage_tree_create(buffer);
01037
01038 break;
01039
01040 case OBAMP_TREE_REQ:
01041 OLSR_DEBUG(LOG_PLUGINS, "OBAMP Received OBAMP_TREE_REQ from host %s, port %d\n", text_buffer, ntohs(addr.sin_port));
01042 tree_link_ack((struct OBAMP_tree_link_req *)buffer);
01043 break;
01044
01045 case OBAMP_TREE_ACK:
01046
01047 OLSR_DEBUG(LOG_PLUGINS, "OBAMP Received OBAMP_TREE_ACK from host %s, port %d\n", text_buffer, ntohs(addr.sin_port));
01048 activate_tree_link((struct OBAMP_tree_link_ack *)buffer);
01049 break;
01050
01051
01052 case OBAMP_TREE_DESTROY:
01053
01054 OLSR_DEBUG(LOG_PLUGINS, "OBAMP Received OBAMP_TREE_DESTROY from host %s, port %d\n", text_buffer, ntohs(addr.sin_port));
01055 deactivate_tree_link((struct OBAMP_tree_destroy *)buffer);
01056 break;
01057 }
01058
01059
01060 }
01061
01062 }
01063
01064
01065
01066
01067
01068
01069 int
01070 addObampNode4(struct in_addr *ipv4, u_int8_t status)
01071 {
01072 #if !defined(REMOVE_LOG_DEBUG)
01073 struct ipaddr_str buf;
01074 #endif
01075 struct ObampNode *neighbor_to_add;
01076 struct ObampNode *tmp, *iterator;
01077 neighbor_to_add = olsr_malloc(sizeof(struct ObampNode), "OBAMPNode");
01078
01079
01080
01081
01082 if (list_is_empty(&ListOfObampNodes)) {
01083
01084
01085 memcpy(&neighbor_to_add->neighbor_ip_addr.v4, ipv4, sizeof(neighbor_to_add->neighbor_ip_addr.v4));
01086 list_add_head(&ListOfObampNodes, &(neighbor_to_add->list));
01087
01088 init_overlay_neighbor(neighbor_to_add);
01089 neighbor_to_add->status = status;
01090
01091 OLSR_DEBUG(LOG_PLUGINS, "Added to list node as first node- %s\n", ip4_to_string(&buf, *ipv4));
01092
01093 CoreElection();
01094 return 0;
01095 }
01096
01097
01098
01099
01100 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
01101 if (memcmp(&tmp->neighbor_ip_addr.v4, ipv4, sizeof(tmp->neighbor_ip_addr.v4)) == 0) {
01102
01103 tmp->Texpire = _Texpire_;
01104 neighbor_to_add->status = status;
01105 free(neighbor_to_add);
01106 return 1;
01107 }
01108 }
01109
01110
01111
01112 OLSR_DEBUG(LOG_PLUGINS, "Adding to list node (NOT FIRST)- %s\n", ip4_to_string(&buf, *ipv4));
01113 memcpy(&neighbor_to_add->neighbor_ip_addr.v4, ipv4, sizeof(neighbor_to_add->neighbor_ip_addr.v4));
01114 list_add_tail(&ListOfObampNodes, &(neighbor_to_add->list));
01115 init_overlay_neighbor(neighbor_to_add);
01116 neighbor_to_add->status = status;
01117 CoreElection();
01118 return 0;
01119 }
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130 static void
01131 PacketReceivedFromOLSR(union olsr_ip_addr *originator, const uint8_t *obamp_message, int len)
01132 {
01133 u_int8_t MessageID = obamp_message[0];
01134 const struct OBAMP_alive *alive;
01135 #if !defined(REMOVE_LOG_DEBUG)
01136 struct ipaddr_str buf;
01137 #endif
01138
01139 struct in_addr *myOriginator = (struct in_addr *)originator;
01140
01141
01142 len = 0;
01143
01144
01145 switch (MessageID) {
01146
01147 case OBAMP_ALIVE:
01148 OLSR_DEBUG(LOG_PLUGINS, "OBAMP Received OBAMP_ALIVE from %s\n", ip4_to_string(&buf, *myOriginator));
01149 alive = (const struct OBAMP_alive *)obamp_message;
01150 addObampNode4(myOriginator, alive->status);
01151 printObampNodesList();
01152
01153 break;
01154 }
01155
01156
01157 }
01158
01159
01160
01161 void
01162 olsr_parser(struct olsr_message *msg, struct interface *in_if
01163 __attribute__ ((unused)), union olsr_ip_addr *ipaddr, enum duplicate_status status __attribute__ ((unused)))
01164 {
01165
01166
01167 if (msg->type != MESSAGE_TYPE) {
01168 return;
01169 }
01170
01171
01172
01173 if (olsr_ipcmp(&msg->originator, &olsr_cnf->router_id) == 0)
01174 return;
01175
01176
01177
01178 if (check_neighbor_link(ipaddr) != SYM_LINK) {
01179
01180 return;
01181 }
01182
01183 PacketReceivedFromOLSR(&msg->originator, msg->payload, msg->end - msg->payload);
01184 }
01185
01186
01187 void
01188 olsr_obamp_gen(void *packet, int len)
01189 {
01190
01191 uint8_t buffer[10240];
01192 struct olsr_message msg;
01193 struct interface *ifn, *iterator;
01194 uint8_t *curr, *sizeptr;
01195
01196
01197 msg.type = MESSAGE_TYPE;
01198 msg.vtime = OBAMP_VALID_TIME * MSEC_PER_SEC;
01199 msg.originator = olsr_cnf->router_id;
01200 msg.ttl = MAX_TTL;
01201 msg.hopcnt = 0;
01202 msg.seqno = get_msg_seqno();
01203 msg.size = 0;
01204
01205 curr = buffer;
01206 sizeptr = olsr_put_msg_hdr(&curr, &msg);
01207 memcpy(curr, packet, len);
01208
01209 len = curr - buffer + len;
01210 pkt_put_u16(&sizeptr, len);
01211
01212
01213 OLSR_FOR_ALL_INTERFACES(ifn, iterator) {
01214 if (net_outbuffer_push(ifn, buffer, len) != len) {
01215
01216 net_output(ifn);
01217 if (net_outbuffer_push(ifn, buffer, len) != len) {
01218 OLSR_DEBUG(LOG_PLUGINS, "OBAMP PLUGIN: could not send on interface: %s\n", ifn->int_name);
01219 }
01220 }
01221 }
01222 }
01223
01224 void
01225 outer_tree_create(void *x __attribute__ ((unused)))
01226 {
01227 struct ObampNode *tmp, *iterator;
01228
01229 if (!((DoIHaveATreeLink() == 0) && (myState->iamcore == 0))) {
01230 return;
01231 }
01232
01233 if (list_is_empty(&ListOfObampNodes) == 0) {
01234 OLSR_DEBUG(LOG_PLUGINS, "List empty can't send OUTER_TREE_CREATE");
01235 }
01236
01237 if (DoIHaveAMeshLink() == 0) {
01238 OLSR_DEBUG(LOG_PLUGINS, "Weird, no mesh links. Maybe other OBAMP nodes are very far (HUGE ETX)");
01239 return;
01240 }
01241
01242
01243 memcpy(&myState->myipaddr.v4, &olsr_cnf->router_id, olsr_cnf->ipsize);
01244
01245
01246 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
01247 if ((tmp->neighbor_ip_addr.v4.s_addr < myState->myipaddr.v4.s_addr) && (tmp->isMesh == 1)) {
01248 return;
01249 }
01250 }
01251
01252
01253 OLSR_DEBUG(LOG_PLUGINS, "OUTER TREE CREATE");
01254 tmp = select_tree_anchor();
01255 if (tmp == NULL) {
01256 OLSR_DEBUG(LOG_PLUGINS, "CANT FIND ANCHOR");
01257 return;
01258 }
01259
01260 tmp->isMesh = 1;
01261 tmp->outerTreeLink = 1;
01262 myState->OldParentId.v4 = tmp->neighbor_ip_addr.v4;
01263 myState->ParentId.v4 = tmp->neighbor_ip_addr.v4;
01264 myState->TreeHeartBeat = TREE_HEARTBEAT;
01265 tree_link_req(&tmp->neighbor_ip_addr.v4);
01266 }
01267
01268
01269 void
01270 tree_create(void *x __attribute__ ((unused)))
01271 {
01272 #if !defined(REMOVE_LOG_DEBUG)
01273 struct ipaddr_str buf;
01274 #endif
01275 struct ObampNode *tmp, *iterator;
01276
01277
01278 if (myState->iamcore != 1) {
01279 return;
01280 }
01281
01282 if (list_is_empty(&ListOfObampNodes)) {
01283 OLSR_DEBUG(LOG_PLUGINS, "List empty can't send TREE_CREATE");
01284 return;
01285 }
01286
01287
01288 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
01289 if (tmp->isMesh == 1) {
01290
01291 tree_create_gen(&tmp->neighbor_ip_addr.v4);
01292
01293 OLSR_DEBUG(LOG_PLUGINS, "CORE SENDS TREE CREATE TO node %s ", ip4_to_string(&buf, tmp->neighbor_ip_addr.v4));
01294 }
01295 }
01296 }
01297
01298
01299
01300
01301 void
01302 mesh_create(void *x __attribute__ ((unused)))
01303 {
01304 #if !defined(REMOVE_LOG_DEBUG)
01305 struct ipaddr_str buf;
01306 #endif
01307 struct ObampNode *tmp, *iterator;
01308
01309 struct rt_entry *rt;
01310
01311 unsigned int mincost = 5;
01312
01313 int meshchanged = 0;
01314
01315 if (list_is_empty(&ListOfObampNodes)) {
01316 OLSR_DEBUG(LOG_PLUGINS, "List empty can't create Overlay Mesh");
01317 return;
01318 }
01319
01320
01321 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
01322
01323 tmp->wasMesh = tmp->isMesh;
01324
01325
01326 if (tmp->MeshLock == 0 && tmp->outerTreeLink == 0) {
01327 tmp->isMesh = 0;
01328 }
01329
01330 rt = olsr_lookup_routing_table(&tmp->neighbor_ip_addr);
01331 if (rt == NULL) {
01332 continue;
01333 }
01334
01335
01336
01337 if (rt->rt_best->rtp_metric.cost / 65536 > 5) {
01338 continue;
01339 }
01340
01341
01342 if ((rt->rt_best->rtp_metric.cost / 65536) < mincost) {
01343 mincost = (rt->rt_best->rtp_metric.cost / 65536);
01344 }
01345 }
01346
01347
01348 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
01349 rt = olsr_lookup_routing_table(&tmp->neighbor_ip_addr);
01350 if (rt == NULL) {
01351 continue;
01352 }
01353
01354 if (rt->rt_best->rtp_metric.cost / 65536 > 5) {
01355 continue;
01356 }
01357
01358 if ((rt->rt_best->rtp_metric.cost / 65536) - 1 < mincost) {
01359 tmp->isMesh = 1;
01360 OLSR_DEBUG(LOG_PLUGINS, "Choosed Overlay Neighbor node %s costs %u", ip4_to_string(&buf, tmp->neighbor_ip_addr.v4),
01361 rt->rt_best->rtp_metric.cost / 65536);
01362
01363 obamp_hello(&tmp->neighbor_ip_addr.v4);
01364 }
01365
01366 if (tmp->outerTreeLink == 1) {
01367 obamp_hello(&tmp->neighbor_ip_addr.v4);
01368 }
01369
01370 if (tmp->isMesh != tmp->wasMesh) {
01371 meshchanged++;
01372 if (tmp->isMesh == 0 && tmp->isTree == 1) {
01373 tmp->isTree = 0;
01374 send_destroy_tree_link(&tmp->neighbor_ip_addr.v4);
01375
01376 if (memcmp(&tmp->neighbor_ip_addr.v4, &myState->ParentId.v4, sizeof(struct in_addr)) == 0) {
01377 OLSR_DEBUG(LOG_PLUGINS,"RESET TREE LINKS: I lost tree link with my PARENT");
01378 reset_tree_links();
01379 }
01380 }
01381 }
01382 }
01383
01384 if (meshchanged) {
01385
01386 }
01387 }
01388
01389
01390 void
01391 purge_nodes(void *x __attribute__ ((unused)))
01392 {
01393 #if !defined(REMOVE_LOG_DEBUG)
01394 struct ipaddr_str buf;
01395 #endif
01396 struct ObampNode *tmp, *iterator;
01397 int nodesdeleted = 0;
01398
01399 if (myState->TreeHeartBeat > 0) {
01400 myState->TreeHeartBeat--;
01401 }
01402
01403 if (myState->TreeRequestDelay > 0) {
01404 myState->TreeRequestDelay--;
01405 }
01406
01407 if (myState->TreeHeartBeat == 0 && myState->iamcore == 0){
01408 OLSR_DEBUG(LOG_PLUGINS,"Calling Reset Tree Links");
01409 reset_tree_links();
01410 }
01411
01412
01413 if (list_is_empty(&ListOfObampNodes)) {
01414
01415 return;
01416 }
01417
01418 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
01419 tmp->Texpire--;
01420 if (tmp->MeshLock != 0) {
01421 tmp->MeshLock--;
01422 }
01423
01424
01425 if (tmp->Texpire == 0) {
01426 OLSR_DEBUG(LOG_PLUGINS, "Purging node %s", ip4_to_string(&buf, tmp->neighbor_ip_addr.v4));
01427 list_remove(&tmp->list);
01428
01429
01430 free(tmp);
01431 nodesdeleted++;
01432 }
01433 }
01434
01435 if (nodesdeleted != 0) {
01436 CoreElection();
01437 }
01438 }
01439
01440
01441 void
01442 obamp_alive_gen(void *x __attribute__ ((unused)))
01443 {
01444 struct OBAMP_alive myAlive;
01445 OLSR_DEBUG(LOG_PLUGINS, "Calling obamp_alive_gen\n");
01446
01447 memset(&myAlive, 0, sizeof(myAlive));
01448 myAlive.MessageID = OBAMP_ALIVE;
01449 myAlive.status = DoIHaveATreeLink();
01450 olsr_obamp_gen(&myAlive, sizeof(struct OBAMP_alive));
01451 }
01452
01453
01454
01455
01456
01457
01458
01459
01460 void
01461 EncapFlowInObamp(int skfd, void *data __attribute__ ((unused)), unsigned int flags __attribute__ ((unused)))
01462 {
01463 unsigned char ipPacket[1500];
01464
01465 #if !defined(REMOVE_LOG_DEBUG)
01466 struct ipaddr_str buf;
01467 #endif
01468 struct ObampNode *tmp, *iterator;
01469
01470 union olsr_ip_addr dst;
01471 struct ip *ipHeader;
01472 struct ip6_hdr *ipHeader6;
01473
01474 struct sockaddr_ll pktAddr;
01475 socklen_t addrLen;
01476 int nBytes;
01477
01478 if (skfd < 0) {
01479 return;
01480 }
01481
01482 addrLen = sizeof(pktAddr);
01483
01484 nBytes = recvfrom(skfd, ipPacket, 1500,
01485 0, (struct sockaddr *)&pktAddr, &addrLen);
01486 if (nBytes < 0) {
01487 return;
01488 }
01489
01490
01491
01492
01493
01494
01495
01496 if (nBytes < (int)sizeof(struct ip)) {
01497 OLSR_DEBUG(LOG_PLUGINS, "Captured frame too short");
01498 return;
01499 }
01500
01501 if (pktAddr.sll_pkttype == PACKET_OUTGOING || pktAddr.sll_pkttype == PACKET_MULTICAST)
01502
01503 {
01504
01505 if ((ipPacket[0] & 0xf0) == 0x40) {
01506 ipHeader = (struct ip *)ipPacket;
01507
01508 dst.v4 = ipHeader->ip_dst;
01509
01510
01511 if (!IsMulticast(&dst)) {
01512 return;
01513 }
01514
01515 if (ipHeader->ip_p != SOL_UDP) {
01516
01517 OLSR_DEBUG(LOG_PLUGINS, "NON UDP PACKET\n");
01518 return;
01519 }
01520
01521
01522 OLSR_FOR_ALL_OBAMPNODE_ENTRIES(tmp, iterator) {
01523 if (tmp->isTree == 1) {
01524 OLSR_DEBUG(LOG_PLUGINS, "Pushing data to Tree link to %s", ip4_to_string(&buf, tmp->neighbor_ip_addr.v4));
01525 SendOBAMPData(&tmp->neighbor_ip_addr.v4, ipPacket, nBytes);
01526 }
01527 }
01528 }
01529 else if ((ipPacket[0] & 0xf0) == 0x60) {
01530 ipHeader6 = (struct ip6_hdr *)ipPacket;
01531 if (ipHeader6->ip6_dst.s6_addr[0] != 0xff) {
01532 return;
01533 }
01534
01535 if (ipHeader6->ip6_nxt != SOL_UDP) {
01536
01537 OLSR_DEBUG(LOG_PLUGINS, "NON UDP PACKET\n");
01538 return;
01539 }
01540 }
01541 else {
01542 return;
01543 }
01544
01545
01546
01547 }
01548 }
01549
01550
01551 int
01552 AddObampSniffingIf(const char *ifName,
01553 void *data __attribute__ ((unused)), set_plugin_parameter_addon addon __attribute__ ((unused)))
01554 {
01555
01556 struct ObampSniffingIf *ifToAdd;
01557
01558 OLSR_DEBUG(LOG_PLUGINS, "AddObampSniffingIf");
01559
01560
01561 assert(ifName != NULL);
01562
01563 ifToAdd = olsr_malloc(sizeof(struct ObampSniffingIf), "OBAMPSniffingIf");
01564
01565 strncpy(ifToAdd->ifName, ifName, 16);
01566 ifToAdd->ifName[15] = '\0';
01567
01568 OLSR_DEBUG(LOG_PLUGINS, "Adding interface to list");
01569
01570 list_add_tail(&ListOfObampSniffingIf, &(ifToAdd->list));
01571
01572 OLSR_DEBUG(LOG_PLUGINS, "Adding if %s to list of ObampSniffingIfaces", ifToAdd->ifName);
01573
01574 return 0;
01575 }
01576
01577
01578 int
01579 PreInitOBAMP(void)
01580 {
01581 list_init_head(&ListOfObampSniffingIf);
01582 return 0;
01583
01584 }
01585
01586
01587
01588 int
01589 InitOBAMP(void)
01590 {
01591 #if !defined(REMOVE_LOG_DEBUG)
01592 struct ipaddr_str buf;
01593 #endif
01594
01595
01596
01597 struct olsr_timer_entry *OBAMP_alive_timer;
01598 struct olsr_timer_entry *purge_nodes_timer;
01599 struct olsr_timer_entry *mesh_create_timer;
01600 struct olsr_timer_entry *tree_create_timer;
01601 struct olsr_timer_entry *outer_tree_create_timer;
01602 struct olsr_timer_entry *unsolicited_tree_destroy_timer;
01603
01604
01605
01606 struct olsr_timer_info *OBAMP_alive_gen_timer_cookie = NULL;
01607 struct olsr_timer_info *purge_nodes_timer_cookie = NULL;
01608 struct olsr_timer_info *mesh_create_timer_cookie = NULL;
01609 struct olsr_timer_info *tree_create_timer_cookie = NULL;
01610 struct olsr_timer_info *outer_tree_create_timer_cookie = NULL;
01611 struct olsr_timer_info *unsolicited_tree_destroy_timer_cookie = NULL;
01612
01613 if (olsr_cnf->ip_version == AF_INET6) {
01614 OLSR_ERROR(LOG_PLUGINS, "OBAMP does not support IPv6 at the moment.");
01615 return 1;
01616 }
01617
01618
01619 myState = olsr_malloc(sizeof(struct ObampNodeState), "OBAMPNodeState");
01620 myState->iamcore = 1;
01621 myState->TreeHeartBeat = 0;
01622 myState->TreeRequestDelay = 0;
01623 memcpy(&myState->myipaddr.v4, &olsr_cnf->router_id, olsr_cnf->ipsize);
01624 myState->CoreAddress = myState->myipaddr;
01625
01626 myState->DataSequenceNumber = 0;
01627 myState->TreeCreateSequenceNumber = 0;
01628 myState->tree_req_sn = 0;
01629
01630 memset(&myState->ParentId.v4, 0, sizeof(myState->ParentId.v4));
01631 memset(&myState->OldParentId.v4, 1, sizeof(myState->OldParentId.v4));
01632
01633 OLSR_DEBUG(LOG_PLUGINS, "SETUP: my IP - %s", ip4_to_string(&buf, myState->myipaddr.v4));
01634
01635 list_init_head(&ListOfObampNodes);
01636
01637
01638 OBAMP_alive_gen_timer_cookie =
01639 olsr_timer_add("OBAMP Alive Generation", &obamp_alive_gen, true);
01640 purge_nodes_timer_cookie =
01641 olsr_timer_add("purge nodes Generation", &purge_nodes, true);
01642 mesh_create_timer_cookie =
01643 olsr_timer_add("mesh create Generation", &mesh_create, true);
01644 tree_create_timer_cookie =
01645 olsr_timer_add("tree create Generation", &tree_create, true);
01646 outer_tree_create_timer_cookie =
01647 olsr_timer_add("outer tree create Generation", &outer_tree_create, true);
01648 unsolicited_tree_destroy_timer_cookie =
01649 olsr_timer_add("tree destroy Generation", &unsolicited_tree_destroy, true);
01650
01651
01652
01653 olsr_parser_add_function(&olsr_parser, PARSER_TYPE);
01654
01655
01656 OBAMP_alive_timer =
01657 olsr_timer_start(OBAMP_ALIVE_EIVAL * MSEC_PER_SEC, OBAMP_JITTER, NULL,
01658 OBAMP_alive_gen_timer_cookie);
01659
01660
01661 purge_nodes_timer =
01662 olsr_timer_start(_Texpire_timer_ * MSEC_PER_SEC, OBAMP_JITTER, NULL,
01663 purge_nodes_timer_cookie);
01664
01665
01666 mesh_create_timer =
01667 olsr_timer_start(OBAMP_MESH_CREATE_IVAL * MSEC_PER_SEC, OBAMP_JITTER, NULL,
01668 mesh_create_timer_cookie);
01669
01670
01671 tree_create_timer =
01672 olsr_timer_start(OBAMP_TREE_CREATE_IVAL * MSEC_PER_SEC, OBAMP_JITTER, NULL,
01673 tree_create_timer_cookie);
01674
01675
01676 outer_tree_create_timer =
01677 olsr_timer_start(OBAMP_OUTER_TREE_CREATE_IVAL * MSEC_PER_SEC, OBAMP_JITTER, NULL,
01678 tree_create_timer_cookie);
01679
01680
01681 unsolicited_tree_destroy_timer =
01682 olsr_timer_start(30 * MSEC_PER_SEC, OBAMP_JITTER, NULL,
01683 unsolicited_tree_destroy_timer_cookie);
01684
01685
01686 OLSR_DEBUG(LOG_PLUGINS, "Launch Udp Servers");
01687 if (UdpServer() < 0) {
01688 OLSR_DEBUG(LOG_PLUGINS, "Problem in Launch Udp Servers");
01689
01690 };
01691
01692
01693 CreateObampSniffingInterfaces();
01694
01695 return 0;
01696 }
01697
01698
01699
01700
01701
01702
01703
01704
01705
01706 void
01707 CloseOBAMP(void)
01708 {
01709
01710 }