Exemplo n.º 1
0
int route_del(struct sockaddr_storage *destination, struct sockaddr_storage *gateway,int prefix,unsigned int metric){
	struct rtnl_handle rth;

	// structure of the netlink packet. 
	struct{
		struct nlmsghdr n;
		struct rtmsg r;
		char buf[1024];
	} req;

	char mxbuf[256];
	struct rtattr * mxrta = (void*)mxbuf;
	//unsigned mxlock = 0;
	memset(&req, 0, sizeof(req));

	// Initialisation of a few parameters 
	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
	req.n.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE;
	req.n.nlmsg_type = RTM_DELROUTE;
	req.r.rtm_family = destination->ss_family;
	req.r.rtm_table = get_eigrp_routing_table_number();
	req.r.rtm_dst_len = prefix;
	req.r.rtm_protocol = get_eigrp_routing_protocol_number();
	req.r.rtm_scope = RT_SCOPE_UNIVERSE;
	req.r.rtm_type = RTN_UNICAST;
	mxrta->rta_type = RTA_METRICS;
	mxrta->rta_len = RTA_LENGTH(0);
	
	// RTA_DST and RTA_GW are the two esential parameters for adding a route
	// for ipv4, the length of the address is 4 bytes.
	if(destination->ss_family == AF_INET){
		struct sockaddr_in *dest = (struct sockaddr_in *)destination;
		struct sockaddr_in *gate = (struct sockaddr_in *)gateway;
		addattr_l(&req.n, sizeof(req), RTA_DST, &dest->sin_addr.s_addr, 4);
		addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &gate->sin_addr.s_addr, 4);
	}else{
		struct sockaddr_in6 *dest = (struct sockaddr_in6 *)destination;
		struct sockaddr_in6 *gate = (struct sockaddr_in6 *)gateway;
		addattr_l(&req.n, sizeof(req), RTA_DST, &dest->sin6_addr.s6_addr, sizeof(dest->sin6_addr.s6_addr));
		addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &gate->sin6_addr.s6_addr, sizeof(dest->sin6_addr.s6_addr));
	}

	addattr_l(&req.n, sizeof(req), RTA_PRIORITY, &metric, sizeof(metric));

	int status = 0;

	// opening the netlink socket to communicate with the kernel
	if (rtnl_open(&rth, 0) < 0){
		printf("cannot open rtnetlink\n");
		return -1;
	}

	// sending the packet to the kernel.
	status = rtnl_talk(&rth, &req.n, 0, 0, NULL);
	if (status < 0)
		return status;
	
	rtnl_close(&rth);
	return 0;
}
Exemplo n.º 2
0
static void load_info(void)
{
	struct ifstat_ent *db, *n;
	struct rtnl_handle rth;

	if (rtnl_open(&rth, 0) < 0)
		exit(1);

	if (rtnl_wilddump_request(&rth, AF_INET, RTM_GETLINK) < 0) {
		perror("Cannot send dump request");
		exit(1);
	}

	if (rtnl_dump_filter(&rth, get_netstat_nlmsg, NULL, NULL, NULL) < 0) {
		fprintf(stderr, "Dump terminated\n");
		exit(1);
	}

	rtnl_close(&rth);

	db = kern_db;
	kern_db = NULL;

	while (db) {
		n = db;
		db = db->next;
		n->next = kern_db;
		kern_db = n;
	}
}
Exemplo n.º 3
0
static int flush_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
{
	struct rtnl_handle rth2;
	struct rtmsg *r = NLMSG_DATA(n);
	int len = n->nlmsg_len;
	struct rtattr * tb[FRA_MAX+1];

	len -= NLMSG_LENGTH(sizeof(*r));
	if (len < 0)
		return -1;

	parse_rtattr(tb, FRA_MAX, RTM_RTA(r), len);

	if (tb[FRA_PRIORITY]) {
		n->nlmsg_type = RTM_DELRULE;
		n->nlmsg_flags = NLM_F_REQUEST;

		if (rtnl_open(&rth2, 0) < 0)
			return -1;

		if (rtnl_talk(&rth2, n, 0, 0, NULL, NULL, NULL) < 0)
			return -2;

		rtnl_close(&rth2);
	}

	return 0;
}
Exemplo n.º 4
0
struct rt_entry * rt_fetch(struct rt_entry *r)
{
	struct rtnl_handle rth;

	// open netlink socket of NETLINK_ROUTE
	if (rtnl_open(&rth, 0) < 0) {
		printf("Can not initialize netlink interface...\n");
		return NULL;
	}

	ll_init_map(&rth);

	if (rtnl_wilddump_request(&rth, AF_INET, RTM_GETROUTE) < 0) {
		printf("Cannot send dump request\n");
		close(rth.fd);
		return NULL;
	}

	if (rtnl_dump_filter(&rth, rt_filter, r, NULL, NULL) < 0) {
		printf("Dump terminated.\n");
		close(rth.fd);
		return NULL;
	}

	close(rth.fd);
	return r;
}
Exemplo n.º 5
0
int
iface_gre_del(const char *gre_iface)
{
    struct softgred_config *cfg = softgred_config_ref();
    int ret;
    errno = 0;

    if ((ret=rtnl_open(&cfg->rth, 0)) < 0)
    {
        D_DEBUG1("Cannot open rtnetlink\n");
        return EXIT_FAILURE;
    }

    if(!iplink_have_newlink())
    {
        D_DEBUG1("iplink_have_newlink ret=%d strerror='%s'\n", ret, strerror(errno));
    }
    else
    {
        ret = iplink_modify(RTM_DELLINK, 0, gre_iface, NULL, NULL, NULL, NULL);
        if (ret != 0)
        {
            D_DEBUG1("iplink_modify(RTM_DELLINK) ret=%d strerror='%s'\n", ret, strerror(errno));
        }
    }

    rtnl_close(&cfg->rth);

    return 1;
}
Exemplo n.º 6
0
/*
 * rule_flush_table_range: deletes all the rules which lookup the table X.
 * The table X is any table in the range of `a' <= X <= `b'.
 */
int
rule_flush_table_range(int family, int a, int b)
{
	struct rtnl_handle rth;
	int arg[2];

	if (rtnl_open(&rth, 0) < 0)
		return 1;

	if (rtnl_wilddump_request(&rth, family, RTM_GETRULE) < 0) {
		error("Cannot dump the routing rule table");
		return -1;
	}

	arg[0] = a;
	arg[1] = b;
	if (rtnl_dump_filter
		(&rth, rule_flush_table_range_filter, arg, NULL, NULL) < 0) {
		error("Flush terminated");
		return -1;
	}

	rtnl_close(&rth);

	return 0;
}
Exemplo n.º 7
0
/****************************************************************
 NAME	: ipaddr_list				00/06/02 20:02:23
 AIM	: 
 REMARK	:
****************************************************************/
int ipaddr_list( int ifindex, uint32_t *array, int max_elem )
{
	struct rtnl_handle	rth;
	iplist_ctx	ctx;
	/* init the struct */
	ctx.ifindex	= ifindex;
	ctx.addr	= array;
	ctx.max_elem	= max_elem;
	ctx.nb_elem	= 0;
	/* open the rtnetlink socket */
	if( rtnl_open( &rth, 0) )
		return -1;
	/* send the request */
	if (rtnl_wilddump_request(&rth, AF_INET, RTM_GETADDR) < 0) {
		perror("Cannot send dump request");
		return -1;
	}
	/* parse the answer */
	if (rtnl_dump_filter(&rth, get_addrinfo, &ctx, NULL, NULL) < 0) {
		fprintf(stderr, "Flush terminated\n");
		exit(1);
	}
	
	/* to close the clocket */
 	rtnl_close( &rth );
	
	return ctx.nb_elem;
}
Exemplo n.º 8
0
int tc_qdisc_list(int argc, char **argv)
{
	struct tcmsg t;
	struct rtnl_handle rth;
	char d[16];

	memset(&t, 0, sizeof(t));
	t.tcm_family = AF_UNSPEC;
	memset(&d, 0, sizeof(d));
	
	while (argc > 0) {
		if (strcmp(*argv, "dev") == 0) {
			NEXT_ARG();
			strncpy(d, *argv, sizeof(d)-1);
#ifdef TC_H_INGRESS
                } else if (strcmp(*argv, "ingress") == 0) {
                             if (t.tcm_parent) {
                                     fprintf(stderr, "Duplicate parent ID\n");
                                     usage();
                             }
                             t.tcm_parent = TC_H_INGRESS;
#endif
		} else if (matches(*argv, "help") == 0) {
			usage();
		} else {
			fprintf(stderr, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv);
			return -1;
		}

		argc--; argv++;
	}

	if (rtnl_open(&rth, 0) < 0) {
		fprintf(stderr, "Cannot open rtnetlink\n");
		exit(1);
	}

	ll_init_map(&rth);

	if (d[0]) {
		if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
			fprintf(stderr, "Cannot find device \"%s\"\n", d);
			exit(1);
		}
		filter_ifindex = t.tcm_ifindex;
	}

	if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) {
		perror("Cannot send dump request");
		exit(1);
	}

	if (rtnl_dump_filter(&rth, print_qdisc, stdout, NULL, NULL) < 0) {
		fprintf(stderr, "Dump terminated\n");
		exit(1);
	}

	rtnl_close(&rth);
	return 0;
}
Exemplo n.º 9
0
/* 
 * rule_flush_table_range_filter: rtnl_dump filter for
 * rule_flush_table_range() (see below)
 */
int
rule_flush_table_range_filter(const struct sockaddr_nl *who,
							  struct nlmsghdr *n, void *arg)
{
	struct rtnl_handle rth2;
	struct rtmsg *r = NLMSG_DATA(n);
	int len = n->nlmsg_len;
	struct rtattr *tb[RTA_MAX + 1];
	u_int a = *(u_int *) arg;
	u_int b = *((u_int *) arg + 1);

	len -= NLMSG_LENGTH(sizeof(*r));
	if (len < 0)
		return -1;

	parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);

	if (tb[RTA_PRIORITY] && (r->rtm_table >= a && r->rtm_table <= b)) {
		n->nlmsg_type = RTM_DELRULE;
		n->nlmsg_flags = NLM_F_REQUEST;

		if (rtnl_open(&rth2, 0) < 0)
			return -1;

		if (rtnl_talk(&rth2, n, 0, 0, NULL, NULL, NULL) < 0)
			return -2;

		rtnl_close(&rth2);
	}

	return 0;
}
Exemplo n.º 10
0
/** Initialize interface table
 *
 * Initialize rtnl interface and interface table
 * Call this before any nlif_* function
 *
 * \return file descriptor to netlink socket
 */
struct nlif_handle *nlif_open(void)
{
	struct nlif_handle *h;

	h = calloc(1,  sizeof(struct nlif_handle));
	if (h == NULL)
		goto err;

	h->ifadd_handler.nlmsg_type = RTM_NEWLINK;
	h->ifadd_handler.handlefn = iftable_add;
	h->ifadd_handler.arg = h;
	h->ifdel_handler.nlmsg_type = RTM_DELLINK;
	h->ifdel_handler.handlefn = iftable_del;
	h->ifdel_handler.arg = h;

	h->rtnl_handle = rtnl_open();
	if (h->rtnl_handle == NULL)
		goto err;

	if (rtnl_handler_register(h->rtnl_handle, &h->ifadd_handler) < 0)
		goto err_close;

	if (rtnl_handler_register(h->rtnl_handle, &h->ifdel_handler) < 0)
		goto err_unregister;

	return h;

err_unregister:
	rtnl_handler_unregister(h->rtnl_handle, &h->ifdel_handler);
err_close:
	rtnl_close(h->rtnl_handle);
	free(h);
err:
	return NULL;
}
Exemplo n.º 11
0
/*
 * returns: -1 - address not found, 0 - addr is ok, 1 - addr is tentative
 */
int is_addr_tentative(char * ifacename, int iface, char * addr)
{
    char buf[256];
    char packed1[16];
    char packed2[16];
    struct rtattr * rta_tb[IFA_MAX+1];
    struct nlmsg_list *ainfo = NULL;
    struct nlmsg_list *head = NULL;
    struct rtnl_handle rth;

    int tentative = LOWLEVEL_TENTATIVE_DONT_KNOW;

    inet_pton6(addr,packed1);

    rtnl_open(&rth, 0);

    /* 2nd attribute: AF_UNSPEC, AF_INET, AF_INET6 */
    /* rtnl_wilddump_request(&rth, AF_PACKET, RTM_GETLINK); */
    rtnl_wilddump_request(&rth, AF_INET6, RTM_GETADDR);
    rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL);

    head = ainfo;
    while (ainfo) {
	struct nlmsghdr *n = &ainfo->h;
	struct ifaddrmsg *ifa = NLMSG_DATA(n);
	
	memset(rta_tb, 0, sizeof(*rta_tb));
	
	if (ifa->ifa_index == iface && ifa->ifa_family==AF_INET6) {
	    parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
	    if (!rta_tb[IFA_LOCAL])   rta_tb[IFA_LOCAL]   = rta_tb[IFA_ADDRESS];
	    if (!rta_tb[IFA_ADDRESS]) rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
	    
	    inet_ntop6(RTA_DATA(rta_tb[IFA_LOCAL]), buf /*, sizeof(buf)*/);
	    memcpy(packed2,RTA_DATA(rta_tb[IFA_LOCAL]),16);

	    /* print_packed(packed1); printf(" "); print_packed(packed2); printf("\n"); */

	    /* is this addr which are we looking for? */
	    if (!memcmp(packed1,packed2,16) ) {
		if (ifa->ifa_flags & IFA_F_TENTATIVE)
		    tentative = LOWLEVEL_TENTATIVE_YES;
		else
		    tentative = LOWLEVEL_TENTATIVE_NO;
	    }
	}
	ainfo = ainfo->next;
    }

    /* now delete list */
    while (head) {
	ainfo = head;
	head = head->next;
	free(ainfo);
    }
    
    rtnl_close(&rth);

    return tentative;
}
Exemplo n.º 12
0
void neigh_flush_table(char *iface)
{
	struct rtnl_handle rth;
	char flushb[4096-512];

   DEBUG_MSG("neigh_flush_table %s", iface);
   
	memset(&filter, 0, sizeof(filter));

   filter.state = ~0;
   filter.family = AF_INET;

   /* flush all but permanent and noarp */
    
	filter.state = ~(NUD_PERMANENT|NUD_NOARP);

   /* open the netlink socket */
   
	if (rtnl_open(&rth, 0) < 0)
		ERROR_MSG("rtnl_open()");

	ll_init_map(&rth);

   /* fill the device data */
   
   if ((filter.index = ll_name_to_index(iface)) == 0)
      ERROR_MSG("ll_name_to_index(%s)", iface);


	filter.flushb = flushb;
	filter.flushp = 0;
	filter.flushe = sizeof(flushb);
	filter.rth = &rth;
	filter.state &= ~NUD_FAILED;

	for (;;) {
		if (rtnl_wilddump_request(&rth, filter.family, RTM_GETNEIGH) < 0) 
         ERROR_MSG("rtnl_wilddump_request()");
      
		filter.flushed = 0;
		
      /* 
       * count how many neigh are to be flushed 
       * and prepare the data
       */
      
      if (rtnl_dump_filter(&rth, count_neigh, stdout, NULL, NULL) < 0) 
         ERROR_MSG("rtnl_dump_filter()");
      
		if (filter.flushed == 0)
         return;
		
		if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0)
			ERROR_MSG("rtnl_send()");
      
      filter.flushp = 0;
 
      DEBUG_MSG("*** deleting %d entries ***", filter.flushed);
	}
}
int iprule_list(int argc, char **argv)
{
	struct rtnl_handle rth;
	int af = preferred_family;

	if (af == AF_UNSPEC)
		af = AF_INET;

	if (argc > 0) {
		fprintf(stderr, "\"ip rule show\" does not take any arguments.\n");
		return -1;
	}

	if (rtnl_open(&rth, 0) < 0)
		return 1;

	if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) {
		perror("Cannot send dump request");
		return 1;
	}

	if (rtnl_dump_filter(&rth, print_rule, stdout, NULL, NULL) < 0) {
		fprintf(stderr, "Dump terminated\n");
		return 1;
	}

	return 0;
}
Exemplo n.º 14
0
int
main(int argc, char **argv)
{
	while (argc > 1) {
		char *opt = argv[1];
		if (strcmp(opt,"--") == 0) {
			argc--; argv++;
			break;
		}
		if (opt[0] != '-')
			break;
		if (opt[1] == '-')
			opt++;

		if (matches(opt, "-help") == 0) {
			usage();
		} else if (matches(opt, "-Version") == 0) {
			printf("bridge utility, 0.0\n");
			exit(0);
		} else if (matches(opt, "-stats") == 0 ||
			   matches(opt, "-statistics") == 0) {
			++show_stats;
		} else if (matches(opt, "-details") == 0) {
			++show_details;
		} else if (matches(opt, "-timestamp") == 0) {
			++timestamp;
                } else if (matches(opt, "-family") == 0) {
			argc--;
			argv++;
			if (argc <= 1)
				usage();
			if (strcmp(argv[1], "inet") == 0)
				preferred_family = AF_INET;
			else if (strcmp(argv[1], "inet6") == 0)
				preferred_family = AF_INET6;
			else if (strcmp(argv[1], "help") == 0)
				usage();
			else
				invarg("invalid protocol family", argv[1]);
		} else if (strcmp(opt, "-4") == 0) {
			preferred_family = AF_INET;
		} else if (strcmp(opt, "-6") == 0) {
			preferred_family = AF_INET6;
		} else {
			fprintf(stderr, "Option \"%s\" is unknown, try \"bridge help\".\n", opt);
			exit(-1);
		}
		argc--;	argv++;
	}

	if (rtnl_open(&rth, 0) < 0)
		exit(1);

	if (argc > 1)
		return do_cmd(argv[1], argc-1, argv+1);

	rtnl_close(&rth);
	usage();
}
Exemplo n.º 15
0
int main(int argc, char **argv)
{
	int ret;
	int do_batching = 0;
	char *batchfile = NULL;

	while (argc > 1) {
		if (argv[1][0] != '-')
			break;
		if (matches(argv[1], "-stats") == 0 ||
			 matches(argv[1], "-statistics") == 0) {
			++show_stats;
		} else if (matches(argv[1], "-details") == 0) {
			++show_details;
		} else if (matches(argv[1], "-raw") == 0) {
			++show_raw;
		} else if (matches(argv[1], "-pretty") == 0) {
			++show_pretty;
		} else if (matches(argv[1], "-Version") == 0) {
			printf("tc utility, iproute2-ss%s\n", SNAPSHOT);
			return 0;
		} else if (matches(argv[1], "-iec") == 0) {
			++use_iec;
		} else if (matches(argv[1], "-help") == 0) {
			usage();
			return 0;
		} else if (matches(argv[1], "-force") == 0) {
			++force;
		} else 	if (matches(argv[1], "-batch") == 0) {
			do_batching = 1;
			if (argc > 2)
				batchfile = argv[2];
			argc--;	argv++;
		} else {
			fprintf(stderr, "Option \"%s\" is unknown, try \"tc -help\".\n", argv[1]);
			return -1;
		}
		argc--;	argv++;
	}

	if (do_batching)
		return batch(batchfile);

	if (argc <= 1) {
		usage();
		return 0;
	}

	tc_core_init();
	if (rtnl_open(&rth, 0) < 0) {
		fprintf(stderr, "Cannot open rtnetlink\n");
		exit(1);
	}

	ret = do_cmd(argc-1, argv+1);
	rtnl_close(&rth);

	return ret;
}
Exemplo n.º 16
0
int lowlevelInit()
{
    if (rtnl_open(&rth, 0) < 0) {
	sprintf(Message, "Cannot open rtnetlink\n");
	return LOWLEVEL_ERROR_SOCKET;
    }
    return 0;
}
Exemplo n.º 17
0
int ipneigh_modify(int cmd, int flags, int nud, char *ll_addr, u_int32 ip, char *iface)
{
	struct rtnl_handle rth;
	struct {
		struct nlmsghdr 	n;
		struct ndmsg 		ndm;
		char   			   buf[256];
	} req;
   
	inet_prefix dst;

	memset(&req, 0, sizeof(req));

	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
	req.n.nlmsg_flags = NLM_F_REQUEST | flags;
	req.n.nlmsg_type = cmd;
	req.ndm.ndm_family = AF_INET;
	
   /* 
    * the state of the entries 
    * SARP messages can nevere expire since they are 
    * authenticated. they will be replaced if a new
    * SARP message is received
    */
   
   req.ndm.ndm_state = nud;

   /* add the IP */
  
//daveti: get_addr should be provide by utils.h in libutil...
//Now it is included in utils.h in iproute2 source tar.
   get_addr(&dst, inet_ntoa(*(struct in_addr *)&ip), AF_INET);
	addattr_l(&req.n, sizeof(req), NDA_DST, &dst.data, dst.bytelen);

   /* add the link layer address */
   
	addattr_l(&req.n, sizeof(req), NDA_LLADDR, ll_addr, LL_ADDR_LEN);

   /* open the netlink socket */
   
	if (rtnl_open(&rth, 0) < 0)
		ERROR_MSG("rtnl_open()");

	ll_init_map(&rth);

   /* find the iface index */
   
	if ((req.ndm.ndm_ifindex = ll_name_to_index(iface)) == 0) 
      ERROR_MSG("ll_name_to_index()");

   /* send data */
   
	if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
		ERROR_MSG("rtnl_talk()");
 
   return 0;
}
Exemplo n.º 18
0
int do_monitor(int argc, char **argv)
{
	char *file = NULL;
	unsigned groups = ~RTMGRP_TC;
	int llink=0;
	int lneigh=0;

	rtnl_close(&rth);

	while (argc > 0) {
		if (matches(*argv, "file") == 0) {
			NEXT_ARG();
			file = *argv;
		} else if (matches(*argv, "link") == 0) {
			llink=1;
			groups = 0;
		} else if (matches(*argv, "fdb") == 0) {
			lneigh = 1;
			groups = 0;
		} else if (strcmp(*argv, "all") == 0) {
			groups = ~RTMGRP_TC;
			prefix_banner=1;
		} else if (matches(*argv, "help") == 0) {
			usage();
		} else {
			fprintf(stderr, "Argument \"%s\" is unknown, try \"bridge monitor help\".\n", *argv);
			exit(-1);
		}
		argc--;	argv++;
	}

	if (llink)
		groups |= nl_mgrp(RTNLGRP_LINK);

	if (lneigh) {
		groups |= nl_mgrp(RTNLGRP_NEIGH);
	}

	if (file) {
		FILE *fp;
		fp = fopen(file, "r");
		if (fp == NULL) {
			perror("Cannot fopen");
			exit(-1);
		}
		return rtnl_from_file(fp, accept_msg, stdout);
	}

	if (rtnl_open(&rth, groups) < 0)
		exit(1);
	ll_init_map(&rth);

	if (rtnl_listen(&rth, accept_msg, stdout) < 0)
		exit(2);

	return 0;
}
Exemplo n.º 19
0
int init_bridge_ops(void)
{
    if(rtnl_open(&rth, RTMGRP_LINK) < 0)
    {
        fprintf(stderr, "Couldn't open rtnl socket for monitoring\n");
        return -1;
    }

    if(rtnl_open(&rth_state, 0) < 0)
    {
        fprintf(stderr, "Couldn't open rtnl socket for setting state\n");
        return -1;
    }

    if(rtnl_wilddump_request(&rth, PF_BRIDGE, RTM_GETLINK) < 0)
    {
        //fprintf(stderr, "Cannot send dump request: %m\n");
        fprintf(stderr, "Cannot send dump request: %s\n", strerror(errno));
        return -1;
    }
    if(rtnl_dump_filter(&rth, dump_msg, stdout, NULL, NULL) < 0)
    {
        fprintf(stderr, "Dump terminated\n");
        return -1;
    }
    if(fcntl(rth.fd, F_SETFL, O_NONBLOCK) < 0)
    {
        //fprintf(stderr, "Error setting O_NONBLOCK: %m\n");
        fprintf(stderr, "Error setting O_NONBLOCK: %s\n", strerror(errno));
        return -1;
    }

    br_handler.fd = rth.fd;
    br_handler.arg = NULL;
    br_handler.handler = br_ev_handler;

    if(add_epoll(&br_handler) < 0){
        return -1;
	}

    return 0;
}
Exemplo n.º 20
0
static int PyRtnl_init(PyObject* obj, PyObject* args, PyObject* kwargs)
{
  PyRtnlObject* self = (PyRtnlObject*)obj;

  if (rtnl_open(&self->rth, 0) < 0) {
    PyErr_SetString(PyExc_IOError, "could not open rtnl handle");
    return -1;
  }

  return 0;
}
Exemplo n.º 21
0
/*
 * main body of the program
 */
int
main(int	argc,
     char *	argv[])
{
  struct rtnl_handle	rth;
  int opt;

  /* Check command line options */
  while((opt = getopt_long(argc, argv, "hv", long_opts, NULL)) > 0)
    {
      switch(opt)
	{
	case 'h':
	  iw_usage(0);
	  break;

	case 'v':
	  return(iw_print_version_info("iwevent"));
	  break;

	default:
	  iw_usage(1);
	  break;
	}
    }
  if(optind < argc)
    {
      fputs("Too many arguments.\n", stderr);
      iw_usage(1);
    }

  /* Open netlink channel */
  if(rtnl_open(&rth, RTMGRP_LINK) < 0)
    {
      perror("Can't initialize rtnetlink socket");
      return(1);
    }

#if WIRELESS_EXT > 13
  fprintf(stderr, "Waiting for Wireless Events...\n");
#else	/* WIRELESS_EXT > 13 */
  fprintf(stderr, "Unsupported in Wireless Extensions <= 14 :-(\n");
  return(-1);
#endif	/* WIRELESS_EXT > 13 */

  /* Do what we have to do */
  wait_for_event(&rth);

  /* Cleanup - only if you are pedantic */
  rtnl_close(&rth);

  return(0);
}
Exemplo n.º 22
0
int look_interface_changes(){

	if (rtnl_open(&rtnl_interface, RTNLGRP_LINK) < 0){
		printf("cannot open rtnetlink\n");
		return -1;
	}

	int status = rtnl_listen(&rtnl_interface,handle,0);

	rtnl_close(&rtnl_interface);
	printf("RTNL OFF\n");
	return status;
}
Exemplo n.º 23
0
int test_look_interface_changes(){

	struct rtnl_handle rth;
	if (rtnl_open(&rth, RTNLGRP_LINK) < 0){
		printf("cannot open rtnetlink\n");
		return -1;
	}
	
	int status = rtnl_listen(&rth,test_handle,0);

	rtnl_close(&rth);
	return status;
}
Exemplo n.º 24
0
int rt_restore_entry(struct rt_entry *r)
{
	struct rtnl_handle rth;

	struct {
		struct nlmsghdr n;
		struct rtmsg r;
		char buf[1024];
	} req;

	memset(&req, 0, sizeof(req));

	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
	req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
	req.n.nlmsg_type = RTM_NEWROUTE;

	memcpy(&req.r, r->rtm, sizeof(struct rtmsg));

	if (r->src)
		addattr_l(&req.n, sizeof(req), RTA_SRC, &r->src, 4);
	if (r->psrc)
		addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &r->psrc, 4);
	if (r->dest)
		addattr_l(&req.n, sizeof(req), RTA_DST, &r->dest, 4);
	if (r->gate)
		addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &r->gate, 4);
	if (r->flow)
		addattr_l(&req.n, sizeof(req), RTA_FLOW, &r->flow, 4);

	if (r->oif)
		addattr32(&req.n, sizeof(req), RTA_OIF, r->oif);
	if (r->iif)
		addattr32(&req.n, sizeof(req), RTA_IIF, r->iif);
	if (r->prio)
		addattr32(&req.n, sizeof(req), RTA_PRIORITY, r->prio);
	if (r->metrics)
		addattr32(&req.n, sizeof(req), RTA_METRICS, r->metrics);

	if (rtnl_open(&rth, 0) < 0) {
		printf("Can not initialize netlink interface...\n");
		return -1;
	}

	if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
		printf("Can not talk with netlink interface...\n");
		return -1;
	}

	return 0;
}
Exemplo n.º 25
0
void add_classes() {
// call dump of classes, these needs to be dumped on per device basis
	struct tcmsg t;
	struct rtnl_handle rth;
	char d[16];

	char line[1024];
	FILE *dev;
	char *p;

	memset(&rth,0,sizeof(struct rtnl_handle));
	memset(&t, 0, sizeof(t));

	t.tcm_family = AF_UNSPEC;
	memset(&d, 0, sizeof(d));
	
	if (rtnl_open(&rth, 0) < 0) {
		snmp_log(LOG_NOTICE, "Cannot open rtnetlink");
		return;
	}


	dev=fopen("/proc/net/dev","r");
	if(dev==NULL) {
	    snmp_log(LOG_WARNING,"qos-ext: cannot open /proc/net/dev for reading, class stats won't be avaiable");
	    return;
	}    
	while(fscanf(dev,"%s",line)!=EOF) {
	//find existing devices
	    p=index(line,':'); if(p==NULL) continue;
	    *p='\0';
	    // line contains device_name.... need to translate name into index
	    t.tcm_ifindex = ll_name_to_index(line);
	
	    if (rtnl_dump_request(&rth, RTM_GETTCLASS, &t, sizeof(t)) < 0) {
		//dump all classes on device with index t.tcm_ifindex
	    	snmp_log(LOG_WARNING,"qos-ext: add_new_entries to table: cannot send dump request");
		fclose(dev);
		return;
	    }
	    if (rtnl_dump_filter(&rth, insert_entry, NULL, NULL, NULL) < 0) {
		//append all classes entris into the internal table
		snmp_log(LOG_WARNING, "qos-ext: add_new entries to table: class dump terminated");
		fclose(dev);
		return;
	    }
	}
	fclose(dev);
	rtnl_close(&rth);
}
Exemplo n.º 26
0
static int resolve_mac_from_cache_open(int ai_family)
{
	struct {
		struct nlmsghdr hdr;
		struct ndmsg msg;
	} nlreq;

	memset(&nlreq, 0, sizeof(nlreq));
	nlreq.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(nlreq.msg));
	nlreq.hdr.nlmsg_type = RTM_GETNEIGH;
	nlreq.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
	nlreq.msg.ndm_family = ai_family;

	return rtnl_open(&nlreq, NETLINK_ROUTE);
}
Exemplo n.º 27
0
/**
 * vlan_get_link_open - send a get_link request
 * @ifname: the interface to query
 *
 * Returns 0 in case of success or a negative error code otherwise
 */
static int vlan_get_link_open(const char *ifname)
{
	struct {
		struct nlmsghdr hdr;
		struct ifinfomsg ifi;
	} nlreq;

	memset(&nlreq, 0, sizeof(nlreq));
	nlreq.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(nlreq.ifi));
	nlreq.hdr.nlmsg_type = RTM_GETLINK;
	nlreq.hdr.nlmsg_flags = NLM_F_REQUEST;
	nlreq.ifi.ifi_family = AF_UNSPEC;
	nlreq.ifi.ifi_index = if_nametoindex(ifname);

	return rtnl_open(&nlreq, 0);
}
Exemplo n.º 28
0
/*
 * if_init_all: it initializes all the `ifs_n'# interfaces present in the
 * `ifs' array. If `ifs_n' is zero it gets all the current up interfaces and
 * stores them in `new_ifs', updating the `new_ifs_n' counter too. Then it
 * initializes them.
 * In the `new_ifs' array, which must be at least big as the `ids' array, it
 * stores all the initialized interfaces, updating the `new_ifs_n' counter.
 * On error -1 is returned.
 */
int if_init_all(char *ifs_name[MAX_INTERFACES], int ifs_n, 
		interface *new_ifs, int *new_ifs_n)
{
	struct rtnl_handle rth;
	int ret=0, i, n;

	if (rtnl_open(&rth, 0) < 0) {
		error("Cannot open the rtnetlink socket to talk to the kernel's "
				"soul");
		return -1;
	}
	ll_init_map(&rth);

	if(!ifs_n) {
		ret=get_all_up_ifs(new_ifs, MAX_INTERFACES);

		if(!ret)
			return -1;

		*new_ifs_n=ret;
	} else {
		for(i=0, n=0; i<ifs_n; i++) {
			
			new_ifs[n].dev_idx=ll_name_to_index(ifs_name[n]);
			if(!new_ifs[n].dev_idx) {
				error("Cannot initialize the %s interface. "
						"Ignoring it", ifs_name[n]);
				continue;
			}

			strncpy(new_ifs[n].dev_name, ifs_name[n], IFNAMSIZ);
			n++;
		}
		
		if(!n)
			return -1;
			
		*new_ifs_n=n;
	}

	if(set_all_ifs(new_ifs, *new_ifs_n, set_dev_up) < 0)
		return -1;

	rtnl_close(&rth);

	return ret;
}
Exemplo n.º 29
0
void list(void) {
	struct rtnl_handle rth;

	if (rtnl_open(&rth,0)) {
		perror("Cannot open rtnetlink");
		return;
	}
	if (rtnl_wilddump_request(&rth, AF_UNSPEC, RTM_GETLINK) < 0) {
		perror("Cannot send dump request");
		return;
	}

	if (rtnl_dump_filter(&rth, print_link, NULL) < 0) {
		fprintf(stderr, "Dump terminated\n");
		return;
	}
	rtnl_close(&rth);
}
Exemplo n.º 30
-1
static int flush_addrlabel(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
{
	struct rtnl_handle rth2;
	struct rtmsg *r = NLMSG_DATA(n);
	int len = n->nlmsg_len;
	struct rtattr * tb[IFAL_MAX+1];

	len -= NLMSG_LENGTH(sizeof(*r));
	if (len < 0)
		return -1;

	parse_rtattr(tb, IFAL_MAX, RTM_RTA(r), len);

	if (tb[IFAL_ADDRESS]) {
		n->nlmsg_type = RTM_DELADDRLABEL;
		n->nlmsg_flags = NLM_F_REQUEST;

		if (rtnl_open(&rth2, 0) < 0)
			return -1;

		if (rtnl_talk(&rth2, n, NULL, 0) < 0)
			return -2;

		rtnl_close(&rth2);
	}

	return 0;
}