mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager
synced 2024-10-15 12:34:55 +00:00
platform/wpan: add WPAN utils
Modelled after wifi-utils, sans the complexity of dispatching to anything like WEXT.
This commit is contained in:
parent
5036406b58
commit
4120ad2431
|
@ -1482,6 +1482,8 @@ src_libNetworkManagerBase_la_SOURCES = \
|
|||
src/platform/wifi/nm-wifi-utils-private.h \
|
||||
src/platform/wifi/nm-wifi-utils.c \
|
||||
src/platform/wifi/nm-wifi-utils.h \
|
||||
src/platform/wpan/nm-wpan-utils.c \
|
||||
src/platform/wpan/nm-wpan-utils.h \
|
||||
\
|
||||
src/ndisc/nm-lndp-ndisc.c \
|
||||
src/ndisc/nm-lndp-ndisc.h \
|
||||
|
|
|
@ -57,6 +57,7 @@ sources = files(
|
|||
'platform/nm-netlink.c',
|
||||
'platform/wifi/nm-wifi-utils-nl80211.c',
|
||||
'platform/wifi/nm-wifi-utils.c',
|
||||
'platform/wpan/nm-wpan-utils.c',
|
||||
'platform/nm-linux-platform.c',
|
||||
'platform/nm-platform.c',
|
||||
'platform/nm-platform-utils.c',
|
||||
|
|
286
src/platform/wpan/nm-wpan-utils.c
Normal file
286
src/platform/wpan/nm-wpan-utils.c
Normal file
|
@ -0,0 +1,286 @@
|
|||
/* NetworkManager -- Network link manager
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Copyright (C) 2018 Red Hat, Inc.
|
||||
*/
|
||||
|
||||
#include "nm-default.h"
|
||||
|
||||
#include "nm-wpan-utils.h"
|
||||
|
||||
#include "platform/linux/nl802154.h"
|
||||
#include "platform/nm-netlink.h"
|
||||
|
||||
#define _NMLOG_PREFIX_NAME "wpan-nl802154"
|
||||
#define _NMLOG(level, domain, ...) \
|
||||
G_STMT_START { \
|
||||
nm_log ((level), (domain), NULL, NULL, \
|
||||
"%s: " _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \
|
||||
_NMLOG_PREFIX_NAME \
|
||||
_NM_UTILS_MACRO_REST(__VA_ARGS__)); \
|
||||
} G_STMT_END
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
struct NMWpanUtils {
|
||||
GObject parent;
|
||||
int ifindex;
|
||||
struct nl_sock *nl_sock;
|
||||
int id;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
GObjectClass parent;
|
||||
} NMWpanUtilsClass;
|
||||
|
||||
G_DEFINE_TYPE (NMWpanUtils, nm_wpan_utils, G_TYPE_OBJECT)
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
static int
|
||||
ack_handler (struct nl_msg *msg, void *arg)
|
||||
{
|
||||
int *done = arg;
|
||||
*done = 1;
|
||||
return NL_STOP;
|
||||
}
|
||||
|
||||
static int
|
||||
finish_handler (struct nl_msg *msg, void *arg)
|
||||
{
|
||||
int *done = arg;
|
||||
*done = 1;
|
||||
return NL_SKIP;
|
||||
}
|
||||
|
||||
static int
|
||||
error_handler (struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
|
||||
{
|
||||
int *done = arg;
|
||||
*done = err->error;
|
||||
return NL_SKIP;
|
||||
}
|
||||
|
||||
static struct nl_msg *
|
||||
_nl802154_alloc_msg (int id, int ifindex, guint32 cmd, guint32 flags)
|
||||
{
|
||||
nm_auto_nlmsg struct nl_msg *msg = NULL;
|
||||
|
||||
msg = nlmsg_alloc ();
|
||||
genlmsg_put (msg, 0, 0, id, 0, flags, cmd, 0);
|
||||
NLA_PUT_U32 (msg, NL802154_ATTR_IFINDEX, ifindex);
|
||||
return g_steal_pointer (&msg);
|
||||
|
||||
nla_put_failure:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static struct nl_msg *
|
||||
nl802154_alloc_msg (NMWpanUtils *self, guint32 cmd, guint32 flags)
|
||||
{
|
||||
return _nl802154_alloc_msg (self->id, self->ifindex, cmd, flags);
|
||||
}
|
||||
|
||||
static int
|
||||
_nl802154_send_and_recv (struct nl_sock *nl_sock,
|
||||
struct nl_msg *msg,
|
||||
int (*valid_handler) (struct nl_msg *, void *),
|
||||
void *valid_data)
|
||||
{
|
||||
int err;
|
||||
int done = 0;
|
||||
const struct nl_cb cb = {
|
||||
.err_cb = error_handler,
|
||||
.err_arg = &done,
|
||||
.finish_cb = finish_handler,
|
||||
.finish_arg = &done,
|
||||
.ack_cb = ack_handler,
|
||||
.ack_arg = &done,
|
||||
.valid_cb = valid_handler,
|
||||
.valid_arg = valid_data,
|
||||
};
|
||||
|
||||
g_return_val_if_fail (msg != NULL, -ENOMEM);
|
||||
|
||||
err = nl_send_auto (nl_sock, msg);
|
||||
if (err < 0)
|
||||
return err;
|
||||
|
||||
/* Loop until one of our NL callbacks says we're done; on success
|
||||
* done will be 1, on error it will be < 0.
|
||||
*/
|
||||
while (!done) {
|
||||
err = nl_recvmsgs (nl_sock, &cb);
|
||||
if (err < 0 && err != -EAGAIN) {
|
||||
_LOGW (LOGD_PLATFORM, "nl_recvmsgs() error: (%d) %s",
|
||||
err, nl_geterror (err));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (err >= 0 && done < 0)
|
||||
err = done;
|
||||
return err;
|
||||
}
|
||||
|
||||
static int
|
||||
nl802154_send_and_recv (NMWpanUtils *self,
|
||||
struct nl_msg *msg,
|
||||
int (*valid_handler) (struct nl_msg *, void *),
|
||||
void *valid_data)
|
||||
{
|
||||
return _nl802154_send_and_recv (self->nl_sock, msg,
|
||||
valid_handler, valid_data);
|
||||
}
|
||||
|
||||
struct nl802154_interface {
|
||||
guint16 pan_id;
|
||||
guint16 short_addr;
|
||||
|
||||
gboolean valid;
|
||||
};
|
||||
|
||||
static int
|
||||
nl802154_get_interface_handler (struct nl_msg *msg, void *arg)
|
||||
{
|
||||
struct nl802154_interface *info = arg;
|
||||
struct genlmsghdr *gnlh = nlmsg_data (nlmsg_hdr (msg));
|
||||
struct nlattr *tb[NL802154_ATTR_MAX + 1] = { 0, };
|
||||
static const struct nla_policy nl802154_policy[NL802154_ATTR_MAX + 1] = {
|
||||
[NL802154_ATTR_PAN_ID] = { .type = NLA_U16 },
|
||||
[NL802154_ATTR_SHORT_ADDR] = { .type = NLA_U16 },
|
||||
};
|
||||
|
||||
if (nla_parse (tb, NL802154_ATTR_MAX, genlmsg_attrdata (gnlh, 0),
|
||||
genlmsg_attrlen (gnlh, 0), nl802154_policy) < 0)
|
||||
return NL_SKIP;
|
||||
|
||||
if (tb[NL802154_ATTR_PAN_ID])
|
||||
info->pan_id = le16toh (nla_get_u16 (tb[NL802154_ATTR_PAN_ID]));
|
||||
|
||||
if (tb[NL802154_ATTR_SHORT_ADDR])
|
||||
info->short_addr = le16toh (nla_get_u16 (tb[NL802154_ATTR_SHORT_ADDR]));
|
||||
|
||||
info->valid = TRUE;
|
||||
|
||||
return NL_SKIP;
|
||||
}
|
||||
|
||||
static void
|
||||
nl802154_get_interface (NMWpanUtils *self,
|
||||
struct nl802154_interface *interface)
|
||||
{
|
||||
nm_auto_nlmsg struct nl_msg *msg = NULL;
|
||||
|
||||
memset (interface, 0, sizeof (*interface));
|
||||
|
||||
msg = nl802154_alloc_msg (self, NL802154_CMD_GET_INTERFACE, 0);
|
||||
|
||||
nl802154_send_and_recv (self, msg, nl802154_get_interface_handler, interface);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
guint16
|
||||
nm_wpan_utils_get_pan_id (NMWpanUtils *self)
|
||||
{
|
||||
struct nl802154_interface interface;
|
||||
|
||||
nl802154_get_interface (self, &interface);
|
||||
|
||||
return interface.pan_id;
|
||||
}
|
||||
|
||||
gboolean
|
||||
nm_wpan_utils_set_pan_id (NMWpanUtils *self, guint16 pan_id)
|
||||
{
|
||||
nm_auto_nlmsg struct nl_msg *msg = NULL;
|
||||
int err;
|
||||
|
||||
g_return_val_if_fail (self != NULL, FALSE);
|
||||
|
||||
msg = nl802154_alloc_msg (self, NL802154_CMD_SET_PAN_ID, 0);
|
||||
NLA_PUT_U16 (msg, NL802154_ATTR_PAN_ID, htole16 (pan_id));
|
||||
err = nl802154_send_and_recv (self, msg, NULL, NULL);
|
||||
return err >= 0;
|
||||
|
||||
nla_put_failure:
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
guint16
|
||||
nm_wpan_utils_get_short_addr (NMWpanUtils *self)
|
||||
{
|
||||
struct nl802154_interface interface;
|
||||
|
||||
nl802154_get_interface (self, &interface);
|
||||
|
||||
return interface.short_addr;
|
||||
}
|
||||
|
||||
gboolean
|
||||
nm_wpan_utils_set_short_addr (NMWpanUtils *self, guint16 short_addr)
|
||||
{
|
||||
nm_auto_nlmsg struct nl_msg *msg = NULL;
|
||||
int err;
|
||||
|
||||
g_return_val_if_fail (self != NULL, FALSE);
|
||||
|
||||
msg = nl802154_alloc_msg (self, NL802154_CMD_SET_SHORT_ADDR, 0);
|
||||
NLA_PUT_U16 (msg, NL802154_ATTR_SHORT_ADDR, htole16 (short_addr));
|
||||
err = nl802154_send_and_recv (self, msg, NULL, NULL);
|
||||
return err >= 0;
|
||||
|
||||
nla_put_failure:
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
static void
|
||||
nm_wpan_utils_init (NMWpanUtils *self)
|
||||
{
|
||||
}
|
||||
|
||||
static void
|
||||
nm_wpan_utils_class_init (NMWpanUtilsClass *klass)
|
||||
{
|
||||
}
|
||||
|
||||
NMWpanUtils *
|
||||
nm_wpan_utils_new (int ifindex, struct nl_sock *genl, gboolean check_scan)
|
||||
{
|
||||
NMWpanUtils *self;
|
||||
int id;
|
||||
|
||||
g_return_val_if_fail (ifindex > 0, NULL);
|
||||
|
||||
if (!genl)
|
||||
return NULL;
|
||||
|
||||
id = genl_ctrl_resolve (genl, "nl802154");
|
||||
if (id < 0) {
|
||||
_LOGD (LOGD_PLATFORM, "genl_ctrl_resolve: failed to resolve \"nl802154\"");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
self = g_object_new (NM_TYPE_WPAN_UTILS, NULL);
|
||||
self->ifindex = ifindex;
|
||||
self->nl_sock = genl;
|
||||
self->id = id;
|
||||
|
||||
return self;
|
||||
}
|
47
src/platform/wpan/nm-wpan-utils.h
Normal file
47
src/platform/wpan/nm-wpan-utils.h
Normal file
|
@ -0,0 +1,47 @@
|
|||
/* NetworkManager -- Network link manager
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Copyright (C) 2018 Red Hat, Inc.
|
||||
*/
|
||||
|
||||
#ifndef __WPAN_UTILS_H__
|
||||
#define __WPAN_UTILS_H__
|
||||
|
||||
#include <net/ethernet.h>
|
||||
|
||||
#include "nm-dbus-interface.h"
|
||||
#include "platform/nm-netlink.h"
|
||||
|
||||
typedef struct NMWpanUtils NMWpanUtils;
|
||||
|
||||
#define NM_TYPE_WPAN_UTILS (nm_wpan_utils_get_type ())
|
||||
#define NM_WPAN_UTILS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_WPAN_UTILS, NMWpanUtils))
|
||||
#define NM_WPAN_UTILS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_WPAN_UTILS, NMWpanUtilsClass))
|
||||
#define NM_IS_WPAN_UTILS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_WPAN_UTILS))
|
||||
#define NM_IS_WPAN_UTILS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_WPAN_UTILS))
|
||||
#define NM_WPAN_UTILS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_WPAN_UTILS, NMWpanUtilsClass))
|
||||
|
||||
GType nm_wpan_utils_get_type (void);
|
||||
|
||||
NMWpanUtils *nm_wpan_utils_new (int ifindex, struct nl_sock *genl, gboolean check_scan);
|
||||
|
||||
guint16 nm_wpan_utils_get_pan_id (NMWpanUtils *self);
|
||||
gboolean nm_wpan_utils_set_pan_id (NMWpanUtils *self, guint16 pan_id);
|
||||
|
||||
guint16 nm_wpan_utils_get_short_addr (NMWpanUtils *self);
|
||||
gboolean nm_wpan_utils_set_short_addr (NMWpanUtils *self, guint16 short_addr);
|
||||
|
||||
#endif /* __WPAN_UTILS_H__ */
|
Loading…
Reference in a new issue