diff --git a/sys/include/net/fib.h b/sys/include/net/fib.h index abe714c0b435ab462afd5fc253add10294a8933b..8341a636c5975496b3422bbcb253ab2a09489f1f 100644 --- a/sys/include/net/fib.h +++ b/sys/include/net/fib.h @@ -82,6 +82,11 @@ typedef struct fib_destination_set_entry_t { */ #define FIB_FLAG_RPL_ROUTE (1UL << 0) +/** + * @brief flag to identify if the FIB-Entry is a net prefix (MSB == 1) + */ +#define FIB_FLAG_NET_PREFIX (1UL<<31) + /** * @brief initializes all FIB entries with 0 * diff --git a/sys/net/gnrc/routing/rpl/gnrc_rpl_dodag.c b/sys/net/gnrc/routing/rpl/gnrc_rpl_dodag.c index cef2d059a3eed921a85d5a06295df51f62e4e48d..87ef45d0ad3aef1bbdd19f3dfd599ccec3d098a3 100644 --- a/sys/net/gnrc/routing/rpl/gnrc_rpl_dodag.c +++ b/sys/net/gnrc/routing/rpl/gnrc_rpl_dodag.c @@ -242,7 +242,8 @@ void gnrc_rpl_parent_update(gnrc_rpl_dodag_t *dodag, gnrc_rpl_parent_t *parent) ipv6_addr_t all_RPL_nodes = GNRC_RPL_ALL_NODES_ADDR; kernel_pid_t if_id; if ((if_id = gnrc_ipv6_netif_find_by_addr(NULL, &all_RPL_nodes)) != KERNEL_PID_UNDEF) { - fib_add_entry(&gnrc_ipv6_fib_table, if_id, def.u8, sizeof(ipv6_addr_t), 0x0, + fib_add_entry(&gnrc_ipv6_fib_table, if_id, def.u8, sizeof(ipv6_addr_t), + (FIB_FLAG_NET_PREFIX | 0x0), parent->addr.u8, sizeof(ipv6_addr_t), FIB_FLAG_RPL_ROUTE, (dodag->default_lifetime * dodag->lifetime_unit) * SEC_IN_MS); } @@ -304,7 +305,7 @@ static gnrc_rpl_parent_t *_gnrc_rpl_find_preferred_parent(gnrc_rpl_dodag_t *doda } fib_add_entry(&gnrc_ipv6_fib_table, if_id, def.u8, sizeof(ipv6_addr_t), - 0x0, dodag->parents->addr.u8, sizeof(ipv6_addr_t), + (FIB_FLAG_NET_PREFIX | 0x0), dodag->parents->addr.u8, sizeof(ipv6_addr_t), FIB_FLAG_RPL_ROUTE, (dodag->default_lifetime * dodag->lifetime_unit) * SEC_IN_MS); } diff --git a/sys/net/network_layer/fib/fib.c b/sys/net/network_layer/fib/fib.c index d42253871c5ceb210197cd0cfa44c1091e7f26c4..18bfc121d686dcfb9c8a48c8f8a8c884eb864289 100644 --- a/sys/net/network_layer/fib/fib.c +++ b/sys/net/network_layer/fib/fib.c @@ -132,15 +132,18 @@ static int fib_find_entry(fib_table_t *table, uint8_t *dst, size_t dst_size, } else { /* we try to find the most fitting prefix */ - if (ret_comp == 1) { - entry_arr[0] = &(table->data.entries[i]); - /* we could find a better one so we move on */ - ret = 0; - - prefix_size = match_size; - match_size = dst_size << 3; - count = 1; + if ((ret_comp == 1) + && (table->data.entries[i].global_flags & FIB_FLAG_NET_PREFIX)) { + if ((prefix_size == 0) || (match_size > prefix_size)) { + entry_arr[0] = &(table->data.entries[i]); + /* we could find a better one so we move on */ + ret = 0; + + prefix_size = match_size; + count = 1; + } } + match_size = dst_size<<3; } } } @@ -1499,6 +1502,7 @@ void fib_print_routes(fib_table_t *table) { mutex_lock(&(table->mtx_access)); uint64_t now = xtimer_now64(); + if (table->table_type == FIB_TABLE_TYPE_SH) { printf("%-" FIB_ADDR_PRINT_LENS "s %-6s %-" FIB_ADDR_PRINT_LENS "s %-6s %-16s Interface\n" , "Destination", "Flags", "Next Hop", "Flags", "Expires"); @@ -1507,9 +1511,14 @@ void fib_print_routes(fib_table_t *table) if (table->data.entries[i].lifetime != 0) { fib_print_address(table->data.entries[i].global); printf(" 0x%04"PRIx32" ", table->data.entries[i].global_flags); + if(table->data.entries[i].global_flags & FIB_FLAG_NET_PREFIX) { + printf("N "); + } else { + printf("H "); + } + fib_print_address(table->data.entries[i].next_hop); printf(" 0x%04"PRIx32" ", table->data.entries[i].next_hop_flags); - if (table->data.entries[i].lifetime != FIB_LIFETIME_NO_EXPIRE) { uint64_t tm = table->data.entries[i].lifetime - now; diff --git a/tests/unittests/tests-fib/tests-fib.c b/tests/unittests/tests-fib/tests-fib.c index a9bd191de0e22e58c425888341035b5082987b06..9c68c28b13fbe3c7ca8d38cbb7beeb192799723d 100644 --- a/tests/unittests/tests-fib/tests-fib.c +++ b/tests/unittests/tests-fib/tests-fib.c @@ -492,20 +492,24 @@ static void test_fib_14_exact_and_prefix_match(void) snprintf(addr_dst, add_buf_size, "Test addr12"); snprintf(addr_nxt, add_buf_size, "Test address %02d", 12); + fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, - add_buf_size - 1, 0x12, (uint8_t *)addr_nxt, add_buf_size - 1, + add_buf_size - 1, (FIB_FLAG_NET_PREFIX | 0x12), + (uint8_t *)addr_nxt, add_buf_size - 1, 0x12, 100000); snprintf(addr_dst, add_buf_size, "Test addr123"); snprintf(addr_nxt, add_buf_size, "Test address %02d", 23); fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, - add_buf_size - 1, 0x123, (uint8_t *)addr_nxt, add_buf_size - 1, + add_buf_size - 1, (FIB_FLAG_NET_PREFIX | 0x123), + (uint8_t *)addr_nxt, add_buf_size - 1, 0x23, 100000); snprintf(addr_dst, add_buf_size, "Test addr1234"); snprintf(addr_nxt, add_buf_size, "Test address %02d", 34); fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, - add_buf_size - 1, 0x1234, (uint8_t *)addr_nxt, add_buf_size - 1, + add_buf_size - 1, (FIB_FLAG_NET_PREFIX | 0x1234), + (uint8_t *)addr_nxt, add_buf_size - 1, 0x34, 100000); memset(addr_lookup, 0, add_buf_size); @@ -609,12 +613,14 @@ static void test_fib_16_prefix_match(void) addr_lookup[14] = (char)0x87; /* 1000 0111 */ fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, - add_buf_size - 1, 0x123, (uint8_t *)addr_nxt, add_buf_size - 1, + add_buf_size - 1, (FIB_FLAG_NET_PREFIX | 0x123), + (uint8_t *)addr_nxt, add_buf_size - 1, 0x23, 100000); addr_dst[14] = (char)0x3c; /* 0011 1100 */ fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, - add_buf_size - 1, 0x123, (uint8_t *)addr_nxt, add_buf_size - 1, + add_buf_size - 1, (FIB_FLAG_NET_PREFIX | 0x123), + (uint8_t *)addr_nxt, add_buf_size - 1, 0x23, 100000); memset(addr_nxt, 0, add_buf_size); @@ -631,7 +637,8 @@ static void test_fib_16_prefix_match(void) add_buf_size = 16; fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, - add_buf_size - 1, 0x123, (uint8_t *)addr_nxt, add_buf_size - + add_buf_size - 1, (FIB_FLAG_NET_PREFIX | 0x123), + (uint8_t *)addr_nxt, add_buf_size - 1, 0x23, 100000); memset(addr_nxt, 0, add_buf_size); @@ -687,6 +694,9 @@ static void test_fib_17_get_entry_set(void) fib_destination_set_entry_t arr_dst[arr_size]; char prefix[addr_buf_size]; memset(prefix,0, addr_buf_size); + /* cppcheck: prefix is set to all 0 before adding an address + */ + /* cppcheck-suppress redundantCopy */ snprintf(prefix, addr_buf_size, "Test address 1"); int ret = fib_get_destination_set(&test_fib_table, @@ -699,6 +709,9 @@ static void test_fib_17_get_entry_set(void) arr_size = 20; memset(prefix,0, addr_buf_size); + /* cppcheck: prefix is set to all 0 before adding an address + */ + /* cppcheck-suppress redundantCopy */ snprintf(prefix, addr_buf_size, "Test address 0"); ret = fib_get_destination_set(&test_fib_table, @@ -800,7 +813,8 @@ static void test_fib_19_default_gateway(void) /* add a default gateway entry */ fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, - add_buf_size, 0x123, (uint8_t *)addr_nxt, add_buf_size, 0x23, + add_buf_size, (FIB_FLAG_NET_PREFIX | 0x123), + (uint8_t *)addr_nxt, add_buf_size, 0x23, 100000); /* check if it matches all */ @@ -876,7 +890,8 @@ static void test_fib_20_replace_prefix(void) /* add a prefix entry */ fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, - add_buf_size, 0x123, (uint8_t *)addr_nxt, add_buf_size, 0x23, + add_buf_size, (FIB_FLAG_NET_PREFIX | 0x123), + (uint8_t *)addr_nxt, add_buf_size, 0x23, 100000); /* check if it matches */ @@ -905,7 +920,8 @@ static void test_fib_20_replace_prefix(void) /* change the prefix entry */ fib_add_entry(&test_fib_table, 42, (uint8_t *)addr_dst, - add_buf_size, 0x123, (uint8_t *)addr_nxt, add_buf_size, 0x24, + add_buf_size, (FIB_FLAG_NET_PREFIX | 0x123), + (uint8_t *)addr_nxt, add_buf_size, 0x24, 100000); /* and check again if it matches */