blob: 7e2d5274333fb7302a2354371ad93eb5556d3080 [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>
Ben Hutchings0187bdf2008-06-19 16:15:47 -070093#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include <linux/notifier.h>
95#include <linux/skbuff.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020096#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#include <net/sock.h>
98#include <linux/rtnetlink.h>
99#include <linux/proc_fs.h>
100#include <linux/seq_file.h>
101#include <linux/stat.h>
102#include <linux/if_bridge.h>
Patrick McHardyb863ceb2007-07-14 18:55:06 -0700103#include <linux/if_macvlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#include <net/dst.h>
105#include <net/pkt_sched.h>
106#include <net/checksum.h>
107#include <linux/highmem.h>
108#include <linux/init.h>
109#include <linux/kmod.h>
110#include <linux/module.h>
111#include <linux/kallsyms.h>
112#include <linux/netpoll.h>
113#include <linux/rcupdate.h>
114#include <linux/delay.h>
Johannes Berg295f4a12007-04-26 20:43:56 -0700115#include <net/wext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#include <net/iw_handler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#include <asm/current.h>
Steve Grubb5bdb9882005-12-03 08:39:35 -0500118#include <linux/audit.h>
Chris Leechdb217332006-06-17 21:24:58 -0700119#include <linux/dmaengine.h>
Herbert Xuf6a78bf2006-06-22 02:57:17 -0700120#include <linux/err.h>
David S. Millerc7fa9d12006-08-15 16:34:13 -0700121#include <linux/ctype.h>
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700122#include <linux/if_arp.h>
Ben Hutchings6de329e2008-06-16 17:02:28 -0700123#include <linux/if_vlan.h>
David S. Miller8f0f2222008-07-15 03:47:03 -0700124#include <linux/ip.h>
125#include <linux/ipv6.h>
126#include <linux/in.h>
David S. Millerb6b2fed2008-07-21 09:48:06 -0700127#include <linux/jhash.h>
128#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Pavel Emelyanov342709e2007-10-23 21:14:45 -0700130#include "net-sysfs.h"
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*
133 * The list of packet types we will receive (as opposed to discard)
134 * and the routines to invoke.
135 *
136 * Why 16. Because with 16 the only overlap we get on a hash of the
137 * low nibble of the protocol value is RARP/SNAP/X.25.
138 *
139 * NOTE: That is no longer true with the addition of VLAN tags. Not
140 * sure which should go first, but I bet it won't make much
141 * difference if we are running VLANs. The good news is that
142 * this protocol won't be in the list unless compiled in, so
Stephen Hemminger3041a062006-05-26 13:25:24 -0700143 * the average user (w/out VLANs) will not be adversely affected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 * --BLG
145 *
146 * 0800 IP
147 * 8100 802.1Q VLAN
148 * 0001 802.3
149 * 0002 AX.25
150 * 0004 802.2
151 * 8035 RARP
152 * 0005 SNAP
153 * 0805 X.25
154 * 0806 ARP
155 * 8137 IPX
156 * 0009 Localtalk
157 * 86DD IPv6
158 */
159
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +0800160#define PTYPE_HASH_SIZE (16)
161#define PTYPE_HASH_MASK (PTYPE_HASH_SIZE - 1)
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static DEFINE_SPINLOCK(ptype_lock);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +0800164static struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -0700165static struct list_head ptype_all __read_mostly; /* Taps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Chris Leechdb217332006-06-17 21:24:58 -0700167#ifdef CONFIG_NET_DMA
Dan Williamsd379b012007-07-09 11:56:42 -0700168struct net_dma {
169 struct dma_client client;
170 spinlock_t lock;
171 cpumask_t channel_mask;
Mike Travis0c0b0ac2008-05-02 16:43:08 -0700172 struct dma_chan **channels;
Dan Williamsd379b012007-07-09 11:56:42 -0700173};
174
175static enum dma_state_client
176netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
177 enum dma_state state);
178
179static struct net_dma net_dma = {
180 .client = {
181 .event_callback = netdev_dma_event,
182 },
183};
Chris Leechdb217332006-06-17 21:24:58 -0700184#endif
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
Pavel Emelianov7562f872007-05-03 15:13:45 -0700187 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * semaphore.
189 *
190 * Pure readers hold dev_base_lock for reading.
191 *
192 * Writers must hold the rtnl semaphore while they loop through the
Pavel Emelianov7562f872007-05-03 15:13:45 -0700193 * dev_base_head list, and hold dev_base_lock for writing when they do the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * actual updates. This allows pure readers to access the list even
195 * while a writer is preparing to update it.
196 *
197 * To put it another way, dev_base_lock is held for writing only to
198 * protect against pure readers; the rtnl semaphore provides the
199 * protection against other writers.
200 *
201 * See, for example usages, register_netdevice() and
202 * unregister_netdevice(), which must be called with the rtnl
203 * semaphore held.
204 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205DEFINE_RWLOCK(dev_base_lock);
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207EXPORT_SYMBOL(dev_base_lock);
208
209#define NETDEV_HASHBITS 8
Eric W. Biederman881d9662007-09-17 11:56:21 -0700210#define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Eric W. Biederman881d9662007-09-17 11:56:21 -0700212static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
Eric W. Biederman881d9662007-09-17 11:56:21 -0700215 return &net->dev_name_head[hash & ((1 << NETDEV_HASHBITS) - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Eric W. Biederman881d9662007-09-17 11:56:21 -0700218static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700220 return &net->dev_index_head[ifindex & ((1 << NETDEV_HASHBITS) - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Eric W. Biedermance286d32007-09-12 13:53:49 +0200223/* Device list insertion */
224static int list_netdevice(struct net_device *dev)
225{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900226 struct net *net = dev_net(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +0200227
228 ASSERT_RTNL();
229
230 write_lock_bh(&dev_base_lock);
231 list_add_tail(&dev->dev_list, &net->dev_base_head);
232 hlist_add_head(&dev->name_hlist, dev_name_hash(net, dev->name));
233 hlist_add_head(&dev->index_hlist, dev_index_hash(net, dev->ifindex));
234 write_unlock_bh(&dev_base_lock);
235 return 0;
236}
237
238/* Device list removal */
239static void unlist_netdevice(struct net_device *dev)
240{
241 ASSERT_RTNL();
242
243 /* Unlink dev from the device chain */
244 write_lock_bh(&dev_base_lock);
245 list_del(&dev->dev_list);
246 hlist_del(&dev->name_hlist);
247 hlist_del(&dev->index_hlist);
248 write_unlock_bh(&dev_base_lock);
249}
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*
252 * Our notifier list
253 */
254
Alan Sternf07d5b92006-05-09 15:23:03 -0700255static RAW_NOTIFIER_HEAD(netdev_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257/*
258 * Device drivers call our routines to queue packets here. We empty the
259 * queue in the local softnet handler.
260 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700261
262DEFINE_PER_CPU(struct softnet_data, softnet_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700264#ifdef CONFIG_DEBUG_LOCK_ALLOC
265/*
David S. Millerc773e842008-07-08 23:13:53 -0700266 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
Jarek Poplawski723e98b2007-05-15 22:46:18 -0700267 * according to dev->type
268 */
269static const unsigned short netdev_lock_type[] =
270 {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
271 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
272 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
273 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
274 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
275 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
276 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
277 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
278 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
279 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
280 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
281 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
282 ARPHRD_FCFABRIC, ARPHRD_IEEE802_TR, ARPHRD_IEEE80211,
283 ARPHRD_IEEE80211_PRISM, ARPHRD_IEEE80211_RADIOTAP, ARPHRD_VOID,
284 ARPHRD_NONE};
285
286static const char *netdev_lock_name[] =
287 {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
288 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
289 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
290 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
291 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
292 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
293 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
294 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
295 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
296 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
297 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
298 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
299 "_xmit_FCFABRIC", "_xmit_IEEE802_TR", "_xmit_IEEE80211",
300 "_xmit_IEEE80211_PRISM", "_xmit_IEEE80211_RADIOTAP", "_xmit_VOID",
301 "_xmit_NONE"};
302
303static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
304
305static inline unsigned short netdev_lock_pos(unsigned short dev_type)
306{
307 int i;
308
309 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
310 if (netdev_lock_type[i] == dev_type)
311 return i;
312 /* the last key is used by default */
313 return ARRAY_SIZE(netdev_lock_type) - 1;
314}
315
316static inline void netdev_set_lockdep_class(spinlock_t *lock,
317 unsigned short dev_type)
318{
319 int i;
320
321 i = netdev_lock_pos(dev_type);
322 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
323 netdev_lock_name[i]);
324}
325#else
326static inline void netdev_set_lockdep_class(spinlock_t *lock,
327 unsigned short dev_type)
328{
329}
330#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332/*******************************************************************************
333
334 Protocol management and registration routines
335
336*******************************************************************************/
337
338/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * Add a protocol ID to the list. Now that the input handler is
340 * smarter we can dispense with all the messy stuff that used to be
341 * here.
342 *
343 * BEWARE!!! Protocol handlers, mangling input packets,
344 * MUST BE last in hash buckets and checking protocol handlers
345 * MUST start from promiscuous ptype_all chain in net_bh.
346 * It is true now, do not change it.
347 * Explanation follows: if protocol handler, mangling packet, will
348 * be the first on list, it is not able to sense, that packet
349 * is cloned and should be copied-on-write, so that it will
350 * change it and subsequent readers will get broken packet.
351 * --ANK (980803)
352 */
353
354/**
355 * dev_add_pack - add packet handler
356 * @pt: packet type declaration
357 *
358 * Add a protocol handler to the networking stack. The passed &packet_type
359 * is linked into kernel lists and may not be freed until it has been
360 * removed from the kernel lists.
361 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900362 * This call does not sleep therefore it can not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 * guarantee all CPU's that are in middle of receiving packets
364 * will see the new packet type (until the next received packet).
365 */
366
367void dev_add_pack(struct packet_type *pt)
368{
369 int hash;
370
371 spin_lock_bh(&ptype_lock);
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -0700372 if (pt->type == htons(ETH_P_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 list_add_rcu(&pt->list, &ptype_all);
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -0700374 else {
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +0800375 hash = ntohs(pt->type) & PTYPE_HASH_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 list_add_rcu(&pt->list, &ptype_base[hash]);
377 }
378 spin_unlock_bh(&ptype_lock);
379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/**
382 * __dev_remove_pack - remove packet handler
383 * @pt: packet type declaration
384 *
385 * Remove a protocol handler that was previously added to the kernel
386 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
387 * from the kernel lists and can be freed or reused once this function
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900388 * returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 *
390 * The packet type might still be in use by receivers
391 * and must not be freed until after all the CPU's have gone
392 * through a quiescent state.
393 */
394void __dev_remove_pack(struct packet_type *pt)
395{
396 struct list_head *head;
397 struct packet_type *pt1;
398
399 spin_lock_bh(&ptype_lock);
400
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -0700401 if (pt->type == htons(ETH_P_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 head = &ptype_all;
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -0700403 else
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +0800404 head = &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 list_for_each_entry(pt1, head, list) {
407 if (pt == pt1) {
408 list_del_rcu(&pt->list);
409 goto out;
410 }
411 }
412
413 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
414out:
415 spin_unlock_bh(&ptype_lock);
416}
417/**
418 * dev_remove_pack - remove packet handler
419 * @pt: packet type declaration
420 *
421 * Remove a protocol handler that was previously added to the kernel
422 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
423 * from the kernel lists and can be freed or reused once this function
424 * returns.
425 *
426 * This call sleeps to guarantee that no CPU is looking at the packet
427 * type after return.
428 */
429void dev_remove_pack(struct packet_type *pt)
430{
431 __dev_remove_pack(pt);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 synchronize_net();
434}
435
436/******************************************************************************
437
438 Device Boot-time Settings Routines
439
440*******************************************************************************/
441
442/* Boot time configuration table */
443static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
444
445/**
446 * netdev_boot_setup_add - add new setup entry
447 * @name: name of the device
448 * @map: configured settings for the device
449 *
450 * Adds new setup entry to the dev_boot_setup list. The function
451 * returns 0 on error and 1 on success. This is a generic routine to
452 * all netdevices.
453 */
454static int netdev_boot_setup_add(char *name, struct ifmap *map)
455{
456 struct netdev_boot_setup *s;
457 int i;
458
459 s = dev_boot_setup;
460 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
461 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
462 memset(s[i].name, 0, sizeof(s[i].name));
Wang Chen93b3cff2008-07-01 19:57:19 -0700463 strlcpy(s[i].name, name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 memcpy(&s[i].map, map, sizeof(s[i].map));
465 break;
466 }
467 }
468
469 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
470}
471
472/**
473 * netdev_boot_setup_check - check boot time settings
474 * @dev: the netdevice
475 *
476 * Check boot time settings for the device.
477 * The found settings are set for the device to be used
478 * later in the device probing.
479 * Returns 0 if no settings found, 1 if they are.
480 */
481int netdev_boot_setup_check(struct net_device *dev)
482{
483 struct netdev_boot_setup *s = dev_boot_setup;
484 int i;
485
486 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
487 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
Wang Chen93b3cff2008-07-01 19:57:19 -0700488 !strcmp(dev->name, s[i].name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 dev->irq = s[i].map.irq;
490 dev->base_addr = s[i].map.base_addr;
491 dev->mem_start = s[i].map.mem_start;
492 dev->mem_end = s[i].map.mem_end;
493 return 1;
494 }
495 }
496 return 0;
497}
498
499
500/**
501 * netdev_boot_base - get address from boot time settings
502 * @prefix: prefix for network device
503 * @unit: id for network device
504 *
505 * Check boot time settings for the base address of device.
506 * The found settings are set for the device to be used
507 * later in the device probing.
508 * Returns 0 if no settings found.
509 */
510unsigned long netdev_boot_base(const char *prefix, int unit)
511{
512 const struct netdev_boot_setup *s = dev_boot_setup;
513 char name[IFNAMSIZ];
514 int i;
515
516 sprintf(name, "%s%d", prefix, unit);
517
518 /*
519 * If device already registered then return base of 1
520 * to indicate not to probe for this interface
521 */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700522 if (__dev_get_by_name(&init_net, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return 1;
524
525 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
526 if (!strcmp(name, s[i].name))
527 return s[i].map.base_addr;
528 return 0;
529}
530
531/*
532 * Saves at boot time configured settings for any netdevice.
533 */
534int __init netdev_boot_setup(char *str)
535{
536 int ints[5];
537 struct ifmap map;
538
539 str = get_options(str, ARRAY_SIZE(ints), ints);
540 if (!str || !*str)
541 return 0;
542
543 /* Save settings */
544 memset(&map, 0, sizeof(map));
545 if (ints[0] > 0)
546 map.irq = ints[1];
547 if (ints[0] > 1)
548 map.base_addr = ints[2];
549 if (ints[0] > 2)
550 map.mem_start = ints[3];
551 if (ints[0] > 3)
552 map.mem_end = ints[4];
553
554 /* Add new entry to the list */
555 return netdev_boot_setup_add(str, &map);
556}
557
558__setup("netdev=", netdev_boot_setup);
559
560/*******************************************************************************
561
562 Device Interface Subroutines
563
564*******************************************************************************/
565
566/**
567 * __dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700568 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 * @name: name to find
570 *
571 * Find an interface by name. Must be called under RTNL semaphore
572 * or @dev_base_lock. If the name is found a pointer to the device
573 * is returned. If the name is not found then %NULL is returned. The
574 * reference counters are not incremented so the caller must be
575 * careful with locks.
576 */
577
Eric W. Biederman881d9662007-09-17 11:56:21 -0700578struct net_device *__dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 struct hlist_node *p;
581
Eric W. Biederman881d9662007-09-17 11:56:21 -0700582 hlist_for_each(p, dev_name_hash(net, name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct net_device *dev
584 = hlist_entry(p, struct net_device, name_hlist);
585 if (!strncmp(dev->name, name, IFNAMSIZ))
586 return dev;
587 }
588 return NULL;
589}
590
591/**
592 * dev_get_by_name - find a device by its name
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700593 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 * @name: name to find
595 *
596 * Find an interface by name. This can be called from any
597 * context and does its own locking. The returned handle has
598 * the usage count incremented and the caller must use dev_put() to
599 * release it when it is no longer needed. %NULL is returned if no
600 * matching device is found.
601 */
602
Eric W. Biederman881d9662007-09-17 11:56:21 -0700603struct net_device *dev_get_by_name(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 struct net_device *dev;
606
607 read_lock(&dev_base_lock);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700608 dev = __dev_get_by_name(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (dev)
610 dev_hold(dev);
611 read_unlock(&dev_base_lock);
612 return dev;
613}
614
615/**
616 * __dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700617 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 * @ifindex: index of device
619 *
620 * Search for an interface by index. Returns %NULL if the device
621 * is not found or a pointer to the device. The device has not
622 * had its reference counter increased so the caller must be careful
623 * about locking. The caller must hold either the RTNL semaphore
624 * or @dev_base_lock.
625 */
626
Eric W. Biederman881d9662007-09-17 11:56:21 -0700627struct net_device *__dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 struct hlist_node *p;
630
Eric W. Biederman881d9662007-09-17 11:56:21 -0700631 hlist_for_each(p, dev_index_hash(net, ifindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct net_device *dev
633 = hlist_entry(p, struct net_device, index_hlist);
634 if (dev->ifindex == ifindex)
635 return dev;
636 }
637 return NULL;
638}
639
640
641/**
642 * dev_get_by_index - find a device by its ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700643 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 * @ifindex: index of device
645 *
646 * Search for an interface by index. Returns NULL if the device
647 * is not found or a pointer to the device. The device returned has
648 * had a reference added and the pointer is safe until the user calls
649 * dev_put to indicate they have finished with it.
650 */
651
Eric W. Biederman881d9662007-09-17 11:56:21 -0700652struct net_device *dev_get_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 struct net_device *dev;
655
656 read_lock(&dev_base_lock);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700657 dev = __dev_get_by_index(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (dev)
659 dev_hold(dev);
660 read_unlock(&dev_base_lock);
661 return dev;
662}
663
664/**
665 * dev_getbyhwaddr - find a device by its hardware address
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700666 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 * @type: media type of device
668 * @ha: hardware address
669 *
670 * Search for an interface by MAC address. Returns NULL if the device
671 * is not found or a pointer to the device. The caller must hold the
672 * rtnl semaphore. The returned device has not had its ref count increased
673 * and the caller must therefore be careful about locking
674 *
675 * BUGS:
676 * If the API was consistent this would be __dev_get_by_hwaddr
677 */
678
Eric W. Biederman881d9662007-09-17 11:56:21 -0700679struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type, char *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 struct net_device *dev;
682
683 ASSERT_RTNL();
684
Denis V. Lunev81103a52007-12-12 10:47:38 -0800685 for_each_netdev(net, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (dev->type == type &&
687 !memcmp(dev->dev_addr, ha, dev->addr_len))
Pavel Emelianov7562f872007-05-03 15:13:45 -0700688 return dev;
689
690 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
Jochen Friedrichcf309e32005-09-22 04:44:55 -0300693EXPORT_SYMBOL(dev_getbyhwaddr);
694
Eric W. Biederman881d9662007-09-17 11:56:21 -0700695struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700696{
697 struct net_device *dev;
698
699 ASSERT_RTNL();
Eric W. Biederman881d9662007-09-17 11:56:21 -0700700 for_each_netdev(net, dev)
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700701 if (dev->type == type)
Pavel Emelianov7562f872007-05-03 15:13:45 -0700702 return dev;
703
704 return NULL;
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700705}
706
707EXPORT_SYMBOL(__dev_getfirstbyhwtype);
708
Eric W. Biederman881d9662007-09-17 11:56:21 -0700709struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 struct net_device *dev;
712
713 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -0700714 dev = __dev_getfirstbyhwtype(net, type);
Patrick McHardy4e9cac22007-05-03 03:28:13 -0700715 if (dev)
716 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 rtnl_unlock();
718 return dev;
719}
720
721EXPORT_SYMBOL(dev_getfirstbyhwtype);
722
723/**
724 * dev_get_by_flags - find any device with given flags
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700725 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 * @if_flags: IFF_* values
727 * @mask: bitmask of bits in if_flags to check
728 *
729 * Search for any interface with the given flags. Returns NULL if a device
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900730 * is not found or a pointer to the device. The device returned has
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 * had a reference added and the pointer is safe until the user calls
732 * dev_put to indicate they have finished with it.
733 */
734
Eric W. Biederman881d9662007-09-17 11:56:21 -0700735struct net_device * dev_get_by_flags(struct net *net, unsigned short if_flags, unsigned short mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Pavel Emelianov7562f872007-05-03 15:13:45 -0700737 struct net_device *dev, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Pavel Emelianov7562f872007-05-03 15:13:45 -0700739 ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 read_lock(&dev_base_lock);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700741 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (((dev->flags ^ if_flags) & mask) == 0) {
743 dev_hold(dev);
Pavel Emelianov7562f872007-05-03 15:13:45 -0700744 ret = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 break;
746 }
747 }
748 read_unlock(&dev_base_lock);
Pavel Emelianov7562f872007-05-03 15:13:45 -0700749 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
752/**
753 * dev_valid_name - check if name is okay for network device
754 * @name: name string
755 *
756 * Network device names need to be valid file names to
David S. Millerc7fa9d12006-08-15 16:34:13 -0700757 * to allow sysfs to work. We also disallow any kind of
758 * whitespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 */
Mitch Williamsc2373ee2005-11-09 10:34:45 -0800760int dev_valid_name(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
David S. Millerc7fa9d12006-08-15 16:34:13 -0700762 if (*name == '\0')
763 return 0;
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -0700764 if (strlen(name) >= IFNAMSIZ)
765 return 0;
David S. Millerc7fa9d12006-08-15 16:34:13 -0700766 if (!strcmp(name, ".") || !strcmp(name, ".."))
767 return 0;
768
769 while (*name) {
770 if (*name == '/' || isspace(*name))
771 return 0;
772 name++;
773 }
774 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777/**
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200778 * __dev_alloc_name - allocate a name for a device
779 * @net: network namespace to allocate the device name in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 * @name: name format string
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200781 * @buf: scratch buffer and result name string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 *
783 * Passed a format string - eg "lt%d" it will try and find a suitable
Stephen Hemminger3041a062006-05-26 13:25:24 -0700784 * id. It scans list of devices to build up a free map, then chooses
785 * the first empty slot. The caller must hold the dev_base or rtnl lock
786 * while allocating the name and adding the device in order to avoid
787 * duplicates.
788 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
789 * Returns the number of the unit assigned or a negative errno code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 */
791
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200792static int __dev_alloc_name(struct net *net, const char *name, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 const char *p;
796 const int max_netdevices = 8*PAGE_SIZE;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700797 unsigned long *inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct net_device *d;
799
800 p = strnchr(name, IFNAMSIZ-1, '%');
801 if (p) {
802 /*
803 * Verify the string as this thing may have come from
804 * the user. There must be either one "%d" and no other "%"
805 * characters.
806 */
807 if (p[1] != 'd' || strchr(p + 2, '%'))
808 return -EINVAL;
809
810 /* Use one page as a bit array of possible slots */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700811 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (!inuse)
813 return -ENOMEM;
814
Eric W. Biederman881d9662007-09-17 11:56:21 -0700815 for_each_netdev(net, d) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (!sscanf(d->name, name, &i))
817 continue;
818 if (i < 0 || i >= max_netdevices)
819 continue;
820
821 /* avoid cases where sscanf is not exact inverse of printf */
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200822 snprintf(buf, IFNAMSIZ, name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (!strncmp(buf, d->name, IFNAMSIZ))
824 set_bit(i, inuse);
825 }
826
827 i = find_first_zero_bit(inuse, max_netdevices);
828 free_page((unsigned long) inuse);
829 }
830
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200831 snprintf(buf, IFNAMSIZ, name, i);
832 if (!__dev_get_by_name(net, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 /* It is possible to run out of possible slots
836 * when the name is long and there isn't enough space left
837 * for the digits, or if all bits are used.
838 */
839 return -ENFILE;
840}
841
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200842/**
843 * dev_alloc_name - allocate a name for a device
844 * @dev: device
845 * @name: name format string
846 *
847 * Passed a format string - eg "lt%d" it will try and find a suitable
848 * id. It scans list of devices to build up a free map, then chooses
849 * the first empty slot. The caller must hold the dev_base or rtnl lock
850 * while allocating the name and adding the device in order to avoid
851 * duplicates.
852 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
853 * Returns the number of the unit assigned or a negative errno code.
854 */
855
856int dev_alloc_name(struct net_device *dev, const char *name)
857{
858 char buf[IFNAMSIZ];
859 struct net *net;
860 int ret;
861
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900862 BUG_ON(!dev_net(dev));
863 net = dev_net(dev);
Eric W. Biedermanb267b172007-09-12 13:48:45 +0200864 ret = __dev_alloc_name(net, name, buf);
865 if (ret >= 0)
866 strlcpy(dev->name, buf, IFNAMSIZ);
867 return ret;
868}
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871/**
872 * dev_change_name - change name of a device
873 * @dev: device
874 * @newname: name (or format string) must be at least IFNAMSIZ
875 *
876 * Change name of a device, can pass format strings "eth%d".
877 * for wildcarding.
878 */
879int dev_change_name(struct net_device *dev, char *newname)
880{
Herbert Xufcc5a032007-07-30 17:03:38 -0700881 char oldname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 int err = 0;
Herbert Xufcc5a032007-07-30 17:03:38 -0700883 int ret;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700884 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 ASSERT_RTNL();
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900887 BUG_ON(!dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900889 net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (dev->flags & IFF_UP)
891 return -EBUSY;
892
893 if (!dev_valid_name(newname))
894 return -EINVAL;
895
Stephen Hemmingerc8d90dc2007-10-26 03:53:42 -0700896 if (strncmp(newname, dev->name, IFNAMSIZ) == 0)
897 return 0;
898
Herbert Xufcc5a032007-07-30 17:03:38 -0700899 memcpy(oldname, dev->name, IFNAMSIZ);
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (strchr(newname, '%')) {
902 err = dev_alloc_name(dev, newname);
903 if (err < 0)
904 return err;
905 strcpy(newname, dev->name);
906 }
Eric W. Biederman881d9662007-09-17 11:56:21 -0700907 else if (__dev_get_by_name(net, newname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return -EEXIST;
909 else
910 strlcpy(dev->name, newname, IFNAMSIZ);
911
Herbert Xufcc5a032007-07-30 17:03:38 -0700912rollback:
Stephen Hemmingerdcc99772008-05-14 22:33:38 -0700913 err = device_rename(&dev->dev, dev->name);
914 if (err) {
915 memcpy(dev->name, oldname, IFNAMSIZ);
916 return err;
917 }
Herbert Xu7f988ea2007-07-30 16:35:46 -0700918
919 write_lock_bh(&dev_base_lock);
Eric W. Biederman92749822007-04-03 00:07:30 -0600920 hlist_del(&dev->name_hlist);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700921 hlist_add_head(&dev->name_hlist, dev_name_hash(net, dev->name));
Herbert Xu7f988ea2007-07-30 16:35:46 -0700922 write_unlock_bh(&dev_base_lock);
923
Pavel Emelyanov056925a2007-09-16 15:42:43 -0700924 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -0700925 ret = notifier_to_errno(ret);
926
927 if (ret) {
928 if (err) {
929 printk(KERN_ERR
930 "%s: name change rollback failed: %d.\n",
931 dev->name, ret);
932 } else {
933 err = ret;
934 memcpy(dev->name, oldname, IFNAMSIZ);
935 goto rollback;
936 }
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 return err;
940}
941
942/**
Stephen Hemminger3041a062006-05-26 13:25:24 -0700943 * netdev_features_change - device changes features
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -0700944 * @dev: device to cause notification
945 *
946 * Called to indicate a device has changed features.
947 */
948void netdev_features_change(struct net_device *dev)
949{
Pavel Emelyanov056925a2007-09-16 15:42:43 -0700950 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -0700951}
952EXPORT_SYMBOL(netdev_features_change);
953
954/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 * netdev_state_change - device changes state
956 * @dev: device to cause notification
957 *
958 * Called to indicate a device has changed state. This function calls
959 * the notifier chains for netdev_chain and sends a NEWLINK message
960 * to the routing socket.
961 */
962void netdev_state_change(struct net_device *dev)
963{
964 if (dev->flags & IFF_UP) {
Pavel Emelyanov056925a2007-09-16 15:42:43 -0700965 call_netdevice_notifiers(NETDEV_CHANGE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
967 }
968}
969
Or Gerlitzc1da4ac2008-06-13 18:12:00 -0700970void netdev_bonding_change(struct net_device *dev)
971{
972 call_netdevice_notifiers(NETDEV_BONDING_FAILOVER, dev);
973}
974EXPORT_SYMBOL(netdev_bonding_change);
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976/**
977 * dev_load - load a network module
Randy Dunlapc4ea43c2007-10-12 21:17:49 -0700978 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 * @name: name of interface
980 *
981 * If a network interface is not present and the process has suitable
982 * privileges this function loads the module. If module loading is not
983 * available in this kernel then it becomes a nop.
984 */
985
Eric W. Biederman881d9662007-09-17 11:56:21 -0700986void dev_load(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900988 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 read_lock(&dev_base_lock);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700991 dev = __dev_get_by_name(net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 read_unlock(&dev_base_lock);
993
994 if (!dev && capable(CAP_SYS_MODULE))
995 request_module("%s", name);
996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998/**
999 * dev_open - prepare an interface for use.
1000 * @dev: device to open
1001 *
1002 * Takes a device from down to up state. The device's private open
1003 * function is invoked and then the multicast lists are loaded. Finally
1004 * the device is moved into the up state and a %NETDEV_UP message is
1005 * sent to the netdev notifier chain.
1006 *
1007 * Calling this function on an active interface is a nop. On a failure
1008 * a negative errno code is returned.
1009 */
1010int dev_open(struct net_device *dev)
1011{
1012 int ret = 0;
1013
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001014 ASSERT_RTNL();
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 /*
1017 * Is it already up?
1018 */
1019
1020 if (dev->flags & IFF_UP)
1021 return 0;
1022
1023 /*
1024 * Is it even present?
1025 */
1026 if (!netif_device_present(dev))
1027 return -ENODEV;
1028
1029 /*
1030 * Call device private open method
1031 */
1032 set_bit(__LINK_STATE_START, &dev->state);
Jeff Garzikbada3392007-10-23 20:19:37 -07001033
1034 if (dev->validate_addr)
1035 ret = dev->validate_addr(dev);
1036
1037 if (!ret && dev->open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 ret = dev->open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001040 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 * If it went open OK then:
1042 */
1043
Jeff Garzikbada3392007-10-23 20:19:37 -07001044 if (ret)
1045 clear_bit(__LINK_STATE_START, &dev->state);
1046 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /*
1048 * Set the flags.
1049 */
1050 dev->flags |= IFF_UP;
1051
1052 /*
1053 * Initialize multicasting status
1054 */
Patrick McHardy4417da62007-06-27 01:28:10 -07001055 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 /*
1058 * Wakeup transmit queue engine
1059 */
1060 dev_activate(dev);
1061
1062 /*
1063 * ... and announce new interface.
1064 */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001065 call_netdevice_notifiers(NETDEV_UP, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
Jeff Garzikbada3392007-10-23 20:19:37 -07001067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return ret;
1069}
1070
1071/**
1072 * dev_close - shutdown an interface.
1073 * @dev: device to shutdown
1074 *
1075 * This function moves an active device into down state. A
1076 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1077 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1078 * chain.
1079 */
1080int dev_close(struct net_device *dev)
1081{
Ben Hutchingse46b66b2008-05-08 02:53:17 -07001082 ASSERT_RTNL();
1083
David S. Miller9d5010d2007-09-12 14:33:25 +02001084 might_sleep();
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (!(dev->flags & IFF_UP))
1087 return 0;
1088
1089 /*
1090 * Tell people we are going down, so that they can
1091 * prepare to death, when device is still operating.
1092 */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001093 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 clear_bit(__LINK_STATE_START, &dev->state);
1096
1097 /* Synchronize to scheduled poll. We cannot touch poll list,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001098 * it can be even on different cpu. So just clear netif_running().
1099 *
1100 * dev->stop() will invoke napi_disable() on all of it's
1101 * napi_struct instances on this device.
1102 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 smp_mb__after_clear_bit(); /* Commit netif_running(). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Matti Linnanvuorid8b2a4d2008-02-12 23:10:11 -08001105 dev_deactivate(dev);
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 /*
1108 * Call the device specific close. This cannot fail.
1109 * Only if device is UP
1110 *
1111 * We allow it to be called even after a DETACH hot-plug
1112 * event.
1113 */
1114 if (dev->stop)
1115 dev->stop(dev);
1116
1117 /*
1118 * Device is now down.
1119 */
1120
1121 dev->flags &= ~IFF_UP;
1122
1123 /*
1124 * Tell people we are down
1125 */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07001126 call_netdevice_notifiers(NETDEV_DOWN, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 return 0;
1129}
1130
1131
Ben Hutchings0187bdf2008-06-19 16:15:47 -07001132/**
1133 * dev_disable_lro - disable Large Receive Offload on a device
1134 * @dev: device
1135 *
1136 * Disable Large Receive Offload (LRO) on a net device. Must be
1137 * called under RTNL. This is needed if received packets may be
1138 * forwarded to another interface.
1139 */
1140void dev_disable_lro(struct net_device *dev)
1141{
1142 if (dev->ethtool_ops && dev->ethtool_ops->get_flags &&
1143 dev->ethtool_ops->set_flags) {
1144 u32 flags = dev->ethtool_ops->get_flags(dev);
1145 if (flags & ETH_FLAG_LRO) {
1146 flags &= ~ETH_FLAG_LRO;
1147 dev->ethtool_ops->set_flags(dev, flags);
1148 }
1149 }
1150 WARN_ON(dev->features & NETIF_F_LRO);
1151}
1152EXPORT_SYMBOL(dev_disable_lro);
1153
1154
Eric W. Biederman881d9662007-09-17 11:56:21 -07001155static int dev_boot_phase = 1;
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157/*
1158 * Device change register/unregister. These are not inline or static
1159 * as we export them to the world.
1160 */
1161
1162/**
1163 * register_netdevice_notifier - register a network notifier block
1164 * @nb: notifier
1165 *
1166 * Register a notifier to be called when network device events occur.
1167 * The notifier passed is linked into the kernel structures and must
1168 * not be reused until it has been unregistered. A negative errno code
1169 * is returned on a failure.
1170 *
1171 * When registered all registration and up events are replayed
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001172 * to the new notifier to allow device to have a race free
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 * view of the network device list.
1174 */
1175
1176int register_netdevice_notifier(struct notifier_block *nb)
1177{
1178 struct net_device *dev;
Herbert Xufcc5a032007-07-30 17:03:38 -07001179 struct net_device *last;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001180 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 int err;
1182
1183 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001184 err = raw_notifier_chain_register(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001185 if (err)
1186 goto unlock;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001187 if (dev_boot_phase)
1188 goto unlock;
1189 for_each_net(net) {
1190 for_each_netdev(net, dev) {
1191 err = nb->notifier_call(nb, NETDEV_REGISTER, dev);
1192 err = notifier_to_errno(err);
1193 if (err)
1194 goto rollback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Eric W. Biederman881d9662007-09-17 11:56:21 -07001196 if (!(dev->flags & IFF_UP))
1197 continue;
Herbert Xufcc5a032007-07-30 17:03:38 -07001198
Eric W. Biederman881d9662007-09-17 11:56:21 -07001199 nb->notifier_call(nb, NETDEV_UP, dev);
1200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001202
1203unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 rtnl_unlock();
1205 return err;
Herbert Xufcc5a032007-07-30 17:03:38 -07001206
1207rollback:
1208 last = dev;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001209 for_each_net(net) {
1210 for_each_netdev(net, dev) {
1211 if (dev == last)
1212 break;
Herbert Xufcc5a032007-07-30 17:03:38 -07001213
Eric W. Biederman881d9662007-09-17 11:56:21 -07001214 if (dev->flags & IFF_UP) {
1215 nb->notifier_call(nb, NETDEV_GOING_DOWN, dev);
1216 nb->notifier_call(nb, NETDEV_DOWN, dev);
1217 }
1218 nb->notifier_call(nb, NETDEV_UNREGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07001219 }
Herbert Xufcc5a032007-07-30 17:03:38 -07001220 }
Pavel Emelyanovc67625a2007-11-14 15:53:16 -08001221
1222 raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xufcc5a032007-07-30 17:03:38 -07001223 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226/**
1227 * unregister_netdevice_notifier - unregister a network notifier block
1228 * @nb: notifier
1229 *
1230 * Unregister a notifier previously registered by
1231 * register_netdevice_notifier(). The notifier is unlinked into the
1232 * kernel structures and may then be reused. A negative errno code
1233 * is returned on a failure.
1234 */
1235
1236int unregister_netdevice_notifier(struct notifier_block *nb)
1237{
Herbert Xu9f514952006-03-25 01:24:25 -08001238 int err;
1239
1240 rtnl_lock();
Alan Sternf07d5b92006-05-09 15:23:03 -07001241 err = raw_notifier_chain_unregister(&netdev_chain, nb);
Herbert Xu9f514952006-03-25 01:24:25 -08001242 rtnl_unlock();
1243 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
1246/**
1247 * call_netdevice_notifiers - call all network notifier blocks
1248 * @val: value passed unmodified to notifier function
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07001249 * @dev: net_device pointer passed unmodified to notifier function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 *
1251 * Call all network notifier blocks. Parameters and return value
Alan Sternf07d5b92006-05-09 15:23:03 -07001252 * are as for raw_notifier_call_chain().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 */
1254
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07001255int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Eric W. Biedermanad7379d2007-09-16 15:33:32 -07001257 return raw_notifier_call_chain(&netdev_chain, val, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
1260/* When > 0 there are consumers of rx skb time stamps */
1261static atomic_t netstamp_needed = ATOMIC_INIT(0);
1262
1263void net_enable_timestamp(void)
1264{
1265 atomic_inc(&netstamp_needed);
1266}
1267
1268void net_disable_timestamp(void)
1269{
1270 atomic_dec(&netstamp_needed);
1271}
1272
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001273static inline void net_timestamp(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 if (atomic_read(&netstamp_needed))
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001276 __net_timestamp(skb);
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07001277 else
1278 skb->tstamp.tv64 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
1281/*
1282 * Support routine. Sends outgoing frames to any network
1283 * taps currently in use.
1284 */
1285
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001286static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
1288 struct packet_type *ptype;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001289
1290 net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 rcu_read_lock();
1293 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1294 /* Never send packets back to the socket
1295 * they originated from - MvS (miquels@drinkel.ow.org)
1296 */
1297 if ((ptype->dev == dev || !ptype->dev) &&
1298 (ptype->af_packet_priv == NULL ||
1299 (struct sock *)ptype->af_packet_priv != skb->sk)) {
1300 struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
1301 if (!skb2)
1302 break;
1303
1304 /* skb->nh should be correctly
1305 set by sender, so that the second statement is
1306 just protection against buggy protocols.
1307 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001308 skb_reset_mac_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001310 if (skb_network_header(skb2) < skb2->data ||
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001311 skb2->network_header > skb2->tail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (net_ratelimit())
1313 printk(KERN_CRIT "protocol %04x is "
1314 "buggy, dev %s\n",
1315 skb2->protocol, dev->name);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07001316 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001319 skb2->transport_header = skb2->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 skb2->pkt_type = PACKET_OUTGOING;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001321 ptype->func(skb2, skb->dev, ptype, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323 }
1324 rcu_read_unlock();
1325}
1326
Denis Vlasenko56079432006-03-29 15:57:29 -08001327
David S. Miller37437bb2008-07-16 02:15:04 -07001328void __netif_schedule(struct Qdisc *q)
Denis Vlasenko56079432006-03-29 15:57:29 -08001329{
David S. Miller37437bb2008-07-16 02:15:04 -07001330 BUG_ON(q == &noop_qdisc);
David S. Miller86d804e2008-07-08 23:11:25 -07001331
David S. Miller37437bb2008-07-16 02:15:04 -07001332 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state)) {
Denis Vlasenko56079432006-03-29 15:57:29 -08001333 struct softnet_data *sd;
David S. Miller86d804e2008-07-08 23:11:25 -07001334 unsigned long flags;
Denis Vlasenko56079432006-03-29 15:57:29 -08001335
1336 local_irq_save(flags);
1337 sd = &__get_cpu_var(softnet_data);
David S. Miller37437bb2008-07-16 02:15:04 -07001338 q->next_sched = sd->output_queue;
1339 sd->output_queue = q;
Denis Vlasenko56079432006-03-29 15:57:29 -08001340 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1341 local_irq_restore(flags);
1342 }
1343}
1344EXPORT_SYMBOL(__netif_schedule);
1345
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001346void dev_kfree_skb_irq(struct sk_buff *skb)
Denis Vlasenko56079432006-03-29 15:57:29 -08001347{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001348 if (atomic_dec_and_test(&skb->users)) {
1349 struct softnet_data *sd;
1350 unsigned long flags;
Denis Vlasenko56079432006-03-29 15:57:29 -08001351
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001352 local_irq_save(flags);
1353 sd = &__get_cpu_var(softnet_data);
1354 skb->next = sd->completion_queue;
1355 sd->completion_queue = skb;
1356 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1357 local_irq_restore(flags);
1358 }
Denis Vlasenko56079432006-03-29 15:57:29 -08001359}
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001360EXPORT_SYMBOL(dev_kfree_skb_irq);
Denis Vlasenko56079432006-03-29 15:57:29 -08001361
1362void dev_kfree_skb_any(struct sk_buff *skb)
1363{
1364 if (in_irq() || irqs_disabled())
1365 dev_kfree_skb_irq(skb);
1366 else
1367 dev_kfree_skb(skb);
1368}
1369EXPORT_SYMBOL(dev_kfree_skb_any);
1370
1371
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001372/**
1373 * netif_device_detach - mark device as removed
1374 * @dev: network device
1375 *
1376 * Mark device as removed from system and therefore no longer available.
1377 */
Denis Vlasenko56079432006-03-29 15:57:29 -08001378void netif_device_detach(struct net_device *dev)
1379{
1380 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
1381 netif_running(dev)) {
1382 netif_stop_queue(dev);
1383 }
1384}
1385EXPORT_SYMBOL(netif_device_detach);
1386
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001387/**
1388 * netif_device_attach - mark device as attached
1389 * @dev: network device
1390 *
1391 * Mark device as attached from system and restart if needed.
1392 */
Denis Vlasenko56079432006-03-29 15:57:29 -08001393void netif_device_attach(struct net_device *dev)
1394{
1395 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
1396 netif_running(dev)) {
1397 netif_wake_queue(dev);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001398 __netdev_watchdog_up(dev);
Denis Vlasenko56079432006-03-29 15:57:29 -08001399 }
1400}
1401EXPORT_SYMBOL(netif_device_attach);
1402
Ben Hutchings6de329e2008-06-16 17:02:28 -07001403static bool can_checksum_protocol(unsigned long features, __be16 protocol)
1404{
1405 return ((features & NETIF_F_GEN_CSUM) ||
1406 ((features & NETIF_F_IP_CSUM) &&
1407 protocol == htons(ETH_P_IP)) ||
1408 ((features & NETIF_F_IPV6_CSUM) &&
1409 protocol == htons(ETH_P_IPV6)));
1410}
1411
1412static bool dev_can_checksum(struct net_device *dev, struct sk_buff *skb)
1413{
1414 if (can_checksum_protocol(dev->features, skb->protocol))
1415 return true;
1416
1417 if (skb->protocol == htons(ETH_P_8021Q)) {
1418 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
1419 if (can_checksum_protocol(dev->features & dev->vlan_features,
1420 veh->h_vlan_encapsulated_proto))
1421 return true;
1422 }
1423
1424 return false;
1425}
Denis Vlasenko56079432006-03-29 15:57:29 -08001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427/*
1428 * Invalidate hardware checksum when packet is to be mangled, and
1429 * complete checksum manually on outgoing path.
1430 */
Patrick McHardy84fa7932006-08-29 16:44:56 -07001431int skb_checksum_help(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Al Virod3bc23e2006-11-14 21:24:49 -08001433 __wsum csum;
Herbert Xu663ead32007-04-09 11:59:07 -07001434 int ret = 0, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Patrick McHardy84fa7932006-08-29 16:44:56 -07001436 if (skb->ip_summed == CHECKSUM_COMPLETE)
Herbert Xua430a432006-07-08 13:34:56 -07001437 goto out_set_summed;
1438
1439 if (unlikely(skb_shinfo(skb)->gso_size)) {
Herbert Xua430a432006-07-08 13:34:56 -07001440 /* Let GSO fix up the checksum. */
1441 goto out_set_summed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
1443
Herbert Xua0308472007-10-15 01:47:15 -07001444 offset = skb->csum_start - skb_headroom(skb);
1445 BUG_ON(offset >= skb_headlen(skb));
1446 csum = skb_checksum(skb, offset, skb->len - offset, 0);
1447
1448 offset += skb->csum_offset;
1449 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
1450
1451 if (skb_cloned(skb) &&
1452 !skb_clone_writable(skb, offset + sizeof(__sum16))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1454 if (ret)
1455 goto out;
1456 }
1457
Herbert Xua0308472007-10-15 01:47:15 -07001458 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
Herbert Xua430a432006-07-08 13:34:56 -07001459out_set_summed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 skb->ip_summed = CHECKSUM_NONE;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001461out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return ret;
1463}
1464
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001465/**
1466 * skb_gso_segment - Perform segmentation on skb.
1467 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001468 * @features: features for the output path (see dev->features)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001469 *
1470 * This function segments the given skb and returns a list of segments.
Herbert Xu576a30e2006-06-27 13:22:38 -07001471 *
1472 * It may return NULL if the skb requires no segmentation. This is
1473 * only possible when GSO is used for verifying header integrity.
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001474 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001475struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001476{
1477 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
1478 struct packet_type *ptype;
Al Viro252e33462006-11-14 20:48:11 -08001479 __be16 type = skb->protocol;
Herbert Xua430a432006-07-08 13:34:56 -07001480 int err;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001481
1482 BUG_ON(skb_shinfo(skb)->frag_list);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001483
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001484 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001485 skb->mac_len = skb->network_header - skb->mac_header;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001486 __skb_pull(skb, skb->mac_len);
1487
Herbert Xuf9d106a2007-04-23 22:36:13 -07001488 if (WARN_ON(skb->ip_summed != CHECKSUM_PARTIAL)) {
Herbert Xua430a432006-07-08 13:34:56 -07001489 if (skb_header_cloned(skb) &&
1490 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
1491 return ERR_PTR(err);
1492 }
1493
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001494 rcu_read_lock();
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08001495 list_for_each_entry_rcu(ptype,
1496 &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001497 if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
Patrick McHardy84fa7932006-08-29 16:44:56 -07001498 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
Herbert Xua430a432006-07-08 13:34:56 -07001499 err = ptype->gso_send_check(skb);
1500 segs = ERR_PTR(err);
1501 if (err || skb_gso_ok(skb, features))
1502 break;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001503 __skb_push(skb, (skb->data -
1504 skb_network_header(skb)));
Herbert Xua430a432006-07-08 13:34:56 -07001505 }
Herbert Xu576a30e2006-06-27 13:22:38 -07001506 segs = ptype->gso_segment(skb, features);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001507 break;
1508 }
1509 }
1510 rcu_read_unlock();
1511
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001512 __skb_push(skb, skb->data - skb_mac_header(skb));
Herbert Xu576a30e2006-06-27 13:22:38 -07001513
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001514 return segs;
1515}
1516
1517EXPORT_SYMBOL(skb_gso_segment);
1518
Herbert Xufb286bb2005-11-10 13:01:24 -08001519/* Take action when hardware reception checksum errors are detected. */
1520#ifdef CONFIG_BUG
1521void netdev_rx_csum_fault(struct net_device *dev)
1522{
1523 if (net_ratelimit()) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001524 printk(KERN_ERR "%s: hw csum failure.\n",
Stephen Hemminger246a4212005-12-08 15:21:39 -08001525 dev ? dev->name : "<unknown>");
Herbert Xufb286bb2005-11-10 13:01:24 -08001526 dump_stack();
1527 }
1528}
1529EXPORT_SYMBOL(netdev_rx_csum_fault);
1530#endif
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532/* Actually, we should eliminate this check as soon as we know, that:
1533 * 1. IOMMU is present and allows to map all the memory.
1534 * 2. No high memory really exists on this machine.
1535 */
1536
1537static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1538{
Herbert Xu3d3a8532006-06-27 13:33:10 -07001539#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 int i;
1541
1542 if (dev->features & NETIF_F_HIGHDMA)
1543 return 0;
1544
1545 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1546 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1547 return 1;
1548
Herbert Xu3d3a8532006-06-27 13:33:10 -07001549#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return 0;
1551}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001553struct dev_gso_cb {
1554 void (*destructor)(struct sk_buff *skb);
1555};
1556
1557#define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
1558
1559static void dev_gso_skb_destructor(struct sk_buff *skb)
1560{
1561 struct dev_gso_cb *cb;
1562
1563 do {
1564 struct sk_buff *nskb = skb->next;
1565
1566 skb->next = nskb->next;
1567 nskb->next = NULL;
1568 kfree_skb(nskb);
1569 } while (skb->next);
1570
1571 cb = DEV_GSO_CB(skb);
1572 if (cb->destructor)
1573 cb->destructor(skb);
1574}
1575
1576/**
1577 * dev_gso_segment - Perform emulated hardware segmentation on skb.
1578 * @skb: buffer to segment
1579 *
1580 * This function segments the given skb and stores the list of segments
1581 * in skb->next.
1582 */
1583static int dev_gso_segment(struct sk_buff *skb)
1584{
1585 struct net_device *dev = skb->dev;
1586 struct sk_buff *segs;
Herbert Xu576a30e2006-06-27 13:22:38 -07001587 int features = dev->features & ~(illegal_highdma(dev, skb) ?
1588 NETIF_F_SG : 0);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001589
Herbert Xu576a30e2006-06-27 13:22:38 -07001590 segs = skb_gso_segment(skb, features);
1591
1592 /* Verifying header integrity only. */
1593 if (!segs)
1594 return 0;
1595
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07001596 if (IS_ERR(segs))
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001597 return PTR_ERR(segs);
1598
1599 skb->next = segs;
1600 DEV_GSO_CB(skb)->destructor = skb->destructor;
1601 skb->destructor = dev_gso_skb_destructor;
1602
1603 return 0;
1604}
1605
David S. Millerfd2ea0a2008-07-17 01:56:23 -07001606int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
1607 struct netdev_queue *txq)
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001608{
1609 if (likely(!skb->next)) {
Stephen Hemminger9be9a6b2007-04-20 17:02:45 -07001610 if (!list_empty(&ptype_all))
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001611 dev_queue_xmit_nit(skb, dev);
1612
Herbert Xu576a30e2006-06-27 13:22:38 -07001613 if (netif_needs_gso(dev, skb)) {
1614 if (unlikely(dev_gso_segment(skb)))
1615 goto out_kfree_skb;
1616 if (skb->next)
1617 goto gso;
1618 }
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001619
Herbert Xu576a30e2006-06-27 13:22:38 -07001620 return dev->hard_start_xmit(skb, dev);
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001621 }
1622
Herbert Xu576a30e2006-06-27 13:22:38 -07001623gso:
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001624 do {
1625 struct sk_buff *nskb = skb->next;
1626 int rc;
1627
1628 skb->next = nskb->next;
1629 nskb->next = NULL;
1630 rc = dev->hard_start_xmit(nskb, dev);
1631 if (unlikely(rc)) {
Michael Chanf54d9e82006-06-25 23:57:04 -07001632 nskb->next = skb->next;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001633 skb->next = nskb;
1634 return rc;
1635 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -07001636 if (unlikely(netif_tx_queue_stopped(txq) && skb->next))
Michael Chanf54d9e82006-06-25 23:57:04 -07001637 return NETDEV_TX_BUSY;
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001638 } while (skb->next);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001639
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001640 skb->destructor = DEV_GSO_CB(skb)->destructor;
1641
1642out_kfree_skb:
1643 kfree_skb(skb);
1644 return 0;
1645}
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647/**
1648 * dev_queue_xmit - transmit a buffer
1649 * @skb: buffer to transmit
1650 *
1651 * Queue a buffer for transmission to a network device. The caller must
1652 * have set the device and priority and built the buffer before calling
1653 * this function. The function can be called from an interrupt.
1654 *
1655 * A negative errno code is returned on a failure. A success does not
1656 * guarantee the frame will be transmitted as it may be dropped due
1657 * to congestion or traffic shaping.
Ben Greearaf191362005-04-24 20:12:36 -07001658 *
1659 * -----------------------------------------------------------------------------------
1660 * I notice this method can also return errors from the queue disciplines,
1661 * including NET_XMIT_DROP, which is a positive value. So, errors can also
1662 * be positive.
1663 *
1664 * Regardless of the return value, the skb is consumed, so it is currently
1665 * difficult to retry a send to this method. (You can bump the ref count
1666 * before sending to hold a reference for retry if you are careful.)
1667 *
1668 * When calling this method, interrupts MUST be enabled. This is because
1669 * the BH enable code must have IRQs enabled so that it will not deadlock.
1670 * --BLG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 */
1672
David S. Millerb6b2fed2008-07-21 09:48:06 -07001673static u32 simple_tx_hashrnd;
1674static int simple_tx_hashrnd_initialized = 0;
1675
David S. Miller8f0f2222008-07-15 03:47:03 -07001676static u16 simple_tx_hash(struct net_device *dev, struct sk_buff *skb)
1677{
David S. Millerb6b2fed2008-07-21 09:48:06 -07001678 u32 addr1, addr2, ports;
1679 u32 hash, ihl;
David S. Miller8f0f2222008-07-15 03:47:03 -07001680 u8 ip_proto;
David S. Millerb6b2fed2008-07-21 09:48:06 -07001681
1682 if (unlikely(!simple_tx_hashrnd_initialized)) {
1683 get_random_bytes(&simple_tx_hashrnd, 4);
1684 simple_tx_hashrnd_initialized = 1;
1685 }
David S. Miller8f0f2222008-07-15 03:47:03 -07001686
1687 switch (skb->protocol) {
1688 case __constant_htons(ETH_P_IP):
1689 ip_proto = ip_hdr(skb)->protocol;
David S. Millerb6b2fed2008-07-21 09:48:06 -07001690 addr1 = ip_hdr(skb)->saddr;
1691 addr2 = ip_hdr(skb)->daddr;
David S. Miller8f0f2222008-07-15 03:47:03 -07001692 ihl = ip_hdr(skb)->ihl;
David S. Miller8f0f2222008-07-15 03:47:03 -07001693 break;
1694 case __constant_htons(ETH_P_IPV6):
1695 ip_proto = ipv6_hdr(skb)->nexthdr;
David S. Millerb6b2fed2008-07-21 09:48:06 -07001696 addr1 = ipv6_hdr(skb)->saddr.s6_addr32[3];
1697 addr2 = ipv6_hdr(skb)->daddr.s6_addr32[3];
David S. Miller8f0f2222008-07-15 03:47:03 -07001698 ihl = (40 >> 2);
David S. Miller8f0f2222008-07-15 03:47:03 -07001699 break;
1700 default:
1701 return 0;
1702 }
1703
David S. Miller8f0f2222008-07-15 03:47:03 -07001704
1705 switch (ip_proto) {
1706 case IPPROTO_TCP:
1707 case IPPROTO_UDP:
1708 case IPPROTO_DCCP:
1709 case IPPROTO_ESP:
1710 case IPPROTO_AH:
1711 case IPPROTO_SCTP:
1712 case IPPROTO_UDPLITE:
David S. Millerb6b2fed2008-07-21 09:48:06 -07001713 ports = *((u32 *) (skb_network_header(skb) + (ihl * 4)));
David S. Miller8f0f2222008-07-15 03:47:03 -07001714 break;
1715
1716 default:
David S. Millerb6b2fed2008-07-21 09:48:06 -07001717 ports = 0;
David S. Miller8f0f2222008-07-15 03:47:03 -07001718 break;
1719 }
1720
David S. Millerb6b2fed2008-07-21 09:48:06 -07001721 hash = jhash_3words(addr1, addr2, ports, simple_tx_hashrnd);
1722
1723 return (u16) (((u64) hash * dev->real_num_tx_queues) >> 32);
David S. Miller8f0f2222008-07-15 03:47:03 -07001724}
1725
David S. Millere8a04642008-07-17 00:34:19 -07001726static struct netdev_queue *dev_pick_tx(struct net_device *dev,
1727 struct sk_buff *skb)
1728{
David S. Millerfd2ea0a2008-07-17 01:56:23 -07001729 u16 queue_index = 0;
1730
David S. Millereae792b2008-07-15 03:03:33 -07001731 if (dev->select_queue)
1732 queue_index = dev->select_queue(dev, skb);
David S. Miller8f0f2222008-07-15 03:47:03 -07001733 else if (dev->real_num_tx_queues > 1)
1734 queue_index = simple_tx_hash(dev, skb);
David S. Millereae792b2008-07-15 03:03:33 -07001735
David S. Millerfd2ea0a2008-07-17 01:56:23 -07001736 skb_set_queue_mapping(skb, queue_index);
1737 return netdev_get_tx_queue(dev, queue_index);
David S. Millere8a04642008-07-17 00:34:19 -07001738}
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740int dev_queue_xmit(struct sk_buff *skb)
1741{
1742 struct net_device *dev = skb->dev;
David S. Millerdc2b4842008-07-08 17:18:23 -07001743 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 struct Qdisc *q;
1745 int rc = -ENOMEM;
1746
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001747 /* GSO will handle the following emulations directly. */
1748 if (netif_needs_gso(dev, skb))
1749 goto gso;
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (skb_shinfo(skb)->frag_list &&
1752 !(dev->features & NETIF_F_FRAGLIST) &&
Herbert Xu364c6ba2006-06-09 16:10:40 -07001753 __skb_linearize(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 goto out_kfree_skb;
1755
1756 /* Fragmented skb is linearized if device does not support SG,
1757 * or if at least one of fragments is in highmem and device
1758 * does not support DMA from it.
1759 */
1760 if (skb_shinfo(skb)->nr_frags &&
1761 (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
Herbert Xu364c6ba2006-06-09 16:10:40 -07001762 __skb_linearize(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 goto out_kfree_skb;
1764
1765 /* If packet is not checksummed and device does not support
1766 * checksumming for this protocol, complete checksumming here.
1767 */
Herbert Xu663ead32007-04-09 11:59:07 -07001768 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1769 skb_set_transport_header(skb, skb->csum_start -
1770 skb_headroom(skb));
Ben Hutchings6de329e2008-06-16 17:02:28 -07001771 if (!dev_can_checksum(dev, skb) && skb_checksum_help(skb))
1772 goto out_kfree_skb;
Herbert Xu663ead32007-04-09 11:59:07 -07001773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Herbert Xuf6a78bf2006-06-22 02:57:17 -07001775gso:
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001776 /* Disable soft irqs for various locks below. Also
1777 * stops preemption for RCU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001779 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
David S. Millereae792b2008-07-15 03:03:33 -07001781 txq = dev_pick_tx(dev, skb);
David S. Millerb0e1e642008-07-08 17:42:10 -07001782 q = rcu_dereference(txq->qdisc);
David S. Miller37437bb2008-07-16 02:15:04 -07001783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784#ifdef CONFIG_NET_CLS_ACT
1785 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
1786#endif
1787 if (q->enqueue) {
David S. Miller37437bb2008-07-16 02:15:04 -07001788 spinlock_t *root_lock = qdisc_root_lock(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
David S. Miller37437bb2008-07-16 02:15:04 -07001790 spin_lock(root_lock);
1791
Jussi Kivilinna5f861732008-07-20 00:08:04 -07001792 rc = qdisc_enqueue_root(skb, q);
David S. Miller37437bb2008-07-16 02:15:04 -07001793 qdisc_run(q);
1794
1795 spin_unlock(root_lock);
1796
1797 rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
1798 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800
1801 /* The device has no queue. Common case for software devices:
1802 loopback, all the sorts of tunnels...
1803
Herbert Xu932ff272006-06-09 12:20:56 -07001804 Really, it is unlikely that netif_tx_lock protection is necessary
1805 here. (f.e. loopback and IP tunnels are clean ignoring statistics
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 counters.)
1807 However, it is possible, that they rely on protection
1808 made by us here.
1809
1810 Check this and shot the lock. It is not prone from deadlocks.
1811 Either shot noqueue qdisc, it is even simpler 8)
1812 */
1813 if (dev->flags & IFF_UP) {
1814 int cpu = smp_processor_id(); /* ok because BHs are off */
1815
David S. Millerc773e842008-07-08 23:13:53 -07001816 if (txq->xmit_lock_owner != cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
David S. Millerc773e842008-07-08 23:13:53 -07001818 HARD_TX_LOCK(dev, txq, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
David S. Millerfd2ea0a2008-07-17 01:56:23 -07001820 if (!netif_tx_queue_stopped(txq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 rc = 0;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07001822 if (!dev_hard_start_xmit(skb, dev, txq)) {
David S. Millerc773e842008-07-08 23:13:53 -07001823 HARD_TX_UNLOCK(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 goto out;
1825 }
1826 }
David S. Millerc773e842008-07-08 23:13:53 -07001827 HARD_TX_UNLOCK(dev, txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 if (net_ratelimit())
1829 printk(KERN_CRIT "Virtual device %s asks to "
1830 "queue packet!\n", dev->name);
1831 } else {
1832 /* Recursion is detected! It is possible,
1833 * unfortunately */
1834 if (net_ratelimit())
1835 printk(KERN_CRIT "Dead loop on virtual device "
1836 "%s, fix it urgently!\n", dev->name);
1837 }
1838 }
1839
1840 rc = -ENETDOWN;
Herbert Xud4828d82006-06-22 02:28:18 -07001841 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843out_kfree_skb:
1844 kfree_skb(skb);
1845 return rc;
1846out:
Herbert Xud4828d82006-06-22 02:28:18 -07001847 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return rc;
1849}
1850
1851
1852/*=======================================================================
1853 Receiver routines
1854 =======================================================================*/
1855
Stephen Hemminger6b2bedc2007-03-12 14:33:50 -07001856int netdev_max_backlog __read_mostly = 1000;
1857int netdev_budget __read_mostly = 300;
1858int weight_p __read_mostly = 64; /* old backlog weight */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
1861
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863/**
1864 * netif_rx - post buffer to the network code
1865 * @skb: buffer to post
1866 *
1867 * This function receives a packet from a device driver and queues it for
1868 * the upper (protocol) levels to process. It always succeeds. The buffer
1869 * may be dropped during processing for congestion control or by the
1870 * protocol layers.
1871 *
1872 * return values:
1873 * NET_RX_SUCCESS (no congestion)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 * NET_RX_DROP (packet was dropped)
1875 *
1876 */
1877
1878int netif_rx(struct sk_buff *skb)
1879{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 struct softnet_data *queue;
1881 unsigned long flags;
1882
1883 /* if netpoll wants it, pretend we never saw it */
1884 if (netpoll_rx(skb))
1885 return NET_RX_DROP;
1886
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07001887 if (!skb->tstamp.tv64)
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001888 net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 /*
1891 * The code is rearranged so that the path is the most
1892 * short when CPU is congested, but is still operating.
1893 */
1894 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 queue = &__get_cpu_var(softnet_data);
1896
1897 __get_cpu_var(netdev_rx_stat).total++;
1898 if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1899 if (queue->input_pkt_queue.qlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900enqueue:
1901 dev_hold(skb->dev);
1902 __skb_queue_tail(&queue->input_pkt_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 local_irq_restore(flags);
Stephen Hemminger34008d82005-06-23 20:10:00 -07001904 return NET_RX_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
1906
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001907 napi_schedule(&queue->backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 goto enqueue;
1909 }
1910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 __get_cpu_var(netdev_rx_stat).dropped++;
1912 local_irq_restore(flags);
1913
1914 kfree_skb(skb);
1915 return NET_RX_DROP;
1916}
1917
1918int netif_rx_ni(struct sk_buff *skb)
1919{
1920 int err;
1921
1922 preempt_disable();
1923 err = netif_rx(skb);
1924 if (local_softirq_pending())
1925 do_softirq();
1926 preempt_enable();
1927
1928 return err;
1929}
1930
1931EXPORT_SYMBOL(netif_rx_ni);
1932
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001933static inline struct net_device *skb_bond(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 struct net_device *dev = skb->dev;
1936
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001937 if (dev->master) {
David S. Miller7ea49ed2006-08-14 17:08:36 -07001938 if (skb_bond_should_drop(skb)) {
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001939 kfree_skb(skb);
1940 return NULL;
1941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 skb->dev = dev->master;
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001943 }
David S. Millerf2ccd8f2005-08-09 19:34:12 -07001944
1945 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949static void net_tx_action(struct softirq_action *h)
1950{
1951 struct softnet_data *sd = &__get_cpu_var(softnet_data);
1952
1953 if (sd->completion_queue) {
1954 struct sk_buff *clist;
1955
1956 local_irq_disable();
1957 clist = sd->completion_queue;
1958 sd->completion_queue = NULL;
1959 local_irq_enable();
1960
1961 while (clist) {
1962 struct sk_buff *skb = clist;
1963 clist = clist->next;
1964
1965 BUG_TRAP(!atomic_read(&skb->users));
1966 __kfree_skb(skb);
1967 }
1968 }
1969
1970 if (sd->output_queue) {
David S. Miller37437bb2008-07-16 02:15:04 -07001971 struct Qdisc *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 local_irq_disable();
1974 head = sd->output_queue;
1975 sd->output_queue = NULL;
1976 local_irq_enable();
1977
1978 while (head) {
David S. Miller37437bb2008-07-16 02:15:04 -07001979 struct Qdisc *q = head;
1980 spinlock_t *root_lock;
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 head = head->next_sched;
1983
1984 smp_mb__before_clear_bit();
David S. Miller37437bb2008-07-16 02:15:04 -07001985 clear_bit(__QDISC_STATE_SCHED, &q->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
David S. Miller37437bb2008-07-16 02:15:04 -07001987 root_lock = qdisc_root_lock(q);
1988 if (spin_trylock(root_lock)) {
1989 qdisc_run(q);
1990 spin_unlock(root_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 } else {
David S. Miller37437bb2008-07-16 02:15:04 -07001992 __netif_schedule(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
1994 }
1995 }
1996}
1997
Stephen Hemminger6f05f622007-03-08 20:46:03 -08001998static inline int deliver_skb(struct sk_buff *skb,
1999 struct packet_type *pt_prev,
2000 struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
2002 atomic_inc(&skb->users);
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002003 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
2005
2006#if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
Stephen Hemminger6229e362007-03-21 13:38:47 -07002007/* These hooks defined here for ATM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008struct net_bridge;
2009struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
2010 unsigned char *addr);
Stephen Hemminger6229e362007-03-21 13:38:47 -07002011void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent) __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Stephen Hemminger6229e362007-03-21 13:38:47 -07002013/*
2014 * If bridge module is loaded call bridging hook.
2015 * returns NULL if packet was consumed.
2016 */
2017struct sk_buff *(*br_handle_frame_hook)(struct net_bridge_port *p,
2018 struct sk_buff *skb) __read_mostly;
2019static inline struct sk_buff *handle_bridge(struct sk_buff *skb,
2020 struct packet_type **pt_prev, int *ret,
2021 struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
2023 struct net_bridge_port *port;
2024
Stephen Hemminger6229e362007-03-21 13:38:47 -07002025 if (skb->pkt_type == PACKET_LOOPBACK ||
2026 (port = rcu_dereference(skb->dev->br_port)) == NULL)
2027 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 if (*pt_prev) {
Stephen Hemminger6229e362007-03-21 13:38:47 -07002030 *ret = deliver_skb(skb, *pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 *pt_prev = NULL;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002032 }
2033
Stephen Hemminger6229e362007-03-21 13:38:47 -07002034 return br_handle_frame_hook(port, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036#else
Stephen Hemminger6229e362007-03-21 13:38:47 -07002037#define handle_bridge(skb, pt_prev, ret, orig_dev) (skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038#endif
2039
Patrick McHardyb863ceb2007-07-14 18:55:06 -07002040#if defined(CONFIG_MACVLAN) || defined(CONFIG_MACVLAN_MODULE)
2041struct sk_buff *(*macvlan_handle_frame_hook)(struct sk_buff *skb) __read_mostly;
2042EXPORT_SYMBOL_GPL(macvlan_handle_frame_hook);
2043
2044static inline struct sk_buff *handle_macvlan(struct sk_buff *skb,
2045 struct packet_type **pt_prev,
2046 int *ret,
2047 struct net_device *orig_dev)
2048{
2049 if (skb->dev->macvlan_port == NULL)
2050 return skb;
2051
2052 if (*pt_prev) {
2053 *ret = deliver_skb(skb, *pt_prev, orig_dev);
2054 *pt_prev = NULL;
2055 }
2056 return macvlan_handle_frame_hook(skb);
2057}
2058#else
2059#define handle_macvlan(skb, pt_prev, ret, orig_dev) (skb)
2060#endif
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062#ifdef CONFIG_NET_CLS_ACT
2063/* TODO: Maybe we should just force sch_ingress to be compiled in
2064 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
2065 * a compare and 2 stores extra right now if we dont have it on
2066 * but have CONFIG_NET_CLS_ACT
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002067 * NOTE: This doesnt stop any functionality; if you dont have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 * the ingress scheduler, you just cant add policies on ingress.
2069 *
2070 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002071static int ing_filter(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 struct net_device *dev = skb->dev;
Herbert Xuf697c3e2007-10-14 00:38:47 -07002074 u32 ttl = G_TC_RTTL(skb->tc_verd);
David S. Miller555353c2008-07-08 17:33:13 -07002075 struct netdev_queue *rxq;
2076 int result = TC_ACT_OK;
2077 struct Qdisc *q;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002078
Herbert Xuf697c3e2007-10-14 00:38:47 -07002079 if (MAX_RED_LOOP < ttl++) {
2080 printk(KERN_WARNING
2081 "Redir loop detected Dropping packet (%d->%d)\n",
2082 skb->iif, dev->ifindex);
2083 return TC_ACT_SHOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
2085
Herbert Xuf697c3e2007-10-14 00:38:47 -07002086 skb->tc_verd = SET_TC_RTTL(skb->tc_verd, ttl);
2087 skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_INGRESS);
2088
David S. Miller555353c2008-07-08 17:33:13 -07002089 rxq = &dev->rx_queue;
2090
David S. Miller83874002008-07-17 00:53:03 -07002091 q = rxq->qdisc;
2092 if (q) {
2093 spin_lock(qdisc_lock(q));
Jussi Kivilinna5f861732008-07-20 00:08:04 -07002094 result = qdisc_enqueue_root(skb, q);
David S. Miller83874002008-07-17 00:53:03 -07002095 spin_unlock(qdisc_lock(q));
2096 }
Herbert Xuf697c3e2007-10-14 00:38:47 -07002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 return result;
2099}
Herbert Xuf697c3e2007-10-14 00:38:47 -07002100
2101static inline struct sk_buff *handle_ing(struct sk_buff *skb,
2102 struct packet_type **pt_prev,
2103 int *ret, struct net_device *orig_dev)
2104{
David S. Miller816f3252008-07-08 22:49:00 -07002105 if (!skb->dev->rx_queue.qdisc)
Herbert Xuf697c3e2007-10-14 00:38:47 -07002106 goto out;
2107
2108 if (*pt_prev) {
2109 *ret = deliver_skb(skb, *pt_prev, orig_dev);
2110 *pt_prev = NULL;
2111 } else {
2112 /* Huh? Why does turning on AF_PACKET affect this? */
2113 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
2114 }
2115
2116 switch (ing_filter(skb)) {
2117 case TC_ACT_SHOT:
2118 case TC_ACT_STOLEN:
2119 kfree_skb(skb);
2120 return NULL;
2121 }
2122
2123out:
2124 skb->tc_verd = 0;
2125 return skb;
2126}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127#endif
2128
Patrick McHardybc1d0412008-07-14 22:49:30 -07002129/*
2130 * netif_nit_deliver - deliver received packets to network taps
2131 * @skb: buffer
2132 *
2133 * This function is used to deliver incoming packets to network
2134 * taps. It should be used when the normal netif_receive_skb path
2135 * is bypassed, for example because of VLAN acceleration.
2136 */
2137void netif_nit_deliver(struct sk_buff *skb)
2138{
2139 struct packet_type *ptype;
2140
2141 if (list_empty(&ptype_all))
2142 return;
2143
2144 skb_reset_network_header(skb);
2145 skb_reset_transport_header(skb);
2146 skb->mac_len = skb->network_header - skb->mac_header;
2147
2148 rcu_read_lock();
2149 list_for_each_entry_rcu(ptype, &ptype_all, list) {
2150 if (!ptype->dev || ptype->dev == skb->dev)
2151 deliver_skb(skb, ptype, skb->dev);
2152 }
2153 rcu_read_unlock();
2154}
2155
Stephen Hemminger3b582cc2007-11-01 02:21:47 -07002156/**
2157 * netif_receive_skb - process receive buffer from network
2158 * @skb: buffer to process
2159 *
2160 * netif_receive_skb() is the main receive data processing function.
2161 * It always succeeds. The buffer may be dropped during processing
2162 * for congestion control or by the protocol layers.
2163 *
2164 * This function may only be called from softirq context and interrupts
2165 * should be enabled.
2166 *
2167 * Return values (usually ignored):
2168 * NET_RX_SUCCESS: no congestion
2169 * NET_RX_DROP: packet was dropped
2170 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171int netif_receive_skb(struct sk_buff *skb)
2172{
2173 struct packet_type *ptype, *pt_prev;
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002174 struct net_device *orig_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 int ret = NET_RX_DROP;
Al Viro252e33462006-11-14 20:48:11 -08002176 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 /* if we've gotten here through NAPI, check netpoll */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002179 if (netpoll_receive_skb(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return NET_RX_DROP;
2181
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07002182 if (!skb->tstamp.tv64)
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002183 net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Patrick McHardyc01003c2007-03-29 11:46:52 -07002185 if (!skb->iif)
2186 skb->iif = skb->dev->ifindex;
David S. Miller86e65da2005-08-09 19:36:29 -07002187
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002188 orig_dev = skb_bond(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002190 if (!orig_dev)
2191 return NET_RX_DROP;
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 __get_cpu_var(netdev_rx_stat).total++;
2194
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07002195 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03002196 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002197 skb->mac_len = skb->network_header - skb->mac_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 pt_prev = NULL;
2200
2201 rcu_read_lock();
2202
Eric W. Biedermanb9f75f42008-06-20 22:16:51 -07002203 /* Don't receive packets in an exiting network namespace */
2204 if (!net_alive(dev_net(skb->dev)))
2205 goto out;
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207#ifdef CONFIG_NET_CLS_ACT
2208 if (skb->tc_verd & TC_NCLS) {
2209 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
2210 goto ncls;
2211 }
2212#endif
2213
2214 list_for_each_entry_rcu(ptype, &ptype_all, list) {
2215 if (!ptype->dev || ptype->dev == skb->dev) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002216 if (pt_prev)
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002217 ret = deliver_skb(skb, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 pt_prev = ptype;
2219 }
2220 }
2221
2222#ifdef CONFIG_NET_CLS_ACT
Herbert Xuf697c3e2007-10-14 00:38:47 -07002223 skb = handle_ing(skb, &pt_prev, &ret, orig_dev);
2224 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226ncls:
2227#endif
2228
Stephen Hemminger6229e362007-03-21 13:38:47 -07002229 skb = handle_bridge(skb, &pt_prev, &ret, orig_dev);
2230 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 goto out;
Patrick McHardyb863ceb2007-07-14 18:55:06 -07002232 skb = handle_macvlan(skb, &pt_prev, &ret, orig_dev);
2233 if (!skb)
2234 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 type = skb->protocol;
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08002237 list_for_each_entry_rcu(ptype,
2238 &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (ptype->type == type &&
2240 (!ptype->dev || ptype->dev == skb->dev)) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002241 if (pt_prev)
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002242 ret = deliver_skb(skb, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 pt_prev = ptype;
2244 }
2245 }
2246
2247 if (pt_prev) {
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002248 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 } else {
2250 kfree_skb(skb);
2251 /* Jamal, now you will not able to escape explaining
2252 * me how you were going to use this. :-)
2253 */
2254 ret = NET_RX_DROP;
2255 }
2256
2257out:
2258 rcu_read_unlock();
2259 return ret;
2260}
2261
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002262static int process_backlog(struct napi_struct *napi, int quota)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
2264 int work = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 struct softnet_data *queue = &__get_cpu_var(softnet_data);
2266 unsigned long start_time = jiffies;
2267
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002268 napi->weight = weight_p;
2269 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 struct sk_buff *skb;
2271 struct net_device *dev;
2272
2273 local_irq_disable();
2274 skb = __skb_dequeue(&queue->input_pkt_queue);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002275 if (!skb) {
2276 __napi_complete(napi);
2277 local_irq_enable();
2278 break;
2279 }
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 local_irq_enable();
2282
2283 dev = skb->dev;
2284
2285 netif_receive_skb(skb);
2286
2287 dev_put(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002288 } while (++work < quota && jiffies == start_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002290 return work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002293/**
2294 * __napi_schedule - schedule for receive
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07002295 * @n: entry to schedule
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002296 *
2297 * The entry's receive function will be scheduled to run
2298 */
Harvey Harrisonb5606c22008-02-13 15:03:16 -08002299void __napi_schedule(struct napi_struct *n)
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002300{
2301 unsigned long flags;
2302
2303 local_irq_save(flags);
2304 list_add_tail(&n->poll_list, &__get_cpu_var(softnet_data).poll_list);
2305 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2306 local_irq_restore(flags);
2307}
2308EXPORT_SYMBOL(__napi_schedule);
2309
2310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311static void net_rx_action(struct softirq_action *h)
2312{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002313 struct list_head *list = &__get_cpu_var(softnet_data).poll_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 unsigned long start_time = jiffies;
Stephen Hemminger51b0bde2005-06-23 20:14:40 -07002315 int budget = netdev_budget;
Matt Mackall53fb95d2005-08-11 19:27:43 -07002316 void *have;
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 local_irq_disable();
2319
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002320 while (!list_empty(list)) {
2321 struct napi_struct *n;
2322 int work, weight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002324 /* If softirq window is exhuasted then punt.
2325 *
2326 * Note that this is a slight policy change from the
2327 * previous NAPI code, which would allow up to 2
2328 * jiffies to pass before breaking out. The test
2329 * used to be "jiffies - start_time > 1".
2330 */
2331 if (unlikely(budget <= 0 || jiffies != start_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 goto softnet_break;
2333
2334 local_irq_enable();
2335
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002336 /* Even though interrupts have been re-enabled, this
2337 * access is safe because interrupts can only add new
2338 * entries to the tail of this list, and only ->poll()
2339 * calls can remove this head entry from the list.
2340 */
2341 n = list_entry(list->next, struct napi_struct, poll_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002343 have = netpoll_poll_lock(n);
2344
2345 weight = n->weight;
2346
David S. Miller0a7606c2007-10-29 21:28:47 -07002347 /* This NAPI_STATE_SCHED test is for avoiding a race
2348 * with netpoll's poll_napi(). Only the entity which
2349 * obtains the lock and sees NAPI_STATE_SCHED set will
2350 * actually make the ->poll() call. Therefore we avoid
2351 * accidently calling ->poll() when NAPI is not scheduled.
2352 */
2353 work = 0;
2354 if (test_bit(NAPI_STATE_SCHED, &n->state))
2355 work = n->poll(n, weight);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002356
2357 WARN_ON_ONCE(work > weight);
2358
2359 budget -= work;
2360
2361 local_irq_disable();
2362
2363 /* Drivers must not modify the NAPI state if they
2364 * consume the entire weight. In such cases this code
2365 * still "owns" the NAPI instance and therefore can
2366 * move the instance around on the list at-will.
2367 */
David S. Millerfed17f32008-01-07 21:00:40 -08002368 if (unlikely(work == weight)) {
2369 if (unlikely(napi_disable_pending(n)))
2370 __napi_complete(n);
2371 else
2372 list_move_tail(&n->poll_list, list);
2373 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002374
2375 netpoll_poll_unlock(have);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
2377out:
Shannon Nelson515e06c2007-06-23 23:09:23 -07002378 local_irq_enable();
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002379
Chris Leechdb217332006-06-17 21:24:58 -07002380#ifdef CONFIG_NET_DMA
2381 /*
2382 * There may not be any more sk_buffs coming right now, so push
2383 * any pending DMA copies to hardware
2384 */
Dan Williamsd379b012007-07-09 11:56:42 -07002385 if (!cpus_empty(net_dma.channel_mask)) {
2386 int chan_idx;
2387 for_each_cpu_mask(chan_idx, net_dma.channel_mask) {
2388 struct dma_chan *chan = net_dma.channels[chan_idx];
2389 if (chan)
2390 dma_async_memcpy_issue_pending(chan);
2391 }
Chris Leechdb217332006-06-17 21:24:58 -07002392 }
2393#endif
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 return;
2396
2397softnet_break:
2398 __get_cpu_var(netdev_rx_stat).time_squeeze++;
2399 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2400 goto out;
2401}
2402
2403static gifconf_func_t * gifconf_list [NPROTO];
2404
2405/**
2406 * register_gifconf - register a SIOCGIF handler
2407 * @family: Address family
2408 * @gifconf: Function handler
2409 *
2410 * Register protocol dependent address dumping routines. The handler
2411 * that is passed must not be freed or reused until it has been replaced
2412 * by another handler.
2413 */
2414int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
2415{
2416 if (family >= NPROTO)
2417 return -EINVAL;
2418 gifconf_list[family] = gifconf;
2419 return 0;
2420}
2421
2422
2423/*
2424 * Map an interface index to its name (SIOCGIFNAME)
2425 */
2426
2427/*
2428 * We need this ioctl for efficient implementation of the
2429 * if_indextoname() function required by the IPv6 API. Without
2430 * it, we would have to search all the interfaces to find a
2431 * match. --pb
2432 */
2433
Eric W. Biederman881d9662007-09-17 11:56:21 -07002434static int dev_ifname(struct net *net, struct ifreq __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
2436 struct net_device *dev;
2437 struct ifreq ifr;
2438
2439 /*
2440 * Fetch the caller's info block.
2441 */
2442
2443 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2444 return -EFAULT;
2445
2446 read_lock(&dev_base_lock);
Eric W. Biederman881d9662007-09-17 11:56:21 -07002447 dev = __dev_get_by_index(net, ifr.ifr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (!dev) {
2449 read_unlock(&dev_base_lock);
2450 return -ENODEV;
2451 }
2452
2453 strcpy(ifr.ifr_name, dev->name);
2454 read_unlock(&dev_base_lock);
2455
2456 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2457 return -EFAULT;
2458 return 0;
2459}
2460
2461/*
2462 * Perform a SIOCGIFCONF call. This structure will change
2463 * size eventually, and there is nothing I can do about it.
2464 * Thus we will need a 'compatibility mode'.
2465 */
2466
Eric W. Biederman881d9662007-09-17 11:56:21 -07002467static int dev_ifconf(struct net *net, char __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
2469 struct ifconf ifc;
2470 struct net_device *dev;
2471 char __user *pos;
2472 int len;
2473 int total;
2474 int i;
2475
2476 /*
2477 * Fetch the caller's info block.
2478 */
2479
2480 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
2481 return -EFAULT;
2482
2483 pos = ifc.ifc_buf;
2484 len = ifc.ifc_len;
2485
2486 /*
2487 * Loop over the interfaces, and write an info block for each.
2488 */
2489
2490 total = 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -07002491 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 for (i = 0; i < NPROTO; i++) {
2493 if (gifconf_list[i]) {
2494 int done;
2495 if (!pos)
2496 done = gifconf_list[i](dev, NULL, 0);
2497 else
2498 done = gifconf_list[i](dev, pos + total,
2499 len - total);
2500 if (done < 0)
2501 return -EFAULT;
2502 total += done;
2503 }
2504 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
2507 /*
2508 * All done. Write the updated control block back to the caller.
2509 */
2510 ifc.ifc_len = total;
2511
2512 /*
2513 * Both BSD and Solaris return 0 here, so we do too.
2514 */
2515 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
2516}
2517
2518#ifdef CONFIG_PROC_FS
2519/*
2520 * This is invoked by the /proc filesystem handler to display a device
2521 * in detail.
2522 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523void *dev_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002524 __acquires(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
Denis V. Luneve372c412007-11-19 22:31:54 -08002526 struct net *net = seq_file_net(seq);
Pavel Emelianov7562f872007-05-03 15:13:45 -07002527 loff_t off;
2528 struct net_device *dev;
2529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 read_lock(&dev_base_lock);
Pavel Emelianov7562f872007-05-03 15:13:45 -07002531 if (!*pos)
2532 return SEQ_START_TOKEN;
2533
2534 off = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07002535 for_each_netdev(net, dev)
Pavel Emelianov7562f872007-05-03 15:13:45 -07002536 if (off++ == *pos)
2537 return dev;
2538
2539 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541
2542void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2543{
Denis V. Luneve372c412007-11-19 22:31:54 -08002544 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 ++*pos;
Pavel Emelianov7562f872007-05-03 15:13:45 -07002546 return v == SEQ_START_TOKEN ?
Eric W. Biederman881d9662007-09-17 11:56:21 -07002547 first_net_device(net) : next_net_device((struct net_device *)v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548}
2549
2550void dev_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002551 __releases(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552{
2553 read_unlock(&dev_base_lock);
2554}
2555
2556static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
2557{
Rusty Russellc45d2862007-03-28 14:29:08 -07002558 struct net_device_stats *stats = dev->get_stats(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Rusty Russell5a1b5892007-04-28 21:04:03 -07002560 seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
2561 "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
2562 dev->name, stats->rx_bytes, stats->rx_packets,
2563 stats->rx_errors,
2564 stats->rx_dropped + stats->rx_missed_errors,
2565 stats->rx_fifo_errors,
2566 stats->rx_length_errors + stats->rx_over_errors +
2567 stats->rx_crc_errors + stats->rx_frame_errors,
2568 stats->rx_compressed, stats->multicast,
2569 stats->tx_bytes, stats->tx_packets,
2570 stats->tx_errors, stats->tx_dropped,
2571 stats->tx_fifo_errors, stats->collisions,
2572 stats->tx_carrier_errors +
2573 stats->tx_aborted_errors +
2574 stats->tx_window_errors +
2575 stats->tx_heartbeat_errors,
2576 stats->tx_compressed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
2578
2579/*
2580 * Called from the PROCfs module. This now uses the new arbitrary sized
2581 * /proc/net interface to create /proc/net/dev
2582 */
2583static int dev_seq_show(struct seq_file *seq, void *v)
2584{
2585 if (v == SEQ_START_TOKEN)
2586 seq_puts(seq, "Inter-| Receive "
2587 " | Transmit\n"
2588 " face |bytes packets errs drop fifo frame "
2589 "compressed multicast|bytes packets errs "
2590 "drop fifo colls carrier compressed\n");
2591 else
2592 dev_seq_printf_stats(seq, v);
2593 return 0;
2594}
2595
2596static struct netif_rx_stats *softnet_get_online(loff_t *pos)
2597{
2598 struct netif_rx_stats *rc = NULL;
2599
Mike Travis0c0b0ac2008-05-02 16:43:08 -07002600 while (*pos < nr_cpu_ids)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002601 if (cpu_online(*pos)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 rc = &per_cpu(netdev_rx_stat, *pos);
2603 break;
2604 } else
2605 ++*pos;
2606 return rc;
2607}
2608
2609static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
2610{
2611 return softnet_get_online(pos);
2612}
2613
2614static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2615{
2616 ++*pos;
2617 return softnet_get_online(pos);
2618}
2619
2620static void softnet_seq_stop(struct seq_file *seq, void *v)
2621{
2622}
2623
2624static int softnet_seq_show(struct seq_file *seq, void *v)
2625{
2626 struct netif_rx_stats *s = v;
2627
2628 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
Stephen Hemminger31aa02c2005-06-23 20:12:48 -07002629 s->total, s->dropped, s->time_squeeze, 0,
Stephen Hemmingerc1ebcdb2005-06-23 20:08:59 -07002630 0, 0, 0, 0, /* was fastroute */
2631 s->cpu_collision );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 return 0;
2633}
2634
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002635static const struct seq_operations dev_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 .start = dev_seq_start,
2637 .next = dev_seq_next,
2638 .stop = dev_seq_stop,
2639 .show = dev_seq_show,
2640};
2641
2642static int dev_seq_open(struct inode *inode, struct file *file)
2643{
Denis V. Luneve372c412007-11-19 22:31:54 -08002644 return seq_open_net(inode, file, &dev_seq_ops,
2645 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646}
2647
Arjan van de Ven9a321442007-02-12 00:55:35 -08002648static const struct file_operations dev_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 .owner = THIS_MODULE,
2650 .open = dev_seq_open,
2651 .read = seq_read,
2652 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08002653 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654};
2655
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002656static const struct seq_operations softnet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 .start = softnet_seq_start,
2658 .next = softnet_seq_next,
2659 .stop = softnet_seq_stop,
2660 .show = softnet_seq_show,
2661};
2662
2663static int softnet_seq_open(struct inode *inode, struct file *file)
2664{
2665 return seq_open(file, &softnet_seq_ops);
2666}
2667
Arjan van de Ven9a321442007-02-12 00:55:35 -08002668static const struct file_operations softnet_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 .owner = THIS_MODULE,
2670 .open = softnet_seq_open,
2671 .read = seq_read,
2672 .llseek = seq_lseek,
2673 .release = seq_release,
2674};
2675
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002676static void *ptype_get_idx(loff_t pos)
2677{
2678 struct packet_type *pt = NULL;
2679 loff_t i = 0;
2680 int t;
2681
2682 list_for_each_entry_rcu(pt, &ptype_all, list) {
2683 if (i == pos)
2684 return pt;
2685 ++i;
2686 }
2687
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08002688 for (t = 0; t < PTYPE_HASH_SIZE; t++) {
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002689 list_for_each_entry_rcu(pt, &ptype_base[t], list) {
2690 if (i == pos)
2691 return pt;
2692 ++i;
2693 }
2694 }
2695 return NULL;
2696}
2697
2698static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemminger72348a422008-01-21 02:27:29 -08002699 __acquires(RCU)
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002700{
2701 rcu_read_lock();
2702 return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN;
2703}
2704
2705static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2706{
2707 struct packet_type *pt;
2708 struct list_head *nxt;
2709 int hash;
2710
2711 ++*pos;
2712 if (v == SEQ_START_TOKEN)
2713 return ptype_get_idx(0);
2714
2715 pt = v;
2716 nxt = pt->list.next;
2717 if (pt->type == htons(ETH_P_ALL)) {
2718 if (nxt != &ptype_all)
2719 goto found;
2720 hash = 0;
2721 nxt = ptype_base[0].next;
2722 } else
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08002723 hash = ntohs(pt->type) & PTYPE_HASH_MASK;
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002724
2725 while (nxt == &ptype_base[hash]) {
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08002726 if (++hash >= PTYPE_HASH_SIZE)
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002727 return NULL;
2728 nxt = ptype_base[hash].next;
2729 }
2730found:
2731 return list_entry(nxt, struct packet_type, list);
2732}
2733
2734static void ptype_seq_stop(struct seq_file *seq, void *v)
Stephen Hemminger72348a422008-01-21 02:27:29 -08002735 __releases(RCU)
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002736{
2737 rcu_read_unlock();
2738}
2739
2740static void ptype_seq_decode(struct seq_file *seq, void *sym)
2741{
2742#ifdef CONFIG_KALLSYMS
2743 unsigned long offset = 0, symsize;
2744 const char *symname;
2745 char *modname;
2746 char namebuf[128];
2747
2748 symname = kallsyms_lookup((unsigned long)sym, &symsize, &offset,
2749 &modname, namebuf);
2750
2751 if (symname) {
2752 char *delim = ":";
2753
2754 if (!modname)
2755 modname = delim = "";
2756 seq_printf(seq, "%s%s%s%s+0x%lx", delim, modname, delim,
2757 symname, offset);
2758 return;
2759 }
2760#endif
2761
2762 seq_printf(seq, "[%p]", sym);
2763}
2764
2765static int ptype_seq_show(struct seq_file *seq, void *v)
2766{
2767 struct packet_type *pt = v;
2768
2769 if (v == SEQ_START_TOKEN)
2770 seq_puts(seq, "Type Device Function\n");
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002771 else if (pt->dev == NULL || dev_net(pt->dev) == seq_file_net(seq)) {
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002772 if (pt->type == htons(ETH_P_ALL))
2773 seq_puts(seq, "ALL ");
2774 else
2775 seq_printf(seq, "%04x", ntohs(pt->type));
2776
2777 seq_printf(seq, " %-8s ",
2778 pt->dev ? pt->dev->name : "");
2779 ptype_seq_decode(seq, pt->func);
2780 seq_putc(seq, '\n');
2781 }
2782
2783 return 0;
2784}
2785
2786static const struct seq_operations ptype_seq_ops = {
2787 .start = ptype_seq_start,
2788 .next = ptype_seq_next,
2789 .stop = ptype_seq_stop,
2790 .show = ptype_seq_show,
2791};
2792
2793static int ptype_seq_open(struct inode *inode, struct file *file)
2794{
Pavel Emelyanov2feb27d2008-03-24 14:57:45 -07002795 return seq_open_net(inode, file, &ptype_seq_ops,
2796 sizeof(struct seq_net_private));
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002797}
2798
2799static const struct file_operations ptype_seq_fops = {
2800 .owner = THIS_MODULE,
2801 .open = ptype_seq_open,
2802 .read = seq_read,
2803 .llseek = seq_lseek,
Pavel Emelyanov2feb27d2008-03-24 14:57:45 -07002804 .release = seq_release_net,
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002805};
2806
2807
Pavel Emelyanov46650792007-10-08 20:38:39 -07002808static int __net_init dev_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
2810 int rc = -ENOMEM;
2811
Eric W. Biederman881d9662007-09-17 11:56:21 -07002812 if (!proc_net_fops_create(net, "dev", S_IRUGO, &dev_seq_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 goto out;
Eric W. Biederman881d9662007-09-17 11:56:21 -07002814 if (!proc_net_fops_create(net, "softnet_stat", S_IRUGO, &softnet_seq_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 goto out_dev;
Eric W. Biederman881d9662007-09-17 11:56:21 -07002816 if (!proc_net_fops_create(net, "ptype", S_IRUGO, &ptype_seq_fops))
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02002817 goto out_softnet;
Stephen Hemminger0e1256f2007-03-12 14:35:37 -07002818
Eric W. Biederman881d9662007-09-17 11:56:21 -07002819 if (wext_proc_init(net))
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02002820 goto out_ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 rc = 0;
2822out:
2823 return rc;
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02002824out_ptype:
Eric W. Biederman881d9662007-09-17 11:56:21 -07002825 proc_net_remove(net, "ptype");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826out_softnet:
Eric W. Biederman881d9662007-09-17 11:56:21 -07002827 proc_net_remove(net, "softnet_stat");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828out_dev:
Eric W. Biederman881d9662007-09-17 11:56:21 -07002829 proc_net_remove(net, "dev");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 goto out;
2831}
Eric W. Biederman881d9662007-09-17 11:56:21 -07002832
Pavel Emelyanov46650792007-10-08 20:38:39 -07002833static void __net_exit dev_proc_net_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002834{
2835 wext_proc_exit(net);
2836
2837 proc_net_remove(net, "ptype");
2838 proc_net_remove(net, "softnet_stat");
2839 proc_net_remove(net, "dev");
2840}
2841
Denis V. Lunev022cbae2007-11-13 03:23:50 -08002842static struct pernet_operations __net_initdata dev_proc_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -07002843 .init = dev_proc_net_init,
2844 .exit = dev_proc_net_exit,
2845};
2846
2847static int __init dev_proc_init(void)
2848{
2849 return register_pernet_subsys(&dev_proc_ops);
2850}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851#else
2852#define dev_proc_init() 0
2853#endif /* CONFIG_PROC_FS */
2854
2855
2856/**
2857 * netdev_set_master - set up master/slave pair
2858 * @slave: slave device
2859 * @master: new master device
2860 *
2861 * Changes the master device of the slave. Pass %NULL to break the
2862 * bonding. The caller must hold the RTNL semaphore. On a failure
2863 * a negative errno code is returned. On success the reference counts
2864 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
2865 * function returns zero.
2866 */
2867int netdev_set_master(struct net_device *slave, struct net_device *master)
2868{
2869 struct net_device *old = slave->master;
2870
2871 ASSERT_RTNL();
2872
2873 if (master) {
2874 if (old)
2875 return -EBUSY;
2876 dev_hold(master);
2877 }
2878
2879 slave->master = master;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002880
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 synchronize_net();
2882
2883 if (old)
2884 dev_put(old);
2885
2886 if (master)
2887 slave->flags |= IFF_SLAVE;
2888 else
2889 slave->flags &= ~IFF_SLAVE;
2890
2891 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
2892 return 0;
2893}
2894
Wang Chendad9b332008-06-18 01:48:28 -07002895static int __dev_set_promiscuity(struct net_device *dev, int inc)
Patrick McHardy4417da62007-06-27 01:28:10 -07002896{
2897 unsigned short old_flags = dev->flags;
2898
Patrick McHardy24023452007-07-14 18:51:31 -07002899 ASSERT_RTNL();
2900
Wang Chendad9b332008-06-18 01:48:28 -07002901 dev->flags |= IFF_PROMISC;
2902 dev->promiscuity += inc;
2903 if (dev->promiscuity == 0) {
2904 /*
2905 * Avoid overflow.
2906 * If inc causes overflow, untouch promisc and return error.
2907 */
2908 if (inc < 0)
2909 dev->flags &= ~IFF_PROMISC;
2910 else {
2911 dev->promiscuity -= inc;
2912 printk(KERN_WARNING "%s: promiscuity touches roof, "
2913 "set promiscuity failed, promiscuity feature "
2914 "of device might be broken.\n", dev->name);
2915 return -EOVERFLOW;
2916 }
2917 }
Patrick McHardy4417da62007-06-27 01:28:10 -07002918 if (dev->flags != old_flags) {
2919 printk(KERN_INFO "device %s %s promiscuous mode\n",
2920 dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
2921 "left");
Klaus Heinrich Kiwi7759db82008-01-23 22:57:45 -05002922 if (audit_enabled)
2923 audit_log(current->audit_context, GFP_ATOMIC,
2924 AUDIT_ANOM_PROMISCUOUS,
2925 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
2926 dev->name, (dev->flags & IFF_PROMISC),
2927 (old_flags & IFF_PROMISC),
2928 audit_get_loginuid(current),
2929 current->uid, current->gid,
2930 audit_get_sessionid(current));
Patrick McHardy24023452007-07-14 18:51:31 -07002931
2932 if (dev->change_rx_flags)
2933 dev->change_rx_flags(dev, IFF_PROMISC);
Patrick McHardy4417da62007-06-27 01:28:10 -07002934 }
Wang Chendad9b332008-06-18 01:48:28 -07002935 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07002936}
2937
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938/**
2939 * dev_set_promiscuity - update promiscuity count on a device
2940 * @dev: device
2941 * @inc: modifier
2942 *
Stephen Hemminger3041a062006-05-26 13:25:24 -07002943 * Add or remove promiscuity from a device. While the count in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 * remains above zero the interface remains promiscuous. Once it hits zero
2945 * the device reverts back to normal filtering operation. A negative inc
2946 * value is used to drop promiscuity on the device.
Wang Chendad9b332008-06-18 01:48:28 -07002947 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 */
Wang Chendad9b332008-06-18 01:48:28 -07002949int dev_set_promiscuity(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950{
2951 unsigned short old_flags = dev->flags;
Wang Chendad9b332008-06-18 01:48:28 -07002952 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Wang Chendad9b332008-06-18 01:48:28 -07002954 err = __dev_set_promiscuity(dev, inc);
Patrick McHardy4b5a6982008-07-06 15:49:08 -07002955 if (err < 0)
Wang Chendad9b332008-06-18 01:48:28 -07002956 return err;
Patrick McHardy4417da62007-06-27 01:28:10 -07002957 if (dev->flags != old_flags)
2958 dev_set_rx_mode(dev);
Wang Chendad9b332008-06-18 01:48:28 -07002959 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960}
2961
2962/**
2963 * dev_set_allmulti - update allmulti count on a device
2964 * @dev: device
2965 * @inc: modifier
2966 *
2967 * Add or remove reception of all multicast frames to a device. While the
2968 * count in the device remains above zero the interface remains listening
2969 * to all interfaces. Once it hits zero the device reverts back to normal
2970 * filtering operation. A negative @inc value is used to drop the counter
2971 * when releasing a resource needing all multicasts.
Wang Chendad9b332008-06-18 01:48:28 -07002972 * Return 0 if successful or a negative errno code on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 */
2974
Wang Chendad9b332008-06-18 01:48:28 -07002975int dev_set_allmulti(struct net_device *dev, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
2977 unsigned short old_flags = dev->flags;
2978
Patrick McHardy24023452007-07-14 18:51:31 -07002979 ASSERT_RTNL();
2980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 dev->flags |= IFF_ALLMULTI;
Wang Chendad9b332008-06-18 01:48:28 -07002982 dev->allmulti += inc;
2983 if (dev->allmulti == 0) {
2984 /*
2985 * Avoid overflow.
2986 * If inc causes overflow, untouch allmulti and return error.
2987 */
2988 if (inc < 0)
2989 dev->flags &= ~IFF_ALLMULTI;
2990 else {
2991 dev->allmulti -= inc;
2992 printk(KERN_WARNING "%s: allmulti touches roof, "
2993 "set allmulti failed, allmulti feature of "
2994 "device might be broken.\n", dev->name);
2995 return -EOVERFLOW;
2996 }
2997 }
Patrick McHardy24023452007-07-14 18:51:31 -07002998 if (dev->flags ^ old_flags) {
2999 if (dev->change_rx_flags)
3000 dev->change_rx_flags(dev, IFF_ALLMULTI);
Patrick McHardy4417da62007-06-27 01:28:10 -07003001 dev_set_rx_mode(dev);
Patrick McHardy24023452007-07-14 18:51:31 -07003002 }
Wang Chendad9b332008-06-18 01:48:28 -07003003 return 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07003004}
3005
3006/*
3007 * Upload unicast and multicast address lists to device and
3008 * configure RX filtering. When the device doesn't support unicast
Joe Perches53ccaae2007-12-20 14:02:06 -08003009 * filtering it is put in promiscuous mode while unicast addresses
Patrick McHardy4417da62007-06-27 01:28:10 -07003010 * are present.
3011 */
3012void __dev_set_rx_mode(struct net_device *dev)
3013{
3014 /* dev_open will call this function so the list will stay sane. */
3015 if (!(dev->flags&IFF_UP))
3016 return;
3017
3018 if (!netif_device_present(dev))
YOSHIFUJI Hideaki40b77c92007-07-19 10:43:23 +09003019 return;
Patrick McHardy4417da62007-06-27 01:28:10 -07003020
3021 if (dev->set_rx_mode)
3022 dev->set_rx_mode(dev);
3023 else {
3024 /* Unicast addresses changes may only happen under the rtnl,
3025 * therefore calling __dev_set_promiscuity here is safe.
3026 */
3027 if (dev->uc_count > 0 && !dev->uc_promisc) {
3028 __dev_set_promiscuity(dev, 1);
3029 dev->uc_promisc = 1;
3030 } else if (dev->uc_count == 0 && dev->uc_promisc) {
3031 __dev_set_promiscuity(dev, -1);
3032 dev->uc_promisc = 0;
3033 }
3034
3035 if (dev->set_multicast_list)
3036 dev->set_multicast_list(dev);
3037 }
3038}
3039
3040void dev_set_rx_mode(struct net_device *dev)
3041{
David S. Millerb9e40852008-07-15 00:15:08 -07003042 netif_addr_lock_bh(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07003043 __dev_set_rx_mode(dev);
David S. Millerb9e40852008-07-15 00:15:08 -07003044 netif_addr_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
Patrick McHardy61cbc2f2007-06-30 13:35:52 -07003047int __dev_addr_delete(struct dev_addr_list **list, int *count,
3048 void *addr, int alen, int glbl)
Patrick McHardybf742482007-06-27 01:26:19 -07003049{
3050 struct dev_addr_list *da;
3051
3052 for (; (da = *list) != NULL; list = &da->next) {
3053 if (memcmp(da->da_addr, addr, da->da_addrlen) == 0 &&
3054 alen == da->da_addrlen) {
3055 if (glbl) {
3056 int old_glbl = da->da_gusers;
3057 da->da_gusers = 0;
3058 if (old_glbl == 0)
3059 break;
3060 }
3061 if (--da->da_users)
3062 return 0;
3063
3064 *list = da->next;
3065 kfree(da);
Patrick McHardy61cbc2f2007-06-30 13:35:52 -07003066 (*count)--;
Patrick McHardybf742482007-06-27 01:26:19 -07003067 return 0;
3068 }
3069 }
3070 return -ENOENT;
3071}
3072
Patrick McHardy61cbc2f2007-06-30 13:35:52 -07003073int __dev_addr_add(struct dev_addr_list **list, int *count,
3074 void *addr, int alen, int glbl)
Patrick McHardybf742482007-06-27 01:26:19 -07003075{
3076 struct dev_addr_list *da;
3077
3078 for (da = *list; da != NULL; da = da->next) {
3079 if (memcmp(da->da_addr, addr, da->da_addrlen) == 0 &&
3080 da->da_addrlen == alen) {
3081 if (glbl) {
3082 int old_glbl = da->da_gusers;
3083 da->da_gusers = 1;
3084 if (old_glbl)
3085 return 0;
3086 }
3087 da->da_users++;
3088 return 0;
3089 }
3090 }
3091
Jorge Boncompte [DTI2]12aa3432008-02-19 14:17:04 -08003092 da = kzalloc(sizeof(*da), GFP_ATOMIC);
Patrick McHardybf742482007-06-27 01:26:19 -07003093 if (da == NULL)
3094 return -ENOMEM;
3095 memcpy(da->da_addr, addr, alen);
3096 da->da_addrlen = alen;
3097 da->da_users = 1;
3098 da->da_gusers = glbl ? 1 : 0;
3099 da->next = *list;
3100 *list = da;
Patrick McHardy61cbc2f2007-06-30 13:35:52 -07003101 (*count)++;
Patrick McHardybf742482007-06-27 01:26:19 -07003102 return 0;
3103}
3104
Patrick McHardy4417da62007-06-27 01:28:10 -07003105/**
3106 * dev_unicast_delete - Release secondary unicast address.
3107 * @dev: device
Randy Dunlap0ed72ec2007-07-26 00:03:29 -07003108 * @addr: address to delete
3109 * @alen: length of @addr
Patrick McHardy4417da62007-06-27 01:28:10 -07003110 *
3111 * Release reference to a secondary unicast address and remove it
Randy Dunlap0ed72ec2007-07-26 00:03:29 -07003112 * from the device if the reference count drops to zero.
Patrick McHardy4417da62007-06-27 01:28:10 -07003113 *
3114 * The caller must hold the rtnl_mutex.
3115 */
3116int dev_unicast_delete(struct net_device *dev, void *addr, int alen)
3117{
3118 int err;
3119
3120 ASSERT_RTNL();
3121
David S. Millerb9e40852008-07-15 00:15:08 -07003122 netif_addr_lock_bh(dev);
Patrick McHardy61cbc2f2007-06-30 13:35:52 -07003123 err = __dev_addr_delete(&dev->uc_list, &dev->uc_count, addr, alen, 0);
3124 if (!err)
Patrick McHardy4417da62007-06-27 01:28:10 -07003125 __dev_set_rx_mode(dev);
David S. Millerb9e40852008-07-15 00:15:08 -07003126 netif_addr_unlock_bh(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07003127 return err;
3128}
3129EXPORT_SYMBOL(dev_unicast_delete);
3130
3131/**
3132 * dev_unicast_add - add a secondary unicast address
3133 * @dev: device
Wang Chen5dbaec52008-06-27 19:35:16 -07003134 * @addr: address to add
Randy Dunlap0ed72ec2007-07-26 00:03:29 -07003135 * @alen: length of @addr
Patrick McHardy4417da62007-06-27 01:28:10 -07003136 *
3137 * Add a secondary unicast address to the device or increase
3138 * the reference count if it already exists.
3139 *
3140 * The caller must hold the rtnl_mutex.
3141 */
3142int dev_unicast_add(struct net_device *dev, void *addr, int alen)
3143{
3144 int err;
3145
3146 ASSERT_RTNL();
3147
David S. Millerb9e40852008-07-15 00:15:08 -07003148 netif_addr_lock_bh(dev);
Patrick McHardy61cbc2f2007-06-30 13:35:52 -07003149 err = __dev_addr_add(&dev->uc_list, &dev->uc_count, addr, alen, 0);
3150 if (!err)
Patrick McHardy4417da62007-06-27 01:28:10 -07003151 __dev_set_rx_mode(dev);
David S. Millerb9e40852008-07-15 00:15:08 -07003152 netif_addr_unlock_bh(dev);
Patrick McHardy4417da62007-06-27 01:28:10 -07003153 return err;
3154}
3155EXPORT_SYMBOL(dev_unicast_add);
3156
Chris Leeche83a2ea2008-01-31 16:53:23 -08003157int __dev_addr_sync(struct dev_addr_list **to, int *to_count,
3158 struct dev_addr_list **from, int *from_count)
3159{
3160 struct dev_addr_list *da, *next;
3161 int err = 0;
3162
3163 da = *from;
3164 while (da != NULL) {
3165 next = da->next;
3166 if (!da->da_synced) {
3167 err = __dev_addr_add(to, to_count,
3168 da->da_addr, da->da_addrlen, 0);
3169 if (err < 0)
3170 break;
3171 da->da_synced = 1;
3172 da->da_users++;
3173 } else if (da->da_users == 1) {
3174 __dev_addr_delete(to, to_count,
3175 da->da_addr, da->da_addrlen, 0);
3176 __dev_addr_delete(from, from_count,
3177 da->da_addr, da->da_addrlen, 0);
3178 }
3179 da = next;
3180 }
3181 return err;
3182}
3183
3184void __dev_addr_unsync(struct dev_addr_list **to, int *to_count,
3185 struct dev_addr_list **from, int *from_count)
3186{
3187 struct dev_addr_list *da, *next;
3188
3189 da = *from;
3190 while (da != NULL) {
3191 next = da->next;
3192 if (da->da_synced) {
3193 __dev_addr_delete(to, to_count,
3194 da->da_addr, da->da_addrlen, 0);
3195 da->da_synced = 0;
3196 __dev_addr_delete(from, from_count,
3197 da->da_addr, da->da_addrlen, 0);
3198 }
3199 da = next;
3200 }
3201}
3202
3203/**
3204 * dev_unicast_sync - Synchronize device's unicast list to another device
3205 * @to: destination device
3206 * @from: source device
3207 *
3208 * Add newly added addresses to the destination device and release
3209 * addresses that have no users left. The source device must be
3210 * locked by netif_tx_lock_bh.
3211 *
3212 * This function is intended to be called from the dev->set_rx_mode
3213 * function of layered software devices.
3214 */
3215int dev_unicast_sync(struct net_device *to, struct net_device *from)
3216{
3217 int err = 0;
3218
David S. Millerb9e40852008-07-15 00:15:08 -07003219 netif_addr_lock_bh(to);
Chris Leeche83a2ea2008-01-31 16:53:23 -08003220 err = __dev_addr_sync(&to->uc_list, &to->uc_count,
3221 &from->uc_list, &from->uc_count);
3222 if (!err)
3223 __dev_set_rx_mode(to);
David S. Millerb9e40852008-07-15 00:15:08 -07003224 netif_addr_unlock_bh(to);
Chris Leeche83a2ea2008-01-31 16:53:23 -08003225 return err;
3226}
3227EXPORT_SYMBOL(dev_unicast_sync);
3228
3229/**
Randy Dunlapbc2cda12008-02-13 15:03:25 -08003230 * dev_unicast_unsync - Remove synchronized addresses from the destination device
Chris Leeche83a2ea2008-01-31 16:53:23 -08003231 * @to: destination device
3232 * @from: source device
3233 *
3234 * Remove all addresses that were added to the destination device by
3235 * dev_unicast_sync(). This function is intended to be called from the
3236 * dev->stop function of layered software devices.
3237 */
3238void dev_unicast_unsync(struct net_device *to, struct net_device *from)
3239{
David S. Millerb9e40852008-07-15 00:15:08 -07003240 netif_addr_lock_bh(from);
David S. Millere308a5d2008-07-15 00:13:44 -07003241 netif_addr_lock(to);
Chris Leeche83a2ea2008-01-31 16:53:23 -08003242
3243 __dev_addr_unsync(&to->uc_list, &to->uc_count,
3244 &from->uc_list, &from->uc_count);
3245 __dev_set_rx_mode(to);
3246
David S. Millere308a5d2008-07-15 00:13:44 -07003247 netif_addr_unlock(to);
David S. Millerb9e40852008-07-15 00:15:08 -07003248 netif_addr_unlock_bh(from);
Chris Leeche83a2ea2008-01-31 16:53:23 -08003249}
3250EXPORT_SYMBOL(dev_unicast_unsync);
3251
Denis Cheng12972622007-07-18 02:12:56 -07003252static void __dev_addr_discard(struct dev_addr_list **list)
3253{
3254 struct dev_addr_list *tmp;
3255
3256 while (*list != NULL) {
3257 tmp = *list;
3258 *list = tmp->next;
3259 if (tmp->da_users > tmp->da_gusers)
3260 printk("__dev_addr_discard: address leakage! "
3261 "da_users=%d\n", tmp->da_users);
3262 kfree(tmp);
3263 }
3264}
3265
Denis Cheng26cc2522007-07-18 02:12:03 -07003266static void dev_addr_discard(struct net_device *dev)
Patrick McHardy4417da62007-06-27 01:28:10 -07003267{
David S. Millerb9e40852008-07-15 00:15:08 -07003268 netif_addr_lock_bh(dev);
Denis Cheng26cc2522007-07-18 02:12:03 -07003269
Patrick McHardy4417da62007-06-27 01:28:10 -07003270 __dev_addr_discard(&dev->uc_list);
3271 dev->uc_count = 0;
Patrick McHardy4417da62007-06-27 01:28:10 -07003272
Denis Cheng456ad752007-07-18 02:10:54 -07003273 __dev_addr_discard(&dev->mc_list);
3274 dev->mc_count = 0;
Denis Cheng26cc2522007-07-18 02:12:03 -07003275
David S. Millerb9e40852008-07-15 00:15:08 -07003276 netif_addr_unlock_bh(dev);
Denis Cheng456ad752007-07-18 02:10:54 -07003277}
3278
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279unsigned dev_get_flags(const struct net_device *dev)
3280{
3281 unsigned flags;
3282
3283 flags = (dev->flags & ~(IFF_PROMISC |
3284 IFF_ALLMULTI |
Stefan Rompfb00055a2006-03-20 17:09:11 -08003285 IFF_RUNNING |
3286 IFF_LOWER_UP |
3287 IFF_DORMANT)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 (dev->gflags & (IFF_PROMISC |
3289 IFF_ALLMULTI));
3290
Stefan Rompfb00055a2006-03-20 17:09:11 -08003291 if (netif_running(dev)) {
3292 if (netif_oper_up(dev))
3293 flags |= IFF_RUNNING;
3294 if (netif_carrier_ok(dev))
3295 flags |= IFF_LOWER_UP;
3296 if (netif_dormant(dev))
3297 flags |= IFF_DORMANT;
3298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
3300 return flags;
3301}
3302
3303int dev_change_flags(struct net_device *dev, unsigned flags)
3304{
Thomas Graf7c355f52007-06-05 16:03:03 -07003305 int ret, changes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 int old_flags = dev->flags;
3307
Patrick McHardy24023452007-07-14 18:51:31 -07003308 ASSERT_RTNL();
3309
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 /*
3311 * Set the flags on our device.
3312 */
3313
3314 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
3315 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
3316 IFF_AUTOMEDIA)) |
3317 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
3318 IFF_ALLMULTI));
3319
3320 /*
3321 * Load in the correct multicast list now the flags have changed.
3322 */
3323
David Woodhouse0e917962008-05-20 14:36:14 -07003324 if (dev->change_rx_flags && (old_flags ^ flags) & IFF_MULTICAST)
Patrick McHardy24023452007-07-14 18:51:31 -07003325 dev->change_rx_flags(dev, IFF_MULTICAST);
3326
Patrick McHardy4417da62007-06-27 01:28:10 -07003327 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
3329 /*
3330 * Have we downed the interface. We handle IFF_UP ourselves
3331 * according to user attempts to set it, rather than blindly
3332 * setting it.
3333 */
3334
3335 ret = 0;
3336 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
3337 ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
3338
3339 if (!ret)
Patrick McHardy4417da62007-06-27 01:28:10 -07003340 dev_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 }
3342
3343 if (dev->flags & IFF_UP &&
3344 ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
3345 IFF_VOLATILE)))
Pavel Emelyanov056925a2007-09-16 15:42:43 -07003346 call_netdevice_notifiers(NETDEV_CHANGE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
3348 if ((flags ^ dev->gflags) & IFF_PROMISC) {
3349 int inc = (flags & IFF_PROMISC) ? +1 : -1;
3350 dev->gflags ^= IFF_PROMISC;
3351 dev_set_promiscuity(dev, inc);
3352 }
3353
3354 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
3355 is important. Some (broken) drivers set IFF_PROMISC, when
3356 IFF_ALLMULTI is requested not asking us and not reporting.
3357 */
3358 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
3359 int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
3360 dev->gflags ^= IFF_ALLMULTI;
3361 dev_set_allmulti(dev, inc);
3362 }
3363
Thomas Graf7c355f52007-06-05 16:03:03 -07003364 /* Exclude state transition flags, already notified */
3365 changes = (old_flags ^ dev->flags) & ~(IFF_UP | IFF_RUNNING);
3366 if (changes)
3367 rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
3369 return ret;
3370}
3371
3372int dev_set_mtu(struct net_device *dev, int new_mtu)
3373{
3374 int err;
3375
3376 if (new_mtu == dev->mtu)
3377 return 0;
3378
3379 /* MTU must be positive. */
3380 if (new_mtu < 0)
3381 return -EINVAL;
3382
3383 if (!netif_device_present(dev))
3384 return -ENODEV;
3385
3386 err = 0;
3387 if (dev->change_mtu)
3388 err = dev->change_mtu(dev, new_mtu);
3389 else
3390 dev->mtu = new_mtu;
3391 if (!err && dev->flags & IFF_UP)
Pavel Emelyanov056925a2007-09-16 15:42:43 -07003392 call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 return err;
3394}
3395
3396int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
3397{
3398 int err;
3399
3400 if (!dev->set_mac_address)
3401 return -EOPNOTSUPP;
3402 if (sa->sa_family != dev->type)
3403 return -EINVAL;
3404 if (!netif_device_present(dev))
3405 return -ENODEV;
3406 err = dev->set_mac_address(dev, sa);
3407 if (!err)
Pavel Emelyanov056925a2007-09-16 15:42:43 -07003408 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 return err;
3410}
3411
3412/*
Jeff Garzik14e3e072007-10-08 00:06:32 -07003413 * Perform the SIOCxIFxxx calls, inside read_lock(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 */
Jeff Garzik14e3e072007-10-08 00:06:32 -07003415static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416{
3417 int err;
Eric W. Biederman881d9662007-09-17 11:56:21 -07003418 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
3420 if (!dev)
3421 return -ENODEV;
3422
3423 switch (cmd) {
3424 case SIOCGIFFLAGS: /* Get interface flags */
3425 ifr->ifr_flags = dev_get_flags(dev);
3426 return 0;
3427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 case SIOCGIFMETRIC: /* Get the metric on the interface
3429 (currently unused) */
3430 ifr->ifr_metric = 0;
3431 return 0;
3432
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 case SIOCGIFMTU: /* Get the MTU of a device */
3434 ifr->ifr_mtu = dev->mtu;
3435 return 0;
3436
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 case SIOCGIFHWADDR:
3438 if (!dev->addr_len)
3439 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
3440 else
3441 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
3442 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
3443 ifr->ifr_hwaddr.sa_family = dev->type;
3444 return 0;
3445
Jeff Garzik14e3e072007-10-08 00:06:32 -07003446 case SIOCGIFSLAVE:
3447 err = -EINVAL;
3448 break;
3449
3450 case SIOCGIFMAP:
3451 ifr->ifr_map.mem_start = dev->mem_start;
3452 ifr->ifr_map.mem_end = dev->mem_end;
3453 ifr->ifr_map.base_addr = dev->base_addr;
3454 ifr->ifr_map.irq = dev->irq;
3455 ifr->ifr_map.dma = dev->dma;
3456 ifr->ifr_map.port = dev->if_port;
3457 return 0;
3458
3459 case SIOCGIFINDEX:
3460 ifr->ifr_ifindex = dev->ifindex;
3461 return 0;
3462
3463 case SIOCGIFTXQLEN:
3464 ifr->ifr_qlen = dev->tx_queue_len;
3465 return 0;
3466
3467 default:
3468 /* dev_ioctl() should ensure this case
3469 * is never reached
3470 */
3471 WARN_ON(1);
3472 err = -EINVAL;
3473 break;
3474
3475 }
3476 return err;
3477}
3478
3479/*
3480 * Perform the SIOCxIFxxx calls, inside rtnl_lock()
3481 */
3482static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
3483{
3484 int err;
3485 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
3486
3487 if (!dev)
3488 return -ENODEV;
3489
3490 switch (cmd) {
3491 case SIOCSIFFLAGS: /* Set interface flags */
3492 return dev_change_flags(dev, ifr->ifr_flags);
3493
3494 case SIOCSIFMETRIC: /* Set the metric on the interface
3495 (currently unused) */
3496 return -EOPNOTSUPP;
3497
3498 case SIOCSIFMTU: /* Set the MTU of a device */
3499 return dev_set_mtu(dev, ifr->ifr_mtu);
3500
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 case SIOCSIFHWADDR:
3502 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
3503
3504 case SIOCSIFHWBROADCAST:
3505 if (ifr->ifr_hwaddr.sa_family != dev->type)
3506 return -EINVAL;
3507 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
3508 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
Pavel Emelyanov056925a2007-09-16 15:42:43 -07003509 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 return 0;
3511
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 case SIOCSIFMAP:
3513 if (dev->set_config) {
3514 if (!netif_device_present(dev))
3515 return -ENODEV;
3516 return dev->set_config(dev, &ifr->ifr_map);
3517 }
3518 return -EOPNOTSUPP;
3519
3520 case SIOCADDMULTI:
Patrick McHardy61ee6bd2008-03-26 02:12:11 -07003521 if ((!dev->set_multicast_list && !dev->set_rx_mode) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
3523 return -EINVAL;
3524 if (!netif_device_present(dev))
3525 return -ENODEV;
3526 return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
3527 dev->addr_len, 1);
3528
3529 case SIOCDELMULTI:
Patrick McHardy61ee6bd2008-03-26 02:12:11 -07003530 if ((!dev->set_multicast_list && !dev->set_rx_mode) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
3532 return -EINVAL;
3533 if (!netif_device_present(dev))
3534 return -ENODEV;
3535 return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
3536 dev->addr_len, 1);
3537
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 case SIOCSIFTXQLEN:
3539 if (ifr->ifr_qlen < 0)
3540 return -EINVAL;
3541 dev->tx_queue_len = ifr->ifr_qlen;
3542 return 0;
3543
3544 case SIOCSIFNAME:
3545 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
3546 return dev_change_name(dev, ifr->ifr_newname);
3547
3548 /*
3549 * Unknown or private ioctl
3550 */
3551
3552 default:
3553 if ((cmd >= SIOCDEVPRIVATE &&
3554 cmd <= SIOCDEVPRIVATE + 15) ||
3555 cmd == SIOCBONDENSLAVE ||
3556 cmd == SIOCBONDRELEASE ||
3557 cmd == SIOCBONDSETHWADDR ||
3558 cmd == SIOCBONDSLAVEINFOQUERY ||
3559 cmd == SIOCBONDINFOQUERY ||
3560 cmd == SIOCBONDCHANGEACTIVE ||
3561 cmd == SIOCGMIIPHY ||
3562 cmd == SIOCGMIIREG ||
3563 cmd == SIOCSMIIREG ||
3564 cmd == SIOCBRADDIF ||
3565 cmd == SIOCBRDELIF ||
3566 cmd == SIOCWANDEV) {
3567 err = -EOPNOTSUPP;
3568 if (dev->do_ioctl) {
3569 if (netif_device_present(dev))
3570 err = dev->do_ioctl(dev, ifr,
3571 cmd);
3572 else
3573 err = -ENODEV;
3574 }
3575 } else
3576 err = -EINVAL;
3577
3578 }
3579 return err;
3580}
3581
3582/*
3583 * This function handles all "interface"-type I/O control requests. The actual
3584 * 'doing' part of this is dev_ifsioc above.
3585 */
3586
3587/**
3588 * dev_ioctl - network device ioctl
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07003589 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 * @cmd: command to issue
3591 * @arg: pointer to a struct ifreq in user space
3592 *
3593 * Issue ioctl functions to devices. This is normally called by the
3594 * user space syscall interfaces but can sometimes be useful for
3595 * other purposes. The return value is the return from the syscall if
3596 * positive or a negative errno code on error.
3597 */
3598
Eric W. Biederman881d9662007-09-17 11:56:21 -07003599int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600{
3601 struct ifreq ifr;
3602 int ret;
3603 char *colon;
3604
3605 /* One special case: SIOCGIFCONF takes ifconf argument
3606 and requires shared lock, because it sleeps writing
3607 to user space.
3608 */
3609
3610 if (cmd == SIOCGIFCONF) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08003611 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -07003612 ret = dev_ifconf(net, (char __user *) arg);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08003613 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 return ret;
3615 }
3616 if (cmd == SIOCGIFNAME)
Eric W. Biederman881d9662007-09-17 11:56:21 -07003617 return dev_ifname(net, (struct ifreq __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
3619 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
3620 return -EFAULT;
3621
3622 ifr.ifr_name[IFNAMSIZ-1] = 0;
3623
3624 colon = strchr(ifr.ifr_name, ':');
3625 if (colon)
3626 *colon = 0;
3627
3628 /*
3629 * See which interface the caller is talking about.
3630 */
3631
3632 switch (cmd) {
3633 /*
3634 * These ioctl calls:
3635 * - can be done by all.
3636 * - atomic and do not require locking.
3637 * - return a value
3638 */
3639 case SIOCGIFFLAGS:
3640 case SIOCGIFMETRIC:
3641 case SIOCGIFMTU:
3642 case SIOCGIFHWADDR:
3643 case SIOCGIFSLAVE:
3644 case SIOCGIFMAP:
3645 case SIOCGIFINDEX:
3646 case SIOCGIFTXQLEN:
Eric W. Biederman881d9662007-09-17 11:56:21 -07003647 dev_load(net, ifr.ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 read_lock(&dev_base_lock);
Jeff Garzik14e3e072007-10-08 00:06:32 -07003649 ret = dev_ifsioc_locked(net, &ifr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 read_unlock(&dev_base_lock);
3651 if (!ret) {
3652 if (colon)
3653 *colon = ':';
3654 if (copy_to_user(arg, &ifr,
3655 sizeof(struct ifreq)))
3656 ret = -EFAULT;
3657 }
3658 return ret;
3659
3660 case SIOCETHTOOL:
Eric W. Biederman881d9662007-09-17 11:56:21 -07003661 dev_load(net, ifr.ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -07003663 ret = dev_ethtool(net, &ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 rtnl_unlock();
3665 if (!ret) {
3666 if (colon)
3667 *colon = ':';
3668 if (copy_to_user(arg, &ifr,
3669 sizeof(struct ifreq)))
3670 ret = -EFAULT;
3671 }
3672 return ret;
3673
3674 /*
3675 * These ioctl calls:
3676 * - require superuser power.
3677 * - require strict serialization.
3678 * - return a value
3679 */
3680 case SIOCGMIIPHY:
3681 case SIOCGMIIREG:
3682 case SIOCSIFNAME:
3683 if (!capable(CAP_NET_ADMIN))
3684 return -EPERM;
Eric W. Biederman881d9662007-09-17 11:56:21 -07003685 dev_load(net, ifr.ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -07003687 ret = dev_ifsioc(net, &ifr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 rtnl_unlock();
3689 if (!ret) {
3690 if (colon)
3691 *colon = ':';
3692 if (copy_to_user(arg, &ifr,
3693 sizeof(struct ifreq)))
3694 ret = -EFAULT;
3695 }
3696 return ret;
3697
3698 /*
3699 * These ioctl calls:
3700 * - require superuser power.
3701 * - require strict serialization.
3702 * - do not return a value
3703 */
3704 case SIOCSIFFLAGS:
3705 case SIOCSIFMETRIC:
3706 case SIOCSIFMTU:
3707 case SIOCSIFMAP:
3708 case SIOCSIFHWADDR:
3709 case SIOCSIFSLAVE:
3710 case SIOCADDMULTI:
3711 case SIOCDELMULTI:
3712 case SIOCSIFHWBROADCAST:
3713 case SIOCSIFTXQLEN:
3714 case SIOCSMIIREG:
3715 case SIOCBONDENSLAVE:
3716 case SIOCBONDRELEASE:
3717 case SIOCBONDSETHWADDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 case SIOCBONDCHANGEACTIVE:
3719 case SIOCBRADDIF:
3720 case SIOCBRDELIF:
3721 if (!capable(CAP_NET_ADMIN))
3722 return -EPERM;
Thomas Grafcabcac02006-01-24 12:46:33 -08003723 /* fall through */
3724 case SIOCBONDSLAVEINFOQUERY:
3725 case SIOCBONDINFOQUERY:
Eric W. Biederman881d9662007-09-17 11:56:21 -07003726 dev_load(net, ifr.ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -07003728 ret = dev_ifsioc(net, &ifr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 rtnl_unlock();
3730 return ret;
3731
3732 case SIOCGIFMEM:
3733 /* Get the per device memory space. We can add this but
3734 * currently do not support it */
3735 case SIOCSIFMEM:
3736 /* Set the per device memory buffer space.
3737 * Not applicable in our case */
3738 case SIOCSIFLINK:
3739 return -EINVAL;
3740
3741 /*
3742 * Unknown or private ioctl.
3743 */
3744 default:
3745 if (cmd == SIOCWANDEV ||
3746 (cmd >= SIOCDEVPRIVATE &&
3747 cmd <= SIOCDEVPRIVATE + 15)) {
Eric W. Biederman881d9662007-09-17 11:56:21 -07003748 dev_load(net, ifr.ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -07003750 ret = dev_ifsioc(net, &ifr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 rtnl_unlock();
3752 if (!ret && copy_to_user(arg, &ifr,
3753 sizeof(struct ifreq)))
3754 ret = -EFAULT;
3755 return ret;
3756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 /* Take care of Wireless Extensions */
Johannes Berg295f4a12007-04-26 20:43:56 -07003758 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
Eric W. Biederman881d9662007-09-17 11:56:21 -07003759 return wext_handle_ioctl(net, &ifr, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 return -EINVAL;
3761 }
3762}
3763
3764
3765/**
3766 * dev_new_index - allocate an ifindex
Randy Dunlapc4ea43c2007-10-12 21:17:49 -07003767 * @net: the applicable net namespace
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 *
3769 * Returns a suitable unique value for a new device interface
3770 * number. The caller must hold the rtnl semaphore or the
3771 * dev_base_lock to be sure it remains unique.
3772 */
Eric W. Biederman881d9662007-09-17 11:56:21 -07003773static int dev_new_index(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774{
3775 static int ifindex;
3776 for (;;) {
3777 if (++ifindex <= 0)
3778 ifindex = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07003779 if (!__dev_get_by_index(net, ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 return ifindex;
3781 }
3782}
3783
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784/* Delayed registration/unregisteration */
3785static DEFINE_SPINLOCK(net_todo_list_lock);
Denis Cheng3b5b34f2007-12-07 00:49:17 -08003786static LIST_HEAD(net_todo_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
Stephen Hemminger6f05f622007-03-08 20:46:03 -08003788static void net_set_todo(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789{
3790 spin_lock(&net_todo_list_lock);
3791 list_add_tail(&dev->todo_list, &net_todo_list);
3792 spin_unlock(&net_todo_list_lock);
3793}
3794
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07003795static void rollback_registered(struct net_device *dev)
3796{
3797 BUG_ON(dev_boot_phase);
3798 ASSERT_RTNL();
3799
3800 /* Some devices call without registering for initialization unwind. */
3801 if (dev->reg_state == NETREG_UNINITIALIZED) {
3802 printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
3803 "was registered\n", dev->name, dev);
3804
3805 WARN_ON(1);
3806 return;
3807 }
3808
3809 BUG_ON(dev->reg_state != NETREG_REGISTERED);
3810
3811 /* If device is running, close it first. */
3812 dev_close(dev);
3813
3814 /* And unlink it from device chain. */
3815 unlist_netdevice(dev);
3816
3817 dev->reg_state = NETREG_UNREGISTERING;
3818
3819 synchronize_net();
3820
3821 /* Shutdown queueing discipline. */
3822 dev_shutdown(dev);
3823
3824
3825 /* Notify protocols, that we are about to destroy
3826 this device. They should clean all the things.
3827 */
3828 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
3829
3830 /*
3831 * Flush the unicast and multicast chains
3832 */
3833 dev_addr_discard(dev);
3834
3835 if (dev->uninit)
3836 dev->uninit(dev);
3837
3838 /* Notifier chain MUST detach us from master device. */
3839 BUG_TRAP(!dev->master);
3840
3841 /* Remove entries from kobject tree */
3842 netdev_unregister_kobject(dev);
3843
3844 synchronize_net();
3845
3846 dev_put(dev);
3847}
3848
David S. Millere8a04642008-07-17 00:34:19 -07003849static void __netdev_init_queue_locks_one(struct net_device *dev,
3850 struct netdev_queue *dev_queue,
3851 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07003852{
3853 spin_lock_init(&dev_queue->_xmit_lock);
3854 netdev_set_lockdep_class(&dev_queue->_xmit_lock, dev->type);
3855 dev_queue->xmit_lock_owner = -1;
3856}
3857
3858static void netdev_init_queue_locks(struct net_device *dev)
3859{
David S. Millere8a04642008-07-17 00:34:19 -07003860 netdev_for_each_tx_queue(dev, __netdev_init_queue_locks_one, NULL);
3861 __netdev_init_queue_locks_one(dev, &dev->rx_queue, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07003862}
3863
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864/**
3865 * register_netdevice - register a network device
3866 * @dev: device to register
3867 *
3868 * Take a completed network device structure and add it to the kernel
3869 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3870 * chain. 0 is returned on success. A negative errno code is returned
3871 * on a failure to set up the device, or if the name is a duplicate.
3872 *
3873 * Callers must hold the rtnl semaphore. You may want
3874 * register_netdev() instead of this.
3875 *
3876 * BUGS:
3877 * The locking appears insufficient to guarantee two parallel registers
3878 * will not get the same name.
3879 */
3880
3881int register_netdevice(struct net_device *dev)
3882{
3883 struct hlist_head *head;
3884 struct hlist_node *p;
3885 int ret;
Eric W. Biederman881d9662007-09-17 11:56:21 -07003886 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887
3888 BUG_ON(dev_boot_phase);
3889 ASSERT_RTNL();
3890
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003891 might_sleep();
3892
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 /* When net_device's are persistent, this will be fatal. */
3894 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003895 BUG_ON(!dev_net(dev));
3896 net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
David S. Millerf1f28aa2008-07-15 00:08:33 -07003898 spin_lock_init(&dev->addr_list_lock);
David S. Millerc773e842008-07-08 23:13:53 -07003899 netdev_init_queue_locks(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 dev->iflink = -1;
3902
3903 /* Init, if this function is available */
3904 if (dev->init) {
3905 ret = dev->init(dev);
3906 if (ret) {
3907 if (ret > 0)
3908 ret = -EIO;
Adrian Bunk90833aa2006-11-13 16:02:22 -08003909 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 }
3911 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003912
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 if (!dev_valid_name(dev->name)) {
3914 ret = -EINVAL;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07003915 goto err_uninit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 }
3917
Eric W. Biederman881d9662007-09-17 11:56:21 -07003918 dev->ifindex = dev_new_index(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 if (dev->iflink == -1)
3920 dev->iflink = dev->ifindex;
3921
3922 /* Check for existence of name */
Eric W. Biederman881d9662007-09-17 11:56:21 -07003923 head = dev_name_hash(net, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 hlist_for_each(p, head) {
3925 struct net_device *d
3926 = hlist_entry(p, struct net_device, name_hlist);
3927 if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
3928 ret = -EEXIST;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07003929 goto err_uninit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
Stephen Hemmingerd212f872007-06-27 00:47:37 -07003933 /* Fix illegal checksum combinations */
3934 if ((dev->features & NETIF_F_HW_CSUM) &&
3935 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
3936 printk(KERN_NOTICE "%s: mixed HW and IP checksum settings.\n",
3937 dev->name);
3938 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
3939 }
3940
3941 if ((dev->features & NETIF_F_NO_CSUM) &&
3942 (dev->features & (NETIF_F_HW_CSUM|NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
3943 printk(KERN_NOTICE "%s: mixed no checksumming and other settings.\n",
3944 dev->name);
3945 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM|NETIF_F_HW_CSUM);
3946 }
3947
3948
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 /* Fix illegal SG+CSUM combinations. */
3950 if ((dev->features & NETIF_F_SG) &&
Herbert Xu8648b302006-06-17 22:06:05 -07003951 !(dev->features & NETIF_F_ALL_CSUM)) {
Stephen Hemminger5a8da022006-07-07 16:54:05 -07003952 printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no checksum feature.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 dev->name);
3954 dev->features &= ~NETIF_F_SG;
3955 }
3956
3957 /* TSO requires that SG is present as well. */
3958 if ((dev->features & NETIF_F_TSO) &&
3959 !(dev->features & NETIF_F_SG)) {
Stephen Hemminger5a8da022006-07-07 16:54:05 -07003960 printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no SG feature.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 dev->name);
3962 dev->features &= ~NETIF_F_TSO;
3963 }
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003964 if (dev->features & NETIF_F_UFO) {
3965 if (!(dev->features & NETIF_F_HW_CSUM)) {
3966 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
3967 "NETIF_F_HW_CSUM feature.\n",
3968 dev->name);
3969 dev->features &= ~NETIF_F_UFO;
3970 }
3971 if (!(dev->features & NETIF_F_SG)) {
3972 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
3973 "NETIF_F_SG feature.\n",
3974 dev->name);
3975 dev->features &= ~NETIF_F_UFO;
3976 }
3977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
Daniel Lezcanoaaf8cdc2008-05-02 17:00:58 -07003979 netdev_initialize_kobject(dev);
Eric W. Biederman8b41d182007-09-26 22:02:53 -07003980 ret = netdev_register_kobject(dev);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003981 if (ret)
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07003982 goto err_uninit;
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07003983 dev->reg_state = NETREG_REGISTERED;
3984
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 /*
3986 * Default initial state at registry is that the
3987 * device is present.
3988 */
3989
3990 set_bit(__LINK_STATE_PRESENT, &dev->state);
3991
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 dev_init_scheduler(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 dev_hold(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02003994 list_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
3996 /* Notify protocols, that a new device appeared. */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07003997 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
Herbert Xufcc5a032007-07-30 17:03:38 -07003998 ret = notifier_to_errno(ret);
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07003999 if (ret) {
4000 rollback_registered(dev);
4001 dev->reg_state = NETREG_UNREGISTERED;
4002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
4004out:
4005 return ret;
Herbert Xu7ce1b0e2007-07-30 16:29:40 -07004006
4007err_uninit:
4008 if (dev->uninit)
4009 dev->uninit(dev);
4010 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011}
4012
4013/**
4014 * register_netdev - register a network device
4015 * @dev: device to register
4016 *
4017 * Take a completed network device structure and add it to the kernel
4018 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
4019 * chain. 0 is returned on success. A negative errno code is returned
4020 * on a failure to set up the device, or if the name is a duplicate.
4021 *
Borislav Petkov38b4da32007-04-20 22:14:10 -07004022 * This is a wrapper around register_netdevice that takes the rtnl semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 * and expands the device name if you passed a format string to
4024 * alloc_netdev.
4025 */
4026int register_netdev(struct net_device *dev)
4027{
4028 int err;
4029
4030 rtnl_lock();
4031
4032 /*
4033 * If the name is a format string the caller wants us to do a
4034 * name allocation.
4035 */
4036 if (strchr(dev->name, '%')) {
4037 err = dev_alloc_name(dev, dev->name);
4038 if (err < 0)
4039 goto out;
4040 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004041
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 err = register_netdevice(dev);
4043out:
4044 rtnl_unlock();
4045 return err;
4046}
4047EXPORT_SYMBOL(register_netdev);
4048
4049/*
4050 * netdev_wait_allrefs - wait until all references are gone.
4051 *
4052 * This is called when unregistering network devices.
4053 *
4054 * Any protocol or device that holds a reference should register
4055 * for netdevice notification, and cleanup and put back the
4056 * reference if they receive an UNREGISTER event.
4057 * We can get stuck here if buggy protocols don't correctly
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004058 * call dev_put.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 */
4060static void netdev_wait_allrefs(struct net_device *dev)
4061{
4062 unsigned long rebroadcast_time, warning_time;
4063
4064 rebroadcast_time = warning_time = jiffies;
4065 while (atomic_read(&dev->refcnt) != 0) {
4066 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08004067 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
4069 /* Rebroadcast unregister notification */
Pavel Emelyanov056925a2007-09-16 15:42:43 -07004070 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
4072 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
4073 &dev->state)) {
4074 /* We must not have linkwatch events
4075 * pending on unregister. If this
4076 * happens, we simply run the queue
4077 * unscheduled, resulting in a noop
4078 * for this device.
4079 */
4080 linkwatch_run_queue();
4081 }
4082
Stephen Hemminger6756ae42006-03-20 22:23:58 -08004083 __rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
4085 rebroadcast_time = jiffies;
4086 }
4087
4088 msleep(250);
4089
4090 if (time_after(jiffies, warning_time + 10 * HZ)) {
4091 printk(KERN_EMERG "unregister_netdevice: "
4092 "waiting for %s to become free. Usage "
4093 "count = %d\n",
4094 dev->name, atomic_read(&dev->refcnt));
4095 warning_time = jiffies;
4096 }
4097 }
4098}
4099
4100/* The sequence is:
4101 *
4102 * rtnl_lock();
4103 * ...
4104 * register_netdevice(x1);
4105 * register_netdevice(x2);
4106 * ...
4107 * unregister_netdevice(y1);
4108 * unregister_netdevice(y2);
4109 * ...
4110 * rtnl_unlock();
4111 * free_netdev(y1);
4112 * free_netdev(y2);
4113 *
4114 * We are invoked by rtnl_unlock() after it drops the semaphore.
4115 * This allows us to deal with problems:
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07004116 * 1) We can delete sysfs objects which invoke hotplug
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 * without deadlocking with linkwatch via keventd.
4118 * 2) Since we run with the RTNL semaphore not held, we can sleep
4119 * safely in order to wait for the netdev refcnt to drop to zero.
4120 */
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08004121static DEFINE_MUTEX(net_todo_run_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122void netdev_run_todo(void)
4123{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07004124 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
4126 /* Need to guard against multiple cpu's getting out of order. */
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08004127 mutex_lock(&net_todo_run_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
4129 /* Not safe to do outside the semaphore. We must not return
4130 * until all unregister events invoked by the local processor
4131 * have been completed (either by this todo run, or one on
4132 * another cpu).
4133 */
4134 if (list_empty(&net_todo_list))
4135 goto out;
4136
4137 /* Snapshot list, allow later requests */
4138 spin_lock(&net_todo_list_lock);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07004139 list_replace_init(&net_todo_list, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 spin_unlock(&net_todo_list_lock);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07004141
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 while (!list_empty(&list)) {
4143 struct net_device *dev
4144 = list_entry(list.next, struct net_device, todo_list);
4145 list_del(&dev->todo_list);
4146
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07004147 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 printk(KERN_ERR "network todo '%s' but state %d\n",
4149 dev->name, dev->reg_state);
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07004150 dump_stack();
4151 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 }
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07004153
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07004154 dev->reg_state = NETREG_UNREGISTERED;
4155
4156 netdev_wait_allrefs(dev);
4157
4158 /* paranoia */
4159 BUG_ON(atomic_read(&dev->refcnt));
4160 BUG_TRAP(!dev->ip_ptr);
4161 BUG_TRAP(!dev->ip6_ptr);
4162 BUG_TRAP(!dev->dn_ptr);
4163
Stephen Hemmingerb17a7c12006-05-10 13:21:17 -07004164 if (dev->destructor)
4165 dev->destructor(dev);
Stephen Hemminger9093bbb2007-05-19 15:39:25 -07004166
4167 /* Free network device */
4168 kobject_put(&dev->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 }
4170
4171out:
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08004172 mutex_unlock(&net_todo_run_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173}
4174
Rusty Russell5a1b5892007-04-28 21:04:03 -07004175static struct net_device_stats *internal_stats(struct net_device *dev)
Rusty Russellc45d2862007-03-28 14:29:08 -07004176{
Rusty Russell5a1b5892007-04-28 21:04:03 -07004177 return &dev->stats;
Rusty Russellc45d2862007-03-28 14:29:08 -07004178}
4179
David S. Millerdc2b4842008-07-08 17:18:23 -07004180static void netdev_init_one_queue(struct net_device *dev,
David S. Millere8a04642008-07-17 00:34:19 -07004181 struct netdev_queue *queue,
4182 void *_unused)
David S. Millerdc2b4842008-07-08 17:18:23 -07004183{
David S. Millerdc2b4842008-07-08 17:18:23 -07004184 queue->dev = dev;
4185}
4186
David S. Millerbb949fb2008-07-08 16:55:56 -07004187static void netdev_init_queues(struct net_device *dev)
4188{
David S. Millere8a04642008-07-17 00:34:19 -07004189 netdev_init_one_queue(dev, &dev->rx_queue, NULL);
4190 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
David S. Millerbb949fb2008-07-08 16:55:56 -07004191}
4192
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193/**
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07004194 * alloc_netdev_mq - allocate network device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 * @sizeof_priv: size of private data to allocate space for
4196 * @name: device name format string
4197 * @setup: callback to initialize device
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07004198 * @queue_count: the number of subqueues to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 *
4200 * Allocates a struct net_device with private data area for driver use
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07004201 * and performs basic initialization. Also allocates subquue structs
4202 * for each queue on the device at the end of the netdevice.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 */
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07004204struct net_device *alloc_netdev_mq(int sizeof_priv, const char *name,
4205 void (*setup)(struct net_device *), unsigned int queue_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206{
David S. Millere8a04642008-07-17 00:34:19 -07004207 struct netdev_queue *tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 struct net_device *dev;
4209 int alloc_size;
David S. Millere8a04642008-07-17 00:34:19 -07004210 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07004212 BUG_ON(strlen(name) >= sizeof(dev->name));
4213
David S. Millerfd2ea0a2008-07-17 01:56:23 -07004214 alloc_size = sizeof(struct net_device);
Alexey Dobriyand1643d22008-04-18 15:43:32 -07004215 if (sizeof_priv) {
4216 /* ensure 32-byte alignment of private area */
4217 alloc_size = (alloc_size + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST;
4218 alloc_size += sizeof_priv;
4219 }
4220 /* ensure 32-byte alignment of whole construct */
4221 alloc_size += NETDEV_ALIGN_CONST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222
Paolo 'Blaisorblade' Giarrusso31380de2006-04-06 22:38:28 -07004223 p = kzalloc(alloc_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 if (!p) {
Stephen Hemmingerb6fe17d2006-08-29 17:06:13 -07004225 printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 return NULL;
4227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
David S. Millere8a04642008-07-17 00:34:19 -07004229 tx = kzalloc(sizeof(struct netdev_queue) * queue_count, GFP_KERNEL);
4230 if (!tx) {
4231 printk(KERN_ERR "alloc_netdev: Unable to allocate "
4232 "tx qdiscs.\n");
4233 kfree(p);
4234 return NULL;
4235 }
4236
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 dev = (struct net_device *)
4238 (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
4239 dev->padded = (char *)dev - (char *)p;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004240 dev_net_set(dev, &init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
David S. Millere8a04642008-07-17 00:34:19 -07004242 dev->_tx = tx;
4243 dev->num_tx_queues = queue_count;
David S. Millerfd2ea0a2008-07-17 01:56:23 -07004244 dev->real_num_tx_queues = queue_count;
David S. Millere8a04642008-07-17 00:34:19 -07004245
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07004246 if (sizeof_priv) {
4247 dev->priv = ((char *)dev +
David S. Millerfd2ea0a2008-07-17 01:56:23 -07004248 ((sizeof(struct net_device) + NETDEV_ALIGN_CONST)
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07004249 & ~NETDEV_ALIGN_CONST));
4250 }
4251
Peter P Waskiewicz Jr82cc1a72008-03-21 03:43:19 -07004252 dev->gso_max_size = GSO_MAX_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
David S. Millerbb949fb2008-07-08 16:55:56 -07004254 netdev_init_queues(dev);
4255
Rusty Russell5a1b5892007-04-28 21:04:03 -07004256 dev->get_stats = internal_stats;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004257 netpoll_netdev_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 setup(dev);
4259 strcpy(dev->name, name);
4260 return dev;
4261}
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07004262EXPORT_SYMBOL(alloc_netdev_mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
4264/**
4265 * free_netdev - free network device
4266 * @dev: device
4267 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004268 * This function does the last stage of destroying an allocated device
4269 * interface. The reference to the device object is released.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 * If this is the last reference then it will be freed.
4271 */
4272void free_netdev(struct net_device *dev)
4273{
Denis V. Lunevf3005d72008-04-16 02:02:18 -07004274 release_net(dev_net(dev));
4275
David S. Millere8a04642008-07-17 00:34:19 -07004276 kfree(dev->_tx);
4277
Stephen Hemminger3041a062006-05-26 13:25:24 -07004278 /* Compatibility with error handling in drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 if (dev->reg_state == NETREG_UNINITIALIZED) {
4280 kfree((char *)dev - dev->padded);
4281 return;
4282 }
4283
4284 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
4285 dev->reg_state = NETREG_RELEASED;
4286
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07004287 /* will free via device release */
4288 put_device(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289}
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004290
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291/* Synchronize with packet receive processing. */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09004292void synchronize_net(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293{
4294 might_sleep();
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07004295 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296}
4297
4298/**
4299 * unregister_netdevice - remove device from the kernel
4300 * @dev: device
4301 *
4302 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08004303 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 *
4305 * Callers must hold the rtnl semaphore. You may want
4306 * unregister_netdev() instead of this.
4307 */
4308
Stephen Hemminger22f8cde2007-02-07 00:09:58 -08004309void unregister_netdevice(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310{
Herbert Xua6620712007-12-12 19:21:56 -08004311 ASSERT_RTNL();
4312
Daniel Lezcano93ee31f2007-10-30 15:38:18 -07004313 rollback_registered(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 /* Finish processing unregister after unlock */
4315 net_set_todo(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316}
4317
4318/**
4319 * unregister_netdev - remove device from the kernel
4320 * @dev: device
4321 *
4322 * This function shuts down a device interface and removes it
Wang Chend59b54b2007-12-11 02:28:03 -08004323 * from the kernel tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 *
4325 * This is just a wrapper for unregister_netdevice that takes
4326 * the rtnl semaphore. In general you want to use this and not
4327 * unregister_netdevice.
4328 */
4329void unregister_netdev(struct net_device *dev)
4330{
4331 rtnl_lock();
4332 unregister_netdevice(dev);
4333 rtnl_unlock();
4334}
4335
4336EXPORT_SYMBOL(unregister_netdev);
4337
Eric W. Biedermance286d32007-09-12 13:53:49 +02004338/**
4339 * dev_change_net_namespace - move device to different nethost namespace
4340 * @dev: device
4341 * @net: network namespace
4342 * @pat: If not NULL name pattern to try if the current device name
4343 * is already taken in the destination network namespace.
4344 *
4345 * This function shuts down a device interface and moves it
4346 * to a new network namespace. On success 0 is returned, on
4347 * a failure a netagive errno code is returned.
4348 *
4349 * Callers must hold the rtnl semaphore.
4350 */
4351
4352int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
4353{
4354 char buf[IFNAMSIZ];
4355 const char *destname;
4356 int err;
4357
4358 ASSERT_RTNL();
4359
4360 /* Don't allow namespace local devices to be moved. */
4361 err = -EINVAL;
4362 if (dev->features & NETIF_F_NETNS_LOCAL)
4363 goto out;
4364
4365 /* Ensure the device has been registrered */
4366 err = -EINVAL;
4367 if (dev->reg_state != NETREG_REGISTERED)
4368 goto out;
4369
4370 /* Get out if there is nothing todo */
4371 err = 0;
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09004372 if (net_eq(dev_net(dev), net))
Eric W. Biedermance286d32007-09-12 13:53:49 +02004373 goto out;
4374
4375 /* Pick the destination device name, and ensure
4376 * we can use it in the destination network namespace.
4377 */
4378 err = -EEXIST;
4379 destname = dev->name;
4380 if (__dev_get_by_name(net, destname)) {
4381 /* We get here if we can't use the current device name */
4382 if (!pat)
4383 goto out;
4384 if (!dev_valid_name(pat))
4385 goto out;
4386 if (strchr(pat, '%')) {
4387 if (__dev_alloc_name(net, pat, buf) < 0)
4388 goto out;
4389 destname = buf;
4390 } else
4391 destname = pat;
4392 if (__dev_get_by_name(net, destname))
4393 goto out;
4394 }
4395
4396 /*
4397 * And now a mini version of register_netdevice unregister_netdevice.
4398 */
4399
4400 /* If device is running close it first. */
Pavel Emelyanov9b772652007-10-10 02:49:09 -07004401 dev_close(dev);
Eric W. Biedermance286d32007-09-12 13:53:49 +02004402
4403 /* And unlink it from device chain */
4404 err = -ENODEV;
4405 unlist_netdevice(dev);
4406
4407 synchronize_net();
4408
4409 /* Shutdown queueing discipline. */
4410 dev_shutdown(dev);
4411
4412 /* Notify protocols, that we are about to destroy
4413 this device. They should clean all the things.
4414 */
4415 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
4416
4417 /*
4418 * Flush the unicast and multicast chains
4419 */
4420 dev_addr_discard(dev);
4421
4422 /* Actually switch the network namespace */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004423 dev_net_set(dev, net);
Eric W. Biedermance286d32007-09-12 13:53:49 +02004424
4425 /* Assign the new device name */
4426 if (destname != dev->name)
4427 strcpy(dev->name, destname);
4428
4429 /* If there is an ifindex conflict assign a new one */
4430 if (__dev_get_by_index(net, dev->ifindex)) {
4431 int iflink = (dev->iflink == dev->ifindex);
4432 dev->ifindex = dev_new_index(net);
4433 if (iflink)
4434 dev->iflink = dev->ifindex;
4435 }
4436
Eric W. Biederman8b41d182007-09-26 22:02:53 -07004437 /* Fixup kobjects */
Daniel Lezcanoaaf8cdc2008-05-02 17:00:58 -07004438 netdev_unregister_kobject(dev);
4439 err = netdev_register_kobject(dev);
Eric W. Biederman8b41d182007-09-26 22:02:53 -07004440 WARN_ON(err);
Eric W. Biedermance286d32007-09-12 13:53:49 +02004441
4442 /* Add the device back in the hashes */
4443 list_netdevice(dev);
4444
4445 /* Notify protocols, that a new device appeared. */
4446 call_netdevice_notifiers(NETDEV_REGISTER, dev);
4447
4448 synchronize_net();
4449 err = 0;
4450out:
4451 return err;
4452}
4453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454static int dev_cpu_callback(struct notifier_block *nfb,
4455 unsigned long action,
4456 void *ocpu)
4457{
4458 struct sk_buff **list_skb;
David S. Miller37437bb2008-07-16 02:15:04 -07004459 struct Qdisc **list_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 struct sk_buff *skb;
4461 unsigned int cpu, oldcpu = (unsigned long)ocpu;
4462 struct softnet_data *sd, *oldsd;
4463
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07004464 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 return NOTIFY_OK;
4466
4467 local_irq_disable();
4468 cpu = smp_processor_id();
4469 sd = &per_cpu(softnet_data, cpu);
4470 oldsd = &per_cpu(softnet_data, oldcpu);
4471
4472 /* Find end of our completion_queue. */
4473 list_skb = &sd->completion_queue;
4474 while (*list_skb)
4475 list_skb = &(*list_skb)->next;
4476 /* Append completion queue from offline CPU. */
4477 *list_skb = oldsd->completion_queue;
4478 oldsd->completion_queue = NULL;
4479
4480 /* Find end of our output_queue. */
4481 list_net = &sd->output_queue;
4482 while (*list_net)
4483 list_net = &(*list_net)->next_sched;
4484 /* Append output queue from offline CPU. */
4485 *list_net = oldsd->output_queue;
4486 oldsd->output_queue = NULL;
4487
4488 raise_softirq_irqoff(NET_TX_SOFTIRQ);
4489 local_irq_enable();
4490
4491 /* Process offline CPU's input_pkt_queue */
4492 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
4493 netif_rx(skb);
4494
4495 return NOTIFY_OK;
4496}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
Chris Leechdb217332006-06-17 21:24:58 -07004498#ifdef CONFIG_NET_DMA
4499/**
Randy Dunlap0ed72ec2007-07-26 00:03:29 -07004500 * net_dma_rebalance - try to maintain one DMA channel per CPU
4501 * @net_dma: DMA client and associated data (lock, channels, channel_mask)
4502 *
4503 * This is called when the number of channels allocated to the net_dma client
4504 * changes. The net_dma client tries to have one DMA channel per CPU.
Chris Leechdb217332006-06-17 21:24:58 -07004505 */
Dan Williamsd379b012007-07-09 11:56:42 -07004506
4507static void net_dma_rebalance(struct net_dma *net_dma)
Chris Leechdb217332006-06-17 21:24:58 -07004508{
Dan Williamsd379b012007-07-09 11:56:42 -07004509 unsigned int cpu, i, n, chan_idx;
Chris Leechdb217332006-06-17 21:24:58 -07004510 struct dma_chan *chan;
4511
Dan Williamsd379b012007-07-09 11:56:42 -07004512 if (cpus_empty(net_dma->channel_mask)) {
Chris Leechdb217332006-06-17 21:24:58 -07004513 for_each_online_cpu(cpu)
Alexey Dobriyan29bbd722006-08-02 15:02:31 -07004514 rcu_assign_pointer(per_cpu(softnet_data, cpu).net_dma, NULL);
Chris Leechdb217332006-06-17 21:24:58 -07004515 return;
4516 }
4517
4518 i = 0;
4519 cpu = first_cpu(cpu_online_map);
4520
Dan Williamsd379b012007-07-09 11:56:42 -07004521 for_each_cpu_mask(chan_idx, net_dma->channel_mask) {
4522 chan = net_dma->channels[chan_idx];
4523
4524 n = ((num_online_cpus() / cpus_weight(net_dma->channel_mask))
4525 + (i < (num_online_cpus() %
4526 cpus_weight(net_dma->channel_mask)) ? 1 : 0));
Chris Leechdb217332006-06-17 21:24:58 -07004527
4528 while(n) {
Alexey Dobriyan29bbd722006-08-02 15:02:31 -07004529 per_cpu(softnet_data, cpu).net_dma = chan;
Chris Leechdb217332006-06-17 21:24:58 -07004530 cpu = next_cpu(cpu, cpu_online_map);
4531 n--;
4532 }
4533 i++;
4534 }
Chris Leechdb217332006-06-17 21:24:58 -07004535}
4536
4537/**
4538 * netdev_dma_event - event callback for the net_dma_client
4539 * @client: should always be net_dma_client
Randy Dunlapf4b8ea72006-06-22 16:00:11 -07004540 * @chan: DMA channel for the event
Randy Dunlap0ed72ec2007-07-26 00:03:29 -07004541 * @state: DMA state to be handled
Chris Leechdb217332006-06-17 21:24:58 -07004542 */
Dan Williamsd379b012007-07-09 11:56:42 -07004543static enum dma_state_client
4544netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
4545 enum dma_state state)
Chris Leechdb217332006-06-17 21:24:58 -07004546{
Dan Williamsd379b012007-07-09 11:56:42 -07004547 int i, found = 0, pos = -1;
4548 struct net_dma *net_dma =
4549 container_of(client, struct net_dma, client);
4550 enum dma_state_client ack = DMA_DUP; /* default: take no action */
4551
4552 spin_lock(&net_dma->lock);
4553 switch (state) {
4554 case DMA_RESOURCE_AVAILABLE:
Mike Travis0c0b0ac2008-05-02 16:43:08 -07004555 for (i = 0; i < nr_cpu_ids; i++)
Dan Williamsd379b012007-07-09 11:56:42 -07004556 if (net_dma->channels[i] == chan) {
4557 found = 1;
4558 break;
4559 } else if (net_dma->channels[i] == NULL && pos < 0)
4560 pos = i;
4561
4562 if (!found && pos >= 0) {
4563 ack = DMA_ACK;
4564 net_dma->channels[pos] = chan;
4565 cpu_set(pos, net_dma->channel_mask);
4566 net_dma_rebalance(net_dma);
4567 }
Chris Leechdb217332006-06-17 21:24:58 -07004568 break;
4569 case DMA_RESOURCE_REMOVED:
Mike Travis0c0b0ac2008-05-02 16:43:08 -07004570 for (i = 0; i < nr_cpu_ids; i++)
Dan Williamsd379b012007-07-09 11:56:42 -07004571 if (net_dma->channels[i] == chan) {
4572 found = 1;
4573 pos = i;
4574 break;
4575 }
4576
4577 if (found) {
4578 ack = DMA_ACK;
4579 cpu_clear(pos, net_dma->channel_mask);
4580 net_dma->channels[i] = NULL;
4581 net_dma_rebalance(net_dma);
4582 }
Chris Leechdb217332006-06-17 21:24:58 -07004583 break;
4584 default:
4585 break;
4586 }
Dan Williamsd379b012007-07-09 11:56:42 -07004587 spin_unlock(&net_dma->lock);
4588
4589 return ack;
Chris Leechdb217332006-06-17 21:24:58 -07004590}
4591
4592/**
4593 * netdev_dma_regiser - register the networking subsystem as a DMA client
4594 */
4595static int __init netdev_dma_register(void)
4596{
Mike Travis0c0b0ac2008-05-02 16:43:08 -07004597 net_dma.channels = kzalloc(nr_cpu_ids * sizeof(struct net_dma),
4598 GFP_KERNEL);
4599 if (unlikely(!net_dma.channels)) {
4600 printk(KERN_NOTICE
4601 "netdev_dma: no memory for net_dma.channels\n");
4602 return -ENOMEM;
4603 }
Dan Williamsd379b012007-07-09 11:56:42 -07004604 spin_lock_init(&net_dma.lock);
4605 dma_cap_set(DMA_MEMCPY, net_dma.client.cap_mask);
4606 dma_async_client_register(&net_dma.client);
4607 dma_async_client_chan_request(&net_dma.client);
Chris Leechdb217332006-06-17 21:24:58 -07004608 return 0;
4609}
4610
4611#else
4612static int __init netdev_dma_register(void) { return -ENODEV; }
4613#endif /* CONFIG_NET_DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
Herbert Xu7f353bf2007-08-10 15:47:58 -07004615/**
4616 * netdev_compute_feature - compute conjunction of two feature sets
4617 * @all: first feature set
4618 * @one: second feature set
4619 *
4620 * Computes a new feature set after adding a device with feature set
4621 * @one to the master device with current feature set @all. Returns
4622 * the new feature set.
4623 */
4624int netdev_compute_features(unsigned long all, unsigned long one)
4625{
4626 /* if device needs checksumming, downgrade to hw checksumming */
4627 if (all & NETIF_F_NO_CSUM && !(one & NETIF_F_NO_CSUM))
4628 all ^= NETIF_F_NO_CSUM | NETIF_F_HW_CSUM;
4629
4630 /* if device can't do all checksum, downgrade to ipv4/ipv6 */
4631 if (all & NETIF_F_HW_CSUM && !(one & NETIF_F_HW_CSUM))
4632 all ^= NETIF_F_HW_CSUM
4633 | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
4634
4635 if (one & NETIF_F_GSO)
4636 one |= NETIF_F_GSO_SOFTWARE;
4637 one |= NETIF_F_GSO;
4638
4639 /* If even one device supports robust GSO, enable it for all. */
4640 if (one & NETIF_F_GSO_ROBUST)
4641 all |= NETIF_F_GSO_ROBUST;
4642
4643 all &= one | NETIF_F_LLTX;
4644
4645 if (!(all & NETIF_F_ALL_CSUM))
4646 all &= ~NETIF_F_SG;
4647 if (!(all & NETIF_F_SG))
4648 all &= ~NETIF_F_GSO_MASK;
4649
4650 return all;
4651}
4652EXPORT_SYMBOL(netdev_compute_features);
4653
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07004654static struct hlist_head *netdev_create_hash(void)
4655{
4656 int i;
4657 struct hlist_head *hash;
4658
4659 hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
4660 if (hash != NULL)
4661 for (i = 0; i < NETDEV_HASHENTRIES; i++)
4662 INIT_HLIST_HEAD(&hash[i]);
4663
4664 return hash;
4665}
4666
Eric W. Biederman881d9662007-09-17 11:56:21 -07004667/* Initialize per network namespace state */
Pavel Emelyanov46650792007-10-08 20:38:39 -07004668static int __net_init netdev_init(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07004669{
Eric W. Biederman881d9662007-09-17 11:56:21 -07004670 INIT_LIST_HEAD(&net->dev_base_head);
Eric W. Biederman881d9662007-09-17 11:56:21 -07004671
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07004672 net->dev_name_head = netdev_create_hash();
4673 if (net->dev_name_head == NULL)
4674 goto err_name;
Eric W. Biederman881d9662007-09-17 11:56:21 -07004675
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07004676 net->dev_index_head = netdev_create_hash();
4677 if (net->dev_index_head == NULL)
4678 goto err_idx;
Eric W. Biederman881d9662007-09-17 11:56:21 -07004679
4680 return 0;
Pavel Emelyanov30d97d32007-09-16 15:40:33 -07004681
4682err_idx:
4683 kfree(net->dev_name_head);
4684err_name:
4685 return -ENOMEM;
Eric W. Biederman881d9662007-09-17 11:56:21 -07004686}
4687
Pavel Emelyanov46650792007-10-08 20:38:39 -07004688static void __net_exit netdev_exit(struct net *net)
Eric W. Biederman881d9662007-09-17 11:56:21 -07004689{
4690 kfree(net->dev_name_head);
4691 kfree(net->dev_index_head);
4692}
4693
Denis V. Lunev022cbae2007-11-13 03:23:50 -08004694static struct pernet_operations __net_initdata netdev_net_ops = {
Eric W. Biederman881d9662007-09-17 11:56:21 -07004695 .init = netdev_init,
4696 .exit = netdev_exit,
4697};
4698
Pavel Emelyanov46650792007-10-08 20:38:39 -07004699static void __net_exit default_device_exit(struct net *net)
Eric W. Biedermance286d32007-09-12 13:53:49 +02004700{
4701 struct net_device *dev, *next;
4702 /*
4703 * Push all migratable of the network devices back to the
4704 * initial network namespace
4705 */
4706 rtnl_lock();
4707 for_each_netdev_safe(net, dev, next) {
4708 int err;
Pavel Emelyanovaca51392008-05-08 01:24:25 -07004709 char fb_name[IFNAMSIZ];
Eric W. Biedermance286d32007-09-12 13:53:49 +02004710
4711 /* Ignore unmoveable devices (i.e. loopback) */
4712 if (dev->features & NETIF_F_NETNS_LOCAL)
4713 continue;
4714
4715 /* Push remaing network devices to init_net */
Pavel Emelyanovaca51392008-05-08 01:24:25 -07004716 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
4717 err = dev_change_net_namespace(dev, &init_net, fb_name);
Eric W. Biedermance286d32007-09-12 13:53:49 +02004718 if (err) {
Pavel Emelyanovaca51392008-05-08 01:24:25 -07004719 printk(KERN_EMERG "%s: failed to move %s to init_net: %d\n",
Eric W. Biedermance286d32007-09-12 13:53:49 +02004720 __func__, dev->name, err);
Pavel Emelyanovaca51392008-05-08 01:24:25 -07004721 BUG();
Eric W. Biedermance286d32007-09-12 13:53:49 +02004722 }
4723 }
4724 rtnl_unlock();
4725}
4726
Denis V. Lunev022cbae2007-11-13 03:23:50 -08004727static struct pernet_operations __net_initdata default_device_ops = {
Eric W. Biedermance286d32007-09-12 13:53:49 +02004728 .exit = default_device_exit,
4729};
4730
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731/*
4732 * Initialize the DEV module. At boot time this walks the device list and
4733 * unhooks any devices that fail to initialise (normally hardware not
4734 * present) and leaves us with a valid list of present and active devices.
4735 *
4736 */
4737
4738/*
4739 * This is called single threaded during boot, so no need
4740 * to take the rtnl semaphore.
4741 */
4742static int __init net_dev_init(void)
4743{
4744 int i, rc = -ENOMEM;
4745
4746 BUG_ON(!dev_boot_phase);
4747
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 if (dev_proc_init())
4749 goto out;
4750
Eric W. Biederman8b41d182007-09-26 22:02:53 -07004751 if (netdev_kobject_init())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 goto out;
4753
4754 INIT_LIST_HEAD(&ptype_all);
Pavel Emelyanov82d8a8672007-11-26 20:12:58 +08004755 for (i = 0; i < PTYPE_HASH_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 INIT_LIST_HEAD(&ptype_base[i]);
4757
Eric W. Biederman881d9662007-09-17 11:56:21 -07004758 if (register_pernet_subsys(&netdev_net_ops))
4759 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
Eric W. Biedermance286d32007-09-12 13:53:49 +02004761 if (register_pernet_device(&default_device_ops))
4762 goto out;
4763
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 /*
4765 * Initialise the packet receive queues.
4766 */
4767
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -07004768 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 struct softnet_data *queue;
4770
4771 queue = &per_cpu(softnet_data, i);
4772 skb_queue_head_init(&queue->input_pkt_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 queue->completion_queue = NULL;
4774 INIT_LIST_HEAD(&queue->poll_list);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004775
4776 queue->backlog.poll = process_backlog;
4777 queue->backlog.weight = weight_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 }
4779
Chris Leechdb217332006-06-17 21:24:58 -07004780 netdev_dma_register();
4781
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 dev_boot_phase = 0;
4783
Carlos R. Mafra962cf362008-05-15 11:15:37 -03004784 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
4785 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
4787 hotcpu_notifier(dev_cpu_callback, 0);
4788 dst_init();
4789 dev_mcast_init();
4790 rc = 0;
4791out:
4792 return rc;
4793}
4794
4795subsys_initcall(net_dev_init);
4796
4797EXPORT_SYMBOL(__dev_get_by_index);
4798EXPORT_SYMBOL(__dev_get_by_name);
4799EXPORT_SYMBOL(__dev_remove_pack);
Mitch Williamsc2373ee2005-11-09 10:34:45 -08004800EXPORT_SYMBOL(dev_valid_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801EXPORT_SYMBOL(dev_add_pack);
4802EXPORT_SYMBOL(dev_alloc_name);
4803EXPORT_SYMBOL(dev_close);
4804EXPORT_SYMBOL(dev_get_by_flags);
4805EXPORT_SYMBOL(dev_get_by_index);
4806EXPORT_SYMBOL(dev_get_by_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807EXPORT_SYMBOL(dev_open);
4808EXPORT_SYMBOL(dev_queue_xmit);
4809EXPORT_SYMBOL(dev_remove_pack);
4810EXPORT_SYMBOL(dev_set_allmulti);
4811EXPORT_SYMBOL(dev_set_promiscuity);
4812EXPORT_SYMBOL(dev_change_flags);
4813EXPORT_SYMBOL(dev_set_mtu);
4814EXPORT_SYMBOL(dev_set_mac_address);
4815EXPORT_SYMBOL(free_netdev);
4816EXPORT_SYMBOL(netdev_boot_setup_check);
4817EXPORT_SYMBOL(netdev_set_master);
4818EXPORT_SYMBOL(netdev_state_change);
4819EXPORT_SYMBOL(netif_receive_skb);
4820EXPORT_SYMBOL(netif_rx);
4821EXPORT_SYMBOL(register_gifconf);
4822EXPORT_SYMBOL(register_netdevice);
4823EXPORT_SYMBOL(register_netdevice_notifier);
4824EXPORT_SYMBOL(skb_checksum_help);
4825EXPORT_SYMBOL(synchronize_net);
4826EXPORT_SYMBOL(unregister_netdevice);
4827EXPORT_SYMBOL(unregister_netdevice_notifier);
4828EXPORT_SYMBOL(net_enable_timestamp);
4829EXPORT_SYMBOL(net_disable_timestamp);
4830EXPORT_SYMBOL(dev_get_flags);
4831
4832#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4833EXPORT_SYMBOL(br_handle_frame_hook);
4834EXPORT_SYMBOL(br_fdb_get_hook);
4835EXPORT_SYMBOL(br_fdb_put_hook);
4836#endif
4837
4838#ifdef CONFIG_KMOD
4839EXPORT_SYMBOL(dev_load);
4840#endif
4841
4842EXPORT_PER_CPU_SYMBOL(softnet_data);