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