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
is_admin_up(struct net_device * dev)21 static bool is_admin_up(struct net_device *dev)
22 {
23 return dev && (dev->flags & IFF_UP);
24 }
25
is_slave_up(struct net_device * dev)26 static bool is_slave_up(struct net_device *dev)
27 {
28 return dev && is_admin_up(dev) && netif_oper_up(dev);
29 }
30
hsr_set_operstate(struct hsr_port * master,bool has_carrier)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 netif_set_operstate(dev, IF_OPER_DOWN);
37 return;
38 }
39
40 if (has_carrier)
41 netif_set_operstate(dev, IF_OPER_UP);
42 else
43 netif_set_operstate(dev, IF_OPER_LOWERLAYERDOWN);
44 }
45
hsr_check_carrier(struct hsr_port * master)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_rtnl(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
hsr_check_announce(struct net_device * hsr_dev)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
77 if (hsr->redbox && !timer_pending(&hsr->announce_proxy_timer))
78 mod_timer(&hsr->announce_proxy_timer, jiffies +
79 msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL) / 2);
80 } else {
81 /* Deactivate the announce timer */
82 timer_delete(&hsr->announce_timer);
83 if (hsr->redbox)
84 timer_delete(&hsr->announce_proxy_timer);
85 }
86 }
87
hsr_check_carrier_and_operstate(struct hsr_priv * hsr)88 void hsr_check_carrier_and_operstate(struct hsr_priv *hsr)
89 {
90 struct hsr_port *master;
91 bool has_carrier;
92
93 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
94 /* netif_stacked_transfer_operstate() cannot be used here since
95 * it doesn't set IF_OPER_LOWERLAYERDOWN (?)
96 */
97 has_carrier = hsr_check_carrier(master);
98 hsr_set_operstate(master, has_carrier);
99 hsr_check_announce(master->dev);
100 }
101
hsr_get_max_mtu(struct hsr_priv * hsr)102 int hsr_get_max_mtu(struct hsr_priv *hsr)
103 {
104 unsigned int mtu_max;
105 struct hsr_port *port;
106
107 mtu_max = ETH_DATA_LEN;
108 hsr_for_each_port_rtnl(hsr, port)
109 if (port->type != HSR_PT_MASTER)
110 mtu_max = min(port->dev->mtu, mtu_max);
111
112 if (mtu_max < HSR_HLEN)
113 return 0;
114 return mtu_max - HSR_HLEN;
115 }
116
hsr_dev_change_mtu(struct net_device * dev,int new_mtu)117 static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu)
118 {
119 struct hsr_priv *hsr;
120
121 hsr = netdev_priv(dev);
122
123 if (new_mtu > hsr_get_max_mtu(hsr)) {
124 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",
125 HSR_HLEN);
126 return -EINVAL;
127 }
128
129 WRITE_ONCE(dev->mtu, new_mtu);
130
131 return 0;
132 }
133
hsr_dev_open(struct net_device * dev)134 static int hsr_dev_open(struct net_device *dev)
135 {
136 struct hsr_priv *hsr;
137 struct hsr_port *port;
138 const char *designation = NULL;
139
140 hsr = netdev_priv(dev);
141
142 hsr_for_each_port_rtnl(hsr, port) {
143 if (port->type == HSR_PT_MASTER)
144 continue;
145 switch (port->type) {
146 case HSR_PT_SLAVE_A:
147 designation = "Slave A";
148 break;
149 case HSR_PT_SLAVE_B:
150 designation = "Slave B";
151 break;
152 case HSR_PT_INTERLINK:
153 designation = "Interlink";
154 break;
155 default:
156 designation = "Unknown";
157 }
158 if (!is_slave_up(port->dev))
159 netdev_warn(dev, "%s (%s) is not up; please bring it up to get a fully working HSR network\n",
160 designation, port->dev->name);
161 }
162
163 if (!designation)
164 netdev_warn(dev, "No slave devices configured\n");
165
166 return 0;
167 }
168
hsr_dev_close(struct net_device * dev)169 static int hsr_dev_close(struct net_device *dev)
170 {
171 struct hsr_port *port;
172 struct hsr_priv *hsr;
173
174 hsr = netdev_priv(dev);
175 hsr_for_each_port_rtnl(hsr, port) {
176 if (port->type == HSR_PT_MASTER)
177 continue;
178 switch (port->type) {
179 case HSR_PT_SLAVE_A:
180 case HSR_PT_SLAVE_B:
181 dev_uc_unsync(port->dev, dev);
182 dev_mc_unsync(port->dev, dev);
183 break;
184 default:
185 break;
186 }
187 }
188
189 return 0;
190 }
191
hsr_features_recompute(struct hsr_priv * hsr,netdev_features_t features)192 static netdev_features_t hsr_features_recompute(struct hsr_priv *hsr,
193 netdev_features_t features)
194 {
195 netdev_features_t mask;
196 struct hsr_port *port;
197
198 mask = features;
199
200 /* Mask out all features that, if supported by one device, should be
201 * enabled for all devices (see NETIF_F_ONE_FOR_ALL).
202 *
203 * Anything that's off in mask will not be enabled - so only things
204 * that were in features originally, and also is in NETIF_F_ONE_FOR_ALL,
205 * may become enabled.
206 */
207 features &= ~NETIF_F_ONE_FOR_ALL;
208 hsr_for_each_port_rtnl(hsr, port)
209 features = netdev_increment_features(features,
210 port->dev->features,
211 mask);
212
213 return features;
214 }
215
hsr_fix_features(struct net_device * dev,netdev_features_t features)216 static netdev_features_t hsr_fix_features(struct net_device *dev,
217 netdev_features_t features)
218 {
219 struct hsr_priv *hsr = netdev_priv(dev);
220
221 return hsr_features_recompute(hsr, features);
222 }
223
hsr_dev_xmit(struct sk_buff * skb,struct net_device * dev)224 static netdev_tx_t hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
225 {
226 struct hsr_priv *hsr = netdev_priv(dev);
227 struct hsr_port *master;
228
229 rcu_read_lock();
230 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
231 if (master) {
232 skb->dev = master->dev;
233 skb_reset_mac_header(skb);
234 skb_reset_mac_len(skb);
235 spin_lock_bh(&hsr->seqnr_lock);
236 hsr_forward_skb(skb, master);
237 spin_unlock_bh(&hsr->seqnr_lock);
238 } else {
239 dev_core_stats_tx_dropped_inc(dev);
240 dev_kfree_skb_any(skb);
241 }
242 rcu_read_unlock();
243
244 return NETDEV_TX_OK;
245 }
246
247 static const struct header_ops hsr_header_ops = {
248 .create = eth_header,
249 .parse = eth_header_parse,
250 };
251
hsr_init_skb(struct hsr_port * master,int extra)252 static struct sk_buff *hsr_init_skb(struct hsr_port *master, int extra)
253 {
254 struct hsr_priv *hsr = master->hsr;
255 struct sk_buff *skb;
256 int hlen, tlen;
257 int len;
258
259 hlen = LL_RESERVED_SPACE(master->dev);
260 tlen = master->dev->needed_tailroom;
261 len = sizeof(struct hsr_sup_tag) + sizeof(struct hsr_sup_payload);
262 /* skb size is same for PRP/HSR frames, only difference
263 * being, for PRP it is a trailer and for HSR it is a
264 * header.
265 * RedBox might use @extra more bytes.
266 */
267 skb = dev_alloc_skb(len + extra + hlen + tlen);
268
269 if (!skb)
270 return skb;
271
272 skb_reserve(skb, hlen);
273 skb->dev = master->dev;
274 skb->priority = TC_PRIO_CONTROL;
275
276 skb_reset_network_header(skb);
277 skb_reset_transport_header(skb);
278 if (dev_hard_header(skb, skb->dev, ETH_P_PRP,
279 hsr->sup_multicast_addr,
280 skb->dev->dev_addr, skb->len) <= 0)
281 goto out;
282
283 skb_reset_mac_header(skb);
284 skb_reset_mac_len(skb);
285
286 return skb;
287 out:
288 kfree_skb(skb);
289
290 return NULL;
291 }
292
send_hsr_supervision_frame(struct hsr_port * port,unsigned long * interval,const unsigned char * addr)293 static void send_hsr_supervision_frame(struct hsr_port *port,
294 unsigned long *interval,
295 const unsigned char *addr)
296 {
297 struct hsr_priv *hsr = port->hsr;
298 __u8 type = HSR_TLV_LIFE_CHECK;
299 struct hsr_sup_payload *hsr_sp;
300 struct hsr_sup_tlv *hsr_stlv;
301 struct hsr_sup_tag *hsr_stag;
302 struct sk_buff *skb;
303 int extra = 0;
304
305 *interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
306 if (hsr->announce_count < 3 && hsr->prot_version == 0) {
307 type = HSR_TLV_ANNOUNCE;
308 *interval = msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
309 hsr->announce_count++;
310 }
311
312 if (hsr->redbox)
313 extra = sizeof(struct hsr_sup_tlv) +
314 sizeof(struct hsr_sup_payload);
315
316 skb = hsr_init_skb(port, extra);
317 if (!skb) {
318 netdev_warn_once(port->dev, "HSR: Could not send supervision frame\n");
319 return;
320 }
321
322 hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
323 set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf));
324 set_hsr_stag_HSR_ver(hsr_stag, hsr->prot_version);
325
326 /* From HSRv1 on we have separate supervision sequence numbers. */
327 spin_lock_bh(&hsr->seqnr_lock);
328 if (hsr->prot_version > 0) {
329 hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr);
330 hsr->sup_sequence_nr++;
331 } else {
332 hsr_stag->sequence_nr = htons(hsr->sequence_nr);
333 hsr->sequence_nr++;
334 }
335
336 hsr_stag->tlv.HSR_TLV_type = type;
337 /* TODO: Why 12 in HSRv0? */
338 hsr_stag->tlv.HSR_TLV_length = hsr->prot_version ?
339 sizeof(struct hsr_sup_payload) : 12;
340
341 /* Payload: MacAddressA / SAN MAC from ProxyNodeTable */
342 hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
343 ether_addr_copy(hsr_sp->macaddress_A, addr);
344
345 if (hsr->redbox &&
346 hsr_is_node_in_db(&hsr->proxy_node_db, addr)) {
347 hsr_stlv = skb_put(skb, sizeof(struct hsr_sup_tlv));
348 hsr_stlv->HSR_TLV_type = PRP_TLV_REDBOX_MAC;
349 hsr_stlv->HSR_TLV_length = sizeof(struct hsr_sup_payload);
350
351 /* Payload: MacAddressRedBox */
352 hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
353 ether_addr_copy(hsr_sp->macaddress_A, hsr->macaddress_redbox);
354 }
355
356 if (skb_put_padto(skb, ETH_ZLEN)) {
357 spin_unlock_bh(&hsr->seqnr_lock);
358 return;
359 }
360
361 hsr_forward_skb(skb, port);
362 spin_unlock_bh(&hsr->seqnr_lock);
363 return;
364 }
365
send_prp_supervision_frame(struct hsr_port * master,unsigned long * interval,const unsigned char * addr)366 static void send_prp_supervision_frame(struct hsr_port *master,
367 unsigned long *interval,
368 const unsigned char *addr)
369 {
370 struct hsr_priv *hsr = master->hsr;
371 struct hsr_sup_payload *hsr_sp;
372 struct hsr_sup_tag *hsr_stag;
373 struct sk_buff *skb;
374
375 skb = hsr_init_skb(master, 0);
376 if (!skb) {
377 netdev_warn_once(master->dev, "PRP: Could not send supervision frame\n");
378 return;
379 }
380
381 *interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
382 hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
383 set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf));
384 set_hsr_stag_HSR_ver(hsr_stag, (hsr->prot_version ? 1 : 0));
385
386 /* From HSRv1 on we have separate supervision sequence numbers. */
387 spin_lock_bh(&hsr->seqnr_lock);
388 hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr);
389 hsr->sup_sequence_nr++;
390 hsr_stag->tlv.HSR_TLV_type = PRP_TLV_LIFE_CHECK_DD;
391 hsr_stag->tlv.HSR_TLV_length = sizeof(struct hsr_sup_payload);
392
393 /* Payload: MacAddressA */
394 hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
395 ether_addr_copy(hsr_sp->macaddress_A, master->dev->dev_addr);
396
397 if (skb_put_padto(skb, ETH_ZLEN)) {
398 spin_unlock_bh(&hsr->seqnr_lock);
399 return;
400 }
401
402 hsr_forward_skb(skb, master);
403 spin_unlock_bh(&hsr->seqnr_lock);
404 }
405
406 /* Announce (supervision frame) timer function
407 */
hsr_announce(struct timer_list * t)408 static void hsr_announce(struct timer_list *t)
409 {
410 struct hsr_priv *hsr;
411 struct hsr_port *master;
412 unsigned long interval;
413
414 hsr = timer_container_of(hsr, t, announce_timer);
415
416 rcu_read_lock();
417 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
418 hsr->proto_ops->send_sv_frame(master, &interval, master->dev->dev_addr);
419
420 if (is_admin_up(master->dev))
421 mod_timer(&hsr->announce_timer, jiffies + interval);
422
423 rcu_read_unlock();
424 }
425
426 /* Announce (supervision frame) timer function for RedBox
427 */
hsr_proxy_announce(struct timer_list * t)428 static void hsr_proxy_announce(struct timer_list *t)
429 {
430 struct hsr_priv *hsr = timer_container_of(hsr, t,
431 announce_proxy_timer);
432 struct hsr_port *interlink;
433 unsigned long interval = 0;
434 struct hsr_node *node;
435
436 rcu_read_lock();
437 /* RedBOX sends supervisory frames to HSR network with MAC addresses
438 * of SAN nodes stored in ProxyNodeTable.
439 */
440 interlink = hsr_port_get_hsr(hsr, HSR_PT_INTERLINK);
441 if (!interlink)
442 goto done;
443
444 list_for_each_entry_rcu(node, &hsr->proxy_node_db, mac_list) {
445 if (hsr_addr_is_redbox(hsr, node->macaddress_A))
446 continue;
447 hsr->proto_ops->send_sv_frame(interlink, &interval,
448 node->macaddress_A);
449 }
450
451 if (is_admin_up(interlink->dev)) {
452 if (!interval)
453 interval = msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
454
455 mod_timer(&hsr->announce_proxy_timer, jiffies + interval);
456 }
457
458 done:
459 rcu_read_unlock();
460 }
461
hsr_del_ports(struct hsr_priv * hsr)462 void hsr_del_ports(struct hsr_priv *hsr)
463 {
464 struct hsr_port *port;
465
466 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
467 if (port)
468 hsr_del_port(port);
469
470 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
471 if (port)
472 hsr_del_port(port);
473
474 port = hsr_port_get_hsr(hsr, HSR_PT_INTERLINK);
475 if (port)
476 hsr_del_port(port);
477
478 port = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
479 if (port)
480 hsr_del_port(port);
481 }
482
hsr_set_rx_mode(struct net_device * dev)483 static void hsr_set_rx_mode(struct net_device *dev)
484 {
485 struct hsr_port *port;
486 struct hsr_priv *hsr;
487
488 hsr = netdev_priv(dev);
489
490 hsr_for_each_port_rtnl(hsr, port) {
491 if (port->type == HSR_PT_MASTER)
492 continue;
493 switch (port->type) {
494 case HSR_PT_SLAVE_A:
495 case HSR_PT_SLAVE_B:
496 dev_mc_sync_multiple(port->dev, dev);
497 dev_uc_sync_multiple(port->dev, dev);
498 break;
499 default:
500 break;
501 }
502 }
503 }
504
hsr_change_rx_flags(struct net_device * dev,int change)505 static void hsr_change_rx_flags(struct net_device *dev, int change)
506 {
507 struct hsr_port *port;
508 struct hsr_priv *hsr;
509
510 hsr = netdev_priv(dev);
511
512 hsr_for_each_port_rtnl(hsr, port) {
513 if (port->type == HSR_PT_MASTER)
514 continue;
515 switch (port->type) {
516 case HSR_PT_SLAVE_A:
517 case HSR_PT_SLAVE_B:
518 if (change & IFF_ALLMULTI)
519 dev_set_allmulti(port->dev,
520 dev->flags &
521 IFF_ALLMULTI ? 1 : -1);
522 break;
523 default:
524 break;
525 }
526 }
527 }
528
hsr_ndo_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)529 static int hsr_ndo_vlan_rx_add_vid(struct net_device *dev,
530 __be16 proto, u16 vid)
531 {
532 bool is_slave_a_added = false;
533 bool is_slave_b_added = false;
534 struct hsr_port *port;
535 struct hsr_priv *hsr;
536 int ret = 0;
537
538 hsr = netdev_priv(dev);
539
540 hsr_for_each_port_rtnl(hsr, port) {
541 if (port->type == HSR_PT_MASTER ||
542 port->type == HSR_PT_INTERLINK)
543 continue;
544
545 ret = vlan_vid_add(port->dev, proto, vid);
546 switch (port->type) {
547 case HSR_PT_SLAVE_A:
548 if (ret) {
549 /* clean up Slave-B */
550 netdev_err(dev, "add vid failed for Slave-A\n");
551 if (is_slave_b_added)
552 vlan_vid_del(port->dev, proto, vid);
553 return ret;
554 }
555
556 is_slave_a_added = true;
557 break;
558
559 case HSR_PT_SLAVE_B:
560 if (ret) {
561 /* clean up Slave-A */
562 netdev_err(dev, "add vid failed for Slave-B\n");
563 if (is_slave_a_added)
564 vlan_vid_del(port->dev, proto, vid);
565 return ret;
566 }
567
568 is_slave_b_added = true;
569 break;
570 default:
571 break;
572 }
573 }
574
575 return 0;
576 }
577
hsr_ndo_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)578 static int hsr_ndo_vlan_rx_kill_vid(struct net_device *dev,
579 __be16 proto, u16 vid)
580 {
581 struct hsr_port *port;
582 struct hsr_priv *hsr;
583
584 hsr = netdev_priv(dev);
585
586 hsr_for_each_port_rtnl(hsr, port) {
587 switch (port->type) {
588 case HSR_PT_SLAVE_A:
589 case HSR_PT_SLAVE_B:
590 vlan_vid_del(port->dev, proto, vid);
591 break;
592 default:
593 break;
594 }
595 }
596
597 return 0;
598 }
599
600 static const struct net_device_ops hsr_device_ops = {
601 .ndo_change_mtu = hsr_dev_change_mtu,
602 .ndo_open = hsr_dev_open,
603 .ndo_stop = hsr_dev_close,
604 .ndo_start_xmit = hsr_dev_xmit,
605 .ndo_change_rx_flags = hsr_change_rx_flags,
606 .ndo_fix_features = hsr_fix_features,
607 .ndo_set_rx_mode = hsr_set_rx_mode,
608 .ndo_vlan_rx_add_vid = hsr_ndo_vlan_rx_add_vid,
609 .ndo_vlan_rx_kill_vid = hsr_ndo_vlan_rx_kill_vid,
610 };
611
612 static const struct device_type hsr_type = {
613 .name = "hsr",
614 };
615
616 static struct hsr_proto_ops hsr_ops = {
617 .send_sv_frame = send_hsr_supervision_frame,
618 .create_tagged_frame = hsr_create_tagged_frame,
619 .get_untagged_frame = hsr_get_untagged_frame,
620 .drop_frame = hsr_drop_frame,
621 .fill_frame_info = hsr_fill_frame_info,
622 .invalid_dan_ingress_frame = hsr_invalid_dan_ingress_frame,
623 .register_frame_out = hsr_register_frame_out,
624 };
625
626 static struct hsr_proto_ops prp_ops = {
627 .send_sv_frame = send_prp_supervision_frame,
628 .create_tagged_frame = prp_create_tagged_frame,
629 .get_untagged_frame = prp_get_untagged_frame,
630 .drop_frame = prp_drop_frame,
631 .fill_frame_info = prp_fill_frame_info,
632 .handle_san_frame = prp_handle_san_frame,
633 .update_san_info = prp_update_san_info,
634 .register_frame_out = prp_register_frame_out,
635 };
636
hsr_dev_setup(struct net_device * dev)637 void hsr_dev_setup(struct net_device *dev)
638 {
639 eth_hw_addr_random(dev);
640
641 ether_setup(dev);
642 dev->min_mtu = 0;
643 dev->header_ops = &hsr_header_ops;
644 dev->netdev_ops = &hsr_device_ops;
645 SET_NETDEV_DEVTYPE(dev, &hsr_type);
646 dev->priv_flags |= IFF_NO_QUEUE | IFF_DISABLE_NETPOLL;
647 /* Prevent recursive tx locking */
648 dev->lltx = true;
649 /* Not sure about this. Taken from bridge code. netdevice.h says
650 * it means "Does not change network namespaces".
651 */
652 dev->netns_immutable = true;
653
654 dev->needs_free_netdev = true;
655
656 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
657 NETIF_F_GSO_MASK | NETIF_F_HW_CSUM |
658 NETIF_F_HW_VLAN_CTAG_TX |
659 NETIF_F_HW_VLAN_CTAG_FILTER;
660
661 dev->features = dev->hw_features;
662 }
663
664 /* Return true if dev is a HSR master; return false otherwise.
665 */
is_hsr_master(struct net_device * dev)666 bool is_hsr_master(struct net_device *dev)
667 {
668 return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit);
669 }
670 EXPORT_SYMBOL(is_hsr_master);
671
hsr_get_port_ndev(struct net_device * ndev,enum hsr_port_type pt)672 struct net_device *hsr_get_port_ndev(struct net_device *ndev,
673 enum hsr_port_type pt)
674 {
675 struct hsr_priv *hsr = netdev_priv(ndev);
676 struct hsr_port *port;
677
678 rcu_read_lock();
679 hsr_for_each_port(hsr, port)
680 if (port->type == pt) {
681 dev_hold(port->dev);
682 rcu_read_unlock();
683 return port->dev;
684 }
685 rcu_read_unlock();
686 return NULL;
687 }
688 EXPORT_SYMBOL(hsr_get_port_ndev);
689
690 /* Default multicast address for HSR Supervision frames */
691 static const unsigned char def_multicast_addr[ETH_ALEN] __aligned(2) = {
692 0x01, 0x15, 0x4e, 0x00, 0x01, 0x00
693 };
694
hsr_dev_finalize(struct net_device * hsr_dev,struct net_device * slave[2],struct net_device * interlink,unsigned char multicast_spec,u8 protocol_version,struct netlink_ext_ack * extack)695 int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2],
696 struct net_device *interlink, unsigned char multicast_spec,
697 u8 protocol_version, struct netlink_ext_ack *extack)
698 {
699 bool unregister = false;
700 struct hsr_priv *hsr;
701 int res;
702
703 hsr = netdev_priv(hsr_dev);
704 INIT_LIST_HEAD(&hsr->ports);
705 INIT_LIST_HEAD(&hsr->node_db);
706 INIT_LIST_HEAD(&hsr->proxy_node_db);
707 spin_lock_init(&hsr->list_lock);
708
709 eth_hw_addr_set(hsr_dev, slave[0]->dev_addr);
710
711 /* initialize protocol specific functions */
712 if (protocol_version == PRP_V1) {
713 /* For PRP, lan_id has most significant 3 bits holding
714 * the net_id of PRP_LAN_ID
715 */
716 hsr->net_id = PRP_LAN_ID << 1;
717 hsr->proto_ops = &prp_ops;
718 } else {
719 hsr->proto_ops = &hsr_ops;
720 }
721
722 /* Make sure we recognize frames from ourselves in hsr_rcv() */
723 res = hsr_create_self_node(hsr, hsr_dev->dev_addr,
724 slave[1]->dev_addr);
725 if (res < 0)
726 return res;
727
728 spin_lock_init(&hsr->seqnr_lock);
729 /* Overflow soon to find bugs easier: */
730 hsr->sequence_nr = HSR_SEQNR_START;
731 hsr->sup_sequence_nr = HSR_SUP_SEQNR_START;
732
733 timer_setup(&hsr->announce_timer, hsr_announce, 0);
734 timer_setup(&hsr->prune_timer, hsr_prune_nodes, 0);
735 timer_setup(&hsr->prune_proxy_timer, hsr_prune_proxy_nodes, 0);
736 timer_setup(&hsr->announce_proxy_timer, hsr_proxy_announce, 0);
737
738 ether_addr_copy(hsr->sup_multicast_addr, def_multicast_addr);
739 hsr->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec;
740
741 hsr->prot_version = protocol_version;
742
743 /* Make sure the 1st call to netif_carrier_on() gets through */
744 netif_carrier_off(hsr_dev);
745
746 res = hsr_add_port(hsr, hsr_dev, HSR_PT_MASTER, extack);
747 if (res)
748 goto err_add_master;
749
750 /* HSR forwarding offload supported in lower device? */
751 if ((slave[0]->features & NETIF_F_HW_HSR_FWD) &&
752 (slave[1]->features & NETIF_F_HW_HSR_FWD))
753 hsr->fwd_offloaded = true;
754
755 if ((slave[0]->features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
756 (slave[1]->features & NETIF_F_HW_VLAN_CTAG_FILTER))
757 hsr_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
758
759 res = register_netdevice(hsr_dev);
760 if (res)
761 goto err_unregister;
762
763 unregister = true;
764
765 res = hsr_add_port(hsr, slave[0], HSR_PT_SLAVE_A, extack);
766 if (res)
767 goto err_unregister;
768
769 res = hsr_add_port(hsr, slave[1], HSR_PT_SLAVE_B, extack);
770 if (res)
771 goto err_unregister;
772
773 if (protocol_version == PRP_V1) {
774 eth_hw_addr_set(slave[1], slave[0]->dev_addr);
775 call_netdevice_notifiers(NETDEV_CHANGEADDR, slave[1]);
776 }
777
778 if (interlink) {
779 res = hsr_add_port(hsr, interlink, HSR_PT_INTERLINK, extack);
780 if (res)
781 goto err_unregister;
782
783 hsr->redbox = true;
784 ether_addr_copy(hsr->macaddress_redbox, interlink->dev_addr);
785 mod_timer(&hsr->prune_proxy_timer,
786 jiffies + msecs_to_jiffies(PRUNE_PROXY_PERIOD));
787 }
788
789 hsr_debugfs_init(hsr, hsr_dev);
790 mod_timer(&hsr->prune_timer, jiffies + msecs_to_jiffies(PRUNE_PERIOD));
791
792 return 0;
793
794 err_unregister:
795 hsr_del_ports(hsr);
796 err_add_master:
797 hsr_del_self_node(hsr);
798
799 if (unregister)
800 unregister_netdevice(hsr_dev);
801 return res;
802 }
803