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