1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/etherdevice.h>
16
17#include "dsa_priv.h"
18
19#define MTK_HDR_LEN 4
20#define MTK_HDR_RECV_SOURCE_PORT_MASK GENMASK(2, 0)
21#define MTK_HDR_XMIT_DP_BIT_MASK GENMASK(5, 0)
22
23static struct sk_buff *mtk_tag_xmit(struct sk_buff *skb,
24 struct net_device *dev)
25{
26 struct dsa_slave_priv *p = netdev_priv(dev);
27 u8 *mtk_tag;
28
29 if (skb_cow_head(skb, MTK_HDR_LEN) < 0)
30 return NULL;
31
32 skb_push(skb, MTK_HDR_LEN);
33
34 memmove(skb->data, skb->data + MTK_HDR_LEN, 2 * ETH_ALEN);
35
36
37 mtk_tag = skb->data + 2 * ETH_ALEN;
38 mtk_tag[0] = 0;
39 mtk_tag[1] = (1 << p->dp->index) & MTK_HDR_XMIT_DP_BIT_MASK;
40 mtk_tag[2] = 0;
41 mtk_tag[3] = 0;
42
43 return skb;
44}
45
46static struct sk_buff *mtk_tag_rcv(struct sk_buff *skb, struct net_device *dev,
47 struct packet_type *pt,
48 struct net_device *orig_dev)
49{
50 struct dsa_switch_tree *dst = dev->dsa_ptr;
51 struct dsa_switch *ds;
52 int port;
53 __be16 *phdr, hdr;
54
55 if (unlikely(!pskb_may_pull(skb, MTK_HDR_LEN)))
56 return NULL;
57
58
59
60
61
62 phdr = (__be16 *)(skb->data - 2);
63 hdr = ntohs(*phdr);
64
65
66 skb_pull_rcsum(skb, MTK_HDR_LEN);
67
68 memmove(skb->data - ETH_HLEN,
69 skb->data - ETH_HLEN - MTK_HDR_LEN,
70 2 * ETH_ALEN);
71
72
73
74
75
76 ds = dst->ds[0];
77 if (!ds)
78 return NULL;
79
80
81 port = (hdr & MTK_HDR_RECV_SOURCE_PORT_MASK);
82 if (!ds->ports[port].netdev)
83 return NULL;
84
85 skb->dev = ds->ports[port].netdev;
86
87 return skb;
88}
89
90const struct dsa_device_ops mtk_netdev_ops = {
91 .xmit = mtk_tag_xmit,
92 .rcv = mtk_tag_rcv,
93};
94