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