]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/core/dev.c
[NET] loopback: Panic if registration fails
[net-next-2.6.git] / net / core / dev.c
CommitLineData
1da177e4
LT
1/*
2 * NET3 Protocol independent device support routines.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Derived from the non IP parts of dev.c 1.0.19
02c30a84 10 * Authors: Ross Biro
1da177e4
LT
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Mark Evans, <evansmp@uhura.aston.ac.uk>
13 *
14 * Additional Authors:
15 * Florian la Roche <rzsfl@rz.uni-sb.de>
16 * Alan Cox <gw4pts@gw4pts.ampr.org>
17 * David Hinds <dahinds@users.sourceforge.net>
18 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
19 * Adam Sulmicki <adam@cfar.umd.edu>
20 * Pekka Riikonen <priikone@poesidon.pspt.fi>
21 *
22 * Changes:
23 * D.J. Barrow : Fixed bug where dev->refcnt gets set
24 * to 2 if register_netdev gets called
25 * before net_dev_init & also removed a
26 * few lines of code in the process.
27 * Alan Cox : device private ioctl copies fields back.
28 * Alan Cox : Transmit queue code does relevant
29 * stunts to keep the queue safe.
30 * Alan Cox : Fixed double lock.
31 * Alan Cox : Fixed promisc NULL pointer trap
32 * ???????? : Support the full private ioctl range
33 * Alan Cox : Moved ioctl permission check into
34 * drivers
35 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
36 * Alan Cox : 100 backlog just doesn't cut it when
37 * you start doing multicast video 8)
38 * Alan Cox : Rewrote net_bh and list manager.
39 * Alan Cox : Fix ETH_P_ALL echoback lengths.
40 * Alan Cox : Took out transmit every packet pass
41 * Saved a few bytes in the ioctl handler
42 * Alan Cox : Network driver sets packet type before
43 * calling netif_rx. Saves a function
44 * call a packet.
45 * Alan Cox : Hashed net_bh()
46 * Richard Kooijman: Timestamp fixes.
47 * Alan Cox : Wrong field in SIOCGIFDSTADDR
48 * Alan Cox : Device lock protection.
49 * Alan Cox : Fixed nasty side effect of device close
50 * changes.
51 * Rudi Cilibrasi : Pass the right thing to
52 * set_mac_address()
53 * Dave Miller : 32bit quantity for the device lock to
54 * make it work out on a Sparc.
55 * Bjorn Ekwall : Added KERNELD hack.
56 * Alan Cox : Cleaned up the backlog initialise.
57 * Craig Metz : SIOCGIFCONF fix if space for under
58 * 1 device.
59 * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
60 * is no device open function.
61 * Andi Kleen : Fix error reporting for SIOCGIFCONF
62 * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
63 * Cyrus Durgin : Cleaned for KMOD
64 * Adam Sulmicki : Bug Fix : Network Device Unload
65 * A network device unload needs to purge
66 * the backlog queue.
67 * Paul Rusty Russell : SIOCSIFNAME
68 * Pekka Riikonen : Netdev boot-time settings code
69 * Andrew Morton : Make unregister_netdevice wait
70 * indefinitely on dev->refcnt
71 * J Hadi Salim : - Backlog queue sampling
72 * - netif_rx() feedback
73 */
74
75#include <asm/uaccess.h>
76#include <asm/system.h>
77#include <linux/bitops.h>
4fc268d2 78#include <linux/capability.h>
1da177e4
LT
79#include <linux/cpu.h>
80#include <linux/types.h>
81#include <linux/kernel.h>
82#include <linux/sched.h>
4a3e2f71 83#include <linux/mutex.h>
1da177e4
LT
84#include <linux/string.h>
85#include <linux/mm.h>
86#include <linux/socket.h>
87#include <linux/sockios.h>
88#include <linux/errno.h>
89#include <linux/interrupt.h>
90#include <linux/if_ether.h>
91#include <linux/netdevice.h>
92#include <linux/etherdevice.h>
93#include <linux/notifier.h>
94#include <linux/skbuff.h>
95#include <net/sock.h>
96#include <linux/rtnetlink.h>
97#include <linux/proc_fs.h>
98#include <linux/seq_file.h>
99#include <linux/stat.h>
100#include <linux/if_bridge.h>
b863ceb7 101#include <linux/if_macvlan.h>
1da177e4
LT
102#include <net/dst.h>
103#include <net/pkt_sched.h>
104#include <net/checksum.h>
105#include <linux/highmem.h>
106#include <linux/init.h>
107#include <linux/kmod.h>
108#include <linux/module.h>
109#include <linux/kallsyms.h>
110#include <linux/netpoll.h>
111#include <linux/rcupdate.h>
112#include <linux/delay.h>
295f4a1f 113#include <net/wext.h>
1da177e4 114#include <net/iw_handler.h>
1da177e4 115#include <asm/current.h>
5bdb9886 116#include <linux/audit.h>
db217334 117#include <linux/dmaengine.h>
f6a78bfc 118#include <linux/err.h>
c7fa9d18 119#include <linux/ctype.h>
723e98b7 120#include <linux/if_arp.h>
1da177e4 121
1da177e4
LT
122/*
123 * The list of packet types we will receive (as opposed to discard)
124 * and the routines to invoke.
125 *
126 * Why 16. Because with 16 the only overlap we get on a hash of the
127 * low nibble of the protocol value is RARP/SNAP/X.25.
128 *
129 * NOTE: That is no longer true with the addition of VLAN tags. Not
130 * sure which should go first, but I bet it won't make much
131 * difference if we are running VLANs. The good news is that
132 * this protocol won't be in the list unless compiled in, so
3041a069 133 * the average user (w/out VLANs) will not be adversely affected.
1da177e4
LT
134 * --BLG
135 *
136 * 0800 IP
137 * 8100 802.1Q VLAN
138 * 0001 802.3
139 * 0002 AX.25
140 * 0004 802.2
141 * 8035 RARP
142 * 0005 SNAP
143 * 0805 X.25
144 * 0806 ARP
145 * 8137 IPX
146 * 0009 Localtalk
147 * 86DD IPv6
148 */
149
150static DEFINE_SPINLOCK(ptype_lock);
6b2bedc3
SH
151static struct list_head ptype_base[16] __read_mostly; /* 16 way hashed list */
152static struct list_head ptype_all __read_mostly; /* Taps */
1da177e4 153
db217334 154#ifdef CONFIG_NET_DMA
d379b01e
DW
155struct net_dma {
156 struct dma_client client;
157 spinlock_t lock;
158 cpumask_t channel_mask;
159 struct dma_chan *channels[NR_CPUS];
160};
161
162static enum dma_state_client
163netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
164 enum dma_state state);
165
166static struct net_dma net_dma = {
167 .client = {
168 .event_callback = netdev_dma_event,
169 },
170};
db217334
CL
171#endif
172
1da177e4 173/*
7562f876 174 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
1da177e4
LT
175 * semaphore.
176 *
177 * Pure readers hold dev_base_lock for reading.
178 *
179 * Writers must hold the rtnl semaphore while they loop through the
7562f876 180 * dev_base_head list, and hold dev_base_lock for writing when they do the
1da177e4
LT
181 * actual updates. This allows pure readers to access the list even
182 * while a writer is preparing to update it.
183 *
184 * To put it another way, dev_base_lock is held for writing only to
185 * protect against pure readers; the rtnl semaphore provides the
186 * protection against other writers.
187 *
188 * See, for example usages, register_netdevice() and
189 * unregister_netdevice(), which must be called with the rtnl
190 * semaphore held.
191 */
7562f876 192LIST_HEAD(dev_base_head);
1da177e4
LT
193DEFINE_RWLOCK(dev_base_lock);
194
7562f876 195EXPORT_SYMBOL(dev_base_head);
1da177e4
LT
196EXPORT_SYMBOL(dev_base_lock);
197
198#define NETDEV_HASHBITS 8
199static struct hlist_head dev_name_head[1<<NETDEV_HASHBITS];
200static struct hlist_head dev_index_head[1<<NETDEV_HASHBITS];
201
202static inline struct hlist_head *dev_name_hash(const char *name)
203{
204 unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
205 return &dev_name_head[hash & ((1<<NETDEV_HASHBITS)-1)];
206}
207
208static inline struct hlist_head *dev_index_hash(int ifindex)
209{
210 return &dev_index_head[ifindex & ((1<<NETDEV_HASHBITS)-1)];
211}
212
213/*
214 * Our notifier list
215 */
216
f07d5b94 217static RAW_NOTIFIER_HEAD(netdev_chain);
1da177e4
LT
218
219/*
220 * Device drivers call our routines to queue packets here. We empty the
221 * queue in the local softnet handler.
222 */
31aa02c5 223DEFINE_PER_CPU(struct softnet_data, softnet_data) = { NULL };
1da177e4
LT
224
225#ifdef CONFIG_SYSFS
226extern int netdev_sysfs_init(void);
227extern int netdev_register_sysfs(struct net_device *);
228extern void netdev_unregister_sysfs(struct net_device *);
229#else
230#define netdev_sysfs_init() (0)
231#define netdev_register_sysfs(dev) (0)
232#define netdev_unregister_sysfs(dev) do { } while(0)
233#endif
234
723e98b7
JP
235#ifdef CONFIG_DEBUG_LOCK_ALLOC
236/*
237 * register_netdevice() inits dev->_xmit_lock and sets lockdep class
238 * according to dev->type
239 */
240static const unsigned short netdev_lock_type[] =
241 {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
242 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
243 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
244 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
245 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
246 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
247 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
248 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
249 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
250 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
251 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
252 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
253 ARPHRD_FCFABRIC, ARPHRD_IEEE802_TR, ARPHRD_IEEE80211,
254 ARPHRD_IEEE80211_PRISM, ARPHRD_IEEE80211_RADIOTAP, ARPHRD_VOID,
255 ARPHRD_NONE};
256
257static const char *netdev_lock_name[] =
258 {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
259 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
260 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
261 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
262 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
263 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
264 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
265 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
266 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
267 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
268 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
269 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
270 "_xmit_FCFABRIC", "_xmit_IEEE802_TR", "_xmit_IEEE80211",
271 "_xmit_IEEE80211_PRISM", "_xmit_IEEE80211_RADIOTAP", "_xmit_VOID",
272 "_xmit_NONE"};
273
274static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
275
276static inline unsigned short netdev_lock_pos(unsigned short dev_type)
277{
278 int i;
279
280 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
281 if (netdev_lock_type[i] == dev_type)
282 return i;
283 /* the last key is used by default */
284 return ARRAY_SIZE(netdev_lock_type) - 1;
285}
286
287static inline void netdev_set_lockdep_class(spinlock_t *lock,
288 unsigned short dev_type)
289{
290 int i;
291
292 i = netdev_lock_pos(dev_type);
293 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
294 netdev_lock_name[i]);
295}
296#else
297static inline void netdev_set_lockdep_class(spinlock_t *lock,
298 unsigned short dev_type)
299{
300}
301#endif
1da177e4
LT
302
303/*******************************************************************************
304
305 Protocol management and registration routines
306
307*******************************************************************************/
308
1da177e4
LT
309/*
310 * Add a protocol ID to the list. Now that the input handler is
311 * smarter we can dispense with all the messy stuff that used to be
312 * here.
313 *
314 * BEWARE!!! Protocol handlers, mangling input packets,
315 * MUST BE last in hash buckets and checking protocol handlers
316 * MUST start from promiscuous ptype_all chain in net_bh.
317 * It is true now, do not change it.
318 * Explanation follows: if protocol handler, mangling packet, will
319 * be the first on list, it is not able to sense, that packet
320 * is cloned and should be copied-on-write, so that it will
321 * change it and subsequent readers will get broken packet.
322 * --ANK (980803)
323 */
324
325/**
326 * dev_add_pack - add packet handler
327 * @pt: packet type declaration
328 *
329 * Add a protocol handler to the networking stack. The passed &packet_type
330 * is linked into kernel lists and may not be freed until it has been
331 * removed from the kernel lists.
332 *
4ec93edb 333 * This call does not sleep therefore it can not
1da177e4
LT
334 * guarantee all CPU's that are in middle of receiving packets
335 * will see the new packet type (until the next received packet).
336 */
337
338void dev_add_pack(struct packet_type *pt)
339{
340 int hash;
341
342 spin_lock_bh(&ptype_lock);
9be9a6b9 343 if (pt->type == htons(ETH_P_ALL))
1da177e4 344 list_add_rcu(&pt->list, &ptype_all);
9be9a6b9 345 else {
1da177e4
LT
346 hash = ntohs(pt->type) & 15;
347 list_add_rcu(&pt->list, &ptype_base[hash]);
348 }
349 spin_unlock_bh(&ptype_lock);
350}
351
1da177e4
LT
352/**
353 * __dev_remove_pack - remove packet handler
354 * @pt: packet type declaration
355 *
356 * Remove a protocol handler that was previously added to the kernel
357 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
358 * from the kernel lists and can be freed or reused once this function
4ec93edb 359 * returns.
1da177e4
LT
360 *
361 * The packet type might still be in use by receivers
362 * and must not be freed until after all the CPU's have gone
363 * through a quiescent state.
364 */
365void __dev_remove_pack(struct packet_type *pt)
366{
367 struct list_head *head;
368 struct packet_type *pt1;
369
370 spin_lock_bh(&ptype_lock);
371
9be9a6b9 372 if (pt->type == htons(ETH_P_ALL))
1da177e4 373 head = &ptype_all;
9be9a6b9 374 else
1da177e4
LT
375 head = &ptype_base[ntohs(pt->type) & 15];
376
377 list_for_each_entry(pt1, head, list) {
378 if (pt == pt1) {
379 list_del_rcu(&pt->list);
380 goto out;
381 }
382 }
383
384 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
385out:
386 spin_unlock_bh(&ptype_lock);
387}
388/**
389 * dev_remove_pack - remove packet handler
390 * @pt: packet type declaration
391 *
392 * Remove a protocol handler that was previously added to the kernel
393 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
394 * from the kernel lists and can be freed or reused once this function
395 * returns.
396 *
397 * This call sleeps to guarantee that no CPU is looking at the packet
398 * type after return.
399 */
400void dev_remove_pack(struct packet_type *pt)
401{
402 __dev_remove_pack(pt);
4ec93edb 403
1da177e4
LT
404 synchronize_net();
405}
406
407/******************************************************************************
408
409 Device Boot-time Settings Routines
410
411*******************************************************************************/
412
413/* Boot time configuration table */
414static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
415
416/**
417 * netdev_boot_setup_add - add new setup entry
418 * @name: name of the device
419 * @map: configured settings for the device
420 *
421 * Adds new setup entry to the dev_boot_setup list. The function
422 * returns 0 on error and 1 on success. This is a generic routine to
423 * all netdevices.
424 */
425static int netdev_boot_setup_add(char *name, struct ifmap *map)
426{
427 struct netdev_boot_setup *s;
428 int i;
429
430 s = dev_boot_setup;
431 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
432 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
433 memset(s[i].name, 0, sizeof(s[i].name));
434 strcpy(s[i].name, name);
435 memcpy(&s[i].map, map, sizeof(s[i].map));
436 break;
437 }
438 }
439
440 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
441}
442
443/**
444 * netdev_boot_setup_check - check boot time settings
445 * @dev: the netdevice
446 *
447 * Check boot time settings for the device.
448 * The found settings are set for the device to be used
449 * later in the device probing.
450 * Returns 0 if no settings found, 1 if they are.
451 */
452int netdev_boot_setup_check(struct net_device *dev)
453{
454 struct netdev_boot_setup *s = dev_boot_setup;
455 int i;
456
457 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
458 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
459 !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
460 dev->irq = s[i].map.irq;
461 dev->base_addr = s[i].map.base_addr;
462 dev->mem_start = s[i].map.mem_start;
463 dev->mem_end = s[i].map.mem_end;
464 return 1;
465 }
466 }
467 return 0;
468}
469
470
471/**
472 * netdev_boot_base - get address from boot time settings
473 * @prefix: prefix for network device
474 * @unit: id for network device
475 *
476 * Check boot time settings for the base address of device.
477 * The found settings are set for the device to be used
478 * later in the device probing.
479 * Returns 0 if no settings found.
480 */
481unsigned long netdev_boot_base(const char *prefix, int unit)
482{
483 const struct netdev_boot_setup *s = dev_boot_setup;
484 char name[IFNAMSIZ];
485 int i;
486
487 sprintf(name, "%s%d", prefix, unit);
488
489 /*
490 * If device already registered then return base of 1
491 * to indicate not to probe for this interface
492 */
493 if (__dev_get_by_name(name))
494 return 1;
495
496 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
497 if (!strcmp(name, s[i].name))
498 return s[i].map.base_addr;
499 return 0;
500}
501
502/*
503 * Saves at boot time configured settings for any netdevice.
504 */
505int __init netdev_boot_setup(char *str)
506{
507 int ints[5];
508 struct ifmap map;
509
510 str = get_options(str, ARRAY_SIZE(ints), ints);
511 if (!str || !*str)
512 return 0;
513
514 /* Save settings */
515 memset(&map, 0, sizeof(map));
516 if (ints[0] > 0)
517 map.irq = ints[1];
518 if (ints[0] > 1)
519 map.base_addr = ints[2];
520 if (ints[0] > 2)
521 map.mem_start = ints[3];
522 if (ints[0] > 3)
523 map.mem_end = ints[4];
524
525 /* Add new entry to the list */
526 return netdev_boot_setup_add(str, &map);
527}
528
529__setup("netdev=", netdev_boot_setup);
530
531/*******************************************************************************
532
533 Device Interface Subroutines
534
535*******************************************************************************/
536
537/**
538 * __dev_get_by_name - find a device by its name
539 * @name: name to find
540 *
541 * Find an interface by name. Must be called under RTNL semaphore
542 * or @dev_base_lock. If the name is found a pointer to the device
543 * is returned. If the name is not found then %NULL is returned. The
544 * reference counters are not incremented so the caller must be
545 * careful with locks.
546 */
547
548struct net_device *__dev_get_by_name(const char *name)
549{
550 struct hlist_node *p;
551
552 hlist_for_each(p, dev_name_hash(name)) {
553 struct net_device *dev
554 = hlist_entry(p, struct net_device, name_hlist);
555 if (!strncmp(dev->name, name, IFNAMSIZ))
556 return dev;
557 }
558 return NULL;
559}
560
561/**
562 * dev_get_by_name - find a device by its name
563 * @name: name to find
564 *
565 * Find an interface by name. This can be called from any
566 * context and does its own locking. The returned handle has
567 * the usage count incremented and the caller must use dev_put() to
568 * release it when it is no longer needed. %NULL is returned if no
569 * matching device is found.
570 */
571
572struct net_device *dev_get_by_name(const char *name)
573{
574 struct net_device *dev;
575
576 read_lock(&dev_base_lock);
577 dev = __dev_get_by_name(name);
578 if (dev)
579 dev_hold(dev);
580 read_unlock(&dev_base_lock);
581 return dev;
582}
583
584/**
585 * __dev_get_by_index - find a device by its ifindex
586 * @ifindex: index of device
587 *
588 * Search for an interface by index. Returns %NULL if the device
589 * is not found or a pointer to the device. The device has not
590 * had its reference counter increased so the caller must be careful
591 * about locking. The caller must hold either the RTNL semaphore
592 * or @dev_base_lock.
593 */
594
595struct net_device *__dev_get_by_index(int ifindex)
596{
597 struct hlist_node *p;
598
599 hlist_for_each(p, dev_index_hash(ifindex)) {
600 struct net_device *dev
601 = hlist_entry(p, struct net_device, index_hlist);
602 if (dev->ifindex == ifindex)
603 return dev;
604 }
605 return NULL;
606}
607
608
609/**
610 * dev_get_by_index - find a device by its ifindex
611 * @ifindex: index of device
612 *
613 * Search for an interface by index. Returns NULL if the device
614 * is not found or a pointer to the device. The device returned has
615 * had a reference added and the pointer is safe until the user calls
616 * dev_put to indicate they have finished with it.
617 */
618
619struct net_device *dev_get_by_index(int ifindex)
620{
621 struct net_device *dev;
622
623 read_lock(&dev_base_lock);
624 dev = __dev_get_by_index(ifindex);
625 if (dev)
626 dev_hold(dev);
627 read_unlock(&dev_base_lock);
628 return dev;
629}
630
631/**
632 * dev_getbyhwaddr - find a device by its hardware address
633 * @type: media type of device
634 * @ha: hardware address
635 *
636 * Search for an interface by MAC address. Returns NULL if the device
637 * is not found or a pointer to the device. The caller must hold the
638 * rtnl semaphore. The returned device has not had its ref count increased
639 * and the caller must therefore be careful about locking
640 *
641 * BUGS:
642 * If the API was consistent this would be __dev_get_by_hwaddr
643 */
644
645struct net_device *dev_getbyhwaddr(unsigned short type, char *ha)
646{
647 struct net_device *dev;
648
649 ASSERT_RTNL();
650
7562f876 651 for_each_netdev(dev)
1da177e4
LT
652 if (dev->type == type &&
653 !memcmp(dev->dev_addr, ha, dev->addr_len))
7562f876
PE
654 return dev;
655
656 return NULL;
1da177e4
LT
657}
658
cf309e3f
JF
659EXPORT_SYMBOL(dev_getbyhwaddr);
660
4e9cac2b 661struct net_device *__dev_getfirstbyhwtype(unsigned short type)
1da177e4
LT
662{
663 struct net_device *dev;
664
4e9cac2b 665 ASSERT_RTNL();
7562f876 666 for_each_netdev(dev)
4e9cac2b 667 if (dev->type == type)
7562f876
PE
668 return dev;
669
670 return NULL;
4e9cac2b
PM
671}
672
673EXPORT_SYMBOL(__dev_getfirstbyhwtype);
674
675struct net_device *dev_getfirstbyhwtype(unsigned short type)
676{
677 struct net_device *dev;
678
679 rtnl_lock();
680 dev = __dev_getfirstbyhwtype(type);
681 if (dev)
682 dev_hold(dev);
1da177e4
LT
683 rtnl_unlock();
684 return dev;
685}
686
687EXPORT_SYMBOL(dev_getfirstbyhwtype);
688
689/**
690 * dev_get_by_flags - find any device with given flags
691 * @if_flags: IFF_* values
692 * @mask: bitmask of bits in if_flags to check
693 *
694 * Search for any interface with the given flags. Returns NULL if a device
4ec93edb 695 * is not found or a pointer to the device. The device returned has
1da177e4
LT
696 * had a reference added and the pointer is safe until the user calls
697 * dev_put to indicate they have finished with it.
698 */
699
700struct net_device * dev_get_by_flags(unsigned short if_flags, unsigned short mask)
701{
7562f876 702 struct net_device *dev, *ret;
1da177e4 703
7562f876 704 ret = NULL;
1da177e4 705 read_lock(&dev_base_lock);
7562f876 706 for_each_netdev(dev) {
1da177e4
LT
707 if (((dev->flags ^ if_flags) & mask) == 0) {
708 dev_hold(dev);
7562f876 709 ret = dev;
1da177e4
LT
710 break;
711 }
712 }
713 read_unlock(&dev_base_lock);
7562f876 714 return ret;
1da177e4
LT
715}
716
717/**
718 * dev_valid_name - check if name is okay for network device
719 * @name: name string
720 *
721 * Network device names need to be valid file names to
c7fa9d18
DM
722 * to allow sysfs to work. We also disallow any kind of
723 * whitespace.
1da177e4 724 */
c2373ee9 725int dev_valid_name(const char *name)
1da177e4 726{
c7fa9d18
DM
727 if (*name == '\0')
728 return 0;
b6fe17d6
SH
729 if (strlen(name) >= IFNAMSIZ)
730 return 0;
c7fa9d18
DM
731 if (!strcmp(name, ".") || !strcmp(name, ".."))
732 return 0;
733
734 while (*name) {
735 if (*name == '/' || isspace(*name))
736 return 0;
737 name++;
738 }
739 return 1;
1da177e4
LT
740}
741
742/**
743 * dev_alloc_name - allocate a name for a device
744 * @dev: device
745 * @name: name format string
746 *
747 * Passed a format string - eg "lt%d" it will try and find a suitable
3041a069
SH
748 * id. It scans list of devices to build up a free map, then chooses
749 * the first empty slot. The caller must hold the dev_base or rtnl lock
750 * while allocating the name and adding the device in order to avoid
751 * duplicates.
752 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
753 * Returns the number of the unit assigned or a negative errno code.
1da177e4
LT
754 */
755
756int dev_alloc_name(struct net_device *dev, const char *name)
757{
758 int i = 0;
759 char buf[IFNAMSIZ];
760 const char *p;
761 const int max_netdevices = 8*PAGE_SIZE;
762 long *inuse;
763 struct net_device *d;
764
765 p = strnchr(name, IFNAMSIZ-1, '%');
766 if (p) {
767 /*
768 * Verify the string as this thing may have come from
769 * the user. There must be either one "%d" and no other "%"
770 * characters.
771 */
772 if (p[1] != 'd' || strchr(p + 2, '%'))
773 return -EINVAL;
774
775 /* Use one page as a bit array of possible slots */
776 inuse = (long *) get_zeroed_page(GFP_ATOMIC);
777 if (!inuse)
778 return -ENOMEM;
779
7562f876 780 for_each_netdev(d) {
1da177e4
LT
781 if (!sscanf(d->name, name, &i))
782 continue;
783 if (i < 0 || i >= max_netdevices)
784 continue;
785
786 /* avoid cases where sscanf is not exact inverse of printf */
787 snprintf(buf, sizeof(buf), name, i);
788 if (!strncmp(buf, d->name, IFNAMSIZ))
789 set_bit(i, inuse);
790 }
791
792 i = find_first_zero_bit(inuse, max_netdevices);
793 free_page((unsigned long) inuse);
794 }
795
796 snprintf(buf, sizeof(buf), name, i);
797 if (!__dev_get_by_name(buf)) {
798 strlcpy(dev->name, buf, IFNAMSIZ);
799 return i;
800 }
801
802 /* It is possible to run out of possible slots
803 * when the name is long and there isn't enough space left
804 * for the digits, or if all bits are used.
805 */
806 return -ENFILE;
807}
808
809
810/**
811 * dev_change_name - change name of a device
812 * @dev: device
813 * @newname: name (or format string) must be at least IFNAMSIZ
814 *
815 * Change name of a device, can pass format strings "eth%d".
816 * for wildcarding.
817 */
818int dev_change_name(struct net_device *dev, char *newname)
819{
820 int err = 0;
821
822 ASSERT_RTNL();
823
824 if (dev->flags & IFF_UP)
825 return -EBUSY;
826
827 if (!dev_valid_name(newname))
828 return -EINVAL;
829
830 if (strchr(newname, '%')) {
831 err = dev_alloc_name(dev, newname);
832 if (err < 0)
833 return err;
834 strcpy(newname, dev->name);
835 }
836 else if (__dev_get_by_name(newname))
837 return -EEXIST;
838 else
839 strlcpy(dev->name, newname, IFNAMSIZ);
840
92749821 841 device_rename(&dev->dev, dev->name);
7f988eab
HX
842
843 write_lock_bh(&dev_base_lock);
92749821
EB
844 hlist_del(&dev->name_hlist);
845 hlist_add_head(&dev->name_hlist, dev_name_hash(dev->name));
7f988eab
HX
846 write_unlock_bh(&dev_base_lock);
847
92749821 848 raw_notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev);
1da177e4
LT
849
850 return err;
851}
852
d8a33ac4 853/**
3041a069 854 * netdev_features_change - device changes features
d8a33ac4
SH
855 * @dev: device to cause notification
856 *
857 * Called to indicate a device has changed features.
858 */
859void netdev_features_change(struct net_device *dev)
860{
f07d5b94 861 raw_notifier_call_chain(&netdev_chain, NETDEV_FEAT_CHANGE, dev);
d8a33ac4
SH
862}
863EXPORT_SYMBOL(netdev_features_change);
864
1da177e4
LT
865/**
866 * netdev_state_change - device changes state
867 * @dev: device to cause notification
868 *
869 * Called to indicate a device has changed state. This function calls
870 * the notifier chains for netdev_chain and sends a NEWLINK message
871 * to the routing socket.
872 */
873void netdev_state_change(struct net_device *dev)
874{
875 if (dev->flags & IFF_UP) {
f07d5b94 876 raw_notifier_call_chain(&netdev_chain,
e041c683 877 NETDEV_CHANGE, dev);
1da177e4
LT
878 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
879 }
880}
881
882/**
883 * dev_load - load a network module
884 * @name: name of interface
885 *
886 * If a network interface is not present and the process has suitable
887 * privileges this function loads the module. If module loading is not
888 * available in this kernel then it becomes a nop.
889 */
890
891void dev_load(const char *name)
892{
4ec93edb 893 struct net_device *dev;
1da177e4
LT
894
895 read_lock(&dev_base_lock);
896 dev = __dev_get_by_name(name);
897 read_unlock(&dev_base_lock);
898
899 if (!dev && capable(CAP_SYS_MODULE))
900 request_module("%s", name);
901}
902
903static int default_rebuild_header(struct sk_buff *skb)
904{
905 printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n",
906 skb->dev ? skb->dev->name : "NULL!!!");
907 kfree_skb(skb);
908 return 1;
909}
910
1da177e4
LT
911/**
912 * dev_open - prepare an interface for use.
913 * @dev: device to open
914 *
915 * Takes a device from down to up state. The device's private open
916 * function is invoked and then the multicast lists are loaded. Finally
917 * the device is moved into the up state and a %NETDEV_UP message is
918 * sent to the netdev notifier chain.
919 *
920 * Calling this function on an active interface is a nop. On a failure
921 * a negative errno code is returned.
922 */
923int dev_open(struct net_device *dev)
924{
925 int ret = 0;
926
927 /*
928 * Is it already up?
929 */
930
931 if (dev->flags & IFF_UP)
932 return 0;
933
934 /*
935 * Is it even present?
936 */
937 if (!netif_device_present(dev))
938 return -ENODEV;
939
940 /*
941 * Call device private open method
942 */
943 set_bit(__LINK_STATE_START, &dev->state);
944 if (dev->open) {
945 ret = dev->open(dev);
946 if (ret)
947 clear_bit(__LINK_STATE_START, &dev->state);
948 }
949
4ec93edb 950 /*
1da177e4
LT
951 * If it went open OK then:
952 */
953
954 if (!ret) {
955 /*
956 * Set the flags.
957 */
958 dev->flags |= IFF_UP;
959
960 /*
961 * Initialize multicasting status
962 */
4417da66 963 dev_set_rx_mode(dev);
1da177e4
LT
964
965 /*
966 * Wakeup transmit queue engine
967 */
968 dev_activate(dev);
969
970 /*
971 * ... and announce new interface.
972 */
f07d5b94 973 raw_notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
1da177e4
LT
974 }
975 return ret;
976}
977
978/**
979 * dev_close - shutdown an interface.
980 * @dev: device to shutdown
981 *
982 * This function moves an active device into down state. A
983 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
984 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
985 * chain.
986 */
987int dev_close(struct net_device *dev)
988{
989 if (!(dev->flags & IFF_UP))
990 return 0;
991
992 /*
993 * Tell people we are going down, so that they can
994 * prepare to death, when device is still operating.
995 */
f07d5b94 996 raw_notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
1da177e4
LT
997
998 dev_deactivate(dev);
999
1000 clear_bit(__LINK_STATE_START, &dev->state);
1001
1002 /* Synchronize to scheduled poll. We cannot touch poll list,
1003 * it can be even on different cpu. So just clear netif_running(),
1004 * and wait when poll really will happen. Actually, the best place
1005 * for this is inside dev->stop() after device stopped its irq
1006 * engine, but this requires more changes in devices. */
1007
1008 smp_mb__after_clear_bit(); /* Commit netif_running(). */
1009 while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) {
1010 /* No hurry. */
6192b54b 1011 msleep(1);
1da177e4
LT
1012 }
1013
1014 /*
1015 * Call the device specific close. This cannot fail.
1016 * Only if device is UP
1017 *
1018 * We allow it to be called even after a DETACH hot-plug
1019 * event.
1020 */
1021 if (dev->stop)
1022 dev->stop(dev);
1023
1024 /*
1025 * Device is now down.
1026 */
1027
1028 dev->flags &= ~IFF_UP;
1029
1030 /*
1031 * Tell people we are down
1032 */
f07d5b94 1033 raw_notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
1da177e4
LT
1034
1035 return 0;
1036}
1037
1038
1039/*
1040 * Device change register/unregister. These are not inline or static
1041 * as we export them to the world.
1042 */
1043
1044/**
1045 * register_netdevice_notifier - register a network notifier block
1046 * @nb: notifier
1047 *
1048 * Register a notifier to be called when network device events occur.
1049 * The notifier passed is linked into the kernel structures and must
1050 * not be reused until it has been unregistered. A negative errno code
1051 * is returned on a failure.
1052 *
1053 * When registered all registration and up events are replayed
4ec93edb 1054 * to the new notifier to allow device to have a race free
1da177e4
LT
1055 * view of the network device list.
1056 */
1057
1058int register_netdevice_notifier(struct notifier_block *nb)
1059{
1060 struct net_device *dev;
1061 int err;
1062
1063 rtnl_lock();
f07d5b94 1064 err = raw_notifier_chain_register(&netdev_chain, nb);
1da177e4 1065 if (!err) {
7562f876 1066 for_each_netdev(dev) {
1da177e4
LT
1067 nb->notifier_call(nb, NETDEV_REGISTER, dev);
1068
4ec93edb 1069 if (dev->flags & IFF_UP)
1da177e4
LT
1070 nb->notifier_call(nb, NETDEV_UP, dev);
1071 }
1072 }
1073 rtnl_unlock();
1074 return err;
1075}
1076
1077/**
1078 * unregister_netdevice_notifier - unregister a network notifier block
1079 * @nb: notifier
1080 *
1081 * Unregister a notifier previously registered by
1082 * register_netdevice_notifier(). The notifier is unlinked into the
1083 * kernel structures and may then be reused. A negative errno code
1084 * is returned on a failure.
1085 */
1086
1087int unregister_netdevice_notifier(struct notifier_block *nb)
1088{
9f514950
HX
1089 int err;
1090
1091 rtnl_lock();
f07d5b94 1092 err = raw_notifier_chain_unregister(&netdev_chain, nb);
9f514950
HX
1093 rtnl_unlock();
1094 return err;
1da177e4
LT
1095}
1096
1097/**
1098 * call_netdevice_notifiers - call all network notifier blocks
1099 * @val: value passed unmodified to notifier function
1100 * @v: pointer passed unmodified to notifier function
1101 *
1102 * Call all network notifier blocks. Parameters and return value
f07d5b94 1103 * are as for raw_notifier_call_chain().
1da177e4
LT
1104 */
1105
1106int call_netdevice_notifiers(unsigned long val, void *v)
1107{
f07d5b94 1108 return raw_notifier_call_chain(&netdev_chain, val, v);
1da177e4
LT
1109}
1110
1111/* When > 0 there are consumers of rx skb time stamps */
1112static atomic_t netstamp_needed = ATOMIC_INIT(0);
1113
1114void net_enable_timestamp(void)
1115{
1116 atomic_inc(&netstamp_needed);
1117}
1118
1119void net_disable_timestamp(void)
1120{
1121 atomic_dec(&netstamp_needed);
1122}
1123
a61bbcf2 1124static inline void net_timestamp(struct sk_buff *skb)
1da177e4
LT
1125{
1126 if (atomic_read(&netstamp_needed))
a61bbcf2 1127 __net_timestamp(skb);
b7aa0bf7
ED
1128 else
1129 skb->tstamp.tv64 = 0;
1da177e4
LT
1130}
1131
1132/*
1133 * Support routine. Sends outgoing frames to any network
1134 * taps currently in use.
1135 */
1136
f6a78bfc 1137static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1da177e4
LT
1138{
1139 struct packet_type *ptype;
a61bbcf2
PM
1140
1141 net_timestamp(skb);
1da177e4
LT
1142
1143 rcu_read_lock();
1144 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1145 /* Never send packets back to the socket
1146 * they originated from - MvS (miquels@drinkel.ow.org)
1147 */
1148 if ((ptype->dev == dev || !ptype->dev) &&
1149 (ptype->af_packet_priv == NULL ||
1150 (struct sock *)ptype->af_packet_priv != skb->sk)) {
1151 struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
1152 if (!skb2)
1153 break;
1154
1155 /* skb->nh should be correctly
1156 set by sender, so that the second statement is
1157 just protection against buggy protocols.
1158 */
459a98ed 1159 skb_reset_mac_header(skb2);
1da177e4 1160
d56f90a7 1161 if (skb_network_header(skb2) < skb2->data ||
27a884dc 1162 skb2->network_header > skb2->tail) {
1da177e4
LT
1163 if (net_ratelimit())
1164 printk(KERN_CRIT "protocol %04x is "
1165 "buggy, dev %s\n",
1166 skb2->protocol, dev->name);
c1d2bbe1 1167 skb_reset_network_header(skb2);
1da177e4
LT
1168 }
1169
b0e380b1 1170 skb2->transport_header = skb2->network_header;
1da177e4 1171 skb2->pkt_type = PACKET_OUTGOING;
f2ccd8fa 1172 ptype->func(skb2, skb->dev, ptype, skb->dev);
1da177e4
LT
1173 }
1174 }
1175 rcu_read_unlock();
1176}
1177
56079431
DV
1178
1179void __netif_schedule(struct net_device *dev)
1180{
1181 if (!test_and_set_bit(__LINK_STATE_SCHED, &dev->state)) {
1182 unsigned long flags;
1183 struct softnet_data *sd;
1184
1185 local_irq_save(flags);
1186 sd = &__get_cpu_var(softnet_data);
1187 dev->next_sched = sd->output_queue;
1188 sd->output_queue = dev;
1189 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1190 local_irq_restore(flags);
1191 }
1192}
1193EXPORT_SYMBOL(__netif_schedule);
1194
1195void __netif_rx_schedule(struct net_device *dev)
1196{
1197 unsigned long flags;
1198
1199 local_irq_save(flags);
1200 dev_hold(dev);
1201 list_add_tail(&dev->poll_list, &__get_cpu_var(softnet_data).poll_list);
1202 if (dev->quota < 0)
1203 dev->quota += dev->weight;
1204 else
1205 dev->quota = dev->weight;
1206 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
1207 local_irq_restore(flags);
1208}
1209EXPORT_SYMBOL(__netif_rx_schedule);
1210
1211void dev_kfree_skb_any(struct sk_buff *skb)
1212{
1213 if (in_irq() || irqs_disabled())
1214 dev_kfree_skb_irq(skb);
1215 else
1216 dev_kfree_skb(skb);
1217}
1218EXPORT_SYMBOL(dev_kfree_skb_any);
1219
1220
1221/* Hot-plugging. */
1222void netif_device_detach(struct net_device *dev)
1223{
1224 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
1225 netif_running(dev)) {
1226 netif_stop_queue(dev);
1227 }
1228}
1229EXPORT_SYMBOL(netif_device_detach);
1230
1231void netif_device_attach(struct net_device *dev)
1232{
1233 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
1234 netif_running(dev)) {
1235 netif_wake_queue(dev);
4ec93edb 1236 __netdev_watchdog_up(dev);
56079431
DV
1237 }
1238}
1239EXPORT_SYMBOL(netif_device_attach);
1240
1241
1da177e4
LT
1242/*
1243 * Invalidate hardware checksum when packet is to be mangled, and
1244 * complete checksum manually on outgoing path.
1245 */
84fa7933 1246int skb_checksum_help(struct sk_buff *skb)
1da177e4 1247{
d3bc23e7 1248 __wsum csum;
663ead3b 1249 int ret = 0, offset;
1da177e4 1250
84fa7933 1251 if (skb->ip_summed == CHECKSUM_COMPLETE)
a430a43d
HX
1252 goto out_set_summed;
1253
1254 if (unlikely(skb_shinfo(skb)->gso_size)) {
a430a43d
HX
1255 /* Let GSO fix up the checksum. */
1256 goto out_set_summed;
1da177e4
LT
1257 }
1258
1259 if (skb_cloned(skb)) {
1260 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1261 if (ret)
1262 goto out;
1263 }
1264
663ead3b 1265 offset = skb->csum_start - skb_headroom(skb);
09a62660 1266 BUG_ON(offset > (int)skb->len);
1da177e4
LT
1267 csum = skb_checksum(skb, offset, skb->len-offset, 0);
1268
663ead3b 1269 offset = skb_headlen(skb) - offset;
09a62660 1270 BUG_ON(offset <= 0);
ff1dcadb 1271 BUG_ON(skb->csum_offset + 2 > offset);
1da177e4 1272
663ead3b
HX
1273 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) =
1274 csum_fold(csum);
a430a43d 1275out_set_summed:
1da177e4 1276 skb->ip_summed = CHECKSUM_NONE;
4ec93edb 1277out:
1da177e4
LT
1278 return ret;
1279}
1280
f6a78bfc
HX
1281/**
1282 * skb_gso_segment - Perform segmentation on skb.
1283 * @skb: buffer to segment
576a30eb 1284 * @features: features for the output path (see dev->features)
f6a78bfc
HX
1285 *
1286 * This function segments the given skb and returns a list of segments.
576a30eb
HX
1287 *
1288 * It may return NULL if the skb requires no segmentation. This is
1289 * only possible when GSO is used for verifying header integrity.
f6a78bfc 1290 */
576a30eb 1291struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
f6a78bfc
HX
1292{
1293 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
1294 struct packet_type *ptype;
252e3346 1295 __be16 type = skb->protocol;
a430a43d 1296 int err;
f6a78bfc
HX
1297
1298 BUG_ON(skb_shinfo(skb)->frag_list);
f6a78bfc 1299
459a98ed 1300 skb_reset_mac_header(skb);
b0e380b1 1301 skb->mac_len = skb->network_header - skb->mac_header;
f6a78bfc
HX
1302 __skb_pull(skb, skb->mac_len);
1303
f9d106a6 1304 if (WARN_ON(skb->ip_summed != CHECKSUM_PARTIAL)) {
a430a43d
HX
1305 if (skb_header_cloned(skb) &&
1306 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
1307 return ERR_PTR(err);
1308 }
1309
f6a78bfc
HX
1310 rcu_read_lock();
1311 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type) & 15], list) {
1312 if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
84fa7933 1313 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
a430a43d
HX
1314 err = ptype->gso_send_check(skb);
1315 segs = ERR_PTR(err);
1316 if (err || skb_gso_ok(skb, features))
1317 break;
d56f90a7
ACM
1318 __skb_push(skb, (skb->data -
1319 skb_network_header(skb)));
a430a43d 1320 }
576a30eb 1321 segs = ptype->gso_segment(skb, features);
f6a78bfc
HX
1322 break;
1323 }
1324 }
1325 rcu_read_unlock();
1326
98e399f8 1327 __skb_push(skb, skb->data - skb_mac_header(skb));
576a30eb 1328
f6a78bfc
HX
1329 return segs;
1330}
1331
1332EXPORT_SYMBOL(skb_gso_segment);
1333
fb286bb2
HX
1334/* Take action when hardware reception checksum errors are detected. */
1335#ifdef CONFIG_BUG
1336void netdev_rx_csum_fault(struct net_device *dev)
1337{
1338 if (net_ratelimit()) {
4ec93edb 1339 printk(KERN_ERR "%s: hw csum failure.\n",
246a4212 1340 dev ? dev->name : "<unknown>");
fb286bb2
HX
1341 dump_stack();
1342 }
1343}
1344EXPORT_SYMBOL(netdev_rx_csum_fault);
1345#endif
1346
1da177e4
LT
1347/* Actually, we should eliminate this check as soon as we know, that:
1348 * 1. IOMMU is present and allows to map all the memory.
1349 * 2. No high memory really exists on this machine.
1350 */
1351
1352static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1353{
3d3a8533 1354#ifdef CONFIG_HIGHMEM
1da177e4
LT
1355 int i;
1356
1357 if (dev->features & NETIF_F_HIGHDMA)
1358 return 0;
1359
1360 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1361 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1362 return 1;
1363
3d3a8533 1364#endif
1da177e4
LT
1365 return 0;
1366}
1da177e4 1367
f6a78bfc
HX
1368struct dev_gso_cb {
1369 void (*destructor)(struct sk_buff *skb);
1370};
1371
1372#define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
1373
1374static void dev_gso_skb_destructor(struct sk_buff *skb)
1375{
1376 struct dev_gso_cb *cb;
1377
1378 do {
1379 struct sk_buff *nskb = skb->next;
1380
1381 skb->next = nskb->next;
1382 nskb->next = NULL;
1383 kfree_skb(nskb);
1384 } while (skb->next);
1385
1386 cb = DEV_GSO_CB(skb);
1387 if (cb->destructor)
1388 cb->destructor(skb);
1389}
1390
1391/**
1392 * dev_gso_segment - Perform emulated hardware segmentation on skb.
1393 * @skb: buffer to segment
1394 *
1395 * This function segments the given skb and stores the list of segments
1396 * in skb->next.
1397 */
1398static int dev_gso_segment(struct sk_buff *skb)
1399{
1400 struct net_device *dev = skb->dev;
1401 struct sk_buff *segs;
576a30eb
HX
1402 int features = dev->features & ~(illegal_highdma(dev, skb) ?
1403 NETIF_F_SG : 0);
1404
1405 segs = skb_gso_segment(skb, features);
1406
1407 /* Verifying header integrity only. */
1408 if (!segs)
1409 return 0;
f6a78bfc 1410
f6a78bfc
HX
1411 if (unlikely(IS_ERR(segs)))
1412 return PTR_ERR(segs);
1413
1414 skb->next = segs;
1415 DEV_GSO_CB(skb)->destructor = skb->destructor;
1416 skb->destructor = dev_gso_skb_destructor;
1417
1418 return 0;
1419}
1420
1421int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1422{
1423 if (likely(!skb->next)) {
9be9a6b9 1424 if (!list_empty(&ptype_all))
f6a78bfc
HX
1425 dev_queue_xmit_nit(skb, dev);
1426
576a30eb
HX
1427 if (netif_needs_gso(dev, skb)) {
1428 if (unlikely(dev_gso_segment(skb)))
1429 goto out_kfree_skb;
1430 if (skb->next)
1431 goto gso;
1432 }
f6a78bfc 1433
576a30eb 1434 return dev->hard_start_xmit(skb, dev);
f6a78bfc
HX
1435 }
1436
576a30eb 1437gso:
f6a78bfc
HX
1438 do {
1439 struct sk_buff *nskb = skb->next;
1440 int rc;
1441
1442 skb->next = nskb->next;
1443 nskb->next = NULL;
1444 rc = dev->hard_start_xmit(nskb, dev);
1445 if (unlikely(rc)) {
f54d9e8d 1446 nskb->next = skb->next;
f6a78bfc
HX
1447 skb->next = nskb;
1448 return rc;
1449 }
f25f4e44
PWJ
1450 if (unlikely((netif_queue_stopped(dev) ||
1451 netif_subqueue_stopped(dev, skb->queue_mapping)) &&
1452 skb->next))
f54d9e8d 1453 return NETDEV_TX_BUSY;
f6a78bfc 1454 } while (skb->next);
4ec93edb 1455
f6a78bfc
HX
1456 skb->destructor = DEV_GSO_CB(skb)->destructor;
1457
1458out_kfree_skb:
1459 kfree_skb(skb);
1460 return 0;
1461}
1462
1da177e4
LT
1463#define HARD_TX_LOCK(dev, cpu) { \
1464 if ((dev->features & NETIF_F_LLTX) == 0) { \
932ff279 1465 netif_tx_lock(dev); \
1da177e4
LT
1466 } \
1467}
1468
1469#define HARD_TX_UNLOCK(dev) { \
1470 if ((dev->features & NETIF_F_LLTX) == 0) { \
932ff279 1471 netif_tx_unlock(dev); \
1da177e4
LT
1472 } \
1473}
1474
1475/**
1476 * dev_queue_xmit - transmit a buffer
1477 * @skb: buffer to transmit
1478 *
1479 * Queue a buffer for transmission to a network device. The caller must
1480 * have set the device and priority and built the buffer before calling
1481 * this function. The function can be called from an interrupt.
1482 *
1483 * A negative errno code is returned on a failure. A success does not
1484 * guarantee the frame will be transmitted as it may be dropped due
1485 * to congestion or traffic shaping.
af191367
BG
1486 *
1487 * -----------------------------------------------------------------------------------
1488 * I notice this method can also return errors from the queue disciplines,
1489 * including NET_XMIT_DROP, which is a positive value. So, errors can also
1490 * be positive.
1491 *
1492 * Regardless of the return value, the skb is consumed, so it is currently
1493 * difficult to retry a send to this method. (You can bump the ref count
1494 * before sending to hold a reference for retry if you are careful.)
1495 *
1496 * When calling this method, interrupts MUST be enabled. This is because
1497 * the BH enable code must have IRQs enabled so that it will not deadlock.
1498 * --BLG
1da177e4
LT
1499 */
1500
1501int dev_queue_xmit(struct sk_buff *skb)
1502{
1503 struct net_device *dev = skb->dev;
1504 struct Qdisc *q;
1505 int rc = -ENOMEM;
1506
f6a78bfc
HX
1507 /* GSO will handle the following emulations directly. */
1508 if (netif_needs_gso(dev, skb))
1509 goto gso;
1510
1da177e4
LT
1511 if (skb_shinfo(skb)->frag_list &&
1512 !(dev->features & NETIF_F_FRAGLIST) &&
364c6bad 1513 __skb_linearize(skb))
1da177e4
LT
1514 goto out_kfree_skb;
1515
1516 /* Fragmented skb is linearized if device does not support SG,
1517 * or if at least one of fragments is in highmem and device
1518 * does not support DMA from it.
1519 */
1520 if (skb_shinfo(skb)->nr_frags &&
1521 (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
364c6bad 1522 __skb_linearize(skb))
1da177e4
LT
1523 goto out_kfree_skb;
1524
1525 /* If packet is not checksummed and device does not support
1526 * checksumming for this protocol, complete checksumming here.
1527 */
663ead3b
HX
1528 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1529 skb_set_transport_header(skb, skb->csum_start -
1530 skb_headroom(skb));
1531
a298830c
HX
1532 if (!(dev->features & NETIF_F_GEN_CSUM) &&
1533 !((dev->features & NETIF_F_IP_CSUM) &&
1534 skb->protocol == htons(ETH_P_IP)) &&
1535 !((dev->features & NETIF_F_IPV6_CSUM) &&
1536 skb->protocol == htons(ETH_P_IPV6)))
663ead3b
HX
1537 if (skb_checksum_help(skb))
1538 goto out_kfree_skb;
1539 }
1da177e4 1540
f6a78bfc 1541gso:
2d7ceece
ED
1542 spin_lock_prefetch(&dev->queue_lock);
1543
4ec93edb
YH
1544 /* Disable soft irqs for various locks below. Also
1545 * stops preemption for RCU.
1da177e4 1546 */
4ec93edb 1547 rcu_read_lock_bh();
1da177e4 1548
4ec93edb
YH
1549 /* Updates of qdisc are serialized by queue_lock.
1550 * The struct Qdisc which is pointed to by qdisc is now a
1551 * rcu structure - it may be accessed without acquiring
1da177e4 1552 * a lock (but the structure may be stale.) The freeing of the
4ec93edb 1553 * qdisc will be deferred until it's known that there are no
1da177e4 1554 * more references to it.
4ec93edb
YH
1555 *
1556 * If the qdisc has an enqueue function, we still need to
1da177e4
LT
1557 * hold the queue_lock before calling it, since queue_lock
1558 * also serializes access to the device queue.
1559 */
1560
1561 q = rcu_dereference(dev->qdisc);
1562#ifdef CONFIG_NET_CLS_ACT
1563 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
1564#endif
1565 if (q->enqueue) {
1566 /* Grab device queue */
1567 spin_lock(&dev->queue_lock);
85670cc1
PM
1568 q = dev->qdisc;
1569 if (q->enqueue) {
f25f4e44
PWJ
1570 /* reset queue_mapping to zero */
1571 skb->queue_mapping = 0;
85670cc1
PM
1572 rc = q->enqueue(skb, q);
1573 qdisc_run(dev);
1574 spin_unlock(&dev->queue_lock);
1da177e4 1575
85670cc1
PM
1576 rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
1577 goto out;
1578 }
1da177e4 1579 spin_unlock(&dev->queue_lock);
1da177e4
LT
1580 }
1581
1582 /* The device has no queue. Common case for software devices:
1583 loopback, all the sorts of tunnels...
1584
932ff279
HX
1585 Really, it is unlikely that netif_tx_lock protection is necessary
1586 here. (f.e. loopback and IP tunnels are clean ignoring statistics
1da177e4
LT
1587 counters.)
1588 However, it is possible, that they rely on protection
1589 made by us here.
1590
1591 Check this and shot the lock. It is not prone from deadlocks.
1592 Either shot noqueue qdisc, it is even simpler 8)
1593 */
1594 if (dev->flags & IFF_UP) {
1595 int cpu = smp_processor_id(); /* ok because BHs are off */
1596
1597 if (dev->xmit_lock_owner != cpu) {
1598
1599 HARD_TX_LOCK(dev, cpu);
1600
f25f4e44
PWJ
1601 if (!netif_queue_stopped(dev) &&
1602 !netif_subqueue_stopped(dev, skb->queue_mapping)) {
1da177e4 1603 rc = 0;
f6a78bfc 1604 if (!dev_hard_start_xmit(skb, dev)) {
1da177e4
LT
1605 HARD_TX_UNLOCK(dev);
1606 goto out;
1607 }
1608 }
1609 HARD_TX_UNLOCK(dev);
1610 if (net_ratelimit())
1611 printk(KERN_CRIT "Virtual device %s asks to "
1612 "queue packet!\n", dev->name);
1613 } else {
1614 /* Recursion is detected! It is possible,
1615 * unfortunately */
1616 if (net_ratelimit())
1617 printk(KERN_CRIT "Dead loop on virtual device "
1618 "%s, fix it urgently!\n", dev->name);
1619 }
1620 }
1621
1622 rc = -ENETDOWN;
d4828d85 1623 rcu_read_unlock_bh();
1da177e4
LT
1624
1625out_kfree_skb:
1626 kfree_skb(skb);
1627 return rc;
1628out:
d4828d85 1629 rcu_read_unlock_bh();
1da177e4
LT
1630 return rc;
1631}
1632
1633
1634/*=======================================================================
1635 Receiver routines
1636 =======================================================================*/
1637
6b2bedc3
SH
1638int netdev_max_backlog __read_mostly = 1000;
1639int netdev_budget __read_mostly = 300;
1640int weight_p __read_mostly = 64; /* old backlog weight */
1da177e4
LT
1641
1642DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
1643
1644
1da177e4
LT
1645/**
1646 * netif_rx - post buffer to the network code
1647 * @skb: buffer to post
1648 *
1649 * This function receives a packet from a device driver and queues it for
1650 * the upper (protocol) levels to process. It always succeeds. The buffer
1651 * may be dropped during processing for congestion control or by the
1652 * protocol layers.
1653 *
1654 * return values:
1655 * NET_RX_SUCCESS (no congestion)
1656 * NET_RX_CN_LOW (low congestion)
1657 * NET_RX_CN_MOD (moderate congestion)
1658 * NET_RX_CN_HIGH (high congestion)
1659 * NET_RX_DROP (packet was dropped)
1660 *
1661 */
1662
1663int netif_rx(struct sk_buff *skb)
1664{
1da177e4
LT
1665 struct softnet_data *queue;
1666 unsigned long flags;
1667
1668 /* if netpoll wants it, pretend we never saw it */
1669 if (netpoll_rx(skb))
1670 return NET_RX_DROP;
1671
b7aa0bf7 1672 if (!skb->tstamp.tv64)
a61bbcf2 1673 net_timestamp(skb);
1da177e4
LT
1674
1675 /*
1676 * The code is rearranged so that the path is the most
1677 * short when CPU is congested, but is still operating.
1678 */
1679 local_irq_save(flags);
1da177e4
LT
1680 queue = &__get_cpu_var(softnet_data);
1681
1682 __get_cpu_var(netdev_rx_stat).total++;
1683 if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1684 if (queue->input_pkt_queue.qlen) {
1da177e4
LT
1685enqueue:
1686 dev_hold(skb->dev);
1687 __skb_queue_tail(&queue->input_pkt_queue, skb);
1da177e4 1688 local_irq_restore(flags);
34008d8c 1689 return NET_RX_SUCCESS;
1da177e4
LT
1690 }
1691
1da177e4
LT
1692 netif_rx_schedule(&queue->backlog_dev);
1693 goto enqueue;
1694 }
1695
1da177e4
LT
1696 __get_cpu_var(netdev_rx_stat).dropped++;
1697 local_irq_restore(flags);
1698
1699 kfree_skb(skb);
1700 return NET_RX_DROP;
1701}
1702
1703int netif_rx_ni(struct sk_buff *skb)
1704{
1705 int err;
1706
1707 preempt_disable();
1708 err = netif_rx(skb);
1709 if (local_softirq_pending())
1710 do_softirq();
1711 preempt_enable();
1712
1713 return err;
1714}
1715
1716EXPORT_SYMBOL(netif_rx_ni);
1717
f2ccd8fa 1718static inline struct net_device *skb_bond(struct sk_buff *skb)
1da177e4
LT
1719{
1720 struct net_device *dev = skb->dev;
1721
8f903c70 1722 if (dev->master) {
7ea49ed7 1723 if (skb_bond_should_drop(skb)) {
8f903c70
JV
1724 kfree_skb(skb);
1725 return NULL;
1726 }
1da177e4 1727 skb->dev = dev->master;
8f903c70 1728 }
f2ccd8fa
DM
1729
1730 return dev;
1da177e4
LT
1731}
1732
1733static void net_tx_action(struct softirq_action *h)
1734{
1735 struct softnet_data *sd = &__get_cpu_var(softnet_data);
1736
1737 if (sd->completion_queue) {
1738 struct sk_buff *clist;
1739
1740 local_irq_disable();
1741 clist = sd->completion_queue;
1742 sd->completion_queue = NULL;
1743 local_irq_enable();
1744
1745 while (clist) {
1746 struct sk_buff *skb = clist;
1747 clist = clist->next;
1748
1749 BUG_TRAP(!atomic_read(&skb->users));
1750 __kfree_skb(skb);
1751 }
1752 }
1753
1754 if (sd->output_queue) {
1755 struct net_device *head;
1756
1757 local_irq_disable();
1758 head = sd->output_queue;
1759 sd->output_queue = NULL;
1760 local_irq_enable();
1761
1762 while (head) {
1763 struct net_device *dev = head;
1764 head = head->next_sched;
1765
1766 smp_mb__before_clear_bit();
1767 clear_bit(__LINK_STATE_SCHED, &dev->state);
1768
1769 if (spin_trylock(&dev->queue_lock)) {
1770 qdisc_run(dev);
1771 spin_unlock(&dev->queue_lock);
1772 } else {
1773 netif_schedule(dev);
1774 }
1775 }
1776 }
1777}
1778
6f05f629
SH
1779static inline int deliver_skb(struct sk_buff *skb,
1780 struct packet_type *pt_prev,
1781 struct net_device *orig_dev)
1da177e4
LT
1782{
1783 atomic_inc(&skb->users);
f2ccd8fa 1784 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1da177e4
LT
1785}
1786
1787#if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
6229e362 1788/* These hooks defined here for ATM */
1da177e4
LT
1789struct net_bridge;
1790struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
1791 unsigned char *addr);
6229e362 1792void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent) __read_mostly;
1da177e4 1793
6229e362
SH
1794/*
1795 * If bridge module is loaded call bridging hook.
1796 * returns NULL if packet was consumed.
1797 */
1798struct sk_buff *(*br_handle_frame_hook)(struct net_bridge_port *p,
1799 struct sk_buff *skb) __read_mostly;
1800static inline struct sk_buff *handle_bridge(struct sk_buff *skb,
1801 struct packet_type **pt_prev, int *ret,
1802 struct net_device *orig_dev)
1da177e4
LT
1803{
1804 struct net_bridge_port *port;
1805
6229e362
SH
1806 if (skb->pkt_type == PACKET_LOOPBACK ||
1807 (port = rcu_dereference(skb->dev->br_port)) == NULL)
1808 return skb;
1da177e4
LT
1809
1810 if (*pt_prev) {
6229e362 1811 *ret = deliver_skb(skb, *pt_prev, orig_dev);
1da177e4 1812 *pt_prev = NULL;
4ec93edb
YH
1813 }
1814
6229e362 1815 return br_handle_frame_hook(port, skb);
1da177e4
LT
1816}
1817#else
6229e362 1818#define handle_bridge(skb, pt_prev, ret, orig_dev) (skb)
1da177e4
LT
1819#endif
1820
b863ceb7
PM
1821#if defined(CONFIG_MACVLAN) || defined(CONFIG_MACVLAN_MODULE)
1822struct sk_buff *(*macvlan_handle_frame_hook)(struct sk_buff *skb) __read_mostly;
1823EXPORT_SYMBOL_GPL(macvlan_handle_frame_hook);
1824
1825static inline struct sk_buff *handle_macvlan(struct sk_buff *skb,
1826 struct packet_type **pt_prev,
1827 int *ret,
1828 struct net_device *orig_dev)
1829{
1830 if (skb->dev->macvlan_port == NULL)
1831 return skb;
1832
1833 if (*pt_prev) {
1834 *ret = deliver_skb(skb, *pt_prev, orig_dev);
1835 *pt_prev = NULL;
1836 }
1837 return macvlan_handle_frame_hook(skb);
1838}
1839#else
1840#define handle_macvlan(skb, pt_prev, ret, orig_dev) (skb)
1841#endif
1842
1da177e4
LT
1843#ifdef CONFIG_NET_CLS_ACT
1844/* TODO: Maybe we should just force sch_ingress to be compiled in
1845 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
1846 * a compare and 2 stores extra right now if we dont have it on
1847 * but have CONFIG_NET_CLS_ACT
4ec93edb 1848 * NOTE: This doesnt stop any functionality; if you dont have
1da177e4
LT
1849 * the ingress scheduler, you just cant add policies on ingress.
1850 *
1851 */
4ec93edb 1852static int ing_filter(struct sk_buff *skb)
1da177e4
LT
1853{
1854 struct Qdisc *q;
1855 struct net_device *dev = skb->dev;
1856 int result = TC_ACT_OK;
4ec93edb 1857
1da177e4
LT
1858 if (dev->qdisc_ingress) {
1859 __u32 ttl = (__u32) G_TC_RTTL(skb->tc_verd);
1860 if (MAX_RED_LOOP < ttl++) {
c01003c2
PM
1861 printk(KERN_WARNING "Redir loop detected Dropping packet (%d->%d)\n",
1862 skb->iif, skb->dev->ifindex);
1da177e4
LT
1863 return TC_ACT_SHOT;
1864 }
1865
1866 skb->tc_verd = SET_TC_RTTL(skb->tc_verd,ttl);
1867
1868 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_INGRESS);
86e65da9 1869
fd44de7c 1870 spin_lock(&dev->ingress_lock);
1da177e4
LT
1871 if ((q = dev->qdisc_ingress) != NULL)
1872 result = q->enqueue(skb, q);
fd44de7c 1873 spin_unlock(&dev->ingress_lock);
1da177e4
LT
1874
1875 }
1876
1877 return result;
1878}
1879#endif
1880
1881int netif_receive_skb(struct sk_buff *skb)
1882{
1883 struct packet_type *ptype, *pt_prev;
f2ccd8fa 1884 struct net_device *orig_dev;
1da177e4 1885 int ret = NET_RX_DROP;
252e3346 1886 __be16 type;
1da177e4
LT
1887
1888 /* if we've gotten here through NAPI, check netpoll */
1889 if (skb->dev->poll && netpoll_rx(skb))
1890 return NET_RX_DROP;
1891
b7aa0bf7 1892 if (!skb->tstamp.tv64)
a61bbcf2 1893 net_timestamp(skb);
1da177e4 1894
c01003c2
PM
1895 if (!skb->iif)
1896 skb->iif = skb->dev->ifindex;
86e65da9 1897
f2ccd8fa 1898 orig_dev = skb_bond(skb);
1da177e4 1899
8f903c70
JV
1900 if (!orig_dev)
1901 return NET_RX_DROP;
1902
1da177e4
LT
1903 __get_cpu_var(netdev_rx_stat).total++;
1904
c1d2bbe1 1905 skb_reset_network_header(skb);
badff6d0 1906 skb_reset_transport_header(skb);
b0e380b1 1907 skb->mac_len = skb->network_header - skb->mac_header;
1da177e4
LT
1908
1909 pt_prev = NULL;
1910
1911 rcu_read_lock();
1912
1913#ifdef CONFIG_NET_CLS_ACT
1914 if (skb->tc_verd & TC_NCLS) {
1915 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
1916 goto ncls;
1917 }
1918#endif
1919
1920 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1921 if (!ptype->dev || ptype->dev == skb->dev) {
4ec93edb 1922 if (pt_prev)
f2ccd8fa 1923 ret = deliver_skb(skb, pt_prev, orig_dev);
1da177e4
LT
1924 pt_prev = ptype;
1925 }
1926 }
1927
1928#ifdef CONFIG_NET_CLS_ACT
1929 if (pt_prev) {
f2ccd8fa 1930 ret = deliver_skb(skb, pt_prev, orig_dev);
1da177e4
LT
1931 pt_prev = NULL; /* noone else should process this after*/
1932 } else {
1933 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
1934 }
1935
1936 ret = ing_filter(skb);
1937
1938 if (ret == TC_ACT_SHOT || (ret == TC_ACT_STOLEN)) {
1939 kfree_skb(skb);
1940 goto out;
1941 }
1942
1943 skb->tc_verd = 0;
1944ncls:
1945#endif
1946
6229e362 1947 skb = handle_bridge(skb, &pt_prev, &ret, orig_dev);
b863ceb7
PM
1948 if (!skb)
1949 goto out;
1950 skb = handle_macvlan(skb, &pt_prev, &ret, orig_dev);
6229e362 1951 if (!skb)
1da177e4
LT
1952 goto out;
1953
1954 type = skb->protocol;
1955 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type)&15], list) {
1956 if (ptype->type == type &&
1957 (!ptype->dev || ptype->dev == skb->dev)) {
4ec93edb 1958 if (pt_prev)
f2ccd8fa 1959 ret = deliver_skb(skb, pt_prev, orig_dev);
1da177e4
LT
1960 pt_prev = ptype;
1961 }
1962 }
1963
1964 if (pt_prev) {
f2ccd8fa 1965 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1da177e4
LT
1966 } else {
1967 kfree_skb(skb);
1968 /* Jamal, now you will not able to escape explaining
1969 * me how you were going to use this. :-)
1970 */
1971 ret = NET_RX_DROP;
1972 }
1973
1974out:
1975 rcu_read_unlock();
1976 return ret;
1977}
1978
1979static int process_backlog(struct net_device *backlog_dev, int *budget)
1980{
1981 int work = 0;
1982 int quota = min(backlog_dev->quota, *budget);
1983 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1984 unsigned long start_time = jiffies;
1985
e3876605 1986 backlog_dev->weight = weight_p;
1da177e4
LT
1987 for (;;) {
1988 struct sk_buff *skb;
1989 struct net_device *dev;
1990
1991 local_irq_disable();
1992 skb = __skb_dequeue(&queue->input_pkt_queue);
1993 if (!skb)
1994 goto job_done;
1995 local_irq_enable();
1996
1997 dev = skb->dev;
1998
1999 netif_receive_skb(skb);
2000
2001 dev_put(dev);
2002
2003 work++;
2004
2005 if (work >= quota || jiffies - start_time > 1)
2006 break;
2007
2008 }
2009
2010 backlog_dev->quota -= work;
2011 *budget -= work;
2012 return -1;
2013
2014job_done:
2015 backlog_dev->quota -= work;
2016 *budget -= work;
2017
2018 list_del(&backlog_dev->poll_list);
2019 smp_mb__before_clear_bit();
2020 netif_poll_enable(backlog_dev);
2021
1da177e4
LT
2022 local_irq_enable();
2023 return 0;
2024}
2025
2026static void net_rx_action(struct softirq_action *h)
2027{
2028 struct softnet_data *queue = &__get_cpu_var(softnet_data);
2029 unsigned long start_time = jiffies;
51b0bded 2030 int budget = netdev_budget;
53fb95d3
MM
2031 void *have;
2032
1da177e4
LT
2033 local_irq_disable();
2034
2035 while (!list_empty(&queue->poll_list)) {
2036 struct net_device *dev;
2037
2038 if (budget <= 0 || jiffies - start_time > 1)
2039 goto softnet_break;
2040
2041 local_irq_enable();
2042
2043 dev = list_entry(queue->poll_list.next,
2044 struct net_device, poll_list);
53fb95d3 2045 have = netpoll_poll_lock(dev);
1da177e4
LT
2046
2047 if (dev->quota <= 0 || dev->poll(dev, &budget)) {
53fb95d3 2048 netpoll_poll_unlock(have);
1da177e4 2049 local_irq_disable();
8aca8a27 2050 list_move_tail(&dev->poll_list, &queue->poll_list);
1da177e4
LT
2051 if (dev->quota < 0)
2052 dev->quota += dev->weight;
2053 else
2054 dev->quota = dev->weight;
2055 } else {
53fb95d3 2056 netpoll_poll_unlock(have);
1da177e4
LT
2057 dev_put(dev);
2058 local_irq_disable();
2059 }
2060 }
2061out:
515e06c4 2062 local_irq_enable();
db217334
CL
2063#ifdef CONFIG_NET_DMA
2064 /*
2065 * There may not be any more sk_buffs coming right now, so push
2066 * any pending DMA copies to hardware
2067 */
d379b01e
DW
2068 if (!cpus_empty(net_dma.channel_mask)) {
2069 int chan_idx;
2070 for_each_cpu_mask(chan_idx, net_dma.channel_mask) {
2071 struct dma_chan *chan = net_dma.channels[chan_idx];
2072 if (chan)
2073 dma_async_memcpy_issue_pending(chan);
2074 }
db217334
CL
2075 }
2076#endif
1da177e4
LT
2077 return;
2078
2079softnet_break:
2080 __get_cpu_var(netdev_rx_stat).time_squeeze++;
2081 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2082 goto out;
2083}
2084
2085static gifconf_func_t * gifconf_list [NPROTO];
2086
2087/**
2088 * register_gifconf - register a SIOCGIF handler
2089 * @family: Address family
2090 * @gifconf: Function handler
2091 *
2092 * Register protocol dependent address dumping routines. The handler
2093 * that is passed must not be freed or reused until it has been replaced
2094 * by another handler.
2095 */
2096int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
2097{
2098 if (family >= NPROTO)
2099 return -EINVAL;
2100 gifconf_list[family] = gifconf;
2101 return 0;
2102}
2103
2104
2105/*
2106 * Map an interface index to its name (SIOCGIFNAME)
2107 */
2108
2109/*
2110 * We need this ioctl for efficient implementation of the
2111 * if_indextoname() function required by the IPv6 API. Without
2112 * it, we would have to search all the interfaces to find a
2113 * match. --pb
2114 */
2115
2116static int dev_ifname(struct ifreq __user *arg)
2117{
2118 struct net_device *dev;
2119 struct ifreq ifr;
2120
2121 /*
2122 * Fetch the caller's info block.
2123 */
2124
2125 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2126 return -EFAULT;
2127
2128 read_lock(&dev_base_lock);
2129 dev = __dev_get_by_index(ifr.ifr_ifindex);
2130 if (!dev) {
2131 read_unlock(&dev_base_lock);
2132 return -ENODEV;
2133 }
2134
2135 strcpy(ifr.ifr_name, dev->name);
2136 read_unlock(&dev_base_lock);
2137
2138 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2139 return -EFAULT;
2140 return 0;
2141}
2142
2143/*
2144 * Perform a SIOCGIFCONF call. This structure will change
2145 * size eventually, and there is nothing I can do about it.
2146 * Thus we will need a 'compatibility mode'.
2147 */
2148
2149static int dev_ifconf(char __user *arg)
2150{
2151 struct ifconf ifc;
2152 struct net_device *dev;
2153 char __user *pos;
2154 int len;
2155 int total;
2156 int i;
2157
2158 /*
2159 * Fetch the caller's info block.
2160 */
2161
2162 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
2163 return -EFAULT;
2164
2165 pos = ifc.ifc_buf;
2166 len = ifc.ifc_len;
2167
2168 /*
2169 * Loop over the interfaces, and write an info block for each.
2170 */
2171
2172 total = 0;
7562f876 2173 for_each_netdev(dev) {
1da177e4
LT
2174 for (i = 0; i < NPROTO; i++) {
2175 if (gifconf_list[i]) {
2176 int done;
2177 if (!pos)
2178 done = gifconf_list[i](dev, NULL, 0);
2179 else
2180 done = gifconf_list[i](dev, pos + total,
2181 len - total);
2182 if (done < 0)
2183 return -EFAULT;
2184 total += done;
2185 }
2186 }
4ec93edb 2187 }
1da177e4
LT
2188
2189 /*
2190 * All done. Write the updated control block back to the caller.
2191 */
2192 ifc.ifc_len = total;
2193
2194 /*
2195 * Both BSD and Solaris return 0 here, so we do too.
2196 */
2197 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
2198}
2199
2200#ifdef CONFIG_PROC_FS
2201/*
2202 * This is invoked by the /proc filesystem handler to display a device
2203 * in detail.
2204 */
7562f876 2205void *dev_seq_start(struct seq_file *seq, loff_t *pos)
1da177e4 2206{
7562f876 2207 loff_t off;
1da177e4 2208 struct net_device *dev;
1da177e4 2209
7562f876
PE
2210 read_lock(&dev_base_lock);
2211 if (!*pos)
2212 return SEQ_START_TOKEN;
1da177e4 2213
7562f876
PE
2214 off = 1;
2215 for_each_netdev(dev)
2216 if (off++ == *pos)
2217 return dev;
1da177e4 2218
7562f876 2219 return NULL;
1da177e4
LT
2220}
2221
2222void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2223{
2224 ++*pos;
7562f876
PE
2225 return v == SEQ_START_TOKEN ?
2226 first_net_device() : next_net_device((struct net_device *)v);
1da177e4
LT
2227}
2228
2229void dev_seq_stop(struct seq_file *seq, void *v)
2230{
2231 read_unlock(&dev_base_lock);
2232}
2233
2234static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
2235{
c45d286e 2236 struct net_device_stats *stats = dev->get_stats(dev);
1da177e4 2237
5a1b5898
RR
2238 seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
2239 "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
2240 dev->name, stats->rx_bytes, stats->rx_packets,
2241 stats->rx_errors,
2242 stats->rx_dropped + stats->rx_missed_errors,
2243 stats->rx_fifo_errors,
2244 stats->rx_length_errors + stats->rx_over_errors +
2245 stats->rx_crc_errors + stats->rx_frame_errors,
2246 stats->rx_compressed, stats->multicast,
2247 stats->tx_bytes, stats->tx_packets,
2248 stats->tx_errors, stats->tx_dropped,
2249 stats->tx_fifo_errors, stats->collisions,
2250 stats->tx_carrier_errors +
2251 stats->tx_aborted_errors +
2252 stats->tx_window_errors +
2253 stats->tx_heartbeat_errors,
2254 stats->tx_compressed);
1da177e4
LT
2255}
2256
2257/*
2258 * Called from the PROCfs module. This now uses the new arbitrary sized
2259 * /proc/net interface to create /proc/net/dev
2260 */
2261static int dev_seq_show(struct seq_file *seq, void *v)
2262{
2263 if (v == SEQ_START_TOKEN)
2264 seq_puts(seq, "Inter-| Receive "
2265 " | Transmit\n"
2266 " face |bytes packets errs drop fifo frame "
2267 "compressed multicast|bytes packets errs "
2268 "drop fifo colls carrier compressed\n");
2269 else
2270 dev_seq_printf_stats(seq, v);
2271 return 0;
2272}
2273
2274static struct netif_rx_stats *softnet_get_online(loff_t *pos)
2275{
2276 struct netif_rx_stats *rc = NULL;
2277
2278 while (*pos < NR_CPUS)
4ec93edb 2279 if (cpu_online(*pos)) {
1da177e4
LT
2280 rc = &per_cpu(netdev_rx_stat, *pos);
2281 break;
2282 } else
2283 ++*pos;
2284 return rc;
2285}
2286
2287static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
2288{
2289 return softnet_get_online(pos);
2290}
2291
2292static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2293{
2294 ++*pos;
2295 return softnet_get_online(pos);
2296}
2297
2298static void softnet_seq_stop(struct seq_file *seq, void *v)
2299{
2300}
2301
2302static int softnet_seq_show(struct seq_file *seq, void *v)
2303{
2304 struct netif_rx_stats *s = v;
2305
2306 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
31aa02c5 2307 s->total, s->dropped, s->time_squeeze, 0,
c1ebcdb8
SH
2308 0, 0, 0, 0, /* was fastroute */
2309 s->cpu_collision );
1da177e4
LT
2310 return 0;
2311}
2312
f690808e 2313static const struct seq_operations dev_seq_ops = {
1da177e4
LT
2314 .start = dev_seq_start,
2315 .next = dev_seq_next,
2316 .stop = dev_seq_stop,
2317 .show = dev_seq_show,
2318};
2319
2320static int dev_seq_open(struct inode *inode, struct file *file)
2321{
2322 return seq_open(file, &dev_seq_ops);
2323}
2324
9a32144e 2325static const struct file_operations dev_seq_fops = {
1da177e4
LT
2326 .owner = THIS_MODULE,
2327 .open = dev_seq_open,
2328 .read = seq_read,
2329 .llseek = seq_lseek,
2330 .release = seq_release,
2331};
2332
f690808e 2333static const struct seq_operations softnet_seq_ops = {
1da177e4
LT
2334 .start = softnet_seq_start,
2335 .next = softnet_seq_next,
2336 .stop = softnet_seq_stop,
2337 .show = softnet_seq_show,
2338};
2339
2340static int softnet_seq_open(struct inode *inode, struct file *file)
2341{
2342 return seq_open(file, &softnet_seq_ops);
2343}
2344
9a32144e 2345static const struct file_operations softnet_seq_fops = {
1da177e4
LT
2346 .owner = THIS_MODULE,
2347 .open = softnet_seq_open,
2348 .read = seq_read,
2349 .llseek = seq_lseek,
2350 .release = seq_release,
2351};
2352
0e1256ff
SH
2353static void *ptype_get_idx(loff_t pos)
2354{
2355 struct packet_type *pt = NULL;
2356 loff_t i = 0;
2357 int t;
2358
2359 list_for_each_entry_rcu(pt, &ptype_all, list) {
2360 if (i == pos)
2361 return pt;
2362 ++i;
2363 }
2364
2365 for (t = 0; t < 16; t++) {
2366 list_for_each_entry_rcu(pt, &ptype_base[t], list) {
2367 if (i == pos)
2368 return pt;
2369 ++i;
2370 }
2371 }
2372 return NULL;
2373}
2374
2375static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
2376{
2377 rcu_read_lock();
2378 return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN;
2379}
2380
2381static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2382{
2383 struct packet_type *pt;
2384 struct list_head *nxt;
2385 int hash;
2386
2387 ++*pos;
2388 if (v == SEQ_START_TOKEN)
2389 return ptype_get_idx(0);
2390
2391 pt = v;
2392 nxt = pt->list.next;
2393 if (pt->type == htons(ETH_P_ALL)) {
2394 if (nxt != &ptype_all)
2395 goto found;
2396 hash = 0;
2397 nxt = ptype_base[0].next;
2398 } else
2399 hash = ntohs(pt->type) & 15;
2400
2401 while (nxt == &ptype_base[hash]) {
2402 if (++hash >= 16)
2403 return NULL;
2404 nxt = ptype_base[hash].next;
2405 }
2406found:
2407 return list_entry(nxt, struct packet_type, list);
2408}
2409
2410static void ptype_seq_stop(struct seq_file *seq, void *v)
2411{
2412 rcu_read_unlock();
2413}
2414
2415static void ptype_seq_decode(struct seq_file *seq, void *sym)
2416{
2417#ifdef CONFIG_KALLSYMS
2418 unsigned long offset = 0, symsize;
2419 const char *symname;
2420 char *modname;
2421 char namebuf[128];
2422
2423 symname = kallsyms_lookup((unsigned long)sym, &symsize, &offset,
2424 &modname, namebuf);
2425
2426 if (symname) {
2427 char *delim = ":";
2428
2429 if (!modname)
2430 modname = delim = "";
2431 seq_printf(seq, "%s%s%s%s+0x%lx", delim, modname, delim,
2432 symname, offset);
2433 return;
2434 }
2435#endif
2436
2437 seq_printf(seq, "[%p]", sym);
2438}
2439
2440static int ptype_seq_show(struct seq_file *seq, void *v)
2441{
2442 struct packet_type *pt = v;
2443
2444 if (v == SEQ_START_TOKEN)
2445 seq_puts(seq, "Type Device Function\n");
2446 else {
2447 if (pt->type == htons(ETH_P_ALL))
2448 seq_puts(seq, "ALL ");
2449 else
2450 seq_printf(seq, "%04x", ntohs(pt->type));
2451
2452 seq_printf(seq, " %-8s ",
2453 pt->dev ? pt->dev->name : "");
2454 ptype_seq_decode(seq, pt->func);
2455 seq_putc(seq, '\n');
2456 }
2457
2458 return 0;
2459}
2460
2461static const struct seq_operations ptype_seq_ops = {
2462 .start = ptype_seq_start,
2463 .next = ptype_seq_next,
2464 .stop = ptype_seq_stop,
2465 .show = ptype_seq_show,
2466};
2467
2468static int ptype_seq_open(struct inode *inode, struct file *file)
2469{
2470 return seq_open(file, &ptype_seq_ops);
2471}
2472
2473static const struct file_operations ptype_seq_fops = {
2474 .owner = THIS_MODULE,
2475 .open = ptype_seq_open,
2476 .read = seq_read,
2477 .llseek = seq_lseek,
2478 .release = seq_release,
2479};
2480
2481
1da177e4
LT
2482static int __init dev_proc_init(void)
2483{
2484 int rc = -ENOMEM;
2485
2486 if (!proc_net_fops_create("dev", S_IRUGO, &dev_seq_fops))
2487 goto out;
2488 if (!proc_net_fops_create("softnet_stat", S_IRUGO, &softnet_seq_fops))
2489 goto out_dev;
0e1256ff
SH
2490 if (!proc_net_fops_create("ptype", S_IRUGO, &ptype_seq_fops))
2491 goto out_dev2;
2492
295f4a1f 2493 if (wext_proc_init())
1da177e4
LT
2494 goto out_softnet;
2495 rc = 0;
2496out:
2497 return rc;
2498out_softnet:
0e1256ff 2499 proc_net_remove("ptype");
2396a22e
JJS
2500out_dev2:
2501 proc_net_remove("softnet_stat");
1da177e4
LT
2502out_dev:
2503 proc_net_remove("dev");
2504 goto out;
2505}
2506#else
2507#define dev_proc_init() 0
2508#endif /* CONFIG_PROC_FS */
2509
2510
2511/**
2512 * netdev_set_master - set up master/slave pair
2513 * @slave: slave device
2514 * @master: new master device
2515 *
2516 * Changes the master device of the slave. Pass %NULL to break the
2517 * bonding. The caller must hold the RTNL semaphore. On a failure
2518 * a negative errno code is returned. On success the reference counts
2519 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
2520 * function returns zero.
2521 */
2522int netdev_set_master(struct net_device *slave, struct net_device *master)
2523{
2524 struct net_device *old = slave->master;
2525
2526 ASSERT_RTNL();
2527
2528 if (master) {
2529 if (old)
2530 return -EBUSY;
2531 dev_hold(master);
2532 }
2533
2534 slave->master = master;
4ec93edb 2535
1da177e4
LT
2536 synchronize_net();
2537
2538 if (old)
2539 dev_put(old);
2540
2541 if (master)
2542 slave->flags |= IFF_SLAVE;
2543 else
2544 slave->flags &= ~IFF_SLAVE;
2545
2546 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
2547 return 0;
2548}
2549
4417da66 2550static void __dev_set_promiscuity(struct net_device *dev, int inc)
1da177e4
LT
2551{
2552 unsigned short old_flags = dev->flags;
2553
24023451
PM
2554 ASSERT_RTNL();
2555
1da177e4
LT
2556 if ((dev->promiscuity += inc) == 0)
2557 dev->flags &= ~IFF_PROMISC;
52609c0b
DC
2558 else
2559 dev->flags |= IFF_PROMISC;
2560 if (dev->flags != old_flags) {
1da177e4
LT
2561 printk(KERN_INFO "device %s %s promiscuous mode\n",
2562 dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
4ec93edb 2563 "left");
5bdb9886
SG
2564 audit_log(current->audit_context, GFP_ATOMIC,
2565 AUDIT_ANOM_PROMISCUOUS,
2566 "dev=%s prom=%d old_prom=%d auid=%u",
2567 dev->name, (dev->flags & IFF_PROMISC),
2568 (old_flags & IFF_PROMISC),
4ec93edb 2569 audit_get_loginuid(current->audit_context));
24023451
PM
2570
2571 if (dev->change_rx_flags)
2572 dev->change_rx_flags(dev, IFF_PROMISC);
1da177e4
LT
2573 }
2574}
2575
4417da66
PM
2576/**
2577 * dev_set_promiscuity - update promiscuity count on a device
2578 * @dev: device
2579 * @inc: modifier
2580 *
2581 * Add or remove promiscuity from a device. While the count in the device
2582 * remains above zero the interface remains promiscuous. Once it hits zero
2583 * the device reverts back to normal filtering operation. A negative inc
2584 * value is used to drop promiscuity on the device.
2585 */
2586void dev_set_promiscuity(struct net_device *dev, int inc)
2587{
2588 unsigned short old_flags = dev->flags;
2589
2590 __dev_set_promiscuity(dev, inc);
2591 if (dev->flags != old_flags)
2592 dev_set_rx_mode(dev);
2593}
2594
1da177e4
LT
2595/**
2596 * dev_set_allmulti - update allmulti count on a device
2597 * @dev: device
2598 * @inc: modifier
2599 *
2600 * Add or remove reception of all multicast frames to a device. While the
2601 * count in the device remains above zero the interface remains listening
2602 * to all interfaces. Once it hits zero the device reverts back to normal
2603 * filtering operation. A negative @inc value is used to drop the counter
2604 * when releasing a resource needing all multicasts.
2605 */
2606
2607void dev_set_allmulti(struct net_device *dev, int inc)
2608{
2609 unsigned short old_flags = dev->flags;
2610
24023451
PM
2611 ASSERT_RTNL();
2612
1da177e4
LT
2613 dev->flags |= IFF_ALLMULTI;
2614 if ((dev->allmulti += inc) == 0)
2615 dev->flags &= ~IFF_ALLMULTI;
24023451
PM
2616 if (dev->flags ^ old_flags) {
2617 if (dev->change_rx_flags)
2618 dev->change_rx_flags(dev, IFF_ALLMULTI);
4417da66 2619 dev_set_rx_mode(dev);
24023451 2620 }
4417da66
PM
2621}
2622
2623/*
2624 * Upload unicast and multicast address lists to device and
2625 * configure RX filtering. When the device doesn't support unicast
2626 * filtering it is put in promiscous mode while unicast addresses
2627 * are present.
2628 */
2629void __dev_set_rx_mode(struct net_device *dev)
2630{
2631 /* dev_open will call this function so the list will stay sane. */
2632 if (!(dev->flags&IFF_UP))
2633 return;
2634
2635 if (!netif_device_present(dev))
40b77c94 2636 return;
4417da66
PM
2637
2638 if (dev->set_rx_mode)
2639 dev->set_rx_mode(dev);
2640 else {
2641 /* Unicast addresses changes may only happen under the rtnl,
2642 * therefore calling __dev_set_promiscuity here is safe.
2643 */
2644 if (dev->uc_count > 0 && !dev->uc_promisc) {
2645 __dev_set_promiscuity(dev, 1);
2646 dev->uc_promisc = 1;
2647 } else if (dev->uc_count == 0 && dev->uc_promisc) {
2648 __dev_set_promiscuity(dev, -1);
2649 dev->uc_promisc = 0;
2650 }
2651
2652 if (dev->set_multicast_list)
2653 dev->set_multicast_list(dev);
2654 }
2655}
2656
2657void dev_set_rx_mode(struct net_device *dev)
2658{
2659 netif_tx_lock_bh(dev);
2660 __dev_set_rx_mode(dev);
2661 netif_tx_unlock_bh(dev);
1da177e4
LT
2662}
2663
61cbc2fc
PM
2664int __dev_addr_delete(struct dev_addr_list **list, int *count,
2665 void *addr, int alen, int glbl)
bf742482
PM
2666{
2667 struct dev_addr_list *da;
2668
2669 for (; (da = *list) != NULL; list = &da->next) {
2670 if (memcmp(da->da_addr, addr, da->da_addrlen) == 0 &&
2671 alen == da->da_addrlen) {
2672 if (glbl) {
2673 int old_glbl = da->da_gusers;
2674 da->da_gusers = 0;
2675 if (old_glbl == 0)
2676 break;
2677 }
2678 if (--da->da_users)
2679 return 0;
2680
2681 *list = da->next;
2682 kfree(da);
61cbc2fc 2683 (*count)--;
bf742482
PM
2684 return 0;
2685 }
2686 }
2687 return -ENOENT;
2688}
2689
61cbc2fc
PM
2690int __dev_addr_add(struct dev_addr_list **list, int *count,
2691 void *addr, int alen, int glbl)
bf742482
PM
2692{
2693 struct dev_addr_list *da;
2694
2695 for (da = *list; da != NULL; da = da->next) {
2696 if (memcmp(da->da_addr, addr, da->da_addrlen) == 0 &&
2697 da->da_addrlen == alen) {
2698 if (glbl) {
2699 int old_glbl = da->da_gusers;
2700 da->da_gusers = 1;
2701 if (old_glbl)
2702 return 0;
2703 }
2704 da->da_users++;
2705 return 0;
2706 }
2707 }
2708
2709 da = kmalloc(sizeof(*da), GFP_ATOMIC);
2710 if (da == NULL)
2711 return -ENOMEM;
2712 memcpy(da->da_addr, addr, alen);
2713 da->da_addrlen = alen;
2714 da->da_users = 1;
2715 da->da_gusers = glbl ? 1 : 0;
2716 da->next = *list;
2717 *list = da;
61cbc2fc 2718 (*count)++;
bf742482
PM
2719 return 0;
2720}
2721
4417da66
PM
2722/**
2723 * dev_unicast_delete - Release secondary unicast address.
2724 * @dev: device
0ed72ec4
RD
2725 * @addr: address to delete
2726 * @alen: length of @addr
4417da66
PM
2727 *
2728 * Release reference to a secondary unicast address and remove it
0ed72ec4 2729 * from the device if the reference count drops to zero.
4417da66
PM
2730 *
2731 * The caller must hold the rtnl_mutex.
2732 */
2733int dev_unicast_delete(struct net_device *dev, void *addr, int alen)
2734{
2735 int err;
2736
2737 ASSERT_RTNL();
2738
2739 netif_tx_lock_bh(dev);
61cbc2fc
PM
2740 err = __dev_addr_delete(&dev->uc_list, &dev->uc_count, addr, alen, 0);
2741 if (!err)
4417da66 2742 __dev_set_rx_mode(dev);
4417da66
PM
2743 netif_tx_unlock_bh(dev);
2744 return err;
2745}
2746EXPORT_SYMBOL(dev_unicast_delete);
2747
2748/**
2749 * dev_unicast_add - add a secondary unicast address
2750 * @dev: device
0ed72ec4
RD
2751 * @addr: address to delete
2752 * @alen: length of @addr
4417da66
PM
2753 *
2754 * Add a secondary unicast address to the device or increase
2755 * the reference count if it already exists.
2756 *
2757 * The caller must hold the rtnl_mutex.
2758 */
2759int dev_unicast_add(struct net_device *dev, void *addr, int alen)
2760{
2761 int err;
2762
2763 ASSERT_RTNL();
2764
2765 netif_tx_lock_bh(dev);
61cbc2fc
PM
2766 err = __dev_addr_add(&dev->uc_list, &dev->uc_count, addr, alen, 0);
2767 if (!err)
4417da66 2768 __dev_set_rx_mode(dev);
4417da66
PM
2769 netif_tx_unlock_bh(dev);
2770 return err;
2771}
2772EXPORT_SYMBOL(dev_unicast_add);
2773
12972621
DC
2774static void __dev_addr_discard(struct dev_addr_list **list)
2775{
2776 struct dev_addr_list *tmp;
2777
2778 while (*list != NULL) {
2779 tmp = *list;
2780 *list = tmp->next;
2781 if (tmp->da_users > tmp->da_gusers)
2782 printk("__dev_addr_discard: address leakage! "
2783 "da_users=%d\n", tmp->da_users);
2784 kfree(tmp);
2785 }
2786}
2787
26cc2522 2788static void dev_addr_discard(struct net_device *dev)
4417da66
PM
2789{
2790 netif_tx_lock_bh(dev);
26cc2522 2791
4417da66
PM
2792 __dev_addr_discard(&dev->uc_list);
2793 dev->uc_count = 0;
4417da66 2794
456ad75c
DC
2795 __dev_addr_discard(&dev->mc_list);
2796 dev->mc_count = 0;
26cc2522 2797
456ad75c
DC
2798 netif_tx_unlock_bh(dev);
2799}
2800
1da177e4
LT
2801unsigned dev_get_flags(const struct net_device *dev)
2802{
2803 unsigned flags;
2804
2805 flags = (dev->flags & ~(IFF_PROMISC |
2806 IFF_ALLMULTI |
b00055aa
SR
2807 IFF_RUNNING |
2808 IFF_LOWER_UP |
2809 IFF_DORMANT)) |
1da177e4
LT
2810 (dev->gflags & (IFF_PROMISC |
2811 IFF_ALLMULTI));
2812
b00055aa
SR
2813 if (netif_running(dev)) {
2814 if (netif_oper_up(dev))
2815 flags |= IFF_RUNNING;
2816 if (netif_carrier_ok(dev))
2817 flags |= IFF_LOWER_UP;
2818 if (netif_dormant(dev))
2819 flags |= IFF_DORMANT;
2820 }
1da177e4
LT
2821
2822 return flags;
2823}
2824
2825int dev_change_flags(struct net_device *dev, unsigned flags)
2826{
7c355f53 2827 int ret, changes;
1da177e4
LT
2828 int old_flags = dev->flags;
2829
24023451
PM
2830 ASSERT_RTNL();
2831
1da177e4
LT
2832 /*
2833 * Set the flags on our device.
2834 */
2835
2836 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
2837 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
2838 IFF_AUTOMEDIA)) |
2839 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
2840 IFF_ALLMULTI));
2841
2842 /*
2843 * Load in the correct multicast list now the flags have changed.
2844 */
2845
24023451
PM
2846 if (dev->change_rx_flags && (dev->flags ^ flags) & IFF_MULTICAST)
2847 dev->change_rx_flags(dev, IFF_MULTICAST);
2848
4417da66 2849 dev_set_rx_mode(dev);
1da177e4
LT
2850
2851 /*
2852 * Have we downed the interface. We handle IFF_UP ourselves
2853 * according to user attempts to set it, rather than blindly
2854 * setting it.
2855 */
2856
2857 ret = 0;
2858 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
2859 ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
2860
2861 if (!ret)
4417da66 2862 dev_set_rx_mode(dev);
1da177e4
LT
2863 }
2864
2865 if (dev->flags & IFF_UP &&
2866 ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
2867 IFF_VOLATILE)))
f07d5b94 2868 raw_notifier_call_chain(&netdev_chain,
e041c683 2869 NETDEV_CHANGE, dev);
1da177e4
LT
2870
2871 if ((flags ^ dev->gflags) & IFF_PROMISC) {
2872 int inc = (flags & IFF_PROMISC) ? +1 : -1;
2873 dev->gflags ^= IFF_PROMISC;
2874 dev_set_promiscuity(dev, inc);
2875 }
2876
2877 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
2878 is important. Some (broken) drivers set IFF_PROMISC, when
2879 IFF_ALLMULTI is requested not asking us and not reporting.
2880 */
2881 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
2882 int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
2883 dev->gflags ^= IFF_ALLMULTI;
2884 dev_set_allmulti(dev, inc);
2885 }
2886
7c355f53
TG
2887 /* Exclude state transition flags, already notified */
2888 changes = (old_flags ^ dev->flags) & ~(IFF_UP | IFF_RUNNING);
2889 if (changes)
2890 rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
1da177e4
LT
2891
2892 return ret;
2893}
2894
2895int dev_set_mtu(struct net_device *dev, int new_mtu)
2896{
2897 int err;
2898
2899 if (new_mtu == dev->mtu)
2900 return 0;
2901
2902 /* MTU must be positive. */
2903 if (new_mtu < 0)
2904 return -EINVAL;
2905
2906 if (!netif_device_present(dev))
2907 return -ENODEV;
2908
2909 err = 0;
2910 if (dev->change_mtu)
2911 err = dev->change_mtu(dev, new_mtu);
2912 else
2913 dev->mtu = new_mtu;
2914 if (!err && dev->flags & IFF_UP)
f07d5b94 2915 raw_notifier_call_chain(&netdev_chain,
e041c683 2916 NETDEV_CHANGEMTU, dev);
1da177e4
LT
2917 return err;
2918}
2919
2920int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
2921{
2922 int err;
2923
2924 if (!dev->set_mac_address)
2925 return -EOPNOTSUPP;
2926 if (sa->sa_family != dev->type)
2927 return -EINVAL;
2928 if (!netif_device_present(dev))
2929 return -ENODEV;
2930 err = dev->set_mac_address(dev, sa);
2931 if (!err)
f07d5b94 2932 raw_notifier_call_chain(&netdev_chain,
e041c683 2933 NETDEV_CHANGEADDR, dev);
1da177e4
LT
2934 return err;
2935}
2936
2937/*
2938 * Perform the SIOCxIFxxx calls.
2939 */
2940static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
2941{
2942 int err;
2943 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
2944
2945 if (!dev)
2946 return -ENODEV;
2947
2948 switch (cmd) {
2949 case SIOCGIFFLAGS: /* Get interface flags */
2950 ifr->ifr_flags = dev_get_flags(dev);
2951 return 0;
2952
2953 case SIOCSIFFLAGS: /* Set interface flags */
2954 return dev_change_flags(dev, ifr->ifr_flags);
2955
2956 case SIOCGIFMETRIC: /* Get the metric on the interface
2957 (currently unused) */
2958 ifr->ifr_metric = 0;
2959 return 0;
2960
2961 case SIOCSIFMETRIC: /* Set the metric on the interface
2962 (currently unused) */
2963 return -EOPNOTSUPP;
2964
2965 case SIOCGIFMTU: /* Get the MTU of a device */
2966 ifr->ifr_mtu = dev->mtu;
2967 return 0;
2968
2969 case SIOCSIFMTU: /* Set the MTU of a device */
2970 return dev_set_mtu(dev, ifr->ifr_mtu);
2971
2972 case SIOCGIFHWADDR:
2973 if (!dev->addr_len)
2974 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
2975 else
2976 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
2977 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2978 ifr->ifr_hwaddr.sa_family = dev->type;
2979 return 0;
2980
2981 case SIOCSIFHWADDR:
2982 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
2983
2984 case SIOCSIFHWBROADCAST:
2985 if (ifr->ifr_hwaddr.sa_family != dev->type)
2986 return -EINVAL;
2987 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
2988 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
f07d5b94 2989 raw_notifier_call_chain(&netdev_chain,
1da177e4
LT
2990 NETDEV_CHANGEADDR, dev);
2991 return 0;
2992
2993 case SIOCGIFMAP:
2994 ifr->ifr_map.mem_start = dev->mem_start;
2995 ifr->ifr_map.mem_end = dev->mem_end;
2996 ifr->ifr_map.base_addr = dev->base_addr;
2997 ifr->ifr_map.irq = dev->irq;
2998 ifr->ifr_map.dma = dev->dma;
2999 ifr->ifr_map.port = dev->if_port;
3000 return 0;
3001
3002 case SIOCSIFMAP:
3003 if (dev->set_config) {
3004 if (!netif_device_present(dev))
3005 return -ENODEV;
3006 return dev->set_config(dev, &ifr->ifr_map);
3007 }
3008 return -EOPNOTSUPP;
3009
3010 case SIOCADDMULTI:
3011 if (!dev->set_multicast_list ||
3012 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
3013 return -EINVAL;
3014 if (!netif_device_present(dev))
3015 return -ENODEV;
3016 return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
3017 dev->addr_len, 1);
3018
3019 case SIOCDELMULTI:
3020 if (!dev->set_multicast_list ||
3021 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
3022 return -EINVAL;
3023 if (!netif_device_present(dev))
3024 return -ENODEV;
3025 return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
3026 dev->addr_len, 1);
3027
3028 case SIOCGIFINDEX:
3029 ifr->ifr_ifindex = dev->ifindex;
3030 return 0;
3031
3032 case SIOCGIFTXQLEN:
3033 ifr->ifr_qlen = dev->tx_queue_len;
3034 return 0;
3035
3036 case SIOCSIFTXQLEN:
3037 if (ifr->ifr_qlen < 0)
3038 return -EINVAL;
3039 dev->tx_queue_len = ifr->ifr_qlen;
3040 return 0;
3041
3042 case SIOCSIFNAME:
3043 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
3044 return dev_change_name(dev, ifr->ifr_newname);
3045
3046 /*
3047 * Unknown or private ioctl
3048 */
3049
3050 default:
3051 if ((cmd >= SIOCDEVPRIVATE &&
3052 cmd <= SIOCDEVPRIVATE + 15) ||
3053 cmd == SIOCBONDENSLAVE ||
3054 cmd == SIOCBONDRELEASE ||
3055 cmd == SIOCBONDSETHWADDR ||
3056 cmd == SIOCBONDSLAVEINFOQUERY ||
3057 cmd == SIOCBONDINFOQUERY ||
3058 cmd == SIOCBONDCHANGEACTIVE ||
3059 cmd == SIOCGMIIPHY ||
3060 cmd == SIOCGMIIREG ||
3061 cmd == SIOCSMIIREG ||
3062 cmd == SIOCBRADDIF ||
3063 cmd == SIOCBRDELIF ||
3064 cmd == SIOCWANDEV) {
3065 err = -EOPNOTSUPP;
3066 if (dev->do_ioctl) {
3067 if (netif_device_present(dev))
3068 err = dev->do_ioctl(dev, ifr,
3069 cmd);
3070 else
3071 err = -ENODEV;
3072 }
3073 } else
3074 err = -EINVAL;
3075
3076 }
3077 return err;
3078}
3079
3080/*
3081 * This function handles all "interface"-type I/O control requests. The actual
3082 * 'doing' part of this is dev_ifsioc above.
3083 */
3084
3085/**
3086 * dev_ioctl - network device ioctl
3087 * @cmd: command to issue
3088 * @arg: pointer to a struct ifreq in user space
3089 *
3090 * Issue ioctl functions to devices. This is normally called by the
3091 * user space syscall interfaces but can sometimes be useful for
3092 * other purposes. The return value is the return from the syscall if
3093 * positive or a negative errno code on error.
3094 */
3095
3096int dev_ioctl(unsigned int cmd, void __user *arg)
3097{
3098 struct ifreq ifr;
3099 int ret;
3100 char *colon;
3101
3102 /* One special case: SIOCGIFCONF takes ifconf argument
3103 and requires shared lock, because it sleeps writing
3104 to user space.
3105 */
3106
3107 if (cmd == SIOCGIFCONF) {
6756ae4b 3108 rtnl_lock();
1da177e4 3109 ret = dev_ifconf((char __user *) arg);
6756ae4b 3110 rtnl_unlock();
1da177e4
LT
3111 return ret;
3112 }
3113 if (cmd == SIOCGIFNAME)
3114 return dev_ifname((struct ifreq __user *)arg);
3115
3116 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
3117 return -EFAULT;
3118
3119 ifr.ifr_name[IFNAMSIZ-1] = 0;
3120
3121 colon = strchr(ifr.ifr_name, ':');
3122 if (colon)
3123 *colon = 0;
3124
3125 /*
3126 * See which interface the caller is talking about.
3127 */
3128
3129 switch (cmd) {
3130 /*
3131 * These ioctl calls:
3132 * - can be done by all.
3133 * - atomic and do not require locking.
3134 * - return a value
3135 */
3136 case SIOCGIFFLAGS:
3137 case SIOCGIFMETRIC:
3138 case SIOCGIFMTU:
3139 case SIOCGIFHWADDR:
3140 case SIOCGIFSLAVE:
3141 case SIOCGIFMAP:
3142 case SIOCGIFINDEX:
3143 case SIOCGIFTXQLEN:
3144 dev_load(ifr.ifr_name);
3145 read_lock(&dev_base_lock);
3146 ret = dev_ifsioc(&ifr, cmd);
3147 read_unlock(&dev_base_lock);
3148 if (!ret) {
3149 if (colon)
3150 *colon = ':';
3151 if (copy_to_user(arg, &ifr,
3152 sizeof(struct ifreq)))
3153 ret = -EFAULT;
3154 }
3155 return ret;
3156
3157 case SIOCETHTOOL:
3158 dev_load(ifr.ifr_name);
3159 rtnl_lock();
3160 ret = dev_ethtool(&ifr);
3161 rtnl_unlock();
3162 if (!ret) {
3163 if (colon)
3164 *colon = ':';
3165 if (copy_to_user(arg, &ifr,
3166 sizeof(struct ifreq)))
3167 ret = -EFAULT;
3168 }
3169 return ret;
3170
3171 /*
3172 * These ioctl calls:
3173 * - require superuser power.
3174 * - require strict serialization.
3175 * - return a value
3176 */
3177 case SIOCGMIIPHY:
3178 case SIOCGMIIREG:
3179 case SIOCSIFNAME:
3180 if (!capable(CAP_NET_ADMIN))
3181 return -EPERM;
3182 dev_load(ifr.ifr_name);
3183 rtnl_lock();
3184 ret = dev_ifsioc(&ifr, cmd);
3185 rtnl_unlock();
3186 if (!ret) {
3187 if (colon)
3188 *colon = ':';
3189 if (copy_to_user(arg, &ifr,
3190 sizeof(struct ifreq)))
3191 ret = -EFAULT;
3192 }
3193 return ret;
3194
3195 /*
3196 * These ioctl calls:
3197 * - require superuser power.
3198 * - require strict serialization.
3199 * - do not return a value
3200 */
3201 case SIOCSIFFLAGS:
3202 case SIOCSIFMETRIC:
3203 case SIOCSIFMTU:
3204 case SIOCSIFMAP:
3205 case SIOCSIFHWADDR:
3206 case SIOCSIFSLAVE:
3207 case SIOCADDMULTI:
3208 case SIOCDELMULTI:
3209 case SIOCSIFHWBROADCAST:
3210 case SIOCSIFTXQLEN:
3211 case SIOCSMIIREG:
3212 case SIOCBONDENSLAVE:
3213 case SIOCBONDRELEASE:
3214 case SIOCBONDSETHWADDR:
1da177e4
LT
3215 case SIOCBONDCHANGEACTIVE:
3216 case SIOCBRADDIF:
3217 case SIOCBRDELIF:
3218 if (!capable(CAP_NET_ADMIN))
3219 return -EPERM;
cabcac0b
TG
3220 /* fall through */
3221 case SIOCBONDSLAVEINFOQUERY:
3222 case SIOCBONDINFOQUERY:
1da177e4
LT
3223 dev_load(ifr.ifr_name);
3224 rtnl_lock();
3225 ret = dev_ifsioc(&ifr, cmd);
3226 rtnl_unlock();
3227 return ret;
3228
3229 case SIOCGIFMEM:
3230 /* Get the per device memory space. We can add this but
3231 * currently do not support it */
3232 case SIOCSIFMEM:
3233 /* Set the per device memory buffer space.
3234 * Not applicable in our case */
3235 case SIOCSIFLINK:
3236 return -EINVAL;
3237
3238 /*
3239 * Unknown or private ioctl.
3240 */
3241 default:
3242 if (cmd == SIOCWANDEV ||
3243 (cmd >= SIOCDEVPRIVATE &&
3244 cmd <= SIOCDEVPRIVATE + 15)) {
3245 dev_load(ifr.ifr_name);
3246 rtnl_lock();
3247 ret = dev_ifsioc(&ifr, cmd);
3248 rtnl_unlock();
3249 if (!ret && copy_to_user(arg, &ifr,
3250 sizeof(struct ifreq)))
3251 ret = -EFAULT;
3252 return ret;
3253 }
1da177e4 3254 /* Take care of Wireless Extensions */
295f4a1f
JB
3255 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
3256 return wext_handle_ioctl(&ifr, cmd, arg);
1da177e4
LT
3257 return -EINVAL;
3258 }
3259}
3260
3261
3262/**
3263 * dev_new_index - allocate an ifindex
3264 *
3265 * Returns a suitable unique value for a new device interface
3266 * number. The caller must hold the rtnl semaphore or the
3267 * dev_base_lock to be sure it remains unique.
3268 */
3269static int dev_new_index(void)
3270{
3271 static int ifindex;
3272 for (;;) {
3273 if (++ifindex <= 0)
3274 ifindex = 1;
3275 if (!__dev_get_by_index(ifindex))
3276 return ifindex;
3277 }
3278}
3279
3280static int dev_boot_phase = 1;
3281
3282/* Delayed registration/unregisteration */
3283static DEFINE_SPINLOCK(net_todo_list_lock);
3284static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list);
3285
6f05f629 3286static void net_set_todo(struct net_device *dev)
1da177e4
LT
3287{
3288 spin_lock(&net_todo_list_lock);
3289 list_add_tail(&dev->todo_list, &net_todo_list);
3290 spin_unlock(&net_todo_list_lock);
3291}
3292
3293/**
3294 * register_netdevice - register a network device
3295 * @dev: device to register
3296 *
3297 * Take a completed network device structure and add it to the kernel
3298 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3299 * chain. 0 is returned on success. A negative errno code is returned
3300 * on a failure to set up the device, or if the name is a duplicate.
3301 *
3302 * Callers must hold the rtnl semaphore. You may want
3303 * register_netdev() instead of this.
3304 *
3305 * BUGS:
3306 * The locking appears insufficient to guarantee two parallel registers
3307 * will not get the same name.
3308 */
3309
3310int register_netdevice(struct net_device *dev)
3311{
3312 struct hlist_head *head;
3313 struct hlist_node *p;
3314 int ret;
3315
3316 BUG_ON(dev_boot_phase);
3317 ASSERT_RTNL();
3318
b17a7c17
SH
3319 might_sleep();
3320
1da177e4
LT
3321 /* When net_device's are persistent, this will be fatal. */
3322 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
3323
3324 spin_lock_init(&dev->queue_lock);
932ff279 3325 spin_lock_init(&dev->_xmit_lock);
723e98b7 3326 netdev_set_lockdep_class(&dev->_xmit_lock, dev->type);
1da177e4 3327 dev->xmit_lock_owner = -1;
1da177e4 3328 spin_lock_init(&dev->ingress_lock);
1da177e4 3329
1da177e4
LT
3330 dev->iflink = -1;
3331
3332 /* Init, if this function is available */
3333 if (dev->init) {
3334 ret = dev->init(dev);
3335 if (ret) {
3336 if (ret > 0)
3337 ret = -EIO;
90833aa4 3338 goto out;
1da177e4
LT
3339 }
3340 }
4ec93edb 3341
1da177e4
LT
3342 if (!dev_valid_name(dev->name)) {
3343 ret = -EINVAL;
7ce1b0ed 3344 goto err_uninit;
1da177e4
LT
3345 }
3346
3347 dev->ifindex = dev_new_index();
3348 if (dev->iflink == -1)
3349 dev->iflink = dev->ifindex;
3350
3351 /* Check for existence of name */
3352 head = dev_name_hash(dev->name);
3353 hlist_for_each(p, head) {
3354 struct net_device *d
3355 = hlist_entry(p, struct net_device, name_hlist);
3356 if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
3357 ret = -EEXIST;
7ce1b0ed 3358 goto err_uninit;
1da177e4 3359 }
4ec93edb 3360 }
1da177e4 3361
d212f87b
SH
3362 /* Fix illegal checksum combinations */
3363 if ((dev->features & NETIF_F_HW_CSUM) &&
3364 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
3365 printk(KERN_NOTICE "%s: mixed HW and IP checksum settings.\n",
3366 dev->name);
3367 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
3368 }
3369
3370 if ((dev->features & NETIF_F_NO_CSUM) &&
3371 (dev->features & (NETIF_F_HW_CSUM|NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
3372 printk(KERN_NOTICE "%s: mixed no checksumming and other settings.\n",
3373 dev->name);
3374 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM|NETIF_F_HW_CSUM);
3375 }
3376
3377
1da177e4
LT
3378 /* Fix illegal SG+CSUM combinations. */
3379 if ((dev->features & NETIF_F_SG) &&
8648b305 3380 !(dev->features & NETIF_F_ALL_CSUM)) {
5a8da02b 3381 printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no checksum feature.\n",
1da177e4
LT
3382 dev->name);
3383 dev->features &= ~NETIF_F_SG;
3384 }
3385
3386 /* TSO requires that SG is present as well. */
3387 if ((dev->features & NETIF_F_TSO) &&
3388 !(dev->features & NETIF_F_SG)) {
5a8da02b 3389 printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no SG feature.\n",
1da177e4
LT
3390 dev->name);
3391 dev->features &= ~NETIF_F_TSO;
3392 }
e89e9cf5
AR
3393 if (dev->features & NETIF_F_UFO) {
3394 if (!(dev->features & NETIF_F_HW_CSUM)) {
3395 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
3396 "NETIF_F_HW_CSUM feature.\n",
3397 dev->name);
3398 dev->features &= ~NETIF_F_UFO;
3399 }
3400 if (!(dev->features & NETIF_F_SG)) {
3401 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
3402 "NETIF_F_SG feature.\n",
3403 dev->name);
3404 dev->features &= ~NETIF_F_UFO;
3405 }
3406 }
1da177e4
LT
3407
3408 /*
3409 * nil rebuild_header routine,
3410 * that should be never called and used as just bug trap.
3411 */
3412
3413 if (!dev->rebuild_header)
3414 dev->rebuild_header = default_rebuild_header;
3415
b17a7c17
SH
3416 ret = netdev_register_sysfs(dev);
3417 if (ret)
7ce1b0ed 3418 goto err_uninit;
b17a7c17
SH
3419 dev->reg_state = NETREG_REGISTERED;
3420
1da177e4
LT
3421 /*
3422 * Default initial state at registry is that the
3423 * device is present.
3424 */
3425
3426 set_bit(__LINK_STATE_PRESENT, &dev->state);
3427
1da177e4
LT
3428 dev_init_scheduler(dev);
3429 write_lock_bh(&dev_base_lock);
7562f876 3430 list_add_tail(&dev->dev_list, &dev_base_head);
1da177e4
LT
3431 hlist_add_head(&dev->name_hlist, head);
3432 hlist_add_head(&dev->index_hlist, dev_index_hash(dev->ifindex));
3433 dev_hold(dev);
1da177e4
LT
3434 write_unlock_bh(&dev_base_lock);
3435
3436 /* Notify protocols, that a new device appeared. */
f07d5b94 3437 raw_notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
1da177e4 3438
1da177e4
LT
3439 ret = 0;
3440
3441out:
3442 return ret;
7ce1b0ed
HX
3443
3444err_uninit:
3445 if (dev->uninit)
3446 dev->uninit(dev);
3447 goto out;
1da177e4
LT
3448}
3449
3450/**
3451 * register_netdev - register a network device
3452 * @dev: device to register
3453 *
3454 * Take a completed network device structure and add it to the kernel
3455 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3456 * chain. 0 is returned on success. A negative errno code is returned
3457 * on a failure to set up the device, or if the name is a duplicate.
3458 *
38b4da38 3459 * This is a wrapper around register_netdevice that takes the rtnl semaphore
1da177e4
LT
3460 * and expands the device name if you passed a format string to
3461 * alloc_netdev.
3462 */
3463int register_netdev(struct net_device *dev)
3464{
3465 int err;
3466
3467 rtnl_lock();
3468
3469 /*
3470 * If the name is a format string the caller wants us to do a
3471 * name allocation.
3472 */
3473 if (strchr(dev->name, '%')) {
3474 err = dev_alloc_name(dev, dev->name);
3475 if (err < 0)
3476 goto out;
3477 }
4ec93edb 3478
1da177e4
LT
3479 err = register_netdevice(dev);
3480out:
3481 rtnl_unlock();
3482 return err;
3483}
3484EXPORT_SYMBOL(register_netdev);
3485
3486/*
3487 * netdev_wait_allrefs - wait until all references are gone.
3488 *
3489 * This is called when unregistering network devices.
3490 *
3491 * Any protocol or device that holds a reference should register
3492 * for netdevice notification, and cleanup and put back the
3493 * reference if they receive an UNREGISTER event.
3494 * We can get stuck here if buggy protocols don't correctly
4ec93edb 3495 * call dev_put.
1da177e4
LT
3496 */
3497static void netdev_wait_allrefs(struct net_device *dev)
3498{
3499 unsigned long rebroadcast_time, warning_time;
3500
3501 rebroadcast_time = warning_time = jiffies;
3502 while (atomic_read(&dev->refcnt) != 0) {
3503 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
6756ae4b 3504 rtnl_lock();
1da177e4
LT
3505
3506 /* Rebroadcast unregister notification */
f07d5b94 3507 raw_notifier_call_chain(&netdev_chain,
1da177e4
LT
3508 NETDEV_UNREGISTER, dev);
3509
3510 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
3511 &dev->state)) {
3512 /* We must not have linkwatch events
3513 * pending on unregister. If this
3514 * happens, we simply run the queue
3515 * unscheduled, resulting in a noop
3516 * for this device.
3517 */
3518 linkwatch_run_queue();
3519 }
3520
6756ae4b 3521 __rtnl_unlock();
1da177e4
LT
3522
3523 rebroadcast_time = jiffies;
3524 }
3525
3526 msleep(250);
3527
3528 if (time_after(jiffies, warning_time + 10 * HZ)) {
3529 printk(KERN_EMERG "unregister_netdevice: "
3530 "waiting for %s to become free. Usage "
3531 "count = %d\n",
3532 dev->name, atomic_read(&dev->refcnt));
3533 warning_time = jiffies;
3534 }
3535 }
3536}
3537
3538/* The sequence is:
3539 *
3540 * rtnl_lock();
3541 * ...
3542 * register_netdevice(x1);
3543 * register_netdevice(x2);
3544 * ...
3545 * unregister_netdevice(y1);
3546 * unregister_netdevice(y2);
3547 * ...
3548 * rtnl_unlock();
3549 * free_netdev(y1);
3550 * free_netdev(y2);
3551 *
3552 * We are invoked by rtnl_unlock() after it drops the semaphore.
3553 * This allows us to deal with problems:
b17a7c17 3554 * 1) We can delete sysfs objects which invoke hotplug
1da177e4
LT
3555 * without deadlocking with linkwatch via keventd.
3556 * 2) Since we run with the RTNL semaphore not held, we can sleep
3557 * safely in order to wait for the netdev refcnt to drop to zero.
3558 */
4a3e2f71 3559static DEFINE_MUTEX(net_todo_run_mutex);
1da177e4
LT
3560void netdev_run_todo(void)
3561{
626ab0e6 3562 struct list_head list;
1da177e4
LT
3563
3564 /* Need to guard against multiple cpu's getting out of order. */
4a3e2f71 3565 mutex_lock(&net_todo_run_mutex);
1da177e4
LT
3566
3567 /* Not safe to do outside the semaphore. We must not return
3568 * until all unregister events invoked by the local processor
3569 * have been completed (either by this todo run, or one on
3570 * another cpu).
3571 */
3572 if (list_empty(&net_todo_list))
3573 goto out;
3574
3575 /* Snapshot list, allow later requests */
3576 spin_lock(&net_todo_list_lock);
626ab0e6 3577 list_replace_init(&net_todo_list, &list);
1da177e4 3578 spin_unlock(&net_todo_list_lock);
626ab0e6 3579
1da177e4
LT
3580 while (!list_empty(&list)) {
3581 struct net_device *dev
3582 = list_entry(list.next, struct net_device, todo_list);
3583 list_del(&dev->todo_list);
3584
b17a7c17
SH
3585 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
3586 printk(KERN_ERR "network todo '%s' but state %d\n",
3587 dev->name, dev->reg_state);
3588 dump_stack();
3589 continue;
3590 }
1da177e4 3591
b17a7c17 3592 dev->reg_state = NETREG_UNREGISTERED;
1da177e4 3593
b17a7c17 3594 netdev_wait_allrefs(dev);
1da177e4 3595
b17a7c17
SH
3596 /* paranoia */
3597 BUG_ON(atomic_read(&dev->refcnt));
3598 BUG_TRAP(!dev->ip_ptr);
3599 BUG_TRAP(!dev->ip6_ptr);
3600 BUG_TRAP(!dev->dn_ptr);
1da177e4 3601
b17a7c17
SH
3602 if (dev->destructor)
3603 dev->destructor(dev);
9093bbb2
SH
3604
3605 /* Free network device */
3606 kobject_put(&dev->dev.kobj);
1da177e4
LT
3607 }
3608
3609out:
4a3e2f71 3610 mutex_unlock(&net_todo_run_mutex);
1da177e4
LT
3611}
3612
5a1b5898 3613static struct net_device_stats *internal_stats(struct net_device *dev)
c45d286e 3614{
5a1b5898 3615 return &dev->stats;
c45d286e
RR
3616}
3617
1da177e4 3618/**
f25f4e44 3619 * alloc_netdev_mq - allocate network device
1da177e4
LT
3620 * @sizeof_priv: size of private data to allocate space for
3621 * @name: device name format string
3622 * @setup: callback to initialize device
f25f4e44 3623 * @queue_count: the number of subqueues to allocate
1da177e4
LT
3624 *
3625 * Allocates a struct net_device with private data area for driver use
f25f4e44
PWJ
3626 * and performs basic initialization. Also allocates subquue structs
3627 * for each queue on the device at the end of the netdevice.
1da177e4 3628 */
f25f4e44
PWJ
3629struct net_device *alloc_netdev_mq(int sizeof_priv, const char *name,
3630 void (*setup)(struct net_device *), unsigned int queue_count)
1da177e4
LT
3631{
3632 void *p;
3633 struct net_device *dev;
3634 int alloc_size;
3635
b6fe17d6
SH
3636 BUG_ON(strlen(name) >= sizeof(dev->name));
3637
1da177e4 3638 /* ensure 32-byte alignment of both the device and private area */
f25f4e44 3639 alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST +
31ce72a6 3640 (sizeof(struct net_device_subqueue) * (queue_count - 1))) &
f25f4e44 3641 ~NETDEV_ALIGN_CONST;
1da177e4
LT
3642 alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
3643
31380de9 3644 p = kzalloc(alloc_size, GFP_KERNEL);
1da177e4 3645 if (!p) {
b6fe17d6 3646 printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
1da177e4
LT
3647 return NULL;
3648 }
1da177e4
LT
3649
3650 dev = (struct net_device *)
3651 (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
3652 dev->padded = (char *)dev - (char *)p;
3653
f25f4e44
PWJ
3654 if (sizeof_priv) {
3655 dev->priv = ((char *)dev +
3656 ((sizeof(struct net_device) +
3657 (sizeof(struct net_device_subqueue) *
31ce72a6 3658 (queue_count - 1)) + NETDEV_ALIGN_CONST)
f25f4e44
PWJ
3659 & ~NETDEV_ALIGN_CONST));
3660 }
3661
3662 dev->egress_subqueue_count = queue_count;
1da177e4 3663
5a1b5898 3664 dev->get_stats = internal_stats;
1da177e4
LT
3665 setup(dev);
3666 strcpy(dev->name, name);
3667 return dev;
3668}
f25f4e44 3669EXPORT_SYMBOL(alloc_netdev_mq);
1da177e4
LT
3670
3671/**
3672 * free_netdev - free network device
3673 * @dev: device
3674 *
4ec93edb
YH
3675 * This function does the last stage of destroying an allocated device
3676 * interface. The reference to the device object is released.
1da177e4
LT
3677 * If this is the last reference then it will be freed.
3678 */
3679void free_netdev(struct net_device *dev)
3680{
3681#ifdef CONFIG_SYSFS
3041a069 3682 /* Compatibility with error handling in drivers */
1da177e4
LT
3683 if (dev->reg_state == NETREG_UNINITIALIZED) {
3684 kfree((char *)dev - dev->padded);
3685 return;
3686 }
3687
3688 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
3689 dev->reg_state = NETREG_RELEASED;
3690
43cb76d9
GKH
3691 /* will free via device release */
3692 put_device(&dev->dev);
1da177e4
LT
3693#else
3694 kfree((char *)dev - dev->padded);
3695#endif
3696}
4ec93edb 3697
1da177e4 3698/* Synchronize with packet receive processing. */
4ec93edb 3699void synchronize_net(void)
1da177e4
LT
3700{
3701 might_sleep();
fbd568a3 3702 synchronize_rcu();
1da177e4
LT
3703}
3704
3705/**
3706 * unregister_netdevice - remove device from the kernel
3707 * @dev: device
3708 *
3709 * This function shuts down a device interface and removes it
3710 * from the kernel tables. On success 0 is returned, on a failure
3711 * a negative errno code is returned.
3712 *
3713 * Callers must hold the rtnl semaphore. You may want
3714 * unregister_netdev() instead of this.
3715 */
3716
22f8cde5 3717void unregister_netdevice(struct net_device *dev)
1da177e4 3718{
1da177e4
LT
3719 BUG_ON(dev_boot_phase);
3720 ASSERT_RTNL();
3721
3722 /* Some devices call without registering for initialization unwind. */
3723 if (dev->reg_state == NETREG_UNINITIALIZED) {
3724 printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
3725 "was registered\n", dev->name, dev);
22f8cde5
SH
3726
3727 WARN_ON(1);
3728 return;
1da177e4
LT
3729 }
3730
3731 BUG_ON(dev->reg_state != NETREG_REGISTERED);
3732
3733 /* If device is running, close it first. */
3734 if (dev->flags & IFF_UP)
3735 dev_close(dev);
3736
3737 /* And unlink it from device chain. */
7562f876
PE
3738 write_lock_bh(&dev_base_lock);
3739 list_del(&dev->dev_list);
3740 hlist_del(&dev->name_hlist);
3741 hlist_del(&dev->index_hlist);
3742 write_unlock_bh(&dev_base_lock);
1da177e4
LT
3743
3744 dev->reg_state = NETREG_UNREGISTERING;
3745
3746 synchronize_net();
3747
3748 /* Shutdown queueing discipline. */
3749 dev_shutdown(dev);
3750
4ec93edb 3751
1da177e4
LT
3752 /* Notify protocols, that we are about to destroy
3753 this device. They should clean all the things.
3754 */
f07d5b94 3755 raw_notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
4ec93edb 3756
1da177e4 3757 /*
4417da66 3758 * Flush the unicast and multicast chains
1da177e4 3759 */
26cc2522 3760 dev_addr_discard(dev);
1da177e4
LT
3761
3762 if (dev->uninit)
3763 dev->uninit(dev);
3764
3765 /* Notifier chain MUST detach us from master device. */
3766 BUG_TRAP(!dev->master);
3767
9093bbb2
SH
3768 /* Remove entries from sysfs */
3769 netdev_unregister_sysfs(dev);
3770
1da177e4
LT
3771 /* Finish processing unregister after unlock */
3772 net_set_todo(dev);
3773
3774 synchronize_net();
3775
3776 dev_put(dev);
1da177e4
LT
3777}
3778
3779/**
3780 * unregister_netdev - remove device from the kernel
3781 * @dev: device
3782 *
3783 * This function shuts down a device interface and removes it
3784 * from the kernel tables. On success 0 is returned, on a failure
3785 * a negative errno code is returned.
3786 *
3787 * This is just a wrapper for unregister_netdevice that takes
3788 * the rtnl semaphore. In general you want to use this and not
3789 * unregister_netdevice.
3790 */
3791void unregister_netdev(struct net_device *dev)
3792{
3793 rtnl_lock();
3794 unregister_netdevice(dev);
3795 rtnl_unlock();
3796}
3797
3798EXPORT_SYMBOL(unregister_netdev);
3799
1da177e4
LT
3800static int dev_cpu_callback(struct notifier_block *nfb,
3801 unsigned long action,
3802 void *ocpu)
3803{
3804 struct sk_buff **list_skb;
3805 struct net_device **list_net;
3806 struct sk_buff *skb;
3807 unsigned int cpu, oldcpu = (unsigned long)ocpu;
3808 struct softnet_data *sd, *oldsd;
3809
8bb78442 3810 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
1da177e4
LT
3811 return NOTIFY_OK;
3812
3813 local_irq_disable();
3814 cpu = smp_processor_id();
3815 sd = &per_cpu(softnet_data, cpu);
3816 oldsd = &per_cpu(softnet_data, oldcpu);
3817
3818 /* Find end of our completion_queue. */
3819 list_skb = &sd->completion_queue;
3820 while (*list_skb)
3821 list_skb = &(*list_skb)->next;
3822 /* Append completion queue from offline CPU. */
3823 *list_skb = oldsd->completion_queue;
3824 oldsd->completion_queue = NULL;
3825
3826 /* Find end of our output_queue. */
3827 list_net = &sd->output_queue;
3828 while (*list_net)
3829 list_net = &(*list_net)->next_sched;
3830 /* Append output queue from offline CPU. */
3831 *list_net = oldsd->output_queue;
3832 oldsd->output_queue = NULL;
3833
3834 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3835 local_irq_enable();
3836
3837 /* Process offline CPU's input_pkt_queue */
3838 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
3839 netif_rx(skb);
3840
3841 return NOTIFY_OK;
3842}
1da177e4 3843
db217334
CL
3844#ifdef CONFIG_NET_DMA
3845/**
0ed72ec4
RD
3846 * net_dma_rebalance - try to maintain one DMA channel per CPU
3847 * @net_dma: DMA client and associated data (lock, channels, channel_mask)
3848 *
3849 * This is called when the number of channels allocated to the net_dma client
3850 * changes. The net_dma client tries to have one DMA channel per CPU.
db217334 3851 */
d379b01e
DW
3852
3853static void net_dma_rebalance(struct net_dma *net_dma)
db217334 3854{
d379b01e 3855 unsigned int cpu, i, n, chan_idx;
db217334
CL
3856 struct dma_chan *chan;
3857
d379b01e 3858 if (cpus_empty(net_dma->channel_mask)) {
db217334 3859 for_each_online_cpu(cpu)
29bbd72d 3860 rcu_assign_pointer(per_cpu(softnet_data, cpu).net_dma, NULL);
db217334
CL
3861 return;
3862 }
3863
3864 i = 0;
3865 cpu = first_cpu(cpu_online_map);
3866
d379b01e
DW
3867 for_each_cpu_mask(chan_idx, net_dma->channel_mask) {
3868 chan = net_dma->channels[chan_idx];
3869
3870 n = ((num_online_cpus() / cpus_weight(net_dma->channel_mask))
3871 + (i < (num_online_cpus() %
3872 cpus_weight(net_dma->channel_mask)) ? 1 : 0));
db217334
CL
3873
3874 while(n) {
29bbd72d 3875 per_cpu(softnet_data, cpu).net_dma = chan;
db217334
CL
3876 cpu = next_cpu(cpu, cpu_online_map);
3877 n--;
3878 }
3879 i++;
3880 }
db217334
CL
3881}
3882
3883/**
3884 * netdev_dma_event - event callback for the net_dma_client
3885 * @client: should always be net_dma_client
f4b8ea78 3886 * @chan: DMA channel for the event
0ed72ec4 3887 * @state: DMA state to be handled
db217334 3888 */
d379b01e
DW
3889static enum dma_state_client
3890netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
3891 enum dma_state state)
3892{
3893 int i, found = 0, pos = -1;
3894 struct net_dma *net_dma =
3895 container_of(client, struct net_dma, client);
3896 enum dma_state_client ack = DMA_DUP; /* default: take no action */
3897
3898 spin_lock(&net_dma->lock);
3899 switch (state) {
3900 case DMA_RESOURCE_AVAILABLE:
3901 for (i = 0; i < NR_CPUS; i++)
3902 if (net_dma->channels[i] == chan) {
3903 found = 1;
3904 break;
3905 } else if (net_dma->channels[i] == NULL && pos < 0)
3906 pos = i;
3907
3908 if (!found && pos >= 0) {
3909 ack = DMA_ACK;
3910 net_dma->channels[pos] = chan;
3911 cpu_set(pos, net_dma->channel_mask);
3912 net_dma_rebalance(net_dma);
3913 }
db217334
CL
3914 break;
3915 case DMA_RESOURCE_REMOVED:
d379b01e
DW
3916 for (i = 0; i < NR_CPUS; i++)
3917 if (net_dma->channels[i] == chan) {
3918 found = 1;
3919 pos = i;
3920 break;
3921 }
3922
3923 if (found) {
3924 ack = DMA_ACK;
3925 cpu_clear(pos, net_dma->channel_mask);
3926 net_dma->channels[i] = NULL;
3927 net_dma_rebalance(net_dma);
3928 }
db217334
CL
3929 break;
3930 default:
3931 break;
3932 }
d379b01e
DW
3933 spin_unlock(&net_dma->lock);
3934
3935 return ack;
db217334
CL
3936}
3937
3938/**
3939 * netdev_dma_regiser - register the networking subsystem as a DMA client
3940 */
3941static int __init netdev_dma_register(void)
3942{
d379b01e
DW
3943 spin_lock_init(&net_dma.lock);
3944 dma_cap_set(DMA_MEMCPY, net_dma.client.cap_mask);
3945 dma_async_client_register(&net_dma.client);
3946 dma_async_client_chan_request(&net_dma.client);
db217334
CL
3947 return 0;
3948}
3949
3950#else
3951static int __init netdev_dma_register(void) { return -ENODEV; }
3952#endif /* CONFIG_NET_DMA */
1da177e4
LT
3953
3954/*
3955 * Initialize the DEV module. At boot time this walks the device list and
3956 * unhooks any devices that fail to initialise (normally hardware not
3957 * present) and leaves us with a valid list of present and active devices.
3958 *
3959 */
3960
3961/*
3962 * This is called single threaded during boot, so no need
3963 * to take the rtnl semaphore.
3964 */
3965static int __init net_dev_init(void)
3966{
3967 int i, rc = -ENOMEM;
3968
3969 BUG_ON(!dev_boot_phase);
3970
1da177e4
LT
3971 if (dev_proc_init())
3972 goto out;
3973
3974 if (netdev_sysfs_init())
3975 goto out;
3976
3977 INIT_LIST_HEAD(&ptype_all);
4ec93edb 3978 for (i = 0; i < 16; i++)
1da177e4
LT
3979 INIT_LIST_HEAD(&ptype_base[i]);
3980
3981 for (i = 0; i < ARRAY_SIZE(dev_name_head); i++)
3982 INIT_HLIST_HEAD(&dev_name_head[i]);
3983
3984 for (i = 0; i < ARRAY_SIZE(dev_index_head); i++)
3985 INIT_HLIST_HEAD(&dev_index_head[i]);
3986
3987 /*
3988 * Initialise the packet receive queues.
3989 */
3990
6f912042 3991 for_each_possible_cpu(i) {
1da177e4
LT
3992 struct softnet_data *queue;
3993
3994 queue = &per_cpu(softnet_data, i);
3995 skb_queue_head_init(&queue->input_pkt_queue);
1da177e4
LT
3996 queue->completion_queue = NULL;
3997 INIT_LIST_HEAD(&queue->poll_list);
3998 set_bit(__LINK_STATE_START, &queue->backlog_dev.state);
3999 queue->backlog_dev.weight = weight_p;
4000 queue->backlog_dev.poll = process_backlog;
4001 atomic_set(&queue->backlog_dev.refcnt, 1);
4002 }
4003
db217334
CL
4004 netdev_dma_register();
4005
1da177e4
LT
4006 dev_boot_phase = 0;
4007
4008 open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
4009 open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
4010
4011 hotcpu_notifier(dev_cpu_callback, 0);
4012 dst_init();
4013 dev_mcast_init();
4014 rc = 0;
4015out:
4016 return rc;
4017}
4018
4019subsys_initcall(net_dev_init);
4020
4021EXPORT_SYMBOL(__dev_get_by_index);
4022EXPORT_SYMBOL(__dev_get_by_name);
4023EXPORT_SYMBOL(__dev_remove_pack);
c2373ee9 4024EXPORT_SYMBOL(dev_valid_name);
1da177e4
LT
4025EXPORT_SYMBOL(dev_add_pack);
4026EXPORT_SYMBOL(dev_alloc_name);
4027EXPORT_SYMBOL(dev_close);
4028EXPORT_SYMBOL(dev_get_by_flags);
4029EXPORT_SYMBOL(dev_get_by_index);
4030EXPORT_SYMBOL(dev_get_by_name);
1da177e4
LT
4031EXPORT_SYMBOL(dev_open);
4032EXPORT_SYMBOL(dev_queue_xmit);
4033EXPORT_SYMBOL(dev_remove_pack);
4034EXPORT_SYMBOL(dev_set_allmulti);
4035EXPORT_SYMBOL(dev_set_promiscuity);
4036EXPORT_SYMBOL(dev_change_flags);
4037EXPORT_SYMBOL(dev_set_mtu);
4038EXPORT_SYMBOL(dev_set_mac_address);
4039EXPORT_SYMBOL(free_netdev);
4040EXPORT_SYMBOL(netdev_boot_setup_check);
4041EXPORT_SYMBOL(netdev_set_master);
4042EXPORT_SYMBOL(netdev_state_change);
4043EXPORT_SYMBOL(netif_receive_skb);
4044EXPORT_SYMBOL(netif_rx);
4045EXPORT_SYMBOL(register_gifconf);
4046EXPORT_SYMBOL(register_netdevice);
4047EXPORT_SYMBOL(register_netdevice_notifier);
4048EXPORT_SYMBOL(skb_checksum_help);
4049EXPORT_SYMBOL(synchronize_net);
4050EXPORT_SYMBOL(unregister_netdevice);
4051EXPORT_SYMBOL(unregister_netdevice_notifier);
4052EXPORT_SYMBOL(net_enable_timestamp);
4053EXPORT_SYMBOL(net_disable_timestamp);
4054EXPORT_SYMBOL(dev_get_flags);
4055
4056#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4057EXPORT_SYMBOL(br_handle_frame_hook);
4058EXPORT_SYMBOL(br_fdb_get_hook);
4059EXPORT_SYMBOL(br_fdb_put_hook);
4060#endif
4061
4062#ifdef CONFIG_KMOD
4063EXPORT_SYMBOL(dev_load);
4064#endif
4065
4066EXPORT_PER_CPU_SYMBOL(softnet_data);