Blame src/nm-core-utils.h

Packit Service 87a54e
/* SPDX-License-Identifier: GPL-2.0-or-later */
Packit 5756e2
/*
Packit 5756e2
 * Copyright (C) 2004 - 2016 Red Hat, Inc.
Packit 5756e2
 * Copyright (C) 2005 - 2008 Novell, Inc.
Packit 5756e2
 */
Packit 5756e2
Packit 5756e2
#ifndef __NM_CORE_UTILS_H__
Packit 5756e2
#define __NM_CORE_UTILS_H__
Packit 5756e2
Packit 5756e2
#include <stdio.h>
Packit 5756e2
#include <arpa/inet.h>
Packit 5756e2
Packit 5756e2
#include "nm-connection.h"
Packit 5756e2
Packit 5756e2
#include "nm-glib-aux/nm-time-utils.h"
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
#define NM_PLATFORM_LIFETIME_PERMANENT G_MAXUINT32
Packit 5756e2
Packit Service a1bd4f
#define NM_DEFINE_SINGLETON_INSTANCE(TYPE) static TYPE *singleton_instance
Packit Service a1bd4f
Packit Service a1bd4f
#define NM_DEFINE_SINGLETON_REGISTER(TYPE)                                                      \
Packit Service a1bd4f
    NM_DEFINE_SINGLETON_INSTANCE(TYPE);                                                         \
Packit Service a1bd4f
    static void _singleton_instance_weak_ref_cb(gpointer data, GObject *where_the_object_was)   \
Packit Service a1bd4f
    {                                                                                           \
Packit Service a1bd4f
        nm_log_dbg(LOGD_CORE,                                                                   \
Packit Service a1bd4f
                   "disposing %s singleton (" NM_HASH_OBFUSCATE_PTR_FMT ")",                    \
Packit Service a1bd4f
                   G_STRINGIFY(TYPE),                                                           \
Packit Service a1bd4f
                   NM_HASH_OBFUSCATE_PTR(singleton_instance));                                  \
Packit Service a1bd4f
        singleton_instance = NULL;                                                              \
Packit Service a1bd4f
    }                                                                                           \
Packit Service a1bd4f
    static inline void nm_singleton_instance_register(void)                                     \
Packit Service a1bd4f
    {                                                                                           \
Packit Service a1bd4f
        g_object_weak_ref(G_OBJECT(singleton_instance), _singleton_instance_weak_ref_cb, NULL); \
Packit Service a1bd4f
        _nm_singleton_instance_register_destruction(G_OBJECT(singleton_instance));              \
Packit Service a1bd4f
    }                                                                                           \
Packit Service a1bd4f
    _NM_DUMMY_STRUCT_FOR_TRAILING_SEMICOLON
Packit Service a1bd4f
Packit Service a1bd4f
void _nm_singleton_instance_register_destruction(GObject *instance);
Packit 5756e2
Packit 5756e2
/* By default, the getter will assert that the singleton will be created only once. You can
Packit 5756e2
 * change this by redefining NM_DEFINE_SINGLETON_ALLOW_MULTIPLE. */
Packit 5756e2
#ifndef NM_DEFINE_SINGLETON_ALLOW_MULTIPLE
Packit Service a1bd4f
    #define NM_DEFINE_SINGLETON_ALLOW_MULTIPLE FALSE
Packit 5756e2
#endif
Packit 5756e2
Packit Service a1bd4f
#define NM_DEFINE_SINGLETON_GETTER(TYPE, GETTER, GTYPE, ...)                                \
Packit Service a1bd4f
    NM_DEFINE_SINGLETON_INSTANCE(TYPE);                                                     \
Packit Service a1bd4f
    NM_DEFINE_SINGLETON_REGISTER(TYPE);                                                     \
Packit Service a1bd4f
    static char _already_created_##GETTER = FALSE;                                          \
Packit Service a1bd4f
    TYPE *      GETTER(void)                                                                \
Packit Service a1bd4f
    {                                                                                       \
Packit Service a1bd4f
        if (G_UNLIKELY(!singleton_instance)) {                                              \
Packit Service a1bd4f
            g_assert(!(_already_created_##GETTER) || (NM_DEFINE_SINGLETON_ALLOW_MULTIPLE)); \
Packit Service a1bd4f
            (_already_created_##GETTER) = TRUE;                                             \
Packit Service a1bd4f
            singleton_instance          = (g_object_new(GTYPE, ##__VA_ARGS__, NULL));       \
Packit Service a1bd4f
            g_assert(singleton_instance);                                                   \
Packit Service a1bd4f
            nm_singleton_instance_register();                                               \
Packit Service a1bd4f
            nm_log_dbg(LOGD_CORE,                                                           \
Packit Service a1bd4f
                       "create %s singleton (" NM_HASH_OBFUSCATE_PTR_FMT ")",               \
Packit Service a1bd4f
                       G_STRINGIFY(TYPE),                                                   \
Packit Service a1bd4f
                       NM_HASH_OBFUSCATE_PTR(singleton_instance));                          \
Packit Service a1bd4f
        }                                                                                   \
Packit Service a1bd4f
        return singleton_instance;                                                          \
Packit Service a1bd4f
    }                                                                                       \
Packit Service a1bd4f
    _nm_unused static void _nmtst_##GETTER##_reset(TYPE *instance)                          \
Packit Service a1bd4f
    {                                                                                       \
Packit Service a1bd4f
        /* usually, the singleton can only be created once (and further instantiations
Packit Service a1bd4f
     * are guarded by an assert). For testing, we need to reset the singleton to
Packit Service a1bd4f
     * allow multiple instantiations. */      \
Packit Service a1bd4f
        g_assert(G_IS_OBJECT(instance));                                                    \
Packit Service a1bd4f
        g_assert(instance == singleton_instance);                                           \
Packit Service a1bd4f
        g_assert(_already_created_##GETTER);                                                \
Packit Service a1bd4f
        g_object_unref(instance);                                                           \
Packit Service a1bd4f
                                                                                            \
Packit Service a1bd4f
        /* require that the last unref also destroyed the singleton. If this fails,
Packit Service a1bd4f
     * somebody still keeps a reference. Fix your test! */         \
Packit Service a1bd4f
        g_assert(!singleton_instance);                                                      \
Packit Service a1bd4f
        _already_created_##GETTER = FALSE;                                                  \
Packit Service a1bd4f
    }
Packit 5756e2
Packit 5756e2
/* attach @instance to the data or @owner. @owner owns a reference
Packit 5756e2
 * to @instance thus the lifetime of @instance is at least as long
Packit 5756e2
 * as that of @owner. Use this when @owner depends on @instance. */
Packit Service a1bd4f
#define NM_UTILS_KEEP_ALIVE(owner, instance, unique_token)              \
Packit Service a1bd4f
    G_STMT_START                                                        \
Packit Service a1bd4f
    {                                                                   \
Packit Service a1bd4f
        g_object_set_data_full(G_OBJECT(owner),                         \
Packit Service a1bd4f
                               ".nm-utils-keep-alive-" unique_token "", \
Packit Service a1bd4f
                               g_object_ref(instance),                  \
Packit Service a1bd4f
                               g_object_unref);                         \
Packit Service a1bd4f
    }                                                                   \
Packit Service a1bd4f
    G_STMT_END
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
gboolean nm_ether_addr_is_valid(const NMEtherAddr *addr);
Packit Service a1bd4f
gboolean nm_ether_addr_is_valid_str(const char *str);
Packit 5756e2
Packit Service a1bd4f
gconstpointer
Packit Service a1bd4f
nm_utils_ipx_address_clear_host_address(int family, gpointer dst, gconstpointer src, guint8 plen);
Packit Service a1bd4f
in_addr_t              nm_utils_ip4_address_clear_host_address(in_addr_t addr, guint8 plen);
Packit Service a1bd4f
const struct in6_addr *nm_utils_ip6_address_clear_host_address(struct in6_addr *      dst,
Packit Service a1bd4f
                                                               const struct in6_addr *src,
Packit Service a1bd4f
                                                               guint8                 plen);
Packit 5756e2
Packit 5756e2
static inline int
Packit Service a1bd4f
nm_utils_ip4_address_same_prefix_cmp(in_addr_t addr_a, in_addr_t addr_b, guint8 plen)
Packit 5756e2
{
Packit Service a1bd4f
    NM_CMP_DIRECT(htonl(nm_utils_ip4_address_clear_host_address(addr_a, plen)),
Packit Service a1bd4f
                  htonl(nm_utils_ip4_address_clear_host_address(addr_b, plen)));
Packit Service a1bd4f
    return 0;
Packit 5756e2
}
Packit 5756e2
Packit Service a1bd4f
int nm_utils_ip6_address_same_prefix_cmp(const struct in6_addr *addr_a,
Packit Service a1bd4f
                                         const struct in6_addr *addr_b,
Packit Service a1bd4f
                                         guint8                 plen);
Packit 5756e2
Packit 5756e2
static inline int
Packit Service a1bd4f
nm_utils_ip_address_same_prefix_cmp(int           addr_family,
Packit Service a1bd4f
                                    gconstpointer addr_a,
Packit Service a1bd4f
                                    gconstpointer addr_b,
Packit Service a1bd4f
                                    guint8        plen)
Packit 5756e2
{
Packit Service a1bd4f
    nm_assert_addr_family(addr_family);
Packit 5756e2
Packit Service a1bd4f
    NM_CMP_SELF(addr_a, addr_b);
Packit 5756e2
Packit Service a1bd4f
    if (NM_IS_IPv4(addr_family)) {
Packit Service a1bd4f
        return nm_utils_ip4_address_same_prefix_cmp(*((const in_addr_t *) addr_a),
Packit Service a1bd4f
                                                    *((const in_addr_t *) addr_b),
Packit Service a1bd4f
                                                    plen);
Packit Service a1bd4f
    }
Packit 5756e2
Packit Service a1bd4f
    return nm_utils_ip6_address_same_prefix_cmp(addr_a, addr_b, plen);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static inline gboolean
Packit Service a1bd4f
nm_utils_ip4_address_same_prefix(in_addr_t addr_a, in_addr_t addr_b, guint8 plen)
Packit 5756e2
{
Packit Service a1bd4f
    return nm_utils_ip4_address_same_prefix_cmp(addr_a, addr_b, plen) == 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static inline gboolean
Packit Service a1bd4f
nm_utils_ip6_address_same_prefix(const struct in6_addr *addr_a,
Packit Service a1bd4f
                                 const struct in6_addr *addr_b,
Packit Service a1bd4f
                                 guint8                 plen)
Packit 5756e2
{
Packit Service a1bd4f
    return nm_utils_ip6_address_same_prefix_cmp(addr_a, addr_b, plen) == 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static inline gboolean
Packit Service a1bd4f
nm_utils_ip_address_same_prefix(int           addr_family,
Packit Service a1bd4f
                                gconstpointer addr_a,
Packit Service a1bd4f
                                gconstpointer addr_b,
Packit Service a1bd4f
                                guint8        plen)
Packit 5756e2
{
Packit Service a1bd4f
    return nm_utils_ip_address_same_prefix_cmp(addr_family, addr_a, addr_b, plen) == 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
#define NM_CMP_DIRECT_IN4ADDR_SAME_PREFIX(a, b, plen) \
Packit Service a1bd4f
    NM_CMP_RETURN(nm_utils_ip4_address_same_prefix_cmp((a), (b), (plen)))
Packit 5756e2
Packit 5756e2
#define NM_CMP_DIRECT_IN6ADDR_SAME_PREFIX(a, b, plen) \
Packit Service a1bd4f
    NM_CMP_RETURN(nm_utils_ip6_address_same_prefix_cmp((a), (b), (plen)))
Packit 5756e2
Packit 5756e2
static inline void
Packit Service a1bd4f
nm_hash_update_in6addr(NMHashState *h, const struct in6_addr *addr)
Packit 5756e2
{
Packit Service a1bd4f
    nm_assert(addr);
Packit 5756e2
Packit Service a1bd4f
    nm_hash_update(h, addr, sizeof(*addr));
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static inline void
Packit Service a1bd4f
nm_hash_update_in6addr_prefix(NMHashState *h, const struct in6_addr *addr, guint8 plen)
Packit 5756e2
{
Packit Service a1bd4f
    struct in6_addr a;
Packit 5756e2
Packit Service a1bd4f
    nm_assert(addr);
Packit 5756e2
Packit Service a1bd4f
    nm_utils_ip6_address_clear_host_address(&a, addr, plen);
Packit Service a1bd4f
    /* we don't hash plen itself. The caller may want to do that.*/
Packit Service a1bd4f
    nm_hash_update_in6addr(h, &a);
Packit 5756e2
}
Packit 5756e2
Packit Service a1bd4f
double nm_utils_exp10(gint16 e);
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nm_utils_ip6_route_metric_normalize:
Packit 5756e2
 * @metric: the route metric
Packit 5756e2
 *
Packit 5756e2
 * For IPv6 route, when adding a route via netlink, kernel treats the value 0 as IP6_RT_PRIO_USER (1024).
Packit 5756e2
 * So, user space cannot add routes with such a metric, and 0 gets "normalized"
Packit 5756e2
 * to NM_PLATFORM_ROUTE_METRIC_DEFAULT_IP6.
Packit 5756e2
 *
Packit 5756e2
 * Note that kernel itself can add IPv6 routes with metric zero. Also, you can delete
Packit 5756e2
 * them, but mostly because with `ip -6 route delete ... metric 0` the 0 acts as a wildcard
Packit 5756e2
 * and kills the first matching route.
Packit 5756e2
 *
Packit 5756e2
 * Returns: @metric, if @metric is not zero, otherwise 1024.
Packit 5756e2
 */
Packit 5756e2
static inline guint32
Packit Service a1bd4f
nm_utils_ip6_route_metric_normalize(guint32 metric)
Packit 5756e2
{
Packit Service a1bd4f
    return metric ?: 1024 /*NM_PLATFORM_ROUTE_METRIC_DEFAULT_IP6*/;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static inline guint32
Packit Service a1bd4f
nm_utils_ip_route_metric_normalize(int addr_family, guint32 metric)
Packit 5756e2
{
Packit Service a1bd4f
    return NM_IS_IPv4(addr_family) ? metric : nm_utils_ip6_route_metric_normalize(metric);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static inline guint32
Packit Service a1bd4f
nm_utils_ip_route_metric_penalize(guint32 metric, guint32 penalty)
Packit 5756e2
{
Packit Service a1bd4f
    if (metric < G_MAXUINT32 - penalty)
Packit Service a1bd4f
        return metric + penalty;
Packit Service a1bd4f
    return G_MAXUINT32;
Packit 5756e2
}
Packit 5756e2
Packit Service a1bd4f
int nm_utils_modprobe(GError **error, gboolean suppress_error_loggin, const char *arg1, ...)
Packit Service a1bd4f
    G_GNUC_NULL_TERMINATED;
Packit Service a1bd4f
Packit Service a1bd4f
void nm_utils_kill_process_sync(pid_t       pid,
Packit Service a1bd4f
                                guint64     start_time,
Packit Service a1bd4f
                                int         sig,
Packit Service a1bd4f
                                guint64     log_domain,
Packit Service a1bd4f
                                const char *log_name,
Packit Service a1bd4f
                                guint32     wait_before_kill_msec,
Packit Service a1bd4f
                                guint32     sleep_duration_msec,
Packit Service a1bd4f
                                guint32     max_wait_msec);
Packit Service a1bd4f
Packit Service a1bd4f
typedef void (*NMUtilsKillChildAsyncCb)(pid_t    pid,
Packit Service a1bd4f
                                        gboolean success,
Packit Service a1bd4f
                                        int      child_status,
Packit Service a1bd4f
                                        void *   user_data);
Packit Service a1bd4f
void     nm_utils_kill_child_async(pid_t                   pid,
Packit Service a1bd4f
                                   int                     sig,
Packit Service a1bd4f
                                   guint64                 log_domain,
Packit Service a1bd4f
                                   const char *            log_name,
Packit Service a1bd4f
                                   guint32                 wait_before_kill_msec,
Packit Service a1bd4f
                                   NMUtilsKillChildAsyncCb callback,
Packit Service a1bd4f
                                   void *                  user_data);
Packit Service a1bd4f
gboolean nm_utils_kill_child_sync(pid_t       pid,
Packit Service a1bd4f
                                  int         sig,
Packit Service a1bd4f
                                  guint64     log_domain,
Packit Service a1bd4f
                                  const char *log_name,
Packit Service a1bd4f
                                  int *       child_status,
Packit Service a1bd4f
                                  guint32     wait_before_kill_msec,
Packit Service a1bd4f
                                  guint32     sleep_duration_msec);
Packit Service a1bd4f
Packit Service a1bd4f
const char *nm_utils_find_helper(const char *progname, const char *try_first, GError **error);
Packit Service a1bd4f
Packit Service a1bd4f
char *nm_utils_read_link_absolute(const char *link_file, GError **error);
Packit Service a1bd4f
Packit Service a1bd4f
#define NM_MATCH_SPEC_MAC_TAG              "mac:"
Packit Service a1bd4f
#define NM_MATCH_SPEC_S390_SUBCHANNELS_TAG "s390-subchannels:"
Packit Service a1bd4f
#define NM_MATCH_SPEC_INTERFACE_NAME_TAG   "interface-name:"
Packit 5756e2
Packit 5756e2
typedef enum {
Packit Service a1bd4f
    NM_MATCH_SPEC_NO_MATCH  = 0,
Packit Service a1bd4f
    NM_MATCH_SPEC_MATCH     = 1,
Packit Service a1bd4f
    NM_MATCH_SPEC_NEG_MATCH = 2,
Packit 5756e2
} NMMatchSpecMatchType;
Packit 5756e2
Packit Service a1bd4f
NMMatchSpecMatchType nm_match_spec_device(const GSList *specs,
Packit Service a1bd4f
                                          const char *  interface_name,
Packit Service a1bd4f
                                          const char *  device_type,
Packit Service a1bd4f
                                          const char *  driver,
Packit Service a1bd4f
                                          const char *  driver_version,
Packit Service a1bd4f
                                          const char *  hwaddr,
Packit Service a1bd4f
                                          const char *  s390_subchannels,
Packit Service a1bd4f
                                          const char *  dhcp_plugin);
Packit Service a1bd4f
NMMatchSpecMatchType nm_match_spec_config(const GSList *specs, guint nm_version, const char *env);
Packit Service a1bd4f
GSList *             nm_match_spec_split(const char *value);
Packit Service a1bd4f
char *               nm_match_spec_join(GSList *specs);
Packit Service a1bd4f
Packit Service a1bd4f
gboolean nm_wildcard_match_check(const char *str, const char *const *patterns, guint num_patterns);
Packit Service a1bd4f
Packit Service a1bd4f
gboolean nm_utils_kernel_cmdline_match_check(const char *const *proc_cmdline,
Packit Service a1bd4f
                                             const char *const *patterns,
Packit Service a1bd4f
                                             guint              num_patterns,
Packit Service a1bd4f
                                             GError **          error);
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
gboolean nm_utils_connection_has_default_route(NMConnection *connection,
Packit Service a1bd4f
                                               int           addr_family,
Packit Service a1bd4f
                                               gboolean *    out_is_never_default);
Packit 5756e2
Packit Service a1bd4f
char *      nm_utils_new_vlan_name(const char *parent_iface, guint32 vlan_id);
Packit Service a1bd4f
const char *nm_utils_new_infiniband_name(char *name, const char *parent_name, int p_key);
Packit 5756e2
Packit Service a1bd4f
int nm_utils_cmp_connection_by_autoconnect_priority(NMConnection *a, NMConnection *b);
Packit 5756e2
Packit Service a1bd4f
void nm_utils_log_connection_diff(NMConnection *connection,
Packit Service a1bd4f
                                  NMConnection *diff_base,
Packit Service a1bd4f
                                  guint32       level,
Packit Service a1bd4f
                                  guint64       domain,
Packit Service a1bd4f
                                  const char *  name,
Packit Service a1bd4f
                                  const char *  prefix,
Packit Service a1bd4f
                                  const char *  dbus_path);
Packit 5756e2
Packit Service a1bd4f
gboolean    nm_utils_is_valid_path_component(const char *name);
Packit Service a1bd4f
const char *NM_ASSERT_VALID_PATH_COMPONENT(const char *name);
Packit 5756e2
Packit 5756e2
#define NM_UTILS_SYSCTL_IP_CONF_PATH_BUFSIZE 100
Packit 5756e2
Packit Service a1bd4f
const char *
Packit Service a1bd4f
nm_utils_sysctl_ip_conf_path(int addr_family, char *buf, const char *ifname, const char *property);
Packit 5756e2
Packit Service a1bd4f
gboolean nm_utils_sysctl_ip_conf_is_path(int         addr_family,
Packit Service a1bd4f
                                         const char *path,
Packit Service a1bd4f
                                         const char *ifname,
Packit Service a1bd4f
                                         const char *property);
Packit 5756e2
Packit Service a1bd4f
gboolean nm_utils_is_specific_hostname(const char *name);
Packit 5756e2
Packit 5756e2
struct _NMUuid;
Packit 5756e2
Packit Service a1bd4f
const char *          nm_utils_machine_id_str(void);
Packit Service a1bd4f
const struct _NMUuid *nm_utils_machine_id_bin(void);
Packit Service a1bd4f
gboolean              nm_utils_machine_id_is_fake(void);
Packit 5756e2
Packit Service a1bd4f
const char *          nm_utils_boot_id_str(void);
Packit Service a1bd4f
const struct _NMUuid *nm_utils_boot_id_bin(void);
Packit Service a1bd4f
const char *          nm_utils_proc_cmdline(void);
Packit Service a1bd4f
const char *const *   nm_utils_proc_cmdline_split(void);
Packit 5756e2
Packit Service a1bd4f
gboolean nm_utils_host_id_get(const guint8 **out_host_id, gsize *out_host_id_len);
Packit Service a1bd4f
gint64   nm_utils_host_id_get_timestamp_ns(void);
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
int nm_utils_arp_type_detect_from_hwaddrlen(gsize hwaddr_len);
Packit 5756e2
Packit Service a1bd4f
gboolean nm_utils_arp_type_validate_hwaddr(int arp_type, const guint8 *hwaddr, gsize hwaddr_len);
Packit 5756e2
Packit Service a1bd4f
gboolean
Packit Service a1bd4f
nm_utils_arp_type_get_hwaddr_relevant_part(int arp_type, const guint8 **hwaddr, gsize *hwaddr_len);
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
/* IPv6 Interface Identifier helpers */
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * NMUtilsIPv6IfaceId:
Packit 5756e2
 * @id: convenience member for validity checking; never use directly
Packit 5756e2
 * @id_u8: the 64-bit Interface Identifier
Packit 5756e2
 *
Packit 5756e2
 * Holds a 64-bit IPv6 Interface Identifier.  The IID is a sequence of bytes
Packit 5756e2
 * and should not normally be treated as a %guint64, but this is done for
Packit 5756e2
 * convenience of validity checking and initialization.
Packit 5756e2
 */
Packit 5756e2
struct _NMUtilsIPv6IfaceId {
Packit Service a1bd4f
    union {
Packit Service a1bd4f
        guint64 id;
Packit Service a1bd4f
        guint8  id_u8[8];
Packit Service a1bd4f
    };
Packit 5756e2
};
Packit 5756e2
Packit Service a1bd4f
#define NM_UTILS_IPV6_IFACE_ID_INIT \
Packit Service a1bd4f
    {                               \
Packit Service a1bd4f
        {                           \
Packit Service a1bd4f
            .id = 0                 \
Packit Service a1bd4f
        }                           \
Packit Service a1bd4f
    }
Packit 5756e2
Packit Service a1bd4f
void nm_utils_ipv6_addr_set_interface_identifier(struct in6_addr *        addr,
Packit 5756e2
                                                 const NMUtilsIPv6IfaceId iid);
Packit 5756e2
Packit Service a1bd4f
void nm_utils_ipv6_interface_identifier_get_from_addr(NMUtilsIPv6IfaceId *   iid,
Packit 5756e2
                                                      const struct in6_addr *addr);
Packit 5756e2
Packit Service a1bd4f
gboolean nm_utils_ipv6_interface_identifier_get_from_token(NMUtilsIPv6IfaceId *iid,
Packit Service a1bd4f
                                                           const char *        token);
Packit 5756e2
Packit Service a1bd4f
const char *nm_utils_inet6_interface_identifier_to_token(NMUtilsIPv6IfaceId iid,
Packit Service a1bd4f
                                                         char buf[static INET6_ADDRSTRLEN]);
Packit 5756e2
Packit Service a1bd4f
gboolean nm_utils_get_ipv6_interface_identifier(NMLinkType          link_type,
Packit Service a1bd4f
                                                const guint8 *      hwaddr,
Packit Service a1bd4f
                                                guint               len,
Packit Service a1bd4f
                                                guint               dev_id,
Packit Service a1bd4f
                                                NMUtilsIPv6IfaceId *out_iid);
Packit 5756e2
Packit 5756e2
typedef enum {
Packit Service a1bd4f
    /* The stable type. Note that this value is encoded in the
Packit Service a1bd4f
     * generated addresses, thus the numbers MUST not change.
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * Also note, if we ever allocate ID 255, we must take care
Packit Service a1bd4f
     * that nm_utils_ipv6_addr_set_stable_privacy() extends the
Packit Service a1bd4f
     * uint8 encoding of this value. */
Packit Service a1bd4f
    NM_UTILS_STABLE_TYPE_UUID      = 0,
Packit Service a1bd4f
    NM_UTILS_STABLE_TYPE_STABLE_ID = 1,
Packit Service a1bd4f
    NM_UTILS_STABLE_TYPE_GENERATED = 2,
Packit Service a1bd4f
    NM_UTILS_STABLE_TYPE_RANDOM    = 3,
Packit 5756e2
} NMUtilsStableType;
Packit 5756e2
Packit Service a1bd4f
NMUtilsStableType nm_utils_stable_id_parse(const char *stable_id,
Packit Service a1bd4f
                                           const char *deviceid,
Packit Service a1bd4f
                                           const char *hwaddr,
Packit Service a1bd4f
                                           const char *bootid,
Packit Service a1bd4f
                                           const char *uuid,
Packit Service a1bd4f
                                           char **     out_generated);
Packit Service a1bd4f
Packit Service a1bd4f
char *nm_utils_stable_id_random(void);
Packit Service a1bd4f
char *nm_utils_stable_id_generated_complete(const char *msg);
Packit Service a1bd4f
Packit Service a1bd4f
gboolean nm_utils_ipv6_addr_set_stable_privacy_impl(NMUtilsStableType stable_type,
Packit Service a1bd4f
                                                    struct in6_addr * addr,
Packit Service a1bd4f
                                                    const char *      ifname,
Packit Service a1bd4f
                                                    const char *      network_id,
Packit Service a1bd4f
                                                    guint32           dad_counter,
Packit Service a1bd4f
                                                    guint8 *          host_id,
Packit Service a1bd4f
                                                    gsize             host_id_len,
Packit Service a1bd4f
                                                    GError **         error);
Packit Service a1bd4f
Packit Service a1bd4f
gboolean nm_utils_ipv6_addr_set_stable_privacy(NMUtilsStableType id_type,
Packit Service a1bd4f
                                               struct in6_addr * addr,
Packit Service a1bd4f
                                               const char *      ifname,
Packit Service a1bd4f
                                               const char *      network_id,
Packit Service a1bd4f
                                               guint32           dad_counter,
Packit Service a1bd4f
                                               GError **         error);
Packit Service a1bd4f
Packit Service a1bd4f
char *nm_utils_hw_addr_gen_random_eth(const char *current_mac_address,
Packit Service a1bd4f
                                      const char *generate_mac_address_mask);
Packit Service a1bd4f
char *nm_utils_hw_addr_gen_stable_eth_impl(NMUtilsStableType stable_type,
Packit Service a1bd4f
                                           const char *      stable_id,
Packit Service a1bd4f
                                           const guint8 *    host_id,
Packit Service a1bd4f
                                           gsize             host_id_len,
Packit Service a1bd4f
                                           const char *      ifname,
Packit Service a1bd4f
                                           const char *      current_mac_address,
Packit Service a1bd4f
                                           const char *      generate_mac_address_mask);
Packit Service a1bd4f
char *nm_utils_hw_addr_gen_stable_eth(NMUtilsStableType stable_type,
Packit Service a1bd4f
                                      const char *      stable_id,
Packit Service a1bd4f
                                      const char *      ifname,
Packit Service a1bd4f
                                      const char *      current_mac_address,
Packit Service a1bd4f
                                      const char *      generate_mac_address_mask);
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
GBytes *nm_utils_dhcp_client_id_mac(int arp_type, const guint8 *hwaddr, gsize hwaddr_len);
Packit 5756e2
Packit Service a1bd4f
guint32 nm_utils_create_dhcp_iaid(gboolean      legacy_unstable_byteorder,
Packit Service a1bd4f
                                  const guint8 *interface_id,
Packit Service a1bd4f
                                  gsize         interface_id_len);
Packit 5756e2
Packit Service 87a54e
GBytes *nm_utils_dhcp_client_id_duid(guint32 iaid, const guint8 *duid, gsize duid_len);
Packit Service 87a54e
Packit Service a1bd4f
GBytes *nm_utils_dhcp_client_id_systemd_node_specific_full(guint32       iaid,
Packit Service a1bd4f
                                                           const guint8 *machine_id,
Packit Service a1bd4f
                                                           gsize         machine_id_len);
Packit 5756e2
Packit Service a1bd4f
GBytes *nm_utils_dhcp_client_id_systemd_node_specific(guint32 iaid);
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
/* RFC 3315 defines the epoch for the DUID-LLT time field on Jan 1st 2000. */
Packit Service a1bd4f
#define NM_UTILS_EPOCH_DATETIME_200001010000 946684800
Packit 5756e2
Packit 5756e2
struct _NMUuid;
Packit 5756e2
Packit Service a1bd4f
GBytes *
Packit Service a1bd4f
nm_utils_generate_duid_llt(int arp_type, const guint8 *hwaddr, gsize hwaddr_len, gint64 time);
Packit 5756e2
Packit Service a1bd4f
GBytes *nm_utils_generate_duid_ll(int arp_type, const guint8 *hwaddr, gsize hwaddr_len);
Packit 5756e2
Packit Service a1bd4f
GBytes *nm_utils_generate_duid_uuid(const struct _NMUuid *uuid);
Packit 5756e2
Packit Service a1bd4f
GBytes *nm_utils_generate_duid_from_machine_id(void);
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
void nm_utils_array_remove_at_indexes(GArray *array, const guint *indexes_to_delete, gsize len);
Packit 5756e2
Packit Service a1bd4f
void nm_utils_setpgid(gpointer unused);
Packit 5756e2
Packit 5756e2
typedef enum {
Packit Service a1bd4f
    NM_UTILS_TEST_NONE = 0,
Packit 5756e2
Packit Service a1bd4f
    /* Internal flag, marking that either nm_utils_get_testing() or _nm_utils_set_testing() was called. */
Packit Service a1bd4f
    _NM_UTILS_TEST_INITIALIZED = (1LL << 0),
Packit 5756e2
Packit Service a1bd4f
    /* Indicate that test mode is enabled in general. Explicitly calling _nm_utils_set_testing() will always set this flag. */
Packit Service a1bd4f
    _NM_UTILS_TEST_GENERAL = (1LL << 1),
Packit 5756e2
Packit Service a1bd4f
    /* Don't check the owner of keyfiles during testing. */
Packit Service a1bd4f
    NM_UTILS_TEST_NO_KEYFILE_OWNER_CHECK = (1LL << 2),
Packit 5756e2
Packit Service a1bd4f
    _NM_UTILS_TEST_LAST,
Packit Service a1bd4f
    NM_UTILS_TEST_ALL = (((_NM_UTILS_TEST_LAST - 1) << 1) - 1) & ~(_NM_UTILS_TEST_INITIALIZED),
Packit 5756e2
} NMUtilsTestFlags;
Packit 5756e2
Packit Service a1bd4f
gboolean         nm_utils_get_testing_initialized(void);
Packit Service a1bd4f
NMUtilsTestFlags nm_utils_get_testing(void);
Packit Service a1bd4f
void             _nm_utils_set_testing(NMUtilsTestFlags flags);
Packit 5756e2
Packit Service a1bd4f
void nm_utils_g_value_set_strv(GValue *value, GPtrArray *strings);
Packit 5756e2
Packit Service a1bd4f
guint32
Packit Service a1bd4f
nm_utils_lifetime_rebase_relative_time_on_now(guint32 timestamp, guint32 duration, gint32 now);
Packit 5756e2
Packit Service a1bd4f
guint32 nm_utils_lifetime_get(guint32  timestamp,
Packit Service a1bd4f
                              guint32  lifetime,
Packit Service a1bd4f
                              guint32  preferred,
Packit Service a1bd4f
                              gint32   now,
Packit Service a1bd4f
                              guint32 *out_preferred);
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
#define NM_IPV4LL_NETWORK ((in_addr_t)(htonl(0xA9FE0000lu)))
Packit Service a1bd4f
#define NM_IPV4LL_NETMASK ((in_addr_t)(htonl(0xFFFF0000lu)))
Packit 5756e2
Packit 5756e2
static inline gboolean
Packit Service a1bd4f
nm_utils_ip4_address_is_link_local(in_addr_t addr)
Packit 5756e2
{
Packit Service a1bd4f
    return (addr & NM_IPV4LL_NETMASK) == NM_IPV4LL_NETWORK;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static inline gboolean
Packit Service a1bd4f
nm_utils_ip4_address_is_zeronet(in_addr_t network)
Packit 5756e2
{
Packit Service a1bd4f
    /* Same as ipv4_is_zeronet() from kernel's include/linux/in.h. */
Packit Service a1bd4f
    return (network & htonl(0xFF000000u)) == htonl(0x00000000u);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
const char *nm_utils_dnsmasq_status_to_string(int status, char *dest, gsize size);
Packit Service a1bd4f
Packit Service a1bd4f
void nm_utils_get_reverse_dns_domains_ip_4(guint32 ip, guint8 plen, GPtrArray *domains);
Packit Service a1bd4f
void
Packit Service a1bd4f
nm_utils_get_reverse_dns_domains_ip_6(const struct in6_addr *ip, guint8 plen, GPtrArray *domains);
Packit 5756e2
Packit Service a1bd4f
static inline void
Packit Service a1bd4f
nm_utils_get_reverse_dns_domains_ip(int           addr_family,
Packit Service a1bd4f
                                    gconstpointer addr,
Packit Service a1bd4f
                                    guint8        plen,
Packit Service a1bd4f
                                    GPtrArray *   domains)
Packit Service a1bd4f
{
Packit Service a1bd4f
    if (NM_IS_IPv4(addr_family))
Packit Service a1bd4f
        nm_utils_get_reverse_dns_domains_ip_4(*((const in_addr_t *) addr), plen, domains);
Packit Service a1bd4f
    else
Packit Service a1bd4f
        nm_utils_get_reverse_dns_domains_ip_6(addr, plen, domains);
Packit Service a1bd4f
}
Packit 5756e2
Packit 5756e2
struct stat;
Packit 5756e2
Packit Service a1bd4f
gboolean nm_utils_validate_plugin(const char *path, struct stat *stat, GError **error);
Packit Service a1bd4f
char **  nm_utils_read_plugin_paths(const char *dirname, const char *prefix);
Packit Service a1bd4f
char *   nm_utils_format_con_diff_for_audit(GHashTable *diff);
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
/* this enum is compatible with ICMPV6_ROUTER_PREF_* (from <linux/icmpv6.h>,
Packit 5756e2
 * the values for netlink attribute RTA_PREF) and "enum ndp_route_preference"
Packit 5756e2
 * from <ndp.h>. */
Packit Service 018b0a
typedef enum _nm_packed {
Packit Service a1bd4f
    NM_ICMPV6_ROUTER_PREF_MEDIUM  = 0x0, /* ICMPV6_ROUTER_PREF_MEDIUM */
Packit Service a1bd4f
    NM_ICMPV6_ROUTER_PREF_LOW     = 0x3, /* ICMPV6_ROUTER_PREF_LOW */
Packit Service a1bd4f
    NM_ICMPV6_ROUTER_PREF_HIGH    = 0x1, /* ICMPV6_ROUTER_PREF_HIGH */
Packit Service a1bd4f
    NM_ICMPV6_ROUTER_PREF_INVALID = 0x2, /* ICMPV6_ROUTER_PREF_INVALID */
Packit 5756e2
} NMIcmpv6RouterPref;
Packit 5756e2
Packit Service a1bd4f
const char *nm_icmpv6_router_pref_to_string(NMIcmpv6RouterPref pref, char *buf, gsize len);
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
const char *nm_activation_type_to_string(NMActivationType activation_type);
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
const char *nm_utils_parse_dns_domain(const char *domain, gboolean *is_routing);
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
void nm_wifi_utils_parse_ies(const guint8 *bytes,
Packit Service a1bd4f
                             gsize         len,
Packit Service a1bd4f
                             guint32 *     out_max_rate,
Packit Service a1bd4f
                             gboolean *    out_metered,
Packit Service a1bd4f
                             gboolean *    out_owe_transition_mode);
Packit 5756e2
Packit Service a1bd4f
guint8 nm_wifi_utils_level_to_quality(int val);
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
#define NM_VPN_ROUTE_METRIC_DEFAULT 50
Packit 5756e2
Packit Service a1bd4f
#define NM_UTILS_ERROR_MSG_REQ_AUTH_FAILED "Unable to authenticate the request"
Packit Service a1bd4f
#define NM_UTILS_ERROR_MSG_REQ_UID_UKNOWN  "Unable to determine UID of the request"
Packit Service a1bd4f
#define NM_UTILS_ERROR_MSG_INSUFF_PRIV     "Insufficient privileges"
Packit 5756e2
Packit 5756e2
#endif /* __NM_CORE_UTILS_H__ */