dev.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602
  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
  10. * Authors: Ross Biro
  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. #include <asm/uaccess.h>
  75. #include <asm/system.h>
  76. #include <linux/bitops.h>
  77. #include <linux/capability.h>
  78. #include <linux/cpu.h>
  79. #include <linux/types.h>
  80. #include <linux/kernel.h>
  81. #include <linux/sched.h>
  82. #include <linux/mutex.h>
  83. #include <linux/string.h>
  84. #include <linux/mm.h>
  85. #include <linux/socket.h>
  86. #include <linux/sockios.h>
  87. #include <linux/errno.h>
  88. #include <linux/interrupt.h>
  89. #include <linux/if_ether.h>
  90. #include <linux/netdevice.h>
  91. #include <linux/etherdevice.h>
  92. #include <linux/notifier.h>
  93. #include <linux/skbuff.h>
  94. #include <net/sock.h>
  95. #include <linux/rtnetlink.h>
  96. #include <linux/proc_fs.h>
  97. #include <linux/seq_file.h>
  98. #include <linux/stat.h>
  99. #include <linux/if_bridge.h>
  100. #include <linux/divert.h>
  101. #include <net/dst.h>
  102. #include <net/pkt_sched.h>
  103. #include <net/checksum.h>
  104. #include <linux/highmem.h>
  105. #include <linux/init.h>
  106. #include <linux/kmod.h>
  107. #include <linux/module.h>
  108. #include <linux/kallsyms.h>
  109. #include <linux/netpoll.h>
  110. #include <linux/rcupdate.h>
  111. #include <linux/delay.h>
  112. #include <linux/wireless.h>
  113. #include <net/iw_handler.h>
  114. #include <asm/current.h>
  115. #include <linux/audit.h>
  116. #include <linux/dmaengine.h>
  117. #include <linux/err.h>
  118. #include <linux/ctype.h>
  119. /*
  120. * The list of packet types we will receive (as opposed to discard)
  121. * and the routines to invoke.
  122. *
  123. * Why 16. Because with 16 the only overlap we get on a hash of the
  124. * low nibble of the protocol value is RARP/SNAP/X.25.
  125. *
  126. * NOTE: That is no longer true with the addition of VLAN tags. Not
  127. * sure which should go first, but I bet it won't make much
  128. * difference if we are running VLANs. The good news is that
  129. * this protocol won't be in the list unless compiled in, so
  130. * the average user (w/out VLANs) will not be adversely affected.
  131. * --BLG
  132. *
  133. * 0800 IP
  134. * 8100 802.1Q VLAN
  135. * 0001 802.3
  136. * 0002 AX.25
  137. * 0004 802.2
  138. * 8035 RARP
  139. * 0005 SNAP
  140. * 0805 X.25
  141. * 0806 ARP
  142. * 8137 IPX
  143. * 0009 Localtalk
  144. * 86DD IPv6
  145. */
  146. static DEFINE_SPINLOCK(ptype_lock);
  147. static struct list_head ptype_base[16]; /* 16 way hashed list */
  148. static struct list_head ptype_all; /* Taps */
  149. #ifdef CONFIG_NET_DMA
  150. static struct dma_client *net_dma_client;
  151. static unsigned int net_dma_count;
  152. static spinlock_t net_dma_event_lock;
  153. #endif
  154. /*
  155. * The @dev_base list is protected by @dev_base_lock and the rtnl
  156. * semaphore.
  157. *
  158. * Pure readers hold dev_base_lock for reading.
  159. *
  160. * Writers must hold the rtnl semaphore while they loop through the
  161. * dev_base list, and hold dev_base_lock for writing when they do the
  162. * actual updates. This allows pure readers to access the list even
  163. * while a writer is preparing to update it.
  164. *
  165. * To put it another way, dev_base_lock is held for writing only to
  166. * protect against pure readers; the rtnl semaphore provides the
  167. * protection against other writers.
  168. *
  169. * See, for example usages, register_netdevice() and
  170. * unregister_netdevice(), which must be called with the rtnl
  171. * semaphore held.
  172. */
  173. struct net_device *dev_base;
  174. static struct net_device **dev_tail = &dev_base;
  175. DEFINE_RWLOCK(dev_base_lock);
  176. EXPORT_SYMBOL(dev_base);
  177. EXPORT_SYMBOL(dev_base_lock);
  178. #define NETDEV_HASHBITS 8
  179. static struct hlist_head dev_name_head[1<<NETDEV_HASHBITS];
  180. static struct hlist_head dev_index_head[1<<NETDEV_HASHBITS];
  181. static inline struct hlist_head *dev_name_hash(const char *name)
  182. {
  183. unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
  184. return &dev_name_head[hash & ((1<<NETDEV_HASHBITS)-1)];
  185. }
  186. static inline struct hlist_head *dev_index_hash(int ifindex)
  187. {
  188. return &dev_index_head[ifindex & ((1<<NETDEV_HASHBITS)-1)];
  189. }
  190. /*
  191. * Our notifier list
  192. */
  193. static RAW_NOTIFIER_HEAD(netdev_chain);
  194. /*
  195. * Device drivers call our routines to queue packets here. We empty the
  196. * queue in the local softnet handler.
  197. */
  198. DEFINE_PER_CPU(struct softnet_data, softnet_data) = { NULL };
  199. #ifdef CONFIG_SYSFS
  200. extern int netdev_sysfs_init(void);
  201. extern int netdev_register_sysfs(struct net_device *);
  202. extern void netdev_unregister_sysfs(struct net_device *);
  203. #else
  204. #define netdev_sysfs_init() (0)
  205. #define netdev_register_sysfs(dev) (0)
  206. #define netdev_unregister_sysfs(dev) do { } while(0)
  207. #endif
  208. /*******************************************************************************
  209. Protocol management and registration routines
  210. *******************************************************************************/
  211. /*
  212. * For efficiency
  213. */
  214. static int netdev_nit;
  215. /*
  216. * Add a protocol ID to the list. Now that the input handler is
  217. * smarter we can dispense with all the messy stuff that used to be
  218. * here.
  219. *
  220. * BEWARE!!! Protocol handlers, mangling input packets,
  221. * MUST BE last in hash buckets and checking protocol handlers
  222. * MUST start from promiscuous ptype_all chain in net_bh.
  223. * It is true now, do not change it.
  224. * Explanation follows: if protocol handler, mangling packet, will
  225. * be the first on list, it is not able to sense, that packet
  226. * is cloned and should be copied-on-write, so that it will
  227. * change it and subsequent readers will get broken packet.
  228. * --ANK (980803)
  229. */
  230. /**
  231. * dev_add_pack - add packet handler
  232. * @pt: packet type declaration
  233. *
  234. * Add a protocol handler to the networking stack. The passed &packet_type
  235. * is linked into kernel lists and may not be freed until it has been
  236. * removed from the kernel lists.
  237. *
  238. * This call does not sleep therefore it can not
  239. * guarantee all CPU's that are in middle of receiving packets
  240. * will see the new packet type (until the next received packet).
  241. */
  242. void dev_add_pack(struct packet_type *pt)
  243. {
  244. int hash;
  245. spin_lock_bh(&ptype_lock);
  246. if (pt->type == htons(ETH_P_ALL)) {
  247. netdev_nit++;
  248. list_add_rcu(&pt->list, &ptype_all);
  249. } else {
  250. hash = ntohs(pt->type) & 15;
  251. list_add_rcu(&pt->list, &ptype_base[hash]);
  252. }
  253. spin_unlock_bh(&ptype_lock);
  254. }
  255. /**
  256. * __dev_remove_pack - remove packet handler
  257. * @pt: packet type declaration
  258. *
  259. * Remove a protocol handler that was previously added to the kernel
  260. * protocol handlers by dev_add_pack(). The passed &packet_type is removed
  261. * from the kernel lists and can be freed or reused once this function
  262. * returns.
  263. *
  264. * The packet type might still be in use by receivers
  265. * and must not be freed until after all the CPU's have gone
  266. * through a quiescent state.
  267. */
  268. void __dev_remove_pack(struct packet_type *pt)
  269. {
  270. struct list_head *head;
  271. struct packet_type *pt1;
  272. spin_lock_bh(&ptype_lock);
  273. if (pt->type == htons(ETH_P_ALL)) {
  274. netdev_nit--;
  275. head = &ptype_all;
  276. } else
  277. head = &ptype_base[ntohs(pt->type) & 15];
  278. list_for_each_entry(pt1, head, list) {
  279. if (pt == pt1) {
  280. list_del_rcu(&pt->list);
  281. goto out;
  282. }
  283. }
  284. printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
  285. out:
  286. spin_unlock_bh(&ptype_lock);
  287. }
  288. /**
  289. * dev_remove_pack - remove packet handler
  290. * @pt: packet type declaration
  291. *
  292. * Remove a protocol handler that was previously added to the kernel
  293. * protocol handlers by dev_add_pack(). The passed &packet_type is removed
  294. * from the kernel lists and can be freed or reused once this function
  295. * returns.
  296. *
  297. * This call sleeps to guarantee that no CPU is looking at the packet
  298. * type after return.
  299. */
  300. void dev_remove_pack(struct packet_type *pt)
  301. {
  302. __dev_remove_pack(pt);
  303. synchronize_net();
  304. }
  305. /******************************************************************************
  306. Device Boot-time Settings Routines
  307. *******************************************************************************/
  308. /* Boot time configuration table */
  309. static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
  310. /**
  311. * netdev_boot_setup_add - add new setup entry
  312. * @name: name of the device
  313. * @map: configured settings for the device
  314. *
  315. * Adds new setup entry to the dev_boot_setup list. The function
  316. * returns 0 on error and 1 on success. This is a generic routine to
  317. * all netdevices.
  318. */
  319. static int netdev_boot_setup_add(char *name, struct ifmap *map)
  320. {
  321. struct netdev_boot_setup *s;
  322. int i;
  323. s = dev_boot_setup;
  324. for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
  325. if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
  326. memset(s[i].name, 0, sizeof(s[i].name));
  327. strcpy(s[i].name, name);
  328. memcpy(&s[i].map, map, sizeof(s[i].map));
  329. break;
  330. }
  331. }
  332. return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
  333. }
  334. /**
  335. * netdev_boot_setup_check - check boot time settings
  336. * @dev: the netdevice
  337. *
  338. * Check boot time settings for the device.
  339. * The found settings are set for the device to be used
  340. * later in the device probing.
  341. * Returns 0 if no settings found, 1 if they are.
  342. */
  343. int netdev_boot_setup_check(struct net_device *dev)
  344. {
  345. struct netdev_boot_setup *s = dev_boot_setup;
  346. int i;
  347. for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
  348. if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
  349. !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
  350. dev->irq = s[i].map.irq;
  351. dev->base_addr = s[i].map.base_addr;
  352. dev->mem_start = s[i].map.mem_start;
  353. dev->mem_end = s[i].map.mem_end;
  354. return 1;
  355. }
  356. }
  357. return 0;
  358. }
  359. /**
  360. * netdev_boot_base - get address from boot time settings
  361. * @prefix: prefix for network device
  362. * @unit: id for network device
  363. *
  364. * Check boot time settings for the base address of device.
  365. * The found settings are set for the device to be used
  366. * later in the device probing.
  367. * Returns 0 if no settings found.
  368. */
  369. unsigned long netdev_boot_base(const char *prefix, int unit)
  370. {
  371. const struct netdev_boot_setup *s = dev_boot_setup;
  372. char name[IFNAMSIZ];
  373. int i;
  374. sprintf(name, "%s%d", prefix, unit);
  375. /*
  376. * If device already registered then return base of 1
  377. * to indicate not to probe for this interface
  378. */
  379. if (__dev_get_by_name(name))
  380. return 1;
  381. for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
  382. if (!strcmp(name, s[i].name))
  383. return s[i].map.base_addr;
  384. return 0;
  385. }
  386. /*
  387. * Saves at boot time configured settings for any netdevice.
  388. */
  389. int __init netdev_boot_setup(char *str)
  390. {
  391. int ints[5];
  392. struct ifmap map;
  393. str = get_options(str, ARRAY_SIZE(ints), ints);
  394. if (!str || !*str)
  395. return 0;
  396. /* Save settings */
  397. memset(&map, 0, sizeof(map));
  398. if (ints[0] > 0)
  399. map.irq = ints[1];
  400. if (ints[0] > 1)
  401. map.base_addr = ints[2];
  402. if (ints[0] > 2)
  403. map.mem_start = ints[3];
  404. if (ints[0] > 3)
  405. map.mem_end = ints[4];
  406. /* Add new entry to the list */
  407. return netdev_boot_setup_add(str, &map);
  408. }
  409. __setup("netdev=", netdev_boot_setup);
  410. /*******************************************************************************
  411. Device Interface Subroutines
  412. *******************************************************************************/
  413. /**
  414. * __dev_get_by_name - find a device by its name
  415. * @name: name to find
  416. *
  417. * Find an interface by name. Must be called under RTNL semaphore
  418. * or @dev_base_lock. If the name is found a pointer to the device
  419. * is returned. If the name is not found then %NULL is returned. The
  420. * reference counters are not incremented so the caller must be
  421. * careful with locks.
  422. */
  423. struct net_device *__dev_get_by_name(const char *name)
  424. {
  425. struct hlist_node *p;
  426. hlist_for_each(p, dev_name_hash(name)) {
  427. struct net_device *dev
  428. = hlist_entry(p, struct net_device, name_hlist);
  429. if (!strncmp(dev->name, name, IFNAMSIZ))
  430. return dev;
  431. }
  432. return NULL;
  433. }
  434. /**
  435. * dev_get_by_name - find a device by its name
  436. * @name: name to find
  437. *
  438. * Find an interface by name. This can be called from any
  439. * context and does its own locking. The returned handle has
  440. * the usage count incremented and the caller must use dev_put() to
  441. * release it when it is no longer needed. %NULL is returned if no
  442. * matching device is found.
  443. */
  444. struct net_device *dev_get_by_name(const char *name)
  445. {
  446. struct net_device *dev;
  447. read_lock(&dev_base_lock);
  448. dev = __dev_get_by_name(name);
  449. if (dev)
  450. dev_hold(dev);
  451. read_unlock(&dev_base_lock);
  452. return dev;
  453. }
  454. /**
  455. * __dev_get_by_index - find a device by its ifindex
  456. * @ifindex: index of device
  457. *
  458. * Search for an interface by index. Returns %NULL if the device
  459. * is not found or a pointer to the device. The device has not
  460. * had its reference counter increased so the caller must be careful
  461. * about locking. The caller must hold either the RTNL semaphore
  462. * or @dev_base_lock.
  463. */
  464. struct net_device *__dev_get_by_index(int ifindex)
  465. {
  466. struct hlist_node *p;
  467. hlist_for_each(p, dev_index_hash(ifindex)) {
  468. struct net_device *dev
  469. = hlist_entry(p, struct net_device, index_hlist);
  470. if (dev->ifindex == ifindex)
  471. return dev;
  472. }
  473. return NULL;
  474. }
  475. /**
  476. * dev_get_by_index - find a device by its ifindex
  477. * @ifindex: index of device
  478. *
  479. * Search for an interface by index. Returns NULL if the device
  480. * is not found or a pointer to the device. The device returned has
  481. * had a reference added and the pointer is safe until the user calls
  482. * dev_put to indicate they have finished with it.
  483. */
  484. struct net_device *dev_get_by_index(int ifindex)
  485. {
  486. struct net_device *dev;
  487. read_lock(&dev_base_lock);
  488. dev = __dev_get_by_index(ifindex);
  489. if (dev)
  490. dev_hold(dev);
  491. read_unlock(&dev_base_lock);
  492. return dev;
  493. }
  494. /**
  495. * dev_getbyhwaddr - find a device by its hardware address
  496. * @type: media type of device
  497. * @ha: hardware address
  498. *
  499. * Search for an interface by MAC address. Returns NULL if the device
  500. * is not found or a pointer to the device. The caller must hold the
  501. * rtnl semaphore. The returned device has not had its ref count increased
  502. * and the caller must therefore be careful about locking
  503. *
  504. * BUGS:
  505. * If the API was consistent this would be __dev_get_by_hwaddr
  506. */
  507. struct net_device *dev_getbyhwaddr(unsigned short type, char *ha)
  508. {
  509. struct net_device *dev;
  510. ASSERT_RTNL();
  511. for (dev = dev_base; dev; dev = dev->next)
  512. if (dev->type == type &&
  513. !memcmp(dev->dev_addr, ha, dev->addr_len))
  514. break;
  515. return dev;
  516. }
  517. EXPORT_SYMBOL(dev_getbyhwaddr);
  518. struct net_device *dev_getfirstbyhwtype(unsigned short type)
  519. {
  520. struct net_device *dev;
  521. rtnl_lock();
  522. for (dev = dev_base; dev; dev = dev->next) {
  523. if (dev->type == type) {
  524. dev_hold(dev);
  525. break;
  526. }
  527. }
  528. rtnl_unlock();
  529. return dev;
  530. }
  531. EXPORT_SYMBOL(dev_getfirstbyhwtype);
  532. /**
  533. * dev_get_by_flags - find any device with given flags
  534. * @if_flags: IFF_* values
  535. * @mask: bitmask of bits in if_flags to check
  536. *
  537. * Search for any interface with the given flags. Returns NULL if a device
  538. * is not found or a pointer to the device. The device returned has
  539. * had a reference added and the pointer is safe until the user calls
  540. * dev_put to indicate they have finished with it.
  541. */
  542. struct net_device * dev_get_by_flags(unsigned short if_flags, unsigned short mask)
  543. {
  544. struct net_device *dev;
  545. read_lock(&dev_base_lock);
  546. for (dev = dev_base; dev != NULL; dev = dev->next) {
  547. if (((dev->flags ^ if_flags) & mask) == 0) {
  548. dev_hold(dev);
  549. break;
  550. }
  551. }
  552. read_unlock(&dev_base_lock);
  553. return dev;
  554. }
  555. /**
  556. * dev_valid_name - check if name is okay for network device
  557. * @name: name string
  558. *
  559. * Network device names need to be valid file names to
  560. * to allow sysfs to work. We also disallow any kind of
  561. * whitespace.
  562. */
  563. int dev_valid_name(const char *name)
  564. {
  565. if (*name == '\0')
  566. return 0;
  567. if (strlen(name) >= IFNAMSIZ)
  568. return 0;
  569. if (!strcmp(name, ".") || !strcmp(name, ".."))
  570. return 0;
  571. while (*name) {
  572. if (*name == '/' || isspace(*name))
  573. return 0;
  574. name++;
  575. }
  576. return 1;
  577. }
  578. /**
  579. * dev_alloc_name - allocate a name for a device
  580. * @dev: device
  581. * @name: name format string
  582. *
  583. * Passed a format string - eg "lt%d" it will try and find a suitable
  584. * id. It scans list of devices to build up a free map, then chooses
  585. * the first empty slot. The caller must hold the dev_base or rtnl lock
  586. * while allocating the name and adding the device in order to avoid
  587. * duplicates.
  588. * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
  589. * Returns the number of the unit assigned or a negative errno code.
  590. */
  591. int dev_alloc_name(struct net_device *dev, const char *name)
  592. {
  593. int i = 0;
  594. char buf[IFNAMSIZ];
  595. const char *p;
  596. const int max_netdevices = 8*PAGE_SIZE;
  597. long *inuse;
  598. struct net_device *d;
  599. p = strnchr(name, IFNAMSIZ-1, '%');
  600. if (p) {
  601. /*
  602. * Verify the string as this thing may have come from
  603. * the user. There must be either one "%d" and no other "%"
  604. * characters.
  605. */
  606. if (p[1] != 'd' || strchr(p + 2, '%'))
  607. return -EINVAL;
  608. /* Use one page as a bit array of possible slots */
  609. inuse = (long *) get_zeroed_page(GFP_ATOMIC);
  610. if (!inuse)
  611. return -ENOMEM;
  612. for (d = dev_base; d; d = d->next) {
  613. if (!sscanf(d->name, name, &i))
  614. continue;
  615. if (i < 0 || i >= max_netdevices)
  616. continue;
  617. /* avoid cases where sscanf is not exact inverse of printf */
  618. snprintf(buf, sizeof(buf), name, i);
  619. if (!strncmp(buf, d->name, IFNAMSIZ))
  620. set_bit(i, inuse);
  621. }
  622. i = find_first_zero_bit(inuse, max_netdevices);
  623. free_page((unsigned long) inuse);
  624. }
  625. snprintf(buf, sizeof(buf), name, i);
  626. if (!__dev_get_by_name(buf)) {
  627. strlcpy(dev->name, buf, IFNAMSIZ);
  628. return i;
  629. }
  630. /* It is possible to run out of possible slots
  631. * when the name is long and there isn't enough space left
  632. * for the digits, or if all bits are used.
  633. */
  634. return -ENFILE;
  635. }
  636. /**
  637. * dev_change_name - change name of a device
  638. * @dev: device
  639. * @newname: name (or format string) must be at least IFNAMSIZ
  640. *
  641. * Change name of a device, can pass format strings "eth%d".
  642. * for wildcarding.
  643. */
  644. int dev_change_name(struct net_device *dev, char *newname)
  645. {
  646. int err = 0;
  647. ASSERT_RTNL();
  648. if (dev->flags & IFF_UP)
  649. return -EBUSY;
  650. if (!dev_valid_name(newname))
  651. return -EINVAL;
  652. if (strchr(newname, '%')) {
  653. err = dev_alloc_name(dev, newname);
  654. if (err < 0)
  655. return err;
  656. strcpy(newname, dev->name);
  657. }
  658. else if (__dev_get_by_name(newname))
  659. return -EEXIST;
  660. else
  661. strlcpy(dev->name, newname, IFNAMSIZ);
  662. err = class_device_rename(&dev->class_dev, dev->name);
  663. if (!err) {
  664. hlist_del(&dev->name_hlist);
  665. hlist_add_head(&dev->name_hlist, dev_name_hash(dev->name));
  666. raw_notifier_call_chain(&netdev_chain,
  667. NETDEV_CHANGENAME, dev);
  668. }
  669. return err;
  670. }
  671. /**
  672. * netdev_features_change - device changes features
  673. * @dev: device to cause notification
  674. *
  675. * Called to indicate a device has changed features.
  676. */
  677. void netdev_features_change(struct net_device *dev)
  678. {
  679. raw_notifier_call_chain(&netdev_chain, NETDEV_FEAT_CHANGE, dev);
  680. }
  681. EXPORT_SYMBOL(netdev_features_change);
  682. /**
  683. * netdev_state_change - device changes state
  684. * @dev: device to cause notification
  685. *
  686. * Called to indicate a device has changed state. This function calls
  687. * the notifier chains for netdev_chain and sends a NEWLINK message
  688. * to the routing socket.
  689. */
  690. void netdev_state_change(struct net_device *dev)
  691. {
  692. if (dev->flags & IFF_UP) {
  693. raw_notifier_call_chain(&netdev_chain,
  694. NETDEV_CHANGE, dev);
  695. rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
  696. }
  697. }
  698. /**
  699. * dev_load - load a network module
  700. * @name: name of interface
  701. *
  702. * If a network interface is not present and the process has suitable
  703. * privileges this function loads the module. If module loading is not
  704. * available in this kernel then it becomes a nop.
  705. */
  706. void dev_load(const char *name)
  707. {
  708. struct net_device *dev;
  709. read_lock(&dev_base_lock);
  710. dev = __dev_get_by_name(name);
  711. read_unlock(&dev_base_lock);
  712. if (!dev && capable(CAP_SYS_MODULE))
  713. request_module("%s", name);
  714. }
  715. static int default_rebuild_header(struct sk_buff *skb)
  716. {
  717. printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n",
  718. skb->dev ? skb->dev->name : "NULL!!!");
  719. kfree_skb(skb);
  720. return 1;
  721. }
  722. /**
  723. * dev_open - prepare an interface for use.
  724. * @dev: device to open
  725. *
  726. * Takes a device from down to up state. The device's private open
  727. * function is invoked and then the multicast lists are loaded. Finally
  728. * the device is moved into the up state and a %NETDEV_UP message is
  729. * sent to the netdev notifier chain.
  730. *
  731. * Calling this function on an active interface is a nop. On a failure
  732. * a negative errno code is returned.
  733. */
  734. int dev_open(struct net_device *dev)
  735. {
  736. int ret = 0;
  737. /*
  738. * Is it already up?
  739. */
  740. if (dev->flags & IFF_UP)
  741. return 0;
  742. /*
  743. * Is it even present?
  744. */
  745. if (!netif_device_present(dev))
  746. return -ENODEV;
  747. /*
  748. * Call device private open method
  749. */
  750. set_bit(__LINK_STATE_START, &dev->state);
  751. if (dev->open) {
  752. ret = dev->open(dev);
  753. if (ret)
  754. clear_bit(__LINK_STATE_START, &dev->state);
  755. }
  756. /*
  757. * If it went open OK then:
  758. */
  759. if (!ret) {
  760. /*
  761. * Set the flags.
  762. */
  763. dev->flags |= IFF_UP;
  764. /*
  765. * Initialize multicasting status
  766. */
  767. dev_mc_upload(dev);
  768. /*
  769. * Wakeup transmit queue engine
  770. */
  771. dev_activate(dev);
  772. /*
  773. * ... and announce new interface.
  774. */
  775. raw_notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
  776. }
  777. return ret;
  778. }
  779. /**
  780. * dev_close - shutdown an interface.
  781. * @dev: device to shutdown
  782. *
  783. * This function moves an active device into down state. A
  784. * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
  785. * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
  786. * chain.
  787. */
  788. int dev_close(struct net_device *dev)
  789. {
  790. if (!(dev->flags & IFF_UP))
  791. return 0;
  792. /*
  793. * Tell people we are going down, so that they can
  794. * prepare to death, when device is still operating.
  795. */
  796. raw_notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
  797. dev_deactivate(dev);
  798. clear_bit(__LINK_STATE_START, &dev->state);
  799. /* Synchronize to scheduled poll. We cannot touch poll list,
  800. * it can be even on different cpu. So just clear netif_running(),
  801. * and wait when poll really will happen. Actually, the best place
  802. * for this is inside dev->stop() after device stopped its irq
  803. * engine, but this requires more changes in devices. */
  804. smp_mb__after_clear_bit(); /* Commit netif_running(). */
  805. while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) {
  806. /* No hurry. */
  807. msleep(1);
  808. }
  809. /*
  810. * Call the device specific close. This cannot fail.
  811. * Only if device is UP
  812. *
  813. * We allow it to be called even after a DETACH hot-plug
  814. * event.
  815. */
  816. if (dev->stop)
  817. dev->stop(dev);
  818. /*
  819. * Device is now down.
  820. */
  821. dev->flags &= ~IFF_UP;
  822. /*
  823. * Tell people we are down
  824. */
  825. raw_notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
  826. return 0;
  827. }
  828. /*
  829. * Device change register/unregister. These are not inline or static
  830. * as we export them to the world.
  831. */
  832. /**
  833. * register_netdevice_notifier - register a network notifier block
  834. * @nb: notifier
  835. *
  836. * Register a notifier to be called when network device events occur.
  837. * The notifier passed is linked into the kernel structures and must
  838. * not be reused until it has been unregistered. A negative errno code
  839. * is returned on a failure.
  840. *
  841. * When registered all registration and up events are replayed
  842. * to the new notifier to allow device to have a race free
  843. * view of the network device list.
  844. */
  845. int register_netdevice_notifier(struct notifier_block *nb)
  846. {
  847. struct net_device *dev;
  848. int err;
  849. rtnl_lock();
  850. err = raw_notifier_chain_register(&netdev_chain, nb);
  851. if (!err) {
  852. for (dev = dev_base; dev; dev = dev->next) {
  853. nb->notifier_call(nb, NETDEV_REGISTER, dev);
  854. if (dev->flags & IFF_UP)
  855. nb->notifier_call(nb, NETDEV_UP, dev);
  856. }
  857. }
  858. rtnl_unlock();
  859. return err;
  860. }
  861. /**
  862. * unregister_netdevice_notifier - unregister a network notifier block
  863. * @nb: notifier
  864. *
  865. * Unregister a notifier previously registered by
  866. * register_netdevice_notifier(). The notifier is unlinked into the
  867. * kernel structures and may then be reused. A negative errno code
  868. * is returned on a failure.
  869. */
  870. int unregister_netdevice_notifier(struct notifier_block *nb)
  871. {
  872. int err;
  873. rtnl_lock();
  874. err = raw_notifier_chain_unregister(&netdev_chain, nb);
  875. rtnl_unlock();
  876. return err;
  877. }
  878. /**
  879. * call_netdevice_notifiers - call all network notifier blocks
  880. * @val: value passed unmodified to notifier function
  881. * @v: pointer passed unmodified to notifier function
  882. *
  883. * Call all network notifier blocks. Parameters and return value
  884. * are as for raw_notifier_call_chain().
  885. */
  886. int call_netdevice_notifiers(unsigned long val, void *v)
  887. {
  888. return raw_notifier_call_chain(&netdev_chain, val, v);
  889. }
  890. /* When > 0 there are consumers of rx skb time stamps */
  891. static atomic_t netstamp_needed = ATOMIC_INIT(0);
  892. void net_enable_timestamp(void)
  893. {
  894. atomic_inc(&netstamp_needed);
  895. }
  896. void net_disable_timestamp(void)
  897. {
  898. atomic_dec(&netstamp_needed);
  899. }
  900. void __net_timestamp(struct sk_buff *skb)
  901. {
  902. struct timeval tv;
  903. do_gettimeofday(&tv);
  904. skb_set_timestamp(skb, &tv);
  905. }
  906. EXPORT_SYMBOL(__net_timestamp);
  907. static inline void net_timestamp(struct sk_buff *skb)
  908. {
  909. if (atomic_read(&netstamp_needed))
  910. __net_timestamp(skb);
  911. else {
  912. skb->tstamp.off_sec = 0;
  913. skb->tstamp.off_usec = 0;
  914. }
  915. }
  916. /*
  917. * Support routine. Sends outgoing frames to any network
  918. * taps currently in use.
  919. */
  920. static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
  921. {
  922. struct packet_type *ptype;
  923. net_timestamp(skb);
  924. rcu_read_lock();
  925. list_for_each_entry_rcu(ptype, &ptype_all, list) {
  926. /* Never send packets back to the socket
  927. * they originated from - MvS (miquels@drinkel.ow.org)
  928. */
  929. if ((ptype->dev == dev || !ptype->dev) &&
  930. (ptype->af_packet_priv == NULL ||
  931. (struct sock *)ptype->af_packet_priv != skb->sk)) {
  932. struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
  933. if (!skb2)
  934. break;
  935. /* skb->nh should be correctly
  936. set by sender, so that the second statement is
  937. just protection against buggy protocols.
  938. */
  939. skb2->mac.raw = skb2->data;
  940. if (skb2->nh.raw < skb2->data ||
  941. skb2->nh.raw > skb2->tail) {
  942. if (net_ratelimit())
  943. printk(KERN_CRIT "protocol %04x is "
  944. "buggy, dev %s\n",
  945. skb2->protocol, dev->name);
  946. skb2->nh.raw = skb2->data;
  947. }
  948. skb2->h.raw = skb2->nh.raw;
  949. skb2->pkt_type = PACKET_OUTGOING;
  950. ptype->func(skb2, skb->dev, ptype, skb->dev);
  951. }
  952. }
  953. rcu_read_unlock();
  954. }
  955. void __netif_schedule(struct net_device *dev)
  956. {
  957. if (!test_and_set_bit(__LINK_STATE_SCHED, &dev->state)) {
  958. unsigned long flags;
  959. struct softnet_data *sd;
  960. local_irq_save(flags);
  961. sd = &__get_cpu_var(softnet_data);
  962. dev->next_sched = sd->output_queue;
  963. sd->output_queue = dev;
  964. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  965. local_irq_restore(flags);
  966. }
  967. }
  968. EXPORT_SYMBOL(__netif_schedule);
  969. void __netif_rx_schedule(struct net_device *dev)
  970. {
  971. unsigned long flags;
  972. local_irq_save(flags);
  973. dev_hold(dev);
  974. list_add_tail(&dev->poll_list, &__get_cpu_var(softnet_data).poll_list);
  975. if (dev->quota < 0)
  976. dev->quota += dev->weight;
  977. else
  978. dev->quota = dev->weight;
  979. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  980. local_irq_restore(flags);
  981. }
  982. EXPORT_SYMBOL(__netif_rx_schedule);
  983. void dev_kfree_skb_any(struct sk_buff *skb)
  984. {
  985. if (in_irq() || irqs_disabled())
  986. dev_kfree_skb_irq(skb);
  987. else
  988. dev_kfree_skb(skb);
  989. }
  990. EXPORT_SYMBOL(dev_kfree_skb_any);
  991. /* Hot-plugging. */
  992. void netif_device_detach(struct net_device *dev)
  993. {
  994. if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
  995. netif_running(dev)) {
  996. netif_stop_queue(dev);
  997. }
  998. }
  999. EXPORT_SYMBOL(netif_device_detach);
  1000. void netif_device_attach(struct net_device *dev)
  1001. {
  1002. if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
  1003. netif_running(dev)) {
  1004. netif_wake_queue(dev);
  1005. __netdev_watchdog_up(dev);
  1006. }
  1007. }
  1008. EXPORT_SYMBOL(netif_device_attach);
  1009. /*
  1010. * Invalidate hardware checksum when packet is to be mangled, and
  1011. * complete checksum manually on outgoing path.
  1012. */
  1013. int skb_checksum_help(struct sk_buff *skb)
  1014. {
  1015. unsigned int csum;
  1016. int ret = 0, offset = skb->h.raw - skb->data;
  1017. if (skb->ip_summed == CHECKSUM_COMPLETE)
  1018. goto out_set_summed;
  1019. if (unlikely(skb_shinfo(skb)->gso_size)) {
  1020. /* Let GSO fix up the checksum. */
  1021. goto out_set_summed;
  1022. }
  1023. if (skb_cloned(skb)) {
  1024. ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  1025. if (ret)
  1026. goto out;
  1027. }
  1028. BUG_ON(offset > (int)skb->len);
  1029. csum = skb_checksum(skb, offset, skb->len-offset, 0);
  1030. offset = skb->tail - skb->h.raw;
  1031. BUG_ON(offset <= 0);
  1032. BUG_ON(skb->csum + 2 > offset);
  1033. *(u16*)(skb->h.raw + skb->csum) = csum_fold(csum);
  1034. out_set_summed:
  1035. skb->ip_summed = CHECKSUM_NONE;
  1036. out:
  1037. return ret;
  1038. }
  1039. /**
  1040. * skb_gso_segment - Perform segmentation on skb.
  1041. * @skb: buffer to segment
  1042. * @features: features for the output path (see dev->features)
  1043. *
  1044. * This function segments the given skb and returns a list of segments.
  1045. *
  1046. * It may return NULL if the skb requires no segmentation. This is
  1047. * only possible when GSO is used for verifying header integrity.
  1048. */
  1049. struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
  1050. {
  1051. struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
  1052. struct packet_type *ptype;
  1053. int type = skb->protocol;
  1054. int err;
  1055. BUG_ON(skb_shinfo(skb)->frag_list);
  1056. skb->mac.raw = skb->data;
  1057. skb->mac_len = skb->nh.raw - skb->data;
  1058. __skb_pull(skb, skb->mac_len);
  1059. if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
  1060. if (skb_header_cloned(skb) &&
  1061. (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  1062. return ERR_PTR(err);
  1063. }
  1064. rcu_read_lock();
  1065. list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type) & 15], list) {
  1066. if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
  1067. if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
  1068. err = ptype->gso_send_check(skb);
  1069. segs = ERR_PTR(err);
  1070. if (err || skb_gso_ok(skb, features))
  1071. break;
  1072. __skb_push(skb, skb->data - skb->nh.raw);
  1073. }
  1074. segs = ptype->gso_segment(skb, features);
  1075. break;
  1076. }
  1077. }
  1078. rcu_read_unlock();
  1079. __skb_push(skb, skb->data - skb->mac.raw);
  1080. return segs;
  1081. }
  1082. EXPORT_SYMBOL(skb_gso_segment);
  1083. /* Take action when hardware reception checksum errors are detected. */
  1084. #ifdef CONFIG_BUG
  1085. void netdev_rx_csum_fault(struct net_device *dev)
  1086. {
  1087. if (net_ratelimit()) {
  1088. printk(KERN_ERR "%s: hw csum failure.\n",
  1089. dev ? dev->name : "<unknown>");
  1090. dump_stack();
  1091. }
  1092. }
  1093. EXPORT_SYMBOL(netdev_rx_csum_fault);
  1094. #endif
  1095. /* Actually, we should eliminate this check as soon as we know, that:
  1096. * 1. IOMMU is present and allows to map all the memory.
  1097. * 2. No high memory really exists on this machine.
  1098. */
  1099. static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
  1100. {
  1101. #ifdef CONFIG_HIGHMEM
  1102. int i;
  1103. if (dev->features & NETIF_F_HIGHDMA)
  1104. return 0;
  1105. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1106. if (PageHighMem(skb_shinfo(skb)->frags[i].page))
  1107. return 1;
  1108. #endif
  1109. return 0;
  1110. }
  1111. struct dev_gso_cb {
  1112. void (*destructor)(struct sk_buff *skb);
  1113. };
  1114. #define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
  1115. static void dev_gso_skb_destructor(struct sk_buff *skb)
  1116. {
  1117. struct dev_gso_cb *cb;
  1118. do {
  1119. struct sk_buff *nskb = skb->next;
  1120. skb->next = nskb->next;
  1121. nskb->next = NULL;
  1122. kfree_skb(nskb);
  1123. } while (skb->next);
  1124. cb = DEV_GSO_CB(skb);
  1125. if (cb->destructor)
  1126. cb->destructor(skb);
  1127. }
  1128. /**
  1129. * dev_gso_segment - Perform emulated hardware segmentation on skb.
  1130. * @skb: buffer to segment
  1131. *
  1132. * This function segments the given skb and stores the list of segments
  1133. * in skb->next.
  1134. */
  1135. static int dev_gso_segment(struct sk_buff *skb)
  1136. {
  1137. struct net_device *dev = skb->dev;
  1138. struct sk_buff *segs;
  1139. int features = dev->features & ~(illegal_highdma(dev, skb) ?
  1140. NETIF_F_SG : 0);
  1141. segs = skb_gso_segment(skb, features);
  1142. /* Verifying header integrity only. */
  1143. if (!segs)
  1144. return 0;
  1145. if (unlikely(IS_ERR(segs)))
  1146. return PTR_ERR(segs);
  1147. skb->next = segs;
  1148. DEV_GSO_CB(skb)->destructor = skb->destructor;
  1149. skb->destructor = dev_gso_skb_destructor;
  1150. return 0;
  1151. }
  1152. int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1153. {
  1154. if (likely(!skb->next)) {
  1155. if (netdev_nit)
  1156. dev_queue_xmit_nit(skb, dev);
  1157. if (netif_needs_gso(dev, skb)) {
  1158. if (unlikely(dev_gso_segment(skb)))
  1159. goto out_kfree_skb;
  1160. if (skb->next)
  1161. goto gso;
  1162. }
  1163. return dev->hard_start_xmit(skb, dev);
  1164. }
  1165. gso:
  1166. do {
  1167. struct sk_buff *nskb = skb->next;
  1168. int rc;
  1169. skb->next = nskb->next;
  1170. nskb->next = NULL;
  1171. rc = dev->hard_start_xmit(nskb, dev);
  1172. if (unlikely(rc)) {
  1173. nskb->next = skb->next;
  1174. skb->next = nskb;
  1175. return rc;
  1176. }
  1177. if (unlikely(netif_queue_stopped(dev) && skb->next))
  1178. return NETDEV_TX_BUSY;
  1179. } while (skb->next);
  1180. skb->destructor = DEV_GSO_CB(skb)->destructor;
  1181. out_kfree_skb:
  1182. kfree_skb(skb);
  1183. return 0;
  1184. }
  1185. #define HARD_TX_LOCK(dev, cpu) { \
  1186. if ((dev->features & NETIF_F_LLTX) == 0) { \
  1187. netif_tx_lock(dev); \
  1188. } \
  1189. }
  1190. #define HARD_TX_UNLOCK(dev) { \
  1191. if ((dev->features & NETIF_F_LLTX) == 0) { \
  1192. netif_tx_unlock(dev); \
  1193. } \
  1194. }
  1195. /**
  1196. * dev_queue_xmit - transmit a buffer
  1197. * @skb: buffer to transmit
  1198. *
  1199. * Queue a buffer for transmission to a network device. The caller must
  1200. * have set the device and priority and built the buffer before calling
  1201. * this function. The function can be called from an interrupt.
  1202. *
  1203. * A negative errno code is returned on a failure. A success does not
  1204. * guarantee the frame will be transmitted as it may be dropped due
  1205. * to congestion or traffic shaping.
  1206. *
  1207. * -----------------------------------------------------------------------------------
  1208. * I notice this method can also return errors from the queue disciplines,
  1209. * including NET_XMIT_DROP, which is a positive value. So, errors can also
  1210. * be positive.
  1211. *
  1212. * Regardless of the return value, the skb is consumed, so it is currently
  1213. * difficult to retry a send to this method. (You can bump the ref count
  1214. * before sending to hold a reference for retry if you are careful.)
  1215. *
  1216. * When calling this method, interrupts MUST be enabled. This is because
  1217. * the BH enable code must have IRQs enabled so that it will not deadlock.
  1218. * --BLG
  1219. */
  1220. int dev_queue_xmit(struct sk_buff *skb)
  1221. {
  1222. struct net_device *dev = skb->dev;
  1223. struct Qdisc *q;
  1224. int rc = -ENOMEM;
  1225. /* GSO will handle the following emulations directly. */
  1226. if (netif_needs_gso(dev, skb))
  1227. goto gso;
  1228. if (skb_shinfo(skb)->frag_list &&
  1229. !(dev->features & NETIF_F_FRAGLIST) &&
  1230. __skb_linearize(skb))
  1231. goto out_kfree_skb;
  1232. /* Fragmented skb is linearized if device does not support SG,
  1233. * or if at least one of fragments is in highmem and device
  1234. * does not support DMA from it.
  1235. */
  1236. if (skb_shinfo(skb)->nr_frags &&
  1237. (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
  1238. __skb_linearize(skb))
  1239. goto out_kfree_skb;
  1240. /* If packet is not checksummed and device does not support
  1241. * checksumming for this protocol, complete checksumming here.
  1242. */
  1243. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  1244. (!(dev->features & NETIF_F_GEN_CSUM) &&
  1245. (!(dev->features & NETIF_F_IP_CSUM) ||
  1246. skb->protocol != htons(ETH_P_IP))))
  1247. if (skb_checksum_help(skb))
  1248. goto out_kfree_skb;
  1249. gso:
  1250. spin_lock_prefetch(&dev->queue_lock);
  1251. /* Disable soft irqs for various locks below. Also
  1252. * stops preemption for RCU.
  1253. */
  1254. rcu_read_lock_bh();
  1255. /* Updates of qdisc are serialized by queue_lock.
  1256. * The struct Qdisc which is pointed to by qdisc is now a
  1257. * rcu structure - it may be accessed without acquiring
  1258. * a lock (but the structure may be stale.) The freeing of the
  1259. * qdisc will be deferred until it's known that there are no
  1260. * more references to it.
  1261. *
  1262. * If the qdisc has an enqueue function, we still need to
  1263. * hold the queue_lock before calling it, since queue_lock
  1264. * also serializes access to the device queue.
  1265. */
  1266. q = rcu_dereference(dev->qdisc);
  1267. #ifdef CONFIG_NET_CLS_ACT
  1268. skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
  1269. #endif
  1270. if (q->enqueue) {
  1271. /* Grab device queue */
  1272. spin_lock(&dev->queue_lock);
  1273. q = dev->qdisc;
  1274. if (q->enqueue) {
  1275. rc = q->enqueue(skb, q);
  1276. qdisc_run(dev);
  1277. spin_unlock(&dev->queue_lock);
  1278. rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
  1279. goto out;
  1280. }
  1281. spin_unlock(&dev->queue_lock);
  1282. }
  1283. /* The device has no queue. Common case for software devices:
  1284. loopback, all the sorts of tunnels...
  1285. Really, it is unlikely that netif_tx_lock protection is necessary
  1286. here. (f.e. loopback and IP tunnels are clean ignoring statistics
  1287. counters.)
  1288. However, it is possible, that they rely on protection
  1289. made by us here.
  1290. Check this and shot the lock. It is not prone from deadlocks.
  1291. Either shot noqueue qdisc, it is even simpler 8)
  1292. */
  1293. if (dev->flags & IFF_UP) {
  1294. int cpu = smp_processor_id(); /* ok because BHs are off */
  1295. if (dev->xmit_lock_owner != cpu) {
  1296. HARD_TX_LOCK(dev, cpu);
  1297. if (!netif_queue_stopped(dev)) {
  1298. rc = 0;
  1299. if (!dev_hard_start_xmit(skb, dev)) {
  1300. HARD_TX_UNLOCK(dev);
  1301. goto out;
  1302. }
  1303. }
  1304. HARD_TX_UNLOCK(dev);
  1305. if (net_ratelimit())
  1306. printk(KERN_CRIT "Virtual device %s asks to "
  1307. "queue packet!\n", dev->name);
  1308. } else {
  1309. /* Recursion is detected! It is possible,
  1310. * unfortunately */
  1311. if (net_ratelimit())
  1312. printk(KERN_CRIT "Dead loop on virtual device "
  1313. "%s, fix it urgently!\n", dev->name);
  1314. }
  1315. }
  1316. rc = -ENETDOWN;
  1317. rcu_read_unlock_bh();
  1318. out_kfree_skb:
  1319. kfree_skb(skb);
  1320. return rc;
  1321. out:
  1322. rcu_read_unlock_bh();
  1323. return rc;
  1324. }
  1325. /*=======================================================================
  1326. Receiver routines
  1327. =======================================================================*/
  1328. int netdev_max_backlog = 1000;
  1329. int netdev_budget = 300;
  1330. int weight_p = 64; /* old backlog weight */
  1331. DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
  1332. /**
  1333. * netif_rx - post buffer to the network code
  1334. * @skb: buffer to post
  1335. *
  1336. * This function receives a packet from a device driver and queues it for
  1337. * the upper (protocol) levels to process. It always succeeds. The buffer
  1338. * may be dropped during processing for congestion control or by the
  1339. * protocol layers.
  1340. *
  1341. * return values:
  1342. * NET_RX_SUCCESS (no congestion)
  1343. * NET_RX_CN_LOW (low congestion)
  1344. * NET_RX_CN_MOD (moderate congestion)
  1345. * NET_RX_CN_HIGH (high congestion)
  1346. * NET_RX_DROP (packet was dropped)
  1347. *
  1348. */
  1349. int netif_rx(struct sk_buff *skb)
  1350. {
  1351. struct softnet_data *queue;
  1352. unsigned long flags;
  1353. /* if netpoll wants it, pretend we never saw it */
  1354. if (netpoll_rx(skb))
  1355. return NET_RX_DROP;
  1356. if (!skb->tstamp.off_sec)
  1357. net_timestamp(skb);
  1358. /*
  1359. * The code is rearranged so that the path is the most
  1360. * short when CPU is congested, but is still operating.
  1361. */
  1362. local_irq_save(flags);
  1363. queue = &__get_cpu_var(softnet_data);
  1364. __get_cpu_var(netdev_rx_stat).total++;
  1365. if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
  1366. if (queue->input_pkt_queue.qlen) {
  1367. enqueue:
  1368. dev_hold(skb->dev);
  1369. __skb_queue_tail(&queue->input_pkt_queue, skb);
  1370. local_irq_restore(flags);
  1371. return NET_RX_SUCCESS;
  1372. }
  1373. netif_rx_schedule(&queue->backlog_dev);
  1374. goto enqueue;
  1375. }
  1376. __get_cpu_var(netdev_rx_stat).dropped++;
  1377. local_irq_restore(flags);
  1378. kfree_skb(skb);
  1379. return NET_RX_DROP;
  1380. }
  1381. int netif_rx_ni(struct sk_buff *skb)
  1382. {
  1383. int err;
  1384. preempt_disable();
  1385. err = netif_rx(skb);
  1386. if (local_softirq_pending())
  1387. do_softirq();
  1388. preempt_enable();
  1389. return err;
  1390. }
  1391. EXPORT_SYMBOL(netif_rx_ni);
  1392. static inline struct net_device *skb_bond(struct sk_buff *skb)
  1393. {
  1394. struct net_device *dev = skb->dev;
  1395. if (dev->master) {
  1396. if (skb_bond_should_drop(skb)) {
  1397. kfree_skb(skb);
  1398. return NULL;
  1399. }
  1400. skb->dev = dev->master;
  1401. }
  1402. return dev;
  1403. }
  1404. static void net_tx_action(struct softirq_action *h)
  1405. {
  1406. struct softnet_data *sd = &__get_cpu_var(softnet_data);
  1407. if (sd->completion_queue) {
  1408. struct sk_buff *clist;
  1409. local_irq_disable();
  1410. clist = sd->completion_queue;
  1411. sd->completion_queue = NULL;
  1412. local_irq_enable();
  1413. while (clist) {
  1414. struct sk_buff *skb = clist;
  1415. clist = clist->next;
  1416. BUG_TRAP(!atomic_read(&skb->users));
  1417. __kfree_skb(skb);
  1418. }
  1419. }
  1420. if (sd->output_queue) {
  1421. struct net_device *head;
  1422. local_irq_disable();
  1423. head = sd->output_queue;
  1424. sd->output_queue = NULL;
  1425. local_irq_enable();
  1426. while (head) {
  1427. struct net_device *dev = head;
  1428. head = head->next_sched;
  1429. smp_mb__before_clear_bit();
  1430. clear_bit(__LINK_STATE_SCHED, &dev->state);
  1431. if (spin_trylock(&dev->queue_lock)) {
  1432. qdisc_run(dev);
  1433. spin_unlock(&dev->queue_lock);
  1434. } else {
  1435. netif_schedule(dev);
  1436. }
  1437. }
  1438. }
  1439. }
  1440. static __inline__ int deliver_skb(struct sk_buff *skb,
  1441. struct packet_type *pt_prev,
  1442. struct net_device *orig_dev)
  1443. {
  1444. atomic_inc(&skb->users);
  1445. return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
  1446. }
  1447. #if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
  1448. int (*br_handle_frame_hook)(struct net_bridge_port *p, struct sk_buff **pskb);
  1449. struct net_bridge;
  1450. struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
  1451. unsigned char *addr);
  1452. void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent);
  1453. static __inline__ int handle_bridge(struct sk_buff **pskb,
  1454. struct packet_type **pt_prev, int *ret,
  1455. struct net_device *orig_dev)
  1456. {
  1457. struct net_bridge_port *port;
  1458. if ((*pskb)->pkt_type == PACKET_LOOPBACK ||
  1459. (port = rcu_dereference((*pskb)->dev->br_port)) == NULL)
  1460. return 0;
  1461. if (*pt_prev) {
  1462. *ret = deliver_skb(*pskb, *pt_prev, orig_dev);
  1463. *pt_prev = NULL;
  1464. }
  1465. return br_handle_frame_hook(port, pskb);
  1466. }
  1467. #else
  1468. #define handle_bridge(skb, pt_prev, ret, orig_dev) (0)
  1469. #endif
  1470. #ifdef CONFIG_NET_CLS_ACT
  1471. /* TODO: Maybe we should just force sch_ingress to be compiled in
  1472. * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
  1473. * a compare and 2 stores extra right now if we dont have it on
  1474. * but have CONFIG_NET_CLS_ACT
  1475. * NOTE: This doesnt stop any functionality; if you dont have
  1476. * the ingress scheduler, you just cant add policies on ingress.
  1477. *
  1478. */
  1479. static int ing_filter(struct sk_buff *skb)
  1480. {
  1481. struct Qdisc *q;
  1482. struct net_device *dev = skb->dev;
  1483. int result = TC_ACT_OK;
  1484. if (dev->qdisc_ingress) {
  1485. __u32 ttl = (__u32) G_TC_RTTL(skb->tc_verd);
  1486. if (MAX_RED_LOOP < ttl++) {
  1487. printk(KERN_WARNING "Redir loop detected Dropping packet (%s->%s)\n",
  1488. skb->input_dev->name, skb->dev->name);
  1489. return TC_ACT_SHOT;
  1490. }
  1491. skb->tc_verd = SET_TC_RTTL(skb->tc_verd,ttl);
  1492. skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_INGRESS);
  1493. spin_lock(&dev->ingress_lock);
  1494. if ((q = dev->qdisc_ingress) != NULL)
  1495. result = q->enqueue(skb, q);
  1496. spin_unlock(&dev->ingress_lock);
  1497. }
  1498. return result;
  1499. }
  1500. #endif
  1501. int netif_receive_skb(struct sk_buff *skb)
  1502. {
  1503. struct packet_type *ptype, *pt_prev;
  1504. struct net_device *orig_dev;
  1505. int ret = NET_RX_DROP;
  1506. unsigned short type;
  1507. /* if we've gotten here through NAPI, check netpoll */
  1508. if (skb->dev->poll && netpoll_rx(skb))
  1509. return NET_RX_DROP;
  1510. if (!skb->tstamp.off_sec)
  1511. net_timestamp(skb);
  1512. if (!skb->input_dev)
  1513. skb->input_dev = skb->dev;
  1514. orig_dev = skb_bond(skb);
  1515. if (!orig_dev)
  1516. return NET_RX_DROP;
  1517. __get_cpu_var(netdev_rx_stat).total++;
  1518. skb->h.raw = skb->nh.raw = skb->data;
  1519. skb->mac_len = skb->nh.raw - skb->mac.raw;
  1520. pt_prev = NULL;
  1521. rcu_read_lock();
  1522. #ifdef CONFIG_NET_CLS_ACT
  1523. if (skb->tc_verd & TC_NCLS) {
  1524. skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
  1525. goto ncls;
  1526. }
  1527. #endif
  1528. list_for_each_entry_rcu(ptype, &ptype_all, list) {
  1529. if (!ptype->dev || ptype->dev == skb->dev) {
  1530. if (pt_prev)
  1531. ret = deliver_skb(skb, pt_prev, orig_dev);
  1532. pt_prev = ptype;
  1533. }
  1534. }
  1535. #ifdef CONFIG_NET_CLS_ACT
  1536. if (pt_prev) {
  1537. ret = deliver_skb(skb, pt_prev, orig_dev);
  1538. pt_prev = NULL; /* noone else should process this after*/
  1539. } else {
  1540. skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
  1541. }
  1542. ret = ing_filter(skb);
  1543. if (ret == TC_ACT_SHOT || (ret == TC_ACT_STOLEN)) {
  1544. kfree_skb(skb);
  1545. goto out;
  1546. }
  1547. skb->tc_verd = 0;
  1548. ncls:
  1549. #endif
  1550. handle_diverter(skb);
  1551. if (handle_bridge(&skb, &pt_prev, &ret, orig_dev))
  1552. goto out;
  1553. type = skb->protocol;
  1554. list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type)&15], list) {
  1555. if (ptype->type == type &&
  1556. (!ptype->dev || ptype->dev == skb->dev)) {
  1557. if (pt_prev)
  1558. ret = deliver_skb(skb, pt_prev, orig_dev);
  1559. pt_prev = ptype;
  1560. }
  1561. }
  1562. if (pt_prev) {
  1563. ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
  1564. } else {
  1565. kfree_skb(skb);
  1566. /* Jamal, now you will not able to escape explaining
  1567. * me how you were going to use this. :-)
  1568. */
  1569. ret = NET_RX_DROP;
  1570. }
  1571. out:
  1572. rcu_read_unlock();
  1573. return ret;
  1574. }
  1575. static int process_backlog(struct net_device *backlog_dev, int *budget)
  1576. {
  1577. int work = 0;
  1578. int quota = min(backlog_dev->quota, *budget);
  1579. struct softnet_data *queue = &__get_cpu_var(softnet_data);
  1580. unsigned long start_time = jiffies;
  1581. backlog_dev->weight = weight_p;
  1582. for (;;) {
  1583. struct sk_buff *skb;
  1584. struct net_device *dev;
  1585. local_irq_disable();
  1586. skb = __skb_dequeue(&queue->input_pkt_queue);
  1587. if (!skb)
  1588. goto job_done;
  1589. local_irq_enable();
  1590. dev = skb->dev;
  1591. netif_receive_skb(skb);
  1592. dev_put(dev);
  1593. work++;
  1594. if (work >= quota || jiffies - start_time > 1)
  1595. break;
  1596. }
  1597. backlog_dev->quota -= work;
  1598. *budget -= work;
  1599. return -1;
  1600. job_done:
  1601. backlog_dev->quota -= work;
  1602. *budget -= work;
  1603. list_del(&backlog_dev->poll_list);
  1604. smp_mb__before_clear_bit();
  1605. netif_poll_enable(backlog_dev);
  1606. local_irq_enable();
  1607. return 0;
  1608. }
  1609. static void net_rx_action(struct softirq_action *h)
  1610. {
  1611. struct softnet_data *queue = &__get_cpu_var(softnet_data);
  1612. unsigned long start_time = jiffies;
  1613. int budget = netdev_budget;
  1614. void *have;
  1615. local_irq_disable();
  1616. while (!list_empty(&queue->poll_list)) {
  1617. struct net_device *dev;
  1618. if (budget <= 0 || jiffies - start_time > 1)
  1619. goto softnet_break;
  1620. local_irq_enable();
  1621. dev = list_entry(queue->poll_list.next,
  1622. struct net_device, poll_list);
  1623. have = netpoll_poll_lock(dev);
  1624. if (dev->quota <= 0 || dev->poll(dev, &budget)) {
  1625. netpoll_poll_unlock(have);
  1626. local_irq_disable();
  1627. list_move_tail(&dev->poll_list, &queue->poll_list);
  1628. if (dev->quota < 0)
  1629. dev->quota += dev->weight;
  1630. else
  1631. dev->quota = dev->weight;
  1632. } else {
  1633. netpoll_poll_unlock(have);
  1634. dev_put(dev);
  1635. local_irq_disable();
  1636. }
  1637. }
  1638. out:
  1639. #ifdef CONFIG_NET_DMA
  1640. /*
  1641. * There may not be any more sk_buffs coming right now, so push
  1642. * any pending DMA copies to hardware
  1643. */
  1644. if (net_dma_client) {
  1645. struct dma_chan *chan;
  1646. rcu_read_lock();
  1647. list_for_each_entry_rcu(chan, &net_dma_client->channels, client_node)
  1648. dma_async_memcpy_issue_pending(chan);
  1649. rcu_read_unlock();
  1650. }
  1651. #endif
  1652. local_irq_enable();
  1653. return;
  1654. softnet_break:
  1655. __get_cpu_var(netdev_rx_stat).time_squeeze++;
  1656. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  1657. goto out;
  1658. }
  1659. static gifconf_func_t * gifconf_list [NPROTO];
  1660. /**
  1661. * register_gifconf - register a SIOCGIF handler
  1662. * @family: Address family
  1663. * @gifconf: Function handler
  1664. *
  1665. * Register protocol dependent address dumping routines. The handler
  1666. * that is passed must not be freed or reused until it has been replaced
  1667. * by another handler.
  1668. */
  1669. int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
  1670. {
  1671. if (family >= NPROTO)
  1672. return -EINVAL;
  1673. gifconf_list[family] = gifconf;
  1674. return 0;
  1675. }
  1676. /*
  1677. * Map an interface index to its name (SIOCGIFNAME)
  1678. */
  1679. /*
  1680. * We need this ioctl for efficient implementation of the
  1681. * if_indextoname() function required by the IPv6 API. Without
  1682. * it, we would have to search all the interfaces to find a
  1683. * match. --pb
  1684. */
  1685. static int dev_ifname(struct ifreq __user *arg)
  1686. {
  1687. struct net_device *dev;
  1688. struct ifreq ifr;
  1689. /*
  1690. * Fetch the caller's info block.
  1691. */
  1692. if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
  1693. return -EFAULT;
  1694. read_lock(&dev_base_lock);
  1695. dev = __dev_get_by_index(ifr.ifr_ifindex);
  1696. if (!dev) {
  1697. read_unlock(&dev_base_lock);
  1698. return -ENODEV;
  1699. }
  1700. strcpy(ifr.ifr_name, dev->name);
  1701. read_unlock(&dev_base_lock);
  1702. if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
  1703. return -EFAULT;
  1704. return 0;
  1705. }
  1706. /*
  1707. * Perform a SIOCGIFCONF call. This structure will change
  1708. * size eventually, and there is nothing I can do about it.
  1709. * Thus we will need a 'compatibility mode'.
  1710. */
  1711. static int dev_ifconf(char __user *arg)
  1712. {
  1713. struct ifconf ifc;
  1714. struct net_device *dev;
  1715. char __user *pos;
  1716. int len;
  1717. int total;
  1718. int i;
  1719. /*
  1720. * Fetch the caller's info block.
  1721. */
  1722. if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
  1723. return -EFAULT;
  1724. pos = ifc.ifc_buf;
  1725. len = ifc.ifc_len;
  1726. /*
  1727. * Loop over the interfaces, and write an info block for each.
  1728. */
  1729. total = 0;
  1730. for (dev = dev_base; dev; dev = dev->next) {
  1731. for (i = 0; i < NPROTO; i++) {
  1732. if (gifconf_list[i]) {
  1733. int done;
  1734. if (!pos)
  1735. done = gifconf_list[i](dev, NULL, 0);
  1736. else
  1737. done = gifconf_list[i](dev, pos + total,
  1738. len - total);
  1739. if (done < 0)
  1740. return -EFAULT;
  1741. total += done;
  1742. }
  1743. }
  1744. }
  1745. /*
  1746. * All done. Write the updated control block back to the caller.
  1747. */
  1748. ifc.ifc_len = total;
  1749. /*
  1750. * Both BSD and Solaris return 0 here, so we do too.
  1751. */
  1752. return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
  1753. }
  1754. #ifdef CONFIG_PROC_FS
  1755. /*
  1756. * This is invoked by the /proc filesystem handler to display a device
  1757. * in detail.
  1758. */
  1759. static __inline__ struct net_device *dev_get_idx(loff_t pos)
  1760. {
  1761. struct net_device *dev;
  1762. loff_t i;
  1763. for (i = 0, dev = dev_base; dev && i < pos; ++i, dev = dev->next);
  1764. return i == pos ? dev : NULL;
  1765. }
  1766. void *dev_seq_start(struct seq_file *seq, loff_t *pos)
  1767. {
  1768. read_lock(&dev_base_lock);
  1769. return *pos ? dev_get_idx(*pos - 1) : SEQ_START_TOKEN;
  1770. }
  1771. void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1772. {
  1773. ++*pos;
  1774. return v == SEQ_START_TOKEN ? dev_base : ((struct net_device *)v)->next;
  1775. }
  1776. void dev_seq_stop(struct seq_file *seq, void *v)
  1777. {
  1778. read_unlock(&dev_base_lock);
  1779. }
  1780. static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
  1781. {
  1782. if (dev->get_stats) {
  1783. struct net_device_stats *stats = dev->get_stats(dev);
  1784. seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
  1785. "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
  1786. dev->name, stats->rx_bytes, stats->rx_packets,
  1787. stats->rx_errors,
  1788. stats->rx_dropped + stats->rx_missed_errors,
  1789. stats->rx_fifo_errors,
  1790. stats->rx_length_errors + stats->rx_over_errors +
  1791. stats->rx_crc_errors + stats->rx_frame_errors,
  1792. stats->rx_compressed, stats->multicast,
  1793. stats->tx_bytes, stats->tx_packets,
  1794. stats->tx_errors, stats->tx_dropped,
  1795. stats->tx_fifo_errors, stats->collisions,
  1796. stats->tx_carrier_errors +
  1797. stats->tx_aborted_errors +
  1798. stats->tx_window_errors +
  1799. stats->tx_heartbeat_errors,
  1800. stats->tx_compressed);
  1801. } else
  1802. seq_printf(seq, "%6s: No statistics available.\n", dev->name);
  1803. }
  1804. /*
  1805. * Called from the PROCfs module. This now uses the new arbitrary sized
  1806. * /proc/net interface to create /proc/net/dev
  1807. */
  1808. static int dev_seq_show(struct seq_file *seq, void *v)
  1809. {
  1810. if (v == SEQ_START_TOKEN)
  1811. seq_puts(seq, "Inter-| Receive "
  1812. " | Transmit\n"
  1813. " face |bytes packets errs drop fifo frame "
  1814. "compressed multicast|bytes packets errs "
  1815. "drop fifo colls carrier compressed\n");
  1816. else
  1817. dev_seq_printf_stats(seq, v);
  1818. return 0;
  1819. }
  1820. static struct netif_rx_stats *softnet_get_online(loff_t *pos)
  1821. {
  1822. struct netif_rx_stats *rc = NULL;
  1823. while (*pos < NR_CPUS)
  1824. if (cpu_online(*pos)) {
  1825. rc = &per_cpu(netdev_rx_stat, *pos);
  1826. break;
  1827. } else
  1828. ++*pos;
  1829. return rc;
  1830. }
  1831. static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
  1832. {
  1833. return softnet_get_online(pos);
  1834. }
  1835. static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1836. {
  1837. ++*pos;
  1838. return softnet_get_online(pos);
  1839. }
  1840. static void softnet_seq_stop(struct seq_file *seq, void *v)
  1841. {
  1842. }
  1843. static int softnet_seq_show(struct seq_file *seq, void *v)
  1844. {
  1845. struct netif_rx_stats *s = v;
  1846. seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
  1847. s->total, s->dropped, s->time_squeeze, 0,
  1848. 0, 0, 0, 0, /* was fastroute */
  1849. s->cpu_collision );
  1850. return 0;
  1851. }
  1852. static struct seq_operations dev_seq_ops = {
  1853. .start = dev_seq_start,
  1854. .next = dev_seq_next,
  1855. .stop = dev_seq_stop,
  1856. .show = dev_seq_show,
  1857. };
  1858. static int dev_seq_open(struct inode *inode, struct file *file)
  1859. {
  1860. return seq_open(file, &dev_seq_ops);
  1861. }
  1862. static struct file_operations dev_seq_fops = {
  1863. .owner = THIS_MODULE,
  1864. .open = dev_seq_open,
  1865. .read = seq_read,
  1866. .llseek = seq_lseek,
  1867. .release = seq_release,
  1868. };
  1869. static struct seq_operations softnet_seq_ops = {
  1870. .start = softnet_seq_start,
  1871. .next = softnet_seq_next,
  1872. .stop = softnet_seq_stop,
  1873. .show = softnet_seq_show,
  1874. };
  1875. static int softnet_seq_open(struct inode *inode, struct file *file)
  1876. {
  1877. return seq_open(file, &softnet_seq_ops);
  1878. }
  1879. static struct file_operations softnet_seq_fops = {
  1880. .owner = THIS_MODULE,
  1881. .open = softnet_seq_open,
  1882. .read = seq_read,
  1883. .llseek = seq_lseek,
  1884. .release = seq_release,
  1885. };
  1886. #ifdef CONFIG_WIRELESS_EXT
  1887. extern int wireless_proc_init(void);
  1888. #else
  1889. #define wireless_proc_init() 0
  1890. #endif
  1891. static int __init dev_proc_init(void)
  1892. {
  1893. int rc = -ENOMEM;
  1894. if (!proc_net_fops_create("dev", S_IRUGO, &dev_seq_fops))
  1895. goto out;
  1896. if (!proc_net_fops_create("softnet_stat", S_IRUGO, &softnet_seq_fops))
  1897. goto out_dev;
  1898. if (wireless_proc_init())
  1899. goto out_softnet;
  1900. rc = 0;
  1901. out:
  1902. return rc;
  1903. out_softnet:
  1904. proc_net_remove("softnet_stat");
  1905. out_dev:
  1906. proc_net_remove("dev");
  1907. goto out;
  1908. }
  1909. #else
  1910. #define dev_proc_init() 0
  1911. #endif /* CONFIG_PROC_FS */
  1912. /**
  1913. * netdev_set_master - set up master/slave pair
  1914. * @slave: slave device
  1915. * @master: new master device
  1916. *
  1917. * Changes the master device of the slave. Pass %NULL to break the
  1918. * bonding. The caller must hold the RTNL semaphore. On a failure
  1919. * a negative errno code is returned. On success the reference counts
  1920. * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
  1921. * function returns zero.
  1922. */
  1923. int netdev_set_master(struct net_device *slave, struct net_device *master)
  1924. {
  1925. struct net_device *old = slave->master;
  1926. ASSERT_RTNL();
  1927. if (master) {
  1928. if (old)
  1929. return -EBUSY;
  1930. dev_hold(master);
  1931. }
  1932. slave->master = master;
  1933. synchronize_net();
  1934. if (old)
  1935. dev_put(old);
  1936. if (master)
  1937. slave->flags |= IFF_SLAVE;
  1938. else
  1939. slave->flags &= ~IFF_SLAVE;
  1940. rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
  1941. return 0;
  1942. }
  1943. /**
  1944. * dev_set_promiscuity - update promiscuity count on a device
  1945. * @dev: device
  1946. * @inc: modifier
  1947. *
  1948. * Add or remove promiscuity from a device. While the count in the device
  1949. * remains above zero the interface remains promiscuous. Once it hits zero
  1950. * the device reverts back to normal filtering operation. A negative inc
  1951. * value is used to drop promiscuity on the device.
  1952. */
  1953. void dev_set_promiscuity(struct net_device *dev, int inc)
  1954. {
  1955. unsigned short old_flags = dev->flags;
  1956. if ((dev->promiscuity += inc) == 0)
  1957. dev->flags &= ~IFF_PROMISC;
  1958. else
  1959. dev->flags |= IFF_PROMISC;
  1960. if (dev->flags != old_flags) {
  1961. dev_mc_upload(dev);
  1962. printk(KERN_INFO "device %s %s promiscuous mode\n",
  1963. dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
  1964. "left");
  1965. audit_log(current->audit_context, GFP_ATOMIC,
  1966. AUDIT_ANOM_PROMISCUOUS,
  1967. "dev=%s prom=%d old_prom=%d auid=%u",
  1968. dev->name, (dev->flags & IFF_PROMISC),
  1969. (old_flags & IFF_PROMISC),
  1970. audit_get_loginuid(current->audit_context));
  1971. }
  1972. }
  1973. /**
  1974. * dev_set_allmulti - update allmulti count on a device
  1975. * @dev: device
  1976. * @inc: modifier
  1977. *
  1978. * Add or remove reception of all multicast frames to a device. While the
  1979. * count in the device remains above zero the interface remains listening
  1980. * to all interfaces. Once it hits zero the device reverts back to normal
  1981. * filtering operation. A negative @inc value is used to drop the counter
  1982. * when releasing a resource needing all multicasts.
  1983. */
  1984. void dev_set_allmulti(struct net_device *dev, int inc)
  1985. {
  1986. unsigned short old_flags = dev->flags;
  1987. dev->flags |= IFF_ALLMULTI;
  1988. if ((dev->allmulti += inc) == 0)
  1989. dev->flags &= ~IFF_ALLMULTI;
  1990. if (dev->flags ^ old_flags)
  1991. dev_mc_upload(dev);
  1992. }
  1993. unsigned dev_get_flags(const struct net_device *dev)
  1994. {
  1995. unsigned flags;
  1996. flags = (dev->flags & ~(IFF_PROMISC |
  1997. IFF_ALLMULTI |
  1998. IFF_RUNNING |
  1999. IFF_LOWER_UP |
  2000. IFF_DORMANT)) |
  2001. (dev->gflags & (IFF_PROMISC |
  2002. IFF_ALLMULTI));
  2003. if (netif_running(dev)) {
  2004. if (netif_oper_up(dev))
  2005. flags |= IFF_RUNNING;
  2006. if (netif_carrier_ok(dev))
  2007. flags |= IFF_LOWER_UP;
  2008. if (netif_dormant(dev))
  2009. flags |= IFF_DORMANT;
  2010. }
  2011. return flags;
  2012. }
  2013. int dev_change_flags(struct net_device *dev, unsigned flags)
  2014. {
  2015. int ret;
  2016. int old_flags = dev->flags;
  2017. /*
  2018. * Set the flags on our device.
  2019. */
  2020. dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
  2021. IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
  2022. IFF_AUTOMEDIA)) |
  2023. (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
  2024. IFF_ALLMULTI));
  2025. /*
  2026. * Load in the correct multicast list now the flags have changed.
  2027. */
  2028. dev_mc_upload(dev);
  2029. /*
  2030. * Have we downed the interface. We handle IFF_UP ourselves
  2031. * according to user attempts to set it, rather than blindly
  2032. * setting it.
  2033. */
  2034. ret = 0;
  2035. if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
  2036. ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
  2037. if (!ret)
  2038. dev_mc_upload(dev);
  2039. }
  2040. if (dev->flags & IFF_UP &&
  2041. ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
  2042. IFF_VOLATILE)))
  2043. raw_notifier_call_chain(&netdev_chain,
  2044. NETDEV_CHANGE, dev);
  2045. if ((flags ^ dev->gflags) & IFF_PROMISC) {
  2046. int inc = (flags & IFF_PROMISC) ? +1 : -1;
  2047. dev->gflags ^= IFF_PROMISC;
  2048. dev_set_promiscuity(dev, inc);
  2049. }
  2050. /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
  2051. is important. Some (broken) drivers set IFF_PROMISC, when
  2052. IFF_ALLMULTI is requested not asking us and not reporting.
  2053. */
  2054. if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
  2055. int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
  2056. dev->gflags ^= IFF_ALLMULTI;
  2057. dev_set_allmulti(dev, inc);
  2058. }
  2059. if (old_flags ^ dev->flags)
  2060. rtmsg_ifinfo(RTM_NEWLINK, dev, old_flags ^ dev->flags);
  2061. return ret;
  2062. }
  2063. int dev_set_mtu(struct net_device *dev, int new_mtu)
  2064. {
  2065. int err;
  2066. if (new_mtu == dev->mtu)
  2067. return 0;
  2068. /* MTU must be positive. */
  2069. if (new_mtu < 0)
  2070. return -EINVAL;
  2071. if (!netif_device_present(dev))
  2072. return -ENODEV;
  2073. err = 0;
  2074. if (dev->change_mtu)
  2075. err = dev->change_mtu(dev, new_mtu);
  2076. else
  2077. dev->mtu = new_mtu;
  2078. if (!err && dev->flags & IFF_UP)
  2079. raw_notifier_call_chain(&netdev_chain,
  2080. NETDEV_CHANGEMTU, dev);
  2081. return err;
  2082. }
  2083. int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
  2084. {
  2085. int err;
  2086. if (!dev->set_mac_address)
  2087. return -EOPNOTSUPP;
  2088. if (sa->sa_family != dev->type)
  2089. return -EINVAL;
  2090. if (!netif_device_present(dev))
  2091. return -ENODEV;
  2092. err = dev->set_mac_address(dev, sa);
  2093. if (!err)
  2094. raw_notifier_call_chain(&netdev_chain,
  2095. NETDEV_CHANGEADDR, dev);
  2096. return err;
  2097. }
  2098. /*
  2099. * Perform the SIOCxIFxxx calls.
  2100. */
  2101. static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
  2102. {
  2103. int err;
  2104. struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
  2105. if (!dev)
  2106. return -ENODEV;
  2107. switch (cmd) {
  2108. case SIOCGIFFLAGS: /* Get interface flags */
  2109. ifr->ifr_flags = dev_get_flags(dev);
  2110. return 0;
  2111. case SIOCSIFFLAGS: /* Set interface flags */
  2112. return dev_change_flags(dev, ifr->ifr_flags);
  2113. case SIOCGIFMETRIC: /* Get the metric on the interface
  2114. (currently unused) */
  2115. ifr->ifr_metric = 0;
  2116. return 0;
  2117. case SIOCSIFMETRIC: /* Set the metric on the interface
  2118. (currently unused) */
  2119. return -EOPNOTSUPP;
  2120. case SIOCGIFMTU: /* Get the MTU of a device */
  2121. ifr->ifr_mtu = dev->mtu;
  2122. return 0;
  2123. case SIOCSIFMTU: /* Set the MTU of a device */
  2124. return dev_set_mtu(dev, ifr->ifr_mtu);
  2125. case SIOCGIFHWADDR:
  2126. if (!dev->addr_len)
  2127. memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
  2128. else
  2129. memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
  2130. min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
  2131. ifr->ifr_hwaddr.sa_family = dev->type;
  2132. return 0;
  2133. case SIOCSIFHWADDR:
  2134. return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
  2135. case SIOCSIFHWBROADCAST:
  2136. if (ifr->ifr_hwaddr.sa_family != dev->type)
  2137. return -EINVAL;
  2138. memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
  2139. min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
  2140. raw_notifier_call_chain(&netdev_chain,
  2141. NETDEV_CHANGEADDR, dev);
  2142. return 0;
  2143. case SIOCGIFMAP:
  2144. ifr->ifr_map.mem_start = dev->mem_start;
  2145. ifr->ifr_map.mem_end = dev->mem_end;
  2146. ifr->ifr_map.base_addr = dev->base_addr;
  2147. ifr->ifr_map.irq = dev->irq;
  2148. ifr->ifr_map.dma = dev->dma;
  2149. ifr->ifr_map.port = dev->if_port;
  2150. return 0;
  2151. case SIOCSIFMAP:
  2152. if (dev->set_config) {
  2153. if (!netif_device_present(dev))
  2154. return -ENODEV;
  2155. return dev->set_config(dev, &ifr->ifr_map);
  2156. }
  2157. return -EOPNOTSUPP;
  2158. case SIOCADDMULTI:
  2159. if (!dev->set_multicast_list ||
  2160. ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
  2161. return -EINVAL;
  2162. if (!netif_device_present(dev))
  2163. return -ENODEV;
  2164. return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
  2165. dev->addr_len, 1);
  2166. case SIOCDELMULTI:
  2167. if (!dev->set_multicast_list ||
  2168. ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
  2169. return -EINVAL;
  2170. if (!netif_device_present(dev))
  2171. return -ENODEV;
  2172. return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
  2173. dev->addr_len, 1);
  2174. case SIOCGIFINDEX:
  2175. ifr->ifr_ifindex = dev->ifindex;
  2176. return 0;
  2177. case SIOCGIFTXQLEN:
  2178. ifr->ifr_qlen = dev->tx_queue_len;
  2179. return 0;
  2180. case SIOCSIFTXQLEN:
  2181. if (ifr->ifr_qlen < 0)
  2182. return -EINVAL;
  2183. dev->tx_queue_len = ifr->ifr_qlen;
  2184. return 0;
  2185. case SIOCSIFNAME:
  2186. ifr->ifr_newname[IFNAMSIZ-1] = '\0';
  2187. return dev_change_name(dev, ifr->ifr_newname);
  2188. /*
  2189. * Unknown or private ioctl
  2190. */
  2191. default:
  2192. if ((cmd >= SIOCDEVPRIVATE &&
  2193. cmd <= SIOCDEVPRIVATE + 15) ||
  2194. cmd == SIOCBONDENSLAVE ||
  2195. cmd == SIOCBONDRELEASE ||
  2196. cmd == SIOCBONDSETHWADDR ||
  2197. cmd == SIOCBONDSLAVEINFOQUERY ||
  2198. cmd == SIOCBONDINFOQUERY ||
  2199. cmd == SIOCBONDCHANGEACTIVE ||
  2200. cmd == SIOCGMIIPHY ||
  2201. cmd == SIOCGMIIREG ||
  2202. cmd == SIOCSMIIREG ||
  2203. cmd == SIOCBRADDIF ||
  2204. cmd == SIOCBRDELIF ||
  2205. cmd == SIOCWANDEV) {
  2206. err = -EOPNOTSUPP;
  2207. if (dev->do_ioctl) {
  2208. if (netif_device_present(dev))
  2209. err = dev->do_ioctl(dev, ifr,
  2210. cmd);
  2211. else
  2212. err = -ENODEV;
  2213. }
  2214. } else
  2215. err = -EINVAL;
  2216. }
  2217. return err;
  2218. }
  2219. /*
  2220. * This function handles all "interface"-type I/O control requests. The actual
  2221. * 'doing' part of this is dev_ifsioc above.
  2222. */
  2223. /**
  2224. * dev_ioctl - network device ioctl
  2225. * @cmd: command to issue
  2226. * @arg: pointer to a struct ifreq in user space
  2227. *
  2228. * Issue ioctl functions to devices. This is normally called by the
  2229. * user space syscall interfaces but can sometimes be useful for
  2230. * other purposes. The return value is the return from the syscall if
  2231. * positive or a negative errno code on error.
  2232. */
  2233. int dev_ioctl(unsigned int cmd, void __user *arg)
  2234. {
  2235. struct ifreq ifr;
  2236. int ret;
  2237. char *colon;
  2238. /* One special case: SIOCGIFCONF takes ifconf argument
  2239. and requires shared lock, because it sleeps writing
  2240. to user space.
  2241. */
  2242. if (cmd == SIOCGIFCONF) {
  2243. rtnl_lock();
  2244. ret = dev_ifconf((char __user *) arg);
  2245. rtnl_unlock();
  2246. return ret;
  2247. }
  2248. if (cmd == SIOCGIFNAME)
  2249. return dev_ifname((struct ifreq __user *)arg);
  2250. if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
  2251. return -EFAULT;
  2252. ifr.ifr_name[IFNAMSIZ-1] = 0;
  2253. colon = strchr(ifr.ifr_name, ':');
  2254. if (colon)
  2255. *colon = 0;
  2256. /*
  2257. * See which interface the caller is talking about.
  2258. */
  2259. switch (cmd) {
  2260. /*
  2261. * These ioctl calls:
  2262. * - can be done by all.
  2263. * - atomic and do not require locking.
  2264. * - return a value
  2265. */
  2266. case SIOCGIFFLAGS:
  2267. case SIOCGIFMETRIC:
  2268. case SIOCGIFMTU:
  2269. case SIOCGIFHWADDR:
  2270. case SIOCGIFSLAVE:
  2271. case SIOCGIFMAP:
  2272. case SIOCGIFINDEX:
  2273. case SIOCGIFTXQLEN:
  2274. dev_load(ifr.ifr_name);
  2275. read_lock(&dev_base_lock);
  2276. ret = dev_ifsioc(&ifr, cmd);
  2277. read_unlock(&dev_base_lock);
  2278. if (!ret) {
  2279. if (colon)
  2280. *colon = ':';
  2281. if (copy_to_user(arg, &ifr,
  2282. sizeof(struct ifreq)))
  2283. ret = -EFAULT;
  2284. }
  2285. return ret;
  2286. case SIOCETHTOOL:
  2287. dev_load(ifr.ifr_name);
  2288. rtnl_lock();
  2289. ret = dev_ethtool(&ifr);
  2290. rtnl_unlock();
  2291. if (!ret) {
  2292. if (colon)
  2293. *colon = ':';
  2294. if (copy_to_user(arg, &ifr,
  2295. sizeof(struct ifreq)))
  2296. ret = -EFAULT;
  2297. }
  2298. return ret;
  2299. /*
  2300. * These ioctl calls:
  2301. * - require superuser power.
  2302. * - require strict serialization.
  2303. * - return a value
  2304. */
  2305. case SIOCGMIIPHY:
  2306. case SIOCGMIIREG:
  2307. case SIOCSIFNAME:
  2308. if (!capable(CAP_NET_ADMIN))
  2309. return -EPERM;
  2310. dev_load(ifr.ifr_name);
  2311. rtnl_lock();
  2312. ret = dev_ifsioc(&ifr, cmd);
  2313. rtnl_unlock();
  2314. if (!ret) {
  2315. if (colon)
  2316. *colon = ':';
  2317. if (copy_to_user(arg, &ifr,
  2318. sizeof(struct ifreq)))
  2319. ret = -EFAULT;
  2320. }
  2321. return ret;
  2322. /*
  2323. * These ioctl calls:
  2324. * - require superuser power.
  2325. * - require strict serialization.
  2326. * - do not return a value
  2327. */
  2328. case SIOCSIFFLAGS:
  2329. case SIOCSIFMETRIC:
  2330. case SIOCSIFMTU:
  2331. case SIOCSIFMAP:
  2332. case SIOCSIFHWADDR:
  2333. case SIOCSIFSLAVE:
  2334. case SIOCADDMULTI:
  2335. case SIOCDELMULTI:
  2336. case SIOCSIFHWBROADCAST:
  2337. case SIOCSIFTXQLEN:
  2338. case SIOCSMIIREG:
  2339. case SIOCBONDENSLAVE:
  2340. case SIOCBONDRELEASE:
  2341. case SIOCBONDSETHWADDR:
  2342. case SIOCBONDCHANGEACTIVE:
  2343. case SIOCBRADDIF:
  2344. case SIOCBRDELIF:
  2345. if (!capable(CAP_NET_ADMIN))
  2346. return -EPERM;
  2347. /* fall through */
  2348. case SIOCBONDSLAVEINFOQUERY:
  2349. case SIOCBONDINFOQUERY:
  2350. dev_load(ifr.ifr_name);
  2351. rtnl_lock();
  2352. ret = dev_ifsioc(&ifr, cmd);
  2353. rtnl_unlock();
  2354. return ret;
  2355. case SIOCGIFMEM:
  2356. /* Get the per device memory space. We can add this but
  2357. * currently do not support it */
  2358. case SIOCSIFMEM:
  2359. /* Set the per device memory buffer space.
  2360. * Not applicable in our case */
  2361. case SIOCSIFLINK:
  2362. return -EINVAL;
  2363. /*
  2364. * Unknown or private ioctl.
  2365. */
  2366. default:
  2367. if (cmd == SIOCWANDEV ||
  2368. (cmd >= SIOCDEVPRIVATE &&
  2369. cmd <= SIOCDEVPRIVATE + 15)) {
  2370. dev_load(ifr.ifr_name);
  2371. rtnl_lock();
  2372. ret = dev_ifsioc(&ifr, cmd);
  2373. rtnl_unlock();
  2374. if (!ret && copy_to_user(arg, &ifr,
  2375. sizeof(struct ifreq)))
  2376. ret = -EFAULT;
  2377. return ret;
  2378. }
  2379. #ifdef CONFIG_WIRELESS_EXT
  2380. /* Take care of Wireless Extensions */
  2381. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
  2382. /* If command is `set a parameter', or
  2383. * `get the encoding parameters', check if
  2384. * the user has the right to do it */
  2385. if (IW_IS_SET(cmd) || cmd == SIOCGIWENCODE
  2386. || cmd == SIOCGIWENCODEEXT) {
  2387. if (!capable(CAP_NET_ADMIN))
  2388. return -EPERM;
  2389. }
  2390. dev_load(ifr.ifr_name);
  2391. rtnl_lock();
  2392. /* Follow me in net/core/wireless.c */
  2393. ret = wireless_process_ioctl(&ifr, cmd);
  2394. rtnl_unlock();
  2395. if (IW_IS_GET(cmd) &&
  2396. copy_to_user(arg, &ifr,
  2397. sizeof(struct ifreq)))
  2398. ret = -EFAULT;
  2399. return ret;
  2400. }
  2401. #endif /* CONFIG_WIRELESS_EXT */
  2402. return -EINVAL;
  2403. }
  2404. }
  2405. /**
  2406. * dev_new_index - allocate an ifindex
  2407. *
  2408. * Returns a suitable unique value for a new device interface
  2409. * number. The caller must hold the rtnl semaphore or the
  2410. * dev_base_lock to be sure it remains unique.
  2411. */
  2412. static int dev_new_index(void)
  2413. {
  2414. static int ifindex;
  2415. for (;;) {
  2416. if (++ifindex <= 0)
  2417. ifindex = 1;
  2418. if (!__dev_get_by_index(ifindex))
  2419. return ifindex;
  2420. }
  2421. }
  2422. static int dev_boot_phase = 1;
  2423. /* Delayed registration/unregisteration */
  2424. static DEFINE_SPINLOCK(net_todo_list_lock);
  2425. static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list);
  2426. static inline void net_set_todo(struct net_device *dev)
  2427. {
  2428. spin_lock(&net_todo_list_lock);
  2429. list_add_tail(&dev->todo_list, &net_todo_list);
  2430. spin_unlock(&net_todo_list_lock);
  2431. }
  2432. /**
  2433. * register_netdevice - register a network device
  2434. * @dev: device to register
  2435. *
  2436. * Take a completed network device structure and add it to the kernel
  2437. * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
  2438. * chain. 0 is returned on success. A negative errno code is returned
  2439. * on a failure to set up the device, or if the name is a duplicate.
  2440. *
  2441. * Callers must hold the rtnl semaphore. You may want
  2442. * register_netdev() instead of this.
  2443. *
  2444. * BUGS:
  2445. * The locking appears insufficient to guarantee two parallel registers
  2446. * will not get the same name.
  2447. */
  2448. int register_netdevice(struct net_device *dev)
  2449. {
  2450. struct hlist_head *head;
  2451. struct hlist_node *p;
  2452. int ret;
  2453. BUG_ON(dev_boot_phase);
  2454. ASSERT_RTNL();
  2455. might_sleep();
  2456. /* When net_device's are persistent, this will be fatal. */
  2457. BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
  2458. spin_lock_init(&dev->queue_lock);
  2459. spin_lock_init(&dev->_xmit_lock);
  2460. dev->xmit_lock_owner = -1;
  2461. #ifdef CONFIG_NET_CLS_ACT
  2462. spin_lock_init(&dev->ingress_lock);
  2463. #endif
  2464. ret = alloc_divert_blk(dev);
  2465. if (ret)
  2466. goto out;
  2467. dev->iflink = -1;
  2468. /* Init, if this function is available */
  2469. if (dev->init) {
  2470. ret = dev->init(dev);
  2471. if (ret) {
  2472. if (ret > 0)
  2473. ret = -EIO;
  2474. goto out_err;
  2475. }
  2476. }
  2477. if (!dev_valid_name(dev->name)) {
  2478. ret = -EINVAL;
  2479. goto out_err;
  2480. }
  2481. dev->ifindex = dev_new_index();
  2482. if (dev->iflink == -1)
  2483. dev->iflink = dev->ifindex;
  2484. /* Check for existence of name */
  2485. head = dev_name_hash(dev->name);
  2486. hlist_for_each(p, head) {
  2487. struct net_device *d
  2488. = hlist_entry(p, struct net_device, name_hlist);
  2489. if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
  2490. ret = -EEXIST;
  2491. goto out_err;
  2492. }
  2493. }
  2494. /* Fix illegal SG+CSUM combinations. */
  2495. if ((dev->features & NETIF_F_SG) &&
  2496. !(dev->features & NETIF_F_ALL_CSUM)) {
  2497. printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no checksum feature.\n",
  2498. dev->name);
  2499. dev->features &= ~NETIF_F_SG;
  2500. }
  2501. /* TSO requires that SG is present as well. */
  2502. if ((dev->features & NETIF_F_TSO) &&
  2503. !(dev->features & NETIF_F_SG)) {
  2504. printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no SG feature.\n",
  2505. dev->name);
  2506. dev->features &= ~NETIF_F_TSO;
  2507. }
  2508. if (dev->features & NETIF_F_UFO) {
  2509. if (!(dev->features & NETIF_F_HW_CSUM)) {
  2510. printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
  2511. "NETIF_F_HW_CSUM feature.\n",
  2512. dev->name);
  2513. dev->features &= ~NETIF_F_UFO;
  2514. }
  2515. if (!(dev->features & NETIF_F_SG)) {
  2516. printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
  2517. "NETIF_F_SG feature.\n",
  2518. dev->name);
  2519. dev->features &= ~NETIF_F_UFO;
  2520. }
  2521. }
  2522. /*
  2523. * nil rebuild_header routine,
  2524. * that should be never called and used as just bug trap.
  2525. */
  2526. if (!dev->rebuild_header)
  2527. dev->rebuild_header = default_rebuild_header;
  2528. ret = netdev_register_sysfs(dev);
  2529. if (ret)
  2530. goto out_err;
  2531. dev->reg_state = NETREG_REGISTERED;
  2532. /*
  2533. * Default initial state at registry is that the
  2534. * device is present.
  2535. */
  2536. set_bit(__LINK_STATE_PRESENT, &dev->state);
  2537. dev->next = NULL;
  2538. dev_init_scheduler(dev);
  2539. write_lock_bh(&dev_base_lock);
  2540. *dev_tail = dev;
  2541. dev_tail = &dev->next;
  2542. hlist_add_head(&dev->name_hlist, head);
  2543. hlist_add_head(&dev->index_hlist, dev_index_hash(dev->ifindex));
  2544. dev_hold(dev);
  2545. write_unlock_bh(&dev_base_lock);
  2546. /* Notify protocols, that a new device appeared. */
  2547. raw_notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
  2548. ret = 0;
  2549. out:
  2550. return ret;
  2551. out_err:
  2552. free_divert_blk(dev);
  2553. goto out;
  2554. }
  2555. /**
  2556. * register_netdev - register a network device
  2557. * @dev: device to register
  2558. *
  2559. * Take a completed network device structure and add it to the kernel
  2560. * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
  2561. * chain. 0 is returned on success. A negative errno code is returned
  2562. * on a failure to set up the device, or if the name is a duplicate.
  2563. *
  2564. * This is a wrapper around register_netdev that takes the rtnl semaphore
  2565. * and expands the device name if you passed a format string to
  2566. * alloc_netdev.
  2567. */
  2568. int register_netdev(struct net_device *dev)
  2569. {
  2570. int err;
  2571. rtnl_lock();
  2572. /*
  2573. * If the name is a format string the caller wants us to do a
  2574. * name allocation.
  2575. */
  2576. if (strchr(dev->name, '%')) {
  2577. err = dev_alloc_name(dev, dev->name);
  2578. if (err < 0)
  2579. goto out;
  2580. }
  2581. /*
  2582. * Back compatibility hook. Kill this one in 2.5
  2583. */
  2584. if (dev->name[0] == 0 || dev->name[0] == ' ') {
  2585. err = dev_alloc_name(dev, "eth%d");
  2586. if (err < 0)
  2587. goto out;
  2588. }
  2589. err = register_netdevice(dev);
  2590. out:
  2591. rtnl_unlock();
  2592. return err;
  2593. }
  2594. EXPORT_SYMBOL(register_netdev);
  2595. /*
  2596. * netdev_wait_allrefs - wait until all references are gone.
  2597. *
  2598. * This is called when unregistering network devices.
  2599. *
  2600. * Any protocol or device that holds a reference should register
  2601. * for netdevice notification, and cleanup and put back the
  2602. * reference if they receive an UNREGISTER event.
  2603. * We can get stuck here if buggy protocols don't correctly
  2604. * call dev_put.
  2605. */
  2606. static void netdev_wait_allrefs(struct net_device *dev)
  2607. {
  2608. unsigned long rebroadcast_time, warning_time;
  2609. rebroadcast_time = warning_time = jiffies;
  2610. while (atomic_read(&dev->refcnt) != 0) {
  2611. if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
  2612. rtnl_lock();
  2613. /* Rebroadcast unregister notification */
  2614. raw_notifier_call_chain(&netdev_chain,
  2615. NETDEV_UNREGISTER, dev);
  2616. if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
  2617. &dev->state)) {
  2618. /* We must not have linkwatch events
  2619. * pending on unregister. If this
  2620. * happens, we simply run the queue
  2621. * unscheduled, resulting in a noop
  2622. * for this device.
  2623. */
  2624. linkwatch_run_queue();
  2625. }
  2626. __rtnl_unlock();
  2627. rebroadcast_time = jiffies;
  2628. }
  2629. msleep(250);
  2630. if (time_after(jiffies, warning_time + 10 * HZ)) {
  2631. printk(KERN_EMERG "unregister_netdevice: "
  2632. "waiting for %s to become free. Usage "
  2633. "count = %d\n",
  2634. dev->name, atomic_read(&dev->refcnt));
  2635. warning_time = jiffies;
  2636. }
  2637. }
  2638. }
  2639. /* The sequence is:
  2640. *
  2641. * rtnl_lock();
  2642. * ...
  2643. * register_netdevice(x1);
  2644. * register_netdevice(x2);
  2645. * ...
  2646. * unregister_netdevice(y1);
  2647. * unregister_netdevice(y2);
  2648. * ...
  2649. * rtnl_unlock();
  2650. * free_netdev(y1);
  2651. * free_netdev(y2);
  2652. *
  2653. * We are invoked by rtnl_unlock() after it drops the semaphore.
  2654. * This allows us to deal with problems:
  2655. * 1) We can delete sysfs objects which invoke hotplug
  2656. * without deadlocking with linkwatch via keventd.
  2657. * 2) Since we run with the RTNL semaphore not held, we can sleep
  2658. * safely in order to wait for the netdev refcnt to drop to zero.
  2659. */
  2660. static DEFINE_MUTEX(net_todo_run_mutex);
  2661. void netdev_run_todo(void)
  2662. {
  2663. struct list_head list;
  2664. /* Need to guard against multiple cpu's getting out of order. */
  2665. mutex_lock(&net_todo_run_mutex);
  2666. /* Not safe to do outside the semaphore. We must not return
  2667. * until all unregister events invoked by the local processor
  2668. * have been completed (either by this todo run, or one on
  2669. * another cpu).
  2670. */
  2671. if (list_empty(&net_todo_list))
  2672. goto out;
  2673. /* Snapshot list, allow later requests */
  2674. spin_lock(&net_todo_list_lock);
  2675. list_replace_init(&net_todo_list, &list);
  2676. spin_unlock(&net_todo_list_lock);
  2677. while (!list_empty(&list)) {
  2678. struct net_device *dev
  2679. = list_entry(list.next, struct net_device, todo_list);
  2680. list_del(&dev->todo_list);
  2681. if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
  2682. printk(KERN_ERR "network todo '%s' but state %d\n",
  2683. dev->name, dev->reg_state);
  2684. dump_stack();
  2685. continue;
  2686. }
  2687. netdev_unregister_sysfs(dev);
  2688. dev->reg_state = NETREG_UNREGISTERED;
  2689. netdev_wait_allrefs(dev);
  2690. /* paranoia */
  2691. BUG_ON(atomic_read(&dev->refcnt));
  2692. BUG_TRAP(!dev->ip_ptr);
  2693. BUG_TRAP(!dev->ip6_ptr);
  2694. BUG_TRAP(!dev->dn_ptr);
  2695. /* It must be the very last action,
  2696. * after this 'dev' may point to freed up memory.
  2697. */
  2698. if (dev->destructor)
  2699. dev->destructor(dev);
  2700. }
  2701. out:
  2702. mutex_unlock(&net_todo_run_mutex);
  2703. }
  2704. /**
  2705. * alloc_netdev - allocate network device
  2706. * @sizeof_priv: size of private data to allocate space for
  2707. * @name: device name format string
  2708. * @setup: callback to initialize device
  2709. *
  2710. * Allocates a struct net_device with private data area for driver use
  2711. * and performs basic initialization.
  2712. */
  2713. struct net_device *alloc_netdev(int sizeof_priv, const char *name,
  2714. void (*setup)(struct net_device *))
  2715. {
  2716. void *p;
  2717. struct net_device *dev;
  2718. int alloc_size;
  2719. BUG_ON(strlen(name) >= sizeof(dev->name));
  2720. /* ensure 32-byte alignment of both the device and private area */
  2721. alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST;
  2722. alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
  2723. p = kzalloc(alloc_size, GFP_KERNEL);
  2724. if (!p) {
  2725. printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
  2726. return NULL;
  2727. }
  2728. dev = (struct net_device *)
  2729. (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
  2730. dev->padded = (char *)dev - (char *)p;
  2731. if (sizeof_priv)
  2732. dev->priv = netdev_priv(dev);
  2733. setup(dev);
  2734. strcpy(dev->name, name);
  2735. return dev;
  2736. }
  2737. EXPORT_SYMBOL(alloc_netdev);
  2738. /**
  2739. * free_netdev - free network device
  2740. * @dev: device
  2741. *
  2742. * This function does the last stage of destroying an allocated device
  2743. * interface. The reference to the device object is released.
  2744. * If this is the last reference then it will be freed.
  2745. */
  2746. void free_netdev(struct net_device *dev)
  2747. {
  2748. #ifdef CONFIG_SYSFS
  2749. /* Compatibility with error handling in drivers */
  2750. if (dev->reg_state == NETREG_UNINITIALIZED) {
  2751. kfree((char *)dev - dev->padded);
  2752. return;
  2753. }
  2754. BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
  2755. dev->reg_state = NETREG_RELEASED;
  2756. /* will free via class release */
  2757. class_device_put(&dev->class_dev);
  2758. #else
  2759. kfree((char *)dev - dev->padded);
  2760. #endif
  2761. }
  2762. /* Synchronize with packet receive processing. */
  2763. void synchronize_net(void)
  2764. {
  2765. might_sleep();
  2766. synchronize_rcu();
  2767. }
  2768. /**
  2769. * unregister_netdevice - remove device from the kernel
  2770. * @dev: device
  2771. *
  2772. * This function shuts down a device interface and removes it
  2773. * from the kernel tables. On success 0 is returned, on a failure
  2774. * a negative errno code is returned.
  2775. *
  2776. * Callers must hold the rtnl semaphore. You may want
  2777. * unregister_netdev() instead of this.
  2778. */
  2779. int unregister_netdevice(struct net_device *dev)
  2780. {
  2781. struct net_device *d, **dp;
  2782. BUG_ON(dev_boot_phase);
  2783. ASSERT_RTNL();
  2784. /* Some devices call without registering for initialization unwind. */
  2785. if (dev->reg_state == NETREG_UNINITIALIZED) {
  2786. printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
  2787. "was registered\n", dev->name, dev);
  2788. return -ENODEV;
  2789. }
  2790. BUG_ON(dev->reg_state != NETREG_REGISTERED);
  2791. /* If device is running, close it first. */
  2792. if (dev->flags & IFF_UP)
  2793. dev_close(dev);
  2794. /* And unlink it from device chain. */
  2795. for (dp = &dev_base; (d = *dp) != NULL; dp = &d->next) {
  2796. if (d == dev) {
  2797. write_lock_bh(&dev_base_lock);
  2798. hlist_del(&dev->name_hlist);
  2799. hlist_del(&dev->index_hlist);
  2800. if (dev_tail == &dev->next)
  2801. dev_tail = dp;
  2802. *dp = d->next;
  2803. write_unlock_bh(&dev_base_lock);
  2804. break;
  2805. }
  2806. }
  2807. if (!d) {
  2808. printk(KERN_ERR "unregister net_device: '%s' not found\n",
  2809. dev->name);
  2810. return -ENODEV;
  2811. }
  2812. dev->reg_state = NETREG_UNREGISTERING;
  2813. synchronize_net();
  2814. /* Shutdown queueing discipline. */
  2815. dev_shutdown(dev);
  2816. /* Notify protocols, that we are about to destroy
  2817. this device. They should clean all the things.
  2818. */
  2819. raw_notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
  2820. /*
  2821. * Flush the multicast chain
  2822. */
  2823. dev_mc_discard(dev);
  2824. if (dev->uninit)
  2825. dev->uninit(dev);
  2826. /* Notifier chain MUST detach us from master device. */
  2827. BUG_TRAP(!dev->master);
  2828. free_divert_blk(dev);
  2829. /* Finish processing unregister after unlock */
  2830. net_set_todo(dev);
  2831. synchronize_net();
  2832. dev_put(dev);
  2833. return 0;
  2834. }
  2835. /**
  2836. * unregister_netdev - remove device from the kernel
  2837. * @dev: device
  2838. *
  2839. * This function shuts down a device interface and removes it
  2840. * from the kernel tables. On success 0 is returned, on a failure
  2841. * a negative errno code is returned.
  2842. *
  2843. * This is just a wrapper for unregister_netdevice that takes
  2844. * the rtnl semaphore. In general you want to use this and not
  2845. * unregister_netdevice.
  2846. */
  2847. void unregister_netdev(struct net_device *dev)
  2848. {
  2849. rtnl_lock();
  2850. unregister_netdevice(dev);
  2851. rtnl_unlock();
  2852. }
  2853. EXPORT_SYMBOL(unregister_netdev);
  2854. #ifdef CONFIG_HOTPLUG_CPU
  2855. static int dev_cpu_callback(struct notifier_block *nfb,
  2856. unsigned long action,
  2857. void *ocpu)
  2858. {
  2859. struct sk_buff **list_skb;
  2860. struct net_device **list_net;
  2861. struct sk_buff *skb;
  2862. unsigned int cpu, oldcpu = (unsigned long)ocpu;
  2863. struct softnet_data *sd, *oldsd;
  2864. if (action != CPU_DEAD)
  2865. return NOTIFY_OK;
  2866. local_irq_disable();
  2867. cpu = smp_processor_id();
  2868. sd = &per_cpu(softnet_data, cpu);
  2869. oldsd = &per_cpu(softnet_data, oldcpu);
  2870. /* Find end of our completion_queue. */
  2871. list_skb = &sd->completion_queue;
  2872. while (*list_skb)
  2873. list_skb = &(*list_skb)->next;
  2874. /* Append completion queue from offline CPU. */
  2875. *list_skb = oldsd->completion_queue;
  2876. oldsd->completion_queue = NULL;
  2877. /* Find end of our output_queue. */
  2878. list_net = &sd->output_queue;
  2879. while (*list_net)
  2880. list_net = &(*list_net)->next_sched;
  2881. /* Append output queue from offline CPU. */
  2882. *list_net = oldsd->output_queue;
  2883. oldsd->output_queue = NULL;
  2884. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  2885. local_irq_enable();
  2886. /* Process offline CPU's input_pkt_queue */
  2887. while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
  2888. netif_rx(skb);
  2889. return NOTIFY_OK;
  2890. }
  2891. #endif /* CONFIG_HOTPLUG_CPU */
  2892. #ifdef CONFIG_NET_DMA
  2893. /**
  2894. * net_dma_rebalance -
  2895. * This is called when the number of channels allocated to the net_dma_client
  2896. * changes. The net_dma_client tries to have one DMA channel per CPU.
  2897. */
  2898. static void net_dma_rebalance(void)
  2899. {
  2900. unsigned int cpu, i, n;
  2901. struct dma_chan *chan;
  2902. if (net_dma_count == 0) {
  2903. for_each_online_cpu(cpu)
  2904. rcu_assign_pointer(per_cpu(softnet_data, cpu).net_dma, NULL);
  2905. return;
  2906. }
  2907. i = 0;
  2908. cpu = first_cpu(cpu_online_map);
  2909. rcu_read_lock();
  2910. list_for_each_entry(chan, &net_dma_client->channels, client_node) {
  2911. n = ((num_online_cpus() / net_dma_count)
  2912. + (i < (num_online_cpus() % net_dma_count) ? 1 : 0));
  2913. while(n) {
  2914. per_cpu(softnet_data, cpu).net_dma = chan;
  2915. cpu = next_cpu(cpu, cpu_online_map);
  2916. n--;
  2917. }
  2918. i++;
  2919. }
  2920. rcu_read_unlock();
  2921. }
  2922. /**
  2923. * netdev_dma_event - event callback for the net_dma_client
  2924. * @client: should always be net_dma_client
  2925. * @chan: DMA channel for the event
  2926. * @event: event type
  2927. */
  2928. static void netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
  2929. enum dma_event event)
  2930. {
  2931. spin_lock(&net_dma_event_lock);
  2932. switch (event) {
  2933. case DMA_RESOURCE_ADDED:
  2934. net_dma_count++;
  2935. net_dma_rebalance();
  2936. break;
  2937. case DMA_RESOURCE_REMOVED:
  2938. net_dma_count--;
  2939. net_dma_rebalance();
  2940. break;
  2941. default:
  2942. break;
  2943. }
  2944. spin_unlock(&net_dma_event_lock);
  2945. }
  2946. /**
  2947. * netdev_dma_regiser - register the networking subsystem as a DMA client
  2948. */
  2949. static int __init netdev_dma_register(void)
  2950. {
  2951. spin_lock_init(&net_dma_event_lock);
  2952. net_dma_client = dma_async_client_register(netdev_dma_event);
  2953. if (net_dma_client == NULL)
  2954. return -ENOMEM;
  2955. dma_async_client_chan_request(net_dma_client, num_online_cpus());
  2956. return 0;
  2957. }
  2958. #else
  2959. static int __init netdev_dma_register(void) { return -ENODEV; }
  2960. #endif /* CONFIG_NET_DMA */
  2961. /*
  2962. * Initialize the DEV module. At boot time this walks the device list and
  2963. * unhooks any devices that fail to initialise (normally hardware not
  2964. * present) and leaves us with a valid list of present and active devices.
  2965. *
  2966. */
  2967. /*
  2968. * This is called single threaded during boot, so no need
  2969. * to take the rtnl semaphore.
  2970. */
  2971. static int __init net_dev_init(void)
  2972. {
  2973. int i, rc = -ENOMEM;
  2974. BUG_ON(!dev_boot_phase);
  2975. net_random_init();
  2976. if (dev_proc_init())
  2977. goto out;
  2978. if (netdev_sysfs_init())
  2979. goto out;
  2980. INIT_LIST_HEAD(&ptype_all);
  2981. for (i = 0; i < 16; i++)
  2982. INIT_LIST_HEAD(&ptype_base[i]);
  2983. for (i = 0; i < ARRAY_SIZE(dev_name_head); i++)
  2984. INIT_HLIST_HEAD(&dev_name_head[i]);
  2985. for (i = 0; i < ARRAY_SIZE(dev_index_head); i++)
  2986. INIT_HLIST_HEAD(&dev_index_head[i]);
  2987. /*
  2988. * Initialise the packet receive queues.
  2989. */
  2990. for_each_possible_cpu(i) {
  2991. struct softnet_data *queue;
  2992. queue = &per_cpu(softnet_data, i);
  2993. skb_queue_head_init(&queue->input_pkt_queue);
  2994. queue->completion_queue = NULL;
  2995. INIT_LIST_HEAD(&queue->poll_list);
  2996. set_bit(__LINK_STATE_START, &queue->backlog_dev.state);
  2997. queue->backlog_dev.weight = weight_p;
  2998. queue->backlog_dev.poll = process_backlog;
  2999. atomic_set(&queue->backlog_dev.refcnt, 1);
  3000. }
  3001. netdev_dma_register();
  3002. dev_boot_phase = 0;
  3003. open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
  3004. open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
  3005. hotcpu_notifier(dev_cpu_callback, 0);
  3006. dst_init();
  3007. dev_mcast_init();
  3008. rc = 0;
  3009. out:
  3010. return rc;
  3011. }
  3012. subsys_initcall(net_dev_init);
  3013. EXPORT_SYMBOL(__dev_get_by_index);
  3014. EXPORT_SYMBOL(__dev_get_by_name);
  3015. EXPORT_SYMBOL(__dev_remove_pack);
  3016. EXPORT_SYMBOL(dev_valid_name);
  3017. EXPORT_SYMBOL(dev_add_pack);
  3018. EXPORT_SYMBOL(dev_alloc_name);
  3019. EXPORT_SYMBOL(dev_close);
  3020. EXPORT_SYMBOL(dev_get_by_flags);
  3021. EXPORT_SYMBOL(dev_get_by_index);
  3022. EXPORT_SYMBOL(dev_get_by_name);
  3023. EXPORT_SYMBOL(dev_open);
  3024. EXPORT_SYMBOL(dev_queue_xmit);
  3025. EXPORT_SYMBOL(dev_remove_pack);
  3026. EXPORT_SYMBOL(dev_set_allmulti);
  3027. EXPORT_SYMBOL(dev_set_promiscuity);
  3028. EXPORT_SYMBOL(dev_change_flags);
  3029. EXPORT_SYMBOL(dev_set_mtu);
  3030. EXPORT_SYMBOL(dev_set_mac_address);
  3031. EXPORT_SYMBOL(free_netdev);
  3032. EXPORT_SYMBOL(netdev_boot_setup_check);
  3033. EXPORT_SYMBOL(netdev_set_master);
  3034. EXPORT_SYMBOL(netdev_state_change);
  3035. EXPORT_SYMBOL(netif_receive_skb);
  3036. EXPORT_SYMBOL(netif_rx);
  3037. EXPORT_SYMBOL(register_gifconf);
  3038. EXPORT_SYMBOL(register_netdevice);
  3039. EXPORT_SYMBOL(register_netdevice_notifier);
  3040. EXPORT_SYMBOL(skb_checksum_help);
  3041. EXPORT_SYMBOL(synchronize_net);
  3042. EXPORT_SYMBOL(unregister_netdevice);
  3043. EXPORT_SYMBOL(unregister_netdevice_notifier);
  3044. EXPORT_SYMBOL(net_enable_timestamp);
  3045. EXPORT_SYMBOL(net_disable_timestamp);
  3046. EXPORT_SYMBOL(dev_get_flags);
  3047. #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
  3048. EXPORT_SYMBOL(br_handle_frame_hook);
  3049. EXPORT_SYMBOL(br_fdb_get_hook);
  3050. EXPORT_SYMBOL(br_fdb_put_hook);
  3051. #endif
  3052. #ifdef CONFIG_KMOD
  3053. EXPORT_SYMBOL(dev_load);
  3054. #endif
  3055. EXPORT_PER_CPU_SYMBOL(softnet_data);