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 #include "hsr_slave.h" 9 #include <linux/etherdevice.h> 10 #include <linux/if_arp.h> 11 #include <linux/if_vlan.h> 12 #include "hsr_main.h" 13 #include "hsr_device.h" 14 #include "hsr_forward.h" 15 #include "hsr_framereg.h" 16 17 static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb) 18 { 19 struct sk_buff *skb = *pskb; 20 struct hsr_port *port; 21 u16 protocol; 22 23 if (!skb_mac_header_was_set(skb)) { 24 WARN_ONCE(1, "%s: skb invalid", __func__); 25 return RX_HANDLER_PASS; 26 } 27 28 rcu_read_lock(); /* hsr->node_db, hsr->ports */ 29 port = hsr_port_get_rcu(skb->dev); 30 if (!port) 31 goto finish_pass; 32 33 if (hsr_addr_is_self(port->hsr, eth_hdr(skb)->h_source)) { 34 /* Directly kill frames sent by ourselves */ 35 kfree_skb(skb); 36 goto finish_consume; 37 } 38 39 protocol = eth_hdr(skb)->h_proto; 40 if (protocol != htons(ETH_P_PRP) && protocol != htons(ETH_P_HSR)) 41 goto finish_pass; 42 43 skb_push(skb, ETH_HLEN); 44 45 hsr_forward_skb(skb, port); 46 47 finish_consume: 48 rcu_read_unlock(); /* hsr->node_db, hsr->ports */ 49 return RX_HANDLER_CONSUMED; 50 51 finish_pass: 52 rcu_read_unlock(); /* hsr->node_db, hsr->ports */ 53 return RX_HANDLER_PASS; 54 } 55 56 bool hsr_port_exists(const struct net_device *dev) 57 { 58 return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame; 59 } 60 61 static int hsr_check_dev_ok(struct net_device *dev) 62 { 63 /* Don't allow HSR on non-ethernet like devices */ 64 if ((dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER || 65 dev->addr_len != ETH_ALEN) { 66 netdev_info(dev, "Cannot use loopback or non-ethernet device as HSR slave.\n"); 67 return -EINVAL; 68 } 69 70 /* Don't allow enslaving hsr devices */ 71 if (is_hsr_master(dev)) { 72 netdev_info(dev, "Cannot create trees of HSR devices.\n"); 73 return -EINVAL; 74 } 75 76 if (hsr_port_exists(dev)) { 77 netdev_info(dev, "This device is already a HSR slave.\n"); 78 return -EINVAL; 79 } 80 81 if (is_vlan_dev(dev)) { 82 netdev_info(dev, "HSR on top of VLAN is not yet supported in this driver.\n"); 83 return -EINVAL; 84 } 85 86 if (dev->priv_flags & IFF_DONT_BRIDGE) { 87 netdev_info(dev, "This device does not support bridging.\n"); 88 return -EOPNOTSUPP; 89 } 90 91 /* HSR over bonded devices has not been tested, but I'm not sure it 92 * won't work... 93 */ 94 95 return 0; 96 } 97 98 /* Setup device to be added to the HSR bridge. */ 99 static int hsr_portdev_setup(struct net_device *dev, struct hsr_port *port) 100 { 101 int res; 102 103 dev_hold(dev); 104 res = dev_set_promiscuity(dev, 1); 105 if (res) 106 goto fail_promiscuity; 107 108 /* FIXME: 109 * What does net device "adjacency" mean? Should we do 110 * res = netdev_master_upper_dev_link(port->dev, port->hsr->dev); ? 111 */ 112 113 res = netdev_rx_handler_register(dev, hsr_handle_frame, port); 114 if (res) 115 goto fail_rx_handler; 116 dev_disable_lro(dev); 117 118 return 0; 119 120 fail_rx_handler: 121 dev_set_promiscuity(dev, -1); 122 fail_promiscuity: 123 dev_put(dev); 124 125 return res; 126 } 127 128 int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev, 129 enum hsr_port_type type) 130 { 131 struct hsr_port *port, *master; 132 int res; 133 134 if (type != HSR_PT_MASTER) { 135 res = hsr_check_dev_ok(dev); 136 if (res) 137 return res; 138 } 139 140 port = hsr_port_get_hsr(hsr, type); 141 if (port) 142 return -EBUSY; /* This port already exists */ 143 144 port = kzalloc(sizeof(*port), GFP_KERNEL); 145 if (!port) 146 return -ENOMEM; 147 148 if (type != HSR_PT_MASTER) { 149 res = hsr_portdev_setup(dev, port); 150 if (res) 151 goto fail_dev_setup; 152 } 153 154 port->hsr = hsr; 155 port->dev = dev; 156 port->type = type; 157 158 list_add_tail_rcu(&port->port_list, &hsr->ports); 159 synchronize_rcu(); 160 161 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); 162 netdev_update_features(master->dev); 163 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr)); 164 165 return 0; 166 167 fail_dev_setup: 168 kfree(port); 169 return res; 170 } 171 172 void hsr_del_port(struct hsr_port *port) 173 { 174 struct hsr_priv *hsr; 175 struct hsr_port *master; 176 177 hsr = port->hsr; 178 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); 179 list_del_rcu(&port->port_list); 180 181 if (port != master) { 182 if (master) { 183 netdev_update_features(master->dev); 184 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr)); 185 } 186 netdev_rx_handler_unregister(port->dev); 187 dev_set_promiscuity(port->dev, -1); 188 } 189 190 /* FIXME? 191 * netdev_upper_dev_unlink(port->dev, port->hsr->dev); 192 */ 193 194 synchronize_rcu(); 195 196 if (port != master) 197 dev_put(port->dev); 198 kfree(port); 199 } 200