blob: 68d8df0992abfd425c517fc8b9df3d6a40fe71ff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NET3 Protocol independent device support routines.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Derived from the non IP parts of dev.c 1.0.19
Jesper Juhl02c30a82005-05-05 16:16:16 -070010 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Mark Evans, <evansmp@uhura.aston.ac.uk>
13 *
14 * Additional Authors:
15 * Florian la Roche <rzsfl@rz.uni-sb.de>
16 * Alan Cox <gw4pts@gw4pts.ampr.org>
17 * David Hinds <dahinds@users.sourceforge.net>
18 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
19 * Adam Sulmicki <adam@cfar.umd.edu>
20 * Pekka Riikonen <priikone@poesidon.pspt.fi>
21 *
22 * Changes:
23 * D.J. Barrow : Fixed bug where dev->refcnt gets set
24 * to 2 if register_netdev gets called
25 * before net_dev_init & also removed a
26 * few lines of code in the process.
27 * Alan Cox : device private ioctl copies fields back.
28 * Alan Cox : Transmit queue code does relevant
29 * stunts to keep the queue safe.
30 * Alan Cox : Fixed double lock.
31 * Alan Cox : Fixed promisc NULL pointer trap
32 * ???????? : Support the full private ioctl range
33 * Alan Cox : Moved ioctl permission check into
34 * drivers
35 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
36 * Alan Cox : 100 backlog just doesn't cut it when
37 * you start doing multicast video 8)
38 * Alan Cox : Rewrote net_bh and list manager.
39 * Alan Cox : Fix ETH_P_ALL echoback lengths.
40 * Alan Cox : Took out transmit every packet pass
41 * Saved a few bytes in the ioctl handler
42 * Alan Cox : Network driver sets packet type before
43 * calling netif_rx. Saves a function
44 * call a packet.
45 * Alan Cox : Hashed net_bh()
46 * Richard Kooijman: Timestamp fixes.
47 * Alan Cox : Wrong field in SIOCGIFDSTADDR
48 * Alan Cox : Device lock protection.
49 * Alan Cox : Fixed nasty side effect of device close
50 * changes.
51 * Rudi Cilibrasi : Pass the right thing to
52 * set_mac_address()
53 * Dave Miller : 32bit quantity for the device lock to
54 * make it work out on a Sparc.
55 * Bjorn Ekwall : Added KERNELD hack.
56 * Alan Cox : Cleaned up the backlog initialise.
57 * Craig Metz : SIOCGIFCONF fix if space for under
58 * 1 device.
59 * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
60 * is no device open function.
61 * Andi Kleen : Fix error reporting for SIOCGIFCONF
62 * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
63 * Cyrus Durgin : Cleaned for KMOD
64 * Adam Sulmicki : Bug Fix : Network Device Unload
65 * A network device unload needs to purge
66 * the backlog queue.
67 * Paul Rusty Russell : SIOCSIFNAME
68 * Pekka Riikonen : Netdev boot-time settings code
69 * Andrew Morton : Make unregister_netdevice wait
70 * indefinitely on dev->refcnt
71 * J Hadi Salim : - Backlog queue sampling
72 * - netif_rx() feedback
73 */
74
75#include <asm/uaccess.h>
76#include <asm/system.h>
77#include <linux/bitops.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080078#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/cpu.h>
80#include <linux/types.h>
81#include <linux/kernel.h>
82#include <linux/sched.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080083#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/string.h>
85#include <linux/mm.h>
86#include <linux/socket.h>
87#include <linux/sockios.h>
88#include <linux/errno.h>
89#include <linux/interrupt.h>
90#include <linux/if_ether.h>
91#include <linux/netdevice.h>
92#include <linux/etherdevice.h>
93#include <linux/notifier.h>
94#include <linux/skbuff.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020095#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <net/sock.h>
97#include <linux/rtnetlink.h>
98#include <linux/proc_fs.h>
99#include <linux/seq_file.h>
100#include <linux/stat.h>
101#include <linux/if_bridge.h>
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700102#include <linux/if_macvlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#include <net/dst.h>
104#include <net/pkt_sched.h>
105#include <net/checksum.h>
106#include <linux/highmem.h>
107#include <linux/init.h>
108#include <linux/kmod.h>
109#include <linux/module.h>
110#include <linux/kallsyms.h>
111#include <linux/netpoll.h>
112#include <linux/rcupdate.h>
113#include <linux/delay.h>
Johannes Berg295f4a12007-04-26 20:43:56 -0700114#include <net/wext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#include <asm/current.h>
Steve Grubb5bdb9882005-12-03 08:39:35 -0500117#include <linux/audit.h>
Chris Leechdb217332006-06-17 21:24:58 -0700118#include <linux/dmaengine.h>
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700119#include <linux/err.h>
David S. Millerc7fa9d12006-08-15 16:34:13 -0700120#include <linux/ctype.h>
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700121#include <linux/if_arp.h>
Ben Hutchings6de329e2008-06-16 17:02:28 -0700122#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Pavel Emelyanov342709e2007-10-23 21:14:45 -0700124#include "net-sysfs.h"
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/*
127 * The list of packet types we will receive (as opposed to discard)
128 * and the routines to invoke.
129 *
130 * Why 16. Because with 16 the only overlap we get on a hash of the
131 * low nibble of the protocol value is RARP/SNAP/X.25.
132 *
133 * NOTE: That is no longer true with the addition of VLAN tags. Not
134 * sure which should go first, but I bet it won't make much
135 * difference if we are running VLANs. The good news is that
136 * this protocol won't be in the list unless compiled in, so
Stephen Hemminger3041a062006-05-26 13:25:24 -0700137 * the average user (w/out VLANs) will not be adversely affected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * --BLG
139 *
140 * 0800 IP
141 * 8100 802.1Q VLAN
142 * 0001 802.3
143 * 0002 AX.25
144 * 0004 802.2
145 * 8035 RARP
146 * 0005 SNAP
147 * 0805 X.25
148 * 0806 ARP
149 * 8137 IPX
150 * 0009 Localtalk
151 * 86DD IPv6
152 */
153
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +0800154#define PTYPE_HASH_SIZE (16)
155#define PTYPE_HASH_MASK (PTYPE_HASH_SIZE - 1)
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static DEFINE_SPINLOCK(ptype_lock);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +0800158static struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -0700159static struct list_head ptype_all __read_mostly; /* Taps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Chris Leechdb217332006-06-17 21:24:58 -0700161#ifdef CONFIG_NET_DMA
Dan Williamsd379b012007-07-09 11:56:42 -0700162struct net_dma {
163 struct dma_client client;
164 spinlock_t lock;
165 cpumask_t channel_mask;
Mike Travis0c0b0ac2008-05-02 16:43:08 -0700166 struct dma_chan **channels;
Dan Williamsd379b012007-07-09 11:56:42 -0700167};
168
169static enum dma_state_client
170netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
171 enum dma_state state);
172
173static struct net_dma net_dma = {
174 .client = {
175 .event_callback = netdev_dma_event,
176 },
177};
Chris Leechdb217332006-06-17 21:24:58 -0700178#endif
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/*
Pavel Emelianov7562f872007-05-03 15:13:45 -0700181 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * semaphore.
183 *
184 * Pure readers hold dev_base_lock for reading.
185 *
186 * Writers must hold the rtnl semaphore while they loop through the
Pavel Emelianov7562f872007-05-03 15:13:45 -0700187 * dev_base_head list, and hold dev_base_lock for writing when they do the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * actual updates. This allows pure readers to access the list even
189 * while a writer is preparing to update it.
190 *
191 * To put it another way, dev_base_lock is held for writing only to
192 * protect against pure readers; the rtnl semaphore provides the
193 * protection against other writers.
194 *
195 * See, for example usages, register_netdevice() and
196 * unregister_netdevice(), which must be called with the rtnl
197 * semaphore held.
198 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199DEFINE_RWLOCK(dev_base_lock);
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201EXPORT_SYMBOL(dev_base_lock);
202
203#define NETDEV_HASHBITS 8
Eric W. Biederman881d9662007-09-17 11:56:21 -0700204#define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Eric W. Biederman881d9662007-09-17 11:56:21 -0700206static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
Eric W. Biederman881d9662007-09-17 11:56:21 -0700209 return &net->dev_name_head[hash & ((1 << NETDEV_HASHBITS) - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Eric W. Biederman881d9662007-09-17 11:56:21 -0700212static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700214 return &net->dev_index_head[ifindex & ((1 << NETDEV_HASHBITS) - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Eric W. Biedermance286d32007-09-12 13:53:49 +0200217/* Device list insertion */
218static int list_netdevice(struct net_device *dev)
219{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900220 struct net *net = dev_net(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200221
222 ASSERT_RTNL();
223
224 write_lock_bh(&dev_base_lock);
225 list_add_tail(&dev->dev_list, &net->dev_base_head);
226 hlist_add_head(&dev->name_hlist, dev_name_hash(net, dev->name));
227 hlist_add_head(&dev->index_hlist, dev_index_hash(net, dev->ifindex));
228 write_unlock_bh(&dev_base_lock);
229 return 0;
230}
231
232/* Device list removal */
233static void unlist_netdevice(struct net_device *dev)
234{
235 ASSERT_RTNL();
236
237 /* Unlink dev from the device chain */
238 write_lock_bh(&dev_base_lock);
239 list_del(&dev->dev_list);
240 hlist_del(&dev->name_hlist);
241 hlist_del(&dev->index_hlist);
242 write_unlock_bh(&dev_base_lock);
243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/*
246 * Our notifier list
247 */
248
Alan Sternf07d5b92006-05-09 15:23:03 -0700249static RAW_NOTIFIER_HEAD(netdev_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251/*
252 * Device drivers call our routines to queue packets here. We empty the
253 * queue in the local softnet handler.
254 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700255
256DEFINE_PER_CPU(struct softnet_data, softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700258#ifdef CONFIG_DEBUG_LOCK_ALLOC
259/*
260 * register_netdevice() inits dev->_xmit_lock and sets lockdep class
261 * according to dev->type
262 */
263static const unsigned short netdev_lock_type[] =
264 {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
265 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
266 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
267 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
268 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
269 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
270 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
271 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
272 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
273 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
274 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
275 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
276 ARPHRD_FCFABRIC, ARPHRD_IEEE802_TR, ARPHRD_IEEE80211,
277 ARPHRD_IEEE80211_PRISM, ARPHRD_IEEE80211_RADIOTAP, ARPHRD_VOID,
278 ARPHRD_NONE};
279
280static const char *netdev_lock_name[] =
281 {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
282 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
283 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
284 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
285 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
286 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
287 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
288 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
289 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
290 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
291 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
292 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
293 "_xmit_FCFABRIC", "_xmit_IEEE802_TR", "_xmit_IEEE80211",
294 "_xmit_IEEE80211_PRISM", "_xmit_IEEE80211_RADIOTAP", "_xmit_VOID",
295 "_xmit_NONE"};
296
297static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
298
299static inline unsigned short netdev_lock_pos(unsigned short dev_type)
300{
301 int i;
302
303 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
304 if (netdev_lock_type[i] == dev_type)
305 return i;
306 /* the last key is used by default */
307 return ARRAY_SIZE(netdev_lock_type) - 1;
308}
309
310static inline void netdev_set_lockdep_class(spinlock_t *lock,
311 unsigned short dev_type)
312{
313 int i;
314
315 i = netdev_lock_pos(dev_type);
316 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
317 netdev_lock_name[i]);
318}
319#else
320static inline void netdev_set_lockdep_class(spinlock_t *lock,
321 unsigned short dev_type)
322{
323}
324#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326/*******************************************************************************
327
328 Protocol management and registration routines
329
330*******************************************************************************/
331
332/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 * Add a protocol ID to the list. Now that the input handler is
334 * smarter we can dispense with all the messy stuff that used to be
335 * here.
336 *
337 * BEWARE!!! Protocol handlers, mangling input packets,
338 * MUST BE last in hash buckets and checking protocol handlers
339 * MUST start from promiscuous ptype_all chain in net_bh.
340 * It is true now, do not change it.
341 * Explanation follows: if protocol handler, mangling packet, will
342 * be the first on list, it is not able to sense, that packet
343 * is cloned and should be copied-on-write, so that it will
344 * change it and subsequent readers will get broken packet.
345 * --ANK (980803)
346 */
347
348/**
349 * dev_add_pack - add packet handler
350 * @pt: packet type declaration
351 *
352 * Add a protocol handler to the networking stack. The passed &packet_type
353 * is linked into kernel lists and may not be freed until it has been
354 * removed from the kernel lists.
355 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900356 * This call does not sleep therefore it can not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 * guarantee all CPU's that are in middle of receiving packets
358 * will see the new packet type (until the next received packet).
359 */
360
361void dev_add_pack(struct packet_type *pt)
362{
363 int hash;
364
365 spin_lock_bh(&ptype_lock);
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -0700366 if (pt->type == htons(ETH_P_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 list_add_rcu(&pt->list, &ptype_all);
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -0700368 else {
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +0800369 hash = ntohs(pt->type) & PTYPE_HASH_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 list_add_rcu(&pt->list, &ptype_base[hash]);
371 }
372 spin_unlock_bh(&ptype_lock);
373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/**
376 * __dev_remove_pack - remove packet handler
377 * @pt: packet type declaration
378 *
379 * Remove a protocol handler that was previously added to the kernel
380 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
381 * from the kernel lists and can be freed or reused once this function
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900382 * returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 *
384 * The packet type might still be in use by receivers
385 * and must not be freed until after all the CPU's have gone
386 * through a quiescent state.
387 */
388void __dev_remove_pack(struct packet_type *pt)
389{
390 struct list_head *head;
391 struct packet_type *pt1;
392
393 spin_lock_bh(&ptype_lock);
394
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -0700395 if (pt->type == htons(ETH_P_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 head = &ptype_all;
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -0700397 else
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +0800398 head = &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 list_for_each_entry(pt1, head, list) {
401 if (pt == pt1) {
402 list_del_rcu(&pt->list);
403 goto out;
404 }
405 }
406
407 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
408out:
409 spin_unlock_bh(&ptype_lock);
410}
411/**
412 * dev_remove_pack - remove packet handler
413 * @pt: packet type declaration
414 *
415 * Remove a protocol handler that was previously added to the kernel
416 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
417 * from the kernel lists and can be freed or reused once this function
418 * returns.
419 *
420 * This call sleeps to guarantee that no CPU is looking at the packet
421 * type after return.
422 */
423void dev_remove_pack(struct packet_type *pt)
424{
425 __dev_remove_pack(pt);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 synchronize_net();
428}
429
430/******************************************************************************
431
432 Device Boot-time Settings Routines
433
434*******************************************************************************/
435
436/* Boot time configuration table */
437static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
438
439/**
440 * netdev_boot_setup_add - add new setup entry
441 * @name: name of the device
442 * @map: configured settings for the device
443 *
444 * Adds new setup entry to the dev_boot_setup list. The function
445 * returns 0 on error and 1 on success. This is a generic routine to
446 * all netdevices.
447 */
448static int netdev_boot_setup_add(char *name, struct ifmap *map)
449{
450 struct netdev_boot_setup *s;
451 int i;
452
453 s = dev_boot_setup;
454 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
455 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
456 memset(s[i].name, 0, sizeof(s[i].name));
457 strcpy(s[i].name, name);
458 memcpy(&s[i].map, map, sizeof(s[i].map));
459 break;
460 }
461 }
462
463 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
464}
465
466/**
467 * netdev_boot_setup_check - check boot time settings
468 * @dev: the netdevice
469 *
470 * Check boot time settings for the device.
471 * The found settings are set for the device to be used
472 * later in the device probing.
473 * Returns 0 if no settings found, 1 if they are.
474 */
475int netdev_boot_setup_check(struct net_device *dev)
476{
477 struct netdev_boot_setup *s = dev_boot_setup;
478 int i;
479
480 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
481 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
482 !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
483 dev->irq = s[i].map.irq;
484 dev->base_addr = s[i].map.base_addr;
485 dev->mem_start = s[i].map.mem_start;
486 dev->mem_end = s[i].map.mem_end;
487 return 1;
488 }
489 }
490 return 0;
491}
492
493
494/**
495 * netdev_boot_base - get address from boot time settings
496 * @prefix: prefix for network device
497 * @unit: id for network device
498 *
499 * Check boot time settings for the base address of device.
500 * The found settings are set for the device to be used
501 * later in the device probing.
502 * Returns 0 if no settings found.
503 */
504unsigned long netdev_boot_base(const char *prefix, int unit)
505{
506 const struct netdev_boot_setup *s = dev_boot_setup;
507 char name[IFNAMSIZ];
508 int i;
509
510 sprintf(name, "%s%d", prefix, unit);
511
512 /*
513 * If device already registered then return base of 1
514 * to indicate not to probe for this interface
515 */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700516 if (__dev_get_by_name(&init_net, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return 1;
518
519 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
520 if (!strcmp(name, s[i].name))
521 return s[i].map.base_addr;
522 return 0;
523}
524
525/*
526 * Saves at boot time configured settings for any netdevice.
527 */
528int __init netdev_boot_setup(char *str)
529{
530 int ints[5];
531 struct ifmap map;
532
533 str = get_options(str, ARRAY_SIZE(ints), ints);
534 if (!str || !*str)
535 return 0;
536
537 /* Save settings */
538 memset(&map, 0, sizeof(map));
539 if (ints[0] > 0)
540 map.irq = ints[1];
541 if (ints[0] > 1)
542 map.base_addr = ints[2];
543 if (ints[0] > 2)
544 map.mem_start = ints[3];
545 if (ints[0] > 3)
546 map.mem_end = ints[4];
547
548 /* Add new entry to the list */
549 return netdev_boot_setup_add(str, &map);
550}
551
552__setup("netdev=", netdev_boot_setup);
553
554/*******************************************************************************
555
556 Device Interface Subroutines
557
558*******************************************************************************/
559
560/**
561 * __dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700562 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 * @name: name to find
564 *
565 * Find an interface by name. Must be called under RTNL semaphore
566 * or @dev_base_lock. If the name is found a pointer to the device
567 * is returned. If the name is not found then %NULL is returned. The
568 * reference counters are not incremented so the caller must be
569 * careful with locks.
570 */
571
Eric W. Biederman881d9662007-09-17 11:56:21 -0700572struct net_device *__dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 struct hlist_node *p;
575
Eric W. Biederman881d9662007-09-17 11:56:21 -0700576 hlist_for_each(p, dev_name_hash(net, name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 struct net_device *dev
578 = hlist_entry(p, struct net_device, name_hlist);
579 if (!strncmp(dev->name, name, IFNAMSIZ))
580 return dev;
581 }
582 return NULL;
583}
584
585/**
586 * dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700587 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 * @name: name to find
589 *
590 * Find an interface by name. This can be called from any
591 * context and does its own locking. The returned handle has
592 * the usage count incremented and the caller must use dev_put() to
593 * release it when it is no longer needed. %NULL is returned if no
594 * matching device is found.
595 */
596
Eric W. Biederman881d9662007-09-17 11:56:21 -0700597struct net_device *dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 struct net_device *dev;
600
601 read_lock(&dev_base_lock);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700602 dev = __dev_get_by_name(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (dev)
604 dev_hold(dev);
605 read_unlock(&dev_base_lock);
606 return dev;
607}
608
609/**
610 * __dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700611 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * @ifindex: index of device
613 *
614 * Search for an interface by index. Returns %NULL if the device
615 * is not found or a pointer to the device. The device has not
616 * had its reference counter increased so the caller must be careful
617 * about locking. The caller must hold either the RTNL semaphore
618 * or @dev_base_lock.
619 */
620
Eric W. Biederman881d9662007-09-17 11:56:21 -0700621struct net_device *__dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 struct hlist_node *p;
624
Eric W. Biederman881d9662007-09-17 11:56:21 -0700625 hlist_for_each(p, dev_index_hash(net, ifindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 struct net_device *dev
627 = hlist_entry(p, struct net_device, index_hlist);
628 if (dev->ifindex == ifindex)
629 return dev;
630 }
631 return NULL;
632}
633
634
635/**
636 * dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700637 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * @ifindex: index of device
639 *
640 * Search for an interface by index. Returns NULL if the device
641 * is not found or a pointer to the device. The device returned has
642 * had a reference added and the pointer is safe until the user calls
643 * dev_put to indicate they have finished with it.
644 */
645
Eric W. Biederman881d9662007-09-17 11:56:21 -0700646struct net_device *dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 struct net_device *dev;
649
650 read_lock(&dev_base_lock);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700651 dev = __dev_get_by_index(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (dev)
653 dev_hold(dev);
654 read_unlock(&dev_base_lock);
655 return dev;
656}
657
658/**
659 * dev_getbyhwaddr - find a device by its hardware address
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700660 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 * @type: media type of device
662 * @ha: hardware address
663 *
664 * Search for an interface by MAC address. Returns NULL if the device
665 * is not found or a pointer to the device. The caller must hold the
666 * rtnl semaphore. The returned device has not had its ref count increased
667 * and the caller must therefore be careful about locking
668 *
669 * BUGS:
670 * If the API was consistent this would be __dev_get_by_hwaddr
671 */
672
Eric W. Biederman881d9662007-09-17 11:56:21 -0700673struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type, char *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 struct net_device *dev;
676
677 ASSERT_RTNL();
678
Denis V. Lunev81103a52007-12-12 10:47:38 -0800679 for_each_netdev(net, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (dev->type == type &&
681 !memcmp(dev->dev_addr, ha, dev->addr_len))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700682 return dev;
683
684 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300687EXPORT_SYMBOL(dev_getbyhwaddr);
688
Eric W. Biederman881d9662007-09-17 11:56:21 -0700689struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700690{
691 struct net_device *dev;
692
693 ASSERT_RTNL();
Eric W. Biederman881d9662007-09-17 11:56:21 -0700694 for_each_netdev(net, dev)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700695 if (dev->type == type)
Pavel Emelianov7562f872007-05-03 15:13:45 -0700696 return dev;
697
698 return NULL;
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700699}
700
701EXPORT_SYMBOL(__dev_getfirstbyhwtype);
702
Eric W. Biederman881d9662007-09-17 11:56:21 -0700703struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct net_device *dev;
706
707 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -0700708 dev = __dev_getfirstbyhwtype(net, type);
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700709 if (dev)
710 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 rtnl_unlock();
712 return dev;
713}
714
715EXPORT_SYMBOL(dev_getfirstbyhwtype);
716
717/**
718 * dev_get_by_flags - find any device with given flags
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700719 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * @if_flags: IFF_* values
721 * @mask: bitmask of bits in if_flags to check
722 *
723 * Search for any interface with the given flags. Returns NULL if a device
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900724 * is not found or a pointer to the device. The device returned has
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 * had a reference added and the pointer is safe until the user calls
726 * dev_put to indicate they have finished with it.
727 */
728
Eric W. Biederman881d9662007-09-17 11:56:21 -0700729struct net_device * dev_get_by_flags(struct net *net, unsigned short if_flags, unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Pavel Emelianov7562f872007-05-03 15:13:45 -0700731 struct net_device *dev, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Pavel Emelianov7562f872007-05-03 15:13:45 -0700733 ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 read_lock(&dev_base_lock);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700735 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (((dev->flags ^ if_flags) & mask) == 0) {
737 dev_hold(dev);
Pavel Emelianov7562f872007-05-03 15:13:45 -0700738 ret = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 break;
740 }
741 }
742 read_unlock(&dev_base_lock);
Pavel Emelianov7562f872007-05-03 15:13:45 -0700743 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
746/**
747 * dev_valid_name - check if name is okay for network device
748 * @name: name string
749 *
750 * Network device names need to be valid file names to
David S. Millerc7fa9d12006-08-15 16:34:13 -0700751 * to allow sysfs to work. We also disallow any kind of
752 * whitespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
Mitch Williamsc2373ee2005-11-09 10:34:45 -0800754int dev_valid_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
David S. Millerc7fa9d12006-08-15 16:34:13 -0700756 if (*name == '\0')
757 return 0;
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -0700758 if (strlen(name) >= IFNAMSIZ)
759 return 0;
David S. Millerc7fa9d12006-08-15 16:34:13 -0700760 if (!strcmp(name, ".") || !strcmp(name, ".."))
761 return 0;
762
763 while (*name) {
764 if (*name == '/' || isspace(*name))
765 return 0;
766 name++;
767 }
768 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
771/**
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200772 * __dev_alloc_name - allocate a name for a device
773 * @net: network namespace to allocate the device name in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 * @name: name format string
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200775 * @buf: scratch buffer and result name string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 *
777 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -0700778 * id. It scans list of devices to build up a free map, then chooses
779 * the first empty slot. The caller must hold the dev_base or rtnl lock
780 * while allocating the name and adding the device in order to avoid
781 * duplicates.
782 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
783 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
785
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200786static int __dev_alloc_name(struct net *net, const char *name, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 const char *p;
790 const int max_netdevices = 8*PAGE_SIZE;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700791 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct net_device *d;
793
794 p = strnchr(name, IFNAMSIZ-1, '%');
795 if (p) {
796 /*
797 * Verify the string as this thing may have come from
798 * the user. There must be either one "%d" and no other "%"
799 * characters.
800 */
801 if (p[1] != 'd' || strchr(p + 2, '%'))
802 return -EINVAL;
803
804 /* Use one page as a bit array of possible slots */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700805 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (!inuse)
807 return -ENOMEM;
808
Eric W. Biederman881d9662007-09-17 11:56:21 -0700809 for_each_netdev(net, d) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (!sscanf(d->name, name, &i))
811 continue;
812 if (i < 0 || i >= max_netdevices)
813 continue;
814
815 /* avoid cases where sscanf is not exact inverse of printf */
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200816 snprintf(buf, IFNAMSIZ, name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (!strncmp(buf, d->name, IFNAMSIZ))
818 set_bit(i, inuse);
819 }
820
821 i = find_first_zero_bit(inuse, max_netdevices);
822 free_page((unsigned long) inuse);
823 }
824
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200825 snprintf(buf, IFNAMSIZ, name, i);
826 if (!__dev_get_by_name(net, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 /* It is possible to run out of possible slots
830 * when the name is long and there isn't enough space left
831 * for the digits, or if all bits are used.
832 */
833 return -ENFILE;
834}
835
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200836/**
837 * dev_alloc_name - allocate a name for a device
838 * @dev: device
839 * @name: name format string
840 *
841 * Passed a format string - eg "lt%d" it will try and find a suitable
842 * id. It scans list of devices to build up a free map, then chooses
843 * the first empty slot. The caller must hold the dev_base or rtnl lock
844 * while allocating the name and adding the device in order to avoid
845 * duplicates.
846 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
847 * Returns the number of the unit assigned or a negative errno code.
848 */
849
850int dev_alloc_name(struct net_device *dev, const char *name)
851{
852 char buf[IFNAMSIZ];
853 struct net *net;
854 int ret;
855
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900856 BUG_ON(!dev_net(dev));
857 net = dev_net(dev);
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200858 ret = __dev_alloc_name(net, name, buf);
859 if (ret >= 0)
860 strlcpy(dev->name, buf, IFNAMSIZ);
861 return ret;
862}
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865/**
866 * dev_change_name - change name of a device
867 * @dev: device
868 * @newname: name (or format string) must be at least IFNAMSIZ
869 *
870 * Change name of a device, can pass format strings "eth%d".
871 * for wildcarding.
872 */
873int dev_change_name(struct net_device *dev, char *newname)
874{
Herbert Xufcc5a032007-07-30 17:03:38 -0700875 char oldname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 int err = 0;
Herbert Xufcc5a032007-07-30 17:03:38 -0700877 int ret;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700878 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 ASSERT_RTNL();
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900881 BUG_ON(!dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900883 net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (dev->flags & IFF_UP)
885 return -EBUSY;
886
887 if (!dev_valid_name(newname))
888 return -EINVAL;
889
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -0700890 if (strncmp(newname, dev->name, IFNAMSIZ) == 0)
891 return 0;
892
Herbert Xufcc5a032007-07-30 17:03:38 -0700893 memcpy(oldname, dev->name, IFNAMSIZ);
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (strchr(newname, '%')) {
896 err = dev_alloc_name(dev, newname);
897 if (err < 0)
898 return err;
899 strcpy(newname, dev->name);
900 }
Eric W. Biederman881d9662007-09-17 11:56:21 -0700901 else if (__dev_get_by_name(net, newname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return -EEXIST;
903 else
904 strlcpy(dev->name, newname, IFNAMSIZ);
905
Herbert Xufcc5a032007-07-30 17:03:38 -0700906rollback:
Stephen Hemmingerdcc99772008-05-14 22:33:38 -0700907 err = device_rename(&dev->dev, dev->name);
908 if (err) {
909 memcpy(dev->name, oldname, IFNAMSIZ);
910 return err;
911 }
Herbert Xu7f988ea2007-07-30 16:35:46 -0700912
913 write_lock_bh(&dev_base_lock);
Eric W. Biederman92749822007-04-03 00:07:30 -0600914 hlist_del(&dev->name_hlist);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700915 hlist_add_head(&dev->name_hlist, dev_name_hash(net, dev->name));
Herbert Xu7f988ea2007-07-30 16:35:46 -0700916 write_unlock_bh(&dev_base_lock);
917
Pavel Emelyanov056925a2007-09-16 15:42:43 -0700918 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -0700919 ret = notifier_to_errno(ret);
920
921 if (ret) {
922 if (err) {
923 printk(KERN_ERR
924 "%s: name change rollback failed: %d.\n",
925 dev->name, ret);
926 } else {
927 err = ret;
928 memcpy(dev->name, oldname, IFNAMSIZ);
929 goto rollback;
930 }
931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 return err;
934}
935
936/**
Stephen Hemminger3041a062006-05-26 13:25:24 -0700937 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -0700938 * @dev: device to cause notification
939 *
940 * Called to indicate a device has changed features.
941 */
942void netdev_features_change(struct net_device *dev)
943{
Pavel Emelyanov056925a2007-09-16 15:42:43 -0700944 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -0700945}
946EXPORT_SYMBOL(netdev_features_change);
947
948/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 * netdev_state_change - device changes state
950 * @dev: device to cause notification
951 *
952 * Called to indicate a device has changed state. This function calls
953 * the notifier chains for netdev_chain and sends a NEWLINK message
954 * to the routing socket.
955 */
956void netdev_state_change(struct net_device *dev)
957{
958 if (dev->flags & IFF_UP) {
Pavel Emelyanov056925a2007-09-16 15:42:43 -0700959 call_netdevice_notifiers(NETDEV_CHANGE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
961 }
962}
963
964/**
965 * dev_load - load a network module
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700966 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 * @name: name of interface
968 *
969 * If a network interface is not present and the process has suitable
970 * privileges this function loads the module. If module loading is not
971 * available in this kernel then it becomes a nop.
972 */
973
Eric W. Biederman881d9662007-09-17 11:56:21 -0700974void dev_load(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900976 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 read_lock(&dev_base_lock);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700979 dev = __dev_get_by_name(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 read_unlock(&dev_base_lock);
981
982 if (!dev && capable(CAP_SYS_MODULE))
983 request_module("%s", name);
984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986/**
987 * dev_open - prepare an interface for use.
988 * @dev: device to open
989 *
990 * Takes a device from down to up state. The device's private open
991 * function is invoked and then the multicast lists are loaded. Finally
992 * the device is moved into the up state and a %NETDEV_UP message is
993 * sent to the netdev notifier chain.
994 *
995 * Calling this function on an active interface is a nop. On a failure
996 * a negative errno code is returned.
997 */
998int dev_open(struct net_device *dev)
999{
1000 int ret = 0;
1001
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001002 ASSERT_RTNL();
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /*
1005 * Is it already up?
1006 */
1007
1008 if (dev->flags & IFF_UP)
1009 return 0;
1010
1011 /*
1012 * Is it even present?
1013 */
1014 if (!netif_device_present(dev))
1015 return -ENODEV;
1016
1017 /*
1018 * Call device private open method
1019 */
1020 set_bit(__LINK_STATE_START, &dev->state);
Jeff Garzikbada3392007-10-23 20:19:37 -07001021
1022 if (dev->validate_addr)
1023 ret = dev->validate_addr(dev);
1024
1025 if (!ret && dev->open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 ret = dev->open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001028 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 * If it went open OK then:
1030 */
1031
Jeff Garzikbada3392007-10-23 20:19:37 -07001032 if (ret)
1033 clear_bit(__LINK_STATE_START, &dev->state);
1034 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 /*
1036 * Set the flags.
1037 */
1038 dev->flags |= IFF_UP;
1039
1040 /*
1041 * Initialize multicasting status
1042 */
Patrick McHardy4417da62007-06-27 01:28:10 -07001043 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 /*
1046 * Wakeup transmit queue engine
1047 */
1048 dev_activate(dev);
1049
1050 /*
1051 * ... and announce new interface.
1052 */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001053 call_netdevice_notifiers(NETDEV_UP, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
Jeff Garzikbada3392007-10-23 20:19:37 -07001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return ret;
1057}
1058
1059/**
1060 * dev_close - shutdown an interface.
1061 * @dev: device to shutdown
1062 *
1063 * This function moves an active device into down state. A
1064 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1065 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1066 * chain.
1067 */
1068int dev_close(struct net_device *dev)
1069{
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001070 ASSERT_RTNL();
1071
David S. Miller9d5010d2007-09-12 14:33:25 +02001072 might_sleep();
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (!(dev->flags & IFF_UP))
1075 return 0;
1076
1077 /*
1078 * Tell people we are going down, so that they can
1079 * prepare to death, when device is still operating.
1080 */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001081 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 clear_bit(__LINK_STATE_START, &dev->state);
1084
1085 /* Synchronize to scheduled poll. We cannot touch poll list,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001086 * it can be even on different cpu. So just clear netif_running().
1087 *
1088 * dev->stop() will invoke napi_disable() on all of it's
1089 * napi_struct instances on this device.
1090 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 smp_mb__after_clear_bit(); /* Commit netif_running(). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Matti Linnanvuorid8b2a4d2008-02-12 23:10:11 -08001093 dev_deactivate(dev);
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 /*
1096 * Call the device specific close. This cannot fail.
1097 * Only if device is UP
1098 *
1099 * We allow it to be called even after a DETACH hot-plug
1100 * event.
1101 */
1102 if (dev->stop)
1103 dev->stop(dev);
1104
1105 /*
1106 * Device is now down.
1107 */
1108
1109 dev->flags &= ~IFF_UP;
1110
1111 /*
1112 * Tell people we are down
1113 */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001114 call_netdevice_notifiers(NETDEV_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 return 0;
1117}
1118
1119
Eric W. Biederman881d9662007-09-17 11:56:21 -07001120static int dev_boot_phase = 1;
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122/*
1123 * Device change register/unregister. These are not inline or static
1124 * as we export them to the world.
1125 */
1126
1127/**
1128 * register_netdevice_notifier - register a network notifier block
1129 * @nb: notifier
1130 *
1131 * Register a notifier to be called when network device events occur.
1132 * The notifier passed is linked into the kernel structures and must
1133 * not be reused until it has been unregistered. A negative errno code
1134 * is returned on a failure.
1135 *
1136 * When registered all registration and up events are replayed
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001137 * to the new notifier to allow device to have a race free
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 * view of the network device list.
1139 */
1140
1141int register_netdevice_notifier(struct notifier_block *nb)
1142{
1143 struct net_device *dev;
Herbert Xufcc5a032007-07-30 17:03:38 -07001144 struct net_device *last;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001145 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 int err;
1147
1148 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001149 err = raw_notifier_chain_register(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001150 if (err)
1151 goto unlock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001152 if (dev_boot_phase)
1153 goto unlock;
1154 for_each_net(net) {
1155 for_each_netdev(net, dev) {
1156 err = nb->notifier_call(nb, NETDEV_REGISTER, dev);
1157 err = notifier_to_errno(err);
1158 if (err)
1159 goto rollback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Eric W. Biederman881d9662007-09-17 11:56:21 -07001161 if (!(dev->flags & IFF_UP))
1162 continue;
Herbert Xufcc5a032007-07-30 17:03:38 -07001163
Eric W. Biederman881d9662007-09-17 11:56:21 -07001164 nb->notifier_call(nb, NETDEV_UP, dev);
1165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001167
1168unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 rtnl_unlock();
1170 return err;
Herbert Xufcc5a032007-07-30 17:03:38 -07001171
1172rollback:
1173 last = dev;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001174 for_each_net(net) {
1175 for_each_netdev(net, dev) {
1176 if (dev == last)
1177 break;
Herbert Xufcc5a032007-07-30 17:03:38 -07001178
Eric W. Biederman881d9662007-09-17 11:56:21 -07001179 if (dev->flags & IFF_UP) {
1180 nb->notifier_call(nb, NETDEV_GOING_DOWN, dev);
1181 nb->notifier_call(nb, NETDEV_DOWN, dev);
1182 }
1183 nb->notifier_call(nb, NETDEV_UNREGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001184 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001185 }
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001186
1187 raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001188 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191/**
1192 * unregister_netdevice_notifier - unregister a network notifier block
1193 * @nb: notifier
1194 *
1195 * Unregister a notifier previously registered by
1196 * register_netdevice_notifier(). The notifier is unlinked into the
1197 * kernel structures and may then be reused. A negative errno code
1198 * is returned on a failure.
1199 */
1200
1201int unregister_netdevice_notifier(struct notifier_block *nb)
1202{
Herbert Xu9f514952006-03-25 01:24:25 -08001203 int err;
1204
1205 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001206 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xu9f514952006-03-25 01:24:25 -08001207 rtnl_unlock();
1208 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
1211/**
1212 * call_netdevice_notifiers - call all network notifier blocks
1213 * @val: value passed unmodified to notifier function
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001214 * @dev: net_device pointer passed unmodified to notifier function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 *
1216 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07001217 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
1219
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07001220int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07001222 return raw_notifier_call_chain(&netdev_chain, val, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
1225/* When > 0 there are consumers of rx skb time stamps */
1226static atomic_t netstamp_needed = ATOMIC_INIT(0);
1227
1228void net_enable_timestamp(void)
1229{
1230 atomic_inc(&netstamp_needed);
1231}
1232
1233void net_disable_timestamp(void)
1234{
1235 atomic_dec(&netstamp_needed);
1236}
1237
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001238static inline void net_timestamp(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 if (atomic_read(&netstamp_needed))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001241 __net_timestamp(skb);
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07001242 else
1243 skb->tstamp.tv64 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
1246/*
1247 * Support routine. Sends outgoing frames to any network
1248 * taps currently in use.
1249 */
1250
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001251static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253 struct packet_type *ptype;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001254
1255 net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 rcu_read_lock();
1258 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1259 /* Never send packets back to the socket
1260 * they originated from - MvS (miquels@drinkel.ow.org)
1261 */
1262 if ((ptype->dev == dev || !ptype->dev) &&
1263 (ptype->af_packet_priv == NULL ||
1264 (struct sock *)ptype->af_packet_priv != skb->sk)) {
1265 struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
1266 if (!skb2)
1267 break;
1268
1269 /* skb->nh should be correctly
1270 set by sender, so that the second statement is
1271 just protection against buggy protocols.
1272 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001273 skb_reset_mac_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001275 if (skb_network_header(skb2) < skb2->data ||
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001276 skb2->network_header > skb2->tail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (net_ratelimit())
1278 printk(KERN_CRIT "protocol %04x is "
1279 "buggy, dev %s\n",
1280 skb2->protocol, dev->name);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07001281 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001284 skb2->transport_header = skb2->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 skb2->pkt_type = PACKET_OUTGOING;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001286 ptype->func(skb2, skb->dev, ptype, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288 }
1289 rcu_read_unlock();
1290}
1291
Denis Vlasenko56079432006-03-29 15:57:29 -08001292
1293void __netif_schedule(struct net_device *dev)
1294{
1295 if (!test_and_set_bit(__LINK_STATE_SCHED, &dev->state)) {
1296 unsigned long flags;
1297 struct softnet_data *sd;
1298
1299 local_irq_save(flags);
1300 sd = &__get_cpu_var(softnet_data);
1301 dev->next_sched = sd->output_queue;
1302 sd->output_queue = dev;
1303 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1304 local_irq_restore(flags);
1305 }
1306}
1307EXPORT_SYMBOL(__netif_schedule);
1308
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001309void dev_kfree_skb_irq(struct sk_buff *skb)
Denis Vlasenko56079432006-03-29 15:57:29 -08001310{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001311 if (atomic_dec_and_test(&skb->users)) {
1312 struct softnet_data *sd;
1313 unsigned long flags;
Denis Vlasenko56079432006-03-29 15:57:29 -08001314
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001315 local_irq_save(flags);
1316 sd = &__get_cpu_var(softnet_data);
1317 skb->next = sd->completion_queue;
1318 sd->completion_queue = skb;
1319 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1320 local_irq_restore(flags);
1321 }
Denis Vlasenko56079432006-03-29 15:57:29 -08001322}
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001323EXPORT_SYMBOL(dev_kfree_skb_irq);
Denis Vlasenko56079432006-03-29 15:57:29 -08001324
1325void dev_kfree_skb_any(struct sk_buff *skb)
1326{
1327 if (in_irq() || irqs_disabled())
1328 dev_kfree_skb_irq(skb);
1329 else
1330 dev_kfree_skb(skb);
1331}
1332EXPORT_SYMBOL(dev_kfree_skb_any);
1333
1334
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001335/**
1336 * netif_device_detach - mark device as removed
1337 * @dev: network device
1338 *
1339 * Mark device as removed from system and therefore no longer available.
1340 */
Denis Vlasenko56079432006-03-29 15:57:29 -08001341void netif_device_detach(struct net_device *dev)
1342{
1343 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
1344 netif_running(dev)) {
1345 netif_stop_queue(dev);
1346 }
1347}
1348EXPORT_SYMBOL(netif_device_detach);
1349
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001350/**
1351 * netif_device_attach - mark device as attached
1352 * @dev: network device
1353 *
1354 * Mark device as attached from system and restart if needed.
1355 */
Denis Vlasenko56079432006-03-29 15:57:29 -08001356void netif_device_attach(struct net_device *dev)
1357{
1358 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
1359 netif_running(dev)) {
1360 netif_wake_queue(dev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001361 __netdev_watchdog_up(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08001362 }
1363}
1364EXPORT_SYMBOL(netif_device_attach);
1365
Ben Hutchings6de329e2008-06-16 17:02:28 -07001366static bool can_checksum_protocol(unsigned long features, __be16 protocol)
1367{
1368 return ((features & NETIF_F_GEN_CSUM) ||
1369 ((features & NETIF_F_IP_CSUM) &&
1370 protocol == htons(ETH_P_IP)) ||
1371 ((features & NETIF_F_IPV6_CSUM) &&
1372 protocol == htons(ETH_P_IPV6)));
1373}
1374
1375static bool dev_can_checksum(struct net_device *dev, struct sk_buff *skb)
1376{
1377 if (can_checksum_protocol(dev->features, skb->protocol))
1378 return true;
1379
1380 if (skb->protocol == htons(ETH_P_8021Q)) {
1381 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
1382 if (can_checksum_protocol(dev->features & dev->vlan_features,
1383 veh->h_vlan_encapsulated_proto))
1384 return true;
1385 }
1386
1387 return false;
1388}
Denis Vlasenko56079432006-03-29 15:57:29 -08001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390/*
1391 * Invalidate hardware checksum when packet is to be mangled, and
1392 * complete checksum manually on outgoing path.
1393 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07001394int skb_checksum_help(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Al Virod3bc23e2006-11-14 21:24:49 -08001396 __wsum csum;
Herbert Xu663ead32007-04-09 11:59:07 -07001397 int ret = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Patrick McHardy84fa7932006-08-29 16:44:56 -07001399 if (skb->ip_summed == CHECKSUM_COMPLETE)
Herbert Xua430a432006-07-08 13:34:56 -07001400 goto out_set_summed;
1401
1402 if (unlikely(skb_shinfo(skb)->gso_size)) {
Herbert Xua430a432006-07-08 13:34:56 -07001403 /* Let GSO fix up the checksum. */
1404 goto out_set_summed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406
Herbert Xua0308472007-10-15 01:47:15 -07001407 offset = skb->csum_start - skb_headroom(skb);
1408 BUG_ON(offset >= skb_headlen(skb));
1409 csum = skb_checksum(skb, offset, skb->len - offset, 0);
1410
1411 offset += skb->csum_offset;
1412 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
1413
1414 if (skb_cloned(skb) &&
1415 !skb_clone_writable(skb, offset + sizeof(__sum16))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1417 if (ret)
1418 goto out;
1419 }
1420
Herbert Xua0308472007-10-15 01:47:15 -07001421 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
Herbert Xua430a432006-07-08 13:34:56 -07001422out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 skb->ip_summed = CHECKSUM_NONE;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001424out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return ret;
1426}
1427
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001428/**
1429 * skb_gso_segment - Perform segmentation on skb.
1430 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001431 * @features: features for the output path (see dev->features)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001432 *
1433 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07001434 *
1435 * It may return NULL if the skb requires no segmentation. This is
1436 * only possible when GSO is used for verifying header integrity.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001437 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001438struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001439{
1440 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
1441 struct packet_type *ptype;
Al Viro252e3342006-11-14 20:48:11 -08001442 __be16 type = skb->protocol;
Herbert Xua430a432006-07-08 13:34:56 -07001443 int err;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001444
1445 BUG_ON(skb_shinfo(skb)->frag_list);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001446
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001447 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001448 skb->mac_len = skb->network_header - skb->mac_header;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001449 __skb_pull(skb, skb->mac_len);
1450
Herbert Xuf9d106a2007-04-23 22:36:13 -07001451 if (WARN_ON(skb->ip_summed != CHECKSUM_PARTIAL)) {
Herbert Xua430a432006-07-08 13:34:56 -07001452 if (skb_header_cloned(skb) &&
1453 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
1454 return ERR_PTR(err);
1455 }
1456
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001457 rcu_read_lock();
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08001458 list_for_each_entry_rcu(ptype,
1459 &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001460 if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
Patrick McHardy84fa7932006-08-29 16:44:56 -07001461 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
Herbert Xua430a432006-07-08 13:34:56 -07001462 err = ptype->gso_send_check(skb);
1463 segs = ERR_PTR(err);
1464 if (err || skb_gso_ok(skb, features))
1465 break;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001466 __skb_push(skb, (skb->data -
1467 skb_network_header(skb)));
Herbert Xua430a432006-07-08 13:34:56 -07001468 }
Herbert Xu576a30e2006-06-27 13:22:38 -07001469 segs = ptype->gso_segment(skb, features);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001470 break;
1471 }
1472 }
1473 rcu_read_unlock();
1474
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001475 __skb_push(skb, skb->data - skb_mac_header(skb));
Herbert Xu576a30e2006-06-27 13:22:38 -07001476
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001477 return segs;
1478}
1479
1480EXPORT_SYMBOL(skb_gso_segment);
1481
Herbert Xufb286bb2005-11-10 13:01:24 -08001482/* Take action when hardware reception checksum errors are detected. */
1483#ifdef CONFIG_BUG
1484void netdev_rx_csum_fault(struct net_device *dev)
1485{
1486 if (net_ratelimit()) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001487 printk(KERN_ERR "%s: hw csum failure.\n",
Stephen Hemminger246a4212005-12-08 15:21:39 -08001488 dev ? dev->name : "<unknown>");
Herbert Xufb286bb2005-11-10 13:01:24 -08001489 dump_stack();
1490 }
1491}
1492EXPORT_SYMBOL(netdev_rx_csum_fault);
1493#endif
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495/* Actually, we should eliminate this check as soon as we know, that:
1496 * 1. IOMMU is present and allows to map all the memory.
1497 * 2. No high memory really exists on this machine.
1498 */
1499
1500static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1501{
Herbert Xu3d3a8532006-06-27 13:33:10 -07001502#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 int i;
1504
1505 if (dev->features & NETIF_F_HIGHDMA)
1506 return 0;
1507
1508 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1509 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1510 return 1;
1511
Herbert Xu3d3a8532006-06-27 13:33:10 -07001512#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return 0;
1514}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001516struct dev_gso_cb {
1517 void (*destructor)(struct sk_buff *skb);
1518};
1519
1520#define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
1521
1522static void dev_gso_skb_destructor(struct sk_buff *skb)
1523{
1524 struct dev_gso_cb *cb;
1525
1526 do {
1527 struct sk_buff *nskb = skb->next;
1528
1529 skb->next = nskb->next;
1530 nskb->next = NULL;
1531 kfree_skb(nskb);
1532 } while (skb->next);
1533
1534 cb = DEV_GSO_CB(skb);
1535 if (cb->destructor)
1536 cb->destructor(skb);
1537}
1538
1539/**
1540 * dev_gso_segment - Perform emulated hardware segmentation on skb.
1541 * @skb: buffer to segment
1542 *
1543 * This function segments the given skb and stores the list of segments
1544 * in skb->next.
1545 */
1546static int dev_gso_segment(struct sk_buff *skb)
1547{
1548 struct net_device *dev = skb->dev;
1549 struct sk_buff *segs;
Herbert Xu576a30e2006-06-27 13:22:38 -07001550 int features = dev->features & ~(illegal_highdma(dev, skb) ?
1551 NETIF_F_SG : 0);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001552
Herbert Xu576a30e2006-06-27 13:22:38 -07001553 segs = skb_gso_segment(skb, features);
1554
1555 /* Verifying header integrity only. */
1556 if (!segs)
1557 return 0;
1558
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07001559 if (IS_ERR(segs))
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001560 return PTR_ERR(segs);
1561
1562 skb->next = segs;
1563 DEV_GSO_CB(skb)->destructor = skb->destructor;
1564 skb->destructor = dev_gso_skb_destructor;
1565
1566 return 0;
1567}
1568
1569int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1570{
1571 if (likely(!skb->next)) {
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -07001572 if (!list_empty(&ptype_all))
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001573 dev_queue_xmit_nit(skb, dev);
1574
Herbert Xu576a30e2006-06-27 13:22:38 -07001575 if (netif_needs_gso(dev, skb)) {
1576 if (unlikely(dev_gso_segment(skb)))
1577 goto out_kfree_skb;
1578 if (skb->next)
1579 goto gso;
1580 }
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001581
Herbert Xu576a30e2006-06-27 13:22:38 -07001582 return dev->hard_start_xmit(skb, dev);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001583 }
1584
Herbert Xu576a30e2006-06-27 13:22:38 -07001585gso:
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001586 do {
1587 struct sk_buff *nskb = skb->next;
1588 int rc;
1589
1590 skb->next = nskb->next;
1591 nskb->next = NULL;
1592 rc = dev->hard_start_xmit(nskb, dev);
1593 if (unlikely(rc)) {
Michael Chanf54d9e82006-06-25 23:57:04 -07001594 nskb->next = skb->next;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001595 skb->next = nskb;
1596 return rc;
1597 }
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07001598 if (unlikely((netif_queue_stopped(dev) ||
Pavel Emelyanov668f8952007-10-21 17:01:56 -07001599 netif_subqueue_stopped(dev, skb)) &&
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07001600 skb->next))
Michael Chanf54d9e82006-06-25 23:57:04 -07001601 return NETDEV_TX_BUSY;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001602 } while (skb->next);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001603
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001604 skb->destructor = DEV_GSO_CB(skb)->destructor;
1605
1606out_kfree_skb:
1607 kfree_skb(skb);
1608 return 0;
1609}
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611/**
1612 * dev_queue_xmit - transmit a buffer
1613 * @skb: buffer to transmit
1614 *
1615 * Queue a buffer for transmission to a network device. The caller must
1616 * have set the device and priority and built the buffer before calling
1617 * this function. The function can be called from an interrupt.
1618 *
1619 * A negative errno code is returned on a failure. A success does not
1620 * guarantee the frame will be transmitted as it may be dropped due
1621 * to congestion or traffic shaping.
Ben Greearaf191362005-04-24 20:12:36 -07001622 *
1623 * -----------------------------------------------------------------------------------
1624 * I notice this method can also return errors from the queue disciplines,
1625 * including NET_XMIT_DROP, which is a positive value. So, errors can also
1626 * be positive.
1627 *
1628 * Regardless of the return value, the skb is consumed, so it is currently
1629 * difficult to retry a send to this method. (You can bump the ref count
1630 * before sending to hold a reference for retry if you are careful.)
1631 *
1632 * When calling this method, interrupts MUST be enabled. This is because
1633 * the BH enable code must have IRQs enabled so that it will not deadlock.
1634 * --BLG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 */
1636
1637int dev_queue_xmit(struct sk_buff *skb)
1638{
1639 struct net_device *dev = skb->dev;
1640 struct Qdisc *q;
1641 int rc = -ENOMEM;
1642
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001643 /* GSO will handle the following emulations directly. */
1644 if (netif_needs_gso(dev, skb))
1645 goto gso;
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (skb_shinfo(skb)->frag_list &&
1648 !(dev->features & NETIF_F_FRAGLIST) &&
Herbert Xu364c6ba2006-06-09 16:10:40 -07001649 __skb_linearize(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 goto out_kfree_skb;
1651
1652 /* Fragmented skb is linearized if device does not support SG,
1653 * or if at least one of fragments is in highmem and device
1654 * does not support DMA from it.
1655 */
1656 if (skb_shinfo(skb)->nr_frags &&
1657 (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
Herbert Xu364c6ba2006-06-09 16:10:40 -07001658 __skb_linearize(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 goto out_kfree_skb;
1660
1661 /* If packet is not checksummed and device does not support
1662 * checksumming for this protocol, complete checksumming here.
1663 */
Herbert Xu663ead32007-04-09 11:59:07 -07001664 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1665 skb_set_transport_header(skb, skb->csum_start -
1666 skb_headroom(skb));
Ben Hutchings6de329e2008-06-16 17:02:28 -07001667 if (!dev_can_checksum(dev, skb) && skb_checksum_help(skb))
1668 goto out_kfree_skb;
Herbert Xu663ead32007-04-09 11:59:07 -07001669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001671gso:
Eric Dumazet2d7ceec2005-09-27 15:22:58 -07001672 spin_lock_prefetch(&dev->queue_lock);
1673
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001674 /* Disable soft irqs for various locks below. Also
1675 * stops preemption for RCU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001677 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001679 /* Updates of qdisc are serialized by queue_lock.
1680 * The struct Qdisc which is pointed to by qdisc is now a
1681 * rcu structure - it may be accessed without acquiring
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 * a lock (but the structure may be stale.) The freeing of the
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001683 * qdisc will be deferred until it's known that there are no
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 * more references to it.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001685 *
1686 * If the qdisc has an enqueue function, we still need to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 * hold the queue_lock before calling it, since queue_lock
1688 * also serializes access to the device queue.
1689 */
1690
1691 q = rcu_dereference(dev->qdisc);
1692#ifdef CONFIG_NET_CLS_ACT
1693 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
1694#endif
1695 if (q->enqueue) {
1696 /* Grab device queue */
1697 spin_lock(&dev->queue_lock);
Patrick McHardy85670cc2006-09-27 16:45:45 -07001698 q = dev->qdisc;
1699 if (q->enqueue) {
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07001700 /* reset queue_mapping to zero */
Pavel Emelyanovdfa40912007-10-21 16:57:55 -07001701 skb_set_queue_mapping(skb, 0);
Patrick McHardy85670cc2006-09-27 16:45:45 -07001702 rc = q->enqueue(skb, q);
1703 qdisc_run(dev);
1704 spin_unlock(&dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Patrick McHardy85670cc2006-09-27 16:45:45 -07001706 rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
1707 goto out;
1708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 spin_unlock(&dev->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711
1712 /* The device has no queue. Common case for software devices:
1713 loopback, all the sorts of tunnels...
1714
Herbert Xu932ff272006-06-09 12:20:56 -07001715 Really, it is unlikely that netif_tx_lock protection is necessary
1716 here. (f.e. loopback and IP tunnels are clean ignoring statistics
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 counters.)
1718 However, it is possible, that they rely on protection
1719 made by us here.
1720
1721 Check this and shot the lock. It is not prone from deadlocks.
1722 Either shot noqueue qdisc, it is even simpler 8)
1723 */
1724 if (dev->flags & IFF_UP) {
1725 int cpu = smp_processor_id(); /* ok because BHs are off */
1726
1727 if (dev->xmit_lock_owner != cpu) {
1728
1729 HARD_TX_LOCK(dev, cpu);
1730
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07001731 if (!netif_queue_stopped(dev) &&
Pavel Emelyanov668f8952007-10-21 17:01:56 -07001732 !netif_subqueue_stopped(dev, skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 rc = 0;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001734 if (!dev_hard_start_xmit(skb, dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 HARD_TX_UNLOCK(dev);
1736 goto out;
1737 }
1738 }
1739 HARD_TX_UNLOCK(dev);
1740 if (net_ratelimit())
1741 printk(KERN_CRIT "Virtual device %s asks to "
1742 "queue packet!\n", dev->name);
1743 } else {
1744 /* Recursion is detected! It is possible,
1745 * unfortunately */
1746 if (net_ratelimit())
1747 printk(KERN_CRIT "Dead loop on virtual device "
1748 "%s, fix it urgently!\n", dev->name);
1749 }
1750 }
1751
1752 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07001753 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755out_kfree_skb:
1756 kfree_skb(skb);
1757 return rc;
1758out:
Herbert Xud4828d82006-06-22 02:28:18 -07001759 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return rc;
1761}
1762
1763
1764/*=======================================================================
1765 Receiver routines
1766 =======================================================================*/
1767
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07001768int netdev_max_backlog __read_mostly = 1000;
1769int netdev_budget __read_mostly = 300;
1770int weight_p __read_mostly = 64; /* old backlog weight */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
1773
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775/**
1776 * netif_rx - post buffer to the network code
1777 * @skb: buffer to post
1778 *
1779 * This function receives a packet from a device driver and queues it for
1780 * the upper (protocol) levels to process. It always succeeds. The buffer
1781 * may be dropped during processing for congestion control or by the
1782 * protocol layers.
1783 *
1784 * return values:
1785 * NET_RX_SUCCESS (no congestion)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 * NET_RX_DROP (packet was dropped)
1787 *
1788 */
1789
1790int netif_rx(struct sk_buff *skb)
1791{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 struct softnet_data *queue;
1793 unsigned long flags;
1794
1795 /* if netpoll wants it, pretend we never saw it */
1796 if (netpoll_rx(skb))
1797 return NET_RX_DROP;
1798
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07001799 if (!skb->tstamp.tv64)
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001800 net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 /*
1803 * The code is rearranged so that the path is the most
1804 * short when CPU is congested, but is still operating.
1805 */
1806 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 queue = &__get_cpu_var(softnet_data);
1808
1809 __get_cpu_var(netdev_rx_stat).total++;
1810 if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1811 if (queue->input_pkt_queue.qlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812enqueue:
1813 dev_hold(skb->dev);
1814 __skb_queue_tail(&queue->input_pkt_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 local_irq_restore(flags);
Stephen Hemminger34008d82005-06-23 20:10:00 -07001816 return NET_RX_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
1818
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001819 napi_schedule(&queue->backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 goto enqueue;
1821 }
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 __get_cpu_var(netdev_rx_stat).dropped++;
1824 local_irq_restore(flags);
1825
1826 kfree_skb(skb);
1827 return NET_RX_DROP;
1828}
1829
1830int netif_rx_ni(struct sk_buff *skb)
1831{
1832 int err;
1833
1834 preempt_disable();
1835 err = netif_rx(skb);
1836 if (local_softirq_pending())
1837 do_softirq();
1838 preempt_enable();
1839
1840 return err;
1841}
1842
1843EXPORT_SYMBOL(netif_rx_ni);
1844
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001845static inline struct net_device *skb_bond(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
1847 struct net_device *dev = skb->dev;
1848
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001849 if (dev->master) {
David S. Miller7ea49ed2006-08-14 17:08:36 -07001850 if (skb_bond_should_drop(skb)) {
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001851 kfree_skb(skb);
1852 return NULL;
1853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 skb->dev = dev->master;
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001855 }
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001856
1857 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
1859
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861static void net_tx_action(struct softirq_action *h)
1862{
1863 struct softnet_data *sd = &__get_cpu_var(softnet_data);
1864
1865 if (sd->completion_queue) {
1866 struct sk_buff *clist;
1867
1868 local_irq_disable();
1869 clist = sd->completion_queue;
1870 sd->completion_queue = NULL;
1871 local_irq_enable();
1872
1873 while (clist) {
1874 struct sk_buff *skb = clist;
1875 clist = clist->next;
1876
1877 BUG_TRAP(!atomic_read(&skb->users));
1878 __kfree_skb(skb);
1879 }
1880 }
1881
1882 if (sd->output_queue) {
1883 struct net_device *head;
1884
1885 local_irq_disable();
1886 head = sd->output_queue;
1887 sd->output_queue = NULL;
1888 local_irq_enable();
1889
1890 while (head) {
1891 struct net_device *dev = head;
1892 head = head->next_sched;
1893
1894 smp_mb__before_clear_bit();
1895 clear_bit(__LINK_STATE_SCHED, &dev->state);
1896
1897 if (spin_trylock(&dev->queue_lock)) {
1898 qdisc_run(dev);
1899 spin_unlock(&dev->queue_lock);
1900 } else {
1901 netif_schedule(dev);
1902 }
1903 }
1904 }
1905}
1906
Stephen Hemminger6f05f622007-03-08 20:46:03 -08001907static inline int deliver_skb(struct sk_buff *skb,
1908 struct packet_type *pt_prev,
1909 struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
1911 atomic_inc(&skb->users);
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001912 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914
1915#if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
Stephen Hemminger6229e362007-03-21 13:38:47 -07001916/* These hooks defined here for ATM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917struct net_bridge;
1918struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
1919 unsigned char *addr);
Stephen Hemminger6229e362007-03-21 13:38:47 -07001920void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent) __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Stephen Hemminger6229e362007-03-21 13:38:47 -07001922/*
1923 * If bridge module is loaded call bridging hook.
1924 * returns NULL if packet was consumed.
1925 */
1926struct sk_buff *(*br_handle_frame_hook)(struct net_bridge_port *p,
1927 struct sk_buff *skb) __read_mostly;
1928static inline struct sk_buff *handle_bridge(struct sk_buff *skb,
1929 struct packet_type **pt_prev, int *ret,
1930 struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 struct net_bridge_port *port;
1933
Stephen Hemminger6229e362007-03-21 13:38:47 -07001934 if (skb->pkt_type == PACKET_LOOPBACK ||
1935 (port = rcu_dereference(skb->dev->br_port)) == NULL)
1936 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 if (*pt_prev) {
Stephen Hemminger6229e362007-03-21 13:38:47 -07001939 *ret = deliver_skb(skb, *pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 *pt_prev = NULL;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001941 }
1942
Stephen Hemminger6229e362007-03-21 13:38:47 -07001943 return br_handle_frame_hook(port, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945#else
Stephen Hemminger6229e362007-03-21 13:38:47 -07001946#define handle_bridge(skb, pt_prev, ret, orig_dev) (skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947#endif
1948
Patrick McHardyb863ceb2007-07-14 18:55:06 -07001949#if defined(CONFIG_MACVLAN) || defined(CONFIG_MACVLAN_MODULE)
1950struct sk_buff *(*macvlan_handle_frame_hook)(struct sk_buff *skb) __read_mostly;
1951EXPORT_SYMBOL_GPL(macvlan_handle_frame_hook);
1952
1953static inline struct sk_buff *handle_macvlan(struct sk_buff *skb,
1954 struct packet_type **pt_prev,
1955 int *ret,
1956 struct net_device *orig_dev)
1957{
1958 if (skb->dev->macvlan_port == NULL)
1959 return skb;
1960
1961 if (*pt_prev) {
1962 *ret = deliver_skb(skb, *pt_prev, orig_dev);
1963 *pt_prev = NULL;
1964 }
1965 return macvlan_handle_frame_hook(skb);
1966}
1967#else
1968#define handle_macvlan(skb, pt_prev, ret, orig_dev) (skb)
1969#endif
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971#ifdef CONFIG_NET_CLS_ACT
1972/* TODO: Maybe we should just force sch_ingress to be compiled in
1973 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
1974 * a compare and 2 stores extra right now if we dont have it on
1975 * but have CONFIG_NET_CLS_ACT
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001976 * NOTE: This doesnt stop any functionality; if you dont have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 * the ingress scheduler, you just cant add policies on ingress.
1978 *
1979 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001980static int ing_filter(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 struct Qdisc *q;
1983 struct net_device *dev = skb->dev;
1984 int result = TC_ACT_OK;
Herbert Xuf697c3e2007-10-14 00:38:47 -07001985 u32 ttl = G_TC_RTTL(skb->tc_verd);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001986
Herbert Xuf697c3e2007-10-14 00:38:47 -07001987 if (MAX_RED_LOOP < ttl++) {
1988 printk(KERN_WARNING
1989 "Redir loop detected Dropping packet (%d->%d)\n",
1990 skb->iif, dev->ifindex);
1991 return TC_ACT_SHOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
1993
Herbert Xuf697c3e2007-10-14 00:38:47 -07001994 skb->tc_verd = SET_TC_RTTL(skb->tc_verd, ttl);
1995 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_INGRESS);
1996
1997 spin_lock(&dev->ingress_lock);
1998 if ((q = dev->qdisc_ingress) != NULL)
1999 result = q->enqueue(skb, q);
2000 spin_unlock(&dev->ingress_lock);
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return result;
2003}
Herbert Xuf697c3e2007-10-14 00:38:47 -07002004
2005static inline struct sk_buff *handle_ing(struct sk_buff *skb,
2006 struct packet_type **pt_prev,
2007 int *ret, struct net_device *orig_dev)
2008{
2009 if (!skb->dev->qdisc_ingress)
2010 goto out;
2011
2012 if (*pt_prev) {
2013 *ret = deliver_skb(skb, *pt_prev, orig_dev);
2014 *pt_prev = NULL;
2015 } else {
2016 /* Huh? Why does turning on AF_PACKET affect this? */
2017 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
2018 }
2019
2020 switch (ing_filter(skb)) {
2021 case TC_ACT_SHOT:
2022 case TC_ACT_STOLEN:
2023 kfree_skb(skb);
2024 return NULL;
2025 }
2026
2027out:
2028 skb->tc_verd = 0;
2029 return skb;
2030}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031#endif
2032
Stephen Hemminger3b582cc2007-11-01 02:21:47 -07002033/**
2034 * netif_receive_skb - process receive buffer from network
2035 * @skb: buffer to process
2036 *
2037 * netif_receive_skb() is the main receive data processing function.
2038 * It always succeeds. The buffer may be dropped during processing
2039 * for congestion control or by the protocol layers.
2040 *
2041 * This function may only be called from softirq context and interrupts
2042 * should be enabled.
2043 *
2044 * Return values (usually ignored):
2045 * NET_RX_SUCCESS: no congestion
2046 * NET_RX_DROP: packet was dropped
2047 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048int netif_receive_skb(struct sk_buff *skb)
2049{
2050 struct packet_type *ptype, *pt_prev;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002051 struct net_device *orig_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 int ret = NET_RX_DROP;
Al Viro252e3342006-11-14 20:48:11 -08002053 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 /* if we've gotten here through NAPI, check netpoll */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002056 if (netpoll_receive_skb(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return NET_RX_DROP;
2058
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002059 if (!skb->tstamp.tv64)
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002060 net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Patrick McHardyc01003c2007-03-29 11:46:52 -07002062 if (!skb->iif)
2063 skb->iif = skb->dev->ifindex;
David S. Miller86e65da2005-08-09 19:36:29 -07002064
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002065 orig_dev = skb_bond(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002067 if (!orig_dev)
2068 return NET_RX_DROP;
2069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 __get_cpu_var(netdev_rx_stat).total++;
2071
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07002072 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03002073 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002074 skb->mac_len = skb->network_header - skb->mac_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 pt_prev = NULL;
2077
2078 rcu_read_lock();
2079
2080#ifdef CONFIG_NET_CLS_ACT
2081 if (skb->tc_verd & TC_NCLS) {
2082 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
2083 goto ncls;
2084 }
2085#endif
2086
2087 list_for_each_entry_rcu(ptype, &ptype_all, list) {
2088 if (!ptype->dev || ptype->dev == skb->dev) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002089 if (pt_prev)
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002090 ret = deliver_skb(skb, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 pt_prev = ptype;
2092 }
2093 }
2094
2095#ifdef CONFIG_NET_CLS_ACT
Herbert Xuf697c3e2007-10-14 00:38:47 -07002096 skb = handle_ing(skb, &pt_prev, &ret, orig_dev);
2097 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099ncls:
2100#endif
2101
Stephen Hemminger6229e362007-03-21 13:38:47 -07002102 skb = handle_bridge(skb, &pt_prev, &ret, orig_dev);
2103 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 goto out;
Patrick McHardyb863ceb2007-07-14 18:55:06 -07002105 skb = handle_macvlan(skb, &pt_prev, &ret, orig_dev);
2106 if (!skb)
2107 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 type = skb->protocol;
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08002110 list_for_each_entry_rcu(ptype,
2111 &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (ptype->type == type &&
2113 (!ptype->dev || ptype->dev == skb->dev)) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002114 if (pt_prev)
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002115 ret = deliver_skb(skb, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 pt_prev = ptype;
2117 }
2118 }
2119
2120 if (pt_prev) {
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002121 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 } else {
2123 kfree_skb(skb);
2124 /* Jamal, now you will not able to escape explaining
2125 * me how you were going to use this. :-)
2126 */
2127 ret = NET_RX_DROP;
2128 }
2129
2130out:
2131 rcu_read_unlock();
2132 return ret;
2133}
2134
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002135static int process_backlog(struct napi_struct *napi, int quota)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
2137 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 struct softnet_data *queue = &__get_cpu_var(softnet_data);
2139 unsigned long start_time = jiffies;
2140
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002141 napi->weight = weight_p;
2142 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 struct sk_buff *skb;
2144 struct net_device *dev;
2145
2146 local_irq_disable();
2147 skb = __skb_dequeue(&queue->input_pkt_queue);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002148 if (!skb) {
2149 __napi_complete(napi);
2150 local_irq_enable();
2151 break;
2152 }
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 local_irq_enable();
2155
2156 dev = skb->dev;
2157
2158 netif_receive_skb(skb);
2159
2160 dev_put(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002161 } while (++work < quota && jiffies == start_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002163 return work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002166/**
2167 * __napi_schedule - schedule for receive
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07002168 * @n: entry to schedule
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002169 *
2170 * The entry's receive function will be scheduled to run
2171 */
Harvey Harrisonb5606c22008-02-13 15:03:16 -08002172void __napi_schedule(struct napi_struct *n)
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002173{
2174 unsigned long flags;
2175
2176 local_irq_save(flags);
2177 list_add_tail(&n->poll_list, &__get_cpu_var(softnet_data).poll_list);
2178 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2179 local_irq_restore(flags);
2180}
2181EXPORT_SYMBOL(__napi_schedule);
2182
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184static void net_rx_action(struct softirq_action *h)
2185{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002186 struct list_head *list = &__get_cpu_var(softnet_data).poll_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 unsigned long start_time = jiffies;
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07002188 int budget = netdev_budget;
Matt Mackall53fb95d2005-08-11 19:27:43 -07002189 void *have;
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 local_irq_disable();
2192
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002193 while (!list_empty(list)) {
2194 struct napi_struct *n;
2195 int work, weight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002197 /* If softirq window is exhuasted then punt.
2198 *
2199 * Note that this is a slight policy change from the
2200 * previous NAPI code, which would allow up to 2
2201 * jiffies to pass before breaking out. The test
2202 * used to be "jiffies - start_time > 1".
2203 */
2204 if (unlikely(budget <= 0 || jiffies != start_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 goto softnet_break;
2206
2207 local_irq_enable();
2208
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002209 /* Even though interrupts have been re-enabled, this
2210 * access is safe because interrupts can only add new
2211 * entries to the tail of this list, and only ->poll()
2212 * calls can remove this head entry from the list.
2213 */
2214 n = list_entry(list->next, struct napi_struct, poll_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002216 have = netpoll_poll_lock(n);
2217
2218 weight = n->weight;
2219
David S. Miller0a7606c2007-10-29 21:28:47 -07002220 /* This NAPI_STATE_SCHED test is for avoiding a race
2221 * with netpoll's poll_napi(). Only the entity which
2222 * obtains the lock and sees NAPI_STATE_SCHED set will
2223 * actually make the ->poll() call. Therefore we avoid
2224 * accidently calling ->poll() when NAPI is not scheduled.
2225 */
2226 work = 0;
2227 if (test_bit(NAPI_STATE_SCHED, &n->state))
2228 work = n->poll(n, weight);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002229
2230 WARN_ON_ONCE(work > weight);
2231
2232 budget -= work;
2233
2234 local_irq_disable();
2235
2236 /* Drivers must not modify the NAPI state if they
2237 * consume the entire weight. In such cases this code
2238 * still "owns" the NAPI instance and therefore can
2239 * move the instance around on the list at-will.
2240 */
David S. Millerfed17f32008-01-07 21:00:40 -08002241 if (unlikely(work == weight)) {
2242 if (unlikely(napi_disable_pending(n)))
2243 __napi_complete(n);
2244 else
2245 list_move_tail(&n->poll_list, list);
2246 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002247
2248 netpoll_poll_unlock(have);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
2250out:
Shannon Nelson515e06c2007-06-23 23:09:23 -07002251 local_irq_enable();
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002252
Chris Leechdb217332006-06-17 21:24:58 -07002253#ifdef CONFIG_NET_DMA
2254 /*
2255 * There may not be any more sk_buffs coming right now, so push
2256 * any pending DMA copies to hardware
2257 */
Dan Williamsd379b012007-07-09 11:56:42 -07002258 if (!cpus_empty(net_dma.channel_mask)) {
2259 int chan_idx;
2260 for_each_cpu_mask(chan_idx, net_dma.channel_mask) {
2261 struct dma_chan *chan = net_dma.channels[chan_idx];
2262 if (chan)
2263 dma_async_memcpy_issue_pending(chan);
2264 }
Chris Leechdb217332006-06-17 21:24:58 -07002265 }
2266#endif
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return;
2269
2270softnet_break:
2271 __get_cpu_var(netdev_rx_stat).time_squeeze++;
2272 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2273 goto out;
2274}
2275
2276static gifconf_func_t * gifconf_list [NPROTO];
2277
2278/**
2279 * register_gifconf - register a SIOCGIF handler
2280 * @family: Address family
2281 * @gifconf: Function handler
2282 *
2283 * Register protocol dependent address dumping routines. The handler
2284 * that is passed must not be freed or reused until it has been replaced
2285 * by another handler.
2286 */
2287int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
2288{
2289 if (family >= NPROTO)
2290 return -EINVAL;
2291 gifconf_list[family] = gifconf;
2292 return 0;
2293}
2294
2295
2296/*
2297 * Map an interface index to its name (SIOCGIFNAME)
2298 */
2299
2300/*
2301 * We need this ioctl for efficient implementation of the
2302 * if_indextoname() function required by the IPv6 API. Without
2303 * it, we would have to search all the interfaces to find a
2304 * match. --pb
2305 */
2306
Eric W. Biederman881d9662007-09-17 11:56:21 -07002307static int dev_ifname(struct net *net, struct ifreq __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
2309 struct net_device *dev;
2310 struct ifreq ifr;
2311
2312 /*
2313 * Fetch the caller's info block.
2314 */
2315
2316 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2317 return -EFAULT;
2318
2319 read_lock(&dev_base_lock);
Eric W. Biederman881d9662007-09-17 11:56:21 -07002320 dev = __dev_get_by_index(net, ifr.ifr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (!dev) {
2322 read_unlock(&dev_base_lock);
2323 return -ENODEV;
2324 }
2325
2326 strcpy(ifr.ifr_name, dev->name);
2327 read_unlock(&dev_base_lock);
2328
2329 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2330 return -EFAULT;
2331 return 0;
2332}
2333
2334/*
2335 * Perform a SIOCGIFCONF call. This structure will change
2336 * size eventually, and there is nothing I can do about it.
2337 * Thus we will need a 'compatibility mode'.
2338 */
2339
Eric W. Biederman881d9662007-09-17 11:56:21 -07002340static int dev_ifconf(struct net *net, char __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
2342 struct ifconf ifc;
2343 struct net_device *dev;
2344 char __user *pos;
2345 int len;
2346 int total;
2347 int i;
2348
2349 /*
2350 * Fetch the caller's info block.
2351 */
2352
2353 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
2354 return -EFAULT;
2355
2356 pos = ifc.ifc_buf;
2357 len = ifc.ifc_len;
2358
2359 /*
2360 * Loop over the interfaces, and write an info block for each.
2361 */
2362
2363 total = 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -07002364 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 for (i = 0; i < NPROTO; i++) {
2366 if (gifconf_list[i]) {
2367 int done;
2368 if (!pos)
2369 done = gifconf_list[i](dev, NULL, 0);
2370 else
2371 done = gifconf_list[i](dev, pos + total,
2372 len - total);
2373 if (done < 0)
2374 return -EFAULT;
2375 total += done;
2376 }
2377 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380 /*
2381 * All done. Write the updated control block back to the caller.
2382 */
2383 ifc.ifc_len = total;
2384
2385 /*
2386 * Both BSD and Solaris return 0 here, so we do too.
2387 */
2388 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
2389}
2390
2391#ifdef CONFIG_PROC_FS
2392/*
2393 * This is invoked by the /proc filesystem handler to display a device
2394 * in detail.
2395 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396void *dev_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002397 __acquires(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
Denis V. Luneve372c412007-11-19 22:31:54 -08002399 struct net *net = seq_file_net(seq);
Pavel Emelianov7562f872007-05-03 15:13:45 -07002400 loff_t off;
2401 struct net_device *dev;
2402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 read_lock(&dev_base_lock);
Pavel Emelianov7562f872007-05-03 15:13:45 -07002404 if (!*pos)
2405 return SEQ_START_TOKEN;
2406
2407 off = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07002408 for_each_netdev(net, dev)
Pavel Emelianov7562f872007-05-03 15:13:45 -07002409 if (off++ == *pos)
2410 return dev;
2411
2412 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414
2415void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2416{
Denis V. Luneve372c412007-11-19 22:31:54 -08002417 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 ++*pos;
Pavel Emelianov7562f872007-05-03 15:13:45 -07002419 return v == SEQ_START_TOKEN ?
Eric W. Biederman881d9662007-09-17 11:56:21 -07002420 first_net_device(net) : next_net_device((struct net_device *)v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
2422
2423void dev_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002424 __releases(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
2426 read_unlock(&dev_base_lock);
2427}
2428
2429static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
2430{
Rusty Russellc45d2862007-03-28 14:29:08 -07002431 struct net_device_stats *stats = dev->get_stats(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Rusty Russell5a1b5892007-04-28 21:04:03 -07002433 seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
2434 "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
2435 dev->name, stats->rx_bytes, stats->rx_packets,
2436 stats->rx_errors,
2437 stats->rx_dropped + stats->rx_missed_errors,
2438 stats->rx_fifo_errors,
2439 stats->rx_length_errors + stats->rx_over_errors +
2440 stats->rx_crc_errors + stats->rx_frame_errors,
2441 stats->rx_compressed, stats->multicast,
2442 stats->tx_bytes, stats->tx_packets,
2443 stats->tx_errors, stats->tx_dropped,
2444 stats->tx_fifo_errors, stats->collisions,
2445 stats->tx_carrier_errors +
2446 stats->tx_aborted_errors +
2447 stats->tx_window_errors +
2448 stats->tx_heartbeat_errors,
2449 stats->tx_compressed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
2451
2452/*
2453 * Called from the PROCfs module. This now uses the new arbitrary sized
2454 * /proc/net interface to create /proc/net/dev
2455 */
2456static int dev_seq_show(struct seq_file *seq, void *v)
2457{
2458 if (v == SEQ_START_TOKEN)
2459 seq_puts(seq, "Inter-| Receive "
2460 " | Transmit\n"
2461 " face |bytes packets errs drop fifo frame "
2462 "compressed multicast|bytes packets errs "
2463 "drop fifo colls carrier compressed\n");
2464 else
2465 dev_seq_printf_stats(seq, v);
2466 return 0;
2467}
2468
2469static struct netif_rx_stats *softnet_get_online(loff_t *pos)
2470{
2471 struct netif_rx_stats *rc = NULL;
2472
Mike Travis0c0b0ac2008-05-02 16:43:08 -07002473 while (*pos < nr_cpu_ids)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002474 if (cpu_online(*pos)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 rc = &per_cpu(netdev_rx_stat, *pos);
2476 break;
2477 } else
2478 ++*pos;
2479 return rc;
2480}
2481
2482static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
2483{
2484 return softnet_get_online(pos);
2485}
2486
2487static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2488{
2489 ++*pos;
2490 return softnet_get_online(pos);
2491}
2492
2493static void softnet_seq_stop(struct seq_file *seq, void *v)
2494{
2495}
2496
2497static int softnet_seq_show(struct seq_file *seq, void *v)
2498{
2499 struct netif_rx_stats *s = v;
2500
2501 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
Stephen Hemminger31aa02c2005-06-23 20:12:48 -07002502 s->total, s->dropped, s->time_squeeze, 0,
Stephen Hemmingerc1ebcdb2005-06-23 20:08:59 -07002503 0, 0, 0, 0, /* was fastroute */
2504 s->cpu_collision );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 return 0;
2506}
2507
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002508static const struct seq_operations dev_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 .start = dev_seq_start,
2510 .next = dev_seq_next,
2511 .stop = dev_seq_stop,
2512 .show = dev_seq_show,
2513};
2514
2515static int dev_seq_open(struct inode *inode, struct file *file)
2516{
Denis V. Luneve372c412007-11-19 22:31:54 -08002517 return seq_open_net(inode, file, &dev_seq_ops,
2518 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519}
2520
Arjan van de Ven9a321442007-02-12 00:55:35 -08002521static const struct file_operations dev_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 .owner = THIS_MODULE,
2523 .open = dev_seq_open,
2524 .read = seq_read,
2525 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08002526 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527};
2528
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002529static const struct seq_operations softnet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 .start = softnet_seq_start,
2531 .next = softnet_seq_next,
2532 .stop = softnet_seq_stop,
2533 .show = softnet_seq_show,
2534};
2535
2536static int softnet_seq_open(struct inode *inode, struct file *file)
2537{
2538 return seq_open(file, &softnet_seq_ops);
2539}
2540
Arjan van de Ven9a321442007-02-12 00:55:35 -08002541static const struct file_operations softnet_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 .owner = THIS_MODULE,
2543 .open = softnet_seq_open,
2544 .read = seq_read,
2545 .llseek = seq_lseek,
2546 .release = seq_release,
2547};
2548
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002549static void *ptype_get_idx(loff_t pos)
2550{
2551 struct packet_type *pt = NULL;
2552 loff_t i = 0;
2553 int t;
2554
2555 list_for_each_entry_rcu(pt, &ptype_all, list) {
2556 if (i == pos)
2557 return pt;
2558 ++i;
2559 }
2560
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08002561 for (t = 0; t < PTYPE_HASH_SIZE; t++) {
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002562 list_for_each_entry_rcu(pt, &ptype_base[t], list) {
2563 if (i == pos)
2564 return pt;
2565 ++i;
2566 }
2567 }
2568 return NULL;
2569}
2570
2571static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemminger72348a42008-01-21 02:27:29 -08002572 __acquires(RCU)
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002573{
2574 rcu_read_lock();
2575 return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN;
2576}
2577
2578static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2579{
2580 struct packet_type *pt;
2581 struct list_head *nxt;
2582 int hash;
2583
2584 ++*pos;
2585 if (v == SEQ_START_TOKEN)
2586 return ptype_get_idx(0);
2587
2588 pt = v;
2589 nxt = pt->list.next;
2590 if (pt->type == htons(ETH_P_ALL)) {
2591 if (nxt != &ptype_all)
2592 goto found;
2593 hash = 0;
2594 nxt = ptype_base[0].next;
2595 } else
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08002596 hash = ntohs(pt->type) & PTYPE_HASH_MASK;
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002597
2598 while (nxt == &ptype_base[hash]) {
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08002599 if (++hash >= PTYPE_HASH_SIZE)
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002600 return NULL;
2601 nxt = ptype_base[hash].next;
2602 }
2603found:
2604 return list_entry(nxt, struct packet_type, list);
2605}
2606
2607static void ptype_seq_stop(struct seq_file *seq, void *v)
Stephen Hemminger72348a42008-01-21 02:27:29 -08002608 __releases(RCU)
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002609{
2610 rcu_read_unlock();
2611}
2612
2613static void ptype_seq_decode(struct seq_file *seq, void *sym)
2614{
2615#ifdef CONFIG_KALLSYMS
2616 unsigned long offset = 0, symsize;
2617 const char *symname;
2618 char *modname;
2619 char namebuf[128];
2620
2621 symname = kallsyms_lookup((unsigned long)sym, &symsize, &offset,
2622 &modname, namebuf);
2623
2624 if (symname) {
2625 char *delim = ":";
2626
2627 if (!modname)
2628 modname = delim = "";
2629 seq_printf(seq, "%s%s%s%s+0x%lx", delim, modname, delim,
2630 symname, offset);
2631 return;
2632 }
2633#endif
2634
2635 seq_printf(seq, "[%p]", sym);
2636}
2637
2638static int ptype_seq_show(struct seq_file *seq, void *v)
2639{
2640 struct packet_type *pt = v;
2641
2642 if (v == SEQ_START_TOKEN)
2643 seq_puts(seq, "Type Device Function\n");
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002644 else if (pt->dev == NULL || dev_net(pt->dev) == seq_file_net(seq)) {
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002645 if (pt->type == htons(ETH_P_ALL))
2646 seq_puts(seq, "ALL ");
2647 else
2648 seq_printf(seq, "%04x", ntohs(pt->type));
2649
2650 seq_printf(seq, " %-8s ",
2651 pt->dev ? pt->dev->name : "");
2652 ptype_seq_decode(seq, pt->func);
2653 seq_putc(seq, '\n');
2654 }
2655
2656 return 0;
2657}
2658
2659static const struct seq_operations ptype_seq_ops = {
2660 .start = ptype_seq_start,
2661 .next = ptype_seq_next,
2662 .stop = ptype_seq_stop,
2663 .show = ptype_seq_show,
2664};
2665
2666static int ptype_seq_open(struct inode *inode, struct file *file)
2667{
Pavel Emelyanov2feb27d2008-03-24 14:57:45 -07002668 return seq_open_net(inode, file, &ptype_seq_ops,
2669 sizeof(struct seq_net_private));
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002670}
2671
2672static const struct file_operations ptype_seq_fops = {
2673 .owner = THIS_MODULE,
2674 .open = ptype_seq_open,
2675 .read = seq_read,
2676 .llseek = seq_lseek,
Pavel Emelyanov2feb27d2008-03-24 14:57:45 -07002677 .release = seq_release_net,
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002678};
2679
2680
Pavel Emelyanov46650792007-10-08 20:38:39 -07002681static int __net_init dev_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682{
2683 int rc = -ENOMEM;
2684
Eric W. Biederman881d9662007-09-17 11:56:21 -07002685 if (!proc_net_fops_create(net, "dev", S_IRUGO, &dev_seq_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 goto out;
Eric W. Biederman881d9662007-09-17 11:56:21 -07002687 if (!proc_net_fops_create(net, "softnet_stat", S_IRUGO, &softnet_seq_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 goto out_dev;
Eric W. Biederman881d9662007-09-17 11:56:21 -07002689 if (!proc_net_fops_create(net, "ptype", S_IRUGO, &ptype_seq_fops))
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02002690 goto out_softnet;
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002691
Eric W. Biederman881d9662007-09-17 11:56:21 -07002692 if (wext_proc_init(net))
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02002693 goto out_ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 rc = 0;
2695out:
2696 return rc;
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02002697out_ptype:
Eric W. Biederman881d9662007-09-17 11:56:21 -07002698 proc_net_remove(net, "ptype");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699out_softnet:
Eric W. Biederman881d9662007-09-17 11:56:21 -07002700 proc_net_remove(net, "softnet_stat");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701out_dev:
Eric W. Biederman881d9662007-09-17 11:56:21 -07002702 proc_net_remove(net, "dev");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 goto out;
2704}
Eric W. Biederman881d9662007-09-17 11:56:21 -07002705
Pavel Emelyanov46650792007-10-08 20:38:39 -07002706static void __net_exit dev_proc_net_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002707{
2708 wext_proc_exit(net);
2709
2710 proc_net_remove(net, "ptype");
2711 proc_net_remove(net, "softnet_stat");
2712 proc_net_remove(net, "dev");
2713}
2714
Denis V. Lunev022cbae2007-11-13 03:23:50 -08002715static struct pernet_operations __net_initdata dev_proc_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -07002716 .init = dev_proc_net_init,
2717 .exit = dev_proc_net_exit,
2718};
2719
2720static int __init dev_proc_init(void)
2721{
2722 return register_pernet_subsys(&dev_proc_ops);
2723}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724#else
2725#define dev_proc_init() 0
2726#endif /* CONFIG_PROC_FS */
2727
2728
2729/**
2730 * netdev_set_master - set up master/slave pair
2731 * @slave: slave device
2732 * @master: new master device
2733 *
2734 * Changes the master device of the slave. Pass %NULL to break the
2735 * bonding. The caller must hold the RTNL semaphore. On a failure
2736 * a negative errno code is returned. On success the reference counts
2737 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
2738 * function returns zero.
2739 */
2740int netdev_set_master(struct net_device *slave, struct net_device *master)
2741{
2742 struct net_device *old = slave->master;
2743
2744 ASSERT_RTNL();
2745
2746 if (master) {
2747 if (old)
2748 return -EBUSY;
2749 dev_hold(master);
2750 }
2751
2752 slave->master = master;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 synchronize_net();
2755
2756 if (old)
2757 dev_put(old);
2758
2759 if (master)
2760 slave->flags |= IFF_SLAVE;
2761 else
2762 slave->flags &= ~IFF_SLAVE;
2763
2764 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
2765 return 0;
2766}
2767
Patrick McHardy4417da62007-06-27 01:28:10 -07002768static void __dev_set_promiscuity(struct net_device *dev, int inc)
2769{
2770 unsigned short old_flags = dev->flags;
2771
Patrick McHardy24023452007-07-14 18:51:31 -07002772 ASSERT_RTNL();
2773
Patrick McHardy4417da62007-06-27 01:28:10 -07002774 if ((dev->promiscuity += inc) == 0)
2775 dev->flags &= ~IFF_PROMISC;
2776 else
2777 dev->flags |= IFF_PROMISC;
2778 if (dev->flags != old_flags) {
2779 printk(KERN_INFO "device %s %s promiscuous mode\n",
2780 dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
2781 "left");
Klaus Heinrich Kiwi7759db82008-01-23 22:57:45 -05002782 if (audit_enabled)
2783 audit_log(current->audit_context, GFP_ATOMIC,
2784 AUDIT_ANOM_PROMISCUOUS,
2785 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
2786 dev->name, (dev->flags & IFF_PROMISC),
2787 (old_flags & IFF_PROMISC),
2788 audit_get_loginuid(current),
2789 current->uid, current->gid,
2790 audit_get_sessionid(current));
Patrick McHardy24023452007-07-14 18:51:31 -07002791
2792 if (dev->change_rx_flags)
2793 dev->change_rx_flags(dev, IFF_PROMISC);
Patrick McHardy4417da62007-06-27 01:28:10 -07002794 }
2795}
2796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797/**
2798 * dev_set_promiscuity - update promiscuity count on a device
2799 * @dev: device
2800 * @inc: modifier
2801 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07002802 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 * remains above zero the interface remains promiscuous. Once it hits zero
2804 * the device reverts back to normal filtering operation. A negative inc
2805 * value is used to drop promiscuity on the device.
2806 */
2807void dev_set_promiscuity(struct net_device *dev, int inc)
2808{
2809 unsigned short old_flags = dev->flags;
2810
Patrick McHardy4417da62007-06-27 01:28:10 -07002811 __dev_set_promiscuity(dev, inc);
2812 if (dev->flags != old_flags)
2813 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814}
2815
2816/**
2817 * dev_set_allmulti - update allmulti count on a device
2818 * @dev: device
2819 * @inc: modifier
2820 *
2821 * Add or remove reception of all multicast frames to a device. While the
2822 * count in the device remains above zero the interface remains listening
2823 * to all interfaces. Once it hits zero the device reverts back to normal
2824 * filtering operation. A negative @inc value is used to drop the counter
2825 * when releasing a resource needing all multicasts.
2826 */
2827
2828void dev_set_allmulti(struct net_device *dev, int inc)
2829{
2830 unsigned short old_flags = dev->flags;
2831
Patrick McHardy24023452007-07-14 18:51:31 -07002832 ASSERT_RTNL();
2833
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 dev->flags |= IFF_ALLMULTI;
2835 if ((dev->allmulti += inc) == 0)
2836 dev->flags &= ~IFF_ALLMULTI;
Patrick McHardy24023452007-07-14 18:51:31 -07002837 if (dev->flags ^ old_flags) {
2838 if (dev->change_rx_flags)
2839 dev->change_rx_flags(dev, IFF_ALLMULTI);
Patrick McHardy4417da62007-06-27 01:28:10 -07002840 dev_set_rx_mode(dev);
Patrick McHardy24023452007-07-14 18:51:31 -07002841 }
Patrick McHardy4417da62007-06-27 01:28:10 -07002842}
2843
2844/*
2845 * Upload unicast and multicast address lists to device and
2846 * configure RX filtering. When the device doesn't support unicast
Joe Perches53ccaae2007-12-20 14:02:06 -08002847 * filtering it is put in promiscuous mode while unicast addresses
Patrick McHardy4417da62007-06-27 01:28:10 -07002848 * are present.
2849 */
2850void __dev_set_rx_mode(struct net_device *dev)
2851{
2852 /* dev_open will call this function so the list will stay sane. */
2853 if (!(dev->flags&IFF_UP))
2854 return;
2855
2856 if (!netif_device_present(dev))
YOSHIFUJI Hideaki40b77c92007-07-19 10:43:23 +09002857 return;
Patrick McHardy4417da62007-06-27 01:28:10 -07002858
2859 if (dev->set_rx_mode)
2860 dev->set_rx_mode(dev);
2861 else {
2862 /* Unicast addresses changes may only happen under the rtnl,
2863 * therefore calling __dev_set_promiscuity here is safe.
2864 */
2865 if (dev->uc_count > 0 && !dev->uc_promisc) {
2866 __dev_set_promiscuity(dev, 1);
2867 dev->uc_promisc = 1;
2868 } else if (dev->uc_count == 0 && dev->uc_promisc) {
2869 __dev_set_promiscuity(dev, -1);
2870 dev->uc_promisc = 0;
2871 }
2872
2873 if (dev->set_multicast_list)
2874 dev->set_multicast_list(dev);
2875 }
2876}
2877
2878void dev_set_rx_mode(struct net_device *dev)
2879{
2880 netif_tx_lock_bh(dev);
2881 __dev_set_rx_mode(dev);
2882 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}
2884
Patrick McHardy61cbc2f2007-06-30 13:35:52 -07002885int __dev_addr_delete(struct dev_addr_list **list, int *count,
2886 void *addr, int alen, int glbl)
Patrick McHardybf742482007-06-27 01:26:19 -07002887{
2888 struct dev_addr_list *da;
2889
2890 for (; (da = *list) != NULL; list = &da->next) {
2891 if (memcmp(da->da_addr, addr, da->da_addrlen) == 0 &&
2892 alen == da->da_addrlen) {
2893 if (glbl) {
2894 int old_glbl = da->da_gusers;
2895 da->da_gusers = 0;
2896 if (old_glbl == 0)
2897 break;
2898 }
2899 if (--da->da_users)
2900 return 0;
2901
2902 *list = da->next;
2903 kfree(da);
Patrick McHardy61cbc2f2007-06-30 13:35:52 -07002904 (*count)--;
Patrick McHardybf742482007-06-27 01:26:19 -07002905 return 0;
2906 }
2907 }
2908 return -ENOENT;
2909}
2910
Patrick McHardy61cbc2f2007-06-30 13:35:52 -07002911int __dev_addr_add(struct dev_addr_list **list, int *count,
2912 void *addr, int alen, int glbl)
Patrick McHardybf742482007-06-27 01:26:19 -07002913{
2914 struct dev_addr_list *da;
2915
2916 for (da = *list; da != NULL; da = da->next) {
2917 if (memcmp(da->da_addr, addr, da->da_addrlen) == 0 &&
2918 da->da_addrlen == alen) {
2919 if (glbl) {
2920 int old_glbl = da->da_gusers;
2921 da->da_gusers = 1;
2922 if (old_glbl)
2923 return 0;
2924 }
2925 da->da_users++;
2926 return 0;
2927 }
2928 }
2929
Jorge Boncompte [DTI2]12aa3432008-02-19 14:17:04 -08002930 da = kzalloc(sizeof(*da), GFP_ATOMIC);
Patrick McHardybf742482007-06-27 01:26:19 -07002931 if (da == NULL)
2932 return -ENOMEM;
2933 memcpy(da->da_addr, addr, alen);
2934 da->da_addrlen = alen;
2935 da->da_users = 1;
2936 da->da_gusers = glbl ? 1 : 0;
2937 da->next = *list;
2938 *list = da;
Patrick McHardy61cbc2f2007-06-30 13:35:52 -07002939 (*count)++;
Patrick McHardybf742482007-06-27 01:26:19 -07002940 return 0;
2941}
2942
Patrick McHardy4417da62007-06-27 01:28:10 -07002943/**
2944 * dev_unicast_delete - Release secondary unicast address.
2945 * @dev: device
Randy Dunlap0ed72ec2007-07-26 00:03:29 -07002946 * @addr: address to delete
2947 * @alen: length of @addr
Patrick McHardy4417da62007-06-27 01:28:10 -07002948 *
2949 * Release reference to a secondary unicast address and remove it
Randy Dunlap0ed72ec2007-07-26 00:03:29 -07002950 * from the device if the reference count drops to zero.
Patrick McHardy4417da62007-06-27 01:28:10 -07002951 *
2952 * The caller must hold the rtnl_mutex.
2953 */
2954int dev_unicast_delete(struct net_device *dev, void *addr, int alen)
2955{
2956 int err;
2957
2958 ASSERT_RTNL();
2959
2960 netif_tx_lock_bh(dev);
Patrick McHardy61cbc2f2007-06-30 13:35:52 -07002961 err = __dev_addr_delete(&dev->uc_list, &dev->uc_count, addr, alen, 0);
2962 if (!err)
Patrick McHardy4417da62007-06-27 01:28:10 -07002963 __dev_set_rx_mode(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07002964 netif_tx_unlock_bh(dev);
2965 return err;
2966}
2967EXPORT_SYMBOL(dev_unicast_delete);
2968
2969/**
2970 * dev_unicast_add - add a secondary unicast address
2971 * @dev: device
Randy Dunlap0ed72ec2007-07-26 00:03:29 -07002972 * @addr: address to delete
2973 * @alen: length of @addr
Patrick McHardy4417da62007-06-27 01:28:10 -07002974 *
2975 * Add a secondary unicast address to the device or increase
2976 * the reference count if it already exists.
2977 *
2978 * The caller must hold the rtnl_mutex.
2979 */
2980int dev_unicast_add(struct net_device *dev, void *addr, int alen)
2981{
2982 int err;
2983
2984 ASSERT_RTNL();
2985
2986 netif_tx_lock_bh(dev);
Patrick McHardy61cbc2f2007-06-30 13:35:52 -07002987 err = __dev_addr_add(&dev->uc_list, &dev->uc_count, addr, alen, 0);
2988 if (!err)
Patrick McHardy4417da62007-06-27 01:28:10 -07002989 __dev_set_rx_mode(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07002990 netif_tx_unlock_bh(dev);
2991 return err;
2992}
2993EXPORT_SYMBOL(dev_unicast_add);
2994
Chris Leeche83a2ea2008-01-31 16:53:23 -08002995int __dev_addr_sync(struct dev_addr_list **to, int *to_count,
2996 struct dev_addr_list **from, int *from_count)
2997{
2998 struct dev_addr_list *da, *next;
2999 int err = 0;
3000
3001 da = *from;
3002 while (da != NULL) {
3003 next = da->next;
3004 if (!da->da_synced) {
3005 err = __dev_addr_add(to, to_count,
3006 da->da_addr, da->da_addrlen, 0);
3007 if (err < 0)
3008 break;
3009 da->da_synced = 1;
3010 da->da_users++;
3011 } else if (da->da_users == 1) {
3012 __dev_addr_delete(to, to_count,
3013 da->da_addr, da->da_addrlen, 0);
3014 __dev_addr_delete(from, from_count,
3015 da->da_addr, da->da_addrlen, 0);
3016 }
3017 da = next;
3018 }
3019 return err;
3020}
3021
3022void __dev_addr_unsync(struct dev_addr_list **to, int *to_count,
3023 struct dev_addr_list **from, int *from_count)
3024{
3025 struct dev_addr_list *da, *next;
3026
3027 da = *from;
3028 while (da != NULL) {
3029 next = da->next;
3030 if (da->da_synced) {
3031 __dev_addr_delete(to, to_count,
3032 da->da_addr, da->da_addrlen, 0);
3033 da->da_synced = 0;
3034 __dev_addr_delete(from, from_count,
3035 da->da_addr, da->da_addrlen, 0);
3036 }
3037 da = next;
3038 }
3039}
3040
3041/**
3042 * dev_unicast_sync - Synchronize device's unicast list to another device
3043 * @to: destination device
3044 * @from: source device
3045 *
3046 * Add newly added addresses to the destination device and release
3047 * addresses that have no users left. The source device must be
3048 * locked by netif_tx_lock_bh.
3049 *
3050 * This function is intended to be called from the dev->set_rx_mode
3051 * function of layered software devices.
3052 */
3053int dev_unicast_sync(struct net_device *to, struct net_device *from)
3054{
3055 int err = 0;
3056
3057 netif_tx_lock_bh(to);
3058 err = __dev_addr_sync(&to->uc_list, &to->uc_count,
3059 &from->uc_list, &from->uc_count);
3060 if (!err)
3061 __dev_set_rx_mode(to);
3062 netif_tx_unlock_bh(to);
3063 return err;
3064}
3065EXPORT_SYMBOL(dev_unicast_sync);
3066
3067/**
Randy Dunlapbc2cda12008-02-13 15:03:25 -08003068 * dev_unicast_unsync - Remove synchronized addresses from the destination device
Chris Leeche83a2ea2008-01-31 16:53:23 -08003069 * @to: destination device
3070 * @from: source device
3071 *
3072 * Remove all addresses that were added to the destination device by
3073 * dev_unicast_sync(). This function is intended to be called from the
3074 * dev->stop function of layered software devices.
3075 */
3076void dev_unicast_unsync(struct net_device *to, struct net_device *from)
3077{
3078 netif_tx_lock_bh(from);
3079 netif_tx_lock_bh(to);
3080
3081 __dev_addr_unsync(&to->uc_list, &to->uc_count,
3082 &from->uc_list, &from->uc_count);
3083 __dev_set_rx_mode(to);
3084
3085 netif_tx_unlock_bh(to);
3086 netif_tx_unlock_bh(from);
3087}
3088EXPORT_SYMBOL(dev_unicast_unsync);
3089
Denis Cheng12972622007-07-18 02:12:56 -07003090static void __dev_addr_discard(struct dev_addr_list **list)
3091{
3092 struct dev_addr_list *tmp;
3093
3094 while (*list != NULL) {
3095 tmp = *list;
3096 *list = tmp->next;
3097 if (tmp->da_users > tmp->da_gusers)
3098 printk("__dev_addr_discard: address leakage! "
3099 "da_users=%d\n", tmp->da_users);
3100 kfree(tmp);
3101 }
3102}
3103
Denis Cheng26cc2522007-07-18 02:12:03 -07003104static void dev_addr_discard(struct net_device *dev)
Patrick McHardy4417da62007-06-27 01:28:10 -07003105{
3106 netif_tx_lock_bh(dev);
Denis Cheng26cc2522007-07-18 02:12:03 -07003107
Patrick McHardy4417da62007-06-27 01:28:10 -07003108 __dev_addr_discard(&dev->uc_list);
3109 dev->uc_count = 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07003110
Denis Cheng456ad752007-07-18 02:10:54 -07003111 __dev_addr_discard(&dev->mc_list);
3112 dev->mc_count = 0;
Denis Cheng26cc2522007-07-18 02:12:03 -07003113
Denis Cheng456ad752007-07-18 02:10:54 -07003114 netif_tx_unlock_bh(dev);
3115}
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117unsigned dev_get_flags(const struct net_device *dev)
3118{
3119 unsigned flags;
3120
3121 flags = (dev->flags & ~(IFF_PROMISC |
3122 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08003123 IFF_RUNNING |
3124 IFF_LOWER_UP |
3125 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 (dev->gflags & (IFF_PROMISC |
3127 IFF_ALLMULTI));
3128
Stefan Rompfb00055a2006-03-20 17:09:11 -08003129 if (netif_running(dev)) {
3130 if (netif_oper_up(dev))
3131 flags |= IFF_RUNNING;
3132 if (netif_carrier_ok(dev))
3133 flags |= IFF_LOWER_UP;
3134 if (netif_dormant(dev))
3135 flags |= IFF_DORMANT;
3136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
3138 return flags;
3139}
3140
3141int dev_change_flags(struct net_device *dev, unsigned flags)
3142{
Thomas Graf7c355f52007-06-05 16:03:03 -07003143 int ret, changes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 int old_flags = dev->flags;
3145
Patrick McHardy24023452007-07-14 18:51:31 -07003146 ASSERT_RTNL();
3147
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 /*
3149 * Set the flags on our device.
3150 */
3151
3152 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
3153 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
3154 IFF_AUTOMEDIA)) |
3155 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
3156 IFF_ALLMULTI));
3157
3158 /*
3159 * Load in the correct multicast list now the flags have changed.
3160 */
3161
David Woodhouse0e917962008-05-20 14:36:14 -07003162 if (dev->change_rx_flags && (old_flags ^ flags) & IFF_MULTICAST)
Patrick McHardy24023452007-07-14 18:51:31 -07003163 dev->change_rx_flags(dev, IFF_MULTICAST);
3164
Patrick McHardy4417da62007-06-27 01:28:10 -07003165 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
3167 /*
3168 * Have we downed the interface. We handle IFF_UP ourselves
3169 * according to user attempts to set it, rather than blindly
3170 * setting it.
3171 */
3172
3173 ret = 0;
3174 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
3175 ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
3176
3177 if (!ret)
Patrick McHardy4417da62007-06-27 01:28:10 -07003178 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 }
3180
3181 if (dev->flags & IFF_UP &&
3182 ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
3183 IFF_VOLATILE)))
Pavel Emelyanov056925a2007-09-16 15:42:43 -07003184 call_netdevice_notifiers(NETDEV_CHANGE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
3186 if ((flags ^ dev->gflags) & IFF_PROMISC) {
3187 int inc = (flags & IFF_PROMISC) ? +1 : -1;
3188 dev->gflags ^= IFF_PROMISC;
3189 dev_set_promiscuity(dev, inc);
3190 }
3191
3192 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
3193 is important. Some (broken) drivers set IFF_PROMISC, when
3194 IFF_ALLMULTI is requested not asking us and not reporting.
3195 */
3196 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
3197 int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
3198 dev->gflags ^= IFF_ALLMULTI;
3199 dev_set_allmulti(dev, inc);
3200 }
3201
Thomas Graf7c355f52007-06-05 16:03:03 -07003202 /* Exclude state transition flags, already notified */
3203 changes = (old_flags ^ dev->flags) & ~(IFF_UP | IFF_RUNNING);
3204 if (changes)
3205 rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
3207 return ret;
3208}
3209
3210int dev_set_mtu(struct net_device *dev, int new_mtu)
3211{
3212 int err;
3213
3214 if (new_mtu == dev->mtu)
3215 return 0;
3216
3217 /* MTU must be positive. */
3218 if (new_mtu < 0)
3219 return -EINVAL;
3220
3221 if (!netif_device_present(dev))
3222 return -ENODEV;
3223
3224 err = 0;
3225 if (dev->change_mtu)
3226 err = dev->change_mtu(dev, new_mtu);
3227 else
3228 dev->mtu = new_mtu;
3229 if (!err && dev->flags & IFF_UP)
Pavel Emelyanov056925a2007-09-16 15:42:43 -07003230 call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 return err;
3232}
3233
3234int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
3235{
3236 int err;
3237
3238 if (!dev->set_mac_address)
3239 return -EOPNOTSUPP;
3240 if (sa->sa_family != dev->type)
3241 return -EINVAL;
3242 if (!netif_device_present(dev))
3243 return -ENODEV;
3244 err = dev->set_mac_address(dev, sa);
3245 if (!err)
Pavel Emelyanov056925a2007-09-16 15:42:43 -07003246 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return err;
3248}
3249
3250/*
Jeff Garzik14e3e072007-10-08 00:06:32 -07003251 * Perform the SIOCxIFxxx calls, inside read_lock(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 */
Jeff Garzik14e3e072007-10-08 00:06:32 -07003253static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254{
3255 int err;
Eric W. Biederman881d9662007-09-17 11:56:21 -07003256 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
3258 if (!dev)
3259 return -ENODEV;
3260
3261 switch (cmd) {
3262 case SIOCGIFFLAGS: /* Get interface flags */
3263 ifr->ifr_flags = dev_get_flags(dev);
3264 return 0;
3265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 case SIOCGIFMETRIC: /* Get the metric on the interface
3267 (currently unused) */
3268 ifr->ifr_metric = 0;
3269 return 0;
3270
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 case SIOCGIFMTU: /* Get the MTU of a device */
3272 ifr->ifr_mtu = dev->mtu;
3273 return 0;
3274
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 case SIOCGIFHWADDR:
3276 if (!dev->addr_len)
3277 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
3278 else
3279 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
3280 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
3281 ifr->ifr_hwaddr.sa_family = dev->type;
3282 return 0;
3283
Jeff Garzik14e3e072007-10-08 00:06:32 -07003284 case SIOCGIFSLAVE:
3285 err = -EINVAL;
3286 break;
3287
3288 case SIOCGIFMAP:
3289 ifr->ifr_map.mem_start = dev->mem_start;
3290 ifr->ifr_map.mem_end = dev->mem_end;
3291 ifr->ifr_map.base_addr = dev->base_addr;
3292 ifr->ifr_map.irq = dev->irq;
3293 ifr->ifr_map.dma = dev->dma;
3294 ifr->ifr_map.port = dev->if_port;
3295 return 0;
3296
3297 case SIOCGIFINDEX:
3298 ifr->ifr_ifindex = dev->ifindex;
3299 return 0;
3300
3301 case SIOCGIFTXQLEN:
3302 ifr->ifr_qlen = dev->tx_queue_len;
3303 return 0;
3304
3305 default:
3306 /* dev_ioctl() should ensure this case
3307 * is never reached
3308 */
3309 WARN_ON(1);
3310 err = -EINVAL;
3311 break;
3312
3313 }
3314 return err;
3315}
3316
3317/*
3318 * Perform the SIOCxIFxxx calls, inside rtnl_lock()
3319 */
3320static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
3321{
3322 int err;
3323 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
3324
3325 if (!dev)
3326 return -ENODEV;
3327
3328 switch (cmd) {
3329 case SIOCSIFFLAGS: /* Set interface flags */
3330 return dev_change_flags(dev, ifr->ifr_flags);
3331
3332 case SIOCSIFMETRIC: /* Set the metric on the interface
3333 (currently unused) */
3334 return -EOPNOTSUPP;
3335
3336 case SIOCSIFMTU: /* Set the MTU of a device */
3337 return dev_set_mtu(dev, ifr->ifr_mtu);
3338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 case SIOCSIFHWADDR:
3340 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
3341
3342 case SIOCSIFHWBROADCAST:
3343 if (ifr->ifr_hwaddr.sa_family != dev->type)
3344 return -EINVAL;
3345 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
3346 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
Pavel Emelyanov056925a2007-09-16 15:42:43 -07003347 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 return 0;
3349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 case SIOCSIFMAP:
3351 if (dev->set_config) {
3352 if (!netif_device_present(dev))
3353 return -ENODEV;
3354 return dev->set_config(dev, &ifr->ifr_map);
3355 }
3356 return -EOPNOTSUPP;
3357
3358 case SIOCADDMULTI:
Patrick McHardy61ee6bd2008-03-26 02:12:11 -07003359 if ((!dev->set_multicast_list && !dev->set_rx_mode) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
3361 return -EINVAL;
3362 if (!netif_device_present(dev))
3363 return -ENODEV;
3364 return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
3365 dev->addr_len, 1);
3366
3367 case SIOCDELMULTI:
Patrick McHardy61ee6bd2008-03-26 02:12:11 -07003368 if ((!dev->set_multicast_list && !dev->set_rx_mode) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
3370 return -EINVAL;
3371 if (!netif_device_present(dev))
3372 return -ENODEV;
3373 return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
3374 dev->addr_len, 1);
3375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 case SIOCSIFTXQLEN:
3377 if (ifr->ifr_qlen < 0)
3378 return -EINVAL;
3379 dev->tx_queue_len = ifr->ifr_qlen;
3380 return 0;
3381
3382 case SIOCSIFNAME:
3383 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
3384 return dev_change_name(dev, ifr->ifr_newname);
3385
3386 /*
3387 * Unknown or private ioctl
3388 */
3389
3390 default:
3391 if ((cmd >= SIOCDEVPRIVATE &&
3392 cmd <= SIOCDEVPRIVATE + 15) ||
3393 cmd == SIOCBONDENSLAVE ||
3394 cmd == SIOCBONDRELEASE ||
3395 cmd == SIOCBONDSETHWADDR ||
3396 cmd == SIOCBONDSLAVEINFOQUERY ||
3397 cmd == SIOCBONDINFOQUERY ||
3398 cmd == SIOCBONDCHANGEACTIVE ||
3399 cmd == SIOCGMIIPHY ||
3400 cmd == SIOCGMIIREG ||
3401 cmd == SIOCSMIIREG ||
3402 cmd == SIOCBRADDIF ||
3403 cmd == SIOCBRDELIF ||
3404 cmd == SIOCWANDEV) {
3405 err = -EOPNOTSUPP;
3406 if (dev->do_ioctl) {
3407 if (netif_device_present(dev))
3408 err = dev->do_ioctl(dev, ifr,
3409 cmd);
3410 else
3411 err = -ENODEV;
3412 }
3413 } else
3414 err = -EINVAL;
3415
3416 }
3417 return err;
3418}
3419
3420/*
3421 * This function handles all "interface"-type I/O control requests. The actual
3422 * 'doing' part of this is dev_ifsioc above.
3423 */
3424
3425/**
3426 * dev_ioctl - network device ioctl
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07003427 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 * @cmd: command to issue
3429 * @arg: pointer to a struct ifreq in user space
3430 *
3431 * Issue ioctl functions to devices. This is normally called by the
3432 * user space syscall interfaces but can sometimes be useful for
3433 * other purposes. The return value is the return from the syscall if
3434 * positive or a negative errno code on error.
3435 */
3436
Eric W. Biederman881d9662007-09-17 11:56:21 -07003437int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438{
3439 struct ifreq ifr;
3440 int ret;
3441 char *colon;
3442
3443 /* One special case: SIOCGIFCONF takes ifconf argument
3444 and requires shared lock, because it sleeps writing
3445 to user space.
3446 */
3447
3448 if (cmd == SIOCGIFCONF) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08003449 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -07003450 ret = dev_ifconf(net, (char __user *) arg);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08003451 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 return ret;
3453 }
3454 if (cmd == SIOCGIFNAME)
Eric W. Biederman881d9662007-09-17 11:56:21 -07003455 return dev_ifname(net, (struct ifreq __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
3457 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
3458 return -EFAULT;
3459
3460 ifr.ifr_name[IFNAMSIZ-1] = 0;
3461
3462 colon = strchr(ifr.ifr_name, ':');
3463 if (colon)
3464 *colon = 0;
3465
3466 /*
3467 * See which interface the caller is talking about.
3468 */
3469
3470 switch (cmd) {
3471 /*
3472 * These ioctl calls:
3473 * - can be done by all.
3474 * - atomic and do not require locking.
3475 * - return a value
3476 */
3477 case SIOCGIFFLAGS:
3478 case SIOCGIFMETRIC:
3479 case SIOCGIFMTU:
3480 case SIOCGIFHWADDR:
3481 case SIOCGIFSLAVE:
3482 case SIOCGIFMAP:
3483 case SIOCGIFINDEX:
3484 case SIOCGIFTXQLEN:
Eric W. Biederman881d9662007-09-17 11:56:21 -07003485 dev_load(net, ifr.ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 read_lock(&dev_base_lock);
Jeff Garzik14e3e072007-10-08 00:06:32 -07003487 ret = dev_ifsioc_locked(net, &ifr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 read_unlock(&dev_base_lock);
3489 if (!ret) {
3490 if (colon)
3491 *colon = ':';
3492 if (copy_to_user(arg, &ifr,
3493 sizeof(struct ifreq)))
3494 ret = -EFAULT;
3495 }
3496 return ret;
3497
3498 case SIOCETHTOOL:
Eric W. Biederman881d9662007-09-17 11:56:21 -07003499 dev_load(net, ifr.ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -07003501 ret = dev_ethtool(net, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 rtnl_unlock();
3503 if (!ret) {
3504 if (colon)
3505 *colon = ':';
3506 if (copy_to_user(arg, &ifr,
3507 sizeof(struct ifreq)))
3508 ret = -EFAULT;
3509 }
3510 return ret;
3511
3512 /*
3513 * These ioctl calls:
3514 * - require superuser power.
3515 * - require strict serialization.
3516 * - return a value
3517 */
3518 case SIOCGMIIPHY:
3519 case SIOCGMIIREG:
3520 case SIOCSIFNAME:
3521 if (!capable(CAP_NET_ADMIN))
3522 return -EPERM;
Eric W. Biederman881d9662007-09-17 11:56:21 -07003523 dev_load(net, ifr.ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -07003525 ret = dev_ifsioc(net, &ifr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 rtnl_unlock();
3527 if (!ret) {
3528 if (colon)
3529 *colon = ':';
3530 if (copy_to_user(arg, &ifr,
3531 sizeof(struct ifreq)))
3532 ret = -EFAULT;
3533 }
3534 return ret;
3535
3536 /*
3537 * These ioctl calls:
3538 * - require superuser power.
3539 * - require strict serialization.
3540 * - do not return a value
3541 */
3542 case SIOCSIFFLAGS:
3543 case SIOCSIFMETRIC:
3544 case SIOCSIFMTU:
3545 case SIOCSIFMAP:
3546 case SIOCSIFHWADDR:
3547 case SIOCSIFSLAVE:
3548 case SIOCADDMULTI:
3549 case SIOCDELMULTI:
3550 case SIOCSIFHWBROADCAST:
3551 case SIOCSIFTXQLEN:
3552 case SIOCSMIIREG:
3553 case SIOCBONDENSLAVE:
3554 case SIOCBONDRELEASE:
3555 case SIOCBONDSETHWADDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 case SIOCBONDCHANGEACTIVE:
3557 case SIOCBRADDIF:
3558 case SIOCBRDELIF:
3559 if (!capable(CAP_NET_ADMIN))
3560 return -EPERM;
Thomas Grafcabcac02006-01-24 12:46:33 -08003561 /* fall through */
3562 case SIOCBONDSLAVEINFOQUERY:
3563 case SIOCBONDINFOQUERY:
Eric W. Biederman881d9662007-09-17 11:56:21 -07003564 dev_load(net, ifr.ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -07003566 ret = dev_ifsioc(net, &ifr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 rtnl_unlock();
3568 return ret;
3569
3570 case SIOCGIFMEM:
3571 /* Get the per device memory space. We can add this but
3572 * currently do not support it */
3573 case SIOCSIFMEM:
3574 /* Set the per device memory buffer space.
3575 * Not applicable in our case */
3576 case SIOCSIFLINK:
3577 return -EINVAL;
3578
3579 /*
3580 * Unknown or private ioctl.
3581 */
3582 default:
3583 if (cmd == SIOCWANDEV ||
3584 (cmd >= SIOCDEVPRIVATE &&
3585 cmd <= SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -07003586 dev_load(net, ifr.ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -07003588 ret = dev_ifsioc(net, &ifr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 rtnl_unlock();
3590 if (!ret && copy_to_user(arg, &ifr,
3591 sizeof(struct ifreq)))
3592 ret = -EFAULT;
3593 return ret;
3594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 /* Take care of Wireless Extensions */
Johannes Berg295f4a12007-04-26 20:43:56 -07003596 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
Eric W. Biederman881d9662007-09-17 11:56:21 -07003597 return wext_handle_ioctl(net, &ifr, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 return -EINVAL;
3599 }
3600}
3601
3602
3603/**
3604 * dev_new_index - allocate an ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07003605 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 *
3607 * Returns a suitable unique value for a new device interface
3608 * number. The caller must hold the rtnl semaphore or the
3609 * dev_base_lock to be sure it remains unique.
3610 */
Eric W. Biederman881d9662007-09-17 11:56:21 -07003611static int dev_new_index(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612{
3613 static int ifindex;
3614 for (;;) {
3615 if (++ifindex <= 0)
3616 ifindex = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07003617 if (!__dev_get_by_index(net, ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 return ifindex;
3619 }
3620}
3621
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622/* Delayed registration/unregisteration */
3623static DEFINE_SPINLOCK(net_todo_list_lock);
Denis Cheng3b5b34f2007-12-07 00:49:17 -08003624static LIST_HEAD(net_todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625
Stephen Hemminger6f05f622007-03-08 20:46:03 -08003626static void net_set_todo(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
3628 spin_lock(&net_todo_list_lock);
3629 list_add_tail(&dev->todo_list, &net_todo_list);
3630 spin_unlock(&net_todo_list_lock);
3631}
3632
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07003633static void rollback_registered(struct net_device *dev)
3634{
3635 BUG_ON(dev_boot_phase);
3636 ASSERT_RTNL();
3637
3638 /* Some devices call without registering for initialization unwind. */
3639 if (dev->reg_state == NETREG_UNINITIALIZED) {
3640 printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
3641 "was registered\n", dev->name, dev);
3642
3643 WARN_ON(1);
3644 return;
3645 }
3646
3647 BUG_ON(dev->reg_state != NETREG_REGISTERED);
3648
3649 /* If device is running, close it first. */
3650 dev_close(dev);
3651
3652 /* And unlink it from device chain. */
3653 unlist_netdevice(dev);
3654
3655 dev->reg_state = NETREG_UNREGISTERING;
3656
3657 synchronize_net();
3658
3659 /* Shutdown queueing discipline. */
3660 dev_shutdown(dev);
3661
3662
3663 /* Notify protocols, that we are about to destroy
3664 this device. They should clean all the things.
3665 */
3666 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
3667
3668 /*
3669 * Flush the unicast and multicast chains
3670 */
3671 dev_addr_discard(dev);
3672
3673 if (dev->uninit)
3674 dev->uninit(dev);
3675
3676 /* Notifier chain MUST detach us from master device. */
3677 BUG_TRAP(!dev->master);
3678
3679 /* Remove entries from kobject tree */
3680 netdev_unregister_kobject(dev);
3681
3682 synchronize_net();
3683
3684 dev_put(dev);
3685}
3686
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687/**
3688 * register_netdevice - register a network device
3689 * @dev: device to register
3690 *
3691 * Take a completed network device structure and add it to the kernel
3692 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3693 * chain. 0 is returned on success. A negative errno code is returned
3694 * on a failure to set up the device, or if the name is a duplicate.
3695 *
3696 * Callers must hold the rtnl semaphore. You may want
3697 * register_netdev() instead of this.
3698 *
3699 * BUGS:
3700 * The locking appears insufficient to guarantee two parallel registers
3701 * will not get the same name.
3702 */
3703
3704int register_netdevice(struct net_device *dev)
3705{
3706 struct hlist_head *head;
3707 struct hlist_node *p;
3708 int ret;
Eric W. Biederman881d9662007-09-17 11:56:21 -07003709 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
3711 BUG_ON(dev_boot_phase);
3712 ASSERT_RTNL();
3713
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003714 might_sleep();
3715
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 /* When net_device's are persistent, this will be fatal. */
3717 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003718 BUG_ON(!dev_net(dev));
3719 net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
3721 spin_lock_init(&dev->queue_lock);
Herbert Xu932ff272006-06-09 12:20:56 -07003722 spin_lock_init(&dev->_xmit_lock);
Jarek Poplawski723e98b2007-05-15 22:46:18 -07003723 netdev_set_lockdep_class(&dev->_xmit_lock, dev->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 dev->xmit_lock_owner = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 spin_lock_init(&dev->ingress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 dev->iflink = -1;
3728
3729 /* Init, if this function is available */
3730 if (dev->init) {
3731 ret = dev->init(dev);
3732 if (ret) {
3733 if (ret > 0)
3734 ret = -EIO;
Adrian Bunk90833aa2006-11-13 16:02:22 -08003735 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 }
3737 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003738
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 if (!dev_valid_name(dev->name)) {
3740 ret = -EINVAL;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07003741 goto err_uninit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 }
3743
Eric W. Biederman881d9662007-09-17 11:56:21 -07003744 dev->ifindex = dev_new_index(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 if (dev->iflink == -1)
3746 dev->iflink = dev->ifindex;
3747
3748 /* Check for existence of name */
Eric W. Biederman881d9662007-09-17 11:56:21 -07003749 head = dev_name_hash(net, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 hlist_for_each(p, head) {
3751 struct net_device *d
3752 = hlist_entry(p, struct net_device, name_hlist);
3753 if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
3754 ret = -EEXIST;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07003755 goto err_uninit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Stephen Hemmingerd212f872007-06-27 00:47:37 -07003759 /* Fix illegal checksum combinations */
3760 if ((dev->features & NETIF_F_HW_CSUM) &&
3761 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
3762 printk(KERN_NOTICE "%s: mixed HW and IP checksum settings.\n",
3763 dev->name);
3764 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
3765 }
3766
3767 if ((dev->features & NETIF_F_NO_CSUM) &&
3768 (dev->features & (NETIF_F_HW_CSUM|NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
3769 printk(KERN_NOTICE "%s: mixed no checksumming and other settings.\n",
3770 dev->name);
3771 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM|NETIF_F_HW_CSUM);
3772 }
3773
3774
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 /* Fix illegal SG+CSUM combinations. */
3776 if ((dev->features & NETIF_F_SG) &&
Herbert Xu8648b302006-06-17 22:06:05 -07003777 !(dev->features & NETIF_F_ALL_CSUM)) {
Stephen Hemminger5a8da022006-07-07 16:54:05 -07003778 printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no checksum feature.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 dev->name);
3780 dev->features &= ~NETIF_F_SG;
3781 }
3782
3783 /* TSO requires that SG is present as well. */
3784 if ((dev->features & NETIF_F_TSO) &&
3785 !(dev->features & NETIF_F_SG)) {
Stephen Hemminger5a8da022006-07-07 16:54:05 -07003786 printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no SG feature.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 dev->name);
3788 dev->features &= ~NETIF_F_TSO;
3789 }
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003790 if (dev->features & NETIF_F_UFO) {
3791 if (!(dev->features & NETIF_F_HW_CSUM)) {
3792 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
3793 "NETIF_F_HW_CSUM feature.\n",
3794 dev->name);
3795 dev->features &= ~NETIF_F_UFO;
3796 }
3797 if (!(dev->features & NETIF_F_SG)) {
3798 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
3799 "NETIF_F_SG feature.\n",
3800 dev->name);
3801 dev->features &= ~NETIF_F_UFO;
3802 }
3803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
Daniel Lezcanoaaf8cdc2008-05-02 17:00:58 -07003805 netdev_initialize_kobject(dev);
Eric W. Biederman8b41d182007-09-26 22:02:53 -07003806 ret = netdev_register_kobject(dev);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003807 if (ret)
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07003808 goto err_uninit;
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003809 dev->reg_state = NETREG_REGISTERED;
3810
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 /*
3812 * Default initial state at registry is that the
3813 * device is present.
3814 */
3815
3816 set_bit(__LINK_STATE_PRESENT, &dev->state);
3817
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 dev_init_scheduler(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 dev_hold(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02003820 list_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
3822 /* Notify protocols, that a new device appeared. */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07003823 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07003824 ret = notifier_to_errno(ret);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07003825 if (ret) {
3826 rollback_registered(dev);
3827 dev->reg_state = NETREG_UNREGISTERED;
3828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
3830out:
3831 return ret;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07003832
3833err_uninit:
3834 if (dev->uninit)
3835 dev->uninit(dev);
3836 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837}
3838
3839/**
3840 * register_netdev - register a network device
3841 * @dev: device to register
3842 *
3843 * Take a completed network device structure and add it to the kernel
3844 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3845 * chain. 0 is returned on success. A negative errno code is returned
3846 * on a failure to set up the device, or if the name is a duplicate.
3847 *
Borislav Petkov38b4da32007-04-20 22:14:10 -07003848 * This is a wrapper around register_netdevice that takes the rtnl semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 * and expands the device name if you passed a format string to
3850 * alloc_netdev.
3851 */
3852int register_netdev(struct net_device *dev)
3853{
3854 int err;
3855
3856 rtnl_lock();
3857
3858 /*
3859 * If the name is a format string the caller wants us to do a
3860 * name allocation.
3861 */
3862 if (strchr(dev->name, '%')) {
3863 err = dev_alloc_name(dev, dev->name);
3864 if (err < 0)
3865 goto out;
3866 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003867
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 err = register_netdevice(dev);
3869out:
3870 rtnl_unlock();
3871 return err;
3872}
3873EXPORT_SYMBOL(register_netdev);
3874
3875/*
3876 * netdev_wait_allrefs - wait until all references are gone.
3877 *
3878 * This is called when unregistering network devices.
3879 *
3880 * Any protocol or device that holds a reference should register
3881 * for netdevice notification, and cleanup and put back the
3882 * reference if they receive an UNREGISTER event.
3883 * We can get stuck here if buggy protocols don't correctly
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003884 * call dev_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 */
3886static void netdev_wait_allrefs(struct net_device *dev)
3887{
3888 unsigned long rebroadcast_time, warning_time;
3889
3890 rebroadcast_time = warning_time = jiffies;
3891 while (atomic_read(&dev->refcnt) != 0) {
3892 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08003893 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894
3895 /* Rebroadcast unregister notification */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07003896 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
3898 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
3899 &dev->state)) {
3900 /* We must not have linkwatch events
3901 * pending on unregister. If this
3902 * happens, we simply run the queue
3903 * unscheduled, resulting in a noop
3904 * for this device.
3905 */
3906 linkwatch_run_queue();
3907 }
3908
Stephen Hemminger6756ae42006-03-20 22:23:58 -08003909 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
3911 rebroadcast_time = jiffies;
3912 }
3913
3914 msleep(250);
3915
3916 if (time_after(jiffies, warning_time + 10 * HZ)) {
3917 printk(KERN_EMERG "unregister_netdevice: "
3918 "waiting for %s to become free. Usage "
3919 "count = %d\n",
3920 dev->name, atomic_read(&dev->refcnt));
3921 warning_time = jiffies;
3922 }
3923 }
3924}
3925
3926/* The sequence is:
3927 *
3928 * rtnl_lock();
3929 * ...
3930 * register_netdevice(x1);
3931 * register_netdevice(x2);
3932 * ...
3933 * unregister_netdevice(y1);
3934 * unregister_netdevice(y2);
3935 * ...
3936 * rtnl_unlock();
3937 * free_netdev(y1);
3938 * free_netdev(y2);
3939 *
3940 * We are invoked by rtnl_unlock() after it drops the semaphore.
3941 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003942 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 * without deadlocking with linkwatch via keventd.
3944 * 2) Since we run with the RTNL semaphore not held, we can sleep
3945 * safely in order to wait for the netdev refcnt to drop to zero.
3946 */
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003947static DEFINE_MUTEX(net_todo_run_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948void netdev_run_todo(void)
3949{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07003950 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
3952 /* Need to guard against multiple cpu's getting out of order. */
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003953 mutex_lock(&net_todo_run_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
3955 /* Not safe to do outside the semaphore. We must not return
3956 * until all unregister events invoked by the local processor
3957 * have been completed (either by this todo run, or one on
3958 * another cpu).
3959 */
3960 if (list_empty(&net_todo_list))
3961 goto out;
3962
3963 /* Snapshot list, allow later requests */
3964 spin_lock(&net_todo_list_lock);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07003965 list_replace_init(&net_todo_list, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 spin_unlock(&net_todo_list_lock);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07003967
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 while (!list_empty(&list)) {
3969 struct net_device *dev
3970 = list_entry(list.next, struct net_device, todo_list);
3971 list_del(&dev->todo_list);
3972
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003973 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 printk(KERN_ERR "network todo '%s' but state %d\n",
3975 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003976 dump_stack();
3977 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003979
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003980 dev->reg_state = NETREG_UNREGISTERED;
3981
3982 netdev_wait_allrefs(dev);
3983
3984 /* paranoia */
3985 BUG_ON(atomic_read(&dev->refcnt));
3986 BUG_TRAP(!dev->ip_ptr);
3987 BUG_TRAP(!dev->ip6_ptr);
3988 BUG_TRAP(!dev->dn_ptr);
3989
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003990 if (dev->destructor)
3991 dev->destructor(dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07003992
3993 /* Free network device */
3994 kobject_put(&dev->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 }
3996
3997out:
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003998 mutex_unlock(&net_todo_run_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999}
4000
Rusty Russell5a1b5892007-04-28 21:04:03 -07004001static struct net_device_stats *internal_stats(struct net_device *dev)
Rusty Russellc45d2862007-03-28 14:29:08 -07004002{
Rusty Russell5a1b5892007-04-28 21:04:03 -07004003 return &dev->stats;
Rusty Russellc45d2862007-03-28 14:29:08 -07004004}
4005
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006/**
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07004007 * alloc_netdev_mq - allocate network device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 * @sizeof_priv: size of private data to allocate space for
4009 * @name: device name format string
4010 * @setup: callback to initialize device
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07004011 * @queue_count: the number of subqueues to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 *
4013 * Allocates a struct net_device with private data area for driver use
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07004014 * and performs basic initialization. Also allocates subquue structs
4015 * for each queue on the device at the end of the netdevice.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 */
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07004017struct net_device *alloc_netdev_mq(int sizeof_priv, const char *name,
4018 void (*setup)(struct net_device *), unsigned int queue_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019{
4020 void *p;
4021 struct net_device *dev;
4022 int alloc_size;
4023
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07004024 BUG_ON(strlen(name) >= sizeof(dev->name));
4025
Alexey Dobriyand1643d22008-04-18 15:43:32 -07004026 alloc_size = sizeof(struct net_device) +
4027 sizeof(struct net_device_subqueue) * (queue_count - 1);
4028 if (sizeof_priv) {
4029 /* ensure 32-byte alignment of private area */
4030 alloc_size = (alloc_size + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST;
4031 alloc_size += sizeof_priv;
4032 }
4033 /* ensure 32-byte alignment of whole construct */
4034 alloc_size += NETDEV_ALIGN_CONST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
Paolo 'Blaisorblade' Giarrusso31380de2006-04-06 22:38:28 -07004036 p = kzalloc(alloc_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 if (!p) {
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07004038 printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 return NULL;
4040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
4042 dev = (struct net_device *)
4043 (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
4044 dev->padded = (char *)dev - (char *)p;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004045 dev_net_set(dev, &init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07004047 if (sizeof_priv) {
4048 dev->priv = ((char *)dev +
4049 ((sizeof(struct net_device) +
4050 (sizeof(struct net_device_subqueue) *
Patrick McHardy31ce72a2007-07-20 19:45:45 -07004051 (queue_count - 1)) + NETDEV_ALIGN_CONST)
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07004052 & ~NETDEV_ALIGN_CONST));
4053 }
4054
4055 dev->egress_subqueue_count = queue_count;
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07004056 dev->gso_max_size = GSO_MAX_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057
Rusty Russell5a1b5892007-04-28 21:04:03 -07004058 dev->get_stats = internal_stats;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004059 netpoll_netdev_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 setup(dev);
4061 strcpy(dev->name, name);
4062 return dev;
4063}
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07004064EXPORT_SYMBOL(alloc_netdev_mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065
4066/**
4067 * free_netdev - free network device
4068 * @dev: device
4069 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004070 * This function does the last stage of destroying an allocated device
4071 * interface. The reference to the device object is released.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 * If this is the last reference then it will be freed.
4073 */
4074void free_netdev(struct net_device *dev)
4075{
Denis V. Lunevf3005d72008-04-16 02:02:18 -07004076 release_net(dev_net(dev));
4077
Stephen Hemminger3041a062006-05-26 13:25:24 -07004078 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 if (dev->reg_state == NETREG_UNINITIALIZED) {
4080 kfree((char *)dev - dev->padded);
4081 return;
4082 }
4083
4084 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
4085 dev->reg_state = NETREG_RELEASED;
4086
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07004087 /* will free via device release */
4088 put_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089}
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004090
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091/* Synchronize with packet receive processing. */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004092void synchronize_net(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093{
4094 might_sleep();
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07004095 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096}
4097
4098/**
4099 * unregister_netdevice - remove device from the kernel
4100 * @dev: device
4101 *
4102 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08004103 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 *
4105 * Callers must hold the rtnl semaphore. You may want
4106 * unregister_netdev() instead of this.
4107 */
4108
Stephen Hemminger22f8cde2007-02-07 00:09:58 -08004109void unregister_netdevice(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110{
Herbert Xua6620712007-12-12 19:21:56 -08004111 ASSERT_RTNL();
4112
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07004113 rollback_registered(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 /* Finish processing unregister after unlock */
4115 net_set_todo(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116}
4117
4118/**
4119 * unregister_netdev - remove device from the kernel
4120 * @dev: device
4121 *
4122 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08004123 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 *
4125 * This is just a wrapper for unregister_netdevice that takes
4126 * the rtnl semaphore. In general you want to use this and not
4127 * unregister_netdevice.
4128 */
4129void unregister_netdev(struct net_device *dev)
4130{
4131 rtnl_lock();
4132 unregister_netdevice(dev);
4133 rtnl_unlock();
4134}
4135
4136EXPORT_SYMBOL(unregister_netdev);
4137
Eric W. Biedermance286d32007-09-12 13:53:49 +02004138/**
4139 * dev_change_net_namespace - move device to different nethost namespace
4140 * @dev: device
4141 * @net: network namespace
4142 * @pat: If not NULL name pattern to try if the current device name
4143 * is already taken in the destination network namespace.
4144 *
4145 * This function shuts down a device interface and moves it
4146 * to a new network namespace. On success 0 is returned, on
4147 * a failure a netagive errno code is returned.
4148 *
4149 * Callers must hold the rtnl semaphore.
4150 */
4151
4152int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
4153{
4154 char buf[IFNAMSIZ];
4155 const char *destname;
4156 int err;
4157
4158 ASSERT_RTNL();
4159
4160 /* Don't allow namespace local devices to be moved. */
4161 err = -EINVAL;
4162 if (dev->features & NETIF_F_NETNS_LOCAL)
4163 goto out;
4164
4165 /* Ensure the device has been registrered */
4166 err = -EINVAL;
4167 if (dev->reg_state != NETREG_REGISTERED)
4168 goto out;
4169
4170 /* Get out if there is nothing todo */
4171 err = 0;
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09004172 if (net_eq(dev_net(dev), net))
Eric W. Biedermance286d32007-09-12 13:53:49 +02004173 goto out;
4174
4175 /* Pick the destination device name, and ensure
4176 * we can use it in the destination network namespace.
4177 */
4178 err = -EEXIST;
4179 destname = dev->name;
4180 if (__dev_get_by_name(net, destname)) {
4181 /* We get here if we can't use the current device name */
4182 if (!pat)
4183 goto out;
4184 if (!dev_valid_name(pat))
4185 goto out;
4186 if (strchr(pat, '%')) {
4187 if (__dev_alloc_name(net, pat, buf) < 0)
4188 goto out;
4189 destname = buf;
4190 } else
4191 destname = pat;
4192 if (__dev_get_by_name(net, destname))
4193 goto out;
4194 }
4195
4196 /*
4197 * And now a mini version of register_netdevice unregister_netdevice.
4198 */
4199
4200 /* If device is running close it first. */
Pavel Emelyanov9b772652007-10-10 02:49:09 -07004201 dev_close(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02004202
4203 /* And unlink it from device chain */
4204 err = -ENODEV;
4205 unlist_netdevice(dev);
4206
4207 synchronize_net();
4208
4209 /* Shutdown queueing discipline. */
4210 dev_shutdown(dev);
4211
4212 /* Notify protocols, that we are about to destroy
4213 this device. They should clean all the things.
4214 */
4215 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
4216
4217 /*
4218 * Flush the unicast and multicast chains
4219 */
4220 dev_addr_discard(dev);
4221
4222 /* Actually switch the network namespace */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004223 dev_net_set(dev, net);
Eric W. Biedermance286d32007-09-12 13:53:49 +02004224
4225 /* Assign the new device name */
4226 if (destname != dev->name)
4227 strcpy(dev->name, destname);
4228
4229 /* If there is an ifindex conflict assign a new one */
4230 if (__dev_get_by_index(net, dev->ifindex)) {
4231 int iflink = (dev->iflink == dev->ifindex);
4232 dev->ifindex = dev_new_index(net);
4233 if (iflink)
4234 dev->iflink = dev->ifindex;
4235 }
4236
Eric W. Biederman8b41d182007-09-26 22:02:53 -07004237 /* Fixup kobjects */
Daniel Lezcanoaaf8cdc2008-05-02 17:00:58 -07004238 netdev_unregister_kobject(dev);
4239 err = netdev_register_kobject(dev);
Eric W. Biederman8b41d182007-09-26 22:02:53 -07004240 WARN_ON(err);
Eric W. Biedermance286d32007-09-12 13:53:49 +02004241
4242 /* Add the device back in the hashes */
4243 list_netdevice(dev);
4244
4245 /* Notify protocols, that a new device appeared. */
4246 call_netdevice_notifiers(NETDEV_REGISTER, dev);
4247
4248 synchronize_net();
4249 err = 0;
4250out:
4251 return err;
4252}
4253
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254static int dev_cpu_callback(struct notifier_block *nfb,
4255 unsigned long action,
4256 void *ocpu)
4257{
4258 struct sk_buff **list_skb;
4259 struct net_device **list_net;
4260 struct sk_buff *skb;
4261 unsigned int cpu, oldcpu = (unsigned long)ocpu;
4262 struct softnet_data *sd, *oldsd;
4263
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07004264 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 return NOTIFY_OK;
4266
4267 local_irq_disable();
4268 cpu = smp_processor_id();
4269 sd = &per_cpu(softnet_data, cpu);
4270 oldsd = &per_cpu(softnet_data, oldcpu);
4271
4272 /* Find end of our completion_queue. */
4273 list_skb = &sd->completion_queue;
4274 while (*list_skb)
4275 list_skb = &(*list_skb)->next;
4276 /* Append completion queue from offline CPU. */
4277 *list_skb = oldsd->completion_queue;
4278 oldsd->completion_queue = NULL;
4279
4280 /* Find end of our output_queue. */
4281 list_net = &sd->output_queue;
4282 while (*list_net)
4283 list_net = &(*list_net)->next_sched;
4284 /* Append output queue from offline CPU. */
4285 *list_net = oldsd->output_queue;
4286 oldsd->output_queue = NULL;
4287
4288 raise_softirq_irqoff(NET_TX_SOFTIRQ);
4289 local_irq_enable();
4290
4291 /* Process offline CPU's input_pkt_queue */
4292 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
4293 netif_rx(skb);
4294
4295 return NOTIFY_OK;
4296}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
Chris Leechdb217332006-06-17 21:24:58 -07004298#ifdef CONFIG_NET_DMA
4299/**
Randy Dunlap0ed72ec2007-07-26 00:03:29 -07004300 * net_dma_rebalance - try to maintain one DMA channel per CPU
4301 * @net_dma: DMA client and associated data (lock, channels, channel_mask)
4302 *
4303 * This is called when the number of channels allocated to the net_dma client
4304 * changes. The net_dma client tries to have one DMA channel per CPU.
Chris Leechdb217332006-06-17 21:24:58 -07004305 */
Dan Williamsd379b012007-07-09 11:56:42 -07004306
4307static void net_dma_rebalance(struct net_dma *net_dma)
Chris Leechdb217332006-06-17 21:24:58 -07004308{
Dan Williamsd379b012007-07-09 11:56:42 -07004309 unsigned int cpu, i, n, chan_idx;
Chris Leechdb217332006-06-17 21:24:58 -07004310 struct dma_chan *chan;
4311
Dan Williamsd379b012007-07-09 11:56:42 -07004312 if (cpus_empty(net_dma->channel_mask)) {
Chris Leechdb217332006-06-17 21:24:58 -07004313 for_each_online_cpu(cpu)
Alexey Dobriyan29bbd722006-08-02 15:02:31 -07004314 rcu_assign_pointer(per_cpu(softnet_data, cpu).net_dma, NULL);
Chris Leechdb217332006-06-17 21:24:58 -07004315 return;
4316 }
4317
4318 i = 0;
4319 cpu = first_cpu(cpu_online_map);
4320
Dan Williamsd379b012007-07-09 11:56:42 -07004321 for_each_cpu_mask(chan_idx, net_dma->channel_mask) {
4322 chan = net_dma->channels[chan_idx];
4323
4324 n = ((num_online_cpus() / cpus_weight(net_dma->channel_mask))
4325 + (i < (num_online_cpus() %
4326 cpus_weight(net_dma->channel_mask)) ? 1 : 0));
Chris Leechdb217332006-06-17 21:24:58 -07004327
4328 while(n) {
Alexey Dobriyan29bbd722006-08-02 15:02:31 -07004329 per_cpu(softnet_data, cpu).net_dma = chan;
Chris Leechdb217332006-06-17 21:24:58 -07004330 cpu = next_cpu(cpu, cpu_online_map);
4331 n--;
4332 }
4333 i++;
4334 }
Chris Leechdb217332006-06-17 21:24:58 -07004335}
4336
4337/**
4338 * netdev_dma_event - event callback for the net_dma_client
4339 * @client: should always be net_dma_client
Randy Dunlapf4b8ea72006-06-22 16:00:11 -07004340 * @chan: DMA channel for the event
Randy Dunlap0ed72ec2007-07-26 00:03:29 -07004341 * @state: DMA state to be handled
Chris Leechdb217332006-06-17 21:24:58 -07004342 */
Dan Williamsd379b012007-07-09 11:56:42 -07004343static enum dma_state_client
4344netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
4345 enum dma_state state)
Chris Leechdb217332006-06-17 21:24:58 -07004346{
Dan Williamsd379b012007-07-09 11:56:42 -07004347 int i, found = 0, pos = -1;
4348 struct net_dma *net_dma =
4349 container_of(client, struct net_dma, client);
4350 enum dma_state_client ack = DMA_DUP; /* default: take no action */
4351
4352 spin_lock(&net_dma->lock);
4353 switch (state) {
4354 case DMA_RESOURCE_AVAILABLE:
Mike Travis0c0b0ac2008-05-02 16:43:08 -07004355 for (i = 0; i < nr_cpu_ids; i++)
Dan Williamsd379b012007-07-09 11:56:42 -07004356 if (net_dma->channels[i] == chan) {
4357 found = 1;
4358 break;
4359 } else if (net_dma->channels[i] == NULL && pos < 0)
4360 pos = i;
4361
4362 if (!found && pos >= 0) {
4363 ack = DMA_ACK;
4364 net_dma->channels[pos] = chan;
4365 cpu_set(pos, net_dma->channel_mask);
4366 net_dma_rebalance(net_dma);
4367 }
Chris Leechdb217332006-06-17 21:24:58 -07004368 break;
4369 case DMA_RESOURCE_REMOVED:
Mike Travis0c0b0ac2008-05-02 16:43:08 -07004370 for (i = 0; i < nr_cpu_ids; i++)
Dan Williamsd379b012007-07-09 11:56:42 -07004371 if (net_dma->channels[i] == chan) {
4372 found = 1;
4373 pos = i;
4374 break;
4375 }
4376
4377 if (found) {
4378 ack = DMA_ACK;
4379 cpu_clear(pos, net_dma->channel_mask);
4380 net_dma->channels[i] = NULL;
4381 net_dma_rebalance(net_dma);
4382 }
Chris Leechdb217332006-06-17 21:24:58 -07004383 break;
4384 default:
4385 break;
4386 }
Dan Williamsd379b012007-07-09 11:56:42 -07004387 spin_unlock(&net_dma->lock);
4388
4389 return ack;
Chris Leechdb217332006-06-17 21:24:58 -07004390}
4391
4392/**
4393 * netdev_dma_regiser - register the networking subsystem as a DMA client
4394 */
4395static int __init netdev_dma_register(void)
4396{
Mike Travis0c0b0ac2008-05-02 16:43:08 -07004397 net_dma.channels = kzalloc(nr_cpu_ids * sizeof(struct net_dma),
4398 GFP_KERNEL);
4399 if (unlikely(!net_dma.channels)) {
4400 printk(KERN_NOTICE
4401 "netdev_dma: no memory for net_dma.channels\n");
4402 return -ENOMEM;
4403 }
Dan Williamsd379b012007-07-09 11:56:42 -07004404 spin_lock_init(&net_dma.lock);
4405 dma_cap_set(DMA_MEMCPY, net_dma.client.cap_mask);
4406 dma_async_client_register(&net_dma.client);
4407 dma_async_client_chan_request(&net_dma.client);
Chris Leechdb217332006-06-17 21:24:58 -07004408 return 0;
4409}
4410
4411#else
4412static int __init netdev_dma_register(void) { return -ENODEV; }
4413#endif /* CONFIG_NET_DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
Herbert Xu7f353bf2007-08-10 15:47:58 -07004415/**
4416 * netdev_compute_feature - compute conjunction of two feature sets
4417 * @all: first feature set
4418 * @one: second feature set
4419 *
4420 * Computes a new feature set after adding a device with feature set
4421 * @one to the master device with current feature set @all. Returns
4422 * the new feature set.
4423 */
4424int netdev_compute_features(unsigned long all, unsigned long one)
4425{
4426 /* if device needs checksumming, downgrade to hw checksumming */
4427 if (all & NETIF_F_NO_CSUM && !(one & NETIF_F_NO_CSUM))
4428 all ^= NETIF_F_NO_CSUM | NETIF_F_HW_CSUM;
4429
4430 /* if device can't do all checksum, downgrade to ipv4/ipv6 */
4431 if (all & NETIF_F_HW_CSUM && !(one & NETIF_F_HW_CSUM))
4432 all ^= NETIF_F_HW_CSUM
4433 | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
4434
4435 if (one & NETIF_F_GSO)
4436 one |= NETIF_F_GSO_SOFTWARE;
4437 one |= NETIF_F_GSO;
4438
4439 /* If even one device supports robust GSO, enable it for all. */
4440 if (one & NETIF_F_GSO_ROBUST)
4441 all |= NETIF_F_GSO_ROBUST;
4442
4443 all &= one | NETIF_F_LLTX;
4444
4445 if (!(all & NETIF_F_ALL_CSUM))
4446 all &= ~NETIF_F_SG;
4447 if (!(all & NETIF_F_SG))
4448 all &= ~NETIF_F_GSO_MASK;
4449
4450 return all;
4451}
4452EXPORT_SYMBOL(netdev_compute_features);
4453
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07004454static struct hlist_head *netdev_create_hash(void)
4455{
4456 int i;
4457 struct hlist_head *hash;
4458
4459 hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
4460 if (hash != NULL)
4461 for (i = 0; i < NETDEV_HASHENTRIES; i++)
4462 INIT_HLIST_HEAD(&hash[i]);
4463
4464 return hash;
4465}
4466
Eric W. Biederman881d9662007-09-17 11:56:21 -07004467/* Initialize per network namespace state */
Pavel Emelyanov46650792007-10-08 20:38:39 -07004468static int __net_init netdev_init(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07004469{
Eric W. Biederman881d9662007-09-17 11:56:21 -07004470 INIT_LIST_HEAD(&net->dev_base_head);
Eric W. Biederman881d9662007-09-17 11:56:21 -07004471
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07004472 net->dev_name_head = netdev_create_hash();
4473 if (net->dev_name_head == NULL)
4474 goto err_name;
Eric W. Biederman881d9662007-09-17 11:56:21 -07004475
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07004476 net->dev_index_head = netdev_create_hash();
4477 if (net->dev_index_head == NULL)
4478 goto err_idx;
Eric W. Biederman881d9662007-09-17 11:56:21 -07004479
4480 return 0;
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07004481
4482err_idx:
4483 kfree(net->dev_name_head);
4484err_name:
4485 return -ENOMEM;
Eric W. Biederman881d9662007-09-17 11:56:21 -07004486}
4487
Pavel Emelyanov46650792007-10-08 20:38:39 -07004488static void __net_exit netdev_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07004489{
4490 kfree(net->dev_name_head);
4491 kfree(net->dev_index_head);
4492}
4493
Denis V. Lunev022cbae2007-11-13 03:23:50 -08004494static struct pernet_operations __net_initdata netdev_net_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -07004495 .init = netdev_init,
4496 .exit = netdev_exit,
4497};
4498
Pavel Emelyanov46650792007-10-08 20:38:39 -07004499static void __net_exit default_device_exit(struct net *net)
Eric W. Biedermance286d32007-09-12 13:53:49 +02004500{
4501 struct net_device *dev, *next;
4502 /*
4503 * Push all migratable of the network devices back to the
4504 * initial network namespace
4505 */
4506 rtnl_lock();
4507 for_each_netdev_safe(net, dev, next) {
4508 int err;
Pavel Emelyanovaca51392008-05-08 01:24:25 -07004509 char fb_name[IFNAMSIZ];
Eric W. Biedermance286d32007-09-12 13:53:49 +02004510
4511 /* Ignore unmoveable devices (i.e. loopback) */
4512 if (dev->features & NETIF_F_NETNS_LOCAL)
4513 continue;
4514
4515 /* Push remaing network devices to init_net */
Pavel Emelyanovaca51392008-05-08 01:24:25 -07004516 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
4517 err = dev_change_net_namespace(dev, &init_net, fb_name);
Eric W. Biedermance286d32007-09-12 13:53:49 +02004518 if (err) {
Pavel Emelyanovaca51392008-05-08 01:24:25 -07004519 printk(KERN_EMERG "%s: failed to move %s to init_net: %d\n",
Eric W. Biedermance286d32007-09-12 13:53:49 +02004520 __func__, dev->name, err);
Pavel Emelyanovaca51392008-05-08 01:24:25 -07004521 BUG();
Eric W. Biedermance286d32007-09-12 13:53:49 +02004522 }
4523 }
4524 rtnl_unlock();
4525}
4526
Denis V. Lunev022cbae2007-11-13 03:23:50 -08004527static struct pernet_operations __net_initdata default_device_ops = {
Eric W. Biedermance286d32007-09-12 13:53:49 +02004528 .exit = default_device_exit,
4529};
4530
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531/*
4532 * Initialize the DEV module. At boot time this walks the device list and
4533 * unhooks any devices that fail to initialise (normally hardware not
4534 * present) and leaves us with a valid list of present and active devices.
4535 *
4536 */
4537
4538/*
4539 * This is called single threaded during boot, so no need
4540 * to take the rtnl semaphore.
4541 */
4542static int __init net_dev_init(void)
4543{
4544 int i, rc = -ENOMEM;
4545
4546 BUG_ON(!dev_boot_phase);
4547
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 if (dev_proc_init())
4549 goto out;
4550
Eric W. Biederman8b41d182007-09-26 22:02:53 -07004551 if (netdev_kobject_init())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 goto out;
4553
4554 INIT_LIST_HEAD(&ptype_all);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08004555 for (i = 0; i < PTYPE_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 INIT_LIST_HEAD(&ptype_base[i]);
4557
Eric W. Biederman881d9662007-09-17 11:56:21 -07004558 if (register_pernet_subsys(&netdev_net_ops))
4559 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560
Eric W. Biedermance286d32007-09-12 13:53:49 +02004561 if (register_pernet_device(&default_device_ops))
4562 goto out;
4563
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 /*
4565 * Initialise the packet receive queues.
4566 */
4567
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07004568 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 struct softnet_data *queue;
4570
4571 queue = &per_cpu(softnet_data, i);
4572 skb_queue_head_init(&queue->input_pkt_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 queue->completion_queue = NULL;
4574 INIT_LIST_HEAD(&queue->poll_list);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004575
4576 queue->backlog.poll = process_backlog;
4577 queue->backlog.weight = weight_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 }
4579
Chris Leechdb217332006-06-17 21:24:58 -07004580 netdev_dma_register();
4581
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 dev_boot_phase = 0;
4583
4584 open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
4585 open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
4586
4587 hotcpu_notifier(dev_cpu_callback, 0);
4588 dst_init();
4589 dev_mcast_init();
4590 rc = 0;
4591out:
4592 return rc;
4593}
4594
4595subsys_initcall(net_dev_init);
4596
4597EXPORT_SYMBOL(__dev_get_by_index);
4598EXPORT_SYMBOL(__dev_get_by_name);
4599EXPORT_SYMBOL(__dev_remove_pack);
Mitch Williamsc2373ee2005-11-09 10:34:45 -08004600EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601EXPORT_SYMBOL(dev_add_pack);
4602EXPORT_SYMBOL(dev_alloc_name);
4603EXPORT_SYMBOL(dev_close);
4604EXPORT_SYMBOL(dev_get_by_flags);
4605EXPORT_SYMBOL(dev_get_by_index);
4606EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607EXPORT_SYMBOL(dev_open);
4608EXPORT_SYMBOL(dev_queue_xmit);
4609EXPORT_SYMBOL(dev_remove_pack);
4610EXPORT_SYMBOL(dev_set_allmulti);
4611EXPORT_SYMBOL(dev_set_promiscuity);
4612EXPORT_SYMBOL(dev_change_flags);
4613EXPORT_SYMBOL(dev_set_mtu);
4614EXPORT_SYMBOL(dev_set_mac_address);
4615EXPORT_SYMBOL(free_netdev);
4616EXPORT_SYMBOL(netdev_boot_setup_check);
4617EXPORT_SYMBOL(netdev_set_master);
4618EXPORT_SYMBOL(netdev_state_change);
4619EXPORT_SYMBOL(netif_receive_skb);
4620EXPORT_SYMBOL(netif_rx);
4621EXPORT_SYMBOL(register_gifconf);
4622EXPORT_SYMBOL(register_netdevice);
4623EXPORT_SYMBOL(register_netdevice_notifier);
4624EXPORT_SYMBOL(skb_checksum_help);
4625EXPORT_SYMBOL(synchronize_net);
4626EXPORT_SYMBOL(unregister_netdevice);
4627EXPORT_SYMBOL(unregister_netdevice_notifier);
4628EXPORT_SYMBOL(net_enable_timestamp);
4629EXPORT_SYMBOL(net_disable_timestamp);
4630EXPORT_SYMBOL(dev_get_flags);
4631
4632#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4633EXPORT_SYMBOL(br_handle_frame_hook);
4634EXPORT_SYMBOL(br_fdb_get_hook);
4635EXPORT_SYMBOL(br_fdb_put_hook);
4636#endif
4637
4638#ifdef CONFIG_KMOD
4639EXPORT_SYMBOL(dev_load);
4640#endif
4641
4642EXPORT_PER_CPU_SYMBOL(softnet_data);