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
|
// SPDX-License-Identifier: BSD-3-Clause
/**
* @file network_monitor.c
*
* @brief mptcpd network device monitoring.
*
* Copyright (c) 2017-2022, 2024, Intel Corporation
*/
#ifdef HAVE_CONFIG_H
# include <mptcpd/private/config.h> // For NDEBUG
#endif
#define _POSIX_C_SOURCE 200112L ///< For XSI-compliant strerror_r().
#define _DEFAULT_SOURCE ///< For standard network interface flags.
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <linux/rtnetlink.h>
#include <arpa/inet.h>
#include <net/if.h> // For standard network interface flags.
#include <netinet/in.h>
#include <ell/ell.h>
#include <mptcpd/private/path_manager.h>
#include <mptcpd/private/sockaddr.h>
#include <mptcpd/private/network_monitor.h>
#include <mptcpd/network_monitor.h>
// See IETF RFC 3849: IPv6 Address Prefix Reserved for Documentation.
// 2001:DB8::/32
static struct in6_addr const test_net_v6 = {
.s6_addr = {0x20, 0x01, 0x0d, 0xb8, } };
// See IETF RFC 5737: IPv4 Address Blocks Reserved for Documentation.
static struct in_addr const test_net_v4 = {
.s_addr = MPTCPD_CONSTANT_HTONL(0xc0000201) };
// -------------------------------------------------------------------
/**
* @struct nm_ops_info
*
* @brief Network monitoring event tracking callback information.
*/
struct nm_ops_info
{
/// Network monitor event tracking operations.
struct mptcpd_nm_ops const *ops;
/// Data passed to the network event tracking operations.
void *user_data;
};
/**
* @brief Data needed to run the network monitor.
*/
struct mptcpd_nm
{
/// ELL netlink object for the @c NETLINK_ROUTE protocol.
struct l_netlink *rtnl;
/// "Link" rtnetlink multicast notification IDs.
unsigned int link_id;
/// IPv4 rtnetlink multicast notification IDs.
unsigned int ipv4_id;
/// IPv6 rtnetlink multicast notification IDs.
unsigned int ipv6_id;
/// List of @c mptcpd_interface objects.
struct l_queue *interfaces;
/// List of @c nm_ops_info objects.
struct l_queue *ops;
/// Flags controlling address notification.
uint32_t notify_flags;
/// Enable/disable loopback network interface monitoring.
bool monitor_loopback;
};
// -------------------------------------------------------------------
// Helper Functions
// -------------------------------------------------------------------
/**
* @brief Wrap different versions of ELL @c l_netlink_send().
*
* ELL 0.68 changed the API for @c l_netlink_send(). This helper
* function wraps the two different function calls so that mptcpd will
* work with both pre- and post-0.68 @c l_netlink_send() APIs.
*/
static unsigned int netlink_send(struct l_netlink *netlink,
uint16_t type,
uint16_t flags,
void const *data,
uint32_t len,
l_netlink_command_func_t function,
void *user_data,
l_netlink_destroy_func_t destroy)
{
#ifdef HAVE_L_NETLINK_MESSAGE_NEW_SIZED
// ELL >= 0.68
struct l_netlink_message *const message =
l_netlink_message_new_sized(type, flags, len);
l_netlink_message_add_header(message, data, len);
return l_netlink_send(netlink,
message,
function,
user_data,
destroy);
#else
// ELL < 0.68
return l_netlink_send(netlink,
type,
flags,
data,
len,
function,
user_data,
destroy);
#endif
}
// -------------------------------------------------------------------
// Network Address Information Handling
// -------------------------------------------------------------------
/**
* @struct mptcpd_rtm_addr
*
* @brief Encapsulate network address information.
*
* This is a convenience structure that encapsulates rtnetlink
* @c RTM_NEWADDR, @c RTM_DELADDR, and @c RTM_GETADDR message data for
* a given network address.
*/
struct mptcpd_rtm_addr
{
/**
* @c RTM_NEWADDR, @c RTM_DELADDR, and @c RTM_GETADDR message
* data.
*/
struct ifaddrmsg const *ifa;
/**
* Network address retrieved from @c IFA_ADDRESS rtnetlink
* attribute.
*/
void const *const addr;
};
/**
* @struct nm_addr_info
*
* @brief Convenience structure to bundle address information.
*
* Note that Network monitor users (tests and sspi plugin) assume
* that interface->addr is a list of sockaddr. Placing the
* sockaddr_storage as the first field allow expanding the address
* metadata without breaking that assumption
*/
struct nm_addr_info
{
/// Network address information.
struct sockaddr_storage address;
/// Weak reference to network interface.
struct mptcpd_interface const *interface;
/// Network interface index
int index;
/// Usage counter, free this when reaches 0
int count;
/// Route check timeout
struct l_timeout *timeout;
/// Route check attemps
int attempts;
// Network monitor reference
struct mptcpd_nm *nm;
};
/**
* @brief Create an object that contains internet network
* address-specific information.
*
* @param[in] info Network address-specific information retrieved from
* the @c RTM_NEWADDR message.
*/
static struct nm_addr_info *
mptcpd_addr_create(struct mptcpd_rtm_addr const *info)
{
assert(info != NULL);
sa_family_t const family = info->ifa->ifa_family;
if (family != AF_INET && family != AF_INET6)
return NULL;
struct nm_addr_info *const ai = l_new(struct nm_addr_info, 1);
ai->address.ss_family = family;
ai->count = 1;
if (family == AF_INET) {
struct sockaddr_in *const a =
(struct sockaddr_in *) &ai->address;
/*
Kernel nla_put_in_addr() inserts a big endian 32
bit unsigned integer, not struct in_addr.
*/
a->sin_addr.s_addr = *(uint32_t const*) info->addr;
} else {
struct sockaddr_in6 *const a =
(struct sockaddr_in6 *) &ai->address;
struct in6_addr const *const sa =
(struct in6_addr const*) info->addr;
memcpy(&a->sin6_addr, sa, sizeof(*sa));
}
return ai;
}
static void mptcpd_addr_cancel_timeout(struct nm_addr_info *ai)
{
if (!ai->timeout)
return;
l_timeout_remove(ai->timeout);
ai->timeout = NULL;
}
/**
* @brief Drop a reference count to a @c nm_addr_info object.
*
* Drop reference count to a @c nm_addr_info object, eventually destroying
* it when the reference count drops to zero.
*
* @param[in,out] Pointer to the @c nm_addr_info object to be released.
*/
static void mptcpd_addr_put(void *data)
{
if (data == NULL)
return;
struct nm_addr_info *const ai = data;
if (--ai->count == 0) {
mptcpd_addr_cancel_timeout(ai);
l_free(data);
}
}
/**
* @brief Acquire a reference to a @c nm_addr_info object
*
* @param[in,out] Pointer to the @c nm_addr_info object to be destroyed.
*/
static void mptcpd_addr_get(struct nm_addr_info *ai)
{
ai->count++;
}
static const char *mptcpd_addr_to_string(struct nm_addr_info const *ai,
char *out,
int size)
{
void const *src;
if (ai->address.ss_family == AF_INET) {
src = &((struct sockaddr_in const *)&ai->address)->sin_addr;
} else {
src = &((struct sockaddr_in6 const *)&ai->address)->sin6_addr;
}
return inet_ntop(ai->address.ss_family, src, out, size);
}
/**
* @brief Compare two @c nm_addr_info objects.
*
* Compare @c nm_addr_info objects to determine where in the network
* address list the first object, @a a, will be inserted relative to
* the second object, @a b.
*
* @return Always returns 1 to make insertions append to the queue
* since there is no need to sort.
*
* @see l_queue_insert()
*/
static int
mptcpd_addr_compare(void const *a, void const *b, void *user_data)
{
(void) a;
(void) b;
(void) user_data;
// No need to sort.
return 1;
}
/**
* @brief Match a @c nm_addr_info object.
*
* A network address represented by @a a (@c struct @c nm_addr_info)
* matches if its @c family and IPv4/IPv6 address members match those
* in @a b.
*
* @param[in] a Currently monitored network address of type @c struct
* @c nm_addr_info*.
* @param[in] b Network address information of type @c struct
* @c mptcpd_rtm_addr* to be compared against network
* address @a a.
*
* @return @c true if the network address represented by @a a matches
* the address @a b, and @c false otherwise.
*
* @see l_queue_find()
* @see l_queue_remove_if()
*/
static bool mptcpd_addr_match(void const *a, void const *b)
{
struct nm_addr_info const *const lhs = a;
struct mptcpd_rtm_addr const *const rhs = b;
assert(lhs);
assert(rhs);
assert(lhs->address.ss_family == AF_INET
|| lhs->address.ss_family == AF_INET6);
bool matched = (lhs->address.ss_family == rhs->ifa->ifa_family);
if (!matched)
return matched;
if (lhs->address.ss_family == AF_INET) {
struct sockaddr_in const *const addr =
(struct sockaddr_in const *) &lhs->address;
/*
Kernel nla_put_in_addr() inserts a big endian 32 bit
unsigned integer, not struct in_addr.
*/
uint32_t const sa = *(uint32_t const*) rhs->addr;
matched = (addr->sin_addr.s_addr == sa);
} else {
struct sockaddr_in6 const *const addr =
(struct sockaddr_in6 const *) &lhs->address;
struct in6_addr const *const sa =
(struct in6_addr const*) rhs->addr;
matched = (memcmp(&addr->sin6_addr,
sa,
sizeof(addr->sin6_addr))
== 0);
}
return matched;
}
// -------------------------------------------------------------------
// Network Interface Information Handling
// -------------------------------------------------------------------
/**
* @brief Callback information supplied by the user.
*/
struct mptcpd_interface_callback_data
{
/**
* @brief Function to be called when iterating over network
* interfaces.
*/
mptcpd_nm_callback callback;
/**
* @brief User supplied data.
*
* Data supplied by the user to be passed to the @c callback
* function during network interface iteration.
*/
void *user_data;
};
/**
* @brief Create an object that contains network interface-specific
* information.
*
* @param[in] ifi Network interface-specific information retrieved
* from the @c RTM_NEWLINK message.
* @param[in] len Length of the @C RTM_NEWLINK Netlink message,
* potentially including @c rtattr attributes.
*/
static struct mptcpd_interface *
mptcpd_interface_create(struct ifinfomsg const *ifi, uint32_t len)
{
assert(ifi != NULL);
/*
See rtnetlink(7) man page for struct ifinfomsg details.
ifi->
ifi_family: AF_UNSPEC
ifi_type: ARP protocol hardware identifer,
e.g. ARPHRD_ETHER (see <net/if_arp.h>)
ifi_index: network device/interface index
ifi_flags: device flags
ifi_change: change mask
*/
l_debug("\n"
"ifi_family: %s\n"
"ifi_type: %d\n"
"ifi_index: %d\n"
"ifi_flags: 0x%08x\n"
"ifi_change: 0x%08x\n",
ifi->ifi_family == AF_UNSPEC ? "AF_UNSPEC"
: "<unexpected family>",
ifi->ifi_type,
ifi->ifi_index,
ifi->ifi_flags,
ifi->ifi_change);
struct mptcpd_interface *const interface =
l_new(struct mptcpd_interface, 1);
interface->family = ifi->ifi_family;
interface->type = ifi->ifi_type;
interface->index = ifi->ifi_index;
interface->flags = ifi->ifi_flags;
size_t bytes = len - NLMSG_ALIGN(sizeof(*ifi));
/**
* @todo Can we retrieve the IP address associated with each
* network interface from IFLA_* attributes? It seemed
* like we could potentially pull them from the
* IFLA_AF_SPEC nested attribute but that attribute
* appears to only contain configuration information
* exposed through the sysctl interface. We may be
* stuck retrieving interfaces through the RTM_GETADDR
* rtnetlink message.
*
* @todo Add support for filtering networking interfaces based
* on whether or not they have been marked MPTCP-enabled
* through an mptcpd configuration/setting.
*/
for (struct rtattr const *rta = IFLA_RTA(ifi);
RTA_OK(rta, bytes);
rta = RTA_NEXT(rta, bytes)) {
switch (rta->rta_type) {
case IFLA_IFNAME:
if (RTA_PAYLOAD(rta) < IF_NAMESIZE) {
l_strlcpy(interface->name,
RTA_DATA(rta),
L_ARRAY_SIZE(interface->name));
l_debug("link found: %s",
interface->name);
}
break;
default:
break;
}
}
interface->addrs = l_queue_new();
return interface;
}
/**
* @brief Destroy a @c mptcpd_interface object.
*
* @param[in,out] Pointer to the @c mptcpd_interface object to be
* destroyed.
*/
static void mptcpd_interface_destroy(void *data)
{
struct mptcpd_interface *const i = data;
l_queue_destroy(i->addrs, mptcpd_addr_put);
l_free(i);
}
/**
* @brief Compare two @c mptcpd_interface objects.
*
* Compare @c mptcpd_interface objects to determine where in the
* network interface list the first object, @a a, will be inserted
* relative to the second object, @a b.
*
* @retval 1 Insert @a after @a b.
* @retval -1 Insert @a before @a b.
*
* @see l_queue_insert()
*/
static int
mptcpd_interface_compare(void const *a, void const *b, void *user_data)
{
assert(a);
assert(b);
(void) user_data;
struct mptcpd_interface const *const lhs = a;
struct mptcpd_interface const *const rhs = b;
// Sort by network interface index.
return lhs->index > rhs->index ? 1 : -1;
}
/**
* @brief Match an @c mptcpd_interface object.
*
* A network interface represented by @a a (@c struct
* @c mptcpd_interface) matches if its network interface index matches
* the provided index @a b (@c int).
*
* @return @c true if the index for the network interface represented
* by @a a matches the user supplied index @a b, and @c false
* otherwise.
*
* @see l_queue_find()
* @see l_queue_remove_if()
*/
static bool mptcpd_interface_match(void const *a, void const *b)
{
assert(a);
assert(b);
struct mptcpd_interface const *const lhs = a;
int const *const index = b;
return lhs->index == *index;
}
/**
* @brief Network interface queue iteration function.
*
* This function will be called when iterating over the network
* interface queue through the @c l_queue_foreach() function.
*
* @param[in] data Pointer to @c mptcpd_interface object.
* @param[in] user_data User supplied data.
*/
static void mptcpd_interface_callback(void *data, void *user_data)
{
struct mptcpd_interface *const interface = data;
struct mptcpd_interface_callback_data *const cb = user_data;
cb->callback(interface, cb->user_data);
}
// -------------------------------------------------------------------
// Network Interface and Address Change Handling
// -------------------------------------------------------------------
/**
* @brief Check if network interface is ready for use.
*
* @param[in] ifi Network interface-specific information retrieved
* from the @c RTM_NEWLINK message.
*
* @return @c true if network interface is ready, and @c false other.
*/
static bool is_interface_ready(struct mptcpd_nm const *nm,
struct ifinfomsg const *ifi)
{
// Only accept network interfaces that are up and running.
static unsigned int iff_ready = IFF_UP | IFF_RUNNING;
return (ifi->ifi_flags & iff_ready) == iff_ready
&& ((ifi->ifi_flags & IFF_LOOPBACK) == 0
|| nm->monitor_loopback);
}
/**
* @brief Notify new network interface event subscriber.
*
* @param[in] data Network event tracking callbacks and data.
* @param[in] user_data @c mptcpd network interface information.
*/
static void notify_new_interface(void *data, void *user_data)
{
struct nm_ops_info *const info = data;
struct mptcpd_nm_ops const *const ops = info->ops;
struct mptcpd_interface const *const i = user_data;
if (ops->new_interface)
ops->new_interface(i, info->user_data);
}
/**
* @brief Notify updated network interface event subscriber.
*
* @param[in] data Network event tracking callbacks and data.
* @param[in] user_data @c mptcpd network interface information.
*/
static void notify_update_interface(void *data, void *user_data)
{
struct nm_ops_info *const info = data;
struct mptcpd_nm_ops const *const ops = info->ops;
struct mptcpd_interface const *const i = user_data;
if (ops->update_interface)
ops->update_interface(i, info->user_data);
}
/**
* @brief Notify removed network interface event subscriber.
*
* @param[in] data Set of network event tracking callbacks.
* @param[in] user_data @c mptcpd network interface information.
*/
static void notify_delete_interface(void *data, void *user_data)
{
struct nm_ops_info *const info = data;
struct mptcpd_nm_ops const *const ops = info->ops;
struct mptcpd_interface const *const i = user_data;
if (ops->delete_interface)
ops->delete_interface(i, info->user_data);
}
/**
* @brief Register network interface (link) with network monitor.
*
* @param[in] ifi Network interface-specific information retrieved
* from the @c RTM_NEWLINK messages.
* @param[in] len Length of the Netlink message.
* @param[in] nm Pointer to the @c mptcpd_nm object that contains the
* list (queue) to which network interface information
* will be inserted.
*
* @return Inserted @c mptcpd_interface object corresponding to the
* new network link/interface.
*/
static struct mptcpd_interface *
insert_link(struct ifinfomsg const *ifi,
uint32_t len,
struct mptcpd_nm *nm)
{
struct mptcpd_interface *interface =
mptcpd_interface_create(ifi, len);
if (!l_queue_insert(nm->interfaces,
interface,
mptcpd_interface_compare,
NULL)) {
mptcpd_interface_destroy(interface);
interface = NULL;
l_error("Unable to queue network interface information.");
}
return interface;
}
/**
* @brief Update monitored network interface (link) information.
*
* @param[in] ifi Network interface-specific information retrieved
* from the @c RTM_NEWLINK messages.
* @param[in] len Length of the Netlink message.
* @param[in] nm Pointer to the @c mptcpd_nm object that contains the
* list (queue) to which network interface information
* will be inserted.
*/
static void update_link(struct ifinfomsg const *ifi,
uint32_t len,
struct mptcpd_nm *nm)
{
struct mptcpd_interface *i =
l_queue_find(nm->interfaces,
mptcpd_interface_match,
&ifi->ifi_index);
if (i == NULL) {
i = insert_link(ifi, len, nm);
// Notify new network interface event observers.
if (i != NULL)
l_queue_foreach(nm->ops, notify_new_interface, i);
} else {
i->flags = ifi->ifi_flags;
// Notify updated network interface event observers.
l_queue_foreach(nm->ops, notify_update_interface, i);
}
}
/**
* @brief Remove network interface (link) from the network monitor.
*
* @param[in] ifi Network interface-specific information retrieved
* from @c RTM_NEWLINK (update case) or @c RTM_DELLINK
* messages.
* @param[in] nm Pointer to the @c mptcpd_nm object that contains the
* list (queue) to which network interface information
* will be inserted.
*/
static void remove_link(struct ifinfomsg const *ifi,
struct mptcpd_nm *nm)
{
struct mptcpd_interface *const interface =
l_queue_remove_if(nm->interfaces,
mptcpd_interface_match,
&ifi->ifi_index);
if (interface == NULL) {
l_debug("Network interface %d not monitored. "
"Ignoring monitoring removal failure.",
ifi->ifi_index);
return;
}
// Notify removed network interface event observers.
l_queue_foreach(nm->ops, notify_delete_interface, interface);
mptcpd_interface_destroy(interface);
}
/**
* @brief Handle changes to network interfaces.
*
* This is the @c RTNLGRP_LINK message handler.
*
* @param[in] type Netlink message content type.
* @param[in] data Pointer to rtnetlink @c ifinfomsg object
* corresponding to a specific network interface.
* @param[in] len Length of the Netlink message.
* @param[in] user_data Pointer to the @c mptcpd_nm object that
* contains the list (queue) to which network
* interface information will be inserted.
*/
static void handle_link(uint16_t type,
void const *data,
uint32_t len,
void *user_data)
{
struct ifinfomsg const *const ifi = data;
struct mptcpd_nm *const nm = user_data;
switch (type) {
case RTM_NEWLINK:
if (is_interface_ready(nm, ifi))
update_link(ifi, len, nm);
else
remove_link(ifi, nm); // Interface disabled.
break;
case RTM_DELLINK:
remove_link(ifi, nm);
break;
default:
l_error("Unexpected message in RTNLGRP_LINK handler");
break;
}
}
/**
* @brief Notify new network address event subscriber.
*
* @param[in] data Network event tracking callbacks and data.
* @param[in] user_data Network address information.
*/
static void notify_new_address(void *data, void *user_data)
{
struct nm_ops_info *const info = data;
struct mptcpd_nm_ops const *const ops = info->ops;
struct nm_addr_info const *const ai = user_data;
if (ops->new_address)
ops->new_address(ai->interface,
(struct sockaddr const *)&ai->address,
info->user_data);
}
/**
* @brief Notify network address event subscriber of deleted address.
*
* @param[in] data Network event tracking callbacks and data.
* @param[in] user_data Network address information.
*/
static void notify_delete_address(void *data, void *user_data)
{
struct nm_ops_info *const info = data;
struct mptcpd_nm_ops const *const ops = info->ops;
struct nm_addr_info const *const ai = user_data;
if (ops->delete_address)
ops->delete_address(ai->interface,
(struct sockaddr const *)&ai->address,
info->user_data);
}
/**
* @brief Register network address with network monitor.
*
* @param[in] interface @c mptcpd network interface information.
* @param[in] rtm_addr New network address information.
*
* @return Inserted @c nm_addr_info object corresponding to the new
* network address.
*/
static struct nm_addr_info *
insert_addr_return(struct mptcpd_interface *interface,
struct mptcpd_rtm_addr const *rtm_addr)
{
struct nm_addr_info *addr = mptcpd_addr_create(rtm_addr);
if (addr == NULL
|| !l_queue_insert(interface->addrs,
addr,
mptcpd_addr_compare,
NULL)) {
mptcpd_addr_put(addr);
addr = NULL;
l_error("Unable to track internet address information.");
return addr;
}
addr->index = interface->index;
addr->interface = interface;
addr->index = interface->index;
return addr;
}
/**
* @brief Register network address with network monitor (no return).
*
* @param[in] nm @c mptcpd_nm object that contains the list
* (queue) of network monitoring event
* subscribers.
* @param[in] interface @c mptcpd network interface information.
* @param[in] rtm_addr New network address information.
*
* This wrapper function drops the @c insert_addr_return() return
* value so that it matches the @c handle_ifaddr_func_t type to allow
* it to be used as an argument to @c foreach_ifaddr().
*
* @note This function is only used at mptcpd start when the initial
* network address is retrieved.
*/
static void insert_addr(struct mptcpd_nm *nm,
struct mptcpd_interface *interface,
struct mptcpd_rtm_addr const *rtm_addr)
{
(void) nm;
(void) insert_addr_return(interface, rtm_addr);
}
static size_t raw_add_attr(struct rtattr *attr, unsigned short type,
void const *data, size_t len)
{
attr->rta_type = type;
attr->rta_len = RTA_LENGTH(len);
memcpy(RTA_DATA(attr), data, len);
return RTA_SPACE(len);
}
static size_t add_attr_u32(void *buf, unsigned short type, uint32_t value)
{
return raw_add_attr(buf, type, &value, sizeof(uint32_t));
}
static size_t add_attr_address(void *buf,
unsigned short type,
bool is_ipv4,
void const *addr)
{
return raw_add_attr(buf, type, addr,
is_ipv4
? sizeof(struct in_addr)
: sizeof(struct in6_addr));
}
static void check_default_route(struct nm_addr_info *ai);
static void handle_rtm_timeout(struct l_timeout *timeout,
void *user_data)
{
struct nm_addr_info *ai = user_data;
(void) timeout;
check_default_route(ai);
}
#define MPTCPD_MAX_ROUTE_CHECK 3
static void schedule_route_check(struct nm_addr_info *ai)
{
if (ai->attempts++ > MPTCPD_MAX_ROUTE_CHECK) {
char str[INET6_ADDRSTRLEN];
l_debug("timeout while waiting for "
"default route on address %s",
mptcpd_addr_to_string(ai, str, INET6_ADDRSTRLEN));
mptcpd_addr_put(ai);
return;
}
if (!ai->timeout) {
ai->timeout =
l_timeout_create_ms(1,
handle_rtm_timeout,
ai,
NULL);
if (!ai->timeout) {
l_error("can't arm route re-check timeout");
mptcpd_addr_put(ai);
}
} else {
/* Use exponential backoff */
l_timeout_modify_ms(ai->timeout, 1 << ai->attempts);
}
}
static void handle_rtm_getroute(int error,
uint16_t type,
void const *data,
uint32_t len,
void *user_data)
{
struct nm_addr_info *ai = user_data;
char str[INET6_ADDRSTRLEN];
uint32_t ifindex = 0;
char *dst = NULL;
if (error != 0) {
l_info("can't resolved default route "
"from %s interface %d: %d",
mptcpd_addr_to_string(ai, str, INET6_ADDRSTRLEN),
ai->index,
error);
goto again;
}
(void) type;
assert(type == RTM_NEWROUTE);
if (ai->address.ss_family == AF_INET) {
l_rtnl_route4_extract(data,
len,
NULL,
&ifindex,
&dst,
NULL,
NULL);
} else {
l_rtnl_route6_extract(data,
len,
NULL,
&ifindex,
&dst,
NULL,
NULL);
}
if (dst)
goto bad_dst;
if ((int)ifindex != ai->index) {
l_info("interface mismatch %d:%d", ifindex, ai->index);
goto again;
}
/* default route found! try to notify address*/
mptcpd_addr_cancel_timeout(ai);
/* this happens asincronusly, remove_link() could have delete
* the relevant interface, re-check it
*/
ai->interface = l_queue_find(ai->nm->interfaces,
mptcpd_interface_match,
&ai->index);
l_info("found default route for address %s on interface %d",
mptcpd_addr_to_string(ai, str, INET6_ADDRSTRLEN), ai->index);
if (ai->interface)
l_queue_foreach(ai->nm->ops,
notify_new_address,
ai);
mptcpd_addr_put(ai);
return;
bad_dst:
l_info("found non-default route with destination %s",
dst ? dst : "");
again:
schedule_route_check(ai);
}
static void check_default_route(struct nm_addr_info *ai)
{
bool const is_ipv4 = ai->address.ss_family == AF_INET;
struct {
struct rtmsg route_msg;
char buf[1024];
} store = {
.route_msg = {
.rtm_family = ai->address.ss_family,
.rtm_flags = RTM_F_LOOKUP_TABLE | RTM_F_FIB_MATCH,
.rtm_dst_len = is_ipv4 ? 32: 128
}
};
char *buf = (char *) &store + NLMSG_ALIGN(sizeof(struct rtmsg));
buf += add_attr_u32(buf, RTA_OIF, ai->index);
void const *addr = &test_net_v6;
if (is_ipv4)
addr = &test_net_v4;
buf += add_attr_address(buf, RTA_DST, is_ipv4, addr);
/*
An address delete event can attempt to free this addr info
before we get the route reply. Acquire a reference so that
memory will not be released before the handler accesses it.
*/
mptcpd_addr_get(ai);
if (netlink_send(ai->nm->rtnl,
RTM_GETROUTE,
0,
&store,
buf - (char *) &store,
handle_rtm_getroute,
ai,
NULL) == 0) {
l_debug("Route lookup failed");
mptcpd_addr_put(ai);
}
}
/**
* @brief Register or update network address with network monitor.
*
* @param[in] nm @c mptcpd_nm object that contains the list
* (queue) of network monitoring event
* subscribers.
* @param[in] interface @c mptcpd network interface information.
* @param[in] rtm_addr New or updated network address information.
*/
static void update_addr(struct mptcpd_nm *nm,
struct mptcpd_interface *interface,
struct mptcpd_rtm_addr const *rtm_addr)
{
if ((nm->notify_flags & MPTCPD_NOTIFY_FLAG_SKIP_LL)
&& (rtm_addr->ifa->ifa_scope == RT_SCOPE_LINK))
return;
if ((nm->notify_flags & MPTCPD_NOTIFY_FLAG_SKIP_HOST)
&& (rtm_addr->ifa->ifa_scope == RT_SCOPE_HOST))
return;
struct nm_addr_info *addr =
l_queue_find(interface->addrs,
mptcpd_addr_match,
rtm_addr);
if (addr == NULL) {
addr = insert_addr_return(interface, rtm_addr);
// Notify new network interface event observers.
if (addr != NULL) {
if (nm->notify_flags & MPTCPD_NOTIFY_FLAG_ROUTE_CHECK) {
addr->nm = nm;
check_default_route(addr);
} else {
l_queue_foreach(nm->ops,
notify_new_address,
addr);
}
}
} else {
/**
* @todo Will we actually care about updating
* information related to an existing network
* address? We only track the address itself
* rather than other fields in the @c ifaddrmsg
* structure (flags, etc).
*/
l_debug("Network address information updated.");
}
}
/**
* @brief Remove network address from the network monitor.
*
* @param[in] nm @c mptcpd_nm object that contains the list
* (queue) of network monitoring event
* subscribers.
* @param[in] interface @c mptcpd network interface information.
* @param[in] rtm_addr Removed network address information.
*/
static void remove_addr(struct mptcpd_nm *nm,
struct mptcpd_interface *interface,
struct mptcpd_rtm_addr const *rtm_addr)
{
struct sockaddr *const addr =
l_queue_remove_if(interface->addrs,
mptcpd_addr_match,
rtm_addr);
if (addr == NULL) {
l_debug("Network address not monitored. "
"Ignoring monitoring removal "
"failure.");
return;
}
l_queue_foreach(nm->ops, notify_delete_address, addr);
mptcpd_addr_put(addr);
}
/**
* @brief Get @c mptcpd_interface instance.
*
* Get @c mptcpd_interface instance that matches the interface/link
* index, i.e. @a ifa->ifa_index.
*
* @param[in] ifa Network address-specific information retrieved
* from @c RTM_NEWADDR or @c RTM_DELADDR messages.
* @param[in] nm Pointer to the @c mptcpd_nm object that contains the
* list (queue) of @c mptcpd_interface objects.
*/
static struct mptcpd_interface *get_mptcpd_interface(
struct ifaddrmsg const * ifa,
struct mptcpd_nm * nm)
{
/* See rtnetlink(7) man page for struct ifaddrmsg details. */
/**
* @todo The below debug log message assumes that only
* AF_INET and AF_INET6 families are supported by
* RTM_GETADDR. While that is true at the moment, it
* may change in the future.
*/
l_debug("\n"
"ifa_family: %s\n"
"ifa_prefixlen: %u\n"
"ifa_flags: 0x%02x\n"
"ifa_scope: %u\n"
"ifa_index: %d",
ifa->ifa_family == AF_INET ? "AF_INET" : "AF_INET6",
ifa->ifa_prefixlen,
ifa->ifa_flags,
ifa->ifa_scope,
ifa->ifa_index);
struct mptcpd_interface *const interface =
l_queue_find(nm->interfaces,
mptcpd_interface_match,
&ifa->ifa_index);
if (interface == NULL)
l_debug("Ignoring address for unmonitored "
"network interface (%d).",
ifa->ifa_index);
return interface;
}
/**
* @brief Network address handler function signature.
*/
typedef
void (*handle_ifaddr_func_t)(struct mptcpd_nm *nm,
struct mptcpd_interface *interface,
struct mptcpd_rtm_addr const *rtm_addr);
/**
* @brief @c RTM_NEWADDR and @c RTM_DELADDR attribute iteration.
*
* Call @a handler for all attributes found in a @c RTM_NEWADDR or
* @c RTM_DELADDR event.
*
* @param[in] ifa Network address-specific information retrieved
* from @c RTM_NEWADDR or @c RTM_DELADDR messages.
* @param[in] len Length of the rtnetlink message.
* @param[in] addrs List of tracked network addresses.
* @param[in] handler Function to be called for each @c IFA_ADDRESS
* attribute in the @c RTM_NEWADDR or
* @c RTM_DELADDR event.
*/
static void foreach_ifaddr(struct ifaddrmsg const *ifa,
uint32_t len,
struct mptcpd_nm *nm,
struct mptcpd_interface *interface,
handle_ifaddr_func_t handler)
{
assert(ifa != NULL);
assert(len != 0);
assert(interface != NULL);
assert(handler != NULL);
size_t bytes = len - NLMSG_ALIGN(sizeof(*ifa));
for (struct rtattr const *rta = IFA_RTA(ifa);
RTA_OK(rta, bytes);
rta = RTA_NEXT(rta, bytes)) {
if (rta->rta_type == IFA_ADDRESS) {
struct mptcpd_rtm_addr const rtm_addr = {
.ifa = ifa,
.addr = RTA_DATA(rta)
};
handler(nm, interface, &rtm_addr);
}
}
}
/**
* @brief Handle changes to network addresses.
*
* This is the @c RTNLGRP_IPV4_IFADDR and @c RTNLGRP_IPV6_IFADDR
* message handler.
*
* @param[in] type Netlink message content type (unused).
* @param[in] data Pointer to rtnetlink @c ifaddrmsg object
* corresponding to a specific network interface.
* @param[in] len Length of the Netlink message.
* @param[in] user_data Pointer to the @c mptcpd_nm object that
* contains the list (queue) to which network
* interface information will be inserted.
*/
static void handle_ifaddr(uint16_t type,
void const *data,
uint32_t len,
void *user_data)
{
struct ifaddrmsg const *const ifa = data;
struct mptcpd_nm *const nm = user_data;
struct mptcpd_interface *const interface =
get_mptcpd_interface(ifa, nm);
/*
Verify that the address belongs to a network interface being
monitored.
*/
if (interface == NULL)
return;
handle_ifaddr_func_t handler = NULL;
switch (type) {
case RTM_NEWADDR:
handler = update_addr;
break;
case RTM_DELADDR:
handler = remove_addr;
break;
default:
l_error("Unexpected message in RTNLGRP_IPV4/V6_IFADDR "
"handler");
return;
}
foreach_ifaddr(ifa, len, nm, interface, handler);
}
// -------------------------------------------------------------------
// rtnetlink Command Handling
// -------------------------------------------------------------------
/**
* @brief Handle error that occurred during Netlink operation.
*
* @param[in] fn Name of function that failed.
* @param[in] error Number of error (@c errno) that occurred during
* Netlink operation.
*
* @return 0 if no error occurred, an @c errno otherwise.
*
* @todo There is nothing Netlink specific about this function.
* Should it be made available to the rest of mptcpd as a
* utility function?
*/
static int handle_error(char const *fn, int error)
{
if (error != 0) {
char errmsg[80];
int const r =
strerror_r(error, errmsg, L_ARRAY_SIZE(errmsg));
l_error("%s failed: %s",
fn,
r == 0 ? errmsg : "<unknown error>");
}
return error;
}
/**
* @brief Handle results from @c RTM_GETLINK rtnetlink command.
*
* @param[in] error Number of error (@c errno) that occurred
* during @c RTM_GETLINK operation.
* @param[in] type Netlink message content type (unused).
* @param[in] data Pointer to rtnetlink @c ifinfomsg object
* corresponding to a specific network interface.
* @param[in] len Length of the Netlink message (reply) for the
* @c RTM_GETLINK command, potentially including
* @c rtattr attributes.
* @param[in] user_data Pointer to the @c mptcpd_nm object that
* contains the list (queue) to which network
* interface information will be inserted.
*/
static void handle_rtm_getlink(int error,
uint16_t type,
void const *data,
uint32_t len,
void *user_data)
{
if (handle_error(__func__, error) != 0)
return;
(void) type;
assert(type == RTM_NEWLINK);
struct ifinfomsg const *const ifi = data;
struct mptcpd_nm *const nm = user_data;
if (is_interface_ready(nm, ifi)) {
(void) insert_link(ifi, len, nm);
}
}
/**
* @brief Handle results from RTM_GETADDR rtnetlink command.
*
* @param[in] error Number of error (@c errno) that occurred
* during @c RTM_GETADDR operation.
* @param[in] type Netlink message content type (unused).
* @param[in] data Pointer to rtnetlink @c ifaddrmsg object
* corresponding to a specific network address.
* @param[in] len Length of the Netlink message (reply) for the
* @c RTM_GETADDR command, potentially including
* @c rtattr attributes.
* @param[in] user_data Pointer to the @c mptcpd_nm object that
* contains the list (queue) to which network
* address information will be inserted.
*/
static void handle_rtm_getaddr(int error,
uint16_t type,
void const *data,
uint32_t len,
void *user_data)
{
if (handle_error(__func__, error) != 0)
return;
(void) type;
assert(type == RTM_NEWADDR);
struct ifaddrmsg const *const ifa = data;
struct mptcpd_nm *const nm = user_data;
struct mptcpd_interface *const interface =
get_mptcpd_interface(ifa, nm);
if (interface == NULL)
return;
handle_ifaddr_func_t handler = insert_addr;
if (nm->notify_flags & MPTCPD_NOTIFY_FLAG_EXISTING)
handler = update_addr;
foreach_ifaddr(ifa, len, nm, interface, handler);
}
/**
* @brief Send rtnetlink command to retrieve network addresses.
*
* @param[in] user_data Pointer to the @c mptcpd_nm object that
* contains information needed to issue the
* rtnetlink RTM_GETADDR command.
*/
static void send_getaddr_command(void *user_data)
{
struct mptcpd_nm *const nm = user_data;
/*
Don't bother attempting to retrieve IP addresses if no
network interfaces are being tracked.
*/
if (l_queue_isempty(nm->interfaces))
return;
// Get IP addresses.
struct ifaddrmsg addr_msg = { .ifa_family = AF_UNSPEC };
if (netlink_send(nm->rtnl,
RTM_GETADDR,
NLM_F_DUMP,
&addr_msg,
sizeof(addr_msg),
handle_rtm_getaddr,
nm,
NULL) == 0) {
l_error("Unable to obtain IP addresses.");
/*
Continue running since addresses may be appear
dynamically later on.
*/
}
}
// -------------------------------------------------------------------
// Public API
// -------------------------------------------------------------------
struct mptcpd_nm *mptcpd_nm_create(uint32_t flags)
{
struct mptcpd_nm *const nm = l_new(struct mptcpd_nm, 1);
// No need to check for NULL. l_new() abort()s on failure.
nm->rtnl = l_netlink_new(NETLINK_ROUTE);
if (nm->rtnl == NULL) {
l_free(nm);
return NULL;
}
// Listen for link (network device) changes.
nm->link_id = l_netlink_register(nm->rtnl,
RTNLGRP_LINK,
handle_link,
nm, // user_data
NULL); // destroy
if (nm->link_id == 0) {
l_error("Unable to monitor network device changes.");
mptcpd_nm_destroy(nm);
return NULL;
}
// Listen for IPv4 address changes.
nm->ipv4_id = l_netlink_register(nm->rtnl,
RTNLGRP_IPV4_IFADDR,
handle_ifaddr,
nm, // user_data
NULL); // destroy
if (nm->ipv4_id == 0) {
l_error("Unable to monitor IPv4 address changes.");
mptcpd_nm_destroy(nm);
return NULL;
}
// Listen for IPv6 address changes.
nm->ipv6_id = l_netlink_register(nm->rtnl,
RTNLGRP_IPV6_IFADDR,
handle_ifaddr,
nm, // user_data
NULL); // destroy
if (nm->ipv6_id == 0) {
l_error("Unable to monitor IPv6 address changes.");
mptcpd_nm_destroy(nm);
return NULL;
}
nm->notify_flags = flags;
nm->interfaces = l_queue_new();
nm->ops = l_queue_new();
nm->monitor_loopback = false;
/**
* Get network interface information.
*
* @note We force the second rtnetlink command, RTM_GETADDR,
* to be sent after this one completes by doing so in
* the destroy callback for this command. That
* guarantees that the potential multipart message
* response from this dump is fully read prior to
* sending the second command. This works around an
* issue in ELL where it was possible that the second
* command would be sent prior to receiving all of the
* parts of the multipart @c RTM_GETLINK response, which
* resulted in an EBUSY error.
*/
struct ifinfomsg link_msg = { .ifi_family = AF_UNSPEC };
if (netlink_send(nm->rtnl,
RTM_GETLINK,
NLM_F_DUMP,
&link_msg,
sizeof(link_msg),
handle_rtm_getlink,
nm,
send_getaddr_command)
== 0) {
l_error("Unable to obtain network devices.");
mptcpd_nm_destroy(nm);
return NULL;
}
return nm;
}
void mptcpd_nm_destroy(struct mptcpd_nm *nm)
{
if (nm == NULL)
return;
if (nm->link_id != 0
&& !l_netlink_unregister(nm->rtnl, nm->link_id))
l_error("Failed to unregister link monitor.");
if (nm->ipv4_id != 0
&& !l_netlink_unregister(nm->rtnl, nm->ipv4_id))
l_error("Failed to unregister IPv4 monitor.");
if (nm->ipv6_id != 0
&& !l_netlink_unregister(nm->rtnl, nm->ipv6_id))
l_error("Failed to unregister IPv6 monitor.");
l_queue_destroy(nm->ops, l_free);
nm->ops = NULL;
l_queue_destroy(nm->interfaces, mptcpd_interface_destroy);
nm->interfaces = NULL;
l_netlink_destroy(nm->rtnl);
l_free(nm);
}
void mptcpd_nm_foreach_interface(struct mptcpd_nm const *nm,
mptcpd_nm_callback callback,
void *callback_data)
{
if (nm == NULL || callback == NULL)
return;
struct mptcpd_interface_callback_data cb_data = {
.callback = callback,
.user_data = callback_data
};
l_queue_foreach(nm->interfaces,
mptcpd_interface_callback,
&cb_data);
}
bool mptcpd_nm_register_ops(struct mptcpd_nm *nm,
struct mptcpd_nm_ops const *ops,
void *user_data)
{
if (nm == NULL || ops == NULL)
return false;
if (ops->new_interface == NULL
&& ops->update_interface == NULL
&& ops->delete_interface == NULL
&& ops->new_address == NULL
&& ops->delete_address == NULL) {
l_error("No network monitor event tracking "
"ops were set.");
return false;
}
struct nm_ops_info *const info = l_malloc(sizeof(*info));
info->ops = ops;
info->user_data = user_data;
bool const registered = l_queue_push_tail(nm->ops, info);
if (!registered)
l_free(info);
return registered;
}
bool mptcpd_nm_monitor_loopback(struct mptcpd_nm *nm, bool enable)
{
if (nm == NULL)
return false;
nm->monitor_loopback = enable;
return true;
}
/*
Local Variables:
c-file-style: "linux"
End:
*/
|