Page MenuHomeFreeBSD

netlink: add snl writer
ClosedPublic

Authored by melifaro on Mar 7 2023, 5:36 PM.
Tags
None
Referenced Files
Unknown Object (File)
Thu, Nov 14, 5:11 PM
Unknown Object (File)
Oct 13 2024, 6:46 AM
Unknown Object (File)
Sep 17 2024, 8:00 PM
Unknown Object (File)
Sep 16 2024, 5:37 PM
Unknown Object (File)
Sep 16 2024, 5:37 PM
Unknown Object (File)
Sep 16 2024, 5:37 PM
Unknown Object (File)
Sep 16 2024, 5:37 PM
Unknown Object (File)
Sep 16 2024, 5:37 PM
Subscribers

Details

Summary

Add basic netlink writing API.

Usage:

struct snl_state ss = {};
struct snl_writer nw;
int nl_type = 0, nl_flags = 0;

if (!snl_init(&ss, NETLINK_ROUTE))
        return (ENOTSUP);
snl_init_writer(&ss, &nw);

struct nlmsghdr *hdr = snl_create_msg_request(&nw, nl_type);
hdr->nlmsg_flags |= nl_flags;

struct rtmsg *rtm = snl_reserve_msg_object(&nw, struct rtmsg);
rtm->rtm_family = dst->sa_family;
rtm->rtm_protocol = RTPROT_STATIC;
rtm->rtm_type = rtm_type;
rtm->rtm_dst_len = plen;

snl_add_msg_attr_ip(&nw, RTA_DST, dst);
snl_add_msg_attr_u32(&nw, RTA_TABLE, fib);

hdr = snl_finalize_msg(&nw);
if (hdr != NULL) {
        hdr = snl_get_reply(&ss, hdr);
        struct snl_errmsg_data e = {};
        if (!snl_check_return(&ss, hdr, &e))
                return (EINVAL);
        return (e.error);
}

Documentation to follow soon.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

melifaro added reviewers: network, kp.
sys/netlink/netlink_snl.h
616

This makes clang sad:

In file included from netlink_netlink_snl.c:1:
/usr/obj/usr/src/amd64.amd64/tmp/usr/include/netlink/netlink_snl.h:568:13: error: cast from 'char *' to 'struct nlmsghdr *' increases required alignment from 1 to 4 [-Werror,-Wcast-align]
                nw->hdr = (struct nlmsghdr *)((char *)new_base + hdr_off);
                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sys/netlink/netlink_snl.h
616

Ack. Will add (void *) cast to silence this.

This revision was not accepted when it landed; it landed in state Needs Review.Mar 9 2023, 2:35 PM
This revision was automatically updated to reflect the committed changes.