1 /* Copyright 2011-2014 Autronica Fire and Security AS
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the Free
5 * Software Foundation; either version 2 of the License, or (at your option)
9 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
12 #include "hsr_slave.h"
13 #include <linux/etherdevice.h>
14 #include <linux/if_arp.h>
15 #include <linux/if_vlan.h>
17 #include "hsr_device.h"
18 #include "hsr_forward.h"
19 #include "hsr_framereg.h"
22 static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
24 struct sk_buff *skb = *pskb;
25 struct hsr_port *port;
28 if (!skb_mac_header_was_set(skb)) {
29 WARN_ONCE(1, "%s: skb invalid", __func__);
30 return RX_HANDLER_PASS;
33 rcu_read_lock(); /* hsr->node_db, hsr->ports */
34 port = hsr_port_get_rcu(skb->dev);
36 if (hsr_addr_is_self(port->hsr, eth_hdr(skb)->h_source)) {
37 /* Directly kill frames sent by ourselves */
42 protocol = eth_hdr(skb)->h_proto;
43 if (protocol != htons(ETH_P_PRP) && protocol != htons(ETH_P_HSR))
46 skb_push(skb, ETH_HLEN);
48 hsr_forward_skb(skb, port);
51 rcu_read_unlock(); /* hsr->node_db, hsr->ports */
52 return RX_HANDLER_CONSUMED;
55 rcu_read_unlock(); /* hsr->node_db, hsr->ports */
56 return RX_HANDLER_PASS;
59 bool hsr_port_exists(const struct net_device *dev)
61 return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame;
65 static int hsr_check_dev_ok(struct net_device *dev)
67 /* Don't allow HSR on non-ethernet like devices */
68 if ((dev->flags & IFF_LOOPBACK) || (dev->type != ARPHRD_ETHER) ||
69 (dev->addr_len != ETH_ALEN)) {
70 netdev_info(dev, "Cannot use loopback or non-ethernet device as HSR slave.\n");
74 /* Don't allow enslaving hsr devices */
75 if (is_hsr_master(dev)) {
76 netdev_info(dev, "Cannot create trees of HSR devices.\n");
80 if (hsr_port_exists(dev)) {
81 netdev_info(dev, "This device is already a HSR slave.\n");
85 if (is_vlan_dev(dev)) {
86 netdev_info(dev, "HSR on top of VLAN is not yet supported in this driver.\n");
90 if (dev->priv_flags & IFF_DONT_BRIDGE) {
91 netdev_info(dev, "This device does not support bridging.\n");
95 /* HSR over bonded devices has not been tested, but I'm not sure it
103 /* Setup device to be added to the HSR bridge. */
104 static int hsr_portdev_setup(struct net_device *dev, struct hsr_port *port)
109 res = dev_set_promiscuity(dev, 1);
111 goto fail_promiscuity;
114 * What does net device "adjacency" mean? Should we do
115 * res = netdev_master_upper_dev_link(port->dev, port->hsr->dev); ?
118 res = netdev_rx_handler_register(dev, hsr_handle_frame, port);
120 goto fail_rx_handler;
121 dev_disable_lro(dev);
126 dev_set_promiscuity(dev, -1);
133 int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev,
134 enum hsr_port_type type)
136 struct hsr_port *port, *master;
139 if (type != HSR_PT_MASTER) {
140 res = hsr_check_dev_ok(dev);
145 port = hsr_port_get_hsr(hsr, type);
147 return -EBUSY; /* This port already exists */
149 port = kzalloc(sizeof(*port), GFP_KERNEL);
153 if (type != HSR_PT_MASTER) {
154 res = hsr_portdev_setup(dev, port);
163 list_add_tail_rcu(&port->port_list, &hsr->ports);
166 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
167 netdev_update_features(master->dev);
168 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
177 void hsr_del_port(struct hsr_port *port)
179 struct hsr_priv *hsr;
180 struct hsr_port *master;
183 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
184 list_del_rcu(&port->port_list);
186 if (port != master) {
187 if (master != NULL) {
188 netdev_update_features(master->dev);
189 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
191 netdev_rx_handler_unregister(port->dev);
192 dev_set_promiscuity(port->dev, -1);
196 * netdev_upper_dev_unlink(port->dev, port->hsr->dev);