NETWORK ATTACKS FRAMEWORK  1.0.0
A NETwork Attacks framework. Making network attacks impact evaluation easier!
NA_aodv_socket.h File Reference
#include "NA_defs_aodv.h"
#include "NA_aodv_rerr.h"
#include "NA_params.h"
#include <netinet/ip.h>

Go to the source code of this file.

Defines

#define IPHDR_SIZE   sizeof(struct iphdr)
#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
#define CMSG_NXTHDR_FIX(mhdr, cmsg)   cmsg_nxthdr_fix((mhdr), (cmsg))

Functions

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, double delay=-1)
AODV_msgaodv_socket_new_msg ()
AODV_msgaodv_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)
struct cmsghdr * cmsg_nxthdr_fix (struct msghdr *__msg, struct cmsghdr *__cmsg)

Variables

struct timeval rreq_ratel [RREQ_RATELIMIT-1]
struct timeval rerr_ratel [RERR_RATELIMIT-1]
int num_rreq
int num_rerr

Define Documentation

Definition at line 43 of file NA_aodv_socket.h.

#define CMSG_NXTHDR_FIX (   mhdr,
  cmsg 
)    cmsg_nxthdr_fix((mhdr), (cmsg))

Definition at line 65 of file NA_aodv_socket.h.

#define IPHDR_SIZE   sizeof(struct iphdr)

Definition at line 37 of file NA_aodv_socket.h.

Definition at line 44 of file NA_aodv_socket.h.

Definition at line 45 of file NA_aodv_socket.h.


Function Documentation

void aodv_socket_cleanup ( void  )

Definition at line 741 of file NA_aodv_socket.cc.

{
#ifndef NS_PORT
    int i;

    for (i = 0; i < MAX_NR_INTERFACES; i++)
    {
        if (!DEV_NR(i).enabled)
            continue;
        close(DEV_NR(i).sock);
    }
#endif              /* NS_PORT */
}
void aodv_socket_init ( )

Definition at line 87 of file NA_aodv_socket.cc.

{
#ifndef NS_PORT
    struct sockaddr_in aodv_addr;
    struct ifreq ifr;
    int i, retval = 0;
    int on = 1;
    int tos = IPTOS_LOWDELAY;
    int bufsize = SO_RECVBUF_SIZE;
    socklen_t optlen = sizeof(bufsize);

    /* Create a UDP socket */

    if (this_host.nif == 0)
    {
        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);
        }
#ifdef CONFIG_GATEWAY
        /* Data packet send socket */
        DEV_NR(i).psock = socket(PF_INET, SOCK_RAW, IPPROTO_RAW);

        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);
        }
#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);
        }
#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);
        }
    }
#endif              /* NS_PORT */

    num_rreq = 0;
    num_rerr = 0;
}
void aodv_socket_process_packet ( AODV_msg aodv_msg,
int  len,
struct in_addr  src,
struct in_addr  dst,
int  ttl,
unsigned int  ifindex 
)

Definition at line 236 of file NA_aodv_socket.cc.

{
    /* If this was a HELLO message... Process as HELLO. */
#ifndef OMNETPP
    if ((aodv_msg->type == AODV_RREP && ttl == 1 &&
            dst.s_addr == AODV_BROADCAST))
    {
        hello_process((RREP *) aodv_msg, len, ifindex);
        return;
    }
#else
    if ((aodv_msg->type == AODV_RREP && ttl == 0 && // ttl is decremented for ip layer before send to aodv
            dst.s_addr == ManetAddress(IPv4Address(AODV_BROADCAST))))
    {
        hello_process((RREP *) aodv_msg, len, ifindex);
        return;
    }
#endif
    /* Make sure we add/update neighbors */
    neighbor_add(aodv_msg, src, ifindex);

    /* Check what type of msg we received and call the corresponding
       function to handle the msg... */
    switch (aodv_msg->type)
    {

    case AODV_RREQ:
        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;
    case AODV_RERR:
        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;
    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));
        break;
    }
}
AODV_msg* aodv_socket_queue_msg ( AODV_msg aodv_msg,
int  size 
)
void aodv_socket_send ( AODV_msg aodv_msg,
struct in_addr  dst,
int  len,
u_int8_t  ttl,
struct dev_info dev,
double  delay = -1 
)

Definition at line 405 of file NA_aodv_socket.cc.

{

    struct timeval now;
    int retval = 0;
    /* Rate limit stuff: */

#ifdef OMNETPP
    if (ttl<=0)
    {
        delete aodv_msg;
        return;
    }
#endif

#ifndef NS_PORT

    struct sockaddr_in dst_addr;

    if (wait_on_reboot && aodv_msg->type == AODV_RREP)
        return;

    memset(&dst_addr, 0, sizeof(dst_addr));
    dst_addr.sin_family = AF_INET;
    dst_addr.sin_addr = dst;
    dst_addr.sin_port = htons(AODV_PORT);

    /* Set ttl */
    if (setsockopt(dev->sock, SOL_IP, IP_TTL, &ttl, sizeof(ttl)) < 0)
    {
        alog(LOG_WARNING, 0, __FUNCTION__, "ERROR setting ttl!");
        return;
    }
#else

    /*
       NS_PORT: Sending of AODV_msg messages to other AODV-UU routing agents
       by encapsulating them in a Packet.

       Note: This method is _only_ for sending AODV packets to other routing
       agents, _not_ for forwarding "regular" IP packets!
     */

    /* If we are in waiting phase after reboot, don't send any RREPs */
    if (wait_on_reboot && aodv_msg->type == AODV_RREP)
    {
#ifdef OMNETPP
        delete aodv_msg;
#endif
        return;
    }
#ifndef OMNETPP
    /*
       NS_PORT: Don't allocate packet until now. Otherwise packet uid
       (unique ID) space is unnecessarily exhausted at the beginning of
       the simulation, resulting in uid:s starting at values greater than 0.
     */
    Packet *p = allocpkt();
    struct hdr_cmn *ch = HDR_CMN(p);
    struct hdr_ip *ih = HDR_IP(p);
    hdr_aodvuu *ah = HDR_AODVUU(p);

    // Clear AODVUU part of packet
    memset(ah, '\0', ah->size());

    // Copy message contents into packet
    memcpy(ah, aodv_msg, len);

    // Set common header fields
    ch->ptype() = PT_AODVUU;
    ch->direction() = hdr_cmn::DOWN;
    ch->size() = IP_HDR_LEN + len;
    ch->iface() = -2;
    ch->getBitErrorRate() = 0;
    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->ttl() = ttl;

    // Note: Port number for routing agents, not AODV port number!
    ih->sport() = RT_PORT;
    ih->dport() = RT_PORT;

    // Fake success
    retval = len;
#endif /*omnet++ */
#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
       if the time since last transmit of that type of packet is less
       than the allowed RATE LIMIT time... */

    if (ratelimit)
    {

        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]));
#ifdef OMNETPP
                    delete aodv_msg;
#else
#ifdef NS_PORT
                    Packet::free(p);
#endif
#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]));
#ifdef OMNETPP
                    delete aodv_msg;
#else
#ifdef NS_PORT
                    Packet::free(p);
#endif
#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;
        }
    }

    /* If we broadcast this message we update the time of last broadcast
       to prevent unnecessary broadcasts of HELLO msg's */
    if (dst.s_addr == ManetAddress(IPv4Address(AODV_BROADCAST)))
    {
        gettimeofday(&this_host.bcast_time, NULL);

#ifdef NS_PORT
#ifndef OMNETPP
        ch->addr_type() = NS_AF_NONE;
        sendPacket(p, dst, 0.0);
#else
//       IPv4Address   desAddIp4(dst.s_addr);
//       IPvXAddress destAdd(desAddIp4);
// In the floading proccess the random delay prevent collision for the synchronization between the nodes.
        ManetAddress destAdd;
        aodv_msg->prevFix=this->isStaticNode();

        if (this->isStaticNode())
        {
            if (dynamic_cast<RREP*>(aodv_msg))
            {
                dynamic_cast<RREP*>(aodv_msg)->cost += costStatic;
            }
            else if (dynamic_cast<RREQ*> (aodv_msg))
            {
                dynamic_cast<RREQ*>(aodv_msg)->cost += costStatic;
            }
        }
        else
        {
            if (dynamic_cast<RREP*>(aodv_msg))
            {
                dynamic_cast<RREP*>(aodv_msg)->cost += costMobile;
            }
            else if (dynamic_cast<RREQ*>(aodv_msg))
            {
                dynamic_cast<RREQ*>(aodv_msg)->cost += costMobile;
            }
        }



        if (dst.s_addr == ManetAddress(IPv4Address(AODV_BROADCAST)))
        {
            destAdd = ManetAddress(IPv4Address::ALLONES_ADDRESS);
        }
        else
        {
            destAdd = dst.s_addr;
        }
        if (delay>0)
        {
            if (useIndex)
                sendToIp(aodv_msg, 654, destAdd, 654, ttl, delay, dev->ifindex);
            else
                sendToIp(aodv_msg, 654, destAdd, 654, ttl, delay, dev->ipaddr.s_addr);
        }
        else
        {
            if (useIndex)
                sendToIp(aodv_msg, 654, destAdd, 654, ttl, par ("broadcastDelay").doubleValue(), dev->ifindex);
            else
                sendToIp(aodv_msg, 654, destAdd, 654, ttl, par ("broadcastDelay").doubleValue(), dev->ipaddr.s_addr);
        }
        totalSend++;
//       sendToIp(aodv_msg, 654, destAdd, 654,ttl);
#endif /*omnet++ */
#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 bc %s",
                 ip_to_str(dst));
            return;
        }
#endif
    }
    else
    {

#ifdef NS_PORT
#ifndef OMNETPP
        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);
#else
        // IPv4Address   desAddIp4(dst.s_addr);
        // IPvXAddress destAdd(desAddIp4);
        ManetAddress destAdd;
        if (dst.s_addr == ManetAddress(IPv4Address(AODV_BROADCAST)))
        {
            destAdd = ManetAddress(IPv4Address::ALLONES_ADDRESS);
            if (delay>0)
            {
                if (useIndex)
                    sendToIp(aodv_msg, 654, destAdd, 654,ttl,delay,dev->ifindex);
                else
                    sendToIp(aodv_msg, 654, destAdd, 654,ttl,delay,dev->ipaddr.s_addr);
            }
            else
            {
                if (useIndex)
                    sendToIp(aodv_msg, 654, destAdd, 654,ttl,par("broadcastDelay").doubleValue(),dev->ifindex);
                else
                    sendToIp(aodv_msg, 654, destAdd, 654,ttl,par("broadcastDelay").doubleValue(),dev->ipaddr.s_addr);
            }
        }
        else
        {
            destAdd = dst.s_addr;
            if (delay>0)
            {
                if (useIndex)
                    sendToIp(aodv_msg, 654, destAdd, 654,ttl,delay,dev->ifindex);
                else
                    sendToIp(aodv_msg, 654, destAdd, 654,ttl,delay,dev->ipaddr.s_addr);
            }
            else
            {
                if (useIndex)
                    sendToIp(aodv_msg, 654, destAdd, 654,ttl,par("unicastDelay").doubleValue(),dev->ifindex);
                else
                    sendToIp(aodv_msg, 654, destAdd, 654,ttl,par("unicastDelay").doubleValue(),dev->ipaddr.s_addr);
            }
        }
        totalSend++;
#endif
#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;
        }
#endif
    }

    /* Do not print hello msgs... */
    if (!(aodv_msg->type == AODV_RREP && (dst.s_addr == ManetAddress(IPv4Address(AODV_BROADCAST)))))
        DEBUG(LOG_INFO, 0, "AODV msg to %s ttl=%d retval=%u size=%u",
              ip_to_str(dst), ttl, retval, len);

    return;
}
struct cmsghdr* cmsg_nxthdr_fix ( struct msghdr *  __msg,
struct cmsghdr *  __cmsg 
) [read]

Variable Documentation

int num_rerr

Definition at line 52 of file NA_aodv_socket.h.

int num_rreq

Definition at line 51 of file NA_aodv_socket.h.

struct timeval rerr_ratel[RERR_RATELIMIT-1]

Definition at line 50 of file NA_aodv_socket.h.

struct timeval rreq_ratel[RREQ_RATELIMIT-1]

Definition at line 50 of file NA_aodv_socket.h.

 All Classes Files Functions Variables Typedefs Enumerator Defines