blob: 4ff6e02d8b733335b8d85fedb19f7f5eaa546e4e [file] [log] [blame]
Arvid Brodin81ba6af2014-07-04 23:35:24 +02001/* 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 Brodin51f3c602014-07-04 23:37:27 +020014#include <linux/if_arp.h>
Arvid Brodin81ba6af2014-07-04 23:35:24 +020015#include "hsr_main.h"
Arvid Brodin51f3c602014-07-04 23:37:27 +020016#include "hsr_device.h"
Arvid Brodinf266a682014-07-04 23:41:03 +020017#include "hsr_forward.h"
Arvid Brodin81ba6af2014-07-04 23:35:24 +020018#include "hsr_framereg.h"
19
20
Arvid Brodinf266a682014-07-04 23:41:03 +020021static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
22{
23 struct sk_buff *skb = *pskb;
24 struct hsr_port *port;
Peter Heiseee1c2792016-04-13 13:52:22 +020025 u16 protocol;
Arvid Brodinf266a682014-07-04 23:41:03 +020026
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);
Eric Dumazeta29ce732020-02-03 10:15:07 -080034 if (!port)
35 goto finish_pass;
Arvid Brodinf266a682014-07-04 23:41:03 +020036
37 if (hsr_addr_is_self(port->hsr, eth_hdr(skb)->h_source)) {
38 /* Directly kill frames sent by ourselves */
39 kfree_skb(skb);
40 goto finish_consume;
41 }
42
Peter Heiseee1c2792016-04-13 13:52:22 +020043 protocol = eth_hdr(skb)->h_proto;
44 if (protocol != htons(ETH_P_PRP) && protocol != htons(ETH_P_HSR))
Arvid Brodinf266a682014-07-04 23:41:03 +020045 goto finish_pass;
46
47 skb_push(skb, ETH_HLEN);
48
49 hsr_forward_skb(skb, port);
50
51finish_consume:
52 rcu_read_unlock(); /* hsr->node_db, hsr->ports */
53 return RX_HANDLER_CONSUMED;
54
55finish_pass:
56 rcu_read_unlock(); /* hsr->node_db, hsr->ports */
57 return RX_HANDLER_PASS;
58}
59
60bool hsr_port_exists(const struct net_device *dev)
61{
62 return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame;
63}
64
65
Arvid Brodinc5a75912014-07-04 23:38:05 +020066static int hsr_check_dev_ok(struct net_device *dev)
Arvid Brodin51f3c602014-07-04 23:37:27 +020067{
68 /* Don't allow HSR on non-ethernet like devices */
69 if ((dev->flags & IFF_LOOPBACK) || (dev->type != ARPHRD_ETHER) ||
70 (dev->addr_len != ETH_ALEN)) {
71 netdev_info(dev, "Cannot use loopback or non-ethernet device as HSR slave.\n");
72 return -EINVAL;
73 }
74
75 /* Don't allow enslaving hsr devices */
76 if (is_hsr_master(dev)) {
77 netdev_info(dev, "Cannot create trees of HSR devices.\n");
78 return -EINVAL;
79 }
80
Arvid Brodinc5a75912014-07-04 23:38:05 +020081 if (hsr_port_exists(dev)) {
Arvid Brodin51f3c602014-07-04 23:37:27 +020082 netdev_info(dev, "This device is already a HSR slave.\n");
83 return -EINVAL;
84 }
85
86 if (dev->priv_flags & IFF_802_1Q_VLAN) {
87 netdev_info(dev, "HSR on top of VLAN is not yet supported in this driver.\n");
88 return -EINVAL;
89 }
90
Arvid Brodinf266a682014-07-04 23:41:03 +020091 if (dev->priv_flags & IFF_DONT_BRIDGE) {
92 netdev_info(dev, "This device does not support bridging.\n");
93 return -EOPNOTSUPP;
94 }
95
Arvid Brodin51f3c602014-07-04 23:37:27 +020096 /* HSR over bonded devices has not been tested, but I'm not sure it
97 * won't work...
98 */
99
100 return 0;
101}
102
103
Arvid Brodinc5a75912014-07-04 23:38:05 +0200104/* Setup device to be added to the HSR bridge. */
105static int hsr_portdev_setup(struct net_device *dev, struct hsr_port *port)
Arvid Brodin51f3c602014-07-04 23:37:27 +0200106{
107 int res;
108
109 dev_hold(dev);
Arvid Brodin51f3c602014-07-04 23:37:27 +0200110 res = dev_set_promiscuity(dev, 1);
111 if (res)
Arvid Brodinc5a75912014-07-04 23:38:05 +0200112 goto fail_promiscuity;
Arvid Brodin51f3c602014-07-04 23:37:27 +0200113
Arvid Brodinc5a75912014-07-04 23:38:05 +0200114 /* FIXME:
115 * What does net device "adjacency" mean? Should we do
116 * res = netdev_master_upper_dev_link(port->dev, port->hsr->dev); ?
117 */
Arvid Brodin51f3c602014-07-04 23:37:27 +0200118
Arvid Brodinf266a682014-07-04 23:41:03 +0200119 res = netdev_rx_handler_register(dev, hsr_handle_frame, port);
120 if (res)
121 goto fail_rx_handler;
122 dev_disable_lro(dev);
123
Arvid Brodin51f3c602014-07-04 23:37:27 +0200124 return 0;
125
126fail_rx_handler:
127 dev_set_promiscuity(dev, -1);
Arvid Brodinc5a75912014-07-04 23:38:05 +0200128fail_promiscuity:
Arvid Brodin51f3c602014-07-04 23:37:27 +0200129 dev_put(dev);
Arvid Brodinc5a75912014-07-04 23:38:05 +0200130
Arvid Brodin51f3c602014-07-04 23:37:27 +0200131 return res;
132}
133
Arvid Brodinc5a75912014-07-04 23:38:05 +0200134int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev,
135 enum hsr_port_type type)
Arvid Brodin51f3c602014-07-04 23:37:27 +0200136{
Arvid Brodinc5a75912014-07-04 23:38:05 +0200137 struct hsr_port *port, *master;
138 int res;
Arvid Brodin51f3c602014-07-04 23:37:27 +0200139
Arvid Brodinc5a75912014-07-04 23:38:05 +0200140 if (type != HSR_PT_MASTER) {
141 res = hsr_check_dev_ok(dev);
142 if (res)
143 return res;
Arvid Brodin51f3c602014-07-04 23:37:27 +0200144 }
145
Arvid Brodinc5a75912014-07-04 23:38:05 +0200146 port = hsr_port_get_hsr(hsr, type);
147 if (port != NULL)
148 return -EBUSY; /* This port already exists */
149
150 port = kzalloc(sizeof(*port), GFP_KERNEL);
151 if (port == NULL)
152 return -ENOMEM;
153
154 if (type != HSR_PT_MASTER) {
155 res = hsr_portdev_setup(dev, port);
156 if (res)
157 goto fail_dev_setup;
158 }
159
160 port->hsr = hsr;
161 port->dev = dev;
162 port->type = type;
163
164 list_add_tail_rcu(&port->port_list, &hsr->ports);
Arvid Brodin51f3c602014-07-04 23:37:27 +0200165 synchronize_rcu();
Arvid Brodinc5a75912014-07-04 23:38:05 +0200166
167 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
Arvid Brodin1cc1eb52014-07-04 23:38:57 +0200168 netdev_update_features(master->dev);
Arvid Brodinc5a75912014-07-04 23:38:05 +0200169 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
170
171 return 0;
172
173fail_dev_setup:
174 kfree(port);
175 return res;
176}
177
178void hsr_del_port(struct hsr_port *port)
179{
180 struct hsr_priv *hsr;
181 struct hsr_port *master;
182
183 hsr = port->hsr;
184 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
185 list_del_rcu(&port->port_list);
186
187 if (port != master) {
Arvid Brodin56b08fd2015-02-27 21:26:03 +0100188 if (master != NULL) {
189 netdev_update_features(master->dev);
190 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
191 }
Arvid Brodinc5a75912014-07-04 23:38:05 +0200192 netdev_rx_handler_unregister(port->dev);
193 dev_set_promiscuity(port->dev, -1);
194 }
195
196 /* FIXME?
197 * netdev_upper_dev_unlink(port->dev, port->hsr->dev);
198 */
199
200 synchronize_rcu();
Arvid Brodin56b08fd2015-02-27 21:26:03 +0100201
202 if (port != master)
203 dev_put(port->dev);
Arvid Brodin51f3c602014-07-04 23:37:27 +0200204}