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