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