From 7d8330b9bee878591d51889c876f79d69323951e Mon Sep 17 00:00:00 2001 From: Daniel Adolfsson Date: Fri, 13 Dec 2019 12:33:55 +0100 Subject: [PATCH] Cleanup and add support for rule->table --- src/addr.c | 45 ++++++++++++++++++++++----------------------- src/conf.c | 16 +++++++++++++--- src/ndppd.c | 4 +++- src/proxy.c | 2 +- src/rule.h | 2 ++ 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/addr.c b/src/addr.c index 70f63b7..5480537 100644 --- a/src/addr.c +++ b/src/addr.c @@ -29,7 +29,7 @@ /*! Returns the string representation of addr. * * @note This function returns a pointer to static data. It uses three different static arrays - * to allow the function to be chained. + * to allow the function to be chained. */ const char *nd_aton(nd_addr_t *addr) { @@ -105,11 +105,11 @@ bool nd_addr_match(nd_addr_t *first, nd_addr_t *second, int pflen) static int ndL_count_bits(uint32_t n) { - n = (n & 0x55555555u) + ((n >> 1) & 0x55555555u); - n = (n & 0x33333333u) + ((n >> 2) & 0x33333333u); - n = (n & 0x0f0f0f0fu) + ((n >> 4) & 0x0f0f0f0fu); - n = (n & 0x00ff00ffu) + ((n >> 8) & 0x00ff00ffu); - n = (n & 0x0000ffffu) + ((n >> 16) & 0x0000ffffu); + n = (n & 0x55555555) + ((n >> 1) & 0x55555555); + n = (n & 0x33333333) + ((n >> 2) & 0x33333333); + n = (n & 0x0f0f0f0f) + ((n >> 4) & 0x0f0f0f0f); + n = (n & 0x00ff00ff) + ((n >> 8) & 0x00ff00ff); + n = (n & 0x0000ffff) + ((n >> 16) & 0x0000ffff); return n; } @@ -123,38 +123,37 @@ void nd_addr_from_pflen(int pflen, nd_addr_t *netmask) { if (pflen >= 97) { - netmask->s6_addr32[0] = 0xffffffffu; - netmask->s6_addr32[1] = 0xffffffffu; - netmask->s6_addr32[2] = 0xffffffffu; + netmask->s6_addr32[0] = 0xffffffff; + netmask->s6_addr32[1] = 0xffffffff; + netmask->s6_addr32[2] = 0xffffffff; netmask->s6_addr32[3] = ndL_masks[pflen - 97]; } else if (pflen >= 65) { - netmask->s6_addr32[0] = 0xffffffffu; - netmask->s6_addr32[1] = 0xffffffffu; + netmask->s6_addr32[0] = 0xffffffff; + netmask->s6_addr32[1] = 0xffffffff; netmask->s6_addr32[2] = ndL_masks[pflen - 65]; - netmask->s6_addr32[3] = 0x00000000u; + netmask->s6_addr32[3] = 0x00000000; } else if (pflen >= 33) { - netmask->s6_addr32[0] = 0xffffffffu; + netmask->s6_addr32[0] = 0xffffffff; netmask->s6_addr32[1] = ndL_masks[pflen - 33]; - netmask->s6_addr32[2] = 0x00000000u; - netmask->s6_addr32[3] = 0x00000000u; + netmask->s6_addr32[2] = 0x00000000; + netmask->s6_addr32[3] = 0x00000000; } else if (pflen >= 1) { netmask->s6_addr32[0] = ndL_masks[pflen - 1]; - netmask->s6_addr32[1] = 0x00000000u; - netmask->s6_addr32[2] = 0x00000000u; - netmask->s6_addr32[3] = 0x00000000u; + netmask->s6_addr32[1] = 0x00000000; + netmask->s6_addr32[2] = 0x00000000; + netmask->s6_addr32[3] = 0x00000000; } else { - netmask->s6_addr32[0] = 0x00000000u; - netmask->s6_addr32[1] = 0x00000000u; - netmask->s6_addr32[2] = 0x00000000u; - netmask->s6_addr32[3] = 0x00000000u; + netmask->s6_addr32[0] = 0x00000000; + netmask->s6_addr32[1] = 0x00000000; + netmask->s6_addr32[2] = 0x00000000; + netmask->s6_addr32[3] = 0x00000000; } } - diff --git a/src/conf.c b/src/conf.c index c8630a7..ba6f183 100644 --- a/src/conf.c +++ b/src/conf.c @@ -73,7 +73,7 @@ enum { NDL_DEFAULT, NDL_PROXY, - NDL_ROUTE + NDL_RULE }; /* Configuration types. */ @@ -111,8 +111,12 @@ static const ndL_cfinfo_t ndL_cfinfo_table[] = { { "retrans-time", NDL_DEFAULT, NDL_INT, (uintptr_t)&nd_conf_retrans_time, 0, 60000, NULL }, { "keepalive", NDL_DEFAULT, NDL_BOOL, (uintptr_t)&nd_conf_keepalive, 0, 0, NULL }, { "router", NDL_PROXY, NDL_BOOL, offsetof(nd_proxy_t, router), 0, 0, NULL }, - { "auto", NDL_ROUTE, NDL_BOOL, offsetof(nd_rule_t, is_auto), 0, 0, NULL }, + { "auto", NDL_RULE, NDL_BOOL, offsetof(nd_rule_t, is_auto), 0, 0, NULL }, + { "autowire", NDL_RULE, NDL_BOOL, offsetof(nd_rule_t, autowire), 0, 0, NULL }, { "promisc", NDL_PROXY, NDL_BOOL, offsetof(nd_proxy_t, promisc), 0, 0, NULL }, +#ifndef __FreeBSD__ + { "table", NDL_RULE, NDL_INT, offsetof(nd_rule_t, table), 0, 255, NULL }, +#endif { NULL, 0, 0, 0, 0, 0, NULL }, }; @@ -388,7 +392,13 @@ static bool ndL_parse_rule(ndL_state_t *state, nd_proxy_t *proxy) rule->prefix = 128; } - return ndL_parse_block(state, NDL_ROUTE, rule); +#ifdef __linux__ + rule->table = 254; +#else + rule->table = 0; +#endif + + return ndL_parse_block(state, NDL_RULE, rule); } static bool ndL_parse_proxy(ndL_state_t *state, __attribute__((unused)) void *unused) diff --git a/src/ndppd.c b/src/ndppd.c index 25073d8..af0ed39 100644 --- a/src/ndppd.c +++ b/src/ndppd.c @@ -233,4 +233,6 @@ int main(int argc, char *argv[]) } return 0; -} \ No newline at end of file +} + + diff --git a/src/proxy.c b/src/proxy.c index 7537d83..fe27636 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -107,7 +107,7 @@ void nd_proxy_handle_ns(nd_proxy_t *proxy, nd_addr_t *src, __attribute__((unused { /* TODO: Loop through valid routes. */ - nd_rtnl_route_t *route = nd_rtnl_find_route(tgt, 254); + nd_rtnl_route_t *route = nd_rtnl_find_route(tgt, rule->table); if (!route || route->oif == proxy->iface->index) { diff --git a/src/rule.h b/src/rule.h index c4b3a20..f26f296 100644 --- a/src/rule.h +++ b/src/rule.h @@ -34,6 +34,8 @@ struct nd_rule nd_iface_t *iface; bool is_auto; + bool autowire; + int table; }; nd_rule_t *nd_rule_create(nd_proxy_t *proxy);