From a8289044288f4db6c92ef316427e984b87d96e16 Mon Sep 17 00:00:00 2001 From: Moritz Warning Date: Sat, 6 Jun 2020 18:27:48 +0200 Subject: [PATCH 1/2] unify code style done with Clang-Format 1.9.0, four space identation and BreakBeforeBraces set to Linux Signed-off-by: Moritz Warning --- aodv_hello.c | 290 +++++++------- aodv_hello.h | 22 +- aodv_neighbor.c | 230 ++++++------ aodv_neighbor.h | 16 +- aodv_rerr.c | 241 ++++++------ aodv_rerr.h | 29 +- aodv_rrep.c | 417 ++++++++++----------- aodv_rrep.h | 61 ++- aodv_rreq.c | 547 ++++++++++++++------------- aodv_rreq.h | 66 ++-- aodv_socket.c | 513 +++++++++++++------------ aodv_socket.h | 28 +- aodv_timeout.c | 332 ++++++++-------- aodv_timeout.h | 10 +- debug.c | 363 +++++++++--------- debug.h | 16 +- defs.h | 90 ++--- endian.c | 11 +- list.c | 18 +- list.h | 36 +- llf.c | 390 +++++++++---------- llf.h | 2 +- lnx/kaodv-debug.c | 22 +- lnx/kaodv-debug.h | 35 +- lnx/kaodv-expl.c | 489 ++++++++++++------------ lnx/kaodv-expl.h | 20 +- lnx/kaodv-ipenc.c | 66 ++-- lnx/kaodv-ipenc.h | 20 +- lnx/kaodv-mod.c | 530 +++++++++++++------------- lnx/kaodv-mod.h | 147 ++++---- lnx/kaodv-netlink.c | 434 ++++++++++----------- lnx/kaodv-netlink.h | 112 +++--- lnx/kaodv-queue.c | 425 +++++++++++---------- lnx/kaodv-queue.h | 4 +- lnx/kaodv.h | 12 +- locality.c | 66 ++-- locality.h | 10 +- main.c | 687 +++++++++++++++++----------------- nl.c | 895 ++++++++++++++++++++++---------------------- nl.h | 9 +- params.h | 60 +-- routing_table.c | 871 +++++++++++++++++++++--------------------- routing_table.h | 83 ++-- seek_list.c | 51 +-- seek_list.h | 22 +- timer_queue.c | 121 +++--- timer_queue.h | 30 +- 47 files changed, 4436 insertions(+), 4513 deletions(-) diff --git a/aodv_hello.c b/aodv_hello.c index 75d2234..c6ecf5c 100644 --- a/aodv_hello.c +++ b/aodv_hello.c @@ -16,25 +16,25 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, - * + * Authors: Erik Nordström, + * * *****************************************************************************/ #ifdef NS_PORT #include "ns-2/aodv-uu.h" #else -#include #include "aodv_hello.h" -#include "aodv_timeout.h" #include "aodv_rrep.h" #include "aodv_rreq.h" -#include "routing_table.h" -#include "timer_queue.h" -#include "params.h" #include "aodv_socket.h" -#include "defs.h" +#include "aodv_timeout.h" #include "debug.h" +#include "defs.h" +#include "params.h" +#include "routing_table.h" +#include "timer_queue.h" +#include extern int unidir_hack, receive_n_hellos, hello_jittering, optimized_hellos; static struct timer hello_timer; @@ -43,24 +43,23 @@ static struct timer hello_timer; /* #define DEBUG_HELLO */ - long NS_CLASS hello_jitter() { if (hello_jittering) { #ifdef NS_PORT - return (long) (((float) Random::integer(RAND_MAX + 1) / RAND_MAX - 0.5) - * JITTER_INTERVAL); + return (long)(((float)Random::integer(RAND_MAX + 1) / RAND_MAX - 0.5) * + JITTER_INTERVAL); #else - return (long) (((float) random() / RAND_MAX - 0.5) * JITTER_INTERVAL); + return (long)(((float)random() / RAND_MAX - 0.5) * JITTER_INTERVAL); #endif } else - return 0; + return 0; } void NS_CLASS hello_start() { if (hello_timer.used) - return; + return; gettimeofday(&this_host.fwd_time, NULL); @@ -73,7 +72,7 @@ void NS_CLASS hello_start() void NS_CLASS hello_stop() { DEBUG(LOG_DEBUG, 0, - "No active forwarding routes - stopped sending HELLOs!"); + "No active forwarding routes - stopped sending HELLOs!"); timer_remove(&hello_timer); } @@ -91,9 +90,9 @@ void NS_CLASS hello_send(void *arg) gettimeofday(&now, NULL); if (optimized_hellos && - timeval_diff(&now, &this_host.fwd_time) > ACTIVE_ROUTE_TIMEOUT) { - hello_stop(); - return; + timeval_diff(&now, &this_host.fwd_time) > ACTIVE_ROUTE_TIMEOUT) { + hello_stop(); + return; } time_diff = timeval_diff(&now, &this_host.bcast_time); @@ -103,69 +102,68 @@ void NS_CLASS hello_send(void *arg) we have sent other bcast msgs within HELLO_INTERVAL */ if (time_diff >= HELLO_INTERVAL) { - for (i = 0; i < MAX_NR_INTERFACES; i++) { - if (!DEV_NR(i).enabled) - continue; + for (i = 0; i < MAX_NR_INTERFACES; i++) { + if (!DEV_NR(i).enabled) + continue; #ifdef DEBUG_HELLO - DEBUG(LOG_DEBUG, 0, "sending Hello to 255.255.255.255"); + DEBUG(LOG_DEBUG, 0, "sending Hello to 255.255.255.255"); #endif - rrep = rrep_create(flags, 0, 0, DEV_NR(i).ipaddr, - this_host.seqno, - DEV_NR(i).ipaddr, - ALLOWED_HELLO_LOSS * HELLO_INTERVAL); - - /* Assemble a RREP extension which contain our neighbor set... */ - if (unidir_hack) { - int i; - - if (ext) - ext = AODV_EXT_NEXT(ext); - else - ext = (AODV_ext *) ((char *) rrep + RREP_SIZE); - - ext->type = RREP_HELLO_NEIGHBOR_SET_EXT; - ext->length = 0; - - for (i = 0; i < RT_TABLESIZE; i++) { - list_t *pos; - list_foreach(pos, &rt_tbl.tbl[i]) { - rt_table_t *rt = (rt_table_t *) pos; - /* If an entry has an active hello timer, we assume - that we are receiving hello messages from that - node... */ - if (rt->hello_timer.used) { + rrep = rrep_create(flags, 0, 0, DEV_NR(i).ipaddr, this_host.seqno, + DEV_NR(i).ipaddr, + ALLOWED_HELLO_LOSS * HELLO_INTERVAL); + + /* Assemble a RREP extension which contain our neighbor set... */ + if (unidir_hack) { + int i; + + if (ext) + ext = AODV_EXT_NEXT(ext); + else + ext = (AODV_ext *)((char *)rrep + RREP_SIZE); + + ext->type = RREP_HELLO_NEIGHBOR_SET_EXT; + ext->length = 0; + + for (i = 0; i < RT_TABLESIZE; i++) { + list_t *pos; + list_foreach(pos, &rt_tbl.tbl[i]) + { + rt_table_t *rt = (rt_table_t *)pos; + /* If an entry has an active hello timer, we assume + that we are receiving hello messages from that + node... */ + if (rt->hello_timer.used) { #ifdef DEBUG_HELLO - DEBUG(LOG_INFO, 0, - "Adding %s to hello neighbor set ext", - ip_to_str(rt->dest_addr)); + DEBUG(LOG_INFO, 0, + "Adding %s to hello neighbor set ext", + ip_to_str(rt->dest_addr)); #endif - memcpy(AODV_EXT_DATA(ext), &rt->dest_addr, - sizeof(struct in_addr)); - ext->length += sizeof(struct in_addr); - } - } - } - if (ext->length) - msg_size = RREP_SIZE + AODV_EXT_SIZE(ext); - } - dest.s_addr = AODV_BROADCAST; - aodv_socket_send((AODV_msg *) rrep, dest, msg_size, 1, &DEV_NR(i)); - } - - timer_set_timeout(&hello_timer, HELLO_INTERVAL + jitter); + memcpy(AODV_EXT_DATA(ext), &rt->dest_addr, + sizeof(struct in_addr)); + ext->length += sizeof(struct in_addr); + } + } + } + if (ext->length) + msg_size = RREP_SIZE + AODV_EXT_SIZE(ext); + } + dest.s_addr = AODV_BROADCAST; + aodv_socket_send((AODV_msg *)rrep, dest, msg_size, 1, &DEV_NR(i)); + } + + timer_set_timeout(&hello_timer, HELLO_INTERVAL + jitter); } else { - if (HELLO_INTERVAL - time_diff + jitter < 0) - timer_set_timeout(&hello_timer, - HELLO_INTERVAL - time_diff - jitter); - else - timer_set_timeout(&hello_timer, - HELLO_INTERVAL - time_diff + jitter); + if (HELLO_INTERVAL - time_diff + jitter < 0) + timer_set_timeout(&hello_timer, + HELLO_INTERVAL - time_diff - jitter); + else + timer_set_timeout(&hello_timer, + HELLO_INTERVAL - time_diff + jitter); } } - /* Process a hello message */ -void NS_CLASS hello_process(RREP * hello, int rreplen, unsigned int ifindex) +void NS_CLASS hello_process(RREP *hello, int rreplen, unsigned int ifindex) { u_int32_t hello_seqno, timeout, hello_interval = HELLO_INTERVAL; u_int8_t state, flags = 0; @@ -183,115 +181,115 @@ void NS_CLASS hello_process(RREP * hello, int rreplen, unsigned int ifindex) rt = rt_table_find(hello_dest); if (rt) - flags = rt->flags; + flags = rt->flags; if (unidir_hack) - flags |= RT_UNIDIR; + flags |= RT_UNIDIR; /* Check for hello interval extension: */ - ext = (AODV_ext *) ((char *) hello + RREP_SIZE); - - while (rreplen > (int) RREP_SIZE) { - switch (ext->type) { - case RREP_HELLO_INTERVAL_EXT: - if (ext->length == 4) { - memcpy(&hello_interval, AODV_EXT_DATA(ext), 4); - hello_interval = ntohl(hello_interval); + ext = (AODV_ext *)((char *)hello + RREP_SIZE); + + while (rreplen > (int)RREP_SIZE) { + switch (ext->type) { + case RREP_HELLO_INTERVAL_EXT: + if (ext->length == 4) { + memcpy(&hello_interval, AODV_EXT_DATA(ext), 4); + hello_interval = ntohl(hello_interval); #ifdef DEBUG_HELLO - DEBUG(LOG_INFO, 0, "Hello extension interval=%lu!", - hello_interval); + DEBUG(LOG_INFO, 0, "Hello extension interval=%lu!", + hello_interval); #endif - } else - alog(LOG_WARNING, 0, - __FUNCTION__, "Bad hello interval extension!"); - break; - case RREP_HELLO_NEIGHBOR_SET_EXT: + } else + alog(LOG_WARNING, 0, __FUNCTION__, + "Bad hello interval extension!"); + break; + case RREP_HELLO_NEIGHBOR_SET_EXT: #ifdef DEBUG_HELLO - DEBUG(LOG_INFO, 0, "RREP_HELLO_NEIGHBOR_SET_EXT"); + DEBUG(LOG_INFO, 0, "RREP_HELLO_NEIGHBOR_SET_EXT"); #endif - for (i = 0; i < ext->length; i = i + 4) { - ext_neighbor.s_addr = - *(in_addr_t *) ((char *) AODV_EXT_DATA(ext) + i); - - if (ext_neighbor.s_addr == DEV_IFINDEX(ifindex).ipaddr.s_addr) - flags &= ~RT_UNIDIR; - } - break; - default: - alog(LOG_WARNING, 0, __FUNCTION__, - "Bad extension!! type=%d, length=%d", ext->type, ext->length); - ext = NULL; - break; - } - if (ext == NULL) - break; - - rreplen -= AODV_EXT_SIZE(ext); - ext = AODV_EXT_NEXT(ext); + for (i = 0; i < ext->length; i = i + 4) { + ext_neighbor.s_addr = + *(in_addr_t *)((char *)AODV_EXT_DATA(ext) + i); + + if (ext_neighbor.s_addr == DEV_IFINDEX(ifindex).ipaddr.s_addr) + flags &= ~RT_UNIDIR; + } + break; + default: + alog(LOG_WARNING, 0, __FUNCTION__, + "Bad extension!! type=%d, length=%d", ext->type, ext->length); + ext = NULL; + break; + } + if (ext == NULL) + break; + + rreplen -= AODV_EXT_SIZE(ext); + ext = AODV_EXT_NEXT(ext); } #ifdef DEBUG_HELLO - DEBUG(LOG_DEBUG, 0, "rcvd HELLO from %s, seqno %lu", - ip_to_str(hello_dest), hello_seqno); + DEBUG(LOG_DEBUG, 0, "rcvd HELLO from %s, seqno %lu", ip_to_str(hello_dest), + hello_seqno); #endif /* This neighbor should only be valid after receiving 3 consecutive hello messages... */ if (receive_n_hellos) - state = INVALID; + state = INVALID; else - state = VALID; + state = VALID; timeout = ALLOWED_HELLO_LOSS * hello_interval + ROUTE_TIMEOUT_SLACK; if (!rt) { - /* No active or expired route in the routing table. So we add a - new entry... */ + /* No active or expired route in the routing table. So we add a + new entry... */ - rt = rt_table_insert(hello_dest, hello_dest, 1, - hello_seqno, timeout, state, flags, ifindex); + rt = rt_table_insert(hello_dest, hello_dest, 1, hello_seqno, timeout, + state, flags, ifindex); - if (flags & RT_UNIDIR) { - DEBUG(LOG_INFO, 0, "%s new NEIGHBOR, link UNI-DIR", - ip_to_str(rt->dest_addr)); - } else { - DEBUG(LOG_INFO, 0, "%s new NEIGHBOR!", ip_to_str(rt->dest_addr)); - } - rt->hello_cnt = 1; + if (flags & RT_UNIDIR) { + DEBUG(LOG_INFO, 0, "%s new NEIGHBOR, link UNI-DIR", + ip_to_str(rt->dest_addr)); + } else { + DEBUG(LOG_INFO, 0, "%s new NEIGHBOR!", ip_to_str(rt->dest_addr)); + } + rt->hello_cnt = 1; } else { - if ((flags & RT_UNIDIR) && rt->state == VALID && rt->hcnt > 1) { - goto hello_update; - } - - if (receive_n_hellos && rt->hello_cnt < (receive_n_hellos - 1)) { - if (timeval_diff(&now, &rt->last_hello_time) < - (long) (hello_interval + hello_interval / 2)) - rt->hello_cnt++; - else - rt->hello_cnt = 1; - - memcpy(&rt->last_hello_time, &now, sizeof(struct timeval)); - return; - } - rt_table_update(rt, hello_dest, 1, hello_seqno, timeout, VALID, flags); + if ((flags & RT_UNIDIR) && rt->state == VALID && rt->hcnt > 1) { + goto hello_update; + } + + if (receive_n_hellos && rt->hello_cnt < (receive_n_hellos - 1)) { + if (timeval_diff(&now, &rt->last_hello_time) < + (long)(hello_interval + hello_interval / 2)) + rt->hello_cnt++; + else + rt->hello_cnt = 1; + + memcpy(&rt->last_hello_time, &now, sizeof(struct timeval)); + return; + } + rt_table_update(rt, hello_dest, 1, hello_seqno, timeout, VALID, flags); } - hello_update: +hello_update: hello_update_timeout(rt, &now, ALLOWED_HELLO_LOSS * hello_interval); return; } +#define HELLO_DELAY \ + 50 /* The extra time we should allow an hello \ + message to take (due to processing) before \ + assuming lost . */ -#define HELLO_DELAY 50 /* The extra time we should allow an hello - message to take (due to processing) before - assuming lost . */ - -NS_INLINE void NS_CLASS hello_update_timeout(rt_table_t * rt, - struct timeval *now, long time) +NS_INLINE void NS_CLASS hello_update_timeout(rt_table_t *rt, + struct timeval *now, long time) { timer_set_timeout(&rt->hello_timer, time + HELLO_DELAY); memcpy(&rt->last_hello_time, now, sizeof(struct timeval)); diff --git a/aodv_hello.h b/aodv_hello.h index cfd1676..8a4fcf9 100644 --- a/aodv_hello.h +++ b/aodv_hello.h @@ -16,18 +16,18 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, - * + * Authors: Erik Nordström, + * * *****************************************************************************/ #ifndef _AODV_HELLO_H #define _AODV_HELLO_H #ifndef NS_NO_GLOBALS -#include "defs.h" #include "aodv_rrep.h" +#include "defs.h" #include "routing_table.h" -#endif /* NS_NO_GLOBALS */ +#endif /* NS_NO_GLOBALS */ #ifndef NS_NO_DECLARATIONS @@ -37,15 +37,15 @@ void hello_start(); void hello_stop(); void hello_send(void *arg); -void hello_process(RREP * hello, int rreplen, unsigned int ifindex); -void hello_process_non_hello(AODV_msg * aodv_msg, struct in_addr source, - unsigned int ifindex); -NS_INLINE void hello_update_timeout(rt_table_t * rt, struct timeval *now, - long time); +void hello_process(RREP *hello, int rreplen, unsigned int ifindex); +void hello_process_non_hello(AODV_msg *aodv_msg, struct in_addr source, + unsigned int ifindex); +NS_INLINE void hello_update_timeout(rt_table_t *rt, struct timeval *now, + long time); #ifdef NS_PORT long hello_jitter(); #endif -#endif /* NS_NO_DECLARATIONS */ +#endif /* NS_NO_DECLARATIONS */ -#endif /* AODV_HELLO_H */ +#endif /* AODV_HELLO_H */ diff --git a/aodv_neighbor.c b/aodv_neighbor.c index 219363d..d21a726 100644 --- a/aodv_neighbor.c +++ b/aodv_neighbor.c @@ -16,29 +16,28 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, + * Authors: Erik Nordström, * *****************************************************************************/ #ifdef NS_PORT #include "ns-2/aodv-uu.h" #else +#include "aodv_hello.h" #include "aodv_neighbor.h" #include "aodv_rerr.h" -#include "aodv_hello.h" #include "aodv_socket.h" -#include "routing_table.h" -#include "params.h" -#include "defs.h" #include "debug.h" +#include "defs.h" +#include "params.h" +#include "routing_table.h" extern int llfeedback; -#endif /* NS_PORT */ - +#endif /* NS_PORT */ /* Add/Update neighbor from a non HELLO AODV control message... */ -void NS_CLASS neighbor_add(AODV_msg * aodv_msg, struct in_addr source, - unsigned int ifindex) +void NS_CLASS neighbor_add(AODV_msg *aodv_msg, struct in_addr source, + unsigned int ifindex) { struct timeval now; rt_table_t *rt = NULL; @@ -49,28 +48,28 @@ void NS_CLASS neighbor_add(AODV_msg * aodv_msg, struct in_addr source, rt = rt_table_find(source); if (!rt) { - DEBUG(LOG_DEBUG, 0, "%s new NEIGHBOR!", ip_to_str(source)); - rt = rt_table_insert(source, source, 1, 0, - ACTIVE_ROUTE_TIMEOUT, VALID, 0, ifindex); + DEBUG(LOG_DEBUG, 0, "%s new NEIGHBOR!", ip_to_str(source)); + rt = rt_table_insert(source, source, 1, 0, ACTIVE_ROUTE_TIMEOUT, VALID, + 0, ifindex); } else { - /* Don't update anything if this is a uni-directional link... */ - if (rt->flags & RT_UNIDIR) - return; + /* Don't update anything if this is a uni-directional link... */ + if (rt->flags & RT_UNIDIR) + return; - if (rt->dest_seqno != 0) - seqno = rt->dest_seqno; + if (rt->dest_seqno != 0) + seqno = rt->dest_seqno; - rt_table_update(rt, source, 1, seqno, ACTIVE_ROUTE_TIMEOUT, - VALID, rt->flags); + rt_table_update(rt, source, 1, seqno, ACTIVE_ROUTE_TIMEOUT, VALID, + rt->flags); } if (!llfeedback && rt->hello_timer.used) - hello_update_timeout(rt, &now, ALLOWED_HELLO_LOSS * HELLO_INTERVAL); + hello_update_timeout(rt, &now, ALLOWED_HELLO_LOSS * HELLO_INTERVAL); return; } -void NS_CLASS neighbor_link_break(rt_table_t * rt) +void NS_CLASS neighbor_link_break(rt_table_t *rt) { /* If hopcount = 1, this is a direct neighbor and a link break has occured. Send a RERR with the incremented sequence number */ @@ -82,12 +81,12 @@ void NS_CLASS neighbor_link_break(rt_table_t * rt) rerr_unicast_dest.s_addr = 0; if (!rt) - return; + return; if (rt->hcnt != 1) { - DEBUG(LOG_DEBUG, 0, "%s is not a neighbor, hcnt=%d!!!", - ip_to_str(rt->dest_addr), rt->hcnt); - return; + DEBUG(LOG_DEBUG, 0, "%s is not a neighbor, hcnt=%d!!!", + ip_to_str(rt->dest_addr), rt->hcnt); + return; } DEBUG(LOG_DEBUG, 0, "Link %s down!", ip_to_str(rt->dest_addr)); @@ -97,109 +96,110 @@ void NS_CLASS neighbor_link_break(rt_table_t * rt) /* Create a route error msg, unless the route is to be repaired */ if (rt->nprec && !(rt->flags & RT_REPAIR)) { - rerr = rerr_create(0, rt->dest_addr, rt->dest_seqno); - DEBUG(LOG_DEBUG, 0, "Added %s as unreachable, seqno=%lu", - ip_to_str(rt->dest_addr), rt->dest_seqno); + rerr = rerr_create(0, rt->dest_addr, rt->dest_seqno); + DEBUG(LOG_DEBUG, 0, "Added %s as unreachable, seqno=%lu", + ip_to_str(rt->dest_addr), rt->dest_seqno); - if (rt->nprec == 1) - rerr_unicast_dest = FIRST_PREC(rt->precursors)->neighbor; + if (rt->nprec == 1) + rerr_unicast_dest = FIRST_PREC(rt->precursors)->neighbor; } /* Purge precursor list: */ if (!(rt->flags & RT_REPAIR)) - precursor_list_destroy(rt); + precursor_list_destroy(rt); /* Check the routing table for entries which have the unreachable destination (dest) as next hop. These entries (destinations) cannot be reached either since dest is down. They should therefore also be included in the RERR. */ for (i = 0; i < RT_TABLESIZE; i++) { - list_t *pos; - list_foreach(pos, &rt_tbl.tbl[i]) { - rt_table_t *rt_u = (rt_table_t *) pos; - - if (rt_u->state == VALID && - rt_u->next_hop.s_addr == rt->dest_addr.s_addr && - rt_u->dest_addr.s_addr != rt->dest_addr.s_addr) { - - /* If the link that broke are marked for repair, - then do the same for all additional unreachable - destinations. */ - - if ((rt->flags & RT_REPAIR) && rt_u->hcnt <= MAX_REPAIR_TTL) { - - rt_u->flags |= RT_REPAIR; - DEBUG(LOG_DEBUG, 0, "Marking %s for REPAIR", - ip_to_str(rt_u->dest_addr)); - - rt_table_invalidate(rt_u); - continue; - } - - rt_table_invalidate(rt_u); - - if (rt_u->nprec) { - - if (!rerr) { - rerr = - rerr_create(0, rt_u->dest_addr, rt_u->dest_seqno); - - if (rt_u->nprec == 1) - rerr_unicast_dest = - FIRST_PREC(rt_u->precursors)->neighbor; - - DEBUG(LOG_DEBUG, 0, - "Added %s as unreachable, seqno=%lu", - ip_to_str(rt_u->dest_addr), rt_u->dest_seqno); - } else { - /* Decide whether new precursors make this a non unicast - RERR */ - rerr_add_udest(rerr, rt_u->dest_addr, rt_u->dest_seqno); - - if (rerr_unicast_dest.s_addr) { - list_t *pos2; - list_foreach(pos2, &rt_u->precursors) { - precursor_t *pr = (precursor_t *) pos2; - if (pr->neighbor.s_addr != - rerr_unicast_dest.s_addr) { - rerr_unicast_dest.s_addr = 0; - break; - } - } - } - DEBUG(LOG_DEBUG, 0, - "Added %s as unreachable, seqno=%lu", - ip_to_str(rt_u->dest_addr), rt_u->dest_seqno); - } - } - precursor_list_destroy(rt_u); - } - } + list_t *pos; + list_foreach(pos, &rt_tbl.tbl[i]) + { + rt_table_t *rt_u = (rt_table_t *)pos; + + if (rt_u->state == VALID && + rt_u->next_hop.s_addr == rt->dest_addr.s_addr && + rt_u->dest_addr.s_addr != rt->dest_addr.s_addr) { + + /* If the link that broke are marked for repair, + then do the same for all additional unreachable + destinations. */ + + if ((rt->flags & RT_REPAIR) && rt_u->hcnt <= MAX_REPAIR_TTL) { + + rt_u->flags |= RT_REPAIR; + DEBUG(LOG_DEBUG, 0, "Marking %s for REPAIR", + ip_to_str(rt_u->dest_addr)); + + rt_table_invalidate(rt_u); + continue; + } + + rt_table_invalidate(rt_u); + + if (rt_u->nprec) { + + if (!rerr) { + rerr = + rerr_create(0, rt_u->dest_addr, rt_u->dest_seqno); + + if (rt_u->nprec == 1) + rerr_unicast_dest = + FIRST_PREC(rt_u->precursors)->neighbor; + + DEBUG(LOG_DEBUG, 0, + "Added %s as unreachable, seqno=%lu", + ip_to_str(rt_u->dest_addr), rt_u->dest_seqno); + } else { + /* Decide whether new precursors make this a non unicast + RERR */ + rerr_add_udest(rerr, rt_u->dest_addr, rt_u->dest_seqno); + + if (rerr_unicast_dest.s_addr) { + list_t *pos2; + list_foreach(pos2, &rt_u->precursors) + { + precursor_t *pr = (precursor_t *)pos2; + if (pr->neighbor.s_addr != + rerr_unicast_dest.s_addr) { + rerr_unicast_dest.s_addr = 0; + break; + } + } + } + DEBUG(LOG_DEBUG, 0, + "Added %s as unreachable, seqno=%lu", + ip_to_str(rt_u->dest_addr), rt_u->dest_seqno); + } + } + precursor_list_destroy(rt_u); + } + } } if (rerr) { - DEBUG(LOG_DEBUG, 0, "RERR created, %d bytes.", RERR_CALC_SIZE(rerr)); - - rt_u = rt_table_find(rerr_unicast_dest); - - if (rt_u && rerr->dest_count == 1 && rerr_unicast_dest.s_addr) - aodv_socket_send((AODV_msg *) rerr, - rerr_unicast_dest, - RERR_CALC_SIZE(rerr), 1, - &DEV_IFINDEX(rt_u->ifindex)); - - else if (rerr->dest_count > 0) { - /* FIXME: Should only transmit RERR on those interfaces - * which have precursor nodes for the broken route */ - for (i = 0; i < MAX_NR_INTERFACES; i++) { - struct in_addr dest; - - if (!DEV_NR(i).enabled) - continue; - dest.s_addr = AODV_BROADCAST; - aodv_socket_send((AODV_msg *) rerr, dest, - RERR_CALC_SIZE(rerr), 1, &DEV_NR(i)); - } - } + DEBUG(LOG_DEBUG, 0, "RERR created, %d bytes.", RERR_CALC_SIZE(rerr)); + + rt_u = rt_table_find(rerr_unicast_dest); + + if (rt_u && rerr->dest_count == 1 && rerr_unicast_dest.s_addr) + aodv_socket_send((AODV_msg *)rerr, rerr_unicast_dest, + RERR_CALC_SIZE(rerr), 1, + &DEV_IFINDEX(rt_u->ifindex)); + + else if (rerr->dest_count > 0) { + /* FIXME: Should only transmit RERR on those interfaces + * which have precursor nodes for the broken route */ + for (i = 0; i < MAX_NR_INTERFACES; i++) { + struct in_addr dest; + + if (!DEV_NR(i).enabled) + continue; + dest.s_addr = AODV_BROADCAST; + aodv_socket_send((AODV_msg *)rerr, dest, RERR_CALC_SIZE(rerr), + 1, &DEV_NR(i)); + } + } } } diff --git a/aodv_neighbor.h b/aodv_neighbor.h index d6b7e3c..60ad417 100644 --- a/aodv_neighbor.h +++ b/aodv_neighbor.h @@ -16,8 +16,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, - * + * Authors: Erik Nordström, + * * *****************************************************************************/ #ifndef _AODV_NEIGHBOR_H @@ -26,14 +26,14 @@ #ifndef NS_NO_GLOBALS #include "defs.h" #include "routing_table.h" -#endif /* NS_NO_GLOBALS */ +#endif /* NS_NO_GLOBALS */ #ifndef NS_NO_DECLARATIONS -void neighbor_add(AODV_msg * aodv_msg, struct in_addr source, - unsigned int ifindex); -void neighbor_link_break(rt_table_t * rt); +void neighbor_add(AODV_msg *aodv_msg, struct in_addr source, + unsigned int ifindex); +void neighbor_link_break(rt_table_t *rt); -#endif /* NS_NO_DECLARATIONS */ +#endif /* NS_NO_DECLARATIONS */ -#endif /* AODV_NEIGHBOR_H */ +#endif /* AODV_NEIGHBOR_H */ diff --git a/aodv_rerr.c b/aodv_rerr.c index 4128387..ef2f114 100644 --- a/aodv_rerr.c +++ b/aodv_rerr.c @@ -16,34 +16,34 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, - * + * Authors: Erik Nordström, + * * *****************************************************************************/ #ifdef NS_PORT #include "ns-2/aodv-uu.h" #else -#include #include "aodv_rerr.h" -#include "routing_table.h" #include "aodv_socket.h" #include "aodv_timeout.h" -#include "defs.h" #include "debug.h" +#include "defs.h" #include "params.h" +#include "routing_table.h" +#include #endif RERR *NS_CLASS rerr_create(u_int8_t flags, struct in_addr dest_addr, - u_int32_t dest_seqno) + u_int32_t dest_seqno) { RERR *rerr; DEBUG(LOG_DEBUG, 0, "Assembling RERR about %s seqno=%d", - ip_to_str(dest_addr), dest_seqno); + ip_to_str(dest_addr), dest_seqno); - rerr = (RERR *) aodv_socket_new_msg(); + rerr = (RERR *)aodv_socket_new_msg(); rerr->type = AODV_RERR; rerr->n = (flags & RERR_NODELETE ? 1 : 0); rerr->res1 = 0; @@ -55,20 +55,19 @@ RERR *NS_CLASS rerr_create(u_int8_t flags, struct in_addr dest_addr, return rerr; } -void NS_CLASS rerr_add_udest(RERR * rerr, struct in_addr udest, - u_int32_t udest_seqno) +void NS_CLASS rerr_add_udest(RERR *rerr, struct in_addr udest, + u_int32_t udest_seqno) { RERR_udest *ud; - ud = (RERR_udest *) ((char *) rerr + RERR_CALC_SIZE(rerr)); + ud = (RERR_udest *)((char *)rerr + RERR_CALC_SIZE(rerr)); ud->dest_addr = udest.s_addr; ud->dest_seqno = htonl(udest_seqno); rerr->dest_count++; } - -void NS_CLASS rerr_process(RERR * rerr, int rerrlen, struct in_addr ip_src, - struct in_addr ip_dst) +void NS_CLASS rerr_process(RERR *rerr, int rerrlen, struct in_addr ip_src, + struct in_addr ip_dst) { RERR *new_rerr = NULL; RERR_udest *udest; @@ -81,15 +80,15 @@ void NS_CLASS rerr_process(RERR * rerr, int rerrlen, struct in_addr ip_src, DEBUG(LOG_DEBUG, 0, "ip_src=%s", ip_to_str(ip_src)); - log_pkt_fields((AODV_msg *) rerr); + log_pkt_fields((AODV_msg *)rerr); - if (rerrlen < ((int) RERR_CALC_SIZE(rerr))) { - alog(LOG_WARNING, 0, __FUNCTION__, - "IP data too short (%u bytes) from %s to %s. Should be %d bytes.", - rerrlen, ip_to_str(ip_src), ip_to_str(ip_dst), - RERR_CALC_SIZE(rerr)); + if (rerrlen < ((int)RERR_CALC_SIZE(rerr))) { + alog(LOG_WARNING, 0, __FUNCTION__, + "IP data too short (%u bytes) from %s to %s. Should be %d bytes.", + rerrlen, ip_to_str(ip_src), ip_to_str(ip_dst), + RERR_CALC_SIZE(rerr)); - return; + return; } /* Check which destinations that are unreachable. */ @@ -97,114 +96,116 @@ void NS_CLASS rerr_process(RERR * rerr, int rerrlen, struct in_addr ip_src, while (rerr->dest_count) { - udest_addr.s_addr = udest->dest_addr; - rerr_dest_seqno = ntohl(udest->dest_seqno); - DEBUG(LOG_DEBUG, 0, "unreachable dest=%s seqno=%lu", - ip_to_str(udest_addr), rerr_dest_seqno); + udest_addr.s_addr = udest->dest_addr; + rerr_dest_seqno = ntohl(udest->dest_seqno); + DEBUG(LOG_DEBUG, 0, "unreachable dest=%s seqno=%lu", + ip_to_str(udest_addr), rerr_dest_seqno); - rt = rt_table_find(udest_addr); + rt = rt_table_find(udest_addr); - if (rt && rt->state == VALID && rt->next_hop.s_addr == ip_src.s_addr) { + if (rt && rt->state == VALID && rt->next_hop.s_addr == ip_src.s_addr) { - /* Checking sequence numbers here is an out of draft - * addition to AODV-UU. It is here because it makes a lot - * of sense... */ - if (0 && (int32_t) rt->dest_seqno > (int32_t) rerr_dest_seqno) { - DEBUG(LOG_DEBUG, 0, "Udest ignored because of seqno"); - udest = RERR_UDEST_NEXT(udest); - rerr->dest_count--; - continue; - } - DEBUG(LOG_DEBUG, 0, "removing rte %s - WAS IN RERR!!", - ip_to_str(udest_addr)); + /* Checking sequence numbers here is an out of draft + * addition to AODV-UU. It is here because it makes a lot + * of sense... */ + if (0 && (int32_t)rt->dest_seqno > (int32_t)rerr_dest_seqno) { + DEBUG(LOG_DEBUG, 0, "Udest ignored because of seqno"); + udest = RERR_UDEST_NEXT(udest); + rerr->dest_count--; + continue; + } + DEBUG(LOG_DEBUG, 0, "removing rte %s - WAS IN RERR!!", + ip_to_str(udest_addr)); #ifdef NS_PORT - interfaceQueue((nsaddr_t) udest_addr.s_addr, IFQ_DROP_BY_DEST); + interfaceQueue((nsaddr_t)udest_addr.s_addr, IFQ_DROP_BY_DEST); #endif - /* Invalidate route: */ - if (!rerr->n) { - rt_table_invalidate(rt); - } - /* (a) updates the corresponding destination sequence number - with the Destination Sequence Number in the packet, and */ - rt->dest_seqno = rerr_dest_seqno; - - /* (d) check precursor list for emptiness. If not empty, include - the destination as an unreachable destination in the - RERR... */ - if (rt->nprec && !(rt->flags & RT_REPAIR)) { - - if (!new_rerr) { - u_int8_t flags = 0; - - if (rerr->n) - flags |= RERR_NODELETE; - - new_rerr = rerr_create(flags, rt->dest_addr, - rt->dest_seqno); - DEBUG(LOG_DEBUG, 0, "Added %s as unreachable, seqno=%lu", - ip_to_str(rt->dest_addr), rt->dest_seqno); - - if (rt->nprec == 1) - rerr_unicast_dest = - FIRST_PREC(rt->precursors)->neighbor; - - } else { - /* Decide whether new precursors make this a non unicast RERR */ - rerr_add_udest(new_rerr, rt->dest_addr, rt->dest_seqno); - - DEBUG(LOG_DEBUG, 0, "Added %s as unreachable, seqno=%lu", - ip_to_str(rt->dest_addr), rt->dest_seqno); - - if (rerr_unicast_dest.s_addr) { - list_t *pos2; - list_foreach(pos2, &rt->precursors) { - precursor_t *pr = (precursor_t *) pos2; - if (pr->neighbor.s_addr != rerr_unicast_dest.s_addr) { - rerr_unicast_dest.s_addr = 0; - break; - } - } - } - } - } else { - DEBUG(LOG_DEBUG, 0, - "Not sending RERR, no precursors or route in RT_REPAIR"); - } - /* We should delete the precursor list for all unreachable - destinations. */ - if (rt->state == INVALID) - precursor_list_destroy(rt); - } else { - DEBUG(LOG_DEBUG, 0, "Ignoring UDEST %s", ip_to_str(udest_addr)); - } - udest = RERR_UDEST_NEXT(udest); - rerr->dest_count--; - } /* End while() */ + /* Invalidate route: */ + if (!rerr->n) { + rt_table_invalidate(rt); + } + /* (a) updates the corresponding destination sequence number + with the Destination Sequence Number in the packet, and */ + rt->dest_seqno = rerr_dest_seqno; + + /* (d) check precursor list for emptiness. If not empty, include + the destination as an unreachable destination in the + RERR... */ + if (rt->nprec && !(rt->flags & RT_REPAIR)) { + + if (!new_rerr) { + u_int8_t flags = 0; + + if (rerr->n) + flags |= RERR_NODELETE; + + new_rerr = + rerr_create(flags, rt->dest_addr, rt->dest_seqno); + DEBUG(LOG_DEBUG, 0, "Added %s as unreachable, seqno=%lu", + ip_to_str(rt->dest_addr), rt->dest_seqno); + + if (rt->nprec == 1) + rerr_unicast_dest = + FIRST_PREC(rt->precursors)->neighbor; + + } else { + /* Decide whether new precursors make this a non unicast + * RERR */ + rerr_add_udest(new_rerr, rt->dest_addr, rt->dest_seqno); + + DEBUG(LOG_DEBUG, 0, "Added %s as unreachable, seqno=%lu", + ip_to_str(rt->dest_addr), rt->dest_seqno); + + if (rerr_unicast_dest.s_addr) { + list_t *pos2; + list_foreach(pos2, &rt->precursors) + { + precursor_t *pr = (precursor_t *)pos2; + if (pr->neighbor.s_addr != + rerr_unicast_dest.s_addr) { + rerr_unicast_dest.s_addr = 0; + break; + } + } + } + } + } else { + DEBUG(LOG_DEBUG, 0, + "Not sending RERR, no precursors or route in RT_REPAIR"); + } + /* We should delete the precursor list for all unreachable + destinations. */ + if (rt->state == INVALID) + precursor_list_destroy(rt); + } else { + DEBUG(LOG_DEBUG, 0, "Ignoring UDEST %s", ip_to_str(udest_addr)); + } + udest = RERR_UDEST_NEXT(udest); + rerr->dest_count--; + } /* End while() */ /* If a RERR was created, then send it now... */ if (new_rerr) { - rt = rt_table_find(rerr_unicast_dest); - - if (rt && new_rerr->dest_count == 1 && rerr_unicast_dest.s_addr) - aodv_socket_send((AODV_msg *) new_rerr, - rerr_unicast_dest, - RERR_CALC_SIZE(new_rerr), 1, - &DEV_IFINDEX(rt->ifindex)); - - else if (new_rerr->dest_count > 0) { - /* FIXME: Should only transmit RERR on those interfaces - * which have precursor nodes for the broken route */ - for (i = 0; i < MAX_NR_INTERFACES; i++) { - struct in_addr dest; - - if (!DEV_NR(i).enabled) - continue; - dest.s_addr = AODV_BROADCAST; - aodv_socket_send((AODV_msg *) new_rerr, dest, - RERR_CALC_SIZE(new_rerr), 1, &DEV_NR(i)); - } - } + rt = rt_table_find(rerr_unicast_dest); + + if (rt && new_rerr->dest_count == 1 && rerr_unicast_dest.s_addr) + aodv_socket_send((AODV_msg *)new_rerr, rerr_unicast_dest, + RERR_CALC_SIZE(new_rerr), 1, + &DEV_IFINDEX(rt->ifindex)); + + else if (new_rerr->dest_count > 0) { + /* FIXME: Should only transmit RERR on those interfaces + * which have precursor nodes for the broken route */ + for (i = 0; i < MAX_NR_INTERFACES; i++) { + struct in_addr dest; + + if (!DEV_NR(i).enabled) + continue; + dest.s_addr = AODV_BROADCAST; + aodv_socket_send((AODV_msg *)new_rerr, dest, + RERR_CALC_SIZE(new_rerr), 1, &DEV_NR(i)); + } + } } } diff --git a/aodv_rerr.h b/aodv_rerr.h index 2c080a8..2ee7a2e 100644 --- a/aodv_rerr.h +++ b/aodv_rerr.h @@ -16,8 +16,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, - * + * Authors: Erik Nordström, + * * *****************************************************************************/ #ifndef _AODV_RERR_H @@ -35,11 +35,11 @@ typedef struct { u_int8_t type; #if defined(__LITTLE_ENDIAN) - u_int8_t res1:7; - u_int8_t n:1; + u_int8_t res1 : 7; + u_int8_t n : 1; #elif defined(__BIG_ENDIAN) - u_int8_t n:1; - u_int8_t res1:7; + u_int8_t n : 1; + u_int8_t res1 : 7; #else #error "Adjust your defines" #endif @@ -61,17 +61,18 @@ typedef struct { /* Given the total number of unreachable destination this macro returns the RERR size */ -#define RERR_CALC_SIZE(rerr) (RERR_SIZE + (rerr->dest_count-1)*RERR_UDEST_SIZE) +#define RERR_CALC_SIZE(rerr) \ + (RERR_SIZE + (rerr->dest_count - 1) * RERR_UDEST_SIZE) #define RERR_UDEST_FIRST(rerr) ((RERR_udest *)&rerr->dest_addr) #define RERR_UDEST_NEXT(udest) ((RERR_udest *)((char *)udest + RERR_UDEST_SIZE)) -#endif /* NS_NO_GLOBALS */ +#endif /* NS_NO_GLOBALS */ #ifndef NS_NO_DECLARATIONS RERR *rerr_create(u_int8_t flags, struct in_addr dest_addr, - u_int32_t dest_seqno); -void rerr_add_udest(RERR * rerr, struct in_addr udest, u_int32_t udest_seqno); -void rerr_process(RERR * rerr, int rerrlen, struct in_addr ip_src, - struct in_addr ip_dst); -#endif /* NS_NO_DECLARATIONS */ + u_int32_t dest_seqno); +void rerr_add_udest(RERR *rerr, struct in_addr udest, u_int32_t udest_seqno); +void rerr_process(RERR *rerr, int rerrlen, struct in_addr ip_src, + struct in_addr ip_dst); +#endif /* NS_NO_DECLARATIONS */ -#endif /* AODV_RERR_H */ +#endif /* AODV_RERR_H */ diff --git a/aodv_rrep.c b/aodv_rrep.c index 7b33420..22cccfe 100644 --- a/aodv_rrep.c +++ b/aodv_rrep.c @@ -16,40 +16,37 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, - * + * Authors: Erik Nordström, + * * *****************************************************************************/ #ifdef NS_PORT #include "ns-2/aodv-uu.h" #else -#include -#include "aodv_rrep.h" -#include "aodv_neighbor.h" #include "aodv_hello.h" -#include "routing_table.h" -#include "aodv_timeout.h" -#include "timer_queue.h" +#include "aodv_neighbor.h" +#include "aodv_rrep.h" #include "aodv_socket.h" -#include "defs.h" +#include "aodv_timeout.h" #include "debug.h" +#include "defs.h" #include "params.h" +#include "routing_table.h" +#include "timer_queue.h" +#include extern int unidir_hack, optimized_hellos, llfeedback; #endif -RREP *NS_CLASS rrep_create(u_int8_t flags, - u_int8_t prefix, - u_int8_t hcnt, - struct in_addr dest_addr, - u_int32_t dest_seqno, - struct in_addr orig_addr, u_int32_t life) +RREP *NS_CLASS rrep_create(u_int8_t flags, u_int8_t prefix, u_int8_t hcnt, + struct in_addr dest_addr, u_int32_t dest_seqno, + struct in_addr orig_addr, u_int32_t life) { RREP *rrep; - rrep = (RREP *) aodv_socket_new_msg(); + rrep = (RREP *)aodv_socket_new_msg(); rrep->type = AODV_RREP; rrep->res1 = 0; rrep->res2 = 0; @@ -61,15 +58,15 @@ RREP *NS_CLASS rrep_create(u_int8_t flags, rrep->lifetime = htonl(life); if (flags & RREP_REPAIR) - rrep->r = 1; + rrep->r = 1; if (flags & RREP_ACK) - rrep->a = 1; + rrep->a = 1; - /* Don't print information about hello messages... */ + /* Don't print information about hello messages... */ #ifdef DEBUG_OUTPUT if (rrep->dest_addr != rrep->orig_addr) { - DEBUG(LOG_DEBUG, 0, "Assembled RREP:"); - log_pkt_fields((AODV_msg *) rrep); + DEBUG(LOG_DEBUG, 0, "Assembled RREP:"); + log_pkt_fields((AODV_msg *)rrep); } #endif @@ -80,7 +77,7 @@ RREP_ack *NS_CLASS rrep_ack_create() { RREP_ack *rrep_ack; - rrep_ack = (RREP_ack *) aodv_socket_new_msg(); + rrep_ack = (RREP_ack *)aodv_socket_new_msg(); rrep_ack->type = AODV_RREP_ACK; DEBUG(LOG_DEBUG, 0, "Assembled RREP_ack"); @@ -88,17 +85,17 @@ RREP_ack *NS_CLASS rrep_ack_create() return rrep_ack; } -void NS_CLASS rrep_ack_process(RREP_ack * rrep_ack, int rrep_acklen, - struct in_addr ip_src, struct in_addr ip_dst) +void NS_CLASS rrep_ack_process(RREP_ack *rrep_ack, int rrep_acklen, + struct in_addr ip_src, struct in_addr ip_dst) { rt_table_t *rt; rt = rt_table_find(ip_src); if (rt == NULL) { - DEBUG(LOG_WARNING, 0, "No RREP_ACK expected for %s", ip_to_str(ip_src)); + DEBUG(LOG_WARNING, 0, "No RREP_ACK expected for %s", ip_to_str(ip_src)); - return; + return; } DEBUG(LOG_DEBUG, 0, "Received RREP_ACK from %s", ip_to_str(ip_src)); @@ -106,15 +103,15 @@ void NS_CLASS rrep_ack_process(RREP_ack * rrep_ack, int rrep_acklen, timer_remove(&rt->ack_timer); } -AODV_ext *NS_CLASS rrep_add_ext(RREP * rrep, int type, unsigned int offset, - int len, char *data) +AODV_ext *NS_CLASS rrep_add_ext(RREP *rrep, int type, unsigned int offset, + int len, char *data) { AODV_ext *ext = NULL; if (offset < RREP_SIZE) - return NULL; + return NULL; - ext = (AODV_ext *) ((char *) rrep + offset); + ext = (AODV_ext *)((char *)rrep + offset); ext->type = type; ext->length = len; @@ -124,73 +121,73 @@ AODV_ext *NS_CLASS rrep_add_ext(RREP * rrep, int type, unsigned int offset, return ext; } -void NS_CLASS rrep_send(RREP * rrep, rt_table_t * rev_rt, - rt_table_t * fwd_rt, int size) +void NS_CLASS rrep_send(RREP *rrep, rt_table_t *rev_rt, rt_table_t *fwd_rt, + int size) { u_int8_t rrep_flags = 0; struct in_addr dest; if (!rev_rt) { - DEBUG(LOG_WARNING, 0, "Can't send RREP, rev_rt = NULL!"); - return; + DEBUG(LOG_WARNING, 0, "Can't send RREP, rev_rt = NULL!"); + return; } dest.s_addr = rrep->dest_addr; /* Check if we should request a RREP-ACK */ if ((rev_rt->state == VALID && rev_rt->flags & RT_UNIDIR) || - (rev_rt->hcnt == 1 && unidir_hack)) { - rt_table_t *neighbor = rt_table_find(rev_rt->next_hop); - - if (neighbor && neighbor->state == VALID && !neighbor->ack_timer.used) { - /* If the node we received a RREQ for is a neighbor we are - probably facing a unidirectional link... Better request a - RREP-ack */ - rrep_flags |= RREP_ACK; - neighbor->flags |= RT_UNIDIR; - - /* Must remove any pending hello timeouts when we set the - RT_UNIDIR flag, else the route may expire after we begin to - ignore hellos... */ - timer_remove(&neighbor->hello_timer); - neighbor_link_break(neighbor); - - DEBUG(LOG_DEBUG, 0, "Link to %s is unidirectional!", - ip_to_str(neighbor->dest_addr)); - - timer_set_timeout(&neighbor->ack_timer, NEXT_HOP_WAIT); - } + (rev_rt->hcnt == 1 && unidir_hack)) { + rt_table_t *neighbor = rt_table_find(rev_rt->next_hop); + + if (neighbor && neighbor->state == VALID && !neighbor->ack_timer.used) { + /* If the node we received a RREQ for is a neighbor we are + probably facing a unidirectional link... Better request a + RREP-ack */ + rrep_flags |= RREP_ACK; + neighbor->flags |= RT_UNIDIR; + + /* Must remove any pending hello timeouts when we set the + RT_UNIDIR flag, else the route may expire after we begin to + ignore hellos... */ + timer_remove(&neighbor->hello_timer); + neighbor_link_break(neighbor); + + DEBUG(LOG_DEBUG, 0, "Link to %s is unidirectional!", + ip_to_str(neighbor->dest_addr)); + + timer_set_timeout(&neighbor->ack_timer, NEXT_HOP_WAIT); + } } DEBUG(LOG_DEBUG, 0, "Sending RREP to next hop %s about %s->%s", - ip_to_str(rev_rt->next_hop), ip_to_str(rev_rt->dest_addr), - ip_to_str(dest)); + ip_to_str(rev_rt->next_hop), ip_to_str(rev_rt->dest_addr), + ip_to_str(dest)); - aodv_socket_send((AODV_msg *) rrep, rev_rt->next_hop, size, MAXTTL, - &DEV_IFINDEX(rev_rt->ifindex)); + aodv_socket_send((AODV_msg *)rrep, rev_rt->next_hop, size, MAXTTL, + &DEV_IFINDEX(rev_rt->ifindex)); /* Update precursor lists */ if (fwd_rt) { - precursor_add(fwd_rt, rev_rt->next_hop); - precursor_add(rev_rt, fwd_rt->next_hop); + precursor_add(fwd_rt, rev_rt->next_hop); + precursor_add(rev_rt, fwd_rt->next_hop); } if (!llfeedback && optimized_hellos) - hello_start(); + hello_start(); } -void NS_CLASS rrep_forward(RREP * rrep, int size, rt_table_t * rev_rt, - rt_table_t * fwd_rt, int ttl) +void NS_CLASS rrep_forward(RREP *rrep, int size, rt_table_t *rev_rt, + rt_table_t *fwd_rt, int ttl) { /* Sanity checks... */ if (!fwd_rt || !rev_rt) { - DEBUG(LOG_WARNING, 0, "Could not forward RREP because of NULL route!"); - return; + DEBUG(LOG_WARNING, 0, "Could not forward RREP because of NULL route!"); + return; } if (!rrep) { - DEBUG(LOG_WARNING, 0, "No RREP to forward!"); - return; + DEBUG(LOG_WARNING, 0, "No RREP to forward!"); + return; } DEBUG(LOG_DEBUG, 0, "Forwarding RREP to %s", ip_to_str(rev_rt->next_hop)); @@ -198,32 +195,32 @@ void NS_CLASS rrep_forward(RREP * rrep, int size, rt_table_t * rev_rt, /* Here we should do a check if we should request a RREP_ACK, i.e we suspect a unidirectional link.. But how? */ if (0) { - rt_table_t *neighbor; - - /* If the source of the RREP is not a neighbor we must find the - neighbor (link) entry which is the next hop towards the RREP - source... */ - if (rev_rt->dest_addr.s_addr != rev_rt->next_hop.s_addr) - neighbor = rt_table_find(rev_rt->next_hop); - else - neighbor = rev_rt; - - if (neighbor && !neighbor->ack_timer.used) { - /* If the node we received a RREQ for is a neighbor we are - probably facing a unidirectional link... Better request a - RREP-ack */ - rrep->a = 1; - neighbor->flags |= RT_UNIDIR; - - timer_set_timeout(&neighbor->ack_timer, NEXT_HOP_WAIT); - } + rt_table_t *neighbor; + + /* If the source of the RREP is not a neighbor we must find the + neighbor (link) entry which is the next hop towards the RREP + source... */ + if (rev_rt->dest_addr.s_addr != rev_rt->next_hop.s_addr) + neighbor = rt_table_find(rev_rt->next_hop); + else + neighbor = rev_rt; + + if (neighbor && !neighbor->ack_timer.used) { + /* If the node we received a RREQ for is a neighbor we are + probably facing a unidirectional link... Better request a + RREP-ack */ + rrep->a = 1; + neighbor->flags |= RT_UNIDIR; + + timer_set_timeout(&neighbor->ack_timer, NEXT_HOP_WAIT); + } } - rrep = (RREP *) aodv_socket_queue_msg((AODV_msg *) rrep, size); - rrep->hcnt = fwd_rt->hcnt; /* Update the hopcount */ + rrep = (RREP *)aodv_socket_queue_msg((AODV_msg *)rrep, size); + rrep->hcnt = fwd_rt->hcnt; /* Update the hopcount */ - aodv_socket_send((AODV_msg *) rrep, rev_rt->next_hop, size, ttl, - &DEV_IFINDEX(rev_rt->ifindex)); + aodv_socket_send((AODV_msg *)rrep, rev_rt->next_hop, size, ttl, + &DEV_IFINDEX(rev_rt->ifindex)); precursor_add(fwd_rt, rev_rt->next_hop); precursor_add(rev_rt, fwd_rt->next_hop); @@ -231,10 +228,9 @@ void NS_CLASS rrep_forward(RREP * rrep, int size, rt_table_t * rev_rt, rt_table_update_timeout(rev_rt, ACTIVE_ROUTE_TIMEOUT); } - -void NS_CLASS rrep_process(RREP * rrep, int rreplen, struct in_addr ip_src, - struct in_addr ip_dst, int ip_ttl, - unsigned int ifindex) +void NS_CLASS rrep_process(RREP *rrep, int rreplen, struct in_addr ip_src, + struct in_addr ip_dst, int ip_ttl, + unsigned int ifindex) { u_int32_t rrep_lifetime, rrep_seqno, rrep_new_hcnt; u_int8_t pre_repair_hcnt = 0, pre_repair_flags = 0; @@ -256,55 +252,56 @@ void NS_CLASS rrep_process(RREP * rrep, int rreplen, struct in_addr ip_src, /* Increment RREP hop count to account for intermediate node... */ rrep_new_hcnt = rrep->hcnt + 1; - if (rreplen < (int) RREP_SIZE) { - alog(LOG_WARNING, 0, __FUNCTION__, - "IP data field too short (%u bytes)" - " from %s to %s", rreplen, ip_to_str(ip_src), ip_to_str(ip_dst)); - return; + if (rreplen < (int)RREP_SIZE) { + alog(LOG_WARNING, 0, __FUNCTION__, + "IP data field too short (%u bytes)" + " from %s to %s", + rreplen, ip_to_str(ip_src), ip_to_str(ip_dst)); + return; } /* Ignore messages which aim to a create a route to one self */ if (rrep_dest.s_addr == DEV_IFINDEX(ifindex).ipaddr.s_addr) - return; + return; - DEBUG(LOG_DEBUG, 0, "from %s about %s->%s", - ip_to_str(ip_src), ip_to_str(rrep_orig), ip_to_str(rrep_dest)); + DEBUG(LOG_DEBUG, 0, "from %s about %s->%s", ip_to_str(ip_src), + ip_to_str(rrep_orig), ip_to_str(rrep_dest)); #ifdef DEBUG_OUTPUT - log_pkt_fields((AODV_msg *) rrep); + log_pkt_fields((AODV_msg *)rrep); #endif /* Determine whether there are any extensions */ - ext = (AODV_ext *) ((char *) rrep + RREP_SIZE); + ext = (AODV_ext *)((char *)rrep + RREP_SIZE); while ((rreplen - extlen) > RREP_SIZE) { - switch (ext->type) { - case RREP_EXT: - DEBUG(LOG_INFO, 0, "RREP include EXTENSION"); - /* Do something here */ - break; + switch (ext->type) { + case RREP_EXT: + DEBUG(LOG_INFO, 0, "RREP include EXTENSION"); + /* Do something here */ + break; #ifdef CONFIG_GATEWAY - case RREP_INET_DEST_EXT: - if (ext->length == sizeof(u_int32_t)) { - - /* Destination address in RREP is the gateway address, while the - * extension holds the real destination */ - memcpy(&inet_dest_addr, AODV_EXT_DATA(ext), ext->length); - - DEBUG(LOG_DEBUG, 0, "RREP_INET_DEST_EXT: <%s>", - ip_to_str(inet_dest_addr)); - /* This was a RREP from a gateway */ - rt_flags |= RT_GATEWAY; - inet_rrep = 1; - break; - } + case RREP_INET_DEST_EXT: + if (ext->length == sizeof(u_int32_t)) { + + /* Destination address in RREP is the gateway address, while the + * extension holds the real destination */ + memcpy(&inet_dest_addr, AODV_EXT_DATA(ext), ext->length); + + DEBUG(LOG_DEBUG, 0, "RREP_INET_DEST_EXT: <%s>", + ip_to_str(inet_dest_addr)); + /* This was a RREP from a gateway */ + rt_flags |= RT_GATEWAY; + inet_rrep = 1; + break; + } #endif - default: - alog(LOG_WARNING, 0, __FUNCTION__, "Unknown or bad extension %d", - ext->type); - break; - } - extlen += AODV_EXT_SIZE(ext); - ext = AODV_EXT_NEXT(ext); + default: + alog(LOG_WARNING, 0, __FUNCTION__, "Unknown or bad extension %d", + ext->type); + break; + } + extlen += AODV_EXT_SIZE(ext); + ext = AODV_EXT_NEXT(ext); } /* ---------- CHECK IF WE SHOULD MAKE A FORWARD ROUTE ------------ */ @@ -313,112 +310,112 @@ void NS_CLASS rrep_process(RREP * rrep, int rreplen, struct in_addr ip_src, rev_rt = rt_table_find(rrep_orig); if (!fwd_rt) { - /* We didn't have an existing entry, so we insert a new one. */ - fwd_rt = rt_table_insert(rrep_dest, ip_src, rrep_new_hcnt, rrep_seqno, - rrep_lifetime, VALID, rt_flags, ifindex); + /* We didn't have an existing entry, so we insert a new one. */ + fwd_rt = rt_table_insert(rrep_dest, ip_src, rrep_new_hcnt, rrep_seqno, + rrep_lifetime, VALID, rt_flags, ifindex); } else if (fwd_rt->dest_seqno == 0 || - (int32_t) rrep_seqno > (int32_t) fwd_rt->dest_seqno || - (rrep_seqno == fwd_rt->dest_seqno && - (fwd_rt->state == INVALID || fwd_rt->flags & RT_UNIDIR || - rrep_new_hcnt < fwd_rt->hcnt))) { - pre_repair_hcnt = fwd_rt->hcnt; - pre_repair_flags = fwd_rt->flags; - - fwd_rt = rt_table_update(fwd_rt, ip_src, rrep_new_hcnt, rrep_seqno, - rrep_lifetime, VALID, - rt_flags | fwd_rt->flags); + (int32_t)rrep_seqno > (int32_t)fwd_rt->dest_seqno || + (rrep_seqno == fwd_rt->dest_seqno && + (fwd_rt->state == INVALID || fwd_rt->flags & RT_UNIDIR || + rrep_new_hcnt < fwd_rt->hcnt))) { + pre_repair_hcnt = fwd_rt->hcnt; + pre_repair_flags = fwd_rt->flags; + + fwd_rt = + rt_table_update(fwd_rt, ip_src, rrep_new_hcnt, rrep_seqno, + rrep_lifetime, VALID, rt_flags | fwd_rt->flags); } else { - if (fwd_rt->hcnt > 1) { - DEBUG(LOG_DEBUG, 0, - "Dropping RREP, fwd_rt->hcnt=%d fwd_rt->seqno=%ld", - fwd_rt->hcnt, fwd_rt->dest_seqno); - } - return; + if (fwd_rt->hcnt > 1) { + DEBUG(LOG_DEBUG, 0, + "Dropping RREP, fwd_rt->hcnt=%d fwd_rt->seqno=%ld", + fwd_rt->hcnt, fwd_rt->dest_seqno); + } + return; } - /* If the RREP_ACK flag is set we must send a RREP acknowledgement to the destination that replied... */ if (rrep->a) { - RREP_ack *rrep_ack; + RREP_ack *rrep_ack; - rrep_ack = rrep_ack_create(); - aodv_socket_send((AODV_msg *) rrep_ack, fwd_rt->next_hop, - NEXT_HOP_WAIT, MAXTTL, &DEV_IFINDEX(fwd_rt->ifindex)); - /* Remove RREP_ACK flag... */ - rrep->a = 0; + rrep_ack = rrep_ack_create(); + aodv_socket_send((AODV_msg *)rrep_ack, fwd_rt->next_hop, NEXT_HOP_WAIT, + MAXTTL, &DEV_IFINDEX(fwd_rt->ifindex)); + /* Remove RREP_ACK flag... */ + rrep->a = 0; } /* Check if this RREP was for us (i.e. we previously made a RREQ for this host). */ if (rrep_orig.s_addr == DEV_IFINDEX(ifindex).ipaddr.s_addr) { #ifdef CONFIG_GATEWAY - if (inet_rrep) { - rt_table_t *inet_rt; - inet_rt = rt_table_find(inet_dest_addr); - - /* Add a "fake" route indicating that this is an Internet - * destination, thus should be encapsulated and routed through a - * gateway... */ - if (!inet_rt) - rt_table_insert(inet_dest_addr, rrep_dest, rrep_new_hcnt, 0, - rrep_lifetime, VALID, RT_INET_DEST, ifindex); - else if (inet_rt->state == INVALID || rrep_new_hcnt < inet_rt->hcnt) { - rt_table_update(inet_rt, rrep_dest, rrep_new_hcnt, 0, - rrep_lifetime, VALID, RT_INET_DEST | - inet_rt->flags); - } else { - DEBUG(LOG_DEBUG, 0, "INET Response, but no update %s", - ip_to_str(inet_dest_addr)); - } - } -#endif /* CONFIG_GATEWAY */ - - /* If the route was previously in repair, a NO DELETE RERR should be - sent to the source of the route, so that it may choose to reinitiate - route discovery for the destination. Fixed a bug here that caused the - repair flag to be unset and the RERR never being sent. Thanks to - McWood for discovering this. */ - if (pre_repair_flags & RT_REPAIR) { - if (fwd_rt->hcnt > pre_repair_hcnt) { - RERR *rerr; - u_int8_t rerr_flags = 0; - struct in_addr dest; - - dest.s_addr = AODV_BROADCAST; - - rerr_flags |= RERR_NODELETE; - rerr = rerr_create(rerr_flags, fwd_rt->dest_addr, - fwd_rt->dest_seqno); - - if (fwd_rt->nprec) - aodv_socket_send((AODV_msg *) rerr, dest, - RERR_CALC_SIZE(rerr), 1, - &DEV_IFINDEX(fwd_rt->ifindex)); - } - } + if (inet_rrep) { + rt_table_t *inet_rt; + inet_rt = rt_table_find(inet_dest_addr); + + /* Add a "fake" route indicating that this is an Internet + * destination, thus should be encapsulated and routed through a + * gateway... */ + if (!inet_rt) + rt_table_insert(inet_dest_addr, rrep_dest, rrep_new_hcnt, 0, + rrep_lifetime, VALID, RT_INET_DEST, ifindex); + else if (inet_rt->state == INVALID || + rrep_new_hcnt < inet_rt->hcnt) { + rt_table_update(inet_rt, rrep_dest, rrep_new_hcnt, 0, + rrep_lifetime, VALID, + RT_INET_DEST | inet_rt->flags); + } else { + DEBUG(LOG_DEBUG, 0, "INET Response, but no update %s", + ip_to_str(inet_dest_addr)); + } + } +#endif /* CONFIG_GATEWAY */ + + /* If the route was previously in repair, a NO DELETE RERR should be + sent to the source of the route, so that it may choose to reinitiate + route discovery for the destination. Fixed a bug here that caused the + repair flag to be unset and the RERR never being sent. Thanks to + McWood for discovering this. */ + if (pre_repair_flags & RT_REPAIR) { + if (fwd_rt->hcnt > pre_repair_hcnt) { + RERR *rerr; + u_int8_t rerr_flags = 0; + struct in_addr dest; + + dest.s_addr = AODV_BROADCAST; + + rerr_flags |= RERR_NODELETE; + rerr = rerr_create(rerr_flags, fwd_rt->dest_addr, + fwd_rt->dest_seqno); + + if (fwd_rt->nprec) + aodv_socket_send((AODV_msg *)rerr, dest, + RERR_CALC_SIZE(rerr), 1, + &DEV_IFINDEX(fwd_rt->ifindex)); + } + } } else { - /* --- Here we FORWARD the RREP on the REVERSE route --- */ - if (rev_rt && rev_rt->state == VALID) { - rrep_forward(rrep, rreplen, rev_rt, fwd_rt, --ip_ttl); - } else { - DEBUG(LOG_DEBUG, 0, "Could not forward RREP - NO ROUTE!!!"); - } + /* --- Here we FORWARD the RREP on the REVERSE route --- */ + if (rev_rt && rev_rt->state == VALID) { + rrep_forward(rrep, rreplen, rev_rt, fwd_rt, --ip_ttl); + } else { + DEBUG(LOG_DEBUG, 0, "Could not forward RREP - NO ROUTE!!!"); + } } if (!llfeedback && optimized_hellos) - hello_start(); + hello_start(); } /************************************************************************/ /* Include a Hello Interval Extension on the RREP and return new offset */ -int rrep_add_hello_ext(RREP * rrep, int offset, u_int32_t interval) +int rrep_add_hello_ext(RREP *rrep, int offset, u_int32_t interval) { AODV_ext *ext; - ext = (AODV_ext *) ((char *) rrep + RREP_SIZE + offset); + ext = (AODV_ext *)((char *)rrep + RREP_SIZE + offset); ext->type = RREP_HELLO_INTERVAL_EXT; ext->length = sizeof(interval); memcpy(AODV_EXT_DATA(ext), &interval, sizeof(interval)); diff --git a/aodv_rrep.h b/aodv_rrep.h index db94aad..4976f1b 100644 --- a/aodv_rrep.h +++ b/aodv_rrep.h @@ -16,8 +16,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, - * + * Authors: Erik Nordström, + * * *****************************************************************************/ #ifndef _AODV_RREP_H @@ -31,23 +31,23 @@ /* RREP Flags: */ -#define RREP_ACK 0x1 -#define RREP_REPAIR 0x2 +#define RREP_ACK 0x1 +#define RREP_REPAIR 0x2 typedef struct { u_int8_t type; #if defined(__LITTLE_ENDIAN) - u_int16_t res1:6; - u_int16_t a:1; - u_int16_t r:1; - u_int16_t prefix:5; - u_int16_t res2:3; + u_int16_t res1 : 6; + u_int16_t a : 1; + u_int16_t r : 1; + u_int16_t prefix : 5; + u_int16_t res2 : 3; #elif defined(__BIG_ENDIAN) - u_int16_t r:1; - u_int16_t a:1; - u_int16_t res1:6; - u_int16_t res2:3; - u_int16_t prefix:5; + u_int16_t r : 1; + u_int16_t a : 1; + u_int16_t res1 : 6; + u_int16_t res2 : 3; + u_int16_t prefix : 5; #else #error "Adjust your defines" #endif @@ -66,26 +66,23 @@ typedef struct { } RREP_ack; #define RREP_ACK_SIZE sizeof(RREP_ack) -#endif /* NS_NO_GLOBALS */ +#endif /* NS_NO_GLOBALS */ #ifndef NS_NO_DECLARATIONS -RREP *rrep_create(u_int8_t flags, - u_int8_t prefix, - u_int8_t hcnt, - struct in_addr dest_addr, - u_int32_t dest_seqno, - struct in_addr orig_addr, u_int32_t life); +RREP *rrep_create(u_int8_t flags, u_int8_t prefix, u_int8_t hcnt, + struct in_addr dest_addr, u_int32_t dest_seqno, + struct in_addr orig_addr, u_int32_t life); RREP_ack *rrep_ack_create(); -AODV_ext *rrep_add_ext(RREP * rrep, int type, unsigned int offset, - int len, char *data); -void rrep_send(RREP * rrep, rt_table_t * rev_rt, rt_table_t * fwd_rt, int size); -void rrep_forward(RREP * rrep, int size, rt_table_t * rev_rt, - rt_table_t * fwd_rt, int ttl); -void rrep_process(RREP * rrep, int rreplen, struct in_addr ip_src, - struct in_addr ip_dst, int ip_ttl, unsigned int ifindex); -void rrep_ack_process(RREP_ack * rrep_ack, int rreplen, struct in_addr ip_src, - struct in_addr ip_dst); -#endif /* NS_NO_DECLARATIONS */ +AODV_ext *rrep_add_ext(RREP *rrep, int type, unsigned int offset, int len, + char *data); +void rrep_send(RREP *rrep, rt_table_t *rev_rt, rt_table_t *fwd_rt, int size); +void rrep_forward(RREP *rrep, int size, rt_table_t *rev_rt, rt_table_t *fwd_rt, + int ttl); +void rrep_process(RREP *rrep, int rreplen, struct in_addr ip_src, + struct in_addr ip_dst, int ip_ttl, unsigned int ifindex); +void rrep_ack_process(RREP_ack *rrep_ack, int rreplen, struct in_addr ip_src, + struct in_addr ip_dst); +#endif /* NS_NO_DECLARATIONS */ -#endif /* AODV_RREP_H */ +#endif /* AODV_RREP_H */ diff --git a/aodv_rreq.c b/aodv_rreq.c index 48cb7d2..9a4e37c 100644 --- a/aodv_rreq.c +++ b/aodv_rreq.c @@ -16,8 +16,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, - * + * Authors: Erik Nordström, + * * *****************************************************************************/ @@ -26,16 +26,16 @@ #else #include -#include "aodv_rreq.h" #include "aodv_rrep.h" -#include "routing_table.h" -#include "aodv_timeout.h" -#include "timer_queue.h" +#include "aodv_rreq.h" #include "aodv_socket.h" +#include "aodv_timeout.h" +#include "debug.h" +#include "defs.h" #include "params.h" +#include "routing_table.h" #include "seek_list.h" -#include "defs.h" -#include "debug.h" +#include "timer_queue.h" #include "locality.h" #endif @@ -48,9 +48,9 @@ static LIST(rreq_records); static LIST(rreq_blacklist); static struct rreq_record *rreq_record_insert(struct in_addr orig_addr, - u_int32_t rreq_id); + u_int32_t rreq_id); static struct rreq_record *rreq_record_find(struct in_addr orig_addr, - u_int32_t rreq_id); + u_int32_t rreq_id); struct blacklist *rreq_blacklist_find(struct in_addr dest_addr); @@ -59,11 +59,11 @@ extern int internet_gw_mode; #endif RREQ *NS_CLASS rreq_create(u_int8_t flags, struct in_addr dest_addr, - u_int32_t dest_seqno, struct in_addr orig_addr) + u_int32_t dest_seqno, struct in_addr orig_addr) { RREQ *rreq; - rreq = (RREQ *) aodv_socket_new_msg(); + rreq = (RREQ *)aodv_socket_new_msg(); rreq->type = AODV_RREQ; rreq->res1 = 0; rreq->res2 = 0; @@ -79,31 +79,31 @@ RREQ *NS_CLASS rreq_create(u_int8_t flags, struct in_addr dest_addr, rreq->orig_seqno = htonl(this_host.seqno); if (flags & RREQ_JOIN) - rreq->j = 1; + rreq->j = 1; if (flags & RREQ_REPAIR) - rreq->r = 1; + rreq->r = 1; if (flags & RREQ_GRATUITOUS) - rreq->g = 1; + rreq->g = 1; if (flags & RREQ_DEST_ONLY) - rreq->d = 1; + rreq->d = 1; DEBUG(LOG_DEBUG, 0, "Assembled RREQ %s", ip_to_str(dest_addr)); #ifdef DEBUG_OUTPUT - log_pkt_fields((AODV_msg *) rreq); + log_pkt_fields((AODV_msg *)rreq); #endif return rreq; } -AODV_ext *rreq_add_ext(RREQ * rreq, int type, unsigned int offset, - int len, char *data) +AODV_ext *rreq_add_ext(RREQ *rreq, int type, unsigned int offset, int len, + char *data) { AODV_ext *ext = NULL; if (offset < RREQ_SIZE) - return NULL; + return NULL; - ext = (AODV_ext *) ((char *) rreq + offset); + ext = (AODV_ext *)((char *)rreq + offset); ext->type = type; ext->length = len; @@ -113,8 +113,8 @@ AODV_ext *rreq_add_ext(RREQ * rreq, int type, unsigned int offset, return ext; } -void NS_CLASS rreq_send(struct in_addr dest_addr, u_int32_t dest_seqno, - int ttl, u_int8_t flags) +void NS_CLASS rreq_send(struct in_addr dest_addr, u_int32_t dest_seqno, int ttl, + u_int8_t flags) { RREQ *rreq; struct in_addr dest; @@ -124,18 +124,18 @@ void NS_CLASS rreq_send(struct in_addr dest_addr, u_int32_t dest_seqno, /* Check if we should force the gratuitous flag... (-g option). */ if (rreq_gratuitous) - flags |= RREQ_GRATUITOUS; + flags |= RREQ_GRATUITOUS; /* Broadcast on all interfaces */ for (i = 0; i < MAX_NR_INTERFACES; i++) { - if (!DEV_NR(i).enabled) - continue; - rreq = rreq_create(flags, dest_addr, dest_seqno, DEV_NR(i).ipaddr); - aodv_socket_send((AODV_msg *) rreq, dest, RREQ_SIZE, ttl, &DEV_NR(i)); + if (!DEV_NR(i).enabled) + continue; + rreq = rreq_create(flags, dest_addr, dest_seqno, DEV_NR(i).ipaddr); + aodv_socket_send((AODV_msg *)rreq, dest, RREQ_SIZE, ttl, &DEV_NR(i)); } } -void NS_CLASS rreq_forward(RREQ * rreq, int size, int ttl) +void NS_CLASS rreq_forward(RREQ *rreq, int size, int ttl) { struct in_addr dest, orig; int i; @@ -144,26 +144,26 @@ void NS_CLASS rreq_forward(RREQ * rreq, int size, int ttl) orig.s_addr = rreq->orig_addr; /* FORWARD the RREQ if the TTL allows it. */ - DEBUG(LOG_INFO, 0, "forwarding RREQ src=%s, rreq_id=%lu", - ip_to_str(orig), ntohl(rreq->rreq_id)); + DEBUG(LOG_INFO, 0, "forwarding RREQ src=%s, rreq_id=%lu", ip_to_str(orig), + ntohl(rreq->rreq_id)); /* Queue the received message in the send buffer */ - rreq = (RREQ *) aodv_socket_queue_msg((AODV_msg *) rreq, size); + rreq = (RREQ *)aodv_socket_queue_msg((AODV_msg *)rreq, size); - rreq->hcnt++; /* Increase hopcount to account for - * intermediate route */ + rreq->hcnt++; /* Increase hopcount to account for + * intermediate route */ /* Send out on all interfaces */ for (i = 0; i < MAX_NR_INTERFACES; i++) { - if (!DEV_NR(i).enabled) - continue; - aodv_socket_send((AODV_msg *) rreq, dest, size, ttl, &DEV_NR(i)); + if (!DEV_NR(i).enabled) + continue; + aodv_socket_send((AODV_msg *)rreq, dest, size, ttl, &DEV_NR(i)); } } -void NS_CLASS rreq_process(RREQ * rreq, int rreqlen, struct in_addr ip_src, - struct in_addr ip_dst, int ip_ttl, - unsigned int ifindex) +void NS_CLASS rreq_process(RREQ *rreq, int rreqlen, struct in_addr ip_src, + struct in_addr ip_dst, int ip_ttl, + unsigned int ifindex) { AODV_ext *ext; @@ -182,58 +182,58 @@ void NS_CLASS rreq_process(RREQ * rreq, int rreqlen, struct in_addr ip_src, rreq_orig_seqno = ntohl(rreq->orig_seqno); rreq_new_hcnt = rreq->hcnt + 1; - /* Ignore RREQ's that originated from this node. Either we do this or we buffer our own sent RREQ's as we do with others we receive. */ if (rreq_orig.s_addr == DEV_IFINDEX(ifindex).ipaddr.s_addr) - return; + return; DEBUG(LOG_DEBUG, 0, "ip_src=%s rreq_orig=%s rreq_dest=%s ttl=%d", - ip_to_str(ip_src), ip_to_str(rreq_orig), ip_to_str(rreq_dest), - ip_ttl); - - if (rreqlen < (int) RREQ_SIZE) { - alog(LOG_WARNING, 0, - __FUNCTION__, "IP data field too short (%u bytes)" - "from %s to %s", rreqlen, ip_to_str(ip_src), ip_to_str(ip_dst)); - return; + ip_to_str(ip_src), ip_to_str(rreq_orig), ip_to_str(rreq_dest), + ip_ttl); + + if (rreqlen < (int)RREQ_SIZE) { + alog(LOG_WARNING, 0, __FUNCTION__, + "IP data field too short (%u bytes)" + "from %s to %s", + rreqlen, ip_to_str(ip_src), ip_to_str(ip_dst)); + return; } /* Check if the previous hop of the RREQ is in the blacklist set. If it is, then ignore the RREQ. */ if (rreq_blacklist_find(ip_src)) { - DEBUG(LOG_DEBUG, 0, "prev hop of RREQ blacklisted, ignoring!"); - return; + DEBUG(LOG_DEBUG, 0, "prev hop of RREQ blacklisted, ignoring!"); + return; } /* Ignore already processed RREQs. */ if (rreq_record_find(rreq_orig, rreq_id)) - return; + return; /* Now buffer this RREQ so that we don't process a similar RREQ we get within PATH_DISCOVERY_TIME. */ rreq_record_insert(rreq_orig, rreq_id); /* Determine whether there are any RREQ extensions */ - ext = (AODV_ext *) ((char *) rreq + RREQ_SIZE); + ext = (AODV_ext *)((char *)rreq + RREQ_SIZE); while ((rreqlen - extlen) > RREQ_SIZE) { - switch (ext->type) { - case RREQ_EXT: - DEBUG(LOG_INFO, 0, "RREQ include EXTENSION"); - /* Do something here */ - break; - default: - alog(LOG_WARNING, 0, __FUNCTION__, "Unknown extension type %d", - ext->type); - break; - } - extlen += AODV_EXT_SIZE(ext); - ext = AODV_EXT_NEXT(ext); + switch (ext->type) { + case RREQ_EXT: + DEBUG(LOG_INFO, 0, "RREQ include EXTENSION"); + /* Do something here */ + break; + default: + alog(LOG_WARNING, 0, __FUNCTION__, "Unknown extension type %d", + ext->type); + break; + } + extlen += AODV_EXT_SIZE(ext); + ext = AODV_EXT_NEXT(ext); } #ifdef DEBUG_OUTPUT - log_pkt_fields((AODV_msg *) rreq); + log_pkt_fields((AODV_msg *)rreq); #endif /* The node always creates or updates a REVERSE ROUTE entry to the @@ -244,31 +244,31 @@ void NS_CLASS rreq_process(RREQ * rreq, int rreqlen, struct in_addr ip_src, life = PATH_DISCOVERY_TIME - 2 * rreq_new_hcnt * NODE_TRAVERSAL_TIME; if (rev_rt == NULL) { - DEBUG(LOG_DEBUG, 0, "Creating REVERSE route entry, RREQ orig: %s", - ip_to_str(rreq_orig)); + DEBUG(LOG_DEBUG, 0, "Creating REVERSE route entry, RREQ orig: %s", + ip_to_str(rreq_orig)); - rev_rt = rt_table_insert(rreq_orig, ip_src, rreq_new_hcnt, - rreq_orig_seqno, life, VALID, 0, ifindex); + rev_rt = rt_table_insert(rreq_orig, ip_src, rreq_new_hcnt, + rreq_orig_seqno, life, VALID, 0, ifindex); } else { - if (rev_rt->dest_seqno == 0 || - (int32_t) rreq_orig_seqno > (int32_t) rev_rt->dest_seqno || - (rreq_orig_seqno == rev_rt->dest_seqno && - (rev_rt->state == INVALID || rreq_new_hcnt < rev_rt->hcnt))) { - rev_rt = rt_table_update(rev_rt, ip_src, rreq_new_hcnt, - rreq_orig_seqno, life, VALID, - rev_rt->flags); - } + if (rev_rt->dest_seqno == 0 || + (int32_t)rreq_orig_seqno > (int32_t)rev_rt->dest_seqno || + (rreq_orig_seqno == rev_rt->dest_seqno && + (rev_rt->state == INVALID || rreq_new_hcnt < rev_rt->hcnt))) { + rev_rt = + rt_table_update(rev_rt, ip_src, rreq_new_hcnt, rreq_orig_seqno, + life, VALID, rev_rt->flags); + } #ifdef DISABLED - /* This is a out of draft modification of AODV-UU to prevent - nodes from creating routing entries to themselves during - the RREP phase. We simple drop the RREQ if there is a - missmatch between the reverse path on the node and the one - suggested by the RREQ. */ - - else if (rev_rt->next_hop.s_addr != ip_src.s_addr) { - DEBUG(LOG_DEBUG, 0, "Dropping RREQ due to reverse route mismatch!"); - return; - } + /* This is a out of draft modification of AODV-UU to prevent + nodes from creating routing entries to themselves during + the RREP phase. We simple drop the RREQ if there is a + missmatch between the reverse path on the node and the one + suggested by the RREQ. */ + + else if (rev_rt->next_hop.s_addr != ip_src.s_addr) { + DEBUG(LOG_DEBUG, 0, "Dropping RREQ due to reverse route mismatch!"); + return; + } #endif } /**** END updating/creating REVERSE route ****/ @@ -276,149 +276,149 @@ void NS_CLASS rreq_process(RREQ * rreq, int rreqlen, struct in_addr ip_src, #ifdef CONFIG_GATEWAY /* This is a gateway */ if (internet_gw_mode) { - /* Subnet locality decision */ - switch (locality(rreq_dest, ifindex)) { - case HOST_ADHOC: - break; - case HOST_INET: - /* We must increase the gw's sequence number before sending a RREP, - * otherwise intermediate nodes will not forward the RREP. */ - seqno_incr(this_host.seqno); - rrep = rrep_create(0, 0, 0, DEV_IFINDEX(rev_rt->ifindex).ipaddr, - this_host.seqno, rev_rt->dest_addr, - ACTIVE_ROUTE_TIMEOUT); - - ext = rrep_add_ext(rrep, RREP_INET_DEST_EXT, rrep_size, - sizeof(struct in_addr), (char *) &rreq_dest); - - rrep_size += AODV_EXT_SIZE(ext); - - DEBUG(LOG_DEBUG, 0, - "Responding for INTERNET dest: %s rrep_size=%d", - ip_to_str(rreq_dest), rrep_size); - - rrep_send(rrep, rev_rt, NULL, rrep_size); - - return; - - case HOST_UNKNOWN: - default: - DEBUG(LOG_DEBUG, 0, "GW: Destination unkown"); - } + /* Subnet locality decision */ + switch (locality(rreq_dest, ifindex)) { + case HOST_ADHOC: + break; + case HOST_INET: + /* We must increase the gw's sequence number before sending a RREP, + * otherwise intermediate nodes will not forward the RREP. */ + seqno_incr(this_host.seqno); + rrep = rrep_create(0, 0, 0, DEV_IFINDEX(rev_rt->ifindex).ipaddr, + this_host.seqno, rev_rt->dest_addr, + ACTIVE_ROUTE_TIMEOUT); + + ext = rrep_add_ext(rrep, RREP_INET_DEST_EXT, rrep_size, + sizeof(struct in_addr), (char *)&rreq_dest); + + rrep_size += AODV_EXT_SIZE(ext); + + DEBUG(LOG_DEBUG, 0, "Responding for INTERNET dest: %s rrep_size=%d", + ip_to_str(rreq_dest), rrep_size); + + rrep_send(rrep, rev_rt, NULL, rrep_size); + + return; + + case HOST_UNKNOWN: + default: + DEBUG(LOG_DEBUG, 0, "GW: Destination unkown"); + } } #endif /* Are we the destination of the RREQ?, if so we should immediately send a RREP.. */ if (rreq_dest.s_addr == DEV_IFINDEX(ifindex).ipaddr.s_addr) { - /* WE are the RREQ DESTINATION. Update the node's own - sequence number to the maximum of the current seqno and the - one in the RREQ. */ - if (rreq_dest_seqno != 0) { - if ((int32_t) this_host.seqno < (int32_t) rreq_dest_seqno) - this_host.seqno = rreq_dest_seqno; - else if (this_host.seqno == rreq_dest_seqno) - seqno_incr(this_host.seqno); - } - rrep = rrep_create(0, 0, 0, DEV_IFINDEX(rev_rt->ifindex).ipaddr, - this_host.seqno, rev_rt->dest_addr, - MY_ROUTE_TIMEOUT); - - rrep_send(rrep, rev_rt, NULL, RREP_SIZE); + /* WE are the RREQ DESTINATION. Update the node's own + sequence number to the maximum of the current seqno and the + one in the RREQ. */ + if (rreq_dest_seqno != 0) { + if ((int32_t)this_host.seqno < (int32_t)rreq_dest_seqno) + this_host.seqno = rreq_dest_seqno; + else if (this_host.seqno == rreq_dest_seqno) + seqno_incr(this_host.seqno); + } + rrep = + rrep_create(0, 0, 0, DEV_IFINDEX(rev_rt->ifindex).ipaddr, + this_host.seqno, rev_rt->dest_addr, MY_ROUTE_TIMEOUT); + + rrep_send(rrep, rev_rt, NULL, RREP_SIZE); } else { - /* We are an INTERMEDIATE node. - check if we have an active - * route entry */ + /* We are an INTERMEDIATE node. - check if we have an active + * route entry */ - fwd_rt = rt_table_find(rreq_dest); + fwd_rt = rt_table_find(rreq_dest); - if (fwd_rt && fwd_rt->state == VALID && !rreq->d) { - struct timeval now; - u_int32_t lifetime; + if (fwd_rt && fwd_rt->state == VALID && !rreq->d) { + struct timeval now; + u_int32_t lifetime; - /* GENERATE RREP, i.e we have an ACTIVE route entry that is fresh - enough (our destination sequence number for that route is - larger than the one in the RREQ). */ + /* GENERATE RREP, i.e we have an ACTIVE route entry that is fresh + enough (our destination sequence number for that route is + larger than the one in the RREQ). */ - gettimeofday(&now, NULL); + gettimeofday(&now, NULL); #ifdef CONFIG_GATEWAY_DISABLED - if (fwd_rt->flags & RT_INET_DEST) { - rt_table_t *gw_rt; - /* This node knows that this is a rreq for an Internet - * destination and it has a valid route to the gateway */ - - goto forward; // DISABLED - - gw_rt = rt_table_find(fwd_rt->next_hop); - - if (!gw_rt || gw_rt->state == INVALID) - goto forward; - - lifetime = timeval_diff(&gw_rt->rt_timer.timeout, &now); - - rrep = rrep_create(0, 0, gw_rt->hcnt, gw_rt->dest_addr, - gw_rt->dest_seqno, rev_rt->dest_addr, - lifetime); - - ext = rrep_add_ext(rrep, RREP_INET_DEST_EXT, rrep_size, - sizeof(struct in_addr), (char *) &rreq_dest); - - rrep_size += AODV_EXT_SIZE(ext); - - DEBUG(LOG_DEBUG, 0, - "Intermediate node response for INTERNET dest: %s rrep_size=%d", - ip_to_str(rreq_dest), rrep_size); - - rrep_send(rrep, rev_rt, gw_rt, rrep_size); - return; - } -#endif /* CONFIG_GATEWAY_DISABLED */ - - /* Respond only if the sequence number is fresh enough... */ - if (fwd_rt->dest_seqno != 0 && - (int32_t) fwd_rt->dest_seqno >= (int32_t) rreq_dest_seqno) { - lifetime = timeval_diff(&fwd_rt->rt_timer.timeout, &now); - rrep = rrep_create(0, 0, fwd_rt->hcnt, fwd_rt->dest_addr, - fwd_rt->dest_seqno, rev_rt->dest_addr, - lifetime); - rrep_send(rrep, rev_rt, fwd_rt, rrep_size); - } else { - goto forward; - } - /* If the GRATUITOUS flag is set, we must also unicast a - gratuitous RREP to the destination. */ - if (rreq->g) { - rrep = rrep_create(0, 0, rev_rt->hcnt, rev_rt->dest_addr, - rev_rt->dest_seqno, fwd_rt->dest_addr, - lifetime); - - rrep_send(rrep, fwd_rt, rev_rt, RREP_SIZE); - - DEBUG(LOG_INFO, 0, "Sending G-RREP to %s with rte to %s", - ip_to_str(rreq_dest), ip_to_str(rreq_orig)); - } - return; - } - forward: - if (ip_ttl > 1) { - /* Update the sequence number in case the maintained one is - * larger */ - if (fwd_rt && !(fwd_rt->flags & RT_INET_DEST) && - (int32_t) fwd_rt->dest_seqno > (int32_t) rreq_dest_seqno) - rreq->dest_seqno = htonl(fwd_rt->dest_seqno); - - rreq_forward(rreq, rreqlen, --ip_ttl); - - } else { - DEBUG(LOG_DEBUG, 0, "RREQ not forwarded - ttl=0"); - } + if (fwd_rt->flags & RT_INET_DEST) { + rt_table_t *gw_rt; + /* This node knows that this is a rreq for an Internet + * destination and it has a valid route to the gateway */ + + goto forward; // DISABLED + + gw_rt = rt_table_find(fwd_rt->next_hop); + + if (!gw_rt || gw_rt->state == INVALID) + goto forward; + + lifetime = timeval_diff(&gw_rt->rt_timer.timeout, &now); + + rrep = + rrep_create(0, 0, gw_rt->hcnt, gw_rt->dest_addr, + gw_rt->dest_seqno, rev_rt->dest_addr, lifetime); + + ext = rrep_add_ext(rrep, RREP_INET_DEST_EXT, rrep_size, + sizeof(struct in_addr), (char *)&rreq_dest); + + rrep_size += AODV_EXT_SIZE(ext); + + DEBUG(LOG_DEBUG, 0, + "Intermediate node response for INTERNET dest: %s " + "rrep_size=%d", + ip_to_str(rreq_dest), rrep_size); + + rrep_send(rrep, rev_rt, gw_rt, rrep_size); + return; + } +#endif /* CONFIG_GATEWAY_DISABLED */ + + /* Respond only if the sequence number is fresh enough... */ + if (fwd_rt->dest_seqno != 0 && + (int32_t)fwd_rt->dest_seqno >= (int32_t)rreq_dest_seqno) { + lifetime = timeval_diff(&fwd_rt->rt_timer.timeout, &now); + rrep = rrep_create(0, 0, fwd_rt->hcnt, fwd_rt->dest_addr, + fwd_rt->dest_seqno, rev_rt->dest_addr, + lifetime); + rrep_send(rrep, rev_rt, fwd_rt, rrep_size); + } else { + goto forward; + } + /* If the GRATUITOUS flag is set, we must also unicast a + gratuitous RREP to the destination. */ + if (rreq->g) { + rrep = rrep_create(0, 0, rev_rt->hcnt, rev_rt->dest_addr, + rev_rt->dest_seqno, fwd_rt->dest_addr, + lifetime); + + rrep_send(rrep, fwd_rt, rev_rt, RREP_SIZE); + + DEBUG(LOG_INFO, 0, "Sending G-RREP to %s with rte to %s", + ip_to_str(rreq_dest), ip_to_str(rreq_orig)); + } + return; + } + forward: + if (ip_ttl > 1) { + /* Update the sequence number in case the maintained one is + * larger */ + if (fwd_rt && !(fwd_rt->flags & RT_INET_DEST) && + (int32_t)fwd_rt->dest_seqno > (int32_t)rreq_dest_seqno) + rreq->dest_seqno = htonl(fwd_rt->dest_seqno); + + rreq_forward(rreq, rreqlen, --ip_ttl); + + } else { + DEBUG(LOG_DEBUG, 0, "RREQ not forwarded - ttl=0"); + } } } /* Perform route discovery for a unicast destination */ void NS_CLASS rreq_route_discovery(struct in_addr dest_addr, u_int8_t flags, - struct ip_data *ipd) + struct ip_data *ipd) { struct timeval now; rt_table_t *rt; @@ -430,34 +430,34 @@ void NS_CLASS rreq_route_discovery(struct in_addr dest_addr, u_int8_t flags, gettimeofday(&now, NULL); if (seek_list_find(dest_addr)) - return; + return; /* If we already have a route entry, we use information from it. */ rt = rt_table_find(dest_addr); - ttl = NET_DIAMETER; /* This is the TTL if we don't use expanding - ring search */ + ttl = NET_DIAMETER; /* This is the TTL if we don't use expanding + ring search */ if (!rt) { - dest_seqno = 0; + dest_seqno = 0; - if (expanding_ring_search) - ttl = TTL_START; + if (expanding_ring_search) + ttl = TTL_START; } else { - dest_seqno = rt->dest_seqno; + dest_seqno = rt->dest_seqno; - if (expanding_ring_search) { - ttl = rt->hcnt + TTL_INCREMENT; - } + if (expanding_ring_search) { + ttl = rt->hcnt + TTL_INCREMENT; + } -/* if (rt->flags & RT_INET_DEST) */ -/* flags |= RREQ_DEST_ONLY; */ + /* if (rt->flags & RT_INET_DEST) */ + /* flags |= RREQ_DEST_ONLY; */ - /* A routing table entry waiting for a RREP should not be expunged - before 2 * NET_TRAVERSAL_TIME... */ - if (timeval_diff(&rt->rt_timer.timeout, &now) < - (2 * NET_TRAVERSAL_TIME)) - rt_table_update_timeout(rt, 2 * NET_TRAVERSAL_TIME); + /* A routing table entry waiting for a RREP should not be expunged + before 2 * NET_TRAVERSAL_TIME... */ + if (timeval_diff(&rt->rt_timer.timeout, &now) < + (2 * NET_TRAVERSAL_TIME)) + rt_table_update_timeout(rt, 2 * NET_TRAVERSAL_TIME); } rreq_send(dest_addr, dest_seqno, ttl, flags); @@ -467,9 +467,9 @@ void NS_CLASS rreq_route_discovery(struct in_addr dest_addr, u_int8_t flags, /* Set a timer for this RREQ */ if (expanding_ring_search) - timer_set_timeout(&seek_entry->seek_timer, RING_TRAVERSAL_TIME); + timer_set_timeout(&seek_entry->seek_timer, RING_TRAVERSAL_TIME); else - timer_set_timeout(&seek_entry->seek_timer, NET_TRAVERSAL_TIME); + timer_set_timeout(&seek_entry->seek_timer, NET_TRAVERSAL_TIME); DEBUG(LOG_DEBUG, 0, "Seeking %s ttl=%d", ip_to_str(dest_addr), ttl); @@ -477,8 +477,8 @@ void NS_CLASS rreq_route_discovery(struct in_addr dest_addr, u_int8_t flags, } /* Local repair is very similar to route discovery... */ -void NS_CLASS rreq_local_repair(rt_table_t * rt, struct in_addr src_addr, - struct ip_data *ipd) +void NS_CLASS rreq_local_repair(rt_table_t *rt, struct in_addr src_addr, + struct ip_data *ipd) { struct timeval now; seek_list_t *seek_entry; @@ -487,13 +487,13 @@ void NS_CLASS rreq_local_repair(rt_table_t * rt, struct in_addr src_addr, u_int8_t flags = 0; if (!rt) - return; + return; if (seek_list_find(rt->dest_addr)) - return; + return; if (!(rt->flags & RT_REPAIR)) - return; + return; gettimeofday(&now, NULL); @@ -506,42 +506,40 @@ void NS_CLASS rreq_local_repair(rt_table_t * rt, struct in_addr src_addr, src_entry = rt_table_find(src_addr); if (src_entry) - ttl = (int) (max(rt->hcnt, 0.5 * src_entry->hcnt) + LOCAL_ADD_TTL); + ttl = (int)(max(rt->hcnt, 0.5 * src_entry->hcnt) + LOCAL_ADD_TTL); else - ttl = rt->hcnt + LOCAL_ADD_TTL; + ttl = rt->hcnt + LOCAL_ADD_TTL; DEBUG(LOG_DEBUG, 0, "%s, rreq ttl=%d, dest_hcnt=%d", - ip_to_str(rt->dest_addr), ttl, rt->hcnt); + ip_to_str(rt->dest_addr), ttl, rt->hcnt); /* Reset the timeout handler, was probably previously local_repair_timeout */ rt->rt_timer.handler = &NS_CLASS route_expire_timeout; if (timeval_diff(&rt->rt_timer.timeout, &now) < (2 * NET_TRAVERSAL_TIME)) - rt_table_update_timeout(rt, 2 * NET_TRAVERSAL_TIME); - + rt_table_update_timeout(rt, 2 * NET_TRAVERSAL_TIME); rreq_send(rt->dest_addr, rt->dest_seqno, ttl, flags); /* Remember that we are seeking this destination and setup the timers */ - seek_entry = seek_list_insert(rt->dest_addr, rt->dest_seqno, - ttl, flags, ipd); + seek_entry = + seek_list_insert(rt->dest_addr, rt->dest_seqno, ttl, flags, ipd); if (expanding_ring_search) - timer_set_timeout(&seek_entry->seek_timer, - 2 * ttl * NODE_TRAVERSAL_TIME); + timer_set_timeout(&seek_entry->seek_timer, + 2 * ttl * NODE_TRAVERSAL_TIME); else - timer_set_timeout(&seek_entry->seek_timer, NET_TRAVERSAL_TIME); + timer_set_timeout(&seek_entry->seek_timer, NET_TRAVERSAL_TIME); DEBUG(LOG_DEBUG, 0, "Seeking %s ttl=%d", ip_to_str(rt->dest_addr), ttl); return; } -NS_STATIC struct rreq_record *NS_CLASS rreq_record_insert(struct in_addr - orig_addr, - u_int32_t rreq_id) +NS_STATIC struct rreq_record *NS_CLASS +rreq_record_insert(struct in_addr orig_addr, u_int32_t rreq_id) { struct rreq_record *rec; @@ -550,12 +548,12 @@ NS_STATIC struct rreq_record *NS_CLASS rreq_record_insert(struct in_addr /* If already buffered, should we update the timer??? */ if (rec) - return rec; + return rec; - if ((rec = - (struct rreq_record *) malloc(sizeof(struct rreq_record))) == NULL) { - fprintf(stderr, "Malloc failed!!!\n"); - exit(-1); + if ((rec = (struct rreq_record *)malloc(sizeof(struct rreq_record))) == + NULL) { + fprintf(stderr, "Malloc failed!!!\n"); + exit(-1); } rec->orig_addr = orig_addr; rec->rreq_id = rreq_id; @@ -565,30 +563,30 @@ NS_STATIC struct rreq_record *NS_CLASS rreq_record_insert(struct in_addr list_add(&rreq_records, &rec->l); DEBUG(LOG_INFO, 0, "Buffering RREQ %s rreq_id=%lu time=%u", - ip_to_str(orig_addr), rreq_id, PATH_DISCOVERY_TIME); + ip_to_str(orig_addr), rreq_id, PATH_DISCOVERY_TIME); timer_set_timeout(&rec->rec_timer, PATH_DISCOVERY_TIME); return rec; } -NS_STATIC struct rreq_record *NS_CLASS rreq_record_find(struct in_addr - orig_addr, - u_int32_t rreq_id) +NS_STATIC struct rreq_record *NS_CLASS +rreq_record_find(struct in_addr orig_addr, u_int32_t rreq_id) { list_t *pos; - list_foreach(pos, &rreq_records) { - struct rreq_record *rec = (struct rreq_record *) pos; - if (rec->orig_addr.s_addr == orig_addr.s_addr && - (rec->rreq_id == rreq_id)) - return rec; + list_foreach(pos, &rreq_records) + { + struct rreq_record *rec = (struct rreq_record *)pos; + if (rec->orig_addr.s_addr == orig_addr.s_addr && + (rec->rreq_id == rreq_id)) + return rec; } return NULL; } void NS_CLASS rreq_record_timeout(void *arg) { - struct rreq_record *rec = (struct rreq_record *) arg; + struct rreq_record *rec = (struct rreq_record *)arg; list_detach(&rec->l); free(rec); @@ -604,11 +602,11 @@ struct blacklist *NS_CLASS rreq_blacklist_insert(struct in_addr dest_addr) /* If already buffered, should we update the timer??? */ if (bl) - return bl; + return bl; - if ((bl = (struct blacklist *) malloc(sizeof(struct blacklist))) == NULL) { - fprintf(stderr, "Malloc failed!!!\n"); - exit(-1); + if ((bl = (struct blacklist *)malloc(sizeof(struct blacklist))) == NULL) { + fprintf(stderr, "Malloc failed!!!\n"); + exit(-1); } bl->dest_addr.s_addr = dest_addr.s_addr; @@ -624,11 +622,12 @@ struct blacklist *NS_CLASS rreq_blacklist_find(struct in_addr dest_addr) { list_t *pos; - list_foreach(pos, &rreq_blacklist) { - struct blacklist *bl = (struct blacklist *) pos; + list_foreach(pos, &rreq_blacklist) + { + struct blacklist *bl = (struct blacklist *)pos; - if (bl->dest_addr.s_addr == dest_addr.s_addr) - return bl; + if (bl->dest_addr.s_addr == dest_addr.s_addr) + return bl; } return NULL; } @@ -636,7 +635,7 @@ struct blacklist *NS_CLASS rreq_blacklist_find(struct in_addr dest_addr) void NS_CLASS rreq_blacklist_timeout(void *arg) { - struct blacklist *bl = (struct blacklist *) arg; + struct blacklist *bl = (struct blacklist *)arg; list_detach(&bl->l); free(bl); diff --git a/aodv_rreq.h b/aodv_rreq.h index 62a5e3a..ec38e7f 100644 --- a/aodv_rreq.h +++ b/aodv_rreq.h @@ -16,8 +16,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, - * + * Authors: Erik Nordström, + * * *****************************************************************************/ #ifndef _AODV_RREQ_H @@ -27,29 +27,29 @@ #include #include "defs.h" -#include "seek_list.h" #include "routing_table.h" +#include "seek_list.h" /* RREQ Flags: */ -#define RREQ_JOIN 0x1 -#define RREQ_REPAIR 0x2 -#define RREQ_GRATUITOUS 0x4 -#define RREQ_DEST_ONLY 0x8 +#define RREQ_JOIN 0x1 +#define RREQ_REPAIR 0x2 +#define RREQ_GRATUITOUS 0x4 +#define RREQ_DEST_ONLY 0x8 typedef struct { u_int8_t type; #if defined(__LITTLE_ENDIAN) - u_int8_t res1:4; - u_int8_t d:1; - u_int8_t g:1; - u_int8_t r:1; - u_int8_t j:1; + u_int8_t res1 : 4; + u_int8_t d : 1; + u_int8_t g : 1; + u_int8_t r : 1; + u_int8_t j : 1; #elif defined(__BIG_ENDIAN) - u_int8_t j:1; /* Join flag (multicast) */ - u_int8_t r:1; /* Repair flag */ - u_int8_t g:1; /* Gratuitous RREP flag */ - u_int8_t d:1; /* Destination only respond */ - u_int8_t res1:4; + u_int8_t j : 1; /* Join flag (multicast) */ + u_int8_t r : 1; /* Repair flag */ + u_int8_t g : 1; /* Gratuitous RREP flag */ + u_int8_t d : 1; /* Destination only respond */ + u_int8_t res1 : 4; #else #error "Adjust your defines" #endif @@ -67,8 +67,8 @@ typedef struct { /* A data structure to buffer information about received RREQ's */ struct rreq_record { list_t l; - struct in_addr orig_addr; /* Source of the RREQ */ - u_int32_t rreq_id; /* RREQ's broadcast ID */ + struct in_addr orig_addr; /* Source of the RREQ */ + u_int32_t rreq_id; /* RREQ's broadcast ID */ struct timer rec_timer; }; @@ -77,32 +77,32 @@ struct blacklist { struct in_addr dest_addr; struct timer bl_timer; }; -#endif /* NS_NO_GLOBALS */ +#endif /* NS_NO_GLOBALS */ #ifndef NS_NO_DECLARATIONS RREQ *rreq_create(u_int8_t flags, struct in_addr dest_addr, - u_int32_t dest_seqno, struct in_addr orig_addr); + u_int32_t dest_seqno, struct in_addr orig_addr); void rreq_send(struct in_addr dest_addr, u_int32_t dest_seqno, int ttl, - u_int8_t flags); -void rreq_forward(RREQ * rreq, int size, int ttl); -void rreq_process(RREQ * rreq, int rreqlen, struct in_addr ip_src, - struct in_addr ip_dst, int ip_ttl, unsigned int ifindex); + u_int8_t flags); +void rreq_forward(RREQ *rreq, int size, int ttl); +void rreq_process(RREQ *rreq, int rreqlen, struct in_addr ip_src, + struct in_addr ip_dst, int ip_ttl, unsigned int ifindex); void rreq_route_discovery(struct in_addr dest_addr, u_int8_t flags, - struct ip_data *ipd); + struct ip_data *ipd); void rreq_record_timeout(void *arg); struct blacklist *rreq_blacklist_insert(struct in_addr dest_addr); void rreq_blacklist_timeout(void *arg); -void rreq_local_repair(rt_table_t * rt, struct in_addr src_addr, - struct ip_data *ipd); +void rreq_local_repair(rt_table_t *rt, struct in_addr src_addr, + struct ip_data *ipd); #ifdef NS_PORT struct rreq_record *rreq_record_insert(struct in_addr orig_addr, - u_int32_t rreq_id); + u_int32_t rreq_id); struct rreq_record *rreq_record_find(struct in_addr orig_addr, - u_int32_t rreq_id); + u_int32_t rreq_id); struct blacklist *rreq_blacklist_find(struct in_addr dest_addr); -#endif /* NS_PORT */ +#endif /* NS_PORT */ -#endif /* NS_NO_DECLARATIONS */ +#endif /* NS_NO_DECLARATIONS */ -#endif /* AODV_RREQ_H */ +#endif /* AODV_RREQ_H */ diff --git a/aodv_socket.c b/aodv_socket.c index 20da688..a60dce4 100644 --- a/aodv_socket.c +++ b/aodv_socket.c @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, + * Authors: Erik Nordström, * *****************************************************************************/ @@ -25,25 +25,25 @@ #ifdef NS_PORT #include "ns-2/aodv-uu.h" #else -#include -#include -#include -#include -#include "aodv_socket.h" -#include "timer_queue.h" -#include "aodv_rreq.h" -#include "aodv_rerr.h" -#include "aodv_rrep.h" -#include "params.h" #include "aodv_hello.h" #include "aodv_neighbor.h" +#include "aodv_rerr.h" +#include "aodv_rrep.h" +#include "aodv_rreq.h" +#include "aodv_socket.h" #include "debug.h" #include "defs.h" +#include "params.h" +#include "timer_queue.h" +#include +#include +#include +#include -#endif /* NS_PORT */ +#endif /* NS_PORT */ #ifndef NS_PORT -#define SO_RECVBUF_SIZE 256*1024 +#define SO_RECVBUF_SIZE 256 * 1024 static char recv_buf[RECV_BUF_SIZE]; static char send_buf[SEND_BUF_SIZE]; @@ -56,14 +56,14 @@ static void aodv_socket_read(int fd); * CMSG_NXTHDR() routine... redefining it here */ static struct cmsghdr *__cmsg_nxthdr_fix(void *__ctl, size_t __size, - struct cmsghdr *__cmsg) + struct cmsghdr *__cmsg) { struct cmsghdr *__ptr; - __ptr = (struct cmsghdr *) (((unsigned char *) __cmsg) + - CMSG_ALIGN(__cmsg->cmsg_len)); - if ((unsigned long) ((char *) (__ptr + 1) - (char *) __ctl) > __size) - return NULL; + __ptr = (struct cmsghdr *)(((unsigned char *)__cmsg) + + CMSG_ALIGN(__cmsg->cmsg_len)); + if ((unsigned long)((char *)(__ptr + 1) - (char *)__ctl) > __size) + return NULL; return __ptr; } @@ -73,8 +73,7 @@ struct cmsghdr *cmsg_nxthdr_fix(struct msghdr *__msg, struct cmsghdr *__cmsg) return __cmsg_nxthdr_fix(__msg->msg_control, __msg->msg_controllen, __cmsg); } -#endif /* NS_PORT */ - +#endif /* NS_PORT */ void NS_CLASS aodv_socket_init() { @@ -90,135 +89,134 @@ void NS_CLASS aodv_socket_init() /* Create a UDP socket */ if (this_host.nif == 0) { - fprintf(stderr, "No interfaces configured\n"); - exit(-1); + fprintf(stderr, "No interfaces configured\n"); + exit(-1); } /* Open a socket for every AODV enabled interface */ for (i = 0; i < MAX_NR_INTERFACES; i++) { - if (!DEV_NR(i).enabled) - continue; - - /* AODV socket */ - DEV_NR(i).sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (DEV_NR(i).sock < 0) { - perror(""); - exit(-1); - } + if (!DEV_NR(i).enabled) + continue; + + /* AODV socket */ + DEV_NR(i).sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (DEV_NR(i).sock < 0) { + perror(""); + exit(-1); + } #ifdef CONFIG_GATEWAY - /* Data packet send socket */ - DEV_NR(i).psock = socket(PF_INET, SOCK_RAW, IPPROTO_RAW); + /* Data packet send socket */ + DEV_NR(i).psock = socket(PF_INET, SOCK_RAW, IPPROTO_RAW); - if (DEV_NR(i).psock < 0) { - perror(""); - exit(-1); - } + if (DEV_NR(i).psock < 0) { + perror(""); + exit(-1); + } #endif - /* Bind the socket to the AODV port number */ - memset(&aodv_addr, 0, sizeof(aodv_addr)); - aodv_addr.sin_family = AF_INET; - aodv_addr.sin_port = htons(AODV_PORT); - aodv_addr.sin_addr.s_addr = htonl(INADDR_ANY); - - retval = bind(DEV_NR(i).sock, (struct sockaddr *) &aodv_addr, - sizeof(struct sockaddr)); - - if (retval < 0) { - perror("Bind failed "); - exit(-1); - } - if (setsockopt(DEV_NR(i).sock, SOL_SOCKET, SO_BROADCAST, - &on, sizeof(int)) < 0) { - perror("SO_BROADCAST failed "); - exit(-1); - } - - memset(&ifr, 0, sizeof(struct ifreq)); - strcpy(ifr.ifr_name, DEV_NR(i).ifname); - - if (setsockopt(DEV_NR(i).sock, SOL_SOCKET, SO_BINDTODEVICE, - &ifr, sizeof(ifr)) < 0) { - fprintf(stderr, "SO_BINDTODEVICE failed for %s", DEV_NR(i).ifname); - perror(" "); - exit(-1); - } - - if (setsockopt(DEV_NR(i).sock, SOL_SOCKET, SO_PRIORITY, - &tos, sizeof(int)) < 0) { - perror("Setsockopt SO_PRIORITY failed "); - exit(-1); - } - - if (setsockopt(DEV_NR(i).sock, SOL_IP, IP_RECVTTL, - &on, sizeof(int)) < 0) { - perror("Setsockopt IP_RECVTTL failed "); - exit(-1); - } - - if (setsockopt(DEV_NR(i).sock, SOL_IP, IP_PKTINFO, - &on, sizeof(int)) < 0) { - perror("Setsockopt IP_PKTINFO failed "); - exit(-1); - } + /* Bind the socket to the AODV port number */ + memset(&aodv_addr, 0, sizeof(aodv_addr)); + aodv_addr.sin_family = AF_INET; + aodv_addr.sin_port = htons(AODV_PORT); + aodv_addr.sin_addr.s_addr = htonl(INADDR_ANY); + + retval = bind(DEV_NR(i).sock, (struct sockaddr *)&aodv_addr, + sizeof(struct sockaddr)); + + if (retval < 0) { + perror("Bind failed "); + exit(-1); + } + if (setsockopt(DEV_NR(i).sock, SOL_SOCKET, SO_BROADCAST, &on, + sizeof(int)) < 0) { + perror("SO_BROADCAST failed "); + exit(-1); + } + + memset(&ifr, 0, sizeof(struct ifreq)); + strcpy(ifr.ifr_name, DEV_NR(i).ifname); + + if (setsockopt(DEV_NR(i).sock, SOL_SOCKET, SO_BINDTODEVICE, &ifr, + sizeof(ifr)) < 0) { + fprintf(stderr, "SO_BINDTODEVICE failed for %s", DEV_NR(i).ifname); + perror(" "); + exit(-1); + } + + if (setsockopt(DEV_NR(i).sock, SOL_SOCKET, SO_PRIORITY, &tos, + sizeof(int)) < 0) { + perror("Setsockopt SO_PRIORITY failed "); + exit(-1); + } + + if (setsockopt(DEV_NR(i).sock, SOL_IP, IP_RECVTTL, &on, sizeof(int)) < + 0) { + perror("Setsockopt IP_RECVTTL failed "); + exit(-1); + } + + if (setsockopt(DEV_NR(i).sock, SOL_IP, IP_PKTINFO, &on, sizeof(int)) < + 0) { + perror("Setsockopt IP_PKTINFO failed "); + exit(-1); + } #ifdef CONFIG_GATEWAY - if (setsockopt(DEV_NR(i).psock, SOL_SOCKET, SO_BINDTODEVICE, - &ifr, sizeof(ifr)) < 0) { - fprintf(stderr, "SO_BINDTODEVICE failed for %s", DEV_NR(i).ifname); - perror(" "); - exit(-1); - } - - bufsize = 4 * 65535; - - if (setsockopt(DEV_NR(i).psock, SOL_SOCKET, SO_SNDBUF, - (char *) &bufsize, optlen) < 0) { - DEBUG(LOG_NOTICE, 0, "Could not set send socket buffer size"); - } - if (getsockopt(DEV_NR(i).psock, SOL_SOCKET, SO_SNDBUF, - (char *) &bufsize, &optlen) == 0) { - alog(LOG_NOTICE, 0, __FUNCTION__, - "RAW send socket buffer size set to %d", bufsize); - } + if (setsockopt(DEV_NR(i).psock, SOL_SOCKET, SO_BINDTODEVICE, &ifr, + sizeof(ifr)) < 0) { + fprintf(stderr, "SO_BINDTODEVICE failed for %s", DEV_NR(i).ifname); + perror(" "); + exit(-1); + } + + bufsize = 4 * 65535; + + if (setsockopt(DEV_NR(i).psock, SOL_SOCKET, SO_SNDBUF, (char *)&bufsize, + optlen) < 0) { + DEBUG(LOG_NOTICE, 0, "Could not set send socket buffer size"); + } + if (getsockopt(DEV_NR(i).psock, SOL_SOCKET, SO_SNDBUF, (char *)&bufsize, + &optlen) == 0) { + alog(LOG_NOTICE, 0, __FUNCTION__, + "RAW send socket buffer size set to %d", bufsize); + } #endif - /* Set max allowable receive buffer size... */ - for (;; bufsize -= 1024) { - if (setsockopt(DEV_NR(i).sock, SOL_SOCKET, SO_RCVBUF, - (char *) &bufsize, optlen) == 0) { - alog(LOG_NOTICE, 0, __FUNCTION__, - "Receive buffer size set to %d", bufsize); - break; - } - if (bufsize < RECV_BUF_SIZE) { - alog(LOG_ERR, 0, __FUNCTION__, - "Could not set receive buffer size"); - exit(-1); - } - } - - retval = attach_callback_func(DEV_NR(i).sock, aodv_socket_read); - - if (retval < 0) { - perror("register input handler failed "); - exit(-1); - } + /* Set max allowable receive buffer size... */ + for (;; bufsize -= 1024) { + if (setsockopt(DEV_NR(i).sock, SOL_SOCKET, SO_RCVBUF, + (char *)&bufsize, optlen) == 0) { + alog(LOG_NOTICE, 0, __FUNCTION__, + "Receive buffer size set to %d", bufsize); + break; + } + if (bufsize < RECV_BUF_SIZE) { + alog(LOG_ERR, 0, __FUNCTION__, + "Could not set receive buffer size"); + exit(-1); + } + } + + retval = attach_callback_func(DEV_NR(i).sock, aodv_socket_read); + + if (retval < 0) { + perror("register input handler failed "); + exit(-1); + } } -#endif /* NS_PORT */ +#endif /* NS_PORT */ num_rreq = 0; num_rerr = 0; } -void NS_CLASS aodv_socket_process_packet(AODV_msg * aodv_msg, int len, - struct in_addr src, - struct in_addr dst, - int ttl, unsigned int ifindex) +void NS_CLASS aodv_socket_process_packet(AODV_msg *aodv_msg, int len, + struct in_addr src, struct in_addr dst, + int ttl, unsigned int ifindex) { /* If this was a HELLO message... Process as HELLO. */ if ((aodv_msg->type == AODV_RREP && ttl == 1 && - dst.s_addr == AODV_BROADCAST)) { - hello_process((RREP *) aodv_msg, len, ifindex); - return; + dst.s_addr == AODV_BROADCAST)) { + hello_process((RREP *)aodv_msg, len, ifindex); + return; } /* Make sure we add/update neighbors */ @@ -229,29 +227,29 @@ void NS_CLASS aodv_socket_process_packet(AODV_msg * aodv_msg, int len, switch (aodv_msg->type) { case AODV_RREQ: - rreq_process((RREQ *) aodv_msg, len, src, dst, ttl, ifindex); - break; + rreq_process((RREQ *)aodv_msg, len, src, dst, ttl, ifindex); + break; case AODV_RREP: - DEBUG(LOG_DEBUG, 0, "Received RREP"); - rrep_process((RREP *) aodv_msg, len, src, dst, ttl, ifindex); - break; + DEBUG(LOG_DEBUG, 0, "Received RREP"); + rrep_process((RREP *)aodv_msg, len, src, dst, ttl, ifindex); + break; case AODV_RERR: - DEBUG(LOG_DEBUG, 0, "Received RERR"); - rerr_process((RERR *) aodv_msg, len, src, dst); - break; + DEBUG(LOG_DEBUG, 0, "Received RERR"); + rerr_process((RERR *)aodv_msg, len, src, dst); + break; case AODV_RREP_ACK: - DEBUG(LOG_DEBUG, 0, "Received RREP_ACK"); - rrep_ack_process((RREP_ack *) aodv_msg, len, src, dst); - break; + DEBUG(LOG_DEBUG, 0, "Received RREP_ACK"); + rrep_ack_process((RREP_ack *)aodv_msg, len, src, dst); + break; default: - alog(LOG_WARNING, 0, __FUNCTION__, - "Unknown msg type %u rcvd from %s to %s", aodv_msg->type, - ip_to_str(src), ip_to_str(dst)); + alog(LOG_WARNING, 0, __FUNCTION__, + "Unknown msg type %u rcvd from %s to %s", aodv_msg->type, + ip_to_str(src), ip_to_str(dst)); } } #ifdef NS_PORT -void NS_CLASS recvAODVUUPacket(Packet * p) +void NS_CLASS recvAODVUUPacket(Packet *p) { int len, i, ttl = 0; struct in_addr src, dst; @@ -264,7 +262,7 @@ void NS_CLASS recvAODVUUPacket(Packet * p) len = ch->size() - IP_HDR_LEN; ttl = ih->ttl(); - AODV_msg *aodv_msg = (AODV_msg *) recv_buf; + AODV_msg *aodv_msg = (AODV_msg *)recv_buf; /* Only handle AODVUU packets */ assert(ch->ptype() == PT_AODVUU); @@ -280,10 +278,9 @@ void NS_CLASS recvAODVUUPacket(Packet * p) /* Ignore messages generated locally */ for (i = 0; i < MAX_NR_INTERFACES; i++) - if (this_host.devs[i].enabled && - memcmp(&src, &this_host.devs[i].ipaddr, - sizeof(struct in_addr)) == 0) - return; + if (this_host.devs[i].enabled && memcmp(&src, &this_host.devs[i].ipaddr, + sizeof(struct in_addr)) == 0) + return; aodv_socket_process_packet(aodv_msg, len, src, dst, ttl, NS_IFINDEX); } @@ -298,7 +295,7 @@ static void aodv_socket_read(int fd) struct cmsghdr *cmsg; struct iovec iov; char ctrlbuf[CMSG_SPACE(sizeof(int)) + - CMSG_SPACE(sizeof(struct in_pktinfo))]; + CMSG_SPACE(sizeof(struct in_pktinfo))]; struct sockaddr_in src_addr; dst.s_addr = -1; @@ -315,56 +312,54 @@ static void aodv_socket_read(int fd) len = recvmsg(fd, &msgh, 0); if (len < 0) { - alog(LOG_WARNING, 0, __FUNCTION__, "receive ERROR len=%d!", len); - return; + alog(LOG_WARNING, 0, __FUNCTION__, "receive ERROR len=%d!", len); + return; } src.s_addr = src_addr.sin_addr.s_addr; /* Get the ttl and destination address from the control message */ for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL; - cmsg = CMSG_NXTHDR_FIX(&msgh, cmsg)) { - if (cmsg->cmsg_level == SOL_IP) { - switch (cmsg->cmsg_type) { - case IP_TTL: - ttl = *(CMSG_DATA(cmsg)); - break; - case IP_PKTINFO: - { - struct in_pktinfo *pi = (struct in_pktinfo *)CMSG_DATA(cmsg); - dst.s_addr = pi->ipi_addr.s_addr; - } - } - } + cmsg = CMSG_NXTHDR_FIX(&msgh, cmsg)) { + if (cmsg->cmsg_level == SOL_IP) { + switch (cmsg->cmsg_type) { + case IP_TTL: + ttl = *(CMSG_DATA(cmsg)); + break; + case IP_PKTINFO: { + struct in_pktinfo *pi = (struct in_pktinfo *)CMSG_DATA(cmsg); + dst.s_addr = pi->ipi_addr.s_addr; + } + } + } } if (ttl < 0) { - DEBUG(LOG_DEBUG, 0, "No TTL, packet ignored!"); - return; + DEBUG(LOG_DEBUG, 0, "No TTL, packet ignored!"); + return; } /* Ignore messages generated locally */ for (i = 0; i < MAX_NR_INTERFACES; i++) - if (this_host.devs[i].enabled && - memcmp(&src, &this_host.devs[i].ipaddr, - sizeof(struct in_addr)) == 0) - return; + if (this_host.devs[i].enabled && memcmp(&src, &this_host.devs[i].ipaddr, + sizeof(struct in_addr)) == 0) + return; - aodv_msg = (AODV_msg *) recv_buf; + aodv_msg = (AODV_msg *)recv_buf; dev = devfromsock(fd); if (!dev) { - DEBUG(LOG_ERR, 0, "Could not get device info!\n"); - return; + DEBUG(LOG_ERR, 0, "Could not get device info!\n"); + return; } aodv_socket_process_packet(aodv_msg, len, src, dst, ttl, dev->ifindex); } -#endif /* NS_PORT */ +#endif /* NS_PORT */ -void NS_CLASS aodv_socket_send(AODV_msg * aodv_msg, struct in_addr dst, - int len, u_int8_t ttl, struct dev_info *dev) +void NS_CLASS aodv_socket_send(AODV_msg *aodv_msg, struct in_addr dst, int len, + u_int8_t ttl, struct dev_info *dev) { int retval = 0; struct timeval now; @@ -375,7 +370,7 @@ void NS_CLASS aodv_socket_send(AODV_msg * aodv_msg, struct in_addr dst, struct sockaddr_in dst_addr; if (wait_on_reboot && aodv_msg->type == AODV_RREP) - return; + return; memset(&dst_addr, 0, sizeof(dst_addr)); dst_addr.sin_family = AF_INET; @@ -384,8 +379,8 @@ void NS_CLASS aodv_socket_send(AODV_msg * aodv_msg, struct in_addr dst, /* Set ttl */ if (setsockopt(dev->sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl)) < 0) { - alog(LOG_WARNING, 0, __FUNCTION__, "ERROR setting ttl!"); - return; + alog(LOG_WARNING, 0, __FUNCTION__, "ERROR setting ttl!"); + return; } #else @@ -399,7 +394,7 @@ void NS_CLASS aodv_socket_send(AODV_msg * aodv_msg, struct in_addr dst, /* If we are in waiting phase after reboot, don't send any RREPs */ if (wait_on_reboot && aodv_msg->type == AODV_RREP) - return; + return; /* NS_PORT: Don't allocate packet until now. Otherwise packet uid @@ -423,11 +418,11 @@ void NS_CLASS aodv_socket_send(AODV_msg * aodv_msg, struct in_addr dst, ch->size() += len + IP_HDR_LEN; ch->iface() = -2; ch->error() = 0; - ch->prev_hop_ = (nsaddr_t) dev->ipaddr.s_addr; + ch->prev_hop_ = (nsaddr_t)dev->ipaddr.s_addr; // Set IP header fields - ih->saddr() = (nsaddr_t) dev->ipaddr.s_addr; - ih->daddr() = (nsaddr_t) dst.s_addr; + ih->saddr() = (nsaddr_t)dev->ipaddr.s_addr; + ih->daddr() = (nsaddr_t)dst.s_addr; ih->ttl() = ttl; // Note: Port number for routing agents, not AODV port number! @@ -436,7 +431,7 @@ void NS_CLASS aodv_socket_send(AODV_msg * aodv_msg, struct in_addr dst, // Fake success retval = len; -#endif /* NS_PORT */ +#endif /* NS_PORT */ /* If rate limiting is enabled, check if we are sending either a RREQ or a RERR. In that case, drop the outgoing control packet @@ -445,101 +440,101 @@ void NS_CLASS aodv_socket_send(AODV_msg * aodv_msg, struct in_addr dst, if (ratelimit) { - gettimeofday(&now, NULL); + gettimeofday(&now, NULL); - switch (aodv_msg->type) { - case AODV_RREQ: - if (num_rreq == (RREQ_RATELIMIT - 1)) { - if (timeval_diff(&now, &rreq_ratel[0]) < 1000) { - DEBUG(LOG_DEBUG, 0, "RATELIMIT: Dropping RREQ %ld ms", - timeval_diff(&now, &rreq_ratel[0])); + switch (aodv_msg->type) { + case AODV_RREQ: + if (num_rreq == (RREQ_RATELIMIT - 1)) { + if (timeval_diff(&now, &rreq_ratel[0]) < 1000) { + DEBUG(LOG_DEBUG, 0, "RATELIMIT: Dropping RREQ %ld ms", + timeval_diff(&now, &rreq_ratel[0])); #ifdef NS_PORT - Packet::free(p); + Packet::free(p); #endif - return; - } else { - memmove(rreq_ratel, &rreq_ratel[1], - sizeof(struct timeval) * (num_rreq - 1)); - memcpy(&rreq_ratel[num_rreq - 1], &now, - sizeof(struct timeval)); - } - } else { - memcpy(&rreq_ratel[num_rreq], &now, sizeof(struct timeval)); - num_rreq++; - } - break; - case AODV_RERR: - if (num_rerr == (RERR_RATELIMIT - 1)) { - if (timeval_diff(&now, &rerr_ratel[0]) < 1000) { - DEBUG(LOG_DEBUG, 0, "RATELIMIT: Dropping RERR %ld ms", - timeval_diff(&now, &rerr_ratel[0])); + return; + } else { + memmove(rreq_ratel, &rreq_ratel[1], + sizeof(struct timeval) * (num_rreq - 1)); + memcpy(&rreq_ratel[num_rreq - 1], &now, + sizeof(struct timeval)); + } + } else { + memcpy(&rreq_ratel[num_rreq], &now, sizeof(struct timeval)); + num_rreq++; + } + break; + case AODV_RERR: + if (num_rerr == (RERR_RATELIMIT - 1)) { + if (timeval_diff(&now, &rerr_ratel[0]) < 1000) { + DEBUG(LOG_DEBUG, 0, "RATELIMIT: Dropping RERR %ld ms", + timeval_diff(&now, &rerr_ratel[0])); #ifdef NS_PORT - Packet::free(p); + Packet::free(p); #endif - return; - } else { - memmove(rerr_ratel, &rerr_ratel[1], - sizeof(struct timeval) * (num_rerr - 1)); - memcpy(&rerr_ratel[num_rerr - 1], &now, - sizeof(struct timeval)); - } - } else { - memcpy(&rerr_ratel[num_rerr], &now, sizeof(struct timeval)); - num_rerr++; - } - break; - } + return; + } else { + memmove(rerr_ratel, &rerr_ratel[1], + sizeof(struct timeval) * (num_rerr - 1)); + memcpy(&rerr_ratel[num_rerr - 1], &now, + sizeof(struct timeval)); + } + } else { + memcpy(&rerr_ratel[num_rerr], &now, sizeof(struct timeval)); + num_rerr++; + } + break; + } } /* If we broadcast this message we update the time of last broadcast to prevent unnecessary broadcasts of HELLO msg's */ if (dst.s_addr == AODV_BROADCAST) { - gettimeofday(&this_host.bcast_time, NULL); + gettimeofday(&this_host.bcast_time, NULL); #ifdef NS_PORT - ch->addr_type() = NS_AF_NONE; + ch->addr_type() = NS_AF_NONE; - sendPacket(p, dst, 0.0); + sendPacket(p, dst, 0.0); #else - retval = sendto(dev->sock, send_buf, len, 0, - (struct sockaddr *) &dst_addr, sizeof(dst_addr)); + retval = sendto(dev->sock, send_buf, len, 0, + (struct sockaddr *)&dst_addr, sizeof(dst_addr)); - if (retval < 0) { + if (retval < 0) { - alog(LOG_WARNING, errno, __FUNCTION__, "Failed send to bc %s", - ip_to_str(dst)); - return; - } + alog(LOG_WARNING, errno, __FUNCTION__, "Failed send to bc %s", + ip_to_str(dst)); + return; + } #endif } else { #ifdef NS_PORT - ch->addr_type() = NS_AF_INET; - /* We trust the decision of next hop for all AODV messages... */ + ch->addr_type() = NS_AF_INET; + /* We trust the decision of next hop for all AODV messages... */ - if (dst.s_addr == AODV_BROADCAST) - sendPacket(p, dst, 0.001 * Random::uniform()); - else - sendPacket(p, dst, 0.0); + if (dst.s_addr == AODV_BROADCAST) + sendPacket(p, dst, 0.001 * Random::uniform()); + else + sendPacket(p, dst, 0.0); #else - retval = sendto(dev->sock, send_buf, len, 0, - (struct sockaddr *) &dst_addr, sizeof(dst_addr)); - - if (retval < 0) { - alog(LOG_WARNING, errno, __FUNCTION__, "Failed send to %s", - ip_to_str(dst)); - return; - } + retval = sendto(dev->sock, send_buf, len, 0, + (struct sockaddr *)&dst_addr, sizeof(dst_addr)); + + if (retval < 0) { + alog(LOG_WARNING, errno, __FUNCTION__, "Failed send to %s", + ip_to_str(dst)); + return; + } #endif } /* Do not print hello msgs... */ if (!(aodv_msg->type == AODV_RREP && (dst.s_addr == AODV_BROADCAST))) - DEBUG(LOG_INFO, 0, "AODV msg to %s ttl=%d size=%u", - ip_to_str(dst), ttl, retval, len); + DEBUG(LOG_INFO, 0, "AODV msg to %s ttl=%d size=%u", ip_to_str(dst), ttl, + retval, len); return; } @@ -547,14 +542,14 @@ void NS_CLASS aodv_socket_send(AODV_msg * aodv_msg, struct in_addr dst, AODV_msg *NS_CLASS aodv_socket_new_msg(void) { memset(send_buf, '\0', SEND_BUF_SIZE); - return (AODV_msg *) (send_buf); + return (AODV_msg *)(send_buf); } /* Copy an existing AODV message to the send buffer */ -AODV_msg *NS_CLASS aodv_socket_queue_msg(AODV_msg * aodv_msg, int size) +AODV_msg *NS_CLASS aodv_socket_queue_msg(AODV_msg *aodv_msg, int size) { - memcpy((char *) send_buf, aodv_msg, size); - return (AODV_msg *) send_buf; + memcpy((char *)send_buf, aodv_msg, size); + return (AODV_msg *)send_buf; } void aodv_socket_cleanup(void) @@ -563,9 +558,9 @@ void aodv_socket_cleanup(void) int i; for (i = 0; i < MAX_NR_INTERFACES; i++) { - if (!DEV_NR(i).enabled) - continue; - close(DEV_NR(i).sock); + if (!DEV_NR(i).enabled) + continue; + close(DEV_NR(i).sock); } -#endif /* NS_PORT */ +#endif /* NS_PORT */ } diff --git a/aodv_socket.h b/aodv_socket.h index 2a267d3..2402116 100644 --- a/aodv_socket.h +++ b/aodv_socket.h @@ -16,8 +16,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, - * + * Authors: Erik Nordström, + * * *****************************************************************************/ #ifndef _AODV_SOCKET_H @@ -26,8 +26,8 @@ #ifndef NS_NO_GLOBALS #include -#include "defs.h" #include "aodv_rerr.h" +#include "defs.h" #include "params.h" #define IPHDR_SIZE sizeof(struct iphdr) @@ -38,7 +38,7 @@ #define AODV_MSG_MAX_SIZE RERR_SIZE + 100 * RERR_UDEST_SIZE #define RECV_BUF_SIZE AODV_MSG_MAX_SIZE #define SEND_BUF_SIZE RECV_BUF_SIZE -#endif /* NS_NO_GLOBALS */ +#endif /* NS_NO_GLOBALS */ #ifndef NS_NO_DECLARATIONS @@ -47,21 +47,21 @@ int num_rreq; int num_rerr; void aodv_socket_init(); -void aodv_socket_send(AODV_msg * aodv_msg, struct in_addr dst, int len, - u_int8_t ttl, struct dev_info *dev); +void aodv_socket_send(AODV_msg *aodv_msg, struct in_addr dst, int len, + u_int8_t ttl, struct dev_info *dev); AODV_msg *aodv_socket_new_msg(); -AODV_msg *aodv_socket_queue_msg(AODV_msg * aodv_msg, int size); +AODV_msg *aodv_socket_queue_msg(AODV_msg *aodv_msg, int size); void aodv_socket_cleanup(void); -void aodv_socket_process_packet(AODV_msg * aodv_msg, int len, - struct in_addr src, struct in_addr dst, int ttl, - unsigned int ifindex); +void aodv_socket_process_packet(AODV_msg *aodv_msg, int len, struct in_addr src, + struct in_addr dst, int ttl, + unsigned int ifindex); #define CMSG_NXTHDR_FIX(mhdr, cmsg) cmsg_nxthdr_fix((mhdr), (cmsg)) struct cmsghdr *cmsg_nxthdr_fix(struct msghdr *__msg, struct cmsghdr *__cmsg); #ifdef NS_PORT -void recvAODVUUPacket(Packet * p); -#endif /* NS_PORT */ +void recvAODVUUPacket(Packet *p); +#endif /* NS_PORT */ -#endif /* NS_NO_DECLARATIONS */ +#endif /* NS_NO_DECLARATIONS */ -#endif /* AODV_SOCKET_H */ +#endif /* AODV_SOCKET_H */ diff --git a/aodv_timeout.c b/aodv_timeout.c index ece2c3b..aa2d32e 100644 --- a/aodv_timeout.c +++ b/aodv_timeout.c @@ -16,8 +16,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, - * + * Authors: Erik Nordström, + * * *****************************************************************************/ @@ -26,19 +26,19 @@ #ifdef NS_PORT #include "ns-2/aodv-uu.h" #else -#include "defs.h" -#include "aodv_timeout.h" -#include "aodv_socket.h" -#include "aodv_neighbor.h" -#include "aodv_rreq.h" #include "aodv_hello.h" +#include "aodv_neighbor.h" #include "aodv_rerr.h" -#include "timer_queue.h" +#include "aodv_rreq.h" +#include "aodv_socket.h" +#include "aodv_timeout.h" #include "debug.h" +#include "defs.h" +#include "nl.h" #include "params.h" #include "routing_table.h" #include "seek_list.h" -#include "nl.h" +#include "timer_queue.h" extern int expanding_ring_search, local_repair; void route_delete_timeout(void *arg); @@ -49,243 +49,235 @@ void route_delete_timeout(void *arg); void NS_CLASS route_discovery_timeout(void *arg) { - struct timeval now; - seek_list_t *seek_entry; - rt_table_t *rt, *repair_rt; - seek_entry = (seek_list_t *) arg; + struct timeval now; + seek_list_t *seek_entry; + rt_table_t *rt, *repair_rt; + seek_entry = (seek_list_t *)arg; #define TTL_VALUE seek_entry->ttl - /* Sanity check... */ - if (!seek_entry) - return; + /* Sanity check... */ + if (!seek_entry) + return; - gettimeofday(&now, NULL); + gettimeofday(&now, NULL); - DEBUG(LOG_DEBUG, 0, "%s", ip_to_str(seek_entry->dest_addr)); + DEBUG(LOG_DEBUG, 0, "%s", ip_to_str(seek_entry->dest_addr)); - if (seek_entry->reqs < RREQ_RETRIES) { + if (seek_entry->reqs < RREQ_RETRIES) { - if (expanding_ring_search) { + if (expanding_ring_search) { - if (TTL_VALUE < TTL_THRESHOLD) - TTL_VALUE += TTL_INCREMENT; - else { - TTL_VALUE = NET_DIAMETER; - seek_entry->reqs++; - } - /* Set a new timer for seeking this destination */ - timer_set_timeout(&seek_entry->seek_timer, - RING_TRAVERSAL_TIME); - } else { - seek_entry->reqs++; - timer_set_timeout(&seek_entry->seek_timer, - seek_entry->reqs * 2 * - NET_TRAVERSAL_TIME); - } - /* AODV should use a binary exponential backoff RREP waiting - time. */ - DEBUG(LOG_DEBUG, 0, "Seeking %s ttl=%d wait=%d", - ip_to_str(seek_entry->dest_addr), - TTL_VALUE, 2 * TTL_VALUE * NODE_TRAVERSAL_TIME); + if (TTL_VALUE < TTL_THRESHOLD) + TTL_VALUE += TTL_INCREMENT; + else { + TTL_VALUE = NET_DIAMETER; + seek_entry->reqs++; + } + /* Set a new timer for seeking this destination */ + timer_set_timeout(&seek_entry->seek_timer, RING_TRAVERSAL_TIME); + } else { + seek_entry->reqs++; + timer_set_timeout(&seek_entry->seek_timer, + seek_entry->reqs * 2 * NET_TRAVERSAL_TIME); + } + /* AODV should use a binary exponential backoff RREP waiting + time. */ + DEBUG(LOG_DEBUG, 0, "Seeking %s ttl=%d wait=%d", + ip_to_str(seek_entry->dest_addr), TTL_VALUE, + 2 * TTL_VALUE * NODE_TRAVERSAL_TIME); - /* A routing table entry waiting for a RREP should not be expunged - before 2 * NET_TRAVERSAL_TIME... */ - rt = rt_table_find(seek_entry->dest_addr); + /* A routing table entry waiting for a RREP should not be expunged + before 2 * NET_TRAVERSAL_TIME... */ + rt = rt_table_find(seek_entry->dest_addr); - if (rt && timeval_diff(&rt->rt_timer.timeout, &now) < - (2 * NET_TRAVERSAL_TIME)) - rt_table_update_timeout(rt, 2 * NET_TRAVERSAL_TIME); + if (rt && timeval_diff(&rt->rt_timer.timeout, &now) < + (2 * NET_TRAVERSAL_TIME)) + rt_table_update_timeout(rt, 2 * NET_TRAVERSAL_TIME); - rreq_send(seek_entry->dest_addr, seek_entry->dest_seqno, - TTL_VALUE, seek_entry->flags); + rreq_send(seek_entry->dest_addr, seek_entry->dest_seqno, TTL_VALUE, + seek_entry->flags); - } else { + } else { - DEBUG(LOG_DEBUG, 0, "NO ROUTE FOUND!"); + DEBUG(LOG_DEBUG, 0, "NO ROUTE FOUND!"); #ifdef NS_PORT - packet_queue_set_verdict(seek_entry->dest_addr, PQ_DROP); + packet_queue_set_verdict(seek_entry->dest_addr, PQ_DROP); #else - nl_send_no_route_found_msg(seek_entry->dest_addr); + nl_send_no_route_found_msg(seek_entry->dest_addr); #endif - repair_rt = rt_table_find(seek_entry->dest_addr); - - seek_list_remove(seek_entry); - - /* If this route has been in repair, then we should timeout - the route at this point. */ - if (repair_rt && (repair_rt->flags & RT_REPAIR)) { - DEBUG(LOG_DEBUG, 0, "REPAIR for %s failed!", - ip_to_str(repair_rt->dest_addr)); - local_repair_timeout(repair_rt); - } - } + repair_rt = rt_table_find(seek_entry->dest_addr); + + seek_list_remove(seek_entry); + + /* If this route has been in repair, then we should timeout + the route at this point. */ + if (repair_rt && (repair_rt->flags & RT_REPAIR)) { + DEBUG(LOG_DEBUG, 0, "REPAIR for %s failed!", + ip_to_str(repair_rt->dest_addr)); + local_repair_timeout(repair_rt); + } + } } void NS_CLASS local_repair_timeout(void *arg) { - rt_table_t *rt; - struct in_addr rerr_dest; - RERR *rerr = NULL; + rt_table_t *rt; + struct in_addr rerr_dest; + RERR *rerr = NULL; - rt = (rt_table_t *) arg; + rt = (rt_table_t *)arg; - if (!rt) - return; + if (!rt) + return; - rerr_dest.s_addr = AODV_BROADCAST; /* Default destination */ + rerr_dest.s_addr = AODV_BROADCAST; /* Default destination */ - /* Unset the REPAIR flag */ - rt->flags &= ~RT_REPAIR; + /* Unset the REPAIR flag */ + rt->flags &= ~RT_REPAIR; #ifndef NS_PORT - nl_send_del_route_msg(rt->dest_addr, rt->next_hop, rt->hcnt); + nl_send_del_route_msg(rt->dest_addr, rt->next_hop, rt->hcnt); #endif - /* Route should already be invalidated. */ - - if (rt->nprec) { - - rerr = rerr_create(0, rt->dest_addr, rt->dest_seqno); - - if (rt->nprec == 1) { - rerr_dest = FIRST_PREC(rt->precursors)->neighbor; - - aodv_socket_send((AODV_msg *) rerr, rerr_dest, - RERR_CALC_SIZE(rerr), 1, - &DEV_IFINDEX(rt->ifindex)); - } else { - int i; - - for (i = 0; i < MAX_NR_INTERFACES; i++) { - if (!DEV_NR(i).enabled) - continue; - aodv_socket_send((AODV_msg *) rerr, rerr_dest, - RERR_CALC_SIZE(rerr), 1, - &DEV_NR(i)); - } - } - DEBUG(LOG_DEBUG, 0, "Sending RERR about %s to %s", - ip_to_str(rt->dest_addr), ip_to_str(rerr_dest)); - } - precursor_list_destroy(rt); - - /* Purge any packets that may be queued */ - /* packet_queue_set_verdict(rt->dest_addr, PQ_DROP); */ - - rt->rt_timer.handler = &NS_CLASS route_delete_timeout; - timer_set_timeout(&rt->rt_timer, DELETE_PERIOD); - - DEBUG(LOG_DEBUG, 0, "%s removed in %u msecs", - ip_to_str(rt->dest_addr), DELETE_PERIOD); -} + /* Route should already be invalidated. */ + + if (rt->nprec) { + + rerr = rerr_create(0, rt->dest_addr, rt->dest_seqno); + if (rt->nprec == 1) { + rerr_dest = FIRST_PREC(rt->precursors)->neighbor; + + aodv_socket_send((AODV_msg *)rerr, rerr_dest, RERR_CALC_SIZE(rerr), + 1, &DEV_IFINDEX(rt->ifindex)); + } else { + int i; + + for (i = 0; i < MAX_NR_INTERFACES; i++) { + if (!DEV_NR(i).enabled) + continue; + aodv_socket_send((AODV_msg *)rerr, rerr_dest, + RERR_CALC_SIZE(rerr), 1, &DEV_NR(i)); + } + } + DEBUG(LOG_DEBUG, 0, "Sending RERR about %s to %s", + ip_to_str(rt->dest_addr), ip_to_str(rerr_dest)); + } + precursor_list_destroy(rt); + + /* Purge any packets that may be queued */ + /* packet_queue_set_verdict(rt->dest_addr, PQ_DROP); */ + + rt->rt_timer.handler = &NS_CLASS route_delete_timeout; + timer_set_timeout(&rt->rt_timer, DELETE_PERIOD); + + DEBUG(LOG_DEBUG, 0, "%s removed in %u msecs", ip_to_str(rt->dest_addr), + DELETE_PERIOD); +} void NS_CLASS route_expire_timeout(void *arg) { - rt_table_t *rt; + rt_table_t *rt; - rt = (rt_table_t *) arg; + rt = (rt_table_t *)arg; - if (!rt) { - alog(LOG_WARNING, 0, __FUNCTION__, - "arg was NULL, ignoring timeout!"); - return; - } + if (!rt) { + alog(LOG_WARNING, 0, __FUNCTION__, "arg was NULL, ignoring timeout!"); + return; + } - DEBUG(LOG_DEBUG, 0, "Route %s DOWN, seqno=%d", - ip_to_str(rt->dest_addr), rt->dest_seqno); + DEBUG(LOG_DEBUG, 0, "Route %s DOWN, seqno=%d", ip_to_str(rt->dest_addr), + rt->dest_seqno); - if (rt->hcnt == 1) - neighbor_link_break(rt); - else { - rt_table_invalidate(rt); - precursor_list_destroy(rt); - } + if (rt->hcnt == 1) + neighbor_link_break(rt); + else { + rt_table_invalidate(rt); + precursor_list_destroy(rt); + } - return; + return; } void NS_CLASS route_delete_timeout(void *arg) { - rt_table_t *rt; + rt_table_t *rt; - rt = (rt_table_t *) arg; + rt = (rt_table_t *)arg; - /* Sanity check: */ - if (!rt) - return; + /* Sanity check: */ + if (!rt) + return; - DEBUG(LOG_DEBUG, 0, "%s", ip_to_str(rt->dest_addr)); + DEBUG(LOG_DEBUG, 0, "%s", ip_to_str(rt->dest_addr)); - rt_table_delete(rt); + rt_table_delete(rt); } /* This is called when we stop receiveing hello messages from a node. For now this is basically the same as a route timeout. */ void NS_CLASS hello_timeout(void *arg) { - rt_table_t *rt; - struct timeval now; + rt_table_t *rt; + struct timeval now; - rt = (rt_table_t *) arg; + rt = (rt_table_t *)arg; - if (!rt) - return; + if (!rt) + return; - gettimeofday(&now, NULL); + gettimeofday(&now, NULL); - DEBUG(LOG_DEBUG, 0, "LINK/HELLO FAILURE %s last HELLO: %d", - ip_to_str(rt->dest_addr), timeval_diff(&now, - &rt->last_hello_time)); + DEBUG(LOG_DEBUG, 0, "LINK/HELLO FAILURE %s last HELLO: %d", + ip_to_str(rt->dest_addr), timeval_diff(&now, &rt->last_hello_time)); - if (rt && rt->state == VALID && !(rt->flags & RT_UNIDIR)) { + if (rt && rt->state == VALID && !(rt->flags & RT_UNIDIR)) { - /* If the we can repair the route, then mark it to be - repaired.. */ - if (local_repair && rt->hcnt <= MAX_REPAIR_TTL) { - rt->flags |= RT_REPAIR; - DEBUG(LOG_DEBUG, 0, "Marking %s for REPAIR", - ip_to_str(rt->dest_addr)); + /* If the we can repair the route, then mark it to be + repaired.. */ + if (local_repair && rt->hcnt <= MAX_REPAIR_TTL) { + rt->flags |= RT_REPAIR; + DEBUG(LOG_DEBUG, 0, "Marking %s for REPAIR", + ip_to_str(rt->dest_addr)); #ifdef NS_PORT - /* Buffer pending packets from interface queue */ - interfaceQueue((nsaddr_t) rt->dest_addr.s_addr, - IFQ_BUFFER); + /* Buffer pending packets from interface queue */ + interfaceQueue((nsaddr_t)rt->dest_addr.s_addr, IFQ_BUFFER); #endif - } - neighbor_link_break(rt); - } + } + neighbor_link_break(rt); + } } void NS_CLASS rrep_ack_timeout(void *arg) { - rt_table_t *rt; + rt_table_t *rt; - /* We must be really sure here, that this entry really exists at - this point... (Though it should). */ - rt = (rt_table_t *) arg; + /* We must be really sure here, that this entry really exists at + this point... (Though it should). */ + rt = (rt_table_t *)arg; - if (!rt) - return; + if (!rt) + return; - /* When a RREP transmission fails (i.e. lack of RREP-ACK), add to - blacklist set... */ - rreq_blacklist_insert(rt->dest_addr); + /* When a RREP transmission fails (i.e. lack of RREP-ACK), add to + blacklist set... */ + rreq_blacklist_insert(rt->dest_addr); - DEBUG(LOG_DEBUG, 0, "%s", ip_to_str(rt->dest_addr)); + DEBUG(LOG_DEBUG, 0, "%s", ip_to_str(rt->dest_addr)); } void NS_CLASS wait_on_reboot_timeout(void *arg) { - *((int *) arg) = 0; + *((int *)arg) = 0; - DEBUG(LOG_DEBUG, 0, "Wait on reboot over!!"); + DEBUG(LOG_DEBUG, 0, "Wait on reboot over!!"); } #ifdef NS_PORT void NS_CLASS packet_queue_timeout(void *arg) { - packet_queue_garbage_collect(); - timer_set_timeout(&PQ.garbage_collect_timer, GARBAGE_COLLECT_TIME); + packet_queue_garbage_collect(); + timer_set_timeout(&PQ.garbage_collect_timer, GARBAGE_COLLECT_TIME); } #endif diff --git a/aodv_timeout.h b/aodv_timeout.h index 9b60bf6..b2c50de 100644 --- a/aodv_timeout.h +++ b/aodv_timeout.h @@ -16,8 +16,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, - * + * Authors: Erik Nordström, + * * *****************************************************************************/ #ifndef _AODV_TIMEOUT_H @@ -25,7 +25,7 @@ #ifndef NS_NO_GLOBALS #include "defs.h" -#endif /* NS_NO_GLOBALS */ +#endif /* NS_NO_GLOBALS */ #ifndef NS_NO_DECLARATIONS void route_delete_timeout(void *arg); @@ -36,6 +36,6 @@ void hello_timeout(void *arg); void rrep_ack_timeout(void *arg); void wait_on_reboot_timeout(void *arg); void packet_queue_timeout(void *arg); -#endif /* NS_NO_DECLARATIONS */ +#endif /* NS_NO_DECLARATIONS */ -#endif /* AODV_TIMEOUT_H */ +#endif /* AODV_TIMEOUT_H */ diff --git a/debug.c b/debug.c index 6a6c122..92dd6b8 100644 --- a/debug.c +++ b/debug.c @@ -16,31 +16,31 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, - * + * Authors: Erik Nordström, + * * *****************************************************************************/ -#include -#include -#include -#include #include +#include +#include #include +#include +#include #include #ifdef NS_PORT #include "ns-2/aodv-uu.h" #else -#include -#include "aodv_rreq.h" -#include "aodv_rrep.h" #include "aodv_rerr.h" -#include "defs.h" +#include "aodv_rrep.h" +#include "aodv_rreq.h" #include "debug.h" +#include "defs.h" #include "params.h" -#include "timer_queue.h" #include "routing_table.h" +#include "timer_queue.h" +#include #endif #ifndef NS_PORT @@ -60,33 +60,30 @@ void NS_CLASS log_init() #ifdef NS_PORT char AODV_LOG_PATH[strlen(AODV_LOG_PATH_PREFIX) + - strlen(AODV_LOG_PATH_SUFFIX) + 16]; + strlen(AODV_LOG_PATH_SUFFIX) + 16]; char AODV_RT_LOG_PATH[strlen(AODV_LOG_PATH_PREFIX) + - strlen(AODV_RT_LOG_PATH_SUFFIX) + 16]; - + strlen(AODV_RT_LOG_PATH_SUFFIX) + 16]; sprintf(AODV_LOG_PATH, "%s%d%s", AODV_LOG_PATH_PREFIX, node_id, - AODV_LOG_PATH_SUFFIX); + AODV_LOG_PATH_SUFFIX); sprintf(AODV_RT_LOG_PATH, "%s%d%s", AODV_LOG_PATH_PREFIX, node_id, - AODV_RT_LOG_PATH_SUFFIX); + AODV_RT_LOG_PATH_SUFFIX); -#endif /* NS_PORT */ +#endif /* NS_PORT */ if (log_to_file) { - if ((log_file_fd = - open(AODV_LOG_PATH, O_RDWR | O_CREAT | O_TRUNC, - S_IROTH | S_IWUSR | S_IRUSR | S_IRGRP)) < 0) { - perror("open log file failed!"); - exit(-1); - } + if ((log_file_fd = open(AODV_LOG_PATH, O_RDWR | O_CREAT | O_TRUNC, + S_IROTH | S_IWUSR | S_IRUSR | S_IRGRP)) < 0) { + perror("open log file failed!"); + exit(-1); + } } if (rt_log_interval) { - if ((log_rt_fd = - open(AODV_RT_LOG_PATH, O_RDWR | O_CREAT | O_TRUNC, - S_IROTH | S_IWUSR | S_IRUSR | S_IRGRP)) < 0) { - perror("open rt log file failed!"); - exit(-1); - } + if ((log_rt_fd = open(AODV_RT_LOG_PATH, O_RDWR | O_CREAT | O_TRUNC, + S_IROTH | S_IWUSR | S_IRUSR | S_IRGRP)) < 0) { + perror("open rt log file failed!"); + exit(-1); + } } openlog(progname, 0, LOG_DAEMON); } @@ -100,23 +97,23 @@ void NS_CLASS log_rt_table_init() void NS_CLASS log_cleanup() { if (log_to_file && log_file_fd) { - if (NS_OUTSIDE_CLASS close(log_file_fd) < 0) - fprintf(stderr, "Could not close log_file_fd!\n"); + if (NS_OUTSIDE_CLASS close(log_file_fd) < 0) + fprintf(stderr, "Could not close log_file_fd!\n"); } } void NS_CLASS write_to_log_file(char *msg, int len) { if (!log_file_fd) { - fprintf(stderr, "Could not write to log file\n"); - return; + fprintf(stderr, "Could not write to log file\n"); + return; } if (len <= 0) { - fprintf(stderr, "len=0\n"); - return; + fprintf(stderr, "len=0\n"); + return; } if (write(log_file_fd, msg, len) < 0) - perror("Could not write to log file"); + perror("Could not write to log file"); } char *packet_type(u_int type) @@ -125,19 +122,19 @@ char *packet_type(u_int type) switch (type) { case AODV_RREQ: - return "AODV_RREQ"; + return "AODV_RREQ"; case AODV_RREP: - return "AODV_RREP"; + return "AODV_RREP"; case AODV_RERR: - return "AODV_RERR"; + return "AODV_RERR"; default: - sprintf(temp, "Unknown packet type %d", type); - return temp; + sprintf(temp, "Unknown packet type %d", type); + return temp; } } void NS_CLASS alog(int type, int errnum, const char *function, char *format, - ...) + ...) { va_list ap; static char buffer[256] = ""; @@ -150,23 +147,23 @@ void NS_CLASS alog(int type, int errnum, const char *function, char *format, /* NS_PORT: Include IP address in log */ #ifdef NS_PORT if (DEV_NR(NS_DEV_NR).enabled == 1) { - len += sprintf(log_buf + len, "%s: ", - ip_to_str(DEV_NR(NS_DEV_NR).ipaddr)); + len += + sprintf(log_buf + len, "%s: ", ip_to_str(DEV_NR(NS_DEV_NR).ipaddr)); } -#endif /* NS_PORT */ +#endif /* NS_PORT */ va_start(ap, format); if (type == LOG_WARNING) - msg = &buffer[9]; + msg = &buffer[9]; else - msg = buffer; + msg = buffer; vsprintf(msg, format, ap); va_end(ap); if (!debug && !log_to_file) - goto syslog; + goto syslog; gettimeofday(&now, NULL); @@ -177,59 +174,58 @@ void NS_CLASS alog(int type, int errnum, const char *function, char *format, #endif /* if (type <= LOG_NOTICE) */ -/* len += sprintf(log_buf + len, "%s: ", progname); */ + /* len += sprintf(log_buf + len, "%s: ", progname); */ - len += sprintf(log_buf + len, "%02d:%02d:%02d.%03ld %s: %s", time->tm_hour, - time->tm_min, time->tm_sec, now.tv_usec / 1000, function, - msg); + len += + sprintf(log_buf + len, "%02d:%02d:%02d.%03ld %s: %s", time->tm_hour, + time->tm_min, time->tm_sec, now.tv_usec / 1000, function, msg); if (errnum == 0) - len += sprintf(log_buf + len, "\n"); + len += sprintf(log_buf + len, "\n"); else - len += sprintf(log_buf + len, ": %s\n", strerror(errnum)); + len += sprintf(log_buf + len, ": %s\n", strerror(errnum)); if (len > 1024) { - fprintf(stderr, "alog(): buffer to small! len = %d\n", len); - goto syslog; + fprintf(stderr, "alog(): buffer to small! len = %d\n", len); + goto syslog; } /* OK, we are clear to write the buffer to the aodv log file... */ if (log_to_file) - write_to_log_file(log_buf, len); + write_to_log_file(log_buf, len); /* If we have the debug option set, also write to stdout */ if (debug) - printf("%s", log_buf); + printf("%s", log_buf); /* Syslog all messages that are of severity LOG_NOTICE or worse */ - syslog: +syslog: if (type <= LOG_NOTICE) { - if (errnum != 0) { - errno = errnum; - syslog(type, "%s: %s: %m", function, msg); - } else - syslog(type, "%s: %s", function, msg); + if (errnum != 0) { + errno = errnum; + syslog(type, "%s: %s: %m", function, msg); + } else + syslog(type, "%s: %s", function, msg); } /* Exit on error */ if (type <= LOG_ERR) - exit(-1); + exit(-1); } - -char *NS_CLASS rreq_flags_to_str(RREQ * rreq) +char *NS_CLASS rreq_flags_to_str(RREQ *rreq) { static char buf[5]; int len = 0; char *str; if (rreq->j) - buf[len++] = 'J'; + buf[len++] = 'J'; if (rreq->r) - buf[len++] = 'R'; + buf[len++] = 'R'; if (rreq->g) - buf[len++] = 'G'; + buf[len++] = 'G'; if (rreq->d) - buf[len++] = 'D'; + buf[len++] = 'D'; buf[len] = '\0'; @@ -237,16 +233,16 @@ char *NS_CLASS rreq_flags_to_str(RREQ * rreq) return str; } -char *NS_CLASS rrep_flags_to_str(RREP * rrep) +char *NS_CLASS rrep_flags_to_str(RREP *rrep) { static char buf[3]; int len = 0; char *str; if (rrep->r) - buf[len++] = 'R'; + buf[len++] = 'R'; if (rrep->a) - buf[len++] = 'A'; + buf[len++] = 'A'; buf[len] = '\0'; @@ -254,7 +250,7 @@ char *NS_CLASS rrep_flags_to_str(RREP * rrep) return str; } -void NS_CLASS log_pkt_fields(AODV_msg * msg) +void NS_CLASS log_pkt_fields(AODV_msg *msg) { RREQ *rreq; @@ -264,33 +260,33 @@ void NS_CLASS log_pkt_fields(AODV_msg * msg) switch (msg->type) { case AODV_RREQ: - rreq = (RREQ *) msg; - dest.s_addr = rreq->dest_addr; - orig.s_addr = rreq->orig_addr; - DEBUG(LOG_DEBUG, 0, - "rreq->flags:%s rreq->hopcount=%d rreq->rreq_id=%ld", - rreq_flags_to_str(rreq), rreq->hcnt, ntohl(rreq->rreq_id)); - DEBUG(LOG_DEBUG, 0, "rreq->dest_addr:%s rreq->dest_seqno=%lu", - ip_to_str(dest), ntohl(rreq->dest_seqno)); - DEBUG(LOG_DEBUG, 0, "rreq->orig_addr:%s rreq->orig_seqno=%ld", - ip_to_str(orig), ntohl(rreq->orig_seqno)); - break; + rreq = (RREQ *)msg; + dest.s_addr = rreq->dest_addr; + orig.s_addr = rreq->orig_addr; + DEBUG(LOG_DEBUG, 0, + "rreq->flags:%s rreq->hopcount=%d rreq->rreq_id=%ld", + rreq_flags_to_str(rreq), rreq->hcnt, ntohl(rreq->rreq_id)); + DEBUG(LOG_DEBUG, 0, "rreq->dest_addr:%s rreq->dest_seqno=%lu", + ip_to_str(dest), ntohl(rreq->dest_seqno)); + DEBUG(LOG_DEBUG, 0, "rreq->orig_addr:%s rreq->orig_seqno=%ld", + ip_to_str(orig), ntohl(rreq->orig_seqno)); + break; case AODV_RREP: - rrep = (RREP *) msg; - dest.s_addr = rrep->dest_addr; - orig.s_addr = rrep->orig_addr; - DEBUG(LOG_DEBUG, 0, "rrep->flags:%s rrep->hcnt=%d", - rrep_flags_to_str(rrep), rrep->hcnt); - DEBUG(LOG_DEBUG, 0, "rrep->dest_addr:%s rrep->dest_seqno=%d", - ip_to_str(dest), ntohl(rrep->dest_seqno)); - DEBUG(LOG_DEBUG, 0, "rrep->orig_addr:%s rrep->lifetime=%d", - ip_to_str(orig), ntohl(rrep->lifetime)); - break; + rrep = (RREP *)msg; + dest.s_addr = rrep->dest_addr; + orig.s_addr = rrep->orig_addr; + DEBUG(LOG_DEBUG, 0, "rrep->flags:%s rrep->hcnt=%d", + rrep_flags_to_str(rrep), rrep->hcnt); + DEBUG(LOG_DEBUG, 0, "rrep->dest_addr:%s rrep->dest_seqno=%d", + ip_to_str(dest), ntohl(rrep->dest_seqno)); + DEBUG(LOG_DEBUG, 0, "rrep->orig_addr:%s rrep->lifetime=%d", + ip_to_str(orig), ntohl(rrep->lifetime)); + break; case AODV_RERR: - rerr = (RERR *) msg; - DEBUG(LOG_DEBUG, 0, "rerr->dest_count:%d rerr->flags=%s", - rerr->dest_count, rerr->n ? "N" : "-"); - break; + rerr = (RERR *)msg; + DEBUG(LOG_DEBUG, 0, "rerr->dest_count:%d rerr->flags=%s", + rerr->dest_count, rerr->n ? "N" : "-"); + break; } } @@ -301,13 +297,13 @@ char *NS_CLASS rt_flags_to_str(u_int16_t flags) char *str; if (flags & RT_UNIDIR) - buf[len++] = 'U'; + buf[len++] = 'U'; if (flags & RT_REPAIR) - buf[len++] = 'R'; + buf[len++] = 'R'; if (flags & RT_INET_DEST) - buf[len++] = 'I'; + buf[len++] = 'I'; if (flags & RT_GATEWAY) - buf[len++] = 'G'; + buf[len++] = 'G'; buf[len] = '\0'; str = buf; @@ -317,11 +313,11 @@ char *NS_CLASS rt_flags_to_str(u_int16_t flags) char *NS_CLASS state_to_str(u_int8_t state) { if (state == VALID) - return "VAL"; + return "VAL"; else if (state == INVALID) - return "INV"; + return "INV"; else - return "?"; + return "?"; } char *NS_CLASS devs_ip_to_str() @@ -331,9 +327,9 @@ char *NS_CLASS devs_ip_to_str() int i, index = 0; for (i = 0; i < MAX_NR_INTERFACES; i++) { - if (!DEV_NR(i).enabled) - continue; - index += sprintf(buf + index, "%s,", ip_to_str(DEV_NR(i).ipaddr)); + if (!DEV_NR(i).enabled) + continue; + index += sprintf(buf + index, "%s,", ip_to_str(DEV_NR(i).ipaddr)); } str = buf; return str; @@ -349,7 +345,7 @@ void NS_CLASS print_rt_table(void *arg) ssize_t written; if (rt_tbl.num_entries == 0) - goto schedule; + goto schedule; gettimeofday(&now, NULL); @@ -359,86 +355,87 @@ void NS_CLASS print_rt_table(void *arg) time = localtime(&now.tv_sec); #endif - len += - sprintf(rt_buf, - "# Time: %02d:%02d:%02d.%03ld IP: %s seqno: %u entries/active: %u/%u\n", - time->tm_hour, time->tm_min, time->tm_sec, now.tv_usec / 1000, - devs_ip_to_str(), this_host.seqno, rt_tbl.num_entries, - rt_tbl.num_active); - len += - sprintf(rt_buf + len, - "%-15s %-15s %-3s %-3s %-5s %-6s %-5s %-5s %-15s\n", - "Destination", "Next hop", "HC", "St.", "Seqno", "Expire", - "Flags", "Iface", "Precursors"); + len += sprintf( + rt_buf, + "# Time: %02d:%02d:%02d.%03ld IP: %s seqno: %u entries/active: %u/%u\n", + time->tm_hour, time->tm_min, time->tm_sec, now.tv_usec / 1000, + devs_ip_to_str(), this_host.seqno, rt_tbl.num_entries, + rt_tbl.num_active); + len += sprintf(rt_buf + len, + "%-15s %-15s %-3s %-3s %-5s %-6s %-5s %-5s %-15s\n", + "Destination", "Next hop", "HC", "St.", "Seqno", "Expire", + "Flags", "Iface", "Precursors"); written = write(log_rt_fd, rt_buf, len); len = 0; for (i = 0; i < RT_TABLESIZE; i++) { - list_t *pos; - list_foreach(pos, &rt_tbl.tbl[i]) { - rt_table_t *rt = (rt_table_t *) pos; - - if (rt->dest_seqno == 0) - sprintf(seqno_str, "-"); - else - sprintf(seqno_str, "%u", rt->dest_seqno); - - /* Print routing table entries one by one... */ - if (list_empty(&rt->precursors)) - len += sprintf(rt_buf + len, - "%-15s %-15s %-3d %-3s %-5s %-6lu %-5s %-5s\n", - ip_to_str(rt->dest_addr), - ip_to_str(rt->next_hop), rt->hcnt, - state_to_str(rt->state), seqno_str, - (rt->hcnt == 255) ? 0 : - timeval_diff(&rt->rt_timer.timeout, &now), - rt_flags_to_str(rt->flags), - if_indextoname(rt->ifindex, ifname)); - - else { - list_t *pos2; - len += sprintf(rt_buf + len, - "%-15s %-15s %-3d %-3s %-5s %-6lu %-5s %-5s %-15s\n", - ip_to_str(rt->dest_addr), - ip_to_str(rt->next_hop), rt->hcnt, - state_to_str(rt->state), seqno_str, - (rt->hcnt == 255) ? 0 : - timeval_diff(&rt->rt_timer.timeout, &now), - rt_flags_to_str(rt->flags), - if_indextoname(rt->ifindex, ifname), - ip_to_str(((precursor_t *) rt->precursors.next)-> - neighbor)); - - /* Print all precursors for the current routing entry */ - list_foreach(pos2, &rt->precursors) { - precursor_t *pr = (precursor_t *) pos2; - - /* Skip first entry since it is already printed */ - if (pos2->prev == &rt->precursors) - continue; - - len += sprintf(rt_buf + len, "%64s %-15s\n", " ", - ip_to_str(pr->neighbor)); - - /* Since the precursor list is grown dynamically - * the write buffer should be flushed for every - * entry to avoid buffer overflows */ - written = write(log_rt_fd, rt_buf, len); - - len = 0; - - } - } - if (len > 0) { - written = write(log_rt_fd, rt_buf, len); - len = 0; - } - } + list_t *pos; + list_foreach(pos, &rt_tbl.tbl[i]) + { + rt_table_t *rt = (rt_table_t *)pos; + + if (rt->dest_seqno == 0) + sprintf(seqno_str, "-"); + else + sprintf(seqno_str, "%u", rt->dest_seqno); + + /* Print routing table entries one by one... */ + if (list_empty(&rt->precursors)) + len += + sprintf(rt_buf + len, + "%-15s %-15s %-3d %-3s %-5s %-6lu %-5s %-5s\n", + ip_to_str(rt->dest_addr), ip_to_str(rt->next_hop), + rt->hcnt, state_to_str(rt->state), seqno_str, + (rt->hcnt == 255) + ? 0 + : timeval_diff(&rt->rt_timer.timeout, &now), + rt_flags_to_str(rt->flags), + if_indextoname(rt->ifindex, ifname)); + + else { + list_t *pos2; + len += sprintf( + rt_buf + len, + "%-15s %-15s %-3d %-3s %-5s %-6lu %-5s %-5s %-15s\n", + ip_to_str(rt->dest_addr), ip_to_str(rt->next_hop), rt->hcnt, + state_to_str(rt->state), seqno_str, + (rt->hcnt == 255) + ? 0 + : timeval_diff(&rt->rt_timer.timeout, &now), + rt_flags_to_str(rt->flags), + if_indextoname(rt->ifindex, ifname), + ip_to_str(((precursor_t *)rt->precursors.next)->neighbor)); + + /* Print all precursors for the current routing entry */ + list_foreach(pos2, &rt->precursors) + { + precursor_t *pr = (precursor_t *)pos2; + + /* Skip first entry since it is already printed */ + if (pos2->prev == &rt->precursors) + continue; + + len += sprintf(rt_buf + len, "%64s %-15s\n", " ", + ip_to_str(pr->neighbor)); + + /* Since the precursor list is grown dynamically + * the write buffer should be flushed for every + * entry to avoid buffer overflows */ + written = write(log_rt_fd, rt_buf, len); + + len = 0; + } + } + if (len > 0) { + written = write(log_rt_fd, rt_buf, len); + len = 0; + } + } } /* Schedule a new printing of routing table... */ - schedule: +schedule: timer_set_timeout(&rt_log_timer, rt_log_interval); } diff --git a/debug.h b/debug.h index 8ef9794..f662d65 100644 --- a/debug.h +++ b/debug.h @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, + * Authors: Erik Nordström, * *****************************************************************************/ #ifndef _DEBUG_H @@ -32,7 +32,7 @@ void log_cleanup(); char *packet_type(unsigned int type); void alog(int type, int errnum, const char *function, char *format, ...); -void log_pkt_fields(AODV_msg * msg); +void log_pkt_fields(AODV_msg *msg); void print_rt_table(void *arg); void log_rt_table_init(); char *ip_to_str(struct in_addr addr); @@ -40,21 +40,21 @@ char *ip_to_str(struct in_addr addr); #ifdef NS_PORT void write_to_log_file(char *msg, int len); char *devs_ip_to_str(); -char *rreq_flags_to_str(RREQ * rreq); -char *rrep_flags_to_str(RREP * rrep); +char *rreq_flags_to_str(RREQ *rreq); +char *rrep_flags_to_str(RREP *rrep); char *rt_flags_to_str(u_int16_t flags); char *state_to_str(u_int8_t state); #endif -#endif /* NS_NO_DECLARATIONS */ +#endif /* NS_NO_DECLARATIONS */ #ifndef NS_NO_GLOBALS #ifdef DEBUG #undef DEBUG #define DEBUG_OUTPUT -#define DEBUG(l, s, args...) alog(l, s, __FUNCTION__, ## args) +#define DEBUG(l, s, args...) alog(l, s, __FUNCTION__, ##args) #else #define DEBUG(l, s, args...) #endif -#endif /* NS_NO_GLOBALS */ +#endif /* NS_NO_GLOBALS */ -#endif /* _DEBUG_H */ +#endif /* _DEBUG_H */ diff --git a/defs.h b/defs.h index 399d6b7..f12d7da 100644 --- a/defs.h +++ b/defs.h @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, + * Authors: Erik Nordström, * *****************************************************************************/ #ifndef _DEFS_H @@ -24,22 +24,22 @@ #include #include -#include #include #include +#include #ifndef NS_PORT -#include -#include #include +#include #include #include +#include #endif -#include #include -#include #include +#include +#include #ifndef NS_PORT #include "timer_queue.h" @@ -71,11 +71,11 @@ #else #define AODV_LOG_PATH "/var/log/aodvd.log" #define AODV_RT_LOG_PATH "/var/log/aodvd.rtlog" -#endif /* NS_PORT */ +#endif /* NS_PORT */ -#define max(A,B) ( (A) > (B) ? (A):(B)) +#define max(A, B) ((A) > (B) ? (A) : (B)) -#define MINTTL 1 /* min TTL in the packets sent locally */ +#define MINTTL 1 /* min TTL in the packets sent locally */ #define MAX_NR_INTERFACES 10 #define MAX_IFINDEX (MAX_NR_INTERFACES - 1) @@ -86,28 +86,29 @@ /* Data for a network device */ struct dev_info { - int enabled; /* 1 if struct is used, else 0 */ - int sock; /* AODV socket associated with this device */ + int enabled; /* 1 if struct is used, else 0 */ + int sock; /* AODV socket associated with this device */ #ifdef CONFIG_GATEWAY - int psock; /* Socket to send buffered data packets. */ + int psock; /* Socket to send buffered data packets. */ #endif unsigned int ifindex; char ifname[IFNAMSIZ]; - struct in_addr ipaddr; /* The local IP address */ - struct in_addr netmask; /* The netmask we use */ + struct in_addr ipaddr; /* The local IP address */ + struct in_addr netmask; /* The netmask we use */ struct in_addr broadcast; }; struct host_info { - u_int32_t seqno; /* Sequence number */ - struct timeval bcast_time; /* The time of the last broadcast msg sent */ - struct timeval fwd_time; /* The time a data packet was last forwarded */ - u_int32_t rreq_id; /* RREQ id */ - int nif; /* Number of interfaces to broadcast on */ - struct dev_info devs[MAX_NR_INTERFACES+1]; /* Add +1 for returning as "error" in ifindex2devindex. */ + u_int32_t seqno; /* Sequence number */ + struct timeval bcast_time; /* The time of the last broadcast msg sent */ + struct timeval fwd_time; /* The time a data packet was last forwarded */ + u_int32_t rreq_id; /* RREQ id */ + int nif; /* Number of interfaces to broadcast on */ + struct dev_info + devs[MAX_NR_INTERFACES + + 1]; /* Add +1 for returning as "error" in ifindex2devindex. */ }; - /* NS_PORT: TEMPORARY SOLUTION: Moved the two variables into the AODVUU class, and placed the function definition after the AODVUU class definition. @@ -132,8 +133,8 @@ static inline unsigned int ifindex2devindex(unsigned int ifindex) int i; for (i = 0; i < this_host.nif; i++) - if (dev_indices[i] == ifindex) - return i; + if (dev_indices[i] == ifindex) + return i; return MAX_NR_INTERFACES; } @@ -143,8 +144,8 @@ static inline struct dev_info *devfromsock(int sock) int i; for (i = 0; i < this_host.nif; i++) { - if (this_host.devs[i].sock == sock) - return &this_host.devs[i]; + if (this_host.devs[i].sock == sock) + return &this_host.devs[i]; } return NULL; } @@ -154,30 +155,29 @@ static inline int name2index(char *name) int i; for (i = 0; i < this_host.nif; i++) - if (strcmp(name, this_host.devs[i].ifname) == 0) - return this_host.devs[i].ifindex; + if (strcmp(name, this_host.devs[i].ifname) == 0) + return this_host.devs[i].ifindex; return -1; } #endif - /* Two macros to simplify retriving of a dev_info struct. Either using an ifindex or a device number (index into devs array). */ #define DEV_IFINDEX(ifindex) (this_host.devs[ifindex2devindex(ifindex)]) #define DEV_NR(n) (this_host.devs[n]) - /* Broadcast address according to draft (255.255.255.255) */ -#define AODV_BROADCAST ((in_addr_t) 0xFFFFFFFF) +/* Broadcast address according to draft (255.255.255.255) */ +#define AODV_BROADCAST ((in_addr_t)0xFFFFFFFF) #define AODV_PORT 654 /* AODV Message types */ -#define AODV_HELLO 0 /* Really never used as a separate type... */ -#define AODV_RREQ 1 -#define AODV_RREP 2 -#define AODV_RERR 3 +#define AODV_HELLO 0 /* Really never used as a separate type... */ +#define AODV_RREQ 1 +#define AODV_RREP 2 +#define AODV_RERR 3 #define AODV_RREP_ACK 4 /* A generic AODV packet header struct... */ @@ -190,20 +190,19 @@ typedef struct { /* NS_PORT: Additions for the AODVUU packet type in ns-2 */ #ifdef NS_PORT - static int offset_; // Required by PacketHeaderManager + static int offset_; // Required by PacketHeaderManager - inline static int &offset() { - return offset_; - } - inline static AODV_msg *access(const Packet * p) { - return (AODV_msg *) p->access(offset_); + inline static int &offset() { return offset_; } + inline static AODV_msg *access(const Packet *p) + { + return (AODV_msg *)p->access(offset_); } int size(); }; -typedef AODV_msg hdr_aodvuu; // Name convention for headers -#define HDR_AODVUU(p) ((hdr_aodvuu *) hdr_aodvuu::access(p)) +typedef AODV_msg hdr_aodvuu; // Name convention for headers +#define HDR_AODVUU(p) ((hdr_aodvuu *)hdr_aodvuu::access(p)) #else } AODV_msg; #endif @@ -225,13 +224,14 @@ typedef struct { /* MACROS to access AODV extensions... */ #define AODV_EXT_HDR_SIZE sizeof(AODV_ext) #define AODV_EXT_DATA(ext) ((char *)((char *)ext + AODV_EXT_HDR_SIZE)) -#define AODV_EXT_NEXT(ext) ((AODV_ext *)((char *)ext + AODV_EXT_HDR_SIZE + ext->length)) +#define AODV_EXT_NEXT(ext) \ + ((AODV_ext *)((char *)ext + AODV_EXT_HDR_SIZE + ext->length)) #define AODV_EXT_SIZE(ext) (AODV_EXT_HDR_SIZE + ext->length) #ifndef NS_PORT /* The callback function */ -typedef void (*callback_func_t) (int); +typedef void (*callback_func_t)(int); extern int attach_callback_func(int fd, callback_func_t func); #endif -#endif /* DEFS_H */ +#endif /* DEFS_H */ diff --git a/endian.c b/endian.c index 1b8279a..befcafb 100644 --- a/endian.c +++ b/endian.c @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Björn Wiberg + * Authors: Bj�rn Wiberg * *****************************************************************************/ @@ -36,14 +36,11 @@ int litend(void) { int i = 0; - ((char *) (&i))[0] = 1; + ((char *)(&i))[0] = 1; return (i == 1); } -int bigend(void) -{ - return !litend(); -} +int bigend(void) { return !litend(); } int main(int argc, char **argv) { @@ -51,7 +48,7 @@ int main(int argc, char **argv) printf("#define ENDIAN_H\n"); printf("#define __LITTLE_ENDIAN 1234\n"); printf("#define __BIG_ENDIAN 4321\n"); - printf("#define __BYTE_ORDER __%s_ENDIAN\n", litend()? "LITTLE" : "BIG"); + printf("#define __BYTE_ORDER __%s_ENDIAN\n", litend() ? "LITTLE" : "BIG"); printf("#endif\n"); return 0; } diff --git a/list.c b/list.c index d0d6de5..d13ff0e 100644 --- a/list.c +++ b/list.c @@ -16,14 +16,14 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, + * Authors: Erik Nordström, * *****************************************************************************/ #include #include "list.h" -static inline int listelm_detach(list_t * prev, list_t * next) +static inline int listelm_detach(list_t *prev, list_t *next) { next->prev = prev; prev->next = next; @@ -31,7 +31,7 @@ static inline int listelm_detach(list_t * prev, list_t * next) return LIST_SUCCESS; } -static inline int listelm_add(list_t * le, list_t * prev, list_t * next) +static inline int listelm_add(list_t *le, list_t *prev, list_t *next) { prev->next = le; le->prev = prev; @@ -41,32 +41,32 @@ static inline int listelm_add(list_t * le, list_t * prev, list_t * next) return LIST_SUCCESS; } -int list_add(list_t * head, list_t * le) +int list_add(list_t *head, list_t *le) { if (!head || !le) - return LIST_NULL; + return LIST_NULL; listelm_add(le, head, head->next); return LIST_SUCCESS; } -int list_add_tail(list_t * head, list_t * le) +int list_add_tail(list_t *head, list_t *le) { if (!head || !le) - return LIST_NULL; + return LIST_NULL; listelm_add(le, head->prev, head); return LIST_SUCCESS; } -int list_detach(list_t * le) +int list_detach(list_t *le) { if (!le) - return LIST_NULL; + return LIST_NULL; listelm_detach(le->prev, le->next); diff --git a/list.h b/list.h index b973ccf..e5ff18c 100644 --- a/list.h +++ b/list.h @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, + * Authors: Erik Nordström, * *****************************************************************************/ #ifndef _LIST_H @@ -30,26 +30,30 @@ typedef struct list_t { #define LIST_NULL -1 #define LIST_SUCCESS 1 -#define LIST(name) list_t name = { &(name), &(name) } +#define LIST(name) list_t name = {&(name), &(name)} -#define INIT_LIST_HEAD(h) do { \ - (h)->next = (h); (h)->prev = (h); \ -} while (0) +#define INIT_LIST_HEAD(h) \ + do { \ + (h)->next = (h); \ + (h)->prev = (h); \ + } while (0) -#define INIT_LIST_ELM(le) do { \ - (le)->next = NULL; (le)->prev = NULL; \ -} while (0) +#define INIT_LIST_ELM(le) \ + do { \ + (le)->next = NULL; \ + (le)->prev = NULL; \ + } while (0) -int list_detach(list_t * le); -int list_add_tail(list_t * head, list_t * le); -int list_add(list_t * head, list_t * le); +int list_detach(list_t *le); +int list_add_tail(list_t *head, list_t *le); +int list_add(list_t *head, list_t *le); -#define list_foreach(curr, head) \ - for (curr = (head)->next; curr != (head); curr = curr->next) +#define list_foreach(curr, head) \ + for (curr = (head)->next; curr != (head); curr = curr->next) -#define list_foreach_safe(pos, tmp, head) \ - for (pos = (head)->next, tmp = pos->next; pos != (head); \ - pos = tmp, tmp = pos->next) +#define list_foreach_safe(pos, tmp, head) \ + for (pos = (head)->next, tmp = pos->next; pos != (head); \ + pos = tmp, tmp = pos->next) #define list_empty(head) ((head) == (head)->next) diff --git a/llf.c b/llf.c index 45a7769..f41ece4 100644 --- a/llf.c +++ b/llf.c @@ -16,24 +16,24 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Author: Erik Nordström, + * Author: Erik Nordström, * *****************************************************************************/ -#include "defs.h" #include "debug.h" +#include "defs.h" -#include -#include -#include #include #include -#include -#include -#include /* struct ether_addr */ +#include #include #include #include -#include +#include /* struct ether_addr */ +#include +#include +#include +#include +#include #include "aodv_neighbor.h" #include "routing_table.h" @@ -50,37 +50,27 @@ struct rtnl_handle { }; static inline int llf_rtnl_open(struct rtnl_handle *rth, - unsigned subscriptions); + unsigned subscriptions); static inline void llf_handle_netlink_events(struct rtnl_handle *rth); static struct rtnl_handle rth; -static void llf_callback(int fd) -{ - - llf_handle_netlink_events(&rth); -} +static void llf_callback(int fd) { llf_handle_netlink_events(&rth); } void llf_init() { if (llf_rtnl_open(&rth, RTMGRP_LINK) < 0) { - DEBUG(LOG_ERR, 0, "Can't initialize rtnetlink socket"); - return; + DEBUG(LOG_ERR, 0, "Can't initialize rtnetlink socket"); + return; } if (attach_callback_func(rth.fd, llf_callback) < 0) { - alog(LOG_ERR, 0, __FUNCTION__, "Could not attach callback"); - return; + alog(LOG_ERR, 0, __FUNCTION__, "Could not attach callback"); + return; } } - -void llf_cleanup() -{ - close(rth.fd); -} - - +void llf_cleanup() { close(rth.fd); } static inline int llf_rtnl_open(struct rtnl_handle *rth, unsigned subscriptions) { @@ -90,36 +80,35 @@ static inline int llf_rtnl_open(struct rtnl_handle *rth, unsigned subscriptions) rth->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); if (rth->fd < 0) { - perror("Cannot open netlink socket"); - return -1; + perror("Cannot open netlink socket"); + return -1; } memset(&rth->local, 0, sizeof(rth->local)); rth->local.nl_family = AF_NETLINK; rth->local.nl_groups = subscriptions; - if (bind(rth->fd, (struct sockaddr *) &rth->local, sizeof(rth->local)) < 0) { - perror("Cannot bind netlink socket"); - return -1; + if (bind(rth->fd, (struct sockaddr *)&rth->local, sizeof(rth->local)) < 0) { + perror("Cannot bind netlink socket"); + return -1; } addr_len = sizeof(rth->local); - if (getsockname(rth->fd, (struct sockaddr *) &rth->local, &addr_len) < 0) { - perror("Cannot getsockname"); - return -1; + if (getsockname(rth->fd, (struct sockaddr *)&rth->local, &addr_len) < 0) { + perror("Cannot getsockname"); + return -1; } if (addr_len != sizeof(rth->local)) { - fprintf(stderr, "Wrong address length %d\n", addr_len); - return -1; + fprintf(stderr, "Wrong address length %d\n", addr_len); + return -1; } if (rth->local.nl_family != AF_NETLINK) { - fprintf(stderr, "Wrong address family %d\n", rth->local.nl_family); - return -1; + fprintf(stderr, "Wrong address family %d\n", rth->local.nl_family); + return -1; } rth->seq = time(NULL); return 0; } - /* * Static information about wireless interface. * We cache this info for performance reason. @@ -129,11 +118,11 @@ typedef struct wireless_iface { struct wireless_iface *next; /* Interface identification */ - int ifindex; /* Interface index == black magic */ + int ifindex; /* Interface index == black magic */ /* Interface data */ - char ifname[IFNAMSIZ + 1]; /* Interface name */ - struct iw_range range; /* Wireless static data */ + char ifname[IFNAMSIZ + 1]; /* Interface name */ + struct iw_range range; /* Wireless static data */ int has_range; } wireless_iface; @@ -155,9 +144,9 @@ static inline int index2name(int skfd, int ifindex, char *name) /* Get interface name */ irq.ifr_ifindex = ifindex; if (ioctl(skfd, SIOCGIFNAME, &irq) < 0) - ret = -1; + ret = -1; else - strncpy(name, irq.ifr_name, IFNAMSIZ); + strncpy(name, irq.ifr_name, IFNAMSIZ); return (ret); } @@ -165,46 +154,46 @@ static inline int index2name(int skfd, int ifindex, char *name) static struct wireless_iface *iw_get_interface_data(int ifindex) { struct wireless_iface *curr; - int skfd = -1; /* ioctl socket */ + int skfd = -1; /* ioctl socket */ /* Search for it in the database */ curr = interface_cache; while (curr != NULL) { - /* Match ? */ - if (curr->ifindex == ifindex) { - //printf("Cache : found %d-%s\n", curr->ifindex, curr->ifname); - - /* Return */ - return (curr); - } - /* Next entry */ - curr = curr->next; + /* Match ? */ + if (curr->ifindex == ifindex) { + // printf("Cache : found %d-%s\n", curr->ifindex, curr->ifname); + + /* Return */ + return (curr); + } + /* Next entry */ + curr = curr->next; } /* Create a channel to the NET kernel. Doesn't happen too often, so * socket creation overhead is minimal... */ if ((skfd = iw_sockets_open()) < 0) { - perror("iw_sockets_open"); - return (NULL); + perror("iw_sockets_open"); + return (NULL); } /* Create new entry, zero, init */ curr = calloc(1, sizeof(struct wireless_iface)); if (!curr) { - fprintf(stderr, "Malloc failed\n"); - return (NULL); + fprintf(stderr, "Malloc failed\n"); + return (NULL); } curr->ifindex = ifindex; /* Extract static data */ if (index2name(skfd, ifindex, curr->ifname) < 0) { - perror("index2name"); - free(curr); - return (NULL); + perror("index2name"); + free(curr); + return (NULL); } curr->has_range = - (iw_get_range_info(skfd, curr->ifname, &curr->range) >= 0); - //printf("Cache : create %d-%s\n", curr->ifindex, curr->ifname); + (iw_get_range_info(skfd, curr->ifname, &curr->range) >= 0); + // printf("Cache : create %d-%s\n", curr->ifindex, curr->ifname); /* Done */ iw_sockets_close(skfd); @@ -229,37 +218,37 @@ int mac_to_ip(struct sockaddr *hwaddr, struct in_addr *ip_addr, char *ifname) struct ether_addr eth; if ((fp = fopen("/proc/net/arp", "r")) == NULL) { - perror("/proc/net/arp"); - return (-1); + perror("/proc/net/arp"); + return (-1); } /* Bypass header -- read until newline */ - if (fgets(line, sizeof(line), fp) != (char *) NULL) { - strcpy(mask, "-"); - strcpy(dev, "-"); - /* Read the ARP cache entries. */ - for (; fgets(line, sizeof(line), fp);) { - num = sscanf(line, "%s 0x%x 0x%x %100s %100s %100s\n", - ip, &type, &flags, hwa, mask, dev); - if (num < 4) - break; - - iw_ether_aton(hwa, ð); - - if (memcmp(ð, hwaddr->sa_data, ETH_ALEN) == 0) { - - inet_aton(ip, ip_addr); - - fclose(fp); - return 0; - } - } + if (fgets(line, sizeof(line), fp) != (char *)NULL) { + strcpy(mask, "-"); + strcpy(dev, "-"); + /* Read the ARP cache entries. */ + for (; fgets(line, sizeof(line), fp);) { + num = sscanf(line, "%s 0x%x 0x%x %100s %100s %100s\n", ip, &type, + &flags, hwa, mask, dev); + if (num < 4) + break; + + iw_ether_aton(hwa, ð); + + if (memcmp(ð, hwaddr->sa_data, ETH_ALEN) == 0) { + + inet_aton(ip, ip_addr); + + fclose(fp); + return 0; + } + } } fclose(fp); return -1; } static inline int llf_print_event(struct iw_event *event, - struct iw_range *iwrange, int has_iwrange) + struct iw_range *iwrange, int has_iwrange) { char buffer[128]; struct in_addr ip; @@ -269,26 +258,25 @@ static inline int llf_print_event(struct iw_event *event, switch (event->cmd) { case IWEVTXDROP: - DEBUG(LOG_DEBUG, 0, "Tx packet dropped:%s", - iw_pr_ether(buffer, event->u.addr.sa_data)); - + DEBUG(LOG_DEBUG, 0, "Tx packet dropped:%s", + iw_pr_ether(buffer, event->u.addr.sa_data)); - if (mac_to_ip(&event->u.addr, &ip, this_host.devs[0].ifname) != 0) { - DEBUG(LOG_DEBUG, 0, "failed mac_to_ip"); - return 0; - } - //printf("IP=%s\n", ip_to_str(ip)); + if (mac_to_ip(&event->u.addr, &ip, this_host.devs[0].ifname) != 0) { + DEBUG(LOG_DEBUG, 0, "failed mac_to_ip"); + return 0; + } + // printf("IP=%s\n", ip_to_str(ip)); - rt = rt_table_find(ip); + rt = rt_table_find(ip); - if (rt) - neighbor_link_break(rt); - else - DEBUG(LOG_DEBUG, 0, "no route for ip=%s", ip_to_str(ip)); - break; + if (rt) + neighbor_link_break(rt); + else + DEBUG(LOG_DEBUG, 0, "no route for ip=%s", ip_to_str(ip)); + break; default: - DEBUG(LOG_DEBUG, 0, "(Unknown Wireless event 0x%04X)", event->cmd); + DEBUG(LOG_DEBUG, 0, "(Unknown Wireless event 0x%04X)", event->cmd); } return 0; @@ -297,123 +285,117 @@ static inline int llf_print_event(struct iw_event *event, static inline void llf_handle_netlink_events(struct rtnl_handle *rth) { while (1) { - struct sockaddr_nl sanl; - socklen_t sanllen; - struct nlmsghdr *h; - struct ifinfomsg *ifi; - int amt; - char buf[8192]; - - amt = - recvfrom(rth->fd, buf, sizeof(buf), MSG_DONTWAIT, - (struct sockaddr *) &sanl, &sanllen); - if (amt < 0) { - if (errno != EINTR && errno != EAGAIN) { - fprintf(stderr, "%s: error reading netlink: %s.\n", - __PRETTY_FUNCTION__, strerror(errno)); - } - return; - } - - if (amt == 0) { - fprintf(stderr, "%s: EOF on netlink??\n", __PRETTY_FUNCTION__); - return; - } - - h = (struct nlmsghdr *) buf; - while (amt >= (int) sizeof(*h)) { - int len = h->nlmsg_len; - int l = len - sizeof(*h); - - if (l < 0 || len > amt) { - fprintf(stderr, "%s: malformed netlink message: len=%d\n", - __PRETTY_FUNCTION__, len); - break; - } - - switch (h->nlmsg_type) { - case RTM_NEWLINK: -// LinkCatcher(h); - if (h->nlmsg_type != RTM_NEWLINK) - return; - - ifi = NLMSG_DATA(h); - - /* Check for attributes */ - if (h->nlmsg_len > NLMSG_ALIGN(sizeof(struct ifinfomsg))) { - int attrlen = - h->nlmsg_len - NLMSG_ALIGN(sizeof(struct ifinfomsg)); - struct rtattr *attr = - (void *) ifi + NLMSG_ALIGN(sizeof(struct ifinfomsg)); - - while (RTA_OK(attr, attrlen)) { - /* Check if the Wireless kind */ - if (attr->rta_type == IFLA_WIRELESS) { - struct iw_event iwe; - struct stream_descr stream; - int ret; + struct sockaddr_nl sanl; + socklen_t sanllen; + struct nlmsghdr *h; + struct ifinfomsg *ifi; + int amt; + char buf[8192]; + + amt = recvfrom(rth->fd, buf, sizeof(buf), MSG_DONTWAIT, + (struct sockaddr *)&sanl, &sanllen); + if (amt < 0) { + if (errno != EINTR && errno != EAGAIN) { + fprintf(stderr, "%s: error reading netlink: %s.\n", + __PRETTY_FUNCTION__, strerror(errno)); + } + return; + } + + if (amt == 0) { + fprintf(stderr, "%s: EOF on netlink??\n", __PRETTY_FUNCTION__); + return; + } + + h = (struct nlmsghdr *)buf; + while (amt >= (int)sizeof(*h)) { + int len = h->nlmsg_len; + int l = len - sizeof(*h); + + if (l < 0 || len > amt) { + fprintf(stderr, "%s: malformed netlink message: len=%d\n", + __PRETTY_FUNCTION__, len); + break; + } + + switch (h->nlmsg_type) { + case RTM_NEWLINK: + // LinkCatcher(h); + if (h->nlmsg_type != RTM_NEWLINK) + return; + + ifi = NLMSG_DATA(h); + + /* Check for attributes */ + if (h->nlmsg_len > NLMSG_ALIGN(sizeof(struct ifinfomsg))) { + int attrlen = + h->nlmsg_len - NLMSG_ALIGN(sizeof(struct ifinfomsg)); + struct rtattr *attr = + (void *)ifi + NLMSG_ALIGN(sizeof(struct ifinfomsg)); + + while (RTA_OK(attr, attrlen)) { + /* Check if the Wireless kind */ + if (attr->rta_type == IFLA_WIRELESS) { + struct iw_event iwe; + struct stream_descr stream; + int ret; #if WE_VERSION >= 17 - struct wireless_iface *wireless_data; + struct wireless_iface *wireless_data; - /* Get data from cache */ - wireless_data = - iw_get_interface_data(ifi->ifi_index); + /* Get data from cache */ + wireless_data = + iw_get_interface_data(ifi->ifi_index); - if (wireless_data == NULL) - return; + if (wireless_data == NULL) + return; #endif - /* Go to display it */ - - iw_init_event_stream(&stream, - (void *) attr + - RTA_ALIGN(sizeof - (struct rtattr)), - attr->rta_len - - RTA_ALIGN(sizeof - (struct rtattr))); - do { - /* Extract an event and print it */ + /* Go to display it */ + + iw_init_event_stream( + &stream, + (void *)attr + RTA_ALIGN(sizeof(struct rtattr)), + attr->rta_len - + RTA_ALIGN(sizeof(struct rtattr))); + do { + /* Extract an event and print it */ #if WE_VERSION >= 17 - ret = - iw_extract_event_stream(&stream, &iwe, - wireless_data-> - range. - we_version_compiled); + ret = iw_extract_event_stream( + &stream, &iwe, + wireless_data->range.we_version_compiled); #else - ret = iw_extract_event_stream(&stream, &iwe); + ret = iw_extract_event_stream(&stream, &iwe); #endif - if (ret != 0) { - if (ret > 0) - llf_print_event(&iwe, NULL, 0); - else - DEBUG(LOG_WARNING, 0, - "Invalid iw event"); - } - } while (ret > 0); - - } - attr = RTA_NEXT(attr, attrlen); - } - } - - break; - default: + if (ret != 0) { + if (ret > 0) + llf_print_event(&iwe, NULL, 0); + else + DEBUG(LOG_WARNING, 0, + "Invalid iw event"); + } + } while (ret > 0); + } + attr = RTA_NEXT(attr, attrlen); + } + } + + break; + default: #if 0 fprintf(stderr, "%s: got nlmsg of type %#x.\n", __PRETTY_FUNCTION__, h->nlmsg_type); #endif - break; - } + break; + } - len = NLMSG_ALIGN(len); - amt -= len; - h = (struct nlmsghdr *) ((char *) h + len); - } + len = NLMSG_ALIGN(len); + amt -= len; + h = (struct nlmsghdr *)((char *)h + len); + } - if (amt > 0) - fprintf(stderr, "%s: remnant of size %d on netlink\n", - __PRETTY_FUNCTION__, amt); + if (amt > 0) + fprintf(stderr, "%s: remnant of size %d on netlink\n", + __PRETTY_FUNCTION__, amt); } } diff --git a/llf.h b/llf.h index cbe1551..a13d21a 100644 --- a/llf.h +++ b/llf.h @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Author: Erik Nordström, + * Author: Erik Nordström, * *****************************************************************************/ #ifndef _LLF_H diff --git a/lnx/kaodv-debug.c b/lnx/kaodv-debug.c index c01d6b5..2b425c6 100644 --- a/lnx/kaodv-debug.c +++ b/lnx/kaodv-debug.c @@ -19,26 +19,26 @@ * Author: Erik Nordström, * *****************************************************************************/ -#include #include +#include #include "kaodv-debug.h" #include "kaodv-netlink.h" int trace(const char *fmt, ...) { - char buf[512]; - va_list args; - int len; + char buf[512]; + va_list args; + int len; + + va_start(args, fmt); - va_start(args, fmt); + len = vsnprintf(buf, 512, fmt, args); - len = vsnprintf(buf, 512, fmt, args); + va_end(args); - va_end(args); - - /* Send the message off to user space... */ - kaodv_netlink_send_debug_msg(buf, len + 1); + /* Send the message off to user space... */ + kaodv_netlink_send_debug_msg(buf, len + 1); - return 0; + return 0; } diff --git a/lnx/kaodv-debug.h b/lnx/kaodv-debug.h index 7224cc9..dd72c45 100644 --- a/lnx/kaodv-debug.h +++ b/lnx/kaodv-debug.h @@ -31,37 +31,34 @@ #define KAODV_DEBUG(fmt, ...) #endif - static inline char *print_ip(__u32 addr) { - static char buf[16 * 4]; - static int index = 0; - char *str; + static char buf[16 * 4]; + static int index = 0; + char *str; - sprintf(&buf[index], "%d.%d.%d.%d", - 0x0ff & addr, - 0x0ff & (addr >> 8), - 0x0ff & (addr >> 16), 0x0ff & (addr >> 24)); + sprintf(&buf[index], "%d.%d.%d.%d", 0x0ff & addr, 0x0ff & (addr >> 8), + 0x0ff & (addr >> 16), 0x0ff & (addr >> 24)); - str = &buf[index]; - index += 16; - index %= 64; + str = &buf[index]; + index += 16; + index %= 64; - return str; + return str; } static inline char *print_eth(char *addr) { - static char buf[30]; + static char buf[30]; - sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", - (unsigned char)addr[0], (unsigned char)addr[1], - (unsigned char)addr[2], (unsigned char)addr[3], - (unsigned char)addr[4], (unsigned char)addr[5]); + sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", (unsigned char)addr[0], + (unsigned char)addr[1], (unsigned char)addr[2], + (unsigned char)addr[3], (unsigned char)addr[4], + (unsigned char)addr[5]); - return buf; + return buf; } int trace(const char *fmt, ...); -#endif /* !KAODV-DEBUG_H_ */ +#endif /* !KAODV-DEBUG_H_ */ diff --git a/lnx/kaodv-expl.c b/lnx/kaodv-expl.c index d4002b5..57683a5 100644 --- a/lnx/kaodv-expl.c +++ b/lnx/kaodv-expl.c @@ -21,18 +21,18 @@ *****************************************************************************/ /* Expire list for aodv route information */ -#include #include -#include #include +#include +#include #include #include -#include +#include +#include "kaodv-debug.h" #include "kaodv-expl.h" #include "kaodv-netlink.h" #include "kaodv-queue.h" -#include "kaodv-debug.h" #define EXPL_MAX_LEN 1024 @@ -53,379 +53,378 @@ static void kaodv_expl_timeout(unsigned long data); static inline void __kaodv_expl_set_next_timeout(void) { - struct expl_entry *ne; - - if (list_empty(&expl_head)) - return; - - /* Get first entry */ - ne = (struct expl_entry *)expl_head.next; - - if (timer_pending(&expl_timer)) { - mod_timer(&expl_timer, ne->expires); - } else { - expl_timer.function = kaodv_expl_timeout; - expl_timer.expires = ne->expires; - expl_timer.data = 0; - add_timer(&expl_timer); - } + struct expl_entry *ne; + + if (list_empty(&expl_head)) + return; + + /* Get first entry */ + ne = (struct expl_entry *)expl_head.next; + + if (timer_pending(&expl_timer)) { + mod_timer(&expl_timer, ne->expires); + } else { + expl_timer.function = kaodv_expl_timeout; + expl_timer.expires = ne->expires; + expl_timer.data = 0; + add_timer(&expl_timer); + } } static void kaodv_expl_timeout(unsigned long data) { - struct list_head *pos, *tmp; - int time = jiffies; + struct list_head *pos, *tmp; + int time = jiffies; - write_lock_bh(&expl_lock); + write_lock_bh(&expl_lock); - list_for_each_safe(pos, tmp, &expl_head) { - struct expl_entry *e = (struct expl_entry *)pos; + list_for_each_safe(pos, tmp, &expl_head) + { + struct expl_entry *e = (struct expl_entry *)pos; - if (e->expires > time) - break; + if (e->expires > time) + break; - list_del(&e->l); - expl_len--; + list_del(&e->l); + expl_len--; - /* Flush any queued packets for this dest */ - kaodv_queue_set_verdict(KAODV_QUEUE_DROP, e->daddr); + /* Flush any queued packets for this dest */ + kaodv_queue_set_verdict(KAODV_QUEUE_DROP, e->daddr); - /* printk("expl_timeout: sending timeout event!\n"); */ - kaodv_netlink_send_rt_msg(KAODVM_TIMEOUT, e->daddr); - } - __kaodv_expl_set_next_timeout(); - write_unlock_bh(&expl_lock); + /* printk("expl_timeout: sending timeout event!\n"); */ + kaodv_netlink_send_rt_msg(KAODVM_TIMEOUT, e->daddr); + } + __kaodv_expl_set_next_timeout(); + write_unlock_bh(&expl_lock); } -#endif /* EXPL_TIMER */ +#endif /* EXPL_TIMER */ static inline void __kaodv_expl_flush(void) { - struct list_head *pos, *tmp; - - list_for_each_safe(pos, tmp, &expl_head) { - struct expl_entry *e = (struct expl_entry *)pos; - list_del(&e->l); - expl_len--; - kfree(e); - } + struct list_head *pos, *tmp; + + list_for_each_safe(pos, tmp, &expl_head) + { + struct expl_entry *e = (struct expl_entry *)pos; + list_del(&e->l); + expl_len--; + kfree(e); + } } static inline int __kaodv_expl_add(struct expl_entry *e) { - if (expl_len >= EXPL_MAX_LEN) { - printk(KERN_WARNING "kaodv_expl: Max list len reached\n"); - return -ENOSPC; - } - - if (list_empty(&expl_head)) { - list_add(&e->l, &expl_head); - } else { - struct list_head *pos; - - list_for_each(pos, &expl_head) { - struct expl_entry *curr = (struct expl_entry *)pos; - - if (curr->expires > e->expires) - break; - } - list_add(&e->l, pos->prev); - } - return 1; + if (expl_len >= EXPL_MAX_LEN) { + printk(KERN_WARNING "kaodv_expl: Max list len reached\n"); + return -ENOSPC; + } + + if (list_empty(&expl_head)) { + list_add(&e->l, &expl_head); + } else { + struct list_head *pos; + + list_for_each(pos, &expl_head) + { + struct expl_entry *curr = (struct expl_entry *)pos; + + if (curr->expires > e->expires) + break; + } + list_add(&e->l, pos->prev); + } + return 1; } static inline struct expl_entry *__kaodv_expl_find(__u32 daddr) { - struct list_head *pos; + struct list_head *pos; - list_for_each(pos, &expl_head) { - struct expl_entry *e = (struct expl_entry *)pos; + list_for_each(pos, &expl_head) + { + struct expl_entry *e = (struct expl_entry *)pos; - if (e->daddr == daddr) - return e; - } - return NULL; + if (e->daddr == daddr) + return e; + } + return NULL; } static inline int __kaodv_expl_del(struct expl_entry *e) { - if (e == NULL) - return 0; + if (e == NULL) + return 0; - if (list_is_first(e)) { + if (list_is_first(e)) { - list_del(&e->l); + list_del(&e->l); #ifdef EXPL_TIMER - if (!list_empty(&expl_head)) { - /* Get the first entry */ - struct expl_entry *f = - (struct expl_entry *)expl_head.next; - - /* Update the timer */ - mod_timer(&expl_timer, f->expires); - } + if (!list_empty(&expl_head)) { + /* Get the first entry */ + struct expl_entry *f = (struct expl_entry *)expl_head.next; + + /* Update the timer */ + mod_timer(&expl_timer, f->expires); + } #endif - } else - list_del(&e->l); + } else + list_del(&e->l); - expl_len--; + expl_len--; - return 1; + return 1; } int kaodv_expl_del(__u32 daddr) { - int res; - struct expl_entry *e; + int res; + struct expl_entry *e; + + write_lock_bh(&expl_lock); - write_lock_bh(&expl_lock); + e = __kaodv_expl_find(daddr); - e = __kaodv_expl_find(daddr); + if (e == NULL) { + res = 0; + goto unlock; + } - if (e == NULL) { - res = 0; - goto unlock; - } - - res = __kaodv_expl_del(e); + res = __kaodv_expl_del(e); - if (res) { - kfree(e); - } - unlock: - write_unlock_bh(&expl_lock); + if (res) { + kfree(e); + } +unlock: + write_unlock_bh(&expl_lock); - return res; + return res; } int kaodv_expl_get(__u32 daddr, struct expl_entry *e_in) { - struct expl_entry *e; - int res = 0; + struct expl_entry *e; + int res = 0; -/* printk("Checking activeness\n"); */ - read_lock_bh(&expl_lock); - e = __kaodv_expl_find(daddr); + /* printk("Checking activeness\n"); */ + read_lock_bh(&expl_lock); + e = __kaodv_expl_find(daddr); - if (e) { - res = 1; - if (e_in) - memcpy(e_in, e, sizeof(struct expl_entry)); - } + if (e) { + res = 1; + if (e_in) + memcpy(e_in, e, sizeof(struct expl_entry)); + } - read_unlock_bh(&expl_lock); - return res; + read_unlock_bh(&expl_lock); + return res; } int kaodv_expl_add(__u32 daddr, __u32 nhop, unsigned long time, - unsigned short flags, int ifindex) + unsigned short flags, int ifindex) { - struct expl_entry *e; - int status = 0; + struct expl_entry *e; + int status = 0; - if (kaodv_expl_get(daddr, NULL)) - return 0; + if (kaodv_expl_get(daddr, NULL)) + return 0; - e = kmalloc(sizeof(struct expl_entry), GFP_ATOMIC); + e = kmalloc(sizeof(struct expl_entry), GFP_ATOMIC); - if (e == NULL) { - printk(KERN_ERR "expl: OOM in expl_add\n"); - return -ENOMEM; - } + if (e == NULL) { + printk(KERN_ERR "expl: OOM in expl_add\n"); + return -ENOMEM; + } - e->daddr = daddr; - e->nhop = nhop; - e->flags = flags; - e->ifindex = ifindex; - e->expires = jiffies + (time * HZ) / 1000; + e->daddr = daddr; + e->nhop = nhop; + e->flags = flags; + e->ifindex = ifindex; + e->expires = jiffies + (time * HZ) / 1000; - write_lock_bh(&expl_lock); + write_lock_bh(&expl_lock); - status = __kaodv_expl_add(e); + status = __kaodv_expl_add(e); - if (status) - expl_len++; + if (status) + expl_len++; #ifdef EXPL_TIMER - /* If the added element was added first in the list we update the timer */ - if (status && list_is_first(e)) { - - if (timer_pending(&expl_timer)) - mod_timer(&expl_timer, e->expires); - else { - expl_timer.function = expl_timeout; - expl_timer.expires = e->expires; - expl_timer.data = 0; - add_timer(&expl_timer); - } - } + /* If the added element was added first in the list we update the timer */ + if (status && list_is_first(e)) { + + if (timer_pending(&expl_timer)) + mod_timer(&expl_timer, e->expires); + else { + expl_timer.function = expl_timeout; + expl_timer.expires = e->expires; + expl_timer.data = 0; + add_timer(&expl_timer); + } + } #endif - write_unlock_bh(&expl_lock); + write_unlock_bh(&expl_lock); - if (status < 0) - kfree(e); + if (status < 0) + kfree(e); - return status; + return status; } static int kaodv_expl_print(char *buf) { - struct list_head *pos; - int len = 0; + struct list_head *pos; + int len = 0; - read_lock_bh(&expl_lock); + read_lock_bh(&expl_lock); - len += sprintf(buf, "# Total entries: %u\n", expl_len); - len += sprintf(buf + len, "# %-15s %-15s %-5s %-5s Expires\n", - "Addr", "Nhop", "Flags", "Iface"); + len += sprintf(buf, "# Total entries: %u\n", expl_len); + len += sprintf(buf + len, "# %-15s %-15s %-5s %-5s Expires\n", "Addr", + "Nhop", "Flags", "Iface"); - list_for_each(pos, &expl_head) { - char addr[16], nhop[16], flags[4]; - struct net_device *dev; - int num_flags = 0; - struct expl_entry *e = (struct expl_entry *)pos; + list_for_each(pos, &expl_head) + { + char addr[16], nhop[16], flags[4]; + struct net_device *dev; + int num_flags = 0; + struct expl_entry *e = (struct expl_entry *)pos; -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)) - dev = dev_get_by_index(e->ifindex); +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)) + dev = dev_get_by_index(e->ifindex); #else - dev = dev_get_by_index(&init_net, e->ifindex); + dev = dev_get_by_index(&init_net, e->ifindex); #endif - if (!dev) - continue; + if (!dev) + continue; - sprintf(addr, "%d.%d.%d.%d", - 0x0ff & e->daddr, - 0x0ff & (e->daddr >> 8), - 0x0ff & (e->daddr >> 16), 0x0ff & (e->daddr >> 24)); + sprintf(addr, "%d.%d.%d.%d", 0x0ff & e->daddr, 0x0ff & (e->daddr >> 8), + 0x0ff & (e->daddr >> 16), 0x0ff & (e->daddr >> 24)); - sprintf(nhop, "%d.%d.%d.%d", - 0x0ff & e->nhop, - 0x0ff & (e->nhop >> 8), - 0x0ff & (e->nhop >> 16), 0x0ff & (e->nhop >> 24)); + sprintf(nhop, "%d.%d.%d.%d", 0x0ff & e->nhop, 0x0ff & (e->nhop >> 8), + 0x0ff & (e->nhop >> 16), 0x0ff & (e->nhop >> 24)); - if (e->flags & KAODV_RT_GW_ENCAP) - flags[num_flags++] = 'E'; + if (e->flags & KAODV_RT_GW_ENCAP) + flags[num_flags++] = 'E'; - if (e->flags & KAODV_RT_REPAIR) - flags[num_flags++] = 'R'; + if (e->flags & KAODV_RT_REPAIR) + flags[num_flags++] = 'R'; - flags[num_flags] = '\0'; + flags[num_flags] = '\0'; - len += sprintf(buf + len, " %-15s %-15s %-5s %-5s %lu\n", - addr, nhop, flags, dev->name, - (e->expires - jiffies) * 1000 / HZ); + len += sprintf(buf + len, " %-15s %-15s %-5s %-5s %lu\n", addr, nhop, + flags, dev->name, (e->expires - jiffies) * 1000 / HZ); - dev_put(dev); - } + dev_put(dev); + } - read_unlock_bh(&expl_lock); - return len; + read_unlock_bh(&expl_lock); + return len; } -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)) -static int -kaodv_expl_proc_info(char *buffer, char **start, off_t offset, int length) +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)) +static int kaodv_expl_proc_info(char *buffer, char **start, off_t offset, + int length) { - int len; + int len; - len = kaodv_expl_print(buffer); + len = kaodv_expl_print(buffer); - *start = buffer + offset; - len -= offset; - if (len > length) - len = length; - else if (len < 0) - len = 0; - return len; + *start = buffer + offset; + len -= offset; + if (len > length) + len = length; + else if (len < 0) + len = 0; + return len; } #else static int kaodv_expl_proc_info(char *page, char **start, off_t off, int count, - int *eof, void *data) + int *eof, void *data) { - int len; + int len; - len = kaodv_expl_print(page); + len = kaodv_expl_print(page); - *start = page + off; - len -= off; - if (len > count) - len = count; - else if (len < 0) - len = 0; - return len; + *start = page + off; + len -= off; + if (len > count) + len = count; + else if (len < 0) + len = 0; + return len; } #endif int kaodv_expl_update(__u32 daddr, __u32 nhop, unsigned long time, - unsigned short flags, int ifindex) + unsigned short flags, int ifindex) { - int ret = 0; - struct expl_entry *e; + int ret = 0; + struct expl_entry *e; - write_lock_bh(&expl_lock); + write_lock_bh(&expl_lock); - e = __kaodv_expl_find(daddr); + e = __kaodv_expl_find(daddr); - if (e == NULL) { - /* printk("expl_update: No entry to update!\n"); */ - ret = -1; - goto unlock; - } - e->nhop = nhop; - e->flags = flags; - e->ifindex = ifindex; - /* Update expire time */ - e->expires = jiffies + (time * HZ) / 1000; + if (e == NULL) { + /* printk("expl_update: No entry to update!\n"); */ + ret = -1; + goto unlock; + } + e->nhop = nhop; + e->flags = flags; + e->ifindex = ifindex; + /* Update expire time */ + e->expires = jiffies + (time * HZ) / 1000; - /* Remove from list */ - list_del(&e->l); + /* Remove from list */ + list_del(&e->l); - __kaodv_expl_add(e); + __kaodv_expl_add(e); #ifdef EXPL_TIMER - __kaodv_expl_set_next_timeout(); + __kaodv_expl_set_next_timeout(); #endif - unlock: - write_unlock_bh(&expl_lock); +unlock: + write_unlock_bh(&expl_lock); - return ret; + return ret; } void kaodv_expl_flush(void) { #ifdef EXPL_TIMER - if (timer_pending(&expl_timer)) - del_timer(&expl_timer); + if (timer_pending(&expl_timer)) + del_timer(&expl_timer); #endif - write_lock_bh(&expl_lock); + write_lock_bh(&expl_lock); - __kaodv_expl_flush(); + __kaodv_expl_flush(); - write_unlock_bh(&expl_lock); + write_unlock_bh(&expl_lock); } void kaodv_expl_init(void) { -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)) - proc_net_create("kaodv_expl", 0, kaodv_expl_proc_info); +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)) + proc_net_create("kaodv_expl", 0, kaodv_expl_proc_info); #else - create_proc_read_entry("kaodv_expl", 0, - init_net.proc_net, kaodv_expl_proc_info, NULL); + create_proc_read_entry("kaodv_expl", 0, init_net.proc_net, + kaodv_expl_proc_info, NULL); #endif - expl_len = 0; + expl_len = 0; #ifdef EXPL_TIMER - init_timer(&expl_timer); + init_timer(&expl_timer); #endif } void kaodv_expl_fini(void) { - kaodv_expl_flush(); -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)) - proc_net_remove("kaodv_expl"); + kaodv_expl_flush(); +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)) + proc_net_remove("kaodv_expl"); #else - proc_net_remove(&init_net, "kaodv_expl"); + proc_net_remove(&init_net, "kaodv_expl"); #endif } diff --git a/lnx/kaodv-expl.h b/lnx/kaodv-expl.h index 8c5a0d9..8b1d348 100644 --- a/lnx/kaodv-expl.h +++ b/lnx/kaodv-expl.h @@ -27,25 +27,25 @@ #include struct expl_entry { - struct list_head l; - unsigned long expires; - unsigned short flags; - __u32 daddr; - __u32 nhop; - int ifindex; + struct list_head l; + unsigned long expires; + unsigned short flags; + __u32 daddr; + __u32 nhop; + int ifindex; }; void kaodv_expl_init(void); void kaodv_expl_flush(void); int kaodv_expl_get(__u32 daddr, struct expl_entry *e_in); int kaodv_expl_add(__u32 daddr, __u32 nhop, unsigned long time, - unsigned short flags, int ifindex); + unsigned short flags, int ifindex); int kaodv_expl_update(__u32 daddr, __u32 nhop, unsigned long time, - unsigned short flags, int ifindex); + unsigned short flags, int ifindex); int kaodv_expl_del(__u32 daddr); void kaodv_expl_fini(void); -#endif /* __KERNEL__ */ +#endif /* __KERNEL__ */ -#endif /* _KAODV_EXPL_H */ +#endif /* _KAODV_EXPL_H */ diff --git a/lnx/kaodv-ipenc.c b/lnx/kaodv-ipenc.c index b34ff01..7d47d0f 100644 --- a/lnx/kaodv-ipenc.c +++ b/lnx/kaodv-ipenc.c @@ -16,76 +16,74 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, + * Authors: Erik Nordström, * *****************************************************************************/ -#include #include #include +#include -#include "kaodv-ipenc.h" #include "kaodv-expl.h" /* For print_ip() */ +#include "kaodv-ipenc.h" #include "kaodv.h" /* Simple function (based on R. Stevens) to calculate IP header checksum */ static u_int16_t ip_csum(unsigned short *buf, int nshorts) { u_int32_t sum; - + for (sum = 0; nshorts > 0; nshorts--) { sum += *buf++; } - + sum = (sum >> 16) + (sum & 0xffff); sum += (sum >> 16); - + return ~sum; } struct sk_buff *ip_pkt_encapsulate(struct sk_buff *skb, __u32 dest) { - - struct min_ipenc_hdr *ipe; + struct min_ipenc_hdr *ipe; struct sk_buff *nskb; struct iphdr *iph; - + /* Allocate new data space at head */ nskb = skb_copy_expand(skb, skb_headroom(skb), - skb_tailroom(skb) + - sizeof(struct min_ipenc_hdr), - GFP_ATOMIC); + skb_tailroom(skb) + sizeof(struct min_ipenc_hdr), + GFP_ATOMIC); if (nskb == NULL) { - printk("Could not allocate new skb\n"); - kfree_skb(skb); - return NULL; + printk("Could not allocate new skb\n"); + kfree_skb(skb); + return NULL; } /* Set old owner */ if (skb->sk != NULL) - skb_set_owner_w(nskb, skb->sk); + skb_set_owner_w(nskb, skb->sk); iph = SKB_NETWORK_HDR_IPH(skb); skb_put(nskb, sizeof(struct min_ipenc_hdr)); - + /* Move the IP header */ memcpy(nskb->data, skb->data, (iph->ihl << 2)); /* Move the data */ - memcpy(nskb->data + (iph->ihl << 2) + sizeof(struct min_ipenc_hdr), - skb->data + (iph->ihl << 2), skb->len - (iph->ihl << 2)); - + memcpy(nskb->data + (iph->ihl << 2) + sizeof(struct min_ipenc_hdr), + skb->data + (iph->ihl << 2), skb->len - (iph->ihl << 2)); + kfree_skb(skb); skb = nskb; - + /* Update pointers */ - + SKB_SET_NETWORK_HDR(skb, 0); iph = SKB_NETWORK_HDR_IPH(skb); ipe = (struct min_ipenc_hdr *)(SKB_NETWORK_HDR_RAW(skb) + (iph->ihl << 2)); - + /* Save the old ip header information in the encapsulation header */ ipe->protocol = iph->protocol; ipe->s = 0; /* No source address field in the encapsulation header */ @@ -97,15 +95,15 @@ struct sk_buff *ip_pkt_encapsulate(struct sk_buff *skb, __u32 dest) iph->daddr = dest; iph->protocol = IPPROTO_MIPE; iph->tot_len = htons(ntohs(iph->tot_len) + sizeof(struct min_ipenc_hdr)); - + /* Recalculate checksums */ ipe->check = ip_csum((unsigned short *)ipe, 4); ip_send_check(iph); if (iph->id == 0) - ip_select_ident(iph, skb_dst(skb), NULL); - + ip_select_ident(iph, skb_dst(skb), NULL); + return skb; } @@ -119,19 +117,19 @@ struct sk_buff *ip_pkt_decapsulate(struct sk_buff *skb) iph->protocol = ipe->protocol; iph->daddr = ipe->daddr; - + /* Shift the data to the left, overwriting the encap header */ - memmove(skb->data + (iph->ihl << 2), - skb->data + (iph->ihl << 2) + sizeof(struct min_ipenc_hdr), - skb->len - (iph->ihl << 2) - sizeof(struct min_ipenc_hdr)); - + memmove(skb->data + (iph->ihl << 2), + skb->data + (iph->ihl << 2) + sizeof(struct min_ipenc_hdr), + skb->len - (iph->ihl << 2) - sizeof(struct min_ipenc_hdr)); + skb_trim(skb, skb->len - sizeof(struct min_ipenc_hdr)); - + SKB_SET_NETWORK_HDR(skb, 0); iph = SKB_NETWORK_HDR_IPH(skb); - iph->tot_len = htons((ntohs(iph->tot_len) - sizeof(struct min_ipenc_hdr))); + iph->tot_len = htons((ntohs(iph->tot_len) - sizeof(struct min_ipenc_hdr))); ip_send_check(iph); - + return skb; } diff --git a/lnx/kaodv-ipenc.h b/lnx/kaodv-ipenc.h index f291408..44511d8 100644 --- a/lnx/kaodv-ipenc.h +++ b/lnx/kaodv-ipenc.h @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, + * Authors: Erik Nordström, * *****************************************************************************/ /* Definitions for Minimal IP Encapsulation (RFC 2004) */ @@ -24,27 +24,27 @@ #ifndef _KAODV_IPENC_H #define _KAODV_IPENC_H +#include #include #include -#include #define IPPROTO_MIPE 55 struct min_ipenc_hdr { u_int8_t protocol; #if defined(__LITTLE_ENDIAN) - u_int8_t res:7; - u_int8_t s:1; + u_int8_t res : 7; + u_int8_t s : 1; #elif defined(__BIG_ENDIAN) - u_int8_t s:1; - u_int8_t res:7; -#else -#error "Adjust your defines" + u_int8_t s : 1; + u_int8_t res : 7; +#else +#error "Adjust your defines" #endif u_int16_t check; u_int32_t daddr; - /* u_int32_t saddr; */ + /* u_int32_t saddr; */ }; struct sk_buff *ip_pkt_encapsulate(struct sk_buff *skb, __u32 dest); struct sk_buff *ip_pkt_decapsulate(struct sk_buff *skb); -#endif /* _KAODV_IPENC_H */ +#endif /* _KAODV_IPENC_H */ diff --git a/lnx/kaodv-mod.c b/lnx/kaodv-mod.c index 0157695..8f33218 100644 --- a/lnx/kaodv-mod.c +++ b/lnx/kaodv-mod.c @@ -16,42 +16,42 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Author: Erik Nordström, - * + * Author: Erik Nordström, + * *****************************************************************************/ #include -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)) +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)) #include #endif #ifdef KERNEL26 #include #endif -#include -#include -#include -#include #include -#include -#include +#include #include +#include +#include +#include +#include +#include #include #include -#include -#include -#include +#include +#include #include -#include +#include #include +#include -#include "kaodv-mod.h" +#include "kaodv-debug.h" #include "kaodv-expl.h" +#include "kaodv-ipenc.h" +#include "kaodv-mod.h" #include "kaodv-netlink.h" #include "kaodv-queue.h" -#include "kaodv-ipenc.h" -#include "kaodv-debug.h" #include "kaodv.h" -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)) +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)) #define NF_INET_PRE_ROUTING NF_IP_PRE_ROUTING #define NF_INET_LOCAL_IN NF_IP_LOCAL_IN #define NF_INET_FORWARD NF_IP_FORWARD @@ -68,11 +68,11 @@ static unsigned long pkts_dropped = 0; int qual_th = 0; int is_gateway = 1; int active_route_timeout = 3000; -//static unsigned int loindex = 0; +// static unsigned int loindex = 0; -MODULE_DESCRIPTION - ("AODV-UU kernel support. © Uppsala University & Ericsson AB"); -MODULE_AUTHOR("Erik Nordström"); +MODULE_DESCRIPTION( + "AODV-UU kernel support. � Uppsala University & Ericsson AB"); +MODULE_AUTHOR("Erik Nordström"); #ifdef MODULE_LICENSE MODULE_LICENSE("GPL"); #endif @@ -81,209 +81,200 @@ MODULE_LICENSE("GPL"); #define ADDR_BROADCAST 2 void kaodv_update_route_timeouts(int hooknum, const struct net_device *dev, - struct iphdr *iph) + struct iphdr *iph) { - struct expl_entry e; - struct in_addr bcaddr; - int res; + struct expl_entry e; + struct in_addr bcaddr; + int res; - bcaddr.s_addr = 0; /* Stop compiler from complaining about - * uninitialized bcaddr */ + bcaddr.s_addr = 0; /* Stop compiler from complaining about + * uninitialized bcaddr */ - res = if_info_from_ifindex(NULL, &bcaddr, dev->ifindex); + res = if_info_from_ifindex(NULL, &bcaddr, dev->ifindex); - if (res < 0) - return; + if (res < 0) + return; - if (hooknum == NF_INET_PRE_ROUTING) - kaodv_netlink_send_rt_update_msg(PKT_INBOUND, iph->saddr, - iph->daddr, dev->ifindex); - else if (iph->daddr != INADDR_BROADCAST && iph->daddr != bcaddr.s_addr) - kaodv_netlink_send_rt_update_msg(PKT_OUTBOUND, iph->saddr, - iph->daddr, dev->ifindex); + if (hooknum == NF_INET_PRE_ROUTING) + kaodv_netlink_send_rt_update_msg(PKT_INBOUND, iph->saddr, iph->daddr, + dev->ifindex); + else if (iph->daddr != INADDR_BROADCAST && iph->daddr != bcaddr.s_addr) + kaodv_netlink_send_rt_update_msg(PKT_OUTBOUND, iph->saddr, iph->daddr, + dev->ifindex); - /* First update forward route and next hop */ - if (kaodv_expl_get(iph->daddr, &e)) { + /* First update forward route and next hop */ + if (kaodv_expl_get(iph->daddr, &e)) { - kaodv_expl_update(e.daddr, e.nhop, ACTIVE_ROUTE_TIMEOUT, - e.flags, dev->ifindex); + kaodv_expl_update(e.daddr, e.nhop, ACTIVE_ROUTE_TIMEOUT, e.flags, + dev->ifindex); - if (e.nhop != e.daddr && kaodv_expl_get(e.nhop, &e)) - kaodv_expl_update(e.daddr, e.nhop, ACTIVE_ROUTE_TIMEOUT, - e.flags, dev->ifindex); - } - /* Update reverse route */ - if (kaodv_expl_get(iph->saddr, &e)) { + if (e.nhop != e.daddr && kaodv_expl_get(e.nhop, &e)) + kaodv_expl_update(e.daddr, e.nhop, ACTIVE_ROUTE_TIMEOUT, e.flags, + dev->ifindex); + } + /* Update reverse route */ + if (kaodv_expl_get(iph->saddr, &e)) { - kaodv_expl_update(e.daddr, e.nhop, ACTIVE_ROUTE_TIMEOUT, - e.flags, dev->ifindex); + kaodv_expl_update(e.daddr, e.nhop, ACTIVE_ROUTE_TIMEOUT, e.flags, + dev->ifindex); - if (e.nhop != e.daddr && kaodv_expl_get(e.nhop, &e)) - kaodv_expl_update(e.daddr, e.nhop, ACTIVE_ROUTE_TIMEOUT, - e.flags, dev->ifindex); - } + if (e.nhop != e.daddr && kaodv_expl_get(e.nhop, &e)) + kaodv_expl_update(e.daddr, e.nhop, ACTIVE_ROUTE_TIMEOUT, e.flags, + dev->ifindex); + } } -static unsigned int kaodv_hook(unsigned int hooknum, - struct sk_buff *skb, - const struct net_device *in, - const struct net_device *out, - int (*okfn) (struct sk_buff *)) +static unsigned int kaodv_hook(unsigned int hooknum, struct sk_buff *skb, + const struct net_device *in, + const struct net_device *out, + int (*okfn)(struct sk_buff *)) { - struct iphdr *iph = SKB_NETWORK_HDR_IPH(skb); - struct expl_entry e; - struct in_addr ifaddr, bcaddr; - int res = 0; + struct iphdr *iph = SKB_NETWORK_HDR_IPH(skb); + struct expl_entry e; + struct in_addr ifaddr, bcaddr; + int res = 0; + + memset(&ifaddr, 0, sizeof(struct in_addr)); + memset(&bcaddr, 0, sizeof(struct in_addr)); - memset(&ifaddr, 0, sizeof(struct in_addr)); - memset(&bcaddr, 0, sizeof(struct in_addr)); + /* We are only interested in IP packets */ + if (iph == NULL) + return NF_ACCEPT; - /* We are only interested in IP packets */ - if (iph == NULL) - return NF_ACCEPT; - - /* We want AODV control messages to go through directly to the - * AODV socket.... */ - if (iph && iph->protocol == IPPROTO_UDP) { - struct udphdr *udph; + /* We want AODV control messages to go through directly to the + * AODV socket.... */ + if (iph && iph->protocol == IPPROTO_UDP) { + struct udphdr *udph; - udph = (struct udphdr *)((char *)iph + (iph->ihl << 2)); + udph = (struct udphdr *)((char *)iph + (iph->ihl << 2)); - if (ntohs(udph->dest) == AODV_PORT || - ntohs(udph->source) == AODV_PORT) { + if (ntohs(udph->dest) == AODV_PORT || + ntohs(udph->source) == AODV_PORT) { #ifdef CONFIG_QUAL_THRESHOLD -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)) - qual = (int)(skb)->__unused; -#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)) - qual = (skb)->iwq.qual; +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)) + qual = (int)(skb)->__unused; +#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)) + qual = (skb)->iwq.qual; #endif - if (qual_th && hooknum == NF_INET_PRE_ROUTING) { + if (qual_th && hooknum == NF_INET_PRE_ROUTING) { - if (qual && qual < qual_th) { - pkts_dropped++; - return NF_DROP; - } - } + if (qual && qual < qual_th) { + pkts_dropped++; + return NF_DROP; + } + } #endif /* CONFIG_QUAL_THRESHOLD */ - if (hooknum == NF_INET_PRE_ROUTING && in) - kaodv_update_route_timeouts(hooknum, in, iph); - - return NF_ACCEPT; - } - } - - if (hooknum == NF_INET_PRE_ROUTING) - res = if_info_from_ifindex(&ifaddr, &bcaddr, in->ifindex); - else - res = if_info_from_ifindex(&ifaddr, &bcaddr, out->ifindex); - - if (res < 0) - return NF_ACCEPT; - - - /* Ignore broadcast and multicast packets */ - if (iph->daddr == INADDR_BROADCAST || - IN_MULTICAST(ntohl(iph->daddr)) || - iph->daddr == bcaddr.s_addr) - return NF_ACCEPT; - - - /* Check which hook the packet is on... */ - switch (hooknum) { - case NF_INET_PRE_ROUTING: - kaodv_update_route_timeouts(hooknum, in, iph); - - /* If we are a gateway maybe we need to decapsulate? */ - if (is_gateway && iph->protocol == IPPROTO_MIPE && - iph->daddr == ifaddr.s_addr) { - ip_pkt_decapsulate(skb); - iph = SKB_NETWORK_HDR_IPH(skb); - return NF_ACCEPT; - } - /* Ignore packets generated locally or that are for this - * node. */ - if (iph->saddr == ifaddr.s_addr || - iph->daddr == ifaddr.s_addr) { - return NF_ACCEPT; - } - /* Check for unsolicited data packets */ - else if (!kaodv_expl_get(iph->daddr, &e)) { - kaodv_netlink_send_rerr_msg(PKT_INBOUND, iph->saddr, - iph->daddr, in->ifindex); - return NF_DROP; - - } - /* Check if we should repair the route */ - else if (e.flags & KAODV_RT_REPAIR) { - - kaodv_netlink_send_rt_msg(KAODVM_REPAIR, iph->saddr, - iph->daddr); - - kaodv_queue_enqueue_packet(skb, okfn); - - return NF_STOLEN; - } - break; - case NF_INET_LOCAL_OUT: - - if (!kaodv_expl_get(iph->daddr, &e) || - (e.flags & KAODV_RT_REPAIR)) { - - if (!kaodv_queue_find(iph->daddr)) - kaodv_netlink_send_rt_msg(KAODVM_ROUTE_REQ, - 0, - iph->daddr); - - kaodv_queue_enqueue_packet(skb, okfn); - - return NF_STOLEN; - - } else if (e.flags & KAODV_RT_GW_ENCAP) { - /* Make sure that also the virtual Internet - * dest entry is refreshed */ - kaodv_update_route_timeouts(hooknum, out, iph); - - skb = ip_pkt_encapsulate(skb, e.nhop); - - if (!skb) - return NF_STOLEN; - - ip_route_me_harder(skb, RTN_LOCAL); - } - break; - case NF_INET_POST_ROUTING: - kaodv_update_route_timeouts(hooknum, out, iph); - } - return NF_ACCEPT; + if (hooknum == NF_INET_PRE_ROUTING && in) + kaodv_update_route_timeouts(hooknum, in, iph); + + return NF_ACCEPT; + } + } + + if (hooknum == NF_INET_PRE_ROUTING) + res = if_info_from_ifindex(&ifaddr, &bcaddr, in->ifindex); + else + res = if_info_from_ifindex(&ifaddr, &bcaddr, out->ifindex); + + if (res < 0) + return NF_ACCEPT; + + /* Ignore broadcast and multicast packets */ + if (iph->daddr == INADDR_BROADCAST || IN_MULTICAST(ntohl(iph->daddr)) || + iph->daddr == bcaddr.s_addr) + return NF_ACCEPT; + + /* Check which hook the packet is on... */ + switch (hooknum) { + case NF_INET_PRE_ROUTING: + kaodv_update_route_timeouts(hooknum, in, iph); + + /* If we are a gateway maybe we need to decapsulate? */ + if (is_gateway && iph->protocol == IPPROTO_MIPE && + iph->daddr == ifaddr.s_addr) { + ip_pkt_decapsulate(skb); + iph = SKB_NETWORK_HDR_IPH(skb); + return NF_ACCEPT; + } + /* Ignore packets generated locally or that are for this + * node. */ + if (iph->saddr == ifaddr.s_addr || iph->daddr == ifaddr.s_addr) { + return NF_ACCEPT; + } + /* Check for unsolicited data packets */ + else if (!kaodv_expl_get(iph->daddr, &e)) { + kaodv_netlink_send_rerr_msg(PKT_INBOUND, iph->saddr, iph->daddr, + in->ifindex); + return NF_DROP; + + } + /* Check if we should repair the route */ + else if (e.flags & KAODV_RT_REPAIR) { + + kaodv_netlink_send_rt_msg(KAODVM_REPAIR, iph->saddr, iph->daddr); + + kaodv_queue_enqueue_packet(skb, okfn); + + return NF_STOLEN; + } + break; + case NF_INET_LOCAL_OUT: + + if (!kaodv_expl_get(iph->daddr, &e) || (e.flags & KAODV_RT_REPAIR)) { + + if (!kaodv_queue_find(iph->daddr)) + kaodv_netlink_send_rt_msg(KAODVM_ROUTE_REQ, 0, iph->daddr); + + kaodv_queue_enqueue_packet(skb, okfn); + + return NF_STOLEN; + + } else if (e.flags & KAODV_RT_GW_ENCAP) { + /* Make sure that also the virtual Internet + * dest entry is refreshed */ + kaodv_update_route_timeouts(hooknum, out, iph); + + skb = ip_pkt_encapsulate(skb, e.nhop); + + if (!skb) + return NF_STOLEN; + + ip_route_me_harder(skb, RTN_LOCAL); + } + break; + case NF_INET_POST_ROUTING: + kaodv_update_route_timeouts(hooknum, out, iph); + } + return NF_ACCEPT; } int kaodv_proc_info(char *buffer, char **start, off_t offset, int length) { - int len; - - len = - sprintf(buffer, - "qual threshold=%d\npkts dropped=%lu\nlast qual=%d\ngateway_mode=%d\n", - qual_th, pkts_dropped, qual, is_gateway); - - *start = buffer + offset; - len -= offset; - if (len > length) - len = length; - else if (len < 0) - len = 0; - return len; + int len; + + len = sprintf( + buffer, + "qual threshold=%d\npkts dropped=%lu\nlast qual=%d\ngateway_mode=%d\n", + qual_th, pkts_dropped, qual, is_gateway); + + *start = buffer + offset; + len -= offset; + if (len > length) + len = length; + else if (len < 0) + len = 0; + return len; } /* * Called when the module is inserted in the kernel. */ -static char *ifname[MAX_INTERFACES] = { "eth0" }; +static char *ifname[MAX_INTERFACES] = {"eth0"}; #ifdef KERNEL26 static int num_parms = 0; -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)) +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 10)) module_param_array(ifname, charp, num_parms, 0444); #else module_param_array(ifname, charp, &num_parms, 0444); @@ -295,41 +286,42 @@ MODULE_PARM(qual_th, "i"); #endif static struct nf_hook_ops kaodv_ops[] = { - { - .hook = kaodv_hook, + { + .hook = kaodv_hook, #ifdef KERNEL26 - .owner = THIS_MODULE, + .owner = THIS_MODULE, #endif - .pf = PF_INET, - .hooknum = NF_INET_PRE_ROUTING, - .priority = NF_IP_PRI_FIRST, - }, - { - .hook = kaodv_hook, + .pf = PF_INET, + .hooknum = NF_INET_PRE_ROUTING, + .priority = NF_IP_PRI_FIRST, + }, + { + .hook = kaodv_hook, #ifdef KERNEL26 - .owner = THIS_MODULE, + .owner = THIS_MODULE, #endif - .pf = PF_INET, - .hooknum = NF_INET_LOCAL_OUT, - .priority = NF_IP_PRI_FILTER, - }, - { - .hook = kaodv_hook, + .pf = PF_INET, + .hooknum = NF_INET_LOCAL_OUT, + .priority = NF_IP_PRI_FILTER, + }, + { + .hook = kaodv_hook, #ifdef KERNEL26 - .owner = THIS_MODULE, + .owner = THIS_MODULE, #endif - .pf = PF_INET, - .hooknum = NF_INET_POST_ROUTING, - .priority = NF_IP_PRI_FILTER, - }, + .pf = PF_INET, + .hooknum = NF_INET_POST_ROUTING, + .priority = NF_IP_PRI_FILTER, + }, }; static int kaodv_read_proc(char *page, char **start, off_t off, int count, - int *eof, void *data) + int *eof, void *data) { int len; - len = sprintf(page, + len = sprintf( + page, "qual threshold=%d\npkts dropped=%lu\nlast qual=%d\ngateway_mode=%d\n", qual_th, pkts_dropped, qual, is_gateway); @@ -342,102 +334,100 @@ static int kaodv_read_proc(char *page, char **start, off_t off, int count, return len; } - static int __init kaodv_init(void) { - struct net_device *dev = NULL; - int i, ret = -ENOMEM; + struct net_device *dev = NULL; + int i, ret = -ENOMEM; #ifndef KERNEL26 - EXPORT_NO_SYMBOLS; + EXPORT_NO_SYMBOLS; #endif - kaodv_expl_init(); + kaodv_expl_init(); + + ret = kaodv_queue_init(); - ret = kaodv_queue_init(); + if (ret < 0) + return ret; - if (ret < 0) - return ret; + ret = kaodv_netlink_init(); - ret = kaodv_netlink_init(); + if (ret < 0) + goto cleanup_queue; - if (ret < 0) - goto cleanup_queue; + ret = nf_register_hook(&kaodv_ops[0]); - ret = nf_register_hook(&kaodv_ops[0]); + if (ret < 0) + goto cleanup_netlink; - if (ret < 0) - goto cleanup_netlink; + ret = nf_register_hook(&kaodv_ops[1]); - ret = nf_register_hook(&kaodv_ops[1]); + if (ret < 0) + goto cleanup_hook0; - if (ret < 0) - goto cleanup_hook0; + ret = nf_register_hook(&kaodv_ops[2]); - ret = nf_register_hook(&kaodv_ops[2]); + if (ret < 0) + goto cleanup_hook1; - if (ret < 0) - goto cleanup_hook1; + /* Prefetch network device info (ip, broadcast address, ifindex). */ + for (i = 0; i < MAX_INTERFACES; i++) { + if (!ifname[i]) + break; - /* Prefetch network device info (ip, broadcast address, ifindex). */ - for (i = 0; i < MAX_INTERFACES; i++) { - if (!ifname[i]) - break; + dev = dev_get_by_name(&init_net, ifname[i]); - dev = dev_get_by_name(&init_net, ifname[i]); + if (!dev) { + printk("No device %s available, ignoring!\n", ifname[i]); + continue; + } + if_info_add(dev); - if (!dev) { - printk("No device %s available, ignoring!\n", - ifname[i]); - continue; - } - if_info_add(dev); + dev_put(dev); + } - dev_put(dev); - } - -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)) - proc_net_create("kaodv", 0, kaodv_proc_info); +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)) + proc_net_create("kaodv", 0, kaodv_proc_info); #else if (!create_proc_read_entry("kaodv", 0, init_net.proc_net, kaodv_read_proc, - NULL)) + NULL)) KAODV_DEBUG("Could not create kaodv proc entry"); #endif - KAODV_DEBUG("Module init OK"); + KAODV_DEBUG("Module init OK"); - return ret; + return ret; cleanup_hook1: - nf_unregister_hook(&kaodv_ops[1]); + nf_unregister_hook(&kaodv_ops[1]); cleanup_hook0: - nf_unregister_hook(&kaodv_ops[0]); + nf_unregister_hook(&kaodv_ops[0]); cleanup_netlink: - kaodv_netlink_fini(); + kaodv_netlink_fini(); cleanup_queue: - kaodv_queue_fini(); + kaodv_queue_fini(); - return ret; + return ret; } /* - * Called when removing the module from memory... + * Called when removing the module from memory... */ static void __exit kaodv_exit(void) { - unsigned int i; - - if_info_purge(); - - for (i = 0; i < sizeof(kaodv_ops) / sizeof(struct nf_hook_ops); i++) - nf_unregister_hook(&kaodv_ops[i]); -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)) - proc_net_remove("kaodv"); + unsigned int i; + + if_info_purge(); + + for (i = 0; i < sizeof(kaodv_ops) / sizeof(struct nf_hook_ops); i++) + nf_unregister_hook(&kaodv_ops[i]); +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)) + proc_net_remove("kaodv"); #else - proc_net_remove(&init_net, "kaodv"); + proc_net_remove(&init_net, "kaodv"); #endif - kaodv_queue_fini(); - kaodv_expl_fini(); - kaodv_netlink_fini(); + kaodv_queue_fini(); + kaodv_expl_fini(); + kaodv_netlink_fini(); } module_init(kaodv_init); diff --git a/lnx/kaodv-mod.h b/lnx/kaodv-mod.h index d46adc9..3af814e 100644 --- a/lnx/kaodv-mod.h +++ b/lnx/kaodv-mod.h @@ -1,19 +1,19 @@ #ifndef _KAODV_MOD_H #define _KAODV_MOD_H -#include +#include "kaodv-ipenc.h" #include #include +#include #include -#include "kaodv-ipenc.h" /* Interface information */ struct if_info { - struct list_head l; - struct in_addr if_addr; - struct in_addr bc_addr; - int orig_mtu; - struct net_device *dev; + struct list_head l; + struct in_addr if_addr; + struct in_addr bc_addr; + int orig_mtu; + struct net_device *dev; }; static LIST_HEAD(ifihead); @@ -25,91 +25,92 @@ static rwlock_t ifilock = RW_LOCK_UNLOCKED; static inline int if_info_add(struct net_device *dev) { - struct if_info *ifi; - struct in_device *indev; + struct if_info *ifi; + struct in_device *indev; + + ifi = (struct if_info *)kmalloc(sizeof(struct if_info), GFP_ATOMIC); - ifi = (struct if_info *)kmalloc(sizeof(struct if_info), GFP_ATOMIC); + if (!ifi) + return -1; - if (!ifi) - return -1; + ifi->dev = dev; - ifi->dev = dev; - - dev_hold(dev); + dev_hold(dev); - ifi->orig_mtu = dev->mtu; + ifi->orig_mtu = dev->mtu; - rtnl_lock(); - dev_set_mtu(dev, dev->mtu - MIN_IP_ENCAP_SIZE); - rtnl_unlock(); + rtnl_lock(); + dev_set_mtu(dev, dev->mtu - MIN_IP_ENCAP_SIZE); + rtnl_unlock(); - indev = in_dev_get(dev); + indev = in_dev_get(dev); - if (indev) { - struct in_ifaddr **ifap; - struct in_ifaddr *ifa; + if (indev) { + struct in_ifaddr **ifap; + struct in_ifaddr *ifa; - for (ifap = &indev->ifa_list; (ifa = *ifap) != NULL; - ifap = &ifa->ifa_next) - if (!strcmp(dev->name, ifa->ifa_label)) - break; + for (ifap = &indev->ifa_list; (ifa = *ifap) != NULL; + ifap = &ifa->ifa_next) + if (!strcmp(dev->name, ifa->ifa_label)) + break; - if (ifa) { - ifi->if_addr.s_addr = ifa->ifa_address; - ifi->bc_addr.s_addr = ifa->ifa_broadcast; - } - in_dev_put(indev); - } + if (ifa) { + ifi->if_addr.s_addr = ifa->ifa_address; + ifi->bc_addr.s_addr = ifa->ifa_broadcast; + } + in_dev_put(indev); + } - write_lock(&ifilock); - list_add(&ifi->l, &ifihead); - write_unlock(&ifilock); + write_lock(&ifilock); + list_add(&ifi->l, &ifihead); + write_unlock(&ifilock); - return 0; + return 0; } static inline void if_info_purge(void) { - struct list_head *pos, *n; - - write_lock(&ifilock); - list_for_each_safe(pos, n, &ifihead) { - struct if_info *ifi = (struct if_info *)pos; - list_del(&ifi->l); - rtnl_lock(); - dev_set_mtu(ifi->dev, ifi->orig_mtu); - rtnl_unlock(); - dev_put(ifi->dev); - kfree(ifi); - } - write_unlock(&ifilock); + struct list_head *pos, *n; + + write_lock(&ifilock); + list_for_each_safe(pos, n, &ifihead) + { + struct if_info *ifi = (struct if_info *)pos; + list_del(&ifi->l); + rtnl_lock(); + dev_set_mtu(ifi->dev, ifi->orig_mtu); + rtnl_unlock(); + dev_put(ifi->dev); + kfree(ifi); + } + write_unlock(&ifilock); } -static inline int if_info_from_ifindex(struct in_addr *ifa, - struct in_addr *bc, - int ifindex) +static inline int if_info_from_ifindex(struct in_addr *ifa, struct in_addr *bc, + int ifindex) { - struct list_head *pos; - int res = -1; - - read_lock(&ifilock); - list_for_each(pos, &ifihead) { - struct if_info *ifi = (struct if_info *)pos; - if (ifi->dev->ifindex == ifindex) { - if (ifa) - *ifa = ifi->if_addr; - - if (bc) - *bc = ifi->bc_addr; - res = 0; - break; - } - } - read_unlock(&ifilock); - - return res; + struct list_head *pos; + int res = -1; + + read_lock(&ifilock); + list_for_each(pos, &ifihead) + { + struct if_info *ifi = (struct if_info *)pos; + if (ifi->dev->ifindex == ifindex) { + if (ifa) + *ifa = ifi->if_addr; + + if (bc) + *bc = ifi->bc_addr; + res = 0; + break; + } + } + read_unlock(&ifilock); + + return res; } void kaodv_update_route_timeouts(int hooknum, const struct net_device *dev, - struct iphdr *iph); + struct iphdr *iph); #endif diff --git a/lnx/kaodv-netlink.c b/lnx/kaodv-netlink.c index dbd8ad9..43b0eae 100644 --- a/lnx/kaodv-netlink.c +++ b/lnx/kaodv-netlink.c @@ -20,13 +20,13 @@ * *****************************************************************************/ #include -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)) +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)) #include #endif #include +#include #include #include -#include #include #ifdef KERNEL26 @@ -34,10 +34,10 @@ #endif #include -#include "kaodv-netlink.h" +#include "kaodv-debug.h" #include "kaodv-expl.h" +#include "kaodv-netlink.h" #include "kaodv-queue.h" -#include "kaodv-debug.h" #include "kaodv.h" static int peer_pid; @@ -54,268 +54,270 @@ extern int active_route_timeout, qual_th, is_gateway; static struct sk_buff *kaodv_netlink_build_msg(int type, void *data, int len) { - unsigned char *old_tail; - size_t size = 0; - struct sk_buff *skb; - struct nlmsghdr *nlh; - void *m; + unsigned char *old_tail; + size_t size = 0; + struct sk_buff *skb; + struct nlmsghdr *nlh; + void *m; - size = NLMSG_SPACE(len); + size = NLMSG_SPACE(len); - skb = alloc_skb(size, GFP_ATOMIC); + skb = alloc_skb(size, GFP_ATOMIC); - if (!skb) - goto nlmsg_failure; + if (!skb) + goto nlmsg_failure; - old_tail = SKB_TAIL_PTR(skb); - nlh = NLMSG_PUT(skb, 0, 0, type, size - sizeof(*nlh)); + old_tail = SKB_TAIL_PTR(skb); + nlh = NLMSG_PUT(skb, 0, 0, type, size - sizeof(*nlh)); - m = NLMSG_DATA(nlh); + m = NLMSG_DATA(nlh); - memcpy(m, data, len); - - nlh->nlmsg_len = SKB_TAIL_PTR(skb) - old_tail; - NETLINK_CB(skb).pid = 0; /* from kernel */ - - return skb; + memcpy(m, data, len); - nlmsg_failure: - if (skb) - kfree_skb(skb); + nlh->nlmsg_len = SKB_TAIL_PTR(skb) - old_tail; + NETLINK_CB(skb).pid = 0; /* from kernel */ - printk(KERN_ERR "kaodv: error creating rt timeout message\n"); + return skb; - return NULL; +nlmsg_failure: + if (skb) + kfree_skb(skb); + + printk(KERN_ERR "kaodv: error creating rt timeout message\n"); + + return NULL; } void kaodv_netlink_send_debug_msg(char *buf, int len) { - struct sk_buff *skb = NULL; + struct sk_buff *skb = NULL; - skb = kaodv_netlink_build_msg(KAODVM_DEBUG, buf, len); + skb = kaodv_netlink_build_msg(KAODVM_DEBUG, buf, len); - if (skb == NULL) { - printk("kaodv_netlink: skb=NULL\n"); - return; - } + if (skb == NULL) { + printk("kaodv_netlink: skb=NULL\n"); + return; + } - netlink_broadcast(kaodvnl, skb, peer_pid, AODVGRP_NOTIFY, GFP_USER); + netlink_broadcast(kaodvnl, skb, peer_pid, AODVGRP_NOTIFY, GFP_USER); } void kaodv_netlink_send_rt_msg(int type, __u32 src, __u32 dest) { - struct sk_buff *skb = NULL; - struct kaodv_rt_msg m; + struct sk_buff *skb = NULL; + struct kaodv_rt_msg m; - memset(&m, 0, sizeof(m)); + memset(&m, 0, sizeof(m)); - m.src = src; - m.dst = dest; + m.src = src; + m.dst = dest; - skb = kaodv_netlink_build_msg(type, &m, sizeof(struct kaodv_rt_msg)); + skb = kaodv_netlink_build_msg(type, &m, sizeof(struct kaodv_rt_msg)); - if (skb == NULL) { - printk("kaodv_netlink: skb=NULL\n"); - return; - } + if (skb == NULL) { + printk("kaodv_netlink: skb=NULL\n"); + return; + } -/* netlink_unicast(kaodvnl, skb, peer_pid, MSG_DONTWAIT); */ - netlink_broadcast(kaodvnl, skb, 0, AODVGRP_NOTIFY, GFP_USER); + /* netlink_unicast(kaodvnl, skb, peer_pid, MSG_DONTWAIT); */ + netlink_broadcast(kaodvnl, skb, 0, AODVGRP_NOTIFY, GFP_USER); } void kaodv_netlink_send_rt_update_msg(int type, __u32 src, __u32 dest, - int ifindex) + int ifindex) { - struct sk_buff *skb = NULL; - struct kaodv_rt_msg m; + struct sk_buff *skb = NULL; + struct kaodv_rt_msg m; - memset(&m, 0, sizeof(m)); + memset(&m, 0, sizeof(m)); - m.type = type; - m.src = src; - m.dst = dest; - m.ifindex = ifindex; + m.type = type; + m.src = src; + m.dst = dest; + m.ifindex = ifindex; - skb = kaodv_netlink_build_msg(KAODVM_ROUTE_UPDATE, &m, - sizeof(struct kaodv_rt_msg)); + skb = kaodv_netlink_build_msg(KAODVM_ROUTE_UPDATE, &m, + sizeof(struct kaodv_rt_msg)); - if (skb == NULL) { - printk("kaodv_netlink: skb=NULL\n"); - return; - } - /* netlink_unicast(kaodvnl, skb, peer_pid, MSG_DONTWAIT); */ - netlink_broadcast(kaodvnl, skb, 0, AODVGRP_NOTIFY, GFP_USER); + if (skb == NULL) { + printk("kaodv_netlink: skb=NULL\n"); + return; + } + /* netlink_unicast(kaodvnl, skb, peer_pid, MSG_DONTWAIT); */ + netlink_broadcast(kaodvnl, skb, 0, AODVGRP_NOTIFY, GFP_USER); } void kaodv_netlink_send_rerr_msg(int type, __u32 src, __u32 dest, int ifindex) { - struct sk_buff *skb = NULL; - struct kaodv_rt_msg m; + struct sk_buff *skb = NULL; + struct kaodv_rt_msg m; - memset(&m, 0, sizeof(m)); + memset(&m, 0, sizeof(m)); - m.type = type; - m.src = src; - m.dst = dest; - m.ifindex = ifindex; + m.type = type; + m.src = src; + m.dst = dest; + m.ifindex = ifindex; - skb = kaodv_netlink_build_msg(KAODVM_SEND_RERR, &m, - sizeof(struct kaodv_rt_msg)); + skb = kaodv_netlink_build_msg(KAODVM_SEND_RERR, &m, + sizeof(struct kaodv_rt_msg)); - if (skb == NULL) { - printk("kaodv_netlink: skb=NULL\n"); - return; - } - /* netlink_unicast(kaodvnl, skb, peer_pid, MSG_DONTWAIT); */ - netlink_broadcast(kaodvnl, skb, 0, AODVGRP_NOTIFY, GFP_USER); + if (skb == NULL) { + printk("kaodv_netlink: skb=NULL\n"); + return; + } + /* netlink_unicast(kaodvnl, skb, peer_pid, MSG_DONTWAIT); */ + netlink_broadcast(kaodvnl, skb, 0, AODVGRP_NOTIFY, GFP_USER); } static int kaodv_netlink_receive_peer(unsigned char type, void *msg, - unsigned int len) + unsigned int len) { - int ret = 0; - struct kaodv_rt_msg *m; - struct kaodv_conf_msg *cm; - struct expl_entry e; - - KAODV_DEBUG("Received msg: %s", kaodv_msg_type_to_str(type)); - - switch (type) { - case KAODVM_ADDROUTE: - if (len < sizeof(struct kaodv_rt_msg)) - return -EINVAL; - - m = (struct kaodv_rt_msg *)msg; - - ret = kaodv_expl_get(m->dst, &e); - - if (ret < 0) { - ret = kaodv_expl_update(m->dst, m->nhop, m->time, - m->flags, m->ifindex); - } else { - ret = kaodv_expl_add(m->dst, m->nhop, m->time, - m->flags, m->ifindex); - } - kaodv_queue_set_verdict(KAODV_QUEUE_SEND, m->dst); - break; - case KAODVM_DELROUTE: - if (len < sizeof(struct kaodv_rt_msg)) - return -EINVAL; - - m = (struct kaodv_rt_msg *)msg; - kaodv_expl_del(m->dst); - kaodv_queue_set_verdict(KAODV_QUEUE_DROP, m->dst); - break; - case KAODVM_NOROUTE_FOUND: - if (len < sizeof(struct kaodv_rt_msg)) - return -EINVAL; - - m = (struct kaodv_rt_msg *)msg; - KAODV_DEBUG("No route found for %s", print_ip(m->dst)); - kaodv_queue_set_verdict(KAODV_QUEUE_DROP, m->dst); - break; - case KAODVM_CONFIG: - if (len < sizeof(struct kaodv_conf_msg)) - return -EINVAL; - - cm = (struct kaodv_conf_msg *)msg; - active_route_timeout = cm->active_route_timeout; - qual_th = cm->qual_th; - is_gateway = cm->is_gateway; - break; - default: - printk("kaodv-netlink: Unknown message type\n"); - ret = -EINVAL; - } - return ret; + int ret = 0; + struct kaodv_rt_msg *m; + struct kaodv_conf_msg *cm; + struct expl_entry e; + + KAODV_DEBUG("Received msg: %s", kaodv_msg_type_to_str(type)); + + switch (type) { + case KAODVM_ADDROUTE: + if (len < sizeof(struct kaodv_rt_msg)) + return -EINVAL; + + m = (struct kaodv_rt_msg *)msg; + + ret = kaodv_expl_get(m->dst, &e); + + if (ret < 0) { + ret = kaodv_expl_update(m->dst, m->nhop, m->time, m->flags, + m->ifindex); + } else { + ret = + kaodv_expl_add(m->dst, m->nhop, m->time, m->flags, m->ifindex); + } + kaodv_queue_set_verdict(KAODV_QUEUE_SEND, m->dst); + break; + case KAODVM_DELROUTE: + if (len < sizeof(struct kaodv_rt_msg)) + return -EINVAL; + + m = (struct kaodv_rt_msg *)msg; + kaodv_expl_del(m->dst); + kaodv_queue_set_verdict(KAODV_QUEUE_DROP, m->dst); + break; + case KAODVM_NOROUTE_FOUND: + if (len < sizeof(struct kaodv_rt_msg)) + return -EINVAL; + + m = (struct kaodv_rt_msg *)msg; + KAODV_DEBUG("No route found for %s", print_ip(m->dst)); + kaodv_queue_set_verdict(KAODV_QUEUE_DROP, m->dst); + break; + case KAODVM_CONFIG: + if (len < sizeof(struct kaodv_conf_msg)) + return -EINVAL; + + cm = (struct kaodv_conf_msg *)msg; + active_route_timeout = cm->active_route_timeout; + qual_th = cm->qual_th; + is_gateway = cm->is_gateway; + break; + default: + printk("kaodv-netlink: Unknown message type\n"); + ret = -EINVAL; + } + return ret; } static int kaodv_netlink_rcv_nl_event(struct notifier_block *this, - unsigned long event, void *ptr) + unsigned long event, void *ptr) { - struct netlink_notify *n = ptr; - - - if (event == NETLINK_URELEASE && n->protocol == NETLINK_AODV && n->pid) { - if (n->pid == peer_pid) { - peer_pid = 0; - kaodv_expl_flush(); - kaodv_queue_flush(); - } - return NOTIFY_DONE; - } - return NOTIFY_DONE; + struct netlink_notify *n = ptr; + + if (event == NETLINK_URELEASE && n->protocol == NETLINK_AODV && n->pid) { + if (n->pid == peer_pid) { + peer_pid = 0; + kaodv_expl_flush(); + kaodv_queue_flush(); + } + return NOTIFY_DONE; + } + return NOTIFY_DONE; } static struct notifier_block kaodv_nl_notifier = { - .notifier_call = kaodv_netlink_rcv_nl_event, + .notifier_call = kaodv_netlink_rcv_nl_event, }; -#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0) +#define RCV_SKB_FAIL(err) \ + do { \ + netlink_ack(skb, nlh, (err)); \ + return; \ + } while (0) static inline void kaodv_netlink_rcv_skb(struct sk_buff *skb) { - int status, type, pid, flags, nlmsglen, skblen; - struct nlmsghdr *nlh; + int status, type, pid, flags, nlmsglen, skblen; + struct nlmsghdr *nlh; - skblen = skb->len; - if (skblen < sizeof(struct nlmsghdr)) { - printk("skblen to small\n"); - return; - } + skblen = skb->len; + if (skblen < sizeof(struct nlmsghdr)) { + printk("skblen to small\n"); + return; + } - nlh = (struct nlmsghdr *)skb->data; - nlmsglen = nlh->nlmsg_len; - - if (nlmsglen < sizeof(struct nlmsghdr) || skblen < nlmsglen) { - printk("nlsmsg=%d skblen=%d to small\n", nlmsglen, skblen); - return; - } + nlh = (struct nlmsghdr *)skb->data; + nlmsglen = nlh->nlmsg_len; - pid = nlh->nlmsg_pid; - flags = nlh->nlmsg_flags; + if (nlmsglen < sizeof(struct nlmsghdr) || skblen < nlmsglen) { + printk("nlsmsg=%d skblen=%d to small\n", nlmsglen, skblen); + return; + } - if (pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI) - RCV_SKB_FAIL(-EINVAL); + pid = nlh->nlmsg_pid; + flags = nlh->nlmsg_flags; + if (pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI) + RCV_SKB_FAIL(-EINVAL); - if (flags & MSG_TRUNC) - RCV_SKB_FAIL(-ECOMM); + if (flags & MSG_TRUNC) + RCV_SKB_FAIL(-ECOMM); - type = nlh->nlmsg_type; + type = nlh->nlmsg_type; -/* printk("kaodv_netlink: type=%d\n", type); */ - /* if (type < NLMSG_NOOP || type >= IPQM_MAX) */ + /* printk("kaodv_netlink: type=%d\n", type); */ + /* if (type < NLMSG_NOOP || type >= IPQM_MAX) */ /* RCV_SKB_FAIL(-EINVAL); */ #ifdef KERNEL26 -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)) - if (security_netlink_recv(skb)) - RCV_SKB_FAIL(-EPERM); +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)) + if (security_netlink_recv(skb)) + RCV_SKB_FAIL(-EPERM); #else - if (security_netlink_recv(skb, CAP_NET_ADMIN)) - RCV_SKB_FAIL(-EPERM); + if (security_netlink_recv(skb, CAP_NET_ADMIN)) + RCV_SKB_FAIL(-EPERM); #endif #endif - //write_lock_bh(&queue_lock); - - if (peer_pid) { - if (peer_pid != pid) { - //write_unlock_bh(&queue_lock); - RCV_SKB_FAIL(-EBUSY); - } - } else - peer_pid = pid; - - //write_unlock_bh(&queue_lock); - - status = kaodv_netlink_receive_peer(type, NLMSG_DATA(nlh), - skblen - NLMSG_LENGTH(0)); - if (status < 0) - RCV_SKB_FAIL(status); - - if (flags & NLM_F_ACK) - netlink_ack(skb, nlh, 0); - return; + // write_lock_bh(&queue_lock); + + if (peer_pid) { + if (peer_pid != pid) { + // write_unlock_bh(&queue_lock); + RCV_SKB_FAIL(-EBUSY); + } + } else + peer_pid = pid; + + // write_unlock_bh(&queue_lock); + + status = kaodv_netlink_receive_peer(type, NLMSG_DATA(nlh), + skblen - NLMSG_LENGTH(0)); + if (status < 0) + RCV_SKB_FAIL(status); + + if (flags & NLM_F_ACK) + netlink_ack(skb, nlh, 0); + return; } #if 0 @@ -342,30 +344,32 @@ static void kaodv_netlink_rcv_sk(struct sock *sk, int len) int kaodv_netlink_init(void) { - netlink_register_notifier(&kaodv_nl_notifier); -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)) - kaodvnl = netlink_kernel_create(NETLINK_AODV, kaodv_netlink_rcv_sk); -#elif (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)) - kaodvnl = netlink_kernel_create(NETLINK_AODV, AODVGRP_MAX, kaodv_netlink_rcv_sk, THIS_MODULE); -#elif (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)) - kaodvnl = netlink_kernel_create(NETLINK_AODV, AODVGRP_MAX, kaodv_netlink_rcv_sk, NULL, THIS_MODULE); + netlink_register_notifier(&kaodv_nl_notifier); +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 14)) + kaodvnl = netlink_kernel_create(NETLINK_AODV, kaodv_netlink_rcv_sk); +#elif (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)) + kaodvnl = netlink_kernel_create(NETLINK_AODV, AODVGRP_MAX, + kaodv_netlink_rcv_sk, THIS_MODULE); +#elif (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)) + kaodvnl = netlink_kernel_create(NETLINK_AODV, AODVGRP_MAX, + kaodv_netlink_rcv_sk, NULL, THIS_MODULE); #else - kaodvnl = netlink_kernel_create(&init_net, NETLINK_AODV, AODVGRP_MAX, - kaodv_netlink_rcv_skb, NULL, THIS_MODULE); + kaodvnl = netlink_kernel_create(&init_net, NETLINK_AODV, AODVGRP_MAX, + kaodv_netlink_rcv_skb, NULL, THIS_MODULE); #endif - if (kaodvnl == NULL) { - printk(KERN_ERR "kaodv_netlink: failed to create netlink socket\n"); - netlink_unregister_notifier(&kaodv_nl_notifier); - return -1; - } - return 0; + if (kaodvnl == NULL) { + printk(KERN_ERR "kaodv_netlink: failed to create netlink socket\n"); + netlink_unregister_notifier(&kaodv_nl_notifier); + return -1; + } + return 0; } void kaodv_netlink_fini(void) { - sock_release(kaodvnl->sk_socket); - down(&kaodvnl_sem); - up(&kaodvnl_sem); + sock_release(kaodvnl->sk_socket); + down(&kaodvnl_sem); + up(&kaodvnl_sem); - netlink_unregister_notifier(&kaodv_nl_notifier); + netlink_unregister_notifier(&kaodv_nl_notifier); } diff --git a/lnx/kaodv-netlink.h b/lnx/kaodv-netlink.h index f5d595d..ceb1cae 100644 --- a/lnx/kaodv-netlink.h +++ b/lnx/kaodv-netlink.h @@ -24,104 +24,104 @@ /* A communication link between the kernel and the AODV daemon */ -#include #include #include +#include /* Setting to MAX_LINKS-1 should ensure we use a free NETLINK * socket type. */ -#define NETLINK_AODV (MAX_LINKS-1) +#define NETLINK_AODV (MAX_LINKS - 1) enum { - AODVGRP_NOTIFY = 1, -#define AODVGRP_NOTIFY AODVGRP_NOTIFY - __AODVGRP_MAX + AODVGRP_NOTIFY = 1, +#define AODVGRP_NOTIFY AODVGRP_NOTIFY + __AODVGRP_MAX }; -#define AODVGRP_MAX (__AODVGRP_MAX - 1) +#define AODVGRP_MAX (__AODVGRP_MAX - 1) /* Message types that can be passed between the kernel and user * space. I do not really know a good way to set KAODVM_BASE. Just * set to 100 and hope there are no conflicts. */ enum { - KAODVM_BASE = 100, + KAODVM_BASE = 100, #define KAODVM_BASE KAODVM_BASE - KAODVM_ADDROUTE, + KAODVM_ADDROUTE, #define KAODVM_ADDROUTE KAODVM_ADDROUTE - KAODVM_DELROUTE, + KAODVM_DELROUTE, #define KAODVM_DELROUTE KAODVM_DELROUTE - KAODVM_TIMEOUT, + KAODVM_TIMEOUT, #define KAODVM_TIMEOUT KAODVM_TIMEOUT - KAODVM_ROUTE_REQ, + KAODVM_ROUTE_REQ, #define KAODVM_ROUTE_REQ KAODVM_ROUTE_REQ - KAODVM_REPAIR, + KAODVM_REPAIR, #define KAODVM_REPAIR KAODVM_REPAIR - KAODVM_NOROUTE_FOUND, + KAODVM_NOROUTE_FOUND, #define KAODVM_NOROUTE_FOUND KAODVM_NOROUTE_FOUND - KAODVM_ROUTE_UPDATE, + KAODVM_ROUTE_UPDATE, #define KAODVM_ROUTE_UPDATE KAODVM_ROUTE_UPDATE - KAODVM_SEND_RERR, + KAODVM_SEND_RERR, #define KAODVM_SEND_RERR KAODVM_SEND_RERR - KAODVM_CONFIG, + KAODVM_CONFIG, #define KAODVM_CONFIG KAODVM_CONFIG - KAODVM_DEBUG, + KAODVM_DEBUG, #define KAODVM_DEBUG KAODVM_DEBUG - __KAODV_MAX, + __KAODV_MAX, #define KAODVM_MAX __KAODV_MAX }; static struct { - int type; - char *name; -} typenames[KAODVM_MAX] = { - { KAODVM_ADDROUTE, "Add route" }, - { KAODVM_DELROUTE, "Delete route" }, - { KAODVM_TIMEOUT, "Timeout" }, - { KAODVM_ROUTE_REQ, "Route Request" }, - { KAODVM_REPAIR, "Route repair" }, - { KAODVM_NOROUTE_FOUND, "No route found" }, - { KAODVM_ROUTE_UPDATE, "Route update" }, - { KAODVM_SEND_RERR, "Send route error" }, - { KAODVM_CONFIG, "Configuration" }, - { KAODVM_DEBUG, "Debug"}, + int type; + char *name; +} typenames[KAODVM_MAX] = { + {KAODVM_ADDROUTE, "Add route"}, + {KAODVM_DELROUTE, "Delete route"}, + {KAODVM_TIMEOUT, "Timeout"}, + {KAODVM_ROUTE_REQ, "Route Request"}, + {KAODVM_REPAIR, "Route repair"}, + {KAODVM_NOROUTE_FOUND, "No route found"}, + {KAODVM_ROUTE_UPDATE, "Route update"}, + {KAODVM_SEND_RERR, "Send route error"}, + {KAODVM_CONFIG, "Configuration"}, + {KAODVM_DEBUG, "Debug"}, }; static inline char *kaodv_msg_type_to_str(int type) { - int i; - - for (i = 0; i < KAODVM_MAX; i++) { - if (type == typenames[i].type) { - return typenames[i].name; - } - } - return "Unknown message type"; + int i; + + for (i = 0; i < KAODVM_MAX; i++) { + if (type == typenames[i].type) { + return typenames[i].name; + } + } + return "Unknown message type"; } -typedef struct kaodv_rt_msg { - u_int8_t type; - u_int32_t src; - u_int32_t dst; - u_int32_t nhop; - u_int8_t flags; - int ifindex; - long time; +typedef struct kaodv_rt_msg { + u_int8_t type; + u_int32_t src; + u_int32_t dst; + u_int32_t nhop; + u_int8_t flags; + int ifindex; + long time; } kaodv_rt_msg_t; /* Route information flag */ -#define KAODV_RT_GW_ENCAP 0x1 -#define KAODV_RT_REPAIR 0x2 +#define KAODV_RT_GW_ENCAP 0x1 +#define KAODV_RT_REPAIR 0x2 /* Two types of route update messages. Packet is coming in our is going out. */ -#define PKT_INBOUND 1 +#define PKT_INBOUND 1 #define PKT_OUTBOUND 2 /* Send configuration paramaters to the kernel. Could be expanded in the * future. */ typedef struct kaodv_conf_msg { - int active_route_timeout; - int qual_th; - int is_gateway; + int active_route_timeout; + int qual_th; + int is_gateway; } kaodv_conf_msg_t; /* Stuff below is not exported to user space */ @@ -131,11 +131,11 @@ int kaodv_netlink_init(void); void kaodv_netlink_fini(void); void kaodv_netlink_send_rt_msg(int type, __u32 src, __u32 dest); -void kaodv_netlink_send_rt_update_msg(int type, __u32 src, - __u32 dest, int ifindex); +void kaodv_netlink_send_rt_update_msg(int type, __u32 src, __u32 dest, + int ifindex); void kaodv_netlink_send_rerr_msg(int type, __u32 src, __u32 dest, int ifindex); void kaodv_netlink_send_debug_msg(char *buf, int len); -#endif /* __KERNEL__ */ +#endif /* __KERNEL__ */ #endif diff --git a/lnx/kaodv-queue.c b/lnx/kaodv-queue.c index f288c27..333c691 100644 --- a/lnx/kaodv-queue.c +++ b/lnx/kaodv-queue.c @@ -17,27 +17,27 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Author: Erik Nordström, - * + * *****************************************************************************/ -#include -#include -#include #include #include -#include +#include #include #include +#include +#include +#include #include #include -#include -#include -#include +#include #include +#include +#include -#include "kaodv-queue.h" #include "kaodv-expl.h" -#include "kaodv-netlink.h" #include "kaodv-ipenc.h" +#include "kaodv-netlink.h" +#include "kaodv-queue.h" #include "kaodv.h" /* * This is basically a shameless rippoff of the linux kernel's ip_queue module. @@ -49,19 +49,19 @@ #define NET_KAODV_QUEUE_QMAX_NAME "kaodv_queue_maxlen" struct kaodv_rt_info { - __u8 tos; - __u32 daddr; - __u32 saddr; + __u8 tos; + __u32 daddr; + __u32 saddr; }; struct kaodv_queue_entry { - struct list_head list; - struct sk_buff *skb; - int (*okfn) (struct sk_buff *); - struct kaodv_rt_info rt_info; + struct list_head list; + struct sk_buff *skb; + int (*okfn)(struct sk_buff *); + struct kaodv_rt_info rt_info; }; -typedef int (*kaodv_queue_cmpfn) (struct kaodv_queue_entry *, unsigned long); +typedef int (*kaodv_queue_cmpfn)(struct kaodv_queue_entry *, unsigned long); static unsigned int queue_maxlen = KAODV_QUEUE_QMAX_DEFAULT; static rwlock_t queue_lock = RW_LOCK_UNLOCKED; @@ -70,293 +70,288 @@ static LIST_HEAD(queue_list); static inline int __kaodv_queue_enqueue_entry(struct kaodv_queue_entry *entry) { - if (queue_total >= queue_maxlen) { - if (net_ratelimit()) - printk(KERN_WARNING "kaodv-queue: full at %d entries, " - "dropping packet(s).\n", queue_total); - return -ENOSPC; - } - list_add(&entry->list, &queue_list); - queue_total++; - return 0; + if (queue_total >= queue_maxlen) { + if (net_ratelimit()) + printk(KERN_WARNING "kaodv-queue: full at %d entries, " + "dropping packet(s).\n", + queue_total); + return -ENOSPC; + } + list_add(&entry->list, &queue_list); + queue_total++; + return 0; } /* * Find and return a queued entry matched by cmpfn, or return the last * entry if cmpfn is NULL. */ -static inline struct kaodv_queue_entry -*__kaodv_queue_find_entry(kaodv_queue_cmpfn cmpfn, unsigned long data) +static inline struct kaodv_queue_entry * +__kaodv_queue_find_entry(kaodv_queue_cmpfn cmpfn, unsigned long data) { - struct list_head *p; + struct list_head *p; - list_for_each_prev(p, &queue_list) { - struct kaodv_queue_entry *entry = (struct kaodv_queue_entry *)p; + list_for_each_prev(p, &queue_list) + { + struct kaodv_queue_entry *entry = (struct kaodv_queue_entry *)p; - if (!cmpfn || cmpfn(entry, data)) - return entry; - } - return NULL; + if (!cmpfn || cmpfn(entry, data)) + return entry; + } + return NULL; } -static inline struct kaodv_queue_entry -*__kaodv_queue_find_dequeue_entry(kaodv_queue_cmpfn cmpfn, unsigned long data) +static inline struct kaodv_queue_entry * +__kaodv_queue_find_dequeue_entry(kaodv_queue_cmpfn cmpfn, unsigned long data) { - struct kaodv_queue_entry *entry; + struct kaodv_queue_entry *entry; - entry = __kaodv_queue_find_entry(cmpfn, data); - if (entry == NULL) - return NULL; + entry = __kaodv_queue_find_entry(cmpfn, data); + if (entry == NULL) + return NULL; - list_del(&entry->list); - queue_total--; + list_del(&entry->list); + queue_total--; - return entry; + return entry; } static inline void __kaodv_queue_flush(void) { - struct kaodv_queue_entry *entry; + struct kaodv_queue_entry *entry; - while ((entry = __kaodv_queue_find_dequeue_entry(NULL, 0))) { - kfree_skb(entry->skb); - kfree(entry); - } + while ((entry = __kaodv_queue_find_dequeue_entry(NULL, 0))) { + kfree_skb(entry->skb); + kfree(entry); + } } -static inline void __kaodv_queue_reset(void) -{ - __kaodv_queue_flush(); -} +static inline void __kaodv_queue_reset(void) { __kaodv_queue_flush(); } -static struct kaodv_queue_entry -*kaodv_queue_find_dequeue_entry(kaodv_queue_cmpfn cmpfn, unsigned long data) +static struct kaodv_queue_entry * +kaodv_queue_find_dequeue_entry(kaodv_queue_cmpfn cmpfn, unsigned long data) { - struct kaodv_queue_entry *entry; + struct kaodv_queue_entry *entry; - write_lock_bh(&queue_lock); - entry = __kaodv_queue_find_dequeue_entry(cmpfn, data); - write_unlock_bh(&queue_lock); - return entry; + write_lock_bh(&queue_lock); + entry = __kaodv_queue_find_dequeue_entry(cmpfn, data); + write_unlock_bh(&queue_lock); + return entry; } void kaodv_queue_flush(void) { - write_lock_bh(&queue_lock); - __kaodv_queue_flush(); - write_unlock_bh(&queue_lock); + write_lock_bh(&queue_lock); + __kaodv_queue_flush(); + write_unlock_bh(&queue_lock); } -int -kaodv_queue_enqueue_packet(struct sk_buff *skb, int (*okfn) (struct sk_buff *)) +int kaodv_queue_enqueue_packet(struct sk_buff *skb, + int (*okfn)(struct sk_buff *)) { - int status = -EINVAL; - struct kaodv_queue_entry *entry; - struct iphdr *iph = SKB_NETWORK_HDR_IPH(skb); + int status = -EINVAL; + struct kaodv_queue_entry *entry; + struct iphdr *iph = SKB_NETWORK_HDR_IPH(skb); - entry = kmalloc(sizeof(*entry), GFP_ATOMIC); + entry = kmalloc(sizeof(*entry), GFP_ATOMIC); - if (entry == NULL) { - printk(KERN_ERR - "kaodv_queue: OOM in kaodv_queue_enqueue_packet()\n"); - return -ENOMEM; - } + if (entry == NULL) { + printk(KERN_ERR "kaodv_queue: OOM in kaodv_queue_enqueue_packet()\n"); + return -ENOMEM; + } - /* printk("enquing packet queue_len=%d\n", queue_total); */ - entry->okfn = okfn; - entry->skb = skb; - entry->rt_info.tos = iph->tos; - entry->rt_info.daddr = iph->daddr; - entry->rt_info.saddr = iph->saddr; + /* printk("enquing packet queue_len=%d\n", queue_total); */ + entry->okfn = okfn; + entry->skb = skb; + entry->rt_info.tos = iph->tos; + entry->rt_info.daddr = iph->daddr; + entry->rt_info.saddr = iph->saddr; - write_lock_bh(&queue_lock); + write_lock_bh(&queue_lock); - status = __kaodv_queue_enqueue_entry(entry); + status = __kaodv_queue_enqueue_entry(entry); - if (status < 0) - goto err_out_unlock; + if (status < 0) + goto err_out_unlock; - write_unlock_bh(&queue_lock); - return status; + write_unlock_bh(&queue_lock); + return status; - err_out_unlock: - write_unlock_bh(&queue_lock); - kfree(entry); +err_out_unlock: + write_unlock_bh(&queue_lock); + kfree(entry); - return status; + return status; } static inline int dest_cmp(struct kaodv_queue_entry *e, unsigned long daddr) { - return (daddr == e->rt_info.daddr); + return (daddr == e->rt_info.daddr); } int kaodv_queue_find(__u32 daddr) { - struct kaodv_queue_entry *entry; - int res = 0; + struct kaodv_queue_entry *entry; + int res = 0; - read_lock_bh(&queue_lock); - entry = __kaodv_queue_find_entry(dest_cmp, daddr); - if (entry != NULL) - res = 1; + read_lock_bh(&queue_lock); + entry = __kaodv_queue_find_entry(dest_cmp, daddr); + if (entry != NULL) + res = 1; - read_unlock_bh(&queue_lock); - return res; + read_unlock_bh(&queue_lock); + return res; } int kaodv_queue_set_verdict(int verdict, __u32 daddr) { - struct kaodv_queue_entry *entry; - int pkts = 0; - - if (verdict == KAODV_QUEUE_DROP) { - - while (1) { - entry = kaodv_queue_find_dequeue_entry(dest_cmp, daddr); - - if (entry == NULL) - return pkts; - - /* Send an ICMP message informing the application that the - * destination was unreachable. */ - if (pkts == 0) - icmp_send(entry->skb, ICMP_DEST_UNREACH, - ICMP_HOST_UNREACH, 0); - - kfree_skb(entry->skb); - kfree(entry); - pkts++; - } - } else if (verdict == KAODV_QUEUE_SEND) { - struct expl_entry e; - - while (1) { - entry = kaodv_queue_find_dequeue_entry(dest_cmp, daddr); - - if (entry == NULL) - return pkts; - - if (!kaodv_expl_get(daddr, &e)) { - kfree_skb(entry->skb); - goto next; - } - if (e.flags & KAODV_RT_GW_ENCAP) { - - entry->skb = ip_pkt_encapsulate(entry->skb, e.nhop); - if (!entry->skb) - goto next; - } -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)) - ip_route_me_harder(&entry->skb); -#elif (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)) - ip_route_me_harder(&entry->skb, RTN_LOCAL); + struct kaodv_queue_entry *entry; + int pkts = 0; + + if (verdict == KAODV_QUEUE_DROP) { + + while (1) { + entry = kaodv_queue_find_dequeue_entry(dest_cmp, daddr); + + if (entry == NULL) + return pkts; + + /* Send an ICMP message informing the application that the + * destination was unreachable. */ + if (pkts == 0) + icmp_send(entry->skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0); + + kfree_skb(entry->skb); + kfree(entry); + pkts++; + } + } else if (verdict == KAODV_QUEUE_SEND) { + struct expl_entry e; + + while (1) { + entry = kaodv_queue_find_dequeue_entry(dest_cmp, daddr); + + if (entry == NULL) + return pkts; + + if (!kaodv_expl_get(daddr, &e)) { + kfree_skb(entry->skb); + goto next; + } + if (e.flags & KAODV_RT_GW_ENCAP) { + + entry->skb = ip_pkt_encapsulate(entry->skb, e.nhop); + if (!entry->skb) + goto next; + } +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)) + ip_route_me_harder(&entry->skb); +#elif (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)) + ip_route_me_harder(&entry->skb, RTN_LOCAL); #else - ip_route_me_harder(entry->skb, RTN_LOCAL); + ip_route_me_harder(entry->skb, RTN_LOCAL); #endif - pkts++; - - /* Inject packet */ - entry->okfn(entry->skb); - next: - kfree(entry); - } - } - return 0; + pkts++; + + /* Inject packet */ + entry->okfn(entry->skb); + next: + kfree(entry); + } + } + return 0; } -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)) -static int kaodv_queue_get_info(char *buffer, char **start, off_t offset, int length) +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)) +static int kaodv_queue_get_info(char *buffer, char **start, off_t offset, + int length) { - int len; + int len; - read_lock_bh(&queue_lock); + read_lock_bh(&queue_lock); - len = sprintf(buffer, - "Queue length : %u\n" - "Queue max. length : %u\n", queue_total, queue_maxlen); + len = sprintf(buffer, + "Queue length : %u\n" + "Queue max. length : %u\n", + queue_total, queue_maxlen); - read_unlock_bh(&queue_lock); + read_unlock_bh(&queue_lock); - *start = buffer + offset; - len -= offset; - if (len > length) - len = length; - else if (len < 0) - len = 0; - return len; + *start = buffer + offset; + len -= offset; + if (len > length) + len = length; + else if (len < 0) + len = 0; + return len; } #else static int kaodv_queue_get_info(char *page, char **start, off_t off, int count, - int *eof, void *data) + int *eof, void *data) { - int len; + int len; - read_lock_bh(&queue_lock); + read_lock_bh(&queue_lock); - len = sprintf(page, - "Queue length : %u\n" - "Queue max. length : %u\n", queue_total, queue_maxlen); + len = sprintf(page, + "Queue length : %u\n" + "Queue max. length : %u\n", + queue_total, queue_maxlen); - read_unlock_bh(&queue_lock); + read_unlock_bh(&queue_lock); - *start = page + off; - len -= off; - if (len > count) - len = count; - else if (len < 0) - len = 0; - return len; + *start = page + off; + len -= off; + if (len > count) + len = count; + else if (len < 0) + len = 0; + return len; } #endif static int init_or_cleanup(int init) { - int status = -ENOMEM; - struct proc_dir_entry *proc; + int status = -ENOMEM; + struct proc_dir_entry *proc; - if (!init) - goto cleanup; + if (!init) + goto cleanup; - queue_total = 0; + queue_total = 0; -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)) - proc = proc_net_create(KAODV_QUEUE_PROC_FS_NAME, 0, kaodv_queue_get_info); +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)) + proc = proc_net_create(KAODV_QUEUE_PROC_FS_NAME, 0, kaodv_queue_get_info); #else - proc = create_proc_read_entry(KAODV_QUEUE_PROC_FS_NAME, 0, init_net.proc_net, kaodv_queue_get_info, NULL); + proc = + create_proc_read_entry(KAODV_QUEUE_PROC_FS_NAME, 0, init_net.proc_net, + kaodv_queue_get_info, NULL); #endif - if (!proc) { - printk(KERN_ERR "kaodv_queue: failed to create proc entry\n"); - return -1; - } + if (!proc) { + printk(KERN_ERR "kaodv_queue: failed to create proc entry\n"); + return -1; + } -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)) - proc->owner = THIS_MODULE; +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)) + proc->owner = THIS_MODULE; #endif - return 1; - - cleanup: + return 1; + +cleanup: #ifdef KERNEL26 - synchronize_net(); + synchronize_net(); #endif - kaodv_queue_flush(); + kaodv_queue_flush(); -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)) - proc_net_remove(KAODV_QUEUE_PROC_FS_NAME); +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)) + proc_net_remove(KAODV_QUEUE_PROC_FS_NAME); #else - proc_net_remove(&init_net, KAODV_QUEUE_PROC_FS_NAME); + proc_net_remove(&init_net, KAODV_QUEUE_PROC_FS_NAME); #endif - return status; + return status; } -int kaodv_queue_init(void) -{ - - return init_or_cleanup(1); -} +int kaodv_queue_init(void) { return init_or_cleanup(1); } -void kaodv_queue_fini(void) -{ - init_or_cleanup(0); -} +void kaodv_queue_fini(void) { init_or_cleanup(0); } diff --git a/lnx/kaodv-queue.h b/lnx/kaodv-queue.h index 718c983..1b3aaff 100644 --- a/lnx/kaodv-queue.h +++ b/lnx/kaodv-queue.h @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Author: Erik Nordström, - * + * *****************************************************************************/ #ifndef _KAODV_QUEUE_H #define _KAODV_QUEUE_H @@ -27,7 +27,7 @@ int kaodv_queue_find(__u32 daddr); int kaodv_queue_enqueue_packet(struct sk_buff *skb, - int (*okfn) (struct sk_buff *)); + int (*okfn)(struct sk_buff *)); int kaodv_queue_set_verdict(int verdict, __u32 daddr); void kaodv_queue_flush(void); int kaodv_queue_init(void); diff --git a/lnx/kaodv.h b/lnx/kaodv.h index 3d64403..d6b1eeb 100644 --- a/lnx/kaodv.h +++ b/lnx/kaodv.h @@ -17,12 +17,12 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Author: Erik Nordström, - * + * *****************************************************************************/ #ifndef _KAODV_H #define _KAODV_H -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)) +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)) #define SKB_TAIL_PTR(skb) skb->tail #define SKB_NETWORK_HDR_RAW(skb) skb->nh.raw #define SKB_NETWORK_HDR_RIPH(skb) skb->nh.iph @@ -36,20 +36,18 @@ #define SKB_SET_NETWORK_HDR(skb, offset) skb_set_network_header(skb, offset) #endif -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,31)) +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31)) static inline struct dst_entry *skb_dst(const struct sk_buff *skb) { - return (struct dst_entry *)skb->dst; + return (struct dst_entry *)skb->dst; } static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst) { - skb->dst = dst; + skb->dst = dst; } #endif - - #define AODV_PORT 654 #endif /* _KAODV_H */ diff --git a/locality.c b/locality.c index 803bc41..b16641c 100644 --- a/locality.c +++ b/locality.c @@ -25,60 +25,58 @@ #include extern int h_errno; -#include "locality.h" -#include "defs.h" #include "debug.h" +#include "defs.h" +#include "locality.h" extern int gw_prefix; #endif - int NS_CLASS locality(struct in_addr dest, unsigned int ifindex) { #ifndef NS_PORT if (gw_prefix) { - if ((dest.s_addr & DEV_IFINDEX(ifindex).netmask.s_addr) == - (DEV_IFINDEX(ifindex).ipaddr.s_addr & DEV_IFINDEX(ifindex).netmask. - s_addr)) - return HOST_ADHOC; - else - return HOST_INET; + if ((dest.s_addr & DEV_IFINDEX(ifindex).netmask.s_addr) == + (DEV_IFINDEX(ifindex).ipaddr.s_addr & + DEV_IFINDEX(ifindex).netmask.s_addr)) + return HOST_ADHOC; + else + return HOST_INET; } else { - struct hostent *hent; - - hent = gethostbyaddr(&dest, sizeof(struct in_addr), AF_INET); + struct hostent *hent; - if (!hent) { - switch (h_errno) { - case HOST_NOT_FOUND: - DEBUG(LOG_DEBUG, 0, "RREQ for Non-Internet dest %s", - ip_to_str(dest)); - return HOST_UNKNOWN; - default: - DEBUG(LOG_DEBUG, 0, "Unknown DNS error"); - break; + hent = gethostbyaddr(&dest, sizeof(struct in_addr), AF_INET); - } - } else - return HOST_INET; + if (!hent) { + switch (h_errno) { + case HOST_NOT_FOUND: + DEBUG(LOG_DEBUG, 0, "RREQ for Non-Internet dest %s", + ip_to_str(dest)); + return HOST_UNKNOWN; + default: + DEBUG(LOG_DEBUG, 0, "Unknown DNS error"); + break; + } + } else + return HOST_INET; } #else char *dstnet = Address::instance().get_subnetaddr(dest.s_addr); char *subnet = - Address::instance().get_subnetaddr(DEV_NR(NS_DEV_NR).ipaddr.s_addr); + Address::instance().get_subnetaddr(DEV_NR(NS_DEV_NR).ipaddr.s_addr); DEBUG(LOG_DEBUG, 0, "myaddr=%d, dest=%d dstnet=%s subnet=%s", - DEV_NR(NS_DEV_NR).ipaddr.s_addr, dest.s_addr, dstnet, subnet); + DEV_NR(NS_DEV_NR).ipaddr.s_addr, dest.s_addr, dstnet, subnet); if (subnet != NULL) { - if (dstnet != NULL) { - if (strcmp(dstnet, subnet) != 0) { - delete[]dstnet; - return HOST_INET; - } - delete[]dstnet; - } - delete[]subnet; + if (dstnet != NULL) { + if (strcmp(dstnet, subnet) != 0) { + delete[] dstnet; + return HOST_INET; + } + delete[] dstnet; + } + delete[] subnet; } assert(dstnet == NULL); return HOST_UNKNOWN; diff --git a/locality.h b/locality.h index 5b035c4..ba475a1 100644 --- a/locality.h +++ b/locality.h @@ -24,15 +24,11 @@ #ifndef NS_NO_GLOBALS -enum { - HOST_ADHOC, - HOST_INET, - HOST_UNKNOWN -}; -#endif /* NS_NO_GLOBALS */ +enum { HOST_ADHOC, HOST_INET, HOST_UNKNOWN }; +#endif /* NS_NO_GLOBALS */ #ifndef NS_NO_DECLARATIONS int locality(struct in_addr dest, unsigned int ifindex); -#endif /* NS_NO_DECLARATIONS */ +#endif /* NS_NO_DECLARATIONS */ #endif diff --git a/main.c b/main.c index b269a65..9020de1 100644 --- a/main.c +++ b/main.c @@ -16,31 +16,31 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Author: Erik Nordström, + * Author: Erik Nordström, * *****************************************************************************/ +#include +#include +#include +#include +#include +#include #include #include -#include -#include -#include #include #include -#include -#include -#include -#include -#include +#include +#include -#include "defs.h" -#include "debug.h" -#include "timer_queue.h" -#include "params.h" +#include "aodv_hello.h" #include "aodv_socket.h" #include "aodv_timeout.h" -#include "routing_table.h" -#include "aodv_hello.h" +#include "debug.h" +#include "defs.h" #include "nl.h" +#include "params.h" +#include "routing_table.h" +#include "timer_queue.h" #ifdef LLFEEDBACK #include "llf.h" @@ -48,7 +48,7 @@ /* Global variables: */ int log_to_file = 0; -int rt_log_interval = 0; /* msecs between routing table logging 0=off */ +int rt_log_interval = 0; /* msecs between routing table logging 0=off */ int unidir_hack = 0; int rreq_gratuitous = 0; int expanding_ring_search = 1; @@ -57,13 +57,13 @@ int local_repair = 0; int receive_n_hellos = 0; int hello_jittering = 1; int optimized_hellos = 0; -int ratelimit = 1; /* Option for rate limiting RREQs and RERRs. */ +int ratelimit = 1; /* Option for rate limiting RREQs and RERRs. */ char *progname; int wait_on_reboot = 1; int qual_threshold = 0; int llfeedback = 0; int gw_prefix = 1; -struct timer worb_timer; /* Wait on reboot timer */ +struct timer worb_timer; /* Wait on reboot timer */ /* Dynamic configuration values */ int active_route_timeout = ACTIVE_ROUTE_TIMEOUT_HELLO; @@ -72,58 +72,65 @@ int delete_period = DELETE_PERIOD_HELLO; static void cleanup(); -struct option longopts[] = { - {"interface", required_argument, NULL, 'i'}, - {"hello-jitter", no_argument, NULL, 'j'}, - {"log", no_argument, NULL, 'l'}, - {"n-hellos", required_argument, NULL, 'n'}, - {"daemon", no_argument, NULL, 'd'}, - {"force-gratuitous", no_argument, NULL, 'g'}, - {"opt-hellos", no_argument, NULL, 'o'}, - {"quality-threshold", required_argument, NULL, 'q'}, - {"log-rt-table", required_argument, NULL, 'r'}, - {"unidir_hack", no_argument, NULL, 'u'}, - {"gateway-mode", no_argument, NULL, 'w'}, - {"help", no_argument, NULL, 'h'}, - {"no-expanding-ring", no_argument, NULL, 'x'}, - {"no-worb", no_argument, NULL, 'D'}, - {"local-repair", no_argument, NULL, 'L'}, - {"rate-limit", no_argument, NULL, 'R'}, - {"version", no_argument, NULL, 'V'}, - {"llfeedback", no_argument, NULL, 'f'}, - {0} -}; +struct option longopts[] = {{"interface", required_argument, NULL, 'i'}, + {"hello-jitter", no_argument, NULL, 'j'}, + {"log", no_argument, NULL, 'l'}, + {"n-hellos", required_argument, NULL, 'n'}, + {"daemon", no_argument, NULL, 'd'}, + {"force-gratuitous", no_argument, NULL, 'g'}, + {"opt-hellos", no_argument, NULL, 'o'}, + {"quality-threshold", required_argument, NULL, 'q'}, + {"log-rt-table", required_argument, NULL, 'r'}, + {"unidir_hack", no_argument, NULL, 'u'}, + {"gateway-mode", no_argument, NULL, 'w'}, + {"help", no_argument, NULL, 'h'}, + {"no-expanding-ring", no_argument, NULL, 'x'}, + {"no-worb", no_argument, NULL, 'D'}, + {"local-repair", no_argument, NULL, 'L'}, + {"rate-limit", no_argument, NULL, 'R'}, + {"version", no_argument, NULL, 'V'}, + {"llfeedback", no_argument, NULL, 'f'}, + {0}}; void usage(int status) { if (status != 0) { - fprintf(stderr, "Try `%s --help' for more information.\n", progname); - exit(status); + fprintf(stderr, "Try `%s --help' for more information.\n", progname); + exit(status); } - printf - ("\nUsage: %s [-dghjlouwxLDRV] [-i if0,if1,..] [-r N] [-n N] [-q THR]\n\n" - "-d, --daemon Daemon mode, i.e. detach from the console.\n" - "-g, --force-gratuitous Force the gratuitous flag to be set on all RREQ's.\n" - "-h, --help This information.\n" - "-i, --interface Network interfaces to attach to. Defaults to first\n" - " wireless interface.\n" - "-j, --hello-jitter Toggle hello jittering (default ON).\n" - "-l, --log Log debug output to %s.\n" - "-o, --opt-hellos Send HELLOs only when forwarding data (experimental).\n" - "-r, --log-rt-table Log routing table to %s every N secs.\n" - "-n, --n-hellos Receive N hellos from host before treating as neighbor.\n" - "-u, --unidir-hack Detect and avoid unidirectional links (experimental).\n" - "-w, --gateway-mode Enable experimental Internet gateway support.\n" - "-x, --no-expanding-ring Disable expanding ring search for RREQs.\n" - "-D, --no-worb Disable 15 seconds wait on reboot delay.\n" - "-L, --local-repair Enable local repair.\n" - "-f, --llfeedback Enable link layer feedback.\n" - "-R, --rate-limit Toggle rate limiting of RREQs and RERRs (default ON).\n" - "-q, --quality-threshold Set a minimum signal quality threshold for control packets.\n" - "-V, --version Show version.\n\n" - "Erik Nordström, \n\n", - progname, AODV_LOG_PATH, AODV_RT_LOG_PATH); + printf( + "\nUsage: %s [-dghjlouwxLDRV] [-i if0,if1,..] [-r N] [-n N] [-q " + "THR]\n\n" + "-d, --daemon Daemon mode, i.e. detach from the console.\n" + "-g, --force-gratuitous Force the gratuitous flag to be set on all " + "RREQ's.\n" + "-h, --help This information.\n" + "-i, --interface Network interfaces to attach to. Defaults to " + "first\n" + " wireless interface.\n" + "-j, --hello-jitter Toggle hello jittering (default ON).\n" + "-l, --log Log debug output to %s.\n" + "-o, --opt-hellos Send HELLOs only when forwarding data " + "(experimental).\n" + "-r, --log-rt-table Log routing table to %s every N secs.\n" + "-n, --n-hellos Receive N hellos from host before treating as " + "neighbor.\n" + "-u, --unidir-hack Detect and avoid unidirectional links " + "(experimental).\n" + "-w, --gateway-mode Enable experimental Internet gateway " + "support.\n" + "-x, --no-expanding-ring Disable expanding ring search for RREQs.\n" + "-D, --no-worb Disable 15 seconds wait on reboot delay.\n" + "-L, --local-repair Enable local repair.\n" + "-f, --llfeedback Enable link layer feedback.\n" + "-R, --rate-limit Toggle rate limiting of RREQs and RERRs " + "(default ON).\n" + "-q, --quality-threshold Set a minimum signal quality threshold for " + "control packets.\n" + "-V, --version Show version.\n\n" + "Erik Nordström, \n\n", + progname, AODV_LOG_PATH, AODV_RT_LOG_PATH); exit(status); } @@ -136,65 +143,64 @@ int set_kernel_options() char command[64]; if ((fd = open("/proc/sys/net/ipv4/ip_forward", O_WRONLY)) < 0) - return -1; + return -1; if (write(fd, &on, sizeof(char)) < 0) - return -1; + return -1; close(fd); /* Disabled for kernel 2.6.28. if ((fd = open("/proc/sys/net/ipv4/route/max_delay", O_WRONLY)) < 0) - return -1; + return -1; if (write(fd, &off, sizeof(char)) < 0) - return -1; + return -1; close(fd); if ((fd = open("/proc/sys/net/ipv4/route/min_delay", O_WRONLY)) < 0) - return -1; + return -1; if (write(fd, &off, sizeof(char)) < 0) - return -1; + return -1; close(fd); */ /* Disable ICMP redirects on all interfaces: */ for (i = 0; i < MAX_NR_INTERFACES; i++) { - if (!DEV_NR(i).enabled) - continue; - - memset(command, '\0', 64); - sprintf(command, "/proc/sys/net/ipv4/conf/%s/send_redirects", - DEV_NR(i).ifname); - if ((fd = open(command, O_WRONLY)) < 0) - return -1; - if (write(fd, &off, sizeof(char)) < 0) - return -1; - close(fd); - memset(command, '\0', 64); - sprintf(command, "/proc/sys/net/ipv4/conf/%s/accept_redirects", - DEV_NR(i).ifname); - if ((fd = open(command, O_WRONLY)) < 0) - return -1; - if (write(fd, &off, sizeof(char)) < 0) - return -1; - close(fd); + if (!DEV_NR(i).enabled) + continue; + + memset(command, '\0', 64); + sprintf(command, "/proc/sys/net/ipv4/conf/%s/send_redirects", + DEV_NR(i).ifname); + if ((fd = open(command, O_WRONLY)) < 0) + return -1; + if (write(fd, &off, sizeof(char)) < 0) + return -1; + close(fd); + memset(command, '\0', 64); + sprintf(command, "/proc/sys/net/ipv4/conf/%s/accept_redirects", + DEV_NR(i).ifname); + if ((fd = open(command, O_WRONLY)) < 0) + return -1; + if (write(fd, &off, sizeof(char)) < 0) + return -1; + close(fd); } memset(command, '\0', 64); sprintf(command, "/proc/sys/net/ipv4/conf/all/send_redirects"); if ((fd = open(command, O_WRONLY)) < 0) - return -1; + return -1; if (write(fd, &off, sizeof(char)) < 0) - return -1; + return -1; close(fd); - memset(command, '\0', 64); sprintf(command, "/proc/sys/net/ipv4/conf/all/accept_redirects"); if ((fd = open(command, O_WRONLY)) < 0) - return -1; + return -1; if (write(fd, &off, sizeof(char)) < 0) - return -1; + return -1; close(fd); return 0; @@ -208,23 +214,23 @@ int find_default_gw(void) route = fopen("/proc/net/route", "r"); if (route == NULL) { - perror("open /proc/net/route"); - exit(-1); + perror("open /proc/net/route"); + exit(-1); } while (fgets(buf, sizeof(buf), route)) { - l = strtok(buf, " \t"); - l = strtok(NULL, " \t"); - if (l != NULL) { - if (strcmp("00000000", l) == 0) { - l = strtok(NULL, " \t"); - l = strtok(NULL, " \t"); - if (strcmp("0003", l) == 0) { - fclose(route); - return 1; - } - } - } + l = strtok(buf, " \t"); + l = strtok(NULL, " \t"); + if (l != NULL) { + if (strcmp("00000000", l) == 0) { + l = strtok(NULL, " \t"); + l = strtok(NULL, " \t"); + if (strcmp("0003", l) == 0) { + fclose(route); + return 1; + } + } + } } fclose(route); return 0; @@ -244,14 +250,14 @@ struct sockaddr_in *get_if_info(char *ifname, int type) strcpy(ifr.ifr_name, ifname); if (ioctl(skfd, type, &ifr) < 0) { - alog(LOG_ERR, errno, __FUNCTION__, - "Could not get address of %s ", ifname); - close(skfd); - return NULL; + alog(LOG_ERR, errno, __FUNCTION__, "Could not get address of %s ", + ifname); + close(skfd); + return NULL; } else { - ina = (struct sockaddr_in *) &ifr.ifr_addr; - close(skfd); - return ina; + ina = (struct sockaddr_in *)&ifr.ifr_addr; + close(skfd); + return ina; } } @@ -268,8 +274,8 @@ static int nr_callbacks = 0; int attach_callback_func(int fd, callback_func_t func) { if (nr_callbacks >= CALLBACK_FUNCS) { - fprintf(stderr, "callback attach limit reached!!\n"); - exit(-1); + fprintf(stderr, "callback attach limit reached!!\n"); + exit(-1); } callbacks[nr_callbacks].fd = fd; callbacks[nr_callbacks].func = func; @@ -291,11 +297,11 @@ void load_modules(char *ifname) memset(buf, '\0', 64); if (stat("./kaodv.ko", &st) == 0) - sprintf(buf, "/sbin/insmod kaodv.ko ifname=%s &>/dev/null", ifname); + sprintf(buf, "/sbin/insmod kaodv.ko ifname=%s &>/dev/null", ifname); else if (stat("./kaodv.o", &st) == 0) - sprintf(buf, "/sbin/insmod kaodv.o ifname=%s &>/dev/null", ifname); + sprintf(buf, "/sbin/insmod kaodv.o ifname=%s &>/dev/null", ifname); else - sprintf(buf, "/sbin/modprobe kaodv ifname=%s &>/dev/null", ifname); + sprintf(buf, "/sbin/modprobe kaodv ifname=%s &>/dev/null", ifname); if (system(buf) == -1) { fprintf(stderr, "Could not load kaodv module\n"); @@ -307,41 +313,42 @@ void load_modules(char *ifname) /* Check result */ m = fopen("/proc/modules", "r"); while (fgets(buf, sizeof(buf), m)) { - l = strtok(buf, " \t"); - if (!strcmp(l, "kaodv")) - found++; - if (!strcmp(l, "ipchains")) { - fprintf(stderr, - "The ipchains kernel module is loaded and prevents AODV-UU from functioning properly.\n"); - exit(-1); - } + l = strtok(buf, " \t"); + if (!strcmp(l, "kaodv")) + found++; + if (!strcmp(l, "ipchains")) { + fprintf(stderr, "The ipchains kernel module is loaded and prevents " + "AODV-UU from functioning properly.\n"); + exit(-1); + } } fclose(m); if (found < 1) { - fprintf(stderr, - "A kernel module could not be loaded, check your installation... %d\n", - found); - exit(-1); + fprintf(stderr, + "A kernel module could not be loaded, check your " + "installation... %d\n", + found); + exit(-1); } } void remove_modules(void) { - int ret; + int ret; - ret = system("/sbin/rmmod kaodv &>/dev/null"); + ret = system("/sbin/rmmod kaodv &>/dev/null"); - if (ret != 0) { - fprintf(stderr, "Could not remove kernel module kaodv\n"); - } + if (ret != 0) { + fprintf(stderr, "Could not remove kernel module kaodv\n"); + } } void host_init(char *ifname) { struct sockaddr_in *ina; char buf[1024], tmp_ifname[IFNAMSIZ], - ifnames[(IFNAMSIZ + 1) * MAX_NR_INTERFACES], *iface; + ifnames[(IFNAMSIZ + 1) * MAX_NR_INTERFACES], *iface; struct ifconf ifc; struct ifreq ifreq, *ifr; int i, iw_sock, if_sock = 0; @@ -350,43 +357,43 @@ void host_init(char *ifname) memset(dev_indices, 0, sizeof(unsigned int) * MAX_NR_INTERFACES); if (!ifname) { - /* No interface was given... search for first wireless. */ - iw_sock = socket(PF_INET, SOCK_DGRAM, 0); - ifc.ifc_len = sizeof(buf); - ifc.ifc_buf = buf; - if (ioctl(iw_sock, SIOCGIFCONF, &ifc) < 0) { - fprintf(stderr, "Could not get wireless info\n"); - exit(-1); - } - ifr = ifc.ifc_req; - for (i = ifc.ifc_len / sizeof(struct ifreq); i >= 0; i--, ifr++) { - struct iwreq req; - - strcpy(req.ifr_name, ifr->ifr_name); - if (ioctl(iw_sock, SIOCGIWNAME, &req) >= 0) { - strcpy(tmp_ifname, ifr->ifr_name); - break; - } - } - /* Did we find a wireless interface? */ - if (!strlen(tmp_ifname)) { - fprintf(stderr, "\nCould not find a wireless interface!\n"); - fprintf(stderr, "Use -i to override...\n\n"); - exit(-1); - } - strcpy(ifreq.ifr_name, tmp_ifname); - if (ioctl(iw_sock, SIOCGIFINDEX, &ifreq) < 0) { - alog(LOG_ERR, errno, __FUNCTION__, - "Could not get index of %s", tmp_ifname); - close(if_sock); - exit(-1); - } - close(iw_sock); - - ifname = tmp_ifname; - - alog(LOG_NOTICE, 0, __FUNCTION__, - "Attaching to %s, override with -i .", tmp_ifname); + /* No interface was given... search for first wireless. */ + iw_sock = socket(PF_INET, SOCK_DGRAM, 0); + ifc.ifc_len = sizeof(buf); + ifc.ifc_buf = buf; + if (ioctl(iw_sock, SIOCGIFCONF, &ifc) < 0) { + fprintf(stderr, "Could not get wireless info\n"); + exit(-1); + } + ifr = ifc.ifc_req; + for (i = ifc.ifc_len / sizeof(struct ifreq); i >= 0; i--, ifr++) { + struct iwreq req; + + strcpy(req.ifr_name, ifr->ifr_name); + if (ioctl(iw_sock, SIOCGIWNAME, &req) >= 0) { + strcpy(tmp_ifname, ifr->ifr_name); + break; + } + } + /* Did we find a wireless interface? */ + if (!strlen(tmp_ifname)) { + fprintf(stderr, "\nCould not find a wireless interface!\n"); + fprintf(stderr, "Use -i to override...\n\n"); + exit(-1); + } + strcpy(ifreq.ifr_name, tmp_ifname); + if (ioctl(iw_sock, SIOCGIFINDEX, &ifreq) < 0) { + alog(LOG_ERR, errno, __FUNCTION__, "Could not get index of %s", + tmp_ifname); + close(if_sock); + exit(-1); + } + close(iw_sock); + + ifname = tmp_ifname; + + alog(LOG_NOTICE, 0, __FUNCTION__, + "Attaching to %s, override with -i .", tmp_ifname); } strcpy(ifnames, ifname); @@ -407,46 +414,46 @@ void host_init(char *ifname) /* OK, now lookup interface information, and store it... */ do { - strcpy(ifreq.ifr_name, iface); - if (ioctl(if_sock, SIOCGIFINDEX, &ifreq) < 0) { - alog(LOG_ERR, errno, __FUNCTION__, "Could not get index of %s", - iface); - close(if_sock); - exit(-1); - } - this_host.devs[this_host.nif].ifindex = ifreq.ifr_ifindex; + strcpy(ifreq.ifr_name, iface); + if (ioctl(if_sock, SIOCGIFINDEX, &ifreq) < 0) { + alog(LOG_ERR, errno, __FUNCTION__, "Could not get index of %s", + iface); + close(if_sock); + exit(-1); + } + this_host.devs[this_host.nif].ifindex = ifreq.ifr_ifindex; - dev_indices[this_host.nif++] = ifreq.ifr_ifindex; + dev_indices[this_host.nif++] = ifreq.ifr_ifindex; - strcpy(DEV_IFINDEX(ifreq.ifr_ifindex).ifname, iface); + strcpy(DEV_IFINDEX(ifreq.ifr_ifindex).ifname, iface); - /* Get IP-address of interface... */ - ina = get_if_info(iface, SIOCGIFADDR); + /* Get IP-address of interface... */ + ina = get_if_info(iface, SIOCGIFADDR); - if (ina == NULL) - exit(-1); + if (ina == NULL) + exit(-1); - DEV_IFINDEX(ifreq.ifr_ifindex).ipaddr = ina->sin_addr; + DEV_IFINDEX(ifreq.ifr_ifindex).ipaddr = ina->sin_addr; - /* Get netmask of interface... */ - ina = get_if_info(iface, SIOCGIFNETMASK); + /* Get netmask of interface... */ + ina = get_if_info(iface, SIOCGIFNETMASK); - if (ina == NULL) - exit(-1); + if (ina == NULL) + exit(-1); - DEV_IFINDEX(ifreq.ifr_ifindex).netmask = ina->sin_addr; + DEV_IFINDEX(ifreq.ifr_ifindex).netmask = ina->sin_addr; - ina = get_if_info(iface, SIOCGIFBRDADDR); + ina = get_if_info(iface, SIOCGIFBRDADDR); - if (ina == NULL) - exit(-1); + if (ina == NULL) + exit(-1); - DEV_IFINDEX(ifreq.ifr_ifindex).broadcast = ina->sin_addr; + DEV_IFINDEX(ifreq.ifr_ifindex).broadcast = ina->sin_addr; - DEV_IFINDEX(ifreq.ifr_ifindex).enabled = 1; + DEV_IFINDEX(ifreq.ifr_ifindex).enabled = 1; - if (this_host.nif >= MAX_NR_INTERFACES) - break; + if (this_host.nif >= MAX_NR_INTERFACES) + break; } while ((iface = strtok(NULL, ","))); @@ -457,8 +464,8 @@ void host_init(char *ifname) /* Enable IP forwarding and set other kernel options... */ if (set_kernel_options() < 0) { - fprintf(stderr, "Could not set kernel options!\n"); - exit(-1); + fprintf(stderr, "Could not set kernel options!\n"); + exit(-1); } } @@ -468,19 +475,20 @@ void signal_handler(int type) switch (type) { case SIGSEGV: - alog(LOG_ERR, 0, __FUNCTION__, "SEGMENTATION FAULT!!!! Exiting!!! " - "To get a core dump, compile with DEBUG option."); + alog(LOG_ERR, 0, __FUNCTION__, + "SEGMENTATION FAULT!!!! Exiting!!! " + "To get a core dump, compile with DEBUG option."); case SIGINT: case SIGHUP: case SIGTERM: default: - exit(0); + exit(0); } } int main(int argc, char **argv) { - static char *ifname = NULL; /* Name of interface to attach to */ + static char *ifname = NULL; /* Name of interface to attach to */ fd_set rfds, readers; int n, nfds = 0, i; int daemonize = 0; @@ -493,21 +501,21 @@ int main(int argc, char **argv) progname = strrchr(argv[0], '/'); if (progname) - progname++; + progname++; else - progname = argv[0]; + progname = argv[0]; /* Use debug output as default */ debug = 1; - memset (&sigact, 0, sizeof(struct sigaction)); + memset(&sigact, 0, sizeof(struct sigaction)); sigact.sa_handler = signal_handler; - + /* This server should shut down on these signals. */ sigaction(SIGTERM, &sigact, 0); sigaction(SIGHUP, &sigact, 0); sigaction(SIGINT, &sigact, 0); - + sigaddset(&mask, SIGTERM); sigaddset(&mask, SIGHUP); sigaddset(&mask, SIGINT); @@ -522,105 +530,105 @@ int main(int argc, char **argv) /* Parse command line: */ while (1) { - int opt; - - opt = getopt_long(argc, argv, "i:fjln:dghoq:r:s:uwxDLRV", longopts, 0); - - if (opt == EOF) - break; - - switch (opt) { - case 0: - break; - case 'd': - debug = 0; - daemonize = 1; - break; - case 'f': - llfeedback = 1; - active_route_timeout = ACTIVE_ROUTE_TIMEOUT_LLF; - break; - case 'g': - rreq_gratuitous = !rreq_gratuitous; - break; - case 'i': - ifname = optarg; - break; - case 'j': - hello_jittering = !hello_jittering; - break; - case 'l': - log_to_file = !log_to_file; - break; - case 'n': - if (optarg && isdigit(*optarg)) { - receive_n_hellos = atoi(optarg); - if (receive_n_hellos < 2) { - fprintf(stderr, "-n should be at least 2!\n"); - exit(-1); - } - } - break; - case 'o': - optimized_hellos = !optimized_hellos; - break; - case 'q': - if (optarg && isdigit(*optarg)) - qual_threshold = atoi(optarg); - break; - case 'r': - if (optarg && isdigit(*optarg)) - rt_log_interval = atof(optarg) * 1000; - break; - case 'u': - unidir_hack = !unidir_hack; - break; - case 'w': - internet_gw_mode = !internet_gw_mode; - break; - case 'x': - expanding_ring_search = !expanding_ring_search; - break; - case 'L': - local_repair = !local_repair; - break; - case 'D': - wait_on_reboot = !wait_on_reboot; - break; - case 'R': - ratelimit = !ratelimit; - break; - case 'V': - printf - ("\nAODV-UU v%s, %s © Uppsala University & Ericsson AB.\nAuthor: Erik Nordström, \n\n", - AODV_UU_VERSION, DRAFT_VERSION); - exit(0); - break; - case '?': - case ':': - exit(0); - default: - usage(0); - } + int opt; + + opt = getopt_long(argc, argv, "i:fjln:dghoq:r:s:uwxDLRV", longopts, 0); + + if (opt == EOF) + break; + + switch (opt) { + case 0: + break; + case 'd': + debug = 0; + daemonize = 1; + break; + case 'f': + llfeedback = 1; + active_route_timeout = ACTIVE_ROUTE_TIMEOUT_LLF; + break; + case 'g': + rreq_gratuitous = !rreq_gratuitous; + break; + case 'i': + ifname = optarg; + break; + case 'j': + hello_jittering = !hello_jittering; + break; + case 'l': + log_to_file = !log_to_file; + break; + case 'n': + if (optarg && isdigit(*optarg)) { + receive_n_hellos = atoi(optarg); + if (receive_n_hellos < 2) { + fprintf(stderr, "-n should be at least 2!\n"); + exit(-1); + } + } + break; + case 'o': + optimized_hellos = !optimized_hellos; + break; + case 'q': + if (optarg && isdigit(*optarg)) + qual_threshold = atoi(optarg); + break; + case 'r': + if (optarg && isdigit(*optarg)) + rt_log_interval = atof(optarg) * 1000; + break; + case 'u': + unidir_hack = !unidir_hack; + break; + case 'w': + internet_gw_mode = !internet_gw_mode; + break; + case 'x': + expanding_ring_search = !expanding_ring_search; + break; + case 'L': + local_repair = !local_repair; + break; + case 'D': + wait_on_reboot = !wait_on_reboot; + break; + case 'R': + ratelimit = !ratelimit; + break; + case 'V': + printf("\nAODV-UU v%s, %s © Uppsala University & Ericsson " + "AB.\nAuthor: Erik Nordström, \n\n", + AODV_UU_VERSION, DRAFT_VERSION); + exit(0); + break; + case '?': + case ':': + exit(0); + default: + usage(0); + } } /* Check that we are running as root */ if (geteuid() != 0) { - fprintf(stderr, "must be root\n"); - exit(1); + fprintf(stderr, "must be root\n"); + exit(1); } /* Detach from terminal */ if (daemonize) { - if (fork() != 0) - exit(0); - /* Close stdin, stdout and stderr... */ - /* close(0); */ - close(1); - close(2); - setsid(); + if (fork() != 0) + exit(0); + /* Close stdin, stdout and stderr... */ + /* close(0); */ + close(1); + close(2); + setsid(); } /* Make sure we cleanup at exit... */ - atexit((void *) &cleanup); + atexit((void *)&cleanup); /* Initialize data structures and services... */ rt_table_init(); @@ -633,59 +641,60 @@ int main(int argc, char **argv) aodv_socket_init(); #ifdef LLFEEDBACK if (llfeedback) { - llf_init(); + llf_init(); } #endif /* Set sockets to watch... */ FD_ZERO(&readers); for (i = 0; i < nr_callbacks; i++) { - FD_SET(callbacks[i].fd, &readers); - if (callbacks[i].fd >= nfds) - nfds = callbacks[i].fd + 1; + FD_SET(callbacks[i].fd, &readers); + if (callbacks[i].fd >= nfds) + nfds = callbacks[i].fd + 1; } /* Set the wait on reboot timer... */ if (wait_on_reboot) { - timer_init(&worb_timer, wait_on_reboot_timeout, &wait_on_reboot); - timer_set_timeout(&worb_timer, DELETE_PERIOD); - alog(LOG_NOTICE, 0, __FUNCTION__, - "In wait on reboot for %d milliseconds. Disable with \"-D\".", - DELETE_PERIOD); + timer_init(&worb_timer, wait_on_reboot_timeout, &wait_on_reboot); + timer_set_timeout(&worb_timer, DELETE_PERIOD); + alog(LOG_NOTICE, 0, __FUNCTION__, + "In wait on reboot for %d milliseconds. Disable with \"-D\".", + DELETE_PERIOD); } /* Schedule the first Hello */ if (!optimized_hellos && !llfeedback) - hello_start(); + hello_start(); if (rt_log_interval) - log_rt_table_init(); + log_rt_table_init(); while (1) { - memcpy((char *) &rfds, (char *) &readers, sizeof(rfds)); - - timeout = timer_age_queue(); - - timeout_spec.tv_sec = timeout->tv_sec; - timeout_spec.tv_nsec = timeout->tv_usec * 1000; - - if ((n = pselect(nfds, &rfds, NULL, NULL, &timeout_spec, &origmask)) < 0) { - if (errno != EINTR) - alog(LOG_WARNING, errno, __FUNCTION__, - "Failed select (main loop)"); - continue; - } - - if (n > 0) { - for (i = 0; i < nr_callbacks; i++) { - if (FD_ISSET(callbacks[i].fd, &rfds)) { - /* We don't want any timer SIGALRM's while executing the - callback functions, therefore we block the timer... */ - (*callbacks[i].func) (callbacks[i].fd); - } - } - } - } /* Main loop */ + memcpy((char *)&rfds, (char *)&readers, sizeof(rfds)); + + timeout = timer_age_queue(); + + timeout_spec.tv_sec = timeout->tv_sec; + timeout_spec.tv_nsec = timeout->tv_usec * 1000; + + if ((n = pselect(nfds, &rfds, NULL, NULL, &timeout_spec, &origmask)) < + 0) { + if (errno != EINTR) + alog(LOG_WARNING, errno, __FUNCTION__, + "Failed select (main loop)"); + continue; + } + + if (n > 0) { + for (i = 0; i < nr_callbacks; i++) { + if (FD_ISSET(callbacks[i].fd, &rfds)) { + /* We don't want any timer SIGALRM's while executing the + callback functions, therefore we block the timer... */ + (*callbacks[i].func)(callbacks[i].fd); + } + } + } + } /* Main loop */ return 0; } @@ -696,7 +705,7 @@ static void cleanup(void) aodv_socket_cleanup(); #ifdef LLFEEDBACK if (llfeedback) - llf_cleanup(); + llf_cleanup(); #endif log_cleanup(); nl_cleanup(); diff --git a/nl.c b/nl.c index 77b3b1c..b56620e 100644 --- a/nl.c +++ b/nl.c @@ -17,44 +17,44 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Author: Erik Nordström, - * + * *****************************************************************************/ -#include +#include +#include +#include +#include +#include +#include #include +#include #include +#include +#include #include -#include -#include #include -#include -#include -#include -#include -#include -#include -#include +#include -#include "defs.h" -#include "lnx/kaodv-netlink.h" -#include "debug.h" +#include "aodv_hello.h" +#include "aodv_rerr.h" #include "aodv_rreq.h" +#include "aodv_socket.h" #include "aodv_timeout.h" -#include "routing_table.h" -#include "aodv_hello.h" +#include "debug.h" +#include "defs.h" +#include "lnx/kaodv-netlink.h" #include "params.h" -#include "aodv_socket.h" -#include "aodv_rerr.h" +#include "routing_table.h" /* Implements a Netlink socket communication channel to the kernel. Route * information and refresh messages are passed. */ struct nlsock { - int sock; - int seq; - struct sockaddr_nl local; + int sock; + int seq; + struct sockaddr_nl local; }; -struct sockaddr_nl peer = { AF_NETLINK, 0, 0, 0 }; +struct sockaddr_nl peer = {AF_NETLINK, 0, 0, 0}; struct nlsock aodvnl; struct nlsock rtnl; @@ -72,533 +72,514 @@ extern struct timer worb_timer; void nl_init(void) { - int status; - unsigned int addrlen; - - memset(&peer, 0, sizeof(struct sockaddr_nl)); - peer.nl_family = AF_NETLINK; - peer.nl_pid = 0; - peer.nl_groups = 0; - - memset(&aodvnl, 0, sizeof(struct nlsock)); - aodvnl.seq = 0; - aodvnl.local.nl_family = AF_NETLINK; - aodvnl.local.nl_groups = AODVGRP_NOTIFY; - aodvnl.local.nl_pid = getpid(); - - /* This is the AODV specific socket to communicate with the - AODV kernel module */ - aodvnl.sock = socket(PF_NETLINK, SOCK_RAW, NETLINK_AODV); - - if (aodvnl.sock < 0) { - perror("Unable to create AODV netlink socket"); - exit(-1); - } - - - status = bind(aodvnl.sock, (struct sockaddr *) &aodvnl.local, - sizeof(aodvnl.local)); - - if (status == -1) { - perror("Bind for AODV netlink socket failed"); - exit(-1); - } - - addrlen = sizeof(aodvnl.local); - - if (getsockname - (aodvnl.sock, (struct sockaddr *) &aodvnl.local, &addrlen) < 0) { - perror("Getsockname failed "); - exit(-1); - } - - if (attach_callback_func(aodvnl.sock, nl_kaodv_callback) < 0) { - alog(LOG_ERR, 0, __FUNCTION__, "Could not attach callback."); - } - /* This socket is the generic routing socket for adding and - removing kernel routing table entries */ - - memset(&rtnl, 0, sizeof(struct nlsock)); - rtnl.seq = 0; - rtnl.local.nl_family = AF_NETLINK; - rtnl.local.nl_groups = - RTMGRP_NOTIFY | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE; - rtnl.local.nl_pid = getpid(); - - rtnl.sock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); - - if (rtnl.sock < 0) { - perror("Unable to create RT netlink socket"); - exit(-1); - } - - addrlen = sizeof(rtnl.local); - - status = bind(rtnl.sock, (struct sockaddr *) &rtnl.local, addrlen); - - if (status == -1) { - perror("Bind for RT netlink socket failed"); - exit(-1); - } - - if (getsockname(rtnl.sock, (struct sockaddr *) &rtnl.local, &addrlen) < - 0) { - perror("Getsockname failed "); - exit(-1); - } - - if (attach_callback_func(rtnl.sock, nl_rt_callback) < 0) { - alog(LOG_ERR, 0, __FUNCTION__, "Could not attach callback."); - } + int status; + unsigned int addrlen; + + memset(&peer, 0, sizeof(struct sockaddr_nl)); + peer.nl_family = AF_NETLINK; + peer.nl_pid = 0; + peer.nl_groups = 0; + + memset(&aodvnl, 0, sizeof(struct nlsock)); + aodvnl.seq = 0; + aodvnl.local.nl_family = AF_NETLINK; + aodvnl.local.nl_groups = AODVGRP_NOTIFY; + aodvnl.local.nl_pid = getpid(); + + /* This is the AODV specific socket to communicate with the + AODV kernel module */ + aodvnl.sock = socket(PF_NETLINK, SOCK_RAW, NETLINK_AODV); + + if (aodvnl.sock < 0) { + perror("Unable to create AODV netlink socket"); + exit(-1); + } + + status = bind(aodvnl.sock, (struct sockaddr *)&aodvnl.local, + sizeof(aodvnl.local)); + + if (status == -1) { + perror("Bind for AODV netlink socket failed"); + exit(-1); + } + + addrlen = sizeof(aodvnl.local); + + if (getsockname(aodvnl.sock, (struct sockaddr *)&aodvnl.local, &addrlen) < + 0) { + perror("Getsockname failed "); + exit(-1); + } + + if (attach_callback_func(aodvnl.sock, nl_kaodv_callback) < 0) { + alog(LOG_ERR, 0, __FUNCTION__, "Could not attach callback."); + } + /* This socket is the generic routing socket for adding and + removing kernel routing table entries */ + + memset(&rtnl, 0, sizeof(struct nlsock)); + rtnl.seq = 0; + rtnl.local.nl_family = AF_NETLINK; + rtnl.local.nl_groups = + RTMGRP_NOTIFY | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE; + rtnl.local.nl_pid = getpid(); + + rtnl.sock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); + + if (rtnl.sock < 0) { + perror("Unable to create RT netlink socket"); + exit(-1); + } + + addrlen = sizeof(rtnl.local); + + status = bind(rtnl.sock, (struct sockaddr *)&rtnl.local, addrlen); + + if (status == -1) { + perror("Bind for RT netlink socket failed"); + exit(-1); + } + + if (getsockname(rtnl.sock, (struct sockaddr *)&rtnl.local, &addrlen) < 0) { + perror("Getsockname failed "); + exit(-1); + } + + if (attach_callback_func(rtnl.sock, nl_rt_callback) < 0) { + alog(LOG_ERR, 0, __FUNCTION__, "Could not attach callback."); + } } void nl_cleanup(void) { - close(aodvnl.sock); - close(rtnl.sock); + close(aodvnl.sock); + close(rtnl.sock); } - static void nl_kaodv_callback(int sock) { - int len; - socklen_t addrlen; - struct nlmsghdr *nlm; - struct nlmsgerr *nlmerr; - char buf[BUFLEN]; - struct in_addr dest_addr, src_addr; - kaodv_rt_msg_t *m; - rt_table_t *rt, *fwd_rt, *rev_rt = NULL; - - addrlen = sizeof(struct sockaddr_nl); - - - len = - recvfrom(sock, buf, BUFLEN, 0, (struct sockaddr *) &peer, &addrlen); - - if (len <= 0) - return; - - nlm = (struct nlmsghdr *) buf; - - switch (nlm->nlmsg_type) { - case NLMSG_ERROR: - nlmerr = NLMSG_DATA(nlm); - if (nlmerr->error == 0) { - /* DEBUG(LOG_DEBUG, 0, "NLMSG_ACK"); */ - } else { - DEBUG(LOG_DEBUG, 0, "NLMSG_ERROR, error=%d type=%s", - nlmerr->error, - kaodv_msg_type_to_str(nlmerr->msg.nlmsg_type)); - } - break; - - case KAODVM_DEBUG: - DEBUG(LOG_DEBUG, 0, "kaodv: %s", NLMSG_DATA(nlm)); - break; - case KAODVM_TIMEOUT: - m = NLMSG_DATA(nlm); - dest_addr.s_addr = m->dst; - - DEBUG(LOG_DEBUG, 0, - "Got TIMEOUT msg from kernel for %s", - ip_to_str(dest_addr)); - - rt = rt_table_find(dest_addr); - - if (rt && rt->state == VALID) - route_expire_timeout(rt); - else - DEBUG(LOG_DEBUG, 0, - "Got rt timeoute event but there is no route"); - break; - case KAODVM_ROUTE_REQ: - m = NLMSG_DATA(nlm); - dest_addr.s_addr = m->dst; - - DEBUG(LOG_DEBUG, 0, "Got ROUTE_REQ: %s from kernel", - ip_to_str(dest_addr)); - - rreq_route_discovery(dest_addr, 0, NULL); - break; - case KAODVM_REPAIR: - m = NLMSG_DATA(nlm); - dest_addr.s_addr = m->dst; - src_addr.s_addr = m->src; - - DEBUG(LOG_DEBUG, 0, "Got REPAIR from kernel for %s", - ip_to_str(dest_addr)); - - fwd_rt = rt_table_find(dest_addr); - - if (fwd_rt) - rreq_local_repair(fwd_rt, src_addr, NULL); - - break; - case KAODVM_ROUTE_UPDATE: - m = NLMSG_DATA(nlm); - - - dest_addr.s_addr = m->dst; - src_addr.s_addr = m->src; - - // DEBUG(LOG_DEBUG, 0, "ROute update s=%s d=%s", ip_to_str(src_addr), ip_to_str(dest_addr)); - if (dest_addr.s_addr == AODV_BROADCAST || - dest_addr.s_addr == - DEV_IFINDEX(m->ifindex).broadcast.s_addr) - return; - - fwd_rt = rt_table_find(dest_addr); - rev_rt = rt_table_find(src_addr); - - rt_table_update_route_timeouts(fwd_rt, rev_rt); - - break; - case KAODVM_SEND_RERR: - m = NLMSG_DATA(nlm); - dest_addr.s_addr = m->dst; - src_addr.s_addr = m->src; - - if (dest_addr.s_addr == AODV_BROADCAST || - dest_addr.s_addr == - DEV_IFINDEX(m->ifindex).broadcast.s_addr) - return; - - fwd_rt = rt_table_find(dest_addr); - rev_rt = rt_table_find(src_addr); - - do { - struct in_addr rerr_dest; - RERR *rerr; - - DEBUG(LOG_DEBUG, 0, - "Sending RERR for unsolicited message from %s to dest %s", - ip_to_str(src_addr), ip_to_str(dest_addr)); - - if (fwd_rt) { - rerr = rerr_create(0, fwd_rt->dest_addr, - fwd_rt->dest_seqno); - - rt_table_update_timeout(fwd_rt, DELETE_PERIOD); - } else - rerr = rerr_create(0, dest_addr, 0); - - /* Unicast the RERR to the source of the data transmission - * if possible, otherwise we broadcast it. */ - - if (rev_rt && rev_rt->state == VALID) - rerr_dest = rev_rt->next_hop; - else - rerr_dest.s_addr = AODV_BROADCAST; - - aodv_socket_send((AODV_msg *) rerr, rerr_dest, - RERR_CALC_SIZE(rerr), 1, - &DEV_IFINDEX(m->ifindex)); - - if (wait_on_reboot) { - DEBUG(LOG_DEBUG, 0, - "Wait on reboot timer reset."); - timer_set_timeout(&worb_timer, DELETE_PERIOD); - } - } while (0); - break; - default: - DEBUG(LOG_DEBUG, 0, "Got mesg type=%d\n", nlm->nlmsg_type); - } - + int len; + socklen_t addrlen; + struct nlmsghdr *nlm; + struct nlmsgerr *nlmerr; + char buf[BUFLEN]; + struct in_addr dest_addr, src_addr; + kaodv_rt_msg_t *m; + rt_table_t *rt, *fwd_rt, *rev_rt = NULL; + + addrlen = sizeof(struct sockaddr_nl); + + len = recvfrom(sock, buf, BUFLEN, 0, (struct sockaddr *)&peer, &addrlen); + + if (len <= 0) + return; + + nlm = (struct nlmsghdr *)buf; + + switch (nlm->nlmsg_type) { + case NLMSG_ERROR: + nlmerr = NLMSG_DATA(nlm); + if (nlmerr->error == 0) { + /* DEBUG(LOG_DEBUG, 0, "NLMSG_ACK"); */ + } else { + DEBUG(LOG_DEBUG, 0, "NLMSG_ERROR, error=%d type=%s", nlmerr->error, + kaodv_msg_type_to_str(nlmerr->msg.nlmsg_type)); + } + break; + + case KAODVM_DEBUG: + DEBUG(LOG_DEBUG, 0, "kaodv: %s", NLMSG_DATA(nlm)); + break; + case KAODVM_TIMEOUT: + m = NLMSG_DATA(nlm); + dest_addr.s_addr = m->dst; + + DEBUG(LOG_DEBUG, 0, "Got TIMEOUT msg from kernel for %s", + ip_to_str(dest_addr)); + + rt = rt_table_find(dest_addr); + + if (rt && rt->state == VALID) + route_expire_timeout(rt); + else + DEBUG(LOG_DEBUG, 0, "Got rt timeoute event but there is no route"); + break; + case KAODVM_ROUTE_REQ: + m = NLMSG_DATA(nlm); + dest_addr.s_addr = m->dst; + + DEBUG(LOG_DEBUG, 0, "Got ROUTE_REQ: %s from kernel", + ip_to_str(dest_addr)); + + rreq_route_discovery(dest_addr, 0, NULL); + break; + case KAODVM_REPAIR: + m = NLMSG_DATA(nlm); + dest_addr.s_addr = m->dst; + src_addr.s_addr = m->src; + + DEBUG(LOG_DEBUG, 0, "Got REPAIR from kernel for %s", + ip_to_str(dest_addr)); + + fwd_rt = rt_table_find(dest_addr); + + if (fwd_rt) + rreq_local_repair(fwd_rt, src_addr, NULL); + + break; + case KAODVM_ROUTE_UPDATE: + m = NLMSG_DATA(nlm); + + dest_addr.s_addr = m->dst; + src_addr.s_addr = m->src; + + // DEBUG(LOG_DEBUG, 0, "ROute update s=%s d=%s", + //ip_to_str(src_addr), ip_to_str(dest_addr)); + if (dest_addr.s_addr == AODV_BROADCAST || + dest_addr.s_addr == DEV_IFINDEX(m->ifindex).broadcast.s_addr) + return; + + fwd_rt = rt_table_find(dest_addr); + rev_rt = rt_table_find(src_addr); + + rt_table_update_route_timeouts(fwd_rt, rev_rt); + + break; + case KAODVM_SEND_RERR: + m = NLMSG_DATA(nlm); + dest_addr.s_addr = m->dst; + src_addr.s_addr = m->src; + + if (dest_addr.s_addr == AODV_BROADCAST || + dest_addr.s_addr == DEV_IFINDEX(m->ifindex).broadcast.s_addr) + return; + + fwd_rt = rt_table_find(dest_addr); + rev_rt = rt_table_find(src_addr); + + do { + struct in_addr rerr_dest; + RERR *rerr; + + DEBUG(LOG_DEBUG, 0, + "Sending RERR for unsolicited message from %s to dest %s", + ip_to_str(src_addr), ip_to_str(dest_addr)); + + if (fwd_rt) { + rerr = rerr_create(0, fwd_rt->dest_addr, fwd_rt->dest_seqno); + + rt_table_update_timeout(fwd_rt, DELETE_PERIOD); + } else + rerr = rerr_create(0, dest_addr, 0); + + /* Unicast the RERR to the source of the data transmission + * if possible, otherwise we broadcast it. */ + + if (rev_rt && rev_rt->state == VALID) + rerr_dest = rev_rt->next_hop; + else + rerr_dest.s_addr = AODV_BROADCAST; + + aodv_socket_send((AODV_msg *)rerr, rerr_dest, RERR_CALC_SIZE(rerr), + 1, &DEV_IFINDEX(m->ifindex)); + + if (wait_on_reboot) { + DEBUG(LOG_DEBUG, 0, "Wait on reboot timer reset."); + timer_set_timeout(&worb_timer, DELETE_PERIOD); + } + } while (0); + break; + default: + DEBUG(LOG_DEBUG, 0, "Got mesg type=%d\n", nlm->nlmsg_type); + } } static void nl_rt_callback(int sock) { - int len, attrlen; - socklen_t addrlen; - struct nlmsghdr *nlm; - struct nlmsgerr *nlmerr; - char buf[BUFLEN]; - struct ifaddrmsg *ifm; - struct rtattr *rta; - - addrlen = sizeof(struct sockaddr_nl); - - len = - recvfrom(sock, buf, BUFLEN, 0, (struct sockaddr *) &peer, &addrlen); - - if (len <= 0) - return; - - nlm = (struct nlmsghdr *) buf; - - switch (nlm->nlmsg_type) { - case NLMSG_ERROR: - nlmerr = NLMSG_DATA(nlm); - if (nlmerr->error == 0) { - /* DEBUG(LOG_DEBUG, 0, "NLMSG_ACK"); */ - } else { - DEBUG(LOG_DEBUG, 0, "NLMSG_ERROR, error=%d type=%d", - nlmerr->error, nlmerr->msg.nlmsg_type); - } - break; - case RTM_NEWLINK: - DEBUG(LOG_DEBUG, 0, "RTM_NEWADDR"); - break; - case RTM_NEWADDR: - ifm = NLMSG_DATA(nlm); - - rta = (struct rtattr *) ((char *) ifm + sizeof(ifm)); - - attrlen = nlm->nlmsg_len - - sizeof(struct nlmsghdr) - sizeof(struct ifaddrmsg); - - for (; RTA_OK(rta, attrlen); rta = RTA_NEXT(rta, attrlen)) { - - if (rta->rta_type == IFA_ADDRESS) { - struct in_addr ifaddr; - - memcpy(&ifaddr, RTA_DATA(rta), - RTA_PAYLOAD(rta)); - - DEBUG(LOG_DEBUG, 0, - "Interface index %d changed address to %s", - ifm->ifa_index, ip_to_str(ifaddr)); - } - } - break; - case RTM_NEWROUTE: - /* DEBUG(LOG_DEBUG, 0, "RTM_NEWROUTE"); */ - break; - } - return; + int len, attrlen; + socklen_t addrlen; + struct nlmsghdr *nlm; + struct nlmsgerr *nlmerr; + char buf[BUFLEN]; + struct ifaddrmsg *ifm; + struct rtattr *rta; + + addrlen = sizeof(struct sockaddr_nl); + + len = recvfrom(sock, buf, BUFLEN, 0, (struct sockaddr *)&peer, &addrlen); + + if (len <= 0) + return; + + nlm = (struct nlmsghdr *)buf; + + switch (nlm->nlmsg_type) { + case NLMSG_ERROR: + nlmerr = NLMSG_DATA(nlm); + if (nlmerr->error == 0) { + /* DEBUG(LOG_DEBUG, 0, "NLMSG_ACK"); */ + } else { + DEBUG(LOG_DEBUG, 0, "NLMSG_ERROR, error=%d type=%d", nlmerr->error, + nlmerr->msg.nlmsg_type); + } + break; + case RTM_NEWLINK: + DEBUG(LOG_DEBUG, 0, "RTM_NEWADDR"); + break; + case RTM_NEWADDR: + ifm = NLMSG_DATA(nlm); + + rta = (struct rtattr *)((char *)ifm + sizeof(ifm)); + + attrlen = + nlm->nlmsg_len - sizeof(struct nlmsghdr) - sizeof(struct ifaddrmsg); + + for (; RTA_OK(rta, attrlen); rta = RTA_NEXT(rta, attrlen)) { + + if (rta->rta_type == IFA_ADDRESS) { + struct in_addr ifaddr; + + memcpy(&ifaddr, RTA_DATA(rta), RTA_PAYLOAD(rta)); + + DEBUG(LOG_DEBUG, 0, "Interface index %d changed address to %s", + ifm->ifa_index, ip_to_str(ifaddr)); + } + } + break; + case RTM_NEWROUTE: + /* DEBUG(LOG_DEBUG, 0, "RTM_NEWROUTE"); */ + break; + } + return; } int prefix_length(int family, void *nm) { - int prefix = 0; + int prefix = 0; - if (family == AF_INET) { - unsigned int tmp; - memcpy(&tmp, nm, sizeof(unsigned int)); + if (family == AF_INET) { + unsigned int tmp; + memcpy(&tmp, nm, sizeof(unsigned int)); - while (tmp) { - tmp = tmp << 1; - prefix++; - } - return prefix; + while (tmp) { + tmp = tmp << 1; + prefix++; + } + return prefix; - } else { - DEBUG(LOG_DEBUG, 0, "Unsupported address family"); - } + } else { + DEBUG(LOG_DEBUG, 0, "Unsupported address family"); + } - return 0; + return 0; } -/* Utility function comes from iproute2. +/* Utility function comes from iproute2. Authors: Alexey Kuznetsov, */ int addattr(struct nlmsghdr *n, int type, void *data, int alen) { - struct rtattr *attr; - int len = RTA_LENGTH(alen); + struct rtattr *attr; + int len = RTA_LENGTH(alen); - attr = (struct rtattr *) (((char *) n) + NLMSG_ALIGN(n->nlmsg_len)); - attr->rta_type = type; - attr->rta_len = len; - memcpy(RTA_DATA(attr), data, alen); - n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len; + attr = (struct rtattr *)(((char *)n) + NLMSG_ALIGN(n->nlmsg_len)); + attr->rta_type = type; + attr->rta_len = len; + memcpy(RTA_DATA(attr), data, alen); + n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len; - return 0; + return 0; } - #define ATTR_BUFLEN 512 int nl_send(struct nlsock *nl, struct nlmsghdr *n) { - int res; - struct iovec iov = { (void *) n, n->nlmsg_len }; - struct msghdr msg = - { (void *) &peer, sizeof(peer), &iov, 1, NULL, 0, 0 }; - // int flags = 0; + int res; + struct iovec iov = {(void *)n, n->nlmsg_len}; + struct msghdr msg = {(void *)&peer, sizeof(peer), &iov, 1, NULL, 0, 0}; + // int flags = 0; - if (!nl) - return -1; + if (!nl) + return -1; - n->nlmsg_seq = ++nl->seq; - n->nlmsg_pid = nl->local.nl_pid; + n->nlmsg_seq = ++nl->seq; + n->nlmsg_pid = nl->local.nl_pid; - /* Request an acknowledgement by setting NLM_F_ACK */ - n->nlmsg_flags |= NLM_F_ACK; + /* Request an acknowledgement by setting NLM_F_ACK */ + n->nlmsg_flags |= NLM_F_ACK; - /* Send message to netlink interface. */ - res = sendmsg(nl->sock, &msg, 0); + /* Send message to netlink interface. */ + res = sendmsg(nl->sock, &msg, 0); - if (res < 0) { - fprintf(stderr, "error: %s\n", strerror(errno)); - return -1; - } - return 0; + if (res < 0) { + fprintf(stderr, "error: %s\n", strerror(errno)); + return -1; + } + return 0; } /* Function to add, remove and update entries in the kernel routing * table */ -int nl_kern_route(int action, int flags, int family, - int index, struct in_addr *dst, struct in_addr *gw, - struct in_addr *nm, int metric) +int nl_kern_route(int action, int flags, int family, int index, + struct in_addr *dst, struct in_addr *gw, struct in_addr *nm, + int metric) { - struct { - struct nlmsghdr nlh; - struct rtmsg rtm; - char attrbuf[1024]; - } req; + struct { + struct nlmsghdr nlh; + struct rtmsg rtm; + char attrbuf[1024]; + } req; - if (!dst || !gw) - return -1; + if (!dst || !gw) + return -1; - req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); - req.nlh.nlmsg_type = action; - req.nlh.nlmsg_flags = NLM_F_REQUEST | flags; - req.nlh.nlmsg_pid = 0; + req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); + req.nlh.nlmsg_type = action; + req.nlh.nlmsg_flags = NLM_F_REQUEST | flags; + req.nlh.nlmsg_pid = 0; - req.rtm.rtm_family = family; + req.rtm.rtm_family = family; - if (!nm) - req.rtm.rtm_dst_len = sizeof(struct in_addr) * 8; - else - req.rtm.rtm_dst_len = prefix_length(AF_INET, nm); + if (!nm) + req.rtm.rtm_dst_len = sizeof(struct in_addr) * 8; + else + req.rtm.rtm_dst_len = prefix_length(AF_INET, nm); - req.rtm.rtm_src_len = 0; - req.rtm.rtm_tos = 0; - req.rtm.rtm_table = RT_TABLE_MAIN; - req.rtm.rtm_protocol = 100; - req.rtm.rtm_scope = RT_SCOPE_LINK; - req.rtm.rtm_type = RTN_UNICAST; - req.rtm.rtm_flags = 0; + req.rtm.rtm_src_len = 0; + req.rtm.rtm_tos = 0; + req.rtm.rtm_table = RT_TABLE_MAIN; + req.rtm.rtm_protocol = 100; + req.rtm.rtm_scope = RT_SCOPE_LINK; + req.rtm.rtm_type = RTN_UNICAST; + req.rtm.rtm_flags = 0; - addattr(&req.nlh, RTA_DST, dst, sizeof(struct in_addr)); + addattr(&req.nlh, RTA_DST, dst, sizeof(struct in_addr)); - if (memcmp(dst, gw, sizeof(struct in_addr)) != 0) { - req.rtm.rtm_scope = RT_SCOPE_UNIVERSE; - addattr(&req.nlh, RTA_GATEWAY, gw, sizeof(struct in_addr)); - } + if (memcmp(dst, gw, sizeof(struct in_addr)) != 0) { + req.rtm.rtm_scope = RT_SCOPE_UNIVERSE; + addattr(&req.nlh, RTA_GATEWAY, gw, sizeof(struct in_addr)); + } - if (index > 0) - addattr(&req.nlh, RTA_OIF, &index, sizeof(index)); + if (index > 0) + addattr(&req.nlh, RTA_OIF, &index, sizeof(index)); - addattr(&req.nlh, RTA_PRIORITY, &metric, sizeof(metric)); + addattr(&req.nlh, RTA_PRIORITY, &metric, sizeof(metric)); - return nl_send(&rtnl, &req.nlh); + return nl_send(&rtnl, &req.nlh); } int nl_send_add_route_msg(struct in_addr dest, struct in_addr next_hop, - int metric, u_int32_t lifetime, int rt_flags, - int ifindex) + int metric, u_int32_t lifetime, int rt_flags, + int ifindex) { - struct { - struct nlmsghdr n; - struct kaodv_rt_msg m; - } areq; + struct { + struct nlmsghdr n; + struct kaodv_rt_msg m; + } areq; - DEBUG(LOG_DEBUG, 0, "ADD/UPDATE: %s:%s ifindex=%d", - ip_to_str(dest), ip_to_str(next_hop), ifindex); + DEBUG(LOG_DEBUG, 0, "ADD/UPDATE: %s:%s ifindex=%d", ip_to_str(dest), + ip_to_str(next_hop), ifindex); - memset(&areq, 0, sizeof(areq)); + memset(&areq, 0, sizeof(areq)); - areq.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct kaodv_rt_msg)); - areq.n.nlmsg_type = KAODVM_ADDROUTE; - areq.n.nlmsg_flags = NLM_F_REQUEST; + areq.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct kaodv_rt_msg)); + areq.n.nlmsg_type = KAODVM_ADDROUTE; + areq.n.nlmsg_flags = NLM_F_REQUEST; - areq.m.dst = dest.s_addr; - areq.m.nhop = next_hop.s_addr; - areq.m.time = lifetime; - areq.m.ifindex = ifindex; + areq.m.dst = dest.s_addr; + areq.m.nhop = next_hop.s_addr; + areq.m.time = lifetime; + areq.m.ifindex = ifindex; - if (rt_flags & RT_INET_DEST) { - areq.m.flags |= KAODV_RT_GW_ENCAP; - } + if (rt_flags & RT_INET_DEST) { + areq.m.flags |= KAODV_RT_GW_ENCAP; + } - if (rt_flags & RT_REPAIR) - areq.m.flags |= KAODV_RT_REPAIR; + if (rt_flags & RT_REPAIR) + areq.m.flags |= KAODV_RT_REPAIR; - if (nl_send(&aodvnl, &areq.n) < 0) { - DEBUG(LOG_DEBUG, 0, "Failed to send netlink message"); - return -1; - } + if (nl_send(&aodvnl, &areq.n) < 0) { + DEBUG(LOG_DEBUG, 0, "Failed to send netlink message"); + return -1; + } #ifdef DEBUG_NETLINK - DEBUG(LOG_DEBUG, 0, "Sending add route"); + DEBUG(LOG_DEBUG, 0, "Sending add route"); #endif - return nl_kern_route(RTM_NEWROUTE, NLM_F_CREATE, - AF_INET, ifindex, &dest, &next_hop, NULL, metric); + return nl_kern_route(RTM_NEWROUTE, NLM_F_CREATE, AF_INET, ifindex, &dest, + &next_hop, NULL, metric); } int nl_send_no_route_found_msg(struct in_addr dest) { - struct { - struct nlmsghdr n; - kaodv_rt_msg_t m; - } areq; + struct { + struct nlmsghdr n; + kaodv_rt_msg_t m; + } areq; - memset(&areq, 0, sizeof(areq)); + memset(&areq, 0, sizeof(areq)); - areq.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct kaodv_rt_msg)); - areq.n.nlmsg_type = KAODVM_NOROUTE_FOUND; - areq.n.nlmsg_flags = NLM_F_REQUEST; + areq.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct kaodv_rt_msg)); + areq.n.nlmsg_type = KAODVM_NOROUTE_FOUND; + areq.n.nlmsg_flags = NLM_F_REQUEST; - areq.m.dst = dest.s_addr; + areq.m.dst = dest.s_addr; - DEBUG(LOG_DEBUG, 0, "Send NOROUTE_FOUND to kernel: %s", - ip_to_str(dest)); + DEBUG(LOG_DEBUG, 0, "Send NOROUTE_FOUND to kernel: %s", ip_to_str(dest)); - return nl_send(&aodvnl, &areq.n); + return nl_send(&aodvnl, &areq.n); } -int nl_send_del_route_msg(struct in_addr dest, struct in_addr next_hop, int metric) +int nl_send_del_route_msg(struct in_addr dest, struct in_addr next_hop, + int metric) { - int index = -1; - struct { - struct nlmsghdr n; - struct kaodv_rt_msg m; - } areq; + int index = -1; + struct { + struct nlmsghdr n; + struct kaodv_rt_msg m; + } areq; - DEBUG(LOG_DEBUG, 0, "Send DEL_ROUTE to kernel: %s", ip_to_str(dest)); + DEBUG(LOG_DEBUG, 0, "Send DEL_ROUTE to kernel: %s", ip_to_str(dest)); - memset(&areq, 0, sizeof(areq)); + memset(&areq, 0, sizeof(areq)); - areq.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct kaodv_rt_msg)); - areq.n.nlmsg_type = KAODVM_DELROUTE; - areq.n.nlmsg_flags = NLM_F_REQUEST; + areq.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct kaodv_rt_msg)); + areq.n.nlmsg_type = KAODVM_DELROUTE; + areq.n.nlmsg_flags = NLM_F_REQUEST; - areq.m.dst = dest.s_addr; - areq.m.nhop = next_hop.s_addr; - areq.m.time = 0; - areq.m.flags = 0; + areq.m.dst = dest.s_addr; + areq.m.nhop = next_hop.s_addr; + areq.m.time = 0; + areq.m.flags = 0; - if (nl_send(&aodvnl, &areq.n) < 0) { - DEBUG(LOG_DEBUG, 0, "Failed to send netlink message"); - return -1; - } + if (nl_send(&aodvnl, &areq.n) < 0) { + DEBUG(LOG_DEBUG, 0, "Failed to send netlink message"); + return -1; + } #ifdef DEBUG_NETLINK - DEBUG(LOG_DEBUG, 0, "Sending del route"); + DEBUG(LOG_DEBUG, 0, "Sending del route"); #endif - return nl_kern_route(RTM_DELROUTE, 0, AF_INET, index, &dest, &next_hop, - NULL, metric); + return nl_kern_route(RTM_DELROUTE, 0, AF_INET, index, &dest, &next_hop, + NULL, metric); } int nl_send_conf_msg(void) { - struct { - struct nlmsghdr n; - kaodv_conf_msg_t cm; - } areq; + struct { + struct nlmsghdr n; + kaodv_conf_msg_t cm; + } areq; - memset(&areq, 0, sizeof(areq)); + memset(&areq, 0, sizeof(areq)); - areq.n.nlmsg_len = NLMSG_LENGTH(sizeof(kaodv_conf_msg_t)); - areq.n.nlmsg_type = KAODVM_CONFIG; - areq.n.nlmsg_flags = NLM_F_REQUEST; + areq.n.nlmsg_len = NLMSG_LENGTH(sizeof(kaodv_conf_msg_t)); + areq.n.nlmsg_type = KAODVM_CONFIG; + areq.n.nlmsg_flags = NLM_F_REQUEST; - areq.cm.qual_th = qual_threshold; - areq.cm.active_route_timeout = active_route_timeout; - areq.cm.is_gateway = internet_gw_mode; + areq.cm.qual_th = qual_threshold; + areq.cm.active_route_timeout = active_route_timeout; + areq.cm.is_gateway = internet_gw_mode; #ifdef DEBUG_NETLINK - DEBUG(LOG_DEBUG, 0, "Sending aodv conf msg"); + DEBUG(LOG_DEBUG, 0, "Sending aodv conf msg"); #endif - return nl_send(&aodvnl, &areq.n); + return nl_send(&aodvnl, &areq.n); } diff --git a/nl.h b/nl.h index d139419..3166951 100644 --- a/nl.h +++ b/nl.h @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Author: Erik Nordström, - * + * *****************************************************************************/ #ifndef _NL_H #define _NL_H @@ -27,9 +27,10 @@ void nl_init(void); void nl_cleanup(void); int nl_send_add_route_msg(struct in_addr dest, struct in_addr next_hop, - int metric, u_int32_t lifetime, int rt_flags, - int ifindex); -int nl_send_del_route_msg(struct in_addr dest, struct in_addr next_hop, int metric); + int metric, u_int32_t lifetime, int rt_flags, + int ifindex); +int nl_send_del_route_msg(struct in_addr dest, struct in_addr next_hop, + int metric); int nl_send_no_route_found_msg(struct in_addr dest); int nl_send_conf_msg(void); diff --git a/params.h b/params.h index 3fbb920..af0913d 100644 --- a/params.h +++ b/params.h @@ -16,8 +16,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, - * + * Authors: Erik Nordström, + * * *****************************************************************************/ #ifndef _PARAMS_H @@ -25,7 +25,7 @@ #include "defs.h" -#define K 5 +#define K 5 /* Dynamic configuration values. Default numbers are for HELLO messages. */ #define ACTIVE_ROUTE_TIMEOUT active_route_timeout @@ -33,35 +33,39 @@ #define DELETE_PERIOD delete_period /* Settings for Link Layer Feedback */ -#define ACTIVE_ROUTE_TIMEOUT_LLF 10000 -#define TTL_START_LLF 1 -#define DELETE_PERIOD_LLF ACTIVE_ROUTE_TIMEOUT_LLF +#define ACTIVE_ROUTE_TIMEOUT_LLF 10000 +#define TTL_START_LLF 1 +#define DELETE_PERIOD_LLF ACTIVE_ROUTE_TIMEOUT_LLF /* Settings for HELLO messages */ -#define ACTIVE_ROUTE_TIMEOUT_HELLO 3000 -#define TTL_START_HELLO 2 -#define DELETE_PERIOD_HELLO K * max(ACTIVE_ROUTE_TIMEOUT_HELLO, ALLOWED_HELLO_LOSS * HELLO_INTERVAL) +#define ACTIVE_ROUTE_TIMEOUT_HELLO 3000 +#define TTL_START_HELLO 2 +#define DELETE_PERIOD_HELLO \ + K *max(ACTIVE_ROUTE_TIMEOUT_HELLO, ALLOWED_HELLO_LOSS *HELLO_INTERVAL) /* Non runtime modifiable settings */ -#define ALLOWED_HELLO_LOSS 2 +#define ALLOWED_HELLO_LOSS 2 /* If expanding ring search is used, BLACKLIST_TIMEOUT should be?: */ -#define BLACKLIST_TIMEOUT RREQ_RETRIES * NET_TRAVERSAL_TIME + (TTL_THRESHOLD - TTL_START)/TTL_INCREMENT + 1 + RREQ_RETRIES -#define HELLO_INTERVAL 1000 -#define LOCAL_ADD_TTL 2 -#define MAX_REPAIR_TTL 3 * NET_DIAMETER / 10 -#define MY_ROUTE_TIMEOUT 2 * ACTIVE_ROUTE_TIMEOUT -#define NET_DIAMETER 35 -#define NET_TRAVERSAL_TIME 2 * NODE_TRAVERSAL_TIME * NET_DIAMETER -#define NEXT_HOP_WAIT NODE_TRAVERSAL_TIME + 10 -#define NODE_TRAVERSAL_TIME 40 -#define PATH_DISCOVERY_TIME 2 * NET_TRAVERSAL_TIME -#define RERR_RATELIMIT 10 -#define RING_TRAVERSAL_TIME 2 * NODE_TRAVERSAL_TIME * (TTL_VALUE + TIMEOUT_BUFFER) -#define RREQ_RETRIES 2 -#define RREQ_RATELIMIT 10 -#define TIMEOUT_BUFFER 2 -#define TTL_INCREMENT 2 -#define TTL_THRESHOLD 7 +#define BLACKLIST_TIMEOUT \ + RREQ_RETRIES *NET_TRAVERSAL_TIME + \ + (TTL_THRESHOLD - TTL_START) / TTL_INCREMENT + 1 + RREQ_RETRIES +#define HELLO_INTERVAL 1000 +#define LOCAL_ADD_TTL 2 +#define MAX_REPAIR_TTL 3 * NET_DIAMETER / 10 +#define MY_ROUTE_TIMEOUT 2 * ACTIVE_ROUTE_TIMEOUT +#define NET_DIAMETER 35 +#define NET_TRAVERSAL_TIME 2 * NODE_TRAVERSAL_TIME *NET_DIAMETER +#define NEXT_HOP_WAIT NODE_TRAVERSAL_TIME + 10 +#define NODE_TRAVERSAL_TIME 40 +#define PATH_DISCOVERY_TIME 2 * NET_TRAVERSAL_TIME +#define RERR_RATELIMIT 10 +#define RING_TRAVERSAL_TIME \ + 2 * NODE_TRAVERSAL_TIME *(TTL_VALUE + TIMEOUT_BUFFER) +#define RREQ_RETRIES 2 +#define RREQ_RATELIMIT 10 +#define TIMEOUT_BUFFER 2 +#define TTL_INCREMENT 2 +#define TTL_THRESHOLD 7 #ifndef NS_PORT /* Dynamic configuration values */ @@ -70,4 +74,4 @@ extern int ttl_start; extern int delete_period; #endif -#endif /* _PARAMS_H */ +#endif /* _PARAMS_H */ diff --git a/routing_table.c b/routing_table.c index 6fe8dd9..657ef71 100644 --- a/routing_table.c +++ b/routing_table.c @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, + * Authors: Erik Nordström, * *****************************************************************************/ @@ -25,614 +25,607 @@ #ifdef NS_PORT #include "ns-2/aodv-uu.h" #else -#include "routing_table.h" -#include "aodv_timeout.h" -#include "aodv_rerr.h" #include "aodv_hello.h" -#include "aodv_socket.h" #include "aodv_neighbor.h" -#include "timer_queue.h" -#include "defs.h" +#include "aodv_rerr.h" +#include "aodv_socket.h" +#include "aodv_timeout.h" #include "debug.h" +#include "defs.h" +#include "nl.h" #include "params.h" +#include "routing_table.h" #include "seek_list.h" -#include "nl.h" -#endif /* NS_PORT */ +#include "timer_queue.h" +#endif /* NS_PORT */ -static unsigned int hashing(struct in_addr *addr, hash_value * hash); +static unsigned int hashing(struct in_addr *addr, hash_value *hash); extern int llfeedback; void NS_CLASS rt_table_init() { - int i; + int i; - rt_tbl.num_entries = 0; - rt_tbl.num_active = 0; + rt_tbl.num_entries = 0; + rt_tbl.num_active = 0; - /* We do a for loop here... NS does not like us to use memset() */ - for (i = 0; i < RT_TABLESIZE; i++) { - INIT_LIST_HEAD(&rt_tbl.tbl[i]); - } + /* We do a for loop here... NS does not like us to use memset() */ + for (i = 0; i < RT_TABLESIZE; i++) { + INIT_LIST_HEAD(&rt_tbl.tbl[i]); + } } void NS_CLASS rt_table_destroy() { - int i; - list_t *tmp = NULL, *pos = NULL; + int i; + list_t *tmp = NULL, *pos = NULL; - for (i = 0; i < RT_TABLESIZE; i++) { - list_foreach_safe(pos, tmp, &rt_tbl.tbl[i]) { - rt_table_t *rt = (rt_table_t *) pos; + for (i = 0; i < RT_TABLESIZE; i++) { + list_foreach_safe(pos, tmp, &rt_tbl.tbl[i]) + { + rt_table_t *rt = (rt_table_t *)pos; - rt_table_delete(rt); - } - } + rt_table_delete(rt); + } + } } /* Calculate a hash value and table index given a key... */ -unsigned int hashing(struct in_addr *addr, hash_value * hash) +unsigned int hashing(struct in_addr *addr, hash_value *hash) { - /* *hash = (*addr & 0x7fffffff); */ - *hash = (hash_value) addr->s_addr; + /* *hash = (*addr & 0x7fffffff); */ + *hash = (hash_value)addr->s_addr; - return (*hash & RT_TABLEMASK); + return (*hash & RT_TABLEMASK); } rt_table_t *NS_CLASS rt_table_insert(struct in_addr dest_addr, - struct in_addr next, - u_int8_t hops, u_int32_t seqno, - u_int32_t life, u_int8_t state, - u_int16_t flags, unsigned int ifindex) + struct in_addr next, u_int8_t hops, + u_int32_t seqno, u_int32_t life, + u_int8_t state, u_int16_t flags, + unsigned int ifindex) { - hash_value hash; - unsigned int index; - list_t *pos; - rt_table_t *rt; - struct in_addr nm; - nm.s_addr = 0; + hash_value hash; + unsigned int index; + list_t *pos; + rt_table_t *rt; + struct in_addr nm; + nm.s_addr = 0; - /* Calculate hash key */ - index = hashing(&dest_addr, &hash); + /* Calculate hash key */ + index = hashing(&dest_addr, &hash); - /* Check if we already have an entry for dest_addr */ - list_foreach(pos, &rt_tbl.tbl[index]) { - rt = (rt_table_t *) pos; - if (memcmp(&rt->dest_addr, &dest_addr, sizeof(struct in_addr)) - == 0) { - DEBUG(LOG_INFO, 0, "%s already exist in routing table!", - ip_to_str(dest_addr)); + /* Check if we already have an entry for dest_addr */ + list_foreach(pos, &rt_tbl.tbl[index]) + { + rt = (rt_table_t *)pos; + if (memcmp(&rt->dest_addr, &dest_addr, sizeof(struct in_addr)) == 0) { + DEBUG(LOG_INFO, 0, "%s already exist in routing table!", + ip_to_str(dest_addr)); - return NULL; - } - } + return NULL; + } + } - if ((rt = (rt_table_t *) malloc(sizeof(rt_table_t))) == NULL) { - fprintf(stderr, "Malloc failed!\n"); - exit(-1); - } + if ((rt = (rt_table_t *)malloc(sizeof(rt_table_t))) == NULL) { + fprintf(stderr, "Malloc failed!\n"); + exit(-1); + } - memset(rt, 0, sizeof(rt_table_t)); + memset(rt, 0, sizeof(rt_table_t)); - rt->dest_addr = dest_addr; - rt->next_hop = next; - rt->dest_seqno = seqno; - rt->flags = flags; - rt->hcnt = hops; - rt->ifindex = ifindex; - rt->hash = hash; - rt->state = state; + rt->dest_addr = dest_addr; + rt->next_hop = next; + rt->dest_seqno = seqno; + rt->flags = flags; + rt->hcnt = hops; + rt->ifindex = ifindex; + rt->hash = hash; + rt->state = state; - timer_init(&rt->rt_timer, &NS_CLASS route_expire_timeout, rt); + timer_init(&rt->rt_timer, &NS_CLASS route_expire_timeout, rt); - timer_init(&rt->ack_timer, &NS_CLASS rrep_ack_timeout, rt); + timer_init(&rt->ack_timer, &NS_CLASS rrep_ack_timeout, rt); - timer_init(&rt->hello_timer, &NS_CLASS hello_timeout, rt); + timer_init(&rt->hello_timer, &NS_CLASS hello_timeout, rt); - rt->last_hello_time.tv_sec = 0; - rt->last_hello_time.tv_usec = 0; - rt->hello_cnt = 0; + rt->last_hello_time.tv_sec = 0; + rt->last_hello_time.tv_usec = 0; + rt->hello_cnt = 0; - rt->nprec = 0; - INIT_LIST_HEAD(&rt->precursors); + rt->nprec = 0; + INIT_LIST_HEAD(&rt->precursors); - /* Insert first in bucket... */ + /* Insert first in bucket... */ - rt_tbl.num_entries++; + rt_tbl.num_entries++; - DEBUG(LOG_INFO, 0, "Inserting %s (bucket %d) next hop %s", - ip_to_str(dest_addr), index, ip_to_str(next)); + DEBUG(LOG_INFO, 0, "Inserting %s (bucket %d) next hop %s", + ip_to_str(dest_addr), index, ip_to_str(next)); - list_add(&rt_tbl.tbl[index], &rt->l); + list_add(&rt_tbl.tbl[index], &rt->l); - if (state == INVALID) { + if (state == INVALID) { - if (flags & RT_REPAIR) { - rt->rt_timer.handler = &NS_CLASS local_repair_timeout; - life = ACTIVE_ROUTE_TIMEOUT; - } else { - rt->rt_timer.handler = &NS_CLASS route_delete_timeout; - life = DELETE_PERIOD; - } + if (flags & RT_REPAIR) { + rt->rt_timer.handler = &NS_CLASS local_repair_timeout; + life = ACTIVE_ROUTE_TIMEOUT; + } else { + rt->rt_timer.handler = &NS_CLASS route_delete_timeout; + life = DELETE_PERIOD; + } - } else { - rt_tbl.num_active++; + } else { + rt_tbl.num_active++; #ifndef NS_PORT - nl_send_add_route_msg(dest_addr, next, hops, life, flags, - ifindex); + nl_send_add_route_msg(dest_addr, next, hops, life, flags, ifindex); #endif - } + } #ifdef CONFIG_GATEWAY_DISABLE - if (rt->flags & RT_GATEWAY) - rt_table_update_inet_rt(rt, life); + if (rt->flags & RT_GATEWAY) + rt_table_update_inet_rt(rt, life); #endif -//#ifdef NS_PORT - DEBUG(LOG_INFO, 0, "New timer for %s, life=%d", - ip_to_str(rt->dest_addr), life); + //#ifdef NS_PORT + DEBUG(LOG_INFO, 0, "New timer for %s, life=%d", ip_to_str(rt->dest_addr), + life); - if (life != 0) - timer_set_timeout(&rt->rt_timer, life); -//#endif - /* In case there are buffered packets for this destination, we - * send them on the new route. */ - if (rt->state == VALID && seek_list_remove(seek_list_find(dest_addr))) { + if (life != 0) + timer_set_timeout(&rt->rt_timer, life); + //#endif + /* In case there are buffered packets for this destination, we + * send them on the new route. */ + if (rt->state == VALID && seek_list_remove(seek_list_find(dest_addr))) { #ifdef NS_PORT - if (rt->flags & RT_INET_DEST) - packet_queue_set_verdict(dest_addr, PQ_ENC_SEND); - else - packet_queue_set_verdict(dest_addr, PQ_SEND); + if (rt->flags & RT_INET_DEST) + packet_queue_set_verdict(dest_addr, PQ_ENC_SEND); + else + packet_queue_set_verdict(dest_addr, PQ_SEND); #endif - } - return rt; + } + return rt; } -rt_table_t *NS_CLASS rt_table_update(rt_table_t * rt, struct in_addr next, - u_int8_t hops, u_int32_t seqno, - u_int32_t lifetime, u_int8_t state, - u_int16_t flags) +rt_table_t *NS_CLASS rt_table_update(rt_table_t *rt, struct in_addr next, + u_int8_t hops, u_int32_t seqno, + u_int32_t lifetime, u_int8_t state, + u_int16_t flags) { - struct in_addr nm; - nm.s_addr = 0; + struct in_addr nm; + nm.s_addr = 0; - if (rt->state == INVALID && state == VALID) { + if (rt->state == INVALID && state == VALID) { - /* If this previously was an expired route, but will now be - active again we must add it to the kernel routing - table... */ - rt_tbl.num_active++; + /* If this previously was an expired route, but will now be + active again we must add it to the kernel routing + table... */ + rt_tbl.num_active++; - if (rt->flags & RT_REPAIR) - flags &= ~RT_REPAIR; + if (rt->flags & RT_REPAIR) + flags &= ~RT_REPAIR; #ifndef NS_PORT - nl_send_add_route_msg(rt->dest_addr, next, hops, lifetime, - flags, rt->ifindex); + nl_send_add_route_msg(rt->dest_addr, next, hops, lifetime, flags, + rt->ifindex); #endif - } else if (rt->next_hop.s_addr != 0 && - rt->next_hop.s_addr != next.s_addr) { + } else if (rt->next_hop.s_addr != 0 && rt->next_hop.s_addr != next.s_addr) { - DEBUG(LOG_INFO, 0, "rt->next_hop=%s, new_next_hop=%s", - ip_to_str(rt->next_hop), ip_to_str(next)); + DEBUG(LOG_INFO, 0, "rt->next_hop=%s, new_next_hop=%s", + ip_to_str(rt->next_hop), ip_to_str(next)); #ifndef NS_PORT - nl_send_add_route_msg(rt->dest_addr, next, hops, lifetime, - flags, rt->ifindex); + nl_send_add_route_msg(rt->dest_addr, next, hops, lifetime, flags, + rt->ifindex); #endif - } - - if (hops > 1 && rt->hcnt == 1) { - rt->last_hello_time.tv_sec = 0; - rt->last_hello_time.tv_usec = 0; - rt->hello_cnt = 0; - timer_remove(&rt->hello_timer); - /* Must also do a "link break" when updating a 1 hop - neighbor in case another routing entry use this as - next hop... */ - neighbor_link_break(rt); - } - - rt->flags = flags; - rt->dest_seqno = seqno; - rt->next_hop = next; - rt->hcnt = hops; + } + + if (hops > 1 && rt->hcnt == 1) { + rt->last_hello_time.tv_sec = 0; + rt->last_hello_time.tv_usec = 0; + rt->hello_cnt = 0; + timer_remove(&rt->hello_timer); + /* Must also do a "link break" when updating a 1 hop + neighbor in case another routing entry use this as + next hop... */ + neighbor_link_break(rt); + } + + rt->flags = flags; + rt->dest_seqno = seqno; + rt->next_hop = next; + rt->hcnt = hops; #ifdef CONFIG_GATEWAY - if (rt->flags & RT_GATEWAY) - rt_table_update_inet_rt(rt, lifetime); + if (rt->flags & RT_GATEWAY) + rt_table_update_inet_rt(rt, lifetime); #endif -//#ifdef NS_PORT - rt->rt_timer.handler = &NS_CLASS route_expire_timeout; + //#ifdef NS_PORT + rt->rt_timer.handler = &NS_CLASS route_expire_timeout; - if (!(rt->flags & RT_INET_DEST)) - rt_table_update_timeout(rt, lifetime); -//#endif + if (!(rt->flags & RT_INET_DEST)) + rt_table_update_timeout(rt, lifetime); + //#endif - /* Finally, mark as VALID */ - rt->state = state; + /* Finally, mark as VALID */ + rt->state = state; - /* In case there are buffered packets for this destination, we send - * them on the new route. */ - if (rt->state == VALID - && seek_list_remove(seek_list_find(rt->dest_addr))) { + /* In case there are buffered packets for this destination, we send + * them on the new route. */ + if (rt->state == VALID && seek_list_remove(seek_list_find(rt->dest_addr))) { #ifdef NS_PORT - if (rt->flags & RT_INET_DEST) - packet_queue_set_verdict(rt->dest_addr, PQ_ENC_SEND); - else - packet_queue_set_verdict(rt->dest_addr, PQ_SEND); + if (rt->flags & RT_INET_DEST) + packet_queue_set_verdict(rt->dest_addr, PQ_ENC_SEND); + else + packet_queue_set_verdict(rt->dest_addr, PQ_SEND); #endif - } - return rt; + } + return rt; } -NS_INLINE rt_table_t *NS_CLASS rt_table_update_timeout(rt_table_t * rt, - u_int32_t lifetime) +NS_INLINE rt_table_t *NS_CLASS rt_table_update_timeout(rt_table_t *rt, + u_int32_t lifetime) { - struct timeval new_timeout; + struct timeval new_timeout; - if (!rt) - return NULL; + if (!rt) + return NULL; - if (rt->state == VALID) { - /* Check if the current valid timeout is larger than the new - one - in that case keep the old one. */ - gettimeofday(&new_timeout, NULL); - timeval_add_msec(&new_timeout, lifetime); + if (rt->state == VALID) { + /* Check if the current valid timeout is larger than the new + one - in that case keep the old one. */ + gettimeofday(&new_timeout, NULL); + timeval_add_msec(&new_timeout, lifetime); - if (timeval_diff(&rt->rt_timer.timeout, &new_timeout) < 0) - timer_set_timeout(&rt->rt_timer, lifetime); - } else - timer_set_timeout(&rt->rt_timer, lifetime); + if (timeval_diff(&rt->rt_timer.timeout, &new_timeout) < 0) + timer_set_timeout(&rt->rt_timer, lifetime); + } else + timer_set_timeout(&rt->rt_timer, lifetime); - return rt; + return rt; } /* Update route timeouts in response to an incoming or outgoing data packet. */ -void NS_CLASS rt_table_update_route_timeouts(rt_table_t * fwd_rt, - rt_table_t * rev_rt) +void NS_CLASS rt_table_update_route_timeouts(rt_table_t *fwd_rt, + rt_table_t *rev_rt) { - rt_table_t *next_hop_rt = NULL; - - /* When forwarding a packet, we update the lifetime of the - destination's routing table entry, as well as the entry for the - next hop neighbor (if not the same). AODV draft 10, section - 6.2. */ - - if (fwd_rt && fwd_rt->state == VALID) { - - if (llfeedback || fwd_rt->flags & RT_INET_DEST || - fwd_rt->hcnt != 1 || fwd_rt->hello_timer.used) - rt_table_update_timeout(fwd_rt, ACTIVE_ROUTE_TIMEOUT); - - next_hop_rt = rt_table_find(fwd_rt->next_hop); - - if (next_hop_rt && next_hop_rt->state == VALID && - next_hop_rt->dest_addr.s_addr != fwd_rt->dest_addr.s_addr && - (llfeedback || fwd_rt->hello_timer.used)) - rt_table_update_timeout(next_hop_rt, - ACTIVE_ROUTE_TIMEOUT); - - } - /* Also update the reverse route and reverse next hop along the - path back, since routes between originators and the destination - are expected to be symmetric. */ - if (rev_rt && rev_rt->state == VALID) { - - if (llfeedback || rev_rt->hcnt != 1 || rev_rt->hello_timer.used) - rt_table_update_timeout(rev_rt, ACTIVE_ROUTE_TIMEOUT); - - next_hop_rt = rt_table_find(rev_rt->next_hop); - - if (next_hop_rt && next_hop_rt->state == VALID && rev_rt && - next_hop_rt->dest_addr.s_addr != rev_rt->dest_addr.s_addr && - (llfeedback || rev_rt->hello_timer.used)) - rt_table_update_timeout(next_hop_rt, - ACTIVE_ROUTE_TIMEOUT); - - /* Update HELLO timer of next hop neighbor if active */ -/* if (!llfeedback && next_hop_rt->hello_timer.used) { */ -/* struct timeval now; */ - -/* gettimeofday(&now, NULL); */ -/* hello_update_timeout(next_hop_rt, &now, */ -/* ALLOWED_HELLO_LOSS * HELLO_INTERVAL); */ -/* } */ - } + rt_table_t *next_hop_rt = NULL; + + /* When forwarding a packet, we update the lifetime of the + destination's routing table entry, as well as the entry for the + next hop neighbor (if not the same). AODV draft 10, section + 6.2. */ + + if (fwd_rt && fwd_rt->state == VALID) { + + if (llfeedback || fwd_rt->flags & RT_INET_DEST || fwd_rt->hcnt != 1 || + fwd_rt->hello_timer.used) + rt_table_update_timeout(fwd_rt, ACTIVE_ROUTE_TIMEOUT); + + next_hop_rt = rt_table_find(fwd_rt->next_hop); + + if (next_hop_rt && next_hop_rt->state == VALID && + next_hop_rt->dest_addr.s_addr != fwd_rt->dest_addr.s_addr && + (llfeedback || fwd_rt->hello_timer.used)) + rt_table_update_timeout(next_hop_rt, ACTIVE_ROUTE_TIMEOUT); + } + /* Also update the reverse route and reverse next hop along the + path back, since routes between originators and the destination + are expected to be symmetric. */ + if (rev_rt && rev_rt->state == VALID) { + + if (llfeedback || rev_rt->hcnt != 1 || rev_rt->hello_timer.used) + rt_table_update_timeout(rev_rt, ACTIVE_ROUTE_TIMEOUT); + + next_hop_rt = rt_table_find(rev_rt->next_hop); + + if (next_hop_rt && next_hop_rt->state == VALID && rev_rt && + next_hop_rt->dest_addr.s_addr != rev_rt->dest_addr.s_addr && + (llfeedback || rev_rt->hello_timer.used)) + rt_table_update_timeout(next_hop_rt, ACTIVE_ROUTE_TIMEOUT); + + /* Update HELLO timer of next hop neighbor if active */ + /* if (!llfeedback && next_hop_rt->hello_timer.used) { */ + /* struct timeval now; */ + + /* gettimeofday(&now, NULL); */ + /* hello_update_timeout(next_hop_rt, &now, */ + /* ALLOWED_HELLO_LOSS * HELLO_INTERVAL); + */ + /* } */ + } } rt_table_t *NS_CLASS rt_table_find(struct in_addr dest_addr) { - hash_value hash; - unsigned int index; - list_t *pos; - - if (rt_tbl.num_entries == 0) - return NULL; + hash_value hash; + unsigned int index; + list_t *pos; - /* Calculate index */ - index = hashing(&dest_addr, &hash); + if (rt_tbl.num_entries == 0) + return NULL; - /* Handle collisions: */ - list_foreach(pos, &rt_tbl.tbl[index]) { - rt_table_t *rt = (rt_table_t *) pos; + /* Calculate index */ + index = hashing(&dest_addr, &hash); - if (rt->hash != hash) - continue; + /* Handle collisions: */ + list_foreach(pos, &rt_tbl.tbl[index]) + { + rt_table_t *rt = (rt_table_t *)pos; - if (memcmp(&dest_addr, &rt->dest_addr, sizeof(struct in_addr)) - == 0) - return rt; + if (rt->hash != hash) + continue; - } - return NULL; + if (memcmp(&dest_addr, &rt->dest_addr, sizeof(struct in_addr)) == 0) + return rt; + } + return NULL; } rt_table_t *NS_CLASS rt_table_find_gateway() { - rt_table_t *gw = NULL; - int i; - - for (i = 0; i < RT_TABLESIZE; i++) { - list_t *pos; - list_foreach(pos, &rt_tbl.tbl[i]) { - rt_table_t *rt = (rt_table_t *) pos; - - if (rt->flags & RT_GATEWAY && rt->state == VALID) { - if (!gw || rt->hcnt < gw->hcnt) - gw = rt; - } - } - } - return gw; + rt_table_t *gw = NULL; + int i; + + for (i = 0; i < RT_TABLESIZE; i++) { + list_t *pos; + list_foreach(pos, &rt_tbl.tbl[i]) + { + rt_table_t *rt = (rt_table_t *)pos; + + if (rt->flags & RT_GATEWAY && rt->state == VALID) { + if (!gw || rt->hcnt < gw->hcnt) + gw = rt; + } + } + } + return gw; } #ifdef CONFIG_GATEWAY -int NS_CLASS rt_table_update_inet_rt(rt_table_t * gw, u_int32_t life) +int NS_CLASS rt_table_update_inet_rt(rt_table_t *gw, u_int32_t life) { - int n = 0; - int i; - - if (!gw) - return -1; - - for (i = 0; i < RT_TABLESIZE; i++) { - list_t *pos; - list_foreach(pos, &rt_tbl.tbl[i]) { - rt_table_t *rt = (rt_table_t *) pos; - - if (rt->flags & RT_INET_DEST && rt->state == VALID) { - rt_table_update(rt, gw->dest_addr, gw->hcnt, 0, - life, VALID, rt->flags); - n++; - } - } - } - return n; + int n = 0; + int i; + + if (!gw) + return -1; + + for (i = 0; i < RT_TABLESIZE; i++) { + list_t *pos; + list_foreach(pos, &rt_tbl.tbl[i]) + { + rt_table_t *rt = (rt_table_t *)pos; + + if (rt->flags & RT_INET_DEST && rt->state == VALID) { + rt_table_update(rt, gw->dest_addr, gw->hcnt, 0, life, VALID, + rt->flags); + n++; + } + } + } + return n; } -#endif /* CONFIG_GATEWAY_DISABLED */ +#endif /* CONFIG_GATEWAY_DISABLED */ /* Route expiry and Deletion. */ -int NS_CLASS rt_table_invalidate(rt_table_t * rt) +int NS_CLASS rt_table_invalidate(rt_table_t *rt) { - struct timeval now; + struct timeval now; - gettimeofday(&now, NULL); + gettimeofday(&now, NULL); - if (rt == NULL) - return -1; + if (rt == NULL) + return -1; - /* If the route is already invalidated, do nothing... */ - if (rt->state == INVALID) { - DEBUG(LOG_DEBUG, 0, "Route %s already invalidated!!!", - ip_to_str(rt->dest_addr)); - return -1; - } + /* If the route is already invalidated, do nothing... */ + if (rt->state == INVALID) { + DEBUG(LOG_DEBUG, 0, "Route %s already invalidated!!!", + ip_to_str(rt->dest_addr)); + return -1; + } - if (rt->hello_timer.used) { - DEBUG(LOG_DEBUG, 0, "last HELLO: %ld", - timeval_diff(&now, &rt->last_hello_time)); - } + if (rt->hello_timer.used) { + DEBUG(LOG_DEBUG, 0, "last HELLO: %ld", + timeval_diff(&now, &rt->last_hello_time)); + } - /* Remove any pending, but now obsolete timers. */ - timer_remove(&rt->rt_timer); - timer_remove(&rt->hello_timer); - timer_remove(&rt->ack_timer); + /* Remove any pending, but now obsolete timers. */ + timer_remove(&rt->rt_timer); + timer_remove(&rt->hello_timer); + timer_remove(&rt->ack_timer); - /* Mark the route as invalid */ - rt->state = INVALID; - rt_tbl.num_active--; + /* Mark the route as invalid */ + rt->state = INVALID; + rt_tbl.num_active--; - rt->hello_cnt = 0; + rt->hello_cnt = 0; - /* When the lifetime of a route entry expires, increase the sequence - number for that entry. */ - seqno_incr(rt->dest_seqno); + /* When the lifetime of a route entry expires, increase the sequence + number for that entry. */ + seqno_incr(rt->dest_seqno); - rt->last_hello_time.tv_sec = 0; - rt->last_hello_time.tv_usec = 0; + rt->last_hello_time.tv_sec = 0; + rt->last_hello_time.tv_usec = 0; #ifndef NS_PORT - nl_send_del_route_msg(rt->dest_addr, rt->next_hop, rt->hcnt); + nl_send_del_route_msg(rt->dest_addr, rt->next_hop, rt->hcnt); #endif - #ifdef CONFIG_GATEWAY - /* If this was a gateway, check if any Internet destinations were using - * it. In that case update them to use a backup gateway or invalide them - * too. */ - if (rt->flags & RT_GATEWAY) { - int i; - - rt_table_t *gw = rt_table_find_gateway(); - - for (i = 0; i < RT_TABLESIZE; i++) { - list_t *pos; - list_foreach(pos, &rt_tbl.tbl[i]) { - rt_table_t *rt2 = (rt_table_t *) pos; - - if (rt2->state == VALID - && (rt2->flags & RT_INET_DEST) - && (rt2->next_hop.s_addr == - rt->dest_addr.s_addr)) { - if (0) { - DEBUG(LOG_DEBUG, 0, - "Invalidated GW %s but found new GW %s for %s", - ip_to_str(rt->dest_addr), - ip_to_str(gw->dest_addr), - ip_to_str(rt2-> - dest_addr)); - rt_table_update(rt2, - gw->dest_addr, - gw->hcnt, 0, - timeval_diff - (&rt->rt_timer. - timeout, &now), - VALID, - rt2->flags); - } else { - rt_table_invalidate(rt2); - precursor_list_destroy(rt2); - } - } - } - } - } + /* If this was a gateway, check if any Internet destinations were using + * it. In that case update them to use a backup gateway or invalide them + * too. */ + if (rt->flags & RT_GATEWAY) { + int i; + + rt_table_t *gw = rt_table_find_gateway(); + + for (i = 0; i < RT_TABLESIZE; i++) { + list_t *pos; + list_foreach(pos, &rt_tbl.tbl[i]) + { + rt_table_t *rt2 = (rt_table_t *)pos; + + if (rt2->state == VALID && (rt2->flags & RT_INET_DEST) && + (rt2->next_hop.s_addr == rt->dest_addr.s_addr)) { + if (0) { + DEBUG(LOG_DEBUG, 0, + "Invalidated GW %s but found new GW %s for %s", + ip_to_str(rt->dest_addr), + ip_to_str(gw->dest_addr), + ip_to_str(rt2->dest_addr)); + rt_table_update( + rt2, gw->dest_addr, gw->hcnt, 0, + timeval_diff(&rt->rt_timer.timeout, &now), VALID, + rt2->flags); + } else { + rt_table_invalidate(rt2); + precursor_list_destroy(rt2); + } + } + } + } + } #endif - if (rt->flags & RT_REPAIR) { - /* Set a timeout for the repair */ + if (rt->flags & RT_REPAIR) { + /* Set a timeout for the repair */ - rt->rt_timer.handler = &NS_CLASS local_repair_timeout; - timer_set_timeout(&rt->rt_timer, ACTIVE_ROUTE_TIMEOUT); + rt->rt_timer.handler = &NS_CLASS local_repair_timeout; + timer_set_timeout(&rt->rt_timer, ACTIVE_ROUTE_TIMEOUT); - DEBUG(LOG_DEBUG, 0, "%s kept for repairs during %u msecs", - ip_to_str(rt->dest_addr), ACTIVE_ROUTE_TIMEOUT); - } else { + DEBUG(LOG_DEBUG, 0, "%s kept for repairs during %u msecs", + ip_to_str(rt->dest_addr), ACTIVE_ROUTE_TIMEOUT); + } else { - /* Schedule a deletion timer */ - rt->rt_timer.handler = &NS_CLASS route_delete_timeout; - timer_set_timeout(&rt->rt_timer, DELETE_PERIOD); + /* Schedule a deletion timer */ + rt->rt_timer.handler = &NS_CLASS route_delete_timeout; + timer_set_timeout(&rt->rt_timer, DELETE_PERIOD); - DEBUG(LOG_DEBUG, 0, "%s removed in %u msecs", - ip_to_str(rt->dest_addr), DELETE_PERIOD); - } + DEBUG(LOG_DEBUG, 0, "%s removed in %u msecs", ip_to_str(rt->dest_addr), + DELETE_PERIOD); + } - return 0; + return 0; } -void NS_CLASS rt_table_delete(rt_table_t * rt) +void NS_CLASS rt_table_delete(rt_table_t *rt) { - if (!rt) { - DEBUG(LOG_ERR, 0, "No route entry to delete"); - return; - } + if (!rt) { + DEBUG(LOG_ERR, 0, "No route entry to delete"); + return; + } - list_detach(&rt->l); + list_detach(&rt->l); - precursor_list_destroy(rt); + precursor_list_destroy(rt); - if (rt->state == VALID) { + if (rt->state == VALID) { #ifndef NS_PORT - nl_send_del_route_msg(rt->dest_addr, rt->next_hop, rt->hcnt); + nl_send_del_route_msg(rt->dest_addr, rt->next_hop, rt->hcnt); #endif - rt_tbl.num_active--; - } - /* Make sure timers are removed... */ - timer_remove(&rt->rt_timer); - timer_remove(&rt->hello_timer); - timer_remove(&rt->ack_timer); + rt_tbl.num_active--; + } + /* Make sure timers are removed... */ + timer_remove(&rt->rt_timer); + timer_remove(&rt->hello_timer); + timer_remove(&rt->ack_timer); - rt_tbl.num_entries--; + rt_tbl.num_entries--; - free(rt); - return; + free(rt); + return; } /****************************************************************/ /* Add an neighbor to the active neighbor list. */ -void NS_CLASS precursor_add(rt_table_t * rt, struct in_addr addr) +void NS_CLASS precursor_add(rt_table_t *rt, struct in_addr addr) { - precursor_t *pr; - list_t *pos; + precursor_t *pr; + list_t *pos; - /* Sanity check */ - if (!rt) - return; + /* Sanity check */ + if (!rt) + return; - /* Check if the node is already in the precursors list. */ - list_foreach(pos, &rt->precursors) { - pr = (precursor_t *) pos; + /* Check if the node is already in the precursors list. */ + list_foreach(pos, &rt->precursors) + { + pr = (precursor_t *)pos; - if (pr->neighbor.s_addr == addr.s_addr) - return; - } + if (pr->neighbor.s_addr == addr.s_addr) + return; + } - if ((pr = (precursor_t *) malloc(sizeof(precursor_t))) == NULL) { - perror("Could not allocate memory for precursor node!!\n"); - exit(-1); - } + if ((pr = (precursor_t *)malloc(sizeof(precursor_t))) == NULL) { + perror("Could not allocate memory for precursor node!!\n"); + exit(-1); + } - DEBUG(LOG_INFO, 0, "Adding precursor %s to rte %s", - ip_to_str(addr), ip_to_str(rt->dest_addr)); + DEBUG(LOG_INFO, 0, "Adding precursor %s to rte %s", ip_to_str(addr), + ip_to_str(rt->dest_addr)); - pr->neighbor.s_addr = addr.s_addr; + pr->neighbor.s_addr = addr.s_addr; - /* Insert in precursors list */ + /* Insert in precursors list */ - list_add(&rt->precursors, &pr->l); - rt->nprec++; + list_add(&rt->precursors, &pr->l); + rt->nprec++; - return; + return; } /****************************************************************/ /* Remove a neighbor from the active neighbor list. */ -void NS_CLASS precursor_remove(rt_table_t * rt, struct in_addr addr) +void NS_CLASS precursor_remove(rt_table_t *rt, struct in_addr addr) { - list_t *pos; - - /* Sanity check */ - if (!rt) - return; - - list_foreach(pos, &rt->precursors) { - precursor_t *pr = (precursor_t *) pos; - if (pr->neighbor.s_addr == addr.s_addr) { - DEBUG(LOG_INFO, 0, "Removing precursor %s from rte %s", - ip_to_str(addr), ip_to_str(rt->dest_addr)); - - list_detach(pos); - rt->nprec--; - free(pr); - return; - } - } + list_t *pos; + + /* Sanity check */ + if (!rt) + return; + + list_foreach(pos, &rt->precursors) + { + precursor_t *pr = (precursor_t *)pos; + if (pr->neighbor.s_addr == addr.s_addr) { + DEBUG(LOG_INFO, 0, "Removing precursor %s from rte %s", + ip_to_str(addr), ip_to_str(rt->dest_addr)); + + list_detach(pos); + rt->nprec--; + free(pr); + return; + } + } } /****************************************************************/ /* Delete all entries from the active neighbor list. */ -void precursor_list_destroy(rt_table_t * rt) +void precursor_list_destroy(rt_table_t *rt) { - list_t *pos, *tmp; - - /* Sanity check */ - if (!rt) - return; - - list_foreach_safe(pos, tmp, &rt->precursors) { - precursor_t *pr = (precursor_t *) pos; - list_detach(pos); - rt->nprec--; - free(pr); - } + list_t *pos, *tmp; + + /* Sanity check */ + if (!rt) + return; + + list_foreach_safe(pos, tmp, &rt->precursors) + { + precursor_t *pr = (precursor_t *)pos; + list_detach(pos); + rt->nprec--; + free(pr); + } } diff --git a/routing_table.h b/routing_table.h index 5f84764..4e7c27f 100644 --- a/routing_table.h +++ b/routing_table.h @@ -16,8 +16,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, - * + * Authors: Erik Nordström, + * * *****************************************************************************/ #ifndef _ROUTING_TABLE_H @@ -39,43 +39,42 @@ typedef struct precursor { #define seqno_incr(s) ((s == 0) ? 0 : ((s == 0xFFFFFFFF) ? s = 1 : s++)) -typedef u_int32_t hash_value; /* A hash value */ +typedef u_int32_t hash_value; /* A hash value */ /* Route table entries */ struct rt_table { list_t l; - struct in_addr dest_addr; /* IP address of the destination */ + struct in_addr dest_addr; /* IP address of the destination */ u_int32_t dest_seqno; - unsigned int ifindex; /* Network interface index... */ - struct in_addr next_hop; /* IP address of the next hop to the dest */ - u_int8_t hcnt; /* Distance (in hops) to the destination */ - u_int16_t flags; /* Routing flags */ - u_int8_t state; /* The state of this entry */ - struct timer rt_timer; /* The timer associated with this entry */ - struct timer ack_timer; /* RREP_ack timer for this destination */ + unsigned int ifindex; /* Network interface index... */ + struct in_addr next_hop; /* IP address of the next hop to the dest */ + u_int8_t hcnt; /* Distance (in hops) to the destination */ + u_int16_t flags; /* Routing flags */ + u_int8_t state; /* The state of this entry */ + struct timer rt_timer; /* The timer associated with this entry */ + struct timer ack_timer; /* RREP_ack timer for this destination */ struct timer hello_timer; struct timeval last_hello_time; u_int8_t hello_cnt; hash_value hash; - int nprec; /* Number of precursors */ - list_t precursors; /* List of neighbors using the route */ + int nprec; /* Number of precursors */ + list_t precursors; /* List of neighbors using the route */ }; - /* Route entry flags */ -#define RT_UNIDIR 0x1 -#define RT_REPAIR 0x2 -#define RT_INV_SEQNO 0x4 -#define RT_INET_DEST 0x8 /* Mark for Internet destinations (to be relayed - * through a Internet gateway. */ -#define RT_GATEWAY 0x10 +#define RT_UNIDIR 0x1 +#define RT_REPAIR 0x2 +#define RT_INV_SEQNO 0x4 +#define RT_INET_DEST \ + 0x8 /* Mark for Internet destinations (to be relayed \ + * through a Internet gateway. */ +#define RT_GATEWAY 0x10 /* Route entry states */ -#define INVALID 0 -#define VALID 1 - +#define INVALID 0 +#define VALID 1 -#define RT_TABLESIZE 64 /* Must be a power of 2 */ +#define RT_TABLESIZE 64 /* Must be a power of 2 */ #define RT_TABLEMASK (RT_TABLESIZE - 1) struct routing_table { @@ -84,8 +83,8 @@ struct routing_table { list_t tbl[RT_TABLESIZE]; }; -void precursor_list_destroy(rt_table_t * rt); -#endif /* NS_NO_GLOBALS */ +void precursor_list_destroy(rt_table_t *rt); +#endif /* NS_NO_GLOBALS */ #ifndef NS_NO_DECLARATIONS @@ -94,23 +93,23 @@ struct routing_table rt_tbl; void rt_table_init(); void rt_table_destroy(); rt_table_t *rt_table_insert(struct in_addr dest, struct in_addr next, - u_int8_t hops, u_int32_t seqno, u_int32_t life, - u_int8_t state, u_int16_t flags, - unsigned int ifindex); -rt_table_t *rt_table_update(rt_table_t * rt, struct in_addr next, u_int8_t hops, - u_int32_t seqno, u_int32_t lifetime, u_int8_t state, - u_int16_t flags); -NS_INLINE rt_table_t *rt_table_update_timeout(rt_table_t * rt, - u_int32_t lifetime); -void rt_table_update_route_timeouts(rt_table_t * fwd_rt, rt_table_t * rev_rt); + u_int8_t hops, u_int32_t seqno, u_int32_t life, + u_int8_t state, u_int16_t flags, + unsigned int ifindex); +rt_table_t *rt_table_update(rt_table_t *rt, struct in_addr next, u_int8_t hops, + u_int32_t seqno, u_int32_t lifetime, u_int8_t state, + u_int16_t flags); +NS_INLINE rt_table_t *rt_table_update_timeout(rt_table_t *rt, + u_int32_t lifetime); +void rt_table_update_route_timeouts(rt_table_t *fwd_rt, rt_table_t *rev_rt); rt_table_t *rt_table_find(struct in_addr dest); rt_table_t *rt_table_find_gateway(); -int rt_table_update_inet_rt(rt_table_t * gw, u_int32_t life); -int rt_table_invalidate(rt_table_t * rt); -void rt_table_delete(rt_table_t * rt); -void precursor_add(rt_table_t * rt, struct in_addr addr); -void precursor_remove(rt_table_t * rt, struct in_addr addr); +int rt_table_update_inet_rt(rt_table_t *gw, u_int32_t life); +int rt_table_invalidate(rt_table_t *rt); +void rt_table_delete(rt_table_t *rt); +void precursor_add(rt_table_t *rt, struct in_addr addr); +void precursor_remove(rt_table_t *rt, struct in_addr addr); -#endif /* NS_NO_DECLARATIONS */ +#endif /* NS_NO_DECLARATIONS */ -#endif /* ROUTING_TABLE_H */ +#endif /* ROUTING_TABLE_H */ diff --git a/seek_list.c b/seek_list.c index 12e2196..4135cb5 100644 --- a/seek_list.c +++ b/seek_list.c @@ -16,23 +16,23 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, - * + * Authors: Erik Nordström, + * * *****************************************************************************/ #include #ifdef NS_PORT -#include "ns-2/aodv-uu.h" #include "list.h" +#include "ns-2/aodv-uu.h" #else -#include "seek_list.h" -#include "timer_queue.h" #include "aodv_timeout.h" -#include "defs.h" -#include "params.h" #include "debug.h" +#include "defs.h" #include "list.h" +#include "params.h" +#include "seek_list.h" +#include "timer_queue.h" #endif #ifndef NS_PORT @@ -44,18 +44,17 @@ static LIST(seekhead); #ifdef SEEK_LIST_DEBUG void seek_list_print(); #endif -#endif /* NS_PORT */ +#endif /* NS_PORT */ seek_list_t *NS_CLASS seek_list_insert(struct in_addr dest_addr, - u_int32_t dest_seqno, - int ttl, u_int8_t flags, - struct ip_data *ipd) + u_int32_t dest_seqno, int ttl, + u_int8_t flags, struct ip_data *ipd) { seek_list_t *entry; - if ((entry = (seek_list_t *) malloc(sizeof(seek_list_t))) == NULL) { - fprintf(stderr, "Failed malloc\n"); - exit(-1); + if ((entry = (seek_list_t *)malloc(sizeof(seek_list_t))) == NULL) { + fprintf(stderr, "Failed malloc\n"); + exit(-1); } entry->dest_addr = dest_addr; @@ -74,10 +73,10 @@ seek_list_t *NS_CLASS seek_list_insert(struct in_addr dest_addr, return entry; } -int NS_CLASS seek_list_remove(seek_list_t * entry) +int NS_CLASS seek_list_remove(seek_list_t *entry) { if (!entry) - return 0; + return 0; list_detach(&entry->l); @@ -85,7 +84,7 @@ int NS_CLASS seek_list_remove(seek_list_t * entry) timer_remove(&entry->seek_timer); if (entry->ipd) - free(entry->ipd); + free(entry->ipd); free(entry); return 1; @@ -95,11 +94,12 @@ seek_list_t *NS_CLASS seek_list_find(struct in_addr dest_addr) { list_t *pos; - list_foreach(pos, &seekhead) { - seek_list_t *entry = (seek_list_t *) pos; + list_foreach(pos, &seekhead) + { + seek_list_t *entry = (seek_list_t *)pos; - if (entry->dest_addr.s_addr == dest_addr.s_addr) - return entry; + if (entry->dest_addr.s_addr == dest_addr.s_addr) + return entry; } return NULL; } @@ -109,10 +109,11 @@ void NS_CLASS seek_list_print() { list_t *pos; - list_foreach(pos, &seekhead) { - seek_list_t *entry = (seek_list_t *) pos; - printf("%s %u %d %d\n", ip_to_str(entry->dest_addr), - entry->dest_seqno, entry->reqs, entry->ttl); + list_foreach(pos, &seekhead) + { + seek_list_t *entry = (seek_list_t *)pos; + printf("%s %u %d %d\n", ip_to_str(entry->dest_addr), entry->dest_seqno, + entry->reqs, entry->ttl); } } #endif diff --git a/seek_list.h b/seek_list.h index ee053dc..f258162 100644 --- a/seek_list.h +++ b/seek_list.h @@ -16,8 +16,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, - * + * Authors: Erik Nordström, + * * *****************************************************************************/ #ifndef _SEEK_LIST_H @@ -25,10 +25,10 @@ #ifndef NS_NO_GLOBALS #include "defs.h" -#include "timer_queue.h" #include "list.h" +#include "timer_queue.h" -#define IP_DATA_MAX_LEN 60 + 8 /* Max IP header + 64 bits of data */ +#define IP_DATA_MAX_LEN 60 + 8 /* Max IP header + 64 bits of data */ struct ip_data { char data[IP_DATA_MAX_LEN]; @@ -41,25 +41,25 @@ typedef struct seek_list { struct in_addr dest_addr; u_int32_t dest_seqno; struct ip_data *ipd; - u_int8_t flags; /* The flags we are using for resending the RREQ */ + u_int8_t flags; /* The flags we are using for resending the RREQ */ int reqs; int ttl; struct timer seek_timer; } seek_list_t; -#endif /* NS_NO_GLOBALS */ +#endif /* NS_NO_GLOBALS */ #ifndef NS_NO_DECLARATIONS seek_list_t *seek_list_insert(struct in_addr dest_addr, u_int32_t dest_seqno, - int ttl, u_int8_t flags, struct ip_data *ipd); -int seek_list_remove(seek_list_t * entry); + int ttl, u_int8_t flags, struct ip_data *ipd); +int seek_list_remove(seek_list_t *entry); seek_list_t *seek_list_find(struct in_addr dest_addr); #ifdef NS_PORT #ifdef SEEK_LIST_DEBUG void seek_list_print(); #endif -#endif /* NS_PORT */ +#endif /* NS_PORT */ -#endif /* NS_NO_DECLARATIONS */ +#endif /* NS_NO_DECLARATIONS */ -#endif /* SEEK_LIST_H */ +#endif /* SEEK_LIST_H */ diff --git a/timer_queue.c b/timer_queue.c index 6055cb3..578c4e1 100644 --- a/timer_queue.c +++ b/timer_queue.c @@ -16,34 +16,34 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, + * Authors: Erik Nordström, * *****************************************************************************/ -#include #include +#include #include #ifdef NS_PORT #include "ns-2/aodv-uu.h" #else -#include "timer_queue.h" -#include "defs.h" #include "debug.h" +#include "defs.h" #include "list.h" +#include "timer_queue.h" static LIST(TQ); /* #define DEBUG_TIMER_QUEUE */ #ifdef DEBUG_TIMER_QUEUE -static void printTQ(list_t * l); +static void printTQ(list_t *l); #endif -#endif /* NS_PORT */ +#endif /* NS_PORT */ int NS_CLASS timer_init(struct timer *t, timeout_func_t f, void *data) { if (!t) - return -1; + return -1; INIT_LIST_ELM(&t->l); t->handler = f; @@ -65,32 +65,33 @@ void NS_CLASS timer_timeout(struct timeval *now) printf("\n######## timer_timeout: called!!\n"); #endif /* Remove expired timers from TQ and add them to expTQ */ - list_foreach_safe(pos, tmp, &TQ) { - struct timer *t = (struct timer *) pos; + list_foreach_safe(pos, tmp, &TQ) + { + struct timer *t = (struct timer *)pos; - if (timeval_diff(&t->timeout, now) > 0) - break; + if (timeval_diff(&t->timeout, now) > 0) + break; - list_detach(&t->l); - list_add_tail(&expTQ, &t->l); + list_detach(&t->l); + list_add_tail(&expTQ, &t->l); } /* Execute expired timers in expTQ safely by removing them at the head */ while (!list_empty(&expTQ)) { - struct timer *t = (struct timer *) list_first(&expTQ); - list_detach(&t->l); - t->used = 0; + struct timer *t = (struct timer *)list_first(&expTQ); + list_detach(&t->l); + t->used = 0; #ifdef DEBUG_TIMER_QUEUE - printf("removing timer %lu %d\n", pos); + printf("removing timer %lu %d\n", pos); #endif - /* Execute handler function for expired timer... */ - if (t->handler) { + /* Execute handler function for expired timer... */ + if (t->handler) { #ifdef NS_PORT - (*this.*t->handler) (t->data); + (*this.*t->handler)(t->data); #else - t->handler(t->data); + t->handler(t->data); #endif - } + } } } @@ -100,17 +101,17 @@ NS_STATIC void NS_CLASS timer_add(struct timer *t) /* Sanity checks: */ if (!t) { - perror("NULL timer!!!\n"); - exit(-1); + perror("NULL timer!!!\n"); + exit(-1); } if (!t->handler) { - perror("NULL handler!!!\n"); - exit(-1); + perror("NULL handler!!!\n"); + exit(-1); } /* Make sure we remove unexpired timers before adding a new timeout... */ if (t->used) - timer_remove(t); + timer_remove(t); t->used = 1; @@ -120,16 +121,17 @@ NS_STATIC void NS_CLASS timer_add(struct timer *t) /* Base case when queue is empty: */ if (list_empty(&TQ)) { - list_add(&TQ, &t->l); + list_add(&TQ, &t->l); } else { - list_foreach(pos, &TQ) { - struct timer *curr = (struct timer *) pos; - if (timeval_diff(&t->timeout, &curr->timeout) < 0) { - break; - } - } - list_add(pos->prev, &t->l); + list_foreach(pos, &TQ) + { + struct timer *curr = (struct timer *)pos; + if (timeval_diff(&t->timeout, &curr->timeout) < 0) { + break; + } + } + list_add(pos->prev, &t->l); } #ifdef DEBUG_TIMER_QUEUE @@ -143,45 +145,42 @@ int NS_CLASS timer_remove(struct timer *t) int res = 1; if (!t) - return -1; - + return -1; if (list_unattached(&t->l)) - res = 0; + res = 0; else - list_detach(&t->l); + list_detach(&t->l); t->used = 0; return res; } - int NS_CLASS timer_timeout_now(struct timer *t) { if (timer_remove(t)) { #ifdef NS_PORT - (*this.*t->handler) (t->data); + (*this.*t->handler)(t->data); #else - t->handler(t->data); + t->handler(t->data); #endif - return 1; + return 1; } return -1; } - void NS_CLASS timer_set_timeout(struct timer *t, long msec) { if (t->used) { - timer_remove(t); + timer_remove(t); } gettimeofday(&t->timeout, NULL); if (msec < 0) - DEBUG(LOG_WARNING, 0, "Negative timeout!!!"); + DEBUG(LOG_WARNING, 0, "Negative timeout!!!"); t->timeout.tv_usec += msec * 1000; t->timeout.tv_sec += t->timeout.tv_usec / 1000000; @@ -195,7 +194,7 @@ long timer_left(struct timer *t) struct timeval now; if (!t) - return -1; + return -1; gettimeofday(&now, NULL); @@ -212,30 +211,29 @@ struct timeval *NS_CLASS timer_age_queue() fflush(stdout); if (list_empty(&TQ)) - return NULL; + return NULL; timer_timeout(&now); /* Check emptyness again since the list might have been updated by a * timeout */ if (list_empty(&TQ)) - return NULL; + return NULL; - t = (struct timer *) TQ.next; + t = (struct timer *)TQ.next; remaining.tv_usec = (t->timeout.tv_usec - now.tv_usec); remaining.tv_sec = (t->timeout.tv_sec - now.tv_sec); if (remaining.tv_usec < 0) { - remaining.tv_usec += 1000000; - remaining.tv_sec -= 1; + remaining.tv_usec += 1000000; + remaining.tv_sec -= 1; } return (&remaining); } - #ifdef DEBUG_TIMER_QUEUE -void NS_CLASS printTQ(list_t * l) +void NS_CLASS printTQ(list_t *l) { struct timeval now; int n = 0; @@ -244,13 +242,14 @@ void NS_CLASS printTQ(list_t * l) gettimeofday(&now, NULL); fprintf(stderr, "================\n"); - fprintf(stderr, "%-12s %-4s %lu\n", "left", "n", (unsigned long) l); - - list_foreach(pos, l) { - struct timer *t = (struct timer *) pos; - fprintf(stderr, "%-12ld %-4d %lu\n", timeval_diff(&t->timeout, &now), n, - (unsigned long) pos); - n++; + fprintf(stderr, "%-12s %-4s %lu\n", "left", "n", (unsigned long)l); + + list_foreach(pos, l) + { + struct timer *t = (struct timer *)pos; + fprintf(stderr, "%-12ld %-4d %lu\n", timeval_diff(&t->timeout, &now), n, + (unsigned long)pos); + n++; } } #endif diff --git a/timer_queue.h b/timer_queue.h index 6f4c53b..6c555a8 100644 --- a/timer_queue.h +++ b/timer_queue.h @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * Authors: Erik Nordström, + * Authors: Erik Nordström, * *****************************************************************************/ #ifndef _TIMER_QUEUE_H @@ -29,9 +29,9 @@ #include "list.h" #ifdef NS_PORT -typedef void (AODVUU::*timeout_func_t) (void *); +typedef void (AODVUU::*timeout_func_t)(void *); #else -typedef void (*timeout_func_t) (void *); +typedef void (*timeout_func_t)(void *); #endif struct timer { @@ -44,24 +44,24 @@ struct timer { static inline long timeval_diff(struct timeval *t1, struct timeval *t2) { - long long res; /* We need this to avoid overflows while calculating... */ + long long res; /* We need this to avoid overflows while calculating... */ if (!t1 || !t2) - return -1; + return -1; else { - res = t1->tv_sec; - res = ((res - t2->tv_sec) * 1000000 + t1->tv_usec - t2->tv_usec) / 1000; - return (long) res; + res = t1->tv_sec; + res = ((res - t2->tv_sec) * 1000000 + t1->tv_usec - t2->tv_usec) / 1000; + return (long)res; } } static inline int timeval_add_msec(struct timeval *t, unsigned long msec) { - unsigned long long add; /* Protect against overflows */ + unsigned long long add; /* Protect against overflows */ if (!t) - return -1; + return -1; add = t->tv_usec + (msec * 1000); t->tv_sec += add / 1000000; @@ -69,7 +69,7 @@ static inline int timeval_add_msec(struct timeval *t, unsigned long msec) return 0; } -#endif /* NS_NO_GLOBALS */ +#endif /* NS_NO_GLOBALS */ #ifndef NS_NO_DECLARATIONS void timer_queue_init(); @@ -86,10 +86,10 @@ void timer_timeout(struct timeval *now); #ifdef DEBUG_TIMER_QUEUE void NS_CLASS printTQ(); -#endif /* DEBUG_TIMER_QUEUE */ +#endif /* DEBUG_TIMER_QUEUE */ -#endif /* NS_PORT */ +#endif /* NS_PORT */ -#endif /* NS_NO_DECLARATIONS */ +#endif /* NS_NO_DECLARATIONS */ -#endif /* TIMER_QUEUE_H */ +#endif /* TIMER_QUEUE_H */ From c622b6343bc3b0969b0811a2d5ce8c701f741722 Mon Sep 17 00:00:00 2001 From: Moritz Warning Date: Sat, 6 Jun 2020 21:52:20 +0200 Subject: [PATCH 2/2] add missing include Signed-off-by: Moritz Warning --- debug.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/debug.h b/debug.h index f662d65..61262de 100644 --- a/debug.h +++ b/debug.h @@ -26,6 +26,8 @@ extern int debug; #endif +#include "defs.h" + #ifndef NS_NO_DECLARATIONS void log_init(); void log_cleanup();