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