Commit 60040513 authored by Sven Eckelmann's avatar Sven Eckelmann Committed by Simon Wunderlich

batman-adv: Prepare framework for mesh genl config

The batman-adv configuration interface was implemented solely using sysfs.
This approach was condemned by non-batadv developers as "huge mistake".
Instead a netlink/genl based implementation was suggested.

The main objects for this configuration is the mesh/soft-interface object.
Its actual object in memory already contains most of the available
configuration settings. The genl interface reflects this by allowing to
get/set it using the mesh specific commands.

The BATADV_CMD_GET_MESH_INFO (or short version BATADV_CMD_GET_MESH) is
reused as get command because it already provides the content of other
information from the mesh/soft-interface which are not yet configuration
specific.

The set command BATADV_CMD_SET_MESH will also notify interested userspace
listeners of the "config" mcast group using the BATADV_CMD_SET_MESH command
message type that settings might have been changed and what the current
values are.
Signed-off-by: default avatarSven Eckelmann <sven@narfation.org>
Signed-off-by: default avatarSimon Wunderlich <sw@simonwunderlich.de>
parent c4a7a8d9
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#define BATADV_NL_NAME "batadv" #define BATADV_NL_NAME "batadv"
#define BATADV_NL_MCAST_GROUP_CONFIG "config"
#define BATADV_NL_MCAST_GROUP_TPMETER "tpmeter" #define BATADV_NL_MCAST_GROUP_TPMETER "tpmeter"
/** /**
...@@ -372,10 +373,14 @@ enum batadv_nl_commands { ...@@ -372,10 +373,14 @@ enum batadv_nl_commands {
BATADV_CMD_UNSPEC, BATADV_CMD_UNSPEC,
/** /**
* @BATADV_CMD_GET_MESH_INFO: Query basic information about batman-adv * @BATADV_CMD_GET_MESH: Get attributes from softif/mesh
* device
*/ */
BATADV_CMD_GET_MESH_INFO, BATADV_CMD_GET_MESH,
/**
* @BATADV_CMD_GET_MESH_INFO: Alias for @BATADV_CMD_GET_MESH
*/
BATADV_CMD_GET_MESH_INFO = BATADV_CMD_GET_MESH,
/** /**
* @BATADV_CMD_TP_METER: Start a tp meter session * @BATADV_CMD_TP_METER: Start a tp meter session
...@@ -443,6 +448,11 @@ enum batadv_nl_commands { ...@@ -443,6 +448,11 @@ enum batadv_nl_commands {
*/ */
BATADV_CMD_GET_MCAST_FLAGS, BATADV_CMD_GET_MCAST_FLAGS,
/**
* @BATADV_CMD_SET_MESH: Set attributes for softif/mesh
*/
BATADV_CMD_SET_MESH,
/* add new commands above here */ /* add new commands above here */
/** /**
......
...@@ -62,6 +62,7 @@ struct genl_family batadv_netlink_family; ...@@ -62,6 +62,7 @@ struct genl_family batadv_netlink_family;
/* multicast groups */ /* multicast groups */
enum batadv_netlink_multicast_groups { enum batadv_netlink_multicast_groups {
BATADV_NL_MCGRP_CONFIG,
BATADV_NL_MCGRP_TPMETER, BATADV_NL_MCGRP_TPMETER,
}; };
...@@ -78,6 +79,7 @@ enum batadv_genl_ops_flags { ...@@ -78,6 +79,7 @@ enum batadv_genl_ops_flags {
}; };
static const struct genl_multicast_group batadv_netlink_mcgrps[] = { static const struct genl_multicast_group batadv_netlink_mcgrps[] = {
[BATADV_NL_MCGRP_CONFIG] = { .name = BATADV_NL_MCAST_GROUP_CONFIG },
[BATADV_NL_MCGRP_TPMETER] = { .name = BATADV_NL_MCAST_GROUP_TPMETER }, [BATADV_NL_MCGRP_TPMETER] = { .name = BATADV_NL_MCAST_GROUP_TPMETER },
}; };
...@@ -138,20 +140,29 @@ batadv_netlink_get_ifindex(const struct nlmsghdr *nlh, int attrtype) ...@@ -138,20 +140,29 @@ batadv_netlink_get_ifindex(const struct nlmsghdr *nlh, int attrtype)
} }
/** /**
* batadv_netlink_mesh_info_put() - fill in generic information about mesh * batadv_netlink_mesh_fill() - Fill message with mesh attributes
* interface * @msg: Netlink message to dump into
* @msg: netlink message to be sent back * @bat_priv: the bat priv with all the soft interface information
* @soft_iface: interface for which the data should be taken * @cmd: type of message to generate
* @portid: Port making netlink request
* @seq: sequence number for message
* @flags: Additional flags for message
* *
* Return: 0 on success, < 0 on error * Return: 0 on success or negative error number in case of failure
*/ */
static int static int batadv_netlink_mesh_fill(struct sk_buff *msg,
batadv_netlink_mesh_info_put(struct sk_buff *msg, struct net_device *soft_iface) struct batadv_priv *bat_priv,
enum batadv_nl_commands cmd,
u32 portid, u32 seq, int flags)
{ {
struct batadv_priv *bat_priv = netdev_priv(soft_iface); struct net_device *soft_iface = bat_priv->soft_iface;
struct batadv_hard_iface *primary_if = NULL; struct batadv_hard_iface *primary_if = NULL;
struct net_device *hard_iface; struct net_device *hard_iface;
int ret = -ENOBUFS; void *hdr;
hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, flags, cmd);
if (!hdr)
return -ENOBUFS;
if (nla_put_string(msg, BATADV_ATTR_VERSION, BATADV_SOURCE_VERSION) || if (nla_put_string(msg, BATADV_ATTR_VERSION, BATADV_SOURCE_VERSION) ||
nla_put_string(msg, BATADV_ATTR_ALGO_NAME, nla_put_string(msg, BATADV_ATTR_ALGO_NAME,
...@@ -162,16 +173,16 @@ batadv_netlink_mesh_info_put(struct sk_buff *msg, struct net_device *soft_iface) ...@@ -162,16 +173,16 @@ batadv_netlink_mesh_info_put(struct sk_buff *msg, struct net_device *soft_iface)
soft_iface->dev_addr) || soft_iface->dev_addr) ||
nla_put_u8(msg, BATADV_ATTR_TT_TTVN, nla_put_u8(msg, BATADV_ATTR_TT_TTVN,
(u8)atomic_read(&bat_priv->tt.vn))) (u8)atomic_read(&bat_priv->tt.vn)))
goto out; goto nla_put_failure;
#ifdef CONFIG_BATMAN_ADV_BLA #ifdef CONFIG_BATMAN_ADV_BLA
if (nla_put_u16(msg, BATADV_ATTR_BLA_CRC, if (nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
ntohs(bat_priv->bla.claim_dest.group))) ntohs(bat_priv->bla.claim_dest.group)))
goto out; goto nla_put_failure;
#endif #endif
if (batadv_mcast_mesh_info_put(msg, bat_priv)) if (batadv_mcast_mesh_info_put(msg, bat_priv))
goto out; goto nla_put_failure;
primary_if = batadv_primary_if_get_selected(bat_priv); primary_if = batadv_primary_if_get_selected(bat_priv);
if (primary_if && primary_if->if_status == BATADV_IF_ACTIVE) { if (primary_if && primary_if->if_status == BATADV_IF_ACTIVE) {
...@@ -183,77 +194,95 @@ batadv_netlink_mesh_info_put(struct sk_buff *msg, struct net_device *soft_iface) ...@@ -183,77 +194,95 @@ batadv_netlink_mesh_info_put(struct sk_buff *msg, struct net_device *soft_iface)
hard_iface->name) || hard_iface->name) ||
nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN, nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN,
hard_iface->dev_addr)) hard_iface->dev_addr))
goto out; goto nla_put_failure;
} }
ret = 0; if (primary_if)
batadv_hardif_put(primary_if);
out: genlmsg_end(msg, hdr);
return 0;
nla_put_failure:
if (primary_if) if (primary_if)
batadv_hardif_put(primary_if); batadv_hardif_put(primary_if);
return ret; genlmsg_cancel(msg, hdr);
return -EMSGSIZE;
} }
/** /**
* batadv_netlink_get_mesh_info() - handle incoming BATADV_CMD_GET_MESH_INFO * batadv_netlink_notify_mesh() - send softif attributes to listener
* netlink request * @bat_priv: the bat priv with all the soft interface information
* @skb: received netlink message
* @info: receiver information
* *
* Return: 0 on success, < 0 on error * Return: 0 on success, < 0 on error
*/ */
static int static int batadv_netlink_notify_mesh(struct batadv_priv *bat_priv)
batadv_netlink_get_mesh_info(struct sk_buff *skb, struct genl_info *info)
{ {
struct net *net = genl_info_net(info); struct sk_buff *msg;
struct net_device *soft_iface;
struct sk_buff *msg = NULL;
void *msg_head;
int ifindex;
int ret; int ret;
if (!info->attrs[BATADV_ATTR_MESH_IFINDEX]) msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
return -EINVAL; if (!msg)
return -ENOMEM;
ifindex = nla_get_u32(info->attrs[BATADV_ATTR_MESH_IFINDEX]);
if (!ifindex)
return -EINVAL;
soft_iface = dev_get_by_index(net, ifindex); ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_SET_MESH,
if (!soft_iface || !batadv_softif_is_valid(soft_iface)) { 0, 0, 0);
ret = -ENODEV; if (ret < 0) {
goto out; nlmsg_free(msg);
return ret;
} }
msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); genlmsg_multicast_netns(&batadv_netlink_family,
if (!msg) { dev_net(bat_priv->soft_iface), msg, 0,
ret = -ENOMEM; BATADV_NL_MCGRP_CONFIG, GFP_KERNEL);
goto out;
}
msg_head = genlmsg_put(msg, info->snd_portid, info->snd_seq, return 0;
&batadv_netlink_family, 0, }
BATADV_CMD_GET_MESH_INFO);
if (!msg_head) {
ret = -ENOBUFS;
goto out;
}
ret = batadv_netlink_mesh_info_put(msg, soft_iface); /**
* batadv_netlink_get_mesh() - Get softif attributes
* @skb: Netlink message with request data
* @info: receiver information
*
* Return: 0 on success or negative error number in case of failure
*/
static int batadv_netlink_get_mesh(struct sk_buff *skb, struct genl_info *info)
{
struct batadv_priv *bat_priv = info->user_ptr[0];
struct sk_buff *msg;
int ret;
out: msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
if (soft_iface) if (!msg)
dev_put(soft_iface); return -ENOMEM;
if (ret) { ret = batadv_netlink_mesh_fill(msg, bat_priv, BATADV_CMD_GET_MESH,
if (msg) info->snd_portid, info->snd_seq, 0);
nlmsg_free(msg); if (ret < 0) {
nlmsg_free(msg);
return ret; return ret;
} }
genlmsg_end(msg, msg_head); ret = genlmsg_reply(msg, info);
return genlmsg_reply(msg, info);
return ret;
}
/**
* batadv_netlink_set_mesh() - Set softif attributes
* @skb: Netlink message with request data
* @info: receiver information
*
* Return: 0 on success or negative error number in case of failure
*/
static int batadv_netlink_set_mesh(struct sk_buff *skb, struct genl_info *info)
{
struct batadv_priv *bat_priv = info->user_ptr[0];
batadv_netlink_notify_mesh(bat_priv);
return 0;
} }
/** /**
...@@ -600,10 +629,11 @@ static void batadv_post_doit(const struct genl_ops *ops, struct sk_buff *skb, ...@@ -600,10 +629,11 @@ static void batadv_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
static const struct genl_ops batadv_netlink_ops[] = { static const struct genl_ops batadv_netlink_ops[] = {
{ {
.cmd = BATADV_CMD_GET_MESH_INFO, .cmd = BATADV_CMD_GET_MESH,
.flags = GENL_ADMIN_PERM, /* can be retrieved by unprivileged users */
.policy = batadv_netlink_policy, .policy = batadv_netlink_policy,
.doit = batadv_netlink_get_mesh_info, .doit = batadv_netlink_get_mesh,
.internal_flags = BATADV_FLAG_NEED_MESH,
}, },
{ {
.cmd = BATADV_CMD_TP_METER, .cmd = BATADV_CMD_TP_METER,
...@@ -685,7 +715,13 @@ static const struct genl_ops batadv_netlink_ops[] = { ...@@ -685,7 +715,13 @@ static const struct genl_ops batadv_netlink_ops[] = {
.policy = batadv_netlink_policy, .policy = batadv_netlink_policy,
.dumpit = batadv_mcast_flags_dump, .dumpit = batadv_mcast_flags_dump,
}, },
{
.cmd = BATADV_CMD_SET_MESH,
.flags = GENL_ADMIN_PERM,
.policy = batadv_netlink_policy,
.doit = batadv_netlink_set_mesh,
.internal_flags = BATADV_FLAG_NEED_MESH,
},
}; };
struct genl_family batadv_netlink_family __ro_after_init = { struct genl_family batadv_netlink_family __ro_after_init = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment