1 /* SPDX-License-Identifier: GPL-2.0-or-later */
6 #include <linux/if_vlan.h>
7 #include <linux/list.h>
8 #include <linux/types.h>
14 struct dsa_tag_driver {
15 const struct dsa_device_ops *ops;
16 struct list_head list;
20 extern struct packet_type dsa_pack_type;
22 const struct dsa_device_ops *dsa_tag_driver_get_by_id(int tag_protocol);
23 const struct dsa_device_ops *dsa_tag_driver_get_by_name(const char *name);
24 void dsa_tag_driver_put(const struct dsa_device_ops *ops);
25 const char *dsa_tag_protocol_to_str(const struct dsa_device_ops *ops);
27 static inline int dsa_tag_protocol_overhead(const struct dsa_device_ops *ops)
29 return ops->needed_headroom + ops->needed_tailroom;
32 static inline struct net_device *dsa_conduit_find_user(struct net_device *dev,
35 struct dsa_port *cpu_dp = dev->dsa_ptr;
36 struct dsa_switch_tree *dst = cpu_dp->dst;
39 list_for_each_entry(dp, &dst->ports, list)
40 if (dp->ds->index == device && dp->index == port &&
41 dp->type == DSA_PORT_TYPE_USER)
48 * dsa_software_untag_vlan_aware_bridge: Software untagging for VLAN-aware bridge
49 * @skb: Pointer to received socket buffer (packet)
50 * @br: Pointer to bridge upper interface of ingress port
51 * @vid: Parsed VID from packet
53 * The bridge can process tagged packets. Software like STP/PTP may not. The
54 * bridge can also process untagged packets, to the same effect as if they were
55 * tagged with the PVID of the ingress port. So packets tagged with the PVID of
56 * the bridge port must be software-untagged, to support both use cases.
58 static inline void dsa_software_untag_vlan_aware_bridge(struct sk_buff *skb,
59 struct net_device *br,
65 err = br_vlan_get_proto(br, &proto);
69 err = br_vlan_get_pvid_rcu(skb->dev, &pvid);
73 if (vid == pvid && skb->vlan_proto == htons(proto))
74 __vlan_hwaccel_clear_tag(skb);
78 * dsa_software_untag_vlan_unaware_bridge: Software untagging for VLAN-unaware bridge
79 * @skb: Pointer to received socket buffer (packet)
80 * @br: Pointer to bridge upper interface of ingress port
81 * @vid: Parsed VID from packet
83 * The bridge ignores all VLAN tags. Software like STP/PTP may not (it may run
84 * on the plain port, or on a VLAN upper interface). Maybe packets are coming
85 * to software as tagged with a driver-defined VID which is NOT equal to the
86 * PVID of the bridge port (since the bridge is VLAN-unaware, its configuration
87 * should NOT be committed to hardware). DSA needs a method for this private
88 * VID to be communicated by software to it, and if packets are tagged with it,
89 * software-untag them. Note: the private VID may be different per bridge, to
90 * support the FDB isolation use case.
92 * FIXME: this is currently implemented based on the broken assumption that
93 * the "private VID" used by the driver in VLAN-unaware mode is equal to the
94 * bridge PVID. It should not be, except for a coincidence; the bridge PVID is
95 * irrelevant to the data path in the VLAN-unaware mode. Thus, the VID that
96 * this function removes is wrong.
98 * All users of ds->untag_bridge_pvid should fix their drivers, if necessary,
99 * to make the two independent. Only then, if there still remains a need to
100 * strip the private VID from packets, then a new ds->ops->get_private_vid()
101 * API shall be introduced to communicate to DSA what this VID is, which needs
102 * to be stripped here.
104 static inline void dsa_software_untag_vlan_unaware_bridge(struct sk_buff *skb,
105 struct net_device *br,
108 struct net_device *upper_dev;
112 err = br_vlan_get_proto(br, &proto);
116 err = br_vlan_get_pvid_rcu(skb->dev, &pvid);
120 if (vid != pvid || skb->vlan_proto != htons(proto))
123 /* The sad part about attempting to untag from DSA is that we
124 * don't know, unless we check, if the skb will end up in
125 * the bridge's data path - br_allowed_ingress() - or not.
126 * For example, there might be an 8021q upper for the
127 * default_pvid of the bridge, which will steal VLAN-tagged traffic
128 * from the bridge's data path. This is a configuration that DSA
129 * supports because vlan_filtering is 0. In that case, we should
130 * definitely keep the tag, to make sure it keeps working.
132 upper_dev = __vlan_find_dev_deep_rcu(br, htons(proto), vid);
134 __vlan_hwaccel_clear_tag(skb);
138 * dsa_software_vlan_untag: Software VLAN untagging in DSA receive path
139 * @skb: Pointer to socket buffer (packet)
141 * Receive path method for switches which send some packets as VLAN-tagged
142 * towards the CPU port (generally from VLAN-aware bridge ports) even when the
143 * packet was not tagged on the wire. Called when ds->untag_bridge_pvid
144 * (legacy) or ds->untag_vlan_aware_bridge_pvid is set to true.
146 * As a side effect of this method, any VLAN tag from the skb head is moved
149 static inline struct sk_buff *dsa_software_vlan_untag(struct sk_buff *skb)
151 struct dsa_port *dp = dsa_user_to_port(skb->dev);
152 struct net_device *br = dsa_port_bridge_dev_get(dp);
156 /* software untagging for standalone ports not yet necessary */
160 err = br_vlan_get_proto(br, &proto);
164 /* Move VLAN tag from data to hwaccel */
165 if (!skb_vlan_tag_present(skb) && skb->protocol == htons(proto)) {
166 skb = skb_vlan_untag(skb);
171 if (!skb_vlan_tag_present(skb))
174 vid = skb_vlan_tag_get_id(skb);
176 if (br_vlan_enabled(br)) {
177 if (dp->ds->untag_vlan_aware_bridge_pvid)
178 dsa_software_untag_vlan_aware_bridge(skb, br, vid);
180 if (dp->ds->untag_bridge_pvid)
181 dsa_software_untag_vlan_unaware_bridge(skb, br, vid);
187 /* For switches without hardware support for DSA tagging to be able
188 * to support termination through the bridge.
190 static inline struct net_device *
191 dsa_find_designated_bridge_port_by_vid(struct net_device *conduit, u16 vid)
193 struct dsa_port *cpu_dp = conduit->dsa_ptr;
194 struct dsa_switch_tree *dst = cpu_dp->dst;
195 struct bridge_vlan_info vinfo;
196 struct net_device *user;
200 list_for_each_entry(dp, &dst->ports, list) {
201 if (dp->type != DSA_PORT_TYPE_USER)
207 if (dp->stp_state != BR_STATE_LEARNING &&
208 dp->stp_state != BR_STATE_FORWARDING)
211 /* Since the bridge might learn this packet, keep the CPU port
212 * affinity with the port that will be used for the reply on
215 if (dp->cpu_dp != cpu_dp)
220 err = br_vlan_get_info_rcu(user, vid, &vinfo);
230 /* If the ingress port offloads the bridge, we mark the frame as autonomously
231 * forwarded by hardware, so the software bridge doesn't forward in twice, back
232 * to us, because we already did. However, if we're in fallback mode and we do
233 * software bridging, we are not offloading it, therefore the dp->bridge
234 * pointer is not populated, and flooding needs to be done by software (we are
235 * effectively operating in standalone ports mode).
237 static inline void dsa_default_offload_fwd_mark(struct sk_buff *skb)
239 struct dsa_port *dp = dsa_user_to_port(skb->dev);
241 skb->offload_fwd_mark = !!(dp->bridge);
244 /* Helper for removing DSA header tags from packets in the RX path.
245 * Must not be called before skb_pull(len).
249 * | | | | | | | | | | | | | | | | | | |
250 * +-----------------------+-----------------------+---------------+-------+
251 * | Destination MAC | Source MAC | DSA header | EType |
252 * +-----------------------+-----------------------+---------------+-------+
254 * <----- len -----> <----- len ----->
257 * >>>>>>> | | | | | | | | | | | | | | |
258 * >>>>>>> +-----------------------+-----------------------+-------+
259 * >>>>>>> | Destination MAC | Source MAC | EType |
260 * +-----------------------+-----------------------+-------+
265 static inline void dsa_strip_etype_header(struct sk_buff *skb, int len)
267 memmove(skb->data - ETH_HLEN, skb->data - ETH_HLEN - len, 2 * ETH_ALEN);
270 /* Helper for creating space for DSA header tags in TX path packets.
271 * Must not be called before skb_push(len).
275 * <<<<<<< | | | | | | | | | | | | | | |
276 * ^ <<<<<<< +-----------------------+-----------------------+-------+
277 * | <<<<<<< | Destination MAC | Source MAC | EType |
278 * | +-----------------------+-----------------------+-------+
286 * | | | | | | | | | | | | | | | | | | |
287 * +-----------------------+-----------------------+---------------+-------+
288 * | Destination MAC | Source MAC | DSA header | EType |
289 * +-----------------------+-----------------------+---------------+-------+
291 * | <----- len ----->
294 static inline void dsa_alloc_etype_header(struct sk_buff *skb, int len)
296 memmove(skb->data, skb->data + len, 2 * ETH_ALEN);
299 /* On RX, eth_type_trans() on the DSA conduit pulls ETH_HLEN bytes starting from
300 * skb_mac_header(skb), which leaves skb->data pointing at the first byte after
301 * what the DSA conduit perceives as the EtherType (the beginning of the L3
302 * protocol). Since DSA EtherType header taggers treat the EtherType as part of
303 * the DSA tag itself, and the EtherType is 2 bytes in length, the DSA header
304 * is located 2 bytes behind skb->data. Note that EtherType in this context
305 * means the first 2 bytes of the DSA header, not the encapsulated EtherType
306 * that will become visible after the DSA header is stripped.
308 static inline void *dsa_etype_header_pos_rx(struct sk_buff *skb)
310 return skb->data - 2;
313 /* On TX, skb->data points to the MAC header, which means that EtherType
314 * header taggers start exactly where the EtherType is (the EtherType is
315 * treated as part of the DSA header).
317 static inline void *dsa_etype_header_pos_tx(struct sk_buff *skb)
319 return skb->data + 2 * ETH_ALEN;
322 /* Create 2 modaliases per tagging protocol, one to auto-load the module
323 * given the ID reported by get_tag_protocol(), and the other by name.
325 #define DSA_TAG_DRIVER_ALIAS "dsa_tag:"
326 #define MODULE_ALIAS_DSA_TAG_DRIVER(__proto, __name) \
327 MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS __name); \
328 MODULE_ALIAS(DSA_TAG_DRIVER_ALIAS "id-" \
329 __stringify(__proto##_VALUE))
331 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
333 struct module *owner);
334 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
337 #define dsa_tag_driver_module_drivers(__dsa_tag_drivers_array, __count) \
338 static int __init dsa_tag_driver_module_init(void) \
340 dsa_tag_drivers_register(__dsa_tag_drivers_array, __count, \
344 module_init(dsa_tag_driver_module_init); \
346 static void __exit dsa_tag_driver_module_exit(void) \
348 dsa_tag_drivers_unregister(__dsa_tag_drivers_array, __count); \
350 module_exit(dsa_tag_driver_module_exit)
353 * module_dsa_tag_drivers() - Helper macro for registering DSA tag
355 * @__ops_array: Array of tag driver structures
357 * Helper macro for DSA tag drivers which do not do anything special
358 * in module init/exit. Each module may only use this macro once, and
359 * calling it replaces module_init() and module_exit().
361 #define module_dsa_tag_drivers(__ops_array) \
362 dsa_tag_driver_module_drivers(__ops_array, ARRAY_SIZE(__ops_array))
364 #define DSA_TAG_DRIVER_NAME(__ops) dsa_tag_driver ## _ ## __ops
366 /* Create a static structure we can build a linked list of dsa_tag
369 #define DSA_TAG_DRIVER(__ops) \
370 static struct dsa_tag_driver DSA_TAG_DRIVER_NAME(__ops) = { \
375 * module_dsa_tag_driver() - Helper macro for registering a single DSA tag
377 * @__ops: Single tag driver structures
379 * Helper macro for DSA tag drivers which do not do anything special
380 * in module init/exit. Each module may only use this macro once, and
381 * calling it replaces module_init() and module_exit().
383 #define module_dsa_tag_driver(__ops) \
384 DSA_TAG_DRIVER(__ops); \
386 static struct dsa_tag_driver *dsa_tag_driver_array[] = { \
387 &DSA_TAG_DRIVER_NAME(__ops) \
389 module_dsa_tag_drivers(dsa_tag_driver_array)