3 * Ethernet-type device handling.
5 * Authors: Ben Greear <greearb@candelatech.com>
6 * Please send support related email to: vlan@scry.wanfear.com
7 * VLAN Home Page: http://www.candelatech.com/~greear/vlan.html
9 * Fixes: Mar 22 2001: Martin Bokaemper <mbokaemper@unispherenetworks.com>
10 * - reset skb->pkt_type on incoming packets when MAC was changed
11 * - see that changed MAC is saddr for outgoing packets
12 * Oct 20, 2001: Ard van Breeman:
13 * - Fix MC-list, finally.
14 * - Flush MC-list on VLAN destroy.
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version
20 * 2 of the License, or (at your option) any later version.
23 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <asm/uaccess.h> /* for copy_from_user */
28 #include <linux/skbuff.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <net/datalink.h>
32 #include <net/p8022.h>
37 #include <linux/if_vlan.h>
41 * Rebuild the Ethernet MAC header. This is called after an ARP
42 * (or in future other address resolution) has completed on this
43 * sk_buff. We now let ARP fill in the other fields.
45 * This routine CANNOT use cached dst->neigh!
46 * Really, it is used only when dst->neigh is wrong.
48 * TODO: This needs a checkup, I'm ignorant here. --BLG
50 int vlan_dev_rebuild_header(struct sk_buff
*skb
)
52 struct net_device
*dev
= skb
->dev
;
53 struct vlan_ethhdr
*veth
= (struct vlan_ethhdr
*)(skb
->data
);
55 switch (veth
->h_vlan_encapsulated_proto
) {
57 case __constant_htons(ETH_P_IP
):
59 /* TODO: Confirm this will work with VLAN headers... */
60 return arp_find(veth
->h_dest
, skb
);
64 "%s: unable to resolve type %X addresses.\n",
65 dev
->name
, ntohs(veth
->h_vlan_encapsulated_proto
));
67 memcpy(veth
->h_source
, dev
->dev_addr
, ETH_ALEN
);
74 static inline struct sk_buff
*vlan_check_reorder_header(struct sk_buff
*skb
)
76 if (VLAN_DEV_INFO(skb
->dev
)->flags
& VLAN_FLAG_REORDER_HDR
) {
77 if (skb_shared(skb
) || skb_cloned(skb
)) {
78 struct sk_buff
*nskb
= skb_copy(skb
, GFP_ATOMIC
);
83 /* Lifted from Gleb's VLAN code... */
84 memmove(skb
->data
- ETH_HLEN
,
85 skb
->data
- VLAN_ETH_HLEN
, 12);
86 skb
->mac_header
+= VLAN_HLEN
;
94 * Determine the packet's protocol ID. The rule here is that we
95 * assume 802.3 if the type field is short enough to be a length.
96 * This is normal practice and works for any 'now in use' protocol.
98 * Also, at this point we assume that we ARE dealing exclusively with
99 * VLAN packets, or packets that should be made into VLAN packets based
100 * on a default VLAN ID.
102 * NOTE: Should be similar to ethernet/eth.c.
104 * SANITY NOTE: This method is called when a packet is moving up the stack
105 * towards userland. To get here, it would have already passed
106 * through the ethernet/eth.c eth_type_trans() method.
107 * SANITY NOTE 2: We are referencing to the VLAN_HDR frields, which MAY be
108 * stored UNALIGNED in the memory. RISC systems don't like
109 * such cases very much...
110 * SANITY NOTE 2a: According to Dave Miller & Alexey, it will always be aligned,
111 * so there doesn't need to be any of the unaligned stuff. It has
112 * been commented out now... --Ben
115 int vlan_skb_recv(struct sk_buff
*skb
, struct net_device
*dev
,
116 struct packet_type
* ptype
, struct net_device
*orig_dev
)
118 unsigned char *rawp
= NULL
;
119 struct vlan_hdr
*vhdr
;
121 struct net_device_stats
*stats
;
122 unsigned short vlan_TCI
;
125 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)
128 if (unlikely(!pskb_may_pull(skb
, VLAN_HLEN
))) {
133 vhdr
= (struct vlan_hdr
*)(skb
->data
);
135 /* vlan_TCI = ntohs(get_unaligned(&vhdr->h_vlan_TCI)); */
136 vlan_TCI
= ntohs(vhdr
->h_vlan_TCI
);
138 vid
= (vlan_TCI
& VLAN_VID_MASK
);
141 printk(VLAN_DBG
"%s: skb: %p vlan_id: %hx\n",
142 __FUNCTION__
, skb
, vid
);
145 /* Ok, we will find the correct VLAN device, strip the header,
146 * and then go on as usual.
149 /* We have 12 bits of vlan ID.
151 * We must not drop allow preempt until we hold a
152 * reference to the device (netif_rx does that) or we
157 skb
->dev
= __find_vlan_dev(dev
, vid
);
162 printk(VLAN_DBG
"%s: ERROR: No net_device for VID: %i on dev: %s [%i]\n",
163 __FUNCTION__
, (unsigned int)(vid
), dev
->name
, dev
->ifindex
);
169 skb
->dev
->last_rx
= jiffies
;
171 /* Bump the rx counters for the VLAN device. */
172 stats
= vlan_dev_get_stats(skb
->dev
);
174 stats
->rx_bytes
+= skb
->len
;
176 /* Take off the VLAN header (4 bytes currently) */
177 skb_pull_rcsum(skb
, VLAN_HLEN
);
179 /* Ok, lets check to make sure the device (dev) we
180 * came in on is what this VLAN is attached to.
183 if (dev
!= VLAN_DEV_INFO(skb
->dev
)->real_dev
) {
187 printk(VLAN_DBG
"%s: dropping skb: %p because came in on wrong device, dev: %s real_dev: %s, skb_dev: %s\n",
188 __FUNCTION__
, skb
, dev
->name
,
189 VLAN_DEV_INFO(skb
->dev
)->real_dev
->name
,
198 * Deal with ingress priority mapping.
200 skb
->priority
= vlan_get_ingress_priority(skb
->dev
, ntohs(vhdr
->h_vlan_TCI
));
203 printk(VLAN_DBG
"%s: priority: %lu for TCI: %hu (hbo)\n",
204 __FUNCTION__
, (unsigned long)(skb
->priority
),
205 ntohs(vhdr
->h_vlan_TCI
));
208 /* The ethernet driver already did the pkt_type calculations
211 switch (skb
->pkt_type
) {
212 case PACKET_BROADCAST
: /* Yeah, stats collect these together.. */
213 // stats->broadcast ++; // no such counter :-(
216 case PACKET_MULTICAST
:
220 case PACKET_OTHERHOST
:
221 /* Our lower layer thinks this is not local, let's make sure.
222 * This allows the VLAN to have a different MAC than the underlying
223 * device, and still route correctly.
225 if (!compare_ether_addr(eth_hdr(skb
)->h_dest
, skb
->dev
->dev_addr
)) {
226 /* It is for our (changed) MAC-address! */
227 skb
->pkt_type
= PACKET_HOST
;
234 /* Was a VLAN packet, grab the encapsulated protocol, which the layer
235 * three protocols care about.
237 /* proto = get_unaligned(&vhdr->h_vlan_encapsulated_proto); */
238 proto
= vhdr
->h_vlan_encapsulated_proto
;
240 skb
->protocol
= proto
;
241 if (ntohs(proto
) >= 1536) {
242 /* place it back on the queue to be handled by
243 * true layer 3 protocols.
246 /* See if we are configured to re-write the VLAN header
247 * to make it look like ethernet...
249 skb
= vlan_check_reorder_header(skb
);
251 /* Can be null if skb-clone fails when re-ordering */
255 /* TODO: Add a more specific counter here. */
265 * This is a magic hack to spot IPX packets. Older Novell breaks
266 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
267 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
268 * won't work for fault tolerant netware but does for the rest.
270 if (*(unsigned short *)rawp
== 0xFFFF) {
271 skb
->protocol
= htons(ETH_P_802_3
);
272 /* place it back on the queue to be handled by true layer 3 protocols.
275 /* See if we are configured to re-write the VLAN header
276 * to make it look like ethernet...
278 skb
= vlan_check_reorder_header(skb
);
280 /* Can be null if skb-clone fails when re-ordering */
284 /* TODO: Add a more specific counter here. */
294 skb
->protocol
= htons(ETH_P_802_2
);
295 /* place it back on the queue to be handled by upper layer protocols.
298 /* See if we are configured to re-write the VLAN header
299 * to make it look like ethernet...
301 skb
= vlan_check_reorder_header(skb
);
303 /* Can be null if skb-clone fails when re-ordering */
307 /* TODO: Add a more specific counter here. */
314 static inline unsigned short vlan_dev_get_egress_qos_mask(struct net_device
* dev
,
317 struct vlan_priority_tci_mapping
*mp
=
318 VLAN_DEV_INFO(dev
)->egress_priority_map
[(skb
->priority
& 0xF)];
321 if (mp
->priority
== skb
->priority
) {
322 return mp
->vlan_qos
; /* This should already be shifted to mask
323 * correctly with the VLAN's TCI
332 * Create the VLAN header for an arbitrary protocol layer
334 * saddr=NULL means use device source address
335 * daddr=NULL means leave destination address (eg unresolved arp)
337 * This is called when the SKB is moving down the stack towards the
340 int vlan_dev_hard_header(struct sk_buff
*skb
, struct net_device
*dev
,
341 unsigned short type
, void *daddr
, void *saddr
,
344 struct vlan_hdr
*vhdr
;
345 unsigned short veth_TCI
= 0;
347 int build_vlan_header
= 0;
348 struct net_device
*vdev
= dev
; /* save this for the bottom of the method */
351 printk(VLAN_DBG
"%s: skb: %p type: %hx len: %x vlan_id: %hx, daddr: %p\n",
352 __FUNCTION__
, skb
, type
, len
, VLAN_DEV_INFO(dev
)->vlan_id
, daddr
);
355 /* build vlan header only if re_order_header flag is NOT set. This
356 * fixes some programs that get confused when they see a VLAN device
357 * sending a frame that is VLAN encoded (the consensus is that the VLAN
358 * device should look completely like an Ethernet device when the
359 * REORDER_HEADER flag is set) The drawback to this is some extra
360 * header shuffling in the hard_start_xmit. Users can turn off this
361 * REORDER behaviour with the vconfig tool.
363 if (!(VLAN_DEV_INFO(dev
)->flags
& VLAN_FLAG_REORDER_HDR
))
364 build_vlan_header
= 1;
366 if (build_vlan_header
) {
367 vhdr
= (struct vlan_hdr
*) skb_push(skb
, VLAN_HLEN
);
369 /* build the four bytes that make this a VLAN header. */
371 /* Now, construct the second two bytes. This field looks something
373 * usr_priority: 3 bits (high bits)
375 * VLAN ID 12 bits (low bits)
378 veth_TCI
= VLAN_DEV_INFO(dev
)->vlan_id
;
379 veth_TCI
|= vlan_dev_get_egress_qos_mask(dev
, skb
);
381 vhdr
->h_vlan_TCI
= htons(veth_TCI
);
384 * Set the protocol type.
385 * For a packet of type ETH_P_802_3 we put the length in here instead.
386 * It is up to the 802.2 layer to carry protocol information.
389 if (type
!= ETH_P_802_3
) {
390 vhdr
->h_vlan_encapsulated_proto
= htons(type
);
392 vhdr
->h_vlan_encapsulated_proto
= htons(len
);
395 skb
->protocol
= htons(ETH_P_8021Q
);
396 skb_reset_network_header(skb
);
399 /* Before delegating work to the lower layer, enter our MAC-address */
401 saddr
= dev
->dev_addr
;
403 dev
= VLAN_DEV_INFO(dev
)->real_dev
;
405 /* MPLS can send us skbuffs w/out enough space. This check will grow the
406 * skb if it doesn't have enough headroom. Not a beautiful solution, so
407 * I'll tick a counter so that users can know it's happening... If they
411 /* NOTE: This may still break if the underlying device is not the final
412 * device (and thus there are more headers to add...) It should work for
413 * good-ole-ethernet though.
415 if (skb_headroom(skb
) < dev
->hard_header_len
) {
416 struct sk_buff
*sk_tmp
= skb
;
417 skb
= skb_realloc_headroom(sk_tmp
, dev
->hard_header_len
);
420 struct net_device_stats
*stats
= vlan_dev_get_stats(vdev
);
424 VLAN_DEV_INFO(vdev
)->cnt_inc_headroom_on_tx
++;
426 printk(VLAN_DBG
"%s: %s: had to grow skb.\n", __FUNCTION__
, vdev
->name
);
430 if (build_vlan_header
) {
431 /* Now make the underlying real hard header */
432 rc
= dev
->hard_header(skb
, dev
, ETH_P_8021Q
, daddr
, saddr
, len
+ VLAN_HLEN
);
440 /* If here, then we'll just make a normal looking ethernet frame,
441 * but, the hard_start_xmit method will insert the tag (it has to
442 * be able to do this for bridged and other skbs that don't come
443 * down the protocol stack in an orderly manner.
445 rc
= dev
->hard_header(skb
, dev
, type
, daddr
, saddr
, len
);
451 int vlan_dev_hard_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
453 struct net_device_stats
*stats
= vlan_dev_get_stats(dev
);
454 struct vlan_ethhdr
*veth
= (struct vlan_ethhdr
*)(skb
->data
);
456 /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
458 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
459 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
462 if (veth
->h_vlan_proto
!= htons(ETH_P_8021Q
) ||
463 VLAN_DEV_INFO(dev
)->flags
& VLAN_FLAG_REORDER_HDR
) {
464 int orig_headroom
= skb_headroom(skb
);
465 unsigned short veth_TCI
;
467 /* This is not a VLAN frame...but we can fix that! */
468 VLAN_DEV_INFO(dev
)->cnt_encap_on_xmit
++;
471 printk(VLAN_DBG
"%s: proto to encap: 0x%hx (hbo)\n",
472 __FUNCTION__
, htons(veth
->h_vlan_proto
));
474 /* Construct the second two bytes. This field looks something
476 * usr_priority: 3 bits (high bits)
478 * VLAN ID 12 bits (low bits)
480 veth_TCI
= VLAN_DEV_INFO(dev
)->vlan_id
;
481 veth_TCI
|= vlan_dev_get_egress_qos_mask(dev
, skb
);
483 skb
= __vlan_put_tag(skb
, veth_TCI
);
489 if (orig_headroom
< VLAN_HLEN
) {
490 VLAN_DEV_INFO(dev
)->cnt_inc_headroom_on_tx
++;
495 printk(VLAN_DBG
"%s: about to send skb: %p to dev: %s\n",
496 __FUNCTION__
, skb
, skb
->dev
->name
);
497 printk(VLAN_DBG
" %2hx.%2hx.%2hx.%2xh.%2hx.%2hx %2hx.%2hx.%2hx.%2hx.%2hx.%2hx %4hx %4hx %4hx\n",
498 veth
->h_dest
[0], veth
->h_dest
[1], veth
->h_dest
[2], veth
->h_dest
[3], veth
->h_dest
[4], veth
->h_dest
[5],
499 veth
->h_source
[0], veth
->h_source
[1], veth
->h_source
[2], veth
->h_source
[3], veth
->h_source
[4], veth
->h_source
[5],
500 veth
->h_vlan_proto
, veth
->h_vlan_TCI
, veth
->h_vlan_encapsulated_proto
);
503 stats
->tx_packets
++; /* for statics only */
504 stats
->tx_bytes
+= skb
->len
;
506 skb
->dev
= VLAN_DEV_INFO(dev
)->real_dev
;
512 int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
514 struct net_device_stats
*stats
= vlan_dev_get_stats(dev
);
515 unsigned short veth_TCI
;
517 /* Construct the second two bytes. This field looks something
519 * usr_priority: 3 bits (high bits)
521 * VLAN ID 12 bits (low bits)
523 veth_TCI
= VLAN_DEV_INFO(dev
)->vlan_id
;
524 veth_TCI
|= vlan_dev_get_egress_qos_mask(dev
, skb
);
525 skb
= __vlan_hwaccel_put_tag(skb
, veth_TCI
);
528 stats
->tx_bytes
+= skb
->len
;
530 skb
->dev
= VLAN_DEV_INFO(dev
)->real_dev
;
536 int vlan_dev_change_mtu(struct net_device
*dev
, int new_mtu
)
538 /* TODO: gotta make sure the underlying layer can handle it,
539 * maybe an IFF_VLAN_CAPABLE flag for devices?
541 if (VLAN_DEV_INFO(dev
)->real_dev
->mtu
< new_mtu
)
549 void vlan_dev_set_ingress_priority(const struct net_device
*dev
,
550 u32 skb_prio
, short vlan_prio
)
552 struct vlan_dev_info
*vlan
= VLAN_DEV_INFO(dev
);
554 if (vlan
->ingress_priority_map
[vlan_prio
& 0x7] && !skb_prio
)
555 vlan
->nr_ingress_mappings
--;
556 else if (!vlan
->ingress_priority_map
[vlan_prio
& 0x7] && skb_prio
)
557 vlan
->nr_ingress_mappings
++;
559 vlan
->ingress_priority_map
[vlan_prio
& 0x7] = skb_prio
;
562 int vlan_dev_set_egress_priority(const struct net_device
*dev
,
563 u32 skb_prio
, short vlan_prio
)
565 struct vlan_dev_info
*vlan
= VLAN_DEV_INFO(dev
);
566 struct vlan_priority_tci_mapping
*mp
= NULL
;
567 struct vlan_priority_tci_mapping
*np
;
568 u32 vlan_qos
= (vlan_prio
<< 13) & 0xE000;
570 /* See if a priority mapping exists.. */
571 mp
= vlan
->egress_priority_map
[skb_prio
& 0xF];
573 if (mp
->priority
== skb_prio
) {
574 if (mp
->vlan_qos
&& !vlan_qos
)
575 vlan
->nr_egress_mappings
--;
576 else if (!mp
->vlan_qos
&& vlan_qos
)
577 vlan
->nr_egress_mappings
++;
578 mp
->vlan_qos
= vlan_qos
;
584 /* Create a new mapping then. */
585 mp
= vlan
->egress_priority_map
[skb_prio
& 0xF];
586 np
= kmalloc(sizeof(struct vlan_priority_tci_mapping
), GFP_KERNEL
);
591 np
->priority
= skb_prio
;
592 np
->vlan_qos
= vlan_qos
;
593 vlan
->egress_priority_map
[skb_prio
& 0xF] = np
;
595 vlan
->nr_egress_mappings
++;
599 /* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
600 int vlan_dev_set_vlan_flag(const struct net_device
*dev
,
601 u32 flag
, short flag_val
)
603 /* verify flag is supported */
604 if (flag
== VLAN_FLAG_REORDER_HDR
) {
606 VLAN_DEV_INFO(dev
)->flags
|= VLAN_FLAG_REORDER_HDR
;
608 VLAN_DEV_INFO(dev
)->flags
&= ~VLAN_FLAG_REORDER_HDR
;
612 printk(KERN_ERR
"%s: flag %i is not valid.\n", __FUNCTION__
, flag
);
616 void vlan_dev_get_realdev_name(const struct net_device
*dev
, char *result
)
618 strncpy(result
, VLAN_DEV_INFO(dev
)->real_dev
->name
, 23);
621 void vlan_dev_get_vid(const struct net_device
*dev
, unsigned short *result
)
623 *result
= VLAN_DEV_INFO(dev
)->vlan_id
;
626 int vlan_dev_open(struct net_device
*dev
)
628 struct vlan_dev_info
*vlan
= VLAN_DEV_INFO(dev
);
629 struct net_device
*real_dev
= vlan
->real_dev
;
632 if (!(real_dev
->flags
& IFF_UP
))
635 if (compare_ether_addr(dev
->dev_addr
, real_dev
->dev_addr
)) {
636 err
= dev_unicast_add(real_dev
, dev
->dev_addr
, ETH_ALEN
);
640 memcpy(vlan
->real_dev_addr
, real_dev
->dev_addr
, ETH_ALEN
);
642 if (dev
->flags
& IFF_ALLMULTI
)
643 dev_set_allmulti(real_dev
, 1);
644 if (dev
->flags
& IFF_PROMISC
)
645 dev_set_promiscuity(real_dev
, 1);
650 int vlan_dev_stop(struct net_device
*dev
)
652 struct net_device
*real_dev
= VLAN_DEV_INFO(dev
)->real_dev
;
654 dev_mc_unsync(real_dev
, dev
);
655 if (dev
->flags
& IFF_ALLMULTI
)
656 dev_set_allmulti(real_dev
, -1);
657 if (dev
->flags
& IFF_PROMISC
)
658 dev_set_promiscuity(real_dev
, -1);
660 if (compare_ether_addr(dev
->dev_addr
, real_dev
->dev_addr
))
661 dev_unicast_delete(real_dev
, dev
->dev_addr
, dev
->addr_len
);
666 int vlan_dev_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
668 struct net_device
*real_dev
= VLAN_DEV_INFO(dev
)->real_dev
;
670 int err
= -EOPNOTSUPP
;
672 strncpy(ifrr
.ifr_name
, real_dev
->name
, IFNAMSIZ
);
673 ifrr
.ifr_ifru
= ifr
->ifr_ifru
;
679 if (real_dev
->do_ioctl
&& netif_device_present(real_dev
))
680 err
= real_dev
->do_ioctl(real_dev
, &ifrr
, cmd
);
685 ifr
->ifr_ifru
= ifrr
.ifr_ifru
;
690 void vlan_change_rx_flags(struct net_device
*dev
, int change
)
692 struct net_device
*real_dev
= VLAN_DEV_INFO(dev
)->real_dev
;
694 if (change
& IFF_ALLMULTI
)
695 dev_set_allmulti(real_dev
, dev
->flags
& IFF_ALLMULTI
? 1 : -1);
696 if (change
& IFF_PROMISC
)
697 dev_set_promiscuity(real_dev
, dev
->flags
& IFF_PROMISC
? 1 : -1);
700 /** Taken from Gleb + Lennert's VLAN code, and modified... */
701 void vlan_dev_set_multicast_list(struct net_device
*vlan_dev
)
703 dev_mc_sync(VLAN_DEV_INFO(vlan_dev
)->real_dev
, vlan_dev
);