0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include "hsr_slave.h"
0011 #include <linux/etherdevice.h>
0012 #include <linux/if_arp.h>
0013 #include <linux/if_vlan.h>
0014 #include "hsr_main.h"
0015 #include "hsr_device.h"
0016 #include "hsr_forward.h"
0017 #include "hsr_framereg.h"
0018
0019 bool hsr_invalid_dan_ingress_frame(__be16 protocol)
0020 {
0021 return (protocol != htons(ETH_P_PRP) && protocol != htons(ETH_P_HSR));
0022 }
0023
0024 static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
0025 {
0026 struct sk_buff *skb = *pskb;
0027 struct hsr_port *port;
0028 struct hsr_priv *hsr;
0029 __be16 protocol;
0030
0031
0032 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
0033 return RX_HANDLER_PASS;
0034
0035 if (!skb_mac_header_was_set(skb)) {
0036 WARN_ONCE(1, "%s: skb invalid", __func__);
0037 return RX_HANDLER_PASS;
0038 }
0039
0040 port = hsr_port_get_rcu(skb->dev);
0041 if (!port)
0042 goto finish_pass;
0043 hsr = port->hsr;
0044
0045 if (hsr_addr_is_self(port->hsr, eth_hdr(skb)->h_source)) {
0046
0047 kfree_skb(skb);
0048 goto finish_consume;
0049 }
0050
0051
0052
0053
0054
0055 protocol = eth_hdr(skb)->h_proto;
0056
0057 if (!(port->dev->features & NETIF_F_HW_HSR_TAG_RM) &&
0058 hsr->proto_ops->invalid_dan_ingress_frame &&
0059 hsr->proto_ops->invalid_dan_ingress_frame(protocol))
0060 goto finish_pass;
0061
0062 skb_push(skb, ETH_HLEN);
0063 skb_reset_mac_header(skb);
0064 if ((!hsr->prot_version && protocol == htons(ETH_P_PRP)) ||
0065 protocol == htons(ETH_P_HSR))
0066 skb_set_network_header(skb, ETH_HLEN + HSR_HLEN);
0067 skb_reset_mac_len(skb);
0068
0069 hsr_forward_skb(skb, port);
0070
0071 finish_consume:
0072 return RX_HANDLER_CONSUMED;
0073
0074 finish_pass:
0075 return RX_HANDLER_PASS;
0076 }
0077
0078 bool hsr_port_exists(const struct net_device *dev)
0079 {
0080 return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame;
0081 }
0082
0083 static int hsr_check_dev_ok(struct net_device *dev,
0084 struct netlink_ext_ack *extack)
0085 {
0086
0087 if ((dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
0088 dev->addr_len != ETH_ALEN) {
0089 NL_SET_ERR_MSG_MOD(extack, "Cannot use loopback or non-ethernet device as HSR slave.");
0090 return -EINVAL;
0091 }
0092
0093
0094 if (is_hsr_master(dev)) {
0095 NL_SET_ERR_MSG_MOD(extack,
0096 "Cannot create trees of HSR devices.");
0097 return -EINVAL;
0098 }
0099
0100 if (hsr_port_exists(dev)) {
0101 NL_SET_ERR_MSG_MOD(extack,
0102 "This device is already a HSR slave.");
0103 return -EINVAL;
0104 }
0105
0106 if (is_vlan_dev(dev)) {
0107 NL_SET_ERR_MSG_MOD(extack, "HSR on top of VLAN is not yet supported in this driver.");
0108 return -EINVAL;
0109 }
0110
0111 if (dev->priv_flags & IFF_DONT_BRIDGE) {
0112 NL_SET_ERR_MSG_MOD(extack,
0113 "This device does not support bridging.");
0114 return -EOPNOTSUPP;
0115 }
0116
0117
0118
0119
0120
0121 return 0;
0122 }
0123
0124
0125 static int hsr_portdev_setup(struct hsr_priv *hsr, struct net_device *dev,
0126 struct hsr_port *port,
0127 struct netlink_ext_ack *extack)
0128
0129 {
0130 struct net_device *hsr_dev;
0131 struct hsr_port *master;
0132 int res;
0133
0134 res = dev_set_promiscuity(dev, 1);
0135 if (res)
0136 return res;
0137
0138 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
0139 hsr_dev = master->dev;
0140
0141 res = netdev_upper_dev_link(dev, hsr_dev, extack);
0142 if (res)
0143 goto fail_upper_dev_link;
0144
0145 res = netdev_rx_handler_register(dev, hsr_handle_frame, port);
0146 if (res)
0147 goto fail_rx_handler;
0148 dev_disable_lro(dev);
0149
0150 return 0;
0151
0152 fail_rx_handler:
0153 netdev_upper_dev_unlink(dev, hsr_dev);
0154 fail_upper_dev_link:
0155 dev_set_promiscuity(dev, -1);
0156 return res;
0157 }
0158
0159 int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev,
0160 enum hsr_port_type type, struct netlink_ext_ack *extack)
0161 {
0162 struct hsr_port *port, *master;
0163 int res;
0164
0165 if (type != HSR_PT_MASTER) {
0166 res = hsr_check_dev_ok(dev, extack);
0167 if (res)
0168 return res;
0169 }
0170
0171 port = hsr_port_get_hsr(hsr, type);
0172 if (port)
0173 return -EBUSY;
0174
0175 port = kzalloc(sizeof(*port), GFP_KERNEL);
0176 if (!port)
0177 return -ENOMEM;
0178
0179 port->hsr = hsr;
0180 port->dev = dev;
0181 port->type = type;
0182
0183 if (type != HSR_PT_MASTER) {
0184 res = hsr_portdev_setup(hsr, dev, port, extack);
0185 if (res)
0186 goto fail_dev_setup;
0187 }
0188
0189 list_add_tail_rcu(&port->port_list, &hsr->ports);
0190 synchronize_rcu();
0191
0192 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
0193 netdev_update_features(master->dev);
0194 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
0195
0196 return 0;
0197
0198 fail_dev_setup:
0199 kfree(port);
0200 return res;
0201 }
0202
0203 void hsr_del_port(struct hsr_port *port)
0204 {
0205 struct hsr_priv *hsr;
0206 struct hsr_port *master;
0207
0208 hsr = port->hsr;
0209 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
0210 list_del_rcu(&port->port_list);
0211
0212 if (port != master) {
0213 netdev_update_features(master->dev);
0214 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
0215 netdev_rx_handler_unregister(port->dev);
0216 dev_set_promiscuity(port->dev, -1);
0217 netdev_upper_dev_unlink(port->dev, master->dev);
0218 }
0219
0220 synchronize_rcu();
0221
0222 kfree(port);
0223 }