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 defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
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 */
175 struct vlan_dev_priv {
176 unsigned int nr_ingress_mappings;
177 u32 ingress_priority_map[8];
178 unsigned int nr_egress_mappings;
179 struct vlan_priority_tci_mapping *egress_priority_map[16];
180
181 __be16 vlan_proto;
182 u16 vlan_id;
183 u16 flags;
184
185 struct net_device *real_dev;
186 netdevice_tracker dev_tracker;
187
188 unsigned char real_dev_addr[ETH_ALEN];
189
190 struct proc_dir_entry *dent;
191 struct vlan_pcpu_stats __percpu *vlan_pcpu_stats;
192 #ifdef CONFIG_NET_POLL_CONTROLLER
193 struct netpoll *netpoll;
194 #endif
195 };
196
is_vlan_dev(const struct net_device * dev)197 static inline bool is_vlan_dev(const struct net_device *dev)
198 {
199 return dev->priv_flags & IFF_802_1Q_VLAN;
200 }
201
vlan_dev_priv(const struct net_device * dev)202 static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
203 {
204 return netdev_priv(dev);
205 }
206
207 static inline u16
vlan_dev_get_egress_qos_mask(struct net_device * dev,u32 skprio)208 vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio)
209 {
210 struct vlan_priority_tci_mapping *mp;
211
212 smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */
213
214 mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)];
215 while (mp) {
216 if (mp->priority == skprio) {
217 return mp->vlan_qos; /* This should already be shifted
218 * to mask correctly with the
219 * VLAN's TCI */
220 }
221 mp = mp->next;
222 }
223 return 0;
224 }
225
226 extern bool vlan_do_receive(struct sk_buff **skb);
227
228 extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid);
229 extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid);
230
231 extern int vlan_vids_add_by_dev(struct net_device *dev,
232 const struct net_device *by_dev);
233 extern void vlan_vids_del_by_dev(struct net_device *dev,
234 const struct net_device *by_dev);
235
236 extern bool vlan_uses_dev(const struct net_device *dev);
237
238 #else
is_vlan_dev(const struct net_device * dev)239 static inline bool is_vlan_dev(const struct net_device *dev)
240 {
241 return false;
242 }
243
244 static inline struct net_device *
__vlan_find_dev_deep_rcu(struct net_device * real_dev,__be16 vlan_proto,u16 vlan_id)245 __vlan_find_dev_deep_rcu(struct net_device *real_dev,
246 __be16 vlan_proto, u16 vlan_id)
247 {
248 return NULL;
249 }
250
251 static inline int
vlan_for_each(struct net_device * dev,int (* action)(struct net_device * dev,int vid,void * arg),void * arg)252 vlan_for_each(struct net_device *dev,
253 int (*action)(struct net_device *dev, int vid, void *arg),
254 void *arg)
255 {
256 return 0;
257 }
258
vlan_dev_real_dev(const struct net_device * dev)259 static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
260 {
261 WARN_ON_ONCE(1);
262 return NULL;
263 }
264
vlan_dev_vlan_id(const struct net_device * dev)265 static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
266 {
267 WARN_ON_ONCE(1);
268 return 0;
269 }
270
vlan_dev_vlan_proto(const struct net_device * dev)271 static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev)
272 {
273 WARN_ON_ONCE(1);
274 return 0;
275 }
276
vlan_dev_get_egress_qos_mask(struct net_device * dev,u32 skprio)277 static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev,
278 u32 skprio)
279 {
280 return 0;
281 }
282
vlan_do_receive(struct sk_buff ** skb)283 static inline bool vlan_do_receive(struct sk_buff **skb)
284 {
285 return false;
286 }
287
vlan_vid_add(struct net_device * dev,__be16 proto,u16 vid)288 static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
289 {
290 return 0;
291 }
292
vlan_vid_del(struct net_device * dev,__be16 proto,u16 vid)293 static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
294 {
295 }
296
vlan_vids_add_by_dev(struct net_device * dev,const struct net_device * by_dev)297 static inline int vlan_vids_add_by_dev(struct net_device *dev,
298 const struct net_device *by_dev)
299 {
300 return 0;
301 }
302
vlan_vids_del_by_dev(struct net_device * dev,const struct net_device * by_dev)303 static inline void vlan_vids_del_by_dev(struct net_device *dev,
304 const struct net_device *by_dev)
305 {
306 }
307
vlan_uses_dev(const struct net_device * dev)308 static inline bool vlan_uses_dev(const struct net_device *dev)
309 {
310 return false;
311 }
312 #endif
313
314 /**
315 * eth_type_vlan - check for valid vlan ether type.
316 * @ethertype: ether type to check
317 *
318 * Returns true if the ether type is a vlan ether type.
319 */
eth_type_vlan(__be16 ethertype)320 static inline bool eth_type_vlan(__be16 ethertype)
321 {
322 switch (ethertype) {
323 case htons(ETH_P_8021Q):
324 case htons(ETH_P_8021AD):
325 return true;
326 default:
327 return false;
328 }
329 }
330
vlan_hw_offload_capable(netdev_features_t features,__be16 proto)331 static inline bool vlan_hw_offload_capable(netdev_features_t features,
332 __be16 proto)
333 {
334 if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
335 return true;
336 if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
337 return true;
338 return false;
339 }
340
341 /**
342 * __vlan_insert_inner_tag - inner VLAN tag inserting
343 * @skb: skbuff to tag
344 * @vlan_proto: VLAN encapsulation protocol
345 * @vlan_tci: VLAN TCI to insert
346 * @mac_len: MAC header length including outer vlan headers
347 *
348 * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
349 * Returns error if skb_cow_head fails.
350 *
351 * Does not change skb->protocol so this function can be used during receive.
352 */
__vlan_insert_inner_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci,unsigned int mac_len)353 static inline int __vlan_insert_inner_tag(struct sk_buff *skb,
354 __be16 vlan_proto, u16 vlan_tci,
355 unsigned int mac_len)
356 {
357 struct vlan_ethhdr *veth;
358
359 if (skb_cow_head(skb, VLAN_HLEN) < 0)
360 return -ENOMEM;
361
362 skb_push(skb, VLAN_HLEN);
363
364 /* Move the mac header sans proto to the beginning of the new header. */
365 if (likely(mac_len > ETH_TLEN))
366 memmove(skb->data, skb->data + VLAN_HLEN, mac_len - ETH_TLEN);
367 if (skb_mac_header_was_set(skb))
368 skb->mac_header -= VLAN_HLEN;
369
370 veth = (struct vlan_ethhdr *)(skb->data + mac_len - ETH_HLEN);
371
372 /* first, the ethernet type */
373 if (likely(mac_len >= ETH_TLEN)) {
374 /* h_vlan_encapsulated_proto should already be populated, and
375 * skb->data has space for h_vlan_proto
376 */
377 veth->h_vlan_proto = vlan_proto;
378 } else {
379 /* h_vlan_encapsulated_proto should not be populated, and
380 * skb->data has no space for h_vlan_proto
381 */
382 veth->h_vlan_encapsulated_proto = skb->protocol;
383 }
384
385 /* now, the TCI */
386 veth->h_vlan_TCI = htons(vlan_tci);
387
388 return 0;
389 }
390
391 /**
392 * __vlan_insert_tag - regular VLAN tag inserting
393 * @skb: skbuff to tag
394 * @vlan_proto: VLAN encapsulation protocol
395 * @vlan_tci: VLAN TCI to insert
396 *
397 * Inserts the VLAN tag into @skb as part of the payload
398 * Returns error if skb_cow_head fails.
399 *
400 * Does not change skb->protocol so this function can be used during receive.
401 */
__vlan_insert_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci)402 static inline int __vlan_insert_tag(struct sk_buff *skb,
403 __be16 vlan_proto, u16 vlan_tci)
404 {
405 return __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
406 }
407
408 /**
409 * vlan_insert_inner_tag - inner VLAN tag inserting
410 * @skb: skbuff to tag
411 * @vlan_proto: VLAN encapsulation protocol
412 * @vlan_tci: VLAN TCI to insert
413 * @mac_len: MAC header length including outer vlan headers
414 *
415 * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
416 * Returns a VLAN tagged skb. This might change skb->head.
417 *
418 * Following the skb_unshare() example, in case of error, the calling function
419 * doesn't have to worry about freeing the original skb.
420 *
421 * Does not change skb->protocol so this function can be used during receive.
422 */
vlan_insert_inner_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci,unsigned int mac_len)423 static inline struct sk_buff *vlan_insert_inner_tag(struct sk_buff *skb,
424 __be16 vlan_proto,
425 u16 vlan_tci,
426 unsigned int mac_len)
427 {
428 int err;
429
430 err = __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, mac_len);
431 if (err) {
432 dev_kfree_skb_any(skb);
433 return NULL;
434 }
435 return skb;
436 }
437
438 /**
439 * vlan_insert_tag - regular VLAN tag inserting
440 * @skb: skbuff to tag
441 * @vlan_proto: VLAN encapsulation protocol
442 * @vlan_tci: VLAN TCI to insert
443 *
444 * Inserts the VLAN tag into @skb as part of the payload
445 * Returns a VLAN tagged skb. This might change skb->head.
446 *
447 * Following the skb_unshare() example, in case of error, the calling function
448 * doesn't have to worry about freeing the original skb.
449 *
450 * Does not change skb->protocol so this function can be used during receive.
451 */
vlan_insert_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci)452 static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
453 __be16 vlan_proto, u16 vlan_tci)
454 {
455 return vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
456 }
457
458 /**
459 * vlan_insert_tag_set_proto - regular VLAN tag inserting
460 * @skb: skbuff to tag
461 * @vlan_proto: VLAN encapsulation protocol
462 * @vlan_tci: VLAN TCI to insert
463 *
464 * Inserts the VLAN tag into @skb as part of the payload
465 * Returns a VLAN tagged skb. This might change skb->head.
466 *
467 * Following the skb_unshare() example, in case of error, the calling function
468 * doesn't have to worry about freeing the original skb.
469 */
vlan_insert_tag_set_proto(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci)470 static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb,
471 __be16 vlan_proto,
472 u16 vlan_tci)
473 {
474 skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
475 if (skb)
476 skb->protocol = vlan_proto;
477 return skb;
478 }
479
480 /**
481 * __vlan_hwaccel_clear_tag - clear hardware accelerated VLAN info
482 * @skb: skbuff to clear
483 *
484 * Clears the VLAN information from @skb
485 */
__vlan_hwaccel_clear_tag(struct sk_buff * skb)486 static inline void __vlan_hwaccel_clear_tag(struct sk_buff *skb)
487 {
488 skb->vlan_all = 0;
489 }
490
491 /**
492 * __vlan_hwaccel_copy_tag - copy hardware accelerated VLAN info from another skb
493 * @dst: skbuff to copy to
494 * @src: skbuff to copy from
495 *
496 * Copies VLAN information from @src to @dst (for branchless code)
497 */
__vlan_hwaccel_copy_tag(struct sk_buff * dst,const struct sk_buff * src)498 static inline void __vlan_hwaccel_copy_tag(struct sk_buff *dst, const struct sk_buff *src)
499 {
500 dst->vlan_all = src->vlan_all;
501 }
502
503 /*
504 * __vlan_hwaccel_push_inside - pushes vlan tag to the payload
505 * @skb: skbuff to tag
506 *
507 * Pushes the VLAN tag from @skb->vlan_tci inside to the payload.
508 *
509 * Following the skb_unshare() example, in case of error, the calling function
510 * doesn't have to worry about freeing the original skb.
511 */
__vlan_hwaccel_push_inside(struct sk_buff * skb)512 static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb)
513 {
514 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
515 skb_vlan_tag_get(skb));
516 if (likely(skb))
517 __vlan_hwaccel_clear_tag(skb);
518 return skb;
519 }
520
521 /**
522 * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
523 * @skb: skbuff to tag
524 * @vlan_proto: VLAN encapsulation protocol
525 * @vlan_tci: VLAN TCI to insert
526 *
527 * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
528 */
__vlan_hwaccel_put_tag(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci)529 static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb,
530 __be16 vlan_proto, u16 vlan_tci)
531 {
532 skb->vlan_proto = vlan_proto;
533 skb->vlan_tci = vlan_tci;
534 }
535
536 /**
537 * __vlan_get_tag - get the VLAN ID that is part of the payload
538 * @skb: skbuff to query
539 * @vlan_tci: buffer to store value
540 *
541 * Returns error if the skb is not of VLAN type
542 */
__vlan_get_tag(const struct sk_buff * skb,u16 * vlan_tci)543 static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
544 {
545 struct vlan_ethhdr *veth = skb_vlan_eth_hdr(skb);
546
547 if (!eth_type_vlan(veth->h_vlan_proto))
548 return -ENODATA;
549
550 *vlan_tci = ntohs(veth->h_vlan_TCI);
551 return 0;
552 }
553
554 /**
555 * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
556 * @skb: skbuff to query
557 * @vlan_tci: buffer to store value
558 *
559 * Returns error if @skb->vlan_tci is not set correctly
560 */
__vlan_hwaccel_get_tag(const struct sk_buff * skb,u16 * vlan_tci)561 static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
562 u16 *vlan_tci)
563 {
564 if (skb_vlan_tag_present(skb)) {
565 *vlan_tci = skb_vlan_tag_get(skb);
566 return 0;
567 } else {
568 *vlan_tci = 0;
569 return -ENODATA;
570 }
571 }
572
573 /**
574 * vlan_get_tag - get the VLAN ID from the skb
575 * @skb: skbuff to query
576 * @vlan_tci: buffer to store value
577 *
578 * Returns error if the skb is not VLAN tagged
579 */
vlan_get_tag(const struct sk_buff * skb,u16 * vlan_tci)580 static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
581 {
582 if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
583 return __vlan_hwaccel_get_tag(skb, vlan_tci);
584 } else {
585 return __vlan_get_tag(skb, vlan_tci);
586 }
587 }
588
589 /**
590 * vlan_get_protocol - get protocol EtherType.
591 * @skb: skbuff to query
592 * @type: first vlan protocol
593 * @mac_offset: MAC offset
594 * @depth: buffer to store length of eth and vlan tags in bytes
595 *
596 * Returns the EtherType of the packet, regardless of whether it is
597 * vlan encapsulated (normal or hardware accelerated) or not.
598 */
__vlan_get_protocol_offset(const struct sk_buff * skb,__be16 type,int mac_offset,int * depth)599 static inline __be16 __vlan_get_protocol_offset(const struct sk_buff *skb,
600 __be16 type,
601 int mac_offset,
602 int *depth)
603 {
604 unsigned int vlan_depth = skb->mac_len, parse_depth = VLAN_MAX_DEPTH;
605
606 /* if type is 802.1Q/AD then the header should already be
607 * present at mac_len - VLAN_HLEN (if mac_len > 0), or at
608 * ETH_HLEN otherwise
609 */
610 if (eth_type_vlan(type)) {
611 if (vlan_depth) {
612 if (WARN_ON(vlan_depth < VLAN_HLEN))
613 return 0;
614 vlan_depth -= VLAN_HLEN;
615 } else {
616 vlan_depth = ETH_HLEN;
617 }
618 do {
619 struct vlan_hdr vhdr, *vh;
620
621 vh = skb_header_pointer(skb, mac_offset + vlan_depth,
622 sizeof(vhdr), &vhdr);
623 if (unlikely(!vh || !--parse_depth))
624 return 0;
625
626 type = vh->h_vlan_encapsulated_proto;
627 vlan_depth += VLAN_HLEN;
628 } while (eth_type_vlan(type));
629 }
630
631 if (depth)
632 *depth = vlan_depth;
633
634 return type;
635 }
636
__vlan_get_protocol(const struct sk_buff * skb,__be16 type,int * depth)637 static inline __be16 __vlan_get_protocol(const struct sk_buff *skb, __be16 type,
638 int *depth)
639 {
640 return __vlan_get_protocol_offset(skb, type, 0, depth);
641 }
642
643 /**
644 * vlan_get_protocol - get protocol EtherType.
645 * @skb: skbuff to query
646 *
647 * Returns the EtherType of the packet, regardless of whether it is
648 * vlan encapsulated (normal or hardware accelerated) or not.
649 */
vlan_get_protocol(const struct sk_buff * skb)650 static inline __be16 vlan_get_protocol(const struct sk_buff *skb)
651 {
652 return __vlan_get_protocol(skb, skb->protocol, NULL);
653 }
654
655 /* 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)656 static inline __be16 vlan_get_protocol_and_depth(struct sk_buff *skb,
657 __be16 type, int *depth)
658 {
659 int maclen;
660
661 type = __vlan_get_protocol(skb, type, &maclen);
662
663 if (type) {
664 if (!pskb_may_pull(skb, maclen))
665 type = 0;
666 else if (depth)
667 *depth = maclen;
668 }
669 return type;
670 }
671
672 /* A getter for the SKB protocol field which will handle VLAN tags consistently
673 * whether VLAN acceleration is enabled or not.
674 */
skb_protocol(const struct sk_buff * skb,bool skip_vlan)675 static inline __be16 skb_protocol(const struct sk_buff *skb, bool skip_vlan)
676 {
677 if (!skip_vlan)
678 /* VLAN acceleration strips the VLAN header from the skb and
679 * moves it to skb->vlan_proto
680 */
681 return skb_vlan_tag_present(skb) ? skb->vlan_proto : skb->protocol;
682
683 return vlan_get_protocol(skb);
684 }
685
vlan_set_encap_proto(struct sk_buff * skb,struct vlan_hdr * vhdr)686 static inline void vlan_set_encap_proto(struct sk_buff *skb,
687 struct vlan_hdr *vhdr)
688 {
689 __be16 proto;
690 unsigned short *rawp;
691
692 /*
693 * Was a VLAN packet, grab the encapsulated protocol, which the layer
694 * three protocols care about.
695 */
696
697 proto = vhdr->h_vlan_encapsulated_proto;
698 if (eth_proto_is_802_3(proto)) {
699 skb->protocol = proto;
700 return;
701 }
702
703 rawp = (unsigned short *)(vhdr + 1);
704 if (*rawp == 0xFFFF)
705 /*
706 * This is a magic hack to spot IPX packets. Older Novell
707 * breaks the protocol design and runs IPX over 802.3 without
708 * an 802.2 LLC layer. We look for FFFF which isn't a used
709 * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
710 * but does for the rest.
711 */
712 skb->protocol = htons(ETH_P_802_3);
713 else
714 /*
715 * Real 802.2 LLC
716 */
717 skb->protocol = htons(ETH_P_802_2);
718 }
719
720 /**
721 * vlan_remove_tag - remove outer VLAN tag from payload
722 * @skb: skbuff to remove tag from
723 * @vlan_tci: buffer to store value
724 *
725 * Expects the skb to contain a VLAN tag in the payload, and to have skb->data
726 * pointing at the MAC header.
727 *
728 * Returns a new pointer to skb->data, or NULL on failure to pull.
729 */
vlan_remove_tag(struct sk_buff * skb,u16 * vlan_tci)730 static inline void *vlan_remove_tag(struct sk_buff *skb, u16 *vlan_tci)
731 {
732 struct vlan_hdr *vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
733
734 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
735
736 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
737 vlan_set_encap_proto(skb, vhdr);
738 return __skb_pull(skb, VLAN_HLEN);
739 }
740
741 /**
742 * skb_vlan_tagged - check if skb is vlan tagged.
743 * @skb: skbuff to query
744 *
745 * Returns true if the skb is tagged, regardless of whether it is hardware
746 * accelerated or not.
747 */
skb_vlan_tagged(const struct sk_buff * skb)748 static inline bool skb_vlan_tagged(const struct sk_buff *skb)
749 {
750 if (!skb_vlan_tag_present(skb) &&
751 likely(!eth_type_vlan(skb->protocol)))
752 return false;
753
754 return true;
755 }
756
757 /**
758 * skb_vlan_tagged_multi - check if skb is vlan tagged with multiple headers.
759 * @skb: skbuff to query
760 *
761 * Returns true if the skb is tagged with multiple vlan headers, regardless
762 * of whether it is hardware accelerated or not.
763 */
skb_vlan_tagged_multi(struct sk_buff * skb)764 static inline bool skb_vlan_tagged_multi(struct sk_buff *skb)
765 {
766 __be16 protocol = skb->protocol;
767
768 if (!skb_vlan_tag_present(skb)) {
769 struct vlan_ethhdr *veh;
770
771 if (likely(!eth_type_vlan(protocol)))
772 return false;
773
774 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
775 return false;
776
777 veh = skb_vlan_eth_hdr(skb);
778 protocol = veh->h_vlan_encapsulated_proto;
779 }
780
781 if (!eth_type_vlan(protocol))
782 return false;
783
784 return true;
785 }
786
787 /**
788 * vlan_features_check - drop unsafe features for skb with multiple tags.
789 * @skb: skbuff to query
790 * @features: features to be checked
791 *
792 * Returns features without unsafe ones if the skb has multiple tags.
793 */
vlan_features_check(struct sk_buff * skb,netdev_features_t features)794 static inline netdev_features_t vlan_features_check(struct sk_buff *skb,
795 netdev_features_t features)
796 {
797 if (skb_vlan_tagged_multi(skb)) {
798 /* In the case of multi-tagged packets, use a direct mask
799 * instead of using netdev_interesect_features(), to make
800 * sure that only devices supporting NETIF_F_HW_CSUM will
801 * have checksum offloading support.
802 */
803 features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
804 NETIF_F_FRAGLIST | NETIF_F_HW_VLAN_CTAG_TX |
805 NETIF_F_HW_VLAN_STAG_TX;
806 }
807
808 return features;
809 }
810
811 /**
812 * compare_vlan_header - Compare two vlan headers
813 * @h1: Pointer to vlan header
814 * @h2: Pointer to vlan header
815 *
816 * Compare two vlan headers, returns 0 if equal.
817 *
818 * Please note that alignment of h1 & h2 are only guaranteed to be 16 bits.
819 */
compare_vlan_header(const struct vlan_hdr * h1,const struct vlan_hdr * h2)820 static inline unsigned long compare_vlan_header(const struct vlan_hdr *h1,
821 const struct vlan_hdr *h2)
822 {
823 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
824 return *(u32 *)h1 ^ *(u32 *)h2;
825 #else
826 return ((__force u32)h1->h_vlan_TCI ^ (__force u32)h2->h_vlan_TCI) |
827 ((__force u32)h1->h_vlan_encapsulated_proto ^
828 (__force u32)h2->h_vlan_encapsulated_proto);
829 #endif
830 }
831 #endif /* !(_LINUX_IF_VLAN_H_) */
832