Blame src/platform/nm-netlink.c

Packit Service 87a54e
/* SPDX-License-Identifier: GPL-2.0-or-later */
Packit 5756e2
/*
Packit 5756e2
 * Copyright (C) 2018 Red Hat, Inc.
Packit 5756e2
 */
Packit 5756e2
Packit 5756e2
#include "nm-default.h"
Packit 5756e2
Packit 5756e2
#include "nm-netlink.h"
Packit 5756e2
Packit 5756e2
#include <unistd.h>
Packit 5756e2
#include <fcntl.h>
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
#ifndef SOL_NETLINK
Packit Service a1bd4f
    #define SOL_NETLINK 270
Packit 5756e2
#endif
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
#define NL_SOCK_PASSCRED     (1 << 1)
Packit Service a1bd4f
#define NL_MSG_PEEK          (1 << 3)
Packit Service a1bd4f
#define NL_MSG_PEEK_EXPLICIT (1 << 4)
Packit Service a1bd4f
#define NL_NO_AUTO_ACK       (1 << 5)
Packit 5756e2
Packit 5756e2
#ifndef NETLINK_EXT_ACK
Packit Service a1bd4f
    #define NETLINK_EXT_ACK 11
Packit 5756e2
#endif
Packit 5756e2
Packit 5756e2
struct nl_msg {
Packit Service a1bd4f
    int                nm_protocol;
Packit Service a1bd4f
    struct sockaddr_nl nm_src;
Packit Service a1bd4f
    struct sockaddr_nl nm_dst;
Packit Service a1bd4f
    struct ucred       nm_creds;
Packit Service a1bd4f
    struct nlmsghdr *  nm_nlh;
Packit Service a1bd4f
    size_t             nm_size;
Packit Service a1bd4f
    bool               nm_creds_has : 1;
Packit 5756e2
};
Packit 5756e2
Packit 5756e2
struct nl_sock {
Packit Service a1bd4f
    struct sockaddr_nl s_local;
Packit Service a1bd4f
    struct sockaddr_nl s_peer;
Packit Service a1bd4f
    int                s_fd;
Packit Service a1bd4f
    int                s_proto;
Packit Service a1bd4f
    unsigned int       s_seq_next;
Packit Service a1bd4f
    unsigned int       s_seq_expect;
Packit Service a1bd4f
    int                s_flags;
Packit Service a1bd4f
    size_t             s_bufsize;
Packit 5756e2
};
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
NM_UTILS_ENUM2STR_DEFINE(nl_nlmsgtype2str,
Packit Service a1bd4f
                         int,
Packit Service a1bd4f
                         NM_UTILS_ENUM2STR(NLMSG_NOOP, "NOOP"),
Packit Service a1bd4f
                         NM_UTILS_ENUM2STR(NLMSG_ERROR, "ERROR"),
Packit Service a1bd4f
                         NM_UTILS_ENUM2STR(NLMSG_DONE, "DONE"),
Packit Service a1bd4f
                         NM_UTILS_ENUM2STR(NLMSG_OVERRUN, "OVERRUN"), );
Packit Service a1bd4f
Packit Service a1bd4f
NM_UTILS_FLAGS2STR_DEFINE(nl_nlmsg_flags2str,
Packit Service a1bd4f
                          int,
Packit Service a1bd4f
                          NM_UTILS_FLAGS2STR(NLM_F_REQUEST, "REQUEST"),
Packit Service a1bd4f
                          NM_UTILS_FLAGS2STR(NLM_F_MULTI, "MULTI"),
Packit Service a1bd4f
                          NM_UTILS_FLAGS2STR(NLM_F_ACK, "ACK"),
Packit Service a1bd4f
                          NM_UTILS_FLAGS2STR(NLM_F_ECHO, "ECHO"),
Packit Service a1bd4f
                          NM_UTILS_FLAGS2STR(NLM_F_ROOT, "ROOT"),
Packit Service a1bd4f
                          NM_UTILS_FLAGS2STR(NLM_F_MATCH, "MATCH"),
Packit Service a1bd4f
                          NM_UTILS_FLAGS2STR(NLM_F_ATOMIC, "ATOMIC"),
Packit Service a1bd4f
                          NM_UTILS_FLAGS2STR(NLM_F_REPLACE, "REPLACE"),
Packit Service a1bd4f
                          NM_UTILS_FLAGS2STR(NLM_F_EXCL, "EXCL"),
Packit Service a1bd4f
                          NM_UTILS_FLAGS2STR(NLM_F_CREATE, "CREATE"),
Packit Service a1bd4f
                          NM_UTILS_FLAGS2STR(NLM_F_APPEND, "APPEND"), );
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
const char *
Packit Service a1bd4f
nl_nlmsghdr_to_str(const struct nlmsghdr *hdr, char *buf, gsize len)
Packit 5756e2
{
Packit Service a1bd4f
    const char *b;
Packit Service a1bd4f
    const char *s;
Packit Service a1bd4f
    guint       flags, flags_before;
Packit Service a1bd4f
    const char *prefix;
Packit Service a1bd4f
Packit Service a1bd4f
    if (!nm_utils_to_string_buffer_init_null(hdr, &buf, &len))
Packit Service a1bd4f
        return buf;
Packit Service a1bd4f
Packit Service a1bd4f
    b = buf;
Packit Service a1bd4f
Packit Service a1bd4f
    switch (hdr->nlmsg_type) {
Packit Service a1bd4f
    case RTM_GETLINK:
Packit Service a1bd4f
        s = "RTM_GETLINK";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_NEWLINK:
Packit Service a1bd4f
        s = "RTM_NEWLINK";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_DELLINK:
Packit Service a1bd4f
        s = "RTM_DELLINK";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_SETLINK:
Packit Service a1bd4f
        s = "RTM_SETLINK";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_GETADDR:
Packit Service a1bd4f
        s = "RTM_GETADDR";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_NEWADDR:
Packit Service a1bd4f
        s = "RTM_NEWADDR";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_DELADDR:
Packit Service a1bd4f
        s = "RTM_DELADDR";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_GETROUTE:
Packit Service a1bd4f
        s = "RTM_GETROUTE";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_NEWROUTE:
Packit Service a1bd4f
        s = "RTM_NEWROUTE";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_DELROUTE:
Packit Service a1bd4f
        s = "RTM_DELROUTE";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_GETRULE:
Packit Service a1bd4f
        s = "RTM_GETRULE";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_NEWRULE:
Packit Service a1bd4f
        s = "RTM_NEWRULE";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_DELRULE:
Packit Service a1bd4f
        s = "RTM_DELRULE";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_GETQDISC:
Packit Service a1bd4f
        s = "RTM_GETQDISC";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_NEWQDISC:
Packit Service a1bd4f
        s = "RTM_NEWQDISC";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_DELQDISC:
Packit Service a1bd4f
        s = "RTM_DELQDISC";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_GETTFILTER:
Packit Service a1bd4f
        s = "RTM_GETTFILTER";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_NEWTFILTER:
Packit Service a1bd4f
        s = "RTM_NEWTFILTER";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_DELTFILTER:
Packit Service a1bd4f
        s = "RTM_DELTFILTER";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case NLMSG_NOOP:
Packit Service a1bd4f
        s = "NLMSG_NOOP";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case NLMSG_ERROR:
Packit Service a1bd4f
        s = "NLMSG_ERROR";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case NLMSG_DONE:
Packit Service a1bd4f
        s = "NLMSG_DONE";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case NLMSG_OVERRUN:
Packit Service a1bd4f
        s = "NLMSG_OVERRUN";
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    default:
Packit Service a1bd4f
        s = NULL;
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (s)
Packit Service a1bd4f
        nm_utils_strbuf_append_str(&buf, &len, s);
Packit Service a1bd4f
    else
Packit Service a1bd4f
        nm_utils_strbuf_append(&buf, &len, "(%u)", (unsigned) hdr->nlmsg_type);
Packit Service a1bd4f
Packit Service a1bd4f
    flags = hdr->nlmsg_flags;
Packit Service a1bd4f
Packit Service a1bd4f
    if (!flags) {
Packit Service a1bd4f
        nm_utils_strbuf_append_str(&buf, &len, ", flags 0");
Packit Service a1bd4f
        goto flags_done;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
#define _F(f, n)                                                   \
Packit Service a1bd4f
    G_STMT_START                                                   \
Packit Service a1bd4f
    {                                                              \
Packit Service a1bd4f
        if (NM_FLAGS_ALL(flags, f)) {                              \
Packit Service a1bd4f
            flags &= ~(f);                                         \
Packit Service a1bd4f
            nm_utils_strbuf_append(&buf, &len, "%s%s", prefix, n); \
Packit Service a1bd4f
            if (!flags)                                            \
Packit Service a1bd4f
                goto flags_done;                                   \
Packit Service a1bd4f
            prefix = ",";                                          \
Packit Service a1bd4f
        }                                                          \
Packit Service a1bd4f
    }                                                              \
Packit Service a1bd4f
    G_STMT_END
Packit Service a1bd4f
Packit Service a1bd4f
    prefix       = ", flags ";
Packit Service a1bd4f
    flags_before = flags;
Packit Service a1bd4f
    _F(NLM_F_REQUEST, "request");
Packit Service a1bd4f
    _F(NLM_F_MULTI, "multi");
Packit Service a1bd4f
    _F(NLM_F_ACK, "ack");
Packit Service a1bd4f
    _F(NLM_F_ECHO, "echo");
Packit Service a1bd4f
    _F(NLM_F_DUMP_INTR, "dump_intr");
Packit Service a1bd4f
    _F(0x20 /*NLM_F_DUMP_FILTERED*/, "dump_filtered");
Packit Service a1bd4f
Packit Service a1bd4f
    if (flags_before != flags)
Packit Service a1bd4f
        prefix = ";";
Packit Service a1bd4f
Packit Service a1bd4f
    switch (hdr->nlmsg_type) {
Packit Service a1bd4f
    case RTM_NEWLINK:
Packit Service a1bd4f
    case RTM_NEWADDR:
Packit Service a1bd4f
    case RTM_NEWROUTE:
Packit Service a1bd4f
    case RTM_NEWQDISC:
Packit Service a1bd4f
    case RTM_NEWTFILTER:
Packit Service a1bd4f
        _F(NLM_F_REPLACE, "replace");
Packit Service a1bd4f
        _F(NLM_F_EXCL, "excl");
Packit Service a1bd4f
        _F(NLM_F_CREATE, "create");
Packit Service a1bd4f
        _F(NLM_F_APPEND, "append");
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case RTM_GETLINK:
Packit Service a1bd4f
    case RTM_GETADDR:
Packit Service a1bd4f
    case RTM_GETROUTE:
Packit Service a1bd4f
    case RTM_DELQDISC:
Packit Service a1bd4f
    case RTM_DELTFILTER:
Packit Service a1bd4f
        _F(NLM_F_DUMP, "dump");
Packit Service a1bd4f
        _F(NLM_F_ROOT, "root");
Packit Service a1bd4f
        _F(NLM_F_MATCH, "match");
Packit Service a1bd4f
        _F(NLM_F_ATOMIC, "atomic");
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    }
Packit 5756e2
Packit 5756e2
#undef _F
Packit 5756e2
Packit Service a1bd4f
    if (flags_before != flags)
Packit Service a1bd4f
        prefix = ";";
Packit Service a1bd4f
    nm_utils_strbuf_append(&buf, &len, "%s0x%04x", prefix, flags);
Packit 5756e2
Packit 5756e2
flags_done:
Packit 5756e2
Packit Service a1bd4f
    nm_utils_strbuf_append(&buf, &len, ", seq %u", (unsigned) hdr->nlmsg_seq);
Packit 5756e2
Packit Service a1bd4f
    return b;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
struct nlmsghdr *
Packit Service a1bd4f
nlmsg_hdr(struct nl_msg *n)
Packit 5756e2
{
Packit Service a1bd4f
    return n->nm_nlh;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
void *
Packit Service a1bd4f
nlmsg_reserve(struct nl_msg *n, size_t len, int pad)
Packit 5756e2
{
Packit Service a1bd4f
    char * buf       = (char *) n->nm_nlh;
Packit Service a1bd4f
    size_t nlmsg_len = n->nm_nlh->nlmsg_len;
Packit Service a1bd4f
    size_t tlen;
Packit 5756e2
Packit Service a1bd4f
    nm_assert(pad >= 0);
Packit 5756e2
Packit Service a1bd4f
    if (len > n->nm_size)
Packit Service a1bd4f
        return NULL;
Packit 5756e2
Packit Service a1bd4f
    tlen = pad ? ((len + (pad - 1)) & ~(pad - 1)) : len;
Packit 5756e2
Packit Service a1bd4f
    if ((tlen + nlmsg_len) > n->nm_size)
Packit Service a1bd4f
        return NULL;
Packit 5756e2
Packit Service a1bd4f
    buf += nlmsg_len;
Packit Service a1bd4f
    n->nm_nlh->nlmsg_len += tlen;
Packit 5756e2
Packit Service a1bd4f
    if (tlen > len)
Packit Service a1bd4f
        memset(buf + len, 0, tlen - len);
Packit 5756e2
Packit Service a1bd4f
    return buf;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
struct nlattr *
Packit Service a1bd4f
nla_reserve(struct nl_msg *msg, int attrtype, int attrlen)
Packit 5756e2
{
Packit Service a1bd4f
    struct nlattr *nla;
Packit Service a1bd4f
    int            tlen;
Packit 5756e2
Packit Service a1bd4f
    if (attrlen < 0)
Packit Service a1bd4f
        return NULL;
Packit 5756e2
Packit Service a1bd4f
    tlen = NLMSG_ALIGN(msg->nm_nlh->nlmsg_len) + nla_total_size(attrlen);
Packit 5756e2
Packit Service a1bd4f
    if (tlen > msg->nm_size)
Packit Service a1bd4f
        return NULL;
Packit 5756e2
Packit Service a1bd4f
    nla           = (struct nlattr *) nlmsg_tail(msg->nm_nlh);
Packit Service a1bd4f
    nla->nla_type = attrtype;
Packit Service a1bd4f
    nla->nla_len  = nla_attr_size(attrlen);
Packit 5756e2
Packit Service a1bd4f
    if (attrlen)
Packit Service a1bd4f
        memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
Packit Service a1bd4f
    msg->nm_nlh->nlmsg_len = tlen;
Packit 5756e2
Packit Service a1bd4f
    return nla;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
struct nl_msg *
Packit Service a1bd4f
nlmsg_alloc_size(size_t len)
Packit 5756e2
{
Packit Service a1bd4f
    struct nl_msg *nm;
Packit Service a1bd4f
Packit Service a1bd4f
    if (len < sizeof(struct nlmsghdr))
Packit Service a1bd4f
        len = sizeof(struct nlmsghdr);
Packit Service a1bd4f
Packit Service a1bd4f
    nm  = g_slice_new(struct nl_msg);
Packit Service a1bd4f
    *nm = (struct nl_msg){
Packit Service a1bd4f
        .nm_protocol = -1,
Packit Service a1bd4f
        .nm_size     = len,
Packit Service a1bd4f
        .nm_nlh      = g_malloc0(len),
Packit Service a1bd4f
    };
Packit Service a1bd4f
    nm->nm_nlh->nlmsg_len = nlmsg_total_size(0);
Packit Service a1bd4f
    return nm;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * Allocate a new netlink message with the default maximum payload size.
Packit 5756e2
 *
Packit 5756e2
 * Allocates a new netlink message without any further payload. The
Packit 5756e2
 * maximum payload size defaults to PAGESIZE or as otherwise specified
Packit 5756e2
 * with nlmsg_set_default_size().
Packit 5756e2
 *
Packit 5756e2
 * @return Newly allocated netlink message or NULL.
Packit 5756e2
 */
Packit 5756e2
struct nl_msg *
Packit Service a1bd4f
nlmsg_alloc(void)
Packit 5756e2
{
Packit Service a1bd4f
    return nlmsg_alloc_size(nm_utils_getpagesize());
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
struct nl_msg *
Packit Service a1bd4f
nlmsg_alloc_convert(struct nlmsghdr *hdr)
Packit 5756e2
{
Packit Service a1bd4f
    struct nl_msg *nm;
Packit 5756e2
Packit Service a1bd4f
    nm = nlmsg_alloc_size(NLMSG_ALIGN(hdr->nlmsg_len));
Packit Service a1bd4f
    memcpy(nm->nm_nlh, hdr, hdr->nlmsg_len);
Packit Service a1bd4f
    return nm;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
struct nl_msg *
Packit Service a1bd4f
nlmsg_alloc_simple(int nlmsgtype, int flags)
Packit 5756e2
{
Packit Service a1bd4f
    struct nl_msg *nm;
Packit Service a1bd4f
    struct nlmsghdr *new;
Packit Service a1bd4f
Packit Service a1bd4f
    nm               = nlmsg_alloc();
Packit Service a1bd4f
    new              = nm->nm_nlh;
Packit Service a1bd4f
    new->nlmsg_type  = nlmsgtype;
Packit Service a1bd4f
    new->nlmsg_flags = flags;
Packit Service a1bd4f
    return nm;
Packit 5756e2
}
Packit 5756e2
Packit Service a1bd4f
void
Packit Service a1bd4f
nlmsg_free(struct nl_msg *msg)
Packit 5756e2
{
Packit Service a1bd4f
    if (!msg)
Packit Service a1bd4f
        return;
Packit 5756e2
Packit Service a1bd4f
    g_free(msg->nm_nlh);
Packit Service a1bd4f
    g_slice_free(struct nl_msg, msg);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nlmsg_append(struct nl_msg *n, const void *data, size_t len, int pad)
Packit 5756e2
{
Packit Service a1bd4f
    void *tmp;
Packit 5756e2
Packit Service a1bd4f
    nm_assert(n);
Packit Service a1bd4f
    nm_assert(data);
Packit Service a1bd4f
    nm_assert(len > 0);
Packit Service a1bd4f
    nm_assert(pad >= 0);
Packit 5756e2
Packit Service a1bd4f
    tmp = nlmsg_reserve(n, len, pad);
Packit Service a1bd4f
    if (tmp == NULL)
Packit Service a1bd4f
        return -ENOMEM;
Packit 5756e2
Packit Service a1bd4f
    memcpy(tmp, data, len);
Packit Service a1bd4f
    return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nlmsg_parse(struct nlmsghdr *        nlh,
Packit Service a1bd4f
            int                      hdrlen,
Packit Service a1bd4f
            struct nlattr *          tb[],
Packit Service a1bd4f
            int                      maxtype,
Packit Service a1bd4f
            const struct nla_policy *policy)
Packit 5756e2
{
Packit Service a1bd4f
    if (!nlmsg_valid_hdr(nlh, hdrlen))
Packit Service a1bd4f
        return -NME_NL_MSG_TOOSHORT;
Packit 5756e2
Packit Service a1bd4f
    return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen), nlmsg_attrlen(nlh, hdrlen), policy);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
struct nlmsghdr *
Packit Service a1bd4f
nlmsg_put(struct nl_msg *n, uint32_t pid, uint32_t seq, int type, int payload, int flags)
Packit 5756e2
{
Packit Service a1bd4f
    struct nlmsghdr *nlh;
Packit 5756e2
Packit Service a1bd4f
    if (n->nm_nlh->nlmsg_len < NLMSG_HDRLEN)
Packit Service a1bd4f
        g_return_val_if_reached(NULL);
Packit 5756e2
Packit Service a1bd4f
    nlh              = (struct nlmsghdr *) n->nm_nlh;
Packit Service a1bd4f
    nlh->nlmsg_type  = type;
Packit Service a1bd4f
    nlh->nlmsg_flags = flags;
Packit Service a1bd4f
    nlh->nlmsg_pid   = pid;
Packit Service a1bd4f
    nlh->nlmsg_seq   = seq;
Packit 5756e2
Packit Service a1bd4f
    if (payload > 0 && nlmsg_reserve(n, payload, NLMSG_ALIGNTO) == NULL)
Packit Service a1bd4f
        return NULL;
Packit 5756e2
Packit Service a1bd4f
    return nlh;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
size_t
Packit Service a1bd4f
nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
Packit 5756e2
{
Packit Service a1bd4f
    const char *src;
Packit Service a1bd4f
    size_t      srclen;
Packit Service a1bd4f
    size_t      len;
Packit Service a1bd4f
Packit Service a1bd4f
    /* - Always writes @dstsize bytes to @dst
Packit Service a1bd4f
     * - Copies the first non-NUL characters to @dst.
Packit Service a1bd4f
     *   Any characters after the first NUL bytes in @nla are ignored.
Packit Service a1bd4f
     * - If the string @nla is longer than @dstsize, the string
Packit Service a1bd4f
     *   gets truncated. @dst will always be NUL terminated. */
Packit Service a1bd4f
Packit Service a1bd4f
    if (G_UNLIKELY(dstsize <= 1)) {
Packit Service a1bd4f
        if (dstsize == 1)
Packit Service a1bd4f
            dst[0] = '\0';
Packit Service a1bd4f
        if (nla && (srclen = nla_len(nla)) > 0)
Packit Service a1bd4f
            return strnlen(nla_data(nla), srclen);
Packit Service a1bd4f
        return 0;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    nm_assert(dst);
Packit Service a1bd4f
Packit Service a1bd4f
    if (nla) {
Packit Service a1bd4f
        srclen = nla_len(nla);
Packit Service a1bd4f
        if (srclen > 0) {
Packit Service a1bd4f
            src    = nla_data(nla);
Packit Service a1bd4f
            srclen = strnlen(src, srclen);
Packit Service a1bd4f
            if (srclen > 0) {
Packit Service a1bd4f
                len = NM_MIN(dstsize - 1, srclen);
Packit Service a1bd4f
                memcpy(dst, src, len);
Packit Service a1bd4f
                memset(&dst[len], 0, dstsize - len);
Packit Service a1bd4f
                return srclen;
Packit Service a1bd4f
            }
Packit Service a1bd4f
        }
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    memset(dst, 0, dstsize);
Packit Service a1bd4f
    return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
size_t
Packit Service a1bd4f
nla_memcpy(void *dst, const struct nlattr *nla, size_t dstsize)
Packit 5756e2
{
Packit Service a1bd4f
    size_t len;
Packit Service a1bd4f
    int    srclen;
Packit Service a1bd4f
Packit Service a1bd4f
    if (!nla)
Packit Service a1bd4f
        return 0;
Packit Service a1bd4f
Packit Service a1bd4f
    srclen = nla_len(nla);
Packit Service a1bd4f
Packit Service a1bd4f
    if (srclen <= 0) {
Packit Service a1bd4f
        nm_assert(srclen == 0);
Packit Service a1bd4f
        return 0;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    len = NM_MIN((size_t) srclen, dstsize);
Packit Service a1bd4f
    if (len > 0) {
Packit Service a1bd4f
        /* there is a crucial difference between nla_strlcpy() and nla_memcpy().
Packit Service a1bd4f
         * The former always write @dstsize bytes (akin to strncpy()), here, we only
Packit Service a1bd4f
         * write the bytes that we actually have (leaving the remainder undefined). */
Packit Service a1bd4f
        memcpy(dst, nla_data(nla), len);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    return srclen;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nla_put(struct nl_msg *msg, int attrtype, int datalen, const void *data)
Packit 5756e2
{
Packit Service a1bd4f
    struct nlattr *nla;
Packit 5756e2
Packit Service a1bd4f
    nla = nla_reserve(msg, attrtype, datalen);
Packit Service a1bd4f
    if (!nla) {
Packit Service a1bd4f
        if (datalen < 0)
Packit Service a1bd4f
            g_return_val_if_reached(-NME_BUG);
Packit 5756e2
Packit Service a1bd4f
        return -ENOMEM;
Packit Service a1bd4f
    }
Packit 5756e2
Packit Service a1bd4f
    if (datalen > 0)
Packit Service a1bd4f
        memcpy(nla_data(nla), data, datalen);
Packit 5756e2
Packit Service a1bd4f
    return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
struct nlattr *
Packit Service a1bd4f
nla_find(const struct nlattr *head, int len, int attrtype)
Packit 5756e2
{
Packit Service a1bd4f
    const struct nlattr *nla;
Packit Service a1bd4f
    int                  rem;
Packit 5756e2
Packit Service a1bd4f
    nla_for_each_attr (nla, head, len, rem) {
Packit Service a1bd4f
        if (nla_type(nla) == attrtype)
Packit Service a1bd4f
            return (struct nlattr *) nla;
Packit Service a1bd4f
    }
Packit 5756e2
Packit Service a1bd4f
    return NULL;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
void
Packit Service a1bd4f
nla_nest_cancel(struct nl_msg *msg, const struct nlattr *attr)
Packit 5756e2
{
Packit Service a1bd4f
    ssize_t len;
Packit Service a1bd4f
Packit Service a1bd4f
    len = (char *) nlmsg_tail(msg->nm_nlh) - (char *) attr;
Packit Service a1bd4f
    if (len < 0)
Packit Service a1bd4f
        g_return_if_reached();
Packit Service a1bd4f
    else if (len > 0) {
Packit Service a1bd4f
        msg->nm_nlh->nlmsg_len -= len;
Packit Service a1bd4f
        memset(nlmsg_tail(msg->nm_nlh), 0, len);
Packit Service a1bd4f
    }
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
struct nlattr *
Packit Service a1bd4f
nla_nest_start(struct nl_msg *msg, int attrtype)
Packit 5756e2
{
Packit Service a1bd4f
    struct nlattr *start = (struct nlattr *) nlmsg_tail(msg->nm_nlh);
Packit 5756e2
Packit Service a1bd4f
    if (nla_put(msg, NLA_F_NESTED | attrtype, 0, NULL) < 0)
Packit Service a1bd4f
        return NULL;
Packit 5756e2
Packit Service a1bd4f
    return start;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static int
Packit Service a1bd4f
_nest_end(struct nl_msg *msg, struct nlattr *start, int keep_empty)
Packit 5756e2
{
Packit Service a1bd4f
    size_t pad, len;
Packit Service a1bd4f
Packit Service a1bd4f
    len = (char *) nlmsg_tail(msg->nm_nlh) - (char *) start;
Packit Service a1bd4f
Packit Service a1bd4f
    if (len > USHRT_MAX || (!keep_empty && len == NLA_HDRLEN)) {
Packit Service a1bd4f
        /*
Packit Service a1bd4f
         * Max nlattr size exceeded or empty nested attribute, trim the
Packit Service a1bd4f
         * attribute header again
Packit Service a1bd4f
         */
Packit Service a1bd4f
        nla_nest_cancel(msg, start);
Packit Service a1bd4f
Packit Service a1bd4f
        /* Return error only if nlattr size was exceeded */
Packit Service a1bd4f
        return (len == NLA_HDRLEN) ? 0 : -NME_NL_ATTRSIZE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    start->nla_len = len;
Packit Service a1bd4f
Packit Service a1bd4f
    pad = NLMSG_ALIGN(msg->nm_nlh->nlmsg_len) - msg->nm_nlh->nlmsg_len;
Packit Service a1bd4f
    if (pad > 0) {
Packit Service a1bd4f
        /*
Packit Service a1bd4f
         * Data inside attribute does not end at a alignment boundary.
Packit Service a1bd4f
         * Pad accordingly and account for the additional space in
Packit Service a1bd4f
         * the message. nlmsg_reserve() may never fail in this situation,
Packit Service a1bd4f
         * the allocate message buffer must be a multiple of NLMSG_ALIGNTO.
Packit Service a1bd4f
         */
Packit Service a1bd4f
        if (!nlmsg_reserve(msg, pad, 0))
Packit Service a1bd4f
            g_return_val_if_reached(-NME_BUG);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Packit 5756e2
{
Packit Service a1bd4f
    return _nest_end(msg, start, 0);
Packit 5756e2
}
Packit 5756e2
Packit Service a1bd4f
static const uint16_t nla_attr_minlen[NLA_TYPE_MAX + 1] = {
Packit Service a1bd4f
    [NLA_U8]     = sizeof(uint8_t),
Packit Service a1bd4f
    [NLA_U16]    = sizeof(uint16_t),
Packit Service a1bd4f
    [NLA_U32]    = sizeof(uint32_t),
Packit Service a1bd4f
    [NLA_U64]    = sizeof(uint64_t),
Packit Service a1bd4f
    [NLA_STRING] = 1,
Packit Service a1bd4f
    [NLA_FLAG]   = 0,
Packit 5756e2
};
Packit 5756e2
Packit 5756e2
static int
Packit Service a1bd4f
validate_nla(const struct nlattr *nla, int maxtype, const struct nla_policy *policy)
Packit 5756e2
{
Packit Service a1bd4f
    const struct nla_policy *pt;
Packit Service a1bd4f
    unsigned int             minlen = 0;
Packit Service a1bd4f
    int                      type   = nla_type(nla);
Packit 5756e2
Packit Service a1bd4f
    if (type < 0 || type > maxtype)
Packit Service a1bd4f
        return 0;
Packit 5756e2
Packit Service a1bd4f
    pt = &policy[type];
Packit 5756e2
Packit Service a1bd4f
    if (pt->type > NLA_TYPE_MAX)
Packit Service a1bd4f
        g_return_val_if_reached(-NME_BUG);
Packit 5756e2
Packit Service a1bd4f
    if (pt->minlen)
Packit Service a1bd4f
        minlen = pt->minlen;
Packit Service a1bd4f
    else if (pt->type != NLA_UNSPEC)
Packit Service a1bd4f
        minlen = nla_attr_minlen[pt->type];
Packit 5756e2
Packit Service a1bd4f
    if (nla_len(nla) < minlen)
Packit Service a1bd4f
        return -NME_UNSPEC;
Packit 5756e2
Packit Service a1bd4f
    if (pt->maxlen && nla_len(nla) > pt->maxlen)
Packit Service a1bd4f
        return -NME_UNSPEC;
Packit 5756e2
Packit Service a1bd4f
    if (pt->type == NLA_STRING) {
Packit Service a1bd4f
        const char *data;
Packit 5756e2
Packit Service a1bd4f
        nm_assert(minlen > 0);
Packit 5756e2
Packit Service a1bd4f
        data = nla_data(nla);
Packit Service a1bd4f
        if (data[nla_len(nla) - 1] != '\0')
Packit Service a1bd4f
            return -NME_UNSPEC;
Packit Service a1bd4f
    }
Packit 5756e2
Packit Service a1bd4f
    return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nla_parse(struct nlattr *          tb[],
Packit Service a1bd4f
          int                      maxtype,
Packit Service a1bd4f
          struct nlattr *          head,
Packit Service a1bd4f
          int                      len,
Packit Service a1bd4f
          const struct nla_policy *policy)
Packit 5756e2
{
Packit Service a1bd4f
    struct nlattr *nla;
Packit Service a1bd4f
    int            rem, nmerr;
Packit 5756e2
Packit Service a1bd4f
    memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
Packit 5756e2
Packit Service a1bd4f
    nla_for_each_attr (nla, head, len, rem) {
Packit Service a1bd4f
        int type = nla_type(nla);
Packit 5756e2
Packit Service a1bd4f
        if (type > maxtype)
Packit Service a1bd4f
            continue;
Packit 5756e2
Packit Service a1bd4f
        if (policy) {
Packit Service a1bd4f
            nmerr = validate_nla(nla, maxtype, policy);
Packit Service a1bd4f
            if (nmerr < 0)
Packit Service a1bd4f
                return nmerr;
Packit Service a1bd4f
        }
Packit 5756e2
Packit Service a1bd4f
        tb[type] = nla;
Packit Service a1bd4f
    }
Packit 5756e2
Packit Service a1bd4f
    return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nlmsg_get_proto(struct nl_msg *msg)
Packit 5756e2
{
Packit Service a1bd4f
    return msg->nm_protocol;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
void
Packit Service a1bd4f
nlmsg_set_proto(struct nl_msg *msg, int protocol)
Packit 5756e2
{
Packit Service a1bd4f
    msg->nm_protocol = protocol;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
void
Packit Service a1bd4f
nlmsg_set_src(struct nl_msg *msg, struct sockaddr_nl *addr)
Packit 5756e2
{
Packit Service a1bd4f
    memcpy(&msg->nm_src, addr, sizeof(*addr));
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
struct ucred *
Packit Service a1bd4f
nlmsg_get_creds(struct nl_msg *msg)
Packit 5756e2
{
Packit Service a1bd4f
    if (msg->nm_creds_has)
Packit Service a1bd4f
        return &msg->nm_creds;
Packit Service a1bd4f
    return NULL;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
void
Packit Service a1bd4f
nlmsg_set_creds(struct nl_msg *msg, struct ucred *creds)
Packit 5756e2
{
Packit Service a1bd4f
    if (creds) {
Packit Service a1bd4f
        memcpy(&msg->nm_creds, creds, sizeof(*creds));
Packit Service a1bd4f
        msg->nm_creds_has = TRUE;
Packit Service a1bd4f
    } else
Packit Service a1bd4f
        msg->nm_creds_has = FALSE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
void *
Packit Service a1bd4f
genlmsg_put(struct nl_msg *msg,
Packit Service a1bd4f
            uint32_t       port,
Packit Service a1bd4f
            uint32_t       seq,
Packit Service a1bd4f
            int            family,
Packit Service a1bd4f
            int            hdrlen,
Packit Service a1bd4f
            int            flags,
Packit Service a1bd4f
            uint8_t        cmd,
Packit Service a1bd4f
            uint8_t        version)
Packit 5756e2
{
Packit Service a1bd4f
    struct nlmsghdr * nlh;
Packit Service a1bd4f
    struct genlmsghdr hdr = {
Packit Service a1bd4f
        .cmd     = cmd,
Packit Service a1bd4f
        .version = version,
Packit Service a1bd4f
    };
Packit 5756e2
Packit Service a1bd4f
    nlh = nlmsg_put(msg, port, seq, family, GENL_HDRLEN + hdrlen, flags);
Packit Service a1bd4f
    if (nlh == NULL)
Packit Service a1bd4f
        return NULL;
Packit 5756e2
Packit Service a1bd4f
    memcpy(nlmsg_data(nlh), &hdr, sizeof(hdr));
Packit 5756e2
Packit Service a1bd4f
    return (char *) nlmsg_data(nlh) + GENL_HDRLEN;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
void *
Packit Service a1bd4f
genlmsg_data(const struct genlmsghdr *gnlh)
Packit 5756e2
{
Packit Service a1bd4f
    return ((unsigned char *) gnlh + GENL_HDRLEN);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
void *
Packit Service a1bd4f
genlmsg_user_hdr(const struct genlmsghdr *gnlh)
Packit 5756e2
{
Packit Service a1bd4f
    return genlmsg_data(gnlh);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
struct genlmsghdr *
Packit Service a1bd4f
genlmsg_hdr(struct nlmsghdr *nlh)
Packit 5756e2
{
Packit Service a1bd4f
    return nlmsg_data(nlh);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
void *
Packit Service a1bd4f
genlmsg_user_data(const struct genlmsghdr *gnlh, const int hdrlen)
Packit 5756e2
{
Packit Service a1bd4f
    return (char *) genlmsg_user_hdr(gnlh) + NLMSG_ALIGN(hdrlen);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
struct nlattr *
Packit Service a1bd4f
genlmsg_attrdata(const struct genlmsghdr *gnlh, int hdrlen)
Packit 5756e2
{
Packit Service a1bd4f
    return genlmsg_user_data(gnlh, hdrlen);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
genlmsg_len(const struct genlmsghdr *gnlh)
Packit 5756e2
{
Packit Service a1bd4f
    const struct nlmsghdr *nlh;
Packit 5756e2
Packit Service a1bd4f
    nlh = (const struct nlmsghdr *) ((const unsigned char *) gnlh - NLMSG_HDRLEN);
Packit Service a1bd4f
    return (nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
genlmsg_attrlen(const struct genlmsghdr *gnlh, int hdrlen)
Packit 5756e2
{
Packit Service a1bd4f
    return genlmsg_len(gnlh) - NLMSG_ALIGN(hdrlen);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
genlmsg_valid_hdr(struct nlmsghdr *nlh, int hdrlen)
Packit 5756e2
{
Packit Service a1bd4f
    struct genlmsghdr *ghdr;
Packit 5756e2
Packit Service a1bd4f
    if (!nlmsg_valid_hdr(nlh, GENL_HDRLEN))
Packit Service a1bd4f
        return 0;
Packit 5756e2
Packit Service a1bd4f
    ghdr = nlmsg_data(nlh);
Packit Service a1bd4f
    if (genlmsg_len(ghdr) < NLMSG_ALIGN(hdrlen))
Packit Service a1bd4f
        return 0;
Packit 5756e2
Packit Service a1bd4f
    return 1;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
genlmsg_parse(struct nlmsghdr *        nlh,
Packit Service a1bd4f
              int                      hdrlen,
Packit Service a1bd4f
              struct nlattr *          tb[],
Packit Service a1bd4f
              int                      maxtype,
Packit Service a1bd4f
              const struct nla_policy *policy)
Packit 5756e2
{
Packit Service a1bd4f
    struct genlmsghdr *ghdr;
Packit 5756e2
Packit Service a1bd4f
    if (!genlmsg_valid_hdr(nlh, hdrlen))
Packit Service a1bd4f
        return -NME_NL_MSG_TOOSHORT;
Packit 5756e2
Packit Service a1bd4f
    ghdr = nlmsg_data(nlh);
Packit Service a1bd4f
    return nla_parse(tb,
Packit Service a1bd4f
                     maxtype,
Packit Service a1bd4f
                     genlmsg_attrdata(ghdr, hdrlen),
Packit Service a1bd4f
                     genlmsg_attrlen(ghdr, hdrlen),
Packit Service a1bd4f
                     policy);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static int
Packit Service a1bd4f
_genl_parse_getfamily(struct nl_msg *msg, void *arg)
Packit 5756e2
{
Packit Service a1bd4f
    static const struct nla_policy ctrl_policy[] = {
Packit Service a1bd4f
        [CTRL_ATTR_FAMILY_ID]    = {.type = NLA_U16},
Packit Service a1bd4f
        [CTRL_ATTR_FAMILY_NAME]  = {.type = NLA_STRING, .maxlen = GENL_NAMSIZ},
Packit Service a1bd4f
        [CTRL_ATTR_VERSION]      = {.type = NLA_U32},
Packit Service a1bd4f
        [CTRL_ATTR_HDRSIZE]      = {.type = NLA_U32},
Packit Service a1bd4f
        [CTRL_ATTR_MAXATTR]      = {.type = NLA_U32},
Packit Service a1bd4f
        [CTRL_ATTR_OPS]          = {.type = NLA_NESTED},
Packit Service a1bd4f
        [CTRL_ATTR_MCAST_GROUPS] = {.type = NLA_NESTED},
Packit Service a1bd4f
    };
Packit Service a1bd4f
    struct nlattr *  tb[G_N_ELEMENTS(ctrl_policy)];
Packit Service a1bd4f
    struct nlmsghdr *nlh           = nlmsg_hdr(msg);
Packit Service a1bd4f
    gint32 *         response_data = arg;
Packit Service a1bd4f
Packit Service a1bd4f
    if (genlmsg_parse_arr(nlh, 0, tb, ctrl_policy) < 0)
Packit Service a1bd4f
        return NL_SKIP;
Packit Service a1bd4f
Packit Service a1bd4f
    if (tb[CTRL_ATTR_FAMILY_ID])
Packit Service a1bd4f
        *response_data = nla_get_u16(tb[CTRL_ATTR_FAMILY_ID]);
Packit Service a1bd4f
Packit Service a1bd4f
    return NL_STOP;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
genl_ctrl_resolve(struct nl_sock *sk, const char *name)
Packit 5756e2
{
Packit Service a1bd4f
    nm_auto_nlmsg struct nl_msg *msg = NULL;
Packit Service a1bd4f
    int                          nmerr;
Packit Service a1bd4f
    gint32                       response_data = -1;
Packit Service a1bd4f
    const struct nl_cb           cb            = {
Packit Service a1bd4f
        .valid_cb  = _genl_parse_getfamily,
Packit Service a1bd4f
        .valid_arg = &response_data,
Packit Service a1bd4f
    };
Packit 5756e2
Packit Service a1bd4f
    msg = nlmsg_alloc();
Packit 5756e2
Packit Service a1bd4f
    if (!genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, GENL_ID_CTRL, 0, 0, CTRL_CMD_GETFAMILY, 1))
Packit Service a1bd4f
        return -ENOMEM;
Packit 5756e2
Packit Service a1bd4f
    nmerr = nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, name);
Packit Service a1bd4f
    if (nmerr < 0)
Packit Service a1bd4f
        return nmerr;
Packit 5756e2
Packit Service a1bd4f
    nmerr = nl_send_auto(sk, msg);
Packit Service a1bd4f
    if (nmerr < 0)
Packit Service a1bd4f
        return nmerr;
Packit 5756e2
Packit Service a1bd4f
    nmerr = nl_recvmsgs(sk, &cb;;
Packit Service a1bd4f
    if (nmerr < 0)
Packit Service a1bd4f
        return nmerr;
Packit 5756e2
Packit Service a1bd4f
    /* If search was successful, request may be ACKed after data */
Packit Service a1bd4f
    nmerr = nl_wait_for_ack(sk, NULL);
Packit Service a1bd4f
    if (nmerr < 0)
Packit Service a1bd4f
        return nmerr;
Packit 5756e2
Packit Service a1bd4f
    if (response_data < 0)
Packit Service a1bd4f
        return -NME_UNSPEC;
Packit 5756e2
Packit Service a1bd4f
    return response_data;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
struct nl_sock *
Packit Service a1bd4f
nl_socket_alloc(void)
Packit 5756e2
{
Packit Service a1bd4f
    struct nl_sock *sk;
Packit 5756e2
Packit Service a1bd4f
    sk = g_slice_new0(struct nl_sock);
Packit 5756e2
Packit Service a1bd4f
    sk->s_fd              = -1;
Packit Service a1bd4f
    sk->s_local.nl_family = AF_NETLINK;
Packit Service a1bd4f
    sk->s_peer.nl_family  = AF_NETLINK;
Packit Service a1bd4f
    sk->s_seq_expect = sk->s_seq_next = time(NULL);
Packit 5756e2
Packit Service a1bd4f
    return sk;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
void
Packit Service a1bd4f
nl_socket_free(struct nl_sock *sk)
Packit 5756e2
{
Packit Service a1bd4f
    if (!sk)
Packit Service a1bd4f
        return;
Packit 5756e2
Packit Service a1bd4f
    if (sk->s_fd >= 0)
Packit Service a1bd4f
        nm_close(sk->s_fd);
Packit Service a1bd4f
    g_slice_free(struct nl_sock, sk);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nl_socket_get_fd(const struct nl_sock *sk)
Packit 5756e2
{
Packit Service a1bd4f
    return sk->s_fd;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
uint32_t
Packit Service a1bd4f
nl_socket_get_local_port(const struct nl_sock *sk)
Packit 5756e2
{
Packit Service a1bd4f
    return sk->s_local.nl_pid;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
size_t
Packit Service a1bd4f
nl_socket_get_msg_buf_size(struct nl_sock *sk)
Packit 5756e2
{
Packit Service a1bd4f
    return sk->s_bufsize;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nl_socket_set_passcred(struct nl_sock *sk, int state)
Packit 5756e2
{
Packit Service a1bd4f
    int err;
Packit 5756e2
Packit Service a1bd4f
    if (sk->s_fd == -1)
Packit Service a1bd4f
        return -NME_NL_BAD_SOCK;
Packit 5756e2
Packit Service a1bd4f
    err = setsockopt(sk->s_fd, SOL_SOCKET, SO_PASSCRED, &state, sizeof(state));
Packit Service a1bd4f
    if (err < 0)
Packit Service a1bd4f
        return -nm_errno_from_native(errno);
Packit 5756e2
Packit Service a1bd4f
    if (state)
Packit Service a1bd4f
        sk->s_flags |= NL_SOCK_PASSCRED;
Packit Service a1bd4f
    else
Packit Service a1bd4f
        sk->s_flags &= ~NL_SOCK_PASSCRED;
Packit 5756e2
Packit Service a1bd4f
    return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nl_socket_set_msg_buf_size(struct nl_sock *sk, size_t bufsize)
Packit 5756e2
{
Packit Service a1bd4f
    sk->s_bufsize = bufsize;
Packit 5756e2
Packit Service a1bd4f
    return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
struct sockaddr_nl *
Packit Service a1bd4f
nlmsg_get_dst(struct nl_msg *msg)
Packit 5756e2
{
Packit Service a1bd4f
    return &msg->nm_dst;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nl_socket_set_nonblocking(const struct nl_sock *sk)
Packit 5756e2
{
Packit Service a1bd4f
    if (sk->s_fd == -1)
Packit Service a1bd4f
        return -NME_NL_BAD_SOCK;
Packit 5756e2
Packit Service a1bd4f
    if (fcntl(sk->s_fd, F_SETFL, O_NONBLOCK) < 0)
Packit Service a1bd4f
        return -nm_errno_from_native(errno);
Packit 5756e2
Packit Service a1bd4f
    return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nl_socket_set_buffer_size(struct nl_sock *sk, int rxbuf, int txbuf)
Packit 5756e2
{
Packit Service a1bd4f
    int err;
Packit 5756e2
Packit Service a1bd4f
    if (rxbuf <= 0)
Packit Service a1bd4f
        rxbuf = 32768;
Packit 5756e2
Packit Service a1bd4f
    if (txbuf <= 0)
Packit Service a1bd4f
        txbuf = 32768;
Packit 5756e2
Packit Service a1bd4f
    if (sk->s_fd == -1)
Packit Service a1bd4f
        return -NME_NL_BAD_SOCK;
Packit 5756e2
Packit Service a1bd4f
    err = setsockopt(sk->s_fd, SOL_SOCKET, SO_SNDBUF, &txbuf, sizeof(txbuf));
Packit Service a1bd4f
    if (err < 0) {
Packit Service a1bd4f
        return -nm_errno_from_native(errno);
Packit Service a1bd4f
    }
Packit 5756e2
Packit Service a1bd4f
    err = setsockopt(sk->s_fd, SOL_SOCKET, SO_RCVBUF, &rxbuf, sizeof(rxbuf));
Packit Service a1bd4f
    if (err < 0) {
Packit Service a1bd4f
        return -nm_errno_from_native(errno);
Packit Service a1bd4f
    }
Packit 5756e2
Packit Service a1bd4f
    return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nl_socket_add_memberships(struct nl_sock *sk, int group, ...)
Packit 5756e2
{
Packit Service a1bd4f
    int     err;
Packit Service a1bd4f
    va_list ap;
Packit 5756e2
Packit Service a1bd4f
    if (sk->s_fd == -1)
Packit Service a1bd4f
        return -NME_NL_BAD_SOCK;
Packit 5756e2
Packit Service a1bd4f
    va_start(ap, group);
Packit 5756e2
Packit Service a1bd4f
    while (group != 0) {
Packit Service a1bd4f
        if (group < 0) {
Packit Service a1bd4f
            va_end(ap);
Packit Service a1bd4f
            g_return_val_if_reached(-NME_BUG);
Packit Service a1bd4f
        }
Packit 5756e2
Packit Service a1bd4f
        err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group));
Packit Service a1bd4f
        if (err < 0) {
Packit Service a1bd4f
            int errsv = errno;
Packit 5756e2
Packit Service a1bd4f
            va_end(ap);
Packit Service a1bd4f
            return -nm_errno_from_native(errsv);
Packit Service a1bd4f
        }
Packit 5756e2
Packit Service a1bd4f
        group = va_arg(ap, int);
Packit Service a1bd4f
    }
Packit 5756e2
Packit Service a1bd4f
    va_end(ap);
Packit 5756e2
Packit Service a1bd4f
    return 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nl_socket_set_ext_ack(struct nl_sock *sk, gboolean enable)
Packit 5756e2
{
Packit Service a1bd4f
    int err, val;
Packit 5756e2
Packit Service a1bd4f
    if (sk->s_fd == -1)
Packit Service a1bd4f
        return -NME_NL_BAD_SOCK;
Packit 5756e2
Packit Service a1bd4f
    val = !!enable;
Packit Service a1bd4f
    err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_EXT_ACK, &val, sizeof(val));
Packit Service a1bd4f
    if (err < 0)
Packit Service a1bd4f
        return -nm_errno_from_native(errno);
Packit 5756e2
Packit Service a1bd4f
    return 0;
Packit 5756e2
}
Packit 5756e2
Packit Service a1bd4f
void
Packit Service a1bd4f
nl_socket_disable_msg_peek(struct nl_sock *sk)
Packit 5756e2
{
Packit Service a1bd4f
    sk->s_flags |= NL_MSG_PEEK_EXPLICIT;
Packit Service a1bd4f
    sk->s_flags &= ~NL_MSG_PEEK;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nl_connect(struct nl_sock *sk, int protocol)
Packit 5756e2
{
Packit Service a1bd4f
    int                err, nmerr;
Packit Service a1bd4f
    socklen_t          addrlen;
Packit Service a1bd4f
    struct sockaddr_nl local = {0};
Packit Service a1bd4f
Packit Service a1bd4f
    if (sk->s_fd != -1)
Packit Service a1bd4f
        return -NME_NL_BAD_SOCK;
Packit Service a1bd4f
Packit Service a1bd4f
    sk->s_fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol);
Packit Service a1bd4f
    if (sk->s_fd < 0) {
Packit Service a1bd4f
        nmerr = -nm_errno_from_native(errno);
Packit Service a1bd4f
        goto errout;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    nmerr = nl_socket_set_buffer_size(sk, 0, 0);
Packit Service a1bd4f
    if (nmerr < 0)
Packit Service a1bd4f
        goto errout;
Packit Service a1bd4f
Packit Service a1bd4f
    nm_assert(sk->s_local.nl_pid == 0);
Packit Service a1bd4f
Packit Service a1bd4f
    err = bind(sk->s_fd, (struct sockaddr *) &sk->s_local, sizeof(sk->s_local));
Packit Service a1bd4f
    if (err != 0) {
Packit Service a1bd4f
        nmerr = -nm_errno_from_native(errno);
Packit Service a1bd4f
        goto errout;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    addrlen = sizeof(local);
Packit Service a1bd4f
    err     = getsockname(sk->s_fd, (struct sockaddr *) &local, &addrlen);
Packit Service a1bd4f
    if (err < 0) {
Packit Service a1bd4f
        nmerr = -nm_errno_from_native(errno);
Packit Service a1bd4f
        goto errout;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (addrlen != sizeof(local)) {
Packit Service a1bd4f
        nmerr = -NME_UNSPEC;
Packit Service a1bd4f
        goto errout;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (local.nl_family != AF_NETLINK) {
Packit Service a1bd4f
        nmerr = -NME_UNSPEC;
Packit Service a1bd4f
        goto errout;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    sk->s_local = local;
Packit Service a1bd4f
    sk->s_proto = protocol;
Packit Service a1bd4f
Packit Service a1bd4f
    return 0;
Packit 5756e2
Packit 5756e2
errout:
Packit Service a1bd4f
    if (sk->s_fd != -1) {
Packit Service a1bd4f
        close(sk->s_fd);
Packit Service a1bd4f
        sk->s_fd = -1;
Packit Service a1bd4f
    }
Packit Service a1bd4f
    return nmerr;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
_cb_init(struct nl_cb *dst, const struct nl_cb *src)
Packit 5756e2
{
Packit Service a1bd4f
    nm_assert(dst);
Packit 5756e2
Packit Service a1bd4f
    if (src)
Packit Service a1bd4f
        *dst = *src;
Packit Service a1bd4f
    else
Packit Service a1bd4f
        memset(dst, 0, sizeof(*dst));
Packit 5756e2
}
Packit 5756e2
Packit Service a1bd4f
static int
Packit Service a1bd4f
ack_wait_handler(struct nl_msg *msg, void *arg)
Packit 5756e2
{
Packit Service a1bd4f
    return NL_STOP;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nl_wait_for_ack(struct nl_sock *sk, const struct nl_cb *cb)
Packit 5756e2
{
Packit Service a1bd4f
    struct nl_cb cb2;
Packit 5756e2
Packit Service a1bd4f
    _cb_init(&cb2, cb);
Packit Service a1bd4f
    cb2.ack_cb = ack_wait_handler;
Packit Service a1bd4f
    return nl_recvmsgs(sk, &cb2);
Packit 5756e2
}
Packit 5756e2
Packit Service a1bd4f
#define NL_CB_CALL(cb, type, msg)                                \
Packit Service a1bd4f
    do {                                                         \
Packit Service a1bd4f
        const struct nl_cb *_cb = (cb);                          \
Packit Service a1bd4f
                                                                 \
Packit Service a1bd4f
        if (_cb && _cb->type##_cb) {                             \
Packit Service a1bd4f
            /* the returned value here must be either a negative
Packit Service a1bd4f
         * netlink error number, or one of NL_SKIP, NL_STOP, NL_OK. */ \
Packit Service a1bd4f
            nmerr = _cb->type##_cb((msg), _cb->type##_arg);      \
Packit Service a1bd4f
            switch (nmerr) {                                     \
Packit Service a1bd4f
            case NL_OK:                                          \
Packit Service a1bd4f
                nm_assert(nmerr == 0);                           \
Packit Service a1bd4f
                break;                                           \
Packit Service a1bd4f
            case NL_SKIP:                                        \
Packit Service a1bd4f
                goto skip;                                       \
Packit Service a1bd4f
            case NL_STOP:                                        \
Packit Service a1bd4f
                goto stop;                                       \
Packit Service a1bd4f
            default:                                             \
Packit Service a1bd4f
                if (nmerr >= 0) {                                \
Packit Service a1bd4f
                    nm_assert_not_reached();                     \
Packit Service a1bd4f
                    nmerr = -NME_BUG;                            \
Packit Service a1bd4f
                }                                                \
Packit Service a1bd4f
                goto out;                                        \
Packit Service a1bd4f
            }                                                    \
Packit Service a1bd4f
        }                                                        \
Packit Service a1bd4f
    } while (0)
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nl_recvmsgs(struct nl_sock *sk, const struct nl_cb *cb)
Packit 5756e2
{
Packit Service a1bd4f
    int                    n, nmerr = 0, multipart = 0, interrupted = 0, nrecv = 0;
Packit Service a1bd4f
    gs_free unsigned char *buf = NULL;
Packit Service a1bd4f
    struct nlmsghdr *      hdr;
Packit Service a1bd4f
    struct sockaddr_nl     nla = {0};
Packit Service a1bd4f
    struct ucred           creds;
Packit Service a1bd4f
    gboolean               creds_has;
Packit 5756e2
Packit 5756e2
continue_reading:
Packit Service a1bd4f
    n = nl_recv(sk, &nla, &buf, &creds, &creds_has);
Packit Service a1bd4f
    if (n <= 0)
Packit Service a1bd4f
        return n;
Packit Service a1bd4f
Packit Service a1bd4f
    hdr = (struct nlmsghdr *) buf;
Packit Service a1bd4f
    while (nlmsg_ok(hdr, n)) {
Packit Service a1bd4f
        nm_auto_nlmsg struct nl_msg *msg = NULL;
Packit Service a1bd4f
Packit Service a1bd4f
        msg = nlmsg_alloc_convert(hdr);
Packit Service a1bd4f
Packit Service a1bd4f
        nlmsg_set_proto(msg, sk->s_proto);
Packit Service a1bd4f
        nlmsg_set_src(msg, &nla);
Packit Service a1bd4f
        nlmsg_set_creds(msg, creds_has ? &creds : NULL);
Packit Service a1bd4f
Packit Service a1bd4f
        nrecv++;
Packit Service a1bd4f
Packit Service a1bd4f
        /* Only do sequence checking if auto-ack mode is enabled */
Packit Service a1bd4f
        if (!(sk->s_flags & NL_NO_AUTO_ACK)) {
Packit Service a1bd4f
            if (hdr->nlmsg_seq != sk->s_seq_expect) {
Packit Service a1bd4f
                nmerr = -NME_NL_SEQ_MISMATCH;
Packit Service a1bd4f
                goto out;
Packit Service a1bd4f
            }
Packit Service a1bd4f
        }
Packit Service a1bd4f
Packit Service a1bd4f
        if (hdr->nlmsg_type == NLMSG_DONE || hdr->nlmsg_type == NLMSG_ERROR
Packit Service a1bd4f
            || hdr->nlmsg_type == NLMSG_NOOP || hdr->nlmsg_type == NLMSG_OVERRUN) {
Packit Service a1bd4f
            /* We can't check for !NLM_F_MULTI since some netlink
Packit Service a1bd4f
             * users in the kernel are broken. */
Packit Service a1bd4f
            sk->s_seq_expect++;
Packit Service a1bd4f
        }
Packit Service a1bd4f
Packit Service a1bd4f
        if (hdr->nlmsg_flags & NLM_F_MULTI)
Packit Service a1bd4f
            multipart = 1;
Packit Service a1bd4f
Packit Service a1bd4f
        if (hdr->nlmsg_flags & NLM_F_DUMP_INTR) {
Packit Service a1bd4f
            /*
Packit Service a1bd4f
             * We have to continue reading to clear
Packit Service a1bd4f
             * all messages until a NLMSG_DONE is
Packit Service a1bd4f
             * received and report the inconsistency.
Packit Service a1bd4f
             */
Packit Service a1bd4f
            interrupted = 1;
Packit Service a1bd4f
        }
Packit Service a1bd4f
Packit Service a1bd4f
        /* messages terminates a multipart message, this is
Packit Service a1bd4f
         * usually the end of a message and therefore we slip
Packit Service a1bd4f
         * out of the loop by default. the user may overrule
Packit Service a1bd4f
         * this action by skipping this packet. */
Packit Service a1bd4f
        if (hdr->nlmsg_type == NLMSG_DONE) {
Packit Service a1bd4f
            multipart = 0;
Packit Service a1bd4f
            NL_CB_CALL(cb, finish, msg);
Packit Service a1bd4f
        }
Packit Service a1bd4f
Packit Service a1bd4f
        /* Message to be ignored, the default action is to
Packit Service a1bd4f
         * skip this message if no callback is specified. The
Packit Service a1bd4f
         * user may overrule this action by returning
Packit Service a1bd4f
         * NL_PROCEED. */
Packit Service a1bd4f
        else if (hdr->nlmsg_type == NLMSG_NOOP)
Packit Service a1bd4f
            goto skip;
Packit Service a1bd4f
Packit Service a1bd4f
        /* Data got lost, report back to user. The default action is to
Packit Service a1bd4f
         * quit parsing. The user may overrule this action by returning
Packit Service a1bd4f
         * NL_SKIP or NL_PROCEED (dangerous) */
Packit Service a1bd4f
        else if (hdr->nlmsg_type == NLMSG_OVERRUN) {
Packit Service a1bd4f
            nmerr = -NME_NL_MSG_OVERFLOW;
Packit Service a1bd4f
            goto out;
Packit Service a1bd4f
        }
Packit Service a1bd4f
Packit Service a1bd4f
        /* Message carries a nlmsgerr */
Packit Service a1bd4f
        else if (hdr->nlmsg_type == NLMSG_ERROR) {
Packit Service a1bd4f
            struct nlmsgerr *e = nlmsg_data(hdr);
Packit Service a1bd4f
Packit Service a1bd4f
            if (hdr->nlmsg_len < nlmsg_size(sizeof(*e))) {
Packit Service a1bd4f
                /* Truncated error message, the default action
Packit Service a1bd4f
                 * is to stop parsing. The user may overrule
Packit Service a1bd4f
                 * this action by returning NL_SKIP or
Packit Service a1bd4f
                 * NL_PROCEED (dangerous) */
Packit Service a1bd4f
                nmerr = -NME_NL_MSG_TRUNC;
Packit Service a1bd4f
                goto out;
Packit Service a1bd4f
            }
Packit Service a1bd4f
            if (e->error) {
Packit Service a1bd4f
                /* Error message reported back from kernel. */
Packit Service a1bd4f
                if (cb && cb->err_cb) {
Packit Service a1bd4f
                    /* the returned value here must be either a negative
Packit Service a1bd4f
                     * netlink error number, or one of NL_SKIP, NL_STOP, NL_OK. */
Packit Service a1bd4f
                    nmerr = cb->err_cb(&nla, e, cb->err_arg);
Packit Service a1bd4f
                    if (nmerr < 0)
Packit Service a1bd4f
                        goto out;
Packit Service a1bd4f
                    else if (nmerr == NL_SKIP)
Packit Service a1bd4f
                        goto skip;
Packit Service a1bd4f
                    else if (nmerr == NL_STOP) {
Packit Service a1bd4f
                        nmerr = -nm_errno_from_native(e->error);
Packit Service a1bd4f
                        goto out;
Packit Service a1bd4f
                    }
Packit Service a1bd4f
                    nm_assert(nmerr == NL_OK);
Packit Service a1bd4f
                } else {
Packit Service a1bd4f
                    nmerr = -nm_errno_from_native(e->error);
Packit Service a1bd4f
                    goto out;
Packit Service a1bd4f
                }
Packit Service a1bd4f
            } else
Packit Service a1bd4f
                NL_CB_CALL(cb, ack, msg);
Packit Service a1bd4f
        } else {
Packit Service a1bd4f
            /* Valid message (not checking for MULTIPART bit to
Packit Service a1bd4f
             * get along with broken kernels. NL_SKIP has no
Packit Service a1bd4f
             * effect on this.  */
Packit Service a1bd4f
            NL_CB_CALL(cb, valid, msg);
Packit Service a1bd4f
        }
Packit 5756e2
skip:
Packit Service a1bd4f
        nmerr = 0;
Packit Service a1bd4f
        hdr   = nlmsg_next(hdr, &n);
Packit Service a1bd4f
    }
Packit 5756e2
Packit Service a1bd4f
    if (multipart) {
Packit Service a1bd4f
        /* Multipart message not yet complete, continue reading */
Packit Service a1bd4f
        nm_clear_g_free(&buf;;
Packit 5756e2
Packit Service a1bd4f
        nmerr = 0;
Packit Service a1bd4f
        goto continue_reading;
Packit Service a1bd4f
    }
Packit 5756e2
Packit 5756e2
stop:
Packit Service a1bd4f
    nmerr = 0;
Packit 5756e2
Packit 5756e2
out:
Packit Service a1bd4f
    if (interrupted)
Packit Service a1bd4f
        nmerr = -NME_NL_DUMP_INTR;
Packit 5756e2
Packit Service a1bd4f
    nm_assert(nmerr <= 0);
Packit Service a1bd4f
    return nmerr ?: nrecv;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nl_sendmsg(struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr)
Packit 5756e2
{
Packit Service a1bd4f
    int ret;
Packit 5756e2
Packit Service a1bd4f
    if (sk->s_fd < 0)
Packit Service a1bd4f
        return -NME_NL_BAD_SOCK;
Packit 5756e2
Packit Service a1bd4f
    nlmsg_set_src(msg, &sk->s_local);
Packit 5756e2
Packit Service a1bd4f
    ret = sendmsg(sk->s_fd, hdr, 0);
Packit Service a1bd4f
    if (ret < 0)
Packit Service a1bd4f
        return -nm_errno_from_native(errno);
Packit 5756e2
Packit Service a1bd4f
    return ret;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nl_send_iovec(struct nl_sock *sk, struct nl_msg *msg, struct iovec *iov, unsigned iovlen)
Packit 5756e2
{
Packit Service a1bd4f
    struct sockaddr_nl *dst;
Packit Service a1bd4f
    struct ucred *      creds;
Packit Service a1bd4f
    struct msghdr       hdr = {
Packit Service a1bd4f
        .msg_name    = (void *) &sk->s_peer,
Packit Service a1bd4f
        .msg_namelen = sizeof(struct sockaddr_nl),
Packit Service a1bd4f
        .msg_iov     = iov,
Packit Service a1bd4f
        .msg_iovlen  = iovlen,
Packit Service a1bd4f
    };
Packit Service a1bd4f
    char buf[CMSG_SPACE(sizeof(struct ucred))];
Packit Service a1bd4f
Packit Service a1bd4f
    /* Overwrite destination if specified in the message itself, defaults
Packit Service a1bd4f
     * to the peer address of the socket.
Packit Service a1bd4f
     */
Packit Service a1bd4f
    dst = nlmsg_get_dst(msg);
Packit Service a1bd4f
    if (dst->nl_family == AF_NETLINK)
Packit Service a1bd4f
        hdr.msg_name = dst;
Packit Service a1bd4f
Packit Service a1bd4f
    /* Add credentials if present. */
Packit Service a1bd4f
    creds = nlmsg_get_creds(msg);
Packit Service a1bd4f
    if (creds != NULL) {
Packit Service a1bd4f
        struct cmsghdr *cmsg;
Packit Service a1bd4f
Packit Service a1bd4f
        hdr.msg_control    = buf;
Packit Service a1bd4f
        hdr.msg_controllen = sizeof(buf);
Packit Service a1bd4f
Packit Service a1bd4f
        cmsg             = CMSG_FIRSTHDR(&hdr);
Packit Service a1bd4f
        cmsg->cmsg_level = SOL_SOCKET;
Packit Service a1bd4f
        cmsg->cmsg_type  = SCM_CREDENTIALS;
Packit Service a1bd4f
        cmsg->cmsg_len   = CMSG_LEN(sizeof(struct ucred));
Packit Service a1bd4f
        memcpy(CMSG_DATA(cmsg), creds, sizeof(struct ucred));
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    return nl_sendmsg(sk, msg, &hdr);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
void
Packit Service a1bd4f
nl_complete_msg(struct nl_sock *sk, struct nl_msg *msg)
Packit 5756e2
{
Packit Service a1bd4f
    struct nlmsghdr *nlh;
Packit 5756e2
Packit Service a1bd4f
    nlh = nlmsg_hdr(msg);
Packit Service a1bd4f
    if (nlh->nlmsg_pid == NL_AUTO_PORT)
Packit Service a1bd4f
        nlh->nlmsg_pid = nl_socket_get_local_port(sk);
Packit 5756e2
Packit Service a1bd4f
    if (nlh->nlmsg_seq == NL_AUTO_SEQ)
Packit Service a1bd4f
        nlh->nlmsg_seq = sk->s_seq_next++;
Packit 5756e2
Packit Service a1bd4f
    if (msg->nm_protocol == -1)
Packit Service a1bd4f
        msg->nm_protocol = sk->s_proto;
Packit 5756e2
Packit Service a1bd4f
    nlh->nlmsg_flags |= NLM_F_REQUEST;
Packit 5756e2
Packit Service a1bd4f
    if (!(sk->s_flags & NL_NO_AUTO_ACK))
Packit Service a1bd4f
        nlh->nlmsg_flags |= NLM_F_ACK;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nl_send(struct nl_sock *sk, struct nl_msg *msg)
Packit 5756e2
{
Packit Service a1bd4f
    struct iovec iov = {
Packit Service a1bd4f
        .iov_base = (void *) nlmsg_hdr(msg),
Packit Service a1bd4f
        .iov_len  = nlmsg_hdr(msg)->nlmsg_len,
Packit Service a1bd4f
    };
Packit 5756e2
Packit Service a1bd4f
    return nl_send_iovec(sk, msg, &iov, 1);
Packit 5756e2
}
Packit 5756e2
Packit Service a1bd4f
int
Packit Service a1bd4f
nl_send_auto(struct nl_sock *sk, struct nl_msg *msg)
Packit 5756e2
{
Packit Service a1bd4f
    nl_complete_msg(sk, msg);
Packit 5756e2
Packit Service a1bd4f
    return nl_send(sk, msg);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
nl_recv(struct nl_sock *    sk,
Packit Service a1bd4f
        struct sockaddr_nl *nla,
Packit Service a1bd4f
        unsigned char **    buf,
Packit Service a1bd4f
        struct ucred *      out_creds,
Packit Service a1bd4f
        gboolean *          out_creds_has)
Packit 5756e2
{
Packit Service a1bd4f
    ssize_t       n;
Packit Service a1bd4f
    int           flags = 0;
Packit Service a1bd4f
    struct iovec  iov;
Packit Service a1bd4f
    struct msghdr msg = {
Packit Service a1bd4f
        .msg_name    = (void *) nla,
Packit Service a1bd4f
        .msg_namelen = sizeof(struct sockaddr_nl),
Packit Service a1bd4f
        .msg_iov     = &iov,
Packit Service a1bd4f
        .msg_iovlen  = 1,
Packit Service a1bd4f
    };
Packit Service a1bd4f
    struct ucred tmpcreds;
Packit Service a1bd4f
    gboolean     tmpcreds_has = FALSE;
Packit Service a1bd4f
    int          retval;
Packit Service a1bd4f
    int          errsv;
Packit Service a1bd4f
Packit Service a1bd4f
    nm_assert(nla);
Packit Service a1bd4f
    nm_assert(buf && !*buf);
Packit Service a1bd4f
    nm_assert(!out_creds_has == !out_creds);
Packit Service a1bd4f
Packit Service a1bd4f
    if ((sk->s_flags & NL_MSG_PEEK)
Packit Service a1bd4f
        || (!(sk->s_flags & NL_MSG_PEEK_EXPLICIT) && sk->s_bufsize == 0))
Packit Service a1bd4f
        flags |= MSG_PEEK | MSG_TRUNC;
Packit Service a1bd4f
Packit Service a1bd4f
    iov.iov_len  = sk->s_bufsize ?: (((size_t) nm_utils_getpagesize()) * 4u);
Packit Service a1bd4f
    iov.iov_base = g_malloc(iov.iov_len);
Packit Service a1bd4f
Packit Service a1bd4f
    if (out_creds && (sk->s_flags & NL_SOCK_PASSCRED)) {
Packit Service a1bd4f
        msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
Packit Service a1bd4f
        msg.msg_control    = g_malloc(msg.msg_controllen);
Packit Service a1bd4f
    }
Packit 5756e2
Packit 5756e2
retry:
Packit Service a1bd4f
    n = recvmsg(sk->s_fd, &msg, flags);
Packit Service a1bd4f
    if (!n) {
Packit Service a1bd4f
        retval = 0;
Packit Service a1bd4f
        goto abort;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (n < 0) {
Packit Service a1bd4f
        errsv = errno;
Packit Service a1bd4f
        if (errsv == EINTR)
Packit Service a1bd4f
            goto retry;
Packit Service a1bd4f
        retval = -nm_errno_from_native(errsv);
Packit Service a1bd4f
        goto abort;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (msg.msg_flags & MSG_CTRUNC) {
Packit Service a1bd4f
        if (msg.msg_controllen == 0) {
Packit Service a1bd4f
            retval = -NME_NL_MSG_TRUNC;
Packit Service a1bd4f
            goto abort;
Packit Service a1bd4f
        }
Packit Service a1bd4f
Packit Service a1bd4f
        msg.msg_controllen *= 2;
Packit Service a1bd4f
        msg.msg_control = g_realloc(msg.msg_control, msg.msg_controllen);
Packit Service a1bd4f
        goto retry;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (iov.iov_len < n || (msg.msg_flags & MSG_TRUNC)) {
Packit Service a1bd4f
        /* respond with error to an incomplete message */
Packit Service a1bd4f
        if (flags == 0) {
Packit Service a1bd4f
            retval = -NME_NL_MSG_TRUNC;
Packit Service a1bd4f
            goto abort;
Packit Service a1bd4f
        }
Packit Service a1bd4f
Packit Service a1bd4f
        /* Provided buffer is not long enough, enlarge it
Packit Service a1bd4f
         * to size of n (which should be total length of the message)
Packit Service a1bd4f
         * and try again. */
Packit Service a1bd4f
        iov.iov_base = g_realloc(iov.iov_base, n);
Packit Service a1bd4f
        iov.iov_len  = n;
Packit Service a1bd4f
        flags        = 0;
Packit Service a1bd4f
        goto retry;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (flags != 0) {
Packit Service a1bd4f
        /* Buffer is big enough, do the actual reading */
Packit Service a1bd4f
        flags = 0;
Packit Service a1bd4f
        goto retry;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
Packit Service a1bd4f
        retval = -NME_UNSPEC;
Packit Service a1bd4f
        goto abort;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (out_creds && (sk->s_flags & NL_SOCK_PASSCRED)) {
Packit Service a1bd4f
        struct cmsghdr *cmsg;
Packit Service a1bd4f
Packit Service a1bd4f
        for (cmsg = CMSG_FIRSTHDR(&msg;; cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
Packit Service a1bd4f
            if (cmsg->cmsg_level != SOL_SOCKET)
Packit Service a1bd4f
                continue;
Packit Service a1bd4f
            if (cmsg->cmsg_type != SCM_CREDENTIALS)
Packit Service a1bd4f
                continue;
Packit Service a1bd4f
            memcpy(&tmpcreds, CMSG_DATA(cmsg), sizeof(tmpcreds));
Packit Service a1bd4f
            tmpcreds_has = TRUE;
Packit Service a1bd4f
            break;
Packit Service a1bd4f
        }
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    retval = n;
Packit 5756e2
Packit 5756e2
abort:
Packit Service a1bd4f
    g_free(msg.msg_control);
Packit Service a1bd4f
Packit Service a1bd4f
    if (retval <= 0) {
Packit Service a1bd4f
        g_free(iov.iov_base);
Packit Service a1bd4f
        return retval;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    *buf = iov.iov_base;
Packit Service a1bd4f
    if (out_creds && tmpcreds_has)
Packit Service a1bd4f
        *out_creds = tmpcreds;
Packit Service a1bd4f
    NM_SET_OUT(out_creds_has, tmpcreds_has);
Packit Service a1bd4f
    return retval;
Packit 5756e2
}