2 * The olsr.org Optimized Link-State Routing daemon(olsrd)
3 * Copyright (c) 2004-2009, the olsr.org team - see HISTORY file
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of olsr.org, olsrd nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
33 * Visit http://www.olsr.org for more information.
35 * If you find this software useful feel free to make a donation
36 * to the project. For more information see the website or contact
37 * the copyright holders.
45 #include <stddef.h> /* NULL */
46 #include <sys/types.h> /* ssize_t */
47 #include <string.h> /* strerror() */
48 #include <stdarg.h> /* va_list, va_start, va_end */
49 #include <errno.h> /* errno */
50 #include <assert.h> /* assert() */
53 #include <linux/if_ether.h> /* ETH_P_IP */
54 #include <linux/if_packet.h> /* struct sockaddr_ll, PACKET_MULTICAST */
55 //#include <pthread.h> /* pthread_t, pthread_create() */
56 #include <signal.h> /* sigset_t, sigfillset(), sigdelset(), SIGINT */
57 #include <netinet/ip.h> /* struct ip */
58 #include <netinet/udp.h> /* struct udphdr */
59 #include <unistd.h> /* close() */
61 #include <netinet/in.h>
62 #include <netinet/ip6.h>
67 #include "plugin_util.h" /* set_plugin_int */
68 #include "defs.h" /* olsr_cnf, //OLSR_PRINTF */
70 #include "olsr.h" /* //OLSR_PRINTF */
71 #include "mid_set.h" /* mid_lookup_main_addr() */
72 #include "link_set.h" /* get_best_link_to_neighbor() */
73 #include "net_olsr.h" /* ipequal */
77 #include "NetworkInterfaces.h" /* NonOlsrInterface,
78 CreateBmfNetworkInterfaces(),
79 CloseBmfNetworkInterfaces() */
80 //#include "Address.h" /* IsMulticast() */
81 #include "Packet.h" /* ENCAP_HDR_LEN,
84 #include "PacketHistory.h"
88 int P2pdUseHash = 0; /* Switch off hash filter by default */
89 int P2pdDuplicateTimeout = P2PD_VALID_TIME;
91 /* List of UDP destination address and port information */
92 struct UdpDestPort * UdpDestPortList = NULL;
94 /* List of filter entries to check for duplicate messages
96 struct node * dupFilterHead = NULL;
97 struct node * dupFilterTail = NULL;
99 bool is_broadcast(const struct sockaddr_in addr);
100 bool is_multicast(const struct sockaddr_in addr);
101 char * get_ipv4_str(uint32_t address, char *s, size_t maxlen);
102 char * get_ipv6_str(unsigned char* address, char *s, size_t maxlen);
103 void dump_packet(unsigned char* packet, int length);
104 bool check_and_mark_recent_packet(unsigned char *data, int len);
106 /* -------------------------------------------------------------------------
107 * Function : PacketReceivedFromOLSR
108 * Description: Handle a received packet from a OLSR message
109 * Input : ipPacket into an unsigned char and the lenght of the packet
112 * Data Used : BmfInterfaces
113 * ------------------------------------------------------------------------- */
115 PacketReceivedFromOLSR(unsigned char *encapsulationUdpData, int len)
117 struct ip *ipHeader; /* IP header inside the encapsulated IP packet */
118 struct ip6_hdr *ip6Header; /* IP header inside the encapsulated IP packet */
119 struct udphdr *udpHeader;
120 struct NonOlsrInterface *walker;
121 int stripped_len = 0;
122 union olsr_ip_addr destAddr;
124 bool isInList = false;
126 ipHeader = (struct ip *) ARM_NOWARN_ALIGN(encapsulationUdpData);
127 ip6Header = (struct ip6_hdr *) ARM_NOWARN_ALIGN(encapsulationUdpData);
128 //OLSR_DEBUG(LOG_PLUGINS, "P2PD PLUGIN got packet from OLSR message\n");
130 if (check_and_mark_recent_packet(encapsulationUdpData, len))
133 /* Check with each network interface what needs to be done on it */
134 for (walker = nonOlsrInterfaces; walker != NULL; walker = walker->next) {
135 /* To a non-OLSR interface: unpack encapsulated IP packet and forward it */
136 if (walker->olsrIntf == NULL) {
138 struct sockaddr_ll dest;
140 memset(&dest, 0, sizeof(dest));
141 dest.sll_family = AF_PACKET;
142 if ((encapsulationUdpData[0] & 0xf0) == 0x40) {
143 dest.sll_protocol = htons(ETH_P_IP);
144 stripped_len = ntohs(ipHeader->ip_len);
147 if ((encapsulationUdpData[0] & 0xf0) == 0x60) {
148 dest.sll_protocol = htons(ETH_P_IPV6);
149 stripped_len = 40 + ntohs(ip6Header->ip6_plen); // IPv6 Header size (40)
153 // Sven-Ola: Don't know how to handle the "stripped_len is uninitialized"
154 // condition, maybe exit(1) is better...?
155 if (0 == stripped_len)
158 //TODO: if packet is not IP die here
160 if (stripped_len > len) {
163 dest.sll_ifindex = if_nametoindex(walker->ifName);
164 dest.sll_halen = IFHWADDRLEN;
166 if (olsr_cnf->ip_version == AF_INET) {
167 /* Use all-ones as destination MAC address. When the IP destination is
168 * a multicast address, the destination MAC address should normally also
169 * be a multicast address. E.g., when the destination IP is 224.0.0.1,
170 * the destination MAC should be 01:00:5e:00:00:01. However, it does not
171 * seem to matter when the destination MAC address is set to all-ones
175 if (IsMulticastv4(ipHeader)) {
176 in_addr_t addr = ntohl(ipHeader->ip_dst.s_addr);
178 dest.sll_addr[0] = 0x01;
179 dest.sll_addr[1] = 0x00;
180 dest.sll_addr[2] = 0x5E;
181 dest.sll_addr[3] = (addr >> 16) & 0x7F;
182 dest.sll_addr[4] = (addr >> 8) & 0xFF;
183 dest.sll_addr[5] = addr & 0xFF;
185 /* broadcast or whatever */
186 memset(dest.sll_addr, 0xFF, IFHWADDRLEN);
188 } else /*(olsr_cnf->ip_version == AF_INET6) */ {
189 if (IsMulticastv6(ip6Header)) {
190 dest.sll_addr[0] = 0x33;
191 dest.sll_addr[1] = 0x33;
192 dest.sll_addr[2] = ip6Header->ip6_dst.s6_addr[12];
193 dest.sll_addr[3] = ip6Header->ip6_dst.s6_addr[13];
194 dest.sll_addr[4] = ip6Header->ip6_dst.s6_addr[14];
195 dest.sll_addr[5] = ip6Header->ip6_dst.s6_addr[15];
199 if (olsr_cnf->ip_version == AF_INET) {
200 // Determine the IP address and the port from the header information
201 if (ipHeader->ip_p == SOL_UDP && !IsIpv4Fragment(ipHeader)) {
202 udpHeader = (struct udphdr*) ARM_NOWARN_ALIGN((encapsulationUdpData +
203 GetIpHeaderLength(encapsulationUdpData)));
204 destAddr.v4.s_addr = ipHeader->ip_dst.s_addr;
205 destPort = htons(udpHeader->dest);
206 isInList = InUdpDestPortList(AF_INET, &destAddr, destPort);
207 #ifdef INCLUDE_DEBUG_OUTPUT
211 "%s: Not in dest/port list: %s:%d\n",
213 get_ipv4_str(destAddr.v4.s_addr,
220 } else /* (olsr_cnf->ip_version == AF_INET6) */ {
221 if (ip6Header->ip6_nxt == SOL_UDP && !IsIpv6Fragment(ip6Header)) {
222 udpHeader = (struct udphdr*) ARM_NOWARN_ALIGN((encapsulationUdpData + 40));
223 memcpy(&destAddr.v6, &ip6Header->ip6_dst, sizeof(struct in6_addr));
224 destPort = htons(udpHeader->dest);
225 isInList = InUdpDestPortList(AF_INET6, &destAddr, destPort);
226 #ifdef INCLUDE_DEBUG_OUTPUT
230 "%s: Not in dest/port list: %s:%d\n",
232 get_ipv6_str(destAddr.v6.s6_addr,
242 /* Address/port combination of this packet is not in the UDP dest/port
243 * list and will therefore be suppressed. I.e. just continue with the
244 * next interface to emit on.
249 nBytesWritten = sendto(walker->capturingSkfd,
250 encapsulationUdpData,
253 (struct sockaddr *)&dest,
255 if (nBytesWritten != stripped_len) {
256 P2pdPError("sendto() error forwarding unpacked encapsulated pkt on \"%s\"",
262 // "%s: --> unpacked and forwarded on \"%s\"\n",
263 // PLUGIN_NAME_SHORT,
266 } /* if (walker->olsrIntf == NULL) */
268 } /* PacketReceivedFromOLSR */
270 /* Highest-numbered open socket file descriptor. To be used as first
271 * parameter in calls to select(...).
273 int HighestSkfd = -1;
275 /* Set of socket file descriptors */
278 /* -------------------------------------------------------------------------
279 * Function : p2pd_message_seen
280 * Description: Check whether the current message has been seen before
281 * Input : head - start of the list to check for the message
282 * tail - end of the list to check for the message
283 * m - message to check for in the list
285 * Return : true if message was found, false otherwise
286 * Data Used : P2pdDuplicateTimeout
287 * ------------------------------------------------------------------------- */
289 p2pd_message_seen(struct node **head, struct node **tail, union olsr_message *m)
296 // Check whether any entries have aged
299 struct DupFilterEntry *filter;
300 struct node * next = curr->next; // Save the current pointer since curr may
303 filter = (struct DupFilterEntry*)curr->data;
305 if ((filter->creationtime + P2pdDuplicateTimeout) < now)
306 remove_node(head, tail, curr, true);
308 // Skip to the next element
312 // Now check whether there are any duplicates
313 for (curr = *head; curr; curr = curr->next) {
314 struct DupFilterEntry *filter = (struct DupFilterEntry*)curr->data;
316 if (olsr_cnf->ip_version == AF_INET) {
317 if (filter->address.v4.s_addr == m->v4.originator &&
318 filter->msgtype == m->v4.olsr_msgtype &&
319 filter->seqno == m->v4.seqno) {
322 } else /* if (olsr_cnf->ip_version == AF_INET6) */ {
323 if (memcmp(filter->address.v6.s6_addr,
324 m->v6.originator.s6_addr,
325 sizeof(m->v6.originator.s6_addr)) == 0 &&
326 filter->msgtype == m->v6.olsr_msgtype &&
327 filter->seqno == m->v6.seqno) {
336 /* -------------------------------------------------------------------------
337 * Function : p2pd_store_message
338 * Description: Store a new message in the duplicate message check list
339 * Input : head - start of the list to add the message to
340 * tail - end of the list to add the message to
341 * m - message to add to the list
345 * ------------------------------------------------------------------------- */
347 p2pd_store_message(struct node **head, struct node **tail, union olsr_message *m)
351 // Store a message into the database
352 struct DupFilterEntry *new_dup = calloc(1, sizeof(struct DupFilterEntry));
353 if (new_dup == NULL) {
354 OLSR_PRINTF(1, "%s: Out of memory\n", PLUGIN_NAME_SHORT);
360 new_dup->creationtime = now;
361 if (olsr_cnf->ip_version == AF_INET) {
362 new_dup->address.v4.s_addr = m->v4.originator;
363 new_dup->msgtype = m->v4.olsr_msgtype;
364 new_dup->seqno = m->v4.seqno;
365 } else /* if (olsr_cnf->ip_version == AF_INET6) */ {
366 memcpy(new_dup->address.v6.s6_addr,
367 m->v6.originator.s6_addr,
368 sizeof(m->v6.originator.s6_addr));
369 new_dup->msgtype = m->v6.olsr_msgtype;
370 new_dup->seqno = m->v6.seqno;
373 // Add the element to the head of the list
374 append_node(head, tail, new_dup);
377 /* -------------------------------------------------------------------------
378 * Function : p2pd_is_duplicate_message
379 * Description: Check whether the specified message is a duplicate
380 * Input : msg - message to check for in the list of duplicate messages
382 * Return : true if message was found, false otherwise
384 * ------------------------------------------------------------------------- */
386 p2pd_is_duplicate_message(union olsr_message *msg)
388 if(p2pd_message_seen(&dupFilterHead, &dupFilterTail, msg)) {
392 p2pd_store_message(&dupFilterHead, &dupFilterTail, msg);
397 /* -------------------------------------------------------------------------
398 * Function : olsr_parser
399 * Description: Function to be passed to the parser engine. This function
400 * processes the incoming message and passes it on if necessary.
401 * Input : m - message to parse
402 * in_if - interface to use (unused in this application)
403 * ipaddr - IP-address to use (unused in this application)
405 * Return : false if message should be supressed, true otherwise
407 * ------------------------------------------------------------------------- */
409 olsr_parser(union olsr_message *m,
410 struct interface *in_if __attribute__ ((unused)),
411 union olsr_ip_addr *ipaddr __attribute__ ((unused)))
413 union olsr_ip_addr originator;
416 //OLSR_DEBUG(LOG_PLUGINS, "P2PD PLUGIN: Received msg in parser\n");
418 /* Fetch the originator of the messsage */
419 if (olsr_cnf->ip_version == AF_INET) {
420 memcpy(&originator, &m->v4.originator, olsr_cnf->ipsize);
421 size = ntohs(m->v4.olsr_msgsize);
423 memcpy(&originator, &m->v6.originator, olsr_cnf->ipsize);
424 size = ntohs(m->v6.olsr_msgsize);
427 /* Check if message originated from this node.
428 * If so - back off */
429 if (ipequal(&originator, &olsr_cnf->main_addr))
430 return false; /* Don't forward either */
432 /* Check for duplicate messages for processing */
433 if (p2pd_is_duplicate_message(m))
434 return true; /* Don't process but allow to be forwarded */
436 if (olsr_cnf->ip_version == AF_INET) {
437 PacketReceivedFromOLSR((unsigned char *)&m->v4.message, size - 12);
439 PacketReceivedFromOLSR((unsigned char *)&m->v6.message, size - 12 - 96);
445 /* -------------------------------------------------------------------------
446 * Function : olsr_p2pd_gen
447 * Description: Sends a packet in the OLSR network
448 * Input : packet - packet to send in the OLSR network
449 * len - length of the packet to send
453 * ------------------------------------------------------------------------- */
455 olsr_p2pd_gen(unsigned char *packet, int len)
457 /* send buffer: huge */
460 union olsr_message *message = (union olsr_message *)buffer;
461 struct interface *ifn;
465 if ((aligned_size % 4) != 0) {
466 aligned_size = (aligned_size - (aligned_size % 4)) + 4;
470 if (olsr_cnf->ip_version == AF_INET) {
472 message->v4.olsr_msgtype = P2PD_MESSAGE_TYPE;
473 message->v4.olsr_vtime = reltime_to_me(P2PD_VALID_TIME * MSEC_PER_SEC);
474 memcpy(&message->v4.originator, &olsr_cnf->main_addr, olsr_cnf->ipsize);
475 message->v4.ttl = P2pdTtl ? P2pdTtl : MAX_TTL;
476 message->v4.hopcnt = 0;
477 message->v4.seqno = htons(get_msg_seqno());
478 message->v4.olsr_msgsize = htons(aligned_size + 12);
479 memset(&message->v4.message, 0, aligned_size);
480 memcpy(&message->v4.message, packet, len);
481 aligned_size = aligned_size + 12;
482 } else /* if (olsr_cnf->ip_version == AF_INET6) */ {
484 message->v6.olsr_msgtype = P2PD_MESSAGE_TYPE;
485 message->v6.olsr_vtime = reltime_to_me(P2PD_VALID_TIME * MSEC_PER_SEC);
486 memcpy(&message->v6.originator, &olsr_cnf->main_addr, olsr_cnf->ipsize);
487 message->v6.ttl = P2pdTtl ? P2pdTtl : MAX_TTL;
488 message->v6.hopcnt = 0;
489 message->v6.seqno = htons(get_msg_seqno());
490 message->v6.olsr_msgsize = htons(aligned_size + 12 + 96);
491 memset(&message->v6.message, 0, aligned_size);
492 memcpy(&message->v6.message, packet, len);
493 aligned_size = aligned_size + 12 + 96;
496 /* looping through interfaces */
497 for (ifn = ifnet; ifn; ifn = ifn->int_next) {
498 //OLSR_PRINTF(1, "%s: Generating packet - [%s]\n", PLUGIN_NAME_SHORT, ifn->int_name);
500 if (net_outbuffer_push(ifn, message, aligned_size) != aligned_size) {
501 /* send data and try again */
503 if (net_outbuffer_push(ifn, message, aligned_size) != aligned_size) {
504 //OLSR_PRINTF(1, "%s: could not send on interface: %s\n", PLUGIN_NAME_SHORT, ifn->int_name);
510 /* -------------------------------------------------------------------------
511 * Function : P2pdPError
512 * Description: Prints an error message at OLSR debug level 1.
513 * First the plug-in name is printed. Then (if format is not NULL
514 * and *format is not empty) the arguments are printed, followed
515 * by a colon and a blank. Then the message and a new-line.
516 * Input : format, arguments
520 * ------------------------------------------------------------------------- */
522 P2pdPError(const char *format, ...)
524 #define MAX_STR_DESC 255
525 char strDesc[MAX_STR_DESC];
527 #if !defined REMOVE_LOG_DEBUG
528 char *stringErr = strerror(errno);
531 /* Rely on short-circuit boolean evaluation */
532 if (format == NULL || *format == '\0') {
533 //OLSR_DEBUG(LOG_PLUGINS, "%s: %s\n", PLUGIN_NAME, stringErr);
537 va_start(arglist, format);
538 vsnprintf(strDesc, MAX_STR_DESC, format, arglist);
541 strDesc[MAX_STR_DESC - 1] = '\0'; /* Ensures null termination */
543 #if !defined REMOVE_LOG_DEBUG
544 OLSR_DEBUG(LOG_PLUGINS, "%s: %s\n", strDesc, stringErr);
549 /* -------------------------------------------------------------------------
550 * Function : MainAddressOf
551 * Description: Lookup the main address of a node
552 * Input : ip - IP address of the node
554 * Return : The main IP address of the node
556 * ------------------------------------------------------------------------- */
558 MainAddressOf(union olsr_ip_addr *ip)
560 union olsr_ip_addr *result;
562 /* TODO: mid_lookup_main_addr() is not thread-safe! */
563 result = mid_lookup_main_addr(ip);
564 if (result == NULL) {
568 } /* MainAddressOf */
571 /* -------------------------------------------------------------------------
572 * Function : InUdpDestPortList
573 * Description: Check whether the specified address and port is in the list of
574 * configured UDP destination/port entries
575 * Input : ip_version - IP version to use for this check
576 * addr - address to check for in the list
577 * port - port to check for in the list
579 * Return : true if destination/port combination was found, false otherwise
580 * Data Used : UdpDestPortList
581 * ------------------------------------------------------------------------- */
583 InUdpDestPortList(int ip_version, union olsr_ip_addr *addr, uint16_t port)
585 struct UdpDestPort *walker;
587 for (walker = UdpDestPortList; walker; walker = walker->next) {
588 if (walker->ip_version == ip_version) {
589 if (ip_version == AF_INET) {
590 if (addr->v4.s_addr == walker->address.v4.s_addr &&
591 walker->port == port)
592 return true; // Found so we can stop here
593 } else /* ip_version == AF_INET6 */ {
594 if ((memcmp(addr->v6.s6_addr,
595 walker->address.v6.s6_addr,
596 sizeof(addr->v6.s6_addr)) == 0) &&
597 (walker->port == port))
598 return true; // Found so we can stop here
605 /* -------------------------------------------------------------------------
606 * Function : P2pdPacketCaptured
607 * Description: Handle a captured IP packet
608 * Input : encapsulationUdpData - space for the encapsulation header,
609 * followed by the captured IP packet
610 * nBytes - The number of bytes in the data packet
613 * Data Used : P2pdInterfaces
614 * Notes : The IP packet is assumed to be captured on a socket of family
615 * PF_PACKET and type SOCK_DGRAM (cooked).
616 * ------------------------------------------------------------------------- */
618 P2pdPacketCaptured(unsigned char *encapsulationUdpData, int nBytes)
620 union olsr_ip_addr dst; /* Destination IP address in captured packet */
621 struct ip *ipHeader; /* The IP header inside the captured IP packet */
622 struct ip6_hdr *ipHeader6; /* The IP header inside the captured IP packet */
623 struct udphdr *udpHeader;
626 if ((encapsulationUdpData[0] & 0xf0) == 0x40) { //IPV4
628 ipHeader = (struct ip *) ARM_NOWARN_ALIGN(encapsulationUdpData);
630 dst.v4 = ipHeader->ip_dst;
632 if (ipHeader->ip_p != SOL_UDP) {
634 #ifdef INCLUDE_DEBUG_OUTPUT
635 OLSR_PRINTF(1,"%s: NON UDP PACKET\n", PLUGIN_NAME_SHORT);
640 // If we're dealing with a fragment we bail out here since there's no valid
641 // UDP header in this message
642 if (IsIpv4Fragment(ipHeader)) {
643 #ifdef INCLUDE_DEBUG_OUTPUT
644 OLSR_PRINTF(1, "%s: Is IPv4 fragment\n", PLUGIN_NAME_SHORT);
649 if (check_and_mark_recent_packet(encapsulationUdpData, nBytes))
652 udpHeader = (struct udphdr *) ARM_NOWARN_ALIGN((encapsulationUdpData +
653 GetIpHeaderLength(encapsulationUdpData)));
654 destPort = ntohs(udpHeader->dest);
656 if (!InUdpDestPortList(AF_INET, &dst, destPort)) {
657 #ifdef INCLUDE_DEBUG_OUTPUT
659 OLSR_PRINTF(1, "%s: Not in dest/port list: %s:%d\n", PLUGIN_NAME_SHORT,
660 get_ipv4_str(dst.v4.s_addr, tmp, sizeof(tmp)), destPort);
665 else if ((encapsulationUdpData[0] & 0xf0) == 0x60) { //IPv6
667 ipHeader6 = (struct ip6_hdr *) ARM_NOWARN_ALIGN(encapsulationUdpData);
669 memcpy(&dst.v6, &ipHeader6->ip6_dst, sizeof(struct in6_addr));
671 if (ipHeader6->ip6_dst.s6_addr[0] == 0xff) //Multicast
675 return; //not multicast
677 if (ipHeader6->ip6_nxt != SOL_UDP) {
679 //OLSR_PRINTF(1,"%s: NON UDP PACKET\n", PLUGIN_NAME_SHORT);
683 // Check whether this is a IPv6 fragment
684 if (IsIpv6Fragment(ipHeader6)) {
685 #ifdef INCLUDE_DEBUG_OUTPUT
686 OLSR_PRINTF(1, "%s: Is IPv6 fragment\n", PLUGIN_NAME_SHORT);
691 if (check_and_mark_recent_packet(encapsulationUdpData, nBytes))
694 udpHeader = (struct udphdr *) ARM_NOWARN_ALIGN((encapsulationUdpData + 40));
695 destPort = ntohs(udpHeader->dest);
697 if (!InUdpDestPortList(AF_INET6, &dst, destPort)) {
698 #ifdef INCLUDE_DEBUG_OUTPUT
700 OLSR_PRINTF(1, "%s: Not in dest/port list: %s:%d\n", PLUGIN_NAME_SHORT,
701 get_ipv6_str(dst.v6.s6_addr, tmp, sizeof(tmp)), destPort);
707 return; //Is not IP packet
710 // send the packet to OLSR forward mechanism
711 olsr_p2pd_gen(encapsulationUdpData, nBytes);
712 } /* P2pdPacketCaptured */
715 /* -------------------------------------------------------------------------
717 * Description: This function is registered with the OLSR scheduler and called
718 * when something is captured
723 * ------------------------------------------------------------------------- */
726 void *data __attribute__ ((unused)),
727 unsigned int flags __attribute__ ((unused)))
729 unsigned char rxBuffer[P2PD_BUFFER_SIZE];
731 struct sockaddr_ll pktAddr;
732 socklen_t addrLen = sizeof(pktAddr);
734 unsigned char *ipPacket;
736 /* Receive the captured Ethernet frame, leaving space for the BMF
737 * encapsulation header */
738 ipPacket = GetIpPacket(rxBuffer);
739 nBytes = recvfrom(skfd, ipPacket, P2PD_BUFFER_SIZE,
740 0, (struct sockaddr *)&pktAddr, &addrLen);
741 #ifdef INCLUDE_DEBUG_OUTPUT
742 OLSR_PRINTF(1, "%s: Received %d bytes\n", PLUGIN_NAME_SHORT, nBytes);
750 /* if (nBytes < 0) */
751 /* Check if the number of received bytes is large enough for an IP
752 * packet which contains at least a minimum-size IP header.
753 * Note: There is an apparent bug in the packet socket implementation in
754 * combination with VLAN interfaces. On a VLAN interface, the value returned
755 * by 'recvfrom' may (but need not) be 4 (bytes) larger than the value
756 * returned on a non-VLAN interface, for the same ethernet frame. */
757 if (nBytes < (int)sizeof(struct ip)) {
760 // "%s: captured frame too short (%d bytes) on \"%s\"\n",
761 // PLUGIN_NAME_SHORT,
768 if (pktAddr.sll_pkttype == PACKET_OUTGOING ||
769 pktAddr.sll_pkttype == PACKET_MULTICAST ||
770 pktAddr.sll_pkttype == PACKET_BROADCAST) {
771 #ifdef INCLUDE_DEBUG_OUTPUT
772 OLSR_PRINTF(1, "%s: Multicast or broadcast packet was captured.\n",
774 dump_packet(ipPacket, nBytes);
776 /* A multicast or broadcast packet was captured */
777 P2pdPacketCaptured(ipPacket, nBytes);
779 } /* if (pktAddr.sll_pkttype == ...) */
780 } /* if (skfd >= 0 && (FD_ISSET...)) */
783 /* -------------------------------------------------------------------------
784 * Function : InitP2pd
785 * Description: Initialize the P2pd plugin
786 * Input : skipThisInterface - pointer to interface to skip
790 * ------------------------------------------------------------------------- */
792 InitP2pd(struct interface *skipThisIntf)
795 // Initialize hash table for hash based duplicate IP packet check
799 //Tells OLSR to launch olsr_parser when the packets for this plugin arrive
800 //olsr_parser_add_function(&olsr_parser, PARSER_TYPE,1);
801 olsr_parser_add_function(&olsr_parser, PARSER_TYPE);
803 //Creates captures sockets and register them to the OLSR scheduler
804 CreateNonOlsrNetworkInterfaces(skipThisIntf);
809 /* -------------------------------------------------------------------------
810 * Function : CloseP2pd
811 * Description: Close the P2pd plugin and clean up
816 * ------------------------------------------------------------------------- */
820 CloseNonOlsrNetworkInterfaces();
823 /* -------------------------------------------------------------------------
824 * Function : SetP2pdTtl
825 * Description: Set the TTL for message from this plugin
826 * Input : value - parameter value to evaluate
829 * Data Used : P2pdTtl
830 * ------------------------------------------------------------------------- */
832 SetP2pdTtl(const char *value,
833 void *data __attribute__ ((unused)),
834 set_plugin_parameter_addon addon __attribute__ ((unused)))
836 assert(value != NULL);
837 P2pdTtl = atoi(value);
842 /* -------------------------------------------------------------------------
843 * Function : SetP2pdUseHashFilter
844 * Description: Set the Hash filter flag for this plug-in
845 * Input : value - parameter value to evaluate
846 * data - data associated with this parameter (unused in this app)
847 * addon - additional parameter data
850 * Data Used : P2pdUseHash
851 * ------------------------------------------------------------------------- */
853 SetP2pdUseHashFilter(const char *value,
854 void *data __attribute__ ((unused)),
855 set_plugin_parameter_addon addon __attribute__ ((unused)))
857 assert(value != NULL);
858 P2pdUseHash = atoi(value);
863 /* -------------------------------------------------------------------------
864 * Function : AddUdpDestPort
865 * Description: Set the UDP destination/port combination as an entry in the
867 * Input : value - parameter value to evaluate
869 * Return : -1 on error condition, 0 if all is ok
870 * Data Used : UdpDestPortList
871 * ------------------------------------------------------------------------- */
873 AddUdpDestPort(const char *value,
874 void *data __attribute__ ((unused)),
875 set_plugin_parameter_addon addon __attribute__ ((unused)))
877 char destAddr[INET6_ADDRSTRLEN];
880 struct UdpDestPort * new;
881 struct sockaddr_in addr4;
882 struct sockaddr_in6 addr6;
883 int ip_version = AF_INET;
886 assert(value != NULL);
888 // Retrieve the data from the argument string passed
889 memset(destAddr, 0, sizeof(destAddr));
890 num = sscanf(value, "%45s %hd", destAddr, &destPort);
892 OLSR_PRINTF(1, "%s: Invalid argument for \"UdpDestPort\"",
897 // Check whether we're dealing with an IPv4 or IPv6 address
898 // When the string contains a ':' we can assume we're dealing with IPv6
899 if (strchr(destAddr, (int)':')) {
900 ip_version = AF_INET6;
903 // Check whether the specified address was either IPv4 multicast,
904 // IPv4 broadcast or IPv6 multicast.
906 switch (ip_version) {
908 res = inet_pton(AF_INET, destAddr, &addr4.sin_addr);
909 if (!is_broadcast(addr4) && !is_multicast(addr4)) {
910 OLSR_PRINTF(1,"WARNING: IPv4 address must be multicast or broadcast... ");
914 res = inet_pton(AF_INET6, destAddr, &addr6.sin6_addr);
915 if (addr6.sin6_addr.s6_addr[0] != 0xFF) {
916 OLSR_PRINTF(1,"WARNING: IPv6 address must be multicast... ");
921 // Determine whether it is a valid IP address
923 OLSR_PRINTF(1, "Invalid address specified for \"UdpDestPort\"");
927 // Create a new entry and link it into the chain
928 new = calloc(1, sizeof(struct UdpDestPort));
930 OLSR_PRINTF(1, "%s: Out of memory", PLUGIN_NAME_SHORT);
934 new->ip_version = ip_version;
935 switch (ip_version) {
937 new->address.v4.s_addr = addr4.sin_addr.s_addr;
940 memcpy(&new->address.v6.s6_addr,
941 &addr6.sin6_addr.s6_addr,
942 sizeof(addr6.sin6_addr.s6_addr));
945 new->port = destPort;
946 new->next = UdpDestPortList;
947 UdpDestPortList = new;
949 // And then we're done
953 /* -------------------------------------------------------------------------
954 * Function : get_ipv4_str
955 * Description: Convert the specified address to an IPv4 compatible string
956 * Input : address - IPv4 address to convert to string
957 * s - string buffer to contain the resulting string
958 * maxlen - maximum length of the string buffer
960 * Return : Pointer to the string buffer containing the result
962 * ------------------------------------------------------------------------- */
964 get_ipv4_str(uint32_t address, char *s, size_t maxlen)
966 struct sockaddr_in v4;
968 v4.sin_addr.s_addr = address;
969 inet_ntop(AF_INET, &v4.sin_addr, s, maxlen);
974 /* -------------------------------------------------------------------------
975 * Function : get_ipv6_str
976 * Description: Convert the specified address to an IPv4 compatible string
977 * Input : address - IPv6 address to convert to string
978 * s - string buffer to contain the resulting string
979 * maxlen - maximum length of the string buffer
981 * Return : Pointer to the string buffer containing the result
983 * ------------------------------------------------------------------------- */
985 get_ipv6_str(unsigned char* address, char *s, size_t maxlen)
987 struct sockaddr_in6 v6;
989 memcpy(v6.sin6_addr.s6_addr, address, sizeof(v6.sin6_addr.s6_addr));
990 inet_ntop(AF_INET6, &v6.sin6_addr, s, maxlen);
995 /* -------------------------------------------------------------------------
996 * Function : is_broadcast
997 * Description: Check whether the address represents a broadcast address
998 * Input : addr - IPv4 address to check
1000 * Return : true if broadcast address, false otherwise
1002 * ------------------------------------------------------------------------- */
1004 is_broadcast(const struct sockaddr_in addr)
1006 if (addr.sin_addr.s_addr == 0xFFFFFFFF)
1012 /* -------------------------------------------------------------------------
1013 * Function : is_multicast
1014 * Description: Check whether the address represents a multicast address
1015 * Input : addr - IPv4 address to check
1017 * Return : true if broadcast address, false otherwise
1019 * ------------------------------------------------------------------------- */
1021 is_multicast(const struct sockaddr_in addr)
1023 if ((htonl(addr.sin_addr.s_addr) & 0xE0000000) == 0xE0000000)
1029 #ifdef INCLUDE_DEBUG_OUTPUT
1030 /* -------------------------------------------------------------------------
1031 * Function : dump_packet
1032 * Description: Dump the specified data as hex output
1033 * Input : packet - packet to dump to output
1034 * length - length of the data in the packet
1038 * ------------------------------------------------------------------------- */
1040 dump_packet(unsigned char* packet, int length)
1044 OLSR_PRINTF(1, "%s: ", PLUGIN_NAME_SHORT);
1045 for (idx = 0; idx < length; idx++) {
1046 if (idx > 0 && ((idx % 16) == 0))
1047 OLSR_PRINTF(1, "\n%s: ", PLUGIN_NAME_SHORT);
1048 OLSR_PRINTF(1, "%2.2X ", packet[idx]);
1050 OLSR_PRINTF(1, "\n");
1054 /* -------------------------------------------------------------------------
1055 * Function : check_and_mark_recent_packet
1056 * Description: Wrapper function for the Hash based duplicate check
1057 * Input : data - pointer to a packet of data to be checked
1059 * Return : true if duplicate packet, false otherwise
1060 * Data Used : P2pdUseHash
1061 * ------------------------------------------------------------------------- */
1063 check_and_mark_recent_packet(unsigned char *data,
1064 int len __attribute__ ((unused)))
1066 unsigned char * ipPacket;
1067 uint16_t ipPacketLen;
1070 /* If we don't use this filter bail out here */
1074 /* Clean up the hash table each time before we check it */
1075 PrunePacketHistory(NULL);
1077 /* Check for duplicate IP packets now based on a hash */
1078 ipPacket = GetIpPacket(data);
1079 ipPacketLen = GetIpTotalLength(ipPacket);
1081 /* Calculate packet fingerprint */
1082 crc32 = PacketCrc32(ipPacket, ipPacketLen);
1084 /* Check if this packet was seen recently */
1085 if (CheckAndMarkRecentPacket(crc32))
1089 "%s: --> discarding: packet is duplicate\n",