linux-old/net/core/dev.c
<<
>>
Prefs
   1/*
   2 *      NET3    Protocol independent device support routines.
   3 *
   4 *              This program is free software; you can redistribute it and/or
   5 *              modify it under the terms of the GNU General Public License
   6 *              as published by the Free Software Foundation; either version
   7 *              2 of the License, or (at your option) any later version.
   8 *
   9 *      Derived from the non IP parts of dev.c 1.0.19
  10 *              Authors:        Ross Biro, <bir7@leland.Stanford.Edu>
  11 *                              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12 *                              Mark Evans, <evansmp@uhura.aston.ac.uk>
  13 *
  14 *      Additional Authors:
  15 *              Florian la Roche <rzsfl@rz.uni-sb.de>
  16 *              Alan Cox <gw4pts@gw4pts.ampr.org>
  17 *              David Hinds <dahinds@users.sourceforge.net>
  18 *              Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  19 *              Adam Sulmicki <adam@cfar.umd.edu>
  20 *              Pekka Riikonen <priikone@poesidon.pspt.fi>
  21 *
  22 *      Changes:
  23 *              D.J. Barrow     :       Fixed bug where dev->refcnt gets set to 2
  24 *                                      if register_netdev gets called before
  25 *                                      net_dev_init & also removed a few lines
  26 *                                      of code in the process.
  27 *              Alan Cox        :       device private ioctl copies fields back.
  28 *              Alan Cox        :       Transmit queue code does relevant stunts to
  29 *                                      keep the queue safe.
  30 *              Alan Cox        :       Fixed double lock.
  31 *              Alan Cox        :       Fixed promisc NULL pointer trap
  32 *              ????????        :       Support the full private ioctl range
  33 *              Alan Cox        :       Moved ioctl permission check into drivers
  34 *              Tim Kordas      :       SIOCADDMULTI/SIOCDELMULTI
  35 *              Alan Cox        :       100 backlog just doesn't cut it when
  36 *                                      you start doing multicast video 8)
  37 *              Alan Cox        :       Rewrote net_bh and list manager.
  38 *              Alan Cox        :       Fix ETH_P_ALL echoback lengths.
  39 *              Alan Cox        :       Took out transmit every packet pass
  40 *                                      Saved a few bytes in the ioctl handler
  41 *              Alan Cox        :       Network driver sets packet type before calling netif_rx. Saves
  42 *                                      a function call a packet.
  43 *              Alan Cox        :       Hashed net_bh()
  44 *              Richard Kooijman:       Timestamp fixes.
  45 *              Alan Cox        :       Wrong field in SIOCGIFDSTADDR
  46 *              Alan Cox        :       Device lock protection.
  47 *              Alan Cox        :       Fixed nasty side effect of device close changes.
  48 *              Rudi Cilibrasi  :       Pass the right thing to set_mac_address()
  49 *              Dave Miller     :       32bit quantity for the device lock to make it work out
  50 *                                      on a Sparc.
  51 *              Bjorn Ekwall    :       Added KERNELD hack.
  52 *              Alan Cox        :       Cleaned up the backlog initialise.
  53 *              Craig Metz      :       SIOCGIFCONF fix if space for under
  54 *                                      1 device.
  55 *          Thomas Bogendoerfer :       Return ENODEV for dev_open, if there
  56 *                                      is no device open function.
  57 *              Andi Kleen      :       Fix error reporting for SIOCGIFCONF
  58 *          Michael Chastain    :       Fix signed/unsigned for SIOCGIFCONF
  59 *              Cyrus Durgin    :       Cleaned for KMOD
  60 *              Adam Sulmicki   :       Bug Fix : Network Device Unload
  61 *                                      A network device unload needs to purge
  62 *                                      the backlog queue.
  63 *      Paul Rusty Russell      :       SIOCSIFNAME
  64 *              Pekka Riikonen  :       Netdev boot-time settings code
  65 *              Andrew Morton   :       Make unregister_netdevice wait indefinitely on dev->refcnt
  66 *              J Hadi Salim    :       - Backlog queue sampling
  67 *                                      - netif_rx() feedback   
  68 */
  69
  70#include <asm/uaccess.h>
  71#include <asm/system.h>
  72#include <asm/bitops.h>
  73#include <linux/config.h>
  74#include <linux/types.h>
  75#include <linux/kernel.h>
  76#include <linux/sched.h>
  77#include <linux/string.h>
  78#include <linux/mm.h>
  79#include <linux/socket.h>
  80#include <linux/sockios.h>
  81#include <linux/errno.h>
  82#include <linux/interrupt.h>
  83#include <linux/if_ether.h>
  84#include <linux/netdevice.h>
  85#include <linux/etherdevice.h>
  86#include <linux/notifier.h>
  87#include <linux/skbuff.h>
  88#include <linux/brlock.h>
  89#include <net/sock.h>
  90#include <linux/rtnetlink.h>
  91#include <linux/proc_fs.h>
  92#include <linux/stat.h>
  93#include <linux/if_bridge.h>
  94#include <linux/divert.h>
  95#include <net/dst.h>
  96#include <net/pkt_sched.h>
  97#include <net/profile.h>
  98#include <net/checksum.h>
  99#include <linux/highmem.h>
 100#include <linux/init.h>
 101#include <linux/kmod.h>
 102#include <linux/module.h>
 103#if defined(CONFIG_NET_RADIO) || defined(CONFIG_NET_PCMCIA_RADIO)
 104#include <linux/wireless.h>             /* Note : will define WIRELESS_EXT */
 105#include <net/iw_handler.h>
 106#endif  /* CONFIG_NET_RADIO || CONFIG_NET_PCMCIA_RADIO */
 107#ifdef CONFIG_PLIP
 108extern int plip_init(void);
 109#endif
 110
 111
 112/* This define, if set, will randomly drop a packet when congestion
 113 * is more than moderate.  It helps fairness in the multi-interface
 114 * case when one of them is a hog, but it kills performance for the
 115 * single interface case so it is off now by default.
 116 */
 117#undef RAND_LIE
 118
 119/* Setting this will sample the queue lengths and thus congestion
 120 * via a timer instead of as each packet is received.
 121 */
 122#undef OFFLINE_SAMPLE
 123
 124NET_PROFILE_DEFINE(dev_queue_xmit)
 125NET_PROFILE_DEFINE(softnet_process)
 126
 127const char *if_port_text[] = {
 128  "unknown",
 129  "BNC",
 130  "10baseT",
 131  "AUI",
 132  "100baseT",
 133  "100baseTX",
 134  "100baseFX"
 135};
 136
 137/*
 138 *      The list of packet types we will receive (as opposed to discard)
 139 *      and the routines to invoke.
 140 *
 141 *      Why 16. Because with 16 the only overlap we get on a hash of the
 142 *      low nibble of the protocol value is RARP/SNAP/X.25.
 143 *
 144 *      NOTE:  That is no longer true with the addition of VLAN tags.  Not
 145 *             sure which should go first, but I bet it won't make much
 146 *             difference if we are running VLANs.  The good news is that
 147 *             this protocol won't be in the list unless compiled in, so
 148 *             the average user (w/out VLANs) will not be adversly affected.
 149 *             --BLG
 150 *
 151 *              0800    IP
 152 *              8100    802.1Q VLAN
 153 *              0001    802.3
 154 *              0002    AX.25
 155 *              0004    802.2
 156 *              8035    RARP
 157 *              0005    SNAP
 158 *              0805    X.25
 159 *              0806    ARP
 160 *              8137    IPX
 161 *              0009    Localtalk
 162 *              86DD    IPv6
 163 */
 164
 165static struct packet_type *ptype_base[16];              /* 16 way hashed list */
 166static struct packet_type *ptype_all = NULL;            /* Taps */
 167
 168#ifdef OFFLINE_SAMPLE
 169static void sample_queue(unsigned long dummy);
 170static struct timer_list samp_timer = { function: sample_queue };
 171#endif
 172
 173#ifdef CONFIG_HOTPLUG
 174static int net_run_sbin_hotplug(struct net_device *dev, char *action);
 175#else
 176#define net_run_sbin_hotplug(dev, action) ({ 0; })
 177#endif
 178
 179/*
 180 *      Our notifier list
 181 */
 182 
 183static struct notifier_block *netdev_chain=NULL;
 184
 185/*
 186 *      Device drivers call our routines to queue packets here. We empty the
 187 *      queue in the local softnet handler.
 188 */
 189struct softnet_data softnet_data[NR_CPUS] __cacheline_aligned;
 190
 191#ifdef CONFIG_NET_FASTROUTE
 192int netdev_fastroute;
 193int netdev_fastroute_obstacles;
 194#endif
 195
 196
 197/******************************************************************************************
 198
 199                Protocol management and registration routines
 200
 201*******************************************************************************************/
 202
 203/*
 204 *      For efficiency
 205 */
 206
 207int netdev_nit=0;
 208
 209/*
 210 *      Add a protocol ID to the list. Now that the input handler is
 211 *      smarter we can dispense with all the messy stuff that used to be
 212 *      here.
 213 *
 214 *      BEWARE!!! Protocol handlers, mangling input packets,
 215 *      MUST BE last in hash buckets and checking protocol handlers
 216 *      MUST start from promiscous ptype_all chain in net_bh.
 217 *      It is true now, do not change it.
 218 *      Explantion follows: if protocol handler, mangling packet, will
 219 *      be the first on list, it is not able to sense, that packet
 220 *      is cloned and should be copied-on-write, so that it will
 221 *      change it and subsequent readers will get broken packet.
 222 *                                                      --ANK (980803)
 223 */
 224
 225/**
 226 *      dev_add_pack - add packet handler
 227 *      @pt: packet type declaration
 228 * 
 229 *      Add a protocol handler to the networking stack. The passed &packet_type
 230 *      is linked into kernel lists and may not be freed until it has been
 231 *      removed from the kernel lists.
 232 */
 233 
 234void dev_add_pack(struct packet_type *pt)
 235{
 236        int hash;
 237
 238        br_write_lock_bh(BR_NETPROTO_LOCK);
 239
 240#ifdef CONFIG_NET_FASTROUTE
 241        /* Hack to detect packet socket */
 242        if ((pt->data) && ((int)(pt->data)!=1)) {
 243                netdev_fastroute_obstacles++;
 244                dev_clear_fastroute(pt->dev);
 245        }
 246#endif
 247        if (pt->type == htons(ETH_P_ALL)) {
 248                netdev_nit++;
 249                pt->next=ptype_all;
 250                ptype_all=pt;
 251        } else {
 252                hash=ntohs(pt->type)&15;
 253                pt->next = ptype_base[hash];
 254                ptype_base[hash] = pt;
 255        }
 256        br_write_unlock_bh(BR_NETPROTO_LOCK);
 257}
 258
 259
 260/**
 261 *      dev_remove_pack  - remove packet handler
 262 *      @pt: packet type declaration
 263 * 
 264 *      Remove a protocol handler that was previously added to the kernel
 265 *      protocol handlers by dev_add_pack(). The passed &packet_type is removed
 266 *      from the kernel lists and can be freed or reused once this function
 267 *      returns.
 268 */
 269 
 270void dev_remove_pack(struct packet_type *pt)
 271{
 272        struct packet_type **pt1;
 273
 274        br_write_lock_bh(BR_NETPROTO_LOCK);
 275
 276        if (pt->type == htons(ETH_P_ALL)) {
 277                netdev_nit--;
 278                pt1=&ptype_all;
 279        } else {
 280                pt1=&ptype_base[ntohs(pt->type)&15];
 281        }
 282
 283        for (; (*pt1) != NULL; pt1 = &((*pt1)->next)) {
 284                if (pt == (*pt1)) {
 285                        *pt1 = pt->next;
 286#ifdef CONFIG_NET_FASTROUTE
 287                        if (pt->data)
 288                                netdev_fastroute_obstacles--;
 289#endif
 290                        br_write_unlock_bh(BR_NETPROTO_LOCK);
 291                        return;
 292                }
 293        }
 294        br_write_unlock_bh(BR_NETPROTO_LOCK);
 295        printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
 296}
 297
 298/******************************************************************************
 299
 300                      Device Boot-time Settings Routines
 301
 302*******************************************************************************/
 303
 304/* Boot time configuration table */
 305static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
 306
 307/**
 308 *      netdev_boot_setup_add   - add new setup entry
 309 *      @name: name of the device
 310 *      @map: configured settings for the device
 311 *
 312 *      Adds new setup entry to the dev_boot_setup list.  The function
 313 *      returns 0 on error and 1 on success.  This is a generic routine to
 314 *      all netdevices.
 315 */
 316int netdev_boot_setup_add(char *name, struct ifmap *map)
 317{
 318        struct netdev_boot_setup *s;
 319        int i;
 320
 321        s = dev_boot_setup;
 322        for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
 323                if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
 324                        memset(s[i].name, 0, sizeof(s[i].name));
 325                        strcpy(s[i].name, name);
 326                        memcpy(&s[i].map, map, sizeof(s[i].map));
 327                        break;
 328                }
 329        }
 330
 331        if (i >= NETDEV_BOOT_SETUP_MAX)
 332                return 0;
 333
 334        return 1;
 335}
 336
 337/**
 338 *      netdev_boot_setup_check - check boot time settings
 339 *      @dev: the netdevice
 340 *
 341 *      Check boot time settings for the device.
 342 *      The found settings are set for the device to be used
 343 *      later in the device probing.
 344 *      Returns 0 if no settings found, 1 if they are.
 345 */
 346int netdev_boot_setup_check(struct net_device *dev)
 347{
 348        struct netdev_boot_setup *s;
 349        int i;
 350
 351        s = dev_boot_setup;
 352        for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
 353                if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
 354                    !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
 355                        dev->irq        = s[i].map.irq;
 356                        dev->base_addr  = s[i].map.base_addr;
 357                        dev->mem_start  = s[i].map.mem_start;
 358                        dev->mem_end    = s[i].map.mem_end;
 359                        return 1;
 360                }
 361        }
 362        return 0;
 363}
 364
 365/*
 366 * Saves at boot time configured settings for any netdevice.
 367 */
 368int __init netdev_boot_setup(char *str)
 369{
 370        int ints[5];
 371        struct ifmap map;
 372
 373        str = get_options(str, ARRAY_SIZE(ints), ints);
 374        if (!str || !*str)
 375                return 0;
 376
 377        /* Save settings */
 378        memset(&map, 0, sizeof(map));
 379        if (ints[0] > 0)
 380                map.irq = ints[1];
 381        if (ints[0] > 1)
 382                map.base_addr = ints[2];
 383        if (ints[0] > 2)
 384                map.mem_start = ints[3];
 385        if (ints[0] > 3)
 386                map.mem_end = ints[4];
 387
 388        /* Add new entry to the list */ 
 389        return netdev_boot_setup_add(str, &map);
 390}
 391
 392__setup("netdev=", netdev_boot_setup);
 393
 394/*****************************************************************************************
 395
 396                            Device Interface Subroutines
 397
 398******************************************************************************************/
 399
 400/**
 401 *      __dev_get_by_name       - find a device by its name 
 402 *      @name: name to find
 403 *
 404 *      Find an interface by name. Must be called under RTNL semaphore
 405 *      or @dev_base_lock. If the name is found a pointer to the device
 406 *      is returned. If the name is not found then %NULL is returned. The
 407 *      reference counters are not incremented so the caller must be
 408 *      careful with locks.
 409 */
 410 
 411
 412struct net_device *__dev_get_by_name(const char *name)
 413{
 414        struct net_device *dev;
 415
 416        for (dev = dev_base; dev != NULL; dev = dev->next) {
 417                if (strncmp(dev->name, name, IFNAMSIZ) == 0)
 418                        return dev;
 419        }
 420        return NULL;
 421}
 422
 423/**
 424 *      dev_get_by_name         - find a device by its name
 425 *      @name: name to find
 426 *
 427 *      Find an interface by name. This can be called from any 
 428 *      context and does its own locking. The returned handle has
 429 *      the usage count incremented and the caller must use dev_put() to
 430 *      release it when it is no longer needed. %NULL is returned if no
 431 *      matching device is found.
 432 */
 433
 434struct net_device *dev_get_by_name(const char *name)
 435{
 436        struct net_device *dev;
 437
 438        read_lock(&dev_base_lock);
 439        dev = __dev_get_by_name(name);
 440        if (dev)
 441                dev_hold(dev);
 442        read_unlock(&dev_base_lock);
 443        return dev;
 444}
 445
 446/* 
 447   Return value is changed to int to prevent illegal usage in future.
 448   It is still legal to use to check for device existence.
 449
 450   User should understand, that the result returned by this function
 451   is meaningless, if it was not issued under rtnl semaphore.
 452 */
 453
 454/**
 455 *      dev_get -       test if a device exists
 456 *      @name:  name to test for
 457 *
 458 *      Test if a name exists. Returns true if the name is found. In order
 459 *      to be sure the name is not allocated or removed during the test the
 460 *      caller must hold the rtnl semaphore.
 461 *
 462 *      This function primarily exists for back compatibility with older
 463 *      drivers. 
 464 */
 465 
 466int dev_get(const char *name)
 467{
 468        struct net_device *dev;
 469
 470        read_lock(&dev_base_lock);
 471        dev = __dev_get_by_name(name);
 472        read_unlock(&dev_base_lock);
 473        return dev != NULL;
 474}
 475
 476/**
 477 *      __dev_get_by_index - find a device by its ifindex
 478 *      @ifindex: index of device
 479 *
 480 *      Search for an interface by index. Returns %NULL if the device
 481 *      is not found or a pointer to the device. The device has not
 482 *      had its reference counter increased so the caller must be careful
 483 *      about locking. The caller must hold either the RTNL semaphore
 484 *      or @dev_base_lock.
 485 */
 486
 487struct net_device * __dev_get_by_index(int ifindex)
 488{
 489        struct net_device *dev;
 490
 491        for (dev = dev_base; dev != NULL; dev = dev->next) {
 492                if (dev->ifindex == ifindex)
 493                        return dev;
 494        }
 495        return NULL;
 496}
 497
 498
 499/**
 500 *      dev_get_by_index - find a device by its ifindex
 501 *      @ifindex: index of device
 502 *
 503 *      Search for an interface by index. Returns NULL if the device
 504 *      is not found or a pointer to the device. The device returned has 
 505 *      had a reference added and the pointer is safe until the user calls
 506 *      dev_put to indicate they have finished with it.
 507 */
 508
 509struct net_device * dev_get_by_index(int ifindex)
 510{
 511        struct net_device *dev;
 512
 513        read_lock(&dev_base_lock);
 514        dev = __dev_get_by_index(ifindex);
 515        if (dev)
 516                dev_hold(dev);
 517        read_unlock(&dev_base_lock);
 518        return dev;
 519}
 520
 521/**
 522 *      dev_getbyhwaddr - find a device by its hardware address
 523 *      @type: media type of device
 524 *      @ha: hardware address
 525 *
 526 *      Search for an interface by MAC address. Returns NULL if the device
 527 *      is not found or a pointer to the device. The caller must hold the
 528 *      rtnl semaphore. The returned device has not had its ref count increased
 529 *      and the caller must therefore be careful about locking
 530 *
 531 *      BUGS:
 532 *      If the API was consistent this would be __dev_get_by_hwaddr
 533 */
 534
 535struct net_device *dev_getbyhwaddr(unsigned short type, char *ha)
 536{
 537        struct net_device *dev;
 538
 539        ASSERT_RTNL();
 540
 541        for (dev = dev_base; dev != NULL; dev = dev->next) {
 542                if (dev->type == type &&
 543                    memcmp(dev->dev_addr, ha, dev->addr_len) == 0)
 544                        return dev;
 545        }
 546        return NULL;
 547}
 548
 549/**
 550 *      dev_alloc_name - allocate a name for a device
 551 *      @dev: device 
 552 *      @name: name format string
 553 *
 554 *      Passed a format string - eg "lt%d" it will try and find a suitable
 555 *      id. Not efficient for many devices, not called a lot. The caller
 556 *      must hold the dev_base or rtnl lock while allocating the name and
 557 *      adding the device in order to avoid duplicates. Returns the number
 558 *      of the unit assigned or a negative errno code.
 559 */
 560
 561int dev_alloc_name(struct net_device *dev, const char *name)
 562{
 563        int i;
 564        char buf[32];
 565        char *p;
 566
 567        /*
 568         * Verify the string as this thing may have come from
 569         * the user.  There must be either one "%d" and no other "%"
 570         * characters, or no "%" characters at all.
 571         */
 572        p = strchr(name, '%');
 573        if (p && (p[1] != 'd' || strchr(p+2, '%')))
 574                return -EINVAL;
 575
 576        /*
 577         * If you need over 100 please also fix the algorithm...
 578         */
 579        for (i = 0; i < 100; i++) {
 580                snprintf(buf,sizeof(buf),name,i);
 581                if (__dev_get_by_name(buf) == NULL) {
 582                        strcpy(dev->name, buf);
 583                        return i;
 584                }
 585        }
 586        return -ENFILE; /* Over 100 of the things .. bail out! */
 587}
 588
 589/**
 590 *      dev_alloc - allocate a network device and name
 591 *      @name: name format string
 592 *      @err: error return pointer
 593 *
 594 *      Passed a format string, eg. "lt%d", it will allocate a network device
 595 *      and space for the name. %NULL is returned if no memory is available.
 596 *      If the allocation succeeds then the name is assigned and the 
 597 *      device pointer returned. %NULL is returned if the name allocation
 598 *      failed. The cause of an error is returned as a negative errno code
 599 *      in the variable @err points to.
 600 *
 601 *      The caller must hold the @dev_base or RTNL locks when doing this in
 602 *      order to avoid duplicate name allocations.
 603 */
 604
 605struct net_device *dev_alloc(const char *name, int *err)
 606{
 607        struct net_device *dev=kmalloc(sizeof(struct net_device), GFP_KERNEL);
 608        if (dev == NULL) {
 609                *err = -ENOBUFS;
 610                return NULL;
 611        }
 612        memset(dev, 0, sizeof(struct net_device));
 613        *err = dev_alloc_name(dev, name);
 614        if (*err < 0) {
 615                kfree(dev);
 616                return NULL;
 617        }
 618        return dev;
 619}
 620
 621/**
 622 *      netdev_state_change - device changes state
 623 *      @dev: device to cause notification
 624 *
 625 *      Called to indicate a device has changed state. This function calls
 626 *      the notifier chains for netdev_chain and sends a NEWLINK message
 627 *      to the routing socket.
 628 */
 629 
 630void netdev_state_change(struct net_device *dev)
 631{
 632        if (dev->flags&IFF_UP) {
 633                notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
 634                rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
 635        }
 636}
 637
 638
 639#ifdef CONFIG_KMOD
 640
 641/**
 642 *      dev_load        - load a network module
 643 *      @name: name of interface
 644 *
 645 *      If a network interface is not present and the process has suitable
 646 *      privileges this function loads the module. If module loading is not
 647 *      available in this kernel then it becomes a nop.
 648 */
 649
 650void dev_load(const char *name)
 651{
 652        if (!dev_get(name) && capable(CAP_SYS_MODULE))
 653                request_module(name);
 654}
 655
 656#else
 657
 658extern inline void dev_load(const char *unused){;}
 659
 660#endif
 661
 662static int default_rebuild_header(struct sk_buff *skb)
 663{
 664        printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n", skb->dev ? skb->dev->name : "NULL!!!");
 665        kfree_skb(skb);
 666        return 1;
 667}
 668
 669/**
 670 *      dev_open        - prepare an interface for use. 
 671 *      @dev:   device to open
 672 *
 673 *      Takes a device from down to up state. The device's private open
 674 *      function is invoked and then the multicast lists are loaded. Finally
 675 *      the device is moved into the up state and a %NETDEV_UP message is
 676 *      sent to the netdev notifier chain.
 677 *
 678 *      Calling this function on an active interface is a nop. On a failure
 679 *      a negative errno code is returned.
 680 */
 681 
 682int dev_open(struct net_device *dev)
 683{
 684        int ret = 0;
 685
 686        /*
 687         *      Is it already up?
 688         */
 689
 690        if (dev->flags&IFF_UP)
 691                return 0;
 692
 693        /*
 694         *      Is it even present?
 695         */
 696        if (!netif_device_present(dev))
 697                return -ENODEV;
 698
 699        /*
 700         *      Call device private open method
 701         */
 702        if (try_inc_mod_count(dev->owner)) {
 703                set_bit(__LINK_STATE_START, &dev->state);
 704                if (dev->open) {
 705                        ret = dev->open(dev);
 706                        if (ret != 0) {
 707                                clear_bit(__LINK_STATE_START, &dev->state);
 708                                if (dev->owner)
 709                                        __MOD_DEC_USE_COUNT(dev->owner);
 710                        }
 711                }
 712        } else {
 713                ret = -ENODEV;
 714        }
 715
 716        /*
 717         *      If it went open OK then:
 718         */
 719         
 720        if (ret == 0) 
 721        {
 722                /*
 723                 *      Set the flags.
 724                 */
 725                dev->flags |= IFF_UP;
 726
 727                /*
 728                 *      Initialize multicasting status 
 729                 */
 730                dev_mc_upload(dev);
 731
 732                /*
 733                 *      Wakeup transmit queue engine
 734                 */
 735                dev_activate(dev);
 736
 737                /*
 738                 *      ... and announce new interface.
 739                 */
 740                notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
 741        }
 742        return(ret);
 743}
 744
 745#ifdef CONFIG_NET_FASTROUTE
 746
 747static void dev_do_clear_fastroute(struct net_device *dev)
 748{
 749        if (dev->accept_fastpath) {
 750                int i;
 751
 752                for (i=0; i<=NETDEV_FASTROUTE_HMASK; i++) {
 753                        struct dst_entry *dst;
 754
 755                        write_lock_irq(&dev->fastpath_lock);
 756                        dst = dev->fastpath[i];
 757                        dev->fastpath[i] = NULL;
 758                        write_unlock_irq(&dev->fastpath_lock);
 759
 760                        dst_release(dst);
 761                }
 762        }
 763}
 764
 765void dev_clear_fastroute(struct net_device *dev)
 766{
 767        if (dev) {
 768                dev_do_clear_fastroute(dev);
 769        } else {
 770                read_lock(&dev_base_lock);
 771                for (dev = dev_base; dev; dev = dev->next)
 772                        dev_do_clear_fastroute(dev);
 773                read_unlock(&dev_base_lock);
 774        }
 775}
 776#endif
 777
 778/**
 779 *      dev_close - shutdown an interface.
 780 *      @dev: device to shutdown
 781 *
 782 *      This function moves an active device into down state. A 
 783 *      %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
 784 *      is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
 785 *      chain.
 786 */
 787 
 788int dev_close(struct net_device *dev)
 789{
 790        if (!(dev->flags&IFF_UP))
 791                return 0;
 792
 793        /*
 794         *      Tell people we are going down, so that they can
 795         *      prepare to death, when device is still operating.
 796         */
 797        notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
 798
 799        dev_deactivate(dev);
 800
 801        clear_bit(__LINK_STATE_START, &dev->state);
 802
 803        /* Synchronize to scheduled poll. We cannot touch poll list,
 804         * it can be even on different cpu. So just clear netif_running(),
 805         * and wait when poll really will happen. Actually, the best place
 806         * for this is inside dev->stop() after device stopped its irq
 807         * engine, but this requires more changes in devices. */
 808
 809        smp_mb__after_clear_bit(); /* Commit netif_running(). */
 810        while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) {
 811                /* No hurry. */
 812                current->state = TASK_INTERRUPTIBLE;
 813                schedule_timeout(1);
 814        }
 815
 816        /*
 817         *      Call the device specific close. This cannot fail.
 818         *      Only if device is UP
 819         *
 820         *      We allow it to be called even after a DETACH hot-plug
 821         *      event.
 822         */
 823         
 824        if (dev->stop)
 825                dev->stop(dev);
 826
 827        /*
 828         *      Device is now down.
 829         */
 830
 831        dev->flags &= ~IFF_UP;
 832#ifdef CONFIG_NET_FASTROUTE
 833        dev_clear_fastroute(dev);
 834#endif
 835
 836        /*
 837         *      Tell people we are down
 838         */
 839        notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
 840
 841        /*
 842         * Drop the module refcount
 843         */
 844        if (dev->owner)
 845                __MOD_DEC_USE_COUNT(dev->owner);
 846
 847        return(0);
 848}
 849
 850
 851/*
 852 *      Device change register/unregister. These are not inline or static
 853 *      as we export them to the world.
 854 */
 855 
 856/**
 857 *      register_netdevice_notifier - register a network notifier block
 858 *      @nb: notifier
 859 *
 860 *      Register a notifier to be called when network device events occur.
 861 *      The notifier passed is linked into the kernel structures and must
 862 *      not be reused until it has been unregistered. A negative errno code
 863 *      is returned on a failure.
 864 */
 865
 866int register_netdevice_notifier(struct notifier_block *nb)
 867{
 868        return notifier_chain_register(&netdev_chain, nb);
 869}
 870
 871/**
 872 *      unregister_netdevice_notifier - unregister a network notifier block
 873 *      @nb: notifier
 874 *
 875 *      Unregister a notifier previously registered by
 876 *      register_netdevice_notifier(). The notifier is unlinked into the
 877 *      kernel structures and may then be reused. A negative errno code
 878 *      is returned on a failure.
 879 */
 880
 881int unregister_netdevice_notifier(struct notifier_block *nb)
 882{
 883        return notifier_chain_unregister(&netdev_chain,nb);
 884}
 885
 886/*
 887 *      Support routine. Sends outgoing frames to any network
 888 *      taps currently in use.
 889 */
 890
 891void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
 892{
 893        struct packet_type *ptype;
 894        do_gettimeofday(&skb->stamp);
 895
 896        br_read_lock(BR_NETPROTO_LOCK);
 897        for (ptype = ptype_all; ptype!=NULL; ptype = ptype->next) 
 898        {
 899                /* Never send packets back to the socket
 900                 * they originated from - MvS (miquels@drinkel.ow.org)
 901                 */
 902                if ((ptype->dev == dev || !ptype->dev) &&
 903                        ((struct sock *)ptype->data != skb->sk))
 904                {
 905                        struct sk_buff *skb2;
 906                        if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL)
 907                                break;
 908
 909                        /* skb->nh should be correctly
 910                           set by sender, so that the second statement is
 911                           just protection against buggy protocols.
 912                         */
 913                        skb2->mac.raw = skb2->data;
 914
 915                        if (skb2->nh.raw < skb2->data || skb2->nh.raw > skb2->tail) {
 916                                if (net_ratelimit())
 917                                        printk(KERN_CRIT "protocol %04x is buggy, dev %s\n", skb2->protocol, dev->name);
 918                                skb2->nh.raw = skb2->data;
 919                        }
 920
 921                        skb2->h.raw = skb2->nh.raw;
 922                        skb2->pkt_type = PACKET_OUTGOING;
 923                        ptype->func(skb2, skb->dev, ptype);
 924                }
 925        }
 926        br_read_unlock(BR_NETPROTO_LOCK);
 927}
 928
 929/* Calculate csum in the case, when packet is misrouted.
 930 * If it failed by some reason, ignore and send skb with wrong
 931 * checksum.
 932 */
 933struct sk_buff * skb_checksum_help(struct sk_buff *skb)
 934{
 935        int offset;
 936        unsigned int csum;
 937
 938        offset = skb->h.raw - skb->data;
 939        if (offset > (int)skb->len)
 940                BUG();
 941        csum = skb_checksum(skb, offset, skb->len-offset, 0);
 942
 943        offset = skb->tail - skb->h.raw;
 944        if (offset <= 0)
 945                BUG();
 946        if (skb->csum+2 > offset)
 947                BUG();
 948
 949        *(u16*)(skb->h.raw + skb->csum) = csum_fold(csum);
 950        skb->ip_summed = CHECKSUM_NONE;
 951        return skb;
 952}
 953
 954#ifdef CONFIG_HIGHMEM
 955/* Actually, we should eliminate this check as soon as we know, that:
 956 * 1. IOMMU is present and allows to map all the memory.
 957 * 2. No high memory really exists on this machine.
 958 */
 959
 960static inline int
 961illegal_highdma(struct net_device *dev, struct sk_buff *skb)
 962{
 963        int i;
 964
 965        if (dev->features&NETIF_F_HIGHDMA)
 966                return 0;
 967
 968        for (i=0; i<skb_shinfo(skb)->nr_frags; i++)
 969                if (skb_shinfo(skb)->frags[i].page >= highmem_start_page)
 970                        return 1;
 971
 972        return 0;
 973}
 974#else
 975#define illegal_highdma(dev, skb)       (0)
 976#endif
 977
 978/**
 979 *      dev_queue_xmit - transmit a buffer
 980 *      @skb: buffer to transmit
 981 *      
 982 *      Queue a buffer for transmission to a network device. The caller must
 983 *      have set the device and priority and built the buffer before calling this 
 984 *      function. The function can be called from an interrupt.
 985 *
 986 *      A negative errno code is returned on a failure. A success does not
 987 *      guarantee the frame will be transmitted as it may be dropped due
 988 *      to congestion or traffic shaping.
 989 */
 990
 991int dev_queue_xmit(struct sk_buff *skb)
 992{
 993        struct net_device *dev = skb->dev;
 994        struct Qdisc  *q;
 995
 996        if (skb_shinfo(skb)->frag_list &&
 997            !(dev->features&NETIF_F_FRAGLIST) &&
 998            skb_linearize(skb, GFP_ATOMIC) != 0) {
 999                kfree_skb(skb);
1000                return -ENOMEM;
1001        }
1002
1003        /* Fragmented skb is linearized if device does not support SG,
1004         * or if at least one of fragments is in highmem and device
1005         * does not support DMA from it.
1006         */
1007        if (skb_shinfo(skb)->nr_frags &&
1008            (!(dev->features&NETIF_F_SG) || illegal_highdma(dev, skb)) &&
1009            skb_linearize(skb, GFP_ATOMIC) != 0) {
1010                kfree_skb(skb);
1011                return -ENOMEM;
1012        }
1013
1014        /* If packet is not checksummed and device does not support
1015         * checksumming for this protocol, complete checksumming here.
1016         */
1017        if (skb->ip_summed == CHECKSUM_HW &&
1018            (!(dev->features&(NETIF_F_HW_CSUM|NETIF_F_NO_CSUM)) &&
1019             (!(dev->features&NETIF_F_IP_CSUM) ||
1020              skb->protocol != htons(ETH_P_IP)))) {
1021                if ((skb = skb_checksum_help(skb)) == NULL)
1022                        return -ENOMEM;
1023        }
1024
1025        /* Grab device queue */
1026        spin_lock_bh(&dev->queue_lock);
1027        q = dev->qdisc;
1028        if (q->enqueue) {
1029                int ret = q->enqueue(skb, q);
1030
1031                qdisc_run(dev);
1032
1033                spin_unlock_bh(&dev->queue_lock);
1034                return ret == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : ret;
1035        }
1036
1037        /* The device has no queue. Common case for software devices:
1038           loopback, all the sorts of tunnels...
1039
1040           Really, it is unlikely that xmit_lock protection is necessary here.
1041           (f.e. loopback and IP tunnels are clean ignoring statistics counters.)
1042           However, it is possible, that they rely on protection
1043           made by us here.
1044
1045           Check this and shot the lock. It is not prone from deadlocks.
1046           Either shot noqueue qdisc, it is even simpler 8)
1047         */
1048        if (dev->flags&IFF_UP) {
1049                int cpu = smp_processor_id();
1050
1051                if (dev->xmit_lock_owner != cpu) {
1052                        spin_unlock(&dev->queue_lock);
1053                        spin_lock(&dev->xmit_lock);
1054                        dev->xmit_lock_owner = cpu;
1055
1056                        if (!netif_queue_stopped(dev)) {
1057                                if (netdev_nit)
1058                                        dev_queue_xmit_nit(skb,dev);
1059
1060                                if (dev->hard_start_xmit(skb, dev) == 0) {
1061                                        dev->xmit_lock_owner = -1;
1062                                        spin_unlock_bh(&dev->xmit_lock);
1063                                        return 0;
1064                                }
1065                        }
1066                        dev->xmit_lock_owner = -1;
1067                        spin_unlock_bh(&dev->xmit_lock);
1068                        if (net_ratelimit())
1069                                printk(KERN_CRIT "Virtual device %s asks to queue packet!\n", dev->name);
1070                        kfree_skb(skb);
1071                        return -ENETDOWN;
1072                } else {
1073                        /* Recursion is detected! It is possible, unfortunately */
1074                        if (net_ratelimit())
1075                                printk(KERN_CRIT "Dead loop on virtual device %s, fix it urgently!\n", dev->name);
1076                }
1077        }
1078        spin_unlock_bh(&dev->queue_lock);
1079
1080        kfree_skb(skb);
1081        return -ENETDOWN;
1082}
1083
1084
1085/*=======================================================================
1086                        Receiver routines
1087  =======================================================================*/
1088
1089int netdev_max_backlog = 300;
1090int weight_p = 64;            /* old backlog weight */
1091/* These numbers are selected based on intuition and some
1092 * experimentatiom, if you have more scientific way of doing this
1093 * please go ahead and fix things.
1094 */
1095int no_cong_thresh = 10;
1096int no_cong = 20;
1097int lo_cong = 100;
1098int mod_cong = 290;
1099
1100struct netif_rx_stats netdev_rx_stat[NR_CPUS];
1101
1102
1103#ifdef CONFIG_NET_HW_FLOWCONTROL
1104atomic_t netdev_dropping = ATOMIC_INIT(0);
1105static unsigned long netdev_fc_mask = 1;
1106unsigned long netdev_fc_xoff = 0;
1107spinlock_t netdev_fc_lock = SPIN_LOCK_UNLOCKED;
1108
1109static struct
1110{
1111        void (*stimul)(struct net_device *);
1112        struct net_device *dev;
1113} netdev_fc_slots[BITS_PER_LONG];
1114
1115int netdev_register_fc(struct net_device *dev, void (*stimul)(struct net_device *dev))
1116{
1117        int bit = 0;
1118        unsigned long flags;
1119
1120        spin_lock_irqsave(&netdev_fc_lock, flags);
1121        if (netdev_fc_mask != ~0UL) {
1122                bit = ffz(netdev_fc_mask);
1123                netdev_fc_slots[bit].stimul = stimul;
1124                netdev_fc_slots[bit].dev = dev;
1125                set_bit(bit, &netdev_fc_mask);
1126                clear_bit(bit, &netdev_fc_xoff);
1127        }
1128        spin_unlock_irqrestore(&netdev_fc_lock, flags);
1129        return bit;
1130}
1131
1132void netdev_unregister_fc(int bit)
1133{
1134        unsigned long flags;
1135
1136        spin_lock_irqsave(&netdev_fc_lock, flags);
1137        if (bit > 0) {
1138                netdev_fc_slots[bit].stimul = NULL;
1139                netdev_fc_slots[bit].dev = NULL;
1140                clear_bit(bit, &netdev_fc_mask);
1141                clear_bit(bit, &netdev_fc_xoff);
1142        }
1143        spin_unlock_irqrestore(&netdev_fc_lock, flags);
1144}
1145
1146static void netdev_wakeup(void)
1147{
1148        unsigned long xoff;
1149
1150        spin_lock(&netdev_fc_lock);
1151        xoff = netdev_fc_xoff;
1152        netdev_fc_xoff = 0;
1153        while (xoff) {
1154                int i = ffz(~xoff);
1155                xoff &= ~(1<<i);
1156                netdev_fc_slots[i].stimul(netdev_fc_slots[i].dev);
1157        }
1158        spin_unlock(&netdev_fc_lock);
1159}
1160#endif
1161
1162static void get_sample_stats(int cpu)
1163{
1164#ifdef RAND_LIE
1165        unsigned long rd;
1166        int rq;
1167#endif
1168        int blog = softnet_data[cpu].input_pkt_queue.qlen;
1169        int avg_blog = softnet_data[cpu].avg_blog;
1170
1171        avg_blog = (avg_blog >> 1)+ (blog >> 1);
1172
1173        if (avg_blog > mod_cong) {
1174                /* Above moderate congestion levels. */
1175                softnet_data[cpu].cng_level = NET_RX_CN_HIGH;
1176#ifdef RAND_LIE
1177                rd = net_random();
1178                rq = rd % netdev_max_backlog;
1179                if (rq < avg_blog) /* unlucky bastard */
1180                        softnet_data[cpu].cng_level = NET_RX_DROP;
1181#endif
1182        } else if (avg_blog > lo_cong) {
1183                softnet_data[cpu].cng_level = NET_RX_CN_MOD;
1184#ifdef RAND_LIE
1185                rd = net_random();
1186                rq = rd % netdev_max_backlog;
1187                        if (rq < avg_blog) /* unlucky bastard */
1188                                softnet_data[cpu].cng_level = NET_RX_CN_HIGH;
1189#endif
1190        } else if (avg_blog > no_cong) 
1191                softnet_data[cpu].cng_level = NET_RX_CN_LOW;
1192        else  /* no congestion */
1193                softnet_data[cpu].cng_level = NET_RX_SUCCESS;
1194
1195        softnet_data[cpu].avg_blog = avg_blog;
1196}
1197
1198#ifdef OFFLINE_SAMPLE
1199static void sample_queue(unsigned long dummy)
1200{
1201/* 10 ms 0r 1ms -- i dont care -- JHS */
1202        int next_tick = 1;
1203        int cpu = smp_processor_id();
1204
1205        get_sample_stats(cpu);
1206        next_tick += jiffies;
1207        mod_timer(&samp_timer, next_tick);
1208}
1209#endif
1210
1211
1212/**
1213 *      netif_rx        -       post buffer to the network code
1214 *      @skb: buffer to post
1215 *
1216 *      This function receives a packet from a device driver and queues it for
1217 *      the upper (protocol) levels to process.  It always succeeds. The buffer
1218 *      may be dropped during processing for congestion control or by the 
1219 *      protocol layers.
1220 *      
1221 *      return values:
1222 *      NET_RX_SUCCESS  (no congestion)           
1223 *      NET_RX_CN_LOW     (low congestion) 
1224 *      NET_RX_CN_MOD     (moderate congestion)
1225 *      NET_RX_CN_HIGH    (high congestion) 
1226 *      NET_RX_DROP    (packet was dropped)
1227 *      
1228 *      
1229 */
1230
1231int netif_rx(struct sk_buff *skb)
1232{
1233        int this_cpu = smp_processor_id();
1234        struct softnet_data *queue;
1235        unsigned long flags;
1236
1237        if (skb->stamp.tv_sec == 0)
1238                do_gettimeofday(&skb->stamp);
1239
1240        /* The code is rearranged so that the path is the most
1241           short when CPU is congested, but is still operating.
1242         */
1243        queue = &softnet_data[this_cpu];
1244
1245        local_irq_save(flags);
1246
1247        netdev_rx_stat[this_cpu].total++;
1248        if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1249                if (queue->input_pkt_queue.qlen) {
1250                        if (queue->throttle)
1251                                goto drop;
1252
1253enqueue:
1254                        dev_hold(skb->dev);
1255                        __skb_queue_tail(&queue->input_pkt_queue,skb);
1256                        local_irq_restore(flags);
1257#ifndef OFFLINE_SAMPLE
1258                        get_sample_stats(this_cpu);
1259#endif
1260                        return queue->cng_level;
1261                }
1262
1263                if (queue->throttle) {
1264                        queue->throttle = 0;
1265#ifdef CONFIG_NET_HW_FLOWCONTROL
1266                        if (atomic_dec_and_test(&netdev_dropping))
1267                                netdev_wakeup();
1268#endif
1269                }
1270
1271                netif_rx_schedule(&queue->blog_dev);
1272                goto enqueue;
1273        }
1274
1275        if (queue->throttle == 0) {
1276                queue->throttle = 1;
1277                netdev_rx_stat[this_cpu].throttled++;
1278#ifdef CONFIG_NET_HW_FLOWCONTROL
1279                atomic_inc(&netdev_dropping);
1280#endif
1281        }
1282
1283drop:
1284        netdev_rx_stat[this_cpu].dropped++;
1285        local_irq_restore(flags);
1286
1287        kfree_skb(skb);
1288        return NET_RX_DROP;
1289}
1290
1291/* Deliver skb to an old protocol, which is not threaded well
1292   or which do not understand shared skbs.
1293 */
1294static int deliver_to_old_ones(struct packet_type *pt, struct sk_buff *skb, int last)
1295{
1296        static spinlock_t net_bh_lock = SPIN_LOCK_UNLOCKED;
1297        int ret = NET_RX_DROP;
1298
1299
1300        if (!last) {
1301                skb = skb_clone(skb, GFP_ATOMIC);
1302                if (skb == NULL)
1303                        return ret;
1304        }
1305        if (skb_is_nonlinear(skb) && skb_linearize(skb, GFP_ATOMIC) != 0) {
1306                kfree_skb(skb);
1307                return ret;
1308        }
1309
1310        /* The assumption (correct one) is that old protocols
1311           did not depened on BHs different of NET_BH and TIMER_BH.
1312         */
1313
1314        /* Emulate NET_BH with special spinlock */
1315        spin_lock(&net_bh_lock);
1316
1317        /* Disable timers and wait for all timers completion */
1318        tasklet_disable(bh_task_vec+TIMER_BH);
1319
1320        ret = pt->func(skb, skb->dev, pt);
1321
1322        tasklet_hi_enable(bh_task_vec+TIMER_BH);
1323        spin_unlock(&net_bh_lock);
1324        return ret;
1325}
1326
1327static __inline__ void skb_bond(struct sk_buff *skb)
1328{
1329        struct net_device *dev = skb->dev;
1330
1331        if (dev->master)
1332                skb->dev = dev->master;
1333}
1334
1335static void net_tx_action(struct softirq_action *h)
1336{
1337        int cpu = smp_processor_id();
1338
1339        if (softnet_data[cpu].completion_queue) {
1340                struct sk_buff *clist;
1341
1342                local_irq_disable();
1343                clist = softnet_data[cpu].completion_queue;
1344                softnet_data[cpu].completion_queue = NULL;
1345                local_irq_enable();
1346
1347                while (clist != NULL) {
1348                        struct sk_buff *skb = clist;
1349                        clist = clist->next;
1350
1351                        BUG_TRAP(atomic_read(&skb->users) == 0);
1352                        __kfree_skb(skb);
1353                }
1354        }
1355
1356        if (softnet_data[cpu].output_queue) {
1357                struct net_device *head;
1358
1359                local_irq_disable();
1360                head = softnet_data[cpu].output_queue;
1361                softnet_data[cpu].output_queue = NULL;
1362                local_irq_enable();
1363
1364                while (head != NULL) {
1365                        struct net_device *dev = head;
1366                        head = head->next_sched;
1367
1368                        smp_mb__before_clear_bit();
1369                        clear_bit(__LINK_STATE_SCHED, &dev->state);
1370
1371                        if (spin_trylock(&dev->queue_lock)) {
1372                                qdisc_run(dev);
1373                                spin_unlock(&dev->queue_lock);
1374                        } else {
1375                                netif_schedule(dev);
1376                        }
1377                }
1378        }
1379}
1380
1381
1382#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
1383void (*br_handle_frame_hook)(struct sk_buff *skb) = NULL;
1384#endif
1385
1386static __inline__ int handle_bridge(struct sk_buff *skb,
1387                                     struct packet_type *pt_prev)
1388{
1389        int ret = NET_RX_DROP;
1390
1391        if (pt_prev) {
1392                if (!pt_prev->data)
1393                        ret = deliver_to_old_ones(pt_prev, skb, 0);
1394                else {
1395                        atomic_inc(&skb->users);
1396                        ret = pt_prev->func(skb, skb->dev, pt_prev);
1397                }
1398        }
1399
1400        br_handle_frame_hook(skb);
1401        return ret;
1402}
1403
1404
1405#ifdef CONFIG_NET_DIVERT
1406static inline int handle_diverter(struct sk_buff *skb)
1407{
1408        /* if diversion is supported on device, then divert */
1409        if (skb->dev->divert && skb->dev->divert->divert)
1410                divert_frame(skb);
1411        return 0;
1412}
1413#endif   /* CONFIG_NET_DIVERT */
1414
1415int netif_receive_skb(struct sk_buff *skb)
1416{
1417        struct packet_type *ptype, *pt_prev;
1418        int ret = NET_RX_DROP;
1419        unsigned short type = skb->protocol;
1420
1421        if (skb->stamp.tv_sec == 0)
1422                do_gettimeofday(&skb->stamp);
1423
1424        skb_bond(skb);
1425
1426        netdev_rx_stat[smp_processor_id()].total++;
1427
1428#ifdef CONFIG_NET_FASTROUTE
1429        if (skb->pkt_type == PACKET_FASTROUTE) {
1430                netdev_rx_stat[smp_processor_id()].fastroute_deferred_out++;
1431                return dev_queue_xmit(skb);
1432        }
1433#endif
1434
1435        skb->h.raw = skb->nh.raw = skb->data;
1436
1437        pt_prev = NULL;
1438        for (ptype = ptype_all; ptype; ptype = ptype->next) {
1439                if (!ptype->dev || ptype->dev == skb->dev) {
1440                        if (pt_prev) {
1441                                if (!pt_prev->data) {
1442                                        ret = deliver_to_old_ones(pt_prev, skb, 0);
1443                                } else {
1444                                        atomic_inc(&skb->users);
1445                                        ret = pt_prev->func(skb, skb->dev, pt_prev);
1446                                }
1447                        }
1448                        pt_prev = ptype;
1449                }
1450        }
1451
1452#ifdef CONFIG_NET_DIVERT
1453        if (skb->dev->divert && skb->dev->divert->divert)
1454                ret = handle_diverter(skb);
1455#endif /* CONFIG_NET_DIVERT */
1456                        
1457#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
1458        if (skb->dev->br_port != NULL &&
1459            br_handle_frame_hook != NULL) {
1460                return handle_bridge(skb, pt_prev);
1461        }
1462#endif
1463
1464        for (ptype=ptype_base[ntohs(type)&15];ptype;ptype=ptype->next) {
1465                if (ptype->type == type &&
1466                    (!ptype->dev || ptype->dev == skb->dev)) {
1467                        if (pt_prev) {
1468                                if (!pt_prev->data) {
1469                                        ret = deliver_to_old_ones(pt_prev, skb, 0);
1470                                } else {
1471                                        atomic_inc(&skb->users);
1472                                        ret = pt_prev->func(skb, skb->dev, pt_prev);
1473                                }
1474                        }
1475                        pt_prev = ptype;
1476                }
1477        }
1478
1479        if (pt_prev) {
1480                if (!pt_prev->data) {
1481                        ret = deliver_to_old_ones(pt_prev, skb, 1);
1482                } else {
1483                        ret = pt_prev->func(skb, skb->dev, pt_prev);
1484                }
1485        } else {
1486                kfree_skb(skb);
1487                /* Jamal, now you will not able to escape explaining
1488                 * me how you were going to use this. :-)
1489                 */
1490                ret = NET_RX_DROP;
1491        }
1492
1493        return ret;
1494}
1495
1496static int process_backlog(struct net_device *blog_dev, int *budget)
1497{
1498        int work = 0;
1499        int quota = min(blog_dev->quota, *budget);
1500        int this_cpu = smp_processor_id();
1501        struct softnet_data *queue = &softnet_data[this_cpu];
1502        unsigned long start_time = jiffies;
1503
1504        for (;;) {
1505                struct sk_buff *skb;
1506                struct net_device *dev;
1507
1508                local_irq_disable();
1509                skb = __skb_dequeue(&queue->input_pkt_queue);
1510                if (skb == NULL)
1511                        goto job_done;
1512                local_irq_enable();
1513
1514                dev = skb->dev;
1515
1516                netif_receive_skb(skb);
1517
1518                dev_put(dev);
1519
1520                work++;
1521
1522                if (work >= quota || jiffies - start_time > 1)
1523                        break;
1524
1525#ifdef CONFIG_NET_HW_FLOWCONTROL
1526                if (queue->throttle && queue->input_pkt_queue.qlen < no_cong_thresh ) {
1527                        if (atomic_dec_and_test(&netdev_dropping)) {
1528                                queue->throttle = 0;
1529                                netdev_wakeup();
1530                                break;
1531                        }
1532                }
1533#endif
1534        }
1535
1536        blog_dev->quota -= work;
1537        *budget -= work;
1538        return -1;
1539
1540job_done:
1541        blog_dev->quota -= work;
1542        *budget -= work;
1543
1544        list_del(&blog_dev->poll_list);
1545        clear_bit(__LINK_STATE_RX_SCHED, &blog_dev->state);
1546
1547        if (queue->throttle) {
1548                queue->throttle = 0;
1549#ifdef CONFIG_NET_HW_FLOWCONTROL
1550                if (atomic_dec_and_test(&netdev_dropping))
1551                        netdev_wakeup();
1552#endif
1553        }
1554        local_irq_enable();
1555        return 0;
1556}
1557
1558static void net_rx_action(struct softirq_action *h)
1559{
1560        int this_cpu = smp_processor_id();
1561        struct softnet_data *queue = &softnet_data[this_cpu];
1562        unsigned long start_time = jiffies;
1563        int budget = netdev_max_backlog;
1564
1565        br_read_lock(BR_NETPROTO_LOCK);
1566        local_irq_disable();
1567
1568        while (!list_empty(&queue->poll_list)) {
1569                struct net_device *dev;
1570
1571                if (budget <= 0 || jiffies - start_time > 1)
1572                        goto softnet_break;
1573
1574                local_irq_enable();
1575
1576                dev = list_entry(queue->poll_list.next, struct net_device, poll_list);
1577
1578                if (dev->quota <= 0 || dev->poll(dev, &budget)) {
1579                        local_irq_disable();
1580                        list_del(&dev->poll_list);
1581                        list_add_tail(&dev->poll_list, &queue->poll_list);
1582                        if (dev->quota < 0)
1583                                dev->quota += dev->weight;
1584                        else
1585                                dev->quota = dev->weight;
1586                } else {
1587                        dev_put(dev);
1588                        local_irq_disable();
1589                }
1590        }
1591
1592        local_irq_enable();
1593        br_read_unlock(BR_NETPROTO_LOCK);
1594        return;
1595
1596softnet_break:
1597        netdev_rx_stat[this_cpu].time_squeeze++;
1598        __cpu_raise_softirq(this_cpu, NET_RX_SOFTIRQ);
1599
1600        local_irq_enable();
1601        br_read_unlock(BR_NETPROTO_LOCK);
1602}
1603
1604static gifconf_func_t * gifconf_list [NPROTO];
1605
1606/**
1607 *      register_gifconf        -       register a SIOCGIF handler
1608 *      @family: Address family
1609 *      @gifconf: Function handler
1610 *
1611 *      Register protocol dependent address dumping routines. The handler
1612 *      that is passed must not be freed or reused until it has been replaced
1613 *      by another handler.
1614 */
1615 
1616int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
1617{
1618        if (family>=NPROTO)
1619                return -EINVAL;
1620        gifconf_list[family] = gifconf;
1621        return 0;
1622}
1623
1624
1625/*
1626 *      Map an interface index to its name (SIOCGIFNAME)
1627 */
1628
1629/*
1630 *      We need this ioctl for efficient implementation of the
1631 *      if_indextoname() function required by the IPv6 API.  Without
1632 *      it, we would have to search all the interfaces to find a
1633 *      match.  --pb
1634 */
1635
1636static int dev_ifname(struct ifreq *arg)
1637{
1638        struct net_device *dev;
1639        struct ifreq ifr;
1640
1641        /*
1642         *      Fetch the caller's info block. 
1643         */
1644        
1645        if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
1646                return -EFAULT;
1647
1648        read_lock(&dev_base_lock);
1649        dev = __dev_get_by_index(ifr.ifr_ifindex);
1650        if (!dev) {
1651                read_unlock(&dev_base_lock);
1652                return -ENODEV;
1653        }
1654
1655        strcpy(ifr.ifr_name, dev->name);
1656        read_unlock(&dev_base_lock);
1657
1658        if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
1659                return -EFAULT;
1660        return 0;
1661}
1662
1663/*
1664 *      Perform a SIOCGIFCONF call. This structure will change
1665 *      size eventually, and there is nothing I can do about it.
1666 *      Thus we will need a 'compatibility mode'.
1667 */
1668
1669static int dev_ifconf(char *arg)
1670{
1671        struct ifconf ifc;
1672        struct net_device *dev;
1673        char *pos;
1674        int len;
1675        int total;
1676        int i;
1677
1678        /*
1679         *      Fetch the caller's info block. 
1680         */
1681        
1682        if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
1683                return -EFAULT;
1684
1685        pos = ifc.ifc_buf;
1686        len = ifc.ifc_len;
1687
1688        /*
1689         *      Loop over the interfaces, and write an info block for each. 
1690         */
1691
1692        total = 0;
1693        for (dev = dev_base; dev != NULL; dev = dev->next) {
1694                for (i=0; i<NPROTO; i++) {
1695                        if (gifconf_list[i]) {
1696                                int done;
1697                                if (pos==NULL) {
1698                                        done = gifconf_list[i](dev, NULL, 0);
1699                                } else {
1700                                        done = gifconf_list[i](dev, pos+total, len-total);
1701                                }
1702                                if (done<0) {
1703                                        return -EFAULT;
1704                                }
1705                                total += done;
1706                        }
1707                }
1708        }
1709
1710        /*
1711         *      All done.  Write the updated control block back to the caller. 
1712         */
1713        ifc.ifc_len = total;
1714
1715        if (copy_to_user(arg, &ifc, sizeof(struct ifconf)))
1716                return -EFAULT; 
1717
1718        /* 
1719         *      Both BSD and Solaris return 0 here, so we do too.
1720         */
1721        return 0;
1722}
1723
1724/*
1725 *      This is invoked by the /proc filesystem handler to display a device
1726 *      in detail.
1727 */
1728
1729#ifdef CONFIG_PROC_FS
1730
1731static int sprintf_stats(char *buffer, struct net_device *dev)
1732{
1733        struct net_device_stats *stats = (dev->get_stats ? dev->get_stats(dev): NULL);
1734        int size;
1735        
1736        if (stats)
1737                size = sprintf(buffer, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu %8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
1738                   dev->name,
1739                   stats->rx_bytes,
1740                   stats->rx_packets, stats->rx_errors,
1741                   stats->rx_dropped + stats->rx_missed_errors,
1742                   stats->rx_fifo_errors,
1743                   stats->rx_length_errors + stats->rx_over_errors
1744                   + stats->rx_crc_errors + stats->rx_frame_errors,
1745                   stats->rx_compressed, stats->multicast,
1746                   stats->tx_bytes,
1747                   stats->tx_packets, stats->tx_errors, stats->tx_dropped,
1748                   stats->tx_fifo_errors, stats->collisions,
1749                   stats->tx_carrier_errors + stats->tx_aborted_errors
1750                   + stats->tx_window_errors + stats->tx_heartbeat_errors,
1751                   stats->tx_compressed);
1752        else
1753                size = sprintf(buffer, "%6s: No statistics available.\n", dev->name);
1754
1755        return size;
1756}
1757
1758/*
1759 *      Called from the PROCfs module. This now uses the new arbitrary sized /proc/net interface
1760 *      to create /proc/net/dev
1761 */
1762 
1763static int dev_get_info(char *buffer, char **start, off_t offset, int length)
1764{
1765        int len = 0;
1766        off_t begin = 0;
1767        off_t pos = 0;
1768        int size;
1769        struct net_device *dev;
1770
1771
1772        size = sprintf(buffer, 
1773                "Inter-|   Receive                                                |  Transmit\n"
1774                " face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed\n");
1775        
1776        pos += size;
1777        len += size;
1778        
1779
1780        read_lock(&dev_base_lock);
1781        for (dev = dev_base; dev != NULL; dev = dev->next) {
1782                size = sprintf_stats(buffer+len, dev);
1783                len += size;
1784                pos = begin + len;
1785                                
1786                if (pos < offset) {
1787                        len = 0;
1788                        begin = pos;
1789                }
1790                if (pos > offset + length)
1791                        break;
1792        }
1793        read_unlock(&dev_base_lock);
1794
1795        *start = buffer + (offset - begin);     /* Start of wanted data */
1796        len -= (offset - begin);                /* Start slop */
1797        if (len > length)
1798                len = length;                   /* Ending slop */
1799        if (len < 0)
1800                len = 0;
1801        return len;
1802}
1803
1804static int dev_proc_stats(char *buffer, char **start, off_t offset,
1805                          int length, int *eof, void *data)
1806{
1807        int i, lcpu;
1808        int len=0;
1809
1810        for (lcpu=0; lcpu<smp_num_cpus; lcpu++) {
1811                i = cpu_logical_map(lcpu);
1812                len += sprintf(buffer+len, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
1813                               netdev_rx_stat[i].total,
1814                               netdev_rx_stat[i].dropped,
1815                               netdev_rx_stat[i].time_squeeze,
1816                               netdev_rx_stat[i].throttled,
1817                               netdev_rx_stat[i].fastroute_hit,
1818                               netdev_rx_stat[i].fastroute_success,
1819                               netdev_rx_stat[i].fastroute_defer,
1820                               netdev_rx_stat[i].fastroute_deferred_out,
1821#if 0
1822                               netdev_rx_stat[i].fastroute_latency_reduction
1823#else
1824                               netdev_rx_stat[i].cpu_collision
1825#endif
1826                               );
1827        }
1828
1829        len -= offset;
1830
1831        if (len > length)
1832                len = length;
1833        if (len < 0)
1834                len = 0;
1835
1836        *start = buffer + offset;
1837        *eof = 1;
1838
1839        return len;
1840}
1841
1842#endif  /* CONFIG_PROC_FS */
1843
1844
1845/**
1846 *      netdev_set_master       -       set up master/slave pair
1847 *      @slave: slave device
1848 *      @master: new master device
1849 *
1850 *      Changes the master device of the slave. Pass %NULL to break the
1851 *      bonding. The caller must hold the RTNL semaphore. On a failure
1852 *      a negative errno code is returned. On success the reference counts
1853 *      are adjusted, %RTM_NEWLINK is sent to the routing socket and the
1854 *      function returns zero.
1855 */
1856 
1857int netdev_set_master(struct net_device *slave, struct net_device *master)
1858{
1859        struct net_device *old = slave->master;
1860
1861        ASSERT_RTNL();
1862
1863        if (master) {
1864                if (old)
1865                        return -EBUSY;
1866                dev_hold(master);
1867        }
1868
1869        br_write_lock_bh(BR_NETPROTO_LOCK);
1870        slave->master = master;
1871        br_write_unlock_bh(BR_NETPROTO_LOCK);
1872
1873        if (old)
1874                dev_put(old);
1875
1876        if (master)
1877                slave->flags |= IFF_SLAVE;
1878        else
1879                slave->flags &= ~IFF_SLAVE;
1880
1881        rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
1882        return 0;
1883}
1884
1885/**
1886 *      dev_set_promiscuity     - update promiscuity count on a device
1887 *      @dev: device
1888 *      @inc: modifier
1889 *
1890 *      Add or remove promsicuity from a device. While the count in the device
1891 *      remains above zero the interface remains promiscuous. Once it hits zero
1892 *      the device reverts back to normal filtering operation. A negative inc
1893 *      value is used to drop promiscuity on the device.
1894 */
1895 
1896void dev_set_promiscuity(struct net_device *dev, int inc)
1897{
1898        unsigned short old_flags = dev->flags;
1899
1900        dev->flags |= IFF_PROMISC;
1901        if ((dev->promiscuity += inc) == 0)
1902                dev->flags &= ~IFF_PROMISC;
1903        if (dev->flags^old_flags) {
1904#ifdef CONFIG_NET_FASTROUTE
1905                if (dev->flags&IFF_PROMISC) {
1906                        netdev_fastroute_obstacles++;
1907                        dev_clear_fastroute(dev);
1908                } else
1909                        netdev_fastroute_obstacles--;
1910#endif
1911                dev_mc_upload(dev);
1912                printk(KERN_INFO "device %s %s promiscuous mode\n",
1913                       dev->name, (dev->flags&IFF_PROMISC) ? "entered" : "left");
1914        }
1915}
1916
1917/**
1918 *      dev_set_allmulti        - update allmulti count on a device
1919 *      @dev: device
1920 *      @inc: modifier
1921 *
1922 *      Add or remove reception of all multicast frames to a device. While the
1923 *      count in the device remains above zero the interface remains listening
1924 *      to all interfaces. Once it hits zero the device reverts back to normal
1925 *      filtering operation. A negative @inc value is used to drop the counter
1926 *      when releasing a resource needing all multicasts.
1927 */
1928
1929void dev_set_allmulti(struct net_device *dev, int inc)
1930{
1931        unsigned short old_flags = dev->flags;
1932
1933        dev->flags |= IFF_ALLMULTI;
1934        if ((dev->allmulti += inc) == 0)
1935                dev->flags &= ~IFF_ALLMULTI;
1936        if (dev->flags^old_flags)
1937                dev_mc_upload(dev);
1938}
1939
1940int dev_change_flags(struct net_device *dev, unsigned flags)
1941{
1942        int ret;
1943        int old_flags = dev->flags;
1944
1945        /*
1946         *      Set the flags on our device.
1947         */
1948
1949        dev->flags = (flags & (IFF_DEBUG|IFF_NOTRAILERS|IFF_NOARP|IFF_DYNAMIC|
1950                               IFF_MULTICAST|IFF_PORTSEL|IFF_AUTOMEDIA)) |
1951                                       (dev->flags & (IFF_UP|IFF_VOLATILE|IFF_PROMISC|IFF_ALLMULTI));
1952
1953        /*
1954         *      Load in the correct multicast list now the flags have changed.
1955         */                             
1956
1957        dev_mc_upload(dev);
1958
1959        /*
1960         *      Have we downed the interface. We handle IFF_UP ourselves
1961         *      according to user attempts to set it, rather than blindly
1962         *      setting it.
1963         */
1964
1965        ret = 0;
1966        if ((old_flags^flags)&IFF_UP)   /* Bit is different  ? */
1967        {
1968                ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
1969
1970                if (ret == 0) 
1971                        dev_mc_upload(dev);
1972        }
1973
1974        if (dev->flags&IFF_UP &&
1975            ((old_flags^dev->flags)&~(IFF_UP|IFF_PROMISC|IFF_ALLMULTI|IFF_VOLATILE)))
1976                notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
1977
1978        if ((flags^dev->gflags)&IFF_PROMISC) {
1979                int inc = (flags&IFF_PROMISC) ? +1 : -1;
1980                dev->gflags ^= IFF_PROMISC;
1981                dev_set_promiscuity(dev, inc);
1982        }
1983
1984        /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
1985           is important. Some (broken) drivers set IFF_PROMISC, when
1986           IFF_ALLMULTI is requested not asking us and not reporting.
1987         */
1988        if ((flags^dev->gflags)&IFF_ALLMULTI) {
1989                int inc = (flags&IFF_ALLMULTI) ? +1 : -1;
1990                dev->gflags ^= IFF_ALLMULTI;
1991                dev_set_allmulti(dev, inc);
1992        }
1993
1994        if (old_flags^dev->flags)
1995                rtmsg_ifinfo(RTM_NEWLINK, dev, old_flags^dev->flags);
1996
1997        return ret;
1998}
1999
2000/*
2001 *      Perform the SIOCxIFxxx calls. 
2002 */
2003 
2004static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
2005{
2006        struct net_device *dev;
2007        int err;
2008
2009        if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL)
2010                return -ENODEV;
2011
2012        switch(cmd) 
2013        {
2014                case SIOCGIFFLAGS:      /* Get interface flags */
2015                        ifr->ifr_flags = (dev->flags&~(IFF_PROMISC|IFF_ALLMULTI|IFF_RUNNING))
2016                                |(dev->gflags&(IFF_PROMISC|IFF_ALLMULTI));
2017                        if (netif_running(dev) && netif_carrier_ok(dev))
2018                                ifr->ifr_flags |= IFF_RUNNING;
2019                        return 0;
2020
2021                case SIOCSIFFLAGS:      /* Set interface flags */
2022                        return dev_change_flags(dev, ifr->ifr_flags);
2023                
2024                case SIOCGIFMETRIC:     /* Get the metric on the interface (currently unused) */
2025                        ifr->ifr_metric = 0;
2026                        return 0;
2027                        
2028                case SIOCSIFMETRIC:     /* Set the metric on the interface (currently unused) */
2029                        return -EOPNOTSUPP;
2030        
2031                case SIOCGIFMTU:        /* Get the MTU of a device */
2032                        ifr->ifr_mtu = dev->mtu;
2033                        return 0;
2034        
2035                case SIOCSIFMTU:        /* Set the MTU of a device */
2036                        if (ifr->ifr_mtu == dev->mtu)
2037                                return 0;
2038
2039                        /*
2040                         *      MTU must be positive.
2041                         */
2042                         
2043                        if (ifr->ifr_mtu<0)
2044                                return -EINVAL;
2045
2046                        if (!netif_device_present(dev))
2047                                return -ENODEV;
2048
2049                        if (dev->change_mtu)
2050                                err = dev->change_mtu(dev, ifr->ifr_mtu);
2051                        else {
2052                                dev->mtu = ifr->ifr_mtu;
2053                                err = 0;
2054                        }
2055                        if (!err && dev->flags&IFF_UP)
2056                                notifier_call_chain(&netdev_chain, NETDEV_CHANGEMTU, dev);
2057                        return err;
2058
2059                case SIOCGIFHWADDR:
2060                        memcpy(ifr->ifr_hwaddr.sa_data,dev->dev_addr, MAX_ADDR_LEN);
2061                        ifr->ifr_hwaddr.sa_family=dev->type;
2062                        return 0;
2063                                
2064                case SIOCSIFHWADDR:
2065                        if (dev->set_mac_address == NULL)
2066                                return -EOPNOTSUPP;
2067                        if (ifr->ifr_hwaddr.sa_family!=dev->type)
2068                                return -EINVAL;
2069                        if (!netif_device_present(dev))
2070                                return -ENODEV;
2071                        err = dev->set_mac_address(dev, &ifr->ifr_hwaddr);
2072                        if (!err)
2073                                notifier_call_chain(&netdev_chain, NETDEV_CHANGEADDR, dev);
2074                        return err;
2075                        
2076                case SIOCSIFHWBROADCAST:
2077                        if (ifr->ifr_hwaddr.sa_family!=dev->type)
2078                                return -EINVAL;
2079                        memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data, MAX_ADDR_LEN);
2080                        notifier_call_chain(&netdev_chain, NETDEV_CHANGEADDR, dev);
2081                        return 0;
2082
2083                case SIOCGIFMAP:
2084                        ifr->ifr_map.mem_start=dev->mem_start;
2085                        ifr->ifr_map.mem_end=dev->mem_end;
2086                        ifr->ifr_map.base_addr=dev->base_addr;
2087                        ifr->ifr_map.irq=dev->irq;
2088                        ifr->ifr_map.dma=dev->dma;
2089                        ifr->ifr_map.port=dev->if_port;
2090                        return 0;
2091                        
2092                case SIOCSIFMAP:
2093                        if (dev->set_config) {
2094                                if (!netif_device_present(dev))
2095                                        return -ENODEV;
2096                                return dev->set_config(dev,&ifr->ifr_map);
2097                        }
2098                        return -EOPNOTSUPP;
2099                        
2100                case SIOCADDMULTI:
2101                        if (dev->set_multicast_list == NULL ||
2102                            ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2103                                return -EINVAL;
2104                        if (!netif_device_present(dev))
2105                                return -ENODEV;
2106                        dev_mc_add(dev,ifr->ifr_hwaddr.sa_data, dev->addr_len, 1);
2107                        return 0;
2108
2109                case SIOCDELMULTI:
2110                        if (dev->set_multicast_list == NULL ||
2111                            ifr->ifr_hwaddr.sa_family!=AF_UNSPEC)
2112                                return -EINVAL;
2113                        if (!netif_device_present(dev))
2114                                return -ENODEV;
2115                        dev_mc_delete(dev,ifr->ifr_hwaddr.sa_data,dev->addr_len, 1);
2116                        return 0;
2117
2118                case SIOCGIFINDEX:
2119                        ifr->ifr_ifindex = dev->ifindex;
2120                        return 0;
2121
2122                case SIOCGIFTXQLEN:
2123                        ifr->ifr_qlen = dev->tx_queue_len;
2124                        return 0;
2125
2126                case SIOCSIFTXQLEN:
2127                        if (ifr->ifr_qlen<0)
2128                                return -EINVAL;
2129                        dev->tx_queue_len = ifr->ifr_qlen;
2130                        return 0;
2131
2132                case SIOCSIFNAME:
2133                        if (dev->flags&IFF_UP)
2134                                return -EBUSY;
2135                        if (__dev_get_by_name(ifr->ifr_newname))
2136                                return -EEXIST;
2137                        memcpy(dev->name, ifr->ifr_newname, IFNAMSIZ);
2138                        dev->name[IFNAMSIZ-1] = 0;
2139                        notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev);
2140                        return 0;
2141
2142                /*
2143                 *      Unknown or private ioctl
2144                 */
2145
2146                default:
2147                        if ((cmd >= SIOCDEVPRIVATE &&
2148                            cmd <= SIOCDEVPRIVATE + 15) ||
2149                            cmd == SIOCBONDENSLAVE ||
2150                            cmd == SIOCBONDRELEASE ||
2151                            cmd == SIOCBONDSETHWADDR ||
2152                            cmd == SIOCBONDSLAVEINFOQUERY ||
2153                            cmd == SIOCBONDINFOQUERY ||
2154                            cmd == SIOCBONDCHANGEACTIVE ||
2155                            cmd == SIOCETHTOOL ||
2156                            cmd == SIOCGMIIPHY ||
2157                            cmd == SIOCGMIIREG ||
2158                            cmd == SIOCSMIIREG) {
2159                                if (dev->do_ioctl) {
2160                                        if (!netif_device_present(dev))
2161                                                return -ENODEV;
2162                                        return dev->do_ioctl(dev, ifr, cmd);
2163                                }
2164                                return -EOPNOTSUPP;
2165                        }
2166
2167        }
2168        return -EINVAL;
2169}
2170
2171/*
2172 *      This function handles all "interface"-type I/O control requests. The actual
2173 *      'doing' part of this is dev_ifsioc above.
2174 */
2175
2176/**
2177 *      dev_ioctl       -       network device ioctl
2178 *      @cmd: command to issue
2179 *      @arg: pointer to a struct ifreq in user space
2180 *
2181 *      Issue ioctl functions to devices. This is normally called by the
2182 *      user space syscall interfaces but can sometimes be useful for 
2183 *      other purposes. The return value is the return from the syscall if
2184 *      positive or a negative errno code on error.
2185 */
2186
2187int dev_ioctl(unsigned int cmd, void *arg)
2188{
2189        struct ifreq ifr;
2190        int ret;
2191        char *colon;
2192
2193        /* One special case: SIOCGIFCONF takes ifconf argument
2194           and requires shared lock, because it sleeps writing
2195           to user space.
2196         */
2197           
2198        if (cmd == SIOCGIFCONF) {
2199                rtnl_shlock();
2200                ret = dev_ifconf((char *) arg);
2201                rtnl_shunlock();
2202                return ret;
2203        }
2204        if (cmd == SIOCGIFNAME) {
2205                return dev_ifname((struct ifreq *)arg);
2206        }
2207
2208        if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2209                return -EFAULT;
2210
2211        ifr.ifr_name[IFNAMSIZ-1] = 0;
2212
2213        colon = strchr(ifr.ifr_name, ':');
2214        if (colon)
2215                *colon = 0;
2216
2217        /*
2218         *      See which interface the caller is talking about. 
2219         */
2220         
2221        switch(cmd) 
2222        {
2223                /*
2224                 *      These ioctl calls:
2225                 *      - can be done by all.
2226                 *      - atomic and do not require locking.
2227                 *      - return a value
2228                 */
2229                 
2230                case SIOCGIFFLAGS:
2231                case SIOCGIFMETRIC:
2232                case SIOCGIFMTU:
2233                case SIOCGIFHWADDR:
2234                case SIOCGIFSLAVE:
2235                case SIOCGIFMAP:
2236                case SIOCGIFINDEX:
2237                case SIOCGIFTXQLEN:
2238                        dev_load(ifr.ifr_name);
2239                        read_lock(&dev_base_lock);
2240                        ret = dev_ifsioc(&ifr, cmd);
2241                        read_unlock(&dev_base_lock);
2242                        if (!ret) {
2243                                if (colon)
2244                                        *colon = ':';
2245                                if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2246                                        return -EFAULT;
2247                        }
2248                        return ret;
2249
2250                /*
2251                 *      These ioctl calls:
2252                 *      - require superuser power.
2253                 *      - require strict serialization.
2254                 *      - return a value
2255                 */
2256                 
2257                case SIOCETHTOOL:
2258                case SIOCGMIIPHY:
2259                case SIOCGMIIREG:
2260                        if (!capable(CAP_NET_ADMIN))
2261                                return -EPERM;
2262                        dev_load(ifr.ifr_name);
2263                        dev_probe_lock();
2264                        rtnl_lock();
2265                        ret = dev_ifsioc(&ifr, cmd);
2266                        rtnl_unlock();
2267                        dev_probe_unlock();
2268                        if (!ret) {
2269                                if (colon)
2270                                        *colon = ':';
2271                                if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2272                                        return -EFAULT;
2273                        }
2274                        return ret;
2275
2276                /*
2277                 *      These ioctl calls:
2278                 *      - require superuser power.
2279                 *      - require strict serialization.
2280                 *      - do not return a value
2281                 */
2282                 
2283                case SIOCSIFFLAGS:
2284                case SIOCSIFMETRIC:
2285                case SIOCSIFMTU:
2286                case SIOCSIFMAP:
2287                case SIOCSIFHWADDR:
2288                case SIOCSIFSLAVE:
2289                case SIOCADDMULTI:
2290                case SIOCDELMULTI:
2291                case SIOCSIFHWBROADCAST:
2292                case SIOCSIFTXQLEN:
2293                case SIOCSIFNAME:
2294                case SIOCSMIIREG:
2295                case SIOCBONDENSLAVE:
2296                case SIOCBONDRELEASE:
2297                case SIOCBONDSETHWADDR:
2298                case SIOCBONDSLAVEINFOQUERY:
2299                case SIOCBONDINFOQUERY:
2300                case SIOCBONDCHANGEACTIVE:
2301                        if (!capable(CAP_NET_ADMIN))
2302                                return -EPERM;
2303                        dev_load(ifr.ifr_name);
2304                        dev_probe_lock();
2305                        rtnl_lock();
2306                        ret = dev_ifsioc(&ifr, cmd);
2307                        rtnl_unlock();
2308                        dev_probe_unlock();
2309                        return ret;
2310        
2311                case SIOCGIFMEM:
2312                        /* Get the per device memory space. We can add this but currently
2313                           do not support it */
2314                case SIOCSIFMEM:
2315                        /* Set the per device memory buffer space. Not applicable in our case */
2316                case SIOCSIFLINK:
2317                        return -EINVAL;
2318
2319                /*
2320                 *      Unknown or private ioctl.
2321                 */     
2322                 
2323                default:
2324                        if (cmd >= SIOCDEVPRIVATE &&
2325                            cmd <= SIOCDEVPRIVATE + 15) {
2326                                dev_load(ifr.ifr_name);
2327                                dev_probe_lock();
2328                                rtnl_lock();
2329                                ret = dev_ifsioc(&ifr, cmd);
2330                                rtnl_unlock();
2331                                dev_probe_unlock();
2332                                if (!ret && copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2333                                        return -EFAULT;
2334                                return ret;
2335                        }
2336#ifdef WIRELESS_EXT
2337                        /* Take care of Wireless Extensions */
2338                        if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
2339                                /* If command is `set a parameter', or
2340                                 * `get the encoding parameters', check if
2341                                 * the user has the right to do it */
2342                                if (IW_IS_SET(cmd) || (cmd == SIOCGIWENCODE)) {
2343                                        if(!capable(CAP_NET_ADMIN))
2344                                                return -EPERM;
2345                                }
2346                                dev_load(ifr.ifr_name);
2347                                rtnl_lock();
2348                                /* Follow me in net/core/wireless.c */
2349                                ret = wireless_process_ioctl(&ifr, cmd);
2350                                rtnl_unlock();
2351                                if (!ret && IW_IS_GET(cmd) &&
2352                                    copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2353                                        return -EFAULT;
2354                                return ret;
2355                        }
2356#endif  /* WIRELESS_EXT */
2357                        return -EINVAL;
2358        }
2359}
2360
2361
2362/**
2363 *      dev_new_index   -       allocate an ifindex
2364 *
2365 *      Returns a suitable unique value for a new device interface
2366 *      number.  The caller must hold the rtnl semaphore or the
2367 *      dev_base_lock to be sure it remains unique.
2368 */
2369 
2370int dev_new_index(void)
2371{
2372        static int ifindex;
2373        for (;;) {
2374                if (++ifindex <= 0)
2375                        ifindex=1;
2376                if (__dev_get_by_index(ifindex) == NULL)
2377                        return ifindex;
2378        }
2379}
2380
2381static int dev_boot_phase = 1;
2382
2383/**
2384 *      register_netdevice      - register a network device
2385 *      @dev: device to register
2386 *      
2387 *      Take a completed network device structure and add it to the kernel
2388 *      interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
2389 *      chain. 0 is returned on success. A negative errno code is returned
2390 *      on a failure to set up the device, or if the name is a duplicate.
2391 *
2392 *      Callers must hold the rtnl semaphore.  See the comment at the
2393 *      end of Space.c for details about the locking.  You may want
2394 *      register_netdev() instead of this.
2395 *
2396 *      BUGS:
2397 *      The locking appears insufficient to guarantee two parallel registers
2398 *      will not get the same name.
2399 */
2400
2401int net_dev_init(void);
2402
2403int register_netdevice(struct net_device *dev)
2404{
2405        struct net_device *d, **dp;
2406#ifdef CONFIG_NET_DIVERT
2407        int ret;
2408#endif
2409
2410        spin_lock_init(&dev->queue_lock);
2411        spin_lock_init(&dev->xmit_lock);
2412        dev->xmit_lock_owner = -1;
2413#ifdef CONFIG_NET_FASTROUTE
2414        dev->fastpath_lock=RW_LOCK_UNLOCKED;
2415#endif
2416
2417        if (dev_boot_phase)
2418                net_dev_init();
2419
2420#ifdef CONFIG_NET_DIVERT
2421        ret = alloc_divert_blk(dev);
2422        if (ret)
2423                return ret;
2424#endif /* CONFIG_NET_DIVERT */
2425        
2426        dev->iflink = -1;
2427
2428        /* Init, if this function is available */
2429        if (dev->init && dev->init(dev) != 0) {
2430#ifdef CONFIG_NET_DIVERT
2431                free_divert_blk(dev);
2432#endif
2433                return -EIO;
2434        }
2435
2436        dev->ifindex = dev_new_index();
2437        if (dev->iflink == -1)
2438                dev->iflink = dev->ifindex;
2439
2440        /* Check for existence, and append to tail of chain */
2441        for (dp=&dev_base; (d=*dp) != NULL; dp=&d->next) {
2442                if (d == dev || strcmp(d->name, dev->name) == 0) {
2443#ifdef CONFIG_NET_DIVERT
2444                        free_divert_blk(dev);
2445#endif
2446                        return -EEXIST;
2447                }
2448        }
2449        /*
2450         *      nil rebuild_header routine,
2451         *      that should be never called and used as just bug trap.
2452         */
2453
2454        if (dev->rebuild_header == NULL)
2455                dev->rebuild_header = default_rebuild_header;
2456
2457        /*
2458         *      Default initial state at registry is that the
2459         *      device is present.
2460         */
2461
2462        set_bit(__LINK_STATE_PRESENT, &dev->state);
2463
2464        dev->next = NULL;
2465        dev_init_scheduler(dev);
2466        write_lock_bh(&dev_base_lock);
2467        *dp = dev;
2468        dev_hold(dev);
2469        dev->deadbeaf = 0;
2470        write_unlock_bh(&dev_base_lock);
2471
2472        /* Notify protocols, that a new device appeared. */
2473        notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
2474
2475        net_run_sbin_hotplug(dev, "register");
2476
2477        return 0;
2478}
2479
2480/**
2481 *      netdev_finish_unregister - complete unregistration
2482 *      @dev: device
2483 *
2484 *      Destroy and free a dead device. A value of zero is returned on
2485 *      success.
2486 */
2487 
2488int netdev_finish_unregister(struct net_device *dev)
2489{
2490        BUG_TRAP(dev->ip_ptr==NULL);
2491        BUG_TRAP(dev->ip6_ptr==NULL);
2492        BUG_TRAP(dev->dn_ptr==NULL);
2493
2494        if (!dev->deadbeaf) {
2495                printk(KERN_ERR "Freeing alive device %p, %s\n", dev, dev->name);
2496                return 0;
2497        }
2498#ifdef NET_REFCNT_DEBUG
2499        printk(KERN_DEBUG "netdev_finish_unregister: %s%s.\n", dev->name,
2500               (dev->features & NETIF_F_DYNALLOC)?"":", old style");
2501#endif
2502        if (dev->destructor)
2503                dev->destructor(dev);
2504        if (dev->features & NETIF_F_DYNALLOC)
2505                kfree(dev);
2506        return 0;
2507}
2508
2509/**
2510 *      unregister_netdevice - remove device from the kernel
2511 *      @dev: device
2512 *
2513 *      This function shuts down a device interface and removes it
2514 *      from the kernel tables. On success 0 is returned, on a failure
2515 *      a negative errno code is returned.
2516 *
2517 *      Callers must hold the rtnl semaphore.  See the comment at the
2518 *      end of Space.c for details about the locking.  You may want
2519 *      unregister_netdev() instead of this.
2520 */
2521
2522int unregister_netdevice(struct net_device *dev)
2523{
2524        unsigned long now, warning_time;
2525        struct net_device *d, **dp;
2526
2527        /* If device is running, close it first. */
2528        if (dev->flags & IFF_UP)
2529                dev_close(dev);
2530
2531        BUG_TRAP(dev->deadbeaf==0);
2532        dev->deadbeaf = 1;
2533
2534        /* And unlink it from device chain. */
2535        for (dp = &dev_base; (d=*dp) != NULL; dp=&d->next) {
2536                if (d == dev) {
2537                        write_lock_bh(&dev_base_lock);
2538                        *dp = d->next;
2539                        write_unlock_bh(&dev_base_lock);
2540                        break;
2541                }
2542        }
2543        if (d == NULL) {
2544                printk(KERN_DEBUG "unregister_netdevice: device %s/%p never was registered\n", dev->name, dev);
2545                return -ENODEV;
2546        }
2547
2548        /* Synchronize to net_rx_action. */
2549        br_write_lock_bh(BR_NETPROTO_LOCK);
2550        br_write_unlock_bh(BR_NETPROTO_LOCK);
2551
2552        if (dev_boot_phase == 0) {
2553#ifdef CONFIG_NET_FASTROUTE
2554                dev_clear_fastroute(dev);
2555#endif
2556
2557                /* Shutdown queueing discipline. */
2558                dev_shutdown(dev);
2559
2560                net_run_sbin_hotplug(dev, "unregister");
2561
2562                /* Notify protocols, that we are about to destroy
2563                   this device. They should clean all the things.
2564                 */
2565                notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
2566
2567                /*
2568                 *      Flush the multicast chain
2569                 */
2570                dev_mc_discard(dev);
2571        }
2572
2573        if (dev->uninit)
2574                dev->uninit(dev);
2575
2576        /* Notifier chain MUST detach us from master device. */
2577        BUG_TRAP(dev->master==NULL);
2578
2579#ifdef CONFIG_NET_DIVERT
2580        free_divert_blk(dev);
2581#endif
2582
2583        if (dev->features & NETIF_F_DYNALLOC) {
2584#ifdef NET_REFCNT_DEBUG
2585                if (atomic_read(&dev->refcnt) != 1)
2586                        printk(KERN_DEBUG "unregister_netdevice: holding %s refcnt=%d\n", dev->name, atomic_read(&dev->refcnt)-1);
2587#endif
2588                dev_put(dev);
2589                return 0;
2590        }
2591
2592        /* Last reference is our one */
2593        if (atomic_read(&dev->refcnt) == 1) {
2594                dev_put(dev);
2595                return 0;
2596        }
2597
2598#ifdef NET_REFCNT_DEBUG
2599        printk("unregister_netdevice: waiting %s refcnt=%d\n", dev->name, atomic_read(&dev->refcnt));
2600#endif
2601
2602        /* EXPLANATION. If dev->refcnt is not now 1 (our own reference)
2603           it means that someone in the kernel still has a reference
2604           to this device and we cannot release it.
2605
2606           "New style" devices have destructors, hence we can return from this
2607           function and destructor will do all the work later.  As of kernel 2.4.0
2608           there are very few "New Style" devices.
2609
2610           "Old style" devices expect that the device is free of any references
2611           upon exit from this function.
2612           We cannot return from this function until all such references have
2613           fallen away.  This is because the caller of this function will probably
2614           immediately kfree(*dev) and then be unloaded via sys_delete_module.
2615
2616           So, we linger until all references fall away.  The duration of the
2617           linger is basically unbounded! It is driven by, for example, the
2618           current setting of sysctl_ipfrag_time.
2619
2620           After 1 second, we start to rebroadcast unregister notifications
2621           in hope that careless clients will release the device.
2622
2623         */
2624
2625        now = warning_time = jiffies;
2626        while (atomic_read(&dev->refcnt) != 1) {
2627                if ((jiffies - now) > 1*HZ) {
2628                        /* Rebroadcast unregister notification */
2629                        notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
2630                }
2631                current->state = TASK_INTERRUPTIBLE;
2632                schedule_timeout(HZ/4);
2633                current->state = TASK_RUNNING;
2634                if ((jiffies - warning_time) > 10*HZ) {
2635                        printk(KERN_EMERG "unregister_netdevice: waiting for %s to "
2636                                        "become free. Usage count = %d\n",
2637                                        dev->name, atomic_read(&dev->refcnt));
2638                        warning_time = jiffies;
2639                }
2640        }
2641        dev_put(dev);
2642        return 0;
2643}
2644
2645
2646/*
2647 *      Initialize the DEV module. At boot time this walks the device list and
2648 *      unhooks any devices that fail to initialise (normally hardware not 
2649 *      present) and leaves us with a valid list of present and active devices.
2650 *
2651 */
2652
2653extern void net_device_init(void);
2654extern void ip_auto_config(void);
2655struct proc_dir_entry *proc_net_drivers;
2656#ifdef CONFIG_NET_DIVERT
2657extern void dv_init(void);
2658#endif /* CONFIG_NET_DIVERT */
2659
2660
2661/*
2662 *       Callers must hold the rtnl semaphore.  See the comment at the
2663 *       end of Space.c for details about the locking.
2664 */
2665int __init net_dev_init(void)
2666{
2667        struct net_device *dev, **dp;
2668        int i;
2669
2670        if (!dev_boot_phase)
2671                return 0;
2672
2673
2674#ifdef CONFIG_NET_DIVERT
2675        dv_init();
2676#endif /* CONFIG_NET_DIVERT */
2677        
2678        /*
2679         *      Initialise the packet receive queues.
2680         */
2681
2682        for (i = 0; i < NR_CPUS; i++) {
2683                struct softnet_data *queue;
2684
2685                queue = &softnet_data[i];
2686                skb_queue_head_init(&queue->input_pkt_queue);
2687                queue->throttle = 0;
2688                queue->cng_level = 0;
2689                queue->avg_blog = 10; /* arbitrary non-zero */
2690                queue->completion_queue = NULL;
2691                INIT_LIST_HEAD(&queue->poll_list);
2692                set_bit(__LINK_STATE_START, &queue->blog_dev.state);
2693                queue->blog_dev.weight = weight_p;
2694                queue->blog_dev.poll = process_backlog;
2695                atomic_set(&queue->blog_dev.refcnt, 1);
2696        }
2697
2698#ifdef CONFIG_NET_PROFILE
2699        net_profile_init();
2700        NET_PROFILE_REGISTER(dev_queue_xmit);
2701        NET_PROFILE_REGISTER(softnet_process);
2702#endif
2703
2704#ifdef OFFLINE_SAMPLE
2705        samp_timer.expires = jiffies + (10 * HZ);
2706        add_timer(&samp_timer);
2707#endif
2708
2709        /*
2710         *      Add the devices.
2711         *      If the call to dev->init fails, the dev is removed
2712         *      from the chain disconnecting the device until the
2713         *      next reboot.
2714         *
2715         *      NB At boot phase networking is dead. No locking is required.
2716         *      But we still preserve dev_base_lock for sanity.
2717         */
2718
2719        dp = &dev_base;
2720        while ((dev = *dp) != NULL) {
2721                spin_lock_init(&dev->queue_lock);
2722                spin_lock_init(&dev->xmit_lock);
2723#ifdef CONFIG_NET_FASTROUTE
2724                dev->fastpath_lock = RW_LOCK_UNLOCKED;
2725#endif
2726                dev->xmit_lock_owner = -1;
2727                dev->iflink = -1;
2728                dev_hold(dev);
2729
2730                /*
2731                 * Allocate name. If the init() fails
2732                 * the name will be reissued correctly.
2733                 */
2734                if (strchr(dev->name, '%'))
2735                        dev_alloc_name(dev, dev->name);
2736
2737                /* 
2738                 * Check boot time settings for the device.
2739                 */
2740                netdev_boot_setup_check(dev);
2741
2742                if (dev->init && dev->init(dev)) {
2743                        /*
2744                         * It failed to come up. It will be unhooked later.
2745                         * dev_alloc_name can now advance to next suitable
2746                         * name that is checked next.
2747                         */
2748                        dev->deadbeaf = 1;
2749                        dp = &dev->next;
2750                } else {
2751                        dp = &dev->next;
2752                        dev->ifindex = dev_new_index();
2753                        if (dev->iflink == -1)
2754                                dev->iflink = dev->ifindex;
2755                        if (dev->rebuild_header == NULL)
2756                                dev->rebuild_header = default_rebuild_header;
2757                        dev_init_scheduler(dev);
2758                        set_bit(__LINK_STATE_PRESENT, &dev->state);
2759                }
2760        }
2761
2762        /*
2763         * Unhook devices that failed to come up
2764         */
2765        dp = &dev_base;
2766        while ((dev = *dp) != NULL) {
2767                if (dev->deadbeaf) {
2768                        write_lock_bh(&dev_base_lock);
2769                        *dp = dev->next;
2770                        write_unlock_bh(&dev_base_lock);
2771                        dev_put(dev);
2772                } else {
2773                        dp = &dev->next;
2774                }
2775        }
2776
2777#ifdef CONFIG_PROC_FS
2778        proc_net_create("dev", 0, dev_get_info);
2779        create_proc_read_entry("net/softnet_stat", 0, 0, dev_proc_stats, NULL);
2780        proc_net_drivers = proc_mkdir("net/drivers", 0);
2781#ifdef WIRELESS_EXT
2782        /* Available in net/core/wireless.c */
2783        proc_net_create("wireless", 0, dev_get_wireless_info);
2784#endif  /* WIRELESS_EXT */
2785#endif  /* CONFIG_PROC_FS */
2786
2787        dev_boot_phase = 0;
2788
2789        open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
2790        open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
2791
2792        dst_init();
2793        dev_mcast_init();
2794
2795#ifdef CONFIG_NET_SCHED
2796        pktsched_init();
2797#endif
2798        /*
2799         *      Initialise network devices
2800         */
2801         
2802        net_device_init();
2803
2804        return 0;
2805}
2806
2807#ifdef CONFIG_HOTPLUG
2808
2809/* Notify userspace when a netdevice event occurs,
2810 * by running '/sbin/hotplug net' with certain
2811 * environment variables set.
2812 */
2813
2814static int net_run_sbin_hotplug(struct net_device *dev, char *action)
2815{
2816        char *argv[3], *envp[5], ifname[12 + IFNAMSIZ], action_str[32];
2817        int i;
2818
2819        sprintf(ifname, "INTERFACE=%s", dev->name);
2820        sprintf(action_str, "ACTION=%s", action);
2821
2822        i = 0;
2823        argv[i++] = hotplug_path;
2824        argv[i++] = "net";
2825        argv[i] = 0;
2826
2827        i = 0;
2828        /* minimal command environment */
2829        envp [i++] = "HOME=/";
2830        envp [i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
2831        envp [i++] = ifname;
2832        envp [i++] = action_str;
2833        envp [i] = 0;
2834        
2835        return call_usermodehelper(argv [0], argv, envp);
2836}
2837#endif
2838
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.