NETWORK ATTACKS FRAMEWORK  1.0.0
A NETwork Attacks framework. Making network attacks impact evaluation easier!
NA_nl.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/rtnetlink.h>
#include "defs.h"
#include "lnx/kaodv-netlink.h"
#include "debug.h"
#include "NA_aodv_rreq.h"
#include "NA_aodv_timeout.h"
#include "NA_routing_table.h"
#include "NA_aodv_hello.h"
#include "NA_params.h"
#include "NA_aodv_socket.h"
#include "NA_aodv_rerr.h"

Go to the source code of this file.

Classes

struct  nlsock

Defines

#define BUFLEN   256
#define ATTR_BUFLEN   512

Functions

void nl_init (void)
void nl_cleanup (void)
int prefix_length (int family, void *nm)
int addattr (struct nlmsghdr *n, int type, void *data, int alen)
int nl_send (struct nlsock *nl, struct nlmsghdr *n)
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_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_no_route_found_msg (struct in_addr dest)
int nl_send_del_route_msg (struct in_addr dest, struct in_addr next_hop, int metric)
int nl_send_conf_msg (void)

Variables

struct sockaddr_nl peer = { AF_NETLINK, 0, 0, 0 }
struct nlsock aodvnl
struct nlsock rtnl
int llfeedback
int active_route_timeout
int qual_threshold
int internet_gw_mode
int wait_on_reboot
struct timer worb_timer

Define Documentation

#define ATTR_BUFLEN   512

Definition at line 410 of file NA_nl.c.

#define BUFLEN   256

Definition at line 69 of file NA_nl.c.


Function Documentation

int addattr ( struct nlmsghdr *  n,
int  type,
void *  data,
int  alen 
)

Definition at line 395 of file NA_nl.c.

{
        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;

        return 0;
}
void nl_cleanup ( void  )

Definition at line 155 of file NA_nl.c.

{
        close(aodvnl.sock);
        close(rtnl.sock);
}
void nl_init ( void  )

Definition at line 73 of file NA_nl.c.

{
        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 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 
)

Definition at line 441 of file NA_nl.c.

{
        struct {
                struct nlmsghdr nlh;
                struct rtmsg rtm;
                char attrbuf[1024];
        } req;

        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.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);

        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));

        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));

        addattr(&req.nlh, RTA_PRIORITY, &metric, sizeof(metric));

        return nl_send(&rtnl, &req.nlh);
}
int nl_send ( struct nlsock nl,
struct nlmsghdr *  n 
)

Definition at line 412 of file NA_nl.c.

{
        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;

        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;

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

Definition at line 489 of file NA_nl.c.

{
        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);

        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.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_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;
        }
#ifdef DEBUG_NETLINK
        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);
}
int nl_send_conf_msg ( void  )

Definition at line 583 of file NA_nl.c.

{
        struct {
                struct nlmsghdr n;
                kaodv_conf_msg_t cm;
        } 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.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");
#endif
        return nl_send(&aodvnl, &areq.n);
}
int nl_send_del_route_msg ( struct in_addr  dest,
struct in_addr  next_hop,
int  metric 
)

Definition at line 551 of file NA_nl.c.

{
        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));

        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.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;
        }
#ifdef DEBUG_NETLINK
        DEBUG(LOG_DEBUG, 0, "Sending del route");
#endif
        return nl_kern_route(RTM_DELROUTE, 0, AF_INET, index, &dest, &next_hop,
                             NULL, metric);
}
int nl_send_no_route_found_msg ( struct in_addr  dest)

Definition at line 530 of file NA_nl.c.

{
        struct {
                struct nlmsghdr n;
                kaodv_rt_msg_t m;
        } 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.m.dst = dest.s_addr;

        DEBUG(LOG_DEBUG, 0, "Send NOROUTE_FOUND to kernel: %s",
              ip_to_str(dest));

        return nl_send(&aodvnl, &areq.n);
}
int prefix_length ( int  family,
void *  nm 
)

Definition at line 372 of file NA_nl.c.

{
        int prefix = 0;

        if (family == AF_INET) {
                unsigned int tmp;
                memcpy(&tmp, nm, sizeof(unsigned int));

                while (tmp) {
                        tmp = tmp << 1;
                        prefix++;
                }
                return prefix;

        } else {
                DEBUG(LOG_DEBUG, 0, "Unsupported address family");
        }

        return 0;
}

Variable Documentation

Definition at line 69 of file main.c.

struct nlsock aodvnl

Definition at line 59 of file NA_nl.c.

Definition at line 55 of file main.c.

Definition at line 64 of file main.c.

struct sockaddr_nl peer = { AF_NETLINK, 0, 0, 0 }

Definition at line 57 of file NA_nl.c.

Definition at line 63 of file main.c.

struct nlsock rtnl

Definition at line 60 of file NA_nl.c.

Definition at line 62 of file main.c.

struct timer worb_timer

Definition at line 66 of file main.c.

 All Classes Files Functions Variables Typedefs Enumerator Defines