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>
21 * Alan Cox : device private ioctl copies fields back.
22 * Alan Cox : Transmit queue code does relevant stunts to
23 * keep the queue safe.
24 * Alan Cox : Fixed double lock.
25 * Alan Cox : Fixed promisc NULL pointer trap
26 * ???????? : Support the full private ioctl range
27 * Alan Cox : Moved ioctl permission check into drivers
28 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
29 * Alan Cox : 100 backlog just doesn't cut it when
30 * you start doing multicast video 8)
31 * Alan Cox : Rewrote net_bh and list manager.
32 * Alan Cox : Fix ETH_P_ALL echoback lengths.
33 * Alan Cox : Took out transmit every packet pass
34 * Saved a few bytes in the ioctl handler
35 * Alan Cox : Network driver sets packet type before calling netif_rx. Saves
36 * a function call a packet.
37 * Alan Cox : Hashed net_bh()
38 * Richard Kooijman: Timestamp fixes.
39 * Alan Cox : Wrong field in SIOCGIFDSTADDR
40 * Alan Cox : Device lock protection.
41 * Alan Cox : Fixed nasty side effect of device close changes.
42 * Rudi Cilibrasi : Pass the right thing to set_mac_address()
43 * Dave Miller : 32bit quantity for the device lock to make it work out
45 * Bjorn Ekwall : Added KERNELD hack.
46 * Alan Cox : Cleaned up the backlog initialise.
47 * Craig Metz : SIOCGIFCONF fix if space for under
49 * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
50 * is no device open function.
51 * Andi Kleen : Fix error reporting for SIOCGIFCONF
52 * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
53 * Cyrus Durgin : Cleaned for KMOD
57 #include <asm/uaccess.h>
58 #include <asm/system.h>
59 #include <asm/bitops.h>
60 #include <linux/config.h>
61 #include <linux/types.h>
62 #include <linux/kernel.h>
63 #include <linux/sched.h>
64 #include <linux/string.h>
66 #include <linux/socket.h>
67 #include <linux/sockios.h>
68 #include <linux/errno.h>
69 #include <linux/interrupt.h>
70 #include <linux/if_ether.h>
71 #include <linux/netdevice.h>
72 #include <linux/etherdevice.h>
73 #include <linux/notifier.h>
74 #include <linux/skbuff.h>
76 #include <linux/rtnetlink.h>
78 #include <linux/proc_fs.h>
79 #include <linux/stat.h>
82 #include <net/pkt_sched.h>
83 #include <net/profile.h>
84 #include <linux/init.h>
85 #include <linux/kmod.h>
86 #ifdef CONFIG_NET_RADIO
87 #include <linux/wireless.h>
88 #endif /* CONFIG_NET_RADIO */
90 extern int plip_init(void);
93 NET_PROFILE_DEFINE(dev_queue_xmit
)
94 NET_PROFILE_DEFINE(net_bh
)
95 NET_PROFILE_DEFINE(net_bh_skb
)
98 const char *if_port_text
[] = {
109 * The list of packet types we will receive (as opposed to discard)
110 * and the routines to invoke.
112 * Why 16. Because with 16 the only overlap we get on a hash of the
113 * low nibble of the protocol value is RARP/SNAP/X.25.
128 struct packet_type
*ptype_base
[16]; /* 16 way hashed list */
129 struct packet_type
*ptype_all
= NULL
; /* Taps */
132 * Device list lock. Setting it provides that interface
133 * will not disappear unexpectedly while kernel sleeps.
136 atomic_t dev_lockct
= ATOMIC_INIT(0);
142 static struct notifier_block
*netdev_chain
=NULL
;
145 * Device drivers call our routines to queue packets here. We empty the
146 * queue in the bottom half handler.
149 static struct sk_buff_head backlog
;
151 #ifdef CONFIG_NET_FASTROUTE
152 int netdev_fastroute
;
153 int netdev_fastroute_obstacles
;
154 struct net_fastroute_stats dev_fastroute_stat
;
158 /******************************************************************************************
160 Protocol management and registration routines
162 *******************************************************************************************/
171 * Add a protocol ID to the list. Now that the input handler is
172 * smarter we can dispense with all the messy stuff that used to be
176 void dev_add_pack(struct packet_type
*pt
)
179 #ifdef CONFIG_NET_FASTROUTE
180 /* Hack to detect packet socket */
182 netdev_fastroute_obstacles
++;
183 dev_clear_fastroute(pt
->dev
);
186 if(pt
->type
==htons(ETH_P_ALL
))
194 hash
=ntohs(pt
->type
)&15;
195 pt
->next
= ptype_base
[hash
];
196 ptype_base
[hash
] = pt
;
202 * Remove a protocol ID from the list.
205 void dev_remove_pack(struct packet_type
*pt
)
207 struct packet_type
**pt1
;
208 if(pt
->type
==htons(ETH_P_ALL
))
214 pt1
=&ptype_base
[ntohs(pt
->type
)&15];
215 for(; (*pt1
)!=NULL
; pt1
=&((*pt1
)->next
))
220 #ifdef CONFIG_NET_FASTROUTE
222 netdev_fastroute_obstacles
--;
227 printk(KERN_WARNING
"dev_remove_pack: %p not found.\n", pt
);
230 /*****************************************************************************************
232 Device Interface Subroutines
234 ******************************************************************************************/
237 * Find an interface by name.
240 struct device
*dev_get(const char *name
)
244 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
)
246 if (strcmp(dev
->name
, name
) == 0)
252 struct device
* dev_get_by_index(int ifindex
)
256 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
)
258 if (dev
->ifindex
== ifindex
)
264 struct device
*dev_getbyhwaddr(unsigned short type
, char *ha
)
268 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
)
270 if (dev
->type
== type
&&
271 memcmp(dev
->dev_addr
, ha
, dev
->addr_len
) == 0)
278 * Passed a format string - eg "lt%d" it will try and find a suitable
279 * id. Not efficient for many devices, not called a lot..
282 int dev_alloc_name(struct device
*dev
, const char *name
)
286 * If you need over 100 please also fix the algorithm...
290 sprintf(dev
->name
,name
,i
);
291 if(dev_get(dev
->name
)==NULL
)
294 return -ENFILE
; /* Over 100 of the things .. bail out! */
297 struct device
*dev_alloc(const char *name
, int *err
)
299 struct device
*dev
=kmalloc(sizeof(struct device
)+16, GFP_KERNEL
);
305 dev
->name
=(char *)(dev
+1); /* Name string space */
306 *err
=dev_alloc_name(dev
,name
);
317 * Find and possibly load an interface.
322 void dev_load(const char *name
)
324 if(!dev_get(name
) && capable(CAP_SYS_MODULE
))
325 request_module(name
);
330 extern inline void dev_load(const char *unused
){;}
334 static int default_rebuild_header(struct sk_buff
*skb
)
336 printk(KERN_DEBUG
"%s: default_rebuild_header called -- BUG!\n", skb
->dev
? skb
->dev
->name
: "NULL!!!");
342 * Prepare an interface for use.
345 int dev_open(struct device
*dev
)
353 if (dev
->flags
&IFF_UP
)
357 * Call device private open method
361 ret
= dev
->open(dev
);
364 * If it went open OK then:
370 * nil rebuild_header routine,
371 * that should be never called and used as just bug trap.
374 if (dev
->rebuild_header
== NULL
)
375 dev
->rebuild_header
= default_rebuild_header
;
380 dev
->flags
|= (IFF_UP
| IFF_RUNNING
);
383 * Initialize multicasting status
388 * Wakeup transmit queue engine
393 * ... and announce new interface.
395 notifier_call_chain(&netdev_chain
, NETDEV_UP
, dev
);
401 #ifdef CONFIG_NET_FASTROUTE
403 static __inline__
void dev_do_clear_fastroute(struct device
*dev
)
405 if (dev
->accept_fastpath
) {
408 for (i
=0; i
<=NETDEV_FASTROUTE_HMASK
; i
++)
409 dst_release(xchg(dev
->fastpath
+i
, NULL
));
413 void dev_clear_fastroute(struct device
*dev
)
416 dev_do_clear_fastroute(dev
);
418 for (dev
= dev_base
; dev
; dev
= dev
->next
)
419 dev_do_clear_fastroute(dev
);
425 * Completely shutdown an interface.
428 int dev_close(struct device
*dev
)
430 if (!(dev
->flags
&IFF_UP
))
438 * Call the device specific close. This cannot fail.
439 * Only if device is UP
446 printk("dev_close: bug %s still running\n", dev
->name
);
449 * Device is now down.
452 dev
->flags
&=~(IFF_UP
|IFF_RUNNING
);
453 #ifdef CONFIG_NET_FASTROUTE
454 dev_clear_fastroute(dev
);
458 * Tell people we are going down
461 notifier_call_chain(&netdev_chain
, NETDEV_DOWN
, dev
);
468 * Device change register/unregister. These are not inline or static
469 * as we export them to the world.
472 int register_netdevice_notifier(struct notifier_block
*nb
)
474 return notifier_chain_register(&netdev_chain
, nb
);
477 int unregister_netdevice_notifier(struct notifier_block
*nb
)
479 return notifier_chain_unregister(&netdev_chain
,nb
);
483 * Support routine. Sends outgoing frames to any network
484 * taps currently in use.
487 void dev_queue_xmit_nit(struct sk_buff
*skb
, struct device
*dev
)
489 struct packet_type
*ptype
;
490 get_fast_time(&skb
->stamp
);
492 for (ptype
= ptype_all
; ptype
!=NULL
; ptype
= ptype
->next
)
494 /* Never send packets back to the socket
495 * they originated from - MvS (miquels@drinkel.ow.org)
497 if ((ptype
->dev
== dev
|| !ptype
->dev
) &&
498 ((struct sock
*)ptype
->data
!= skb
->sk
))
500 struct sk_buff
*skb2
;
501 if ((skb2
= skb_clone(skb
, GFP_ATOMIC
)) == NULL
)
504 /* Code, following below is wrong.
506 The only reason, why it does work is that
507 ONLY packet sockets receive outgoing
508 packets. If such a packet will be (occasionally)
509 received by normal packet handler, which expects
510 that mac header is pulled...
513 /* More sensible variant. skb->nh should be correctly
514 set by sender, so that the second statement is
515 just protection against buggy protocols.
517 skb2
->mac
.raw
= skb2
->data
;
519 if (skb2
->nh
.raw
< skb2
->data
|| skb2
->nh
.raw
>= skb2
->tail
) {
521 printk(KERN_DEBUG
"protocol %04x is buggy, dev %s\n", skb2
->protocol
, dev
->name
);
522 skb2
->nh
.raw
= skb2
->data
;
523 if (dev
->hard_header
)
524 skb2
->nh
.raw
+= dev
->hard_header_len
;
527 skb2
->h
.raw
= skb2
->nh
.raw
;
528 skb2
->pkt_type
= PACKET_OUTGOING
;
529 ptype
->func(skb2
, skb
->dev
, ptype
);
535 * Fast path for loopback frames.
538 void dev_loopback_xmit(struct sk_buff
*skb
)
540 struct sk_buff
*newskb
=skb_clone(skb
, GFP_ATOMIC
);
544 newskb
->mac
.raw
= newskb
->data
;
545 skb_pull(newskb
, newskb
->nh
.raw
- newskb
->data
);
546 newskb
->pkt_type
= PACKET_LOOPBACK
;
547 newskb
->ip_summed
= CHECKSUM_UNNECESSARY
;
548 if (newskb
->dst
==NULL
)
549 printk(KERN_DEBUG
"BUG: packet without dst looped back 1\n");
553 int dev_queue_xmit(struct sk_buff
*skb
)
555 struct device
*dev
= skb
->dev
;
558 #ifdef CONFIG_NET_PROFILE
560 NET_PROFILE_ENTER(dev_queue_xmit
);
570 #ifdef CONFIG_NET_PROFILE
571 NET_PROFILE_LEAVE(dev_queue_xmit
);
578 /* The device has no queue. Common case for software devices:
579 loopback, all the sorts of tunnels...
581 Really, it is unlikely that bh protection is necessary here:
582 virtual devices do not generate EOI events.
583 However, it is possible, that they rely on bh protection
586 if (dev
->flags
&IFF_UP
) {
588 dev_queue_xmit_nit(skb
,dev
);
589 if (dev
->hard_start_xmit(skb
, dev
) == 0) {
592 #ifdef CONFIG_NET_PROFILE
593 NET_PROFILE_LEAVE(dev_queue_xmit
);
600 printk(KERN_DEBUG
"Virtual device %s asks to queue packet!\n", dev
->name
);
606 #ifdef CONFIG_NET_PROFILE
607 NET_PROFILE_LEAVE(dev_queue_xmit
);
615 /*=======================================================================
617 =======================================================================*/
619 int netdev_dropping
= 0;
620 int netdev_max_backlog
= 300;
621 atomic_t netdev_rx_dropped
;
622 #ifdef CONFIG_CPU_IS_SLOW
623 int net_cpu_congestion
;
626 #ifdef CONFIG_NET_HW_FLOWCONTROL
627 int netdev_throttle_events
;
628 static unsigned long netdev_fc_mask
= 1;
629 unsigned long netdev_fc_xoff
= 0;
633 void (*stimul
)(struct device
*);
635 } netdev_fc_slots
[32];
637 int netdev_register_fc(struct device
*dev
, void (*stimul
)(struct device
*dev
))
644 if (netdev_fc_mask
!= ~0UL) {
645 bit
= ffz(netdev_fc_mask
);
646 netdev_fc_slots
[bit
].stimul
= stimul
;
647 netdev_fc_slots
[bit
].dev
= dev
;
648 set_bit(bit
, &netdev_fc_mask
);
649 clear_bit(bit
, &netdev_fc_xoff
);
651 restore_flags(flags
);
655 void netdev_unregister_fc(int bit
)
662 netdev_fc_slots
[bit
].stimul
= NULL
;
663 netdev_fc_slots
[bit
].dev
= NULL
;
664 clear_bit(bit
, &netdev_fc_mask
);
665 clear_bit(bit
, &netdev_fc_xoff
);
667 restore_flags(flags
);
670 static void netdev_wakeup(void)
675 xoff
= netdev_fc_xoff
;
678 netdev_throttle_events
++;
682 netdev_fc_slots
[i
].stimul(netdev_fc_slots
[i
].dev
);
690 * Receive a packet from a device driver and queue it for the upper
691 * (protocol) levels. It always succeeds.
694 void netif_rx(struct sk_buff
*skb
)
696 #ifndef CONFIG_CPU_IS_SLOW
697 if(skb
->stamp
.tv_sec
==0)
698 get_fast_time(&skb
->stamp
);
703 /* The code is rearranged so that the path is the most
704 short when CPU is congested, but is still operating.
707 if (backlog
.qlen
<= netdev_max_backlog
) {
709 if (netdev_dropping
== 0) {
710 skb_queue_tail(&backlog
,skb
);
714 atomic_inc(&netdev_rx_dropped
);
718 #ifdef CONFIG_NET_HW_FLOWCONTROL
724 skb_queue_tail(&backlog
,skb
);
729 atomic_inc(&netdev_rx_dropped
);
734 static inline void handle_bridge(struct sk_buff
*skb
, unsigned short type
)
736 if (br_stats
.flags
& BR_UP
&& br_protocol_ok(ntohs(type
)))
739 * We pass the bridge a complete frame. This means
740 * recovering the MAC header first.
745 skb
=skb_clone(skb
, GFP_ATOMIC
);
749 offset
=skb
->data
-skb
->mac
.raw
;
750 skb_push(skb
,offset
); /* Put header back on for bridge */
752 if(br_receive_frame(skb
))
754 kfree_skb(skb
, FREE_READ
);
762 * When we are called the queue is ready to grab, the interrupts are
763 * on and hardware can interrupt and queue to the receive queue as we
764 * run with no problems.
765 * This is run as a bottom half after an interrupt handler that does
771 struct packet_type
*ptype
;
772 struct packet_type
*pt_prev
;
774 unsigned long start_time
= jiffies
;
775 #ifdef CONFIG_CPU_IS_SLOW
776 static unsigned long start_busy
= 0;
777 static unsigned long ave_busy
= 0;
780 start_busy
= start_time
;
781 net_cpu_congestion
= ave_busy
>>8;
784 NET_PROFILE_ENTER(net_bh
);
786 * Can we send anything now? We want to clear the
787 * decks for any more sends that get done as we
788 * process the input. This also minimises the
789 * latency on a transmit interrupt bh.
792 if (qdisc_head
.forw
!= &qdisc_head
)
796 * Any data left to process. This may occur because a
797 * mark_bh() is done after we empty the queue including
798 * that from the device which does a mark_bh() just after
802 * While the queue is not empty..
804 * Note that the queue never shrinks due to
805 * an interrupt, so we can do this test without
806 * disabling interrupts.
809 while (!skb_queue_empty(&backlog
))
811 struct sk_buff
* skb
;
813 /* Give chance to other bottom halves to run */
814 if (jiffies
- start_time
> 1)
818 * We have a packet. Therefore the queue has shrunk
820 skb
= skb_dequeue(&backlog
);
822 #ifdef CONFIG_CPU_IS_SLOW
823 if (ave_busy
> 128*16) {
825 while ((skb
= skb_dequeue(&backlog
)) != NULL
)
833 NET_PROFILE_SKB_PASSED(skb
, net_bh_skb
);
835 #ifdef CONFIG_NET_FASTROUTE
836 if (skb
->pkt_type
== PACKET_FASTROUTE
) {
843 * Fetch the packet protocol ID.
846 type
= skb
->protocol
;
851 * If we are bridging then pass the frame up to the
852 * bridging code (if this protocol is to be bridged).
853 * If it is bridged then move on
855 handle_bridge(skb
, type
);
859 * Bump the pointer to the next structure.
861 * On entry to the protocol layer. skb->data and
862 * skb->nh.raw point to the MAC and encapsulated data
865 /* XXX until we figure out every place to modify.. */
866 skb
->h
.raw
= skb
->nh
.raw
= skb
->data
;
868 if (skb
->mac
.raw
< skb
->head
|| skb
->mac
.raw
> skb
->data
) {
869 printk(KERN_CRIT
"%s: wrong mac.raw ptr, proto=%04x\n", skb
->dev
->name
, skb
->protocol
);
875 * We got a packet ID. Now loop over the "known protocols"
876 * list. There are two lists. The ptype_all list of taps (normally empty)
877 * and the main protocol list which is hashed perfectly for normal protocols.
881 for (ptype
= ptype_all
; ptype
!=NULL
; ptype
=ptype
->next
)
883 if (!ptype
->dev
|| ptype
->dev
== skb
->dev
) {
886 struct sk_buff
*skb2
=skb_clone(skb
, GFP_ATOMIC
);
888 pt_prev
->func(skb2
,skb
->dev
, pt_prev
);
894 for (ptype
= ptype_base
[ntohs(type
)&15]; ptype
!= NULL
; ptype
= ptype
->next
)
896 if (ptype
->type
== type
&& (!ptype
->dev
|| ptype
->dev
==skb
->dev
))
899 * We already have a match queued. Deliver
900 * to it and then remember the new match
904 struct sk_buff
*skb2
;
906 skb2
=skb_clone(skb
, GFP_ATOMIC
);
909 * Kick the protocol handler. This should be fast
910 * and efficient code.
914 pt_prev
->func(skb2
, skb
->dev
, pt_prev
);
916 /* Remember the current last to do */
919 } /* End of protocol list loop */
922 * Is there a last item to send to ?
926 pt_prev
->func(skb
, skb
->dev
, pt_prev
);
928 * Has an unknown packet has been received ?
934 } /* End of queue loop */
937 * We have emptied the queue
941 * One last output flush.
944 if (qdisc_head
.forw
!= &qdisc_head
)
947 #ifdef CONFIG_CPU_IS_SLOW
949 unsigned long start_idle
= jiffies
;
950 ave_busy
+= ((start_idle
- start_busy
)<<3) - (ave_busy
>>4);
954 #ifdef CONFIG_NET_HW_FLOWCONTROL
960 NET_PROFILE_LEAVE(net_bh
);
965 NET_PROFILE_LEAVE(net_bh
);
969 /* Protocol dependent address dumping routines */
971 static gifconf_func_t
* gifconf_list
[NPROTO
];
973 int register_gifconf(unsigned int family
, gifconf_func_t
* gifconf
)
977 gifconf_list
[family
] = gifconf
;
983 * Map an interface index to its name (SIOCGIFNAME)
987 * This call is useful, but I'd remove it too.
989 * The reason is purely aestetical, it is the only call
990 * from SIOC* family using struct ifreq in reversed manner.
991 * Besides that, it is pretty silly to put "drawing" facility
992 * to kernel, it is useful only to print ifindices
993 * in readable form, is not it? --ANK
995 * We need this ioctl for efficient implementation of the
996 * if_indextoname() function required by the IPv6 API. Without
997 * it, we would have to search all the interfaces to find a
1001 static int dev_ifname(struct ifreq
*arg
)
1008 * Fetch the caller's info block.
1011 err
= copy_from_user(&ifr
, arg
, sizeof(struct ifreq
));
1015 dev
= dev_get_by_index(ifr
.ifr_ifindex
);
1019 strcpy(ifr
.ifr_name
, dev
->name
);
1021 err
= copy_to_user(arg
, &ifr
, sizeof(struct ifreq
));
1022 return (err
)?-EFAULT
:0;
1026 * Perform a SIOCGIFCONF call. This structure will change
1027 * size eventually, and there is nothing I can do about it.
1028 * Thus we will need a 'compatibility mode'.
1031 static int dev_ifconf(char *arg
)
1041 * Fetch the caller's info block.
1044 if (copy_from_user(&ifc
, arg
, sizeof(struct ifconf
)))
1051 * Loop over the interfaces, and write an info block for each.
1055 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
) {
1056 for (i
=0; i
<NPROTO
; i
++) {
1057 if (gifconf_list
[i
]) {
1060 done
= gifconf_list
[i
](dev
, NULL
, 0);
1062 done
= gifconf_list
[i
](dev
, pos
+total
, len
-total
);
1072 * All done. Write the updated control block back to the caller.
1074 ifc
.ifc_len
= total
;
1076 if (copy_to_user(arg
, &ifc
, sizeof(struct ifconf
)))
1080 * Both BSD and Solaris return 0 here, so we do too.
1086 * This is invoked by the /proc filesystem handler to display a device
1090 #ifdef CONFIG_PROC_FS
1091 static int sprintf_stats(char *buffer
, struct device
*dev
)
1093 struct net_device_stats
*stats
= (dev
->get_stats
? dev
->get_stats(dev
): NULL
);
1097 size
= sprintf(buffer
, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu %8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
1100 stats
->rx_packets
, stats
->rx_errors
,
1101 stats
->rx_dropped
+ stats
->rx_missed_errors
,
1102 stats
->rx_fifo_errors
,
1103 stats
->rx_length_errors
+ stats
->rx_over_errors
1104 + stats
->rx_crc_errors
+ stats
->rx_frame_errors
,
1105 stats
->rx_compressed
, stats
->multicast
,
1107 stats
->tx_packets
, stats
->tx_errors
, stats
->tx_dropped
,
1108 stats
->tx_fifo_errors
, stats
->collisions
,
1109 stats
->tx_carrier_errors
+ stats
->tx_aborted_errors
1110 + stats
->tx_window_errors
+ stats
->tx_heartbeat_errors
,
1111 stats
->tx_compressed
);
1113 size
= sprintf(buffer
, "%6s: No statistics available.\n", dev
->name
);
1119 * Called from the PROCfs module. This now uses the new arbitrary sized /proc/net interface
1120 * to create /proc/net/dev
1123 int dev_get_info(char *buffer
, char **start
, off_t offset
, int length
, int dummy
)
1133 size
= sprintf(buffer
,
1134 "Inter-| Receive | Transmit\n"
1135 " face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed\n");
1141 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
)
1143 size
= sprintf_stats(buffer
+len
, dev
);
1152 if(pos
>offset
+length
)
1156 *start
=buffer
+(offset
-begin
); /* Start of wanted data */
1157 len
-=(offset
-begin
); /* Start slop */
1159 len
=length
; /* Ending slop */
1163 static int dev_proc_stats(char *buffer
, char **start
, off_t offset
,
1164 int length
, int *eof
, void *data
)
1168 len
= sprintf(buffer
, "%08x %08x %08x %08x %08x\n",
1169 atomic_read(&netdev_rx_dropped
),
1170 #ifdef CONFIG_NET_HW_FLOWCONTROL
1171 netdev_throttle_events
,
1175 #ifdef CONFIG_NET_FASTROUTE
1176 dev_fastroute_stat
.hits
,
1177 dev_fastroute_stat
.succeed
,
1178 dev_fastroute_stat
.deferred
1191 *start
= buffer
+ offset
;
1197 #endif /* CONFIG_PROC_FS */
1200 #ifdef CONFIG_NET_RADIO
1201 #ifdef CONFIG_PROC_FS
1204 * Print one entry of /proc/net/wireless
1205 * This is a clone of /proc/net/dev (just above)
1207 static int sprintf_wireless_stats(char *buffer
, struct device
*dev
)
1209 /* Get stats from the driver */
1210 struct iw_statistics
*stats
= (dev
->get_wireless_stats
?
1211 dev
->get_wireless_stats(dev
) :
1212 (struct iw_statistics
*) NULL
);
1215 if(stats
!= (struct iw_statistics
*) NULL
)
1216 size
= sprintf(buffer
,
1217 "%6s: %02x %3d%c %3d%c %3d%c %5d %5d %5d\n",
1221 stats
->qual
.updated
& 1 ? '.' : ' ',
1223 stats
->qual
.updated
& 2 ? '.' : ' ',
1225 stats
->qual
.updated
& 3 ? '.' : ' ',
1226 stats
->discard
.nwid
,
1227 stats
->discard
.code
,
1228 stats
->discard
.misc
);
1236 * Print info for /proc/net/wireless (print all entries)
1237 * This is a clone of /proc/net/dev (just above)
1239 int dev_get_wireless_info(char * buffer
, char **start
, off_t offset
,
1240 int length
, int dummy
)
1247 struct device
* dev
;
1249 size
= sprintf(buffer
,
1250 "Inter-|sta| Quality | Discarded packets\n"
1251 " face |tus|link level noise| nwid crypt misc\n");
1256 for(dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
)
1258 size
= sprintf_wireless_stats(buffer
+len
, dev
);
1267 if(pos
> offset
+ length
)
1271 *start
= buffer
+ (offset
- begin
); /* Start of wanted data */
1272 len
-= (offset
- begin
); /* Start slop */
1274 len
= length
; /* Ending slop */
1278 #endif /* CONFIG_PROC_FS */
1279 #endif /* CONFIG_NET_RADIO */
1281 void dev_set_promiscuity(struct device
*dev
, int inc
)
1283 unsigned short old_flags
= dev
->flags
;
1285 dev
->flags
|= IFF_PROMISC
;
1286 if ((dev
->promiscuity
+= inc
) == 0)
1287 dev
->flags
&= ~IFF_PROMISC
;
1288 if (dev
->flags
^old_flags
) {
1289 #ifdef CONFIG_NET_FASTROUTE
1290 if (dev
->flags
&IFF_PROMISC
) {
1291 netdev_fastroute_obstacles
++;
1292 dev_clear_fastroute(dev
);
1294 netdev_fastroute_obstacles
--;
1297 printk(KERN_INFO
"device %s %s promiscuous mode\n",
1298 dev
->name
, (dev
->flags
&IFF_PROMISC
) ? "entered" : "left");
1302 void dev_set_allmulti(struct device
*dev
, int inc
)
1304 unsigned short old_flags
= dev
->flags
;
1306 dev
->flags
|= IFF_ALLMULTI
;
1307 if ((dev
->allmulti
+= inc
) == 0)
1308 dev
->flags
&= ~IFF_ALLMULTI
;
1309 if (dev
->flags
^old_flags
)
1313 int dev_change_flags(struct device
*dev
, unsigned flags
)
1316 int old_flags
= dev
->flags
;
1319 * Set the flags on our device.
1322 dev
->flags
= (flags
& (IFF_DEBUG
|IFF_NOTRAILERS
|IFF_RUNNING
|IFF_NOARP
|
1323 IFF_SLAVE
|IFF_MASTER
|
1324 IFF_MULTICAST
|IFF_PORTSEL
|IFF_AUTOMEDIA
)) |
1325 (dev
->flags
& (IFF_UP
|IFF_VOLATILE
|IFF_PROMISC
|IFF_ALLMULTI
));
1328 * Load in the correct multicast list now the flags have changed.
1334 * Have we downed the interface. We handle IFF_UP ourselves
1335 * according to user attempts to set it, rather than blindly
1340 if ((old_flags
^flags
)&IFF_UP
) /* Bit is different ? */
1342 ret
= ((old_flags
& IFF_UP
) ? dev_close
: dev_open
)(dev
);
1348 if (dev
->flags
&IFF_UP
&&
1349 ((old_flags
^dev
->flags
)&~(IFF_UP
|IFF_RUNNING
|IFF_PROMISC
|IFF_ALLMULTI
|IFF_VOLATILE
)))
1350 notifier_call_chain(&netdev_chain
, NETDEV_CHANGE
, dev
);
1352 if ((flags
^dev
->gflags
)&IFF_PROMISC
) {
1353 int inc
= (flags
&IFF_PROMISC
) ? +1 : -1;
1354 dev
->gflags
^= IFF_PROMISC
;
1355 dev_set_promiscuity(dev
, inc
);
1358 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
1359 is important. Some (broken) drivers set IFF_PROMISC, when
1360 IFF_ALLMULTI is requested not asking us and not reporting.
1362 if ((flags
^dev
->gflags
)&IFF_ALLMULTI
) {
1363 int inc
= (flags
&IFF_ALLMULTI
) ? +1 : -1;
1364 dev
->gflags
^= IFF_ALLMULTI
;
1365 dev_set_allmulti(dev
, inc
);
1372 * Perform the SIOCxIFxxx calls.
1375 static int dev_ifsioc(struct ifreq
*ifr
, unsigned int cmd
)
1380 if ((dev
= dev_get(ifr
->ifr_name
)) == NULL
)
1385 case SIOCGIFFLAGS
: /* Get interface flags */
1386 ifr
->ifr_flags
= (dev
->flags
&~(IFF_PROMISC
|IFF_ALLMULTI
))
1387 |(dev
->gflags
&(IFF_PROMISC
|IFF_ALLMULTI
));
1390 case SIOCSIFFLAGS
: /* Set interface flags */
1391 return dev_change_flags(dev
, ifr
->ifr_flags
);
1393 case SIOCGIFMETRIC
: /* Get the metric on the interface (currently unused) */
1394 ifr
->ifr_metric
= dev
->metric
;
1397 case SIOCSIFMETRIC
: /* Set the metric on the interface (currently unused) */
1398 dev
->metric
= ifr
->ifr_metric
;
1401 case SIOCGIFMTU
: /* Get the MTU of a device */
1402 ifr
->ifr_mtu
= dev
->mtu
;
1405 case SIOCSIFMTU
: /* Set the MTU of a device */
1406 if (ifr
->ifr_mtu
== dev
->mtu
)
1410 * MTU must be positive.
1416 if (dev
->change_mtu
)
1417 err
= dev
->change_mtu(dev
, ifr
->ifr_mtu
);
1419 dev
->mtu
= ifr
->ifr_mtu
;
1422 if (!err
&& dev
->flags
&IFF_UP
) {
1423 printk(KERN_DEBUG
"SIFMTU %s(%s)\n", dev
->name
, current
->comm
);
1424 notifier_call_chain(&netdev_chain
, NETDEV_CHANGEMTU
, dev
);
1429 memcpy(ifr
->ifr_hwaddr
.sa_data
,dev
->dev_addr
, MAX_ADDR_LEN
);
1430 ifr
->ifr_hwaddr
.sa_family
=dev
->type
;
1434 if(dev
->set_mac_address
==NULL
)
1436 if(ifr
->ifr_hwaddr
.sa_family
!=dev
->type
)
1438 err
=dev
->set_mac_address(dev
,&ifr
->ifr_hwaddr
);
1440 notifier_call_chain(&netdev_chain
, NETDEV_CHANGEADDR
, dev
);
1443 case SIOCSIFHWBROADCAST
:
1444 if(ifr
->ifr_hwaddr
.sa_family
!=dev
->type
)
1446 memcpy(dev
->broadcast
, ifr
->ifr_hwaddr
.sa_data
, MAX_ADDR_LEN
);
1447 notifier_call_chain(&netdev_chain
, NETDEV_CHANGEADDR
, dev
);
1451 ifr
->ifr_map
.mem_start
=dev
->mem_start
;
1452 ifr
->ifr_map
.mem_end
=dev
->mem_end
;
1453 ifr
->ifr_map
.base_addr
=dev
->base_addr
;
1454 ifr
->ifr_map
.irq
=dev
->irq
;
1455 ifr
->ifr_map
.dma
=dev
->dma
;
1456 ifr
->ifr_map
.port
=dev
->if_port
;
1460 if (dev
->set_config
)
1461 return dev
->set_config(dev
,&ifr
->ifr_map
);
1465 if(dev
->set_multicast_list
==NULL
||
1466 ifr
->ifr_hwaddr
.sa_family
!=AF_UNSPEC
)
1468 dev_mc_add(dev
,ifr
->ifr_hwaddr
.sa_data
, dev
->addr_len
, 1);
1472 if(dev
->set_multicast_list
==NULL
||
1473 ifr
->ifr_hwaddr
.sa_family
!=AF_UNSPEC
)
1475 dev_mc_delete(dev
,ifr
->ifr_hwaddr
.sa_data
,dev
->addr_len
, 1);
1479 ifr
->ifr_ifindex
= dev
->ifindex
;
1483 ifr
->ifr_qlen
= dev
->tx_queue_len
;
1487 if(ifr
->ifr_qlen
<2 || ifr
->ifr_qlen
>1024)
1489 dev
->tx_queue_len
= ifr
->ifr_qlen
;
1493 * Unknown or private ioctl
1497 if(cmd
>= SIOCDEVPRIVATE
&&
1498 cmd
<= SIOCDEVPRIVATE
+ 15) {
1500 return dev
->do_ioctl(dev
, ifr
, cmd
);
1504 #ifdef CONFIG_NET_RADIO
1505 if(cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
) {
1507 return dev
->do_ioctl(dev
, ifr
, cmd
);
1510 #endif /* CONFIG_NET_RADIO */
1518 * This function handles all "interface"-type I/O control requests. The actual
1519 * 'doing' part of this is dev_ifsioc above.
1522 int dev_ioctl(unsigned int cmd
, void *arg
)
1528 /* One special case: SIOCGIFCONF takes ifconf argument
1529 and requires shared lock, because it sleeps writing
1533 if (cmd
== SIOCGIFCONF
) {
1535 ret
= dev_ifconf((char *) arg
);
1539 if (cmd
== SIOCGIFNAME
) {
1540 return dev_ifname((struct ifreq
*)arg
);
1543 if (copy_from_user(&ifr
, arg
, sizeof(struct ifreq
)))
1546 ifr
.ifr_name
[IFNAMSIZ
-1] = 0;
1548 colon
= strchr(ifr
.ifr_name
, ':');
1553 * See which interface the caller is talking about.
1559 * These ioctl calls:
1560 * - can be done by all.
1561 * - atomic and do not require locking.
1573 dev_load(ifr
.ifr_name
);
1574 ret
= dev_ifsioc(&ifr
, cmd
);
1578 if (copy_to_user(arg
, &ifr
, sizeof(struct ifreq
)))
1584 * These ioctl calls:
1585 * - require superuser power.
1586 * - require strict serialization.
1587 * - do not return a value
1598 case SIOCSIFHWBROADCAST
:
1600 if (!capable(CAP_NET_ADMIN
))
1602 dev_load(ifr
.ifr_name
);
1604 ret
= dev_ifsioc(&ifr
, cmd
);
1609 /* Get the per device memory space. We can add this but currently
1610 do not support it */
1612 /* Set the per device memory buffer space. Not applicable in our case */
1617 * Unknown or private ioctl.
1621 if (cmd
>= SIOCDEVPRIVATE
&&
1622 cmd
<= SIOCDEVPRIVATE
+ 15) {
1623 dev_load(ifr
.ifr_name
);
1625 ret
= dev_ifsioc(&ifr
, cmd
);
1627 if (!ret
&& copy_to_user(arg
, &ifr
, sizeof(struct ifreq
)))
1631 #ifdef CONFIG_NET_RADIO
1632 if (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
) {
1633 dev_load(ifr
.ifr_name
);
1634 if (IW_IS_SET(cmd
)) {
1639 ret
= dev_ifsioc(&ifr
, cmd
);
1642 if (!ret
&& IW_IS_GET(cmd
) &&
1643 copy_to_user(arg
, &ifr
, sizeof(struct ifreq
)))
1647 #endif /* CONFIG_NET_RADIO */
1652 int dev_new_index(void)
1658 if (dev_get_by_index(ifindex
) == NULL
)
1663 static int dev_boot_phase
= 1;
1666 int register_netdevice(struct device
*dev
)
1668 struct device
*d
, **dp
;
1670 if (dev_boot_phase
) {
1671 printk(KERN_INFO
"early initialization of device %s is deferred\n", dev
->name
);
1673 /* Check for existence, and append to tail of chain */
1674 for (dp
=&dev_base
; (d
=*dp
) != NULL
; dp
=&d
->next
) {
1675 if (d
== dev
|| strcmp(d
->name
, dev
->name
) == 0)
1685 /* Init, if this function is available */
1686 if (dev
->init
&& dev
->init(dev
) != 0)
1689 /* Check for existence, and append to tail of chain */
1690 for (dp
=&dev_base
; (d
=*dp
) != NULL
; dp
=&d
->next
) {
1691 if (d
== dev
|| strcmp(d
->name
, dev
->name
) == 0)
1695 dev_init_scheduler(dev
);
1696 dev
->ifindex
= dev_new_index();
1697 if (dev
->iflink
== -1)
1698 dev
->iflink
= dev
->ifindex
;
1701 /* Notify protocols, that a new device appeared. */
1702 notifier_call_chain(&netdev_chain
, NETDEV_REGISTER
, dev
);
1707 int unregister_netdevice(struct device
*dev
)
1709 struct device
*d
, **dp
;
1711 if (dev_boot_phase
== 0) {
1712 /* If device is running, close it.
1713 It is very bad idea, really we should
1714 complain loudly here, but random hackery
1715 in linux/drivers/net likes it.
1717 if (dev
->flags
& IFF_UP
)
1720 #ifdef CONFIG_NET_FASTROUTE
1721 dev_clear_fastroute(dev
);
1724 /* Shutdown queueing discipline. */
1727 /* Notify protocols, that we are about to destroy
1728 this device. They should clean all the things.
1730 notifier_call_chain(&netdev_chain
, NETDEV_UNREGISTER
, dev
);
1733 * Flush the multicast chain
1735 dev_mc_discard(dev
);
1737 /* To avoid pointers looking to nowhere,
1738 we wait for end of critical section */
1742 /* And unlink it from device chain. */
1743 for (dp
= &dev_base
; (d
=*dp
) != NULL
; dp
=&d
->next
) {
1747 if (dev
->destructor
)
1748 dev
->destructor(dev
);
1757 * Initialize the DEV module. At boot time this walks the device list and
1758 * unhooks any devices that fail to initialise (normally hardware not
1759 * present) and leaves us with a valid list of present and active devices.
1762 extern int lance_init(void);
1763 extern int bpq_init(void);
1764 extern int scc_init(void);
1765 extern void sdla_setup(void);
1766 extern void dlci_setup(void);
1767 extern int dmascc_init(void);
1768 extern int sm_init(void);
1769 extern int baycom_init(void);
1770 extern int lapbeth_init(void);
1771 extern void arcnet_init(void);
1772 extern void ip_auto_config(void);
1774 extern int cpm_enet_init(void);
1775 #endif /* CONFIG_8xx */
1777 #ifdef CONFIG_PROC_FS
1778 static struct proc_dir_entry proc_net_dev
= {
1779 PROC_NET_DEV
, 3, "dev",
1780 S_IFREG
| S_IRUGO
, 1, 0, 0,
1781 0, &proc_net_inode_operations
,
1786 #ifdef CONFIG_NET_RADIO
1787 #ifdef CONFIG_PROC_FS
1788 static struct proc_dir_entry proc_net_wireless
= {
1789 PROC_NET_WIRELESS
, 8, "wireless",
1790 S_IFREG
| S_IRUGO
, 1, 0, 0,
1791 0, &proc_net_inode_operations
,
1792 dev_get_wireless_info
1794 #endif /* CONFIG_PROC_FS */
1795 #endif /* CONFIG_NET_RADIO */
1797 __initfunc(int net_dev_init(void))
1799 struct device
*dev
, **dp
;
1801 #ifdef CONFIG_NET_SCHED
1806 * Initialise the packet receive queue.
1809 skb_queue_head_init(&backlog
);
1812 * The bridge has to be up before the devices
1815 #ifdef CONFIG_BRIDGE
1820 * This is Very Ugly(tm).
1822 * Some devices want to be initialized early..
1825 #if defined(CONFIG_LANCE)
1828 #if defined(CONFIG_SCC)
1831 #if defined(CONFIG_DMASCC)
1834 #if defined(CONFIG_BPQETHER)
1837 #if defined(CONFIG_DLCI)
1840 #if defined(CONFIG_SDLA)
1843 #if defined(CONFIG_BAYCOM)
1846 #if defined(CONFIG_SOUNDMODEM)
1849 #if defined(CONFIG_LAPBETHER)
1852 #if defined(CONFIG_PLIP)
1855 #if defined(CONFIG_ARCNET)
1858 #if defined(CONFIG_8xx)
1862 * SLHC if present needs attaching so other people see it
1863 * even if not opened.
1865 #if (defined(CONFIG_SLIP) && defined(CONFIG_SLIP_COMPRESSED)) \
1866 || defined(CONFIG_PPP) \
1867 || (defined(CONFIG_ISDN) && defined(CONFIG_ISDN_PPP))
1871 #ifdef CONFIG_NET_PROFILE
1873 NET_PROFILE_REGISTER(dev_queue_xmit
);
1874 NET_PROFILE_REGISTER(net_bh
);
1876 NET_PROFILE_REGISTER(net_bh_skb
);
1881 * If the call to dev->init fails, the dev is removed
1882 * from the chain disconnecting the device until the
1887 while ((dev
= *dp
) != NULL
)
1890 if (dev
->init
&& dev
->init(dev
))
1893 * It failed to come up. Unhook it.
1900 dev
->ifindex
= dev_new_index();
1901 if (dev
->iflink
== -1)
1902 dev
->iflink
= dev
->ifindex
;
1903 dev_init_scheduler(dev
);
1907 #ifdef CONFIG_PROC_FS
1908 proc_net_register(&proc_net_dev
);
1910 struct proc_dir_entry
*ent
= create_proc_entry("net/dev_stat", 0, 0);
1911 ent
->read_proc
= dev_proc_stats
;
1915 #ifdef CONFIG_NET_RADIO
1916 #ifdef CONFIG_PROC_FS
1917 proc_net_register(&proc_net_wireless
);
1918 #endif /* CONFIG_PROC_FS */
1919 #endif /* CONFIG_NET_RADIO */
1921 init_bh(NET_BH
, net_bh
);
1927 #ifdef CONFIG_IP_PNP