Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / net / dsa / tag_8021q.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019, Vladimir Oltean <olteanv@gmail.com>
3  *
4  * This module is not a complete tagger implementation. It only provides
5  * primitives for taggers that rely on 802.1Q VLAN tags to use. The
6  * dsa_8021q_netdev_ops is registered for API compliance and not used
7  * directly by callers.
8  */
9 #include <linux/if_bridge.h>
10 #include <linux/if_vlan.h>
11
12 #include "dsa_priv.h"
13
14 /* Binary structure of the fake 12-bit VID field (when the TPID is
15  * ETH_P_DSA_8021Q):
16  *
17  * | 11  | 10  |  9  |  8  |  7  |  6  |  5  |  4  |  3  |  2  |  1  |  0  |
18  * +-----------+-----+-----------------+-----------+-----------------------+
19  * |    DIR    | RSV |    SWITCH_ID    |    RSV    |          PORT         |
20  * +-----------+-----+-----------------+-----------+-----------------------+
21  *
22  * DIR - VID[11:10]:
23  *      Direction flags.
24  *      * 1 (0b01) for RX VLAN,
25  *      * 2 (0b10) for TX VLAN.
26  *      These values make the special VIDs of 0, 1 and 4095 to be left
27  *      unused by this coding scheme.
28  *
29  * RSV - VID[9]:
30  *      To be used for further expansion of SWITCH_ID or for other purposes.
31  *      Must be transmitted as zero and ignored on receive.
32  *
33  * SWITCH_ID - VID[8:6]:
34  *      Index of switch within DSA tree. Must be between 0 and
35  *      DSA_MAX_SWITCHES - 1.
36  *
37  * RSV - VID[5:4]:
38  *      To be used for further expansion of PORT or for other purposes.
39  *      Must be transmitted as zero and ignored on receive.
40  *
41  * PORT - VID[3:0]:
42  *      Index of switch port. Must be between 0 and DSA_MAX_PORTS - 1.
43  */
44
45 #define DSA_8021Q_DIR_SHIFT             10
46 #define DSA_8021Q_DIR_MASK              GENMASK(11, 10)
47 #define DSA_8021Q_DIR(x)                (((x) << DSA_8021Q_DIR_SHIFT) & \
48                                                  DSA_8021Q_DIR_MASK)
49 #define DSA_8021Q_DIR_RX                DSA_8021Q_DIR(1)
50 #define DSA_8021Q_DIR_TX                DSA_8021Q_DIR(2)
51
52 #define DSA_8021Q_SWITCH_ID_SHIFT       6
53 #define DSA_8021Q_SWITCH_ID_MASK        GENMASK(8, 6)
54 #define DSA_8021Q_SWITCH_ID(x)          (((x) << DSA_8021Q_SWITCH_ID_SHIFT) & \
55                                                  DSA_8021Q_SWITCH_ID_MASK)
56
57 #define DSA_8021Q_PORT_SHIFT            0
58 #define DSA_8021Q_PORT_MASK             GENMASK(3, 0)
59 #define DSA_8021Q_PORT(x)               (((x) << DSA_8021Q_PORT_SHIFT) & \
60                                                  DSA_8021Q_PORT_MASK)
61
62 /* Returns the VID to be inserted into the frame from xmit for switch steering
63  * instructions on egress. Encodes switch ID and port ID.
64  */
65 u16 dsa_8021q_tx_vid(struct dsa_switch *ds, int port)
66 {
67         return DSA_8021Q_DIR_TX | DSA_8021Q_SWITCH_ID(ds->index) |
68                DSA_8021Q_PORT(port);
69 }
70 EXPORT_SYMBOL_GPL(dsa_8021q_tx_vid);
71
72 /* Returns the VID that will be installed as pvid for this switch port, sent as
73  * tagged egress towards the CPU port and decoded by the rcv function.
74  */
75 u16 dsa_8021q_rx_vid(struct dsa_switch *ds, int port)
76 {
77         return DSA_8021Q_DIR_RX | DSA_8021Q_SWITCH_ID(ds->index) |
78                DSA_8021Q_PORT(port);
79 }
80 EXPORT_SYMBOL_GPL(dsa_8021q_rx_vid);
81
82 /* Returns the decoded switch ID from the RX VID. */
83 int dsa_8021q_rx_switch_id(u16 vid)
84 {
85         return (vid & DSA_8021Q_SWITCH_ID_MASK) >> DSA_8021Q_SWITCH_ID_SHIFT;
86 }
87 EXPORT_SYMBOL_GPL(dsa_8021q_rx_switch_id);
88
89 /* Returns the decoded port ID from the RX VID. */
90 int dsa_8021q_rx_source_port(u16 vid)
91 {
92         return (vid & DSA_8021Q_PORT_MASK) >> DSA_8021Q_PORT_SHIFT;
93 }
94 EXPORT_SYMBOL_GPL(dsa_8021q_rx_source_port);
95
96 /* RX VLAN tagging (left) and TX VLAN tagging (right) setup shown for a single
97  * front-panel switch port (here swp0).
98  *
99  * Port identification through VLAN (802.1Q) tags has different requirements
100  * for it to work effectively:
101  *  - On RX (ingress from network): each front-panel port must have a pvid
102  *    that uniquely identifies it, and the egress of this pvid must be tagged
103  *    towards the CPU port, so that software can recover the source port based
104  *    on the VID in the frame. But this would only work for standalone ports;
105  *    if bridged, this VLAN setup would break autonomous forwarding and would
106  *    force all switched traffic to pass through the CPU. So we must also make
107  *    the other front-panel ports members of this VID we're adding, albeit
108  *    we're not making it their PVID (they'll still have their own).
109  *    By the way - just because we're installing the same VID in multiple
110  *    switch ports doesn't mean that they'll start to talk to one another, even
111  *    while not bridged: the final forwarding decision is still an AND between
112  *    the L2 forwarding information (which is limiting forwarding in this case)
113  *    and the VLAN-based restrictions (of which there are none in this case,
114  *    since all ports are members).
115  *  - On TX (ingress from CPU and towards network) we are faced with a problem.
116  *    If we were to tag traffic (from within DSA) with the port's pvid, all
117  *    would be well, assuming the switch ports were standalone. Frames would
118  *    have no choice but to be directed towards the correct front-panel port.
119  *    But because we also want the RX VLAN to not break bridging, then
120  *    inevitably that means that we have to give them a choice (of what
121  *    front-panel port to go out on), and therefore we cannot steer traffic
122  *    based on the RX VID. So what we do is simply install one more VID on the
123  *    front-panel and CPU ports, and profit off of the fact that steering will
124  *    work just by virtue of the fact that there is only one other port that's
125  *    a member of the VID we're tagging the traffic with - the desired one.
126  *
127  * So at the end, each front-panel port will have one RX VID (also the PVID),
128  * the RX VID of all other front-panel ports, and one TX VID. Whereas the CPU
129  * port will have the RX and TX VIDs of all front-panel ports, and on top of
130  * that, is also tagged-input and tagged-output (VLAN trunk).
131  *
132  *               CPU port                               CPU port
133  * +-------------+-----+-------------+    +-------------+-----+-------------+
134  * |  RX VID     |     |             |    |  TX VID     |     |             |
135  * |  of swp0    |     |             |    |  of swp0    |     |             |
136  * |             +-----+             |    |             +-----+             |
137  * |                ^ T              |    |                | Tagged         |
138  * |                |                |    |                | ingress        |
139  * |    +-------+---+---+-------+    |    |    +-----------+                |
140  * |    |       |       |       |    |    |    | Untagged                   |
141  * |    |     U v     U v     U v    |    |    v egress                     |
142  * | +-----+ +-----+ +-----+ +-----+ |    | +-----+ +-----+ +-----+ +-----+ |
143  * | |     | |     | |     | |     | |    | |     | |     | |     | |     | |
144  * | |PVID | |     | |     | |     | |    | |     | |     | |     | |     | |
145  * +-+-----+-+-----+-+-----+-+-----+-+    +-+-----+-+-----+-+-----+-+-----+-+
146  *   swp0    swp1    swp2    swp3           swp0    swp1    swp2    swp3
147  */
148 int dsa_port_setup_8021q_tagging(struct dsa_switch *ds, int port, bool enabled)
149 {
150         int upstream = dsa_upstream_port(ds, port);
151         struct dsa_port *dp = &ds->ports[port];
152         struct dsa_port *upstream_dp = &ds->ports[upstream];
153         u16 rx_vid = dsa_8021q_rx_vid(ds, port);
154         u16 tx_vid = dsa_8021q_tx_vid(ds, port);
155         int i, err;
156
157         /* The CPU port is implicitly configured by
158          * configuring the front-panel ports
159          */
160         if (!dsa_is_user_port(ds, port))
161                 return 0;
162
163         /* Add this user port's RX VID to the membership list of all others
164          * (including itself). This is so that bridging will not be hindered.
165          * L2 forwarding rules still take precedence when there are no VLAN
166          * restrictions, so there are no concerns about leaking traffic.
167          */
168         for (i = 0; i < ds->num_ports; i++) {
169                 struct dsa_port *other_dp = &ds->ports[i];
170                 u16 flags;
171
172                 if (i == upstream)
173                         continue;
174                 else if (i == port)
175                         /* The RX VID is pvid on this port */
176                         flags = BRIDGE_VLAN_INFO_UNTAGGED |
177                                 BRIDGE_VLAN_INFO_PVID;
178                 else
179                         /* The RX VID is a regular VLAN on all others */
180                         flags = BRIDGE_VLAN_INFO_UNTAGGED;
181
182                 if (enabled)
183                         err = dsa_port_vid_add(other_dp, rx_vid, flags);
184                 else
185                         err = dsa_port_vid_del(other_dp, rx_vid);
186                 if (err) {
187                         dev_err(ds->dev, "Failed to apply RX VID %d to port %d: %d\n",
188                                 rx_vid, port, err);
189                         return err;
190                 }
191         }
192
193         /* CPU port needs to see this port's RX VID
194          * as tagged egress.
195          */
196         if (enabled)
197                 err = dsa_port_vid_add(upstream_dp, rx_vid, 0);
198         else
199                 err = dsa_port_vid_del(upstream_dp, rx_vid);
200         if (err) {
201                 dev_err(ds->dev, "Failed to apply RX VID %d to port %d: %d\n",
202                         rx_vid, port, err);
203                 return err;
204         }
205
206         /* Finally apply the TX VID on this port and on the CPU port */
207         if (enabled)
208                 err = dsa_port_vid_add(dp, tx_vid, BRIDGE_VLAN_INFO_UNTAGGED);
209         else
210                 err = dsa_port_vid_del(dp, tx_vid);
211         if (err) {
212                 dev_err(ds->dev, "Failed to apply TX VID %d on port %d: %d\n",
213                         tx_vid, port, err);
214                 return err;
215         }
216         if (enabled)
217                 err = dsa_port_vid_add(upstream_dp, tx_vid, 0);
218         else
219                 err = dsa_port_vid_del(upstream_dp, tx_vid);
220         if (err) {
221                 dev_err(ds->dev, "Failed to apply TX VID %d on port %d: %d\n",
222                         tx_vid, upstream, err);
223                 return err;
224         }
225
226         return 0;
227 }
228 EXPORT_SYMBOL_GPL(dsa_port_setup_8021q_tagging);
229
230 struct sk_buff *dsa_8021q_xmit(struct sk_buff *skb, struct net_device *netdev,
231                                u16 tpid, u16 tci)
232 {
233         /* skb->data points at skb_mac_header, which
234          * is fine for vlan_insert_tag.
235          */
236         return vlan_insert_tag(skb, htons(tpid), tci);
237 }
238 EXPORT_SYMBOL_GPL(dsa_8021q_xmit);
239
240 /* In the DSA packet_type handler, skb->data points in the middle of the VLAN
241  * tag, after tpid and before tci. This is because so far, ETH_HLEN
242  * (DMAC, SMAC, EtherType) bytes were pulled.
243  * There are 2 bytes of VLAN tag left in skb->data, and upper
244  * layers expect the 'real' EtherType to be consumed as well.
245  * Coincidentally, a VLAN header is also of the same size as
246  * the number of bytes that need to be pulled.
247  *
248  * skb_mac_header                                      skb->data
249  * |                                                       |
250  * v                                                       v
251  * |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
252  * +-----------------------+-----------------------+-------+-------+-------+
253  * |    Destination MAC    |      Source MAC       |  TPID |  TCI  | EType |
254  * +-----------------------+-----------------------+-------+-------+-------+
255  * ^                                               |               |
256  * |<--VLAN_HLEN-->to                              <---VLAN_HLEN--->
257  * from            |
258  *       >>>>>>>   v
259  *       >>>>>>>   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
260  *       >>>>>>>   +-----------------------+-----------------------+-------+
261  *       >>>>>>>   |    Destination MAC    |      Source MAC       | EType |
262  *                 +-----------------------+-----------------------+-------+
263  *                 ^                                                       ^
264  * (now part of    |                                                       |
265  *  skb->head)     skb_mac_header                                  skb->data
266  */
267 struct sk_buff *dsa_8021q_remove_header(struct sk_buff *skb)
268 {
269         u8 *from = skb_mac_header(skb);
270         u8 *dest = from + VLAN_HLEN;
271
272         memmove(dest, from, ETH_HLEN - VLAN_HLEN);
273         skb_pull(skb, VLAN_HLEN);
274         skb_push(skb, ETH_HLEN);
275         skb_reset_mac_header(skb);
276         skb_reset_mac_len(skb);
277         skb_pull_rcsum(skb, ETH_HLEN);
278
279         return skb;
280 }
281 EXPORT_SYMBOL_GPL(dsa_8021q_remove_header);
282
283 static const struct dsa_device_ops dsa_8021q_netdev_ops = {
284         .name           = "8021q",
285         .proto          = DSA_TAG_PROTO_8021Q,
286         .overhead       = VLAN_HLEN,
287 };
288
289 MODULE_LICENSE("GPL v2");
290 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_8021Q);
291
292 module_dsa_tag_driver(dsa_8021q_netdev_ops);