3 * Ethernet-type device handling.
5 * Authors: Ben Greear <greearb@candelatech.com>
6 * Please send support related email to: netdev@vger.kernel.org
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 <linux/ethtool.h>
32 #include <net/datalink.h>
33 #include <net/p8022.h>
38 #include <linux/if_vlan.h>
42 * Rebuild the Ethernet MAC header. This is called after an ARP
43 * (or in future other address resolution) has completed on this
44 * sk_buff. We now let ARP fill in the other fields.
46 * This routine CANNOT use cached dst->neigh!
47 * Really, it is used only when dst->neigh is wrong.
49 * TODO: This needs a checkup, I'm ignorant here. --BLG
51 static int vlan_dev_rebuild_header(struct sk_buff *skb)
53 struct net_device *dev = skb->dev;
54 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
56 switch (veth->h_vlan_encapsulated_proto) {
58 case __constant_htons(ETH_P_IP):
60 /* TODO: Confirm this will work with VLAN headers... */
61 return arp_find(veth->h_dest, skb);
64 pr_debug("%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;
93 static inline void vlan_set_encap_proto(struct sk_buff *skb,
94 struct vlan_hdr *vhdr)
100 * Was a VLAN packet, grab the encapsulated protocol, which the layer
101 * three protocols care about.
104 proto = vhdr->h_vlan_encapsulated_proto;
105 if (ntohs(proto) >= 1536) {
106 skb->protocol = proto;
111 if (*(unsigned short *)rawp == 0xFFFF)
113 * This is a magic hack to spot IPX packets. Older Novell
114 * breaks the protocol design and runs IPX over 802.3 without
115 * an 802.2 LLC layer. We look for FFFF which isn't a used
116 * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
117 * but does for the rest.
119 skb->protocol = htons(ETH_P_802_3);
124 skb->protocol = htons(ETH_P_802_2);
128 * Determine the packet's protocol ID. The rule here is that we
129 * assume 802.3 if the type field is short enough to be a length.
130 * This is normal practice and works for any 'now in use' protocol.
132 * Also, at this point we assume that we ARE dealing exclusively with
133 * VLAN packets, or packets that should be made into VLAN packets based
134 * on a default VLAN ID.
136 * NOTE: Should be similar to ethernet/eth.c.
138 * SANITY NOTE: This method is called when a packet is moving up the stack
139 * towards userland. To get here, it would have already passed
140 * through the ethernet/eth.c eth_type_trans() method.
141 * SANITY NOTE 2: We are referencing to the VLAN_HDR frields, which MAY be
142 * stored UNALIGNED in the memory. RISC systems don't like
143 * such cases very much...
144 * SANITY NOTE 2a: According to Dave Miller & Alexey, it will always be
145 * aligned, so there doesn't need to be any of the unaligned
146 * stuff. It has been commented out now... --Ben
149 int vlan_skb_recv(struct sk_buff *skb, struct net_device *dev,
150 struct packet_type *ptype, struct net_device *orig_dev)
152 struct vlan_hdr *vhdr;
153 struct net_device_stats *stats;
157 skb = skb_share_check(skb, GFP_ATOMIC);
161 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
164 vhdr = (struct vlan_hdr *)skb->data;
165 vlan_tci = ntohs(vhdr->h_vlan_TCI);
166 vlan_id = vlan_tci & VLAN_VID_MASK;
169 skb->dev = __find_vlan_dev(dev, vlan_id);
171 pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s\n",
172 __func__, vlan_id, dev->name);
176 skb->dev->last_rx = jiffies;
178 stats = &skb->dev->stats;
180 stats->rx_bytes += skb->len;
182 skb_pull_rcsum(skb, VLAN_HLEN);
184 skb->priority = vlan_get_ingress_priority(skb->dev, vlan_tci);
186 pr_debug("%s: priority: %u for TCI: %hu\n",
187 __func__, skb->priority, vlan_tci);
189 switch (skb->pkt_type) {
190 case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
191 /* stats->broadcast ++; // no such counter :-( */
194 case PACKET_MULTICAST:
198 case PACKET_OTHERHOST:
199 /* Our lower layer thinks this is not local, let's make sure.
200 * This allows the VLAN to have a different MAC than the
201 * underlying device, and still route correctly.
203 if (!compare_ether_addr(eth_hdr(skb)->h_dest,
205 skb->pkt_type = PACKET_HOST;
211 vlan_set_encap_proto(skb, vhdr);
213 skb = vlan_check_reorder_header(skb);
221 return NET_RX_SUCCESS;
231 vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
233 struct vlan_priority_tci_mapping *mp;
235 mp = vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)];
237 if (mp->priority == skb->priority) {
238 return mp->vlan_qos; /* This should already be shifted
239 * to mask correctly with the
248 * Create the VLAN header for an arbitrary protocol layer
250 * saddr=NULL means use device source address
251 * daddr=NULL means leave destination address (eg unresolved arp)
253 * This is called when the SKB is moving down the stack towards the
256 static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
258 const void *daddr, const void *saddr,
261 struct vlan_hdr *vhdr;
264 int build_vlan_header = 0;
265 struct net_device *vdev = dev;
267 pr_debug("%s: skb: %p type: %hx len: %u vlan_id: %hx, daddr: %p\n",
268 __func__, skb, type, len, vlan_dev_info(dev)->vlan_id,
271 /* build vlan header only if re_order_header flag is NOT set. This
272 * fixes some programs that get confused when they see a VLAN device
273 * sending a frame that is VLAN encoded (the consensus is that the VLAN
274 * device should look completely like an Ethernet device when the
275 * REORDER_HEADER flag is set) The drawback to this is some extra
276 * header shuffling in the hard_start_xmit. Users can turn off this
277 * REORDER behaviour with the vconfig tool.
279 if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR))
280 build_vlan_header = 1;
282 if (build_vlan_header) {
283 vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
285 /* build the four bytes that make this a VLAN header. */
287 /* Now, construct the second two bytes. This field looks
289 * usr_priority: 3 bits (high bits)
291 * VLAN ID 12 bits (low bits)
294 vlan_tci = vlan_dev_info(dev)->vlan_id;
295 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
297 vhdr->h_vlan_TCI = htons(vlan_tci);
300 * Set the protocol type. For a packet of type ETH_P_802_3 we
301 * put the length in here instead. It is up to the 802.2
302 * layer to carry protocol information.
305 if (type != ETH_P_802_3)
306 vhdr->h_vlan_encapsulated_proto = htons(type);
308 vhdr->h_vlan_encapsulated_proto = htons(len);
310 skb->protocol = htons(ETH_P_8021Q);
313 /* Before delegating work to the lower layer, enter our MAC-address */
315 saddr = dev->dev_addr;
317 dev = vlan_dev_info(dev)->real_dev;
319 /* MPLS can send us skbuffs w/out enough space. This check will grow
320 * the skb if it doesn't have enough headroom. Not a beautiful solution,
321 * so I'll tick a counter so that users can know it's happening...
325 /* NOTE: This may still break if the underlying device is not the final
326 * device (and thus there are more headers to add...) It should work for
327 * good-ole-ethernet though.
329 if (skb_headroom(skb) < dev->hard_header_len) {
330 struct sk_buff *sk_tmp = skb;
331 skb = skb_realloc_headroom(sk_tmp, dev->hard_header_len);
334 struct net_device_stats *stats = &vdev->stats;
338 vlan_dev_info(vdev)->cnt_inc_headroom_on_tx++;
339 pr_debug("%s: %s: had to grow skb\n", __func__, vdev->name);
342 if (build_vlan_header) {
343 /* Now make the underlying real hard header */
344 rc = dev_hard_header(skb, dev, ETH_P_8021Q, daddr, saddr,
351 /* If here, then we'll just make a normal looking ethernet
352 * frame, but, the hard_start_xmit method will insert the tag
353 * (it has to be able to do this for bridged and other skbs
354 * that don't come down the protocol stack in an orderly manner.
356 rc = dev_hard_header(skb, dev, type, daddr, saddr, len);
361 static int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
363 struct net_device_stats *stats = &dev->stats;
364 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
366 /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
368 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
369 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
372 if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
373 vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) {
374 int orig_headroom = skb_headroom(skb);
377 /* This is not a VLAN frame...but we can fix that! */
378 vlan_dev_info(dev)->cnt_encap_on_xmit++;
380 pr_debug("%s: proto to encap: 0x%hx\n",
381 __func__, ntohs(veth->h_vlan_proto));
382 /* Construct the second two bytes. This field looks something
384 * usr_priority: 3 bits (high bits)
386 * VLAN ID 12 bits (low bits)
388 vlan_tci = vlan_dev_info(dev)->vlan_id;
389 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
391 skb = __vlan_put_tag(skb, vlan_tci);
397 if (orig_headroom < VLAN_HLEN)
398 vlan_dev_info(dev)->cnt_inc_headroom_on_tx++;
401 pr_debug("%s: about to send skb: %p to dev: %s\n",
402 __func__, skb, skb->dev->name);
403 pr_debug(" " MAC_FMT " " MAC_FMT " %4hx %4hx %4hx\n",
404 veth->h_dest[0], veth->h_dest[1], veth->h_dest[2],
405 veth->h_dest[3], veth->h_dest[4], veth->h_dest[5],
406 veth->h_source[0], veth->h_source[1], veth->h_source[2],
407 veth->h_source[3], veth->h_source[4], veth->h_source[5],
408 veth->h_vlan_proto, veth->h_vlan_TCI,
409 veth->h_vlan_encapsulated_proto);
411 stats->tx_packets++; /* for statics only */
412 stats->tx_bytes += skb->len;
414 skb->dev = vlan_dev_info(dev)->real_dev;
420 static int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb,
421 struct net_device *dev)
423 struct net_device_stats *stats = &dev->stats;
426 /* Construct the second two bytes. This field looks something
428 * usr_priority: 3 bits (high bits)
430 * VLAN ID 12 bits (low bits)
432 vlan_tci = vlan_dev_info(dev)->vlan_id;
433 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
434 skb = __vlan_hwaccel_put_tag(skb, vlan_tci);
437 stats->tx_bytes += skb->len;
439 skb->dev = vlan_dev_info(dev)->real_dev;
445 static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
447 /* TODO: gotta make sure the underlying layer can handle it,
448 * maybe an IFF_VLAN_CAPABLE flag for devices?
450 if (vlan_dev_info(dev)->real_dev->mtu < new_mtu)
458 void vlan_dev_set_ingress_priority(const struct net_device *dev,
459 u32 skb_prio, u16 vlan_prio)
461 struct vlan_dev_info *vlan = vlan_dev_info(dev);
463 if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
464 vlan->nr_ingress_mappings--;
465 else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
466 vlan->nr_ingress_mappings++;
468 vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
471 int vlan_dev_set_egress_priority(const struct net_device *dev,
472 u32 skb_prio, u16 vlan_prio)
474 struct vlan_dev_info *vlan = vlan_dev_info(dev);
475 struct vlan_priority_tci_mapping *mp = NULL;
476 struct vlan_priority_tci_mapping *np;
477 u32 vlan_qos = (vlan_prio << 13) & 0xE000;
479 /* See if a priority mapping exists.. */
480 mp = vlan->egress_priority_map[skb_prio & 0xF];
482 if (mp->priority == skb_prio) {
483 if (mp->vlan_qos && !vlan_qos)
484 vlan->nr_egress_mappings--;
485 else if (!mp->vlan_qos && vlan_qos)
486 vlan->nr_egress_mappings++;
487 mp->vlan_qos = vlan_qos;
493 /* Create a new mapping then. */
494 mp = vlan->egress_priority_map[skb_prio & 0xF];
495 np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
500 np->priority = skb_prio;
501 np->vlan_qos = vlan_qos;
502 vlan->egress_priority_map[skb_prio & 0xF] = np;
504 vlan->nr_egress_mappings++;
508 /* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
509 int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask)
511 struct vlan_dev_info *vlan = vlan_dev_info(dev);
512 u32 old_flags = vlan->flags;
514 if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP))
517 vlan->flags = (old_flags & ~mask) | (flags & mask);
519 if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) {
520 if (vlan->flags & VLAN_FLAG_GVRP)
521 vlan_gvrp_request_join(dev);
523 vlan_gvrp_request_leave(dev);
528 void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
530 strncpy(result, vlan_dev_info(dev)->real_dev->name, 23);
533 static int vlan_dev_open(struct net_device *dev)
535 struct vlan_dev_info *vlan = vlan_dev_info(dev);
536 struct net_device *real_dev = vlan->real_dev;
539 if (!(real_dev->flags & IFF_UP))
542 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) {
543 err = dev_unicast_add(real_dev, dev->dev_addr, ETH_ALEN);
547 memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN);
549 if (dev->flags & IFF_ALLMULTI)
550 dev_set_allmulti(real_dev, 1);
551 if (dev->flags & IFF_PROMISC)
552 dev_set_promiscuity(real_dev, 1);
554 if (vlan->flags & VLAN_FLAG_GVRP)
555 vlan_gvrp_request_join(dev);
560 static int vlan_dev_stop(struct net_device *dev)
562 struct vlan_dev_info *vlan = vlan_dev_info(dev);
563 struct net_device *real_dev = vlan->real_dev;
565 if (vlan->flags & VLAN_FLAG_GVRP)
566 vlan_gvrp_request_leave(dev);
568 dev_mc_unsync(real_dev, dev);
569 dev_unicast_unsync(real_dev, dev);
570 if (dev->flags & IFF_ALLMULTI)
571 dev_set_allmulti(real_dev, -1);
572 if (dev->flags & IFF_PROMISC)
573 dev_set_promiscuity(real_dev, -1);
575 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
576 dev_unicast_delete(real_dev, dev->dev_addr, dev->addr_len);
581 static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
583 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
584 struct sockaddr *addr = p;
587 if (!is_valid_ether_addr(addr->sa_data))
588 return -EADDRNOTAVAIL;
590 if (!(dev->flags & IFF_UP))
593 if (compare_ether_addr(addr->sa_data, real_dev->dev_addr)) {
594 err = dev_unicast_add(real_dev, addr->sa_data, ETH_ALEN);
599 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
600 dev_unicast_delete(real_dev, dev->dev_addr, ETH_ALEN);
603 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
607 static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
609 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
611 int err = -EOPNOTSUPP;
613 strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
614 ifrr.ifr_ifru = ifr->ifr_ifru;
620 if (real_dev->do_ioctl && netif_device_present(real_dev))
621 err = real_dev->do_ioctl(real_dev, &ifrr, cmd);
626 ifr->ifr_ifru = ifrr.ifr_ifru;
631 static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
633 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
635 if (change & IFF_ALLMULTI)
636 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
637 if (change & IFF_PROMISC)
638 dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
641 static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
643 dev_mc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
644 dev_unicast_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
648 * vlan network devices have devices nesting below it, and are a special
649 * "super class" of normal network devices; split their locks off into a
650 * separate class since they always nest.
652 static struct lock_class_key vlan_netdev_xmit_lock_key;
654 static const struct header_ops vlan_header_ops = {
655 .create = vlan_dev_hard_header,
656 .rebuild = vlan_dev_rebuild_header,
657 .parse = eth_header_parse,
660 static int vlan_dev_init(struct net_device *dev)
662 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
665 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
666 dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI);
667 dev->iflink = real_dev->ifindex;
668 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
669 (1<<__LINK_STATE_DORMANT))) |
670 (1<<__LINK_STATE_PRESENT);
672 dev->features |= real_dev->features & real_dev->vlan_features;
674 /* ipv6 shared card related stuff */
675 dev->dev_id = real_dev->dev_id;
677 if (is_zero_ether_addr(dev->dev_addr))
678 memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
679 if (is_zero_ether_addr(dev->broadcast))
680 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
682 if (real_dev->features & NETIF_F_HW_VLAN_TX) {
683 dev->header_ops = real_dev->header_ops;
684 dev->hard_header_len = real_dev->hard_header_len;
685 dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
687 dev->header_ops = &vlan_header_ops;
688 dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
689 dev->hard_start_xmit = vlan_dev_hard_start_xmit;
692 if (is_vlan_dev(real_dev))
695 lockdep_set_class_and_subclass(&dev->_xmit_lock,
696 &vlan_netdev_xmit_lock_key, subclass);
700 static void vlan_dev_uninit(struct net_device *dev)
702 struct vlan_priority_tci_mapping *pm;
703 struct vlan_dev_info *vlan = vlan_dev_info(dev);
706 for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
707 while ((pm = vlan->egress_priority_map[i]) != NULL) {
708 vlan->egress_priority_map[i] = pm->next;
714 static u32 vlan_ethtool_get_rx_csum(struct net_device *dev)
716 const struct vlan_dev_info *vlan = vlan_dev_info(dev);
717 struct net_device *real_dev = vlan->real_dev;
719 if (real_dev->ethtool_ops == NULL ||
720 real_dev->ethtool_ops->get_rx_csum == NULL)
722 return real_dev->ethtool_ops->get_rx_csum(real_dev);
725 static const struct ethtool_ops vlan_ethtool_ops = {
726 .get_link = ethtool_op_get_link,
727 .get_rx_csum = vlan_ethtool_get_rx_csum,
730 void vlan_setup(struct net_device *dev)
734 dev->priv_flags |= IFF_802_1Q_VLAN;
735 dev->tx_queue_len = 0;
737 dev->change_mtu = vlan_dev_change_mtu;
738 dev->init = vlan_dev_init;
739 dev->uninit = vlan_dev_uninit;
740 dev->open = vlan_dev_open;
741 dev->stop = vlan_dev_stop;
742 dev->set_mac_address = vlan_dev_set_mac_address;
743 dev->set_rx_mode = vlan_dev_set_rx_mode;
744 dev->set_multicast_list = vlan_dev_set_rx_mode;
745 dev->change_rx_flags = vlan_dev_change_rx_flags;
746 dev->do_ioctl = vlan_dev_ioctl;
747 dev->destructor = free_netdev;
748 dev->ethtool_ops = &vlan_ethtool_ops;
750 memset(dev->broadcast, 0, ETH_ALEN);