blob: 7a541eadf78eb7045054a99120c0c6e72b00ed90 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * VLAN An implementation of 802.1Q VLAN tagging.
3 *
4 * Authors: Ben Greear <greearb@candelatech.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#ifndef _LINUX_IF_VLAN_H_
13#define _LINUX_IF_VLAN_H_
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/netdevice.h>
Stephen Hemminger0610d112006-07-14 16:34:22 -070016#include <linux/etherdevice.h>
Jesse Gross65ac6a52010-10-20 13:56:05 +000017#include <linux/rtnetlink.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050018#include <linux/bug.h>
David Howells607ca462012-10-13 10:46:48 +010019#include <uapi/linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Joe Perchesa3f671b32012-03-18 17:37:56 +000021#define VLAN_HLEN 4 /* The additional bytes required by VLAN
22 * (in addition to the Ethernet header)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define VLAN_ETH_HLEN 18 /* Total octets in header. */
25#define VLAN_ETH_ZLEN 64 /* Min. octets in frame sans FCS */
26
27/*
28 * According to 802.3ac, the packet can be 4 bytes longer. --Klika Jan
29 */
30#define VLAN_ETH_DATA_LEN 1500 /* Max. octets in payload */
31#define VLAN_ETH_FRAME_LEN 1518 /* Max. octets in frame sans FCS */
32
Patrick McHardy740c15d2008-01-21 00:18:53 -080033/*
34 * struct vlan_hdr - vlan header
35 * @h_vlan_TCI: priority and VLAN ID
36 * @h_vlan_encapsulated_proto: packet type ID or len
37 */
38struct vlan_hdr {
39 __be16 h_vlan_TCI;
40 __be16 h_vlan_encapsulated_proto;
41};
42
43/**
44 * struct vlan_ethhdr - vlan ethernet header (ethhdr + vlan_hdr)
45 * @h_dest: destination ethernet address
46 * @h_source: source ethernet address
Olaf Heringa7bf5802013-06-14 17:07:01 +020047 * @h_vlan_proto: ethernet protocol
Patrick McHardy740c15d2008-01-21 00:18:53 -080048 * @h_vlan_TCI: priority and VLAN ID
49 * @h_vlan_encapsulated_proto: packet type ID or len
50 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051struct vlan_ethhdr {
Patrick McHardy740c15d2008-01-21 00:18:53 -080052 unsigned char h_dest[ETH_ALEN];
53 unsigned char h_source[ETH_ALEN];
54 __be16 h_vlan_proto;
55 __be16 h_vlan_TCI;
56 __be16 h_vlan_encapsulated_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
59#include <linux/skbuff.h>
60
61static inline struct vlan_ethhdr *vlan_eth_hdr(const struct sk_buff *skb)
62{
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -070063 return (struct vlan_ethhdr *)skb_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
Eric Dumazet05423b22009-10-26 18:40:35 -070066#define VLAN_PRIO_MASK 0xe000 /* Priority Code Point */
67#define VLAN_PRIO_SHIFT 13
68#define VLAN_CFI_MASK 0x1000 /* Canonical Format Indicator */
Eric Dumazet05423b22009-10-26 18:40:35 -070069#define VLAN_VID_MASK 0x0fff /* VLAN Identifier */
Jesse Grossb7381272010-10-20 13:56:02 +000070#define VLAN_N_VID 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/* found in socket.c */
Eric W. Biederman881d9662007-09-17 11:56:21 -070073extern void vlan_ioctl_set(int (*hook)(struct net *, void __user *));
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Jiri Pirkob618aaa2015-12-04 15:01:31 +010075static inline bool is_vlan_dev(const struct net_device *dev)
Neil Horman6dcbbe22011-05-24 08:31:08 +000076{
77 return dev->priv_flags & IFF_802_1Q_VLAN;
78}
79
Michał Mirosław0c4b2d32018-11-10 19:58:36 +010080#define skb_vlan_tag_present(__skb) ((__skb)->vlan_present)
81#define skb_vlan_tag_get(__skb) ((__skb)->vlan_tci)
Jiri Pirkodf8a39d2015-01-13 17:13:44 +010082#define skb_vlan_tag_get_id(__skb) ((__skb)->vlan_tci & VLAN_VID_MASK)
Michał Mirosław9b319142018-11-07 18:07:03 +010083#define skb_vlan_tag_get_prio(__skb) (((__skb)->vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Gal Pressman9daae9b2018-03-28 17:46:54 +030085static inline int vlan_get_rx_ctag_filter_info(struct net_device *dev)
86{
87 ASSERT_RTNL();
88 return notifier_to_errno(call_netdevice_notifiers(NETDEV_CVLAN_FILTER_PUSH_INFO, dev));
89}
90
91static inline void vlan_drop_rx_ctag_filter_info(struct net_device *dev)
92{
93 ASSERT_RTNL();
94 call_netdevice_notifiers(NETDEV_CVLAN_FILTER_DROP_INFO, dev);
95}
96
97static inline int vlan_get_rx_stag_filter_info(struct net_device *dev)
98{
99 ASSERT_RTNL();
100 return notifier_to_errno(call_netdevice_notifiers(NETDEV_SVLAN_FILTER_PUSH_INFO, dev));
101}
102
103static inline void vlan_drop_rx_stag_filter_info(struct net_device *dev)
104{
105 ASSERT_RTNL();
106 call_netdevice_notifiers(NETDEV_SVLAN_FILTER_DROP_INFO, dev);
107}
108
David S. Millere267cb92013-11-11 00:42:07 -0500109/**
110 * struct vlan_pcpu_stats - VLAN percpu rx/tx stats
111 * @rx_packets: number of received packets
112 * @rx_bytes: number of received bytes
113 * @rx_multicast: number of received multicast packets
114 * @tx_packets: number of transmitted packets
115 * @tx_bytes: number of transmitted bytes
116 * @syncp: synchronization point for 64bit counters
117 * @rx_errors: number of rx errors
118 * @tx_dropped: number of tx drops
119 */
120struct vlan_pcpu_stats {
121 u64 rx_packets;
122 u64 rx_bytes;
123 u64 rx_multicast;
124 u64 tx_packets;
125 u64 tx_bytes;
126 struct u64_stats_sync syncp;
127 u32 rx_errors;
128 u32 tx_dropped;
129};
130
Hannes Frederic Sowa1e85c9b2014-01-06 01:41:20 +0100131#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
132
dingtianhongf06c7f9f2014-05-09 14:58:05 +0800133extern struct net_device *__vlan_find_dev_deep_rcu(struct net_device *real_dev,
Hannes Frederic Sowa1e85c9b2014-01-06 01:41:20 +0100134 __be16 vlan_proto, u16 vlan_id);
Ivan Khoronzhuk960abf62018-11-08 22:27:55 +0200135extern int vlan_for_each(struct net_device *dev,
136 int (*action)(struct net_device *dev, int vid,
137 void *arg), void *arg);
Hannes Frederic Sowa1e85c9b2014-01-06 01:41:20 +0100138extern struct net_device *vlan_dev_real_dev(const struct net_device *dev);
139extern u16 vlan_dev_vlan_id(const struct net_device *dev);
dingtianhong71e415e2014-03-25 17:44:42 +0800140extern __be16 vlan_dev_vlan_proto(const struct net_device *dev);
Hannes Frederic Sowa1e85c9b2014-01-06 01:41:20 +0100141
142/**
143 * struct vlan_priority_tci_mapping - vlan egress priority mappings
144 * @priority: skb priority
145 * @vlan_qos: vlan priority: (skb->priority << 13) & 0xE000
146 * @next: pointer to next struct
147 */
148struct vlan_priority_tci_mapping {
149 u32 priority;
150 u16 vlan_qos;
151 struct vlan_priority_tci_mapping *next;
152};
153
David S. Millere267cb92013-11-11 00:42:07 -0500154struct proc_dir_entry;
155struct netpoll;
156
157/**
158 * struct vlan_dev_priv - VLAN private device data
159 * @nr_ingress_mappings: number of ingress priority mappings
160 * @ingress_priority_map: ingress priority mappings
161 * @nr_egress_mappings: number of egress priority mappings
162 * @egress_priority_map: hash of egress priority mappings
163 * @vlan_proto: VLAN encapsulation protocol
164 * @vlan_id: VLAN identifier
165 * @flags: device flags
166 * @real_dev: underlying netdevice
167 * @real_dev_addr: address of underlying netdevice
168 * @dent: proc dir entry
169 * @vlan_pcpu_stats: ptr to percpu rx stats
170 */
171struct vlan_dev_priv {
172 unsigned int nr_ingress_mappings;
173 u32 ingress_priority_map[8];
174 unsigned int nr_egress_mappings;
175 struct vlan_priority_tci_mapping *egress_priority_map[16];
176
177 __be16 vlan_proto;
178 u16 vlan_id;
179 u16 flags;
180
181 struct net_device *real_dev;
182 unsigned char real_dev_addr[ETH_ALEN];
183
184 struct proc_dir_entry *dent;
185 struct vlan_pcpu_stats __percpu *vlan_pcpu_stats;
186#ifdef CONFIG_NET_POLL_CONTROLLER
187 struct netpoll *netpoll;
188#endif
Vlad Yasevichd38569a2014-05-16 17:04:55 -0400189 unsigned int nest_level;
David S. Millere267cb92013-11-11 00:42:07 -0500190};
191
192static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
193{
194 return netdev_priv(dev);
195}
196
197static inline u16
198vlan_dev_get_egress_qos_mask(struct net_device *dev, u32 skprio)
199{
200 struct vlan_priority_tci_mapping *mp;
201
202 smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */
203
204 mp = vlan_dev_priv(dev)->egress_priority_map[(skprio & 0xF)];
205 while (mp) {
206 if (mp->priority == skprio) {
207 return mp->vlan_qos; /* This should already be shifted
208 * to mask correctly with the
209 * VLAN's TCI */
210 }
211 mp = mp->next;
212 }
213 return 0;
214}
215
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +0000216extern bool vlan_do_receive(struct sk_buff **skb);
Patrick McHardy9b22ea52008-11-04 14:49:57 -0800217
Patrick McHardy80d5c362013-04-19 02:04:28 +0000218extern int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid);
219extern void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid);
Jiri Pirko87002b02011-12-08 04:11:17 +0000220
Jiri Pirko348a1442011-12-08 04:11:19 +0000221extern int vlan_vids_add_by_dev(struct net_device *dev,
222 const struct net_device *by_dev);
223extern void vlan_vids_del_by_dev(struct net_device *dev,
224 const struct net_device *by_dev);
Jiri Pirko9b361c12012-08-23 03:26:52 +0000225
226extern bool vlan_uses_dev(const struct net_device *dev);
Vlad Yaseviche1618d42014-05-20 10:59:26 -0400227
228static inline int vlan_get_encap_level(struct net_device *dev)
229{
230 BUG_ON(!is_vlan_dev(dev));
231 return vlan_dev_priv(dev)->nest_level;
232}
Patrick McHardy7750f402008-07-08 03:23:36 -0700233#else
Jiri Pirkocec9c132011-07-20 04:54:05 +0000234static inline struct net_device *
dingtianhongf06c7f9f2014-05-09 14:58:05 +0800235__vlan_find_dev_deep_rcu(struct net_device *real_dev,
Patrick McHardy9fae27b2013-04-20 23:51:41 +0000236 __be16 vlan_proto, u16 vlan_id)
Jiri Pirkocec9c132011-07-20 04:54:05 +0000237{
238 return NULL;
239}
240
Ivan Khoronzhuk960abf62018-11-08 22:27:55 +0200241static inline int
242vlan_for_each(struct net_device *dev,
243 int (*action)(struct net_device *dev, int vid, void *arg),
244 void *arg)
245{
246 return 0;
247}
248
Patrick McHardy22d1ba72008-07-08 03:23:57 -0700249static inline struct net_device *vlan_dev_real_dev(const struct net_device *dev)
250{
251 BUG();
252 return NULL;
253}
254
255static inline u16 vlan_dev_vlan_id(const struct net_device *dev)
256{
257 BUG();
258 return 0;
259}
260
dingtianhong71e415e2014-03-25 17:44:42 +0800261static inline __be16 vlan_dev_vlan_proto(const struct net_device *dev)
262{
263 BUG();
264 return 0;
265}
266
Eyal Perryd3243532013-11-06 15:37:23 +0200267static inline u16 vlan_dev_get_egress_qos_mask(struct net_device *dev,
268 u32 skprio)
269{
270 return 0;
271}
272
Florian Zumbiehl48cc32d32012-10-07 15:51:58 +0000273static inline bool vlan_do_receive(struct sk_buff **skb)
Patrick McHardy9b22ea52008-11-04 14:49:57 -0800274{
Jesse Gross3701e512010-10-20 13:56:06 +0000275 return false;
Patrick McHardy9b22ea52008-11-04 14:49:57 -0800276}
Herbert Xue1c096e2009-01-06 10:50:09 -0800277
Patrick McHardy9fae27b2013-04-20 23:51:41 +0000278static inline int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
Jiri Pirko87002b02011-12-08 04:11:17 +0000279{
280 return 0;
281}
282
Patrick McHardy9fae27b2013-04-20 23:51:41 +0000283static inline void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
Jiri Pirko87002b02011-12-08 04:11:17 +0000284{
285}
Jiri Pirko348a1442011-12-08 04:11:19 +0000286
287static inline int vlan_vids_add_by_dev(struct net_device *dev,
288 const struct net_device *by_dev)
289{
290 return 0;
291}
292
293static inline void vlan_vids_del_by_dev(struct net_device *dev,
294 const struct net_device *by_dev)
295{
296}
Jiri Pirko9b361c12012-08-23 03:26:52 +0000297
298static inline bool vlan_uses_dev(const struct net_device *dev)
299{
300 return false;
301}
Vlad Yaseviche1618d42014-05-20 10:59:26 -0400302static inline int vlan_get_encap_level(struct net_device *dev)
303{
304 BUG();
305 return 0;
306}
Patrick McHardy7750f402008-07-08 03:23:36 -0700307#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Eric Garverfe19c4f2016-09-07 12:56:58 -0400309/**
310 * eth_type_vlan - check for valid vlan ether type.
311 * @ethertype: ether type to check
312 *
313 * Returns true if the ether type is a vlan ether type.
314 */
315static inline bool eth_type_vlan(__be16 ethertype)
316{
317 switch (ethertype) {
318 case htons(ETH_P_8021Q):
319 case htons(ETH_P_8021AD):
320 return true;
321 default:
322 return false;
323 }
324}
325
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000326static inline bool vlan_hw_offload_capable(netdev_features_t features,
327 __be16 proto)
328{
329 if (proto == htons(ETH_P_8021Q) && features & NETIF_F_HW_VLAN_CTAG_TX)
330 return true;
Patrick McHardy8ad227f2013-04-19 02:04:31 +0000331 if (proto == htons(ETH_P_8021AD) && features & NETIF_F_HW_VLAN_STAG_TX)
332 return true;
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000333 return false;
334}
335
Patrick McHardy9bb85822008-07-08 03:24:44 -0700336/**
Toshiaki Makitacbe71282018-03-13 14:51:28 +0900337 * __vlan_insert_inner_tag - inner VLAN tag inserting
338 * @skb: skbuff to tag
339 * @vlan_proto: VLAN encapsulation protocol
340 * @vlan_tci: VLAN TCI to insert
341 * @mac_len: MAC header length including outer vlan headers
342 *
343 * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
Thadeu Lima de Souza Cascardo8051ac72018-05-31 09:20:20 -0300344 * Returns error if skb_cow_head fails.
Toshiaki Makitacbe71282018-03-13 14:51:28 +0900345 *
346 * Does not change skb->protocol so this function can be used during receive.
347 */
348static inline int __vlan_insert_inner_tag(struct sk_buff *skb,
349 __be16 vlan_proto, u16 vlan_tci,
350 unsigned int mac_len)
351{
352 struct vlan_ethhdr *veth;
353
354 if (skb_cow_head(skb, VLAN_HLEN) < 0)
355 return -ENOMEM;
356
357 skb_push(skb, VLAN_HLEN);
358
359 /* Move the mac header sans proto to the beginning of the new header. */
Toshiaki Makitac769acc2018-03-29 19:05:30 +0900360 if (likely(mac_len > ETH_TLEN))
361 memmove(skb->data, skb->data + VLAN_HLEN, mac_len - ETH_TLEN);
Toshiaki Makitacbe71282018-03-13 14:51:28 +0900362 skb->mac_header -= VLAN_HLEN;
363
364 veth = (struct vlan_ethhdr *)(skb->data + mac_len - ETH_HLEN);
365
366 /* first, the ethernet type */
Toshiaki Makitac769acc2018-03-29 19:05:30 +0900367 if (likely(mac_len >= ETH_TLEN)) {
368 /* h_vlan_encapsulated_proto should already be populated, and
369 * skb->data has space for h_vlan_proto
370 */
371 veth->h_vlan_proto = vlan_proto;
372 } else {
373 /* h_vlan_encapsulated_proto should not be populated, and
374 * skb->data has no space for h_vlan_proto
375 */
376 veth->h_vlan_encapsulated_proto = skb->protocol;
377 }
Toshiaki Makitacbe71282018-03-13 14:51:28 +0900378
379 /* now, the TCI */
380 veth->h_vlan_TCI = htons(vlan_tci);
381
382 return 0;
383}
384
385/**
Jiri Pirko15255a42014-11-19 14:05:00 +0100386 * __vlan_insert_tag - regular VLAN tag inserting
387 * @skb: skbuff to tag
388 * @vlan_proto: VLAN encapsulation protocol
389 * @vlan_tci: VLAN TCI to insert
390 *
391 * Inserts the VLAN tag into @skb as part of the payload
Thadeu Lima de Souza Cascardo8051ac72018-05-31 09:20:20 -0300392 * Returns error if skb_cow_head fails.
Jiri Pirko15255a42014-11-19 14:05:00 +0100393 *
394 * Does not change skb->protocol so this function can be used during receive.
395 */
396static inline int __vlan_insert_tag(struct sk_buff *skb,
397 __be16 vlan_proto, u16 vlan_tci)
398{
Toshiaki Makitacbe71282018-03-13 14:51:28 +0900399 return __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
400}
Jiri Pirko15255a42014-11-19 14:05:00 +0100401
Toshiaki Makitacbe71282018-03-13 14:51:28 +0900402/**
403 * vlan_insert_inner_tag - inner VLAN tag inserting
404 * @skb: skbuff to tag
405 * @vlan_proto: VLAN encapsulation protocol
406 * @vlan_tci: VLAN TCI to insert
407 * @mac_len: MAC header length including outer vlan headers
408 *
409 * Inserts the VLAN tag into @skb as part of the payload at offset mac_len
410 * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
411 *
412 * Following the skb_unshare() example, in case of error, the calling function
413 * doesn't have to worry about freeing the original skb.
414 *
415 * Does not change skb->protocol so this function can be used during receive.
416 */
417static inline struct sk_buff *vlan_insert_inner_tag(struct sk_buff *skb,
418 __be16 vlan_proto,
419 u16 vlan_tci,
420 unsigned int mac_len)
421{
422 int err;
Jiri Pirko15255a42014-11-19 14:05:00 +0100423
Toshiaki Makitacbe71282018-03-13 14:51:28 +0900424 err = __vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, mac_len);
425 if (err) {
426 dev_kfree_skb_any(skb);
427 return NULL;
428 }
429 return skb;
Jiri Pirko15255a42014-11-19 14:05:00 +0100430}
431
432/**
Jiri Pirko0b5c9db2011-06-10 06:56:58 +0000433 * vlan_insert_tag - regular VLAN tag inserting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 * @skb: skbuff to tag
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000435 * @vlan_proto: VLAN encapsulation protocol
Patrick McHardy9bb85822008-07-08 03:24:44 -0700436 * @vlan_tci: VLAN TCI to insert
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *
438 * Inserts the VLAN tag into @skb as part of the payload
439 * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
Patrick McHardy9bb85822008-07-08 03:24:44 -0700440 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 * Following the skb_unshare() example, in case of error, the calling function
442 * doesn't have to worry about freeing the original skb.
Jiri Pirko0b5c9db2011-06-10 06:56:58 +0000443 *
444 * Does not change skb->protocol so this function can be used during receive.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 */
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000446static inline struct sk_buff *vlan_insert_tag(struct sk_buff *skb,
447 __be16 vlan_proto, u16 vlan_tci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Toshiaki Makitacbe71282018-03-13 14:51:28 +0900449 return vlan_insert_inner_tag(skb, vlan_proto, vlan_tci, ETH_HLEN);
Jiri Pirko0b5c9db2011-06-10 06:56:58 +0000450}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Jiri Pirko0b5c9db2011-06-10 06:56:58 +0000452/**
Jiri Pirko62749e22014-11-19 14:04:58 +0100453 * vlan_insert_tag_set_proto - regular VLAN tag inserting
Jiri Pirko0b5c9db2011-06-10 06:56:58 +0000454 * @skb: skbuff to tag
Jiri Pirko62749e22014-11-19 14:04:58 +0100455 * @vlan_proto: VLAN encapsulation protocol
Jiri Pirko0b5c9db2011-06-10 06:56:58 +0000456 * @vlan_tci: VLAN TCI to insert
457 *
458 * Inserts the VLAN tag into @skb as part of the payload
459 * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
460 *
461 * Following the skb_unshare() example, in case of error, the calling function
462 * doesn't have to worry about freeing the original skb.
463 */
Jiri Pirko62749e22014-11-19 14:04:58 +0100464static inline struct sk_buff *vlan_insert_tag_set_proto(struct sk_buff *skb,
465 __be16 vlan_proto,
466 u16 vlan_tci)
Jiri Pirko0b5c9db2011-06-10 06:56:58 +0000467{
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000468 skb = vlan_insert_tag(skb, vlan_proto, vlan_tci);
Jiri Pirko0b5c9db2011-06-10 06:56:58 +0000469 if (skb)
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000470 skb->protocol = vlan_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return skb;
472}
473
Michał Mirosławc8accd52018-11-07 18:07:02 +0100474/**
475 * __vlan_hwaccel_clear_tag - clear hardware accelerated VLAN info
476 * @skb: skbuff to clear
477 *
478 * Clears the VLAN information from @skb
479 */
480static inline void __vlan_hwaccel_clear_tag(struct sk_buff *skb)
481{
Michał Mirosław0c4b2d32018-11-10 19:58:36 +0100482 skb->vlan_present = 0;
Michał Mirosławc8accd52018-11-07 18:07:02 +0100483}
484
Michał Mirosławe0a6b802018-11-07 18:07:02 +0100485/**
486 * __vlan_hwaccel_copy_tag - copy hardware accelerated VLAN info from another skb
487 * @dst: skbuff to copy to
488 * @src: skbuff to copy from
489 *
490 * Copies VLAN information from @src to @dst (for branchless code)
491 */
492static inline void __vlan_hwaccel_copy_tag(struct sk_buff *dst, const struct sk_buff *src)
493{
Michał Mirosław0c4b2d32018-11-10 19:58:36 +0100494 dst->vlan_present = src->vlan_present;
Michał Mirosławe0a6b802018-11-07 18:07:02 +0100495 dst->vlan_proto = src->vlan_proto;
496 dst->vlan_tci = src->vlan_tci;
497}
498
Jiri Pirko59682502014-11-19 14:04:59 +0100499/*
500 * __vlan_hwaccel_push_inside - pushes vlan tag to the payload
501 * @skb: skbuff to tag
502 *
503 * Pushes the VLAN tag from @skb->vlan_tci inside to the payload.
504 *
505 * Following the skb_unshare() example, in case of error, the calling function
506 * doesn't have to worry about freeing the original skb.
507 */
508static inline struct sk_buff *__vlan_hwaccel_push_inside(struct sk_buff *skb)
509{
510 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100511 skb_vlan_tag_get(skb));
Jiri Pirko59682502014-11-19 14:04:59 +0100512 if (likely(skb))
Michał Mirosławc8accd52018-11-07 18:07:02 +0100513 __vlan_hwaccel_clear_tag(skb);
Jiri Pirko59682502014-11-19 14:04:59 +0100514 return skb;
515}
Jiri Pirko59682502014-11-19 14:04:59 +0100516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517/**
518 * __vlan_hwaccel_put_tag - hardware accelerated VLAN inserting
519 * @skb: skbuff to tag
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000520 * @vlan_proto: VLAN encapsulation protocol
Patrick McHardy9bb85822008-07-08 03:24:44 -0700521 * @vlan_tci: VLAN TCI to insert
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 *
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700523 * Puts the VLAN TCI in @skb->vlan_tci and lets the device do the rest
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 */
Jiri Pirkob960a0ac2014-11-19 14:04:56 +0100525static inline void __vlan_hwaccel_put_tag(struct sk_buff *skb,
526 __be16 vlan_proto, u16 vlan_tci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000528 skb->vlan_proto = vlan_proto;
Michał Mirosław0c4b2d32018-11-10 19:58:36 +0100529 skb->vlan_tci = vlan_tci;
530 skb->vlan_present = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 * __vlan_get_tag - get the VLAN ID that is part of the payload
535 * @skb: skbuff to query
Vivien Didelotf4fb8742015-05-27 21:07:26 -0400536 * @vlan_tci: buffer to store value
Patrick McHardy9bb85822008-07-08 03:24:44 -0700537 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * Returns error if the skb is not of VLAN type
539 */
Patrick McHardy9bb85822008-07-08 03:24:44 -0700540static inline int __vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)skb->data;
543
Eric Garverfe19c4f2016-09-07 12:56:58 -0400544 if (!eth_type_vlan(veth->h_vlan_proto))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Patrick McHardy9bb85822008-07-08 03:24:44 -0700547 *vlan_tci = ntohs(veth->h_vlan_TCI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return 0;
549}
550
551/**
552 * __vlan_hwaccel_get_tag - get the VLAN ID that is in @skb->cb[]
553 * @skb: skbuff to query
Vivien Didelotf4fb8742015-05-27 21:07:26 -0400554 * @vlan_tci: buffer to store value
Patrick McHardy9bb85822008-07-08 03:24:44 -0700555 *
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700556 * Returns error if @skb->vlan_tci is not set correctly
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 */
Patrick McHardy18149932008-02-05 16:20:22 -0800558static inline int __vlan_hwaccel_get_tag(const struct sk_buff *skb,
Patrick McHardy9bb85822008-07-08 03:24:44 -0700559 u16 *vlan_tci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100561 if (skb_vlan_tag_present(skb)) {
562 *vlan_tci = skb_vlan_tag_get(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return 0;
564 } else {
Patrick McHardy9bb85822008-07-08 03:24:44 -0700565 *vlan_tci = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return -EINVAL;
567 }
568}
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570/**
571 * vlan_get_tag - get the VLAN ID from the skb
572 * @skb: skbuff to query
Vivien Didelotf4fb8742015-05-27 21:07:26 -0400573 * @vlan_tci: buffer to store value
Patrick McHardy9bb85822008-07-08 03:24:44 -0700574 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 * Returns error if the skb is not VLAN tagged
576 */
Patrick McHardy9bb85822008-07-08 03:24:44 -0700577static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Patrick McHardyf6469682013-04-19 02:04:27 +0000579 if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
Patrick McHardy9bb85822008-07-08 03:24:44 -0700580 return __vlan_hwaccel_get_tag(skb, vlan_tci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 } else {
Patrick McHardy9bb85822008-07-08 03:24:44 -0700582 return __vlan_get_tag(skb, vlan_tci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584}
585
Hao Zheng0a85df02010-11-11 13:47:57 +0000586/**
587 * vlan_get_protocol - get protocol EtherType.
588 * @skb: skbuff to query
Toshiaki Makitad4bcef32015-01-29 20:37:07 +0900589 * @type: first vlan protocol
590 * @depth: buffer to store length of eth and vlan tags in bytes
Hao Zheng0a85df02010-11-11 13:47:57 +0000591 *
592 * Returns the EtherType of the packet, regardless of whether it is
593 * vlan encapsulated (normal or hardware accelerated) or not.
594 */
Toshiaki Makitad4bcef32015-01-29 20:37:07 +0900595static inline __be16 __vlan_get_protocol(struct sk_buff *skb, __be16 type,
596 int *depth)
Hao Zheng0a85df02010-11-11 13:47:57 +0000597{
Toshiaki Makitad4bcef32015-01-29 20:37:07 +0900598 unsigned int vlan_depth = skb->mac_len;
Hao Zheng0a85df02010-11-11 13:47:57 +0000599
Toshiaki Makitad4bcef32015-01-29 20:37:07 +0900600 /* if type is 802.1Q/AD then the header should already be
601 * present at mac_len - VLAN_HLEN (if mac_len > 0), or at
602 * ETH_HLEN otherwise
603 */
Eric Garverfe19c4f2016-09-07 12:56:58 -0400604 if (eth_type_vlan(type)) {
Toshiaki Makitad4bcef32015-01-29 20:37:07 +0900605 if (vlan_depth) {
606 if (WARN_ON(vlan_depth < VLAN_HLEN))
607 return 0;
608 vlan_depth -= VLAN_HLEN;
609 } else {
610 vlan_depth = ETH_HLEN;
611 }
612 do {
613 struct vlan_hdr *vh;
614
615 if (unlikely(!pskb_may_pull(skb,
616 vlan_depth + VLAN_HLEN)))
617 return 0;
618
619 vh = (struct vlan_hdr *)(skb->data + vlan_depth);
620 type = vh->h_vlan_encapsulated_proto;
621 vlan_depth += VLAN_HLEN;
Eric Garverfe19c4f2016-09-07 12:56:58 -0400622 } while (eth_type_vlan(type));
Hao Zheng0a85df02010-11-11 13:47:57 +0000623 }
624
Toshiaki Makitad4bcef32015-01-29 20:37:07 +0900625 if (depth)
626 *depth = vlan_depth;
627
628 return type;
629}
630
631/**
632 * vlan_get_protocol - get protocol EtherType.
633 * @skb: skbuff to query
634 *
635 * Returns the EtherType of the packet, regardless of whether it is
636 * vlan encapsulated (normal or hardware accelerated) or not.
637 */
638static inline __be16 vlan_get_protocol(struct sk_buff *skb)
639{
640 return __vlan_get_protocol(skb, skb->protocol, NULL);
Hao Zheng0a85df02010-11-11 13:47:57 +0000641}
Pravin B Shelar396cf942011-11-18 13:15:54 -0800642
643static inline void vlan_set_encap_proto(struct sk_buff *skb,
644 struct vlan_hdr *vhdr)
645{
646 __be16 proto;
Cong Wangda8c8722013-02-21 23:32:27 +0000647 unsigned short *rawp;
Pravin B Shelar396cf942011-11-18 13:15:54 -0800648
649 /*
650 * Was a VLAN packet, grab the encapsulated protocol, which the layer
651 * three protocols care about.
652 */
653
654 proto = vhdr->h_vlan_encapsulated_proto;
Alexander Duyck9545b222015-05-04 14:34:10 -0700655 if (eth_proto_is_802_3(proto)) {
Pravin B Shelar396cf942011-11-18 13:15:54 -0800656 skb->protocol = proto;
657 return;
658 }
659
Cong Wangda8c8722013-02-21 23:32:27 +0000660 rawp = (unsigned short *)(vhdr + 1);
661 if (*rawp == 0xFFFF)
Pravin B Shelar396cf942011-11-18 13:15:54 -0800662 /*
663 * This is a magic hack to spot IPX packets. Older Novell
664 * breaks the protocol design and runs IPX over 802.3 without
665 * an 802.2 LLC layer. We look for FFFF which isn't a used
666 * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
667 * but does for the rest.
668 */
669 skb->protocol = htons(ETH_P_802_3);
670 else
671 /*
672 * Real 802.2 LLC
673 */
674 skb->protocol = htons(ETH_P_802_2);
675}
Vlad Yasevich44a40852014-05-16 17:20:38 -0400676
Toshiaki Makitaf5a7fb82015-03-27 14:31:11 +0900677/**
678 * skb_vlan_tagged - check if skb is vlan tagged.
679 * @skb: skbuff to query
680 *
681 * Returns true if the skb is tagged, regardless of whether it is hardware
682 * accelerated or not.
683 */
684static inline bool skb_vlan_tagged(const struct sk_buff *skb)
685{
686 if (!skb_vlan_tag_present(skb) &&
Eric Garverfe19c4f2016-09-07 12:56:58 -0400687 likely(!eth_type_vlan(skb->protocol)))
Toshiaki Makitaf5a7fb82015-03-27 14:31:11 +0900688 return false;
689
690 return true;
691}
692
693/**
694 * skb_vlan_tagged_multi - check if skb is vlan tagged with multiple headers.
695 * @skb: skbuff to query
696 *
697 * Returns true if the skb is tagged with multiple vlan headers, regardless
698 * of whether it is hardware accelerated or not.
699 */
Toshiaki Makita7ce23672018-04-17 18:46:14 +0900700static inline bool skb_vlan_tagged_multi(struct sk_buff *skb)
Toshiaki Makitaf5a7fb82015-03-27 14:31:11 +0900701{
702 __be16 protocol = skb->protocol;
703
704 if (!skb_vlan_tag_present(skb)) {
705 struct vlan_ethhdr *veh;
706
Eric Garverfe19c4f2016-09-07 12:56:58 -0400707 if (likely(!eth_type_vlan(protocol)))
Toshiaki Makitaf5a7fb82015-03-27 14:31:11 +0900708 return false;
709
Toshiaki Makita7ce23672018-04-17 18:46:14 +0900710 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
711 return false;
712
Toshiaki Makitaf5a7fb82015-03-27 14:31:11 +0900713 veh = (struct vlan_ethhdr *)skb->data;
714 protocol = veh->h_vlan_encapsulated_proto;
715 }
716
Eric Garverfe19c4f2016-09-07 12:56:58 -0400717 if (!eth_type_vlan(protocol))
Toshiaki Makitaf5a7fb82015-03-27 14:31:11 +0900718 return false;
719
720 return true;
721}
722
Toshiaki Makita8cb65d02015-03-27 14:31:12 +0900723/**
724 * vlan_features_check - drop unsafe features for skb with multiple tags.
725 * @skb: skbuff to query
726 * @features: features to be checked
727 *
728 * Returns features without unsafe ones if the skb has multiple tags.
729 */
Toshiaki Makita7ce23672018-04-17 18:46:14 +0900730static inline netdev_features_t vlan_features_check(struct sk_buff *skb,
Toshiaki Makita8cb65d02015-03-27 14:31:12 +0900731 netdev_features_t features)
732{
Vlad Yasevich35d2f802017-05-23 13:38:41 -0400733 if (skb_vlan_tagged_multi(skb)) {
734 /* In the case of multi-tagged packets, use a direct mask
735 * instead of using netdev_interesect_features(), to make
736 * sure that only devices supporting NETIF_F_HW_CSUM will
737 * have checksum offloading support.
738 */
739 features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
740 NETIF_F_FRAGLIST | NETIF_F_HW_VLAN_CTAG_TX |
741 NETIF_F_HW_VLAN_STAG_TX;
742 }
Toshiaki Makita8cb65d02015-03-27 14:31:12 +0900743
744 return features;
745}
746
Toshiaki Makita66e51332015-06-01 21:55:06 +0900747/**
748 * compare_vlan_header - Compare two vlan headers
749 * @h1: Pointer to vlan header
750 * @h2: Pointer to vlan header
751 *
752 * Compare two vlan headers, returns 0 if equal.
753 *
754 * Please note that alignment of h1 & h2 are only guaranteed to be 16 bits.
755 */
756static inline unsigned long compare_vlan_header(const struct vlan_hdr *h1,
757 const struct vlan_hdr *h2)
758{
759#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
760 return *(u32 *)h1 ^ *(u32 *)h2;
761#else
762 return ((__force u32)h1->h_vlan_TCI ^ (__force u32)h2->h_vlan_TCI) |
763 ((__force u32)h1->h_vlan_encapsulated_proto ^
764 (__force u32)h2->h_vlan_encapsulated_proto);
765#endif
766}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767#endif /* !(_LINUX_IF_VLAN_H_) */