dev.c 112 KB

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