hsr_netlink.c 11.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
/* Copyright 2011-2014 Autronica Fire and Security AS
3 4
 *
 * Author(s):
5
 *	2011-2014 Arvid Brodin, arvid.brodin@alten.se
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * Routines for handling Netlink messages for HSR.
 */

#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 },
P
Peter Heise 已提交
22
	[IFLA_HSR_VERSION]	= { .type = NLA_U8 },
P
Peter Heise 已提交
23
	[IFLA_HSR_SUPERVISION_ADDR]	= { .len = ETH_ALEN },
24
	[IFLA_HSR_SEQ_NR]		= { .type = NLA_U16 },
25 26 27 28 29 30
};

/* 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,
31 32
		       struct nlattr *tb[], struct nlattr *data[],
		       struct netlink_ext_ack *extack)
33 34
{
	struct net_device *link[2];
P
Peter Heise 已提交
35
	unsigned char multicast_spec, hsr_version;
36

37 38 39 40
	if (!data) {
		netdev_info(dev, "HSR: No slave devices specified\n");
		return -EINVAL;
	}
41
	if (!data[IFLA_HSR_SLAVE1]) {
42
		netdev_info(dev, "HSR: Slave1 device not specified\n");
43 44
		return -EINVAL;
	}
45 46
	link[0] = __dev_get_by_index(src_net,
				     nla_get_u32(data[IFLA_HSR_SLAVE1]));
47
	if (!data[IFLA_HSR_SLAVE2]) {
48
		netdev_info(dev, "HSR: Slave2 device not specified\n");
49 50
		return -EINVAL;
	}
51 52
	link[1] = __dev_get_by_index(src_net,
				     nla_get_u32(data[IFLA_HSR_SLAVE2]));
53 54 55 56 57 58 59 60 61 62 63

	if (!link[0] || !link[1])
		return -ENODEV;
	if (link[0] == link[1])
		return -EINVAL;

	if (!data[IFLA_HSR_MULTICAST_SPEC])
		multicast_spec = 0;
	else
		multicast_spec = nla_get_u8(data[IFLA_HSR_MULTICAST_SPEC]);

P
Peter Heise 已提交
64 65 66 67 68 69
	if (!data[IFLA_HSR_VERSION])
		hsr_version = 0;
	else
		hsr_version = nla_get_u8(data[IFLA_HSR_VERSION]);

	return hsr_dev_finalize(dev, link, multicast_spec, hsr_version);
70 71
}

72 73
static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
{
74
	struct hsr_priv *hsr;
75
	struct hsr_port *port;
76
	int res;
77

78
	hsr = netdev_priv(dev);
79

80
	res = 0;
81

82
	rcu_read_lock();
83 84 85
	port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
	if (port)
		res = nla_put_u32(skb, IFLA_HSR_SLAVE1, port->dev->ifindex);
86 87 88 89 90
	rcu_read_unlock();
	if (res)
		goto nla_put_failure;

	rcu_read_lock();
91 92 93
	port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
	if (port)
		res = nla_put_u32(skb, IFLA_HSR_SLAVE2, port->dev->ifindex);
94 95 96
	rcu_read_unlock();
	if (res)
		goto nla_put_failure;
97 98

	if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
99 100
		    hsr->sup_multicast_addr) ||
	    nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr->sequence_nr))
101 102 103 104 105 106 107 108
		goto nla_put_failure;

	return 0;

nla_put_failure:
	return -EMSGSIZE;
}

109 110 111 112 113 114 115
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,
116
	.fill_info	= hsr_fill_info,
117 118 119 120
};

/* attribute policy */
static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
P
Peter Heise 已提交
121 122
	[HSR_A_NODE_ADDR] = { .len = ETH_ALEN },
	[HSR_A_NODE_ADDR_B] = { .len = ETH_ALEN },
123 124 125 126 127 128 129
	[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 },
};

130
static struct genl_family hsr_genl_family;
131

132 133
static const struct genl_multicast_group hsr_mcgrps[] = {
	{ .name = "hsr-network", },
134 135 136 137 138 139
};

/* 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).
 */
140
void hsr_nl_ringerror(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN],
141
		      struct hsr_port *port)
142 143 144
{
	struct sk_buff *skb;
	void *msg_head;
145
	struct hsr_port *master;
146 147 148 149 150 151
	int res;

	skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
	if (!skb)
		goto fail;

152 153
	msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0,
			       HSR_C_RING_ERROR);
154 155 156 157 158 159 160
	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;

161
	res = nla_put_u32(skb, HSR_A_IFINDEX, port->dev->ifindex);
162 163 164 165
	if (res < 0)
		goto nla_put_failure;

	genlmsg_end(skb, msg_head);
166
	genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
167 168 169 170 171 172 173

	return;

nla_put_failure:
	kfree_skb(skb);

fail:
174 175 176 177
	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();
178 179 180 181 182
}

/* 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).
 */
183
void hsr_nl_nodedown(struct hsr_priv *hsr, unsigned char addr[ETH_ALEN])
184 185 186
{
	struct sk_buff *skb;
	void *msg_head;
187
	struct hsr_port *master;
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
	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);
203
	genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
204 205 206 207 208 209 210

	return;

nla_put_failure:
	kfree_skb(skb);

fail:
211 212 213 214
	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();
215 216 217 218 219 220 221 222 223 224 225 226 227 228
}

/* 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;
229
	struct net_device *hsr_dev;
230 231 232 233

	/* For sending */
	struct sk_buff *skb_out;
	void *msg_head;
234
	struct hsr_priv *hsr;
235
	struct hsr_port *port;
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	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;

	hsr_dev = __dev_get_by_index(genl_info_net(info),
255
				     nla_get_u32(info->attrs[HSR_A_IFINDEX]));
256 257 258 259 260 261 262 263 264 265 266 267 268
	if (!hsr_dev)
		goto invalid;
	if (!is_hsr_master(hsr_dev))
		goto invalid;

	/* Send reply */
	skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
	if (!skb_out) {
		res = -ENOMEM;
		goto fail;
	}

	msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
269 270
			       info->snd_seq, &hsr_genl_family, 0,
			       HSR_C_SET_NODE_STATUS);
271 272 273 274 275 276 277 278 279
	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;

280 281
	hsr = netdev_priv(hsr_dev);
	res = hsr_get_node_data(hsr,
282 283 284 285 286 287 288 289
				(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);
290
	if (res < 0)
291
		goto nla_put_failure;
292 293

	res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
294
		      nla_data(info->attrs[HSR_A_NODE_ADDR]));
295 296 297 298 299
	if (res < 0)
		goto nla_put_failure;

	if (addr_b_ifindex > -1) {
		res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
300
			      hsr_node_addr_b);
301 302 303
		if (res < 0)
			goto nla_put_failure;

304 305
		res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX,
				  addr_b_ifindex);
306 307 308 309 310 311 312 313 314 315
		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;
316
	rcu_read_lock();
317 318 319 320
	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);
321
	rcu_read_unlock();
322 323 324 325 326 327 328 329 330
	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;
331
	rcu_read_lock();
332 333 334 335
	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);
336 337 338
	rcu_read_unlock();
	if (res < 0)
		goto nla_put_failure;
339 340 341 342 343 344 345

	genlmsg_end(skb_out, msg_head);
	genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);

	return 0;

invalid:
J
Johannes Berg 已提交
346
	netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
347 348 349 350 351 352 353 354 355 356
	return 0;

nla_put_failure:
	kfree_skb(skb_out);
	/* Fall through */

fail:
	return res;
}

A
Arvid Brodin 已提交
357
/* Get a list of MacAddressA of all nodes known to this node (including self).
358 359 360 361 362 363 364 365 366 367
 */
static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
{
	/* For receiving */
	struct nlattr *na;
	struct net_device *hsr_dev;

	/* For sending */
	struct sk_buff *skb_out;
	void *msg_head;
368
	struct hsr_priv *hsr;
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
	void *pos;
	unsigned char addr[ETH_ALEN];
	int res;

	if (!info)
		goto invalid;

	na = info->attrs[HSR_A_IFINDEX];
	if (!na)
		goto invalid;

	hsr_dev = __dev_get_by_index(genl_info_net(info),
				     nla_get_u32(info->attrs[HSR_A_IFINDEX]));
	if (!hsr_dev)
		goto invalid;
	if (!is_hsr_master(hsr_dev))
		goto invalid;

	/* Send reply */
	skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
	if (!skb_out) {
		res = -ENOMEM;
		goto fail;
	}

	msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
395 396
			       info->snd_seq, &hsr_genl_family, 0,
			       HSR_C_SET_NODE_LIST);
397 398 399 400 401 402 403 404 405
	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;

406
	hsr = netdev_priv(hsr_dev);
407 408

	rcu_read_lock();
409
	pos = hsr_get_next_node(hsr, NULL, addr);
410 411 412 413 414 415
	while (pos) {
		res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
		if (res < 0) {
			rcu_read_unlock();
			goto nla_put_failure;
		}
416
		pos = hsr_get_next_node(hsr, pos, addr);
417 418 419 420 421 422 423 424 425
	}
	rcu_read_unlock();

	genlmsg_end(skb_out, msg_head);
	genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);

	return 0;

invalid:
J
Johannes Berg 已提交
426
	netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL, NULL);
427 428 429 430 431 432 433 434 435 436
	return 0;

nla_put_failure:
	kfree_skb(skb_out);
	/* Fall through */

fail:
	return res;
}

437
static const struct genl_ops hsr_ops[] = {
438 439
	{
		.cmd = HSR_C_GET_NODE_STATUS,
440
		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
441 442 443 444 445 446
		.flags = 0,
		.doit = hsr_get_node_status,
		.dumpit = NULL,
	},
	{
		.cmd = HSR_C_GET_NODE_LIST,
447
		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
448 449 450 451
		.flags = 0,
		.doit = hsr_get_node_list,
		.dumpit = NULL,
	},
452 453
};

454
static struct genl_family hsr_genl_family __ro_after_init = {
455 456 457 458
	.hdrsize = 0,
	.name = "HSR",
	.version = 1,
	.maxattr = HSR_A_MAX,
459
	.policy = hsr_genl_policy,
460 461 462 463 464 465 466
	.module = THIS_MODULE,
	.ops = hsr_ops,
	.n_ops = ARRAY_SIZE(hsr_ops),
	.mcgrps = hsr_mcgrps,
	.n_mcgrps = ARRAY_SIZE(hsr_mcgrps),
};

467 468 469 470 471 472 473 474
int __init hsr_netlink_init(void)
{
	int rc;

	rc = rtnl_link_register(&hsr_link_ops);
	if (rc)
		goto fail_rtnl_link_register;

475
	rc = genl_register_family(&hsr_genl_family);
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
	if (rc)
		goto fail_genl_register_family;

	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");