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