2 * NET3 Protocol independent device support routines.
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.
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>
15 * Florian la Roche <rzsfl@rz.uni-sb.de>
16 * Alan Cox <gw4pts@gw4pts.ampr.org>
17 * David Hinds <dhinds@allegro.stanford.edu>
18 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
19 * Adam Sulmicki <adam@cfar.umd.edu>
20 * Pekka Riikonen <priikone@poesidon.pspt.fi>
23 * Alan Cox : device private ioctl copies fields back.
24 * Alan Cox : Transmit queue code does relevant stunts to
25 * keep the queue safe.
26 * Alan Cox : Fixed double lock.
27 * Alan Cox : Fixed promisc NULL pointer trap
28 * ???????? : Support the full private ioctl range
29 * Alan Cox : Moved ioctl permission check into drivers
30 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
31 * Alan Cox : 100 backlog just doesn't cut it when
32 * you start doing multicast video 8)
33 * Alan Cox : Rewrote net_bh and list manager.
34 * Alan Cox : Fix ETH_P_ALL echoback lengths.
35 * Alan Cox : Took out transmit every packet pass
36 * Saved a few bytes in the ioctl handler
37 * Alan Cox : Network driver sets packet type before calling netif_rx. Saves
38 * a function call a packet.
39 * Alan Cox : Hashed net_bh()
40 * Richard Kooijman: Timestamp fixes.
41 * Alan Cox : Wrong field in SIOCGIFDSTADDR
42 * Alan Cox : Device lock protection.
43 * Alan Cox : Fixed nasty side effect of device close changes.
44 * Rudi Cilibrasi : Pass the right thing to set_mac_address()
45 * Dave Miller : 32bit quantity for the device lock to make it work out
47 * Bjorn Ekwall : Added KERNELD hack.
48 * Alan Cox : Cleaned up the backlog initialise.
49 * Craig Metz : SIOCGIFCONF fix if space for under
51 * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
52 * is no device open function.
53 * Andi Kleen : Fix error reporting for SIOCGIFCONF
54 * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
55 * Cyrus Durgin : Cleaned for KMOD
56 * Adam Sulmicki : Bug Fix : Network Device Unload
57 * A network device unload needs to purge
59 * Paul Rusty Russell : SIOCSIFNAME
60 * Pekka Riikonen : Netdev boot-time settings code
61 * Andrew Morton : Make unregister_netdevice wait indefinitely on dev->refcnt
62 * J Hadi Salim : - Backlog queue sampling
63 * - netif_rx() feedback
66 #include <asm/uaccess.h>
67 #include <asm/system.h>
68 #include <asm/bitops.h>
69 #include <linux/config.h>
70 #include <linux/types.h>
71 #include <linux/kernel.h>
72 #include <linux/sched.h>
73 #include <linux/string.h>
75 #include <linux/socket.h>
76 #include <linux/sockios.h>
77 #include <linux/errno.h>
78 #include <linux/interrupt.h>
79 #include <linux/if_ether.h>
80 #include <linux/netdevice.h>
81 #include <linux/etherdevice.h>
82 #include <linux/notifier.h>
83 #include <linux/skbuff.h>
84 #include <linux/brlock.h>
86 #include <linux/rtnetlink.h>
87 #include <linux/proc_fs.h>
88 #include <linux/stat.h>
89 #include <linux/if_bridge.h>
90 #include <linux/divert.h>
92 #include <net/pkt_sched.h>
93 #include <net/profile.h>
94 #include <linux/init.h>
95 #include <linux/kmod.h>
96 #include <linux/module.h>
97 #if defined(CONFIG_NET_RADIO) || defined(CONFIG_NET_PCMCIA_RADIO)
98 #include <linux/wireless.h> /* Note : will define WIRELESS_EXT */
99 #endif /* CONFIG_NET_RADIO || CONFIG_NET_PCMCIA_RADIO */
101 extern int plip_init(void);
104 /* This define, if set, will randomly drop a packet when congestion
105 * is more than moderate. It helps fairness in the multi-interface
106 * case when one of them is a hog, but it kills performance for the
107 * single interface case so it is off now by default.
111 /* Setting this will sample the queue lengths and thus congestion
112 * via a timer instead of as each packet is received.
114 #undef OFFLINE_SAMPLE
116 NET_PROFILE_DEFINE(dev_queue_xmit
)
117 NET_PROFILE_DEFINE(softnet_process
)
119 const char *if_port_text
[] = {
130 * The list of packet types we will receive (as opposed to discard)
131 * and the routines to invoke.
133 * Why 16. Because with 16 the only overlap we get on a hash of the
134 * low nibble of the protocol value is RARP/SNAP/X.25.
149 static struct packet_type
*ptype_base
[16]; /* 16 way hashed list */
150 static struct packet_type
*ptype_all
= NULL
; /* Taps */
152 #ifdef OFFLINE_SAMPLE
153 static void sample_queue(unsigned long dummy
);
154 static struct timer_list samp_timer
= { function
: sample_queue
};
161 static struct notifier_block
*netdev_chain
=NULL
;
164 * Device drivers call our routines to queue packets here. We empty the
165 * queue in the local softnet handler.
167 struct softnet_data softnet_data
[NR_CPUS
] __cacheline_aligned
;
169 #ifdef CONFIG_NET_FASTROUTE
170 int netdev_fastroute
;
171 int netdev_fastroute_obstacles
;
175 /******************************************************************************************
177 Protocol management and registration routines
179 *******************************************************************************************/
188 * Add a protocol ID to the list. Now that the input handler is
189 * smarter we can dispense with all the messy stuff that used to be
192 * BEWARE!!! Protocol handlers, mangling input packets,
193 * MUST BE last in hash buckets and checking protocol handlers
194 * MUST start from promiscous ptype_all chain in net_bh.
195 * It is true now, do not change it.
196 * Explantion follows: if protocol handler, mangling packet, will
197 * be the first on list, it is not able to sense, that packet
198 * is cloned and should be copied-on-write, so that it will
199 * change it and subsequent readers will get broken packet.
204 * dev_add_pack - add packet handler
205 * @pt: packet type declaration
207 * Add a protocol handler to the networking stack. The passed &packet_type
208 * is linked into kernel lists and may not be freed until it has been
209 * removed from the kernel lists.
212 void dev_add_pack(struct packet_type
*pt
)
216 br_write_lock_bh(BR_NETPROTO_LOCK
);
218 #ifdef CONFIG_NET_FASTROUTE
219 /* Hack to detect packet socket */
221 netdev_fastroute_obstacles
++;
222 dev_clear_fastroute(pt
->dev
);
225 if (pt
->type
== htons(ETH_P_ALL
)) {
230 hash
=ntohs(pt
->type
)&15;
231 pt
->next
= ptype_base
[hash
];
232 ptype_base
[hash
] = pt
;
234 br_write_unlock_bh(BR_NETPROTO_LOCK
);
239 * dev_remove_pack - remove packet handler
240 * @pt: packet type declaration
242 * Remove a protocol handler that was previously added to the kernel
243 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
244 * from the kernel lists and can be freed or reused once this function
248 void dev_remove_pack(struct packet_type
*pt
)
250 struct packet_type
**pt1
;
252 br_write_lock_bh(BR_NETPROTO_LOCK
);
254 if (pt
->type
== htons(ETH_P_ALL
)) {
258 pt1
=&ptype_base
[ntohs(pt
->type
)&15];
261 for (; (*pt1
) != NULL
; pt1
= &((*pt1
)->next
)) {
264 #ifdef CONFIG_NET_FASTROUTE
266 netdev_fastroute_obstacles
--;
268 br_write_unlock_bh(BR_NETPROTO_LOCK
);
272 br_write_unlock_bh(BR_NETPROTO_LOCK
);
273 printk(KERN_WARNING
"dev_remove_pack: %p not found.\n", pt
);
276 /******************************************************************************
278 Device Boot-time Settings Routines
280 *******************************************************************************/
282 /* Boot time configuration table */
283 static struct netdev_boot_setup dev_boot_setup
[NETDEV_BOOT_SETUP_MAX
];
286 * netdev_boot_setup_add - add new setup entry
287 * @name: name of the device
288 * @map: configured settings for the device
290 * Adds new setup entry to the dev_boot_setup list. The function
291 * returns 0 on error and 1 on success. This is a generic routine to
294 int netdev_boot_setup_add(char *name
, struct ifmap
*map
)
296 struct netdev_boot_setup
*s
;
300 for (i
= 0; i
< NETDEV_BOOT_SETUP_MAX
; i
++) {
301 if (s
[i
].name
[0] == '\0' || s
[i
].name
[0] == ' ') {
302 memset(s
[i
].name
, 0, sizeof(s
[i
].name
));
303 strcpy(s
[i
].name
, name
);
304 memcpy(&s
[i
].map
, map
, sizeof(s
[i
].map
));
309 if (i
>= NETDEV_BOOT_SETUP_MAX
)
316 * netdev_boot_setup_check - check boot time settings
317 * @dev: the netdevice
319 * Check boot time settings for the device.
320 * The found settings are set for the device to be used
321 * later in the device probing.
322 * Returns 0 if no settings found, 1 if they are.
324 int netdev_boot_setup_check(struct net_device
*dev
)
326 struct netdev_boot_setup
*s
;
330 for (i
= 0; i
< NETDEV_BOOT_SETUP_MAX
; i
++) {
331 if (s
[i
].name
[0] != '\0' && s
[i
].name
[0] != ' ' &&
332 !strncmp(dev
->name
, s
[i
].name
, strlen(s
[i
].name
))) {
333 dev
->irq
= s
[i
].map
.irq
;
334 dev
->base_addr
= s
[i
].map
.base_addr
;
335 dev
->mem_start
= s
[i
].map
.mem_start
;
336 dev
->mem_end
= s
[i
].map
.mem_end
;
344 * Saves at boot time configured settings for any netdevice.
346 static int __init
netdev_boot_setup(char *str
)
351 str
= get_options(str
, ARRAY_SIZE(ints
), ints
);
356 memset(&map
, -1, sizeof(map
));
360 map
.base_addr
= ints
[2];
362 map
.mem_start
= ints
[3];
364 map
.mem_end
= ints
[4];
366 /* Add new entry to the list */
367 return netdev_boot_setup_add(str
, &map
);
370 __setup("netdev=", netdev_boot_setup
);
372 /*****************************************************************************************
374 Device Interface Subroutines
376 ******************************************************************************************/
379 * __dev_get_by_name - find a device by its name
380 * @name: name to find
382 * Find an interface by name. Must be called under RTNL semaphore
383 * or @dev_base_lock. If the name is found a pointer to the device
384 * is returned. If the name is not found then %NULL is returned. The
385 * reference counters are not incremented so the caller must be
386 * careful with locks.
390 struct net_device
*__dev_get_by_name(const char *name
)
392 struct net_device
*dev
;
394 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
) {
395 if (strcmp(dev
->name
, name
) == 0)
402 * dev_get_by_name - find a device by its name
403 * @name: name to find
405 * Find an interface by name. This can be called from any
406 * context and does its own locking. The returned handle has
407 * the usage count incremented and the caller must use dev_put() to
408 * release it when it is no longer needed. %NULL is returned if no
409 * matching device is found.
412 struct net_device
*dev_get_by_name(const char *name
)
414 struct net_device
*dev
;
416 read_lock(&dev_base_lock
);
417 dev
= __dev_get_by_name(name
);
420 read_unlock(&dev_base_lock
);
425 Return value is changed to int to prevent illegal usage in future.
426 It is still legal to use to check for device existance.
428 User should understand, that the result returned by this function
429 is meaningless, if it was not issued under rtnl semaphore.
433 * dev_get - test if a device exists
434 * @name: name to test for
436 * Test if a name exists. Returns true if the name is found. In order
437 * to be sure the name is not allocated or removed during the test the
438 * caller must hold the rtnl semaphore.
440 * This function primarily exists for back compatibility with older
444 int dev_get(const char *name
)
446 struct net_device
*dev
;
448 read_lock(&dev_base_lock
);
449 dev
= __dev_get_by_name(name
);
450 read_unlock(&dev_base_lock
);
455 * __dev_get_by_index - find a device by its ifindex
456 * @ifindex: index of device
458 * Search for an interface by index. Returns %NULL if the device
459 * is not found or a pointer to the device. The device has not
460 * had its reference counter increased so the caller must be careful
461 * about locking. The caller must hold either the RTNL semaphore
465 struct net_device
* __dev_get_by_index(int ifindex
)
467 struct net_device
*dev
;
469 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
) {
470 if (dev
->ifindex
== ifindex
)
478 * dev_get_by_index - find a device by its ifindex
479 * @ifindex: index of device
481 * Search for an interface by index. Returns NULL if the device
482 * is not found or a pointer to the device. The device returned has
483 * had a reference added and the pointer is safe until the user calls
484 * dev_put to indicate they have finished with it.
487 struct net_device
* dev_get_by_index(int ifindex
)
489 struct net_device
*dev
;
491 read_lock(&dev_base_lock
);
492 dev
= __dev_get_by_index(ifindex
);
495 read_unlock(&dev_base_lock
);
500 * dev_getbyhwaddr - find a device by its hardware addres
501 * @type: media type of device
502 * @ha: hardware address
504 * Search for an interface by MAC address. Returns NULL if the device
505 * is not found or a pointer to the device. The caller must hold the
506 * rtnl semaphore. The returned device has not had its ref count increased
507 * and the caller must therefore be careful about locking
510 * If the API was consistent this would be __dev_get_by_hwaddr
513 struct net_device
*dev_getbyhwaddr(unsigned short type
, char *ha
)
515 struct net_device
*dev
;
519 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
) {
520 if (dev
->type
== type
&&
521 memcmp(dev
->dev_addr
, ha
, dev
->addr_len
) == 0)
528 * dev_alloc_name - allocate a name for a device
530 * @name: name format string
532 * Passed a format string - eg "lt%d" it will try and find a suitable
533 * id. Not efficient for many devices, not called a lot. The caller
534 * must hold the dev_base or rtnl lock while allocating the name and
535 * adding the device in order to avoid duplicates. Returns the number
536 * of the unit assigned or a negative errno code.
539 int dev_alloc_name(struct net_device
*dev
, const char *name
)
545 * If you need over 100 please also fix the algorithm...
547 for (i
= 0; i
< 100; i
++) {
549 if (__dev_get_by_name(buf
) == NULL
) {
550 strcpy(dev
->name
, buf
);
554 return -ENFILE
; /* Over 100 of the things .. bail out! */
558 * dev_alloc - allocate a network device and name
559 * @name: name format string
560 * @err: error return pointer
562 * Passed a format string, eg. "lt%d", it will allocate a network device
563 * and space for the name. %NULL is returned if no memory is available.
564 * If the allocation succeeds then the name is assigned and the
565 * device pointer returned. %NULL is returned if the name allocation
566 * failed. The cause of an error is returned as a negative errno code
567 * in the variable @err points to.
569 * The caller must hold the @dev_base or RTNL locks when doing this in
570 * order to avoid duplicate name allocations.
573 struct net_device
*dev_alloc(const char *name
, int *err
)
575 struct net_device
*dev
=kmalloc(sizeof(struct net_device
), GFP_KERNEL
);
580 memset(dev
, 0, sizeof(struct net_device
));
581 *err
= dev_alloc_name(dev
, name
);
590 * netdev_state_change - device changes state
591 * @dev: device to cause notification
593 * Called to indicate a device has changed state. This function calls
594 * the notifier chains for netdev_chain and sends a NEWLINK message
595 * to the routing socket.
598 void netdev_state_change(struct net_device
*dev
)
600 if (dev
->flags
&IFF_UP
) {
601 notifier_call_chain(&netdev_chain
, NETDEV_CHANGE
, dev
);
602 rtmsg_ifinfo(RTM_NEWLINK
, dev
, 0);
610 * dev_load - load a network module
611 * @name: name of interface
613 * If a network interface is not present and the process has suitable
614 * privileges this function loads the module. If module loading is not
615 * available in this kernel then it becomes a nop.
618 void dev_load(const char *name
)
620 if (!__dev_get_by_name(name
) && capable(CAP_SYS_MODULE
))
621 request_module(name
);
626 extern inline void dev_load(const char *unused
){;}
630 static int default_rebuild_header(struct sk_buff
*skb
)
632 printk(KERN_DEBUG
"%s: default_rebuild_header called -- BUG!\n", skb
->dev
? skb
->dev
->name
: "NULL!!!");
638 * dev_open - prepare an interface for use.
639 * @dev: device to open
641 * Takes a device from down to up state. The device's private open
642 * function is invoked and then the multicast lists are loaded. Finally
643 * the device is moved into the up state and a %NETDEV_UP message is
644 * sent to the netdev notifier chain.
646 * Calling this function on an active interface is a nop. On a failure
647 * a negative errno code is returned.
650 int dev_open(struct net_device
*dev
)
658 if (dev
->flags
&IFF_UP
)
662 * Is it even present?
664 if (!netif_device_present(dev
))
668 * Call device private open method
670 if (try_inc_mod_count(dev
->owner
)) {
672 ret
= dev
->open(dev
);
673 if (ret
!= 0 && dev
->owner
)
674 __MOD_DEC_USE_COUNT(dev
->owner
);
681 * If it went open OK then:
689 dev
->flags
|= IFF_UP
;
691 set_bit(__LINK_STATE_START
, &dev
->state
);
694 * Initialize multicasting status
699 * Wakeup transmit queue engine
704 * ... and announce new interface.
706 notifier_call_chain(&netdev_chain
, NETDEV_UP
, dev
);
711 #ifdef CONFIG_NET_FASTROUTE
713 static void dev_do_clear_fastroute(struct net_device
*dev
)
715 if (dev
->accept_fastpath
) {
718 for (i
=0; i
<=NETDEV_FASTROUTE_HMASK
; i
++) {
719 struct dst_entry
*dst
;
721 write_lock_irq(&dev
->fastpath_lock
);
722 dst
= dev
->fastpath
[i
];
723 dev
->fastpath
[i
] = NULL
;
724 write_unlock_irq(&dev
->fastpath_lock
);
731 void dev_clear_fastroute(struct net_device
*dev
)
734 dev_do_clear_fastroute(dev
);
736 read_lock(&dev_base_lock
);
737 for (dev
= dev_base
; dev
; dev
= dev
->next
)
738 dev_do_clear_fastroute(dev
);
739 read_unlock(&dev_base_lock
);
745 * dev_close - shutdown an interface.
746 * @dev: device to shutdown
748 * This function moves an active device into down state. A
749 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
750 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
754 int dev_close(struct net_device
*dev
)
756 if (!(dev
->flags
&IFF_UP
))
760 * Tell people we are going down, so that they can
761 * prepare to death, when device is still operating.
763 notifier_call_chain(&netdev_chain
, NETDEV_GOING_DOWN
, dev
);
767 clear_bit(__LINK_STATE_START
, &dev
->state
);
770 * Call the device specific close. This cannot fail.
771 * Only if device is UP
773 * We allow it to be called even after a DETACH hot-plug
781 * Device is now down.
784 dev
->flags
&= ~IFF_UP
;
785 #ifdef CONFIG_NET_FASTROUTE
786 dev_clear_fastroute(dev
);
790 * Tell people we are down
792 notifier_call_chain(&netdev_chain
, NETDEV_DOWN
, dev
);
795 * Drop the module refcount
798 __MOD_DEC_USE_COUNT(dev
->owner
);
805 * Device change register/unregister. These are not inline or static
806 * as we export them to the world.
810 * register_netdevice_notifier - register a network notifier block
813 * Register a notifier to be called when network device events occur.
814 * The notifier passed is linked into the kernel structures and must
815 * not be reused until it has been unregistered. A negative errno code
816 * is returned on a failure.
819 int register_netdevice_notifier(struct notifier_block
*nb
)
821 return notifier_chain_register(&netdev_chain
, nb
);
825 * unregister_netdevice_notifier - unregister a network notifier block
828 * Unregister a notifier previously registered by
829 * register_netdevice_notifier(). The notifier is unlinked into the
830 * kernel structures and may then be reused. A negative errno code
831 * is returned on a failure.
834 int unregister_netdevice_notifier(struct notifier_block
*nb
)
836 return notifier_chain_unregister(&netdev_chain
,nb
);
840 * Support routine. Sends outgoing frames to any network
841 * taps currently in use.
844 void dev_queue_xmit_nit(struct sk_buff
*skb
, struct net_device
*dev
)
846 struct packet_type
*ptype
;
847 get_fast_time(&skb
->stamp
);
849 br_read_lock(BR_NETPROTO_LOCK
);
850 for (ptype
= ptype_all
; ptype
!=NULL
; ptype
= ptype
->next
)
852 /* Never send packets back to the socket
853 * they originated from - MvS (miquels@drinkel.ow.org)
855 if ((ptype
->dev
== dev
|| !ptype
->dev
) &&
856 ((struct sock
*)ptype
->data
!= skb
->sk
))
858 struct sk_buff
*skb2
;
859 if ((skb2
= skb_clone(skb
, GFP_ATOMIC
)) == NULL
)
862 /* skb->nh should be correctly
863 set by sender, so that the second statement is
864 just protection against buggy protocols.
866 skb2
->mac
.raw
= skb2
->data
;
868 if (skb2
->nh
.raw
< skb2
->data
|| skb2
->nh
.raw
>= skb2
->tail
) {
870 printk(KERN_DEBUG
"protocol %04x is buggy, dev %s\n", skb2
->protocol
, dev
->name
);
871 skb2
->nh
.raw
= skb2
->data
;
872 if (dev
->hard_header
)
873 skb2
->nh
.raw
+= dev
->hard_header_len
;
876 skb2
->h
.raw
= skb2
->nh
.raw
;
877 skb2
->pkt_type
= PACKET_OUTGOING
;
878 skb2
->rx_dev
= skb
->dev
;
879 dev_hold(skb2
->rx_dev
);
880 ptype
->func(skb2
, skb
->dev
, ptype
);
883 br_read_unlock(BR_NETPROTO_LOCK
);
887 * dev_queue_xmit - transmit a buffer
888 * @skb: buffer to transmit
890 * Queue a buffer for transmission to a network device. The caller must
891 * have set the device and priority and built the buffer before calling this
892 * function. The function can be called from an interrupt.
894 * A negative errno code is returned on a failure. A success does not
895 * guarantee the frame will be transmitted as it may be dropped due
896 * to congestion or traffic shaping.
899 int dev_queue_xmit(struct sk_buff
*skb
)
901 struct net_device
*dev
= skb
->dev
;
904 /* Grab device queue */
905 spin_lock_bh(&dev
->queue_lock
);
908 int ret
= q
->enqueue(skb
, q
);
912 spin_unlock_bh(&dev
->queue_lock
);
913 return ret
== NET_XMIT_BYPASS
? NET_XMIT_SUCCESS
: ret
;
916 /* The device has no queue. Common case for software devices:
917 loopback, all the sorts of tunnels...
919 Really, it is unlikely that xmit_lock protection is necessary here.
920 (f.e. loopback and IP tunnels are clean ignoring statistics counters.)
921 However, it is possible, that they rely on protection
924 Check this and shot the lock. It is not prone from deadlocks.
925 Either shot noqueue qdisc, it is even simpler 8)
927 if (dev
->flags
&IFF_UP
) {
928 int cpu
= smp_processor_id();
930 if (dev
->xmit_lock_owner
!= cpu
) {
931 spin_unlock(&dev
->queue_lock
);
932 spin_lock(&dev
->xmit_lock
);
933 dev
->xmit_lock_owner
= cpu
;
935 if (!netif_queue_stopped(dev
)) {
937 dev_queue_xmit_nit(skb
,dev
);
939 if (dev
->hard_start_xmit(skb
, dev
) == 0) {
940 dev
->xmit_lock_owner
= -1;
941 spin_unlock_bh(&dev
->xmit_lock
);
945 dev
->xmit_lock_owner
= -1;
946 spin_unlock_bh(&dev
->xmit_lock
);
948 printk(KERN_DEBUG
"Virtual device %s asks to queue packet!\n", dev
->name
);
952 /* Recursion is detected! It is possible, unfortunately */
954 printk(KERN_DEBUG
"Dead loop on virtual device %s, fix it urgently!\n", dev
->name
);
957 spin_unlock_bh(&dev
->queue_lock
);
964 /*=======================================================================
966 =======================================================================*/
968 int netdev_max_backlog
= 300;
969 /* These numbers are selected based on intuition and some
970 * experimentatiom, if you have more scientific way of doing this
971 * please go ahead and fix things.
973 int no_cong_thresh
= 10;
978 struct netif_rx_stats netdev_rx_stat
[NR_CPUS
];
981 #ifdef CONFIG_NET_HW_FLOWCONTROL
982 atomic_t netdev_dropping
= ATOMIC_INIT(0);
983 static unsigned long netdev_fc_mask
= 1;
984 unsigned long netdev_fc_xoff
= 0;
985 spinlock_t netdev_fc_lock
= SPIN_LOCK_UNLOCKED
;
989 void (*stimul
)(struct net_device
*);
990 struct net_device
*dev
;
991 } netdev_fc_slots
[32];
993 int netdev_register_fc(struct net_device
*dev
, void (*stimul
)(struct net_device
*dev
))
998 spin_lock_irqsave(&netdev_fc_lock
, flags
);
999 if (netdev_fc_mask
!= ~0UL) {
1000 bit
= ffz(netdev_fc_mask
);
1001 netdev_fc_slots
[bit
].stimul
= stimul
;
1002 netdev_fc_slots
[bit
].dev
= dev
;
1003 set_bit(bit
, &netdev_fc_mask
);
1004 clear_bit(bit
, &netdev_fc_xoff
);
1006 spin_unlock_irqrestore(&netdev_fc_lock
, flags
);
1010 void netdev_unregister_fc(int bit
)
1012 unsigned long flags
;
1014 spin_lock_irqsave(&netdev_fc_lock
, flags
);
1016 netdev_fc_slots
[bit
].stimul
= NULL
;
1017 netdev_fc_slots
[bit
].dev
= NULL
;
1018 clear_bit(bit
, &netdev_fc_mask
);
1019 clear_bit(bit
, &netdev_fc_xoff
);
1021 spin_unlock_irqrestore(&netdev_fc_lock
, flags
);
1024 static void netdev_wakeup(void)
1028 spin_lock(&netdev_fc_lock
);
1029 xoff
= netdev_fc_xoff
;
1034 netdev_fc_slots
[i
].stimul(netdev_fc_slots
[i
].dev
);
1036 spin_unlock(&netdev_fc_lock
);
1040 static void get_sample_stats(int cpu
)
1046 int blog
= softnet_data
[cpu
].input_pkt_queue
.qlen
;
1047 int avg_blog
= softnet_data
[cpu
].avg_blog
;
1049 avg_blog
= (avg_blog
>> 1)+ (blog
>> 1);
1051 if (avg_blog
> mod_cong
) {
1052 /* Above moderate congestion levels. */
1053 softnet_data
[cpu
].cng_level
= NET_RX_CN_HIGH
;
1056 rq
= rd
% netdev_max_backlog
;
1057 if (rq
< avg_blog
) /* unlucky bastard */
1058 softnet_data
[cpu
].cng_level
= NET_RX_DROP
;
1060 } else if (avg_blog
> lo_cong
) {
1061 softnet_data
[cpu
].cng_level
= NET_RX_CN_MOD
;
1064 rq
= rd
% netdev_max_backlog
;
1065 if (rq
< avg_blog
) /* unlucky bastard */
1066 softnet_data
[cpu
].cng_level
= NET_RX_CN_HIGH
;
1068 } else if (avg_blog
> no_cong
)
1069 softnet_data
[cpu
].cng_level
= NET_RX_CN_LOW
;
1070 else /* no congestion */
1071 softnet_data
[cpu
].cng_level
= NET_RX_SUCCESS
;
1073 softnet_data
[cpu
].avg_blog
= avg_blog
;
1076 #ifdef OFFLINE_SAMPLE
1077 static void sample_queue(unsigned long dummy
)
1079 /* 10 ms 0r 1ms -- i dont care -- JHS */
1081 int cpu
= smp_processor_id();
1083 get_sample_stats(cpu
);
1084 next_tick
+= jiffies
;
1085 mod_timer(&samp_timer
, next_tick
);
1091 * netif_rx - post buffer to the network code
1092 * @skb: buffer to post
1094 * This function receives a packet from a device driver and queues it for
1095 * the upper (protocol) levels to process. It always succeeds. The buffer
1096 * may be dropped during processing for congestion control or by the
1100 * NET_RX_SUCCESS (no congestion)
1101 * NET_RX_CN_LOW (low congestion)
1102 * NET_RX_CN_MOD (moderate congestion)
1103 * NET_RX_CN_HIGH (high congestion)
1104 * NET_RX_DROP (packet was dropped)
1109 int netif_rx(struct sk_buff
*skb
)
1111 int this_cpu
= smp_processor_id();
1112 struct softnet_data
*queue
;
1113 unsigned long flags
;
1115 if (skb
->stamp
.tv_sec
== 0)
1116 get_fast_time(&skb
->stamp
);
1118 /* The code is rearranged so that the path is the most
1119 short when CPU is congested, but is still operating.
1121 queue
= &softnet_data
[this_cpu
];
1123 local_irq_save(flags
);
1125 netdev_rx_stat
[this_cpu
].total
++;
1126 if (queue
->input_pkt_queue
.qlen
<= netdev_max_backlog
) {
1127 if (queue
->input_pkt_queue
.qlen
) {
1128 if (queue
->throttle
)
1133 dev_put(skb
->rx_dev
);
1134 skb
->rx_dev
= skb
->dev
;
1135 dev_hold(skb
->rx_dev
);
1136 __skb_queue_tail(&queue
->input_pkt_queue
,skb
);
1137 __cpu_raise_softirq(this_cpu
, NET_RX_SOFTIRQ
);
1138 local_irq_restore(flags
);
1139 #ifndef OFFLINE_SAMPLE
1140 get_sample_stats(this_cpu
);
1142 return softnet_data
[this_cpu
].cng_level
;
1145 if (queue
->throttle
) {
1146 queue
->throttle
= 0;
1147 #ifdef CONFIG_NET_HW_FLOWCONTROL
1148 if (atomic_dec_and_test(&netdev_dropping
))
1155 if (queue
->throttle
== 0) {
1156 queue
->throttle
= 1;
1157 netdev_rx_stat
[this_cpu
].throttled
++;
1158 #ifdef CONFIG_NET_HW_FLOWCONTROL
1159 atomic_inc(&netdev_dropping
);
1164 netdev_rx_stat
[this_cpu
].dropped
++;
1165 local_irq_restore(flags
);
1171 /* Deliver skb to an old protocol, which is not threaded well
1172 or which do not understand shared skbs.
1174 static int deliver_to_old_ones(struct packet_type
*pt
, struct sk_buff
*skb
, int last
)
1176 static spinlock_t net_bh_lock
= SPIN_LOCK_UNLOCKED
;
1177 int ret
= NET_RX_DROP
;
1181 skb
= skb_clone(skb
, GFP_ATOMIC
);
1186 /* The assumption (correct one) is that old protocols
1187 did not depened on BHs different of NET_BH and TIMER_BH.
1190 /* Emulate NET_BH with special spinlock */
1191 spin_lock(&net_bh_lock
);
1193 /* Disable timers and wait for all timers completion */
1194 tasklet_disable(bh_task_vec
+TIMER_BH
);
1196 ret
= pt
->func(skb
, skb
->dev
, pt
);
1198 tasklet_enable(bh_task_vec
+TIMER_BH
);
1199 spin_unlock(&net_bh_lock
);
1203 /* Reparent skb to master device. This function is called
1204 * only from net_rx_action under BR_NETPROTO_LOCK. It is misuse
1205 * of BR_NETPROTO_LOCK, but it is OK for now.
1207 static __inline__
void skb_bond(struct sk_buff
*skb
)
1209 struct net_device
*dev
= skb
->rx_dev
;
1212 dev_hold(dev
->master
);
1213 skb
->dev
= skb
->rx_dev
= dev
->master
;
1218 static void net_tx_action(struct softirq_action
*h
)
1220 int cpu
= smp_processor_id();
1222 if (softnet_data
[cpu
].completion_queue
) {
1223 struct sk_buff
*clist
;
1225 local_irq_disable();
1226 clist
= softnet_data
[cpu
].completion_queue
;
1227 softnet_data
[cpu
].completion_queue
= NULL
;
1230 while (clist
!= NULL
) {
1231 struct sk_buff
*skb
= clist
;
1232 clist
= clist
->next
;
1234 BUG_TRAP(atomic_read(&skb
->users
) == 0);
1239 if (softnet_data
[cpu
].output_queue
) {
1240 struct net_device
*head
;
1242 local_irq_disable();
1243 head
= softnet_data
[cpu
].output_queue
;
1244 softnet_data
[cpu
].output_queue
= NULL
;
1247 while (head
!= NULL
) {
1248 struct net_device
*dev
= head
;
1249 head
= head
->next_sched
;
1251 smp_mb__before_clear_bit();
1252 clear_bit(__LINK_STATE_SCHED
, &dev
->state
);
1254 if (spin_trylock(&dev
->queue_lock
)) {
1256 spin_unlock(&dev
->queue_lock
);
1258 netif_schedule(dev
);
1265 * net_call_rx_atomic
1266 * @fn: function to call
1268 * Make a function call that is atomic with respect to the protocol
1272 void net_call_rx_atomic(void (*fn
)(void))
1274 br_write_lock_bh(BR_NETPROTO_LOCK
);
1276 br_write_unlock_bh(BR_NETPROTO_LOCK
);
1279 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
1280 void (*br_handle_frame_hook
)(struct sk_buff
*skb
) = NULL
;
1283 static int __inline__
handle_bridge(struct sk_buff
*skb
,
1284 struct packet_type
*pt_prev
)
1286 int ret
= NET_RX_DROP
;
1290 ret
= deliver_to_old_ones(pt_prev
, skb
, 0);
1292 atomic_inc(&skb
->users
);
1293 ret
= pt_prev
->func(skb
, skb
->dev
, pt_prev
);
1297 br_handle_frame_hook(skb
);
1302 #ifdef CONFIG_NET_DIVERT
1303 static inline void handle_diverter(struct sk_buff
*skb
)
1305 /* if diversion is supported on device, then divert */
1306 if (skb
->dev
->divert
&& skb
->dev
->divert
->divert
)
1309 #endif /* CONFIG_NET_DIVERT */
1312 static void net_rx_action(struct softirq_action
*h
)
1314 int this_cpu
= smp_processor_id();
1315 struct softnet_data
*queue
= &softnet_data
[this_cpu
];
1316 unsigned long start_time
= jiffies
;
1317 int bugdet
= netdev_max_backlog
;
1319 br_read_lock(BR_NETPROTO_LOCK
);
1322 struct sk_buff
*skb
;
1324 local_irq_disable();
1325 skb
= __skb_dequeue(&queue
->input_pkt_queue
);
1333 #ifdef CONFIG_NET_FASTROUTE
1334 if (skb
->pkt_type
== PACKET_FASTROUTE
) {
1335 netdev_rx_stat
[this_cpu
].fastroute_deferred_out
++;
1336 dev_queue_xmit(skb
);
1340 skb
->h
.raw
= skb
->nh
.raw
= skb
->data
;
1342 struct packet_type
*ptype
, *pt_prev
;
1343 unsigned short type
= skb
->protocol
;
1346 for (ptype
= ptype_all
; ptype
; ptype
= ptype
->next
) {
1347 if (!ptype
->dev
|| ptype
->dev
== skb
->dev
) {
1349 if (!pt_prev
->data
) {
1350 deliver_to_old_ones(pt_prev
, skb
, 0);
1352 atomic_inc(&skb
->users
);
1362 #ifdef CONFIG_NET_DIVERT
1363 if (skb
->dev
->divert
&& skb
->dev
->divert
->divert
)
1364 handle_diverter(skb
);
1365 #endif /* CONFIG_NET_DIVERT */
1368 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
1369 if (skb
->dev
->br_port
!= NULL
&&
1370 br_handle_frame_hook
!= NULL
) {
1371 handle_bridge(skb
, pt_prev
);
1376 for (ptype
=ptype_base
[ntohs(type
)&15];ptype
;ptype
=ptype
->next
) {
1377 if (ptype
->type
== type
&&
1378 (!ptype
->dev
|| ptype
->dev
== skb
->dev
)) {
1381 deliver_to_old_ones(pt_prev
, skb
, 0);
1383 atomic_inc(&skb
->users
);
1395 deliver_to_old_ones(pt_prev
, skb
, 1);
1397 pt_prev
->func(skb
, skb
->dev
, pt_prev
);
1402 if (bugdet
-- < 0 || jiffies
- start_time
> 1)
1405 #ifdef CONFIG_NET_HW_FLOWCONTROL
1406 if (queue
->throttle
&& queue
->input_pkt_queue
.qlen
< no_cong_thresh
) {
1407 if (atomic_dec_and_test(&netdev_dropping
)) {
1408 queue
->throttle
= 0;
1416 br_read_unlock(BR_NETPROTO_LOCK
);
1418 local_irq_disable();
1419 if (queue
->throttle
) {
1420 queue
->throttle
= 0;
1421 #ifdef CONFIG_NET_HW_FLOWCONTROL
1422 if (atomic_dec_and_test(&netdev_dropping
))
1428 NET_PROFILE_LEAVE(softnet_process
);
1432 br_read_unlock(BR_NETPROTO_LOCK
);
1434 local_irq_disable();
1435 netdev_rx_stat
[this_cpu
].time_squeeze
++;
1436 __cpu_raise_softirq(this_cpu
, NET_RX_SOFTIRQ
);
1439 NET_PROFILE_LEAVE(softnet_process
);
1443 static gifconf_func_t
* gifconf_list
[NPROTO
];
1446 * register_gifconf - register a SIOCGIF handler
1447 * @family: Address family
1448 * @gifconf: Function handler
1450 * Register protocol dependent address dumping routines. The handler
1451 * that is passed must not be freed or reused until it has been replaced
1452 * by another handler.
1455 int register_gifconf(unsigned int family
, gifconf_func_t
* gifconf
)
1459 gifconf_list
[family
] = gifconf
;
1465 * Map an interface index to its name (SIOCGIFNAME)
1469 * We need this ioctl for efficient implementation of the
1470 * if_indextoname() function required by the IPv6 API. Without
1471 * it, we would have to search all the interfaces to find a
1475 static int dev_ifname(struct ifreq
*arg
)
1477 struct net_device
*dev
;
1481 * Fetch the caller's info block.
1484 if (copy_from_user(&ifr
, arg
, sizeof(struct ifreq
)))
1487 read_lock(&dev_base_lock
);
1488 dev
= __dev_get_by_index(ifr
.ifr_ifindex
);
1490 read_unlock(&dev_base_lock
);
1494 strcpy(ifr
.ifr_name
, dev
->name
);
1495 read_unlock(&dev_base_lock
);
1497 if (copy_to_user(arg
, &ifr
, sizeof(struct ifreq
)))
1503 * Perform a SIOCGIFCONF call. This structure will change
1504 * size eventually, and there is nothing I can do about it.
1505 * Thus we will need a 'compatibility mode'.
1508 static int dev_ifconf(char *arg
)
1511 struct net_device
*dev
;
1518 * Fetch the caller's info block.
1521 if (copy_from_user(&ifc
, arg
, sizeof(struct ifconf
)))
1528 * Loop over the interfaces, and write an info block for each.
1532 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
) {
1533 for (i
=0; i
<NPROTO
; i
++) {
1534 if (gifconf_list
[i
]) {
1537 done
= gifconf_list
[i
](dev
, NULL
, 0);
1539 done
= gifconf_list
[i
](dev
, pos
+total
, len
-total
);
1550 * All done. Write the updated control block back to the caller.
1552 ifc
.ifc_len
= total
;
1554 if (copy_to_user(arg
, &ifc
, sizeof(struct ifconf
)))
1558 * Both BSD and Solaris return 0 here, so we do too.
1564 * This is invoked by the /proc filesystem handler to display a device
1568 #ifdef CONFIG_PROC_FS
1570 static int sprintf_stats(char *buffer
, struct net_device
*dev
)
1572 struct net_device_stats
*stats
= (dev
->get_stats
? dev
->get_stats(dev
): NULL
);
1576 size
= sprintf(buffer
, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu %8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
1579 stats
->rx_packets
, stats
->rx_errors
,
1580 stats
->rx_dropped
+ stats
->rx_missed_errors
,
1581 stats
->rx_fifo_errors
,
1582 stats
->rx_length_errors
+ stats
->rx_over_errors
1583 + stats
->rx_crc_errors
+ stats
->rx_frame_errors
,
1584 stats
->rx_compressed
, stats
->multicast
,
1586 stats
->tx_packets
, stats
->tx_errors
, stats
->tx_dropped
,
1587 stats
->tx_fifo_errors
, stats
->collisions
,
1588 stats
->tx_carrier_errors
+ stats
->tx_aborted_errors
1589 + stats
->tx_window_errors
+ stats
->tx_heartbeat_errors
,
1590 stats
->tx_compressed
);
1592 size
= sprintf(buffer
, "%6s: No statistics available.\n", dev
->name
);
1598 * Called from the PROCfs module. This now uses the new arbitrary sized /proc/net interface
1599 * to create /proc/net/dev
1602 static int dev_get_info(char *buffer
, char **start
, off_t offset
, int length
)
1608 struct net_device
*dev
;
1611 size
= sprintf(buffer
,
1612 "Inter-| Receive | Transmit\n"
1613 " face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed\n");
1619 read_lock(&dev_base_lock
);
1620 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
) {
1621 size
= sprintf_stats(buffer
+len
, dev
);
1629 if (pos
> offset
+ length
)
1632 read_unlock(&dev_base_lock
);
1634 *start
= buffer
+ (offset
- begin
); /* Start of wanted data */
1635 len
-= (offset
- begin
); /* Start slop */
1637 len
= length
; /* Ending slop */
1643 static int dev_proc_stats(char *buffer
, char **start
, off_t offset
,
1644 int length
, int *eof
, void *data
)
1649 for (lcpu
=0; lcpu
<smp_num_cpus
; lcpu
++) {
1650 i
= cpu_logical_map(lcpu
);
1651 len
+= sprintf(buffer
+len
, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
1652 netdev_rx_stat
[i
].total
,
1653 netdev_rx_stat
[i
].dropped
,
1654 netdev_rx_stat
[i
].time_squeeze
,
1655 netdev_rx_stat
[i
].throttled
,
1656 netdev_rx_stat
[i
].fastroute_hit
,
1657 netdev_rx_stat
[i
].fastroute_success
,
1658 netdev_rx_stat
[i
].fastroute_defer
,
1659 netdev_rx_stat
[i
].fastroute_deferred_out
,
1661 netdev_rx_stat
[i
].fastroute_latency_reduction
1663 netdev_rx_stat
[i
].cpu_collision
1675 *start
= buffer
+ offset
;
1681 #endif /* CONFIG_PROC_FS */
1685 #ifdef CONFIG_PROC_FS
1688 * Print one entry of /proc/net/wireless
1689 * This is a clone of /proc/net/dev (just above)
1691 static int sprintf_wireless_stats(char *buffer
, struct net_device
*dev
)
1693 /* Get stats from the driver */
1694 struct iw_statistics
*stats
= (dev
->get_wireless_stats
?
1695 dev
->get_wireless_stats(dev
) :
1696 (struct iw_statistics
*) NULL
);
1699 if (stats
!= (struct iw_statistics
*) NULL
) {
1700 size
= sprintf(buffer
,
1701 "%6s: %04x %3d%c %3d%c %3d%c %6d %6d %6d\n",
1705 stats
->qual
.updated
& 1 ? '.' : ' ',
1707 stats
->qual
.updated
& 2 ? '.' : ' ',
1709 stats
->qual
.updated
& 4 ? '.' : ' ',
1710 stats
->discard
.nwid
,
1711 stats
->discard
.code
,
1712 stats
->discard
.misc
);
1713 stats
->qual
.updated
= 0;
1722 * Print info for /proc/net/wireless (print all entries)
1723 * This is a clone of /proc/net/dev (just above)
1725 static int dev_get_wireless_info(char * buffer
, char **start
, off_t offset
,
1733 struct net_device
* dev
;
1735 size
= sprintf(buffer
,
1736 "Inter-| sta-| Quality | Discarded packets\n"
1737 " face | tus | link level noise | nwid crypt misc\n"
1743 read_lock(&dev_base_lock
);
1744 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
) {
1745 size
= sprintf_wireless_stats(buffer
+ len
, dev
);
1753 if (pos
> offset
+ length
)
1756 read_unlock(&dev_base_lock
);
1758 *start
= buffer
+ (offset
- begin
); /* Start of wanted data */
1759 len
-= (offset
- begin
); /* Start slop */
1761 len
= length
; /* Ending slop */
1767 #endif /* CONFIG_PROC_FS */
1768 #endif /* WIRELESS_EXT */
1771 * netdev_set_master - set up master/slave pair
1772 * @slave: slave device
1773 * @master: new master device
1775 * Changes the master device of the slave. Pass %NULL to break the
1776 * bonding. The caller must hold the RTNL semaphore. On a failure
1777 * a negative errno code is returned. On success the reference counts
1778 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
1779 * function returns zero.
1782 int netdev_set_master(struct net_device
*slave
, struct net_device
*master
)
1784 struct net_device
*old
= slave
->master
;
1794 br_write_lock_bh(BR_NETPROTO_LOCK
);
1795 slave
->master
= master
;
1796 br_write_unlock_bh(BR_NETPROTO_LOCK
);
1802 slave
->flags
|= IFF_SLAVE
;
1804 slave
->flags
&= ~IFF_SLAVE
;
1806 rtmsg_ifinfo(RTM_NEWLINK
, slave
, IFF_SLAVE
);
1811 * dev_set_promiscuity - update promiscuity count on a device
1815 * Add or remove promsicuity from a device. While the count in the device
1816 * remains above zero the interface remains promiscuous. Once it hits zero
1817 * the device reverts back to normal filtering operation. A negative inc
1818 * value is used to drop promiscuity on the device.
1821 void dev_set_promiscuity(struct net_device
*dev
, int inc
)
1823 unsigned short old_flags
= dev
->flags
;
1825 dev
->flags
|= IFF_PROMISC
;
1826 if ((dev
->promiscuity
+= inc
) == 0)
1827 dev
->flags
&= ~IFF_PROMISC
;
1828 if (dev
->flags
^old_flags
) {
1829 #ifdef CONFIG_NET_FASTROUTE
1830 if (dev
->flags
&IFF_PROMISC
) {
1831 netdev_fastroute_obstacles
++;
1832 dev_clear_fastroute(dev
);
1834 netdev_fastroute_obstacles
--;
1837 printk(KERN_INFO
"device %s %s promiscuous mode\n",
1838 dev
->name
, (dev
->flags
&IFF_PROMISC
) ? "entered" : "left");
1843 * dev_set_allmulti - update allmulti count on a device
1847 * Add or remove reception of all multicast frames to a device. While the
1848 * count in the device remains above zero the interface remains listening
1849 * to all interfaces. Once it hits zero the device reverts back to normal
1850 * filtering operation. A negative @inc value is used to drop the counter
1851 * when releasing a resource needing all multicasts.
1854 void dev_set_allmulti(struct net_device
*dev
, int inc
)
1856 unsigned short old_flags
= dev
->flags
;
1858 dev
->flags
|= IFF_ALLMULTI
;
1859 if ((dev
->allmulti
+= inc
) == 0)
1860 dev
->flags
&= ~IFF_ALLMULTI
;
1861 if (dev
->flags
^old_flags
)
1865 int dev_change_flags(struct net_device
*dev
, unsigned flags
)
1868 int old_flags
= dev
->flags
;
1871 * Set the flags on our device.
1874 dev
->flags
= (flags
& (IFF_DEBUG
|IFF_NOTRAILERS
|IFF_NOARP
|IFF_DYNAMIC
|
1875 IFF_MULTICAST
|IFF_PORTSEL
|IFF_AUTOMEDIA
)) |
1876 (dev
->flags
& (IFF_UP
|IFF_VOLATILE
|IFF_PROMISC
|IFF_ALLMULTI
));
1879 * Load in the correct multicast list now the flags have changed.
1885 * Have we downed the interface. We handle IFF_UP ourselves
1886 * according to user attempts to set it, rather than blindly
1891 if ((old_flags
^flags
)&IFF_UP
) /* Bit is different ? */
1893 ret
= ((old_flags
& IFF_UP
) ? dev_close
: dev_open
)(dev
);
1899 if (dev
->flags
&IFF_UP
&&
1900 ((old_flags
^dev
->flags
)&~(IFF_UP
|IFF_PROMISC
|IFF_ALLMULTI
|IFF_VOLATILE
)))
1901 notifier_call_chain(&netdev_chain
, NETDEV_CHANGE
, dev
);
1903 if ((flags
^dev
->gflags
)&IFF_PROMISC
) {
1904 int inc
= (flags
&IFF_PROMISC
) ? +1 : -1;
1905 dev
->gflags
^= IFF_PROMISC
;
1906 dev_set_promiscuity(dev
, inc
);
1909 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
1910 is important. Some (broken) drivers set IFF_PROMISC, when
1911 IFF_ALLMULTI is requested not asking us and not reporting.
1913 if ((flags
^dev
->gflags
)&IFF_ALLMULTI
) {
1914 int inc
= (flags
&IFF_ALLMULTI
) ? +1 : -1;
1915 dev
->gflags
^= IFF_ALLMULTI
;
1916 dev_set_allmulti(dev
, inc
);
1919 if (old_flags
^dev
->flags
)
1920 rtmsg_ifinfo(RTM_NEWLINK
, dev
, old_flags
^dev
->flags
);
1926 * Perform the SIOCxIFxxx calls.
1929 static int dev_ifsioc(struct ifreq
*ifr
, unsigned int cmd
)
1931 struct net_device
*dev
;
1934 if ((dev
= __dev_get_by_name(ifr
->ifr_name
)) == NULL
)
1939 case SIOCGIFFLAGS
: /* Get interface flags */
1940 ifr
->ifr_flags
= (dev
->flags
&~(IFF_PROMISC
|IFF_ALLMULTI
|IFF_RUNNING
))
1941 |(dev
->gflags
&(IFF_PROMISC
|IFF_ALLMULTI
));
1942 if (netif_running(dev
) && netif_carrier_ok(dev
))
1943 ifr
->ifr_flags
|= IFF_RUNNING
;
1946 case SIOCSIFFLAGS
: /* Set interface flags */
1947 return dev_change_flags(dev
, ifr
->ifr_flags
);
1949 case SIOCGIFMETRIC
: /* Get the metric on the interface (currently unused) */
1950 ifr
->ifr_metric
= 0;
1953 case SIOCSIFMETRIC
: /* Set the metric on the interface (currently unused) */
1956 case SIOCGIFMTU
: /* Get the MTU of a device */
1957 ifr
->ifr_mtu
= dev
->mtu
;
1960 case SIOCSIFMTU
: /* Set the MTU of a device */
1961 if (ifr
->ifr_mtu
== dev
->mtu
)
1965 * MTU must be positive.
1971 if (!netif_device_present(dev
))
1974 if (dev
->change_mtu
)
1975 err
= dev
->change_mtu(dev
, ifr
->ifr_mtu
);
1977 dev
->mtu
= ifr
->ifr_mtu
;
1980 if (!err
&& dev
->flags
&IFF_UP
)
1981 notifier_call_chain(&netdev_chain
, NETDEV_CHANGEMTU
, dev
);
1985 memcpy(ifr
->ifr_hwaddr
.sa_data
,dev
->dev_addr
, MAX_ADDR_LEN
);
1986 ifr
->ifr_hwaddr
.sa_family
=dev
->type
;
1990 if (dev
->set_mac_address
== NULL
)
1992 if (ifr
->ifr_hwaddr
.sa_family
!=dev
->type
)
1994 if (!netif_device_present(dev
))
1996 err
= dev
->set_mac_address(dev
, &ifr
->ifr_hwaddr
);
1998 notifier_call_chain(&netdev_chain
, NETDEV_CHANGEADDR
, dev
);
2001 case SIOCSIFHWBROADCAST
:
2002 if (ifr
->ifr_hwaddr
.sa_family
!=dev
->type
)
2004 memcpy(dev
->broadcast
, ifr
->ifr_hwaddr
.sa_data
, MAX_ADDR_LEN
);
2005 notifier_call_chain(&netdev_chain
, NETDEV_CHANGEADDR
, dev
);
2009 ifr
->ifr_map
.mem_start
=dev
->mem_start
;
2010 ifr
->ifr_map
.mem_end
=dev
->mem_end
;
2011 ifr
->ifr_map
.base_addr
=dev
->base_addr
;
2012 ifr
->ifr_map
.irq
=dev
->irq
;
2013 ifr
->ifr_map
.dma
=dev
->dma
;
2014 ifr
->ifr_map
.port
=dev
->if_port
;
2018 if (dev
->set_config
) {
2019 if (!netif_device_present(dev
))
2021 return dev
->set_config(dev
,&ifr
->ifr_map
);
2026 if (dev
->set_multicast_list
== NULL
||
2027 ifr
->ifr_hwaddr
.sa_family
!= AF_UNSPEC
)
2029 if (!netif_device_present(dev
))
2031 dev_mc_add(dev
,ifr
->ifr_hwaddr
.sa_data
, dev
->addr_len
, 1);
2035 if (dev
->set_multicast_list
== NULL
||
2036 ifr
->ifr_hwaddr
.sa_family
!=AF_UNSPEC
)
2038 if (!netif_device_present(dev
))
2040 dev_mc_delete(dev
,ifr
->ifr_hwaddr
.sa_data
,dev
->addr_len
, 1);
2044 ifr
->ifr_ifindex
= dev
->ifindex
;
2048 ifr
->ifr_qlen
= dev
->tx_queue_len
;
2052 if (ifr
->ifr_qlen
<0)
2054 dev
->tx_queue_len
= ifr
->ifr_qlen
;
2058 if (dev
->flags
&IFF_UP
)
2060 if (__dev_get_by_name(ifr
->ifr_newname
))
2062 memcpy(dev
->name
, ifr
->ifr_newname
, IFNAMSIZ
);
2063 dev
->name
[IFNAMSIZ
-1] = 0;
2064 notifier_call_chain(&netdev_chain
, NETDEV_CHANGENAME
, dev
);
2068 * Unknown or private ioctl
2072 if ((cmd
>= SIOCDEVPRIVATE
&&
2073 cmd
<= SIOCDEVPRIVATE
+ 15) ||
2074 cmd
== SIOCETHTOOL
) {
2075 if (dev
->do_ioctl
) {
2076 if (!netif_device_present(dev
))
2078 return dev
->do_ioctl(dev
, ifr
, cmd
);
2084 if (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
) {
2085 if (dev
->do_ioctl
) {
2086 if (!netif_device_present(dev
))
2088 return dev
->do_ioctl(dev
, ifr
, cmd
);
2092 #endif /* WIRELESS_EXT */
2099 * This function handles all "interface"-type I/O control requests. The actual
2100 * 'doing' part of this is dev_ifsioc above.
2104 * dev_ioctl - network device ioctl
2105 * @cmd: command to issue
2106 * @arg: pointer to a struct ifreq in user space
2108 * Issue ioctl functions to devices. This is normally called by the
2109 * user space syscall interfaces but can sometimes be useful for
2110 * other purposes. The return value is the return from the syscall if
2111 * positive or a negative errno code on error.
2114 int dev_ioctl(unsigned int cmd
, void *arg
)
2120 /* One special case: SIOCGIFCONF takes ifconf argument
2121 and requires shared lock, because it sleeps writing
2125 if (cmd
== SIOCGIFCONF
) {
2127 ret
= dev_ifconf((char *) arg
);
2131 if (cmd
== SIOCGIFNAME
) {
2132 return dev_ifname((struct ifreq
*)arg
);
2135 if (copy_from_user(&ifr
, arg
, sizeof(struct ifreq
)))
2138 ifr
.ifr_name
[IFNAMSIZ
-1] = 0;
2140 colon
= strchr(ifr
.ifr_name
, ':');
2145 * See which interface the caller is talking about.
2151 * These ioctl calls:
2152 * - can be done by all.
2153 * - atomic and do not require locking.
2165 dev_load(ifr
.ifr_name
);
2166 read_lock(&dev_base_lock
);
2167 ret
= dev_ifsioc(&ifr
, cmd
);
2168 read_unlock(&dev_base_lock
);
2172 if (copy_to_user(arg
, &ifr
, sizeof(struct ifreq
)))
2178 * These ioctl calls:
2179 * - require superuser power.
2180 * - require strict serialization.
2181 * - do not return a value
2192 case SIOCSIFHWBROADCAST
:
2195 if (!capable(CAP_NET_ADMIN
))
2197 dev_load(ifr
.ifr_name
);
2199 ret
= dev_ifsioc(&ifr
, cmd
);
2204 /* Get the per device memory space. We can add this but currently
2205 do not support it */
2207 /* Set the per device memory buffer space. Not applicable in our case */
2212 * Unknown or private ioctl.
2216 if ((cmd
>= SIOCDEVPRIVATE
&&
2217 cmd
<= SIOCDEVPRIVATE
+ 15) ||
2218 cmd
== SIOCETHTOOL
) {
2219 dev_load(ifr
.ifr_name
);
2221 ret
= dev_ifsioc(&ifr
, cmd
);
2223 if (!ret
&& copy_to_user(arg
, &ifr
, sizeof(struct ifreq
)))
2228 /* Take care of Wireless Extensions */
2229 if (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
) {
2230 /* If command is `set a parameter', or
2231 * `get the encoding parameters', check if
2232 * the user has the right to do it */
2233 if (IW_IS_SET(cmd
) || (cmd
== SIOCGIWENCODE
)) {
2234 if(!capable(CAP_NET_ADMIN
))
2237 dev_load(ifr
.ifr_name
);
2239 ret
= dev_ifsioc(&ifr
, cmd
);
2241 if (!ret
&& IW_IS_GET(cmd
) &&
2242 copy_to_user(arg
, &ifr
, sizeof(struct ifreq
)))
2246 #endif /* WIRELESS_EXT */
2253 * dev_new_index - allocate an ifindex
2255 * Returns a suitable unique value for a new device interface
2256 * number. The caller must hold the rtnl semaphore or the
2257 * dev_base_lock to be sure it remains unique.
2260 int dev_new_index(void)
2266 if (__dev_get_by_index(ifindex
) == NULL
)
2271 static int dev_boot_phase
= 1;
2274 * register_netdevice - register a network device
2275 * @dev: device to register
2277 * Take a completed network device structure and add it to the kernel
2278 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
2279 * chain. 0 is returned on success. A negative errno code is returned
2280 * on a failure to set up the device, or if the name is a duplicate.
2282 * Callers must hold the rtnl semaphore. See the comment at the
2283 * end of Space.c for details about the locking. You may want
2284 * register_netdev() instead of this.
2287 * The locking appears insufficient to guarantee two parallel registers
2288 * will not get the same name.
2291 int register_netdevice(struct net_device
*dev
)
2293 struct net_device
*d
, **dp
;
2294 #ifdef CONFIG_NET_DIVERT
2298 spin_lock_init(&dev
->queue_lock
);
2299 spin_lock_init(&dev
->xmit_lock
);
2300 dev
->xmit_lock_owner
= -1;
2301 #ifdef CONFIG_NET_FASTROUTE
2302 dev
->fastpath_lock
=RW_LOCK_UNLOCKED
;
2305 if (dev_boot_phase
) {
2306 /* This is NOT bug, but I am not sure, that all the
2307 devices, initialized before netdev module is started
2310 Now they are chained to device boot list
2311 and probed later. If a module is initialized
2312 before netdev, but assumes that dev->init
2313 is really called by register_netdev(), it will fail.
2315 So that this message should be printed for a while.
2317 printk(KERN_INFO
"early initialization of device %s is deferred\n", dev
->name
);
2319 /* Check for existence, and append to tail of chain */
2320 for (dp
=&dev_base
; (d
=*dp
) != NULL
; dp
=&d
->next
) {
2321 if (d
== dev
|| strcmp(d
->name
, dev
->name
) == 0) {
2326 write_lock_bh(&dev_base_lock
);
2329 write_unlock_bh(&dev_base_lock
);
2331 #ifdef CONFIG_NET_DIVERT
2332 ret
= alloc_divert_blk(dev
);
2335 #endif /* CONFIG_NET_DIVERT */
2338 * Default initial state at registry is that the
2339 * device is present.
2342 set_bit(__LINK_STATE_PRESENT
, &dev
->state
);
2349 /* Init, if this function is available */
2350 if (dev
->init
&& dev
->init(dev
) != 0)
2353 dev
->ifindex
= dev_new_index();
2354 if (dev
->iflink
== -1)
2355 dev
->iflink
= dev
->ifindex
;
2357 /* Check for existence, and append to tail of chain */
2358 for (dp
=&dev_base
; (d
=*dp
) != NULL
; dp
=&d
->next
) {
2359 if (d
== dev
|| strcmp(d
->name
, dev
->name
) == 0) {
2364 * nil rebuild_header routine,
2365 * that should be never called and used as just bug trap.
2368 if (dev
->rebuild_header
== NULL
)
2369 dev
->rebuild_header
= default_rebuild_header
;
2372 * Default initial state at registry is that the
2373 * device is present.
2376 set_bit(__LINK_STATE_PRESENT
, &dev
->state
);
2379 dev_init_scheduler(dev
);
2380 write_lock_bh(&dev_base_lock
);
2384 write_unlock_bh(&dev_base_lock
);
2386 #ifdef CONFIG_NET_DIVERT
2387 ret
= alloc_divert_blk(dev
);
2390 #endif /* CONFIG_NET_DIVERT */
2392 /* Notify protocols, that a new device appeared. */
2393 notifier_call_chain(&netdev_chain
, NETDEV_REGISTER
, dev
);
2399 * netdev_finish_unregister - complete unregistration
2402 * Destroy and free a dead device. A value of zero is returned on
2406 int netdev_finish_unregister(struct net_device
*dev
)
2408 BUG_TRAP(dev
->ip_ptr
==NULL
);
2409 BUG_TRAP(dev
->ip6_ptr
==NULL
);
2410 BUG_TRAP(dev
->dn_ptr
==NULL
);
2412 if (!dev
->deadbeaf
) {
2413 printk(KERN_ERR
"Freeing alive device %p, %s\n", dev
, dev
->name
);
2416 #ifdef NET_REFCNT_DEBUG
2417 printk(KERN_DEBUG
"netdev_finish_unregister: %s%s.\n", dev
->name
, dev
->new_style
?"":", old style");
2419 if (dev
->destructor
)
2420 dev
->destructor(dev
);
2427 * unregister_netdevice - remove device from the kernel
2430 * This function shuts down a device interface and removes it
2431 * from the kernel tables. On success 0 is returned, on a failure
2432 * a negative errno code is returned.
2434 * Callers must hold the rtnl semaphore. See the comment at the
2435 * end of Space.c for details about the locking. You may want
2436 * unregister_netdev() instead of this.
2439 int unregister_netdevice(struct net_device
*dev
)
2441 unsigned long now
, warning_time
;
2442 struct net_device
*d
, **dp
;
2444 /* If device is running, close it first. */
2445 if (dev
->flags
& IFF_UP
)
2448 BUG_TRAP(dev
->deadbeaf
==0);
2451 /* And unlink it from device chain. */
2452 for (dp
= &dev_base
; (d
=*dp
) != NULL
; dp
=&d
->next
) {
2454 write_lock_bh(&dev_base_lock
);
2456 write_unlock_bh(&dev_base_lock
);
2461 printk(KERN_DEBUG
"unregister_netdevice: device %s/%p never was registered\n", dev
->name
, dev
);
2465 if (dev_boot_phase
== 0) {
2466 #ifdef CONFIG_NET_FASTROUTE
2467 dev_clear_fastroute(dev
);
2470 /* Shutdown queueing discipline. */
2473 /* Notify protocols, that we are about to destroy
2474 this device. They should clean all the things.
2476 notifier_call_chain(&netdev_chain
, NETDEV_UNREGISTER
, dev
);
2479 * Flush the multicast chain
2481 dev_mc_discard(dev
);
2487 /* Notifier chain MUST detach us from master device. */
2488 BUG_TRAP(dev
->master
==NULL
);
2490 #ifdef CONFIG_NET_DIVERT
2491 free_divert_blk(dev
);
2494 if (dev
->new_style
) {
2495 #ifdef NET_REFCNT_DEBUG
2496 if (atomic_read(&dev
->refcnt
) != 1)
2497 printk(KERN_DEBUG
"unregister_netdevice: holding %s refcnt=%d\n", dev
->name
, atomic_read(&dev
->refcnt
)-1);
2503 /* Last reference is our one */
2504 if (atomic_read(&dev
->refcnt
) == 1) {
2509 #ifdef NET_REFCNT_DEBUG
2510 printk("unregister_netdevice: waiting %s refcnt=%d\n", dev
->name
, atomic_read(&dev
->refcnt
));
2513 /* EXPLANATION. If dev->refcnt is not now 1 (our own reference)
2514 it means that someone in the kernel still has a reference
2515 to this device and we cannot release it.
2517 "New style" devices have destructors, hence we can return from this
2518 function and destructor will do all the work later. As of kernel 2.4.0
2519 there are very few "New Style" devices.
2521 "Old style" devices expect that the device is free of any references
2522 upon exit from this function.
2523 We cannot return from this function until all such references have
2524 fallen away. This is because the caller of this function will probably
2525 immediately kfree(*dev) and then be unloaded via sys_delete_module.
2527 So, we linger until all references fall away. The duration of the
2528 linger is basically unbounded! It is driven by, for example, the
2529 current setting of sysctl_ipfrag_time.
2531 After 1 second, we start to rebroadcast unregister notifications
2532 in hope that careless clients will release the device.
2536 now
= warning_time
= jiffies
;
2537 while (atomic_read(&dev
->refcnt
) != 1) {
2538 if ((jiffies
- now
) > 1*HZ
) {
2539 /* Rebroadcast unregister notification */
2540 notifier_call_chain(&netdev_chain
, NETDEV_UNREGISTER
, dev
);
2542 current
->state
= TASK_INTERRUPTIBLE
;
2543 schedule_timeout(HZ
/4);
2544 current
->state
= TASK_RUNNING
;
2545 if ((jiffies
- warning_time
) > 10*HZ
) {
2546 printk(KERN_EMERG
"unregister_netdevice: waiting for %s to "
2547 "become free. Usage count = %d\n",
2548 dev
->name
, atomic_read(&dev
->refcnt
));
2549 warning_time
= jiffies
;
2558 * Initialize the DEV module. At boot time this walks the device list and
2559 * unhooks any devices that fail to initialise (normally hardware not
2560 * present) and leaves us with a valid list of present and active devices.
2564 extern void net_device_init(void);
2565 extern void ip_auto_config(void);
2566 #ifdef CONFIG_NET_DIVERT
2567 extern void dv_init(void);
2568 #endif /* CONFIG_NET_DIVERT */
2572 * Callers must hold the rtnl semaphore. See the comment at the
2573 * end of Space.c for details about the locking.
2575 int __init
net_dev_init(void)
2577 struct net_device
*dev
, **dp
;
2580 #ifdef CONFIG_NET_SCHED
2584 #ifdef CONFIG_NET_DIVERT
2586 #endif /* CONFIG_NET_DIVERT */
2589 * Initialise the packet receive queues.
2592 for (i
= 0; i
< NR_CPUS
; i
++) {
2593 struct softnet_data
*queue
;
2595 queue
= &softnet_data
[i
];
2596 skb_queue_head_init(&queue
->input_pkt_queue
);
2597 queue
->throttle
= 0;
2598 queue
->cng_level
= 0;
2599 queue
->avg_blog
= 10; /* arbitrary non-zero */
2600 queue
->completion_queue
= NULL
;
2603 #ifdef CONFIG_NET_PROFILE
2605 NET_PROFILE_REGISTER(dev_queue_xmit
);
2606 NET_PROFILE_REGISTER(softnet_process
);
2609 #ifdef OFFLINE_SAMPLE
2610 samp_timer
.expires
= jiffies
+ (10 * HZ
);
2611 add_timer(&samp_timer
);
2616 * If the call to dev->init fails, the dev is removed
2617 * from the chain disconnecting the device until the
2620 * NB At boot phase networking is dead. No locking is required.
2621 * But we still preserve dev_base_lock for sanity.
2625 while ((dev
= *dp
) != NULL
) {
2626 spin_lock_init(&dev
->queue_lock
);
2627 spin_lock_init(&dev
->xmit_lock
);
2628 #ifdef CONFIG_NET_FASTROUTE
2629 dev
->fastpath_lock
= RW_LOCK_UNLOCKED
;
2631 dev
->xmit_lock_owner
= -1;
2636 * Allocate name. If the init() fails
2637 * the name will be reissued correctly.
2639 if (strchr(dev
->name
, '%'))
2640 dev_alloc_name(dev
, dev
->name
);
2643 * Check boot time settings for the device.
2645 netdev_boot_setup_check(dev
);
2647 if (dev
->init
&& dev
->init(dev
)) {
2649 * It failed to come up. It will be unhooked later.
2650 * dev_alloc_name can now advance to next suitable
2651 * name that is checked next.
2657 dev
->ifindex
= dev_new_index();
2658 if (dev
->iflink
== -1)
2659 dev
->iflink
= dev
->ifindex
;
2660 if (dev
->rebuild_header
== NULL
)
2661 dev
->rebuild_header
= default_rebuild_header
;
2662 dev_init_scheduler(dev
);
2663 set_bit(__LINK_STATE_PRESENT
, &dev
->state
);
2668 * Unhook devices that failed to come up
2671 while ((dev
= *dp
) != NULL
) {
2672 if (dev
->deadbeaf
) {
2673 write_lock_bh(&dev_base_lock
);
2675 write_unlock_bh(&dev_base_lock
);
2682 #ifdef CONFIG_PROC_FS
2683 proc_net_create("dev", 0, dev_get_info
);
2684 create_proc_read_entry("net/softnet_stat", 0, 0, dev_proc_stats
, NULL
);
2686 proc_net_create("wireless", 0, dev_get_wireless_info
);
2687 #endif /* WIRELESS_EXT */
2688 #endif /* CONFIG_PROC_FS */
2692 open_softirq(NET_TX_SOFTIRQ
, net_tx_action
, NULL
);
2693 open_softirq(NET_RX_SOFTIRQ
, net_rx_action
, NULL
);
2699 * Initialise network devices