1 // SPDX-License-Identifier: GPL-2.0
3 * Handler for Realtek 4 byte DSA switch tags
4 * Currently only supports protocol "A" found in RTL8366RB
5 * Copyright (c) 2020 Linus Walleij <linus.walleij@linaro.org>
7 * This "proprietary tag" header looks like so:
9 * -------------------------------------------------
10 * | MAC DA | MAC SA | 0x8899 | 2 bytes tag | Type |
11 * -------------------------------------------------
13 * The 2 bytes tag form a 16 bit big endian word. The exact
14 * meaning has been guessed from packet dumps from ingress
15 * frames, as no working egress traffic has been available
16 * we do not know the format of the egress tags or if they
20 #include <linux/etherdevice.h>
21 #include <linux/bits.h>
25 #define RTL4_A_HDR_LEN 4
26 #define RTL4_A_ETHERTYPE 0x8899
27 #define RTL4_A_PROTOCOL_SHIFT 12
29 * 0x1 = Realtek Remote Control protocol (RRCP)
30 * 0x2/0x3 seems to be used for loopback testing
31 * 0x9 = RTL8306 DSA protocol
32 * 0xa = RTL8366RB DSA protocol
34 #define RTL4_A_PROTOCOL_RTL8366RB 0xa
36 static struct sk_buff *rtl4a_tag_xmit(struct sk_buff *skb,
37 struct net_device *dev)
40 * Just let it pass thru, we don't know if it is possible
41 * to tag a frame with the 0x8899 ethertype and direct it
42 * to a specific port, all attempts at reverse-engineering have
43 * ended up with the frames getting dropped.
45 * The VLAN set-up needs to restrict the frames to the right port.
47 * If you have documentation on the tagging format for RTL8366RB
48 * (tag type A) then please contribute.
53 static struct sk_buff *rtl4a_tag_rcv(struct sk_buff *skb,
54 struct net_device *dev,
55 struct packet_type *pt)
64 if (unlikely(!pskb_may_pull(skb, RTL4_A_HDR_LEN)))
67 /* The RTL4 header has its own custom Ethertype 0x8899 and that
68 * starts right at the beginning of the packet, after the src
69 * ethernet addr. Apparantly skb->data always points 2 bytes in,
70 * behind the Ethertype.
75 if (etype != RTL4_A_ETHERTYPE) {
76 /* Not custom, just pass through */
77 netdev_dbg(dev, "non-realtek ethertype 0x%04x\n", etype);
80 p = (__be16 *)(tag + 2);
82 /* The 4 upper bits are the protocol */
83 prot = (protport >> RTL4_A_PROTOCOL_SHIFT) & 0x0f;
84 if (prot != RTL4_A_PROTOCOL_RTL8366RB) {
85 netdev_err(dev, "unknown realtek protocol 0x%01x\n", prot);
88 port = protport & 0xff;
90 skb->dev = dsa_master_find_slave(dev, 0, port);
92 netdev_dbg(dev, "could not find slave for port %d\n", port);
96 /* Remove RTL4 tag and recalculate checksum */
97 skb_pull_rcsum(skb, RTL4_A_HDR_LEN);
99 /* Move ethernet DA and SA in front of the data */
100 memmove(skb->data - ETH_HLEN,
101 skb->data - ETH_HLEN - RTL4_A_HDR_LEN,
104 skb->offload_fwd_mark = 1;
109 static const struct dsa_device_ops rtl4a_netdev_ops = {
111 .proto = DSA_TAG_PROTO_RTL4_A,
112 .xmit = rtl4a_tag_xmit,
113 .rcv = rtl4a_tag_rcv,
114 .overhead = RTL4_A_HDR_LEN,
116 module_dsa_tag_driver(rtl4a_netdev_ops);
118 MODULE_LICENSE("GPL");
119 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_RTL4_A);