1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * VLAN An implementation of 802.1Q VLAN tagging.
4 *
5 * Authors: Ben Greear <greearb@candelatech.com>
6 */
7 #ifndef _LINUX_IF_VLAN_H_
8 #define _LINUX_IF_VLAN_H_
9
10 #include <linux/netdevice.h>
11 #include <linux/etherdevice.h>
12 #include <linux/rtnetlink.h>
13 #include <linux/bug.h>
14 #include <uapi/linux/if_vlan.h>
15
16 #define VLAN_HLEN 4 /* The additional bytes required by VLAN
17 * (in addition to the Ethernet header)
18 */
19 #define VLAN_ETH_HLEN 18 /* Total octets in header. */
20 #define VLAN_ETH_ZLEN 64 /* Min. octets in frame sans FCS */
21
22 /*
23 * According to 802.3ac, the packet can be 4 bytes longer. --Klika Jan
24 */
25 #define VLAN_ETH_DATA_LEN 1500 /* Max. octets in payload */
26 #define VLAN_ETH_FRAME_LEN 1518 /* Max. octets in frame sans FCS */
27
28 #define VLAN_MAX_DEPTH 8 /* Max. number of nested VLAN tags parsed */
29
30 /*
31 * struct vlan_hdr - vlan header
32 * @h_vlan_TCI: priority and VLAN ID
33 * @h_vlan_encapsulated_proto: packet type ID or len
34 */
35 struct vlan_hdr {
36 __be16 h_vlan_TCI;
37 __be16 h_vlan_encapsulated_proto;
38 };
39
40 /**
41 * struct vlan_ethhdr - vlan ethernet header (ethhdr + vlan_hdr)
42 * @h_dest: destination ethernet address
43 * @h_source: source ethernet address
44 * @h_vlan_proto: ethernet protocol
45 * @h_vlan_TCI: priority and VLAN ID
46 * @h_vlan_encapsulated_proto: packet type ID or len
47 */
48 struct vlan_ethhdr {
49 struct_group(addrs,
50 unsigned char h_dest[ETH_ALEN];
51 unsigned char h_source[ETH_ALEN];
52 );
53 __be16 h_vlan_proto;
54 __be16 h_vlan_TCI;
55 __be16 h_vlan_encapsulated_proto;
56 };
57
58 #include <linux/skbuff.h>
59
vlan_eth_hdr(const struct sk_buff * skb)60 static inline struct vlan_ethhdr *vlan_eth_hdr(const struct sk_buff *skb)
61 {
62 return (struct vlan_ethhdr *)skb_mac_header(skb);
63 }
64
65 /* Prefer this version in TX path, instead of
66 * skb_reset_mac_header() + vlan_eth_hdr()
67 */
skb_vlan_eth_hdr(const struct sk_buff * skb)68 static inline struct vlan_ethhdr *skb_vlan_eth_hdr(const struct sk_buff *skb)
69 {
70 return (struct vlan_ethhdr *)skb->data;
71 }
72
73 #define VLAN_PRIO_MASK 0xe000 /* Priority Code Point */
74 #define VLAN_PRIO_SHIFT 13
75 #define VLAN_CFI_MASK 0x1000 /* Canonical Format Indicator / Drop Eligible Indicator */
76 #define VLAN_VID_MASK 0x0fff /* VLAN Identifier */
77 #define VLAN_N_VID 4096
78
79 /* found in socket.c */
80 extern void vlan_ioctl_set(int (*hook)(struct net *, void __user *));
81
82 #define skb_vlan_tag_present(__skb) (!!(__skb)->vlan_all)
83 #define skb_vlan_tag_get(__skb) ((__skb)->vlan_tci)
84 #define skb_vlan_tag_get_id(__skb) ((__skb)->vlan_tci & VLAN_VID_MASK)
85 #define skb_vlan_tag_get_cfi(__skb) (!!((__skb)->vlan_tci & VLAN_CFI_MASK))
86 #define skb_vlan_tag_get_prio(__skb) (((__skb)->vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT)
87
vlan_get_rx_ctag_filter_info(struct net_device * dev)88 static inline int vlan_get_rx_ctag_filter_info(struct net_device *dev)
89 {
90 ASSERT_RTNL();
91 return notifier_to_errno(call_netdevice_notifiers(NETDEV_CVLAN_FILTER_PUSH_INFO, dev));
92 }
93
vlan_drop_rx_ctag_filter_info(struct net_device * dev)94 static inline void vlan_drop_rx_ctag_filter_info(struct net_device *dev)
95 {
96 ASSERT_RTNL();
97 call_netdevice_notifiers(NETDEV_CVLAN_FILTER_DROP_INFO, dev);
98 }
99
vlan_get_rx_stag_filter_info(struct net_device * dev)100 static inline int vlan_get_rx_stag_filter_info(struct net_device *dev)
101 {
102 ASSERT_RTNL();
103 return notifier_to_errno(call_netdevice_notifiers(NETDEV_SVLAN_FILTER_PUSH_INFO, dev));
104 }
105
vlan_drop_rx_stag_filter_info(struct net_device * dev)106 static inline void vlan_drop_rx_stag_filter_info(struct net_device *dev)
107 {
108 ASSERT_RTNL();
109 call_netdevice_notifiers(NETDEV_SVLAN_FILTER_DROP_INFO, dev);
110 }
111
112 /**
113 * struct vlan_pcpu_stats - VLAN percpu rx/tx stats
114 * @rx_packets: number of received packets
115 * @rx_bytes: number of received bytes
116 * @rx_multicast: number of received multicast packets
117 * @tx_packets: number of transmitted packets
118 * @tx_bytes: number of transmitted bytes
119 * @syncp: synchronization point for 64bit counters
120 * @rx_errors: number of rx errors
121 * @tx_dropped: number of tx drops
122 */
123 struct vlan_pcpu_stats {
124 u64_stats_t rx_packets;
125 u64_stats_t rx_bytes;
126 u64_stats_t rx_multicast;
127 u64_stats_t tx_packets;
128 u64_stats_t tx_bytes;
129 struct u64_stats_sync syncp;
130 u32 rx_errors;
131 u32 tx_dropped;
132 };
133
134 #if IS_ENABLED(CONFIG_VLAN_8021Q)
135
136 extern struct net_device *__vlan_find_dev_deep_rcu(struct net_device *real_dev,
137 __be16 vlan_proto, u16 vlan_id);
138 extern int vlan_for_each(struct net_device *dev,
139 int (*action)(struct net_device *dev, int vid,
140 void *arg), void *arg);
141 extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
142 extern u16 vlan_dev_vlan_id(const struct net_device *dev);
143 extern __be16 vlan_dev_vlan_proto(const struct net_device *dev);
144
145 /**
146 * struct vlan_priority_tci_mapping - vlan egress priority mappings
147 * @priority: skb priority
148 * @vlan_qos: vlan priority: (skb->priority << 13) & 0xE000
149 * @next: pointer to next struct
150 */
151 struct vlan_priority_tci_mapping {
152 u32 priority;
153 u16 vlan_qos;
154 struct vlan_priority_tci_mapping *next;
155 };
156
157 struct proc_dir_entry;
158 struct netpoll;
159
160 /**
161 * struct vlan_dev_priv - VLAN private device data
162 * @nr_ingress_mappings: number of ingress priority mappings
163 * @ingress_priority_map: ingress priority mappings
164 * @nr_egress_mappings: number of egress priority mappings
165 * @egress_priority_map: hash of egress priority mappings
166 * @vlan_proto: VLAN encapsulation protocol
167 * @vlan_id: VLAN identifier
168 * @flags: device flags
169 * @real_dev: underlying netdevice
170 * @dev_tracker: refcount tracker for @real_dev reference
171 * @real_dev_addr: address of underlying netdevice
172 * @dent: proc dir entry
173 * @vlan_pcpu_stats: ptr to percpu rx stats
174 * @netpoll: netpoll instance "propagated" down to @real_dev
175 */
176 struct vlan_dev_priv {
177 unsigned int nr_ingress_mappings;
178 u32 ingress_priority_map[8];
179 unsigned int nr_egress_mappings;
180 struct vlan_priority_tci_mapping *egress_priority_map[16];
181
182 __be16 vlan_proto;
183 u16 vlan_id;
184 u16 flags;
185
186 struct net_device *real_dev;
187 netdevice_tracker dev_tracker;
188
189 unsigned char real_dev_addr[ETH_ALEN];
190
191 struct proc_dir_entry *dent;
192 struct vlan_pcpu_stats __percpu *vlan_pcpu_stats;
193 #ifdef CONFIG_NET_POLL_CONTROLLER
194 struct netpoll *netpoll;
195 #endif
196 };
197
is_vlan_dev(const struct net_device * dev)198 static inline bool is_vlan_dev(const struct net_device *dev)
199 {
200 return dev->priv_flags & IFF_802_1Q_VLAN;
201 }
202
vlan_dev_priv(const struct net_device * dev)203 static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
204 {
205 return netdev_priv(dev);
206 }
207
208 static inline u16
vlan_dev_get_egress_qos_mask(struct net_device * dev,u32 skprio)209 vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio)
210 {
211 struct vlan_priority_tci_mapping *mp;
212
213 smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */
214
215 mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)];
216 while (mp) {
217 if (mp->priority == skprio) {
218 return mp->vlan_qos; /* This should already be shifted
219 * to mask correctly with the
220 * VLAN's TCI */
221 }
222 mp = mp->next;
223 }
224 return 0;
225 }
226
227 extern bool vlan_do_receive(struct sk_buff **skb);
228
229 extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid);
230 extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid);
231
232 extern int vlan_vids_add_by_dev(struct net_device *dev,
233 const struct net_device *by_dev);
234 extern void vlan_vids_del_by_dev(struct net_device *dev,
235 const struct net_device *by_dev);
236
237 extern bool vlan_uses_dev(const struct net_device *dev);
238
239 #else
is_vlan_dev(const struct net_device * dev)240 static inline bool is_vlan_dev(const struct net_device *dev)
241 {
242 return false;
243 }
244
245 static inline struct net_device *
__vlan_find_dev_deep_rcu(struct net_device * real_dev,__be16 vlan_proto,u16 vlan_id)246 __vlan_find_dev_deep_rcu(struct net_device *real_dev,
247 __be16 vlan_proto, u16 vlan_id)
248 {
249 return NULL;
250 }
251
252 static inline int
vlan_for_each(struct net_device * dev,int (* action)(struct net_device * dev,int vid,void * arg),void * arg)253 vlan_for_each(struct net_device *dev,
254 int (*action)(struct net_device *dev, int vid, void *arg),
255 void *arg)
256 {
257 return 0;
258 }
259
vlan_dev_real_dev(const struct net_device * dev)260 static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
261 {
262 WARN_ON_ONCE(1);
263 return NULL;
264 }
265
vlan_dev_vlan_id(const struct net_device * dev)266 static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
267 {
268 WARN_ON_ONCE(1);
269 return 0;
270 }
271
vlan_dev_vlan_proto(const struct net_device * dev)272 static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev)
273 {
274 WARN_ON_ONCE(1);
275 return 0;
276 }
277
vlan_dev_get_egress_qos_mask(struct net_device * dev,u32 skprio)278 static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev,
279 u32 skprio)
280 {
281 return 0;
282 }
283
vlan_do_receive(struct sk_buff ** skb)284 static inline bool vlan_do_receive(struct sk_buff **skb)
285 {
286 return false;
287 }
288
vlan_vid_add(struct net_device * dev,__be16 proto,u16 vid)289 static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
290 {
291 return 0;
292 }
293
vlan_vid_del(struct net_device * dev,__be16 proto,u16 vid)294 static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
295 {
296 }
297
vlan_vids_add_by_dev(struct net_device * dev,const struct net_device * by_dev)298 static inline int vlan_vids_add_by_dev(struct net_device *dev,
299 const struct net_device *by_dev)
300 {
301 return 0;
302 }
303
vlan_vids_del_by_dev(struct net_device * dev,const struct net_device * by_dev)304 static inline void vlan_vids_del_by_dev(struct net_device *dev,
305 const struct net_device *by_dev)
306 {
307 }
308
vlan_uses_dev(const struct net_device * dev)309 static inline bool vlan_uses_dev(const struct net_device *dev)
310 {
311 return false;
312 }
313 #endif
314
315 /**
316 * eth_type_vlan - check for valid vlan ether type.
317 * @ethertype: ether type to check
318 *
319 * Returns: true if the ether type is a vlan ether type.
320 */
eth_type_vlan(__be16 ethertype)321 static inline bool eth_type_vlan(__be16 ethertype)
322 {
323 switch (ethertype) {
324 case htons(ETH_P_8021Q):
325 case htons(ETH_P_8021AD):
326 return true;
327 default:
328 return false;
329 }
330 }
331
vlan_hw_offload_capable(netdev_features_t features,__be16 proto)332 static inline bool vlan_hw_offload_capable(netdev_features_t features,
333 __be16 proto)
334 {
335 if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
336 return true;
337 if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
338 return true;
339 return false;
340 }
341
342 /**
343 * __vlan_insert_inner_tag - inner VLAN tag inserting
344 * @skb: skbuff to tag
345 * @vlan_proto: VLAN encapsulation protocol
346 * @vlan_tci: VLAN TCI to insert
347 * @mac_len: MAC header length including outer vlan headers
348 *
349 * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
350 * Does not change skb->protocol so this function can be used during receive.
351 *
352 * Returns: error if skb_cow_head fails.
353 */
__vlan_insert_inner_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci,unsigned int mac_len)354 static inline int __vlan_insert_inner_tag(struct sk_buff *skb,
355 __be16 vlan_proto, u16 vlan_tci,
356 unsigned int mac_len)
357 {
358 const u8 meta_len = mac_len > ETH_TLEN ? skb_metadata_len(skb) : 0;
359 struct vlan_ethhdr *veth;
360
361 if (skb_cow_head(skb, meta_len + VLAN_HLEN) < 0)
362 return -ENOMEM;
363
364 skb_push(skb, VLAN_HLEN);
365
366 /* Move the mac header sans proto to the beginning of the new header. */
367 if (likely(mac_len > ETH_TLEN))
368 skb_postpush_data_move(skb, VLAN_HLEN, mac_len - ETH_TLEN);
369 if (skb_mac_header_was_set(skb))
370 skb->mac_header -= VLAN_HLEN;
371
372 veth = (struct vlan_ethhdr *)(skb->data + mac_len - ETH_HLEN);
373
374 /* first, the ethernet type */
375 if (likely(mac_len >= ETH_TLEN)) {
376 /* h_vlan_encapsulated_proto should already be populated, and
377 * skb->data has space for h_vlan_proto
378 */
379 veth->h_vlan_proto = vlan_proto;
380 } else {
381 /* h_vlan_encapsulated_proto should not be populated, and
382 * skb->data has no space for h_vlan_proto
383 */
384 veth->h_vlan_encapsulated_proto = skb->protocol;
385 }
386
387 /* now, the TCI */
388 veth->h_vlan_TCI = htons(vlan_tci);
389
390 return 0;
391 }
392
393 /**
394 * __vlan_insert_tag - regular VLAN tag inserting
395 * @skb: skbuff to tag
396 * @vlan_proto: VLAN encapsulation protocol
397 * @vlan_tci: VLAN TCI to insert
398 *
399 * Inserts the VLAN tag into @skb as part of the payload
400 * Does not change skb->protocol so this function can be used during receive.
401 *
402 * Returns: error if skb_cow_head fails.
403 */
__vlan_insert_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci)404 static inline int __vlan_insert_tag(struct sk_buff *skb,
405 __be16 vlan_proto, u16 vlan_tci)
406 {
407 return __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
408 }
409
410 /**
411 * vlan_insert_inner_tag - inner VLAN tag inserting
412 * @skb: skbuff to tag
413 * @vlan_proto: VLAN encapsulation protocol
414 * @vlan_tci: VLAN TCI to insert
415 * @mac_len: MAC header length including outer vlan headers
416 *
417 * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
418 * Returns a VLAN tagged skb. This might change skb->head.
419 *
420 * Following the skb_unshare() example, in case of error, the calling function
421 * doesn't have to worry about freeing the original skb.
422 *
423 * Does not change skb->protocol so this function can be used during receive.
424 *
425 * Return: modified @skb on success, NULL on error (@skb is freed).
426 */
vlan_insert_inner_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci,unsigned int mac_len)427 static inline struct sk_buff *vlan_insert_inner_tag(struct sk_buff *skb,
428 __be16 vlan_proto,
429 u16 vlan_tci,
430 unsigned int mac_len)
431 {
432 int err;
433
434 err = __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, mac_len);
435 if (err) {
436 dev_kfree_skb_any(skb);
437 return NULL;
438 }
439 return skb;
440 }
441
442 /**
443 * vlan_insert_tag - regular VLAN tag inserting
444 * @skb: skbuff to tag
445 * @vlan_proto: VLAN encapsulation protocol
446 * @vlan_tci: VLAN TCI to insert
447 *
448 * Inserts the VLAN tag into @skb as part of the payload
449 * Returns a VLAN tagged skb. This might change skb->head.
450 *
451 * Following the skb_unshare() example, in case of error, the calling function
452 * doesn't have to worry about freeing the original skb.
453 *
454 * Does not change skb->protocol so this function can be used during receive.
455 *
456 * Return: modified @skb on success, NULL on error (@skb is freed).
457 */
vlan_insert_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci)458 static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
459 __be16 vlan_proto, u16 vlan_tci)
460 {
461 return vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
462 }
463
464 /**
465 * vlan_insert_tag_set_proto - regular VLAN tag inserting
466 * @skb: skbuff to tag
467 * @vlan_proto: VLAN encapsulation protocol
468 * @vlan_tci: VLAN TCI to insert
469 *
470 * Inserts the VLAN tag into @skb as part of the payload
471 * Returns a VLAN tagged skb. This might change skb->head.
472 *
473 * Following the skb_unshare() example, in case of error, the calling function
474 * doesn't have to worry about freeing the original skb.
475 *
476 * Return: modified @skb on success, NULL on error (@skb is freed).
477 */
vlan_insert_tag_set_proto(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci)478 static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb,
479 __be16 vlan_proto,
480 u16 vlan_tci)
481 {
482 skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
483 if (skb)
484 skb->protocol = vlan_proto;
485 return skb;
486 }
487
488 /**
489 * __vlan_hwaccel_clear_tag - clear hardware accelerated VLAN info
490 * @skb: skbuff to clear
491 *
492 * Clears the VLAN information from @skb
493 */
__vlan_hwaccel_clear_tag(struct sk_buff * skb)494 static inline void __vlan_hwaccel_clear_tag(struct sk_buff *skb)
495 {
496 skb->vlan_all = 0;
497 }
498
499 /**
500 * __vlan_hwaccel_copy_tag - copy hardware accelerated VLAN info from another skb
501 * @dst: skbuff to copy to
502 * @src: skbuff to copy from
503 *
504 * Copies VLAN information from @src to @dst (for branchless code)
505 */
__vlan_hwaccel_copy_tag(struct sk_buff * dst,const struct sk_buff * src)506 static inline void __vlan_hwaccel_copy_tag(struct sk_buff *dst, const struct sk_buff *src)
507 {
508 dst->vlan_all = src->vlan_all;
509 }
510
511 /*
512 * __vlan_hwaccel_push_inside - pushes vlan tag to the payload
513 * @skb: skbuff to tag
514 *
515 * Pushes the VLAN tag from @skb->vlan_tci inside to the payload.
516 *
517 * Following the skb_unshare() example, in case of error, the calling function
518 * doesn't have to worry about freeing the original skb.
519 */
__vlan_hwaccel_push_inside(struct sk_buff * skb)520 static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb)
521 {
522 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
523 skb_vlan_tag_get(skb));
524 if (likely(skb))
525 __vlan_hwaccel_clear_tag(skb);
526 return skb;
527 }
528
529 /**
530 * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
531 * @skb: skbuff to tag
532 * @vlan_proto: VLAN encapsulation protocol
533 * @vlan_tci: VLAN TCI to insert
534 *
535 * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
536 */
__vlan_hwaccel_put_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci)537 static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb,
538 __be16 vlan_proto, u16 vlan_tci)
539 {
540 skb->vlan_proto = vlan_proto;
541 skb->vlan_tci = vlan_tci;
542 }
543
544 /**
545 * __vlan_get_tag - get the VLAN ID that is part of the payload
546 * @skb: skbuff to query
547 * @vlan_tci: buffer to store value
548 *
549 * Returns: error if the skb is not of VLAN type
550 */
__vlan_get_tag(const struct sk_buff * skb,u16 * vlan_tci)551 static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
552 {
553 struct vlan_ethhdr *veth = skb_vlan_eth_hdr(skb);
554
555 if (!eth_type_vlan(veth->h_vlan_proto))
556 return -ENODATA;
557
558 *vlan_tci = ntohs(veth->h_vlan_TCI);
559 return 0;
560 }
561
562 /**
563 * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
564 * @skb: skbuff to query
565 * @vlan_tci: buffer to store value
566 *
567 * Returns: error if @skb->vlan_tci is not set correctly
568 */
__vlan_hwaccel_get_tag(const struct sk_buff * skb,u16 * vlan_tci)569 static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
570 u16 *vlan_tci)
571 {
572 if (skb_vlan_tag_present(skb)) {
573 *vlan_tci = skb_vlan_tag_get(skb);
574 return 0;
575 } else {
576 *vlan_tci = 0;
577 return -ENODATA;
578 }
579 }
580
581 /**
582 * vlan_get_tag - get the VLAN ID from the skb
583 * @skb: skbuff to query
584 * @vlan_tci: buffer to store value
585 *
586 * Returns: error if the skb is not VLAN tagged
587 */
vlan_get_tag(const struct sk_buff * skb,u16 * vlan_tci)588 static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
589 {
590 if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
591 return __vlan_hwaccel_get_tag(skb, vlan_tci);
592 } else {
593 return __vlan_get_tag(skb, vlan_tci);
594 }
595 }
596
597 struct vlan_type_depth {
598 __be16 type;
599 u16 depth;
600 };
601
602 struct vlan_type_depth __vlan_get_protocol_offset(const struct sk_buff *skb,
603 __be16 type,
604 int mac_offset);
605
606 /**
607 * vlan_get_protocol_offset_inline() - get protocol EtherType.
608 * @skb: skbuff to query
609 * @type: first vlan protocol
610 * @mac_offset: MAC offset
611 * @depth: buffer to store length of eth and vlan tags in bytes
612 *
613 * Returns: the EtherType of the packet, regardless of whether it is
614 * vlan encapsulated (normal or hardware accelerated) or not.
615 */
616 static inline
vlan_get_protocol_offset_inline(const struct sk_buff * skb,__be16 type,int mac_offset,int * depth)617 __be16 vlan_get_protocol_offset_inline(const struct sk_buff *skb,
618 __be16 type,
619 int mac_offset,
620 int *depth)
621 {
622 if (eth_type_vlan(type)) {
623 struct vlan_type_depth res;
624
625 res = __vlan_get_protocol_offset(skb, type, mac_offset);
626
627 if (depth && res.type)
628 *depth = res.depth;
629 return res.type;
630 }
631
632 if (depth)
633 *depth = skb->mac_len;
634
635 return type;
636 }
637
__vlan_get_protocol(const struct sk_buff * skb,__be16 type,int * depth)638 static inline __be16 __vlan_get_protocol(const struct sk_buff *skb, __be16 type,
639 int *depth)
640 {
641 return vlan_get_protocol_offset_inline(skb, type, 0, depth);
642 }
643
644 /**
645 * vlan_get_protocol - get protocol EtherType.
646 * @skb: skbuff to query
647 *
648 * Returns: the EtherType of the packet, regardless of whether it is
649 * vlan encapsulated (normal or hardware accelerated) or not.
650 */
vlan_get_protocol(const struct sk_buff * skb)651 static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
652 {
653 return __vlan_get_protocol(skb, skb->protocol, NULL);
654 }
655
656 /* This version of __vlan_get_protocol() also pulls mac header in skb->head */
vlan_get_protocol_and_depth(struct sk_buff * skb,__be16 type,int * depth)657 static inline __be16 vlan_get_protocol_and_depth(struct sk_buff *skb,
658 __be16 type, int *depth)
659 {
660 int maclen;
661
662 type = __vlan_get_protocol(skb, type, &maclen);
663
664 if (type) {
665 if (!pskb_may_pull(skb, maclen))
666 type = 0;
667 else if (depth)
668 *depth = maclen;
669 }
670 return type;
671 }
672
673 /* A getter for the SKB protocol field which will handle VLAN tags consistently
674 * whether VLAN acceleration is enabled or not.
675 */
skb_protocol(const struct sk_buff * skb,bool skip_vlan)676 static inline __be16 skb_protocol(const struct sk_buff *skb, bool skip_vlan)
677 {
678 if (!skip_vlan)
679 /* VLAN acceleration strips the VLAN header from the skb and
680 * moves it to skb->vlan_proto
681 */
682 return skb_vlan_tag_present(skb) ? skb->vlan_proto : skb->protocol;
683
684 return vlan_get_protocol(skb);
685 }
686
vlan_set_encap_proto(struct sk_buff * skb,struct vlan_hdr * vhdr)687 static inline void vlan_set_encap_proto(struct sk_buff *skb,
688 struct vlan_hdr *vhdr)
689 {
690 __be16 proto;
691 unsigned short *rawp;
692
693 /*
694 * Was a VLAN packet, grab the encapsulated protocol, which the layer
695 * three protocols care about.
696 */
697
698 proto = vhdr->h_vlan_encapsulated_proto;
699 if (eth_proto_is_802_3(proto)) {
700 skb->protocol = proto;
701 return;
702 }
703
704 rawp = (unsigned short *)(vhdr + 1);
705 if (*rawp == 0xFFFF)
706 /*
707 * This is a magic hack to spot IPX packets. Older Novell
708 * breaks the protocol design and runs IPX over 802.3 without
709 * an 802.2 LLC layer. We look for FFFF which isn't a used
710 * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
711 * but does for the rest.
712 */
713 skb->protocol = htons(ETH_P_802_3);
714 else
715 /*
716 * Real 802.2 LLC
717 */
718 skb->protocol = htons(ETH_P_802_2);
719 }
720
721 /**
722 * vlan_remove_tag - remove outer VLAN tag from payload
723 * @skb: skbuff to remove tag from
724 * @vlan_tci: buffer to store value
725 *
726 * Expects the skb to contain a VLAN tag in the payload, and to have skb->data
727 * pointing at the MAC header.
728 */
vlan_remove_tag(struct sk_buff * skb,u16 * vlan_tci)729 static inline void vlan_remove_tag(struct sk_buff *skb, u16 *vlan_tci)
730 {
731 struct vlan_hdr *vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
732
733 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
734
735 vlan_set_encap_proto(skb, vhdr);
736 __skb_pull(skb, VLAN_HLEN);
737 skb_postpull_data_move(skb, VLAN_HLEN, 2 * ETH_ALEN);
738 }
739
740 /**
741 * skb_vlan_tagged - check if skb is vlan tagged.
742 * @skb: skbuff to query
743 *
744 * Returns: true if the skb is tagged, regardless of whether it is hardware
745 * accelerated or not.
746 */
skb_vlan_tagged(const struct sk_buff * skb)747 static inline bool skb_vlan_tagged(const struct sk_buff *skb)
748 {
749 if (!skb_vlan_tag_present(skb) &&
750 likely(!eth_type_vlan(skb->protocol)))
751 return false;
752
753 return true;
754 }
755
756 /**
757 * skb_vlan_tagged_multi - check if skb is vlan tagged with multiple headers.
758 * @skb: skbuff to query
759 *
760 * Returns: true if the skb is tagged with multiple vlan headers, regardless
761 * of whether it is hardware accelerated or not.
762 */
skb_vlan_tagged_multi(struct sk_buff * skb)763 static inline bool skb_vlan_tagged_multi(struct sk_buff *skb)
764 {
765 __be16 protocol = skb->protocol;
766
767 if (!skb_vlan_tag_present(skb)) {
768 struct vlan_ethhdr *veh;
769
770 if (likely(!eth_type_vlan(protocol)))
771 return false;
772
773 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
774 return false;
775
776 veh = skb_vlan_eth_hdr(skb);
777 protocol = veh->h_vlan_encapsulated_proto;
778 }
779
780 if (!eth_type_vlan(protocol))
781 return false;
782
783 return true;
784 }
785
786 /**
787 * vlan_features_check - drop unsafe features for skb with multiple tags.
788 * @skb: skbuff to query
789 * @features: features to be checked
790 *
791 * Returns: features without unsafe ones if the skb has multiple tags.
792 */
vlan_features_check(struct sk_buff * skb,netdev_features_t features)793 static inline netdev_features_t vlan_features_check(struct sk_buff *skb,
794 netdev_features_t features)
795 {
796 if (skb_vlan_tagged_multi(skb)) {
797 /* In the case of multi-tagged packets, use a direct mask
798 * instead of using netdev_interesect_features(), to make
799 * sure that only devices supporting NETIF_F_HW_CSUM will
800 * have checksum offloading support.
801 */
802 features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
803 NETIF_F_FRAGLIST | NETIF_F_HW_VLAN_CTAG_TX |
804 NETIF_F_HW_VLAN_STAG_TX;
805 }
806
807 return features;
808 }
809
810 /**
811 * compare_vlan_header - Compare two vlan headers
812 * @h1: Pointer to vlan header
813 * @h2: Pointer to vlan header
814 *
815 * Compare two vlan headers.
816 *
817 * Please note that alignment of h1 & h2 are only guaranteed to be 16 bits.
818 *
819 * Return: 0 if equal, arbitrary non-zero value if not equal.
820 */
compare_vlan_header(const struct vlan_hdr * h1,const struct vlan_hdr * h2)821 static inline unsigned long compare_vlan_header(const struct vlan_hdr *h1,
822 const struct vlan_hdr *h2)
823 {
824 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
825 return *(u32 *)h1 ^ *(u32 *)h2;
826 #else
827 return ((__force u32)h1->h_vlan_TCI ^ (__force u32)h2->h_vlan_TCI) |
828 ((__force u32)h1->h_vlan_encapsulated_proto ^
829 (__force u32)h2->h_vlan_encapsulated_proto);
830 #endif
831 }
832 #endif /* !(_LINUX_IF_VLAN_H_) */
833