dev.c 117 KB

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