1 /* -*- linux-c -*- 2 * INET 802.1Q VLAN 3 * Ethernet-type device handling. 4 * 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 8 * 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. 15 * 16 * 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. 21 */ 22 23 #include <linux/module.h> 24 #include <linux/mm.h> 25 #include <linux/in.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> 33 #include <net/arp.h> 34 35 #include "vlan.h" 36 #include "vlanproc.h" 37 #include <linux/if_vlan.h> 38 #include <net/ip.h> 39 40 /* 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. 44 * 45 * This routine CANNOT use cached dst->neigh! 46 * Really, it is used only when dst->neigh is wrong. 47 * 48 * TODO: This needs a checkup, I'm ignorant here. --BLG 49 */ 50 int vlan_dev_rebuild_header(struct sk_buff *skb) 51 { 52 struct net_device *dev = skb->dev; 53 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data); 54 55 switch (veth->h_vlan_encapsulated_proto) { 56 #ifdef CONFIG_INET 57 case __constant_htons(ETH_P_IP): 58 59 /* TODO: Confirm this will work with VLAN headers... */ 60 return arp_find(veth->h_dest, skb); 61 #endif 62 default: 63 printk(VLAN_DBG 64 "%s: unable to resolve type %X addresses.\n", 65 dev->name, (int)veth->h_vlan_encapsulated_proto); 66 67 memcpy(veth->h_source, dev->dev_addr, ETH_ALEN); 68 break; 69 }; 70 71 return 0; 72 } 73 74 static inline struct sk_buff *vlan_check_reorder_header(struct sk_buff *skb) 75 { 76 if (VLAN_DEV_INFO(skb->dev)->flags & 1) { 77 if (skb_shared(skb) || skb_cloned(skb)) { 78 struct sk_buff *nskb = skb_copy(skb, GFP_ATOMIC); 79 kfree_skb(skb); 80 skb = nskb; 81 } 82 if (skb) { 83 /* Lifted from Gleb's VLAN code... */ 84 memmove(skb->data - ETH_HLEN, 85 skb->data - VLAN_ETH_HLEN, 12); 86 skb->mac.raw += VLAN_HLEN; 87 } 88 } 89 90 return skb; 91 } 92 93 /* 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. 97 * 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. 101 * 102 * NOTE: Should be similar to ethernet/eth.c. 103 * 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 113 * 114 */ 115 int vlan_skb_recv(struct sk_buff *skb, struct net_device *dev, 116 struct packet_type* ptype, struct net_device *orig_dev) 117 { 118 unsigned char *rawp = NULL; 119 struct vlan_hdr *vhdr = (struct vlan_hdr *)(skb->data); 120 unsigned short vid; 121 struct net_device_stats *stats; 122 unsigned short vlan_TCI; 123 __be16 proto; 124 125 /* vlan_TCI = ntohs(get_unaligned(&vhdr->h_vlan_TCI)); */ 126 vlan_TCI = ntohs(vhdr->h_vlan_TCI); 127 128 vid = (vlan_TCI & VLAN_VID_MASK); 129 130 #ifdef VLAN_DEBUG 131 printk(VLAN_DBG "%s: skb: %p vlan_id: %hx\n", 132 __FUNCTION__, skb, vid); 133 #endif 134 135 /* Ok, we will find the correct VLAN device, strip the header, 136 * and then go on as usual. 137 */ 138 139 /* We have 12 bits of vlan ID. 140 * 141 * We must not drop allow preempt until we hold a 142 * reference to the device (netif_rx does that) or we 143 * fail. 144 */ 145 146 rcu_read_lock(); 147 skb->dev = __find_vlan_dev(dev, vid); 148 if (!skb->dev) { 149 rcu_read_unlock(); 150 151 #ifdef VLAN_DEBUG 152 printk(VLAN_DBG "%s: ERROR: No net_device for VID: %i on dev: %s [%i]\n", 153 __FUNCTION__, (unsigned int)(vid), dev->name, dev->ifindex); 154 #endif 155 kfree_skb(skb); 156 return -1; 157 } 158 159 skb->dev->last_rx = jiffies; 160 161 /* Bump the rx counters for the VLAN device. */ 162 stats = vlan_dev_get_stats(skb->dev); 163 stats->rx_packets++; 164 stats->rx_bytes += skb->len; 165 166 skb_pull(skb, VLAN_HLEN); /* take off the VLAN header (4 bytes currently) */ 167 168 /* Need to correct hardware checksum */ 169 skb_postpull_rcsum(skb, vhdr, VLAN_HLEN); 170 171 /* Ok, lets check to make sure the device (dev) we 172 * came in on is what this VLAN is attached to. 173 */ 174 175 if (dev != VLAN_DEV_INFO(skb->dev)->real_dev) { 176 rcu_read_unlock(); 177 178 #ifdef VLAN_DEBUG 179 printk(VLAN_DBG "%s: dropping skb: %p because came in on wrong device, dev: %s real_dev: %s, skb_dev: %s\n", 180 __FUNCTION__, skb, dev->name, 181 VLAN_DEV_INFO(skb->dev)->real_dev->name, 182 skb->dev->name); 183 #endif 184 kfree_skb(skb); 185 stats->rx_errors++; 186 return -1; 187 } 188 189 /* 190 * Deal with ingress priority mapping. 191 */ 192 skb->priority = vlan_get_ingress_priority(skb->dev, ntohs(vhdr->h_vlan_TCI)); 193 194 #ifdef VLAN_DEBUG 195 printk(VLAN_DBG "%s: priority: %lu for TCI: %hu (hbo)\n", 196 __FUNCTION__, (unsigned long)(skb->priority), 197 ntohs(vhdr->h_vlan_TCI)); 198 #endif 199 200 /* The ethernet driver already did the pkt_type calculations 201 * for us... 202 */ 203 switch (skb->pkt_type) { 204 case PACKET_BROADCAST: /* Yeah, stats collect these together.. */ 205 // stats->broadcast ++; // no such counter :-( 206 break; 207 208 case PACKET_MULTICAST: 209 stats->multicast++; 210 break; 211 212 case PACKET_OTHERHOST: 213 /* Our lower layer thinks this is not local, let's make sure. 214 * This allows the VLAN to have a different MAC than the underlying 215 * device, and still route correctly. 216 */ 217 if (!compare_ether_addr(eth_hdr(skb)->h_dest, skb->dev->dev_addr)) { 218 /* It is for our (changed) MAC-address! */ 219 skb->pkt_type = PACKET_HOST; 220 } 221 break; 222 default: 223 break; 224 }; 225 226 /* Was a VLAN packet, grab the encapsulated protocol, which the layer 227 * three protocols care about. 228 */ 229 /* proto = get_unaligned(&vhdr->h_vlan_encapsulated_proto); */ 230 proto = vhdr->h_vlan_encapsulated_proto; 231 232 skb->protocol = proto; 233 if (ntohs(proto) >= 1536) { 234 /* place it back on the queue to be handled by 235 * true layer 3 protocols. 236 */ 237 238 /* See if we are configured to re-write the VLAN header 239 * to make it look like ethernet... 240 */ 241 skb = vlan_check_reorder_header(skb); 242 243 /* Can be null if skb-clone fails when re-ordering */ 244 if (skb) { 245 netif_rx(skb); 246 } else { 247 /* TODO: Add a more specific counter here. */ 248 stats->rx_errors++; 249 } 250 rcu_read_unlock(); 251 return 0; 252 } 253 254 rawp = skb->data; 255 256 /* 257 * This is a magic hack to spot IPX packets. Older Novell breaks 258 * the protocol design and runs IPX over 802.3 without an 802.2 LLC 259 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This 260 * won't work for fault tolerant netware but does for the rest. 261 */ 262 if (*(unsigned short *)rawp == 0xFFFF) { 263 skb->protocol = __constant_htons(ETH_P_802_3); 264 /* place it back on the queue to be handled by true layer 3 protocols. 265 */ 266 267 /* See if we are configured to re-write the VLAN header 268 * to make it look like ethernet... 269 */ 270 skb = vlan_check_reorder_header(skb); 271 272 /* Can be null if skb-clone fails when re-ordering */ 273 if (skb) { 274 netif_rx(skb); 275 } else { 276 /* TODO: Add a more specific counter here. */ 277 stats->rx_errors++; 278 } 279 rcu_read_unlock(); 280 return 0; 281 } 282 283 /* 284 * Real 802.2 LLC 285 */ 286 skb->protocol = __constant_htons(ETH_P_802_2); 287 /* place it back on the queue to be handled by upper layer protocols. 288 */ 289 290 /* See if we are configured to re-write the VLAN header 291 * to make it look like ethernet... 292 */ 293 skb = vlan_check_reorder_header(skb); 294 295 /* Can be null if skb-clone fails when re-ordering */ 296 if (skb) { 297 netif_rx(skb); 298 } else { 299 /* TODO: Add a more specific counter here. */ 300 stats->rx_errors++; 301 } 302 rcu_read_unlock(); 303 return 0; 304 } 305 306 static inline unsigned short vlan_dev_get_egress_qos_mask(struct net_device* dev, 307 struct sk_buff* skb) 308 { 309 struct vlan_priority_tci_mapping *mp = 310 VLAN_DEV_INFO(dev)->egress_priority_map[(skb->priority & 0xF)]; 311 312 while (mp) { 313 if (mp->priority == skb->priority) { 314 return mp->vlan_qos; /* This should already be shifted to mask 315 * correctly with the VLAN's TCI 316 */ 317 } 318 mp = mp->next; 319 } 320 return 0; 321 } 322 323 /* 324 * Create the VLAN header for an arbitrary protocol layer 325 * 326 * saddr=NULL means use device source address 327 * daddr=NULL means leave destination address (eg unresolved arp) 328 * 329 * This is called when the SKB is moving down the stack towards the 330 * physical devices. 331 */ 332 int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev, 333 unsigned short type, void *daddr, void *saddr, 334 unsigned len) 335 { 336 struct vlan_hdr *vhdr; 337 unsigned short veth_TCI = 0; 338 int rc = 0; 339 int build_vlan_header = 0; 340 struct net_device *vdev = dev; /* save this for the bottom of the method */ 341 342 #ifdef VLAN_DEBUG 343 printk(VLAN_DBG "%s: skb: %p type: %hx len: %x vlan_id: %hx, daddr: %p\n", 344 __FUNCTION__, skb, type, len, VLAN_DEV_INFO(dev)->vlan_id, daddr); 345 #endif 346 347 /* build vlan header only if re_order_header flag is NOT set. This 348 * fixes some programs that get confused when they see a VLAN device 349 * sending a frame that is VLAN encoded (the consensus is that the VLAN 350 * device should look completely like an Ethernet device when the 351 * REORDER_HEADER flag is set) The drawback to this is some extra 352 * header shuffling in the hard_start_xmit. Users can turn off this 353 * REORDER behaviour with the vconfig tool. 354 */ 355 build_vlan_header = ((VLAN_DEV_INFO(dev)->flags & 1) == 0); 356 357 if (build_vlan_header) { 358 vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN); 359 360 /* build the four bytes that make this a VLAN header. */ 361 362 /* Now, construct the second two bytes. This field looks something 363 * like: 364 * usr_priority: 3 bits (high bits) 365 * CFI 1 bit 366 * VLAN ID 12 bits (low bits) 367 * 368 */ 369 veth_TCI = VLAN_DEV_INFO(dev)->vlan_id; 370 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb); 371 372 vhdr->h_vlan_TCI = htons(veth_TCI); 373 374 /* 375 * Set the protocol type. 376 * For a packet of type ETH_P_802_3 we put the length in here instead. 377 * It is up to the 802.2 layer to carry protocol information. 378 */ 379 380 if (type != ETH_P_802_3) { 381 vhdr->h_vlan_encapsulated_proto = htons(type); 382 } else { 383 vhdr->h_vlan_encapsulated_proto = htons(len); 384 } 385 } 386 387 /* Before delegating work to the lower layer, enter our MAC-address */ 388 if (saddr == NULL) 389 saddr = dev->dev_addr; 390 391 dev = VLAN_DEV_INFO(dev)->real_dev; 392 393 /* MPLS can send us skbuffs w/out enough space. This check will grow the 394 * skb if it doesn't have enough headroom. Not a beautiful solution, so 395 * I'll tick a counter so that users can know it's happening... If they 396 * care... 397 */ 398 399 /* NOTE: This may still break if the underlying device is not the final 400 * device (and thus there are more headers to add...) It should work for 401 * good-ole-ethernet though. 402 */ 403 if (skb_headroom(skb) < dev->hard_header_len) { 404 struct sk_buff *sk_tmp = skb; 405 skb = skb_realloc_headroom(sk_tmp, dev->hard_header_len); 406 kfree_skb(sk_tmp); 407 if (skb == NULL) { 408 struct net_device_stats *stats = vlan_dev_get_stats(vdev); 409 stats->tx_dropped++; 410 return -ENOMEM; 411 } 412 VLAN_DEV_INFO(vdev)->cnt_inc_headroom_on_tx++; 413 #ifdef VLAN_DEBUG 414 printk(VLAN_DBG "%s: %s: had to grow skb.\n", __FUNCTION__, vdev->name); 415 #endif 416 } 417 418 if (build_vlan_header) { 419 /* Now make the underlying real hard header */ 420 rc = dev->hard_header(skb, dev, ETH_P_8021Q, daddr, saddr, len + VLAN_HLEN); 421 422 if (rc > 0) { 423 rc += VLAN_HLEN; 424 } else if (rc < 0) { 425 rc -= VLAN_HLEN; 426 } 427 } else { 428 /* If here, then we'll just make a normal looking ethernet frame, 429 * but, the hard_start_xmit method will insert the tag (it has to 430 * be able to do this for bridged and other skbs that don't come 431 * down the protocol stack in an orderly manner. 432 */ 433 rc = dev->hard_header(skb, dev, type, daddr, saddr, len); 434 } 435 436 return rc; 437 } 438 439 int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev) 440 { 441 struct net_device_stats *stats = vlan_dev_get_stats(dev); 442 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data); 443 444 /* Handle non-VLAN frames if they are sent to us, for example by DHCP. 445 * 446 * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING 447 * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs... 448 */ 449 450 if (veth->h_vlan_proto != __constant_htons(ETH_P_8021Q)) { 451 int orig_headroom = skb_headroom(skb); 452 unsigned short veth_TCI; 453 454 /* This is not a VLAN frame...but we can fix that! */ 455 VLAN_DEV_INFO(dev)->cnt_encap_on_xmit++; 456 457 #ifdef VLAN_DEBUG 458 printk(VLAN_DBG "%s: proto to encap: 0x%hx (hbo)\n", 459 __FUNCTION__, htons(veth->h_vlan_proto)); 460 #endif 461 /* Construct the second two bytes. This field looks something 462 * like: 463 * usr_priority: 3 bits (high bits) 464 * CFI 1 bit 465 * VLAN ID 12 bits (low bits) 466 */ 467 veth_TCI = VLAN_DEV_INFO(dev)->vlan_id; 468 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb); 469 470 skb = __vlan_put_tag(skb, veth_TCI); 471 if (!skb) { 472 stats->tx_dropped++; 473 return 0; 474 } 475 476 if (orig_headroom < VLAN_HLEN) { 477 VLAN_DEV_INFO(dev)->cnt_inc_headroom_on_tx++; 478 } 479 } 480 481 #ifdef VLAN_DEBUG 482 printk(VLAN_DBG "%s: about to send skb: %p to dev: %s\n", 483 __FUNCTION__, skb, skb->dev->name); 484 printk(VLAN_DBG " %2hx.%2hx.%2hx.%2xh.%2hx.%2hx %2hx.%2hx.%2hx.%2hx.%2hx.%2hx %4hx %4hx %4hx\n", 485 veth->h_dest[0], veth->h_dest[1], veth->h_dest[2], veth->h_dest[3], veth->h_dest[4], veth->h_dest[5], 486 veth->h_source[0], veth->h_source[1], veth->h_source[2], veth->h_source[3], veth->h_source[4], veth->h_source[5], 487 veth->h_vlan_proto, veth->h_vlan_TCI, veth->h_vlan_encapsulated_proto); 488 #endif 489 490 stats->tx_packets++; /* for statics only */ 491 stats->tx_bytes += skb->len; 492 493 skb->dev = VLAN_DEV_INFO(dev)->real_dev; 494 dev_queue_xmit(skb); 495 496 return 0; 497 } 498 499 int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb, struct net_device *dev) 500 { 501 struct net_device_stats *stats = vlan_dev_get_stats(dev); 502 unsigned short veth_TCI; 503 504 /* Construct the second two bytes. This field looks something 505 * like: 506 * usr_priority: 3 bits (high bits) 507 * CFI 1 bit 508 * VLAN ID 12 bits (low bits) 509 */ 510 veth_TCI = VLAN_DEV_INFO(dev)->vlan_id; 511 veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb); 512 skb = __vlan_hwaccel_put_tag(skb, veth_TCI); 513 514 stats->tx_packets++; 515 stats->tx_bytes += skb->len; 516 517 skb->dev = VLAN_DEV_INFO(dev)->real_dev; 518 dev_queue_xmit(skb); 519 520 return 0; 521 } 522 523 int vlan_dev_change_mtu(struct net_device *dev, int new_mtu) 524 { 525 /* TODO: gotta make sure the underlying layer can handle it, 526 * maybe an IFF_VLAN_CAPABLE flag for devices? 527 */ 528 if (VLAN_DEV_INFO(dev)->real_dev->mtu < new_mtu) 529 return -ERANGE; 530 531 dev->mtu = new_mtu; 532 533 return 0; 534 } 535 536 int vlan_dev_set_ingress_priority(char *dev_name, __u32 skb_prio, short vlan_prio) 537 { 538 struct net_device *dev = dev_get_by_name(dev_name); 539 540 if (dev) { 541 if (dev->priv_flags & IFF_802_1Q_VLAN) { 542 /* see if a priority mapping exists.. */ 543 VLAN_DEV_INFO(dev)->ingress_priority_map[vlan_prio & 0x7] = skb_prio; 544 dev_put(dev); 545 return 0; 546 } 547 548 dev_put(dev); 549 } 550 return -EINVAL; 551 } 552 553 int vlan_dev_set_egress_priority(char *dev_name, __u32 skb_prio, short vlan_prio) 554 { 555 struct net_device *dev = dev_get_by_name(dev_name); 556 struct vlan_priority_tci_mapping *mp = NULL; 557 struct vlan_priority_tci_mapping *np; 558 559 if (dev) { 560 if (dev->priv_flags & IFF_802_1Q_VLAN) { 561 /* See if a priority mapping exists.. */ 562 mp = VLAN_DEV_INFO(dev)->egress_priority_map[skb_prio & 0xF]; 563 while (mp) { 564 if (mp->priority == skb_prio) { 565 mp->vlan_qos = ((vlan_prio << 13) & 0xE000); 566 dev_put(dev); 567 return 0; 568 } 569 mp = mp->next; 570 } 571 572 /* Create a new mapping then. */ 573 mp = VLAN_DEV_INFO(dev)->egress_priority_map[skb_prio & 0xF]; 574 np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL); 575 if (np) { 576 np->next = mp; 577 np->priority = skb_prio; 578 np->vlan_qos = ((vlan_prio << 13) & 0xE000); 579 VLAN_DEV_INFO(dev)->egress_priority_map[skb_prio & 0xF] = np; 580 dev_put(dev); 581 return 0; 582 } else { 583 dev_put(dev); 584 return -ENOBUFS; 585 } 586 } 587 dev_put(dev); 588 } 589 return -EINVAL; 590 } 591 592 /* Flags are defined in the vlan_dev_info class in include/linux/if_vlan.h file. */ 593 int vlan_dev_set_vlan_flag(char *dev_name, __u32 flag, short flag_val) 594 { 595 struct net_device *dev = dev_get_by_name(dev_name); 596 597 if (dev) { 598 if (dev->priv_flags & IFF_802_1Q_VLAN) { 599 /* verify flag is supported */ 600 if (flag == 1) { 601 if (flag_val) { 602 VLAN_DEV_INFO(dev)->flags |= 1; 603 } else { 604 VLAN_DEV_INFO(dev)->flags &= ~1; 605 } 606 dev_put(dev); 607 return 0; 608 } else { 609 printk(KERN_ERR "%s: flag %i is not valid.\n", 610 __FUNCTION__, (int)(flag)); 611 dev_put(dev); 612 return -EINVAL; 613 } 614 } else { 615 printk(KERN_ERR 616 "%s: %s is not a vlan device, priv_flags: %hX.\n", 617 __FUNCTION__, dev->name, dev->priv_flags); 618 dev_put(dev); 619 } 620 } else { 621 printk(KERN_ERR "%s: Could not find device: %s\n", 622 __FUNCTION__, dev_name); 623 } 624 625 return -EINVAL; 626 } 627 628 629 int vlan_dev_get_realdev_name(const char *dev_name, char* result) 630 { 631 struct net_device *dev = dev_get_by_name(dev_name); 632 int rv = 0; 633 if (dev) { 634 if (dev->priv_flags & IFF_802_1Q_VLAN) { 635 strncpy(result, VLAN_DEV_INFO(dev)->real_dev->name, 23); 636 rv = 0; 637 } else { 638 rv = -EINVAL; 639 } 640 dev_put(dev); 641 } else { 642 rv = -ENODEV; 643 } 644 return rv; 645 } 646 647 int vlan_dev_get_vid(const char *dev_name, unsigned short* result) 648 { 649 struct net_device *dev = dev_get_by_name(dev_name); 650 int rv = 0; 651 if (dev) { 652 if (dev->priv_flags & IFF_802_1Q_VLAN) { 653 *result = VLAN_DEV_INFO(dev)->vlan_id; 654 rv = 0; 655 } else { 656 rv = -EINVAL; 657 } 658 dev_put(dev); 659 } else { 660 rv = -ENODEV; 661 } 662 return rv; 663 } 664 665 666 int vlan_dev_set_mac_address(struct net_device *dev, void *addr_struct_p) 667 { 668 struct sockaddr *addr = (struct sockaddr *)(addr_struct_p); 669 int i; 670 671 if (netif_running(dev)) 672 return -EBUSY; 673 674 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); 675 676 printk("%s: Setting MAC address to ", dev->name); 677 for (i = 0; i < 6; i++) 678 printk(" %2.2x", dev->dev_addr[i]); 679 printk(".\n"); 680 681 if (memcmp(VLAN_DEV_INFO(dev)->real_dev->dev_addr, 682 dev->dev_addr, 683 dev->addr_len) != 0) { 684 if (!(VLAN_DEV_INFO(dev)->real_dev->flags & IFF_PROMISC)) { 685 int flgs = VLAN_DEV_INFO(dev)->real_dev->flags; 686 687 /* Increment our in-use promiscuity counter */ 688 dev_set_promiscuity(VLAN_DEV_INFO(dev)->real_dev, 1); 689 690 /* Make PROMISC visible to the user. */ 691 flgs |= IFF_PROMISC; 692 printk("VLAN (%s): Setting underlying device (%s) to promiscious mode.\n", 693 dev->name, VLAN_DEV_INFO(dev)->real_dev->name); 694 dev_change_flags(VLAN_DEV_INFO(dev)->real_dev, flgs); 695 } 696 } else { 697 printk("VLAN (%s): Underlying device (%s) has same MAC, not checking promiscious mode.\n", 698 dev->name, VLAN_DEV_INFO(dev)->real_dev->name); 699 } 700 701 return 0; 702 } 703 704 static inline int vlan_dmi_equals(struct dev_mc_list *dmi1, 705 struct dev_mc_list *dmi2) 706 { 707 return ((dmi1->dmi_addrlen == dmi2->dmi_addrlen) && 708 (memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0)); 709 } 710 711 /** dmi is a single entry into a dev_mc_list, a single node. mc_list is 712 * an entire list, and we'll iterate through it. 713 */ 714 static int vlan_should_add_mc(struct dev_mc_list *dmi, struct dev_mc_list *mc_list) 715 { 716 struct dev_mc_list *idmi; 717 718 for (idmi = mc_list; idmi != NULL; ) { 719 if (vlan_dmi_equals(dmi, idmi)) { 720 if (dmi->dmi_users > idmi->dmi_users) 721 return 1; 722 else 723 return 0; 724 } else { 725 idmi = idmi->next; 726 } 727 } 728 729 return 1; 730 } 731 732 static inline void vlan_destroy_mc_list(struct dev_mc_list *mc_list) 733 { 734 struct dev_mc_list *dmi = mc_list; 735 struct dev_mc_list *next; 736 737 while(dmi) { 738 next = dmi->next; 739 kfree(dmi); 740 dmi = next; 741 } 742 } 743 744 static void vlan_copy_mc_list(struct dev_mc_list *mc_list, struct vlan_dev_info *vlan_info) 745 { 746 struct dev_mc_list *dmi, *new_dmi; 747 748 vlan_destroy_mc_list(vlan_info->old_mc_list); 749 vlan_info->old_mc_list = NULL; 750 751 for (dmi = mc_list; dmi != NULL; dmi = dmi->next) { 752 new_dmi = kmalloc(sizeof(*new_dmi), GFP_ATOMIC); 753 if (new_dmi == NULL) { 754 printk(KERN_ERR "vlan: cannot allocate memory. " 755 "Multicast may not work properly from now.\n"); 756 return; 757 } 758 759 /* Copy whole structure, then make new 'next' pointer */ 760 *new_dmi = *dmi; 761 new_dmi->next = vlan_info->old_mc_list; 762 vlan_info->old_mc_list = new_dmi; 763 } 764 } 765 766 static void vlan_flush_mc_list(struct net_device *dev) 767 { 768 struct dev_mc_list *dmi = dev->mc_list; 769 770 while (dmi) { 771 printk(KERN_DEBUG "%s: del %.2x:%.2x:%.2x:%.2x:%.2x:%.2x mcast address from vlan interface\n", 772 dev->name, 773 dmi->dmi_addr[0], 774 dmi->dmi_addr[1], 775 dmi->dmi_addr[2], 776 dmi->dmi_addr[3], 777 dmi->dmi_addr[4], 778 dmi->dmi_addr[5]); 779 dev_mc_delete(dev, dmi->dmi_addr, dmi->dmi_addrlen, 0); 780 dmi = dev->mc_list; 781 } 782 783 /* dev->mc_list is NULL by the time we get here. */ 784 vlan_destroy_mc_list(VLAN_DEV_INFO(dev)->old_mc_list); 785 VLAN_DEV_INFO(dev)->old_mc_list = NULL; 786 } 787 788 int vlan_dev_open(struct net_device *dev) 789 { 790 if (!(VLAN_DEV_INFO(dev)->real_dev->flags & IFF_UP)) 791 return -ENETDOWN; 792 793 return 0; 794 } 795 796 int vlan_dev_stop(struct net_device *dev) 797 { 798 vlan_flush_mc_list(dev); 799 return 0; 800 } 801 802 int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 803 { 804 struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev; 805 struct ifreq ifrr; 806 int err = -EOPNOTSUPP; 807 808 strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ); 809 ifrr.ifr_ifru = ifr->ifr_ifru; 810 811 switch(cmd) { 812 case SIOCGMIIPHY: 813 case SIOCGMIIREG: 814 case SIOCSMIIREG: 815 if (real_dev->do_ioctl && netif_device_present(real_dev)) 816 err = real_dev->do_ioctl(real_dev, &ifrr, cmd); 817 break; 818 819 case SIOCETHTOOL: 820 err = dev_ethtool(&ifrr); 821 } 822 823 if (!err) 824 ifr->ifr_ifru = ifrr.ifr_ifru; 825 826 return err; 827 } 828 829 /** Taken from Gleb + Lennert's VLAN code, and modified... */ 830 void vlan_dev_set_multicast_list(struct net_device *vlan_dev) 831 { 832 struct dev_mc_list *dmi; 833 struct net_device *real_dev; 834 int inc; 835 836 if (vlan_dev && (vlan_dev->priv_flags & IFF_802_1Q_VLAN)) { 837 /* Then it's a real vlan device, as far as we can tell.. */ 838 real_dev = VLAN_DEV_INFO(vlan_dev)->real_dev; 839 840 /* compare the current promiscuity to the last promisc we had.. */ 841 inc = vlan_dev->promiscuity - VLAN_DEV_INFO(vlan_dev)->old_promiscuity; 842 if (inc) { 843 printk(KERN_INFO "%s: dev_set_promiscuity(master, %d)\n", 844 vlan_dev->name, inc); 845 dev_set_promiscuity(real_dev, inc); /* found in dev.c */ 846 VLAN_DEV_INFO(vlan_dev)->old_promiscuity = vlan_dev->promiscuity; 847 } 848 849 inc = vlan_dev->allmulti - VLAN_DEV_INFO(vlan_dev)->old_allmulti; 850 if (inc) { 851 printk(KERN_INFO "%s: dev_set_allmulti(master, %d)\n", 852 vlan_dev->name, inc); 853 dev_set_allmulti(real_dev, inc); /* dev.c */ 854 VLAN_DEV_INFO(vlan_dev)->old_allmulti = vlan_dev->allmulti; 855 } 856 857 /* looking for addresses to add to master's list */ 858 for (dmi = vlan_dev->mc_list; dmi != NULL; dmi = dmi->next) { 859 if (vlan_should_add_mc(dmi, VLAN_DEV_INFO(vlan_dev)->old_mc_list)) { 860 dev_mc_add(real_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0); 861 printk(KERN_DEBUG "%s: add %.2x:%.2x:%.2x:%.2x:%.2x:%.2x mcast address to master interface\n", 862 vlan_dev->name, 863 dmi->dmi_addr[0], 864 dmi->dmi_addr[1], 865 dmi->dmi_addr[2], 866 dmi->dmi_addr[3], 867 dmi->dmi_addr[4], 868 dmi->dmi_addr[5]); 869 } 870 } 871 872 /* looking for addresses to delete from master's list */ 873 for (dmi = VLAN_DEV_INFO(vlan_dev)->old_mc_list; dmi != NULL; dmi = dmi->next) { 874 if (vlan_should_add_mc(dmi, vlan_dev->mc_list)) { 875 /* if we think we should add it to the new list, then we should really 876 * delete it from the real list on the underlying device. 877 */ 878 dev_mc_delete(real_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0); 879 printk(KERN_DEBUG "%s: del %.2x:%.2x:%.2x:%.2x:%.2x:%.2x mcast address from master interface\n", 880 vlan_dev->name, 881 dmi->dmi_addr[0], 882 dmi->dmi_addr[1], 883 dmi->dmi_addr[2], 884 dmi->dmi_addr[3], 885 dmi->dmi_addr[4], 886 dmi->dmi_addr[5]); 887 } 888 } 889 890 /* save multicast list */ 891 vlan_copy_mc_list(vlan_dev->mc_list, VLAN_DEV_INFO(vlan_dev)); 892 } 893 } 894