dev.c 122 KB

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