fib.c 55.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
/**
 * Copyright (C) 2014 Martin Landsmann <Martin.Landsmann@HAW-Hamburg.de>
 *
 * This file is subject to the terms and conditions of the GNU Lesser
 * General Public License v2.1. See the file LICENSE in the top level
 * directory for more details.
 */

/**
 * @ingroup     net_fib
 * @{
 *
 * @file
 * @brief       Functions to manage FIB entries
 *
 * @author      Martin Landsmann <martin.landsmann@haw-hamburg.de>
 * @author      Oliver Hahm <oliver.hahm@inria.fr>
 *
 * @}
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <errno.h>
#include "thread.h"
#include "mutex.h"
#include "msg.h"
#include "xtimer.h"
#include "timex.h"
#include "utlist.h"

#define ENABLE_DEBUG (0)
#include "debug.h"

#include "net/fib.h"
#include "net/fib/table.h"

#ifdef MODULE_IPV6_ADDR
#include "net/ipv6/addr.h"
static char addr_str[IPV6_ADDR_MAX_STR_LEN];
#endif

#ifdef MODULE_IPV6_ADDR
    #define FIB_ADDR_PRINT_LEN      39
#else
    #define FIB_ADDR_PRINT_LEN      32
    #if FIB_ADDR_PRINT_LEN != (UNIVERSAL_ADDRESS_SIZE * 2)
        #error "FIB_ADDR_PRINT_LEN MUST BE (UNIVERSAL_ADDRESS_SIZE * 2)"
    #endif
#endif

#define FIB_ADDR_PRINT_LENS1(X)     #X
#define FIB_ADDR_PRINT_LENS2(X)     FIB_ADDR_PRINT_LENS1(X)
#define FIB_ADDR_PRINT_LENS         FIB_ADDR_PRINT_LENS2(FIB_ADDR_PRINT_LEN)

/**
 * @brief convert an offset given in ms to abolute time in time in us
 * @param[in]  ms       the milliseconds to be converted
 * @param[out] target   the converted point in time
 */
static void fib_lifetime_to_absolute(uint32_t ms, uint64_t *target)
{
    *target = xtimer_now_usec64() + (ms * MS_IN_USEC);
}

/**
 * @brief returns pointer to the entry for the given destination address
 *
 * @param[in] table                the FIB table to search in
 * @param[in] dst                  the destination address
 * @param[in] dst_size             the destination address size
 * @param[out] entry_arr           the array to scribe the found match
 * @param[in, out] entry_arr_size  the number of entries provided by entry_arr (should be always 1)
 *                                 this value is overwritten with the actual found number
 *
 * @return 0 if we found a next-hop prefix
 *         1 if we found the exact address next-hop
 *         -EHOSTUNREACH if no fitting next-hop is available
 */
static int fib_find_entry(fib_table_t *table, uint8_t *dst, size_t dst_size,
                          fib_entry_t **entry_arr, size_t *entry_arr_size) {
    uint64_t now = xtimer_now_usec64();

    size_t count = 0;
    size_t prefix_size = 0;
    size_t match_size = dst_size << 3;
    int ret = -EHOSTUNREACH;
    bool is_all_zeros_addr = true;

#if ENABLE_DEBUG
    DEBUG("[fib_find_entry] dst =");
    for (size_t i = 0; i < dst_size; i++) {
        DEBUG(" %02x", dst[i]);
    }
    DEBUG("\n");
#endif

    for (size_t i = 0; i < dst_size; ++i) {
        if (dst[i] != 0) {
            is_all_zeros_addr = false;
            break;
        }
    }

    for (size_t i = 0; i < table->size; ++i) {

        /* autoinvalidate if the entry lifetime is not set to not expire */
        if (table->data.entries[i].lifetime != FIB_LIFETIME_NO_EXPIRE) {

            /* check if the lifetime expired */
            if (table->data.entries[i].lifetime < now) {
                /* remove this entry if its lifetime expired */
                table->data.entries[i].lifetime = 0;
                table->data.entries[i].global_flags = 0;
                table->data.entries[i].next_hop_flags = 0;
                table->data.entries[i].iface_id = KERNEL_PID_UNDEF;

                if (table->data.entries[i].global != NULL) {
                    universal_address_rem(table->data.entries[i].global);
                    table->data.entries[i].global = NULL;
                }

                if (table->data.entries[i].next_hop != NULL) {
                    universal_address_rem(table->data.entries[i].next_hop);
                    table->data.entries[i].next_hop = NULL;
                }
            }
        }

        if ((prefix_size < (dst_size<<3)) && (table->data.entries[i].global != NULL)) {

            int ret_comp = universal_address_compare(table->data.entries[i].global, dst, &match_size);
            /* If we found an exact match */
            if ((ret_comp == UNIVERSAL_ADDRESS_EQUAL)
                || (is_all_zeros_addr && (ret_comp == UNIVERSAL_ADDRESS_IS_ALL_ZERO_ADDRESS))) {
                entry_arr[0] = &(table->data.entries[i]);
                *entry_arr_size = 1;
                /* we will not find a better one so we return */
                return 1;
            }
            else {
                /* we try to find the most fitting prefix */
                if (ret_comp == UNIVERSAL_ADDRESS_MATCHING_PREFIX) {
                    if (table->data.entries[i].global_flags & FIB_FLAG_NET_PREFIX_MASK) {
                        /* we shift the most upper flag byte back to get the number of prefix bits */
                        size_t global_prefix_len = (table->data.entries[i].global_flags
                                                    & FIB_FLAG_NET_PREFIX_MASK) >> FIB_FLAG_NET_PREFIX_SHIFT;

                        if ((match_size >= global_prefix_len) &&
                            ((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;
                        }
                    }
                 }
                 else if (ret_comp == UNIVERSAL_ADDRESS_IS_ALL_ZERO_ADDRESS) {
                    /* we found the default gateway entry, e.g. ::/0 for IPv6
                     * and we keep it only if there is no better one
                     */
                    if (prefix_size == 0) {
                        entry_arr[0] = &(table->data.entries[i]);
                        /* we could find a better one so we move on */
                        ret = 0;
                        count = 1;
                    }
                }
                match_size = dst_size<<3;
            }
        }
    }

#if ENABLE_DEBUG
    if (count > 0) {
        DEBUG("[fib_find_entry] found prefix on interface %d:", entry_arr[0]->iface_id);
        for (size_t i = 0; i < entry_arr[0]->global->address_size; i++) {
            DEBUG(" %02x", entry_arr[0]->global->address[i]);
        }
        DEBUG("\n");
    }
#endif

    *entry_arr_size = count;
    return ret;
}

/**
 * @brief updates the next hop the lifetime and the interface id for a given entry
 *
 * @param[in] entry          the entry to be updated
 * @param[in] next_hop       the next hop address to be updated
 * @param[in] next_hop_size  the next hop address size
 * @param[in] next_hop_flags the next-hop address flags
 * @param[in] lifetime       the lifetime in ms
 *
 * @return 0 if the entry has been updated
 *         -ENOMEM if the entry cannot be updated due to insufficient RAM
 */
static int fib_upd_entry(fib_entry_t *entry, uint8_t *next_hop,
                         size_t next_hop_size, uint32_t next_hop_flags,
                         uint32_t lifetime)
{
    universal_address_container_t *container = universal_address_add(next_hop, next_hop_size);

    if (container == NULL) {
        return -ENOMEM;
    }

    universal_address_rem(entry->next_hop);
    entry->next_hop = container;
    entry->next_hop_flags = next_hop_flags;

    if (lifetime != (uint32_t)FIB_LIFETIME_NO_EXPIRE) {
        fib_lifetime_to_absolute(lifetime, &entry->lifetime);
    }
    else {
        entry->lifetime = FIB_LIFETIME_NO_EXPIRE;
    }

    return 0;
}

/**
 * @brief creates a new FIB entry with the provided parameters
 *
 * @param[in] table          the FIB table to create the entry in
 * @param[in] iface_id       the interface ID
 * @param[in] dst            the destination address
 * @param[in] dst_size       the destination address size
 * @param[in] dst_flags      the destination address flags
 * @param[in] next_hop       the next hop address
 * @param[in] next_hop_size  the next hop address size
 * @param[in] next_hop_flags the next-hop address flags
 * @param[in] lifetime       the lifetime in ms
 *
 * @return 0 on success
 *         -ENOMEM if no new entry can be created
 */
static int fib_create_entry(fib_table_t *table, kernel_pid_t iface_id,
                            uint8_t *dst, size_t dst_size, uint32_t dst_flags,
                            uint8_t *next_hop, size_t next_hop_size, uint32_t
                            next_hop_flags, uint32_t lifetime)
{
    for (size_t i = 0; i < table->size; ++i) {
        if (table->data.entries[i].lifetime == 0) {

            table->data.entries[i].global = universal_address_add(dst, dst_size);

            if (table->data.entries[i].global != NULL) {
                table->data.entries[i].global_flags = dst_flags;
                table->data.entries[i].next_hop = universal_address_add(next_hop, next_hop_size);
                table->data.entries[i].next_hop_flags = next_hop_flags;
            }

            if (table->data.entries[i].next_hop != NULL) {
                /* everything worked fine */
                table->data.entries[i].iface_id = iface_id;

                if (lifetime != (uint32_t) FIB_LIFETIME_NO_EXPIRE) {
                    fib_lifetime_to_absolute(lifetime, &table->data.entries[i].lifetime);
                }
                else {
                    table->data.entries[i].lifetime = FIB_LIFETIME_NO_EXPIRE;
                }

                return 0;
            }
        }
    }

    return -ENOMEM;
}

/**
 * @brief removes the given entry
 *
 * @param[in] entry the entry to be removed
 *
 * @return 0 on success
 */
static int fib_remove(fib_entry_t *entry)
{
    if (entry->global != NULL) {
        universal_address_rem(entry->global);
    }

    if (entry->next_hop) {
        universal_address_rem(entry->next_hop);
    }

    entry->global = NULL;
    entry->global_flags = 0;
    entry->next_hop = NULL;
    entry->next_hop_flags = 0;

    entry->iface_id = KERNEL_PID_UNDEF;
    entry->lifetime = 0;

    return 0;
}

/**
 * @brief signals (sends a message to) all registered routing protocols
 *        registered with a matching prefix (usually this should be only one).
 *        The receiver MUST copy the content, i.e. the address before reply.
 *
 * @param[in] table     the fib instance to use
 * @param[in] type      the kind of signal
 * @param[in] dat       the data to send
 * @param[in] dat_size  the data size in bytes
 * @param[in] dat_flags the data flags
 *
 * @return 0 on a new available entry,
 *         -ENOENT if no suiting entry is provided.
 */
static int fib_signal_rp(fib_table_t *table, uint16_t type, uint8_t *dat,
                         size_t dat_size, uint32_t dat_flags)
{
    msg_t msg, reply;
    rp_address_msg_t rp_addr_msg;
    int ret = -ENOENT;
    void *content = NULL;

    if (type != FIB_MSG_RP_SIGNAL_SOURCE_ROUTE_CREATED) {
        /* the passed data is an address */
        rp_addr_msg.address = dat;
        rp_addr_msg.address_size = dat_size;
        rp_addr_msg.address_flags = dat_flags;
        content = (void *)&rp_addr_msg;
    }
    else {
        /* the passed data is a sr head
         * dat_size and dat_flags are not used in this case
         */
        content = (void *)dat;
    }

    msg.type = type;
    msg.content.ptr = content;

    for (size_t i = 0; i < FIB_MAX_REGISTERED_RP; ++i) {
        if (table->notify_rp[i] != KERNEL_PID_UNDEF) {
            DEBUG("[fib_signal_rp] send msg@: %p to pid[%d]: %d\n", \
                  msg.content.ptr, (int)i, (int)(table->notify_rp[i]));

            /* do only signal a RP if its registered prefix matches */
            if (type != FIB_MSG_RP_SIGNAL_SOURCE_ROUTE_CREATED) {
                size_t dat_size_in_bits = dat_size<<3;
                if (universal_address_compare(table->prefix_rp[i], dat,
                                              &dat_size_in_bits) != -ENOENT) {
                    /* the receiver, i.e. the RP, MUST copy the content value.
                     * using the provided pointer after replying this message
                     * will lead to errors
                     */
                    msg_send_receive(&msg, &reply, table->notify_rp[i]);
                    DEBUG("[fib_signal_rp] got reply.\n");
                    ret = 0;
                }
            }
            else {
                fib_sr_t *temp_sr = (fib_sr_t *)dat;
                size_t dat_size_in_bits = temp_sr->sr_dest->address->address_size << 3;
                if (universal_address_compare(table->prefix_rp[i],
                                              temp_sr->sr_dest->address->address,
                                              &dat_size_in_bits) != -ENOENT) {
                    /* the receiver, i.e. the RP, MUST copy the content value.
                     * using the provided pointer after replying this message
                     * will lead to errors
                     */
                    msg_send_receive(&msg, &reply, table->notify_rp[i]);
                    DEBUG("[fib_signal_rp] got reply.\n");
                    ret = 0;
                }
            }
        }
    }

    return ret;
}

int fib_add_entry(fib_table_t *table,
                  kernel_pid_t iface_id, uint8_t *dst, size_t dst_size,
                  uint32_t dst_flags, uint8_t *next_hop, size_t next_hop_size,
                  uint32_t next_hop_flags, uint32_t lifetime)
{
    mutex_lock(&(table->mtx_access));
    DEBUG("[fib_add_entry]\n");
    size_t count = 1;
    fib_entry_t *entry[count];

    /* check if dst and next_hop are valid pointers */
    if ((dst == NULL) || (next_hop == NULL)) {
        mutex_unlock(&(table->mtx_access));
        return -EFAULT;
    }

    int ret = fib_find_entry(table, dst, dst_size, &(entry[0]), &count);

    if (ret == 1) {
        /* we must take the according entry and update the values */
        ret = fib_upd_entry(entry[0], next_hop, next_hop_size, next_hop_flags, lifetime);
    }
    else {
        ret = fib_create_entry(table, iface_id, dst, dst_size, dst_flags,
                               next_hop, next_hop_size, next_hop_flags, lifetime);
    }

    mutex_unlock(&(table->mtx_access));
    return ret;
}

int fib_update_entry(fib_table_t *table, uint8_t *dst, size_t dst_size,
                     uint8_t *next_hop, size_t next_hop_size,
                     uint32_t next_hop_flags, uint32_t lifetime)
{
    mutex_lock(&(table->mtx_access));
    DEBUG("[fib_update_entry]\n");
    size_t count = 1;
    fib_entry_t *entry[count];
    int ret = -ENOMEM;

    /* check if dst and next_hop are valid pointers */
    if ((dst == NULL) || (next_hop == NULL)) {
        mutex_unlock(&(table->mtx_access));
        return -EFAULT;
    }

    if (fib_find_entry(table, dst, dst_size, &(entry[0]), &count) == 1) {
        DEBUG("[fib_update_entry] found entry: %p\n", (void *)(entry[0]));
        /* we must take the according entry and update the values */
        ret = fib_upd_entry(entry[0], next_hop, next_hop_size, next_hop_flags, lifetime);
    }
    else {
        /* we have ambiguous entries, i.e. count > 1
         * this should never happen
         */
        DEBUG("[fib_update_entry] ambigious entries detected!!!\n");
    }

    mutex_unlock(&(table->mtx_access));
    return ret;
}

void fib_remove_entry(fib_table_t *table, uint8_t *dst, size_t dst_size)
{
    mutex_lock(&(table->mtx_access));
    DEBUG("[fib_remove_entry]\n");
    size_t count = 1;
    fib_entry_t *entry[count];

    int ret = fib_find_entry(table, dst, dst_size, &(entry[0]), &count);

    if (ret == 1) {
        /* we must take the according entry and update the values */
        fib_remove(entry[0]);
    }
    else {
        /* we have ambiguous entries, i.e. count > 1
         * this should never happen
         */
        DEBUG("[fib_update_entry] ambigious entries detected!!!\n");
    }

    mutex_unlock(&(table->mtx_access));
}

void fib_flush(fib_table_t *table, kernel_pid_t interface)
{
    mutex_lock(&(table->mtx_access));
    DEBUG("[fib_flush]\n");

    for (size_t i = 0; i < table->size; ++i) {
        if ((interface == KERNEL_PID_UNDEF) ||
            (interface == table->data.entries[i].iface_id)) {
            fib_remove(&table->data.entries[i]);
        }
    }

    mutex_unlock(&(table->mtx_access));
}

int fib_get_next_hop(fib_table_t *table, kernel_pid_t *iface_id,
                     uint8_t *next_hop, size_t *next_hop_size,
                     uint32_t *next_hop_flags, uint8_t *dst, size_t dst_size,
                     uint32_t dst_flags)
{
    mutex_lock(&(table->mtx_access));
    DEBUG("[fib_get_next_hop]\n");
    size_t count = 1;
    fib_entry_t *entry[count];

    if ((iface_id == NULL)
        || (next_hop_size == NULL)
        || (next_hop_flags == NULL)) {
            mutex_unlock(&(table->mtx_access));
            return -EINVAL;
        }

    if ((dst == NULL) || (next_hop == NULL)) {
        mutex_unlock(&(table->mtx_access));
        return -EFAULT;
    }

    int ret = fib_find_entry(table, dst, dst_size, &(entry[0]), &count);
    if (!(ret == 0 || ret == 1)) {
        /* notify all responsible RPs for unknown  next-hop for the destination address */
        if (fib_signal_rp(table, FIB_MSG_RP_SIGNAL_UNREACHABLE_DESTINATION,
                          dst, dst_size, dst_flags) == 0) {
            count = 1;
            /* now lets see if the RRPs have found a valid next-hop */
            ret = fib_find_entry(table, dst, dst_size, &(entry[0]), &count);
        }
    }

    if (ret == 0 || ret == 1) {

        uint8_t *address_ret = universal_address_get_address(entry[0]->next_hop,
                               next_hop, next_hop_size);

        if (address_ret == NULL) {
            mutex_unlock(&(table->mtx_access));
            return -ENOBUFS;
        }
    }
    else {
        mutex_unlock(&(table->mtx_access));
        return -EHOSTUNREACH;
    }

    *iface_id = entry[0]->iface_id;
    *next_hop_flags = entry[0]->next_hop_flags;
    mutex_unlock(&(table->mtx_access));
    return 0;
}

int fib_get_destination_set(fib_table_t *table, uint8_t *prefix,
                            size_t prefix_size,
                            fib_destination_set_entry_t *dst_set,
                            size_t* dst_set_size)
{
    mutex_lock(&(table->mtx_access));
    int ret = -EHOSTUNREACH;
    size_t found_entries = 0;

    for (size_t i = 0; i < table->size; ++i) {
        if ((table->data.entries[i].global != NULL) &&
            (universal_address_compare_prefix(table->data.entries[i].global, prefix, prefix_size<<3) >= UNIVERSAL_ADDRESS_EQUAL)) {
            if( (dst_set != NULL) && (found_entries < *dst_set_size) ) {
            /* set the size to full byte usage */
            dst_set[found_entries].dest_size = sizeof(dst_set[found_entries].dest);
            universal_address_get_address(table->data.entries[i].global,
                                          dst_set[found_entries].dest,
                                          &dst_set[found_entries].dest_size);
            }
            found_entries++;
        }
    }

    if (found_entries > *dst_set_size) {
        ret = -ENOBUFS;
    }
    else if (found_entries > 0) {
        ret = 0;
    }

    *dst_set_size = found_entries;

    mutex_unlock(&(table->mtx_access));

    return ret;
}

void fib_init(fib_table_t *table)
{
    DEBUG("[fib_init] hello. Initializing some stuff.\n");
    mutex_init(&(table->mtx_access));
    mutex_lock(&(table->mtx_access));

    for (size_t i = 0; i < FIB_MAX_REGISTERED_RP; ++i) {
        table->notify_rp[i] = KERNEL_PID_UNDEF;
        table->prefix_rp[i] = NULL;
    }

    table->notify_rp_pos = 0;

    if (table->table_type == FIB_TABLE_TYPE_SR) {
        memset(table->data.source_routes->headers, 0,
               sizeof(fib_sr_t) * table->size);
        memset(table->data.source_routes->entry_pool, 0,
               sizeof(fib_sr_entry_t) * table->data.source_routes->entry_pool_size);
    }
    else {
        memset(table->data.entries, 0, (table->size * sizeof(fib_entry_t)));
    }
    universal_address_init();
    mutex_unlock(&(table->mtx_access));
}

void fib_deinit(fib_table_t *table)
{
    DEBUG("[fib_deinit] hello. De-Initializing stuff.\n");
    mutex_lock(&(table->mtx_access));

    for (size_t i = 0; i < FIB_MAX_REGISTERED_RP; ++i) {
        table->notify_rp[i] = KERNEL_PID_UNDEF;
        table->prefix_rp[i] = NULL;
    }

    table->notify_rp_pos = 0;

    if (table->table_type == FIB_TABLE_TYPE_SR) {
        memset(table->data.source_routes->headers, 0,
               sizeof(fib_sr_t) * table->size);
        memset(table->data.source_routes->entry_pool, 0,
               sizeof(fib_sr_entry_t) * table->data.source_routes->entry_pool_size);
    }
    else {
        memset(table->data.entries, 0, (table->size * sizeof(fib_entry_t)));
    }
    universal_address_reset();
    mutex_unlock(&(table->mtx_access));
}

int fib_register_rp(fib_table_t *table, uint8_t *prefix, size_t prefix_addr_type_size)
{
    mutex_lock(&(table->mtx_access));

    if (table->notify_rp_pos >= FIB_MAX_REGISTERED_RP) {
        mutex_unlock(&(table->mtx_access));
        return -ENOMEM;
    }

    if ((prefix == NULL) || (prefix_addr_type_size == 0)) {
        mutex_unlock(&(table->mtx_access));
        return -EINVAL;
    }

    if (table->notify_rp_pos < FIB_MAX_REGISTERED_RP) {
        table->notify_rp[table->notify_rp_pos] = sched_active_pid;
        universal_address_container_t *container = universal_address_add(prefix,
                                                                         prefix_addr_type_size);
        table->prefix_rp[table->notify_rp_pos] = container;
        table->notify_rp_pos++;
    }

    mutex_unlock(&(table->mtx_access));
    return 0;
}

int fib_get_num_used_entries(fib_table_t *table)
{
    mutex_lock(&(table->mtx_access));
    size_t used_entries = 0;

    for (size_t i = 0; i < table->size; ++i) {
        used_entries += (size_t)(table->data.entries[i].global != NULL);
    }

    mutex_unlock(&(table->mtx_access));
    return used_entries;
}

/* source route handling */
int fib_sr_create(fib_table_t *table, fib_sr_t **fib_sr, kernel_pid_t sr_iface_id,
                  uint32_t sr_flags, uint32_t sr_lifetime)
{
    mutex_lock(&(table->mtx_access));
    if ((fib_sr == NULL) || (sr_lifetime == 0)) {
        mutex_unlock(&(table->mtx_access));
        return -EFAULT;
    }

    for (size_t i = 0; i < table->size; ++i) {
        if (table->data.source_routes->headers[i].sr_lifetime == 0) {
            table->data.source_routes->headers[i].sr_iface_id = sr_iface_id;
            table->data.source_routes->headers[i].sr_flags = sr_flags;
            table->data.source_routes->headers[i].sr_path = NULL;
            table->data.source_routes->headers[i].sr_dest = NULL;
            if (sr_lifetime < (uint32_t)FIB_LIFETIME_NO_EXPIRE) {
                fib_lifetime_to_absolute(sr_lifetime,
                                         &table->data.source_routes->headers[i].sr_lifetime);
            }
            else {
                table->data.source_routes->headers[i].sr_lifetime = FIB_LIFETIME_NO_EXPIRE;
            }
            *fib_sr = &table->data.source_routes->headers[i];
            mutex_unlock(&(table->mtx_access));
            return 0;
        }
    }

    mutex_unlock(&(table->mtx_access));
    return -ENOBUFS;
}

/**
* @brief Internal function:
*        checks the lifetime and removes the entry in case it expired
*/
static int fib_sr_check_lifetime(fib_sr_t *fib_sr)
{
    uint64_t tm = fib_sr->sr_lifetime - xtimer_now_usec64();
    /* check if the lifetime expired */
    if ((int64_t)tm < 0) {
        /* remove this sr if its lifetime expired */
        fib_sr->sr_lifetime = 0;

        if (fib_sr->sr_path != NULL) {
            fib_sr_entry_t *elt;
            LL_FOREACH(fib_sr->sr_path, elt) {
                universal_address_rem(elt->address);
            }
            fib_sr->sr_path = NULL;
        }

        /* and return an errorcode */
        return -ENOENT;
    }
    return 0;
}

/**
* @brief Internal function:
*        creates a new entry in the table entry pool for a hop in a source route
*/
static int fib_sr_new_entry(fib_table_t *table, uint8_t *addr, size_t addr_size,
                            fib_sr_entry_t **new_entry)
{
    for (size_t i = 0; i < table->data.source_routes->entry_pool_size; ++i) {
        if (table->data.source_routes->entry_pool[i].address == NULL) {
            table->data.source_routes->entry_pool[i].address = universal_address_add(addr, addr_size);
            if (table->data.source_routes->entry_pool[i].address == NULL) {
                return -ENOMEM;
            }
            else {
                (void)new_entry;
                *new_entry = &table->data.source_routes->entry_pool[i];
                return 0;
            }
        }
    }
    return -ENOMEM;
}

/**
* @brief Internal function:
*        checks if the source route belongs to the given table
*/
static int fib_is_sr_in_table(fib_table_t *table, fib_sr_t *fib_sr)
{
    for (size_t i = 0; i < table->size; ++i) {
        if (&(table->data.source_routes->headers[i]) == fib_sr) {
            return 0;
        }
    }
    return -ENOENT;
}

int fib_sr_read_head(fib_table_t *table, fib_sr_t *fib_sr, kernel_pid_t *iface_id,
                     uint32_t *sr_flags, uint32_t *sr_lifetime)
{
    mutex_lock(&(table->mtx_access));
    if ((fib_sr == NULL) || (iface_id == NULL) || (sr_flags == NULL)
        || (sr_lifetime == NULL) || (fib_is_sr_in_table(table, fib_sr) == -ENOENT) ) {
        mutex_unlock(&(table->mtx_access));
        return -EFAULT;
    }

    if (fib_sr_check_lifetime(fib_sr) == -ENOENT) {
        mutex_unlock(&(table->mtx_access));
        return -ENOENT;
    }

    *iface_id = fib_sr->sr_iface_id;
    *sr_flags = fib_sr->sr_flags;
    *sr_lifetime = fib_sr->sr_lifetime - xtimer_now_usec64();

    mutex_unlock(&(table->mtx_access));
    return 0;
}

int fib_sr_read_destination(fib_table_t *table, fib_sr_t *fib_sr,
                            uint8_t *dst, size_t *dst_size)
{
    mutex_lock(&(table->mtx_access));
    if ((fib_sr == NULL) || (dst == NULL) || (dst_size == NULL)
        || (fib_is_sr_in_table(table, fib_sr) == -ENOENT)) {
        mutex_unlock(&(table->mtx_access));
        return -EFAULT;
    }

    if (fib_sr_check_lifetime(fib_sr) == -ENOENT) {
        mutex_unlock(&(table->mtx_access));
        return -ENOENT;
    }

    if (fib_sr->sr_dest == NULL) {
        mutex_unlock(&(table->mtx_access));
        return -EHOSTUNREACH;
    }

    if (universal_address_get_address(fib_sr->sr_dest->address, dst, dst_size) == NULL) {
        mutex_unlock(&(table->mtx_access));
        return -ENOBUFS;
    }

    mutex_unlock(&(table->mtx_access));
    return 0;
}

int fib_sr_set(fib_table_t *table, fib_sr_t *fib_sr, kernel_pid_t *sr_iface_id,
               uint32_t *sr_flags, uint32_t *sr_lifetime)
{
    mutex_lock(&(table->mtx_access));
    if ((fib_sr == NULL) || (fib_is_sr_in_table(table, fib_sr) == -ENOENT)) {
        mutex_unlock(&(table->mtx_access));
        return -EFAULT;
    }

    if (fib_sr_check_lifetime(fib_sr) == -ENOENT) {
        mutex_unlock(&(table->mtx_access));
        return -ENOENT;
    }

    if (sr_iface_id != NULL) {
        fib_sr->sr_iface_id = *sr_iface_id;
    }

    if (sr_flags != NULL) {
        fib_sr->sr_flags = *sr_flags;
    }

    if (sr_lifetime != NULL) {
        fib_lifetime_to_absolute(*sr_lifetime, &(fib_sr->sr_lifetime));
    }

    mutex_unlock(&(table->mtx_access));
    return 0;
}

int fib_sr_delete(fib_table_t *table, fib_sr_t *fib_sr)
{
    mutex_lock(&(table->mtx_access));
    if ((fib_sr == NULL) || (fib_is_sr_in_table(table, fib_sr) == -ENOENT)) {
        mutex_unlock(&(table->mtx_access));
        return -EFAULT;
    }

    fib_sr->sr_lifetime = 0;

    if (fib_sr->sr_path != NULL) {
        fib_sr_entry_t *elt, *tmp;
        LL_FOREACH_SAFE(fib_sr->sr_path, elt, tmp) {
            universal_address_rem(elt->address);
            elt->address = NULL;
            LL_DELETE(fib_sr->sr_path, elt);
        }
        fib_sr->sr_path = NULL;
    }

    mutex_unlock(&(table->mtx_access));
    return 0;
}

int fib_sr_next(fib_table_t *table, fib_sr_t *fib_sr, fib_sr_entry_t **sr_path_entry)
{
    mutex_lock(&(table->mtx_access));
    if ((fib_sr == NULL) || (sr_path_entry == NULL)
        || (fib_is_sr_in_table(table, fib_sr) == -ENOENT)) {
        mutex_unlock(&(table->mtx_access));
        return -EFAULT;
    }

    if (fib_sr->sr_path == NULL) {
        mutex_unlock(&(table->mtx_access));
        return -EFAULT;
    }

    if (fib_sr_check_lifetime(fib_sr) == -ENOENT) {
        mutex_unlock(&(table->mtx_access));
        return -ENOENT;
    }

    /* if we reach the destination entry, i.e. the last entry we just return 1 */
    if (*sr_path_entry == fib_sr->sr_dest) {
        mutex_unlock(&(table->mtx_access));
        return 1;
    }

    /* when we start, we pass the first entry */
    if (*sr_path_entry == NULL) {
        *sr_path_entry = fib_sr->sr_path;
    }
    else {
        /* in any other case we just return the next entry */
        *sr_path_entry = (*sr_path_entry)->next;
    }

    mutex_unlock(&(table->mtx_access));
    return 0;
}

int fib_sr_search(fib_table_t *table, fib_sr_t *fib_sr, uint8_t *addr, size_t addr_size,
                  fib_sr_entry_t **sr_path_entry)
{
    mutex_lock(&(table->mtx_access));
    if ((fib_sr == NULL) || (addr == NULL) || (sr_path_entry == NULL)
        || (fib_is_sr_in_table(table, fib_sr) == -ENOENT)) {
        mutex_unlock(&(table->mtx_access));
        return -EFAULT;
    }

    if (fib_sr_check_lifetime(fib_sr) == -ENOENT) {
        mutex_unlock(&(table->mtx_access));
        return -ENOENT;
    }

    fib_sr_entry_t *elt;
    LL_FOREACH(fib_sr->sr_path, elt) {
        size_t addr_size_match = addr_size << 3;
        if (universal_address_compare(elt->address, addr, &addr_size_match) == UNIVERSAL_ADDRESS_EQUAL) {

            /* temporary workaround to calm compiler */
            (void)sr_path_entry;

            *sr_path_entry = elt;
            mutex_unlock(&(table->mtx_access));
            return 0;
        }
    }

    mutex_unlock(&(table->mtx_access));
    return -EHOSTUNREACH;
}

int fib_sr_entry_append(fib_table_t *table, fib_sr_t *fib_sr,
                        uint8_t *addr, size_t addr_size)
{
    mutex_lock(&(table->mtx_access));
    if ((fib_sr == NULL) || (addr == NULL)
        || (fib_is_sr_in_table(table, fib_sr) == -ENOENT)) {
        mutex_unlock(&(table->mtx_access));
        return -EFAULT;
    }

    if (fib_sr_check_lifetime(fib_sr) == -ENOENT) {
        mutex_unlock(&(table->mtx_access));
        return -ENOENT;
    }

    fib_sr_entry_t *elt;
    LL_FOREACH(fib_sr->sr_path, elt) {
        size_t addr_size_match = addr_size << 3;
        if (universal_address_compare(elt->address, addr, &addr_size_match) == UNIVERSAL_ADDRESS_EQUAL) {
            mutex_unlock(&(table->mtx_access));
            return -EINVAL;
        }
    }

    fib_sr_entry_t *new_entry[1];
    int ret = fib_sr_new_entry(table, addr, addr_size, &new_entry[0]);

    if (ret == 0) {
        fib_sr_entry_t *tmp = fib_sr->sr_dest;
        if (tmp != NULL) {
            /* we append the new entry behind the former destination */
            tmp->next = new_entry[0];
        }
        else {
            /* this is also our first entry */
            fib_sr->sr_path = new_entry[0];
        }
        fib_sr->sr_dest = new_entry[0];
    }

    mutex_unlock(&(table->mtx_access));
    return ret;
}

int fib_sr_entry_add(fib_table_t *table, fib_sr_t *fib_sr,
                     fib_sr_entry_t *sr_path_entry, uint8_t *addr, size_t addr_size,
                     bool keep_remaining_route)
{
    mutex_lock(&(table->mtx_access));
    if ((fib_sr == NULL) || (sr_path_entry == NULL) || (addr == NULL)
        || (fib_is_sr_in_table(table, fib_sr) == -ENOENT)) {
        mutex_unlock(&(table->mtx_access));
        return -EFAULT;
    }

    if (fib_sr_check_lifetime(fib_sr) == -ENOENT) {
        mutex_unlock(&(table->mtx_access));
        return -ENOENT;
    }

    bool found = false;
    fib_sr_entry_t *elt;
    LL_FOREACH(fib_sr->sr_path, elt) {
        size_t addr_size_match = addr_size << 3;
        if (universal_address_compare(elt->address, addr, &addr_size_match) == UNIVERSAL_ADDRESS_EQUAL) {
            mutex_unlock(&(table->mtx_access));
            return -EINVAL;
        }
        if (sr_path_entry == elt) {
            found = true;
            break;
        }
    }

    int ret = -ENOENT;
    if (found) {
        fib_sr_entry_t *new_entry[1];
        ret = fib_sr_new_entry(table, addr, addr_size, &new_entry[0]);
        if (ret == 0) {
            fib_sr_entry_t *remaining = sr_path_entry->next;
            sr_path_entry->next = new_entry[0];
            if (keep_remaining_route) {
                new_entry[0]->next = remaining;
            }
            else {
                fib_sr_entry_t *elt, *tmp;
                LL_FOREACH_SAFE(remaining, elt, tmp) {
                    universal_address_rem(elt->address);
                    elt->address = NULL;
                    LL_DELETE(remaining, elt);
                }
                new_entry[0]->next = NULL;
                fib_sr->sr_dest = new_entry[0];
            }
        }
    }

    mutex_unlock(&(table->mtx_access));
    return ret;
}

int fib_sr_entry_delete(fib_table_t *table, fib_sr_t *fib_sr, uint8_t *addr, size_t addr_size,
                        bool keep_remaining_route)
{
    mutex_lock(&(table->mtx_access));
    if ((fib_sr == NULL) || (fib_is_sr_in_table(table, fib_sr) == -ENOENT)) {
        mutex_unlock(&(table->mtx_access));
        return -EFAULT;
    }

    if (fib_sr_check_lifetime(fib_sr) == -ENOENT) {
        mutex_unlock(&(table->mtx_access));
        return -ENOENT;
    }

    fib_sr_entry_t *elt, *tmp;
    tmp = fib_sr->sr_path;
    LL_FOREACH(fib_sr->sr_path, elt) {
        size_t addr_size_match = addr_size << 3;

        if (universal_address_compare(elt->address, addr, &addr_size_match) == UNIVERSAL_ADDRESS_EQUAL) {
            universal_address_rem(elt->address);
            if (keep_remaining_route) {
                tmp->next = elt->next;
            }
            else {
                fib_sr_entry_t *elt_del, *tmp_del;
                LL_FOREACH_SAFE(tmp, elt_del, tmp_del) {
                    universal_address_rem(elt_del->address);
                    elt_del->address = NULL;
                    LL_DELETE(tmp, elt_del);
                }
            }
            if (elt == fib_sr->sr_path) {
                /* if we remove the first entry we must adjust the path start */
                fib_sr->sr_path = elt->next;
            }
            if (elt == fib_sr->sr_dest) {
                /* if we remove the last entry we must adjust the destination */
                fib_sr->sr_dest = tmp;
            }
            mutex_unlock(&(table->mtx_access));
            return 0;
        }
        tmp = elt;
    }

    return -ENOENT;
}

int fib_sr_entry_overwrite(fib_table_t *table, fib_sr_t *fib_sr,
                           uint8_t *addr_old, size_t addr_old_size,
                           uint8_t *addr_new, size_t addr_new_size)
{
    mutex_lock(&(table->mtx_access));
    if ((fib_sr == NULL) || (addr_old == NULL) || (addr_new == NULL)
        || (fib_is_sr_in_table(table, fib_sr) == -ENOENT)) {
        mutex_unlock(&(table->mtx_access));
        return -EFAULT;
    }

    if (fib_sr_check_lifetime(fib_sr) == -ENOENT) {
        mutex_unlock(&(table->mtx_access));
        return -ENOENT;
    }

    fib_sr_entry_t *elt, *elt_repl;
    elt_repl = NULL;
    LL_FOREACH(fib_sr->sr_path, elt) {
        size_t addr_old_size_match = addr_old_size << 3;
        size_t addr_new_size_match = addr_old_size << 3;
        if (universal_address_compare(elt->address, addr_old, &addr_old_size_match) == UNIVERSAL_ADDRESS_EQUAL) {
            elt_repl = elt;
        }

        if (universal_address_compare(elt->address, addr_new, &addr_new_size_match) == UNIVERSAL_ADDRESS_EQUAL) {
            mutex_unlock(&(table->mtx_access));
            return -EINVAL;
        }
    }

    if (elt_repl != NULL) {
        universal_address_rem(elt_repl->address);
        universal_address_container_t *add = universal_address_add(addr_new, addr_new_size);

        if (add == NULL) {
            /* if this happened we deleted one entry, i.e. decreased the usecount
             * adding a new one was not possible since lack of memory
             * so we add back the old entry, i.e. increasing the usecount
             */
            universal_address_add(addr_old, addr_old_size);
            mutex_unlock(&(table->mtx_access));
            return -ENOMEM;
        }
        elt_repl->address = add;
    }

    mutex_unlock(&(table->mtx_access));
    return 0;
}

int fib_sr_entry_get_address(fib_table_t *table, fib_sr_t *fib_sr, fib_sr_entry_t *sr_entry,
                             uint8_t *addr, size_t *addr_size)
{
    mutex_lock(&(table->mtx_access));
    if ((fib_sr == NULL) || (fib_is_sr_in_table(table, fib_sr) == -ENOENT)) {
        mutex_unlock(&(table->mtx_access));
        return -EFAULT;
    }

    if (fib_sr_check_lifetime(fib_sr) == -ENOENT) {
        mutex_unlock(&(table->mtx_access));
        return -ENOENT;
    }

    fib_sr_entry_t *elt;
    LL_FOREACH(fib_sr->sr_path, elt) {
        if (elt == sr_entry) {
            if (universal_address_get_address(elt->address, addr, addr_size) != NULL) {
                mutex_unlock(&(table->mtx_access));
                return 0;
            }
            else {
                mutex_unlock(&(table->mtx_access));
                return -ENOMEM;
            }
        }
    }
    mutex_unlock(&(table->mtx_access));
    return -ENOENT;
}

/**
 * @brief helper function to search a partial path to a given destination,
 *         and iff successful to create a new source route
 *
 * @param[in] table the fib table the entry should be added to
 * @param[in] dst pointer to the destination address bytes
 * @param[in] dst_size the size in bytes of the destination address type
 * @param[in] check_free_entry position to start the search for a free entry
 * @param[out] error the state of of this operation when finished
 *
 * @return pointer to the new source route on success
 *         NULL otherwise
*/
static fib_sr_t* _fib_create_sr_from_partial(fib_table_t *table, uint8_t *dst, size_t dst_size,
                                             int check_free_entry, int *error) {
fib_sr_t* hit = NULL;

    for (size_t i = 0; i < table->size; ++i) {
        if (table->data.source_routes->headers[i].sr_lifetime != 0) {

            fib_sr_entry_t *elt;
            LL_FOREACH(table->data.source_routes->headers[i].sr_path, elt) {
                size_t addr_size_match = dst_size << 3;
                if (universal_address_compare(elt->address, dst, &addr_size_match) == UNIVERSAL_ADDRESS_EQUAL) {
                    /* we create a new sr */
                    if (check_free_entry == -1) {
                        /* we have no room to create a new sr
                         * so we just retrun and NOT tell the RPs to find a route
                         * since we cannot save it
                         */
                        *error = -ENOBUFS;
                        return NULL;
                    }
                    else {
                        /* we check if there is a free place for the new sr */
                        fib_sr_t *new_sr = NULL;
                        for (size_t j = check_free_entry; j < table->size; ++j) {
                            if (table->data.source_routes->headers[j].sr_lifetime != 0) {
                                /* not this one, maybe the next one */
                                continue;
                            }
                            else {

                                /* there it is, so we copy the header */
                                new_sr = &table->data.source_routes->headers[j];
                                new_sr->sr_iface_id = table->data.source_routes->headers[i].sr_iface_id;
                                new_sr->sr_flags = table->data.source_routes->headers[i].sr_flags;
                                new_sr->sr_lifetime = table->data.source_routes->headers[i].sr_lifetime;
                                new_sr->sr_path = NULL;

                                /* and the path until the searched destination */
                                fib_sr_entry_t *elt_iter, *elt_add = NULL;

                                LL_FOREACH(table->data.source_routes->headers[i].sr_path, elt_iter) {
                                    fib_sr_entry_t *new_entry;

                                    if (fib_sr_new_entry(table, elt_iter->address->address,
                                                         elt_iter->address->address_size,
                                                         &new_entry) != 0) {
                                        /* we could not create a new entry
                                         * so we return to clean up the partial route
                                         */
                                        *error = -ENOBUFS;
                                        return new_sr;
                                    }

                                    if (new_sr->sr_path == NULL) {
                                        new_sr->sr_path = new_entry;
                                        elt_add = new_sr->sr_path;
                                    }
                                    else {
                                        elt_add->next = new_entry;
                                        elt_add = elt_add->next;
                                    }

                                    if (elt_iter == elt) {
                                        /* we copied until the destination */
                                        new_sr->sr_dest = new_entry;
                                        hit = new_sr;

                                        /* tell the RPs that a new sr has been created
                                         * the size and the flags parameters are ignored
                                         */
                                        if (fib_signal_rp(table, FIB_MSG_RP_SIGNAL_SOURCE_ROUTE_CREATED,
                                                      (uint8_t *)new_sr, 0, 0) != 0) {
                                            /* if no RP can handle the source route
                                             * then the host is not directly reachable
                                             */
                                            *error = -EHOSTUNREACH;
                                        }

                                        /* break from iterating for copy */
                                        break;
                                    }
                                }
                            }
                        }

                        /* break from iterating the found path */
                        break;
                    }
                }
            }
            if (hit != NULL) {
                /* break iterating all sr since we have a path now */
                break;
            }
        }
    }
    return hit;
}

int fib_sr_get_route(fib_table_t *table, uint8_t *dst, size_t dst_size, kernel_pid_t *sr_iface_id,
                     uint32_t *sr_flags,
                     uint8_t *addr_list, size_t *addr_list_elements, size_t *element_size,
                     bool reverse, fib_sr_t **fib_sr)
{
    mutex_lock(&(table->mtx_access));

    if ((dst == NULL) || (sr_iface_id == NULL) || (sr_flags == NULL)
        || (addr_list == NULL) || (addr_list_elements == NULL) || (element_size == NULL)) {
        mutex_unlock(&(table->mtx_access));
        return -EFAULT;
    }

    fib_sr_t *hit = NULL;
    fib_sr_t *tmp_hit = NULL;
    int check_free_entry = -1;

    bool skip = (fib_sr != NULL) && (*fib_sr != NULL)?true:false;
    /* Case 1 - check if we know a direct route */
    for (size_t i = 0; i < table->size; ++i) {

        if (fib_sr_check_lifetime(&table->data.source_routes->headers[i]) == -ENOENT) {
            /* expired, so skip this sr and remember its position */
            if (check_free_entry == -1) {
                /* we want to fill up the source routes from the beginning */
                check_free_entry = i;
            }
            continue;
        }

        if( skip ) {
            if(*fib_sr == &table->data.source_routes->headers[i]) {
                skip = false;
            }
            /* we skip all entries upon the consecutive one to start search */
            continue;
        }

        size_t addr_size_match = dst_size << 3;
        if (universal_address_compare(table->data.source_routes->headers[i].sr_dest->address,
                                      dst, &addr_size_match) == UNIVERSAL_ADDRESS_EQUAL) {
            if (*sr_flags == table->data.source_routes->headers[i].sr_flags) {
                /* found a perfect matching sr, no need to search further */
                hit = &table->data.source_routes->headers[i];
                tmp_hit = NULL;
                if (check_free_entry == -1) {
                    check_free_entry = i;
                }
                break;
            }
            else {
                /* found a sr to the destination but with different flags,
                 * maybe we find a better one.
                 */
                tmp_hit = &table->data.source_routes->headers[i];
            }
        }
    }

    if (hit == NULL) {
        /* we didn't find a perfect sr, but one with distinct flags */
        hit = tmp_hit;
    }

    /* Case 2 - if no hit is found check if there is a matching entry in one sr_path
     * @note the first match wins, if we find one we will NOT continue searching,
     * since this search is very expensive in terms of compare operations
    */
    if (hit == NULL) {
        int error = 0;
        hit = _fib_create_sr_from_partial(table, dst, dst_size, check_free_entry, &error);
        if ((error != 0) && (error != -EHOSTUNREACH)) {
            /* something went wrong, so we clean up our mess
             *
             * @note we could handle -EHOSTUNREACH differently here,
             * since it says that we have a partial source route but no RP
             * to manage it.
             * Thats why I let it pass for now.
             */
            if (hit != NULL) {
                hit->sr_lifetime = 0;

                if (hit->sr_path != NULL) {
                    fib_sr_entry_t *elt, *tmp;
                    LL_FOREACH_SAFE(hit->sr_path, elt, tmp) {
                        universal_address_rem(elt->address);
                        elt->address = NULL;
                        LL_DELETE(hit->sr_path, elt);
                    }
                    hit->sr_path = NULL;
                }
            }
            mutex_unlock(&(table->mtx_access));
            return error;
        }
    }

    /* Final step - copy the list in the desired order */
    if (hit != NULL) {

        /* store the current hit to enable consecutive searches */
        if( fib_sr != NULL ) {
            *fib_sr = hit;
        }

        /* check the list size and if the sr entries will fit */
        int count;
        fib_sr_entry_t *elt = NULL;
        LL_COUNT(hit->sr_path, elt, count);

        if (((size_t)count > *addr_list_elements)
            || (sizeof(hit->sr_path->address->address) > *element_size)) {
            *addr_list_elements = count;
            *element_size = sizeof(hit->sr_path->address->address);
            mutex_unlock(&(table->mtx_access));
            return -ENOBUFS;
        }

        /* start copy the individual entries in the desired order */
        uint8_t *next_entry = addr_list;
        int one_address_size = *element_size;

        if (reverse) {
            /* we move to the last list element */
            next_entry += (count - 1) * sizeof(hit->sr_path->address->address);
            /* and set the storing direction during the iteration */
            one_address_size *= -1;
        }

        elt = NULL;
        LL_FOREACH(hit->sr_path, elt) {
            size_t tmp_size = sizeof(hit->sr_path->address->address);
            universal_address_get_address(elt->address, next_entry, &tmp_size);
            next_entry += one_address_size;
        }
        *sr_iface_id = hit->sr_iface_id;
        *sr_flags = hit->sr_flags;
        *addr_list_elements = count;
        *element_size = sizeof(hit->sr_path->address->address);
    }
    else {

        /* trigger RPs for route discovery */
        fib_signal_rp(table, FIB_MSG_RP_SIGNAL_UNREACHABLE_DESTINATION, dst, dst_size, *sr_flags);

        mutex_unlock(&(table->mtx_access));
        return -EHOSTUNREACH;
    }

    mutex_unlock(&(table->mtx_access));

    if (tmp_hit == NULL) {
        return 0;
    }
    else {
        return 1;
    }
}

/* print functions */

void fib_print_notify_rp(fib_table_t *table)
{
    mutex_lock(&(table->mtx_access));

    for (size_t i = 0; i < FIB_MAX_REGISTERED_RP; ++i) {
        printf("[fib_print_notify_rp] pid[%d]: %d\n", (int)i, (int)(table->notify_rp[i]));
    }

    mutex_unlock(&(table->mtx_access));
}

void fib_print_fib_table(fib_table_t *table)
{
    mutex_lock(&(table->mtx_access));

    for (size_t i = 0; i < table->size; ++i) {
        printf("[fib_print_table] %d) iface_id: %d, global: %p, next hop: %p, lifetime: %"PRIu32"\n",
               (int)i, (int)table->data.entries[i].iface_id,
               (void *)table->data.entries[i].global,
               (void *)table->data.entries[i].next_hop,
               (uint32_t)(table->data.entries[i].lifetime / 1000));
    }

    mutex_unlock(&(table->mtx_access));
}

void fib_print_sr(fib_table_t *table, fib_sr_t *sr)
{
    /* does not adjust the lifetime */
    mutex_lock(&(table->mtx_access));
    if ((sr == NULL) || (fib_is_sr_in_table(table, sr) == -ENOENT)) {
        mutex_unlock(&(table->mtx_access));
        return;
    }

    printf("\n-= Source route (%p) =-\nIface: %d\nflags: %x\npath: %p\ndest: ",
           (void *)sr, sr->sr_iface_id, (unsigned int)sr->sr_flags, (void *)sr->sr_path);

    if (sr->sr_dest != NULL) {
        universal_address_print_entry(sr->sr_dest->address);
    } else {
        puts("Not set.");
    }

    fib_sr_entry_t *nxt = sr->sr_path;
    while (nxt) {
        universal_address_print_entry(nxt->address);
        nxt = nxt->next;
    }
    printf("-= END (%p) =-\n", (void *)sr);

    mutex_unlock(&(table->mtx_access));
}

static void fib_print_address(universal_address_container_t *entry)
{
    uint8_t address[UNIVERSAL_ADDRESS_SIZE];
    size_t addr_size = UNIVERSAL_ADDRESS_SIZE;
    uint8_t *ret = universal_address_get_address(entry, address, &addr_size);

    if (ret == address) {
#ifdef MODULE_IPV6_ADDR
        if (addr_size == sizeof(ipv6_addr_t)) {
            printf("%-" FIB_ADDR_PRINT_LENS "s",
                    ipv6_addr_to_str(addr_str, (ipv6_addr_t *) address, sizeof(addr_str)));
            return;
        }
#endif
        for (size_t i = 0; i < UNIVERSAL_ADDRESS_SIZE; ++i) {
            if (i <= addr_size) {
                printf("%02x", address[i]);
            }
            else {
                printf("  ");
            }
        }
#ifdef MODULE_IPV6_ADDR
        /* print trailing whitespaces */
        for (size_t i = 0; i < FIB_ADDR_PRINT_LEN - (UNIVERSAL_ADDRESS_SIZE * 2); ++i) {
            printf(" ");
        }
#endif
    }
}

void fib_print_routes(fib_table_t *table)
{
    mutex_lock(&(table->mtx_access));
    uint64_t now = xtimer_now_usec64();

    if (table->table_type == FIB_TABLE_TYPE_SH) {
        printf("%-" FIB_ADDR_PRINT_LENS "s %-17s %-" FIB_ADDR_PRINT_LENS "s %-10s %-16s"
                " Interface\n" , "Destination", "Flags", "Next Hop", "Flags", "Expires");

        for (size_t i = 0; i < table->size; ++i) {
            if (table->data.entries[i].lifetime != 0) {
                fib_print_address(table->data.entries[i].global);
                printf(" 0x%08"PRIx32" ", table->data.entries[i].global_flags);
                if(table->data.entries[i].global_flags & FIB_FLAG_NET_PREFIX_MASK) {
                    uint32_t prefix = (table->data.entries[i].global_flags
                                       & FIB_FLAG_NET_PREFIX_MASK);
                    printf("N /%-3d ", (int)(prefix >> FIB_FLAG_NET_PREFIX_SHIFT));
                } else {
                    printf("H      ");
                }

                fib_print_address(table->data.entries[i].next_hop);
                printf(" 0x%08"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;

                    /* we must interpret the values as signed */
                    if ((int64_t)tm < 0 ) {
                        printf("%-16s ", "EXPIRED");
                    }
                    else {
                        printf("%"PRIu32".%05"PRIu32, (uint32_t)(tm / 1000000),
                               (uint32_t)(tm % 1000000));
                    }
                }
                else {
                    printf("%-16s ", "NEVER");
                }

                printf("%d\n", (int)table->data.entries[i].iface_id);
            }
        }
    }
    else if (table->table_type == FIB_TABLE_TYPE_SR) {
        printf("%-" FIB_ADDR_PRINT_LENS "s %-" FIB_ADDR_PRINT_LENS "s %-6s %-16s Interface\n"
               , "SR Destination", "SR First Hop", "SR Flags", "Expires");
        for (size_t i = 0; i < table->size; ++i) {
            if (table->data.source_routes->headers[i].sr_lifetime != 0) {
                fib_print_address(table->data.source_routes->headers[i].sr_dest->address);
                fib_print_address(table->data.source_routes->headers[i].sr_path->address);
                printf(" 0x%04"PRIx32" ", table->data.source_routes->headers[i].sr_flags);

                if (table->data.source_routes->headers[i].sr_lifetime != FIB_LIFETIME_NO_EXPIRE) {

                    uint64_t tm = table->data.source_routes->headers[i].sr_lifetime - now;

                    /* we must interpret the values as signed */
                    if ((int64_t)tm < 0 ) {
                        printf("%-16s ", "EXPIRED");
                    }
                    else {
                        printf("%"PRIu32".%05"PRIu32, (uint32_t)(tm / 1000000),
                               (uint32_t)(tm % 1000000));
                    }
                }
                else {
                    printf("%-16s ", "NEVER");
                }

                printf("%d\n", (int)table->data.source_routes->headers[i].sr_iface_id);
            }
        }
    }
    mutex_unlock(&(table->mtx_access));
}

#if FIB_DEVEL_HELPER
int fib_devel_get_lifetime(fib_table_t *table, uint64_t *lifetime, uint8_t *dst,
                           size_t dst_size)
{
    if (table->table_type == FIB_TABLE_TYPE_SH) {
        size_t count = 1;
        fib_entry_t *entry[count];

        int ret = fib_find_entry(table, dst, dst_size, &(entry[0]), &count);
        if (ret == 1 ) {
            /* only return lifetime of exact matches */
            *lifetime = entry[0]->lifetime;
            return 0;
        }
        return -EHOSTUNREACH;
    }
    else if (table->table_type == FIB_TABLE_TYPE_SR) {
        size_t addr_size_match = dst_size << 3;
        /* first hit wins here */
        for (size_t i = 0; i < table->size; ++i) {
            if (universal_address_compare(table->data.source_routes->headers[i].sr_dest->address,
                                        dst, &addr_size_match) == UNIVERSAL_ADDRESS_EQUAL) {
                *lifetime = table->data.source_routes->headers[i].sr_lifetime;
                return 0;
            }
        }
        return -EHOSTUNREACH;
    }
    return -EFAULT;
}
#endif