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_cow(skb
, skb_headroom(skb
)) < 0)
80 /* Lifted from Gleb's VLAN code... */
81 memmove(skb
->data
- ETH_HLEN
,
82 skb
->data
- VLAN_ETH_HLEN
, 12);
83 skb
->mac_header
+= VLAN_HLEN
;
90 static inline void vlan_set_encap_proto(struct sk_buff
*skb
,
91 struct vlan_hdr
*vhdr
)
97 * Was a VLAN packet, grab the encapsulated protocol, which the layer
98 * three protocols care about.
101 proto
= vhdr
->h_vlan_encapsulated_proto
;
102 if (ntohs(proto
) >= 1536) {
103 skb
->protocol
= proto
;
108 if (*(unsigned short *)rawp
== 0xFFFF)
110 * This is a magic hack to spot IPX packets. Older Novell
111 * breaks the protocol design and runs IPX over 802.3 without
112 * an 802.2 LLC layer. We look for FFFF which isn't a used
113 * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
114 * but does for the rest.
116 skb
->protocol
= htons(ETH_P_802_3
);
121 skb
->protocol
= htons(ETH_P_802_2
);
125 * Determine the packet's protocol ID. The rule here is that we
126 * assume 802.3 if the type field is short enough to be a length.
127 * This is normal practice and works for any 'now in use' protocol.
129 * Also, at this point we assume that we ARE dealing exclusively with
130 * VLAN packets, or packets that should be made into VLAN packets based
131 * on a default VLAN ID.
133 * NOTE: Should be similar to ethernet/eth.c.
135 * SANITY NOTE: This method is called when a packet is moving up the stack
136 * towards userland. To get here, it would have already passed
137 * through the ethernet/eth.c eth_type_trans() method.
138 * SANITY NOTE 2: We are referencing to the VLAN_HDR frields, which MAY be
139 * stored UNALIGNED in the memory. RISC systems don't like
140 * such cases very much...
141 * SANITY NOTE 2a: According to Dave Miller & Alexey, it will always be
142 * aligned, so there doesn't need to be any of the unaligned
143 * stuff. It has been commented out now... --Ben
146 int vlan_skb_recv(struct sk_buff
*skb
, struct net_device
*dev
,
147 struct packet_type
*ptype
, struct net_device
*orig_dev
)
149 struct vlan_hdr
*vhdr
;
150 struct net_device_stats
*stats
;
154 skb
= skb_share_check(skb
, GFP_ATOMIC
);
158 if (unlikely(!pskb_may_pull(skb
, VLAN_HLEN
)))
161 vhdr
= (struct vlan_hdr
*)skb
->data
;
162 vlan_tci
= ntohs(vhdr
->h_vlan_TCI
);
163 vlan_id
= vlan_tci
& VLAN_VID_MASK
;
166 skb
->dev
= __find_vlan_dev(dev
, vlan_id
);
168 pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s\n",
169 __func__
, vlan_id
, dev
->name
);
173 skb
->dev
->last_rx
= jiffies
;
175 stats
= &skb
->dev
->stats
;
177 stats
->rx_bytes
+= skb
->len
;
179 skb_pull_rcsum(skb
, VLAN_HLEN
);
181 skb
->priority
= vlan_get_ingress_priority(skb
->dev
, vlan_tci
);
183 pr_debug("%s: priority: %u for TCI: %hu\n",
184 __func__
, skb
->priority
, vlan_tci
);
186 switch (skb
->pkt_type
) {
187 case PACKET_BROADCAST
: /* Yeah, stats collect these together.. */
188 /* stats->broadcast ++; // no such counter :-( */
191 case PACKET_MULTICAST
:
195 case PACKET_OTHERHOST
:
196 /* Our lower layer thinks this is not local, let's make sure.
197 * This allows the VLAN to have a different MAC than the
198 * underlying device, and still route correctly.
200 if (!compare_ether_addr(eth_hdr(skb
)->h_dest
,
202 skb
->pkt_type
= PACKET_HOST
;
208 vlan_set_encap_proto(skb
, vhdr
);
210 skb
= vlan_check_reorder_header(skb
);
218 return NET_RX_SUCCESS
;
228 vlan_dev_get_egress_qos_mask(struct net_device
*dev
, struct sk_buff
*skb
)
230 struct vlan_priority_tci_mapping
*mp
;
232 mp
= vlan_dev_info(dev
)->egress_priority_map
[(skb
->priority
& 0xF)];
234 if (mp
->priority
== skb
->priority
) {
235 return mp
->vlan_qos
; /* This should already be shifted
236 * to mask correctly with the
245 * Create the VLAN header for an arbitrary protocol layer
247 * saddr=NULL means use device source address
248 * daddr=NULL means leave destination address (eg unresolved arp)
250 * This is called when the SKB is moving down the stack towards the
253 static int vlan_dev_hard_header(struct sk_buff
*skb
, struct net_device
*dev
,
255 const void *daddr
, const void *saddr
,
258 struct vlan_hdr
*vhdr
;
259 unsigned int vhdrlen
= 0;
263 if (WARN_ON(skb_headroom(skb
) < dev
->hard_header_len
))
266 if (!(vlan_dev_info(dev
)->flags
& VLAN_FLAG_REORDER_HDR
)) {
267 vhdr
= (struct vlan_hdr
*) skb_push(skb
, VLAN_HLEN
);
269 vlan_tci
= vlan_dev_info(dev
)->vlan_id
;
270 vlan_tci
|= vlan_dev_get_egress_qos_mask(dev
, skb
);
271 vhdr
->h_vlan_TCI
= htons(vlan_tci
);
274 * Set the protocol type. For a packet of type ETH_P_802_3 we
275 * put the length in here instead. It is up to the 802.2
276 * layer to carry protocol information.
278 if (type
!= ETH_P_802_3
)
279 vhdr
->h_vlan_encapsulated_proto
= htons(type
);
281 vhdr
->h_vlan_encapsulated_proto
= htons(len
);
283 skb
->protocol
= htons(ETH_P_8021Q
);
288 /* Before delegating work to the lower layer, enter our MAC-address */
290 saddr
= dev
->dev_addr
;
292 /* Now make the underlying real hard header */
293 dev
= vlan_dev_info(dev
)->real_dev
;
294 rc
= dev_hard_header(skb
, dev
, type
, daddr
, saddr
, len
+ vhdrlen
);
300 static int vlan_dev_hard_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
302 struct net_device_stats
*stats
= &dev
->stats
;
303 struct vlan_ethhdr
*veth
= (struct vlan_ethhdr
*)(skb
->data
);
305 /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
307 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
308 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
310 if (veth
->h_vlan_proto
!= htons(ETH_P_8021Q
) ||
311 vlan_dev_info(dev
)->flags
& VLAN_FLAG_REORDER_HDR
) {
312 unsigned int orig_headroom
= skb_headroom(skb
);
315 vlan_dev_info(dev
)->cnt_encap_on_xmit
++;
317 vlan_tci
= vlan_dev_info(dev
)->vlan_id
;
318 vlan_tci
|= vlan_dev_get_egress_qos_mask(dev
, skb
);
319 skb
= __vlan_put_tag(skb
, vlan_tci
);
325 if (orig_headroom
< VLAN_HLEN
)
326 vlan_dev_info(dev
)->cnt_inc_headroom_on_tx
++;
330 stats
->tx_bytes
+= skb
->len
;
332 skb
->dev
= vlan_dev_info(dev
)->real_dev
;
337 static int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff
*skb
,
338 struct net_device
*dev
)
340 struct net_device_stats
*stats
= &dev
->stats
;
343 vlan_tci
= vlan_dev_info(dev
)->vlan_id
;
344 vlan_tci
|= vlan_dev_get_egress_qos_mask(dev
, skb
);
345 skb
= __vlan_hwaccel_put_tag(skb
, vlan_tci
);
348 stats
->tx_bytes
+= skb
->len
;
350 skb
->dev
= vlan_dev_info(dev
)->real_dev
;
355 static int vlan_dev_change_mtu(struct net_device
*dev
, int new_mtu
)
357 /* TODO: gotta make sure the underlying layer can handle it,
358 * maybe an IFF_VLAN_CAPABLE flag for devices?
360 if (vlan_dev_info(dev
)->real_dev
->mtu
< new_mtu
)
368 void vlan_dev_set_ingress_priority(const struct net_device
*dev
,
369 u32 skb_prio
, u16 vlan_prio
)
371 struct vlan_dev_info
*vlan
= vlan_dev_info(dev
);
373 if (vlan
->ingress_priority_map
[vlan_prio
& 0x7] && !skb_prio
)
374 vlan
->nr_ingress_mappings
--;
375 else if (!vlan
->ingress_priority_map
[vlan_prio
& 0x7] && skb_prio
)
376 vlan
->nr_ingress_mappings
++;
378 vlan
->ingress_priority_map
[vlan_prio
& 0x7] = skb_prio
;
381 int vlan_dev_set_egress_priority(const struct net_device
*dev
,
382 u32 skb_prio
, u16 vlan_prio
)
384 struct vlan_dev_info
*vlan
= vlan_dev_info(dev
);
385 struct vlan_priority_tci_mapping
*mp
= NULL
;
386 struct vlan_priority_tci_mapping
*np
;
387 u32 vlan_qos
= (vlan_prio
<< 13) & 0xE000;
389 /* See if a priority mapping exists.. */
390 mp
= vlan
->egress_priority_map
[skb_prio
& 0xF];
392 if (mp
->priority
== skb_prio
) {
393 if (mp
->vlan_qos
&& !vlan_qos
)
394 vlan
->nr_egress_mappings
--;
395 else if (!mp
->vlan_qos
&& vlan_qos
)
396 vlan
->nr_egress_mappings
++;
397 mp
->vlan_qos
= vlan_qos
;
403 /* Create a new mapping then. */
404 mp
= vlan
->egress_priority_map
[skb_prio
& 0xF];
405 np
= kmalloc(sizeof(struct vlan_priority_tci_mapping
), GFP_KERNEL
);
410 np
->priority
= skb_prio
;
411 np
->vlan_qos
= vlan_qos
;
412 vlan
->egress_priority_map
[skb_prio
& 0xF] = np
;
414 vlan
->nr_egress_mappings
++;
418 /* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
419 int vlan_dev_change_flags(const struct net_device
*dev
, u32 flags
, u32 mask
)
421 struct vlan_dev_info
*vlan
= vlan_dev_info(dev
);
422 u32 old_flags
= vlan
->flags
;
424 if (mask
& ~(VLAN_FLAG_REORDER_HDR
| VLAN_FLAG_GVRP
))
427 vlan
->flags
= (old_flags
& ~mask
) | (flags
& mask
);
429 if (netif_running(dev
) && (vlan
->flags
^ old_flags
) & VLAN_FLAG_GVRP
) {
430 if (vlan
->flags
& VLAN_FLAG_GVRP
)
431 vlan_gvrp_request_join(dev
);
433 vlan_gvrp_request_leave(dev
);
438 void vlan_dev_get_realdev_name(const struct net_device
*dev
, char *result
)
440 strncpy(result
, vlan_dev_info(dev
)->real_dev
->name
, 23);
443 static int vlan_dev_open(struct net_device
*dev
)
445 struct vlan_dev_info
*vlan
= vlan_dev_info(dev
);
446 struct net_device
*real_dev
= vlan
->real_dev
;
449 if (!(real_dev
->flags
& IFF_UP
))
452 if (compare_ether_addr(dev
->dev_addr
, real_dev
->dev_addr
)) {
453 err
= dev_unicast_add(real_dev
, dev
->dev_addr
, ETH_ALEN
);
458 if (dev
->flags
& IFF_ALLMULTI
) {
459 err
= dev_set_allmulti(real_dev
, 1);
463 if (dev
->flags
& IFF_PROMISC
) {
464 err
= dev_set_promiscuity(real_dev
, 1);
469 memcpy(vlan
->real_dev_addr
, real_dev
->dev_addr
, ETH_ALEN
);
471 if (vlan
->flags
& VLAN_FLAG_GVRP
)
472 vlan_gvrp_request_join(dev
);
477 if (dev
->flags
& IFF_ALLMULTI
)
478 dev_set_allmulti(real_dev
, -1);
480 if (compare_ether_addr(dev
->dev_addr
, real_dev
->dev_addr
))
481 dev_unicast_delete(real_dev
, dev
->dev_addr
, ETH_ALEN
);
486 static int vlan_dev_stop(struct net_device
*dev
)
488 struct vlan_dev_info
*vlan
= vlan_dev_info(dev
);
489 struct net_device
*real_dev
= vlan
->real_dev
;
491 if (vlan
->flags
& VLAN_FLAG_GVRP
)
492 vlan_gvrp_request_leave(dev
);
494 dev_mc_unsync(real_dev
, dev
);
495 dev_unicast_unsync(real_dev
, dev
);
496 if (dev
->flags
& IFF_ALLMULTI
)
497 dev_set_allmulti(real_dev
, -1);
498 if (dev
->flags
& IFF_PROMISC
)
499 dev_set_promiscuity(real_dev
, -1);
501 if (compare_ether_addr(dev
->dev_addr
, real_dev
->dev_addr
))
502 dev_unicast_delete(real_dev
, dev
->dev_addr
, dev
->addr_len
);
507 static int vlan_dev_set_mac_address(struct net_device
*dev
, void *p
)
509 struct net_device
*real_dev
= vlan_dev_info(dev
)->real_dev
;
510 struct sockaddr
*addr
= p
;
513 if (!is_valid_ether_addr(addr
->sa_data
))
514 return -EADDRNOTAVAIL
;
516 if (!(dev
->flags
& IFF_UP
))
519 if (compare_ether_addr(addr
->sa_data
, real_dev
->dev_addr
)) {
520 err
= dev_unicast_add(real_dev
, addr
->sa_data
, ETH_ALEN
);
525 if (compare_ether_addr(dev
->dev_addr
, real_dev
->dev_addr
))
526 dev_unicast_delete(real_dev
, dev
->dev_addr
, ETH_ALEN
);
529 memcpy(dev
->dev_addr
, addr
->sa_data
, ETH_ALEN
);
533 static int vlan_dev_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
535 struct net_device
*real_dev
= vlan_dev_info(dev
)->real_dev
;
537 int err
= -EOPNOTSUPP
;
539 strncpy(ifrr
.ifr_name
, real_dev
->name
, IFNAMSIZ
);
540 ifrr
.ifr_ifru
= ifr
->ifr_ifru
;
546 if (real_dev
->do_ioctl
&& netif_device_present(real_dev
))
547 err
= real_dev
->do_ioctl(real_dev
, &ifrr
, cmd
);
552 ifr
->ifr_ifru
= ifrr
.ifr_ifru
;
557 static void vlan_dev_change_rx_flags(struct net_device
*dev
, int change
)
559 struct net_device
*real_dev
= vlan_dev_info(dev
)->real_dev
;
561 if (change
& IFF_ALLMULTI
)
562 dev_set_allmulti(real_dev
, dev
->flags
& IFF_ALLMULTI
? 1 : -1);
563 if (change
& IFF_PROMISC
)
564 dev_set_promiscuity(real_dev
, dev
->flags
& IFF_PROMISC
? 1 : -1);
567 static void vlan_dev_set_rx_mode(struct net_device
*vlan_dev
)
569 dev_mc_sync(vlan_dev_info(vlan_dev
)->real_dev
, vlan_dev
);
570 dev_unicast_sync(vlan_dev_info(vlan_dev
)->real_dev
, vlan_dev
);
574 * vlan network devices have devices nesting below it, and are a special
575 * "super class" of normal network devices; split their locks off into a
576 * separate class since they always nest.
578 static struct lock_class_key vlan_netdev_xmit_lock_key
;
580 static void vlan_dev_set_lockdep_one(struct netdev_queue
*txq
,
583 lockdep_set_class_and_subclass(&txq
->_xmit_lock
,
584 &vlan_netdev_xmit_lock_key
, subclass
);
587 static void vlan_dev_set_lockdep_class(struct net_device
*dev
, int subclass
)
589 vlan_dev_set_lockdep_one(&dev
->tx_queue
, subclass
);
592 static const struct header_ops vlan_header_ops
= {
593 .create
= vlan_dev_hard_header
,
594 .rebuild
= vlan_dev_rebuild_header
,
595 .parse
= eth_header_parse
,
598 static int vlan_dev_init(struct net_device
*dev
)
600 struct net_device
*real_dev
= vlan_dev_info(dev
)->real_dev
;
603 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
604 dev
->flags
= real_dev
->flags
& ~(IFF_UP
| IFF_PROMISC
| IFF_ALLMULTI
);
605 dev
->iflink
= real_dev
->ifindex
;
606 dev
->state
= (real_dev
->state
& ((1<<__LINK_STATE_NOCARRIER
) |
607 (1<<__LINK_STATE_DORMANT
))) |
608 (1<<__LINK_STATE_PRESENT
);
610 dev
->features
|= real_dev
->features
& real_dev
->vlan_features
;
612 /* ipv6 shared card related stuff */
613 dev
->dev_id
= real_dev
->dev_id
;
615 if (is_zero_ether_addr(dev
->dev_addr
))
616 memcpy(dev
->dev_addr
, real_dev
->dev_addr
, dev
->addr_len
);
617 if (is_zero_ether_addr(dev
->broadcast
))
618 memcpy(dev
->broadcast
, real_dev
->broadcast
, dev
->addr_len
);
620 if (real_dev
->features
& NETIF_F_HW_VLAN_TX
) {
621 dev
->header_ops
= real_dev
->header_ops
;
622 dev
->hard_header_len
= real_dev
->hard_header_len
;
623 dev
->hard_start_xmit
= vlan_dev_hwaccel_hard_start_xmit
;
625 dev
->header_ops
= &vlan_header_ops
;
626 dev
->hard_header_len
= real_dev
->hard_header_len
+ VLAN_HLEN
;
627 dev
->hard_start_xmit
= vlan_dev_hard_start_xmit
;
630 if (is_vlan_dev(real_dev
))
633 vlan_dev_set_lockdep_class(dev
, subclass
);
637 static void vlan_dev_uninit(struct net_device
*dev
)
639 struct vlan_priority_tci_mapping
*pm
;
640 struct vlan_dev_info
*vlan
= vlan_dev_info(dev
);
643 for (i
= 0; i
< ARRAY_SIZE(vlan
->egress_priority_map
); i
++) {
644 while ((pm
= vlan
->egress_priority_map
[i
]) != NULL
) {
645 vlan
->egress_priority_map
[i
] = pm
->next
;
651 static u32
vlan_ethtool_get_rx_csum(struct net_device
*dev
)
653 const struct vlan_dev_info
*vlan
= vlan_dev_info(dev
);
654 struct net_device
*real_dev
= vlan
->real_dev
;
656 if (real_dev
->ethtool_ops
== NULL
||
657 real_dev
->ethtool_ops
->get_rx_csum
== NULL
)
659 return real_dev
->ethtool_ops
->get_rx_csum(real_dev
);
662 static u32
vlan_ethtool_get_flags(struct net_device
*dev
)
664 const struct vlan_dev_info
*vlan
= vlan_dev_info(dev
);
665 struct net_device
*real_dev
= vlan
->real_dev
;
667 if (!(real_dev
->features
& NETIF_F_HW_VLAN_RX
) ||
668 real_dev
->ethtool_ops
== NULL
||
669 real_dev
->ethtool_ops
->get_flags
== NULL
)
671 return real_dev
->ethtool_ops
->get_flags(real_dev
);
674 static const struct ethtool_ops vlan_ethtool_ops
= {
675 .get_link
= ethtool_op_get_link
,
676 .get_rx_csum
= vlan_ethtool_get_rx_csum
,
677 .get_flags
= vlan_ethtool_get_flags
,
680 void vlan_setup(struct net_device
*dev
)
684 dev
->priv_flags
|= IFF_802_1Q_VLAN
;
685 dev
->tx_queue_len
= 0;
687 dev
->change_mtu
= vlan_dev_change_mtu
;
688 dev
->init
= vlan_dev_init
;
689 dev
->uninit
= vlan_dev_uninit
;
690 dev
->open
= vlan_dev_open
;
691 dev
->stop
= vlan_dev_stop
;
692 dev
->set_mac_address
= vlan_dev_set_mac_address
;
693 dev
->set_rx_mode
= vlan_dev_set_rx_mode
;
694 dev
->set_multicast_list
= vlan_dev_set_rx_mode
;
695 dev
->change_rx_flags
= vlan_dev_change_rx_flags
;
696 dev
->do_ioctl
= vlan_dev_ioctl
;
697 dev
->destructor
= free_netdev
;
698 dev
->ethtool_ops
= &vlan_ethtool_ops
;
700 memset(dev
->broadcast
, 0, ETH_ALEN
);