Blame view

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

d46193363   Vladimir Oltean   net: dsa: tag_802...
329
330
331
332
333
334
  	memmove(dest, from, ETH_HLEN - VLAN_HLEN);
  	skb_pull(skb, VLAN_HLEN);
  	skb_push(skb, ETH_HLEN);
  	skb_reset_mac_header(skb);
  	skb_reset_mac_len(skb);
  	skb_pull_rcsum(skb, ETH_HLEN);
f9bbe4477   Vladimir Oltean   net: dsa: Optiona...
335
336
337
  
  	return skb;
  }
d46193363   Vladimir Oltean   net: dsa: tag_802...
338
  EXPORT_SYMBOL_GPL(dsa_8021q_remove_header);
f9bbe4477   Vladimir Oltean   net: dsa: Optiona...
339

f9bbe4477   Vladimir Oltean   net: dsa: Optiona...
340
  MODULE_LICENSE("GPL v2");