xref: /linux/net/hsr/hsr_device.c (revision 8f5b5f78113e881cb8570c961b0dc42b218a1b9e)
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  * This file contains device methods for creating, using and destroying
7  * virtual HSR or PRP devices.
8  */
9 
10 #include <linux/netdevice.h>
11 #include <linux/skbuff.h>
12 #include <linux/etherdevice.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/pkt_sched.h>
15 #include "hsr_device.h"
16 #include "hsr_slave.h"
17 #include "hsr_framereg.h"
18 #include "hsr_main.h"
19 #include "hsr_forward.h"
20 
21 static bool is_admin_up(struct net_device *dev)
22 {
23 	return dev && (dev->flags & IFF_UP);
24 }
25 
26 static bool is_slave_up(struct net_device *dev)
27 {
28 	return dev && is_admin_up(dev) && netif_oper_up(dev);
29 }
30 
31 static void hsr_set_operstate(struct hsr_port *master, bool has_carrier)
32 {
33 	struct net_device *dev = master->dev;
34 
35 	if (!is_admin_up(dev)) {
36 		netdev_set_operstate(dev, IF_OPER_DOWN);
37 		return;
38 	}
39 
40 	if (has_carrier)
41 		netdev_set_operstate(dev, IF_OPER_UP);
42 	else
43 		netdev_set_operstate(dev, IF_OPER_LOWERLAYERDOWN);
44 }
45 
46 static bool hsr_check_carrier(struct hsr_port *master)
47 {
48 	struct hsr_port *port;
49 
50 	ASSERT_RTNL();
51 
52 	hsr_for_each_port(master->hsr, port) {
53 		if (port->type != HSR_PT_MASTER && is_slave_up(port->dev)) {
54 			netif_carrier_on(master->dev);
55 			return true;
56 		}
57 	}
58 
59 	netif_carrier_off(master->dev);
60 
61 	return false;
62 }
63 
64 static void hsr_check_announce(struct net_device *hsr_dev)
65 {
66 	struct hsr_priv *hsr;
67 
68 	hsr = netdev_priv(hsr_dev);
69 	if (netif_running(hsr_dev) && netif_oper_up(hsr_dev)) {
70 		/* Enable announce timer and start sending supervisory frames */
71 		if (!timer_pending(&hsr->announce_timer)) {
72 			hsr->announce_count = 0;
73 			mod_timer(&hsr->announce_timer, jiffies +
74 				  msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL));
75 		}
76 	} else {
77 		/* Deactivate the announce timer  */
78 		timer_delete(&hsr->announce_timer);
79 	}
80 }
81 
82 void hsr_check_carrier_and_operstate(struct hsr_priv *hsr)
83 {
84 	struct hsr_port *master;
85 	bool has_carrier;
86 
87 	master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
88 	/* netif_stacked_transfer_operstate() cannot be used here since
89 	 * it doesn't set IF_OPER_LOWERLAYERDOWN (?)
90 	 */
91 	has_carrier = hsr_check_carrier(master);
92 	hsr_set_operstate(master, has_carrier);
93 	hsr_check_announce(master->dev);
94 }
95 
96 int hsr_get_max_mtu(struct hsr_priv *hsr)
97 {
98 	unsigned int mtu_max;
99 	struct hsr_port *port;
100 
101 	mtu_max = ETH_DATA_LEN;
102 	hsr_for_each_port(hsr, port)
103 		if (port->type != HSR_PT_MASTER)
104 			mtu_max = min(port->dev->mtu, mtu_max);
105 
106 	if (mtu_max < HSR_HLEN)
107 		return 0;
108 	return mtu_max - HSR_HLEN;
109 }
110 
111 static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu)
112 {
113 	struct hsr_priv *hsr;
114 
115 	hsr = netdev_priv(dev);
116 
117 	if (new_mtu > hsr_get_max_mtu(hsr)) {
118 		netdev_info(dev, "A HSR master's MTU cannot be greater than the smallest MTU of its slaves minus the HSR Tag length (%d octets).\n",
119 			    HSR_HLEN);
120 		return -EINVAL;
121 	}
122 
123 	dev->mtu = new_mtu;
124 
125 	return 0;
126 }
127 
128 static int hsr_dev_open(struct net_device *dev)
129 {
130 	struct hsr_priv *hsr;
131 	struct hsr_port *port;
132 	const char *designation = NULL;
133 
134 	hsr = netdev_priv(dev);
135 
136 	hsr_for_each_port(hsr, port) {
137 		if (port->type == HSR_PT_MASTER)
138 			continue;
139 		switch (port->type) {
140 		case HSR_PT_SLAVE_A:
141 			designation = "Slave A";
142 			break;
143 		case HSR_PT_SLAVE_B:
144 			designation = "Slave B";
145 			break;
146 		default:
147 			designation = "Unknown";
148 		}
149 		if (!is_slave_up(port->dev))
150 			netdev_warn(dev, "%s (%s) is not up; please bring it up to get a fully working HSR network\n",
151 				    designation, port->dev->name);
152 	}
153 
154 	if (!designation)
155 		netdev_warn(dev, "No slave devices configured\n");
156 
157 	return 0;
158 }
159 
160 static int hsr_dev_close(struct net_device *dev)
161 {
162 	struct hsr_port *port;
163 	struct hsr_priv *hsr;
164 
165 	hsr = netdev_priv(dev);
166 	hsr_for_each_port(hsr, port) {
167 		if (port->type == HSR_PT_MASTER)
168 			continue;
169 		switch (port->type) {
170 		case HSR_PT_SLAVE_A:
171 		case HSR_PT_SLAVE_B:
172 			dev_uc_unsync(port->dev, dev);
173 			dev_mc_unsync(port->dev, dev);
174 			break;
175 		default:
176 			break;
177 		}
178 	}
179 
180 	return 0;
181 }
182 
183 static netdev_features_t hsr_features_recompute(struct hsr_priv *hsr,
184 						netdev_features_t features)
185 {
186 	netdev_features_t mask;
187 	struct hsr_port *port;
188 
189 	mask = features;
190 
191 	/* Mask out all features that, if supported by one device, should be
192 	 * enabled for all devices (see NETIF_F_ONE_FOR_ALL).
193 	 *
194 	 * Anything that's off in mask will not be enabled - so only things
195 	 * that were in features originally, and also is in NETIF_F_ONE_FOR_ALL,
196 	 * may become enabled.
197 	 */
198 	features &= ~NETIF_F_ONE_FOR_ALL;
199 	hsr_for_each_port(hsr, port)
200 		features = netdev_increment_features(features,
201 						     port->dev->features,
202 						     mask);
203 
204 	return features;
205 }
206 
207 static netdev_features_t hsr_fix_features(struct net_device *dev,
208 					  netdev_features_t features)
209 {
210 	struct hsr_priv *hsr = netdev_priv(dev);
211 
212 	return hsr_features_recompute(hsr, features);
213 }
214 
215 static netdev_tx_t hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
216 {
217 	struct hsr_priv *hsr = netdev_priv(dev);
218 	struct hsr_port *master;
219 
220 	master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
221 	if (master) {
222 		skb->dev = master->dev;
223 		skb_reset_mac_header(skb);
224 		skb_reset_mac_len(skb);
225 		spin_lock_bh(&hsr->seqnr_lock);
226 		hsr_forward_skb(skb, master);
227 		spin_unlock_bh(&hsr->seqnr_lock);
228 	} else {
229 		dev_core_stats_tx_dropped_inc(dev);
230 		dev_kfree_skb_any(skb);
231 	}
232 	return NETDEV_TX_OK;
233 }
234 
235 static const struct header_ops hsr_header_ops = {
236 	.create	 = eth_header,
237 	.parse	 = eth_header_parse,
238 };
239 
240 static struct sk_buff *hsr_init_skb(struct hsr_port *master)
241 {
242 	struct hsr_priv *hsr = master->hsr;
243 	struct sk_buff *skb;
244 	int hlen, tlen;
245 
246 	hlen = LL_RESERVED_SPACE(master->dev);
247 	tlen = master->dev->needed_tailroom;
248 	/* skb size is same for PRP/HSR frames, only difference
249 	 * being, for PRP it is a trailer and for HSR it is a
250 	 * header
251 	 */
252 	skb = dev_alloc_skb(sizeof(struct hsr_sup_tag) +
253 			    sizeof(struct hsr_sup_payload) + hlen + tlen);
254 
255 	if (!skb)
256 		return skb;
257 
258 	skb_reserve(skb, hlen);
259 	skb->dev = master->dev;
260 	skb->priority = TC_PRIO_CONTROL;
261 
262 	if (dev_hard_header(skb, skb->dev, ETH_P_PRP,
263 			    hsr->sup_multicast_addr,
264 			    skb->dev->dev_addr, skb->len) <= 0)
265 		goto out;
266 
267 	skb_reset_mac_header(skb);
268 	skb_reset_mac_len(skb);
269 	skb_reset_network_header(skb);
270 	skb_reset_transport_header(skb);
271 
272 	return skb;
273 out:
274 	kfree_skb(skb);
275 
276 	return NULL;
277 }
278 
279 static void send_hsr_supervision_frame(struct hsr_port *master,
280 				       unsigned long *interval)
281 {
282 	struct hsr_priv *hsr = master->hsr;
283 	__u8 type = HSR_TLV_LIFE_CHECK;
284 	struct hsr_sup_payload *hsr_sp;
285 	struct hsr_sup_tag *hsr_stag;
286 	struct sk_buff *skb;
287 
288 	*interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
289 	if (hsr->announce_count < 3 && hsr->prot_version == 0) {
290 		type = HSR_TLV_ANNOUNCE;
291 		*interval = msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
292 		hsr->announce_count++;
293 	}
294 
295 	skb = hsr_init_skb(master);
296 	if (!skb) {
297 		netdev_warn_once(master->dev, "HSR: Could not send supervision frame\n");
298 		return;
299 	}
300 
301 	hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
302 	set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf));
303 	set_hsr_stag_HSR_ver(hsr_stag, hsr->prot_version);
304 
305 	/* From HSRv1 on we have separate supervision sequence numbers. */
306 	spin_lock_bh(&hsr->seqnr_lock);
307 	if (hsr->prot_version > 0) {
308 		hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr);
309 		hsr->sup_sequence_nr++;
310 	} else {
311 		hsr_stag->sequence_nr = htons(hsr->sequence_nr);
312 		hsr->sequence_nr++;
313 	}
314 
315 	hsr_stag->tlv.HSR_TLV_type = type;
316 	/* TODO: Why 12 in HSRv0? */
317 	hsr_stag->tlv.HSR_TLV_length = hsr->prot_version ?
318 				sizeof(struct hsr_sup_payload) : 12;
319 
320 	/* Payload: MacAddressA */
321 	hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
322 	ether_addr_copy(hsr_sp->macaddress_A, master->dev->dev_addr);
323 
324 	if (skb_put_padto(skb, ETH_ZLEN)) {
325 		spin_unlock_bh(&hsr->seqnr_lock);
326 		return;
327 	}
328 
329 	hsr_forward_skb(skb, master);
330 	spin_unlock_bh(&hsr->seqnr_lock);
331 	return;
332 }
333 
334 static void send_prp_supervision_frame(struct hsr_port *master,
335 				       unsigned long *interval)
336 {
337 	struct hsr_priv *hsr = master->hsr;
338 	struct hsr_sup_payload *hsr_sp;
339 	struct hsr_sup_tag *hsr_stag;
340 	struct sk_buff *skb;
341 
342 	skb = hsr_init_skb(master);
343 	if (!skb) {
344 		netdev_warn_once(master->dev, "PRP: Could not send supervision frame\n");
345 		return;
346 	}
347 
348 	*interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
349 	hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
350 	set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf));
351 	set_hsr_stag_HSR_ver(hsr_stag, (hsr->prot_version ? 1 : 0));
352 
353 	/* From HSRv1 on we have separate supervision sequence numbers. */
354 	spin_lock_bh(&hsr->seqnr_lock);
355 	hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr);
356 	hsr->sup_sequence_nr++;
357 	hsr_stag->tlv.HSR_TLV_type = PRP_TLV_LIFE_CHECK_DD;
358 	hsr_stag->tlv.HSR_TLV_length = sizeof(struct hsr_sup_payload);
359 
360 	/* Payload: MacAddressA */
361 	hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
362 	ether_addr_copy(hsr_sp->macaddress_A, master->dev->dev_addr);
363 
364 	if (skb_put_padto(skb, ETH_ZLEN)) {
365 		spin_unlock_bh(&hsr->seqnr_lock);
366 		return;
367 	}
368 
369 	hsr_forward_skb(skb, master);
370 	spin_unlock_bh(&hsr->seqnr_lock);
371 }
372 
373 /* Announce (supervision frame) timer function
374  */
375 static void hsr_announce(struct timer_list *t)
376 {
377 	struct hsr_priv *hsr;
378 	struct hsr_port *master;
379 	unsigned long interval;
380 
381 	hsr = from_timer(hsr, t, announce_timer);
382 
383 	rcu_read_lock();
384 	master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
385 	hsr->proto_ops->send_sv_frame(master, &interval);
386 
387 	if (is_admin_up(master->dev))
388 		mod_timer(&hsr->announce_timer, jiffies + interval);
389 
390 	rcu_read_unlock();
391 }
392 
393 void hsr_del_ports(struct hsr_priv *hsr)
394 {
395 	struct hsr_port *port;
396 
397 	port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
398 	if (port)
399 		hsr_del_port(port);
400 
401 	port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
402 	if (port)
403 		hsr_del_port(port);
404 
405 	port = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
406 	if (port)
407 		hsr_del_port(port);
408 }
409 
410 static void hsr_set_rx_mode(struct net_device *dev)
411 {
412 	struct hsr_port *port;
413 	struct hsr_priv *hsr;
414 
415 	hsr = netdev_priv(dev);
416 
417 	hsr_for_each_port(hsr, port) {
418 		if (port->type == HSR_PT_MASTER)
419 			continue;
420 		switch (port->type) {
421 		case HSR_PT_SLAVE_A:
422 		case HSR_PT_SLAVE_B:
423 			dev_mc_sync_multiple(port->dev, dev);
424 			dev_uc_sync_multiple(port->dev, dev);
425 			break;
426 		default:
427 			break;
428 		}
429 	}
430 }
431 
432 static void hsr_change_rx_flags(struct net_device *dev, int change)
433 {
434 	struct hsr_port *port;
435 	struct hsr_priv *hsr;
436 
437 	hsr = netdev_priv(dev);
438 
439 	hsr_for_each_port(hsr, port) {
440 		if (port->type == HSR_PT_MASTER)
441 			continue;
442 		switch (port->type) {
443 		case HSR_PT_SLAVE_A:
444 		case HSR_PT_SLAVE_B:
445 			if (change & IFF_ALLMULTI)
446 				dev_set_allmulti(port->dev,
447 						 dev->flags &
448 						 IFF_ALLMULTI ? 1 : -1);
449 			break;
450 		default:
451 			break;
452 		}
453 	}
454 }
455 
456 static const struct net_device_ops hsr_device_ops = {
457 	.ndo_change_mtu = hsr_dev_change_mtu,
458 	.ndo_open = hsr_dev_open,
459 	.ndo_stop = hsr_dev_close,
460 	.ndo_start_xmit = hsr_dev_xmit,
461 	.ndo_change_rx_flags = hsr_change_rx_flags,
462 	.ndo_fix_features = hsr_fix_features,
463 	.ndo_set_rx_mode = hsr_set_rx_mode,
464 };
465 
466 static const struct device_type hsr_type = {
467 	.name = "hsr",
468 };
469 
470 static struct hsr_proto_ops hsr_ops = {
471 	.send_sv_frame = send_hsr_supervision_frame,
472 	.create_tagged_frame = hsr_create_tagged_frame,
473 	.get_untagged_frame = hsr_get_untagged_frame,
474 	.drop_frame = hsr_drop_frame,
475 	.fill_frame_info = hsr_fill_frame_info,
476 	.invalid_dan_ingress_frame = hsr_invalid_dan_ingress_frame,
477 };
478 
479 static struct hsr_proto_ops prp_ops = {
480 	.send_sv_frame = send_prp_supervision_frame,
481 	.create_tagged_frame = prp_create_tagged_frame,
482 	.get_untagged_frame = prp_get_untagged_frame,
483 	.drop_frame = prp_drop_frame,
484 	.fill_frame_info = prp_fill_frame_info,
485 	.handle_san_frame = prp_handle_san_frame,
486 	.update_san_info = prp_update_san_info,
487 };
488 
489 void hsr_dev_setup(struct net_device *dev)
490 {
491 	eth_hw_addr_random(dev);
492 
493 	ether_setup(dev);
494 	dev->min_mtu = 0;
495 	dev->header_ops = &hsr_header_ops;
496 	dev->netdev_ops = &hsr_device_ops;
497 	SET_NETDEV_DEVTYPE(dev, &hsr_type);
498 	dev->priv_flags |= IFF_NO_QUEUE | IFF_DISABLE_NETPOLL;
499 
500 	dev->needs_free_netdev = true;
501 
502 	dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
503 			   NETIF_F_GSO_MASK | NETIF_F_HW_CSUM |
504 			   NETIF_F_HW_VLAN_CTAG_TX;
505 
506 	dev->features = dev->hw_features;
507 
508 	/* Prevent recursive tx locking */
509 	dev->features |= NETIF_F_LLTX;
510 	/* VLAN on top of HSR needs testing and probably some work on
511 	 * hsr_header_create() etc.
512 	 */
513 	dev->features |= NETIF_F_VLAN_CHALLENGED;
514 	/* Not sure about this. Taken from bridge code. netdev_features.h says
515 	 * it means "Does not change network namespaces".
516 	 */
517 	dev->features |= NETIF_F_NETNS_LOCAL;
518 }
519 
520 /* Return true if dev is a HSR master; return false otherwise.
521  */
522 bool is_hsr_master(struct net_device *dev)
523 {
524 	return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit);
525 }
526 EXPORT_SYMBOL(is_hsr_master);
527 
528 /* Default multicast address for HSR Supervision frames */
529 static const unsigned char def_multicast_addr[ETH_ALEN] __aligned(2) = {
530 	0x01, 0x15, 0x4e, 0x00, 0x01, 0x00
531 };
532 
533 int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2],
534 		     unsigned char multicast_spec, u8 protocol_version,
535 		     struct netlink_ext_ack *extack)
536 {
537 	bool unregister = false;
538 	struct hsr_priv *hsr;
539 	int res;
540 
541 	hsr = netdev_priv(hsr_dev);
542 	INIT_LIST_HEAD(&hsr->ports);
543 	INIT_LIST_HEAD(&hsr->node_db);
544 	spin_lock_init(&hsr->list_lock);
545 
546 	eth_hw_addr_set(hsr_dev, slave[0]->dev_addr);
547 
548 	/* initialize protocol specific functions */
549 	if (protocol_version == PRP_V1) {
550 		/* For PRP, lan_id has most significant 3 bits holding
551 		 * the net_id of PRP_LAN_ID
552 		 */
553 		hsr->net_id = PRP_LAN_ID << 1;
554 		hsr->proto_ops = &prp_ops;
555 	} else {
556 		hsr->proto_ops = &hsr_ops;
557 	}
558 
559 	/* Make sure we recognize frames from ourselves in hsr_rcv() */
560 	res = hsr_create_self_node(hsr, hsr_dev->dev_addr,
561 				   slave[1]->dev_addr);
562 	if (res < 0)
563 		return res;
564 
565 	spin_lock_init(&hsr->seqnr_lock);
566 	/* Overflow soon to find bugs easier: */
567 	hsr->sequence_nr = HSR_SEQNR_START;
568 	hsr->sup_sequence_nr = HSR_SUP_SEQNR_START;
569 
570 	timer_setup(&hsr->announce_timer, hsr_announce, 0);
571 	timer_setup(&hsr->prune_timer, hsr_prune_nodes, 0);
572 
573 	ether_addr_copy(hsr->sup_multicast_addr, def_multicast_addr);
574 	hsr->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec;
575 
576 	hsr->prot_version = protocol_version;
577 
578 	/* Make sure the 1st call to netif_carrier_on() gets through */
579 	netif_carrier_off(hsr_dev);
580 
581 	res = hsr_add_port(hsr, hsr_dev, HSR_PT_MASTER, extack);
582 	if (res)
583 		goto err_add_master;
584 
585 	/* HSR forwarding offload supported in lower device? */
586 	if ((slave[0]->features & NETIF_F_HW_HSR_FWD) &&
587 	    (slave[1]->features & NETIF_F_HW_HSR_FWD))
588 		hsr->fwd_offloaded = true;
589 
590 	res = register_netdevice(hsr_dev);
591 	if (res)
592 		goto err_unregister;
593 
594 	unregister = true;
595 
596 	res = hsr_add_port(hsr, slave[0], HSR_PT_SLAVE_A, extack);
597 	if (res)
598 		goto err_unregister;
599 
600 	res = hsr_add_port(hsr, slave[1], HSR_PT_SLAVE_B, extack);
601 	if (res)
602 		goto err_unregister;
603 
604 	hsr_debugfs_init(hsr, hsr_dev);
605 	mod_timer(&hsr->prune_timer, jiffies + msecs_to_jiffies(PRUNE_PERIOD));
606 
607 	return 0;
608 
609 err_unregister:
610 	hsr_del_ports(hsr);
611 err_add_master:
612 	hsr_del_self_node(hsr);
613 
614 	if (unregister)
615 		unregister_netdevice(hsr_dev);
616 	return res;
617 }
618