Murali Karicheri | 0e7623b | 2019-04-05 13:31:34 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arvid Brodin | 81ba6af | 2014-07-04 23:35:24 +0200 | [diff] [blame] | 2 | /* Copyright 2011-2014 Autronica Fire and Security AS |
| 3 | * |
Arvid Brodin | 81ba6af | 2014-07-04 23:35:24 +0200 | [diff] [blame] | 4 | * Author(s): |
| 5 | * 2011-2014 Arvid Brodin, arvid.brodin@alten.se |
Murali Karicheri | 8f4c0e0 | 2020-07-22 10:40:16 -0400 | [diff] [blame] | 6 | * |
| 7 | * Frame handler other utility functions for HSR and PRP. |
Arvid Brodin | 81ba6af | 2014-07-04 23:35:24 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include "hsr_slave.h" |
| 11 | #include <linux/etherdevice.h> |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 12 | #include <linux/if_arp.h> |
Parav Pandit | d0d7b10 | 2017-02-04 11:00:49 -0600 | [diff] [blame] | 13 | #include <linux/if_vlan.h> |
Arvid Brodin | 81ba6af | 2014-07-04 23:35:24 +0200 | [diff] [blame] | 14 | #include "hsr_main.h" |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 15 | #include "hsr_device.h" |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 16 | #include "hsr_forward.h" |
Arvid Brodin | 81ba6af | 2014-07-04 23:35:24 +0200 | [diff] [blame] | 17 | #include "hsr_framereg.h" |
| 18 | |
Murali Karicheri | 451d812 | 2020-07-22 10:40:21 -0400 | [diff] [blame] | 19 | bool hsr_invalid_dan_ingress_frame(__be16 protocol) |
| 20 | { |
| 21 | return (protocol != htons(ETH_P_PRP) && protocol != htons(ETH_P_HSR)); |
| 22 | } |
| 23 | |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 24 | static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb) |
| 25 | { |
| 26 | struct sk_buff *skb = *pskb; |
| 27 | struct hsr_port *port; |
Murali Karicheri | 451d812 | 2020-07-22 10:40:21 -0400 | [diff] [blame] | 28 | struct hsr_priv *hsr; |
Murali Karicheri | f5dda31 | 2020-05-06 11:41:07 -0400 | [diff] [blame] | 29 | __be16 protocol; |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 30 | |
Murali Karicheri | 451d812 | 2020-07-22 10:40:21 -0400 | [diff] [blame] | 31 | /* Packets from dev_loopback_xmit() do not have L2 header, bail out */ |
| 32 | if (unlikely(skb->pkt_type == PACKET_LOOPBACK)) |
| 33 | return RX_HANDLER_PASS; |
| 34 | |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 35 | if (!skb_mac_header_was_set(skb)) { |
| 36 | WARN_ONCE(1, "%s: skb invalid", __func__); |
| 37 | return RX_HANDLER_PASS; |
| 38 | } |
| 39 | |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 40 | port = hsr_port_get_rcu(skb->dev); |
Eric Dumazet | 2b5b825 | 2020-02-03 10:15:07 -0800 | [diff] [blame] | 41 | if (!port) |
| 42 | goto finish_pass; |
Murali Karicheri | 451d812 | 2020-07-22 10:40:21 -0400 | [diff] [blame] | 43 | hsr = port->hsr; |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 44 | |
| 45 | if (hsr_addr_is_self(port->hsr, eth_hdr(skb)->h_source)) { |
| 46 | /* Directly kill frames sent by ourselves */ |
| 47 | kfree_skb(skb); |
| 48 | goto finish_consume; |
| 49 | } |
| 50 | |
George McCollister | dcf0cd1 | 2021-02-09 19:02:11 -0600 | [diff] [blame] | 51 | /* For HSR, only tagged frames are expected (unless the device offloads |
| 52 | * HSR tag removal), but for PRP there could be non tagged frames as |
| 53 | * well from Single attached nodes (SANs). |
Murali Karicheri | 451d812 | 2020-07-22 10:40:21 -0400 | [diff] [blame] | 54 | */ |
Peter Heise | ee1c279 | 2016-04-13 13:52:22 +0200 | [diff] [blame] | 55 | protocol = eth_hdr(skb)->h_proto; |
George McCollister | dcf0cd1 | 2021-02-09 19:02:11 -0600 | [diff] [blame] | 56 | |
| 57 | if (!(port->dev->features & NETIF_F_HW_HSR_TAG_RM) && |
| 58 | hsr->proto_ops->invalid_dan_ingress_frame && |
Murali Karicheri | 451d812 | 2020-07-22 10:40:21 -0400 | [diff] [blame] | 59 | hsr->proto_ops->invalid_dan_ingress_frame(protocol)) |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 60 | goto finish_pass; |
| 61 | |
| 62 | skb_push(skb, ETH_HLEN); |
| 63 | |
Murali Karicheri | 451d812 | 2020-07-22 10:40:21 -0400 | [diff] [blame] | 64 | if (skb_mac_header(skb) != skb->data) { |
| 65 | WARN_ONCE(1, "%s:%d: Malformed frame at source port %s)\n", |
| 66 | __func__, __LINE__, port->dev->name); |
| 67 | goto finish_consume; |
| 68 | } |
| 69 | |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 70 | hsr_forward_skb(skb, port); |
| 71 | |
| 72 | finish_consume: |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 73 | return RX_HANDLER_CONSUMED; |
| 74 | |
| 75 | finish_pass: |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 76 | return RX_HANDLER_PASS; |
| 77 | } |
| 78 | |
| 79 | bool hsr_port_exists(const struct net_device *dev) |
| 80 | { |
| 81 | return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame; |
| 82 | } |
| 83 | |
Taehee Yoo | 13eeb5f | 2020-02-28 18:01:35 +0000 | [diff] [blame] | 84 | static int hsr_check_dev_ok(struct net_device *dev, |
| 85 | struct netlink_ext_ack *extack) |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 86 | { |
| 87 | /* Don't allow HSR on non-ethernet like devices */ |
Murali Karicheri | 5670342 | 2019-04-05 13:31:25 -0400 | [diff] [blame] | 88 | if ((dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER || |
| 89 | dev->addr_len != ETH_ALEN) { |
Taehee Yoo | 13eeb5f | 2020-02-28 18:01:35 +0000 | [diff] [blame] | 90 | NL_SET_ERR_MSG_MOD(extack, "Cannot use loopback or non-ethernet device as HSR slave."); |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 91 | return -EINVAL; |
| 92 | } |
| 93 | |
| 94 | /* Don't allow enslaving hsr devices */ |
| 95 | if (is_hsr_master(dev)) { |
Taehee Yoo | 13eeb5f | 2020-02-28 18:01:35 +0000 | [diff] [blame] | 96 | NL_SET_ERR_MSG_MOD(extack, |
| 97 | "Cannot create trees of HSR devices."); |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 98 | return -EINVAL; |
| 99 | } |
| 100 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 101 | if (hsr_port_exists(dev)) { |
Taehee Yoo | 13eeb5f | 2020-02-28 18:01:35 +0000 | [diff] [blame] | 102 | NL_SET_ERR_MSG_MOD(extack, |
| 103 | "This device is already a HSR slave."); |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 104 | return -EINVAL; |
| 105 | } |
| 106 | |
Parav Pandit | d0d7b10 | 2017-02-04 11:00:49 -0600 | [diff] [blame] | 107 | if (is_vlan_dev(dev)) { |
Taehee Yoo | 13eeb5f | 2020-02-28 18:01:35 +0000 | [diff] [blame] | 108 | NL_SET_ERR_MSG_MOD(extack, "HSR on top of VLAN is not yet supported in this driver."); |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 109 | return -EINVAL; |
| 110 | } |
| 111 | |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 112 | if (dev->priv_flags & IFF_DONT_BRIDGE) { |
Taehee Yoo | 13eeb5f | 2020-02-28 18:01:35 +0000 | [diff] [blame] | 113 | NL_SET_ERR_MSG_MOD(extack, |
| 114 | "This device does not support bridging."); |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 115 | return -EOPNOTSUPP; |
| 116 | } |
| 117 | |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 118 | /* HSR over bonded devices has not been tested, but I'm not sure it |
| 119 | * won't work... |
| 120 | */ |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 125 | /* Setup device to be added to the HSR bridge. */ |
Taehee Yoo | e0a4b99 | 2020-02-28 18:02:10 +0000 | [diff] [blame] | 126 | static int hsr_portdev_setup(struct hsr_priv *hsr, struct net_device *dev, |
| 127 | struct hsr_port *port, |
| 128 | struct netlink_ext_ack *extack) |
| 129 | |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 130 | { |
Taehee Yoo | e0a4b99 | 2020-02-28 18:02:10 +0000 | [diff] [blame] | 131 | struct net_device *hsr_dev; |
| 132 | struct hsr_port *master; |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 133 | int res; |
| 134 | |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 135 | res = dev_set_promiscuity(dev, 1); |
| 136 | if (res) |
Taehee Yoo | 56dc0a0 | 2020-03-05 00:02:54 +0000 | [diff] [blame] | 137 | return res; |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 138 | |
Taehee Yoo | e0a4b99 | 2020-02-28 18:02:10 +0000 | [diff] [blame] | 139 | master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); |
| 140 | hsr_dev = master->dev; |
| 141 | |
| 142 | res = netdev_upper_dev_link(dev, hsr_dev, extack); |
| 143 | if (res) |
| 144 | goto fail_upper_dev_link; |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 145 | |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 146 | res = netdev_rx_handler_register(dev, hsr_handle_frame, port); |
| 147 | if (res) |
| 148 | goto fail_rx_handler; |
| 149 | dev_disable_lro(dev); |
| 150 | |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 151 | return 0; |
| 152 | |
| 153 | fail_rx_handler: |
Taehee Yoo | e0a4b99 | 2020-02-28 18:02:10 +0000 | [diff] [blame] | 154 | netdev_upper_dev_unlink(dev, hsr_dev); |
| 155 | fail_upper_dev_link: |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 156 | dev_set_promiscuity(dev, -1); |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 157 | return res; |
| 158 | } |
| 159 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 160 | int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev, |
Taehee Yoo | 13eeb5f | 2020-02-28 18:01:35 +0000 | [diff] [blame] | 161 | enum hsr_port_type type, struct netlink_ext_ack *extack) |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 162 | { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 163 | struct hsr_port *port, *master; |
| 164 | int res; |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 165 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 166 | if (type != HSR_PT_MASTER) { |
Taehee Yoo | 13eeb5f | 2020-02-28 18:01:35 +0000 | [diff] [blame] | 167 | res = hsr_check_dev_ok(dev, extack); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 168 | if (res) |
| 169 | return res; |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 172 | port = hsr_port_get_hsr(hsr, type); |
Murali Karicheri | 05ca6e6 | 2019-04-05 13:31:28 -0400 | [diff] [blame] | 173 | if (port) |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 174 | return -EBUSY; /* This port already exists */ |
| 175 | |
| 176 | port = kzalloc(sizeof(*port), GFP_KERNEL); |
Murali Karicheri | 05ca6e6 | 2019-04-05 13:31:28 -0400 | [diff] [blame] | 177 | if (!port) |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 178 | return -ENOMEM; |
| 179 | |
Taehee Yoo | 3a303cf | 2020-03-21 06:46:50 +0000 | [diff] [blame] | 180 | port->hsr = hsr; |
| 181 | port->dev = dev; |
| 182 | port->type = type; |
| 183 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 184 | if (type != HSR_PT_MASTER) { |
Taehee Yoo | e0a4b99 | 2020-02-28 18:02:10 +0000 | [diff] [blame] | 185 | res = hsr_portdev_setup(hsr, dev, port, extack); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 186 | if (res) |
| 187 | goto fail_dev_setup; |
| 188 | } |
| 189 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 190 | list_add_tail_rcu(&port->port_list, &hsr->ports); |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 191 | synchronize_rcu(); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 192 | |
| 193 | master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); |
Arvid Brodin | 1cc1eb5 | 2014-07-04 23:38:57 +0200 | [diff] [blame] | 194 | netdev_update_features(master->dev); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 195 | dev_set_mtu(master->dev, hsr_get_max_mtu(hsr)); |
| 196 | |
| 197 | return 0; |
| 198 | |
| 199 | fail_dev_setup: |
| 200 | kfree(port); |
| 201 | return res; |
| 202 | } |
| 203 | |
| 204 | void hsr_del_port(struct hsr_port *port) |
| 205 | { |
| 206 | struct hsr_priv *hsr; |
| 207 | struct hsr_port *master; |
| 208 | |
| 209 | hsr = port->hsr; |
| 210 | master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); |
| 211 | list_del_rcu(&port->port_list); |
| 212 | |
| 213 | if (port != master) { |
Taehee Yoo | e0a4b99 | 2020-02-28 18:02:10 +0000 | [diff] [blame] | 214 | netdev_update_features(master->dev); |
| 215 | dev_set_mtu(master->dev, hsr_get_max_mtu(hsr)); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 216 | netdev_rx_handler_unregister(port->dev); |
| 217 | dev_set_promiscuity(port->dev, -1); |
Taehee Yoo | e0a4b99 | 2020-02-28 18:02:10 +0000 | [diff] [blame] | 218 | netdev_upper_dev_unlink(port->dev, master->dev); |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 221 | synchronize_rcu(); |
Arvid Brodin | 56b08fd | 2015-02-27 21:26:03 +0100 | [diff] [blame] | 222 | |
Cong Wang | 619afef0 | 2019-07-03 17:21:12 -0700 | [diff] [blame] | 223 | kfree(port); |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame] | 224 | } |