2019-04-05 17:31:34 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2014-07-04 21:34:38 +00:00
|
|
|
/* Copyright 2011-2014 Autronica Fire and Security AS
|
2013-10-30 20:10:47 +00:00
|
|
|
*
|
|
|
|
* Author(s):
|
2014-07-04 21:34:38 +00:00
|
|
|
* 2011-2014 Arvid Brodin, arvid.brodin@alten.se
|
2013-10-30 20:10:47 +00:00
|
|
|
*
|
2020-07-22 14:40:16 +00:00
|
|
|
* Routines for handling Netlink messages for HSR and PRP.
|
2013-10-30 20:10:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "hsr_netlink.h"
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <net/rtnetlink.h>
|
|
|
|
#include <net/genetlink.h>
|
|
|
|
#include "hsr_main.h"
|
|
|
|
#include "hsr_device.h"
|
|
|
|
#include "hsr_framereg.h"
|
|
|
|
|
|
|
|
static const struct nla_policy hsr_policy[IFLA_HSR_MAX + 1] = {
|
|
|
|
[IFLA_HSR_SLAVE1] = { .type = NLA_U32 },
|
|
|
|
[IFLA_HSR_SLAVE2] = { .type = NLA_U32 },
|
|
|
|
[IFLA_HSR_MULTICAST_SPEC] = { .type = NLA_U8 },
|
2016-04-13 11:52:22 +00:00
|
|
|
[IFLA_HSR_VERSION] = { .type = NLA_U8 },
|
2016-04-19 11:34:28 +00:00
|
|
|
[IFLA_HSR_SUPERVISION_ADDR] = { .len = ETH_ALEN },
|
2013-11-29 22:38:16 +00:00
|
|
|
[IFLA_HSR_SEQ_NR] = { .type = NLA_U16 },
|
2020-07-22 14:40:16 +00:00
|
|
|
[IFLA_HSR_PROTOCOL] = { .type = NLA_U8 },
|
2013-10-30 20:10:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Here, it seems a netdevice has already been allocated for us, and the
|
|
|
|
* hsr_dev_setup routine has been executed. Nice!
|
|
|
|
*/
|
|
|
|
static int hsr_newlink(struct net *src_net, struct net_device *dev,
|
2017-06-25 21:55:59 +00:00
|
|
|
struct nlattr *tb[], struct nlattr *data[],
|
|
|
|
struct netlink_ext_ack *extack)
|
2013-10-30 20:10:47 +00:00
|
|
|
{
|
2020-07-22 14:40:16 +00:00
|
|
|
enum hsr_version proto_version;
|
|
|
|
unsigned char multicast_spec;
|
|
|
|
u8 proto = HSR_PROTOCOL_HSR;
|
2013-10-30 20:10:47 +00:00
|
|
|
struct net_device *link[2];
|
|
|
|
|
2014-07-04 21:42:00 +00:00
|
|
|
if (!data) {
|
2020-02-28 18:01:35 +00:00
|
|
|
NL_SET_ERR_MSG_MOD(extack, "No slave devices specified");
|
2014-07-04 21:42:00 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-10-30 20:10:47 +00:00
|
|
|
if (!data[IFLA_HSR_SLAVE1]) {
|
2020-02-28 18:01:35 +00:00
|
|
|
NL_SET_ERR_MSG_MOD(extack, "Slave1 device not specified");
|
2013-10-30 20:10:47 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2019-04-05 17:31:23 +00:00
|
|
|
link[0] = __dev_get_by_index(src_net,
|
|
|
|
nla_get_u32(data[IFLA_HSR_SLAVE1]));
|
2020-02-28 18:01:35 +00:00
|
|
|
if (!link[0]) {
|
|
|
|
NL_SET_ERR_MSG_MOD(extack, "Slave1 does not exist");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-10-30 20:10:47 +00:00
|
|
|
if (!data[IFLA_HSR_SLAVE2]) {
|
2020-02-28 18:01:35 +00:00
|
|
|
NL_SET_ERR_MSG_MOD(extack, "Slave2 device not specified");
|
2013-10-30 20:10:47 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2019-04-05 17:31:23 +00:00
|
|
|
link[1] = __dev_get_by_index(src_net,
|
|
|
|
nla_get_u32(data[IFLA_HSR_SLAVE2]));
|
2020-02-28 18:01:35 +00:00
|
|
|
if (!link[1]) {
|
|
|
|
NL_SET_ERR_MSG_MOD(extack, "Slave2 does not exist");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-10-30 20:10:47 +00:00
|
|
|
|
2020-02-28 18:01:35 +00:00
|
|
|
if (link[0] == link[1]) {
|
|
|
|
NL_SET_ERR_MSG_MOD(extack, "Slave1 and Slave2 are same");
|
2013-10-30 20:10:47 +00:00
|
|
|
return -EINVAL;
|
2020-02-28 18:01:35 +00:00
|
|
|
}
|
2013-10-30 20:10:47 +00:00
|
|
|
|
|
|
|
if (!data[IFLA_HSR_MULTICAST_SPEC])
|
|
|
|
multicast_spec = 0;
|
|
|
|
else
|
|
|
|
multicast_spec = nla_get_u8(data[IFLA_HSR_MULTICAST_SPEC]);
|
|
|
|
|
2020-07-22 14:40:16 +00:00
|
|
|
if (data[IFLA_HSR_PROTOCOL])
|
|
|
|
proto = nla_get_u8(data[IFLA_HSR_PROTOCOL]);
|
|
|
|
|
|
|
|
if (proto >= HSR_PROTOCOL_MAX) {
|
2020-09-09 09:38:21 +00:00
|
|
|
NL_SET_ERR_MSG_MOD(extack, "Unsupported protocol");
|
2020-07-22 14:40:16 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-04-07 13:23:21 +00:00
|
|
|
if (!data[IFLA_HSR_VERSION]) {
|
2020-07-22 14:40:16 +00:00
|
|
|
proto_version = HSR_V0;
|
2020-04-07 13:23:21 +00:00
|
|
|
} else {
|
2020-07-22 14:40:16 +00:00
|
|
|
if (proto == HSR_PROTOCOL_PRP) {
|
2020-09-09 09:38:21 +00:00
|
|
|
NL_SET_ERR_MSG_MOD(extack, "PRP version unsupported");
|
2020-07-22 14:40:16 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
proto_version = nla_get_u8(data[IFLA_HSR_VERSION]);
|
|
|
|
if (proto_version > HSR_V1) {
|
2020-04-07 13:23:21 +00:00
|
|
|
NL_SET_ERR_MSG_MOD(extack,
|
2020-09-09 09:38:21 +00:00
|
|
|
"Only HSR version 0/1 supported");
|
2020-04-07 13:23:21 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
2016-04-13 11:52:22 +00:00
|
|
|
|
2020-07-22 14:40:16 +00:00
|
|
|
if (proto == HSR_PROTOCOL_PRP)
|
|
|
|
proto_version = PRP_V1;
|
|
|
|
|
|
|
|
return hsr_dev_finalize(dev, link, multicast_spec, proto_version, extack);
|
2013-10-30 20:10:47 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 13:46:25 +00:00
|
|
|
static void hsr_dellink(struct net_device *dev, struct list_head *head)
|
|
|
|
{
|
|
|
|
struct hsr_priv *hsr = netdev_priv(dev);
|
|
|
|
|
|
|
|
del_timer_sync(&hsr->prune_timer);
|
|
|
|
del_timer_sync(&hsr->announce_timer);
|
|
|
|
|
|
|
|
hsr_debugfs_term(hsr);
|
|
|
|
hsr_del_ports(hsr);
|
|
|
|
|
|
|
|
hsr_del_self_node(hsr);
|
|
|
|
hsr_del_nodes(&hsr->node_db);
|
|
|
|
|
|
|
|
unregister_netdevice_queue(dev, head);
|
|
|
|
}
|
|
|
|
|
2013-11-29 22:38:16 +00:00
|
|
|
static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
|
|
|
|
{
|
2020-02-28 18:01:56 +00:00
|
|
|
struct hsr_priv *hsr = netdev_priv(dev);
|
2020-07-22 14:40:16 +00:00
|
|
|
u8 proto = HSR_PROTOCOL_HSR;
|
2014-07-04 21:38:05 +00:00
|
|
|
struct hsr_port *port;
|
2013-11-29 22:38:16 +00:00
|
|
|
|
2014-07-04 21:38:05 +00:00
|
|
|
port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
|
2020-02-28 18:01:56 +00:00
|
|
|
if (port) {
|
|
|
|
if (nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex))
|
|
|
|
goto nla_put_failure;
|
|
|
|
}
|
2014-07-04 21:37:27 +00:00
|
|
|
|
2014-07-04 21:38:05 +00:00
|
|
|
port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
|
2020-02-28 18:01:56 +00:00
|
|
|
if (port) {
|
|
|
|
if (nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex))
|
|
|
|
goto nla_put_failure;
|
|
|
|
}
|
2013-11-29 22:38:16 +00:00
|
|
|
|
|
|
|
if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
|
2014-07-04 21:34:38 +00:00
|
|
|
hsr->sup_multicast_addr) ||
|
|
|
|
nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
|
2013-11-29 22:38:16 +00:00
|
|
|
goto nla_put_failure;
|
2020-07-22 14:40:16 +00:00
|
|
|
if (hsr->prot_version == PRP_V1)
|
|
|
|
proto = HSR_PROTOCOL_PRP;
|
|
|
|
if (nla_put_u8(skb, IFLA_HSR_PROTOCOL, proto))
|
|
|
|
goto nla_put_failure;
|
2013-11-29 22:38:16 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nla_put_failure:
|
|
|
|
return -EMSGSIZE;
|
|
|
|
}
|
|
|
|
|
2013-10-30 20:10:47 +00:00
|
|
|
static struct rtnl_link_ops hsr_link_ops __read_mostly = {
|
|
|
|
.kind = "hsr",
|
|
|
|
.maxtype = IFLA_HSR_MAX,
|
|
|
|
.policy = hsr_policy,
|
|
|
|
.priv_size = sizeof(struct hsr_priv),
|
|
|
|
.setup = hsr_dev_setup,
|
|
|
|
.newlink = hsr_newlink,
|
2020-06-21 13:46:25 +00:00
|
|
|
.dellink = hsr_dellink,
|
2013-11-29 22:38:16 +00:00
|
|
|
.fill_info = hsr_fill_info,
|
2013-10-30 20:10:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* attribute policy */
|
|
|
|
static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
|
2016-04-19 11:34:28 +00:00
|
|
|
[HSR_A_NODE_ADDR] = { .len = ETH_ALEN },
|
|
|
|
[HSR_A_NODE_ADDR_B] = { .len = ETH_ALEN },
|
2013-10-30 20:10:47 +00:00
|
|
|
[HSR_A_IFINDEX] = { .type = NLA_U32 },
|
|
|
|
[HSR_A_IF1_AGE] = { .type = NLA_U32 },
|
|
|
|
[HSR_A_IF2_AGE] = { .type = NLA_U32 },
|
|
|
|
[HSR_A_IF1_SEQ] = { .type = NLA_U16 },
|
|
|
|
[HSR_A_IF2_SEQ] = { .type = NLA_U16 },
|
|
|
|
};
|
|
|
|
|
2016-10-24 12:40:03 +00:00
|
|
|
static struct genl_family hsr_genl_family;
|
2013-10-30 20:10:47 +00:00
|
|
|
|
2013-11-19 14:19:39 +00:00
|
|
|
static const struct genl_multicast_group hsr_mcgrps[] = {
|
|
|
|
{ .name = "hsr-network", },
|
2013-10-30 20:10:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* This is called if for some node with MAC address addr, we only get frames
|
|
|
|
* over one of the slave interfaces. This would indicate an open network ring
|
|
|
|
* (i.e. a link has failed somewhere).
|
|
|
|
*/
|
2014-07-04 21:34:38 +00:00
|
|
|
void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
|
2014-07-04 21:38:05 +00:00
|
|
|
struct hsr_port *port)
|
2013-10-30 20:10:47 +00:00
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
|
|
|
void *msg_head;
|
2014-07-04 21:38:05 +00:00
|
|
|
struct hsr_port *master;
|
2013-10-30 20:10:47 +00:00
|
|
|
int res;
|
|
|
|
|
|
|
|
skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
|
|
|
|
if (!skb)
|
|
|
|
goto fail;
|
|
|
|
|
2019-04-05 17:31:23 +00:00
|
|
|
msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0,
|
|
|
|
HSR_C_RING_ERROR);
|
2013-10-30 20:10:47 +00:00
|
|
|
if (!msg_head)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
|
|
|
|
if (res < 0)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
2014-07-04 21:38:05 +00:00
|
|
|
res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
|
2013-10-30 20:10:47 +00:00
|
|
|
if (res < 0)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
genlmsg_end(skb, msg_head);
|
2013-11-19 14:19:39 +00:00
|
|
|
genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
|
2013-10-30 20:10:47 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
nla_put_failure:
|
|
|
|
kfree_skb(skb);
|
|
|
|
|
|
|
|
fail:
|
2014-07-04 21:38:05 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
|
|
|
|
netdev_warn(master->dev, "Could not send HSR ring error message\n");
|
|
|
|
rcu_read_unlock();
|
2013-10-30 20:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This is called when we haven't heard from the node with MAC address addr for
|
|
|
|
* some time (just before the node is removed from the node table/list).
|
|
|
|
*/
|
2014-07-04 21:34:38 +00:00
|
|
|
void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
|
2013-10-30 20:10:47 +00:00
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
|
|
|
void *msg_head;
|
2014-07-04 21:38:05 +00:00
|
|
|
struct hsr_port *master;
|
2013-10-30 20:10:47 +00:00
|
|
|
int res;
|
|
|
|
|
|
|
|
skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
|
|
|
|
if (!skb)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
|
|
|
|
if (!msg_head)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
|
|
|
|
if (res < 0)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
genlmsg_end(skb, msg_head);
|
2013-11-19 14:19:39 +00:00
|
|
|
genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
|
2013-10-30 20:10:47 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
nla_put_failure:
|
|
|
|
kfree_skb(skb);
|
|
|
|
|
|
|
|
fail:
|
2014-07-04 21:38:05 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
|
|
|
|
netdev_warn(master->dev, "Could not send HSR node down\n");
|
|
|
|
rcu_read_unlock();
|
2013-10-30 20:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
|
|
|
|
* about the status of a specific node in the network, defined by its MAC
|
|
|
|
* address.
|
|
|
|
*
|
|
|
|
* Input: hsr ifindex, node mac address
|
|
|
|
* Output: hsr ifindex, node mac address (copied from request),
|
|
|
|
* age of latest frame from node over slave 1, slave 2 [ms]
|
|
|
|
*/
|
|
|
|
static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
|
|
|
|
{
|
|
|
|
/* For receiving */
|
|
|
|
struct nlattr *na;
|
2014-07-04 21:38:05 +00:00
|
|
|
struct net_device *hsr_dev;
|
2013-10-30 20:10:47 +00:00
|
|
|
|
|
|
|
/* For sending */
|
|
|
|
struct sk_buff *skb_out;
|
|
|
|
void *msg_head;
|
2014-07-04 21:34:38 +00:00
|
|
|
struct hsr_priv *hsr;
|
2014-07-04 21:38:05 +00:00
|
|
|
struct hsr_port *port;
|
2013-10-30 20:10:47 +00:00
|
|
|
unsigned char hsr_node_addr_b[ETH_ALEN];
|
|
|
|
int hsr_node_if1_age;
|
|
|
|
u16 hsr_node_if1_seq;
|
|
|
|
int hsr_node_if2_age;
|
|
|
|
u16 hsr_node_if2_seq;
|
|
|
|
int addr_b_ifindex;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (!info)
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
na = info->attrs[HSR_A_IFINDEX];
|
|
|
|
if (!na)
|
|
|
|
goto invalid;
|
|
|
|
na = info->attrs[HSR_A_NODE_ADDR];
|
|
|
|
if (!na)
|
|
|
|
goto invalid;
|
|
|
|
|
2020-03-13 06:50:14 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
|
|
|
|
nla_get_u32(info->attrs[HSR_A_IFINDEX]));
|
2013-10-30 20:10:47 +00:00
|
|
|
if (!hsr_dev)
|
2020-03-13 06:50:14 +00:00
|
|
|
goto rcu_unlock;
|
2013-10-30 20:10:47 +00:00
|
|
|
if (!is_hsr_master(hsr_dev))
|
2020-03-13 06:50:14 +00:00
|
|
|
goto rcu_unlock;
|
2013-10-30 20:10:47 +00:00
|
|
|
|
|
|
|
/* Send reply */
|
2020-03-13 06:50:14 +00:00
|
|
|
skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
|
2013-10-30 20:10:47 +00:00
|
|
|
if (!skb_out) {
|
|
|
|
res = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
|
2019-04-05 17:31:26 +00:00
|
|
|
info->snd_seq, &hsr_genl_family, 0,
|
|
|
|
HSR_C_SET_NODE_STATUS);
|
2013-10-30 20:10:47 +00:00
|
|
|
if (!msg_head) {
|
|
|
|
res = -ENOMEM;
|
|
|
|
goto nla_put_failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
|
|
|
|
if (res < 0)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
2014-07-04 21:34:38 +00:00
|
|
|
hsr = netdev_priv(hsr_dev);
|
|
|
|
res = hsr_get_node_data(hsr,
|
2019-04-05 17:31:23 +00:00
|
|
|
(unsigned char *)
|
|
|
|
nla_data(info->attrs[HSR_A_NODE_ADDR]),
|
|
|
|
hsr_node_addr_b,
|
|
|
|
&addr_b_ifindex,
|
|
|
|
&hsr_node_if1_age,
|
|
|
|
&hsr_node_if1_seq,
|
|
|
|
&hsr_node_if2_age,
|
|
|
|
&hsr_node_if2_seq);
|
2013-10-30 20:10:47 +00:00
|
|
|
if (res < 0)
|
2013-11-14 19:12:54 +00:00
|
|
|
goto nla_put_failure;
|
2013-10-30 20:10:47 +00:00
|
|
|
|
|
|
|
res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
|
2019-04-05 17:31:26 +00:00
|
|
|
nla_data(info->attrs[HSR_A_NODE_ADDR]));
|
2013-10-30 20:10:47 +00:00
|
|
|
if (res < 0)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
if (addr_b_ifindex > -1) {
|
|
|
|
res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
|
2019-04-05 17:31:23 +00:00
|
|
|
hsr_node_addr_b);
|
2013-10-30 20:10:47 +00:00
|
|
|
if (res < 0)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
2019-04-05 17:31:23 +00:00
|
|
|
res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX,
|
|
|
|
addr_b_ifindex);
|
2013-10-30 20:10:47 +00:00
|
|
|
if (res < 0)
|
|
|
|
goto nla_put_failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
|
|
|
|
if (res < 0)
|
|
|
|
goto nla_put_failure;
|
|
|
|
res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
|
|
|
|
if (res < 0)
|
|
|
|
goto nla_put_failure;
|
2014-07-04 21:38:05 +00:00
|
|
|
port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
|
|
|
|
if (port)
|
|
|
|
res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
|
|
|
|
port->dev->ifindex);
|
2013-10-30 20:10:47 +00:00
|
|
|
if (res < 0)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
|
|
|
|
if (res < 0)
|
|
|
|
goto nla_put_failure;
|
|
|
|
res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
|
|
|
|
if (res < 0)
|
|
|
|
goto nla_put_failure;
|
2014-07-04 21:38:05 +00:00
|
|
|
port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
|
|
|
|
if (port)
|
|
|
|
res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
|
|
|
|
port->dev->ifindex);
|
2014-07-04 21:37:27 +00:00
|
|
|
if (res < 0)
|
|
|
|
goto nla_put_failure;
|
2013-10-30 20:10:47 +00:00
|
|
|
|
2020-03-13 06:50:14 +00:00
|
|
|
rcu_read_unlock();
|
|
|
|
|
2013-10-30 20:10:47 +00:00
|
|
|
genlmsg_end(skb_out, msg_head);
|
|
|
|
genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2020-03-13 06:50:14 +00:00
|
|
|
rcu_unlock:
|
|
|
|
rcu_read_unlock();
|
2013-10-30 20:10:47 +00:00
|
|
|
invalid:
|
2017-04-12 12:34:04 +00:00
|
|
|
netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
|
2013-10-30 20:10:47 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
nla_put_failure:
|
|
|
|
kfree_skb(skb_out);
|
|
|
|
/* Fall through */
|
|
|
|
|
|
|
|
fail:
|
2020-03-13 06:50:14 +00:00
|
|
|
rcu_read_unlock();
|
2013-10-30 20:10:47 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-07-04 21:41:03 +00:00
|
|
|
/* Get a list of MacAddressA of all nodes known to this node (including self).
|
2013-10-30 20:10:47 +00:00
|
|
|
*/
|
|
|
|
static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
|
|
|
|
{
|
2020-03-13 06:50:24 +00:00
|
|
|
unsigned char addr[ETH_ALEN];
|
2013-10-30 20:10:47 +00:00
|
|
|
struct net_device *hsr_dev;
|
|
|
|
struct sk_buff *skb_out;
|
2014-07-04 21:34:38 +00:00
|
|
|
struct hsr_priv *hsr;
|
2020-03-13 06:50:24 +00:00
|
|
|
bool restart = false;
|
|
|
|
struct nlattr *na;
|
|
|
|
void *pos = NULL;
|
|
|
|
void *msg_head;
|
2013-10-30 20:10:47 +00:00
|
|
|
int res;
|
|
|
|
|
|
|
|
if (!info)
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
na = info->attrs[HSR_A_IFINDEX];
|
|
|
|
if (!na)
|
|
|
|
goto invalid;
|
|
|
|
|
2020-03-13 06:50:14 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
hsr_dev = dev_get_by_index_rcu(genl_info_net(info),
|
|
|
|
nla_get_u32(info->attrs[HSR_A_IFINDEX]));
|
2013-10-30 20:10:47 +00:00
|
|
|
if (!hsr_dev)
|
2020-03-13 06:50:14 +00:00
|
|
|
goto rcu_unlock;
|
2013-10-30 20:10:47 +00:00
|
|
|
if (!is_hsr_master(hsr_dev))
|
2020-03-13 06:50:14 +00:00
|
|
|
goto rcu_unlock;
|
2013-10-30 20:10:47 +00:00
|
|
|
|
2020-03-13 06:50:24 +00:00
|
|
|
restart:
|
2013-10-30 20:10:47 +00:00
|
|
|
/* Send reply */
|
2020-03-13 06:50:24 +00:00
|
|
|
skb_out = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
|
2013-10-30 20:10:47 +00:00
|
|
|
if (!skb_out) {
|
|
|
|
res = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
|
2019-04-05 17:31:26 +00:00
|
|
|
info->snd_seq, &hsr_genl_family, 0,
|
|
|
|
HSR_C_SET_NODE_LIST);
|
2013-10-30 20:10:47 +00:00
|
|
|
if (!msg_head) {
|
|
|
|
res = -ENOMEM;
|
|
|
|
goto nla_put_failure;
|
|
|
|
}
|
|
|
|
|
2020-03-13 06:50:24 +00:00
|
|
|
if (!restart) {
|
|
|
|
res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
|
|
|
|
if (res < 0)
|
|
|
|
goto nla_put_failure;
|
|
|
|
}
|
2013-10-30 20:10:47 +00:00
|
|
|
|
2014-07-04 21:34:38 +00:00
|
|
|
hsr = netdev_priv(hsr_dev);
|
2013-10-30 20:10:47 +00:00
|
|
|
|
2020-03-13 06:50:24 +00:00
|
|
|
if (!pos)
|
|
|
|
pos = hsr_get_next_node(hsr, NULL, addr);
|
2013-10-30 20:10:47 +00:00
|
|
|
while (pos) {
|
|
|
|
res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
|
|
|
|
if (res < 0) {
|
2020-03-13 06:50:24 +00:00
|
|
|
if (res == -EMSGSIZE) {
|
|
|
|
genlmsg_end(skb_out, msg_head);
|
|
|
|
genlmsg_unicast(genl_info_net(info), skb_out,
|
|
|
|
info->snd_portid);
|
|
|
|
restart = true;
|
|
|
|
goto restart;
|
|
|
|
}
|
2013-10-30 20:10:47 +00:00
|
|
|
goto nla_put_failure;
|
|
|
|
}
|
2014-07-04 21:34:38 +00:00
|
|
|
pos = hsr_get_next_node(hsr, pos, addr);
|
2013-10-30 20:10:47 +00:00
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
genlmsg_end(skb_out, msg_head);
|
|
|
|
genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2020-03-13 06:50:14 +00:00
|
|
|
rcu_unlock:
|
|
|
|
rcu_read_unlock();
|
2013-10-30 20:10:47 +00:00
|
|
|
invalid:
|
2017-04-12 12:34:04 +00:00
|
|
|
netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
|
2013-10-30 20:10:47 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
nla_put_failure:
|
2020-03-13 06:50:24 +00:00
|
|
|
nlmsg_free(skb_out);
|
2013-10-30 20:10:47 +00:00
|
|
|
/* Fall through */
|
|
|
|
|
|
|
|
fail:
|
2020-03-13 06:50:14 +00:00
|
|
|
rcu_read_unlock();
|
2013-10-30 20:10:47 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-10-02 21:49:54 +00:00
|
|
|
static const struct genl_small_ops hsr_ops[] = {
|
2013-11-14 16:14:40 +00:00
|
|
|
{
|
|
|
|
.cmd = HSR_C_GET_NODE_STATUS,
|
2019-04-26 12:07:31 +00:00
|
|
|
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
|
2013-11-14 16:14:40 +00:00
|
|
|
.flags = 0,
|
|
|
|
.doit = hsr_get_node_status,
|
|
|
|
.dumpit = NULL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.cmd = HSR_C_GET_NODE_LIST,
|
2019-04-26 12:07:31 +00:00
|
|
|
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
|
2013-11-14 16:14:40 +00:00
|
|
|
.flags = 0,
|
|
|
|
.doit = hsr_get_node_list,
|
|
|
|
.dumpit = NULL,
|
|
|
|
},
|
2013-10-30 20:10:47 +00:00
|
|
|
};
|
|
|
|
|
2016-10-24 12:40:05 +00:00
|
|
|
static struct genl_family hsr_genl_family __ro_after_init = {
|
2016-10-24 12:40:03 +00:00
|
|
|
.hdrsize = 0,
|
|
|
|
.name = "HSR",
|
|
|
|
.version = 1,
|
|
|
|
.maxattr = HSR_A_MAX,
|
genetlink: make policy common to family
Since maxattr is common, the policy can't really differ sanely,
so make it common as well.
The only user that did in fact manage to make a non-common policy
is taskstats, which has to be really careful about it (since it's
still using a common maxattr!). This is no longer supported, but
we can fake it using pre_doit.
This reduces the size of e.g. nl80211.o (which has lots of commands):
text data bss dec hex filename
398745 14323 2240 415308 6564c net/wireless/nl80211.o (before)
397913 14331 2240 414484 65314 net/wireless/nl80211.o (after)
--------------------------------
-832 +8 0 -824
Which is obviously just 8 bytes for each command, and an added 8
bytes for the new policy pointer. I'm not sure why the ops list is
counted as .text though.
Most of the code transformations were done using the following spatch:
@ops@
identifier OPS;
expression POLICY;
@@
struct genl_ops OPS[] = {
...,
{
- .policy = POLICY,
},
...
};
@@
identifier ops.OPS;
expression ops.POLICY;
identifier fam;
expression M;
@@
struct genl_family fam = {
.ops = OPS,
.maxattr = M,
+ .policy = POLICY,
...
};
This also gets rid of devlink_nl_cmd_region_read_dumpit() accessing
the cb->data as ops, which we want to change in a later genl patch.
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-03-21 21:51:02 +00:00
|
|
|
.policy = hsr_genl_policy,
|
2020-03-13 06:50:33 +00:00
|
|
|
.netnsok = true,
|
2016-10-24 12:40:03 +00:00
|
|
|
.module = THIS_MODULE,
|
2020-10-02 21:49:54 +00:00
|
|
|
.small_ops = hsr_ops,
|
|
|
|
.n_small_ops = ARRAY_SIZE(hsr_ops),
|
2016-10-24 12:40:03 +00:00
|
|
|
.mcgrps = hsr_mcgrps,
|
|
|
|
.n_mcgrps = ARRAY_SIZE(hsr_mcgrps),
|
|
|
|
};
|
|
|
|
|
2013-10-30 20:10:47 +00:00
|
|
|
int __init hsr_netlink_init(void)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = rtnl_link_register(&hsr_link_ops);
|
|
|
|
if (rc)
|
|
|
|
goto fail_rtnl_link_register;
|
|
|
|
|
2016-10-24 12:40:03 +00:00
|
|
|
rc = genl_register_family(&hsr_genl_family);
|
2013-10-30 20:10:47 +00:00
|
|
|
if (rc)
|
|
|
|
goto fail_genl_register_family;
|
|
|
|
|
2019-12-22 11:26:27 +00:00
|
|
|
hsr_debugfs_create_root();
|
2013-10-30 20:10:47 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail_genl_register_family:
|
|
|
|
rtnl_link_unregister(&hsr_link_ops);
|
|
|
|
fail_rtnl_link_register:
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __exit hsr_netlink_exit(void)
|
|
|
|
{
|
|
|
|
genl_unregister_family(&hsr_genl_family);
|
|
|
|
rtnl_link_unregister(&hsr_link_ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
MODULE_ALIAS_RTNL_LINK("hsr");
|