### Add Bonding Link Example (C) Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Example code demonstrating how to allocate, configure, and add a bonding type network link using libnl. ```c #include struct rtnl_link *link; link = rtnl_link_bond_alloc(); rtnl_link_set_name(link, "my_bond"); /* requires admin privileges */ if (rtnl_link_add(sk, link, NLM_F_CREATE) < 0) /* error */ rtnl_link_put(link); ``` -------------------------------- ### Example: Get Network Links with nl_send_simple Source: https://github.com/thom311/libnl/blob/main/doc/core.txt Demonstrates using nl_send_simple to send an RTM_GETLINK request. This example shows how to allocate a socket, connect to the NETLINK_ROUTE family, and send a request to retrieve a list of network links from the kernel. ```c #include struct nl_sock *sk; struct rtgenmsg rt_hdr = { .rtgen_family = AF_UNSPEC, }; sk = nl_socket_alloc(); nl_connect(sk, NETLINK_ROUTE); nl_send_simple(sock, RTM_GETLINK, NLM_F_DUMP, &rt_hdr, sizeof(rt_hdr)); ``` -------------------------------- ### Build c-list Project with Meson Source: https://github.com/thom311/libnl/blob/main/third_party/c-list/README.md These commands outline the standard procedure for building and installing the c-list project using the meson build system. It involves creating a build directory, setting up the build environment, compiling the code, running tests, and finally installing the project. ```shell mkdir build cd build meson setup .. ninja meson test ninja install ``` -------------------------------- ### Example: Direct Link Lookup (C) Source: https://github.com/thom311/libnl/blob/main/doc/route.txt An example illustrating the direct lookup of a network link by name. It shows how to call `rtnl_link_get_kernel`, handle potential errors, perform operations on the retrieved link, and then release the link's resources. ```c struct rtnl_link *link; if (rtnl_link_get_kernel(sock, 0, "eth1", &link) < 0) /* error */ /* do something with link */ rtnl_link_put(link); ``` -------------------------------- ### Add XFRMI Device Example (C) Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Provides a C code example for adding an xfrmi (IPsec transform) device. The snippet shows how to allocate the link, set its name, link index, and interface ID before adding it to the netlink socket. ```c struct rtnl_link *link; struct in_addr addr; /* allocate new link object of type xfrmi */ if(!(link = rtnl_link_xfrmi_alloc())) /* error */ /* set xfrmi name */ if ((err = rtnl_link_set_name(link, "ipsec0")) < 0) /* error */ /* set link index */ if ((err = rtnl_link_xfrmi_set_link(link, if_index)) < 0) /* error */ /* set if_id */ if ((err = rtnl_link_xfrmi_set_if_id(link, 16)) < 0) /* error */ if((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) /* error */ rtnl_link_put(link); ``` -------------------------------- ### Allocate and Access Qdisc Cache in C Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Explains how to allocate a cache for qdisc configurations and retrieve specific qdisc objects from it using C. This is used to get the current qdisc setup from the kernel. ```c #include int rtnl_qdisc_alloc_cache(struct nl_sock *sock, struct nl_cache **cache); struct rtnl_qdisc *rtnl_qdisc_get(struct nl_cache *cache, int, uint32_t); struct rtnl_qdisc *rtnl_qdisc_get_by_parent(struct nl_cache *, int, uint32_t); ``` -------------------------------- ### Modify Socket Callback Configuration Example Source: https://github.com/thom311/libnl/blob/main/doc/core.txt A concise example demonstrating how to modify the callback configuration for a Netlink socket. This specific example sets up a custom input function `my_input` to be called for all valid messages received by the socket `sk`. ```c #include // Call my_input() for all valid messages received in socket sk l_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, my_input, NULL); ``` -------------------------------- ### Retrieve Qdisc Configuration in C Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Shows a C example of retrieving the qdisc configuration from the kernel using `rtnl_link_alloc_cache`. It then demonstrates how to search for a specific qdisc by its interface index and handle, and print its details. ```c #include struct nl_cache *all_qdiscs; if (rtnl_link_alloc_cache(sock, &all_qdiscs) < 0) /* error while retrieving qdisc cfg */ struct rtnl_qdisc *qdisc; int ifindex; ifindex = rtnl_link_get_ifindex(eth0_obj); /* search for qdisc on eth0 with handle 1:0 */ if (!(qdisc = rtnl_qdisc_get(all_qdiscs, ifindex, TC_HANDLE(1, 0)))) /* no such qdisc found */ nl_object_dump(OBJ_CAST(qdisc), NULL); rtnl_qdisc_put(qdisc); ``` -------------------------------- ### Construct and Send Custom Netlink Messages in C Source: https://context7.com/thom311/libnl/llms.txt Demonstrates building and sending custom Netlink messages with attributes using libnl. This provides low-level control for protocol extensions and custom kernel modules. It requires libnl and standard C libraries. The example sends a request to get link information and processes the response. ```c #include #include #include int main(void) { struct nl_sock *sock; struct nl_msg *msg; struct nlmsghdr *nlh; int err; sock = nl_socket_alloc(); if ((err = nl_connect(sock, NETLINK_ROUTE)) < 0) { fprintf(stderr, "Connection failed: %s\n", nl_geterror(err)); return err; } /* Disable sequence number checking for this example */ nl_socket_disable_seq_check(sock); /* Allocate netlink message */ msg = nlmsg_alloc(); if (!msg) { fprintf(stderr, "Message allocation failed\n"); nl_close(sock); nl_socket_free(sock); return -1; } /* Build message header: type, flags, seq, port */ nlh = nlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, RTM_GETLINK, 0, NLM_F_REQUEST | NLM_F_DUMP); if (!nlh) { fprintf(stderr, "Failed to build message header\n"); nlmsg_free(msg); nl_close(sock); nl_socket_free(sock); return -1; } /* Add route family header */ struct rtgenmsg rt_hdr = { .rtgen_family = AF_UNSPEC, }; if (nlmsg_append(msg, &rt_hdr, sizeof(rt_hdr), NLMSG_ALIGNTO) < 0) { fprintf(stderr, "Failed to append data\n"); nlmsg_free(msg); nl_close(sock); nl_socket_free(sock); return -1; } /* Send message and wait for ACK */ err = nl_send_auto(sock, msg); if (err < 0) { fprintf(stderr, "Send failed: %s\n", nl_geterror(err)); nlmsg_free(msg); nl_close(sock); nl_socket_free(sock); return err; } printf("Message sent, %d bytes\n", err); /* Receive and process response */ err = nl_recvmsgs_default(sock); if (err < 0) { fprintf(stderr, "Receive failed: %s\n", nl_geterror(err)); } nlmsg_free(msg); nl_close(sock); nl_socket_free(sock); return 0; } ``` -------------------------------- ### Example: Add an IP6TNL Tunnel Device (C) Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Provides a C code example for creating and configuring an IP6TNL tunnel device using libnl. It demonstrates allocating the link, setting its name, link index, local IPv6 address, and remote IPv6 address before adding it. ```c struct rtnl_link *link struct in6_addr addr link = rtnl_link_ip6_tnl_alloc(); rtnl_link_set_name(link, "ip6tnl-tun"); rtnl_link_ip6_tnl_set_link(link, if_index); inet_pton(AF_INET6, "2607:f0d0:1002:51::4", &addr); rtnl_link_ip6_tnl_set_local(link, &addr); inet_pton(AF_INET6, "2607:f0d0:1002:52::5", &addr); rtnl_link_ip6_tnl_set_remote(link, &addr); rtnl_link_add(sk, link, NLM_F_CREATE); rtnl_link_put(link); ``` -------------------------------- ### Netlink Socket Example: Notifications and Multicast Source: https://github.com/thom311/libnl/blob/main/doc/core.txt A comprehensive example demonstrating the usage of libnl sockets. It includes allocating a socket, disabling sequence number checking, setting a custom callback for valid messages, connecting to the Netlink route protocol, subscribing to link notifications, and continuously receiving messages. This showcases a practical implementation for event notifications. ```c #include #include #include /* * This function will be called for each valid netlink message received * in nl_recvmsgs_default() */ static int my_func(struct nl_msg *msg, void *arg) { return 0; } struct nl_sock *sk; /* Allocate a new socket */ sk = nl_socket_alloc(); /* * Notifications do not use sequence numbers, disable sequence number * checking. */ l_socket_disable_seq_check(sk); /* * Define a callback function, which will be called for each notification * received */ l_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, my_func, NULL); /* Connect to routing netlink protocol */ l_connect(sk, NETLINK_ROUTE); /* Subscribe to link notifications group */ l_socket_add_memberships(sk, RTNLGRP_LINK, 0); /* * Start receiving messages. The function nl_recvmsgs_default() will block * until one or more netlink messages (notification) are received which * will be passed on to my_func(). */ while (1) nl_recvmsgs_default(sock); ``` -------------------------------- ### Example: Retrieving Traffic Control Statistics (Drops and Queue Length) Source: https://github.com/thom311/libnl/blob/main/doc/route.txt An example demonstrating how to retrieve specific traffic control statistics, such as the number of dropped packets and the current queue length, using the `rtnl_tc_get_stat` function. Requires including the netlink/route/tc.h header. ```c #include uint64_t drops, qlen; drops = rtnl_tc_get_stat(TC_CAST(qdisc), RTNL_TC_DROPS); qlen = rtnl_tc_get_stat(TC_CAST(qdisc), RTNL_TC_QLEN); ``` -------------------------------- ### Add a VXLAN Device in C Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Example C code demonstrating how to allocate, configure, and add a VXLAN network device using libnl. It covers setting interface name, VXLAN ID, and multicast address. ```c struct rtnl_link *link; struct nl_addr* addr; /* allocate new link object of type vxlan */ link = rtnl_link_vxlan_alloc(); /* set interface name */ rtnl_link_set_name(link, "vxlan128"); /* set VXLAN network identifier */ if ((err = rtnl_link_vxlan_set_id(link, 128)) < 0) /* error */ /* set multicast address to join */ if ((err = nl_addr_parse("239.0.0.1", AF_INET, &addr)) < 0) /* error */ if ((err = rtnl_link_set_group(link, addr)) < 0) /* error */ nl_addr_put(addr); if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) /* error */ rtnl_link_put(link); ``` -------------------------------- ### GET /qdiscs Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Retrieves the current qdisc configuration from the kernel by constructing an RTM_GETQDISC netlink message. ```APIDOC ## Retrieving Qdisc Configuration ### Description The function `rtnl_qdisc_alloc_cache()` is used to retrieve the current qdisc configuration in the kernel. It constructs an `RTM_GETQDISC` netlink message to request all configured qdiscs. ### Method GET (simulated via netlink message) ### Endpoint `/qdiscs` (logical endpoint for retrieving all qdiscs) ### Parameters #### Query Parameters - **sock** (`struct nl_sock *`) - Required - The netlink socket to use for communication. #### Request Body None ### Request Example ```c #include struct nl_cache *all_qdiscs; // Assuming 'sock' is an initialized netlink socket if (rtnl_link_alloc_cache(sock, &all_qdiscs) < 0) { /* error while retrieving qdisc cfg */ } ``` ### Cache Access Functions - **Search qdisc with matching ifindex and handle:** ```c struct rtnl_qdisc *rtnl_qdisc_get(struct nl_cache *cache, int ifindex, uint32_t handle); ``` - **Search qdisc with matching ifindex and parent:** ```c struct rtnl_qdisc *rtnl_qdisc_get_by_parent(struct nl_cache *cache, int ifindex , uint32_t parent); ``` - **Generic cache functions:** (e.g., `nl_cache_search()`, `nl_cache_dump()`, etc.) ### Response #### Success Response (200) - **all_qdiscs** (`struct nl_cache *`) - A cache containing all qdisc configurations. #### Response Example // No direct response body, but the cache is populated. // Example of dumping a qdisc from the cache: ```c struct rtnl_qdisc *qdisc; int ifindex; ifindex = rtnl_link_get_ifindex(eth0_obj); // Assuming eth0_obj is obtained elsewhere /* search for qdisc on eth0 with handle 1:0 */ if (!(qdisc = rtnl_qdisc_get(all_qdiscs, ifindex, TC_HANDLE(1, 0)))) { /* no such qdisc found */ } nl_object_dump(OBJ_CAST(qdisc), NULL); rtnl_qdisc_put(qdisc); ``` ``` -------------------------------- ### Set and Get Traffic Control Object Kind (C) Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Illustrates how to set and get the 'kind' string, which specifies the type of qdisc, class, or classifier. Setting the kind is essential before using type-specific API functions. ```c int rtnl_tc_set_kind(struct rtnl_tc *tc, const char *kind); char *rtnl_tc_get_kind(struct rtnl_tc *tc); ``` -------------------------------- ### Example: Link Cache Usage (C) Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Demonstrates the typical workflow for using a link cache. This includes allocating the cache, retrieving a link by name, performing operations on the link, and then freeing the resources (link and cache). Error handling is included for the cache allocation and link retrieval steps. ```c struct nl_cache *cache; struct rtnl_link *link; if (rtnl_link_alloc_cache(sock, AF_UNSPEC, &cache)) < 0) /* error */ if (!(link = rtnl_link_get_by_name(cache, "eth1"))) /* link does not exist */ /* do something with link */ rtnl_link_put(link); nl_cache_put(cache); ``` -------------------------------- ### Add ip6vti Tunnel Device Example (C) Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Illustrates the process of creating and configuring an ip6vti tunnel device. This includes allocating the link object, setting its name, link index, local and remote IPv6 addresses, and finally adding it to the network. ```c struct rtnl_link *link; struct in6_addr addr; link = rtnl_link_ip6vti_alloc(); rtnl_link_set_name(link, "ip6vti-tun"); rtnl_link_ip6vti_set_link(link, if_index); inet_pton(AF_INET6, "2607:f0d0:1002:51::4", &addr); rtnl_link_ip6vti_set_local(link, &addr); inet_pton(AF_INET6, "2607:f0d0:1002:52::5", &addr); rtnl_link_ip6vti_set_remote(link, &addr); rtnl_link_add(sk, link, NLM_F_CREATE); rtnl_link_put(link); ``` -------------------------------- ### Linking libnl with GCC Source: https://github.com/thom311/libnl/blob/main/doc/core.txt This command-line example shows how to compile a C program (`myprogram.c`) and link it against the libnl library using GCC. It utilizes `pkg-config` to automatically retrieve the necessary compiler and linker flags. ```bash $ gcc myprogram.c -o myprogram $(pkgconfig --cflags --libs libnl-3.0) ``` -------------------------------- ### Example: Allocating and Initializing a Netlink Message Source: https://github.com/thom311/libnl/blob/main/doc/core.txt Demonstrates the basic steps of allocating a Netlink message using `nlmsg_alloc()` and preparing to add a custom header structure. Further steps would involve using `nlmsg_put()` to finalize the Netlink header. ```c #include struct nlmsghdr *hdr; struct nl_msg *msg; struct myhdr { uint32_t foo1, foo2; } shdr = { 10, 20 }; /* Allocate a message with the default maximum message size */ msg = nlmsg_alloc(); /* ``` -------------------------------- ### Delete Link by Name Example Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Example demonstrating how to delete a network link by its name using libnl. ```APIDOC ## Delete Link by Name Example This example shows how to delete a network link using its name. ### Steps 1. Allocate a link object. 2. Set the name of the link to be deleted. 3. Call `rtnl_link_delete` to remove the link. 4. Free the link object. ### Code Example ```c #include #include int main() { struct rtnl_link *link; struct nl_sock *sock; // Assume sock is already created and bound // sock = nl_socket_alloc(); // nl_connect(sock, NETLINK_ROUTE); link = rtnl_link_alloc(); if (!link) { // Handle error return 1; } rtnl_link_set_name(link, "my_vlan"); if (rtnl_link_delete(sock, link) < 0) { // Handle error } rtnl_link_put(link); // nl_socket_free(sock); return 0; } ``` ``` -------------------------------- ### Example: Customizing Callback Set Source: https://github.com/thom311/libnl/blob/main/doc/core.txt Demonstrates allocating a callback set, initializing it to a default verbose configuration, and then modifying it to include a custom handler for valid messages. It also shows how to set a verbose default error handler that directs output to a specified file descriptor. ```c #include /* Allocate a callback set and initialize it to the verbose default set */ struct nl_cb *cb = nl_cb_alloc(NL_CB_VERBOSE); /* Modify the set to call my_func() for all valid messages */ nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, my_func, NULL); /* * Set the error message handler to the verbose default implementation * and direct it to print all errors to the given file descriptor. */ FILE *file = fopen(...); nl_cb_err(cb, NL_CB_VERBOSE, NULL, file); ``` -------------------------------- ### C: Netlink GET Request Flags Source: https://github.com/thom311/libnl/blob/main/doc/core.txt This C code defines additional flags specific to Netlink GET requests. These include flags for returning based on the tree root, matching entries, atomic operations, and dumping all objects. ```c #define NLM_F_ROOT 0x100 #define NLM_F_MATCH 0x200 #define NLM_F_ATOMIC 0x400 #define NLM_F_DUMP (NLM_F_ROOT|NLM_F_MATCH) ``` -------------------------------- ### Example: Add an IPVTI Tunnel Device (C) Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Demonstrates how to create and configure an IPVTI tunnel device using libnl functions. It covers allocating the link object, setting its name, link index, local address, and remote address before adding it to the network. ```c struct rtnl_link *link struct in_addr addr /* allocate new link object of type ipvti */ if(!(link = rtnl_link_ipvti_alloc())) /* error */ /* set ipvti tunnel name */ if ((err = rtnl_link_set_name(link, "ipvti-tun")) < 0) /* error */ /* set link index */ if ((err = rtnl_link_ipvti_set_link(link, if_index)) < 0) /* error */ /* set local address */ inet_pton(AF_INET, "192.168.254.12", &addr.s_addr); if ((err = rtnl_link_ipvti_set_local(link, addr.s_addr)) < 0) /* error */ /* set remote address */ inet_pton(AF_INET, "192.168.254.13", &addr.s_addr if ((err = rtnl_link_ipvti_set_remote(link, addr.s_addr)) < 0) /* error */ if((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) /* error */ rtnl_link_put(link); ``` -------------------------------- ### Include Netlink Header Files Source: https://github.com/thom311/libnl/blob/main/doc/core.txt This C code example shows the essential header files to include when working with the libnl library. It covers the main netlink header and specific headers for cache management and routing link functionalities. ```c #include #include #include ``` -------------------------------- ### ip6vti Link Management Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Functions for allocating, setting, and getting parameters for ip6vti tunnel links. ```APIDOC ## IP6VTI Link Management ### Description Provides functions to manage ip6vti (IPv6 Virtual Tunnel Interface) tunnel interfaces. ### Functions - `rtnl_link_is_ip6vti(link)`: Checks if a link is an ip6vti type. - `rtnl_link_ip6vti_alloc()`: Allocates memory for a new ip6vti link object. - `rtnl_link_ip6vti_add(sk, name)`: Adds a new ip6vti tunnel device with the given name. - `rtnl_link_ip6vti_set_link(link, index)`: Sets the underlying interface index for the ip6vti tunnel. - `rtnl_link_ip6vti_get_link(link, index)`: Retrieves the underlying interface index of the ip6vti tunnel. - `rtnl_link_ip6vti_set_ikey(link, ikey)`: Sets the inbound security association key (IKEY) for the ip6vti tunnel. - `rtnl_link_ip6vti_get_ikey(link, ikey)`: Retrieves the inbound security association key (IKEY) of the ip6vti tunnel. - `rtnl_link_ip6vti_set_okey(link, okey)`: Sets the outbound security association key (OKEY) for the ip6vti tunnel. - `rtnl_link_ip6vti_get_okey(link, okey)`: Retrieves the outbound security association key (OKEY) of the ip6vti tunnel. - `rtnl_link_ip6vti_set_local(link, local)`: Sets the local IPv6 address for the ip6vti tunnel. - `rtnl_link_ip6vti_get_local(link, remote)`: Retrieves the local IPv6 address of the ip6vti tunnel. - `rtnl_link_ip6vti_set_remote(link, remote)`: Sets the remote IPv6 address for the ip6vti tunnel. - `rtnl_link_ip6vti_get_remote(link, remote)`: Retrieves the remote IPv6 address of the ip6vti tunnel. - `rtnl_link_ip6vti_set_fwmark(link, fwmark)`: Sets the firewall mark for the ip6vti tunnel. - `rtnl_link_ip6vti_get_fwmark(link, fwmark)`: Retrieves the firewall mark of the ip6vti tunnel. ### Example: Add an ip6vti tunnel device ```c struct rtnl_link *link; struct in6_addr addr; link = rtnl_link_ip6vti_alloc(); rtnl_link_set_name(link, "ip6vti-tun"); rtnl_link_ip6vti_set_link(link, if_index); inet_pton(AF_INET6, "2607:f0d0:1002:51::4", &addr); rtnl_link_ip6vti_set_local(link, &addr); inet_pton(AF_INET6, "2607:f0d0:1002:52::5", &addr); rtnl_link_ip6vti_set_remote(link, &addr); rtnl_link_add(sk, link, NLM_F_CREATE); rtnl_link_put(link); ``` ``` -------------------------------- ### ip6gre Link Management Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Functions for allocating, setting, and getting parameters for ip6gre tunnel links. ```APIDOC ## ip6gre Link Management ### Description Provides functions to manage ip6gre (IPv6 in GRE) tunnel interfaces. ### Functions - `rtnl_link_ip6gre_alloc()`: Allocates memory for a new ip6gre link object. - `rtnl_link_ip6gre_set_local(link, local)`: Sets the local IPv6 address for the ip6gre tunnel. - `rtnl_link_ip6gre_get_local(link, local)`: Retrieves the local IPv6 address of the ip6gre tunnel. - `rtnl_link_ip6gre_set_remote(link, remote)`: Sets the remote IPv6 address for the ip6gre tunnel. - `rtnl_link_ip6gre_get_remote(link, remote)`: Retrieves the remote IPv6 address of the ip6gre tunnel. - `rtnl_link_ip6gre_set_ttl(link, ttl)`: Sets the Time To Live (TTL) value for the ip6gre tunnel. - `rtnl_link_ip6gre_get_ttl(link, ttl)`: Retrieves the TTL value of the ip6gre tunnel. - `rtnl_link_ip6gre_set_encaplimit(link, encaplimit)`: Sets the encapsulation limit for the ip6gre tunnel. - `rtnl_link_ip6gre_get_encaplimit(link, encaplimit)`: Retrieves the encapsulation limit of the ip6gre tunnel. - `rtnl_link_ip6gre_set_flowinfo(link, flowinfo)`: Sets the flow information for the ip6gre tunnel. - `rtnl_link_ip6gre_get_flowinfo(link, flowinfo)`: Retrieves the flow information of the ip6gre tunnel. - `rtnl_link_ip6gre_set_flags(link, flags)`: Sets various flags for the ip6gre tunnel. - `rtnl_link_ip6gre_get_flags(link, flags)`: Retrieves the flags of the ip6gre tunnel. - `rtnl_link_ip6gre_set_fwmark(link, fwmark)`: Sets the firewall mark for the ip6gre tunnel. - `rtnl_link_ip6gre_get_fwmark(link, fwmark)`: Retrieves the firewall mark of the ip6gre tunnel. ### Example: Add an ip6gre tunnel device ```c struct rtnl_link *link; struct in6_addr addr; link = rtnl_link_ip6gre_alloc(); rtnl_link_set_name(link, "ip6gre-tun"); rtnl_link_ip6gre_set_link(link, if_index); inet_pton(AF_INET6, "2607:f0d0:1002:51::4", &addr); rtnl_link_ip6gre_set_local(link, &addr); inet_pton(AF_INET6, "2607:f0d0:1002:52::5", &addr); rtnl_link_ip6gre_set_remote(link, &addr); rtnl_link_add(sk, link, NLM_F_CREATE); rtnl_link_put(link); ``` ``` -------------------------------- ### Add ip6gre Tunnel Device Example (C) Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Demonstrates how to allocate, configure, and add an ip6gre tunnel device using libnl functions. It involves setting the device name, link index, local and remote addresses, and then adding the link to the netlink socket. ```c struct rtnl_link *link; struct in6_addr addr; link = rtnl_link_ip6gre_alloc(); rtnl_link_set_name(link, "ip6gre-tun"); rtnl_link_ip6gre_set_link(link, if_index); inet_pton(AF_INET6, "2607:f0d0:1002:51::4", &addr); rtnl_link_ip6gre_set_local(link, &addr); inet_pton(AF_INET6, "2607:f0d0:1002:52::5", &addr); rtnl_link_ip6gre_set_remote(link, &addr); rtnl_link_add(sk, link, NLM_F_CREATE); rtnl_link_put(link); ``` -------------------------------- ### xfrmi Link Management Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Functions for allocating, setting, and getting parameters for xfrmi tunnel links. ```APIDOC ## XFRMI Link Management ### Description Provides functions to manage xfrmi (IPsec transform set interface) tunnel interfaces. ### Functions - `rtnl_link_xfrmi_alloc()`: Allocates memory for a new xfrmi link object. - `rtnl_link_xfrmi_set_link(link, index)`: Sets the underlying interface index for the xfrmi tunnel. - `rtnl_link_xfrmi_get_link(link)`: Retrieves the underlying interface index of the xfrmi tunnel. - `rtnl_link_xfrmi_set_if_id(link, if_id)`: Sets the interface ID for the xfrmi tunnel. - `rtnl_link_xfrmi_get_if_id(link)`: Retrieves the interface ID of the xfrmi tunnel. ### Example: Add a xfrmi device ```c struct rtnl_link *link; struct in_addr addr; int err; /* allocate new link object of type xfrmi */ if(!(link = rtnl_link_xfrmi_alloc())) /* error */ /* set xfrmi name */ if ((err = rtnl_link_set_name(link, "ipsec0")) < 0) /* error */ /* set link index */ if ((err = rtnl_link_xfrmi_set_link(link, if_index)) < 0) /* error */ /* set if_id */ if ((err = rtnl_link_xfrmi_set_if_id(link, 16)) < 0) /* error */ if((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) /* error */ rtnl_link_put(link); ``` ``` -------------------------------- ### Netlink Core Module Import Example Source: https://github.com/thom311/libnl/blob/main/python/doc/core.rst This snippet demonstrates the basic import statement for the netlink.core module, aliasing it as 'netlink' for easier use in subsequent code. This is a common pattern for accessing the module's functionalities. ```python import netlink.core as netlink ``` -------------------------------- ### Add an IPIP Tunnel Device in C Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Example C code for creating an IPIP tunnel device using libnl. It demonstrates allocating the link, setting the name, link index, local and remote addresses, and TTL. ```c struct rtnl_link *link struct in_addr addr /* allocate new link object of type ipip */ if(!(link = rtnl_link_ipip_alloc())) /* error */ /* set ipip tunnel name */ if ((err = rtnl_link_set_name(link, "ipip-tun")) < 0) /* error */ /* set link index */ if ((err = rtnl_link_ipip_set_link(link, if_index)) < 0) /* error */ /* set local address */ inet_pton(AF_INET, "192.168.254.12", &addr.s_addr); if ((err = rtnl_link_ipip_set_local(link, addr.s_addr)) < 0) /* error */ /* set remote address */ inet_pton(AF_INET, "192.168.254.13", &addr.s_addr if ((err = rtnl_link_ipip_set_remote(link, addr.s_addr)) < 0) /* error */ /* set tunnel ttl */ if ((err = rtnl_link_ipip_set_ttl(link, 64)) < 0) /* error */ if((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) /* error */ rtnl_link_put(link); ``` -------------------------------- ### Set and Get Number of RX/TX Queues for Link (C) Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Functions to set and retrieve the number of transmit (TX) and receive (RX) queues for a network link. These are considered when creating new network devices. ```c #include void rtnl_link_set_num_tx_queues(struct rtnl_link *link, uint32_t nqueues); uint32_t rtnl_link_get_num_tx_queues(struct rtnl_link *link); void rtnl_link_set_num_rx_queues(struct rtnl_link *link, uint32_t nqueues); uint32_t rtnl_link_get_num_rx_queues(struct rtnl_link *link); ``` -------------------------------- ### Set and Get Link Queueing Discipline (C) Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Functions to set and retrieve the name of the queueing discipline (qdisc) for a link. Note that the set function is for comparison purposes only, as qdisc modification is handled via route_tc. ```c #include void rtnl_link_set_qdisc(struct rtnl_link *link, const char *name); char *rtnl_link_get_qdisc(struct rtnl_link *link); ``` -------------------------------- ### Create and Inspect Abstract Address Source: https://github.com/thom311/libnl/blob/main/python/doc/core.rst This example illustrates the creation of an AbstractAddress object from a string representation of an IP address with a CIDR notation. It then shows how to access properties like the prefix length, address family, and the address itself. It also demonstrates equality comparison between two AbstractAddress objects. ```python addr = netlink.AbstractAddress('127.0.0.1/8') print addr.prefixlen # => '8' print addr.family # => 'inet' print len(addr) # => '4' (32bit ipv4 address) a = netlink.AbstractAddress('10.0.0.1/24') b = netlink.AbstractAddress('10.0.0.2/24') print a == b # => False ``` -------------------------------- ### Configure VXLAN Link Parameters in C Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Provides C functions for setting and getting various parameters for VXLAN network links. These include TTL, TOS, learning status, ageing time, port range, proxy settings, RSC, L2 miss, and L3 miss. ```c extern int rtnl_link_vxlan_set_ttl(struct rtnl_link *, uint8_t); extern int rtnl_link_vxlan_get_ttl(struct rtnl_link *); extern int rtnl_link_vxlan_set_tos(struct rtnl_link *, uint8_t); extern int rtnl_link_vxlan_get_tos(struct rtnl_link *); extern int rtnl_link_vxlan_set_learning(struct rtnl_link *, uint8_t); extern int rtnl_link_vxlan_get_learning(struct rtnl_link *); extern int rtnl_link_vxlan_enable_learning(struct rtnl_link *); extern int rtnl_link_vxlan_disable_learning(struct rtnl_link *); extern int rtnl_link_vxlan_set_ageing(struct rtnl_link *, uint32_t); extern int rtnl_link_vxlan_get_ageing(struct rtnl_link *, uint32_t *); extern int rtnl_link_vxlan_set_limit(struct rtnl_link *, uint32_t); extern int rtnl_link_vxlan_get_limit(struct rtnl_link *, uint32_t *); extern int rtnl_link_vxlan_set_port_range(struct rtnl_link *, struct ifla_vxlan_port_range *); extern int rtnl_link_vxlan_get_port_range(struct rtnl_link *, struct ifla_vxlan_port_range *); extern int rtnl_link_vxlan_set_proxy(struct rtnl_link *, uint8_t); extern int rtnl_link_vxlan_get_proxy(struct rtnl_link *); extern int rtnl_link_vxlan_enable_proxy(struct rtnl_link *); extern int rtnl_link_vxlan_disable_proxy(struct rtnl_link *); extern int rtnl_link_vxlan_set_rsc(struct rtnl_link *, uint8_t); extern int rtnl_link_vxlan_get_rsc(struct rtnl_link *); extern int rtnl_link_vxlan_enable_rsc(struct rtnl_link *); extern int rtnl_link_vxlan_disable_rsc(struct rtnl_link *); extern int rtnl_link_vxlan_set_l2miss(struct rtnl_link *, uint8_t); extern int rtnl_link_vxlan_get_l2miss(struct rtnl_link *); extern int rtnl_link_vxlan_enable_l2miss(struct rtnl_link *); extern int rtnl_link_vxlan_disable_l2miss(struct rtnl_link *); extern int rtnl_link_vxlan_set_l3miss(struct rtnl_link *, uint8_t); extern int rtnl_link_vxlan_get_l3miss(struct rtnl_link *); extern int rtnl_link_vxlan_enable_l3miss(struct rtnl_link *); extern int rtnl_link_vxlan_disable_l3miss(struct rtnl_link *); ``` -------------------------------- ### List Network Interfaces - C Source: https://context7.com/thom311/libnl/llms.txt Retrieves and displays network interface information using libnl's link cache API. This example connects to NETLINK_ROUTE, allocates a link cache, and iterates through available network links. It requires 'netlink/netlink.h', 'netlink/route/link.h', and 'netlink/cache.h'. ```c #include #include #include int main(void) { struct nl_sock *sock; struct nl_cache *link_cache; struct rtnl_link *link; int err; sock = nl_socket_alloc(); if ((err = nl_connect(sock, NETLINK_ROUTE)) < 0) { fprintf(stderr, "Connection failed: %s\n", nl_geterror(err)); return err; } /* Allocate link cache with all address families */ if ((err = rtnl_link_alloc_cache(sock, AF_UNSPEC, &link_cache)) < 0) { fprintf(stderr, "Cache allocation failed: %s\n", nl_geterror(err)); nl_close(sock); nl_socket_free(sock); return err; } /* Iterate through all links */ link = (struct rtnl_link *)nl_cache_get_first(link_cache); while (link != NULL) { printf("Interface: %s (index: %d, MTU: %d)\n", rtnl_link_get_name(link), rtnl_link_get_ifindex(link), rtnl_link_get_mtu(link)); link = (struct rtnl_link *)nl_cache_get_next((struct nl_object *)link); } nl_cache_free(link_cache); nl_close(sock); nl_socket_free(sock); return 0; } ``` -------------------------------- ### Get List of Links Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Retrieves a list of all network links configured in the kernel. It involves allocating a link cache, sending a RTM_GETLINK message, and parsing the RTM_NEWLINK replies. ```APIDOC ## Get List of Links To retrieve the list of links in the kernel, allocate a new link cache using `rtnl_link_alloc_cache()` to hold the links. It will automatically construct and send a `RTM_GETLINK` message requesting a dump of all links from the kernel and feed the returned `RTM_NEWLINK` to the internal link message parser which adds the returned links to the cache. ### Function Signature ```c #include int rtnl_link_alloc_cache(struct nl_sock *sk, int family, struct nl_cache **result); ``` ### Parameters * **sk** (struct nl_sock *) - The netlink socket. * **family** (int) - The address family to filter links by. `AF_UNSPEC` retrieves all families. * **result** (struct nl_cache **) - A pointer to store the allocated link cache. ### Description The cache will contain link objects (`struct rtnl_link`) and can be accessed using standard cache functions. Setting the `family` parameter to an address family other than `AF_UNSPEC` will result in a cache containing only links supporting the specified address family. ### Related Functions * `rtnl_link_get(struct nl_cache *cache, int ifindex)`: Retrieves a link from the cache by its interface index. * `rtnl_link_get_by_name(struct nl_cache *cache, const char *name)`: Retrieves a link from the cache by its name. ### Example ```c #include struct nl_cache *cache; struct rtnl_link *link; // Assuming 'sock' is an initialized nl_sock if (rtnl_link_alloc_cache(sock, AF_UNSPEC, &cache) < 0) { // Handle error } link = rtnl_link_get_by_name(cache, "eth1"); if (!link) { // Link does not exist } /* do something with link */ rtnl_link_put(link); nl_cache_put(cache); ``` ``` -------------------------------- ### Configure HTB Qdisc Rate to Quantum Ratio Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Functions to get and set the rate-to-quantum ratio for an HTB qdisc. This ratio influences how bandwidth is translated into quantum values for scheduling. These functions operate on a `struct rtnl_qdisc` and expect a `uint32_t` value for the rate2quantum setting. ```c uint32_t rtnl_htb_get_rate2quantum(struct rtnl_qdisc *qdisc); int rtnl_htb_set_rate2quantum(struct rtnl_qdisc *qdisc, uint32_t rate2quantum); ``` -------------------------------- ### Configure HTB Qdisc Default Class Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Functions to get and set the default class for an HTB (Hierarchical Token Bucket) qdisc. The default class is used for unclassified traffic. These functions operate on a `struct rtnl_qdisc` and expect a `uint32_t` value representing the default class handle. ```c uint32_t rtnl_htb_get_defcls(struct rtnl_qdisc *qdisc); int rtnl_htb_set_defcls(struct rtnl_qdisc *qdisc, uint32_t defcls); ``` -------------------------------- ### Set and Get Address Family (C) Source: https://github.com/thom311/libnl/blob/main/doc/core.txt Allows setting and retrieving the address family (e.g., AF_INET, AF_INET6) of an abstract network address. This is useful when the family is not known at allocation time or needs to be modified. Requires ``. ```c #include void nl_addr_set_family(struct nl_addr *addr, int family); int nl_addr_get_family(struct nl_addr *addr); ``` -------------------------------- ### List of Qdisc Implementations Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Provides a list of available Queueing Disciplines (qdiscs) supported by libnl, indicating whether they are classful and a brief description. ```APIDOC ## Qdisc Implementations This section lists the available Queueing Disciplines (qdiscs) supported by libnl. ### Qdisc Implementations Table | Qdisc | Classful | Description | |-----------|----------|-------------| | ATM | Yes | FIXME | | Blackhole | No | This qdisc will drop all packets passed to it. | | CBQ | Yes | The CBQ (Class Based Queueing) is a classful qdisc which allows creating traffic classes and enforce bandwidth limitations for each class. | | DRR | Yes | The DRR (Deficit Round Robin) scheduler is a classful qdisc implementing fair queueing. Each class is assigned a quantum specifying the maximum number of bytes that can be served per round. Unused quantum at the end of the round is carried over to the next round. | | DSMARK | Yes | FIXME | | FIFO | No | FIXME | | GRED | No | FIXME | | HFSC | Yes | FIXME | | HTB | Yes | FIXME | | mq | Yes | FIXME | | multiq | Yes | FIXME | | netem | No | FIXME | | Prio | Yes | FIXME | | RED | Yes | FIXME | | SFQ | Yes | FIXME | | TBF | Yes | FIXME | | teql | No | FIXME | ``` -------------------------------- ### Create and Apply Attribute to Netlink Object Source: https://github.com/thom311/libnl/blob/main/python/doc/core.rst This example shows how to create a netlink Object and then apply a specific attribute-value pair to it. It demonstrates modifying an object, such as setting the 'mtu' for a link object. This operation may raise KeyError for unknown attributes or ImmutableError for immutable ones. ```python obj = netlink.Object("route/link", "link") obj.apply("mtu", 1200) # Sets attribute mtu to 1200 (link obj) ``` -------------------------------- ### Set and Get Traffic Control Object Interface Index (C) Source: https://github.com/thom311/libnl/blob/main/doc/route.txt Shows how to set and get the interface index for a traffic control object, specifying the network device it is attached to. `rtnl_tc_set_link()` is preferred for storing a link object reference. ```c void rtnl_tc_set_ifindex(struct rtnl_tc *tc, int ifindex); void rtnl_tc_set_link(struct rtnl_tc *tc, struct rtnl_link *link); int rtnl_tc_get_ifindex(struct rtnl_tc *tc); ``` -------------------------------- ### Set and Get Link Interface Index in libnl Source: https://github.com/thom311/libnl/blob/main/doc/route.txt This C code shows how to set and get the interface index (ifindex) for a network link using libnl. The ifindex is a unique integer identifier for a link. `rtnl_link_set_ifindex()` assigns the index, and `rtnl_link_get_ifindex()` retrieves it. This is useful for identifying existing links. ```c #include struct rtnl_link *link; int ifindex = 1; int retrieved_ifindex; // Allocate and potentially initialize link object... // Set the interface index rtnl_link_set_ifindex(link, ifindex); // Get the interface index retrieved_ifindex = rtnl_link_get_ifindex(link); // Use retrieved_ifindex // Free the link object... ```