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 <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 static 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);
63 pr_debug("%s: unable to resolve type %X addresses.\n",
64 dev->name, ntohs(veth->h_vlan_encapsulated_proto));
66 memcpy(veth->h_source, dev->dev_addr, ETH_ALEN);
73 static inline struct sk_buff *vlan_check_reorder_header(struct sk_buff *skb)
75 if (vlan_dev_info(skb->dev)->flags & VLAN_FLAG_REORDER_HDR) {
76 if (skb_shared(skb) || skb_cloned(skb)) {
77 struct sk_buff *nskb = skb_copy(skb, GFP_ATOMIC);
82 /* Lifted from Gleb's VLAN code... */
83 memmove(skb->data - ETH_HLEN,
84 skb->data - VLAN_ETH_HLEN, 12);
85 skb->mac_header += VLAN_HLEN;
93 * Determine the packet's protocol ID. The rule here is that we
94 * assume 802.3 if the type field is short enough to be a length.
95 * This is normal practice and works for any 'now in use' protocol.
97 * Also, at this point we assume that we ARE dealing exclusively with
98 * VLAN packets, or packets that should be made into VLAN packets based
99 * on a default VLAN ID.
101 * NOTE: Should be similar to ethernet/eth.c.
103 * SANITY NOTE: This method is called when a packet is moving up the stack
104 * towards userland. To get here, it would have already passed
105 * through the ethernet/eth.c eth_type_trans() method.
106 * SANITY NOTE 2: We are referencing to the VLAN_HDR frields, which MAY be
107 * stored UNALIGNED in the memory. RISC systems don't like
108 * such cases very much...
109 * SANITY NOTE 2a: According to Dave Miller & Alexey, it will always be
110 * aligned, so there doesn't need to be any of the unaligned
111 * stuff. It has been commented out now... --Ben
114 int vlan_skb_recv(struct sk_buff *skb, struct net_device *dev,
115 struct packet_type *ptype, struct net_device *orig_dev)
118 struct vlan_hdr *vhdr;
120 struct net_device_stats *stats;
121 unsigned short vlan_TCI;
124 if (dev->nd_net != &init_net)
127 skb = skb_share_check(skb, GFP_ATOMIC);
131 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
134 vhdr = (struct vlan_hdr *)skb->data;
135 vlan_TCI = ntohs(vhdr->h_vlan_TCI);
136 vid = (vlan_TCI & VLAN_VID_MASK);
139 skb->dev = __find_vlan_dev(dev, vid);
141 pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s\n",
142 __FUNCTION__, (unsigned int)vid, dev->name);
146 skb->dev->last_rx = jiffies;
148 stats = &skb->dev->stats;
150 stats->rx_bytes += skb->len;
152 skb_pull_rcsum(skb, VLAN_HLEN);
154 skb->priority = vlan_get_ingress_priority(skb->dev,
155 ntohs(vhdr->h_vlan_TCI));
157 pr_debug("%s: priority: %u for TCI: %hu\n",
158 __FUNCTION__, skb->priority, ntohs(vhdr->h_vlan_TCI));
160 switch (skb->pkt_type) {
161 case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
162 /* stats->broadcast ++; // no such counter :-( */
165 case PACKET_MULTICAST:
169 case PACKET_OTHERHOST:
170 /* Our lower layer thinks this is not local, let's make sure.
171 * This allows the VLAN to have a different MAC than the
172 * underlying device, and still route correctly.
174 if (!compare_ether_addr(eth_hdr(skb)->h_dest,
176 skb->pkt_type = PACKET_HOST;
182 /* Was a VLAN packet, grab the encapsulated protocol, which the layer
183 * three protocols care about.
185 proto = vhdr->h_vlan_encapsulated_proto;
186 if (ntohs(proto) >= 1536) {
187 skb->protocol = proto;
192 * This is a magic hack to spot IPX packets. Older Novell breaks
193 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
194 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
195 * won't work for fault tolerant netware but does for the rest.
198 if (*(unsigned short *)rawp == 0xFFFF) {
199 skb->protocol = htons(ETH_P_802_3);
206 skb->protocol = htons(ETH_P_802_2);
209 skb = vlan_check_reorder_header(skb);
217 return NET_RX_SUCCESS;
226 static inline unsigned short
227 vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
229 struct vlan_priority_tci_mapping *mp;
231 mp = vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)];
233 if (mp->priority == skb->priority) {
234 return mp->vlan_qos; /* This should already be shifted
235 * to mask correctly with the
244 * Create the VLAN header for an arbitrary protocol layer
246 * saddr=NULL means use device source address
247 * daddr=NULL means leave destination address (eg unresolved arp)
249 * This is called when the SKB is moving down the stack towards the
252 static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
254 const void *daddr, const void *saddr,
257 struct vlan_hdr *vhdr;
258 unsigned short veth_TCI = 0;
260 int build_vlan_header = 0;
261 struct net_device *vdev = dev;
263 pr_debug("%s: skb: %p type: %hx len: %u vlan_id: %hx, daddr: %p\n",
264 __FUNCTION__, skb, type, len, vlan_dev_info(dev)->vlan_id,
267 /* build vlan header only if re_order_header flag is NOT set. This
268 * fixes some programs that get confused when they see a VLAN device
269 * sending a frame that is VLAN encoded (the consensus is that the VLAN
270 * device should look completely like an Ethernet device when the
271 * REORDER_HEADER flag is set) The drawback to this is some extra
272 * header shuffling in the hard_start_xmit. Users can turn off this
273 * REORDER behaviour with the vconfig tool.
275 if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR))
276 build_vlan_header = 1;
278 if (build_vlan_header) {
279 vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
281 /* build the four bytes that make this a VLAN header. */
283 /* Now, construct the second two bytes. This field looks
285 * usr_priority: 3 bits (high bits)
287 * VLAN ID 12 bits (low bits)
290 veth_TCI = vlan_dev_info(dev)->vlan_id;
291 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
293 vhdr->h_vlan_TCI = htons(veth_TCI);
296 * Set the protocol type. For a packet of type ETH_P_802_3 we
297 * put the length in here instead. It is up to the 802.2
298 * layer to carry protocol information.
301 if (type != ETH_P_802_3)
302 vhdr->h_vlan_encapsulated_proto = htons(type);
304 vhdr->h_vlan_encapsulated_proto = htons(len);
306 skb->protocol = htons(ETH_P_8021Q);
307 skb_reset_network_header(skb);
310 /* Before delegating work to the lower layer, enter our MAC-address */
312 saddr = dev->dev_addr;
314 dev = vlan_dev_info(dev)->real_dev;
316 /* MPLS can send us skbuffs w/out enough space. This check will grow
317 * the skb if it doesn't have enough headroom. Not a beautiful solution,
318 * so I'll tick a counter so that users can know it's happening...
322 /* NOTE: This may still break if the underlying device is not the final
323 * device (and thus there are more headers to add...) It should work for
324 * good-ole-ethernet though.
326 if (skb_headroom(skb) < dev->hard_header_len) {
327 struct sk_buff *sk_tmp = skb;
328 skb = skb_realloc_headroom(sk_tmp, dev->hard_header_len);
331 struct net_device_stats *stats = &vdev->stats;
335 vlan_dev_info(vdev)->cnt_inc_headroom_on_tx++;
336 pr_debug("%s: %s: had to grow skb\n", __FUNCTION__, vdev->name);
339 if (build_vlan_header) {
340 /* Now make the underlying real hard header */
341 rc = dev_hard_header(skb, dev, ETH_P_8021Q, daddr, saddr,
348 /* If here, then we'll just make a normal looking ethernet
349 * frame, but, the hard_start_xmit method will insert the tag
350 * (it has to be able to do this for bridged and other skbs
351 * that don't come down the protocol stack in an orderly manner.
353 rc = dev_hard_header(skb, dev, type, daddr, saddr, len);
358 static int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
360 struct net_device_stats *stats = &dev->stats;
361 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
363 /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
365 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
366 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
369 if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
370 vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) {
371 int orig_headroom = skb_headroom(skb);
372 unsigned short veth_TCI;
374 /* This is not a VLAN frame...but we can fix that! */
375 vlan_dev_info(dev)->cnt_encap_on_xmit++;
377 pr_debug("%s: proto to encap: 0x%hx\n",
378 __FUNCTION__, htons(veth->h_vlan_proto));
379 /* Construct the second two bytes. This field looks something
381 * usr_priority: 3 bits (high bits)
383 * VLAN ID 12 bits (low bits)
385 veth_TCI = vlan_dev_info(dev)->vlan_id;
386 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
388 skb = __vlan_put_tag(skb, veth_TCI);
394 if (orig_headroom < VLAN_HLEN)
395 vlan_dev_info(dev)->cnt_inc_headroom_on_tx++;
398 pr_debug("%s: about to send skb: %p to dev: %s\n",
399 __FUNCTION__, skb, skb->dev->name);
400 pr_debug(" " MAC_FMT " " MAC_FMT " %4hx %4hx %4hx\n",
401 veth->h_dest[0], veth->h_dest[1], veth->h_dest[2],
402 veth->h_dest[3], veth->h_dest[4], veth->h_dest[5],
403 veth->h_source[0], veth->h_source[1], veth->h_source[2],
404 veth->h_source[3], veth->h_source[4], veth->h_source[5],
405 veth->h_vlan_proto, veth->h_vlan_TCI,
406 veth->h_vlan_encapsulated_proto);
408 stats->tx_packets++; /* for statics only */
409 stats->tx_bytes += skb->len;
411 skb->dev = vlan_dev_info(dev)->real_dev;
417 static int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb,
418 struct net_device *dev)
420 struct net_device_stats *stats = &dev->stats;
421 unsigned short veth_TCI;
423 /* Construct the second two bytes. This field looks something
425 * usr_priority: 3 bits (high bits)
427 * VLAN ID 12 bits (low bits)
429 veth_TCI = vlan_dev_info(dev)->vlan_id;
430 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
431 skb = __vlan_hwaccel_put_tag(skb, veth_TCI);
434 stats->tx_bytes += skb->len;
436 skb->dev = vlan_dev_info(dev)->real_dev;
442 static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
444 /* TODO: gotta make sure the underlying layer can handle it,
445 * maybe an IFF_VLAN_CAPABLE flag for devices?
447 if (vlan_dev_info(dev)->real_dev->mtu < new_mtu)
455 void vlan_dev_set_ingress_priority(const struct net_device *dev,
456 u32 skb_prio, short vlan_prio)
458 struct vlan_dev_info *vlan = vlan_dev_info(dev);
460 if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
461 vlan->nr_ingress_mappings--;
462 else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
463 vlan->nr_ingress_mappings++;
465 vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
468 int vlan_dev_set_egress_priority(const struct net_device *dev,
469 u32 skb_prio, short vlan_prio)
471 struct vlan_dev_info *vlan = vlan_dev_info(dev);
472 struct vlan_priority_tci_mapping *mp = NULL;
473 struct vlan_priority_tci_mapping *np;
474 u32 vlan_qos = (vlan_prio << 13) & 0xE000;
476 /* See if a priority mapping exists.. */
477 mp = vlan->egress_priority_map[skb_prio & 0xF];
479 if (mp->priority == skb_prio) {
480 if (mp->vlan_qos && !vlan_qos)
481 vlan->nr_egress_mappings--;
482 else if (!mp->vlan_qos && vlan_qos)
483 vlan->nr_egress_mappings++;
484 mp->vlan_qos = vlan_qos;
490 /* Create a new mapping then. */
491 mp = vlan->egress_priority_map[skb_prio & 0xF];
492 np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
497 np->priority = skb_prio;
498 np->vlan_qos = vlan_qos;
499 vlan->egress_priority_map[skb_prio & 0xF] = np;
501 vlan->nr_egress_mappings++;
505 /* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
506 int vlan_dev_set_vlan_flag(const struct net_device *dev,
507 u32 flag, short flag_val)
509 /* verify flag is supported */
510 if (flag == VLAN_FLAG_REORDER_HDR) {
512 vlan_dev_info(dev)->flags |= VLAN_FLAG_REORDER_HDR;
514 vlan_dev_info(dev)->flags &= ~VLAN_FLAG_REORDER_HDR;
520 void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
522 strncpy(result, vlan_dev_info(dev)->real_dev->name, 23);
525 void vlan_dev_get_vid(const struct net_device *dev, unsigned short *result)
527 *result = vlan_dev_info(dev)->vlan_id;
530 static int vlan_dev_open(struct net_device *dev)
532 struct vlan_dev_info *vlan = vlan_dev_info(dev);
533 struct net_device *real_dev = vlan->real_dev;
536 if (!(real_dev->flags & IFF_UP))
539 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) {
540 err = dev_unicast_add(real_dev, dev->dev_addr, ETH_ALEN);
544 memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN);
546 if (dev->flags & IFF_ALLMULTI)
547 dev_set_allmulti(real_dev, 1);
548 if (dev->flags & IFF_PROMISC)
549 dev_set_promiscuity(real_dev, 1);
554 static int vlan_dev_stop(struct net_device *dev)
556 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
558 dev_mc_unsync(real_dev, dev);
559 if (dev->flags & IFF_ALLMULTI)
560 dev_set_allmulti(real_dev, -1);
561 if (dev->flags & IFF_PROMISC)
562 dev_set_promiscuity(real_dev, -1);
564 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
565 dev_unicast_delete(real_dev, dev->dev_addr, dev->addr_len);
570 static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
572 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
573 struct sockaddr *addr = p;
576 if (!is_valid_ether_addr(addr->sa_data))
577 return -EADDRNOTAVAIL;
579 if (!(dev->flags & IFF_UP))
582 if (compare_ether_addr(addr->sa_data, real_dev->dev_addr)) {
583 err = dev_unicast_add(real_dev, addr->sa_data, ETH_ALEN);
588 if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
589 dev_unicast_delete(real_dev, dev->dev_addr, ETH_ALEN);
592 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
596 static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
598 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
600 int err = -EOPNOTSUPP;
602 strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
603 ifrr.ifr_ifru = ifr->ifr_ifru;
609 if (real_dev->do_ioctl && netif_device_present(real_dev))
610 err = real_dev->do_ioctl(real_dev, &ifrr, cmd);
615 ifr->ifr_ifru = ifrr.ifr_ifru;
620 static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
622 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
624 if (change & IFF_ALLMULTI)
625 dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
626 if (change & IFF_PROMISC)
627 dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
630 static void vlan_dev_set_multicast_list(struct net_device *vlan_dev)
632 dev_mc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
636 * vlan network devices have devices nesting below it, and are a special
637 * "super class" of normal network devices; split their locks off into a
638 * separate class since they always nest.
640 static struct lock_class_key vlan_netdev_xmit_lock_key;
642 static const struct header_ops vlan_header_ops = {
643 .create = vlan_dev_hard_header,
644 .rebuild = vlan_dev_rebuild_header,
645 .parse = eth_header_parse,
648 static int vlan_dev_init(struct net_device *dev)
650 struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
653 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
654 dev->flags = real_dev->flags & ~IFF_UP;
655 dev->iflink = real_dev->ifindex;
656 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
657 (1<<__LINK_STATE_DORMANT))) |
658 (1<<__LINK_STATE_PRESENT);
660 /* ipv6 shared card related stuff */
661 dev->dev_id = real_dev->dev_id;
663 if (is_zero_ether_addr(dev->dev_addr))
664 memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
665 if (is_zero_ether_addr(dev->broadcast))
666 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
668 if (real_dev->features & NETIF_F_HW_VLAN_TX) {
669 dev->header_ops = real_dev->header_ops;
670 dev->hard_header_len = real_dev->hard_header_len;
671 dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
673 dev->header_ops = &vlan_header_ops;
674 dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
675 dev->hard_start_xmit = vlan_dev_hard_start_xmit;
678 if (real_dev->priv_flags & IFF_802_1Q_VLAN)
681 lockdep_set_class_and_subclass(&dev->_xmit_lock,
682 &vlan_netdev_xmit_lock_key, subclass);
686 void vlan_setup(struct net_device *dev)
690 dev->priv_flags |= IFF_802_1Q_VLAN;
691 dev->tx_queue_len = 0;
693 dev->change_mtu = vlan_dev_change_mtu;
694 dev->init = vlan_dev_init;
695 dev->open = vlan_dev_open;
696 dev->stop = vlan_dev_stop;
697 dev->set_mac_address = vlan_dev_set_mac_address;
698 dev->set_multicast_list = vlan_dev_set_multicast_list;
699 dev->change_rx_flags = vlan_dev_change_rx_flags;
700 dev->do_ioctl = vlan_dev_ioctl;
701 dev->destructor = free_netdev;
703 memset(dev->broadcast, 0, ETH_ALEN);