summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/.gitignore27
-rw-r--r--tests/Makefile37
-rw-r--r--tests/Makefile.am67
-rw-r--r--tests/check-addr.c212
-rw-r--r--tests/check-all.c44
-rw-r--r--tests/check-attr.c88
-rw-r--r--tests/test-cache-mngr.c61
-rw-r--r--tests/test-complex-HTB-with-hash-filters.c761
-rw-r--r--tests/test-create-bond.c29
-rw-r--r--tests/test-create-bridge.c80
-rw-r--r--tests/test-create-ip6tnl.c55
-rw-r--r--tests/test-create-ipgre.c56
-rw-r--r--tests/test-create-ipip.c56
-rw-r--r--tests/test-create-ipvti.c55
-rw-r--r--tests/test-create-macvlan.c48
-rw-r--r--tests/test-create-sit.c56
-rw-r--r--tests/test-create-veth.c42
-rw-r--r--tests/test-create-vlan.c43
-rw-r--r--tests/test-create-vxlan.c47
-rw-r--r--tests/test-delete-link.c28
-rw-r--r--tests/test-genl.c106
-rw-r--r--tests/test-nf-cache-mngr.c23
-rw-r--r--tests/test-socket-creation.c15
-rw-r--r--tests/test-u32-filter-with-actions.c400
-rw-r--r--tests/util.h5
25 files changed, 2337 insertions, 104 deletions
diff --git a/tests/.gitignore b/tests/.gitignore
index 19184ee5..6b77cacc 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -1,4 +1,23 @@
-test-cache-mngr
-test-genl
-test-nf-cache-mngr
-test-socket-creation
+/check-all
+/check-all.log
+/check-all.trs
+/test-*.log
+/test-*.trs
+/test-cache-mngr
+/test-complex-HTB-with-hash-filters
+/test-create-bond
+/test-create-bridge
+/test-create-ip6tnl
+/test-create-ipgre
+/test-create-ipip
+/test-create-ipvti
+/test-create-sit
+/test-create-veth
+/test-create-vlan
+/test-create-vxlan
+/test-delete-link
+/test-genl
+/test-nf-cache-mngr
+/test-socket-creation
+/test-suite.log
+/test-u32-filter-with-actions
diff --git a/tests/Makefile b/tests/Makefile
deleted file mode 100644
index 8494eea0..00000000
--- a/tests/Makefile
+++ /dev/null
@@ -1,37 +0,0 @@
-#
-# src/Makefile
-#
-# This library is free software; you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation version 2.1
-# of the License.
-#
-# Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
-#
-
-ifeq ($(shell [ ! -r ../Makefile.opts ] && echo 1),)
- include ../Makefile.opts
-endif
-
-LDFLAGS += -L../lib -lnl ../src/utils.o
-CIN := $(wildcard test-*.c)
-TOOLS := $(CIN:%.c=%)
-
-all: $(TOOLS)
-
-$(TOOLS): ../src/utils.o
-
-test-%: test-%.c
- @echo " LD $@"; \
- $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) -lnl-genl -lnl-route
-
-clean:
- @echo " CLEAN src"; \
- rm -f $(TOOLS)
-
-distclean: clean
-
-install:
- @true
-
-include ../Makefile.rules
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100644
index 00000000..255033d7
--- /dev/null
+++ b/tests/Makefile.am
@@ -0,0 +1,67 @@
+# -*- Makefile -*-
+
+EXTRA_DIST = \
+ util.h
+
+if ENABLE_UNIT_TESTS
+
+AM_CPPFLAGS = -Wall -I${top_srcdir}/include -I${top_builddir}/include -D_GNU_SOURCE -DSYSCONFDIR=\"$(sysconfdir)/libnl\"
+
+LDADD = \
+ ${top_builddir}/lib/libnl-3.la \
+ ${top_builddir}/lib/libnl-nf-3.la \
+ ${top_builddir}/lib/libnl-genl-3.la \
+ ${top_builddir}/lib/libnl-route-3.la \
+ @CHECK_LIBS@
+
+AM_CFLAGS = @CHECK_CFLAGS@
+
+UNIT_TESTS = check-all
+
+check_PROGRAMS = \
+ test-create-bond \
+ test-create-vlan \
+ test-create-vxlan \
+ test-create-veth \
+ test-create-bridge \
+ test-create-ip6tnl \
+ test-create-ipgre \
+ test-create-ipip \
+ test-create-ipvti \
+ test-create-sit \
+ test-delete-link \
+ test-socket-creation \
+ test-complex-HTB-with-hash-filters \
+ test-u32-filter-with-actions \
+ ${UNIT_TESTS}
+
+TESTS = \
+ ${UNIT_TESTS}
+
+if ENABLE_CLI
+LDADD += ${top_builddir}/src/lib/libnl-cli-3.la
+check_PROGRAMS += \
+ test-cache-mngr \
+ test-genl \
+ test-nf-cache-mngr
+endif
+
+test_cache_mngr_SOURCES = test-cache-mngr.c
+test_create_bond_SOURCES = test-create-bond.c
+test_create_vlan_SOURCES = test-create-vlan.c
+test_create_vxlan_SOURCES = test-create-vxlan.c
+test_create_veth_SOURCES = test-create-veth.c
+test_create_bridge_SOURCES = test-create-bridge.c
+test_delete_link_SOURCES = test-delete-link.c
+test_genl_SOURCES = test-genl.c
+test_nf_cache_mngr_SOURCES = test-nf-cache-mngr.c
+test_socket_creation_SOURCES = test-socket-creation.c
+test_complex_HTB_with_hash_filters_SOURCES = test-complex-HTB-with-hash-filters.c
+test_u32_filter_with_actions_SOURCES = test-u32-filter-with-actions.c
+
+# Unit tests
+check_all_SOURCES = \
+ check-all.c \
+ check-addr.c \
+ check-attr.c
+endif
diff --git a/tests/check-addr.c b/tests/check-addr.c
new file mode 100644
index 00000000..39f3ede4
--- /dev/null
+++ b/tests/check-addr.c
@@ -0,0 +1,212 @@
+/*
+ * tests/check-addr.c nl_addr unit tests
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation version 2.1
+ * of the License.
+ *
+ * Copyright (c) 2013 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include <check.h>
+#include <netlink/addr.h>
+
+START_TEST(addr_alloc)
+{
+ struct nl_addr *addr;
+
+ addr = nl_addr_alloc(16);
+ fail_if(addr == NULL,
+ "Allocation should not return NULL");
+
+ fail_if(nl_addr_iszero(addr) == 0,
+ "New empty address should be all zeros");
+
+ fail_if(nl_addr_get_family(addr) != AF_UNSPEC,
+ "New empty address should have family AF_UNSPEC");
+
+ fail_if(nl_addr_get_prefixlen(addr) != 0,
+ "New empty address should have prefix length 0");
+
+ fail_if(nl_addr_shared(addr),
+ "New empty address should not be shared");
+
+ fail_if(nl_addr_get(addr) != addr,
+ "nl_addr_get() should return pointer to address");
+
+ fail_if(nl_addr_shared(addr) == 0,
+ "Address should be shared after call to nl_addr_get()");
+
+ nl_addr_put(addr);
+
+ fail_if(nl_addr_shared(addr),
+ "Address should not be shared after call to nl_addr_put()");
+
+ fail_if(nl_addr_fill_sockaddr(addr, NULL, 0) == 0,
+ "Socket address filling should fail for empty address");
+
+ nl_addr_put(addr);
+}
+END_TEST
+
+START_TEST(addr_binary_addr)
+{
+ struct nl_addr *addr, *addr2;
+ char baddr[4] = { 0x1, 0x2, 0x3, 0x4 };
+ char baddr2[6] = { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6 };
+
+ addr = nl_addr_alloc(4);
+ fail_if(addr == NULL,
+ "Allocation should not return NULL");
+
+ fail_if(nl_addr_set_binary_addr(addr, baddr, 4) < 0,
+ "Valid binary address should be settable");
+
+ fail_if(nl_addr_get_prefixlen(addr) != 0,
+ "Prefix length should be unchanged after nl_addr_set_binary_addr()");
+
+ fail_if(nl_addr_get_len(addr) != 4,
+ "Address length should be 4");
+
+ fail_if(nl_addr_set_binary_addr(addr, baddr2, 6) == 0,
+ "Should not be able to set binary address exceeding maximum length");
+
+ fail_if(nl_addr_get_len(addr) != 4,
+ "Address length should still be 4");
+
+ fail_if(nl_addr_guess_family(addr) != AF_INET,
+ "Binary address of length 4 should be guessed as AF_INET");
+
+ fail_if(memcmp(baddr, nl_addr_get_binary_addr(addr), 4) != 0,
+ "Binary address mismatches");
+
+ addr2 = nl_addr_build(AF_UNSPEC, baddr, 4);
+ fail_if(addr2 == NULL,
+ "Building of address should not fail");
+
+ nl_addr_set_prefixlen(addr, 32);
+ fail_if(nl_addr_get_prefixlen(addr) != 32,
+ "Prefix length should be successful changed after nl_addr_set_prefixlen()");
+
+ fail_if(nl_addr_cmp(addr, addr2),
+ "Addresses built from same binary address should match");
+
+ nl_addr_put(addr);
+ nl_addr_put(addr2);
+}
+END_TEST
+
+START_TEST(addr_parse4)
+{
+ struct nl_addr *addr4, *clone;
+ struct sockaddr_in sin;
+ socklen_t len = sizeof(sin);
+ char *addr_str = "10.0.0.1/16";
+ char buf[128];
+
+ fail_if(nl_addr_parse(addr_str, AF_INET6, &addr4) == 0,
+ "Should not be able to parse IPv4 address in IPv6 mode");
+
+ fail_if(nl_addr_parse(addr_str, AF_UNSPEC, &addr4) != 0,
+ "Should be able to parse \"%s\"", addr_str);
+
+ fail_if(nl_addr_get_family(addr4) != AF_INET,
+ "Address family should be AF_INET");
+
+ fail_if(nl_addr_get_prefixlen(addr4) != 16,
+ "Prefix length should be 16");
+
+ fail_if(nl_addr_iszero(addr4),
+ "Address should not be all zeroes");
+
+ clone = nl_addr_clone(addr4);
+ fail_if(clone == NULL,
+ "Cloned address should not be NULL");
+
+ fail_if(nl_addr_cmp(addr4, clone) != 0,
+ "Cloned address should not mismatch original");
+
+ fail_if(nl_addr_fill_sockaddr(addr4, (struct sockaddr *) &sin, &len) != 0,
+ "Should be able to fill socketaddr");
+
+ fail_if(strcmp(nl_addr2str(addr4, buf, sizeof(buf)), addr_str),
+ "Address translated back to string does not match original");
+
+ nl_addr_put(addr4);
+ nl_addr_put(clone);
+}
+END_TEST
+
+START_TEST(addr_parse6)
+{
+ struct nl_addr *addr6, *clone;
+ struct sockaddr_in6 sin;
+ socklen_t len = sizeof(sin);
+ char *addr_str = "2001:1:2::3/64";
+ char buf[128];
+
+ fail_if(nl_addr_parse(addr_str, AF_INET, &addr6) == 0,
+ "Should not be able to parse IPv6 address in IPv4 mode");
+
+ fail_if(nl_addr_parse(addr_str, AF_UNSPEC, &addr6) != 0,
+ "Should be able to parse \"%s\"", addr_str);
+
+ fail_if(nl_addr_get_family(addr6) != AF_INET6,
+ "Address family should be AF_INET6");
+
+ fail_if(nl_addr_get_prefixlen(addr6) != 64,
+ "Prefix length should be 64");
+
+ fail_if(nl_addr_iszero(addr6),
+ "Address should not be all zeroes");
+
+ clone = nl_addr_clone(addr6);
+ fail_if(clone == NULL,
+ "Cloned address should not be NULL");
+
+ fail_if(nl_addr_cmp(addr6, clone) != 0,
+ "Cloned address should not mismatch original");
+
+ fail_if(nl_addr_fill_sockaddr(addr6, (struct sockaddr *) &sin, &len) != 0,
+ "Should be able to fill socketaddr");
+
+ fail_if(strcmp(nl_addr2str(addr6, buf, sizeof(buf)), addr_str),
+ "Address translated back to string does not match original");
+
+ nl_addr_put(addr6);
+ nl_addr_put(clone);
+}
+END_TEST
+
+START_TEST(addr_info)
+{
+ struct nl_addr *addr;
+ char *addr_str = "127.0.0.1";
+ struct addrinfo *result;
+
+ fail_if(nl_addr_parse(addr_str, AF_UNSPEC, &addr) != 0,
+ "Parsing of valid address should not fail");
+
+ fail_if(nl_addr_info(addr, &result) != 0,
+ "getaddrinfo() on loopback address should work");
+
+ freeaddrinfo(result);
+ nl_addr_put(addr);
+}
+END_TEST
+
+Suite *make_nl_addr_suite(void)
+{
+ Suite *suite = suite_create("Abstract addresses");
+
+ TCase *tc_addr = tcase_create("Core");
+ tcase_add_test(tc_addr, addr_alloc);
+ tcase_add_test(tc_addr, addr_binary_addr);
+ tcase_add_test(tc_addr, addr_parse4);
+ tcase_add_test(tc_addr, addr_parse6);
+ tcase_add_test(tc_addr, addr_info);
+ suite_add_tcase(suite, tc_addr);
+
+ return suite;
+}
diff --git a/tests/check-all.c b/tests/check-all.c
new file mode 100644
index 00000000..e4318024
--- /dev/null
+++ b/tests/check-all.c
@@ -0,0 +1,44 @@
+/*
+ * tests/check-all.c overall unit test program
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation version 2.1
+ * of the License.
+ *
+ * Copyright (c) 2013 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include <check.h>
+
+extern Suite *make_nl_addr_suite(void);
+extern Suite *make_nl_attr_suite(void);
+
+static Suite *main_suite(void)
+{
+ Suite *suite = suite_create("main");
+
+ return suite;
+}
+
+int main(int argc, char *argv[])
+{
+ SRunner *runner;
+ int nfailed;
+
+ runner = srunner_create(main_suite());
+
+ /* Add testsuites below */
+
+ srunner_add_suite(runner, make_nl_addr_suite());
+ srunner_add_suite(runner, make_nl_attr_suite());
+
+ /* Do not add testsuites below this line */
+
+ srunner_run_all(runner, CK_ENV);
+
+ nfailed = srunner_ntests_failed(runner);
+ srunner_free(runner);
+
+ return nfailed != 0;
+}
diff --git a/tests/check-attr.c b/tests/check-attr.c
new file mode 100644
index 00000000..d8622301
--- /dev/null
+++ b/tests/check-attr.c
@@ -0,0 +1,88 @@
+/*
+ * tests/check-attr.c nla_attr unit tests
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation version 2.1
+ * of the License.
+ *
+ * Copyright (c) 2013 Thomas Graf <tgraf@suug.ch>
+ */
+
+#include "util.h"
+#include <netlink/attr.h>
+#include <netlink/msg.h>
+
+START_TEST(attr_size)
+{
+ fail_if(nla_attr_size(0) != NLA_HDRLEN,
+ "Length of empty attribute should match header size");
+ fail_if(nla_attr_size(1) != NLA_HDRLEN + 1,
+ "Length of 1 bytes payload should be NLA_HDRLEN + 1");
+ fail_if(nla_attr_size(2) != NLA_HDRLEN + 2,
+ "Length of 2 bytes payload should be NLA_HDRLEN + 2");
+ fail_if(nla_attr_size(3) != NLA_HDRLEN + 3,
+ "Length of 3 bytes payload should be NLA_HDRLEN + 3");
+ fail_if(nla_attr_size(4) != NLA_HDRLEN + 4,
+ "Length of 4 bytes payload should be NLA_HDRLEN + 4");
+
+ fail_if(nla_total_size(1) != NLA_HDRLEN + 4,
+ "Total size of 1 bytes payload should result in 8 bytes");
+ fail_if(nla_total_size(2) != NLA_HDRLEN + 4,
+ "Total size of 2 bytes payload should result in 8 bytes");
+ fail_if(nla_total_size(3) != NLA_HDRLEN + 4,
+ "Total size of 3 bytes payload should result in 8 bytes");
+ fail_if(nla_total_size(4) != NLA_HDRLEN + 4,
+ "Total size of 4 bytes payload should result in 8 bytes");
+
+ fail_if(nla_padlen(1) != 3,
+ "2 bytes of payload should result in 3 padding bytes");
+ fail_if(nla_padlen(2) != 2,
+ "2 bytes of payload should result in 2 padding bytes");
+ fail_if(nla_padlen(3) != 1,
+ "3 bytes of payload should result in 1 padding bytes");
+ fail_if(nla_padlen(4) != 0,
+ "4 bytes of payload should result in 0 padding bytes");
+ fail_if(nla_padlen(5) != 3,
+ "5 bytes of payload should result in 3 padding bytes");
+}
+END_TEST
+
+START_TEST(msg_construct)
+{
+ struct nl_msg *msg;
+ struct nlmsghdr *nlh;
+ struct nlattr *a;
+ int i, rem;
+
+ msg = nlmsg_alloc();
+ fail_if(!msg, "Unable to allocate netlink message");
+
+ for (i = 1; i < 256; i++) {
+ fail_if(nla_put_u32(msg, i, i+1) != 0,
+ "Unable to add attribute %d", i);
+ }
+
+ nlh = nlmsg_hdr(msg);
+ i = 1;
+ nlmsg_for_each_attr(a, nlh, 0, rem) {
+ fail_if(nla_type(a) != i, "Expected attribute %d", i);
+ i++;
+ fail_if(nla_get_u32(a) != i, "Expected attribute value %d", i);
+ }
+
+ nlmsg_free(msg);
+}
+END_TEST
+
+Suite *make_nl_attr_suite(void)
+{
+ Suite *suite = suite_create("Netlink attributes");
+
+ TCase *nl_attr = tcase_create("Core");
+ tcase_add_test(nl_attr, attr_size);
+ tcase_add_test(nl_attr, msg_construct);
+ suite_add_tcase(suite, nl_attr);
+
+ return suite;
+}
diff --git a/tests/test-cache-mngr.c b/tests/test-cache-mngr.c
index 777bce89..8999e587 100644
--- a/tests/test-cache-mngr.c
+++ b/tests/test-cache-mngr.c
@@ -1,16 +1,20 @@
-#include "../src/utils.h"
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/cli/utils.h>
#include <signal.h>
+#include <netlink-private/cache-api.h>
+
static int quit = 0;
+static struct nl_dump_params dp = {
+ .dp_type = NL_DUMP_LINE,
+};
+
+
static void change_cb(struct nl_cache *cache, struct nl_object *obj,
- int action)
+ int action, void *data)
{
- struct nl_dump_params dp = {
- .dp_type = NL_DUMP_LINE,
- .dp_fd = stdout,
- };
-
if (action == NL_ACT_NEW)
printf("NEW ");
else if (action == NL_ACT_DEL)
@@ -29,43 +33,34 @@ static void sigint(int arg)
int main(int argc, char *argv[])
{
struct nl_cache_mngr *mngr;
- struct nl_cache *lc, *nc, *ac, *rc;
- struct nl_sock *sock;
- int err;
+ struct nl_cache *cache;
+ int err, i;
+
+ dp.dp_fd = stdout;
signal(SIGINT, sigint);
- sock = nlt_alloc_socket();
- err = nl_cache_mngr_alloc(sock, NETLINK_ROUTE, NL_AUTO_PROVIDE, &mngr);
+ err = nl_cache_mngr_alloc(NULL, NETLINK_ROUTE, NL_AUTO_PROVIDE, &mngr);
if (err < 0)
- fatal(err, "Unable to allocate cache manager: %s",
- nl_geterror(err));
-
- if ((err = nl_cache_mngr_add(mngr, "route/link", &change_cb, &lc)) < 0)
- fatal(err, "Unable to add cache route/link: %s",
- nl_geterror(err));
-
- if ((err = nl_cache_mngr_add(mngr, "route/neigh", &change_cb, &nc)) < 0)
- fatal(err, "Unable to add cache route/neigh: %s",
- nl_geterror(err));
-
- if ((err = nl_cache_mngr_add(mngr, "route/addr", &change_cb, &ac)) < 0)
- fatal(err, "Unable to add cache route/addr: %s",
- nl_geterror(err));
-
- if ((err = nl_cache_mngr_add(mngr, "route/route", &change_cb, &rc)) < 0)
- fatal(err, "Unable to add cache route/route: %s",
- nl_geterror(err));
+ nl_cli_fatal(err, "Unable to allocate cache manager: %s",
+ nl_geterror(err));
+
+ for (i = 1; i < argc; i++) {
+ err = nl_cache_mngr_add(mngr, argv[i], &change_cb, NULL, &cache);
+ if (err < 0)
+ nl_cli_fatal(err, "Unable to add cache %s: %s",
+ argv[i], nl_geterror(err));
+ }
while (!quit) {
- int err = nl_cache_mngr_poll(mngr, 5000);
+ int err = nl_cache_mngr_poll(mngr, 1000);
if (err < 0 && err != -NLE_INTR)
- fatal(err, "Polling failed: %s", nl_geterror(err));
+ nl_cli_fatal(err, "Polling failed: %s", nl_geterror(err));
+ nl_cache_mngr_info(mngr, &dp);
}
nl_cache_mngr_free(mngr);
- nl_socket_free(sock);
return 0;
}
diff --git a/tests/test-complex-HTB-with-hash-filters.c b/tests/test-complex-HTB-with-hash-filters.c
new file mode 100644
index 00000000..48cf5e32
--- /dev/null
+++ b/tests/test-complex-HTB-with-hash-filters.c
@@ -0,0 +1,761 @@
+/*
+ * test/test-complex-HTB-with-hash-filters.c Add HTB qdisc, HTB classes and creates some hash filters
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation version 2.1
+ * of the License.
+ *
+ * Copyright (c) 2011 Adrian Ban <adrian.ban@mantech.ro>
+ */
+
+#include <netlink/route/link.h>
+#include <netlink/route/tc.h>
+#include <netlink/route/qdisc.h>
+#include <netlink/route/qdisc/htb.h>
+#include <netlink/route/qdisc/sfq.h>
+#include <netlink/route/cls/u32.h>
+#include <netlink/route/classifier.h>
+#include <netlink/route/class.h>
+#include <linux/if_ether.h>
+
+#include <netlink/attr.h>
+//#include "include/rtnl_u32.h"
+
+#include <stdio.h>
+#include <string.h>
+//#include "include/rtnl_u32_addon.h"
+
+#define TC_HANDLE(maj, min) (TC_H_MAJ((maj) << 16) | TC_H_MIN(min))
+
+/* some functions are copied from iproute-tc tool */
+int get_u32(__u32 *val, const char *arg, int base)
+{
+ unsigned long res;
+ char *ptr;
+
+ if (!arg || !*arg)
+ return -1;
+ res = strtoul(arg, &ptr, base);
+ if (!ptr || ptr == arg || *ptr || res > 0xFFFFFFFFUL)
+ return -1;
+ *val = res;
+ return 0;
+}
+
+int get_u32_handle(__u32 *handle, const char *str)
+{
+ __u32 htid=0, hash=0, nodeid=0;
+ char *tmp = strchr(str, ':');
+
+ if (tmp == NULL) {
+ if (memcmp("0x", str, 2) == 0)
+ return get_u32(handle, str, 16);
+ return -1;
+ }
+ htid = strtoul(str, &tmp, 16);
+ if (tmp == str && *str != ':' && *str != 0)
+ return -1;
+ if (htid>=0x1000)
+ return -1;
+ if (*tmp) {
+ str = tmp+1;
+ hash = strtoul(str, &tmp, 16);
+ if (tmp == str && *str != ':' && *str != 0)
+ return -1;
+ if (hash>=0x100)
+ return -1;
+ if (*tmp) {
+ str = tmp+1;
+ nodeid = strtoul(str, &tmp, 16);
+ if (tmp == str && *str != 0)
+ return -1;
+ if (nodeid>=0x1000)
+ return -1;
+ }
+ }
+ *handle = (htid<<20)|(hash<<12)|nodeid;
+ return 0;
+}
+
+uint32_t get_u32_parse_handle(const char *cHandle)
+{
+ uint32_t handle=0;
+
+ if(get_u32_handle(&handle, cHandle)) {
+ printf ("Illegal \"ht\"\n");
+ return -1;
+ }
+
+ if (handle && TC_U32_NODE(handle)) {
+ printf("\"link\" must be a hash table.\n");
+ return -1;
+ }
+ return handle;
+}
+
+int get_tc_classid(__u32 *h, const char *str)
+{
+ __u32 maj, min;
+ char *p;
+
+ maj = TC_H_ROOT;
+ if (strcmp(str, "root") == 0)
+ goto ok;
+ maj = TC_H_UNSPEC;
+ if (strcmp(str, "none") == 0)
+ goto ok;
+ maj = strtoul(str, &p, 16);
+ if (p == str) {
+ maj = 0;
+ if (*p != ':')
+ return -1;
+ }
+ if (*p == ':') {
+ if (maj >= (1<<16))
+ return -1;
+ maj <<= 16;
+ str = p+1;
+ min = strtoul(str, &p, 16);
+ if (*p != 0)
+ return -1;
+ if (min >= (1<<16))
+ return -1;
+ maj |= min;
+ } else if (*p != 0)
+ return -1;
+
+ok:
+ *h = maj;
+ return 0;
+}
+
+/*
+ * Function that adds a new filter and attach it to a hash table
+ *
+ */
+int u32_add_filter_on_ht(struct nl_sock *sock, struct rtnl_link *rtnlLink, uint32_t prio,
+ uint32_t keyval, uint32_t keymask, int keyoff, int keyoffmask,
+ uint32_t htid, uint32_t classid
+)
+{
+ struct rtnl_cls *cls;
+ int err;
+
+ //printf("Key Val : 0x%x\n", keyval);
+ //printf("Key Mask : 0x%x\n", keymask);
+
+ cls=rtnl_cls_alloc();
+ if (!(cls)) {
+ printf("Can not allocate classifier\n");
+ nl_socket_free(sock);
+ exit(1);
+ }
+
+ rtnl_tc_set_link(TC_CAST(cls), rtnlLink);
+
+ if ((err = rtnl_tc_set_kind(TC_CAST(cls), "u32"))) {
+ printf("Can not set classifier as u32\n");
+ return 1;
+ }
+
+ rtnl_cls_set_prio(cls, prio);
+ rtnl_cls_set_protocol(cls, ETH_P_IP);
+
+ rtnl_tc_set_parent(TC_CAST(cls), TC_HANDLE(1, 0));
+
+ rtnl_u32_set_hashtable(cls, htid);
+
+ rtnl_u32_add_key_uint32(cls, keyval, keymask, keyoff, keyoffmask); /* 10.0.0.0/8 */
+
+ rtnl_u32_set_classid(cls, classid);
+
+ rtnl_u32_set_cls_terminal(cls);
+
+ if ((err = rtnl_cls_add(sock, cls, NLM_F_CREATE))) {
+ printf("Can not add classifier: %s\n", nl_geterror(err));
+ return -1;
+ }
+ rtnl_cls_put(cls);
+ return 0;
+
+}
+
+/*
+ * Function that adds a new filter and attach it to a hash table
+ * and set next hash table link with hash mask
+ *
+ */
+int u32_add_filter_on_ht_with_hashmask(struct nl_sock *sock, struct rtnl_link *rtnlLink, uint32_t prio,
+ uint32_t keyval, uint32_t keymask, int keyoff, int keyoffmask,
+ uint32_t htid, uint32_t htlink, uint32_t hmask, uint32_t hoffset
+)
+{
+ struct rtnl_cls *cls;
+ int err;
+
+ //printf("Key Val : 0x%x\n", keyval);
+ //printf("Key Mask : 0x%x\n", keymask);
+
+ cls=rtnl_cls_alloc();
+ if (!(cls)) {
+ printf("Can not allocate classifier\n");
+ nl_socket_free(sock);
+ exit(1);
+ }
+
+ rtnl_tc_set_link(TC_CAST(cls), rtnlLink);
+
+ if ((err = rtnl_tc_set_kind(TC_CAST(cls), "u32"))) {
+ printf("Can not set classifier as u32\n");
+ return 1;
+ }
+
+ rtnl_cls_set_prio(cls, prio);
+ rtnl_cls_set_protocol(cls, ETH_P_IP);
+
+ rtnl_tc_set_parent(TC_CAST(cls), TC_HANDLE(1, 0));
+
+ if (htid)
+ rtnl_u32_set_hashtable(cls, htid);
+
+ rtnl_u32_add_key_uint32(cls, keyval, keymask, keyoff, keyoffmask);
+
+ rtnl_u32_set_hashmask(cls, hmask, hoffset);
+
+ rtnl_u32_set_link(cls, htlink);
+
+
+ if ((err = rtnl_cls_add(sock, cls, NLM_F_CREATE))) {
+ printf("Can not add classifier: %s\n", nl_geterror(err));
+ return -1;
+ }
+ rtnl_cls_put(cls);
+ return 0;
+}
+
+/*
+ * function that creates a new hash table
+ */
+int u32_add_ht(struct nl_sock *sock, struct rtnl_link *rtnlLink, uint32_t prio, uint32_t htid, uint32_t divisor)
+{
+
+ int err;
+ struct rtnl_cls *cls;
+
+ cls=rtnl_cls_alloc();
+ if (!(cls)) {
+ printf("Can not allocate classifier\n");
+ nl_socket_free(sock);
+ exit(1);
+ }
+
+ rtnl_tc_set_link(TC_CAST(cls), rtnlLink);
+
+ if ((err = rtnl_tc_set_kind(TC_CAST(cls), "u32"))) {
+ printf("Can not set classifier as u32\n");
+ return 1;
+ }
+
+ rtnl_cls_set_prio(cls, prio);
+ rtnl_cls_set_protocol(cls, ETH_P_IP);
+ rtnl_tc_set_parent(TC_CAST(cls), TC_HANDLE(1, 0));
+
+ rtnl_u32_set_handle(cls, htid, 0x0, 0x0);
+ //printf("htid: 0x%X\n", htid);
+ rtnl_u32_set_divisor(cls, divisor);
+
+ if ((err = rtnl_cls_add(sock, cls, NLM_F_CREATE))) {
+ printf("Can not add classifier: %s\n", nl_geterror(err));
+ return -1;
+ }
+ rtnl_cls_put(cls);
+ return 0;
+}
+
+/*
+ * function that adds a new HTB qdisc and set the default class for unclassified traffic
+ */
+int qdisc_add_HTB(struct nl_sock *sock, struct rtnl_link *rtnlLink, uint32_t defaultClass)
+{
+
+ struct rtnl_qdisc *qdisc;
+ int err;
+
+ /* Allocation of a qdisc object */
+ if (!(qdisc = rtnl_qdisc_alloc())) {
+ printf("Can not allocate Qdisc\n");
+ return -1;
+ }
+
+ //rtnl_tc_set_ifindex(TC_CAST(qdisc), master_index);
+ rtnl_tc_set_link(TC_CAST(qdisc), rtnlLink);
+ rtnl_tc_set_parent(TC_CAST(qdisc), TC_H_ROOT);
+
+ //delete the qdisc
+ //printf("Delete current qdisc\n");
+ rtnl_qdisc_delete(sock, qdisc);
+ //rtnl_qdisc_put(qdisc);
+
+ //add a HTB qdisc
+ //printf("Add a new HTB qdisc\n");
+ rtnl_tc_set_handle(TC_CAST(qdisc), TC_HANDLE(1,0));
+
+ if ((err = rtnl_tc_set_kind(TC_CAST(qdisc), "htb"))) {
+ printf("Can not allocate HTB\n");
+ return -1;
+ }
+
+ /* Set default class for unclassified traffic */
+ //printf("Set default class for unclassified traffic\n");
+ rtnl_htb_set_defcls(qdisc, TC_HANDLE(1, defaultClass));
+ rtnl_htb_set_rate2quantum(qdisc, 1);
+
+ /* Submit request to kernel and wait for response */
+ if ((err = rtnl_qdisc_add(sock, qdisc, NLM_F_CREATE))) {
+ printf("Can not allocate HTB Qdisc\n");
+ return -1;
+ }
+
+ /* Return the qdisc object to free memory resources */
+ rtnl_qdisc_put(qdisc);
+
+ return 0;
+}
+
+/*
+ * function that adds a new HTB class and set its parameters
+ */
+int class_add_HTB(struct nl_sock *sock, struct rtnl_link *rtnlLink,
+ uint32_t parentMaj, uint32_t parentMin,
+ uint32_t childMaj, uint32_t childMin,
+ uint64_t rate, uint64_t ceil,
+ uint32_t burst, uint32_t cburst,
+ uint32_t prio
+)
+{
+ int err;
+ struct rtnl_class *class;
+ //struct rtnl_class *class = (struct rtnl_class *) tc;
+
+ //create a HTB class
+ //class = (struct rtnl_class *)rtnl_class_alloc();
+ if (!(class = rtnl_class_alloc())) {
+ printf("Can not allocate class object\n");
+ return 1;
+ }
+ //
+ rtnl_tc_set_link(TC_CAST(class), rtnlLink);
+ //add a HTB qdisc
+ //printf("Add a new HTB class with 0x%X:0x%X on parent 0x%X:0x%X\n", childMaj, childMin, parentMaj, parentMin);
+ rtnl_tc_set_parent(TC_CAST(class), TC_HANDLE(parentMaj, parentMin));
+ rtnl_tc_set_handle(TC_CAST(class), TC_HANDLE(childMaj, childMin));
+
+ if ((err = rtnl_tc_set_kind(TC_CAST(class), "htb"))) {
+ printf("Can not set HTB to class\n");
+ return 1;
+ }
+
+ //printf("set HTB class prio to %u\n", prio);
+ rtnl_htb_set_prio((struct rtnl_class *)class, prio);
+
+ if (rate) {
+ //rate=rate/8;
+ rtnl_htb_set_rate(class, rate);
+ }
+ if (ceil) {
+ //ceil=ceil/8;
+ rtnl_htb_set_ceil(class, ceil);
+ }
+
+ if (burst) {
+ //printf ("Class HTB: set rate burst: %u\n", burst);
+ rtnl_htb_set_rbuffer(class, burst);
+ }
+ if (cburst) {
+ //printf ("Class HTB: set rate cburst: %u\n", cburst);
+ rtnl_htb_set_cbuffer(class, cburst);
+ }
+ /* Submit request to kernel and wait for response */
+ if ((err = rtnl_class_add(sock, class, NLM_F_CREATE))) {
+ printf("Can not allocate HTB Qdisc\n");
+ return 1;
+ }
+ rtnl_class_put(class);
+ return 0;
+}
+
+/*
+ * function that adds a HTB root class and set its parameters
+ */
+int class_add_HTB_root(struct nl_sock *sock, struct rtnl_link *rtnlLink,
+ uint64_t rate, uint64_t ceil,
+ uint32_t burst, uint32_t cburst
+)
+{
+ int err;
+ struct rtnl_class *class;
+
+ //create a HTB class
+ class = (struct rtnl_class *)rtnl_class_alloc();
+ //class = rtnl_class_alloc();
+ if (!class) {
+ printf("Can not allocate class object\n");
+ return 1;
+ }
+ //
+ rtnl_tc_set_link(TC_CAST(class), rtnlLink);
+ rtnl_tc_set_parent(TC_CAST(class), TC_H_ROOT);
+ //add a HTB class
+ //printf("Add a new HTB ROOT class\n");
+ rtnl_tc_set_handle(TC_CAST(class), 1);
+
+ if ((err = rtnl_tc_set_kind(TC_CAST(class), "htb"))) {
+ printf("Can not set HTB to class\n");
+ return 1;
+ }
+
+ if (rate) {
+ //rate=rate/8;
+ rtnl_htb_set_rate(class, rate);
+ }
+ if (ceil) {
+ //ceil=ceil/8;
+ rtnl_htb_set_ceil(class, ceil);
+ }
+
+ if (burst) {
+ rtnl_htb_set_rbuffer(class, burst);
+ }
+ if (cburst) {
+ rtnl_htb_set_cbuffer(class, cburst);
+ }
+
+ /* Submit request to kernel and wait for response */
+ if ((err = rtnl_class_add(sock, class, NLM_F_CREATE))) {
+ printf("Can not allocate HTB Qdisc\n");
+ return 1;
+ }
+ rtnl_class_put(class);
+ return 0;
+}
+
+/*
+ * function that adds a new SFQ qdisc as a leaf for a HTB class
+ */
+int qdisc_add_SFQ_leaf(struct nl_sock *sock, struct rtnl_link *rtnlLink,
+ uint32_t parentMaj, uint32_t parentMin,
+ int quantum, int limit, int perturb
+)
+{
+ int err;
+ struct rtnl_qdisc *qdisc;
+
+ if (!(qdisc = rtnl_qdisc_alloc())) {
+ printf("Can not allocate qdisc object\n");
+ return 1;
+ }
+ rtnl_tc_set_link(TC_CAST(qdisc), rtnlLink);
+ rtnl_tc_set_parent(TC_CAST(qdisc), TC_HANDLE(parentMaj, parentMin));
+
+ rtnl_tc_set_handle(TC_CAST(qdisc), TC_HANDLE(parentMin,0));
+
+ if ((err = rtnl_tc_set_kind(TC_CAST(qdisc), "sfq"))) {
+ printf("Can not set SQF class\n");
+ return 1;
+ }
+
+ if(quantum) {
+ rtnl_sfq_set_quantum(qdisc, quantum);
+ } else {
+ rtnl_sfq_set_quantum(qdisc, 16000); // tc default value
+ }
+ if(limit) {
+ rtnl_sfq_set_limit(qdisc, limit); // default is 127
+ }
+ if(perturb) {
+ rtnl_sfq_set_perturb(qdisc, perturb); // default never perturb the hash
+ }
+
+ /* Submit request to kernel and wait for response */
+ if ((err = rtnl_qdisc_add(sock, qdisc, NLM_F_CREATE))) {
+ printf("Can not allocate SFQ qdisc\n");
+ return -1;
+ }
+
+ /* Return the qdisc object to free memory resources */
+ rtnl_qdisc_put(qdisc);
+ return 0;
+}
+
+
+
+
+int main() {
+
+ struct nl_sock *sock;
+ struct rtnl_link *link;
+
+ //struct rtnl_qdisc *qdisc;
+ //struct rtnl_class *class;
+ //struct rtnl_cls *cls;
+
+ uint32_t ht, htlink, htid, direction, classid;
+ //uint32_t hash, hashmask, nodeid, divisor, handle;
+ //struct rtnl_u32 *f_u32;
+ char chashlink[16]="";
+
+ //uint64_t drops, qlen;
+
+ //int master_index;
+ int err;
+
+ //uint64_t rate=0, ceil=0;
+
+ struct nl_cache *link_cache;
+
+ if (!(sock = nl_socket_alloc())) {
+ printf("Unable to allocate netlink socket\n");
+ exit(1);
+ }
+
+ if ((err = nl_connect(sock, NETLINK_ROUTE)) < 0 ) {
+ printf("Nu s-a putut conecta la NETLINK!\n");
+ nl_socket_free(sock);
+ exit(1);
+ }
+
+
+ if ((err = rtnl_link_alloc_cache(sock, AF_UNSPEC, &link_cache)) < 0) {
+ printf("Unable to allocate link cache: %s\n",
+ nl_geterror(err));
+ nl_socket_free(sock);
+ exit(1);
+ }
+
+ /* lookup interface index of eth0 */
+ if (!(link = rtnl_link_get_by_name(link_cache, "imq0"))) {
+ /* error */
+ printf("Interface not found\n");
+ nl_socket_free(sock);
+ exit(1);
+ }
+
+ err=qdisc_add_HTB(sock, link, 0xffff);
+ //drops = rtnl_tc_get_stat(TC_CAST(qdisc), RTNL_TC_DROPS);
+
+ //printf("Add ROOT HTB class\n");
+ err=class_add_HTB_root(sock, link, 12500000, 12500000, 25000, 25000);
+ err=class_add_HTB(sock, link, 1, 0, 1, 0xffff, 1250000, 12500000, 25000, 25000, 5);
+ err=qdisc_add_SFQ_leaf(sock, link, 1, 0xffff, 16000, 0, 10);
+ err=class_add_HTB(sock, link, 1, 1, 1, 0x5, 2000000, 2000000, 25000, 25000, 5);
+ err=qdisc_add_SFQ_leaf(sock, link, 1, 0x5, 16000, 0, 10);
+ err=class_add_HTB(sock, link, 1, 1, 1, 0x6, 1000000, 1000000, 25000, 25000, 5);
+ err=qdisc_add_SFQ_leaf(sock, link, 1, 0x6, 16000, 0, 10);
+ //err=class_add_HTB(sock, link, 1, 0, 1, 0x7, 1024000, 100000000, 5);
+ //err=class_add_HTB(sock, link, 1, 0, 1, 0x8, 2048000, 100000000, 5);
+ //err=class_add_HTB(sock, link, 1, 0, 1, 0x9, 4096000, 100000000, 5);
+ //err=class_add_HTB(sock, link, 1, 0, 1, 0xa, 8192000, 100000000, 5);
+
+ //printf("Add main hash table\n");
+
+ /* create u32 first hash filter table
+ *
+ */
+ /* formula calcul handle:
+ * uint32_t handle = (htid << 20) | (hash << 12) | nodeid;
+ */
+
+ /*
+ * Upper limit of number of hash tables: 4096 (0xFFF)
+ * Number of hashes in a table: 256 values (0xFF)
+ *
+ */
+
+ /* using 256 values for hash table
+ * each entry in hash table match a byte from IP address specified later by a hash key
+ */
+
+ uint32_t i;
+ for (i = 1; i <= 0xf; i++)
+ u32_add_ht(sock, link, 1, i, 256);
+
+ /*
+ * attach a u32 filter to the first hash
+ * that redirects all traffic and make a hash key
+ * from the fist byte of the IP address
+ *
+ */
+
+ //divisor=0x0; // unused here
+ //handle = 0x0; // unused here
+ //hash = 0x0; // unused here
+ //htid = 0x0; // unused here
+ //nodeid = 0x0; // unused here
+
+ // direction = 12 -> source IP
+ // direction = 16 -> destination IP
+ direction = 16;
+
+ /*
+ * which hash table will use
+ * in our case is hash table no 1 defined previous
+ *
+ * There are 2 posibilities to set the the hash table:
+ * 1. Using function get_u32_handle and sent a string in
+ * format 10: where 10 is number of the hash table
+ * 2. Create your own value in format: 0xa00000
+ *
+ */
+ strcpy(chashlink, "1:");
+ //printf("Hash Link: %s\n", chashlink);
+ //chashlink=malloc(sizeof(char) *
+ htlink = 0x0; // is used by get_u32_handle to return the correct value of hash table (link)
+
+ if(get_u32_handle(&htlink, chashlink)) {
+ printf ("Illegal \"link\"");
+ nl_socket_free(sock);
+ exit(1);
+ }
+ //printf ("hash link : 0x%X\n", htlink);
+ //printf ("hash link test : %u\n", (htlink && TC_U32_NODE(htlink)));
+
+ if (htlink && TC_U32_NODE(htlink)) {
+ printf("\"link\" must be a hash table.\n");
+ nl_socket_free(sock);
+ exit(1);
+ }
+ /* the hash mask will hit the hash table (link) no 1: in our case
+ */
+
+ /* set the hash key mask */
+ //hashmask = 0xFF000000UL; // the mask that is used to match the hash in specific table, in our case for example 1:a with mean the first byte which is 10 in hash table 1
+
+ /* Here we add a hash filter which match the first byte (see the hashmask value)
+ * of the source IP (offset 12 in the packet header)
+ * You can use also offset 16 to match the destination IP
+ */
+
+ /*
+ * Also we need a filter to match our rule
+ * This mean that we will put a 0.0.0.0/0 filter in our first rule
+ * that match the offset 12 (source IP)
+ * Also you can put offset 16 to match the destination IP
+ */
+
+ u32_add_filter_on_ht_with_hashmask(sock, link, 1,
+ 0x0, 0x0, direction, 0,
+ 0, htlink, 0xff000000, direction);
+
+ /*
+ * For each first byte that we need to match we will create a new hash table
+ * For example: you have those clases: 10.0.0.0/24 and 172.16.0.0/23
+ * For byte 10 and byte 172 will create a separate hash table that will match the second
+ * byte from each class.
+ *
+ */
+
+
+ // Create a new hash table with prio 1, id 2 and 256 entries
+// u32_CreateNewHashTable(sock, link, 1, 2, 256);
+ // Create a new hash table with prio 1, id 3 and 256 entries
+// u32_CreateNewHashTable(sock, link, 1, 3, 256);
+// u32_CreateNewHashTable(sock, link, 1, 4, 256);
+// u32_CreateNewHashTable(sock, link, 1, 5, 256);
+
+ /*
+ * Now we will create other filter under (ATENTION) our first hash table (link) 1:
+ * Previous rule redirects the trafic according the hash mask to hash table (link) no 1:
+ * Here we will match the hash tables from 1:0 to 1:ff. Under each hash table we will attach
+ * other rules that matches next byte from IP source/destination IP and we will repeat the
+ * previous steps.
+ *
+ */
+
+
+ // /8 check
+
+ // 10.0.0.0/8
+ ht=get_u32_parse_handle("1:a:");
+ htid = (ht&0xFFFFF000);
+ htlink=get_u32_parse_handle("2:");
+
+ u32_add_filter_on_ht_with_hashmask(sock, link, 1,
+ 0x0a000000, 0xff000000, direction, 0,
+ htid, htlink, 0x00ff0000, direction);
+
+ // 172.0.0.0/8
+ ht=get_u32_parse_handle("1:ac:");
+ htid = (ht&0xFFFFF000);
+ htlink=get_u32_parse_handle("3:");
+
+ u32_add_filter_on_ht_with_hashmask(sock, link, 1,
+ 0xac000000, 0xff000000, direction, 0,
+ htid, htlink, 0x00ff0000, direction);
+
+
+ // /16 check
+ // 10.0.0.0/16
+ ht=get_u32_parse_handle("2:0:");
+ htid = (ht&0xFFFFF000);
+ htlink=get_u32_parse_handle("4:");
+
+ u32_add_filter_on_ht_with_hashmask(sock, link, 1,
+ 0x0a000000, 0xffff0000, direction, 0,
+ htid, htlink, 0x0000ff00, direction);
+
+ // 172.17.0.0/16
+ ht=get_u32_parse_handle("3:11:");
+ htid = (ht&0xFFFFF000);
+ htlink=get_u32_parse_handle("5:");
+
+ u32_add_filter_on_ht_with_hashmask(sock, link, 1,
+ 0xac110000, 0xffff0000, direction, 0,
+ htid, htlink, 0x0000ff00, direction);
+
+ // /24 check
+ // 10.0.9.0/24
+ ht=get_u32_parse_handle("4:9:");
+ htid = (ht&0xFFFFF000);
+ htlink=get_u32_parse_handle("6:");
+
+ u32_add_filter_on_ht_with_hashmask(sock, link, 1,
+ 0x0a000900, 0xffffff00, direction, 0,
+ htid, htlink, 0x000000ff, direction);
+
+ // 172.17.2.0/16
+ ht=get_u32_parse_handle("5:2:");
+ htid = (ht&0xFFFFF000);
+ htlink=get_u32_parse_handle("7:");
+
+ u32_add_filter_on_ht_with_hashmask(sock, link, 1,
+ 0xac110200, 0xffffff00, direction, 0,
+ htid, htlink, 0x000000ff, direction);
+
+
+ // final filters
+ // 10.0.9.20
+ ht=get_u32_parse_handle("6:14:");
+ htid = (ht&0xFFFFF000);
+
+ err = get_tc_classid(&classid, "1:5");
+
+ u32_add_filter_on_ht(sock, link, 1,
+ 0x0a000914, 0xffffffff, direction, 0,
+ htid, classid);
+
+ // 172.17.2.120
+ ht=get_u32_parse_handle("7:78:");
+ htid = (ht&0xFFFFF000);
+
+ err = get_tc_classid(&classid, "1:6");
+
+ u32_add_filter_on_ht(sock, link, 1,
+ 0xac110278, 0xffffffff, direction, 0,
+ htid, classid);
+
+
+
+ nl_socket_free(sock);
+ return 0;
+}
diff --git a/tests/test-create-bond.c b/tests/test-create-bond.c
new file mode 100644
index 00000000..11bc5b09
--- /dev/null
+++ b/tests/test-create-bond.c
@@ -0,0 +1,29 @@
+#include <netlink/netlink.h>
+#include <netlink/route/link.h>
+#include <netlink/route/link/bonding.h>
+
+int main(int argc, char *argv[])
+{
+ struct rtnl_link *link;
+ struct nl_sock *sk;
+ int err;
+
+ sk = nl_socket_alloc();
+ if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
+ nl_perror(err, "Unable to connect socket");
+ return err;
+ }
+
+ link = rtnl_link_bond_alloc();
+ rtnl_link_set_name(link, "my_bond");
+
+ if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) {
+ nl_perror(err, "Unable to add link");
+ return err;
+ }
+
+ rtnl_link_put(link);
+ nl_close(sk);
+
+ return 0;
+}
diff --git a/tests/test-create-bridge.c b/tests/test-create-bridge.c
new file mode 100644
index 00000000..7202cd7e
--- /dev/null
+++ b/tests/test-create-bridge.c
@@ -0,0 +1,80 @@
+#include <netlink/netlink.h>
+#include <netlink/route/link.h>
+#include <netlink/route/link/bridge.h>
+
+#define TEST_BRIDGE_NAME "testbridge"
+#define TEST_INTERFACE_NAME "testtap1"
+
+int create_bridge(struct nl_sock *sk, struct nl_cache *link_cache, const char *name) {
+ struct rtnl_link *link;
+ int err;
+
+ link = rtnl_link_alloc();
+ if ((err = rtnl_link_set_type(link, "bridge")) < 0) {
+ rtnl_link_put(link);
+ return err;
+ }
+ rtnl_link_set_name(link, name);
+
+ if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) {
+ return err;
+ }
+ rtnl_link_put(link);
+
+ return 0;
+}
+
+int main(int argc, char *argv[])
+{
+ struct rtnl_link *link;
+ struct nl_cache *link_cache;
+ struct nl_sock *sk;
+ int err;
+
+ sk = nl_socket_alloc();
+ if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
+ nl_perror(err, "Unable to connect socket");
+ return err;
+ }
+
+ if ((err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache)) < 0) {
+ nl_perror(err, "Unable to allocate cache");
+ return err;
+ }
+
+ if ((err = create_bridge(sk, link_cache, TEST_BRIDGE_NAME)) < 0) {
+ nl_perror(err, "Unable to allocate testbridge");
+ return err;
+ }
+
+ nl_cache_refill(sk, link_cache);
+
+ link = rtnl_link_get_by_name(link_cache, TEST_BRIDGE_NAME);
+ struct rtnl_link *ltap = rtnl_link_get_by_name(link_cache, TEST_INTERFACE_NAME);
+ if (!ltap) {
+ fprintf(stderr, "You should create a tap interface before lunch this test (# tunctl -t %s)\n", TEST_INTERFACE_NAME);
+ return -1;
+ }
+
+ if ((err = rtnl_link_enslave(sk, link, ltap)) < 0) {
+ nl_perror(err, "Unable to enslave interface to his bridge\n");
+ return err;
+ }
+
+ if(rtnl_link_is_bridge(link) == 0) {
+ fprintf(stderr, "Link is not a bridge\n");
+ return -2;
+ }
+ if(rtnl_link_get_master(ltap) <= 0) {
+ fprintf(stderr, "Interface is not attached to a bridge\n");
+ return -3;
+ }
+
+ rtnl_link_put(ltap);
+ rtnl_link_put(link);
+
+ nl_cache_free(link_cache);
+ nl_socket_free(sk);
+
+ return 0;
+}
diff --git a/tests/test-create-ip6tnl.c b/tests/test-create-ip6tnl.c
new file mode 100644
index 00000000..b36ab3d3
--- /dev/null
+++ b/tests/test-create-ip6tnl.c
@@ -0,0 +1,55 @@
+#include <netlink/route/link/ip6tnl.h>
+#include <netlink-private/netlink.h>
+
+int main(int argc, char *argv[])
+{
+ struct nl_cache *link_cache;
+ struct rtnl_link *link;
+ struct in6_addr addr;
+ struct nl_sock *sk;
+ int err, if_index;
+
+ sk = nl_socket_alloc();
+ if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
+ nl_perror(err, "Unable to connect socket");
+ return err;
+ }
+
+ err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache);
+ if ( err < 0) {
+ nl_perror(err, "Unable to allocate cache");
+ return err;
+ }
+
+ if_index = rtnl_link_name2i(link_cache, "ens33");
+ if (!if_index) {
+ fprintf(stderr, "Unable to lookup ens33");
+ return -1;
+ }
+
+ link = rtnl_link_ip6_tnl_alloc();
+ if(!link) {
+ nl_perror(err, "Unable to allocate link");
+ return -1;
+
+ }
+ rtnl_link_set_name(link, "ip6tnl-tun");
+ rtnl_link_ip6_tnl_set_link(link, if_index);
+
+ inet_pton(AF_INET6, "2607:f0d0:1002:51::4", &addr);
+ rtnl_link_ip6_tnl_set_local(link, &addr);
+
+ inet_pton(AF_INET6, "2607:f0d0:1002:52::5", &addr);
+ rtnl_link_ip6_tnl_set_remote(link, &addr);
+
+ err = rtnl_link_add(sk, link, NLM_F_CREATE);
+ if (err < 0) {
+ nl_perror(err, "Unable to add link");
+ return err;
+ }
+
+ rtnl_link_put(link);
+ nl_close(sk);
+
+ return 0;
+}
diff --git a/tests/test-create-ipgre.c b/tests/test-create-ipgre.c
new file mode 100644
index 00000000..66ea6da8
--- /dev/null
+++ b/tests/test-create-ipgre.c
@@ -0,0 +1,56 @@
+#include <netlink/route/link/ipgre.h>
+#include <netlink-private/netlink.h>
+
+int main(int argc, char *argv[])
+{
+ struct nl_cache *link_cache;
+ struct rtnl_link *link;
+ struct in_addr addr;
+ struct nl_sock *sk;
+ int err, if_index;
+
+ sk = nl_socket_alloc();
+ if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
+ nl_perror(err, "Unable to connect socket");
+ return err;
+ }
+
+ err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache);
+ if ( err < 0) {
+ nl_perror(err, "Unable to allocate cache");
+ return err;
+ }
+
+ if_index = rtnl_link_name2i(link_cache, "eno16777736");
+ if (!if_index) {
+ fprintf(stderr, "Unable to lookup eno16777736");
+ return -1;
+ }
+
+ link = rtnl_link_ipgre_alloc();
+ if(!link) {
+ nl_perror(err, "Unable to allocate link");
+ return -1;
+
+ }
+ rtnl_link_set_name(link, "ipgre-tun");
+ rtnl_link_ipgre_set_link(link, if_index);
+
+ inet_pton(AF_INET, "192.168.254.12", &addr.s_addr);
+ rtnl_link_ipgre_set_local(link, addr.s_addr);
+
+ inet_pton(AF_INET, "192.168.254.13", &addr.s_addr);
+ rtnl_link_ipgre_set_remote(link, addr.s_addr);
+
+ rtnl_link_ipgre_set_ttl(link, 64);
+ err = rtnl_link_add(sk, link, NLM_F_CREATE);
+ if (err < 0) {
+ nl_perror(err, "Unable to add link");
+ return err;
+ }
+
+ rtnl_link_put(link);
+ nl_close(sk);
+
+ return 0;
+}
diff --git a/tests/test-create-ipip.c b/tests/test-create-ipip.c
new file mode 100644
index 00000000..44b9b2c3
--- /dev/null
+++ b/tests/test-create-ipip.c
@@ -0,0 +1,56 @@
+#include <netlink/route/link/ipip.h>
+#include <netlink-private/netlink.h>
+
+int main(int argc, char *argv[])
+{
+ struct nl_cache *link_cache;
+ struct rtnl_link *link;
+ struct in_addr addr;
+ struct nl_sock *sk;
+ int err, if_index;
+
+ sk = nl_socket_alloc();
+ if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
+ nl_perror(err, "Unable to connect socket");
+ return err;
+ }
+
+ err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache);
+ if ( err < 0) {
+ nl_perror(err, "Unable to allocate cache");
+ return err;
+ }
+
+ if_index = rtnl_link_name2i(link_cache, "eno16777736");
+ if (!if_index) {
+ fprintf(stderr, "Unable to lookup eno16777736");
+ return -1;
+ }
+
+ link = rtnl_link_ipip_alloc();
+ if(!link) {
+ nl_perror(err, "Unable to allocate link");
+ return -1;
+ }
+
+ rtnl_link_set_name(link, "ipip-tun");
+ rtnl_link_ipip_set_link(link, if_index);
+
+ inet_pton(AF_INET, "192.168.254.12", &addr.s_addr);
+ rtnl_link_ipip_set_local(link, addr.s_addr);
+
+ inet_pton(AF_INET, "192.168.254.13", &addr.s_addr);
+ rtnl_link_ipip_set_remote(link, addr.s_addr);
+
+ rtnl_link_ipip_set_ttl(link, 64);
+ err = rtnl_link_add(sk, link, NLM_F_CREATE);
+ if (err < 0) {
+ nl_perror(err, "Unable to add link");
+ return err;
+ }
+
+ rtnl_link_put(link);
+ nl_close(sk);
+
+ return 0;
+}
diff --git a/tests/test-create-ipvti.c b/tests/test-create-ipvti.c
new file mode 100644
index 00000000..6cb92d72
--- /dev/null
+++ b/tests/test-create-ipvti.c
@@ -0,0 +1,55 @@
+#include <netlink/route/link/ipvti.h>
+#include <netlink-private/netlink.h>
+
+int main(int argc, char *argv[])
+{
+ struct nl_cache *link_cache;
+ struct rtnl_link *link;
+ struct in_addr addr;
+ struct nl_sock *sk;
+ int err, if_index;
+
+ sk = nl_socket_alloc();
+ if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
+ nl_perror(err, "Unable to connect socket");
+ return err;
+ }
+
+ err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache);
+ if ( err < 0) {
+ nl_perror(err, "Unable to allocate cache");
+ return err;
+ }
+
+ if_index = rtnl_link_name2i(link_cache, "ens33");
+ if (!if_index) {
+ fprintf(stderr, "Unable to lookup ens33");
+ return -1;
+ }
+
+ link = rtnl_link_ipvti_alloc();
+ if(!link) {
+ nl_perror(err, "Unable to allocate link");
+ return -1;
+
+ }
+ rtnl_link_set_name(link, "ipvti-tun");
+ rtnl_link_ipvti_set_link(link, if_index);
+
+ inet_pton(AF_INET, "192.168.254.12", &addr.s_addr);
+ rtnl_link_ipvti_set_local(link, addr.s_addr);
+
+ inet_pton(AF_INET, "192.168.254.13", &addr.s_addr);
+ rtnl_link_ipvti_set_remote(link, addr.s_addr);
+
+ err = rtnl_link_add(sk, link, NLM_F_CREATE);
+ if (err < 0) {
+ nl_perror(err, "Unable to add link");
+ return err;
+ }
+
+ rtnl_link_put(link);
+ nl_close(sk);
+
+ return 0;
+}
diff --git a/tests/test-create-macvlan.c b/tests/test-create-macvlan.c
new file mode 100644
index 00000000..64779237
--- /dev/null
+++ b/tests/test-create-macvlan.c
@@ -0,0 +1,48 @@
+#include <netlink/netlink.h>
+#include <netlink/route/link.h>
+#include <netlink/route/link/macvlan.h>
+
+int main(int argc, char *argv[])
+{
+ struct rtnl_link *link;
+ struct nl_cache *link_cache;
+ struct nl_sock *sk;
+ struct nl_addr* addr;
+ int err, master_index;
+
+ sk = nl_socket_alloc();
+ if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
+ nl_perror(err, "Unable to connect socket");
+ return err;
+ }
+
+ if ((err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache)) < 0) {
+ nl_perror(err, "Unable to allocate cache");
+ return err;
+ }
+
+ if (!(master_index = rtnl_link_name2i(link_cache, "eth0"))) {
+ fprintf(stderr, "Unable to lookup eth0");
+ return -1;
+ }
+
+ link = rtnl_link_macvlan_alloc();
+
+ rtnl_link_set_link(link, master_index);
+
+ addr = nl_addr_build(AF_LLC, ether_aton("00:11:22:33:44:55"), ETH_ALEN);
+ rtnl_link_set_addr(link, addr);
+ nl_addr_put(addr);
+
+ rtnl_link_macvlan_set_mode(link, rtnl_link_macvlan_str2mode("bridge"));
+
+ if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) {
+ nl_perror(err, "Unable to add link");
+ return err;
+ }
+
+ rtnl_link_put(link);
+ nl_close(sk);
+
+ return 0;
+}
diff --git a/tests/test-create-sit.c b/tests/test-create-sit.c
new file mode 100644
index 00000000..d33e4963
--- /dev/null
+++ b/tests/test-create-sit.c
@@ -0,0 +1,56 @@
+#include <netlink/route/link/sit.h>
+#include <netlink-private/netlink.h>
+
+int main(int argc, char *argv[])
+{
+ struct nl_cache *link_cache;
+ struct rtnl_link *link;
+ struct in_addr addr;
+ struct nl_sock *sk;
+ int err, if_index;
+
+ sk = nl_socket_alloc();
+ if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
+ nl_perror(err, "Unable to connect socket");
+ return err;
+ }
+
+ err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache);
+ if ( err < 0) {
+ nl_perror(err, "Unable to allocate cache");
+ return err;
+ }
+
+ if_index = rtnl_link_name2i(link_cache, "eno16777736");
+ if (!if_index) {
+ fprintf(stderr, "Unable to lookup eno16777736");
+ return -1;
+ }
+
+ link = rtnl_link_sit_alloc();
+ if(!link) {
+ nl_perror(err, "Unable to allocate link");
+ return -1;
+
+ }
+ rtnl_link_set_name(link, "sit-tun");
+ rtnl_link_sit_set_link(link, if_index);
+
+ inet_pton(AF_INET, "192.168.254.12", &addr.s_addr);
+ rtnl_link_sit_set_local(link, addr.s_addr);
+
+ inet_pton(AF_INET, "192.168.254.13", &addr.s_addr);
+ rtnl_link_sit_set_remote(link, addr.s_addr);
+
+ rtnl_link_sit_set_ttl(link, 64);
+ err = rtnl_link_add(sk, link, NLM_F_CREATE);
+ if (err < 0) {
+ nl_perror(err, "Unable to add link");
+ return err;
+ }
+
+ rtnl_link_put(link);
+ nl_close(sk);
+
+ return 0;
+}
diff --git a/tests/test-create-veth.c b/tests/test-create-veth.c
new file mode 100644
index 00000000..db5ab8b1
--- /dev/null
+++ b/tests/test-create-veth.c
@@ -0,0 +1,42 @@
+#include <netlink/netlink.h>
+#include <netlink/route/link.h>
+#include <netlink/route/link/veth.h>
+
+int main(int argc, char *argv[])
+{
+ struct rtnl_link *link;
+ struct nl_sock *sk;
+ int err;
+ struct rtnl_link *peer;
+
+ sk = nl_socket_alloc();
+ if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
+ nl_perror(err, "Unable to connect socket");
+ return err;
+ }
+
+#if 0
+ rtnl_link_veth_add(sk, "veth2", "veth3", getpid());
+#else
+ link = rtnl_link_veth_alloc();
+ if (!link) {
+ nl_perror(err, "Unable to alloc link");
+ return err;
+ }
+
+ rtnl_link_set_name(link, "veth8");
+ peer = rtnl_link_veth_get_peer(link);
+ rtnl_link_set_name(peer, "veth9");
+
+ if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) {
+ nl_perror(err, "Unable to add link");
+ return err;
+ }
+ printf("peer is %s\n", rtnl_link_get_name(peer));
+ rtnl_link_put(peer);
+ rtnl_link_put(link);
+#endif
+ nl_close(sk);
+
+ return 0;
+}
diff --git a/tests/test-create-vlan.c b/tests/test-create-vlan.c
new file mode 100644
index 00000000..64e478f4
--- /dev/null
+++ b/tests/test-create-vlan.c
@@ -0,0 +1,43 @@
+#include <netlink/netlink.h>
+#include <netlink/route/link.h>
+#include <netlink/route/link/vlan.h>
+
+int main(int argc, char *argv[])
+{
+ struct rtnl_link *link;
+ struct nl_cache *link_cache;
+ struct nl_sock *sk;
+ int err, master_index;
+
+ sk = nl_socket_alloc();
+ if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
+ nl_perror(err, "Unable to connect socket");
+ return err;
+ }
+
+ if ((err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache)) < 0) {
+ nl_perror(err, "Unable to allocate cache");
+ return err;
+ }
+
+ if (!(master_index = rtnl_link_name2i(link_cache, "eth0"))) {
+ fprintf(stderr, "Unable to lookup eth0");
+ return -1;
+ }
+
+ link = rtnl_link_vlan_alloc();
+
+ rtnl_link_set_link(link, master_index);
+
+ rtnl_link_vlan_set_id(link, 10);
+
+ if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) {
+ nl_perror(err, "Unable to add link");
+ return err;
+ }
+
+ rtnl_link_put(link);
+ nl_close(sk);
+
+ return 0;
+}
diff --git a/tests/test-create-vxlan.c b/tests/test-create-vxlan.c
new file mode 100644
index 00000000..98a5103c
--- /dev/null
+++ b/tests/test-create-vxlan.c
@@ -0,0 +1,47 @@
+#include <netlink/netlink.h>
+#include <netlink/route/link.h>
+#include <netlink/route/link/vxlan.h>
+
+int main(int argc, char *argv[])
+{
+ struct rtnl_link *link;
+ struct nl_addr *addr;
+ struct nl_sock *sk;
+ int err;
+
+ sk = nl_socket_alloc();
+ if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
+ nl_perror(err, "Unable to connect socket");
+ return err;
+ }
+
+ link = rtnl_link_vxlan_alloc();
+
+ rtnl_link_set_name(link, "vxlan128");
+
+ if ((err = rtnl_link_vxlan_set_id(link, 128)) < 0) {
+ nl_perror(err, "Unable to set VXLAN network identifier");
+ return err;
+ }
+
+ if ((err = nl_addr_parse("239.0.0.1", AF_INET, &addr)) < 0) {
+ nl_perror(err, "Unable to parse IP address");
+ return err;
+ }
+
+ if ((err = rtnl_link_vxlan_set_group(link, addr)) < 0) {
+ nl_perror(err, "Unable to set multicast IP address");
+ return err;
+ }
+ nl_addr_put(addr);
+
+ if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) {
+ nl_perror(err, "Unable to add link");
+ return err;
+ }
+
+ rtnl_link_put(link);
+ nl_close(sk);
+
+ return 0;
+}
diff --git a/tests/test-delete-link.c b/tests/test-delete-link.c
new file mode 100644
index 00000000..9cf1034e
--- /dev/null
+++ b/tests/test-delete-link.c
@@ -0,0 +1,28 @@
+#include <netlink/netlink.h>
+#include <netlink/route/link.h>
+
+int main(int argc, char *argv[])
+{
+ struct rtnl_link *link;
+ struct nl_sock *sk;
+ int err;
+
+ sk = nl_socket_alloc();
+ if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
+ nl_perror(err, "Unable to connect socket");
+ return err;
+ }
+
+ link = rtnl_link_alloc();
+ rtnl_link_set_name(link, "my_bond");
+
+ if ((err = rtnl_link_delete(sk, link)) < 0) {
+ nl_perror(err, "Unable to delete link");
+ return err;
+ }
+
+ rtnl_link_put(link);
+ nl_close(sk);
+
+ return 0;
+}
diff --git a/tests/test-genl.c b/tests/test-genl.c
index 8bf60c5c..74aea106 100644
--- a/tests/test-genl.c
+++ b/tests/test-genl.c
@@ -1,4 +1,72 @@
-#include "../src/utils.h"
+#include <netlink/cli/utils.h>
+#include <linux/taskstats.h>
+
+static struct nla_policy attr_policy[TASKSTATS_TYPE_MAX+1] = {
+ [TASKSTATS_TYPE_PID] = { .type = NLA_U32 },
+ [TASKSTATS_TYPE_TGID] = { .type = NLA_U32 },
+ [TASKSTATS_TYPE_STATS] = { .minlen = sizeof(struct taskstats) },
+ [TASKSTATS_TYPE_AGGR_PID] = { .type = NLA_NESTED },
+ [TASKSTATS_TYPE_AGGR_TGID] = { .type = NLA_NESTED },
+};
+
+
+static int parse_cmd_new(struct nl_cache_ops *unused, struct genl_cmd *cmd,
+ struct genl_info *info, void *arg)
+{
+ struct nlattr *attrs[TASKSTATS_TYPE_MAX+1];
+ struct nlattr *nested;
+ int err;
+
+ if (info->attrs[TASKSTATS_TYPE_AGGR_PID])
+ nested = info->attrs[TASKSTATS_TYPE_AGGR_PID];
+ else if (info->attrs[TASKSTATS_TYPE_AGGR_TGID])
+ nested = info->attrs[TASKSTATS_TYPE_AGGR_TGID];
+ else {
+ fprintf(stderr, "Invalid taskstats message: Unable to find "
+ "nested attribute/\n");
+ return NL_SKIP;
+ }
+
+ err = nla_parse_nested(attrs, TASKSTATS_TYPE_MAX, nested, attr_policy);
+ if (err < 0) {
+ nl_perror(err, "Error while parsing generic netlink message");
+ return err;
+ }
+
+
+ if (attrs[TASKSTATS_TYPE_STATS]) {
+ struct taskstats *stats = nla_data(attrs[TASKSTATS_TYPE_STATS]);
+
+ printf("%s pid %u uid %u gid %u parent %u\n",
+ stats->ac_comm, stats->ac_pid, stats->ac_uid,
+ stats->ac_gid, stats->ac_ppid);
+ }
+
+ return 0;
+}
+
+static int parse_cb(struct nl_msg *msg, void *arg)
+{
+ return genl_handle_msg(msg, NULL);
+}
+
+static struct genl_cmd cmds[] = {
+ {
+ .c_id = TASKSTATS_CMD_NEW,
+ .c_name = "taskstats_new()",
+ .c_maxattr = TASKSTATS_TYPE_MAX,
+ .c_attr_policy = attr_policy,
+ .c_msg_parser = &parse_cmd_new,
+ },
+};
+
+#define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0]))
+
+static struct genl_ops ops = {
+ .o_name = TASKSTATS_GENL_NAME,
+ .o_cmds = cmds,
+ .o_ncmds = ARRAY_SIZE(cmds),
+};
int main(int argc, char *argv[])
{
@@ -7,28 +75,42 @@ int main(int argc, char *argv[])
void *hdr;
int err;
- sock = nlt_alloc_socket();
- nlt_connect(sock, NETLINK_GENERIC);
+ sock = nl_cli_alloc_socket();
+ nl_cli_connect(sock, NETLINK_GENERIC);
+
+ if ((err = genl_register_family(&ops)) < 0)
+ nl_cli_fatal(err, "Unable to register Generic Netlink family");
+
+ if ((err = genl_ops_resolve(sock, &ops)) < 0)
+ nl_cli_fatal(err, "Unable to resolve family name");
+
+ if (genl_ctrl_resolve(sock, "nlctrl") != GENL_ID_CTRL)
+ nl_cli_fatal(NLE_INVAL, "Resolving of \"nlctrl\" failed");
msg = nlmsg_alloc();
if (msg == NULL)
- fatal(NLE_NOMEM, "Unable to allocate netlink message");
+ nl_cli_fatal(NLE_NOMEM, "Unable to allocate netlink message");
- hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, GENL_ID_CTRL,
- 0, 0, CTRL_CMD_GETFAMILY, 1);
+ hdr = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, ops.o_id,
+ 0, 0, TASKSTATS_CMD_GET, TASKSTATS_GENL_VERSION);
if (hdr == NULL)
- fatal(ENOMEM, "Unable to write genl header");
+ nl_cli_fatal(ENOMEM, "Unable to write genl header");
- if ((err = nla_put_u32(msg, CTRL_ATTR_FAMILY_ID, GENL_ID_CTRL)) < 0)
- fatal(err, "Unable to add attribute: %s", nl_geterror(err));
+ if ((err = nla_put_u32(msg, TASKSTATS_CMD_ATTR_PID, 1)) < 0)
+ nl_cli_fatal(err, "Unable to add attribute: %s", nl_geterror(err));
if ((err = nl_send_auto_complete(sock, msg)) < 0)
- fatal(err, "Unable to send message: %s", nl_geterror(err));
+ nl_cli_fatal(err, "Unable to send message: %s", nl_geterror(err));
+
+ nlmsg_free(msg);
+
+ if ((err = nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM,
+ parse_cb, NULL)) < 0)
+ nl_cli_fatal(err, "Unable to modify valid message callback");
if ((err = nl_recvmsgs_default(sock)) < 0)
- fatal(err, "Unable to receive message: %s", nl_geterror(err));
+ nl_cli_fatal(err, "Unable to receive message: %s", nl_geterror(err));
- nlmsg_free(msg);
nl_close(sock);
nl_socket_free(sock);
diff --git a/tests/test-nf-cache-mngr.c b/tests/test-nf-cache-mngr.c
index 05485bf6..b4f30223 100644
--- a/tests/test-nf-cache-mngr.c
+++ b/tests/test-nf-cache-mngr.c
@@ -1,13 +1,13 @@
-#include "../src/utils.h"
+#include <netlink/cli/utils.h>
static void change_cb(struct nl_cache *cache, struct nl_object *obj,
- int action)
+ int action, void *data)
{
struct nfnl_ct *ct = (struct nfnl_ct *) obj;
static struct nl_addr *hack = NULL;
if (!hack)
- hack = nl_addr_parse("194.88.212.233", AF_INET);
+ nl_addr_parse("194.88.212.233", AF_INET, &hack);
if (!nl_addr_cmp(hack, nfnl_ct_get_src(ct, 1)) ||
!nl_addr_cmp(hack, nfnl_ct_get_dst(ct, 1))) {
@@ -26,25 +26,26 @@ int main(int argc, char *argv[])
struct nl_cache_mngr *mngr;
struct nl_sock *sock;
struct nl_cache *ct;
+ int err;
- sock = nlt_socket_alloc();
+ sock = nl_cli_alloc_socket();
- mngr = nl_cache_mngr_alloc(sock, NETLINK_NETFILTER, NL_AUTO_PROVIDE);
- if (!mngr) {
- nl_perror("nl_cache_mngr_alloc");
+ err = nl_cache_mngr_alloc(sock, NETLINK_NETFILTER, NL_AUTO_PROVIDE, &mngr);
+ if (err < 0) {
+ nl_perror(err, "nl_cache_mngr_alloc");
return -1;
}
- ct = nl_cache_mngr_add(mngr, "netfilter/ct", &change_cb);
- if (ct == NULL) {
- nl_perror("nl_cache_mngr_add(netfilter/ct)");
+ err = nl_cache_mngr_add(mngr, "netfilter/ct", &change_cb, NULL, &ct);
+ if (err < 0) {
+ nl_perror(err, "nl_cache_mngr_add(netfilter/ct)");
return -1;
}
for (;;) {
int err = nl_cache_mngr_poll(mngr, 5000);
if (err < 0) {
- nl_perror("nl_cache_mngr_poll()");
+ nl_perror(err, "nl_cache_mngr_poll()");
return -1;
}
diff --git a/tests/test-socket-creation.c b/tests/test-socket-creation.c
index a170ccd6..83f3ad46 100644
--- a/tests/test-socket-creation.c
+++ b/tests/test-socket-creation.c
@@ -1,23 +1,24 @@
-#include "../src/utils.h"
+#include <netlink/netlink.h>
+#include <errno.h>
int main(int argc, char *argv[])
{
struct nl_sock *h[1025];
int i;
- h[0] = nl_handle_alloc();
+ h[0] = nl_socket_alloc();
printf("Created handle with port 0x%x\n",
nl_socket_get_local_port(h[0]));
- nl_handle_destroy(h[0]);
- h[0] = nl_handle_alloc();
+ nl_socket_free(h[0]);
+ h[0] = nl_socket_alloc();
printf("Created handle with port 0x%x\n",
nl_socket_get_local_port(h[0]));
- nl_handle_destroy(h[0]);
+ nl_socket_free(h[0]);
for (i = 0; i < 1025; i++) {
- h[i] = nl_handle_alloc();
+ h[i] = nl_socket_alloc();
if (h[i] == NULL)
- nl_perror("Unable to allocate socket");
+ nl_perror(ENOMEM, "Unable to allocate socket");
else
printf("Created handle with port 0x%x\n",
nl_socket_get_local_port(h[i]));
diff --git a/tests/test-u32-filter-with-actions.c b/tests/test-u32-filter-with-actions.c
new file mode 100644
index 00000000..55f913af
--- /dev/null
+++ b/tests/test-u32-filter-with-actions.c
@@ -0,0 +1,400 @@
+/*
+ * test/tests-u32-with-actions.c Add ingress qdisc, create some hash filters, and add redirect action
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation version 2.1
+ * of the License.
+ *
+ * Stolen from tests/test-complex-HTB-with-hash-filters.c
+ *
+ * Copyright (c) 2013 Cong Wang <xiyou.wangcong@gmail.com>
+ */
+
+#include <netlink/route/link.h>
+#include <netlink/route/tc.h>
+#include <netlink/route/qdisc.h>
+#include <netlink/route/cls/u32.h>
+#include <netlink/route/classifier.h>
+#include <netlink/route/action.h>
+#include <netlink/route/act/mirred.h>
+#include <netlink/route/class.h>
+#include <linux/if_ether.h>
+
+#include <netlink/attr.h>
+#include <stdio.h>
+#include <string.h>
+
+#define TC_HANDLE(maj, min) (TC_H_MAJ((maj) << 16) | TC_H_MIN(min))
+
+/* some functions are copied from iproute-tc tool */
+static int get_u32(__u32 *val, const char *arg, int base)
+{
+ unsigned long res;
+ char *ptr;
+
+ if (!arg || !*arg)
+ return -1;
+ res = strtoul(arg, &ptr, base);
+ if (!ptr || ptr == arg || *ptr || res > 0xFFFFFFFFUL)
+ return -1;
+ *val = res;
+ return 0;
+}
+
+static int get_u32_handle(__u32 *handle, const char *str)
+{
+ __u32 htid=0, hash=0, nodeid=0;
+ char *tmp = strchr(str, ':');
+
+ if (tmp == NULL) {
+ if (memcmp("0x", str, 2) == 0)
+ return get_u32(handle, str, 16);
+ return -1;
+ }
+ htid = strtoul(str, &tmp, 16);
+ if (tmp == str && *str != ':' && *str != 0)
+ return -1;
+ if (htid>=0x1000)
+ return -1;
+ if (*tmp) {
+ str = tmp+1;
+ hash = strtoul(str, &tmp, 16);
+ if (tmp == str && *str != ':' && *str != 0)
+ return -1;
+ if (hash>=0x100)
+ return -1;
+ if (*tmp) {
+ str = tmp+1;
+ nodeid = strtoul(str, &tmp, 16);
+ if (tmp == str && *str != 0)
+ return -1;
+ if (nodeid>=0x1000)
+ return -1;
+ }
+ }
+ *handle = (htid<<20)|(hash<<12)|nodeid;
+ return 0;
+}
+
+static uint32_t get_u32_parse_handle(const char *cHandle)
+{
+ uint32_t handle=0;
+
+ if(get_u32_handle(&handle, cHandle)) {
+ printf ("Illegal \"ht\"\n");
+ return -1;
+ }
+
+ if (handle && TC_U32_NODE(handle)) {
+ printf("\"link\" must be a hash table.\n");
+ return -1;
+ }
+ return handle;
+}
+
+/*
+ * Function that adds a new filter and attach it to a hash table
+ * and set next hash table link with hash mask
+ *
+ */
+static
+int u32_add_filter_on_ht_with_hashmask(struct nl_sock *sock, struct rtnl_link *rtnlLink, uint32_t prio,
+ uint32_t keyval, uint32_t keymask, int keyoff, int keyoffmask,
+ uint32_t htid, uint32_t htlink, uint32_t hmask, uint32_t hoffset, struct rtnl_act *act)
+{
+ struct rtnl_cls *cls;
+ int err;
+
+ cls=rtnl_cls_alloc();
+ if (!(cls)) {
+ printf("Can not allocate classifier\n");
+ nl_socket_free(sock);
+ exit(1);
+ }
+
+ rtnl_tc_set_link(TC_CAST(cls), rtnlLink);
+
+ if ((err = rtnl_tc_set_kind(TC_CAST(cls), "u32"))) {
+ printf("Can not set classifier as u32\n");
+ return 1;
+ }
+
+ rtnl_cls_set_prio(cls, prio);
+ rtnl_cls_set_protocol(cls, ETH_P_IP);
+
+ rtnl_tc_set_parent(TC_CAST(cls), TC_HANDLE(0xffff, 0));
+
+ if (htid)
+ rtnl_u32_set_hashtable(cls, htid);
+
+ rtnl_u32_add_key_uint32(cls, keyval, keymask, keyoff, keyoffmask);
+
+ rtnl_u32_set_hashmask(cls, hmask, hoffset);
+
+ rtnl_u32_set_link(cls, htlink);
+
+ rtnl_u32_add_action(cls, act);
+
+
+ if ((err = rtnl_cls_add(sock, cls, NLM_F_CREATE))) {
+ printf("Can not add classifier: %s\n", nl_geterror(err));
+ return -1;
+ }
+ rtnl_cls_put(cls);
+ return 0;
+}
+
+/*
+ * function that creates a new hash table
+ */
+static
+int u32_add_ht(struct nl_sock *sock, struct rtnl_link *rtnlLink, uint32_t prio, uint32_t htid, uint32_t divisor)
+{
+
+ int err;
+ struct rtnl_cls *cls;
+
+ cls=rtnl_cls_alloc();
+ if (!(cls)) {
+ printf("Can not allocate classifier\n");
+ nl_socket_free(sock);
+ exit(1);
+ }
+
+ rtnl_tc_set_link(TC_CAST(cls), rtnlLink);
+
+ if ((err = rtnl_tc_set_kind(TC_CAST(cls), "u32"))) {
+ printf("Can not set classifier as u32\n");
+ return 1;
+ }
+
+ rtnl_cls_set_prio(cls, prio);
+ rtnl_cls_set_protocol(cls, ETH_P_IP);
+ rtnl_tc_set_parent(TC_CAST(cls), TC_HANDLE(0xffff, 0));
+
+ rtnl_u32_set_handle(cls, htid, 0x0, 0x0);
+ //printf("htid: 0x%X\n", htid);
+ rtnl_u32_set_divisor(cls, divisor);
+
+ if ((err = rtnl_cls_add(sock, cls, NLM_F_CREATE))) {
+ printf("Can not add classifier: %s\n", nl_geterror(err));
+ return -1;
+ }
+ rtnl_cls_put(cls);
+ return 0;
+}
+
+/*
+ * function that adds a new ingress qdisc and set the default class for unclassified traffic
+ */
+static
+int qdisc_add_ingress(struct nl_sock *sock, struct rtnl_link *rtnlLink)
+{
+
+ struct rtnl_qdisc *qdisc;
+ int err;
+
+ /* Allocation of a qdisc object */
+ if (!(qdisc = rtnl_qdisc_alloc())) {
+ printf("Can not allocate Qdisc\n");
+ return -1;
+ }
+
+ //rtnl_tc_set_ifindex(TC_CAST(qdisc), master_index);
+ rtnl_tc_set_link(TC_CAST(qdisc), rtnlLink);
+ rtnl_tc_set_parent(TC_CAST(qdisc), TC_H_ROOT);
+
+ //printf("Delete current qdisc\n");
+ rtnl_qdisc_delete(sock, qdisc);
+ //rtnl_qdisc_put(qdisc);
+
+ rtnl_tc_set_handle(TC_CAST(qdisc), TC_HANDLE(0xffff, 0));
+
+ if ((err = rtnl_tc_set_kind(TC_CAST(qdisc), "ingress"))) {
+ printf("Can not allocate ingress\n");
+ return -1;
+ }
+
+ /* Submit request to kernel and wait for response */
+ if ((err = rtnl_qdisc_add(sock, qdisc, NLM_F_CREATE))) {
+ printf("Can not allocate ingress Qdisc\n");
+ return -1;
+ }
+
+ /* Return the qdisc object to free memory resources */
+ rtnl_qdisc_put(qdisc);
+
+ return 0;
+}
+
+int main(void)
+{
+ struct nl_sock *sock;
+ struct rtnl_link *link;
+ uint32_t ht, htlink, htid, direction;
+ char chashlink[16]="";
+ int err;
+ struct nl_cache *link_cache;
+ struct rtnl_act *act;
+
+ if (!(sock = nl_socket_alloc())) {
+ printf("Unable to allocate netlink socket\n");
+ exit(1);
+ }
+
+ if ((err = nl_connect(sock, NETLINK_ROUTE)) < 0 ) {
+ printf("Nu s-a putut conecta la NETLINK!\n");
+ nl_socket_free(sock);
+ exit(1);
+ }
+
+ if ((err = rtnl_link_alloc_cache(sock, AF_UNSPEC, &link_cache)) < 0) {
+ printf("Unable to allocate link cache: %s\n",
+ nl_geterror(err));
+ nl_socket_free(sock);
+ exit(1);
+ }
+
+ /* lookup interface index of eth0 */
+ if (!(link = rtnl_link_get_by_name(link_cache, "eth0"))) {
+ /* error */
+ printf("Interface not found\n");
+ nl_socket_free(sock);
+ exit(1);
+ }
+
+ err=qdisc_add_ingress(sock, link);
+ //printf("Add main hash table\n");
+
+ /* create u32 first hash filter table
+ *
+ */
+ /* formula calcul handle:
+ * uint32_t handle = (htid << 20) | (hash << 12) | nodeid;
+ */
+
+ /*
+ * Upper limit of number of hash tables: 4096 (0xFFF)
+ * Number of hashes in a table: 256 values (0xFF)
+ *
+ */
+
+ /* using 256 values for hash table
+ * each entry in hash table match a byte from IP address specified later by a hash key
+ */
+
+ uint32_t i;
+ for (i = 1; i <= 0xf; i++)
+ u32_add_ht(sock, link, 1, i, 256);
+
+ /*
+ * attach a u32 filter to the first hash
+ * that redirects all traffic and make a hash key
+ * from the fist byte of the IP address
+ *
+ */
+
+ //divisor=0x0; // unused here
+ //handle = 0x0; // unused here
+ //hash = 0x0; // unused here
+ //htid = 0x0; // unused here
+ //nodeid = 0x0; // unused here
+
+ // direction = 12 -> source IP
+ // direction = 16 -> destination IP
+ direction = 16;
+
+ /*
+ * which hash table will use
+ * in our case is hash table no 1 defined previous
+ *
+ * There are 2 posibilities to set the the hash table:
+ * 1. Using function get_u32_handle and sent a string in
+ * format 10: where 10 is number of the hash table
+ * 2. Create your own value in format: 0xa00000
+ *
+ */
+ strcpy(chashlink, "1:");
+ //printf("Hash Link: %s\n", chashlink);
+ //chashlink=malloc(sizeof(char) *
+ htlink = 0x0; // is used by get_u32_handle to return the correct value of hash table (link)
+
+ if(get_u32_handle(&htlink, chashlink)) {
+ printf ("Illegal \"link\"");
+ nl_socket_free(sock);
+ exit(1);
+ }
+ //printf ("hash link : 0x%X\n", htlink);
+ //printf ("hash link test : %u\n", (htlink && TC_U32_NODE(htlink)));
+
+ if (htlink && TC_U32_NODE(htlink)) {
+ printf("\"link\" must be a hash table.\n");
+ nl_socket_free(sock);
+ exit(1);
+ }
+
+ /* the hash mask will hit the hash table (link) no 1: in our case
+ */
+
+ /* set the hash key mask */
+ //hashmask = 0xFF000000UL; // the mask that is used to match the hash in specific table, in our case for example 1:a with mean the first byte which is 10 in hash table 1
+
+ /* Here we add a hash filter which match the first byte (see the hashmask value)
+ * of the source IP (offset 12 in the packet header)
+ * You can use also offset 16 to match the destination IP
+ */
+
+ /*
+ * Also we need a filter to match our rule
+ * This mean that we will put a 0.0.0.0/0 filter in our first rule
+ * that match the offset 12 (source IP)
+ * Also you can put offset 16 to match the destination IP
+ */
+
+ u32_add_filter_on_ht_with_hashmask(sock, link, 1,
+ 0x0, 0x0, direction, 0,
+ 0, htlink, 0xff000000, direction, NULL);
+
+ /*
+ * For each first byte that we need to match we will create a new hash table
+ * For example: you have those clases: 10.0.0.0/24 and 172.16.0.0/23
+ * For byte 10 and byte 172 will create a separate hash table that will match the second
+ * byte from each class.
+ *
+ */
+
+
+ /*
+ * Now we will create other filter under (ATENTION) our first hash table (link) 1:
+ * Previous rule redirects the trafic according the hash mask to hash table (link) no 1:
+ * Here we will match the hash tables from 1:0 to 1:ff. Under each hash table we will attach
+ * other rules that matches next byte from IP source/destination IP and we will repeat the
+ * previous steps.
+ *
+ */
+
+ act = rtnl_act_alloc();
+ if (!act) {
+ printf("rtnl_act_alloc() returns %p\n", act);
+ return -1;
+ }
+ rtnl_tc_set_kind(TC_CAST(act), "mirred");
+ rtnl_mirred_set_action(act, TCA_EGRESS_REDIR);
+ rtnl_mirred_set_policy(act, TC_ACT_STOLEN);
+ rtnl_mirred_set_ifindex(act, rtnl_link_name2i(link_cache, "eth1"));
+ // /8 check
+
+ // 10.0.0.0/8
+ ht=get_u32_parse_handle("1:a:");
+ htid = (ht&0xFFFFF000);
+ htlink=get_u32_parse_handle("2:");
+
+ u32_add_filter_on_ht_with_hashmask(sock, link, 1,
+ 0x0a000000, 0xff000000, direction, 0,
+ htid, htlink, 0x00ff0000, direction, act);
+
+ rtnl_act_put(act);
+ nl_socket_free(sock);
+ return 0;
+}
diff --git a/tests/util.h b/tests/util.h
new file mode 100644
index 00000000..c6753835
--- /dev/null
+++ b/tests/util.h
@@ -0,0 +1,5 @@
+#include <check.h>
+
+#define nl_fail_if(condition, error, message) \
+ fail_if((condition), "nlerr=%d (%s): %s", \
+ (error), nl_geterror(error), (message))