dev.c 73.3 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 	NET3	Protocol independent device support routines.
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 *	Derived from the non IP parts of dev.c 1.0.19
 * 		Authors:	Ross Biro, <bir7@leland.Stanford.Edu>
 *				Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
 *				Mark Evans, <evansmp@uhura.aston.ac.uk>
 *
 *	Additional Authors:
 *		Florian la Roche <rzsfl@rz.uni-sb.de>
 *		Alan Cox <gw4pts@gw4pts.ampr.org>
17
 *		David Hinds <dahinds@users.sourceforge.net>
Linus Torvalds's avatar
Linus Torvalds committed
18 19 20 21 22
 *		Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
 *		Adam Sulmicki <adam@cfar.umd.edu>
 *              Pekka Riikonen <priikone@poesidon.pspt.fi>
 *
 *	Changes:
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
23 24 25 26
 *              D.J. Barrow     :       Fixed bug where dev->refcnt gets set
 *              			to 2 if register_netdev gets called
 *              			before net_dev_init & also removed a
 *              			few lines of code in the process.
Linus Torvalds's avatar
Linus Torvalds committed
27
 *		Alan Cox	:	device private ioctl copies fields back.
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
28 29
 *		Alan Cox	:	Transmit queue code does relevant
 *					stunts to keep the queue safe.
Linus Torvalds's avatar
Linus Torvalds committed
30 31 32
 *		Alan Cox	:	Fixed double lock.
 *		Alan Cox	:	Fixed promisc NULL pointer trap
 *		????????	:	Support the full private ioctl range
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
33 34
 *		Alan Cox	:	Moved ioctl permission check into
 *					drivers
Linus Torvalds's avatar
Linus Torvalds committed
35 36 37 38 39 40 41
 *		Tim Kordas	:	SIOCADDMULTI/SIOCDELMULTI
 *		Alan Cox	:	100 backlog just doesn't cut it when
 *					you start doing multicast video 8)
 *		Alan Cox	:	Rewrote net_bh and list manager.
 *		Alan Cox	: 	Fix ETH_P_ALL echoback lengths.
 *		Alan Cox	:	Took out transmit every packet pass
 *					Saved a few bytes in the ioctl handler
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
42 43 44
 *		Alan Cox	:	Network driver sets packet type before
 *					calling netif_rx. Saves a function
 *					call a packet.
Linus Torvalds's avatar
Linus Torvalds committed
45 46 47 48
 *		Alan Cox	:	Hashed net_bh()
 *		Richard Kooijman:	Timestamp fixes.
 *		Alan Cox	:	Wrong field in SIOCGIFDSTADDR
 *		Alan Cox	:	Device lock protection.
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
49 50 51 52 53 54
 *		Alan Cox	: 	Fixed nasty side effect of device close
 *					changes.
 *		Rudi Cilibrasi	:	Pass the right thing to
 *					set_mac_address()
 *		Dave Miller	:	32bit quantity for the device lock to
 *					make it work out on a Sparc.
Linus Torvalds's avatar
Linus Torvalds committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68
 *		Bjorn Ekwall	:	Added KERNELD hack.
 *		Alan Cox	:	Cleaned up the backlog initialise.
 *		Craig Metz	:	SIOCGIFCONF fix if space for under
 *					1 device.
 *	    Thomas Bogendoerfer :	Return ENODEV for dev_open, if there
 *					is no device open function.
 *		Andi Kleen	:	Fix error reporting for SIOCGIFCONF
 *	    Michael Chastain	:	Fix signed/unsigned for SIOCGIFCONF
 *		Cyrus Durgin	:	Cleaned for KMOD
 *		Adam Sulmicki   :	Bug Fix : Network Device Unload
 *					A network device unload needs to purge
 *					the backlog queue.
 *	Paul Rusty Russell	:	SIOCSIFNAME
 *              Pekka Riikonen  :	Netdev boot-time settings code
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
69 70
 *              Andrew Morton   :       Make unregister_netdevice wait
 *              			indefinitely on dev->refcnt
Linus Torvalds's avatar
Linus Torvalds committed
71
 * 		J Hadi Salim	:	- Backlog queue sampling
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
72
 *				        - netif_rx() feedback
Linus Torvalds's avatar
Linus Torvalds committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
 */

#include <asm/uaccess.h>
#include <asm/system.h>
#include <asm/bitops.h>
#include <linux/config.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/if_ether.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/notifier.h>
#include <linux/skbuff.h>
#include <net/sock.h>
#include <linux/rtnetlink.h>
#include <linux/proc_fs.h>
96
#include <linux/seq_file.h>
Linus Torvalds's avatar
Linus Torvalds committed
97 98 99 100 101
#include <linux/stat.h>
#include <linux/if_bridge.h>
#include <linux/divert.h>
#include <net/dst.h>
#include <net/pkt_sched.h>
Linus Torvalds's avatar
Linus Torvalds committed
102 103
#include <net/checksum.h>
#include <linux/highmem.h>
Linus Torvalds's avatar
Linus Torvalds committed
104 105 106
#include <linux/init.h>
#include <linux/kmod.h>
#include <linux/module.h>
107
#include <linux/kallsyms.h>
108
#ifdef CONFIG_NET_RADIO
Linus Torvalds's avatar
Linus Torvalds committed
109
#include <linux/wireless.h>		/* Note : will define WIRELESS_EXT */
Linus Torvalds's avatar
Linus Torvalds committed
110
#include <net/iw_handler.h>
111
#endif	/* CONFIG_NET_RADIO */
112
#include <asm/current.h>
Linus Torvalds's avatar
Linus Torvalds committed
113

Linus Torvalds's avatar
Linus Torvalds committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
/* This define, if set, will randomly drop a packet when congestion
 * is more than moderate.  It helps fairness in the multi-interface
 * case when one of them is a hog, but it kills performance for the
 * single interface case so it is off now by default.
 */
#undef RAND_LIE

/* Setting this will sample the queue lengths and thus congestion
 * via a timer instead of as each packet is received.
 */
#undef OFFLINE_SAMPLE

/*
 *	The list of packet types we will receive (as opposed to discard)
 *	and the routines to invoke.
 *
 *	Why 16. Because with 16 the only overlap we get on a hash of the
Linus Torvalds's avatar
Linus Torvalds committed
131 132 133 134 135 136 137 138
 *	low nibble of the protocol value is RARP/SNAP/X.25.
 *
 *      NOTE:  That is no longer true with the addition of VLAN tags.  Not
 *             sure which should go first, but I bet it won't make much
 *             difference if we are running VLANs.  The good news is that
 *             this protocol won't be in the list unless compiled in, so
 *             the average user (w/out VLANs) will not be adversly affected.
 *             --BLG
Linus Torvalds's avatar
Linus Torvalds committed
139 140
 *
 *		0800	IP
Linus Torvalds's avatar
Linus Torvalds committed
141
 *		8100    802.1Q VLAN
Linus Torvalds's avatar
Linus Torvalds committed
142 143 144 145 146 147 148 149 150 151 152 153
 *		0001	802.3
 *		0002	AX.25
 *		0004	802.2
 *		8035	RARP
 *		0005	SNAP
 *		0805	X.25
 *		0806	ARP
 *		8137	IPX
 *		0009	Localtalk
 *		86DD	IPv6
 */

154 155 156
static spinlock_t ptype_lock = SPIN_LOCK_UNLOCKED;
static struct list_head ptype_base[16];	/* 16 way hashed list */
static struct list_head ptype_all;		/* Taps */
Linus Torvalds's avatar
Linus Torvalds committed
157 158 159

#ifdef OFFLINE_SAMPLE
static void sample_queue(unsigned long dummy);
Andrew Morton's avatar
Andrew Morton committed
160
static struct timer_list samp_timer = TIMER_INITIALIZER(sample_queue, 0, 0);
Linus Torvalds's avatar
Linus Torvalds committed
161 162 163 164 165
#endif

/*
 *	Our notifier list
 */
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
166 167

static struct notifier_block *netdev_chain;
Linus Torvalds's avatar
Linus Torvalds committed
168 169 170 171 172

/*
 *	Device drivers call our routines to queue packets here. We empty the
 *	queue in the local softnet handler.
 */
173
DEFINE_PER_CPU(struct softnet_data, softnet_data) = { 0, };
Linus Torvalds's avatar
Linus Torvalds committed
174 175 176 177 178 179

#ifdef CONFIG_NET_FASTROUTE
int netdev_fastroute;
int netdev_fastroute_obstacles;
#endif

180 181
extern int netdev_sysfs_init(void);
extern int netdev_register_sysfs(struct net_device *);
182
extern int netdev_unregister_sysfs(struct net_device *);
183

Linus Torvalds's avatar
Linus Torvalds committed
184

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
185
/*******************************************************************************
Linus Torvalds's avatar
Linus Torvalds committed
186 187 188

		Protocol management and registration routines

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
189
*******************************************************************************/
Linus Torvalds's avatar
Linus Torvalds committed
190 191 192 193 194

/*
 *	For efficiency
 */

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
195
int netdev_nit;
Linus Torvalds's avatar
Linus Torvalds committed
196 197 198 199 200 201 202 203

/*
 *	Add a protocol ID to the list. Now that the input handler is
 *	smarter we can dispense with all the messy stuff that used to be
 *	here.
 *
 *	BEWARE!!! Protocol handlers, mangling input packets,
 *	MUST BE last in hash buckets and checking protocol handlers
Steven Cole's avatar
Steven Cole committed
204
 *	MUST start from promiscuous ptype_all chain in net_bh.
Linus Torvalds's avatar
Linus Torvalds committed
205
 *	It is true now, do not change it.
Steven Cole's avatar
Steven Cole committed
206
 *	Explanation follows: if protocol handler, mangling packet, will
Linus Torvalds's avatar
Linus Torvalds committed
207 208 209 210 211 212 213 214 215
 *	be the first on list, it is not able to sense, that packet
 *	is cloned and should be copied-on-write, so that it will
 *	change it and subsequent readers will get broken packet.
 *							--ANK (980803)
 */

/**
 *	dev_add_pack - add packet handler
 *	@pt: packet type declaration
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
216
 *
Linus Torvalds's avatar
Linus Torvalds committed
217 218 219
 *	Add a protocol handler to the networking stack. The passed &packet_type
 *	is linked into kernel lists and may not be freed until it has been
 *	removed from the kernel lists.
220 221 222 223
 *
 *	This call does not sleep therefore it can not 
 *	guarantee all CPU's that are in middle of receiving packets
 *	will see the new packet type (until the next received packet).
Linus Torvalds's avatar
Linus Torvalds committed
224
 */
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
225

Linus Torvalds's avatar
Linus Torvalds committed
226 227 228 229
void dev_add_pack(struct packet_type *pt)
{
	int hash;

230
	spin_lock_bh(&ptype_lock);
Linus Torvalds's avatar
Linus Torvalds committed
231
#ifdef CONFIG_NET_FASTROUTE
232
	if (pt->af_packet_priv) {
Linus Torvalds's avatar
Linus Torvalds committed
233 234 235 236 237 238
		netdev_fastroute_obstacles++;
		dev_clear_fastroute(pt->dev);
	}
#endif
	if (pt->type == htons(ETH_P_ALL)) {
		netdev_nit++;
239
		list_add_rcu(&pt->list, &ptype_all);
Linus Torvalds's avatar
Linus Torvalds committed
240
	} else {
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
241
		hash = ntohs(pt->type) & 15;
242
		list_add_rcu(&pt->list, &ptype_base[hash]);
Linus Torvalds's avatar
Linus Torvalds committed
243
	}
244
	spin_unlock_bh(&ptype_lock);
Linus Torvalds's avatar
Linus Torvalds committed
245 246
}

247
extern void linkwatch_run_queue(void);
Linus Torvalds's avatar
Linus Torvalds committed
248

249 250


Linus Torvalds's avatar
Linus Torvalds committed
251
/**
252
 *	__dev_remove_pack	 - remove packet handler
Linus Torvalds's avatar
Linus Torvalds committed
253
 *	@pt: packet type declaration
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
254
 *
Linus Torvalds's avatar
Linus Torvalds committed
255 256 257
 *	Remove a protocol handler that was previously added to the kernel
 *	protocol handlers by dev_add_pack(). The passed &packet_type is removed
 *	from the kernel lists and can be freed or reused once this function
258 259 260 261 262
 *	returns. 
 *
 *      The packet type might still be in use by receivers
 *	and must not be freed until after all the CPU's have gone
 *	through a quiescent state.
Linus Torvalds's avatar
Linus Torvalds committed
263
 */
264
void __dev_remove_pack(struct packet_type *pt)
Linus Torvalds's avatar
Linus Torvalds committed
265
{
266 267
	struct list_head *head;
	struct packet_type *pt1;
Linus Torvalds's avatar
Linus Torvalds committed
268

269
	spin_lock_bh(&ptype_lock);
Linus Torvalds's avatar
Linus Torvalds committed
270 271 272

	if (pt->type == htons(ETH_P_ALL)) {
		netdev_nit--;
273
		head = &ptype_all;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
274
	} else
275
		head = &ptype_base[ntohs(pt->type) & 15];
Linus Torvalds's avatar
Linus Torvalds committed
276

277 278
	list_for_each_entry(pt1, head, list) {
		if (pt == pt1) {
Linus Torvalds's avatar
Linus Torvalds committed
279
#ifdef CONFIG_NET_FASTROUTE
280
			if (pt->af_packet_priv)
Linus Torvalds's avatar
Linus Torvalds committed
281 282
				netdev_fastroute_obstacles--;
#endif
283
			list_del_rcu(&pt->list);
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
284
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
285 286
		}
	}
287

Linus Torvalds's avatar
Linus Torvalds committed
288
	printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
289
out:
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	spin_unlock_bh(&ptype_lock);
}
/**
 *	dev_remove_pack	 - remove packet handler
 *	@pt: packet type declaration
 *
 *	Remove a protocol handler that was previously added to the kernel
 *	protocol handlers by dev_add_pack(). The passed &packet_type is removed
 *	from the kernel lists and can be freed or reused once this function
 *	returns.
 *
 *	This call sleeps to guarantee that no CPU is looking at the packet
 *	type after return.
 */
void dev_remove_pack(struct packet_type *pt)
{
	__dev_remove_pack(pt);
	
	synchronize_net();
Linus Torvalds's avatar
Linus Torvalds committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
}

/******************************************************************************

		      Device Boot-time Settings Routines

*******************************************************************************/

/* Boot time configuration table */
static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];

/**
 *	netdev_boot_setup_add	- add new setup entry
 *	@name: name of the device
 *	@map: configured settings for the device
 *
 *	Adds new setup entry to the dev_boot_setup list.  The function
 *	returns 0 on error and 1 on success.  This is a generic routine to
 *	all netdevices.
 */
int netdev_boot_setup_add(char *name, struct ifmap *map)
{
	struct netdev_boot_setup *s;
	int i;

	s = dev_boot_setup;
	for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
		if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
			memset(s[i].name, 0, sizeof(s[i].name));
			strcpy(s[i].name, name);
			memcpy(&s[i].map, map, sizeof(s[i].map));
			break;
		}
	}

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
344
	return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
Linus Torvalds's avatar
Linus Torvalds committed
345 346 347 348 349 350 351 352 353 354 355 356 357
}

/**
 *	netdev_boot_setup_check	- check boot time settings
 *	@dev: the netdevice
 *
 * 	Check boot time settings for the device.
 *	The found settings are set for the device to be used
 *	later in the device probing.
 *	Returns 0 if no settings found, 1 if they are.
 */
int netdev_boot_setup_check(struct net_device *dev)
{
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
358
	struct netdev_boot_setup *s = dev_boot_setup;
Linus Torvalds's avatar
Linus Torvalds committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
	int i;

	for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
		if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
		    !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
			dev->irq 	= s[i].map.irq;
			dev->base_addr 	= s[i].map.base_addr;
			dev->mem_start 	= s[i].map.mem_start;
			dev->mem_end 	= s[i].map.mem_end;
			return 1;
		}
	}
	return 0;
}

/*
 * Saves at boot time configured settings for any netdevice.
 */
Linus Torvalds's avatar
Linus Torvalds committed
377
int __init netdev_boot_setup(char *str)
Linus Torvalds's avatar
Linus Torvalds committed
378 379 380 381 382 383 384 385 386
{
	int ints[5];
	struct ifmap map;

	str = get_options(str, ARRAY_SIZE(ints), ints);
	if (!str || !*str)
		return 0;

	/* Save settings */
Linus Torvalds's avatar
Linus Torvalds committed
387
	memset(&map, 0, sizeof(map));
Linus Torvalds's avatar
Linus Torvalds committed
388 389 390 391 392 393 394 395 396
	if (ints[0] > 0)
		map.irq = ints[1];
	if (ints[0] > 1)
		map.base_addr = ints[2];
	if (ints[0] > 2)
		map.mem_start = ints[3];
	if (ints[0] > 3)
		map.mem_end = ints[4];

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
397
	/* Add new entry to the list */
Linus Torvalds's avatar
Linus Torvalds committed
398 399 400 401 402
	return netdev_boot_setup_add(str, &map);
}

__setup("netdev=", netdev_boot_setup);

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
403
/*******************************************************************************
Linus Torvalds's avatar
Linus Torvalds committed
404 405 406

			    Device Interface Subroutines

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
407
*******************************************************************************/
Linus Torvalds's avatar
Linus Torvalds committed
408 409

/**
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
410
 *	__dev_get_by_name	- find a device by its name
Linus Torvalds's avatar
Linus Torvalds committed
411 412 413 414 415 416 417 418 419 420 421 422 423
 *	@name: name to find
 *
 *	Find an interface by name. Must be called under RTNL semaphore
 *	or @dev_base_lock. If the name is found a pointer to the device
 *	is returned. If the name is not found then %NULL is returned. The
 *	reference counters are not incremented so the caller must be
 *	careful with locks.
 */

struct net_device *__dev_get_by_name(const char *name)
{
	struct net_device *dev;

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
424 425 426 427
	for (dev = dev_base; dev; dev = dev->next)
		if (!strncmp(dev->name, name, IFNAMSIZ))
			break;
	return dev;
Linus Torvalds's avatar
Linus Torvalds committed
428 429 430 431 432 433
}

/**
 *	dev_get_by_name		- find a device by its name
 *	@name: name to find
 *
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
434
 *	Find an interface by name. This can be called from any
Linus Torvalds's avatar
Linus Torvalds committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
 *	context and does its own locking. The returned handle has
 *	the usage count incremented and the caller must use dev_put() to
 *	release it when it is no longer needed. %NULL is returned if no
 *	matching device is found.
 */

struct net_device *dev_get_by_name(const char *name)
{
	struct net_device *dev;

	read_lock(&dev_base_lock);
	dev = __dev_get_by_name(name);
	if (dev)
		dev_hold(dev);
	read_unlock(&dev_base_lock);
	return dev;
}

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
453
/*
Linus Torvalds's avatar
Linus Torvalds committed
454
   Return value is changed to int to prevent illegal usage in future.
455
   It is still legal to use to check for device existence.
Linus Torvalds's avatar
Linus Torvalds committed
456 457 458 459 460 461 462 463 464 465 466 467 468

   User should understand, that the result returned by this function
   is meaningless, if it was not issued under rtnl semaphore.
 */

/**
 *	dev_get	-	test if a device exists
 *	@name:	name to test for
 *
 *	Test if a name exists. Returns true if the name is found. In order
 *	to be sure the name is not allocated or removed during the test the
 *	caller must hold the rtnl semaphore.
 *
469
 *	This function exists only for back compatibility with older
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
470
 *	drivers.
Linus Torvalds's avatar
Linus Torvalds committed
471
 */
472
int __dev_get(const char *name)
Linus Torvalds's avatar
Linus Torvalds committed
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
{
	struct net_device *dev;

	read_lock(&dev_base_lock);
	dev = __dev_get_by_name(name);
	read_unlock(&dev_base_lock);
	return dev != NULL;
}

/**
 *	__dev_get_by_index - find a device by its ifindex
 *	@ifindex: index of device
 *
 *	Search for an interface by index. Returns %NULL if the device
 *	is not found or a pointer to the device. The device has not
 *	had its reference counter increased so the caller must be careful
 *	about locking. The caller must hold either the RTNL semaphore
 *	or @dev_base_lock.
 */

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
493
struct net_device *__dev_get_by_index(int ifindex)
Linus Torvalds's avatar
Linus Torvalds committed
494 495 496
{
	struct net_device *dev;

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
497
	for (dev = dev_base; dev; dev = dev->next)
Linus Torvalds's avatar
Linus Torvalds committed
498
		if (dev->ifindex == ifindex)
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
499 500
			break;
	return dev;
Linus Torvalds's avatar
Linus Torvalds committed
501 502 503 504 505 506 507 508
}


/**
 *	dev_get_by_index - find a device by its ifindex
 *	@ifindex: index of device
 *
 *	Search for an interface by index. Returns NULL if the device
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
509
 *	is not found or a pointer to the device. The device returned has
Linus Torvalds's avatar
Linus Torvalds committed
510 511 512 513
 *	had a reference added and the pointer is safe until the user calls
 *	dev_put to indicate they have finished with it.
 */

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
514
struct net_device *dev_get_by_index(int ifindex)
Linus Torvalds's avatar
Linus Torvalds committed
515 516 517 518 519 520 521 522 523 524 525 526
{
	struct net_device *dev;

	read_lock(&dev_base_lock);
	dev = __dev_get_by_index(ifindex);
	if (dev)
		dev_hold(dev);
	read_unlock(&dev_base_lock);
	return dev;
}

/**
Linus Torvalds's avatar
Linus Torvalds committed
527
 *	dev_getbyhwaddr - find a device by its hardware address
Linus Torvalds's avatar
Linus Torvalds committed
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
 *	@type: media type of device
 *	@ha: hardware address
 *
 *	Search for an interface by MAC address. Returns NULL if the device
 *	is not found or a pointer to the device. The caller must hold the
 *	rtnl semaphore. The returned device has not had its ref count increased
 *	and the caller must therefore be careful about locking
 *
 *	BUGS:
 *	If the API was consistent this would be __dev_get_by_hwaddr
 */

struct net_device *dev_getbyhwaddr(unsigned short type, char *ha)
{
	struct net_device *dev;

	ASSERT_RTNL();

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
546
	for (dev = dev_base; dev; dev = dev->next)
Linus Torvalds's avatar
Linus Torvalds committed
547
		if (dev->type == type &&
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
548 549 550
		    !memcmp(dev->dev_addr, ha, dev->addr_len))
			break;
	return dev;
Linus Torvalds's avatar
Linus Torvalds committed
551 552
}

David Stevens's avatar
David Stevens committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
/**
 *	dev_get_by_flags - find any device with given flags
 *	@if_flags: IFF_* values
 *	@mask: bitmask of bits in if_flags to check
 *
 *	Search for any interface with the given flags. Returns NULL if a device
 *	is not found or a pointer to the device. The device returned has 
 *	had a reference added and the pointer is safe until the user calls
 *	dev_put to indicate they have finished with it.
 */

struct net_device * dev_get_by_flags(unsigned short if_flags, unsigned short mask)
{
	struct net_device *dev;

	read_lock(&dev_base_lock);
	dev = __dev_get_by_flags(if_flags, mask);
	if (dev)
		dev_hold(dev);
	read_unlock(&dev_base_lock);
	return dev;
}

/**
 *	__dev_get_by_flags - find any device with given flags
 *	@if_flags: IFF_* values
 *	@mask: bitmask of bits in if_flags to check
 *
 *	Search for any interface with the given flags. Returns NULL if a device
 *	is not found or a pointer to the device. The caller must hold either
 *	the RTNL semaphore or @dev_base_lock.
 */

struct net_device *__dev_get_by_flags(unsigned short if_flags, unsigned short mask)
{
	struct net_device *dev;

	for (dev = dev_base; dev != NULL; dev = dev->next) {
		if (((dev->flags ^ if_flags) & mask) == 0)
			return dev;
	}
	return NULL;
}

Linus Torvalds's avatar
Linus Torvalds committed
597 598
/**
 *	dev_alloc_name - allocate a name for a device
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
599
 *	@dev: device
Linus Torvalds's avatar
Linus Torvalds committed
600 601 602 603 604 605 606 607 608 609 610 611 612
 *	@name: name format string
 *
 *	Passed a format string - eg "lt%d" it will try and find a suitable
 *	id. Not efficient for many devices, not called a lot. The caller
 *	must hold the dev_base or rtnl lock while allocating the name and
 *	adding the device in order to avoid duplicates. Returns the number
 *	of the unit assigned or a negative errno code.
 */

int dev_alloc_name(struct net_device *dev, const char *name)
{
	int i;
	char buf[32];
Linus Torvalds's avatar
Linus Torvalds committed
613
	char *p;
Linus Torvalds's avatar
Linus Torvalds committed
614 615

	/*
Linus Torvalds's avatar
Linus Torvalds committed
616
	 * Verify the string as this thing may have come from
Linus Torvalds's avatar
Linus Torvalds committed
617 618
	 * the user.  There must be either one "%d" and no other "%"
	 * characters, or no "%" characters at all.
Linus Torvalds's avatar
Linus Torvalds committed
619
	 */
Linus Torvalds's avatar
Linus Torvalds committed
620
	p = strchr(name, '%');
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
621
	if (p && (p[1] != 'd' || strchr(p + 2, '%')))
Linus Torvalds's avatar
Linus Torvalds committed
622
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
623 624 625

	/*
	 * If you need over 100 please also fix the algorithm...
Linus Torvalds's avatar
Linus Torvalds committed
626 627
	 */
	for (i = 0; i < 100; i++) {
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
628 629
		snprintf(buf, sizeof(buf), name, i);
		if (!__dev_get_by_name(buf)) {
Linus Torvalds's avatar
Linus Torvalds committed
630 631 632 633 634 635 636 637 638 639 640 641 642 643
			strcpy(dev->name, buf);
			return i;
		}
	}
	return -ENFILE;	/* Over 100 of the things .. bail out! */
}

/**
 *	dev_alloc - allocate a network device and name
 *	@name: name format string
 *	@err: error return pointer
 *
 *	Passed a format string, eg. "lt%d", it will allocate a network device
 *	and space for the name. %NULL is returned if no memory is available.
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
644
 *	If the allocation succeeds then the name is assigned and the
Linus Torvalds's avatar
Linus Torvalds committed
645 646 647 648
 *	device pointer returned. %NULL is returned if the name allocation
 *	failed. The cause of an error is returned as a negative errno code
 *	in the variable @err points to.
 *
649 650
 *	This call is deprecated in favor of alloc_netdev because
 *	the caller must hold the @dev_base or RTNL locks when doing this in
Linus Torvalds's avatar
Linus Torvalds committed
651 652 653
 *	order to avoid duplicate name allocations.
 */

654
struct net_device *__dev_alloc(const char *name, int *err)
Linus Torvalds's avatar
Linus Torvalds committed
655
{
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
656 657 658
	struct net_device *dev = kmalloc(sizeof(*dev), GFP_KERNEL);

	if (!dev)
Linus Torvalds's avatar
Linus Torvalds committed
659
		*err = -ENOBUFS;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
660 661 662 663 664 665 666
	else {
		memset(dev, 0, sizeof(*dev));
		*err = dev_alloc_name(dev, name);
		if (*err < 0) {
			kfree(dev);
			dev = NULL;
		}
Linus Torvalds's avatar
Linus Torvalds committed
667 668 669 670 671 672 673 674 675 676 677 678 679 680
	}
	return dev;
}

/**
 *	netdev_state_change - device changes state
 *	@dev: device to cause notification
 *
 *	Called to indicate a device has changed state. This function calls
 *	the notifier chains for netdev_chain and sends a NEWLINK message
 *	to the routing socket.
 */
void netdev_state_change(struct net_device *dev)
{
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
681
	if (dev->flags & IFF_UP) {
Linus Torvalds's avatar
Linus Torvalds committed
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
		notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
	}
}

/**
 *	dev_load 	- load a network module
 *	@name: name of interface
 *
 *	If a network interface is not present and the process has suitable
 *	privileges this function loads the module. If module loading is not
 *	available in this kernel then it becomes a nop.
 */

void dev_load(const char *name)
{
698 699 700 701 702 703
	struct net_device *dev;  

	read_lock(&dev_base_lock);
	dev = __dev_get_by_name(name);
	read_unlock(&dev_base_lock);

704
	if (!dev && capable(CAP_SYS_MODULE))
705
		request_module("%s", name);
Linus Torvalds's avatar
Linus Torvalds committed
706 707 708 709
}

static int default_rebuild_header(struct sk_buff *skb)
{
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
710 711
	printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n",
	       skb->dev ? skb->dev->name : "NULL!!!");
Linus Torvalds's avatar
Linus Torvalds committed
712 713 714 715 716
	kfree_skb(skb);
	return 1;
}

/**
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
717
 *	dev_open	- prepare an interface for use.
Linus Torvalds's avatar
Linus Torvalds committed
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
 *	@dev:	device to open
 *
 *	Takes a device from down to up state. The device's private open
 *	function is invoked and then the multicast lists are loaded. Finally
 *	the device is moved into the up state and a %NETDEV_UP message is
 *	sent to the netdev notifier chain.
 *
 *	Calling this function on an active interface is a nop. On a failure
 *	a negative errno code is returned.
 */
int dev_open(struct net_device *dev)
{
	int ret = 0;

	/*
	 *	Is it already up?
	 */

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
736
	if (dev->flags & IFF_UP)
Linus Torvalds's avatar
Linus Torvalds committed
737 738 739 740 741 742 743 744 745 746 747
		return 0;

	/*
	 *	Is it even present?
	 */
	if (!netif_device_present(dev))
		return -ENODEV;

	/*
	 *	Call device private open method
	 */
748 749 750 751 752
	set_bit(__LINK_STATE_START, &dev->state);
	if (dev->open) {
		ret = dev->open(dev);
		if (ret)
			clear_bit(__LINK_STATE_START, &dev->state);
Linus Torvalds's avatar
Linus Torvalds committed
753 754 755 756 757
	}

	/*
	 *	If it went open OK then:
	 */
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
758 759

	if (!ret) {
Linus Torvalds's avatar
Linus Torvalds committed
760 761 762 763 764 765
		/*
		 *	Set the flags.
		 */
		dev->flags |= IFF_UP;

		/*
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
766
		 *	Initialize multicasting status
Linus Torvalds's avatar
Linus Torvalds committed
767 768 769 770 771 772 773 774 775 776 777 778 779
		 */
		dev_mc_upload(dev);

		/*
		 *	Wakeup transmit queue engine
		 */
		dev_activate(dev);

		/*
		 *	... and announce new interface.
		 */
		notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
	}
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
780
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
781 782 783 784 785 786 787 788 789
}

#ifdef CONFIG_NET_FASTROUTE

static void dev_do_clear_fastroute(struct net_device *dev)
{
	if (dev->accept_fastpath) {
		int i;

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
790
		for (i = 0; i <= NETDEV_FASTROUTE_HMASK; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
			struct dst_entry *dst;

			write_lock_irq(&dev->fastpath_lock);
			dst = dev->fastpath[i];
			dev->fastpath[i] = NULL;
			write_unlock_irq(&dev->fastpath_lock);

			dst_release(dst);
		}
	}
}

void dev_clear_fastroute(struct net_device *dev)
{
	if (dev) {
		dev_do_clear_fastroute(dev);
	} else {
		read_lock(&dev_base_lock);
		for (dev = dev_base; dev; dev = dev->next)
			dev_do_clear_fastroute(dev);
		read_unlock(&dev_base_lock);
	}
}
#endif

/**
 *	dev_close - shutdown an interface.
 *	@dev: device to shutdown
 *
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
820
 *	This function moves an active device into down state. A
Linus Torvalds's avatar
Linus Torvalds committed
821 822 823 824 825 826
 *	%NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
 *	is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
 *	chain.
 */
int dev_close(struct net_device *dev)
{
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
827
	if (!(dev->flags & IFF_UP))
Linus Torvalds's avatar
Linus Torvalds committed
828 829 830 831 832 833 834 835 836 837 838 839
		return 0;

	/*
	 *	Tell people we are going down, so that they can
	 *	prepare to death, when device is still operating.
	 */
	notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);

	dev_deactivate(dev);

	clear_bit(__LINK_STATE_START, &dev->state);

840 841 842 843 844 845 846
	/* Synchronize to scheduled poll. We cannot touch poll list,
	 * it can be even on different cpu. So just clear netif_running(),
	 * and wait when poll really will happen. Actually, the best place
	 * for this is inside dev->stop() after device stopped its irq
	 * engine, but this requires more changes in devices. */

	smp_mb__after_clear_bit(); /* Commit netif_running(). */
Jeff Garzik's avatar
Jeff Garzik committed
847 848 849 850 851
	while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) {
		/* No hurry. */
		current->state = TASK_INTERRUPTIBLE;
		schedule_timeout(1);
	}
852

Linus Torvalds's avatar
Linus Torvalds committed
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
	/*
	 *	Call the device specific close. This cannot fail.
	 *	Only if device is UP
	 *
	 *	We allow it to be called even after a DETACH hot-plug
	 *	event.
	 */
	if (dev->stop)
		dev->stop(dev);

	/*
	 *	Device is now down.
	 */

	dev->flags &= ~IFF_UP;
#ifdef CONFIG_NET_FASTROUTE
	dev_clear_fastroute(dev);
#endif

	/*
873
	 * Tell people we are down
Linus Torvalds's avatar
Linus Torvalds committed
874 875 876
	 */
	notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
877
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
878 879 880 881 882 883 884
}


/*
 *	Device change register/unregister. These are not inline or static
 *	as we export them to the world.
 */
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
885

Linus Torvalds's avatar
Linus Torvalds committed
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
/**
 *	register_netdevice_notifier - register a network notifier block
 *	@nb: notifier
 *
 *	Register a notifier to be called when network device events occur.
 *	The notifier passed is linked into the kernel structures and must
 *	not be reused until it has been unregistered. A negative errno code
 *	is returned on a failure.
 */

int register_netdevice_notifier(struct notifier_block *nb)
{
	return notifier_chain_register(&netdev_chain, nb);
}

/**
 *	unregister_netdevice_notifier - unregister a network notifier block
 *	@nb: notifier
 *
 *	Unregister a notifier previously registered by
 *	register_netdevice_notifier(). The notifier is unlinked into the
 *	kernel structures and may then be reused. A negative errno code
 *	is returned on a failure.
 */

int unregister_netdevice_notifier(struct notifier_block *nb)
{
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
913
	return notifier_chain_unregister(&netdev_chain, nb);
Linus Torvalds's avatar
Linus Torvalds committed
914 915
}

916 917
/**
 *	call_netdevice_notifiers - call all network notifier blocks
918 919
 *      @val: value passed unmodified to notifier function
 *      @v:   pointer passed unmodified to notifier function
920 921 922 923 924 925 926 927 928 929
 *
 *	Call all network notifier blocks.  Parameters and return value
 *	are as for notifier_call_chain().
 */

int call_netdevice_notifiers(unsigned long val, void *v)
{
	return notifier_call_chain(&netdev_chain, val, v);
}

Linus Torvalds's avatar
Linus Torvalds committed
930 931 932 933 934 935 936 937
/*
 *	Support routine. Sends outgoing frames to any network
 *	taps currently in use.
 */

void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
{
	struct packet_type *ptype;
Linus Torvalds's avatar
Linus Torvalds committed
938
	do_gettimeofday(&skb->stamp);
Linus Torvalds's avatar
Linus Torvalds committed
939

940 941
	rcu_read_lock();
	list_for_each_entry_rcu(ptype, &ptype_all, list) {
Linus Torvalds's avatar
Linus Torvalds committed
942 943 944 945
		/* Never send packets back to the socket
		 * they originated from - MvS (miquels@drinkel.ow.org)
		 */
		if ((ptype->dev == dev || !ptype->dev) &&
946
		    (struct sock *)ptype->af_packet_priv != skb->sk) {
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
947 948
			struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
			if (!skb2)
Linus Torvalds's avatar
Linus Torvalds committed
949 950 951 952 953 954 955 956
				break;

			/* skb->nh should be correctly
			   set by sender, so that the second statement is
			   just protection against buggy protocols.
			 */
			skb2->mac.raw = skb2->data;

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
957 958
			if (skb2->nh.raw < skb2->data ||
			    skb2->nh.raw > skb2->tail) {
Linus Torvalds's avatar
Linus Torvalds committed
959
				if (net_ratelimit())
960 961
					printk(KERN_CRIT "protocol %04x is "
					       "buggy, dev %s\n",
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
962
					       skb2->protocol, dev->name);
Linus Torvalds's avatar
Linus Torvalds committed
963 964 965 966 967 968 969 970
				skb2->nh.raw = skb2->data;
			}

			skb2->h.raw = skb2->nh.raw;
			skb2->pkt_type = PACKET_OUTGOING;
			ptype->func(skb2, skb->dev, ptype);
		}
	}
971
	rcu_read_unlock();
Linus Torvalds's avatar
Linus Torvalds committed
972 973
}

Linus Torvalds's avatar
Linus Torvalds committed
974 975 976 977
/* Calculate csum in the case, when packet is misrouted.
 * If it failed by some reason, ignore and send skb with wrong
 * checksum.
 */
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
978
struct sk_buff *skb_checksum_help(struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
979 980
{
	unsigned int csum;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
981
	int offset = skb->h.raw - skb->data;
Linus Torvalds's avatar
Linus Torvalds committed
982 983 984 985 986 987 988 989

	if (offset > (int)skb->len)
		BUG();
	csum = skb_checksum(skb, offset, skb->len-offset, 0);

	offset = skb->tail - skb->h.raw;
	if (offset <= 0)
		BUG();
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
990
	if (skb->csum + 2 > offset)
Linus Torvalds's avatar
Linus Torvalds committed
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
		BUG();

	*(u16*)(skb->h.raw + skb->csum) = csum_fold(csum);
	skb->ip_summed = CHECKSUM_NONE;
	return skb;
}

#ifdef CONFIG_HIGHMEM
/* Actually, we should eliminate this check as soon as we know, that:
 * 1. IOMMU is present and allows to map all the memory.
 * 2. No high memory really exists on this machine.
 */

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1004
static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
1005 1006 1007
{
	int i;

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1008
	if (dev->features & NETIF_F_HIGHDMA)
Linus Torvalds's avatar
Linus Torvalds committed
1009 1010
		return 0;

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1011
	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Linus Torvalds's avatar
Linus Torvalds committed
1012 1013 1014 1015 1016 1017 1018 1019 1020
		if (skb_shinfo(skb)->frags[i].page >= highmem_start_page)
			return 1;

	return 0;
}
#else
#define illegal_highdma(dev, skb)	(0)
#endif

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
extern void skb_release_data(struct sk_buff *);

/* Keep head the same: replace data */
int __skb_linearize(struct sk_buff *skb, int gfp_mask)
{
	unsigned int size;
	u8 *data;
	long offset;
	struct skb_shared_info *ninfo;
	int headerlen = skb->data - skb->head;
	int expand = (skb->tail + skb->data_len) - skb->end;

	if (skb_shared(skb))
		BUG();

	if (expand <= 0)
		expand = 0;

	size = skb->end - skb->head + expand;
	size = SKB_DATA_ALIGN(size);
	data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
	if (!data)
		return -ENOMEM;

	/* Copy entire thing */
	if (skb_copy_bits(skb, -headerlen, data, headerlen + skb->len))
		BUG();

	/* Set up shinfo */
	ninfo = (struct skb_shared_info*)(data + size);
	atomic_set(&ninfo->dataref, 1);
	ninfo->tso_size = skb_shinfo(skb)->tso_size;
	ninfo->tso_segs = skb_shinfo(skb)->tso_segs;
	ninfo->nr_frags = 0;
	ninfo->frag_list = NULL;

	/* Offset between the two in bytes */
	offset = data - skb->head;

	/* Free old data. */
	skb_release_data(skb);

	skb->head = data;
	skb->end  = data + size;

	/* Set up new pointers */
	skb->h.raw   += offset;
	skb->nh.raw  += offset;
	skb->mac.raw += offset;
	skb->tail    += offset;
	skb->data    += offset;

	/* We are no longer a clone, even if we were. */
	skb->cloned    = 0;

	skb->tail     += skb->data_len;
	skb->data_len  = 0;
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1081 1082 1083
/**
 *	dev_queue_xmit - transmit a buffer
 *	@skb: buffer to transmit
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1084
 *
Linus Torvalds's avatar
Linus Torvalds committed
1085
 *	Queue a buffer for transmission to a network device. The caller must
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1086 1087
 *	have set the device and priority and built the buffer before calling
 *	this function. The function can be called from an interrupt.
Linus Torvalds's avatar
Linus Torvalds committed
1088 1089 1090 1091 1092
 *
 *	A negative errno code is returned on a failure. A success does not
 *	guarantee the frame will be transmitted as it may be dropped due
 *	to congestion or traffic shaping.
 */
Linus Torvalds's avatar
Linus Torvalds committed
1093

Linus Torvalds's avatar
Linus Torvalds committed
1094 1095 1096
int dev_queue_xmit(struct sk_buff *skb)
{
	struct net_device *dev = skb->dev;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1097 1098
	struct Qdisc *q;
	int rc = -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
1099

Linus Torvalds's avatar
Linus Torvalds committed
1100
	if (skb_shinfo(skb)->frag_list &&
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1101
	    !(dev->features & NETIF_F_FRAGLIST) &&
1102
	    __skb_linearize(skb, GFP_ATOMIC))
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1103
		goto out_kfree_skb;
Linus Torvalds's avatar
Linus Torvalds committed
1104 1105 1106 1107 1108 1109

	/* Fragmented skb is linearized if device does not support SG,
	 * or if at least one of fragments is in highmem and device
	 * does not support DMA from it.
	 */
	if (skb_shinfo(skb)->nr_frags &&
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1110
	    (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
1111
	    __skb_linearize(skb, GFP_ATOMIC))
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1112
		goto out_kfree_skb;
Linus Torvalds's avatar
Linus Torvalds committed
1113 1114 1115 1116 1117

	/* If packet is not checksummed and device does not support
	 * checksumming for this protocol, complete checksumming here.
	 */
	if (skb->ip_summed == CHECKSUM_HW &&
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1118 1119
	    (!(dev->features & (NETIF_F_HW_CSUM | NETIF_F_NO_CSUM)) &&
	     (!(dev->features & NETIF_F_IP_CSUM) ||
Linus Torvalds's avatar
Linus Torvalds committed
1120 1121
	      skb->protocol != htons(ETH_P_IP)))) {
		if ((skb = skb_checksum_help(skb)) == NULL)
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1122
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1123 1124
	}

Linus Torvalds's avatar
Linus Torvalds committed
1125 1126 1127 1128
	/* Grab device queue */
	spin_lock_bh(&dev->queue_lock);
	q = dev->qdisc;
	if (q->enqueue) {
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1129
		rc = q->enqueue(skb, q);
Linus Torvalds's avatar
Linus Torvalds committed
1130 1131 1132 1133

		qdisc_run(dev);

		spin_unlock_bh(&dev->queue_lock);
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1134 1135
		rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1136 1137 1138 1139 1140 1141
	}

	/* The device has no queue. Common case for software devices:
	   loopback, all the sorts of tunnels...

	   Really, it is unlikely that xmit_lock protection is necessary here.
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1142 1143
	   (f.e. loopback and IP tunnels are clean ignoring statistics
	   counters.)
Linus Torvalds's avatar
Linus Torvalds committed
1144 1145 1146 1147 1148 1149
	   However, it is possible, that they rely on protection
	   made by us here.

	   Check this and shot the lock. It is not prone from deadlocks.
	   Either shot noqueue qdisc, it is even simpler 8)
	 */
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1150
	if (dev->flags & IFF_UP) {
Linus Torvalds's avatar
Linus Torvalds committed
1151 1152 1153
		int cpu = smp_processor_id();

		if (dev->xmit_lock_owner != cpu) {
Robert Love's avatar
Robert Love committed
1154 1155 1156 1157 1158
			/*
			 * The spin_lock effectivly does a preempt lock, but 
			 * we are about to drop that...
			 */
			preempt_disable();
Linus Torvalds's avatar
Linus Torvalds committed
1159 1160 1161
			spin_unlock(&dev->queue_lock);
			spin_lock(&dev->xmit_lock);
			dev->xmit_lock_owner = cpu;
Robert Love's avatar
Robert Love committed
1162
			preempt_enable();
Linus Torvalds's avatar
Linus Torvalds committed
1163 1164 1165

			if (!netif_queue_stopped(dev)) {
				if (netdev_nit)
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1166
					dev_queue_xmit_nit(skb, dev);
Linus Torvalds's avatar
Linus Torvalds committed
1167

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1168 1169
				rc = 0;
				if (!dev->hard_start_xmit(skb, dev)) {
Linus Torvalds's avatar
Linus Torvalds committed
1170 1171
					dev->xmit_lock_owner = -1;
					spin_unlock_bh(&dev->xmit_lock);
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1172
					goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1173 1174 1175 1176 1177
				}
			}
			dev->xmit_lock_owner = -1;
			spin_unlock_bh(&dev->xmit_lock);
			if (net_ratelimit())
1178
				printk(KERN_CRIT "Virtual device %s asks to "
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1179 1180
				       "queue packet!\n", dev->name);
			goto out_enetdown;
Linus Torvalds's avatar
Linus Torvalds committed
1181
		} else {
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1182 1183
			/* Recursion is detected! It is possible,
			 * unfortunately */
Linus Torvalds's avatar
Linus Torvalds committed
1184
			if (net_ratelimit())
1185
				printk(KERN_CRIT "Dead loop on virtual device "
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1186
				       "%s, fix it urgently!\n", dev->name);
Linus Torvalds's avatar
Linus Torvalds committed
1187 1188 1189
		}
	}
	spin_unlock_bh(&dev->queue_lock);
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1190 1191 1192
out_enetdown:
	rc = -ENETDOWN;
out_kfree_skb:
Linus Torvalds's avatar
Linus Torvalds committed
1193
	kfree_skb(skb);
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1194 1195
out:
	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
1196 1197 1198 1199 1200 1201 1202 1203
}


/*=======================================================================
			Receiver routines
  =======================================================================*/

int netdev_max_backlog = 300;
1204
int weight_p = 64;            /* old backlog weight */
Linus Torvalds's avatar
Linus Torvalds committed
1205 1206 1207 1208 1209 1210 1211 1212 1213
/* These numbers are selected based on intuition and some
 * experimentatiom, if you have more scientific way of doing this
 * please go ahead and fix things.
 */
int no_cong_thresh = 10;
int no_cong = 20;
int lo_cong = 100;
int mod_cong = 290;

1214
DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
Linus Torvalds's avatar
Linus Torvalds committed
1215 1216 1217 1218 1219


#ifdef CONFIG_NET_HW_FLOWCONTROL
atomic_t netdev_dropping = ATOMIC_INIT(0);
static unsigned long netdev_fc_mask = 1;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1220
unsigned long netdev_fc_xoff;
Linus Torvalds's avatar
Linus Torvalds committed
1221 1222 1223 1224 1225 1226
spinlock_t netdev_fc_lock = SPIN_LOCK_UNLOCKED;

static struct
{
	void (*stimul)(struct net_device *);
	struct net_device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
1227
} netdev_fc_slots[BITS_PER_LONG];
Linus Torvalds's avatar
Linus Torvalds committed
1228

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1229 1230
int netdev_register_fc(struct net_device *dev,
		       void (*stimul)(struct net_device *dev))
Linus Torvalds's avatar
Linus Torvalds committed
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
{
	int bit = 0;
	unsigned long flags;

	spin_lock_irqsave(&netdev_fc_lock, flags);
	if (netdev_fc_mask != ~0UL) {
		bit = ffz(netdev_fc_mask);
		netdev_fc_slots[bit].stimul = stimul;
		netdev_fc_slots[bit].dev = dev;
		set_bit(bit, &netdev_fc_mask);
		clear_bit(bit, &netdev_fc_xoff);
	}
	spin_unlock_irqrestore(&netdev_fc_lock, flags);
	return bit;
}

void netdev_unregister_fc(int bit)
{
	unsigned long flags;

	spin_lock_irqsave(&netdev_fc_lock, flags);
	if (bit > 0) {
		netdev_fc_slots[bit].stimul = NULL;
		netdev_fc_slots[bit].dev = NULL;
		clear_bit(bit, &netdev_fc_mask);
		clear_bit(bit, &netdev_fc_xoff);
	}
	spin_unlock_irqrestore(&netdev_fc_lock, flags);
}

static void netdev_wakeup(void)
{
	unsigned long xoff;

	spin_lock(&netdev_fc_lock);
	xoff = netdev_fc_xoff;
	netdev_fc_xoff = 0;
	while (xoff) {
		int i = ffz(~xoff);
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1270
		xoff &= ~(1 << i);
Linus Torvalds's avatar
Linus Torvalds committed
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
		netdev_fc_slots[i].stimul(netdev_fc_slots[i].dev);
	}
	spin_unlock(&netdev_fc_lock);
}
#endif

static void get_sample_stats(int cpu)
{
#ifdef RAND_LIE
	unsigned long rd;
	int rq;
#endif
1283 1284 1285
	struct softnet_data *sd = &per_cpu(softnet_data, cpu);
	int blog = sd->input_pkt_queue.qlen;
	int avg_blog = sd->avg_blog;
Linus Torvalds's avatar
Linus Torvalds committed
1286

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1287
	avg_blog = (avg_blog >> 1) + (blog >> 1);
Linus Torvalds's avatar
Linus Torvalds committed
1288 1289 1290

	if (avg_blog > mod_cong) {
		/* Above moderate congestion levels. */
1291
		sd->cng_level = NET_RX_CN_HIGH;
Linus Torvalds's avatar
Linus Torvalds committed
1292 1293 1294 1295
#ifdef RAND_LIE
		rd = net_random();
		rq = rd % netdev_max_backlog;
		if (rq < avg_blog) /* unlucky bastard */
1296
			sd->cng_level = NET_RX_DROP;
Linus Torvalds's avatar
Linus Torvalds committed
1297 1298
#endif
	} else if (avg_blog > lo_cong) {
1299
		sd->cng_level = NET_RX_CN_MOD;
Linus Torvalds's avatar
Linus Torvalds committed
1300 1301 1302 1303
#ifdef RAND_LIE
		rd = net_random();
		rq = rd % netdev_max_backlog;
			if (rq < avg_blog) /* unlucky bastard */
1304
				sd->cng_level = NET_RX_CN_HIGH;
Linus Torvalds's avatar
Linus Torvalds committed
1305
#endif
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1306
	} else if (avg_blog > no_cong)
1307
		sd->cng_level = NET_RX_CN_LOW;
Linus Torvalds's avatar
Linus Torvalds committed
1308
	else  /* no congestion */
1309
		sd->cng_level = NET_RX_SUCCESS;
Linus Torvalds's avatar
Linus Torvalds committed
1310

1311
	sd->avg_blog = avg_blog;
Linus Torvalds's avatar
Linus Torvalds committed
1312 1313 1314 1315 1316
}

#ifdef OFFLINE_SAMPLE
static void sample_queue(unsigned long dummy)
{
Michael Hayes's avatar
Michael Hayes committed
1317
/* 10 ms 0r 1ms -- i don't care -- JHS */
Linus Torvalds's avatar
Linus Torvalds committed
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
	int next_tick = 1;
	int cpu = smp_processor_id();

	get_sample_stats(cpu);
	next_tick += jiffies;
	mod_timer(&samp_timer, next_tick);
}
#endif


/**
 *	netif_rx	-	post buffer to the network code
 *	@skb: buffer to post
 *
 *	This function receives a packet from a device driver and queues it for
 *	the upper (protocol) levels to process.  It always succeeds. The buffer
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1334
 *	may be dropped during processing for congestion control or by the
Linus Torvalds's avatar
Linus Torvalds committed
1335
 *	protocol layers.
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1336
 *
Linus Torvalds's avatar
Linus Torvalds committed
1337
 *	return values:
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1338 1339 1340 1341 1342 1343
 *	NET_RX_SUCCESS	(no congestion)
 *	NET_RX_CN_LOW   (low congestion)
 *	NET_RX_CN_MOD   (moderate congestion)
 *	NET_RX_CN_HIGH  (high congestion)
 *	NET_RX_DROP     (packet was dropped)
 *
Linus Torvalds's avatar
Linus Torvalds committed
1344 1345 1346 1347
 */

int netif_rx(struct sk_buff *skb)
{
1348
	int this_cpu;
Linus Torvalds's avatar
Linus Torvalds committed
1349 1350 1351
	struct softnet_data *queue;
	unsigned long flags;

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1352
	if (!skb->stamp.tv_sec)
Linus Torvalds's avatar
Linus Torvalds committed
1353
		do_gettimeofday(&skb->stamp);
Linus Torvalds's avatar
Linus Torvalds committed
1354

1355 1356 1357
	/*
	 * The code is rearranged so that the path is the most
	 * short when CPU is congested, but is still operating.
Linus Torvalds's avatar
Linus Torvalds committed
1358 1359
	 */
	local_irq_save(flags);
1360
	this_cpu = smp_processor_id();
1361
	queue = &__get_cpu_var(softnet_data);
Linus Torvalds's avatar
Linus Torvalds committed
1362

1363
	__get_cpu_var(netdev_rx_stat).total++;
Linus Torvalds's avatar
Linus Torvalds committed
1364 1365 1366 1367 1368 1369 1370
	if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
		if (queue->input_pkt_queue.qlen) {
			if (queue->throttle)
				goto drop;

enqueue:
			dev_hold(skb->dev);
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1371
			__skb_queue_tail(&queue->input_pkt_queue, skb);
Linus Torvalds's avatar
Linus Torvalds committed
1372 1373 1374
#ifndef OFFLINE_SAMPLE
			get_sample_stats(this_cpu);
#endif
1375
			local_irq_restore(flags);
1376
			return queue->cng_level;
Linus Torvalds's avatar
Linus Torvalds committed
1377 1378 1379 1380 1381 1382 1383 1384 1385
		}

		if (queue->throttle) {
			queue->throttle = 0;
#ifdef CONFIG_NET_HW_FLOWCONTROL
			if (atomic_dec_and_test(&netdev_dropping))
				netdev_wakeup();
#endif
		}
1386 1387

		netif_rx_schedule(&queue->backlog_dev);
Linus Torvalds's avatar
Linus Torvalds committed
1388 1389 1390
		goto enqueue;
	}

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1391
	if (!queue->throttle) {
Linus Torvalds's avatar
Linus Torvalds committed
1392
		queue->throttle = 1;
1393
		__get_cpu_var(netdev_rx_stat).throttled++;
Linus Torvalds's avatar
Linus Torvalds committed
1394 1395 1396 1397 1398 1399
#ifdef CONFIG_NET_HW_FLOWCONTROL
		atomic_inc(&netdev_dropping);
#endif
	}

drop:
1400
	__get_cpu_var(netdev_rx_stat).dropped++;
Linus Torvalds's avatar
Linus Torvalds committed
1401 1402 1403 1404 1405 1406 1407 1408 1409
	local_irq_restore(flags);

	kfree_skb(skb);
	return NET_RX_DROP;
}

static __inline__ void skb_bond(struct sk_buff *skb)
{
	struct net_device *dev = skb->dev;
1410

1411 1412
	if (dev->master) {
		skb->real_dev = skb->dev;
Linus Torvalds's avatar
Linus Torvalds committed
1413
		skb->dev = dev->master;
1414
	}
Linus Torvalds's avatar
Linus Torvalds committed
1415 1416 1417 1418
}

static void net_tx_action(struct softirq_action *h)
{
1419
	struct softnet_data *sd = &__get_cpu_var(softnet_data);
Linus Torvalds's avatar
Linus Torvalds committed
1420

1421
	if (sd->completion_queue) {
Linus Torvalds's avatar
Linus Torvalds committed
1422 1423 1424
		struct sk_buff *clist;

		local_irq_disable();
1425 1426
		clist = sd->completion_queue;
		sd->completion_queue = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1427 1428
		local_irq_enable();

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1429
		while (clist) {
Linus Torvalds's avatar
Linus Torvalds committed
1430 1431 1432
			struct sk_buff *skb = clist;
			clist = clist->next;

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1433
			BUG_TRAP(!atomic_read(&skb->users));
Linus Torvalds's avatar
Linus Torvalds committed
1434 1435 1436 1437
			__kfree_skb(skb);
		}
	}

1438
	if (sd->output_queue) {
Linus Torvalds's avatar
Linus Torvalds committed
1439 1440 1441
		struct net_device *head;

		local_irq_disable();
1442 1443
		head = sd->output_queue;
		sd->output_queue = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1444 1445
		local_irq_enable();

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1446
		while (head) {
Linus Torvalds's avatar
Linus Torvalds committed
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
			struct net_device *dev = head;
			head = head->next_sched;

			smp_mb__before_clear_bit();
			clear_bit(__LINK_STATE_SCHED, &dev->state);

			if (spin_trylock(&dev->queue_lock)) {
				qdisc_run(dev);
				spin_unlock(&dev->queue_lock);
			} else {
				netif_schedule(dev);
			}
		}
	}
}

1463 1464 1465
static __inline__ int deliver_skb(struct sk_buff *skb,
				  struct packet_type *pt_prev, int last)
{
1466 1467
	atomic_inc(&skb->users);
	return pt_prev->func(skb, skb->dev, pt_prev);
1468 1469 1470
}


1471
#if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
1472
int (*br_handle_frame_hook)(struct sk_buff *skb);
Linus Torvalds's avatar
Linus Torvalds committed
1473

Linus Torvalds's avatar
Linus Torvalds committed
1474
static __inline__ int handle_bridge(struct sk_buff *skb,
Linus Torvalds's avatar
Linus Torvalds committed
1475 1476 1477
				     struct packet_type *pt_prev)
{
	int ret = NET_RX_DROP;
1478 1479
	if (pt_prev)
		ret = deliver_skb(skb, pt_prev, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1480 1481 1482 1483

	return ret;
}

1484
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1485

1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
static inline int __handle_bridge(struct sk_buff *skb,
			struct packet_type **pt_prev, int *ret)
{
#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
	if (skb->dev->br_port) {
		*ret = handle_bridge(skb, *pt_prev);
		if (br_handle_frame_hook(skb) == 0)
			return 1;

		*pt_prev = NULL;
	}
#endif
1498
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1499 1500
}

1501
int netif_receive_skb(struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
1502
{
1503 1504 1505
	struct packet_type *ptype, *pt_prev;
	int ret = NET_RX_DROP;
	unsigned short type = skb->protocol;
Linus Torvalds's avatar
Linus Torvalds committed
1506

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1507
	if (!skb->stamp.tv_sec)
1508
		do_gettimeofday(&skb->stamp);
Linus Torvalds's avatar
Linus Torvalds committed
1509

1510
	skb_bond(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1511

1512
	__get_cpu_var(netdev_rx_stat).total++;
Linus Torvalds's avatar
Linus Torvalds committed
1513 1514

#ifdef CONFIG_NET_FASTROUTE
1515
	if (skb->pkt_type == PACKET_FASTROUTE) {
1516
		__get_cpu_var(netdev_rx_stat).fastroute_deferred_out++;
1517 1518
		return dev_queue_xmit(skb);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1519
#endif
1520 1521 1522 1523

	skb->h.raw = skb->nh.raw = skb->data;

	pt_prev = NULL;
1524 1525
	rcu_read_lock();
	list_for_each_entry_rcu(ptype, &ptype_all, list) {
1526
		if (!ptype->dev || ptype->dev == skb->dev) {
1527 1528
			if (pt_prev) 
				ret = deliver_skb(skb, pt_prev, 0);
1529 1530 1531
			pt_prev = ptype;
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
1532

1533
	handle_diverter(skb);
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1534

1535 1536
	if (__handle_bridge(skb, &pt_prev, &ret))
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1537

1538
	list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type)&15], list) {
1539 1540
		if (ptype->type == type &&
		    (!ptype->dev || ptype->dev == skb->dev)) {
1541 1542
			if (pt_prev) 
				ret = deliver_skb(skb, pt_prev, 0);
1543 1544 1545
			pt_prev = ptype;
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
1546

Andrew Morton's avatar
Andrew Morton committed
1547
	if (pt_prev) {
1548
		ret = pt_prev->func(skb, skb->dev, pt_prev);
Andrew Morton's avatar
Andrew Morton committed
1549
	} else {
1550 1551 1552 1553 1554 1555
		kfree_skb(skb);
		/* Jamal, now you will not able to escape explaining
		 * me how you were going to use this. :-)
		 */
		ret = NET_RX_DROP;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1556

1557
out:
1558
	rcu_read_unlock();
1559 1560
	return ret;
}
Linus Torvalds's avatar
Linus Torvalds committed
1561

1562 1563 1564 1565
static int process_backlog(struct net_device *backlog_dev, int *budget)
{
	int work = 0;
	int quota = min(backlog_dev->quota, *budget);
1566
	struct softnet_data *queue = &__get_cpu_var(softnet_data);
1567 1568 1569 1570 1571 1572 1573 1574
	unsigned long start_time = jiffies;

	for (;;) {
		struct sk_buff *skb;
		struct net_device *dev;

		local_irq_disable();
		skb = __skb_dequeue(&queue->input_pkt_queue);
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1575
		if (!skb)
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
			goto job_done;
		local_irq_enable();

		dev = skb->dev;

		netif_receive_skb(skb);

		dev_put(dev);

		work++;

		if (work >= quota || jiffies - start_time > 1)
			break;
Linus Torvalds's avatar
Linus Torvalds committed
1589 1590

#ifdef CONFIG_NET_HW_FLOWCONTROL
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1591 1592
		if (queue->throttle &&
		    queue->input_pkt_queue.qlen < no_cong_thresh ) {
1593
			queue->throttle = 0;
1594 1595 1596 1597
			if (atomic_dec_and_test(&netdev_dropping)) {
				netdev_wakeup();
				break;
			}
Linus Torvalds's avatar
Linus Torvalds committed
1598 1599 1600 1601
		}
#endif
	}

1602 1603 1604 1605 1606 1607 1608 1609 1610
	backlog_dev->quota -= work;
	*budget -= work;
	return -1;

job_done:
	backlog_dev->quota -= work;
	*budget -= work;

	list_del(&backlog_dev->poll_list);
1611
	smp_mb__before_clear_bit();
1612
	netif_poll_enable(backlog_dev);
1613

Linus Torvalds's avatar
Linus Torvalds committed
1614 1615 1616 1617 1618 1619 1620 1621
	if (queue->throttle) {
		queue->throttle = 0;
#ifdef CONFIG_NET_HW_FLOWCONTROL
		if (atomic_dec_and_test(&netdev_dropping))
			netdev_wakeup();
#endif
	}
	local_irq_enable();
1622 1623
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
1624

1625 1626
static void net_rx_action(struct softirq_action *h)
{
1627
	struct softnet_data *queue = &__get_cpu_var(softnet_data);
1628 1629
	unsigned long start_time = jiffies;
	int budget = netdev_max_backlog;
Linus Torvalds's avatar
Linus Torvalds committed
1630

1631 1632
	
	preempt_disable();
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
	local_irq_disable();

	while (!list_empty(&queue->poll_list)) {
		struct net_device *dev;

		if (budget <= 0 || jiffies - start_time > 1)
			goto softnet_break;

		local_irq_enable();

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1643 1644
		dev = list_entry(queue->poll_list.next,
				 struct net_device, poll_list);
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658

		if (dev->quota <= 0 || dev->poll(dev, &budget)) {
			local_irq_disable();
			list_del(&dev->poll_list);
			list_add_tail(&dev->poll_list, &queue->poll_list);
			if (dev->quota < 0)
				dev->quota += dev->weight;
			else
				dev->quota = dev->weight;
		} else {
			dev_put(dev);
			local_irq_disable();
		}
	}
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1659
out:
1660
	local_irq_enable();
1661
	preempt_enable();
1662
	return;
Linus Torvalds's avatar
Linus Torvalds committed
1663

1664
softnet_break:
1665
	__get_cpu_var(netdev_rx_stat).time_squeeze++;
1666
	__raise_softirq_irqoff(NET_RX_SOFTIRQ);
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1667
	goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
}

static gifconf_func_t * gifconf_list [NPROTO];

/**
 *	register_gifconf	-	register a SIOCGIF handler
 *	@family: Address family
 *	@gifconf: Function handler
 *
 *	Register protocol dependent address dumping routines. The handler
 *	that is passed must not be freed or reused until it has been replaced
 *	by another handler.
 */
int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
{
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1683
	if (family >= NPROTO)
Linus Torvalds's avatar
Linus Torvalds committed
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
		return -EINVAL;
	gifconf_list[family] = gifconf;
	return 0;
}


/*
 *	Map an interface index to its name (SIOCGIFNAME)
 */

/*
 *	We need this ioctl for efficient implementation of the
 *	if_indextoname() function required by the IPv6 API.  Without
 *	it, we would have to search all the interfaces to find a
 *	match.  --pb
 */

static int dev_ifname(struct ifreq *arg)
{
	struct net_device *dev;
	struct ifreq ifr;

	/*
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1707
	 *	Fetch the caller's info block.
Linus Torvalds's avatar
Linus Torvalds committed
1708
	 */
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1709

Linus Torvalds's avatar
Linus Torvalds committed
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
	if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
		return -EFAULT;

	read_lock(&dev_base_lock);
	dev = __dev_get_by_index(ifr.ifr_ifindex);
	if (!dev) {
		read_unlock(&dev_base_lock);
		return -ENODEV;
	}

	strcpy(ifr.ifr_name, dev->name);
	read_unlock(&dev_base_lock);

	if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
		return -EFAULT;
	return 0;
}

/*
 *	Perform a SIOCGIFCONF call. This structure will change
 *	size eventually, and there is nothing I can do about it.
 *	Thus we will need a 'compatibility mode'.
 */

static int dev_ifconf(char *arg)
{
	struct ifconf ifc;
	struct net_device *dev;
	char *pos;
	int len;
	int total;
	int i;

	/*
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1744
	 *	Fetch the caller's info block.
Linus Torvalds's avatar
Linus Torvalds committed
1745
	 */
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1746

Linus Torvalds's avatar
Linus Torvalds committed
1747 1748 1749 1750 1751 1752 1753
	if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
		return -EFAULT;

	pos = ifc.ifc_buf;
	len = ifc.ifc_len;

	/*
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1754
	 *	Loop over the interfaces, and write an info block for each.
Linus Torvalds's avatar
Linus Torvalds committed
1755 1756 1757
	 */

	total = 0;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1758 1759
	for (dev = dev_base; dev; dev = dev->next) {
		for (i = 0; i < NPROTO; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
1760 1761
			if (gifconf_list[i]) {
				int done;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1762
				if (!pos)
Linus Torvalds's avatar
Linus Torvalds committed
1763
					done = gifconf_list[i](dev, NULL, 0);
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1764 1765 1766 1767
				else
					done = gifconf_list[i](dev, pos + total,
							       len - total);
				if (done < 0)
Linus Torvalds's avatar
Linus Torvalds committed
1768 1769 1770 1771 1772 1773 1774
					return -EFAULT;
				total += done;
			}
		}
  	}

	/*
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1775
	 *	All done.  Write the updated control block back to the caller.
Linus Torvalds's avatar
Linus Torvalds committed
1776 1777 1778
	 */
	ifc.ifc_len = total;

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1779
	/*
Linus Torvalds's avatar
Linus Torvalds committed
1780 1781
	 * 	Both BSD and Solaris return 0 here, so we do too.
	 */
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1782
	return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
Linus Torvalds's avatar
Linus Torvalds committed
1783 1784
}

1785
#ifdef CONFIG_PROC_FS
Linus Torvalds's avatar
Linus Torvalds committed
1786 1787 1788 1789
/*
 *	This is invoked by the /proc filesystem handler to display a device
 *	in detail.
 */
1790
static __inline__ struct net_device *dev_get_idx(loff_t pos)
1791 1792 1793
{
	struct net_device *dev;
	loff_t i;
Linus Torvalds's avatar
Linus Torvalds committed
1794

1795
	for (i = 0, dev = dev_base; dev && i < pos; ++i, dev = dev->next);
1796 1797 1798 1799

	return i == pos ? dev : NULL;
}

1800
void *dev_seq_start(struct seq_file *seq, loff_t *pos)
1801 1802
{
	read_lock(&dev_base_lock);
1803
	return *pos ? dev_get_idx(*pos - 1) : SEQ_START_TOKEN;
1804
}
Linus Torvalds's avatar
Linus Torvalds committed
1805

1806
void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1807
{
1808
	++*pos;
1809
	return v == SEQ_START_TOKEN ? dev_base : ((struct net_device *)v)->next;
1810 1811
}

1812
void dev_seq_stop(struct seq_file *seq, void *v)
1813 1814 1815 1816 1817
{
	read_unlock(&dev_base_lock);
}

static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
1818
{
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1819 1820
	struct net_device_stats *stats = dev->get_stats ? dev->get_stats(dev) :
							  NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1821
	if (stats)
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
		seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
				"%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
			   dev->name, stats->rx_bytes, stats->rx_packets,
			   stats->rx_errors,
			   stats->rx_dropped + stats->rx_missed_errors,
			   stats->rx_fifo_errors,
			   stats->rx_length_errors + stats->rx_over_errors +
			     stats->rx_crc_errors + stats->rx_frame_errors,
			   stats->rx_compressed, stats->multicast,
			   stats->tx_bytes, stats->tx_packets,
			   stats->tx_errors, stats->tx_dropped,
			   stats->tx_fifo_errors, stats->collisions,
			   stats->tx_carrier_errors +
			     stats->tx_aborted_errors +
			     stats->tx_window_errors +
			     stats->tx_heartbeat_errors,
			   stats->tx_compressed);
Linus Torvalds's avatar
Linus Torvalds committed
1839
	else
1840
		seq_printf(seq, "%6s: No statistics available.\n", dev->name);
Linus Torvalds's avatar
Linus Torvalds committed
1841 1842 1843
}

/*
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
1844 1845
 *	Called from the PROCfs module. This now uses the new arbitrary sized
 *	/proc/net interface to create /proc/net/dev
Linus Torvalds's avatar
Linus Torvalds committed
1846
 */
1847
static int dev_seq_show(struct seq_file *seq, void *v)
Linus Torvalds's avatar
Linus Torvalds committed
1848
{
1849
	if (v == SEQ_START_TOKEN)
1850 1851 1852 1853 1854
		seq_puts(seq, "Inter-|   Receive                            "
			      "                    |  Transmit\n"
			      " face |bytes    packets errs drop fifo frame "
			      "compressed multicast|bytes    packets errs "
			      "drop fifo colls carrier compressed\n");
1855 1856 1857
	else
		dev_seq_printf_stats(seq, v);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1858 1859
}

1860
static struct netif_rx_stats *softnet_get_online(loff_t *pos)
Linus Torvalds's avatar
Linus Torvalds committed
1861
{
1862
	struct netif_rx_stats *rc = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1863

1864 1865
	while (*pos < NR_CPUS)
	       	if (cpu_online(*pos)) {
1866
			rc = &per_cpu(netdev_rx_stat, *pos);
1867 1868 1869 1870 1871
			break;
		} else
			++*pos;
	return rc;
}
Linus Torvalds's avatar
Linus Torvalds committed
1872

1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
{
	return softnet_get_online(pos);
}

static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{
	++*pos;
	return softnet_get_online(pos);
}
Linus Torvalds's avatar
Linus Torvalds committed
1883

1884 1885 1886 1887 1888 1889 1890
static void softnet_seq_stop(struct seq_file *seq, void *v)
{
}

static int softnet_seq_show(struct seq_file *seq, void *v)
{
	struct netif_rx_stats *s = v;
Linus Torvalds's avatar
Linus Torvalds committed
1891

1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
	seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
		   s->total, s->dropped, s->time_squeeze, s->throttled,
		   s->fastroute_hit, s->fastroute_success, s->fastroute_defer,
		   s->fastroute_deferred_out,
#if 0
		   s->fastroute_latency_reduction
#else
		   s->cpu_collision
#endif
		  );
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1903 1904
}

1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
static struct seq_operations dev_seq_ops = {
	.start = dev_seq_start,
	.next  = dev_seq_next,
	.stop  = dev_seq_stop,
	.show  = dev_seq_show,
};

static int dev_seq_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &dev_seq_ops);
}

static struct file_operations dev_seq_fops = {
1918
	.owner	 = THIS_MODULE,
1919 1920 1921 1922 1923 1924
	.open    = dev_seq_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release,
};

1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
static struct seq_operations softnet_seq_ops = {
	.start = softnet_seq_start,
	.next  = softnet_seq_next,
	.stop  = softnet_seq_stop,
	.show  = softnet_seq_show,
};

static int softnet_seq_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &softnet_seq_ops);
}

static struct file_operations softnet_seq_fops = {
1938
	.owner	 = THIS_MODULE,
1939 1940 1941 1942 1943 1944
	.open    = softnet_seq_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release,
};

1945 1946 1947 1948 1949 1950
#ifdef WIRELESS_EXT
extern int wireless_proc_init(void);
#else
#define wireless_proc_init() 0
#endif

1951 1952 1953 1954
static int __init dev_proc_init(void)
{
	int rc = -ENOMEM;

1955
	if (!proc_net_fops_create("dev", S_IRUGO, &dev_seq_fops))
1956
		goto out;
1957
	if (!proc_net_fops_create("softnet_stat", S_IRUGO, &softnet_seq_fops))
1958
		goto out_dev;
1959 1960
	if (wireless_proc_init())
		goto out_softnet;
1961 1962 1963
	rc = 0;
out:
	return rc;
1964
out_softnet:
1965
	proc_net_remove("softnet_stat");
1966
out_dev:
1967
	proc_net_remove("dev");
1968
	goto out;
1969 1970 1971
}
#else
#define dev_proc_init() 0
Linus Torvalds's avatar
Linus Torvalds committed
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
#endif	/* CONFIG_PROC_FS */


/**
 *	netdev_set_master	-	set up master/slave pair
 *	@slave: slave device
 *	@master: new master device
 *
 *	Changes the master device of the slave. Pass %NULL to break the
 *	bonding. The caller must hold the RTNL semaphore. On a failure
 *	a negative errno code is returned. On success the reference counts
 *	are adjusted, %RTM_NEWLINK is sent to the routing socket and the
 *	function returns zero.
 */
int netdev_set_master(struct net_device *slave, struct net_device *master)
{
	struct net_device *old = slave->master;

	ASSERT_RTNL();

	if (master) {
		if (old)
			return -EBUSY;
		dev_hold(master);
	}

	slave->master = master;
1999 2000
	
	synchronize_net();
Linus Torvalds's avatar
Linus Torvalds committed
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030

	if (old)
		dev_put(old);

	if (master)
		slave->flags |= IFF_SLAVE;
	else
		slave->flags &= ~IFF_SLAVE;

	rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
	return 0;
}

/**
 *	dev_set_promiscuity	- update promiscuity count on a device
 *	@dev: device
 *	@inc: modifier
 *
 *	Add or remove promsicuity from a device. While the count in the device
 *	remains above zero the interface remains promiscuous. Once it hits zero
 *	the device reverts back to normal filtering operation. A negative inc
 *	value is used to drop promiscuity on the device.
 */
void dev_set_promiscuity(struct net_device *dev, int inc)
{
	unsigned short old_flags = dev->flags;

	dev->flags |= IFF_PROMISC;
	if ((dev->promiscuity += inc) == 0)
		dev->flags &= ~IFF_PROMISC;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2031
	if (dev->flags ^ old_flags) {
Linus Torvalds's avatar
Linus Torvalds committed
2032
#ifdef CONFIG_NET_FASTROUTE
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2033
		if (dev->flags & IFF_PROMISC) {
Linus Torvalds's avatar
Linus Torvalds committed
2034 2035 2036 2037 2038 2039 2040
			netdev_fastroute_obstacles++;
			dev_clear_fastroute(dev);
		} else
			netdev_fastroute_obstacles--;
#endif
		dev_mc_upload(dev);
		printk(KERN_INFO "device %s %s promiscuous mode\n",
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2041 2042
		       dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
		       					       "left");
Linus Torvalds's avatar
Linus Torvalds committed
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
	}
}

/**
 *	dev_set_allmulti	- update allmulti count on a device
 *	@dev: device
 *	@inc: modifier
 *
 *	Add or remove reception of all multicast frames to a device. While the
 *	count in the device remains above zero the interface remains listening
 *	to all interfaces. Once it hits zero the device reverts back to normal
 *	filtering operation. A negative @inc value is used to drop the counter
 *	when releasing a resource needing all multicasts.
 */

void dev_set_allmulti(struct net_device *dev, int inc)
{
	unsigned short old_flags = dev->flags;

	dev->flags |= IFF_ALLMULTI;
	if ((dev->allmulti += inc) == 0)
		dev->flags &= ~IFF_ALLMULTI;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2065
	if (dev->flags ^ old_flags)
Linus Torvalds's avatar
Linus Torvalds committed
2066 2067 2068
		dev_mc_upload(dev);
}

2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
unsigned dev_get_flags(const struct net_device *dev)
{
	unsigned flags;

	flags = (dev->flags & ~(IFF_PROMISC |
				IFF_ALLMULTI |
				IFF_RUNNING)) | 
		(dev->gflags & (IFF_PROMISC |
				IFF_ALLMULTI));

	if (netif_running(dev) && netif_carrier_ok(dev))
		flags |= IFF_RUNNING;

	return flags;
}

Linus Torvalds's avatar
Linus Torvalds committed
2085 2086 2087 2088 2089 2090 2091 2092 2093
int dev_change_flags(struct net_device *dev, unsigned flags)
{
	int ret;
	int old_flags = dev->flags;

	/*
	 *	Set the flags on our device.
	 */

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2094 2095 2096 2097 2098
	dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
			       IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
			       IFF_AUTOMEDIA)) |
		     (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
				    IFF_ALLMULTI));
Linus Torvalds's avatar
Linus Torvalds committed
2099 2100 2101

	/*
	 *	Load in the correct multicast list now the flags have changed.
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2102
	 */
Linus Torvalds's avatar
Linus Torvalds committed
2103 2104 2105 2106 2107 2108 2109 2110 2111 2112

	dev_mc_upload(dev);

	/*
	 *	Have we downed the interface. We handle IFF_UP ourselves
	 *	according to user attempts to set it, rather than blindly
	 *	setting it.
	 */

	ret = 0;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2113
	if ((old_flags ^ flags) & IFF_UP) {	/* Bit is different  ? */
Linus Torvalds's avatar
Linus Torvalds committed
2114 2115
		ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2116
		if (!ret)
Linus Torvalds's avatar
Linus Torvalds committed
2117 2118 2119
			dev_mc_upload(dev);
	}

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2120 2121 2122
	if (dev->flags & IFF_UP &&
	    ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
					  IFF_VOLATILE)))
Linus Torvalds's avatar
Linus Torvalds committed
2123 2124
		notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2125 2126
	if ((flags ^ dev->gflags) & IFF_PROMISC) {
		int inc = (flags & IFF_PROMISC) ? +1 : -1;
Linus Torvalds's avatar
Linus Torvalds committed
2127 2128 2129 2130 2131 2132 2133 2134
		dev->gflags ^= IFF_PROMISC;
		dev_set_promiscuity(dev, inc);
	}

	/* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
	   is important. Some (broken) drivers set IFF_PROMISC, when
	   IFF_ALLMULTI is requested not asking us and not reporting.
	 */
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2135 2136
	if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
		int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
Linus Torvalds's avatar
Linus Torvalds committed
2137 2138 2139 2140
		dev->gflags ^= IFF_ALLMULTI;
		dev_set_allmulti(dev, inc);
	}

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2141 2142
	if (old_flags ^ dev->flags)
		rtmsg_ifinfo(RTM_NEWLINK, dev, old_flags ^ dev->flags);
Linus Torvalds's avatar
Linus Torvalds committed
2143 2144 2145 2146

	return ret;
}

2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
int dev_set_mtu(struct net_device *dev, int new_mtu)
{
	int err;

	if (new_mtu == dev->mtu)
		return 0;

	/*	MTU must be positive.	 */
	if (new_mtu < 0)
		return -EINVAL;

	if (!netif_device_present(dev))
		return -ENODEV;

	err = 0;
	if (dev->change_mtu)
		err = dev->change_mtu(dev, new_mtu);
	else
		dev->mtu = new_mtu;
	if (!err && dev->flags & IFF_UP)
		notifier_call_chain(&netdev_chain,
				    NETDEV_CHANGEMTU, dev);
	return err;
}


Linus Torvalds's avatar
Linus Torvalds committed
2173
/*
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2174
 *	Perform the SIOCxIFxxx calls.
Linus Torvalds's avatar
Linus Torvalds committed
2175 2176 2177 2178
 */
static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
{
	int err;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2179
	struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
Linus Torvalds's avatar
Linus Torvalds committed
2180

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2181
	if (!dev)
Linus Torvalds's avatar
Linus Torvalds committed
2182 2183
		return -ENODEV;

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2184
	switch (cmd) {
Linus Torvalds's avatar
Linus Torvalds committed
2185
		case SIOCGIFFLAGS:	/* Get interface flags */
2186
			ifr->ifr_flags = dev_get_flags(dev);
Linus Torvalds's avatar
Linus Torvalds committed
2187 2188 2189 2190
			return 0;

		case SIOCSIFFLAGS:	/* Set interface flags */
			return dev_change_flags(dev, ifr->ifr_flags);
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2191 2192 2193

		case SIOCGIFMETRIC:	/* Get the metric on the interface
					   (currently unused) */
Linus Torvalds's avatar
Linus Torvalds committed
2194 2195
			ifr->ifr_metric = 0;
			return 0;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2196 2197 2198

		case SIOCSIFMETRIC:	/* Set the metric on the interface
					   (currently unused) */
Linus Torvalds's avatar
Linus Torvalds committed
2199
			return -EOPNOTSUPP;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2200

Linus Torvalds's avatar
Linus Torvalds committed
2201 2202 2203
		case SIOCGIFMTU:	/* Get the MTU of a device */
			ifr->ifr_mtu = dev->mtu;
			return 0;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2204

Linus Torvalds's avatar
Linus Torvalds committed
2205
		case SIOCSIFMTU:	/* Set the MTU of a device */
2206
			return dev_set_mtu(dev, ifr->ifr_mtu);
Linus Torvalds's avatar
Linus Torvalds committed
2207 2208

		case SIOCGIFHWADDR:
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2209
			memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
Roland Dreier's avatar
Roland Dreier committed
2210
			       min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2211
			ifr->ifr_hwaddr.sa_family = dev->type;
Linus Torvalds's avatar
Linus Torvalds committed
2212
			return 0;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2213

Linus Torvalds's avatar
Linus Torvalds committed
2214
		case SIOCSIFHWADDR:
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2215
			if (!dev->set_mac_address)
Linus Torvalds's avatar
Linus Torvalds committed
2216
				return -EOPNOTSUPP;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2217
			if (ifr->ifr_hwaddr.sa_family != dev->type)
Linus Torvalds's avatar
Linus Torvalds committed
2218 2219 2220 2221 2222
				return -EINVAL;
			if (!netif_device_present(dev))
				return -ENODEV;
			err = dev->set_mac_address(dev, &ifr->ifr_hwaddr);
			if (!err)
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2223 2224
				notifier_call_chain(&netdev_chain,
						    NETDEV_CHANGEADDR, dev);
Linus Torvalds's avatar
Linus Torvalds committed
2225
			return err;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2226

Linus Torvalds's avatar
Linus Torvalds committed
2227
		case SIOCSIFHWBROADCAST:
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2228
			if (ifr->ifr_hwaddr.sa_family != dev->type)
Linus Torvalds's avatar
Linus Torvalds committed
2229
				return -EINVAL;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2230
			memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
Roland Dreier's avatar
Roland Dreier committed
2231
			       min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2232 2233
			notifier_call_chain(&netdev_chain,
					    NETDEV_CHANGEADDR, dev);
Linus Torvalds's avatar
Linus Torvalds committed
2234 2235 2236
			return 0;

		case SIOCGIFMAP:
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2237 2238 2239 2240 2241 2242
			ifr->ifr_map.mem_start = dev->mem_start;
			ifr->ifr_map.mem_end   = dev->mem_end;
			ifr->ifr_map.base_addr = dev->base_addr;
			ifr->ifr_map.irq       = dev->irq;
			ifr->ifr_map.dma       = dev->dma;
			ifr->ifr_map.port      = dev->if_port;
Linus Torvalds's avatar
Linus Torvalds committed
2243
			return 0;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2244

Linus Torvalds's avatar
Linus Torvalds committed
2245 2246 2247 2248
		case SIOCSIFMAP:
			if (dev->set_config) {
				if (!netif_device_present(dev))
					return -ENODEV;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2249
				return dev->set_config(dev, &ifr->ifr_map);
Linus Torvalds's avatar
Linus Torvalds committed
2250 2251
			}
			return -EOPNOTSUPP;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2252

Linus Torvalds's avatar
Linus Torvalds committed
2253
		case SIOCADDMULTI:
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2254
			if (!dev->set_multicast_list ||
Linus Torvalds's avatar
Linus Torvalds committed
2255 2256 2257 2258
			    ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
				return -EINVAL;
			if (!netif_device_present(dev))
				return -ENODEV;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2259 2260
			dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
				   dev->addr_len, 1);
Linus Torvalds's avatar
Linus Torvalds committed
2261 2262 2263
			return 0;

		case SIOCDELMULTI:
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2264 2265
			if (!dev->set_multicast_list ||
			    ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
Linus Torvalds's avatar
Linus Torvalds committed
2266 2267 2268
				return -EINVAL;
			if (!netif_device_present(dev))
				return -ENODEV;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2269 2270
			dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
				      dev->addr_len, 1);
Linus Torvalds's avatar
Linus Torvalds committed
2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281
			return 0;

		case SIOCGIFINDEX:
			ifr->ifr_ifindex = dev->ifindex;
			return 0;

		case SIOCGIFTXQLEN:
			ifr->ifr_qlen = dev->tx_queue_len;
			return 0;

		case SIOCSIFTXQLEN:
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2282
			if (ifr->ifr_qlen < 0)
Linus Torvalds's avatar
Linus Torvalds committed
2283 2284 2285 2286 2287
				return -EINVAL;
			dev->tx_queue_len = ifr->ifr_qlen;
			return 0;

		case SIOCSIFNAME:
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2288
			if (dev->flags & IFF_UP)
Linus Torvalds's avatar
Linus Torvalds committed
2289
				return -EBUSY;
2290
			ifr->ifr_newname[IFNAMSIZ-1] = '\0';
Linus Torvalds's avatar
Linus Torvalds committed
2291 2292
			if (__dev_get_by_name(ifr->ifr_newname))
				return -EEXIST;
2293 2294 2295 2296 2297 2298 2299 2300 2301
			err = class_device_rename(&dev->class_dev, 
						  ifr->ifr_newname);
			if (!err) {
				strlcpy(dev->name, ifr->ifr_newname, IFNAMSIZ);

				notifier_call_chain(&netdev_chain,
						    NETDEV_CHANGENAME, dev);
			}
			return err;
Linus Torvalds's avatar
Linus Torvalds committed
2302 2303 2304 2305 2306 2307 2308 2309

		/*
		 *	Unknown or private ioctl
		 */

		default:
			if ((cmd >= SIOCDEVPRIVATE &&
			    cmd <= SIOCDEVPRIVATE + 15) ||
Linus Torvalds's avatar
Linus Torvalds committed
2310 2311 2312 2313 2314 2315
			    cmd == SIOCBONDENSLAVE ||
			    cmd == SIOCBONDRELEASE ||
			    cmd == SIOCBONDSETHWADDR ||
			    cmd == SIOCBONDSLAVEINFOQUERY ||
			    cmd == SIOCBONDINFOQUERY ||
			    cmd == SIOCBONDCHANGEACTIVE ||
Linus Torvalds's avatar
Linus Torvalds committed
2316 2317
			    cmd == SIOCGMIIPHY ||
			    cmd == SIOCGMIIREG ||
François Romieu's avatar
François Romieu committed
2318
			    cmd == SIOCSMIIREG ||
François Romieu's avatar
François Romieu committed
2319
			    cmd == SIOCWANDEV) {
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2320
				err = -EOPNOTSUPP;
Linus Torvalds's avatar
Linus Torvalds committed
2321
				if (dev->do_ioctl) {
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2322 2323 2324 2325 2326
					if (netif_device_present(dev))
						err = dev->do_ioctl(dev, ifr,
								    cmd);
					else
						err = -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
2327
				}
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2328 2329
			} else
				err = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
2330 2331

	}
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2332
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
}

/*
 *	This function handles all "interface"-type I/O control requests. The actual
 *	'doing' part of this is dev_ifsioc above.
 */

/**
 *	dev_ioctl	-	network device ioctl
 *	@cmd: command to issue
 *	@arg: pointer to a struct ifreq in user space
 *
 *	Issue ioctl functions to devices. This is normally called by the
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2346
 *	user space syscall interfaces but can sometimes be useful for
Linus Torvalds's avatar
Linus Torvalds committed
2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360
 *	other purposes. The return value is the return from the syscall if
 *	positive or a negative errno code on error.
 */

int dev_ioctl(unsigned int cmd, void *arg)
{
	struct ifreq ifr;
	int ret;
	char *colon;

	/* One special case: SIOCGIFCONF takes ifconf argument
	   and requires shared lock, because it sleeps writing
	   to user space.
	 */
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2361

Linus Torvalds's avatar
Linus Torvalds committed
2362 2363 2364 2365 2366 2367
	if (cmd == SIOCGIFCONF) {
		rtnl_shlock();
		ret = dev_ifconf((char *) arg);
		rtnl_shunlock();
		return ret;
	}
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2368
	if (cmd == SIOCGIFNAME)
Linus Torvalds's avatar
Linus Torvalds committed
2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
		return dev_ifname((struct ifreq *)arg);

	if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
		return -EFAULT;

	ifr.ifr_name[IFNAMSIZ-1] = 0;

	colon = strchr(ifr.ifr_name, ':');
	if (colon)
		*colon = 0;

	/*
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2381
	 *	See which interface the caller is talking about.
Linus Torvalds's avatar
Linus Torvalds committed
2382
	 */
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2383 2384

	switch (cmd) {
Linus Torvalds's avatar
Linus Torvalds committed
2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405
		/*
		 *	These ioctl calls:
		 *	- can be done by all.
		 *	- atomic and do not require locking.
		 *	- return a value
		 */
		case SIOCGIFFLAGS:
		case SIOCGIFMETRIC:
		case SIOCGIFMTU:
		case SIOCGIFHWADDR:
		case SIOCGIFSLAVE:
		case SIOCGIFMAP:
		case SIOCGIFINDEX:
		case SIOCGIFTXQLEN:
			dev_load(ifr.ifr_name);
			read_lock(&dev_base_lock);
			ret = dev_ifsioc(&ifr, cmd);
			read_unlock(&dev_base_lock);
			if (!ret) {
				if (colon)
					*colon = ':';
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2406 2407 2408
				if (copy_to_user(arg, &ifr,
						 sizeof(struct ifreq)))
					ret = -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
2409 2410 2411
			}
			return ret;

2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425
		case SIOCETHTOOL:
			dev_load(ifr.ifr_name);
			rtnl_lock();
			ret = dev_ethtool(&ifr);
			rtnl_unlock();
			if (!ret) {
				if (colon)
					*colon = ':';
				if (copy_to_user(arg, &ifr,
						 sizeof(struct ifreq)))
					ret = -EFAULT;
			}
			return ret;

Linus Torvalds's avatar
Linus Torvalds committed
2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436
		/*
		 *	These ioctl calls:
		 *	- require superuser power.
		 *	- require strict serialization.
		 *	- return a value
		 */
		case SIOCGMIIPHY:
		case SIOCGMIIREG:
			if (!capable(CAP_NET_ADMIN))
				return -EPERM;
			dev_load(ifr.ifr_name);
2437
			rtnl_lock();
Linus Torvalds's avatar
Linus Torvalds committed
2438 2439 2440 2441 2442
			ret = dev_ifsioc(&ifr, cmd);
			rtnl_unlock();
			if (!ret) {
				if (colon)
					*colon = ':';
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2443 2444 2445
				if (copy_to_user(arg, &ifr,
						 sizeof(struct ifreq)))
					ret = -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
2446 2447 2448
			}
			return ret;

Linus Torvalds's avatar
Linus Torvalds committed
2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465
		/*
		 *	These ioctl calls:
		 *	- require superuser power.
		 *	- require strict serialization.
		 *	- do not return a value
		 */
		case SIOCSIFFLAGS:
		case SIOCSIFMETRIC:
		case SIOCSIFMTU:
		case SIOCSIFMAP:
		case SIOCSIFHWADDR:
		case SIOCSIFSLAVE:
		case SIOCADDMULTI:
		case SIOCDELMULTI:
		case SIOCSIFHWBROADCAST:
		case SIOCSIFTXQLEN:
		case SIOCSIFNAME:
Linus Torvalds's avatar
Linus Torvalds committed
2466
		case SIOCSMIIREG:
Linus Torvalds's avatar
Linus Torvalds committed
2467 2468 2469 2470 2471 2472
		case SIOCBONDENSLAVE:
		case SIOCBONDRELEASE:
		case SIOCBONDSETHWADDR:
		case SIOCBONDSLAVEINFOQUERY:
		case SIOCBONDINFOQUERY:
		case SIOCBONDCHANGEACTIVE:
Linus Torvalds's avatar
Linus Torvalds committed
2473 2474 2475
			if (!capable(CAP_NET_ADMIN))
				return -EPERM;
			dev_load(ifr.ifr_name);
2476
			rtnl_lock();
Linus Torvalds's avatar
Linus Torvalds committed
2477 2478 2479
			ret = dev_ifsioc(&ifr, cmd);
			rtnl_unlock();
			return ret;
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2480

Linus Torvalds's avatar
Linus Torvalds committed
2481
		case SIOCGIFMEM:
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2482 2483
			/* Get the per device memory space. We can add this but
			 * currently do not support it */
Linus Torvalds's avatar
Linus Torvalds committed
2484
		case SIOCSIFMEM:
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2485 2486
			/* Set the per device memory buffer space.
			 * Not applicable in our case */
Linus Torvalds's avatar
Linus Torvalds committed
2487 2488 2489 2490 2491
		case SIOCSIFLINK:
			return -EINVAL;

		/*
		 *	Unknown or private ioctl.
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2492
		 */
Linus Torvalds's avatar
Linus Torvalds committed
2493
		default:
François Romieu's avatar
François Romieu committed
2494
			if (cmd == SIOCWANDEV ||
François Romieu's avatar
François Romieu committed
2495 2496
			    (cmd >= SIOCDEVPRIVATE &&
			     cmd <= SIOCDEVPRIVATE + 15)) {
Linus Torvalds's avatar
Linus Torvalds committed
2497
				dev_load(ifr.ifr_name);
2498
				rtnl_lock();
Linus Torvalds's avatar
Linus Torvalds committed
2499 2500
				ret = dev_ifsioc(&ifr, cmd);
				rtnl_unlock();
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2501 2502 2503
				if (!ret && copy_to_user(arg, &ifr,
							 sizeof(struct ifreq)))
					ret = -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
2504 2505 2506 2507 2508 2509 2510 2511
				return ret;
			}
#ifdef WIRELESS_EXT
			/* Take care of Wireless Extensions */
			if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
				/* If command is `set a parameter', or
				 * `get the encoding parameters', check if
				 * the user has the right to do it */
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2512 2513
				if (IW_IS_SET(cmd) || cmd == SIOCGIWENCODE) {
					if (!capable(CAP_NET_ADMIN))
Linus Torvalds's avatar
Linus Torvalds committed
2514 2515 2516
						return -EPERM;
				}
				dev_load(ifr.ifr_name);
2517
				rtnl_lock();
Linus Torvalds's avatar
Linus Torvalds committed
2518 2519
				/* Follow me in net/core/wireless.c */
				ret = wireless_process_ioctl(&ifr, cmd);
Linus Torvalds's avatar
Linus Torvalds committed
2520 2521
				rtnl_unlock();
				if (!ret && IW_IS_GET(cmd) &&
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2522 2523 2524
				    copy_to_user(arg, &ifr,
					    	 sizeof(struct ifreq)))
					ret = -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544
				return ret;
			}
#endif	/* WIRELESS_EXT */
			return -EINVAL;
	}
}


/**
 *	dev_new_index	-	allocate an ifindex
 *
 *	Returns a suitable unique value for a new device interface
 *	number.  The caller must hold the rtnl semaphore or the
 *	dev_base_lock to be sure it remains unique.
 */
int dev_new_index(void)
{
	static int ifindex;
	for (;;) {
		if (++ifindex <= 0)
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2545 2546
			ifindex = 1;
		if (!__dev_get_by_index(ifindex))
Linus Torvalds's avatar
Linus Torvalds committed
2547 2548 2549 2550 2551 2552
			return ifindex;
	}
}

static int dev_boot_phase = 1;

2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563
/* Delayed registration/unregisteration */
static spinlock_t net_todo_list_lock = SPIN_LOCK_UNLOCKED;
static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list);

static inline void net_set_todo(struct net_device *dev)
{
	spin_lock(&net_todo_list_lock);
	list_add_tail(&dev->todo_list, &net_todo_list);
	spin_unlock(&net_todo_list_lock);
}

Linus Torvalds's avatar
Linus Torvalds committed
2564 2565 2566
/**
 *	register_netdevice	- register a network device
 *	@dev: device to register
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2567
 *
Linus Torvalds's avatar
Linus Torvalds committed
2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586
 *	Take a completed network device structure and add it to the kernel
 *	interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
 *	chain. 0 is returned on success. A negative errno code is returned
 *	on a failure to set up the device, or if the name is a duplicate.
 *
 *	Callers must hold the rtnl semaphore.  See the comment at the
 *	end of Space.c for details about the locking.  You may want
 *	register_netdev() instead of this.
 *
 *	BUGS:
 *	The locking appears insufficient to guarantee two parallel registers
 *	will not get the same name.
 */

int register_netdevice(struct net_device *dev)
{
	struct net_device *d, **dp;
	int ret;

2587
	BUG_ON(dev_boot_phase);
2588
	ASSERT_RTNL();
2589

2590
	/* When net_device's are persistent, this will be fatal. */
2591
	BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
2592

Linus Torvalds's avatar
Linus Torvalds committed
2593 2594 2595 2596
	spin_lock_init(&dev->queue_lock);
	spin_lock_init(&dev->xmit_lock);
	dev->xmit_lock_owner = -1;
#ifdef CONFIG_NET_FASTROUTE
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2597
	dev->fastpath_lock = RW_LOCK_UNLOCKED;
Linus Torvalds's avatar
Linus Torvalds committed
2598 2599 2600 2601
#endif

	ret = alloc_divert_blk(dev);
	if (ret)
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2602 2603
		goto out;

Linus Torvalds's avatar
Linus Torvalds committed
2604 2605 2606
	dev->iflink = -1;

	/* Init, if this function is available */
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2607 2608 2609
	ret = -EIO;
	if (dev->init && dev->init(dev))
		goto out_err;
Linus Torvalds's avatar
Linus Torvalds committed
2610 2611 2612 2613 2614 2615

	dev->ifindex = dev_new_index();
	if (dev->iflink == -1)
		dev->iflink = dev->ifindex;

	/* Check for existence, and append to tail of chain */
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2616 2617 2618 2619
	ret = -EEXIST;
	for (dp = &dev_base; (d = *dp) != NULL; dp = &d->next) {
		if (d == dev || !strcmp(d->name, dev->name))
			goto out_err;
Linus Torvalds's avatar
Linus Torvalds committed
2620
	}
2621
	
2622 2623 2624 2625 2626 2627 2628 2629 2630 2631
	/* Fix illegal SG+CSUM combinations. */
	if ((dev->features & NETIF_F_SG) &&
	    !(dev->features & (NETIF_F_IP_CSUM |
			       NETIF_F_NO_CSUM |
			       NETIF_F_HW_CSUM))) {
		printk("%s: Dropping NETIF_F_SG since no checksum feature.\n",
		       dev->name);
		dev->features &= ~NETIF_F_SG;
	}

Linus Torvalds's avatar
Linus Torvalds committed
2632 2633 2634 2635 2636
	/*
	 *	nil rebuild_header routine,
	 *	that should be never called and used as just bug trap.
	 */

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2637
	if (!dev->rebuild_header)
Linus Torvalds's avatar
Linus Torvalds committed
2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651
		dev->rebuild_header = default_rebuild_header;

	/*
	 *	Default initial state at registry is that the
	 *	device is present.
	 */

	set_bit(__LINK_STATE_PRESENT, &dev->state);

	dev->next = NULL;
	dev_init_scheduler(dev);
	write_lock_bh(&dev_base_lock);
	*dp = dev;
	dev_hold(dev);
2652
	dev->reg_state = NETREG_REGISTERING;
Linus Torvalds's avatar
Linus Torvalds committed
2653 2654 2655 2656 2657
	write_unlock_bh(&dev_base_lock);

	/* Notify protocols, that a new device appeared. */
	notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);

2658 2659
	/* Finish registration after unlock */
	net_set_todo(dev);
2660
	ret = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2661

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2662 2663 2664 2665 2666
out:
	return ret;
out_err:
	free_divert_blk(dev);
	goto out;
Linus Torvalds's avatar
Linus Torvalds committed
2667 2668
}

2669 2670
/*
 * netdev_wait_allrefs - wait until all references are gone.
Linus Torvalds's avatar
Linus Torvalds committed
2671
 *
2672 2673 2674 2675 2676 2677 2678
 * This is called when unregistering network devices.
 *
 * Any protocol or device that holds a reference should register
 * for netdevice notification, and cleanup and put back the
 * reference if they receive an UNREGISTER event.
 * We can get stuck here if buggy protocols don't correctly
 * call dev_put. 
Linus Torvalds's avatar
Linus Torvalds committed
2679
 */
2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725
static void netdev_wait_allrefs(struct net_device *dev)
{
	unsigned long rebroadcast_time, warning_time;

	rebroadcast_time = warning_time = jiffies;
	while (atomic_read(&dev->refcnt) != 0) {
		if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
			rtnl_shlock();
			rtnl_exlock();

			/* Rebroadcast unregister notification */
			notifier_call_chain(&netdev_chain,
					    NETDEV_UNREGISTER, dev);

			if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
				     &dev->state)) {
				/* We must not have linkwatch events
				 * pending on unregister. If this
				 * happens, we simply run the queue
				 * unscheduled, resulting in a noop
				 * for this device.
				 */
				linkwatch_run_queue();
			}

			rtnl_exunlock();
			rtnl_shunlock();

			rebroadcast_time = jiffies;
		}

		current->state = TASK_INTERRUPTIBLE;
		schedule_timeout(HZ / 4);

		if (time_after(jiffies, warning_time + 10 * HZ)) {
			printk(KERN_EMERG "unregister_netdevice: "
			       "waiting for %s to become free. Usage "
			       "count = %d\n",
			       dev->name, atomic_read(&dev->refcnt));
			warning_time = jiffies;
		}
	}
}

/* The sequence is:
 *
2726 2727
 *	rtnl_lock();
 *	...
2728 2729
 *	register_netdevice(x1);
 *	register_netdevice(x2);
2730 2731 2732
 *	...
 *	unregister_netdevice(y1);
 *	unregister_netdevice(y2);
2733 2734
 *      ...
 *	rtnl_unlock();
2735 2736
 *	free_netdev(y1);
 *	free_netdev(y2);
2737 2738
 *
 * We are invoked by rtnl_unlock() after it drops the semaphore.
2739 2740 2741
 * This allows us to deal with problems:
 * 1) We can create/delete sysfs objects which invoke hotplug
 *    without deadlocking with linkwatch via keventd.
2742 2743 2744
 * 2) Since we run with the RTNL semaphore not held, we can sleep
 *    safely in order to wait for the netdev refcnt to drop to zero.
 */
2745
static DECLARE_MUTEX(net_todo_run_mutex);
2746
void netdev_run_todo(void)
2747
{
2748
	struct list_head list = LIST_HEAD_INIT(list);
2749

2750 2751 2752 2753 2754
	/* Safe outside mutex since we only care about entries that
	 * this cpu put into queue while under RTNL.
	 */
	if (list_empty(&net_todo_list))
		return;
2755

2756 2757
	/* Need to guard against multiple cpu's getting out of order. */
	down(&net_todo_run_mutex);
2758

2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773
	/* Snapshot list, allow later requests */
	spin_lock(&net_todo_list_lock);
	list_splice_init(&net_todo_list, &list);
	spin_unlock(&net_todo_list_lock);
		
	while (!list_empty(&list)) {
		struct net_device *dev
			= list_entry(list.next, struct net_device, todo_list);
		list_del(&dev->todo_list);

		switch(dev->reg_state) {
		case NETREG_REGISTERING:
			netdev_register_sysfs(dev);
			dev->reg_state = NETREG_REGISTERED;
			break;
2774

2775
		case NETREG_UNREGISTERING:
2776
			netdev_unregister_sysfs(dev);
2777
			dev->reg_state = NETREG_UNREGISTERED;
2778

2779
			netdev_wait_allrefs(dev);
2780 2781

			/* paranoia */
2782
			BUG_ON(atomic_read(&dev->refcnt));
2783 2784 2785 2786 2787 2788 2789 2790 2791 2792
			BUG_TRAP(!dev->ip_ptr);
			BUG_TRAP(!dev->ip6_ptr);
			BUG_TRAP(!dev->dn_ptr);


			/* It must be the very last action, 
			 * after this 'dev' may point to freed up memory.
			 */
			if (dev->destructor)
				dev->destructor(dev);
2793
			break;
2794

2795 2796 2797 2798 2799
		default:
			printk(KERN_ERR "network todo '%s' but state %d\n",
			       dev->name, dev->reg_state);
			break;
		}
2800
	}
2801 2802

	up(&net_todo_run_mutex);
2803 2804
}

2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827
/**
 *	free_netdev - free network device
 *	@dev: device
 *
 *	This function does the last stage of destroying an allocated device 
 * 	interface. The reference to the device object is released.  
 *	If this is the last reference then it will be freed.
 */
void free_netdev(struct net_device *dev)
{
	/*  Compatiablity with error handling in drivers */
	if (dev->reg_state == NETREG_UNINITIALIZED) {
		kfree(dev);
		return;
	}

	BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
	dev->reg_state = NETREG_RELEASED;

	/* will free via class release */
	class_device_put(&dev->class_dev);
}
 
2828 2829 2830
/* Synchronize with packet receive processing. */
void synchronize_net(void) 
{
2831 2832
	might_sleep();
	synchronize_kernel();
2833 2834
}

Linus Torvalds's avatar
Linus Torvalds committed
2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851
/**
 *	unregister_netdevice - remove device from the kernel
 *	@dev: device
 *
 *	This function shuts down a device interface and removes it
 *	from the kernel tables. On success 0 is returned, on a failure
 *	a negative errno code is returned.
 *
 *	Callers must hold the rtnl semaphore.  See the comment at the
 *	end of Space.c for details about the locking.  You may want
 *	unregister_netdev() instead of this.
 */

int unregister_netdevice(struct net_device *dev)
{
	struct net_device *d, **dp;

2852
	BUG_ON(dev_boot_phase);
2853
	ASSERT_RTNL();
2854

2855 2856 2857 2858 2859 2860 2861 2862 2863
	/* Some devices call without registering for initialization unwind. */
	if (dev->reg_state == NETREG_UNINITIALIZED) {
		printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
				  "was registered\n", dev->name, dev);
		return -ENODEV;
	}

	BUG_ON(dev->reg_state != NETREG_REGISTERED);

Linus Torvalds's avatar
Linus Torvalds committed
2864 2865 2866 2867 2868
	/* If device is running, close it first. */
	if (dev->flags & IFF_UP)
		dev_close(dev);

	/* And unlink it from device chain. */
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2869
	for (dp = &dev_base; (d = *dp) != NULL; dp = &d->next) {
Linus Torvalds's avatar
Linus Torvalds committed
2870 2871 2872 2873 2874 2875 2876
		if (d == dev) {
			write_lock_bh(&dev_base_lock);
			*dp = d->next;
			write_unlock_bh(&dev_base_lock);
			break;
		}
	}
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2877
	if (!d) {
2878 2879
		printk(KERN_ERR "unregister net_device: '%s' not found\n",
		       dev->name);
Linus Torvalds's avatar
Linus Torvalds committed
2880 2881 2882
		return -ENODEV;
	}

2883 2884
	dev->reg_state = NETREG_UNREGISTERING;

2885
	synchronize_net();
2886

Linus Torvalds's avatar
Linus Torvalds committed
2887
#ifdef CONFIG_NET_FASTROUTE
2888
	dev_clear_fastroute(dev);
Linus Torvalds's avatar
Linus Torvalds committed
2889 2890
#endif

2891 2892
	/* Shutdown queueing discipline. */
	dev_shutdown(dev);
2893

2894 2895 2896 2897 2898 2899 2900 2901 2902 2903
	
	/* Notify protocols, that we are about to destroy
	   this device. They should clean all the things.
	*/
	notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
	
	/*
	 *	Flush the multicast chain
	 */
	dev_mc_discard(dev);
Linus Torvalds's avatar
Linus Torvalds committed
2904 2905 2906 2907 2908

	if (dev->uninit)
		dev->uninit(dev);

	/* Notifier chain MUST detach us from master device. */
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2909
	BUG_TRAP(!dev->master);
Linus Torvalds's avatar
Linus Torvalds committed
2910 2911 2912

	free_divert_blk(dev);

2913 2914
	/* Finish processing unregister after unlock */
	net_set_todo(dev);
2915

Linus Torvalds's avatar
Linus Torvalds committed
2916 2917 2918 2919 2920 2921 2922
	dev_put(dev);
	return 0;
}


/*
 *	Initialize the DEV module. At boot time this walks the device list and
Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
2923
 *	unhooks any devices that fail to initialise (normally hardware not
Linus Torvalds's avatar
Linus Torvalds committed
2924 2925 2926 2927 2928
 *	present) and leaves us with a valid list of present and active devices.
 *
 */

/*
2929 2930
 *       This is called single threaded during boot, so no need
 *       to take the rtnl semaphore.
Linus Torvalds's avatar
Linus Torvalds committed
2931
 */
2932
static int __init net_dev_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
2933
{
2934
	int i, rc = -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
2935

2936
	BUG_ON(!dev_boot_phase);
2937

2938 2939 2940
	if (dev_proc_init())
		goto out;

2941 2942
	if (netdev_sysfs_init())
		goto out;
2943

2944 2945 2946 2947
	INIT_LIST_HEAD(&ptype_all);
	for (i = 0; i < 16; i++) 
		INIT_LIST_HEAD(&ptype_base[i]);

Linus Torvalds's avatar
Linus Torvalds committed
2948 2949 2950 2951 2952 2953 2954
	/*
	 *	Initialise the packet receive queues.
	 */

	for (i = 0; i < NR_CPUS; i++) {
		struct softnet_data *queue;

2955
		queue = &per_cpu(softnet_data, i);
Linus Torvalds's avatar
Linus Torvalds committed
2956 2957 2958 2959 2960
		skb_queue_head_init(&queue->input_pkt_queue);
		queue->throttle = 0;
		queue->cng_level = 0;
		queue->avg_blog = 10; /* arbitrary non-zero */
		queue->completion_queue = NULL;
2961 2962 2963 2964 2965
		INIT_LIST_HEAD(&queue->poll_list);
		set_bit(__LINK_STATE_START, &queue->backlog_dev.state);
		queue->backlog_dev.weight = weight_p;
		queue->backlog_dev.poll = process_backlog;
		atomic_set(&queue->backlog_dev.refcnt, 1);
Linus Torvalds's avatar
Linus Torvalds committed
2966
	}
2967

Linus Torvalds's avatar
Linus Torvalds committed
2968 2969 2970 2971 2972 2973 2974
#ifdef OFFLINE_SAMPLE
	samp_timer.expires = jiffies + (10 * HZ);
	add_timer(&samp_timer);
#endif

	dev_boot_phase = 0;

2975 2976
	probe_old_netdevs();

Linus Torvalds's avatar
Linus Torvalds committed
2977 2978 2979 2980 2981 2982
	open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
	open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);

	dst_init();
	dev_mcast_init();

Linus Torvalds's avatar
Linus Torvalds committed
2983 2984 2985
#ifdef CONFIG_NET_SCHED
	pktsched_init();
#endif
2986 2987 2988
	rc = 0;
out:
	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
2989 2990
}

2991
subsys_initcall(net_dev_init);
2992 2993 2994 2995 2996 2997 2998 2999 3000

EXPORT_SYMBOL(__dev_get);
EXPORT_SYMBOL(__dev_get_by_flags);
EXPORT_SYMBOL(__dev_get_by_index);
EXPORT_SYMBOL(__dev_get_by_name);
EXPORT_SYMBOL(__dev_remove_pack);
EXPORT_SYMBOL(__skb_linearize);
EXPORT_SYMBOL(call_netdevice_notifiers);
EXPORT_SYMBOL(dev_add_pack);
3001
EXPORT_SYMBOL(__dev_alloc);
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049
EXPORT_SYMBOL(dev_alloc_name);
EXPORT_SYMBOL(dev_close);
EXPORT_SYMBOL(dev_get_by_flags);
EXPORT_SYMBOL(dev_get_by_index);
EXPORT_SYMBOL(dev_get_by_name);
EXPORT_SYMBOL(dev_getbyhwaddr);
EXPORT_SYMBOL(dev_ioctl);
EXPORT_SYMBOL(dev_new_index);
EXPORT_SYMBOL(dev_open);
EXPORT_SYMBOL(dev_queue_xmit);
EXPORT_SYMBOL(dev_queue_xmit_nit);
EXPORT_SYMBOL(dev_remove_pack);
EXPORT_SYMBOL(dev_set_allmulti);
EXPORT_SYMBOL(dev_set_promiscuity);
EXPORT_SYMBOL(free_netdev);
EXPORT_SYMBOL(netdev_boot_setup_check);
EXPORT_SYMBOL(netdev_set_master);
EXPORT_SYMBOL(netdev_state_change);
EXPORT_SYMBOL(netif_receive_skb);
EXPORT_SYMBOL(netif_rx);
EXPORT_SYMBOL(register_gifconf);
EXPORT_SYMBOL(register_netdevice);
EXPORT_SYMBOL(register_netdevice_notifier);
EXPORT_SYMBOL(skb_checksum_help);
EXPORT_SYMBOL(synchronize_net);
EXPORT_SYMBOL(unregister_netdevice);
EXPORT_SYMBOL(unregister_netdevice_notifier);

#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
EXPORT_SYMBOL(br_handle_frame_hook);
#endif
/* for 801q VLAN support */
#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
EXPORT_SYMBOL(dev_change_flags);
#endif
#ifdef CONFIG_KMOD
EXPORT_SYMBOL(dev_load);
#endif
#ifdef CONFIG_NET_HW_FLOWCONTROL
EXPORT_SYMBOL(netdev_dropping);
EXPORT_SYMBOL(netdev_fc_xoff);
EXPORT_SYMBOL(netdev_register_fc);
EXPORT_SYMBOL(netdev_unregister_fc);
#endif
#ifdef CONFIG_NET_FASTROUTE
EXPORT_SYMBOL(netdev_fastroute);
EXPORT_SYMBOL(netdev_fastroute_obstacles);
#endif
3050 3051

EXPORT_PER_CPU_SYMBOL(softnet_data);