dev.c 148 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108
  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/hash.h>
  82. #include <linux/slab.h>
  83. #include <linux/sched.h>
  84. #include <linux/mutex.h>
  85. #include <linux/string.h>
  86. #include <linux/mm.h>
  87. #include <linux/socket.h>
  88. #include <linux/sockios.h>
  89. #include <linux/errno.h>
  90. #include <linux/interrupt.h>
  91. #include <linux/if_ether.h>
  92. #include <linux/netdevice.h>
  93. #include <linux/etherdevice.h>
  94. #include <linux/ethtool.h>
  95. #include <linux/notifier.h>
  96. #include <linux/skbuff.h>
  97. #include <net/net_namespace.h>
  98. #include <net/sock.h>
  99. #include <linux/rtnetlink.h>
  100. #include <linux/proc_fs.h>
  101. #include <linux/seq_file.h>
  102. #include <linux/stat.h>
  103. #include <net/dst.h>
  104. #include <net/pkt_sched.h>
  105. #include <net/checksum.h>
  106. #include <net/xfrm.h>
  107. #include <linux/highmem.h>
  108. #include <linux/init.h>
  109. #include <linux/kmod.h>
  110. #include <linux/module.h>
  111. #include <linux/netpoll.h>
  112. #include <linux/rcupdate.h>
  113. #include <linux/delay.h>
  114. #include <net/wext.h>
  115. #include <net/iw_handler.h>
  116. #include <asm/current.h>
  117. #include <linux/audit.h>
  118. #include <linux/dmaengine.h>
  119. #include <linux/err.h>
  120. #include <linux/ctype.h>
  121. #include <linux/if_arp.h>
  122. #include <linux/if_vlan.h>
  123. #include <linux/ip.h>
  124. #include <net/ip.h>
  125. #include <linux/ipv6.h>
  126. #include <linux/in.h>
  127. #include <linux/jhash.h>
  128. #include <linux/random.h>
  129. #include <trace/events/napi.h>
  130. #include <linux/pci.h>
  131. #include "net-sysfs.h"
  132. /* Instead of increasing this, you should create a hash table. */
  133. #define MAX_GRO_SKBS 8
  134. /* This should be increased if a protocol with a bigger head is added. */
  135. #define GRO_MAX_HEAD (MAX_HEADER + 128)
  136. /*
  137. * The list of packet types we will receive (as opposed to discard)
  138. * and the routines to invoke.
  139. *
  140. * Why 16. Because with 16 the only overlap we get on a hash of the
  141. * low nibble of the protocol value is RARP/SNAP/X.25.
  142. *
  143. * NOTE: That is no longer true with the addition of VLAN tags. Not
  144. * sure which should go first, but I bet it won't make much
  145. * difference if we are running VLANs. The good news is that
  146. * this protocol won't be in the list unless compiled in, so
  147. * the average user (w/out VLANs) will not be adversely affected.
  148. * --BLG
  149. *
  150. * 0800 IP
  151. * 8100 802.1Q VLAN
  152. * 0001 802.3
  153. * 0002 AX.25
  154. * 0004 802.2
  155. * 8035 RARP
  156. * 0005 SNAP
  157. * 0805 X.25
  158. * 0806 ARP
  159. * 8137 IPX
  160. * 0009 Localtalk
  161. * 86DD IPv6
  162. */
  163. #define PTYPE_HASH_SIZE (16)
  164. #define PTYPE_HASH_MASK (PTYPE_HASH_SIZE - 1)
  165. static DEFINE_SPINLOCK(ptype_lock);
  166. static struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
  167. static struct list_head ptype_all __read_mostly; /* Taps */
  168. /*
  169. * The @dev_base_head list is protected by @dev_base_lock and the rtnl
  170. * semaphore.
  171. *
  172. * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
  173. *
  174. * Writers must hold the rtnl semaphore while they loop through the
  175. * dev_base_head list, and hold dev_base_lock for writing when they do the
  176. * actual updates. This allows pure readers to access the list even
  177. * while a writer is preparing to update it.
  178. *
  179. * To put it another way, dev_base_lock is held for writing only to
  180. * protect against pure readers; the rtnl semaphore provides the
  181. * protection against other writers.
  182. *
  183. * See, for example usages, register_netdevice() and
  184. * unregister_netdevice(), which must be called with the rtnl
  185. * semaphore held.
  186. */
  187. DEFINE_RWLOCK(dev_base_lock);
  188. EXPORT_SYMBOL(dev_base_lock);
  189. static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
  190. {
  191. unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
  192. return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
  193. }
  194. static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
  195. {
  196. return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
  197. }
  198. static inline void rps_lock(struct softnet_data *sd)
  199. {
  200. #ifdef CONFIG_RPS
  201. spin_lock(&sd->input_pkt_queue.lock);
  202. #endif
  203. }
  204. static inline void rps_unlock(struct softnet_data *sd)
  205. {
  206. #ifdef CONFIG_RPS
  207. spin_unlock(&sd->input_pkt_queue.lock);
  208. #endif
  209. }
  210. /* Device list insertion */
  211. static int list_netdevice(struct net_device *dev)
  212. {
  213. struct net *net = dev_net(dev);
  214. ASSERT_RTNL();
  215. write_lock_bh(&dev_base_lock);
  216. list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
  217. hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
  218. hlist_add_head_rcu(&dev->index_hlist,
  219. dev_index_hash(net, dev->ifindex));
  220. write_unlock_bh(&dev_base_lock);
  221. return 0;
  222. }
  223. /* Device list removal
  224. * caller must respect a RCU grace period before freeing/reusing dev
  225. */
  226. static void unlist_netdevice(struct net_device *dev)
  227. {
  228. ASSERT_RTNL();
  229. /* Unlink dev from the device chain */
  230. write_lock_bh(&dev_base_lock);
  231. list_del_rcu(&dev->dev_list);
  232. hlist_del_rcu(&dev->name_hlist);
  233. hlist_del_rcu(&dev->index_hlist);
  234. write_unlock_bh(&dev_base_lock);
  235. }
  236. /*
  237. * Our notifier list
  238. */
  239. static RAW_NOTIFIER_HEAD(netdev_chain);
  240. /*
  241. * Device drivers call our routines to queue packets here. We empty the
  242. * queue in the local softnet handler.
  243. */
  244. DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
  245. EXPORT_PER_CPU_SYMBOL(softnet_data);
  246. #ifdef CONFIG_LOCKDEP
  247. /*
  248. * register_netdevice() inits txq->_xmit_lock and sets lockdep class
  249. * according to dev->type
  250. */
  251. static const unsigned short netdev_lock_type[] =
  252. {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
  253. ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
  254. ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
  255. ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
  256. ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
  257. ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
  258. ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
  259. ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
  260. ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
  261. ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
  262. ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
  263. ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
  264. ARPHRD_FCFABRIC, ARPHRD_IEEE802_TR, ARPHRD_IEEE80211,
  265. ARPHRD_IEEE80211_PRISM, ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET,
  266. ARPHRD_PHONET_PIPE, ARPHRD_IEEE802154,
  267. ARPHRD_VOID, ARPHRD_NONE};
  268. static const char *const netdev_lock_name[] =
  269. {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
  270. "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
  271. "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
  272. "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
  273. "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
  274. "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
  275. "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
  276. "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
  277. "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
  278. "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
  279. "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
  280. "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
  281. "_xmit_FCFABRIC", "_xmit_IEEE802_TR", "_xmit_IEEE80211",
  282. "_xmit_IEEE80211_PRISM", "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET",
  283. "_xmit_PHONET_PIPE", "_xmit_IEEE802154",
  284. "_xmit_VOID", "_xmit_NONE"};
  285. static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
  286. static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
  287. static inline unsigned short netdev_lock_pos(unsigned short dev_type)
  288. {
  289. int i;
  290. for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
  291. if (netdev_lock_type[i] == dev_type)
  292. return i;
  293. /* the last key is used by default */
  294. return ARRAY_SIZE(netdev_lock_type) - 1;
  295. }
  296. static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
  297. unsigned short dev_type)
  298. {
  299. int i;
  300. i = netdev_lock_pos(dev_type);
  301. lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
  302. netdev_lock_name[i]);
  303. }
  304. static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
  305. {
  306. int i;
  307. i = netdev_lock_pos(dev->type);
  308. lockdep_set_class_and_name(&dev->addr_list_lock,
  309. &netdev_addr_lock_key[i],
  310. netdev_lock_name[i]);
  311. }
  312. #else
  313. static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
  314. unsigned short dev_type)
  315. {
  316. }
  317. static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
  318. {
  319. }
  320. #endif
  321. /*******************************************************************************
  322. Protocol management and registration routines
  323. *******************************************************************************/
  324. /*
  325. * Add a protocol ID to the list. Now that the input handler is
  326. * smarter we can dispense with all the messy stuff that used to be
  327. * here.
  328. *
  329. * BEWARE!!! Protocol handlers, mangling input packets,
  330. * MUST BE last in hash buckets and checking protocol handlers
  331. * MUST start from promiscuous ptype_all chain in net_bh.
  332. * It is true now, do not change it.
  333. * Explanation follows: if protocol handler, mangling packet, will
  334. * be the first on list, it is not able to sense, that packet
  335. * is cloned and should be copied-on-write, so that it will
  336. * change it and subsequent readers will get broken packet.
  337. * --ANK (980803)
  338. */
  339. static inline struct list_head *ptype_head(const struct packet_type *pt)
  340. {
  341. if (pt->type == htons(ETH_P_ALL))
  342. return &ptype_all;
  343. else
  344. return &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
  345. }
  346. /**
  347. * dev_add_pack - add packet handler
  348. * @pt: packet type declaration
  349. *
  350. * Add a protocol handler to the networking stack. The passed &packet_type
  351. * is linked into kernel lists and may not be freed until it has been
  352. * removed from the kernel lists.
  353. *
  354. * This call does not sleep therefore it can not
  355. * guarantee all CPU's that are in middle of receiving packets
  356. * will see the new packet type (until the next received packet).
  357. */
  358. void dev_add_pack(struct packet_type *pt)
  359. {
  360. struct list_head *head = ptype_head(pt);
  361. spin_lock(&ptype_lock);
  362. list_add_rcu(&pt->list, head);
  363. spin_unlock(&ptype_lock);
  364. }
  365. EXPORT_SYMBOL(dev_add_pack);
  366. /**
  367. * __dev_remove_pack - remove packet handler
  368. * @pt: packet type declaration
  369. *
  370. * Remove a protocol handler that was previously added to the kernel
  371. * protocol handlers by dev_add_pack(). The passed &packet_type is removed
  372. * from the kernel lists and can be freed or reused once this function
  373. * returns.
  374. *
  375. * The packet type might still be in use by receivers
  376. * and must not be freed until after all the CPU's have gone
  377. * through a quiescent state.
  378. */
  379. void __dev_remove_pack(struct packet_type *pt)
  380. {
  381. struct list_head *head = ptype_head(pt);
  382. struct packet_type *pt1;
  383. spin_lock(&ptype_lock);
  384. list_for_each_entry(pt1, head, list) {
  385. if (pt == pt1) {
  386. list_del_rcu(&pt->list);
  387. goto out;
  388. }
  389. }
  390. printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
  391. out:
  392. spin_unlock(&ptype_lock);
  393. }
  394. EXPORT_SYMBOL(__dev_remove_pack);
  395. /**
  396. * dev_remove_pack - remove packet handler
  397. * @pt: packet type declaration
  398. *
  399. * Remove a protocol handler that was previously added to the kernel
  400. * protocol handlers by dev_add_pack(). The passed &packet_type is removed
  401. * from the kernel lists and can be freed or reused once this function
  402. * returns.
  403. *
  404. * This call sleeps to guarantee that no CPU is looking at the packet
  405. * type after return.
  406. */
  407. void dev_remove_pack(struct packet_type *pt)
  408. {
  409. __dev_remove_pack(pt);
  410. synchronize_net();
  411. }
  412. EXPORT_SYMBOL(dev_remove_pack);
  413. /******************************************************************************
  414. Device Boot-time Settings Routines
  415. *******************************************************************************/
  416. /* Boot time configuration table */
  417. static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
  418. /**
  419. * netdev_boot_setup_add - add new setup entry
  420. * @name: name of the device
  421. * @map: configured settings for the device
  422. *
  423. * Adds new setup entry to the dev_boot_setup list. The function
  424. * returns 0 on error and 1 on success. This is a generic routine to
  425. * all netdevices.
  426. */
  427. static int netdev_boot_setup_add(char *name, struct ifmap *map)
  428. {
  429. struct netdev_boot_setup *s;
  430. int i;
  431. s = dev_boot_setup;
  432. for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
  433. if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
  434. memset(s[i].name, 0, sizeof(s[i].name));
  435. strlcpy(s[i].name, name, IFNAMSIZ);
  436. memcpy(&s[i].map, map, sizeof(s[i].map));
  437. break;
  438. }
  439. }
  440. return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
  441. }
  442. /**
  443. * netdev_boot_setup_check - check boot time settings
  444. * @dev: the netdevice
  445. *
  446. * Check boot time settings for the device.
  447. * The found settings are set for the device to be used
  448. * later in the device probing.
  449. * Returns 0 if no settings found, 1 if they are.
  450. */
  451. int netdev_boot_setup_check(struct net_device *dev)
  452. {
  453. struct netdev_boot_setup *s = dev_boot_setup;
  454. int i;
  455. for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
  456. if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
  457. !strcmp(dev->name, s[i].name)) {
  458. dev->irq = s[i].map.irq;
  459. dev->base_addr = s[i].map.base_addr;
  460. dev->mem_start = s[i].map.mem_start;
  461. dev->mem_end = s[i].map.mem_end;
  462. return 1;
  463. }
  464. }
  465. return 0;
  466. }
  467. EXPORT_SYMBOL(netdev_boot_setup_check);
  468. /**
  469. * netdev_boot_base - get address from boot time settings
  470. * @prefix: prefix for network device
  471. * @unit: id for network device
  472. *
  473. * Check boot time settings for the base address of device.
  474. * The found settings are set for the device to be used
  475. * later in the device probing.
  476. * Returns 0 if no settings found.
  477. */
  478. unsigned long netdev_boot_base(const char *prefix, int unit)
  479. {
  480. const struct netdev_boot_setup *s = dev_boot_setup;
  481. char name[IFNAMSIZ];
  482. int i;
  483. sprintf(name, "%s%d", prefix, unit);
  484. /*
  485. * If device already registered then return base of 1
  486. * to indicate not to probe for this interface
  487. */
  488. if (__dev_get_by_name(&init_net, name))
  489. return 1;
  490. for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
  491. if (!strcmp(name, s[i].name))
  492. return s[i].map.base_addr;
  493. return 0;
  494. }
  495. /*
  496. * Saves at boot time configured settings for any netdevice.
  497. */
  498. int __init netdev_boot_setup(char *str)
  499. {
  500. int ints[5];
  501. struct ifmap map;
  502. str = get_options(str, ARRAY_SIZE(ints), ints);
  503. if (!str || !*str)
  504. return 0;
  505. /* Save settings */
  506. memset(&map, 0, sizeof(map));
  507. if (ints[0] > 0)
  508. map.irq = ints[1];
  509. if (ints[0] > 1)
  510. map.base_addr = ints[2];
  511. if (ints[0] > 2)
  512. map.mem_start = ints[3];
  513. if (ints[0] > 3)
  514. map.mem_end = ints[4];
  515. /* Add new entry to the list */
  516. return netdev_boot_setup_add(str, &map);
  517. }
  518. __setup("netdev=", netdev_boot_setup);
  519. /*******************************************************************************
  520. Device Interface Subroutines
  521. *******************************************************************************/
  522. /**
  523. * __dev_get_by_name - find a device by its name
  524. * @net: the applicable net namespace
  525. * @name: name to find
  526. *
  527. * Find an interface by name. Must be called under RTNL semaphore
  528. * or @dev_base_lock. If the name is found a pointer to the device
  529. * is returned. If the name is not found then %NULL is returned. The
  530. * reference counters are not incremented so the caller must be
  531. * careful with locks.
  532. */
  533. struct net_device *__dev_get_by_name(struct net *net, const char *name)
  534. {
  535. struct hlist_node *p;
  536. struct net_device *dev;
  537. struct hlist_head *head = dev_name_hash(net, name);
  538. hlist_for_each_entry(dev, p, head, name_hlist)
  539. if (!strncmp(dev->name, name, IFNAMSIZ))
  540. return dev;
  541. return NULL;
  542. }
  543. EXPORT_SYMBOL(__dev_get_by_name);
  544. /**
  545. * dev_get_by_name_rcu - find a device by its name
  546. * @net: the applicable net namespace
  547. * @name: name to find
  548. *
  549. * Find an interface by name.
  550. * If the name is found a pointer to the device is returned.
  551. * If the name is not found then %NULL is returned.
  552. * The reference counters are not incremented so the caller must be
  553. * careful with locks. The caller must hold RCU lock.
  554. */
  555. struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
  556. {
  557. struct hlist_node *p;
  558. struct net_device *dev;
  559. struct hlist_head *head = dev_name_hash(net, name);
  560. hlist_for_each_entry_rcu(dev, p, head, name_hlist)
  561. if (!strncmp(dev->name, name, IFNAMSIZ))
  562. return dev;
  563. return NULL;
  564. }
  565. EXPORT_SYMBOL(dev_get_by_name_rcu);
  566. /**
  567. * dev_get_by_name - find a device by its name
  568. * @net: the applicable net namespace
  569. * @name: name to find
  570. *
  571. * Find an interface by name. This can be called from any
  572. * context and does its own locking. The returned handle has
  573. * the usage count incremented and the caller must use dev_put() to
  574. * release it when it is no longer needed. %NULL is returned if no
  575. * matching device is found.
  576. */
  577. struct net_device *dev_get_by_name(struct net *net, const char *name)
  578. {
  579. struct net_device *dev;
  580. rcu_read_lock();
  581. dev = dev_get_by_name_rcu(net, name);
  582. if (dev)
  583. dev_hold(dev);
  584. rcu_read_unlock();
  585. return dev;
  586. }
  587. EXPORT_SYMBOL(dev_get_by_name);
  588. /**
  589. * __dev_get_by_index - find a device by its ifindex
  590. * @net: the applicable net namespace
  591. * @ifindex: index of device
  592. *
  593. * Search for an interface by index. Returns %NULL if the device
  594. * is not found or a pointer to the device. The device has not
  595. * had its reference counter increased so the caller must be careful
  596. * about locking. The caller must hold either the RTNL semaphore
  597. * or @dev_base_lock.
  598. */
  599. struct net_device *__dev_get_by_index(struct net *net, int ifindex)
  600. {
  601. struct hlist_node *p;
  602. struct net_device *dev;
  603. struct hlist_head *head = dev_index_hash(net, ifindex);
  604. hlist_for_each_entry(dev, p, head, index_hlist)
  605. if (dev->ifindex == ifindex)
  606. return dev;
  607. return NULL;
  608. }
  609. EXPORT_SYMBOL(__dev_get_by_index);
  610. /**
  611. * dev_get_by_index_rcu - find a device by its ifindex
  612. * @net: the applicable net namespace
  613. * @ifindex: index of device
  614. *
  615. * Search for an interface by index. Returns %NULL if the device
  616. * is not found or a pointer to the device. The device has not
  617. * had its reference counter increased so the caller must be careful
  618. * about locking. The caller must hold RCU lock.
  619. */
  620. struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
  621. {
  622. struct hlist_node *p;
  623. struct net_device *dev;
  624. struct hlist_head *head = dev_index_hash(net, ifindex);
  625. hlist_for_each_entry_rcu(dev, p, head, index_hlist)
  626. if (dev->ifindex == ifindex)
  627. return dev;
  628. return NULL;
  629. }
  630. EXPORT_SYMBOL(dev_get_by_index_rcu);
  631. /**
  632. * dev_get_by_index - find a device by its ifindex
  633. * @net: the applicable net namespace
  634. * @ifindex: index of device
  635. *
  636. * Search for an interface by index. Returns NULL if the device
  637. * is not found or a pointer to the device. The device returned has
  638. * had a reference added and the pointer is safe until the user calls
  639. * dev_put to indicate they have finished with it.
  640. */
  641. struct net_device *dev_get_by_index(struct net *net, int ifindex)
  642. {
  643. struct net_device *dev;
  644. rcu_read_lock();
  645. dev = dev_get_by_index_rcu(net, ifindex);
  646. if (dev)
  647. dev_hold(dev);
  648. rcu_read_unlock();
  649. return dev;
  650. }
  651. EXPORT_SYMBOL(dev_get_by_index);
  652. /**
  653. * dev_getbyhwaddr - find a device by its hardware address
  654. * @net: the applicable net namespace
  655. * @type: media type of device
  656. * @ha: hardware address
  657. *
  658. * Search for an interface by MAC address. Returns NULL if the device
  659. * is not found or a pointer to the device. The caller must hold the
  660. * rtnl semaphore. The returned device has not had its ref count increased
  661. * and the caller must therefore be careful about locking
  662. *
  663. * BUGS:
  664. * If the API was consistent this would be __dev_get_by_hwaddr
  665. */
  666. struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type, char *ha)
  667. {
  668. struct net_device *dev;
  669. ASSERT_RTNL();
  670. for_each_netdev(net, dev)
  671. if (dev->type == type &&
  672. !memcmp(dev->dev_addr, ha, dev->addr_len))
  673. return dev;
  674. return NULL;
  675. }
  676. EXPORT_SYMBOL(dev_getbyhwaddr);
  677. struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
  678. {
  679. struct net_device *dev;
  680. ASSERT_RTNL();
  681. for_each_netdev(net, dev)
  682. if (dev->type == type)
  683. return dev;
  684. return NULL;
  685. }
  686. EXPORT_SYMBOL(__dev_getfirstbyhwtype);
  687. struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
  688. {
  689. struct net_device *dev, *ret = NULL;
  690. rcu_read_lock();
  691. for_each_netdev_rcu(net, dev)
  692. if (dev->type == type) {
  693. dev_hold(dev);
  694. ret = dev;
  695. break;
  696. }
  697. rcu_read_unlock();
  698. return ret;
  699. }
  700. EXPORT_SYMBOL(dev_getfirstbyhwtype);
  701. /**
  702. * dev_get_by_flags_rcu - find any device with given flags
  703. * @net: the applicable net namespace
  704. * @if_flags: IFF_* values
  705. * @mask: bitmask of bits in if_flags to check
  706. *
  707. * Search for any interface with the given flags. Returns NULL if a device
  708. * is not found or a pointer to the device. Must be called inside
  709. * rcu_read_lock(), and result refcount is unchanged.
  710. */
  711. struct net_device *dev_get_by_flags_rcu(struct net *net, unsigned short if_flags,
  712. unsigned short mask)
  713. {
  714. struct net_device *dev, *ret;
  715. ret = NULL;
  716. for_each_netdev_rcu(net, dev) {
  717. if (((dev->flags ^ if_flags) & mask) == 0) {
  718. ret = dev;
  719. break;
  720. }
  721. }
  722. return ret;
  723. }
  724. EXPORT_SYMBOL(dev_get_by_flags_rcu);
  725. /**
  726. * dev_valid_name - check if name is okay for network device
  727. * @name: name string
  728. *
  729. * Network device names need to be valid file names to
  730. * to allow sysfs to work. We also disallow any kind of
  731. * whitespace.
  732. */
  733. int dev_valid_name(const char *name)
  734. {
  735. if (*name == '\0')
  736. return 0;
  737. if (strlen(name) >= IFNAMSIZ)
  738. return 0;
  739. if (!strcmp(name, ".") || !strcmp(name, ".."))
  740. return 0;
  741. while (*name) {
  742. if (*name == '/' || isspace(*name))
  743. return 0;
  744. name++;
  745. }
  746. return 1;
  747. }
  748. EXPORT_SYMBOL(dev_valid_name);
  749. /**
  750. * __dev_alloc_name - allocate a name for a device
  751. * @net: network namespace to allocate the device name in
  752. * @name: name format string
  753. * @buf: scratch buffer and result name string
  754. *
  755. * Passed a format string - eg "lt%d" it will try and find a suitable
  756. * id. It scans list of devices to build up a free map, then chooses
  757. * the first empty slot. The caller must hold the dev_base or rtnl lock
  758. * while allocating the name and adding the device in order to avoid
  759. * duplicates.
  760. * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
  761. * Returns the number of the unit assigned or a negative errno code.
  762. */
  763. static int __dev_alloc_name(struct net *net, const char *name, char *buf)
  764. {
  765. int i = 0;
  766. const char *p;
  767. const int max_netdevices = 8*PAGE_SIZE;
  768. unsigned long *inuse;
  769. struct net_device *d;
  770. p = strnchr(name, IFNAMSIZ-1, '%');
  771. if (p) {
  772. /*
  773. * Verify the string as this thing may have come from
  774. * the user. There must be either one "%d" and no other "%"
  775. * characters.
  776. */
  777. if (p[1] != 'd' || strchr(p + 2, '%'))
  778. return -EINVAL;
  779. /* Use one page as a bit array of possible slots */
  780. inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
  781. if (!inuse)
  782. return -ENOMEM;
  783. for_each_netdev(net, d) {
  784. if (!sscanf(d->name, name, &i))
  785. continue;
  786. if (i < 0 || i >= max_netdevices)
  787. continue;
  788. /* avoid cases where sscanf is not exact inverse of printf */
  789. snprintf(buf, IFNAMSIZ, name, i);
  790. if (!strncmp(buf, d->name, IFNAMSIZ))
  791. set_bit(i, inuse);
  792. }
  793. i = find_first_zero_bit(inuse, max_netdevices);
  794. free_page((unsigned long) inuse);
  795. }
  796. if (buf != name)
  797. snprintf(buf, IFNAMSIZ, name, i);
  798. if (!__dev_get_by_name(net, buf))
  799. return i;
  800. /* It is possible to run out of possible slots
  801. * when the name is long and there isn't enough space left
  802. * for the digits, or if all bits are used.
  803. */
  804. return -ENFILE;
  805. }
  806. /**
  807. * dev_alloc_name - allocate a name for a device
  808. * @dev: device
  809. * @name: name format string
  810. *
  811. * Passed a format string - eg "lt%d" it will try and find a suitable
  812. * id. It scans list of devices to build up a free map, then chooses
  813. * the first empty slot. The caller must hold the dev_base or rtnl lock
  814. * while allocating the name and adding the device in order to avoid
  815. * duplicates.
  816. * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
  817. * Returns the number of the unit assigned or a negative errno code.
  818. */
  819. int dev_alloc_name(struct net_device *dev, const char *name)
  820. {
  821. char buf[IFNAMSIZ];
  822. struct net *net;
  823. int ret;
  824. BUG_ON(!dev_net(dev));
  825. net = dev_net(dev);
  826. ret = __dev_alloc_name(net, name, buf);
  827. if (ret >= 0)
  828. strlcpy(dev->name, buf, IFNAMSIZ);
  829. return ret;
  830. }
  831. EXPORT_SYMBOL(dev_alloc_name);
  832. static int dev_get_valid_name(struct net_device *dev, const char *name, bool fmt)
  833. {
  834. struct net *net;
  835. BUG_ON(!dev_net(dev));
  836. net = dev_net(dev);
  837. if (!dev_valid_name(name))
  838. return -EINVAL;
  839. if (fmt && strchr(name, '%'))
  840. return dev_alloc_name(dev, name);
  841. else if (__dev_get_by_name(net, name))
  842. return -EEXIST;
  843. else if (dev->name != name)
  844. strlcpy(dev->name, name, IFNAMSIZ);
  845. return 0;
  846. }
  847. /**
  848. * dev_change_name - change name of a device
  849. * @dev: device
  850. * @newname: name (or format string) must be at least IFNAMSIZ
  851. *
  852. * Change name of a device, can pass format strings "eth%d".
  853. * for wildcarding.
  854. */
  855. int dev_change_name(struct net_device *dev, const char *newname)
  856. {
  857. char oldname[IFNAMSIZ];
  858. int err = 0;
  859. int ret;
  860. struct net *net;
  861. ASSERT_RTNL();
  862. BUG_ON(!dev_net(dev));
  863. net = dev_net(dev);
  864. if (dev->flags & IFF_UP)
  865. return -EBUSY;
  866. if (strncmp(newname, dev->name, IFNAMSIZ) == 0)
  867. return 0;
  868. memcpy(oldname, dev->name, IFNAMSIZ);
  869. err = dev_get_valid_name(dev, newname, 1);
  870. if (err < 0)
  871. return err;
  872. rollback:
  873. ret = device_rename(&dev->dev, dev->name);
  874. if (ret) {
  875. memcpy(dev->name, oldname, IFNAMSIZ);
  876. return ret;
  877. }
  878. write_lock_bh(&dev_base_lock);
  879. hlist_del(&dev->name_hlist);
  880. write_unlock_bh(&dev_base_lock);
  881. synchronize_rcu();
  882. write_lock_bh(&dev_base_lock);
  883. hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
  884. write_unlock_bh(&dev_base_lock);
  885. ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
  886. ret = notifier_to_errno(ret);
  887. if (ret) {
  888. /* err >= 0 after dev_alloc_name() or stores the first errno */
  889. if (err >= 0) {
  890. err = ret;
  891. memcpy(dev->name, oldname, IFNAMSIZ);
  892. goto rollback;
  893. } else {
  894. printk(KERN_ERR
  895. "%s: name change rollback failed: %d.\n",
  896. dev->name, ret);
  897. }
  898. }
  899. return err;
  900. }
  901. /**
  902. * dev_set_alias - change ifalias of a device
  903. * @dev: device
  904. * @alias: name up to IFALIASZ
  905. * @len: limit of bytes to copy from info
  906. *
  907. * Set ifalias for a device,
  908. */
  909. int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
  910. {
  911. ASSERT_RTNL();
  912. if (len >= IFALIASZ)
  913. return -EINVAL;
  914. if (!len) {
  915. if (dev->ifalias) {
  916. kfree(dev->ifalias);
  917. dev->ifalias = NULL;
  918. }
  919. return 0;
  920. }
  921. dev->ifalias = krealloc(dev->ifalias, len + 1, GFP_KERNEL);
  922. if (!dev->ifalias)
  923. return -ENOMEM;
  924. strlcpy(dev->ifalias, alias, len+1);
  925. return len;
  926. }
  927. /**
  928. * netdev_features_change - device changes features
  929. * @dev: device to cause notification
  930. *
  931. * Called to indicate a device has changed features.
  932. */
  933. void netdev_features_change(struct net_device *dev)
  934. {
  935. call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
  936. }
  937. EXPORT_SYMBOL(netdev_features_change);
  938. /**
  939. * netdev_state_change - device changes state
  940. * @dev: device to cause notification
  941. *
  942. * Called to indicate a device has changed state. This function calls
  943. * the notifier chains for netdev_chain and sends a NEWLINK message
  944. * to the routing socket.
  945. */
  946. void netdev_state_change(struct net_device *dev)
  947. {
  948. if (dev->flags & IFF_UP) {
  949. call_netdevice_notifiers(NETDEV_CHANGE, dev);
  950. rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
  951. }
  952. }
  953. EXPORT_SYMBOL(netdev_state_change);
  954. int netdev_bonding_change(struct net_device *dev, unsigned long event)
  955. {
  956. return call_netdevice_notifiers(event, dev);
  957. }
  958. EXPORT_SYMBOL(netdev_bonding_change);
  959. /**
  960. * dev_load - load a network module
  961. * @net: the applicable net namespace
  962. * @name: name of interface
  963. *
  964. * If a network interface is not present and the process has suitable
  965. * privileges this function loads the module. If module loading is not
  966. * available in this kernel then it becomes a nop.
  967. */
  968. void dev_load(struct net *net, const char *name)
  969. {
  970. struct net_device *dev;
  971. rcu_read_lock();
  972. dev = dev_get_by_name_rcu(net, name);
  973. rcu_read_unlock();
  974. if (!dev && capable(CAP_NET_ADMIN))
  975. request_module("%s", name);
  976. }
  977. EXPORT_SYMBOL(dev_load);
  978. static int __dev_open(struct net_device *dev)
  979. {
  980. const struct net_device_ops *ops = dev->netdev_ops;
  981. int ret;
  982. ASSERT_RTNL();
  983. /*
  984. * Is it even present?
  985. */
  986. if (!netif_device_present(dev))
  987. return -ENODEV;
  988. ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
  989. ret = notifier_to_errno(ret);
  990. if (ret)
  991. return ret;
  992. /*
  993. * Call device private open method
  994. */
  995. set_bit(__LINK_STATE_START, &dev->state);
  996. if (ops->ndo_validate_addr)
  997. ret = ops->ndo_validate_addr(dev);
  998. if (!ret && ops->ndo_open)
  999. ret = ops->ndo_open(dev);
  1000. /*
  1001. * If it went open OK then:
  1002. */
  1003. if (ret)
  1004. clear_bit(__LINK_STATE_START, &dev->state);
  1005. else {
  1006. /*
  1007. * Set the flags.
  1008. */
  1009. dev->flags |= IFF_UP;
  1010. /*
  1011. * Enable NET_DMA
  1012. */
  1013. net_dmaengine_get();
  1014. /*
  1015. * Initialize multicasting status
  1016. */
  1017. dev_set_rx_mode(dev);
  1018. /*
  1019. * Wakeup transmit queue engine
  1020. */
  1021. dev_activate(dev);
  1022. }
  1023. return ret;
  1024. }
  1025. /**
  1026. * dev_open - prepare an interface for use.
  1027. * @dev: device to open
  1028. *
  1029. * Takes a device from down to up state. The device's private open
  1030. * function is invoked and then the multicast lists are loaded. Finally
  1031. * the device is moved into the up state and a %NETDEV_UP message is
  1032. * sent to the netdev notifier chain.
  1033. *
  1034. * Calling this function on an active interface is a nop. On a failure
  1035. * a negative errno code is returned.
  1036. */
  1037. int dev_open(struct net_device *dev)
  1038. {
  1039. int ret;
  1040. /*
  1041. * Is it already up?
  1042. */
  1043. if (dev->flags & IFF_UP)
  1044. return 0;
  1045. /*
  1046. * Open device
  1047. */
  1048. ret = __dev_open(dev);
  1049. if (ret < 0)
  1050. return ret;
  1051. /*
  1052. * ... and announce new interface.
  1053. */
  1054. rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
  1055. call_netdevice_notifiers(NETDEV_UP, dev);
  1056. return ret;
  1057. }
  1058. EXPORT_SYMBOL(dev_open);
  1059. static int __dev_close(struct net_device *dev)
  1060. {
  1061. const struct net_device_ops *ops = dev->netdev_ops;
  1062. ASSERT_RTNL();
  1063. might_sleep();
  1064. /*
  1065. * Tell people we are going down, so that they can
  1066. * prepare to death, when device is still operating.
  1067. */
  1068. call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
  1069. clear_bit(__LINK_STATE_START, &dev->state);
  1070. /* Synchronize to scheduled poll. We cannot touch poll list,
  1071. * it can be even on different cpu. So just clear netif_running().
  1072. *
  1073. * dev->stop() will invoke napi_disable() on all of it's
  1074. * napi_struct instances on this device.
  1075. */
  1076. smp_mb__after_clear_bit(); /* Commit netif_running(). */
  1077. dev_deactivate(dev);
  1078. /*
  1079. * Call the device specific close. This cannot fail.
  1080. * Only if device is UP
  1081. *
  1082. * We allow it to be called even after a DETACH hot-plug
  1083. * event.
  1084. */
  1085. if (ops->ndo_stop)
  1086. ops->ndo_stop(dev);
  1087. /*
  1088. * Device is now down.
  1089. */
  1090. dev->flags &= ~IFF_UP;
  1091. /*
  1092. * Shutdown NET_DMA
  1093. */
  1094. net_dmaengine_put();
  1095. return 0;
  1096. }
  1097. /**
  1098. * dev_close - shutdown an interface.
  1099. * @dev: device to shutdown
  1100. *
  1101. * This function moves an active device into down state. A
  1102. * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
  1103. * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
  1104. * chain.
  1105. */
  1106. int dev_close(struct net_device *dev)
  1107. {
  1108. if (!(dev->flags & IFF_UP))
  1109. return 0;
  1110. __dev_close(dev);
  1111. /*
  1112. * Tell people we are down
  1113. */
  1114. rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
  1115. call_netdevice_notifiers(NETDEV_DOWN, dev);
  1116. return 0;
  1117. }
  1118. EXPORT_SYMBOL(dev_close);
  1119. /**
  1120. * dev_disable_lro - disable Large Receive Offload on a device
  1121. * @dev: device
  1122. *
  1123. * Disable Large Receive Offload (LRO) on a net device. Must be
  1124. * called under RTNL. This is needed if received packets may be
  1125. * forwarded to another interface.
  1126. */
  1127. void dev_disable_lro(struct net_device *dev)
  1128. {
  1129. if (dev->ethtool_ops && dev->ethtool_ops->get_flags &&
  1130. dev->ethtool_ops->set_flags) {
  1131. u32 flags = dev->ethtool_ops->get_flags(dev);
  1132. if (flags & ETH_FLAG_LRO) {
  1133. flags &= ~ETH_FLAG_LRO;
  1134. dev->ethtool_ops->set_flags(dev, flags);
  1135. }
  1136. }
  1137. WARN_ON(dev->features & NETIF_F_LRO);
  1138. }
  1139. EXPORT_SYMBOL(dev_disable_lro);
  1140. static int dev_boot_phase = 1;
  1141. /*
  1142. * Device change register/unregister. These are not inline or static
  1143. * as we export them to the world.
  1144. */
  1145. /**
  1146. * register_netdevice_notifier - register a network notifier block
  1147. * @nb: notifier
  1148. *
  1149. * Register a notifier to be called when network device events occur.
  1150. * The notifier passed is linked into the kernel structures and must
  1151. * not be reused until it has been unregistered. A negative errno code
  1152. * is returned on a failure.
  1153. *
  1154. * When registered all registration and up events are replayed
  1155. * to the new notifier to allow device to have a race free
  1156. * view of the network device list.
  1157. */
  1158. int register_netdevice_notifier(struct notifier_block *nb)
  1159. {
  1160. struct net_device *dev;
  1161. struct net_device *last;
  1162. struct net *net;
  1163. int err;
  1164. rtnl_lock();
  1165. err = raw_notifier_chain_register(&netdev_chain, nb);
  1166. if (err)
  1167. goto unlock;
  1168. if (dev_boot_phase)
  1169. goto unlock;
  1170. for_each_net(net) {
  1171. for_each_netdev(net, dev) {
  1172. err = nb->notifier_call(nb, NETDEV_REGISTER, dev);
  1173. err = notifier_to_errno(err);
  1174. if (err)
  1175. goto rollback;
  1176. if (!(dev->flags & IFF_UP))
  1177. continue;
  1178. nb->notifier_call(nb, NETDEV_UP, dev);
  1179. }
  1180. }
  1181. unlock:
  1182. rtnl_unlock();
  1183. return err;
  1184. rollback:
  1185. last = dev;
  1186. for_each_net(net) {
  1187. for_each_netdev(net, dev) {
  1188. if (dev == last)
  1189. break;
  1190. if (dev->flags & IFF_UP) {
  1191. nb->notifier_call(nb, NETDEV_GOING_DOWN, dev);
  1192. nb->notifier_call(nb, NETDEV_DOWN, dev);
  1193. }
  1194. nb->notifier_call(nb, NETDEV_UNREGISTER, dev);
  1195. nb->notifier_call(nb, NETDEV_UNREGISTER_BATCH, dev);
  1196. }
  1197. }
  1198. raw_notifier_chain_unregister(&netdev_chain, nb);
  1199. goto unlock;
  1200. }
  1201. EXPORT_SYMBOL(register_netdevice_notifier);
  1202. /**
  1203. * unregister_netdevice_notifier - unregister a network notifier block
  1204. * @nb: notifier
  1205. *
  1206. * Unregister a notifier previously registered by
  1207. * register_netdevice_notifier(). The notifier is unlinked into the
  1208. * kernel structures and may then be reused. A negative errno code
  1209. * is returned on a failure.
  1210. */
  1211. int unregister_netdevice_notifier(struct notifier_block *nb)
  1212. {
  1213. int err;
  1214. rtnl_lock();
  1215. err = raw_notifier_chain_unregister(&netdev_chain, nb);
  1216. rtnl_unlock();
  1217. return err;
  1218. }
  1219. EXPORT_SYMBOL(unregister_netdevice_notifier);
  1220. /**
  1221. * call_netdevice_notifiers - call all network notifier blocks
  1222. * @val: value passed unmodified to notifier function
  1223. * @dev: net_device pointer passed unmodified to notifier function
  1224. *
  1225. * Call all network notifier blocks. Parameters and return value
  1226. * are as for raw_notifier_call_chain().
  1227. */
  1228. int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
  1229. {
  1230. ASSERT_RTNL();
  1231. return raw_notifier_call_chain(&netdev_chain, val, dev);
  1232. }
  1233. /* When > 0 there are consumers of rx skb time stamps */
  1234. static atomic_t netstamp_needed = ATOMIC_INIT(0);
  1235. void net_enable_timestamp(void)
  1236. {
  1237. atomic_inc(&netstamp_needed);
  1238. }
  1239. EXPORT_SYMBOL(net_enable_timestamp);
  1240. void net_disable_timestamp(void)
  1241. {
  1242. atomic_dec(&netstamp_needed);
  1243. }
  1244. EXPORT_SYMBOL(net_disable_timestamp);
  1245. static inline void net_timestamp_set(struct sk_buff *skb)
  1246. {
  1247. if (atomic_read(&netstamp_needed))
  1248. __net_timestamp(skb);
  1249. else
  1250. skb->tstamp.tv64 = 0;
  1251. }
  1252. static inline void net_timestamp_check(struct sk_buff *skb)
  1253. {
  1254. if (!skb->tstamp.tv64 && atomic_read(&netstamp_needed))
  1255. __net_timestamp(skb);
  1256. }
  1257. /**
  1258. * dev_forward_skb - loopback an skb to another netif
  1259. *
  1260. * @dev: destination network device
  1261. * @skb: buffer to forward
  1262. *
  1263. * return values:
  1264. * NET_RX_SUCCESS (no congestion)
  1265. * NET_RX_DROP (packet was dropped, but freed)
  1266. *
  1267. * dev_forward_skb can be used for injecting an skb from the
  1268. * start_xmit function of one device into the receive queue
  1269. * of another device.
  1270. *
  1271. * The receiving device may be in another namespace, so
  1272. * we have to clear all information in the skb that could
  1273. * impact namespace isolation.
  1274. */
  1275. int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
  1276. {
  1277. skb_orphan(skb);
  1278. nf_reset(skb);
  1279. if (!(dev->flags & IFF_UP) ||
  1280. (skb->len > (dev->mtu + dev->hard_header_len))) {
  1281. kfree_skb(skb);
  1282. return NET_RX_DROP;
  1283. }
  1284. skb_set_dev(skb, dev);
  1285. skb->tstamp.tv64 = 0;
  1286. skb->pkt_type = PACKET_HOST;
  1287. skb->protocol = eth_type_trans(skb, dev);
  1288. return netif_rx(skb);
  1289. }
  1290. EXPORT_SYMBOL_GPL(dev_forward_skb);
  1291. /*
  1292. * Support routine. Sends outgoing frames to any network
  1293. * taps currently in use.
  1294. */
  1295. static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
  1296. {
  1297. struct packet_type *ptype;
  1298. #ifdef CONFIG_NET_CLS_ACT
  1299. if (!(skb->tstamp.tv64 && (G_TC_FROM(skb->tc_verd) & AT_INGRESS)))
  1300. net_timestamp_set(skb);
  1301. #else
  1302. net_timestamp_set(skb);
  1303. #endif
  1304. rcu_read_lock();
  1305. list_for_each_entry_rcu(ptype, &ptype_all, list) {
  1306. /* Never send packets back to the socket
  1307. * they originated from - MvS (miquels@drinkel.ow.org)
  1308. */
  1309. if ((ptype->dev == dev || !ptype->dev) &&
  1310. (ptype->af_packet_priv == NULL ||
  1311. (struct sock *)ptype->af_packet_priv != skb->sk)) {
  1312. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  1313. if (!skb2)
  1314. break;
  1315. /* skb->nh should be correctly
  1316. set by sender, so that the second statement is
  1317. just protection against buggy protocols.
  1318. */
  1319. skb_reset_mac_header(skb2);
  1320. if (skb_network_header(skb2) < skb2->data ||
  1321. skb2->network_header > skb2->tail) {
  1322. if (net_ratelimit())
  1323. printk(KERN_CRIT "protocol %04x is "
  1324. "buggy, dev %s\n",
  1325. ntohs(skb2->protocol),
  1326. dev->name);
  1327. skb_reset_network_header(skb2);
  1328. }
  1329. skb2->transport_header = skb2->network_header;
  1330. skb2->pkt_type = PACKET_OUTGOING;
  1331. ptype->func(skb2, skb->dev, ptype, skb->dev);
  1332. }
  1333. }
  1334. rcu_read_unlock();
  1335. }
  1336. /*
  1337. * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
  1338. * greater then real_num_tx_queues stale skbs on the qdisc must be flushed.
  1339. */
  1340. void netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
  1341. {
  1342. unsigned int real_num = dev->real_num_tx_queues;
  1343. if (unlikely(txq > dev->num_tx_queues))
  1344. ;
  1345. else if (txq > real_num)
  1346. dev->real_num_tx_queues = txq;
  1347. else if (txq < real_num) {
  1348. dev->real_num_tx_queues = txq;
  1349. qdisc_reset_all_tx_gt(dev, txq);
  1350. }
  1351. }
  1352. EXPORT_SYMBOL(netif_set_real_num_tx_queues);
  1353. static inline void __netif_reschedule(struct Qdisc *q)
  1354. {
  1355. struct softnet_data *sd;
  1356. unsigned long flags;
  1357. local_irq_save(flags);
  1358. sd = &__get_cpu_var(softnet_data);
  1359. q->next_sched = NULL;
  1360. *sd->output_queue_tailp = q;
  1361. sd->output_queue_tailp = &q->next_sched;
  1362. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  1363. local_irq_restore(flags);
  1364. }
  1365. void __netif_schedule(struct Qdisc *q)
  1366. {
  1367. if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
  1368. __netif_reschedule(q);
  1369. }
  1370. EXPORT_SYMBOL(__netif_schedule);
  1371. void dev_kfree_skb_irq(struct sk_buff *skb)
  1372. {
  1373. if (atomic_dec_and_test(&skb->users)) {
  1374. struct softnet_data *sd;
  1375. unsigned long flags;
  1376. local_irq_save(flags);
  1377. sd = &__get_cpu_var(softnet_data);
  1378. skb->next = sd->completion_queue;
  1379. sd->completion_queue = skb;
  1380. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  1381. local_irq_restore(flags);
  1382. }
  1383. }
  1384. EXPORT_SYMBOL(dev_kfree_skb_irq);
  1385. void dev_kfree_skb_any(struct sk_buff *skb)
  1386. {
  1387. if (in_irq() || irqs_disabled())
  1388. dev_kfree_skb_irq(skb);
  1389. else
  1390. dev_kfree_skb(skb);
  1391. }
  1392. EXPORT_SYMBOL(dev_kfree_skb_any);
  1393. /**
  1394. * netif_device_detach - mark device as removed
  1395. * @dev: network device
  1396. *
  1397. * Mark device as removed from system and therefore no longer available.
  1398. */
  1399. void netif_device_detach(struct net_device *dev)
  1400. {
  1401. if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
  1402. netif_running(dev)) {
  1403. netif_tx_stop_all_queues(dev);
  1404. }
  1405. }
  1406. EXPORT_SYMBOL(netif_device_detach);
  1407. /**
  1408. * netif_device_attach - mark device as attached
  1409. * @dev: network device
  1410. *
  1411. * Mark device as attached from system and restart if needed.
  1412. */
  1413. void netif_device_attach(struct net_device *dev)
  1414. {
  1415. if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
  1416. netif_running(dev)) {
  1417. netif_tx_wake_all_queues(dev);
  1418. __netdev_watchdog_up(dev);
  1419. }
  1420. }
  1421. EXPORT_SYMBOL(netif_device_attach);
  1422. static bool can_checksum_protocol(unsigned long features, __be16 protocol)
  1423. {
  1424. return ((features & NETIF_F_GEN_CSUM) ||
  1425. ((features & NETIF_F_IP_CSUM) &&
  1426. protocol == htons(ETH_P_IP)) ||
  1427. ((features & NETIF_F_IPV6_CSUM) &&
  1428. protocol == htons(ETH_P_IPV6)) ||
  1429. ((features & NETIF_F_FCOE_CRC) &&
  1430. protocol == htons(ETH_P_FCOE)));
  1431. }
  1432. static bool dev_can_checksum(struct net_device *dev, struct sk_buff *skb)
  1433. {
  1434. if (can_checksum_protocol(dev->features, skb->protocol))
  1435. return true;
  1436. if (skb->protocol == htons(ETH_P_8021Q)) {
  1437. struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
  1438. if (can_checksum_protocol(dev->features & dev->vlan_features,
  1439. veh->h_vlan_encapsulated_proto))
  1440. return true;
  1441. }
  1442. return false;
  1443. }
  1444. /**
  1445. * skb_dev_set -- assign a new device to a buffer
  1446. * @skb: buffer for the new device
  1447. * @dev: network device
  1448. *
  1449. * If an skb is owned by a device already, we have to reset
  1450. * all data private to the namespace a device belongs to
  1451. * before assigning it a new device.
  1452. */
  1453. #ifdef CONFIG_NET_NS
  1454. void skb_set_dev(struct sk_buff *skb, struct net_device *dev)
  1455. {
  1456. skb_dst_drop(skb);
  1457. if (skb->dev && !net_eq(dev_net(skb->dev), dev_net(dev))) {
  1458. secpath_reset(skb);
  1459. nf_reset(skb);
  1460. skb_init_secmark(skb);
  1461. skb->mark = 0;
  1462. skb->priority = 0;
  1463. skb->nf_trace = 0;
  1464. skb->ipvs_property = 0;
  1465. #ifdef CONFIG_NET_SCHED
  1466. skb->tc_index = 0;
  1467. #endif
  1468. }
  1469. skb->dev = dev;
  1470. }
  1471. EXPORT_SYMBOL(skb_set_dev);
  1472. #endif /* CONFIG_NET_NS */
  1473. /*
  1474. * Invalidate hardware checksum when packet is to be mangled, and
  1475. * complete checksum manually on outgoing path.
  1476. */
  1477. int skb_checksum_help(struct sk_buff *skb)
  1478. {
  1479. __wsum csum;
  1480. int ret = 0, offset;
  1481. if (skb->ip_summed == CHECKSUM_COMPLETE)
  1482. goto out_set_summed;
  1483. if (unlikely(skb_shinfo(skb)->gso_size)) {
  1484. /* Let GSO fix up the checksum. */
  1485. goto out_set_summed;
  1486. }
  1487. offset = skb->csum_start - skb_headroom(skb);
  1488. BUG_ON(offset >= skb_headlen(skb));
  1489. csum = skb_checksum(skb, offset, skb->len - offset, 0);
  1490. offset += skb->csum_offset;
  1491. BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
  1492. if (skb_cloned(skb) &&
  1493. !skb_clone_writable(skb, offset + sizeof(__sum16))) {
  1494. ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  1495. if (ret)
  1496. goto out;
  1497. }
  1498. *(__sum16 *)(skb->data + offset) = csum_fold(csum);
  1499. out_set_summed:
  1500. skb->ip_summed = CHECKSUM_NONE;
  1501. out:
  1502. return ret;
  1503. }
  1504. EXPORT_SYMBOL(skb_checksum_help);
  1505. /**
  1506. * skb_gso_segment - Perform segmentation on skb.
  1507. * @skb: buffer to segment
  1508. * @features: features for the output path (see dev->features)
  1509. *
  1510. * This function segments the given skb and returns a list of segments.
  1511. *
  1512. * It may return NULL if the skb requires no segmentation. This is
  1513. * only possible when GSO is used for verifying header integrity.
  1514. */
  1515. struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
  1516. {
  1517. struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
  1518. struct packet_type *ptype;
  1519. __be16 type = skb->protocol;
  1520. int err;
  1521. skb_reset_mac_header(skb);
  1522. skb->mac_len = skb->network_header - skb->mac_header;
  1523. __skb_pull(skb, skb->mac_len);
  1524. if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
  1525. struct net_device *dev = skb->dev;
  1526. struct ethtool_drvinfo info = {};
  1527. if (dev && dev->ethtool_ops && dev->ethtool_ops->get_drvinfo)
  1528. dev->ethtool_ops->get_drvinfo(dev, &info);
  1529. WARN(1, "%s: caps=(0x%lx, 0x%lx) len=%d data_len=%d "
  1530. "ip_summed=%d",
  1531. info.driver, dev ? dev->features : 0L,
  1532. skb->sk ? skb->sk->sk_route_caps : 0L,
  1533. skb->len, skb->data_len, skb->ip_summed);
  1534. if (skb_header_cloned(skb) &&
  1535. (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  1536. return ERR_PTR(err);
  1537. }
  1538. rcu_read_lock();
  1539. list_for_each_entry_rcu(ptype,
  1540. &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
  1541. if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
  1542. if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
  1543. err = ptype->gso_send_check(skb);
  1544. segs = ERR_PTR(err);
  1545. if (err || skb_gso_ok(skb, features))
  1546. break;
  1547. __skb_push(skb, (skb->data -
  1548. skb_network_header(skb)));
  1549. }
  1550. segs = ptype->gso_segment(skb, features);
  1551. break;
  1552. }
  1553. }
  1554. rcu_read_unlock();
  1555. __skb_push(skb, skb->data - skb_mac_header(skb));
  1556. return segs;
  1557. }
  1558. EXPORT_SYMBOL(skb_gso_segment);
  1559. /* Take action when hardware reception checksum errors are detected. */
  1560. #ifdef CONFIG_BUG
  1561. void netdev_rx_csum_fault(struct net_device *dev)
  1562. {
  1563. if (net_ratelimit()) {
  1564. printk(KERN_ERR "%s: hw csum failure.\n",
  1565. dev ? dev->name : "<unknown>");
  1566. dump_stack();
  1567. }
  1568. }
  1569. EXPORT_SYMBOL(netdev_rx_csum_fault);
  1570. #endif
  1571. /* Actually, we should eliminate this check as soon as we know, that:
  1572. * 1. IOMMU is present and allows to map all the memory.
  1573. * 2. No high memory really exists on this machine.
  1574. */
  1575. static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
  1576. {
  1577. #ifdef CONFIG_HIGHMEM
  1578. int i;
  1579. if (!(dev->features & NETIF_F_HIGHDMA)) {
  1580. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1581. if (PageHighMem(skb_shinfo(skb)->frags[i].page))
  1582. return 1;
  1583. }
  1584. if (PCI_DMA_BUS_IS_PHYS) {
  1585. struct device *pdev = dev->dev.parent;
  1586. if (!pdev)
  1587. return 0;
  1588. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1589. dma_addr_t addr = page_to_phys(skb_shinfo(skb)->frags[i].page);
  1590. if (!pdev->dma_mask || addr + PAGE_SIZE - 1 > *pdev->dma_mask)
  1591. return 1;
  1592. }
  1593. }
  1594. #endif
  1595. return 0;
  1596. }
  1597. struct dev_gso_cb {
  1598. void (*destructor)(struct sk_buff *skb);
  1599. };
  1600. #define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
  1601. static void dev_gso_skb_destructor(struct sk_buff *skb)
  1602. {
  1603. struct dev_gso_cb *cb;
  1604. do {
  1605. struct sk_buff *nskb = skb->next;
  1606. skb->next = nskb->next;
  1607. nskb->next = NULL;
  1608. kfree_skb(nskb);
  1609. } while (skb->next);
  1610. cb = DEV_GSO_CB(skb);
  1611. if (cb->destructor)
  1612. cb->destructor(skb);
  1613. }
  1614. /**
  1615. * dev_gso_segment - Perform emulated hardware segmentation on skb.
  1616. * @skb: buffer to segment
  1617. *
  1618. * This function segments the given skb and stores the list of segments
  1619. * in skb->next.
  1620. */
  1621. static int dev_gso_segment(struct sk_buff *skb)
  1622. {
  1623. struct net_device *dev = skb->dev;
  1624. struct sk_buff *segs;
  1625. int features = dev->features & ~(illegal_highdma(dev, skb) ?
  1626. NETIF_F_SG : 0);
  1627. segs = skb_gso_segment(skb, features);
  1628. /* Verifying header integrity only. */
  1629. if (!segs)
  1630. return 0;
  1631. if (IS_ERR(segs))
  1632. return PTR_ERR(segs);
  1633. skb->next = segs;
  1634. DEV_GSO_CB(skb)->destructor = skb->destructor;
  1635. skb->destructor = dev_gso_skb_destructor;
  1636. return 0;
  1637. }
  1638. /*
  1639. * Try to orphan skb early, right before transmission by the device.
  1640. * We cannot orphan skb if tx timestamp is requested or the sk-reference
  1641. * is needed on driver level for other reasons, e.g. see net/can/raw.c
  1642. */
  1643. static inline void skb_orphan_try(struct sk_buff *skb)
  1644. {
  1645. struct sock *sk = skb->sk;
  1646. if (sk && !skb_shinfo(skb)->tx_flags) {
  1647. /* skb_tx_hash() wont be able to get sk.
  1648. * We copy sk_hash into skb->rxhash
  1649. */
  1650. if (!skb->rxhash)
  1651. skb->rxhash = sk->sk_hash;
  1652. skb_orphan(skb);
  1653. }
  1654. }
  1655. /*
  1656. * Returns true if either:
  1657. * 1. skb has frag_list and the device doesn't support FRAGLIST, or
  1658. * 2. skb is fragmented and the device does not support SG, or if
  1659. * at least one of fragments is in highmem and device does not
  1660. * support DMA from it.
  1661. */
  1662. static inline int skb_needs_linearize(struct sk_buff *skb,
  1663. struct net_device *dev)
  1664. {
  1665. return skb_is_nonlinear(skb) &&
  1666. ((skb_has_frag_list(skb) && !(dev->features & NETIF_F_FRAGLIST)) ||
  1667. (skb_shinfo(skb)->nr_frags && (!(dev->features & NETIF_F_SG) ||
  1668. illegal_highdma(dev, skb))));
  1669. }
  1670. int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
  1671. struct netdev_queue *txq)
  1672. {
  1673. const struct net_device_ops *ops = dev->netdev_ops;
  1674. int rc = NETDEV_TX_OK;
  1675. if (likely(!skb->next)) {
  1676. if (!list_empty(&ptype_all))
  1677. dev_queue_xmit_nit(skb, dev);
  1678. /*
  1679. * If device doesnt need skb->dst, release it right now while
  1680. * its hot in this cpu cache
  1681. */
  1682. if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
  1683. skb_dst_drop(skb);
  1684. skb_orphan_try(skb);
  1685. if (netif_needs_gso(dev, skb)) {
  1686. if (unlikely(dev_gso_segment(skb)))
  1687. goto out_kfree_skb;
  1688. if (skb->next)
  1689. goto gso;
  1690. } else {
  1691. if (skb_needs_linearize(skb, dev) &&
  1692. __skb_linearize(skb))
  1693. goto out_kfree_skb;
  1694. /* If packet is not checksummed and device does not
  1695. * support checksumming for this protocol, complete
  1696. * checksumming here.
  1697. */
  1698. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1699. skb_set_transport_header(skb, skb->csum_start -
  1700. skb_headroom(skb));
  1701. if (!dev_can_checksum(dev, skb) &&
  1702. skb_checksum_help(skb))
  1703. goto out_kfree_skb;
  1704. }
  1705. }
  1706. rc = ops->ndo_start_xmit(skb, dev);
  1707. if (rc == NETDEV_TX_OK)
  1708. txq_trans_update(txq);
  1709. return rc;
  1710. }
  1711. gso:
  1712. do {
  1713. struct sk_buff *nskb = skb->next;
  1714. skb->next = nskb->next;
  1715. nskb->next = NULL;
  1716. /*
  1717. * If device doesnt need nskb->dst, release it right now while
  1718. * its hot in this cpu cache
  1719. */
  1720. if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
  1721. skb_dst_drop(nskb);
  1722. rc = ops->ndo_start_xmit(nskb, dev);
  1723. if (unlikely(rc != NETDEV_TX_OK)) {
  1724. if (rc & ~NETDEV_TX_MASK)
  1725. goto out_kfree_gso_skb;
  1726. nskb->next = skb->next;
  1727. skb->next = nskb;
  1728. return rc;
  1729. }
  1730. txq_trans_update(txq);
  1731. if (unlikely(netif_tx_queue_stopped(txq) && skb->next))
  1732. return NETDEV_TX_BUSY;
  1733. } while (skb->next);
  1734. out_kfree_gso_skb:
  1735. if (likely(skb->next == NULL))
  1736. skb->destructor = DEV_GSO_CB(skb)->destructor;
  1737. out_kfree_skb:
  1738. kfree_skb(skb);
  1739. return rc;
  1740. }
  1741. static u32 hashrnd __read_mostly;
  1742. u16 skb_tx_hash(const struct net_device *dev, const struct sk_buff *skb)
  1743. {
  1744. u32 hash;
  1745. if (skb_rx_queue_recorded(skb)) {
  1746. hash = skb_get_rx_queue(skb);
  1747. while (unlikely(hash >= dev->real_num_tx_queues))
  1748. hash -= dev->real_num_tx_queues;
  1749. return hash;
  1750. }
  1751. if (skb->sk && skb->sk->sk_hash)
  1752. hash = skb->sk->sk_hash;
  1753. else
  1754. hash = (__force u16) skb->protocol ^ skb->rxhash;
  1755. hash = jhash_1word(hash, hashrnd);
  1756. return (u16) (((u64) hash * dev->real_num_tx_queues) >> 32);
  1757. }
  1758. EXPORT_SYMBOL(skb_tx_hash);
  1759. static inline u16 dev_cap_txqueue(struct net_device *dev, u16 queue_index)
  1760. {
  1761. if (unlikely(queue_index >= dev->real_num_tx_queues)) {
  1762. if (net_ratelimit()) {
  1763. pr_warning("%s selects TX queue %d, but "
  1764. "real number of TX queues is %d\n",
  1765. dev->name, queue_index, dev->real_num_tx_queues);
  1766. }
  1767. return 0;
  1768. }
  1769. return queue_index;
  1770. }
  1771. static struct netdev_queue *dev_pick_tx(struct net_device *dev,
  1772. struct sk_buff *skb)
  1773. {
  1774. int queue_index;
  1775. struct sock *sk = skb->sk;
  1776. queue_index = sk_tx_queue_get(sk);
  1777. if (queue_index < 0) {
  1778. const struct net_device_ops *ops = dev->netdev_ops;
  1779. if (ops->ndo_select_queue) {
  1780. queue_index = ops->ndo_select_queue(dev, skb);
  1781. queue_index = dev_cap_txqueue(dev, queue_index);
  1782. } else {
  1783. queue_index = 0;
  1784. if (dev->real_num_tx_queues > 1)
  1785. queue_index = skb_tx_hash(dev, skb);
  1786. if (sk) {
  1787. struct dst_entry *dst = rcu_dereference_check(sk->sk_dst_cache, 1);
  1788. if (dst && skb_dst(skb) == dst)
  1789. sk_tx_queue_set(sk, queue_index);
  1790. }
  1791. }
  1792. }
  1793. skb_set_queue_mapping(skb, queue_index);
  1794. return netdev_get_tx_queue(dev, queue_index);
  1795. }
  1796. static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
  1797. struct net_device *dev,
  1798. struct netdev_queue *txq)
  1799. {
  1800. spinlock_t *root_lock = qdisc_lock(q);
  1801. bool contended = qdisc_is_running(q);
  1802. int rc;
  1803. /*
  1804. * Heuristic to force contended enqueues to serialize on a
  1805. * separate lock before trying to get qdisc main lock.
  1806. * This permits __QDISC_STATE_RUNNING owner to get the lock more often
  1807. * and dequeue packets faster.
  1808. */
  1809. if (unlikely(contended))
  1810. spin_lock(&q->busylock);
  1811. spin_lock(root_lock);
  1812. if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
  1813. kfree_skb(skb);
  1814. rc = NET_XMIT_DROP;
  1815. } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
  1816. qdisc_run_begin(q)) {
  1817. /*
  1818. * This is a work-conserving queue; there are no old skbs
  1819. * waiting to be sent out; and the qdisc is not running -
  1820. * xmit the skb directly.
  1821. */
  1822. if (!(dev->priv_flags & IFF_XMIT_DST_RELEASE))
  1823. skb_dst_force(skb);
  1824. __qdisc_update_bstats(q, skb->len);
  1825. if (sch_direct_xmit(skb, q, dev, txq, root_lock)) {
  1826. if (unlikely(contended)) {
  1827. spin_unlock(&q->busylock);
  1828. contended = false;
  1829. }
  1830. __qdisc_run(q);
  1831. } else
  1832. qdisc_run_end(q);
  1833. rc = NET_XMIT_SUCCESS;
  1834. } else {
  1835. skb_dst_force(skb);
  1836. rc = qdisc_enqueue_root(skb, q);
  1837. if (qdisc_run_begin(q)) {
  1838. if (unlikely(contended)) {
  1839. spin_unlock(&q->busylock);
  1840. contended = false;
  1841. }
  1842. __qdisc_run(q);
  1843. }
  1844. }
  1845. spin_unlock(root_lock);
  1846. if (unlikely(contended))
  1847. spin_unlock(&q->busylock);
  1848. return rc;
  1849. }
  1850. /**
  1851. * dev_queue_xmit - transmit a buffer
  1852. * @skb: buffer to transmit
  1853. *
  1854. * Queue a buffer for transmission to a network device. The caller must
  1855. * have set the device and priority and built the buffer before calling
  1856. * this function. The function can be called from an interrupt.
  1857. *
  1858. * A negative errno code is returned on a failure. A success does not
  1859. * guarantee the frame will be transmitted as it may be dropped due
  1860. * to congestion or traffic shaping.
  1861. *
  1862. * -----------------------------------------------------------------------------------
  1863. * I notice this method can also return errors from the queue disciplines,
  1864. * including NET_XMIT_DROP, which is a positive value. So, errors can also
  1865. * be positive.
  1866. *
  1867. * Regardless of the return value, the skb is consumed, so it is currently
  1868. * difficult to retry a send to this method. (You can bump the ref count
  1869. * before sending to hold a reference for retry if you are careful.)
  1870. *
  1871. * When calling this method, interrupts MUST be enabled. This is because
  1872. * the BH enable code must have IRQs enabled so that it will not deadlock.
  1873. * --BLG
  1874. */
  1875. int dev_queue_xmit(struct sk_buff *skb)
  1876. {
  1877. struct net_device *dev = skb->dev;
  1878. struct netdev_queue *txq;
  1879. struct Qdisc *q;
  1880. int rc = -ENOMEM;
  1881. /* Disable soft irqs for various locks below. Also
  1882. * stops preemption for RCU.
  1883. */
  1884. rcu_read_lock_bh();
  1885. txq = dev_pick_tx(dev, skb);
  1886. q = rcu_dereference_bh(txq->qdisc);
  1887. #ifdef CONFIG_NET_CLS_ACT
  1888. skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_EGRESS);
  1889. #endif
  1890. if (q->enqueue) {
  1891. rc = __dev_xmit_skb(skb, q, dev, txq);
  1892. goto out;
  1893. }
  1894. /* The device has no queue. Common case for software devices:
  1895. loopback, all the sorts of tunnels...
  1896. Really, it is unlikely that netif_tx_lock protection is necessary
  1897. here. (f.e. loopback and IP tunnels are clean ignoring statistics
  1898. counters.)
  1899. However, it is possible, that they rely on protection
  1900. made by us here.
  1901. Check this and shot the lock. It is not prone from deadlocks.
  1902. Either shot noqueue qdisc, it is even simpler 8)
  1903. */
  1904. if (dev->flags & IFF_UP) {
  1905. int cpu = smp_processor_id(); /* ok because BHs are off */
  1906. if (txq->xmit_lock_owner != cpu) {
  1907. HARD_TX_LOCK(dev, txq, cpu);
  1908. if (!netif_tx_queue_stopped(txq)) {
  1909. rc = dev_hard_start_xmit(skb, dev, txq);
  1910. if (dev_xmit_complete(rc)) {
  1911. HARD_TX_UNLOCK(dev, txq);
  1912. goto out;
  1913. }
  1914. }
  1915. HARD_TX_UNLOCK(dev, txq);
  1916. if (net_ratelimit())
  1917. printk(KERN_CRIT "Virtual device %s asks to "
  1918. "queue packet!\n", dev->name);
  1919. } else {
  1920. /* Recursion is detected! It is possible,
  1921. * unfortunately */
  1922. if (net_ratelimit())
  1923. printk(KERN_CRIT "Dead loop on virtual device "
  1924. "%s, fix it urgently!\n", dev->name);
  1925. }
  1926. }
  1927. rc = -ENETDOWN;
  1928. rcu_read_unlock_bh();
  1929. kfree_skb(skb);
  1930. return rc;
  1931. out:
  1932. rcu_read_unlock_bh();
  1933. return rc;
  1934. }
  1935. EXPORT_SYMBOL(dev_queue_xmit);
  1936. /*=======================================================================
  1937. Receiver routines
  1938. =======================================================================*/
  1939. int netdev_max_backlog __read_mostly = 1000;
  1940. int netdev_tstamp_prequeue __read_mostly = 1;
  1941. int netdev_budget __read_mostly = 300;
  1942. int weight_p __read_mostly = 64; /* old backlog weight */
  1943. /* Called with irq disabled */
  1944. static inline void ____napi_schedule(struct softnet_data *sd,
  1945. struct napi_struct *napi)
  1946. {
  1947. list_add_tail(&napi->poll_list, &sd->poll_list);
  1948. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  1949. }
  1950. /*
  1951. * __skb_get_rxhash: calculate a flow hash based on src/dst addresses
  1952. * and src/dst port numbers. Returns a non-zero hash number on success
  1953. * and 0 on failure.
  1954. */
  1955. __u32 __skb_get_rxhash(struct sk_buff *skb)
  1956. {
  1957. int nhoff, hash = 0, poff;
  1958. struct ipv6hdr *ip6;
  1959. struct iphdr *ip;
  1960. u8 ip_proto;
  1961. u32 addr1, addr2, ihl;
  1962. union {
  1963. u32 v32;
  1964. u16 v16[2];
  1965. } ports;
  1966. nhoff = skb_network_offset(skb);
  1967. switch (skb->protocol) {
  1968. case __constant_htons(ETH_P_IP):
  1969. if (!pskb_may_pull(skb, sizeof(*ip) + nhoff))
  1970. goto done;
  1971. ip = (struct iphdr *) (skb->data + nhoff);
  1972. if (ip->frag_off & htons(IP_MF | IP_OFFSET))
  1973. ip_proto = 0;
  1974. else
  1975. ip_proto = ip->protocol;
  1976. addr1 = (__force u32) ip->saddr;
  1977. addr2 = (__force u32) ip->daddr;
  1978. ihl = ip->ihl;
  1979. break;
  1980. case __constant_htons(ETH_P_IPV6):
  1981. if (!pskb_may_pull(skb, sizeof(*ip6) + nhoff))
  1982. goto done;
  1983. ip6 = (struct ipv6hdr *) (skb->data + nhoff);
  1984. ip_proto = ip6->nexthdr;
  1985. addr1 = (__force u32) ip6->saddr.s6_addr32[3];
  1986. addr2 = (__force u32) ip6->daddr.s6_addr32[3];
  1987. ihl = (40 >> 2);
  1988. break;
  1989. default:
  1990. goto done;
  1991. }
  1992. ports.v32 = 0;
  1993. poff = proto_ports_offset(ip_proto);
  1994. if (poff >= 0) {
  1995. nhoff += ihl * 4 + poff;
  1996. if (pskb_may_pull(skb, nhoff + 4)) {
  1997. ports.v32 = * (__force u32 *) (skb->data + nhoff);
  1998. if (ports.v16[1] < ports.v16[0])
  1999. swap(ports.v16[0], ports.v16[1]);
  2000. }
  2001. }
  2002. /* get a consistent hash (same value on both flow directions) */
  2003. if (addr2 < addr1)
  2004. swap(addr1, addr2);
  2005. hash = jhash_3words(addr1, addr2, ports.v32, hashrnd);
  2006. if (!hash)
  2007. hash = 1;
  2008. done:
  2009. return hash;
  2010. }
  2011. EXPORT_SYMBOL(__skb_get_rxhash);
  2012. #ifdef CONFIG_RPS
  2013. /* One global table that all flow-based protocols share. */
  2014. struct rps_sock_flow_table *rps_sock_flow_table __read_mostly;
  2015. EXPORT_SYMBOL(rps_sock_flow_table);
  2016. /*
  2017. * get_rps_cpu is called from netif_receive_skb and returns the target
  2018. * CPU from the RPS map of the receiving queue for a given skb.
  2019. * rcu_read_lock must be held on entry.
  2020. */
  2021. static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
  2022. struct rps_dev_flow **rflowp)
  2023. {
  2024. struct netdev_rx_queue *rxqueue;
  2025. struct rps_map *map = NULL;
  2026. struct rps_dev_flow_table *flow_table;
  2027. struct rps_sock_flow_table *sock_flow_table;
  2028. int cpu = -1;
  2029. u16 tcpu;
  2030. if (skb_rx_queue_recorded(skb)) {
  2031. u16 index = skb_get_rx_queue(skb);
  2032. if (unlikely(index >= dev->num_rx_queues)) {
  2033. WARN_ONCE(dev->num_rx_queues > 1, "%s received packet "
  2034. "on queue %u, but number of RX queues is %u\n",
  2035. dev->name, index, dev->num_rx_queues);
  2036. goto done;
  2037. }
  2038. rxqueue = dev->_rx + index;
  2039. } else
  2040. rxqueue = dev->_rx;
  2041. if (rxqueue->rps_map) {
  2042. map = rcu_dereference(rxqueue->rps_map);
  2043. if (map && map->len == 1) {
  2044. tcpu = map->cpus[0];
  2045. if (cpu_online(tcpu))
  2046. cpu = tcpu;
  2047. goto done;
  2048. }
  2049. } else if (!rxqueue->rps_flow_table) {
  2050. goto done;
  2051. }
  2052. skb_reset_network_header(skb);
  2053. if (!skb_get_rxhash(skb))
  2054. goto done;
  2055. flow_table = rcu_dereference(rxqueue->rps_flow_table);
  2056. sock_flow_table = rcu_dereference(rps_sock_flow_table);
  2057. if (flow_table && sock_flow_table) {
  2058. u16 next_cpu;
  2059. struct rps_dev_flow *rflow;
  2060. rflow = &flow_table->flows[skb->rxhash & flow_table->mask];
  2061. tcpu = rflow->cpu;
  2062. next_cpu = sock_flow_table->ents[skb->rxhash &
  2063. sock_flow_table->mask];
  2064. /*
  2065. * If the desired CPU (where last recvmsg was done) is
  2066. * different from current CPU (one in the rx-queue flow
  2067. * table entry), switch if one of the following holds:
  2068. * - Current CPU is unset (equal to RPS_NO_CPU).
  2069. * - Current CPU is offline.
  2070. * - The current CPU's queue tail has advanced beyond the
  2071. * last packet that was enqueued using this table entry.
  2072. * This guarantees that all previous packets for the flow
  2073. * have been dequeued, thus preserving in order delivery.
  2074. */
  2075. if (unlikely(tcpu != next_cpu) &&
  2076. (tcpu == RPS_NO_CPU || !cpu_online(tcpu) ||
  2077. ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
  2078. rflow->last_qtail)) >= 0)) {
  2079. tcpu = rflow->cpu = next_cpu;
  2080. if (tcpu != RPS_NO_CPU)
  2081. rflow->last_qtail = per_cpu(softnet_data,
  2082. tcpu).input_queue_head;
  2083. }
  2084. if (tcpu != RPS_NO_CPU && cpu_online(tcpu)) {
  2085. *rflowp = rflow;
  2086. cpu = tcpu;
  2087. goto done;
  2088. }
  2089. }
  2090. if (map) {
  2091. tcpu = map->cpus[((u64) skb->rxhash * map->len) >> 32];
  2092. if (cpu_online(tcpu)) {
  2093. cpu = tcpu;
  2094. goto done;
  2095. }
  2096. }
  2097. done:
  2098. return cpu;
  2099. }
  2100. /* Called from hardirq (IPI) context */
  2101. static void rps_trigger_softirq(void *data)
  2102. {
  2103. struct softnet_data *sd = data;
  2104. ____napi_schedule(sd, &sd->backlog);
  2105. sd->received_rps++;
  2106. }
  2107. #endif /* CONFIG_RPS */
  2108. /*
  2109. * Check if this softnet_data structure is another cpu one
  2110. * If yes, queue it to our IPI list and return 1
  2111. * If no, return 0
  2112. */
  2113. static int rps_ipi_queued(struct softnet_data *sd)
  2114. {
  2115. #ifdef CONFIG_RPS
  2116. struct softnet_data *mysd = &__get_cpu_var(softnet_data);
  2117. if (sd != mysd) {
  2118. sd->rps_ipi_next = mysd->rps_ipi_list;
  2119. mysd->rps_ipi_list = sd;
  2120. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  2121. return 1;
  2122. }
  2123. #endif /* CONFIG_RPS */
  2124. return 0;
  2125. }
  2126. /*
  2127. * enqueue_to_backlog is called to queue an skb to a per CPU backlog
  2128. * queue (may be a remote CPU queue).
  2129. */
  2130. static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
  2131. unsigned int *qtail)
  2132. {
  2133. struct softnet_data *sd;
  2134. unsigned long flags;
  2135. sd = &per_cpu(softnet_data, cpu);
  2136. local_irq_save(flags);
  2137. rps_lock(sd);
  2138. if (skb_queue_len(&sd->input_pkt_queue) <= netdev_max_backlog) {
  2139. if (skb_queue_len(&sd->input_pkt_queue)) {
  2140. enqueue:
  2141. __skb_queue_tail(&sd->input_pkt_queue, skb);
  2142. input_queue_tail_incr_save(sd, qtail);
  2143. rps_unlock(sd);
  2144. local_irq_restore(flags);
  2145. return NET_RX_SUCCESS;
  2146. }
  2147. /* Schedule NAPI for backlog device
  2148. * We can use non atomic operation since we own the queue lock
  2149. */
  2150. if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
  2151. if (!rps_ipi_queued(sd))
  2152. ____napi_schedule(sd, &sd->backlog);
  2153. }
  2154. goto enqueue;
  2155. }
  2156. sd->dropped++;
  2157. rps_unlock(sd);
  2158. local_irq_restore(flags);
  2159. kfree_skb(skb);
  2160. return NET_RX_DROP;
  2161. }
  2162. /**
  2163. * netif_rx - post buffer to the network code
  2164. * @skb: buffer to post
  2165. *
  2166. * This function receives a packet from a device driver and queues it for
  2167. * the upper (protocol) levels to process. It always succeeds. The buffer
  2168. * may be dropped during processing for congestion control or by the
  2169. * protocol layers.
  2170. *
  2171. * return values:
  2172. * NET_RX_SUCCESS (no congestion)
  2173. * NET_RX_DROP (packet was dropped)
  2174. *
  2175. */
  2176. int netif_rx(struct sk_buff *skb)
  2177. {
  2178. int ret;
  2179. /* if netpoll wants it, pretend we never saw it */
  2180. if (netpoll_rx(skb))
  2181. return NET_RX_DROP;
  2182. if (netdev_tstamp_prequeue)
  2183. net_timestamp_check(skb);
  2184. #ifdef CONFIG_RPS
  2185. {
  2186. struct rps_dev_flow voidflow, *rflow = &voidflow;
  2187. int cpu;
  2188. preempt_disable();
  2189. rcu_read_lock();
  2190. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  2191. if (cpu < 0)
  2192. cpu = smp_processor_id();
  2193. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  2194. rcu_read_unlock();
  2195. preempt_enable();
  2196. }
  2197. #else
  2198. {
  2199. unsigned int qtail;
  2200. ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
  2201. put_cpu();
  2202. }
  2203. #endif
  2204. return ret;
  2205. }
  2206. EXPORT_SYMBOL(netif_rx);
  2207. int netif_rx_ni(struct sk_buff *skb)
  2208. {
  2209. int err;
  2210. preempt_disable();
  2211. err = netif_rx(skb);
  2212. if (local_softirq_pending())
  2213. do_softirq();
  2214. preempt_enable();
  2215. return err;
  2216. }
  2217. EXPORT_SYMBOL(netif_rx_ni);
  2218. static void net_tx_action(struct softirq_action *h)
  2219. {
  2220. struct softnet_data *sd = &__get_cpu_var(softnet_data);
  2221. if (sd->completion_queue) {
  2222. struct sk_buff *clist;
  2223. local_irq_disable();
  2224. clist = sd->completion_queue;
  2225. sd->completion_queue = NULL;
  2226. local_irq_enable();
  2227. while (clist) {
  2228. struct sk_buff *skb = clist;
  2229. clist = clist->next;
  2230. WARN_ON(atomic_read(&skb->users));
  2231. __kfree_skb(skb);
  2232. }
  2233. }
  2234. if (sd->output_queue) {
  2235. struct Qdisc *head;
  2236. local_irq_disable();
  2237. head = sd->output_queue;
  2238. sd->output_queue = NULL;
  2239. sd->output_queue_tailp = &sd->output_queue;
  2240. local_irq_enable();
  2241. while (head) {
  2242. struct Qdisc *q = head;
  2243. spinlock_t *root_lock;
  2244. head = head->next_sched;
  2245. root_lock = qdisc_lock(q);
  2246. if (spin_trylock(root_lock)) {
  2247. smp_mb__before_clear_bit();
  2248. clear_bit(__QDISC_STATE_SCHED,
  2249. &q->state);
  2250. qdisc_run(q);
  2251. spin_unlock(root_lock);
  2252. } else {
  2253. if (!test_bit(__QDISC_STATE_DEACTIVATED,
  2254. &q->state)) {
  2255. __netif_reschedule(q);
  2256. } else {
  2257. smp_mb__before_clear_bit();
  2258. clear_bit(__QDISC_STATE_SCHED,
  2259. &q->state);
  2260. }
  2261. }
  2262. }
  2263. }
  2264. }
  2265. static inline int deliver_skb(struct sk_buff *skb,
  2266. struct packet_type *pt_prev,
  2267. struct net_device *orig_dev)
  2268. {
  2269. atomic_inc(&skb->users);
  2270. return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
  2271. }
  2272. #if (defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)) && \
  2273. (defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE))
  2274. /* This hook is defined here for ATM LANE */
  2275. int (*br_fdb_test_addr_hook)(struct net_device *dev,
  2276. unsigned char *addr) __read_mostly;
  2277. EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
  2278. #endif
  2279. #ifdef CONFIG_NET_CLS_ACT
  2280. /* TODO: Maybe we should just force sch_ingress to be compiled in
  2281. * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
  2282. * a compare and 2 stores extra right now if we dont have it on
  2283. * but have CONFIG_NET_CLS_ACT
  2284. * NOTE: This doesnt stop any functionality; if you dont have
  2285. * the ingress scheduler, you just cant add policies on ingress.
  2286. *
  2287. */
  2288. static int ing_filter(struct sk_buff *skb)
  2289. {
  2290. struct net_device *dev = skb->dev;
  2291. u32 ttl = G_TC_RTTL(skb->tc_verd);
  2292. struct netdev_queue *rxq;
  2293. int result = TC_ACT_OK;
  2294. struct Qdisc *q;
  2295. if (unlikely(MAX_RED_LOOP < ttl++)) {
  2296. if (net_ratelimit())
  2297. pr_warning( "Redir loop detected Dropping packet (%d->%d)\n",
  2298. skb->skb_iif, dev->ifindex);
  2299. return TC_ACT_SHOT;
  2300. }
  2301. skb->tc_verd = SET_TC_RTTL(skb->tc_verd, ttl);
  2302. skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_INGRESS);
  2303. rxq = &dev->rx_queue;
  2304. q = rxq->qdisc;
  2305. if (q != &noop_qdisc) {
  2306. spin_lock(qdisc_lock(q));
  2307. if (likely(!test_bit(__QDISC_STATE_DEACTIVATED, &q->state)))
  2308. result = qdisc_enqueue_root(skb, q);
  2309. spin_unlock(qdisc_lock(q));
  2310. }
  2311. return result;
  2312. }
  2313. static inline struct sk_buff *handle_ing(struct sk_buff *skb,
  2314. struct packet_type **pt_prev,
  2315. int *ret, struct net_device *orig_dev)
  2316. {
  2317. if (skb->dev->rx_queue.qdisc == &noop_qdisc)
  2318. goto out;
  2319. if (*pt_prev) {
  2320. *ret = deliver_skb(skb, *pt_prev, orig_dev);
  2321. *pt_prev = NULL;
  2322. }
  2323. switch (ing_filter(skb)) {
  2324. case TC_ACT_SHOT:
  2325. case TC_ACT_STOLEN:
  2326. kfree_skb(skb);
  2327. return NULL;
  2328. }
  2329. out:
  2330. skb->tc_verd = 0;
  2331. return skb;
  2332. }
  2333. #endif
  2334. /*
  2335. * netif_nit_deliver - deliver received packets to network taps
  2336. * @skb: buffer
  2337. *
  2338. * This function is used to deliver incoming packets to network
  2339. * taps. It should be used when the normal netif_receive_skb path
  2340. * is bypassed, for example because of VLAN acceleration.
  2341. */
  2342. void netif_nit_deliver(struct sk_buff *skb)
  2343. {
  2344. struct packet_type *ptype;
  2345. if (list_empty(&ptype_all))
  2346. return;
  2347. skb_reset_network_header(skb);
  2348. skb_reset_transport_header(skb);
  2349. skb->mac_len = skb->network_header - skb->mac_header;
  2350. rcu_read_lock();
  2351. list_for_each_entry_rcu(ptype, &ptype_all, list) {
  2352. if (!ptype->dev || ptype->dev == skb->dev)
  2353. deliver_skb(skb, ptype, skb->dev);
  2354. }
  2355. rcu_read_unlock();
  2356. }
  2357. /**
  2358. * netdev_rx_handler_register - register receive handler
  2359. * @dev: device to register a handler for
  2360. * @rx_handler: receive handler to register
  2361. * @rx_handler_data: data pointer that is used by rx handler
  2362. *
  2363. * Register a receive hander for a device. This handler will then be
  2364. * called from __netif_receive_skb. A negative errno code is returned
  2365. * on a failure.
  2366. *
  2367. * The caller must hold the rtnl_mutex.
  2368. */
  2369. int netdev_rx_handler_register(struct net_device *dev,
  2370. rx_handler_func_t *rx_handler,
  2371. void *rx_handler_data)
  2372. {
  2373. ASSERT_RTNL();
  2374. if (dev->rx_handler)
  2375. return -EBUSY;
  2376. rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
  2377. rcu_assign_pointer(dev->rx_handler, rx_handler);
  2378. return 0;
  2379. }
  2380. EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
  2381. /**
  2382. * netdev_rx_handler_unregister - unregister receive handler
  2383. * @dev: device to unregister a handler from
  2384. *
  2385. * Unregister a receive hander from a device.
  2386. *
  2387. * The caller must hold the rtnl_mutex.
  2388. */
  2389. void netdev_rx_handler_unregister(struct net_device *dev)
  2390. {
  2391. ASSERT_RTNL();
  2392. rcu_assign_pointer(dev->rx_handler, NULL);
  2393. rcu_assign_pointer(dev->rx_handler_data, NULL);
  2394. }
  2395. EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
  2396. static inline void skb_bond_set_mac_by_master(struct sk_buff *skb,
  2397. struct net_device *master)
  2398. {
  2399. if (skb->pkt_type == PACKET_HOST) {
  2400. u16 *dest = (u16 *) eth_hdr(skb)->h_dest;
  2401. memcpy(dest, master->dev_addr, ETH_ALEN);
  2402. }
  2403. }
  2404. /* On bonding slaves other than the currently active slave, suppress
  2405. * duplicates except for 802.3ad ETH_P_SLOW, alb non-mcast/bcast, and
  2406. * ARP on active-backup slaves with arp_validate enabled.
  2407. */
  2408. int __skb_bond_should_drop(struct sk_buff *skb, struct net_device *master)
  2409. {
  2410. struct net_device *dev = skb->dev;
  2411. if (master->priv_flags & IFF_MASTER_ARPMON)
  2412. dev->last_rx = jiffies;
  2413. if ((master->priv_flags & IFF_MASTER_ALB) &&
  2414. (master->priv_flags & IFF_BRIDGE_PORT)) {
  2415. /* Do address unmangle. The local destination address
  2416. * will be always the one master has. Provides the right
  2417. * functionality in a bridge.
  2418. */
  2419. skb_bond_set_mac_by_master(skb, master);
  2420. }
  2421. if (dev->priv_flags & IFF_SLAVE_INACTIVE) {
  2422. if ((dev->priv_flags & IFF_SLAVE_NEEDARP) &&
  2423. skb->protocol == __cpu_to_be16(ETH_P_ARP))
  2424. return 0;
  2425. if (master->priv_flags & IFF_MASTER_ALB) {
  2426. if (skb->pkt_type != PACKET_BROADCAST &&
  2427. skb->pkt_type != PACKET_MULTICAST)
  2428. return 0;
  2429. }
  2430. if (master->priv_flags & IFF_MASTER_8023AD &&
  2431. skb->protocol == __cpu_to_be16(ETH_P_SLOW))
  2432. return 0;
  2433. return 1;
  2434. }
  2435. return 0;
  2436. }
  2437. EXPORT_SYMBOL(__skb_bond_should_drop);
  2438. static int __netif_receive_skb(struct sk_buff *skb)
  2439. {
  2440. struct packet_type *ptype, *pt_prev;
  2441. rx_handler_func_t *rx_handler;
  2442. struct net_device *orig_dev;
  2443. struct net_device *master;
  2444. struct net_device *null_or_orig;
  2445. struct net_device *orig_or_bond;
  2446. int ret = NET_RX_DROP;
  2447. __be16 type;
  2448. if (!netdev_tstamp_prequeue)
  2449. net_timestamp_check(skb);
  2450. if (vlan_tx_tag_present(skb))
  2451. vlan_hwaccel_do_receive(skb);
  2452. /* if we've gotten here through NAPI, check netpoll */
  2453. if (netpoll_receive_skb(skb))
  2454. return NET_RX_DROP;
  2455. if (!skb->skb_iif)
  2456. skb->skb_iif = skb->dev->ifindex;
  2457. /*
  2458. * bonding note: skbs received on inactive slaves should only
  2459. * be delivered to pkt handlers that are exact matches. Also
  2460. * the deliver_no_wcard flag will be set. If packet handlers
  2461. * are sensitive to duplicate packets these skbs will need to
  2462. * be dropped at the handler. The vlan accel path may have
  2463. * already set the deliver_no_wcard flag.
  2464. */
  2465. null_or_orig = NULL;
  2466. orig_dev = skb->dev;
  2467. master = ACCESS_ONCE(orig_dev->master);
  2468. if (skb->deliver_no_wcard)
  2469. null_or_orig = orig_dev;
  2470. else if (master) {
  2471. if (skb_bond_should_drop(skb, master)) {
  2472. skb->deliver_no_wcard = 1;
  2473. null_or_orig = orig_dev; /* deliver only exact match */
  2474. } else
  2475. skb->dev = master;
  2476. }
  2477. __this_cpu_inc(softnet_data.processed);
  2478. skb_reset_network_header(skb);
  2479. skb_reset_transport_header(skb);
  2480. skb->mac_len = skb->network_header - skb->mac_header;
  2481. pt_prev = NULL;
  2482. rcu_read_lock();
  2483. #ifdef CONFIG_NET_CLS_ACT
  2484. if (skb->tc_verd & TC_NCLS) {
  2485. skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
  2486. goto ncls;
  2487. }
  2488. #endif
  2489. list_for_each_entry_rcu(ptype, &ptype_all, list) {
  2490. if (ptype->dev == null_or_orig || ptype->dev == skb->dev ||
  2491. ptype->dev == orig_dev) {
  2492. if (pt_prev)
  2493. ret = deliver_skb(skb, pt_prev, orig_dev);
  2494. pt_prev = ptype;
  2495. }
  2496. }
  2497. #ifdef CONFIG_NET_CLS_ACT
  2498. skb = handle_ing(skb, &pt_prev, &ret, orig_dev);
  2499. if (!skb)
  2500. goto out;
  2501. ncls:
  2502. #endif
  2503. /* Handle special case of bridge or macvlan */
  2504. rx_handler = rcu_dereference(skb->dev->rx_handler);
  2505. if (rx_handler) {
  2506. if (pt_prev) {
  2507. ret = deliver_skb(skb, pt_prev, orig_dev);
  2508. pt_prev = NULL;
  2509. }
  2510. skb = rx_handler(skb);
  2511. if (!skb)
  2512. goto out;
  2513. }
  2514. /*
  2515. * Make sure frames received on VLAN interfaces stacked on
  2516. * bonding interfaces still make their way to any base bonding
  2517. * device that may have registered for a specific ptype. The
  2518. * handler may have to adjust skb->dev and orig_dev.
  2519. */
  2520. orig_or_bond = orig_dev;
  2521. if ((skb->dev->priv_flags & IFF_802_1Q_VLAN) &&
  2522. (vlan_dev_real_dev(skb->dev)->priv_flags & IFF_BONDING)) {
  2523. orig_or_bond = vlan_dev_real_dev(skb->dev);
  2524. }
  2525. type = skb->protocol;
  2526. list_for_each_entry_rcu(ptype,
  2527. &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
  2528. if (ptype->type == type && (ptype->dev == null_or_orig ||
  2529. ptype->dev == skb->dev || ptype->dev == orig_dev ||
  2530. ptype->dev == orig_or_bond)) {
  2531. if (pt_prev)
  2532. ret = deliver_skb(skb, pt_prev, orig_dev);
  2533. pt_prev = ptype;
  2534. }
  2535. }
  2536. if (pt_prev) {
  2537. ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
  2538. } else {
  2539. kfree_skb(skb);
  2540. /* Jamal, now you will not able to escape explaining
  2541. * me how you were going to use this. :-)
  2542. */
  2543. ret = NET_RX_DROP;
  2544. }
  2545. out:
  2546. rcu_read_unlock();
  2547. return ret;
  2548. }
  2549. /**
  2550. * netif_receive_skb - process receive buffer from network
  2551. * @skb: buffer to process
  2552. *
  2553. * netif_receive_skb() is the main receive data processing function.
  2554. * It always succeeds. The buffer may be dropped during processing
  2555. * for congestion control or by the protocol layers.
  2556. *
  2557. * This function may only be called from softirq context and interrupts
  2558. * should be enabled.
  2559. *
  2560. * Return values (usually ignored):
  2561. * NET_RX_SUCCESS: no congestion
  2562. * NET_RX_DROP: packet was dropped
  2563. */
  2564. int netif_receive_skb(struct sk_buff *skb)
  2565. {
  2566. if (netdev_tstamp_prequeue)
  2567. net_timestamp_check(skb);
  2568. if (skb_defer_rx_timestamp(skb))
  2569. return NET_RX_SUCCESS;
  2570. #ifdef CONFIG_RPS
  2571. {
  2572. struct rps_dev_flow voidflow, *rflow = &voidflow;
  2573. int cpu, ret;
  2574. rcu_read_lock();
  2575. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  2576. if (cpu >= 0) {
  2577. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  2578. rcu_read_unlock();
  2579. } else {
  2580. rcu_read_unlock();
  2581. ret = __netif_receive_skb(skb);
  2582. }
  2583. return ret;
  2584. }
  2585. #else
  2586. return __netif_receive_skb(skb);
  2587. #endif
  2588. }
  2589. EXPORT_SYMBOL(netif_receive_skb);
  2590. /* Network device is going away, flush any packets still pending
  2591. * Called with irqs disabled.
  2592. */
  2593. static void flush_backlog(void *arg)
  2594. {
  2595. struct net_device *dev = arg;
  2596. struct softnet_data *sd = &__get_cpu_var(softnet_data);
  2597. struct sk_buff *skb, *tmp;
  2598. rps_lock(sd);
  2599. skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
  2600. if (skb->dev == dev) {
  2601. __skb_unlink(skb, &sd->input_pkt_queue);
  2602. kfree_skb(skb);
  2603. input_queue_head_incr(sd);
  2604. }
  2605. }
  2606. rps_unlock(sd);
  2607. skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
  2608. if (skb->dev == dev) {
  2609. __skb_unlink(skb, &sd->process_queue);
  2610. kfree_skb(skb);
  2611. input_queue_head_incr(sd);
  2612. }
  2613. }
  2614. }
  2615. static int napi_gro_complete(struct sk_buff *skb)
  2616. {
  2617. struct packet_type *ptype;
  2618. __be16 type = skb->protocol;
  2619. struct list_head *head = &ptype_base[ntohs(type) & PTYPE_HASH_MASK];
  2620. int err = -ENOENT;
  2621. if (NAPI_GRO_CB(skb)->count == 1) {
  2622. skb_shinfo(skb)->gso_size = 0;
  2623. goto out;
  2624. }
  2625. rcu_read_lock();
  2626. list_for_each_entry_rcu(ptype, head, list) {
  2627. if (ptype->type != type || ptype->dev || !ptype->gro_complete)
  2628. continue;
  2629. err = ptype->gro_complete(skb);
  2630. break;
  2631. }
  2632. rcu_read_unlock();
  2633. if (err) {
  2634. WARN_ON(&ptype->list == head);
  2635. kfree_skb(skb);
  2636. return NET_RX_SUCCESS;
  2637. }
  2638. out:
  2639. return netif_receive_skb(skb);
  2640. }
  2641. inline void napi_gro_flush(struct napi_struct *napi)
  2642. {
  2643. struct sk_buff *skb, *next;
  2644. for (skb = napi->gro_list; skb; skb = next) {
  2645. next = skb->next;
  2646. skb->next = NULL;
  2647. napi_gro_complete(skb);
  2648. }
  2649. napi->gro_count = 0;
  2650. napi->gro_list = NULL;
  2651. }
  2652. EXPORT_SYMBOL(napi_gro_flush);
  2653. enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
  2654. {
  2655. struct sk_buff **pp = NULL;
  2656. struct packet_type *ptype;
  2657. __be16 type = skb->protocol;
  2658. struct list_head *head = &ptype_base[ntohs(type) & PTYPE_HASH_MASK];
  2659. int same_flow;
  2660. int mac_len;
  2661. enum gro_result ret;
  2662. if (!(skb->dev->features & NETIF_F_GRO) || netpoll_rx_on(skb))
  2663. goto normal;
  2664. if (skb_is_gso(skb) || skb_has_frag_list(skb))
  2665. goto normal;
  2666. rcu_read_lock();
  2667. list_for_each_entry_rcu(ptype, head, list) {
  2668. if (ptype->type != type || ptype->dev || !ptype->gro_receive)
  2669. continue;
  2670. skb_set_network_header(skb, skb_gro_offset(skb));
  2671. mac_len = skb->network_header - skb->mac_header;
  2672. skb->mac_len = mac_len;
  2673. NAPI_GRO_CB(skb)->same_flow = 0;
  2674. NAPI_GRO_CB(skb)->flush = 0;
  2675. NAPI_GRO_CB(skb)->free = 0;
  2676. pp = ptype->gro_receive(&napi->gro_list, skb);
  2677. break;
  2678. }
  2679. rcu_read_unlock();
  2680. if (&ptype->list == head)
  2681. goto normal;
  2682. same_flow = NAPI_GRO_CB(skb)->same_flow;
  2683. ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
  2684. if (pp) {
  2685. struct sk_buff *nskb = *pp;
  2686. *pp = nskb->next;
  2687. nskb->next = NULL;
  2688. napi_gro_complete(nskb);
  2689. napi->gro_count--;
  2690. }
  2691. if (same_flow)
  2692. goto ok;
  2693. if (NAPI_GRO_CB(skb)->flush || napi->gro_count >= MAX_GRO_SKBS)
  2694. goto normal;
  2695. napi->gro_count++;
  2696. NAPI_GRO_CB(skb)->count = 1;
  2697. skb_shinfo(skb)->gso_size = skb_gro_len(skb);
  2698. skb->next = napi->gro_list;
  2699. napi->gro_list = skb;
  2700. ret = GRO_HELD;
  2701. pull:
  2702. if (skb_headlen(skb) < skb_gro_offset(skb)) {
  2703. int grow = skb_gro_offset(skb) - skb_headlen(skb);
  2704. BUG_ON(skb->end - skb->tail < grow);
  2705. memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
  2706. skb->tail += grow;
  2707. skb->data_len -= grow;
  2708. skb_shinfo(skb)->frags[0].page_offset += grow;
  2709. skb_shinfo(skb)->frags[0].size -= grow;
  2710. if (unlikely(!skb_shinfo(skb)->frags[0].size)) {
  2711. put_page(skb_shinfo(skb)->frags[0].page);
  2712. memmove(skb_shinfo(skb)->frags,
  2713. skb_shinfo(skb)->frags + 1,
  2714. --skb_shinfo(skb)->nr_frags * sizeof(skb_frag_t));
  2715. }
  2716. }
  2717. ok:
  2718. return ret;
  2719. normal:
  2720. ret = GRO_NORMAL;
  2721. goto pull;
  2722. }
  2723. EXPORT_SYMBOL(dev_gro_receive);
  2724. static inline gro_result_t
  2725. __napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
  2726. {
  2727. struct sk_buff *p;
  2728. for (p = napi->gro_list; p; p = p->next) {
  2729. unsigned long diffs;
  2730. diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
  2731. diffs |= compare_ether_header(skb_mac_header(p),
  2732. skb_gro_mac_header(skb));
  2733. NAPI_GRO_CB(p)->same_flow = !diffs;
  2734. NAPI_GRO_CB(p)->flush = 0;
  2735. }
  2736. return dev_gro_receive(napi, skb);
  2737. }
  2738. gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
  2739. {
  2740. switch (ret) {
  2741. case GRO_NORMAL:
  2742. if (netif_receive_skb(skb))
  2743. ret = GRO_DROP;
  2744. break;
  2745. case GRO_DROP:
  2746. case GRO_MERGED_FREE:
  2747. kfree_skb(skb);
  2748. break;
  2749. case GRO_HELD:
  2750. case GRO_MERGED:
  2751. break;
  2752. }
  2753. return ret;
  2754. }
  2755. EXPORT_SYMBOL(napi_skb_finish);
  2756. void skb_gro_reset_offset(struct sk_buff *skb)
  2757. {
  2758. NAPI_GRO_CB(skb)->data_offset = 0;
  2759. NAPI_GRO_CB(skb)->frag0 = NULL;
  2760. NAPI_GRO_CB(skb)->frag0_len = 0;
  2761. if (skb->mac_header == skb->tail &&
  2762. !PageHighMem(skb_shinfo(skb)->frags[0].page)) {
  2763. NAPI_GRO_CB(skb)->frag0 =
  2764. page_address(skb_shinfo(skb)->frags[0].page) +
  2765. skb_shinfo(skb)->frags[0].page_offset;
  2766. NAPI_GRO_CB(skb)->frag0_len = skb_shinfo(skb)->frags[0].size;
  2767. }
  2768. }
  2769. EXPORT_SYMBOL(skb_gro_reset_offset);
  2770. gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
  2771. {
  2772. skb_gro_reset_offset(skb);
  2773. return napi_skb_finish(__napi_gro_receive(napi, skb), skb);
  2774. }
  2775. EXPORT_SYMBOL(napi_gro_receive);
  2776. void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
  2777. {
  2778. __skb_pull(skb, skb_headlen(skb));
  2779. skb_reserve(skb, NET_IP_ALIGN - skb_headroom(skb));
  2780. napi->skb = skb;
  2781. }
  2782. EXPORT_SYMBOL(napi_reuse_skb);
  2783. struct sk_buff *napi_get_frags(struct napi_struct *napi)
  2784. {
  2785. struct sk_buff *skb = napi->skb;
  2786. if (!skb) {
  2787. skb = netdev_alloc_skb_ip_align(napi->dev, GRO_MAX_HEAD);
  2788. if (skb)
  2789. napi->skb = skb;
  2790. }
  2791. return skb;
  2792. }
  2793. EXPORT_SYMBOL(napi_get_frags);
  2794. gro_result_t napi_frags_finish(struct napi_struct *napi, struct sk_buff *skb,
  2795. gro_result_t ret)
  2796. {
  2797. switch (ret) {
  2798. case GRO_NORMAL:
  2799. case GRO_HELD:
  2800. skb->protocol = eth_type_trans(skb, skb->dev);
  2801. if (ret == GRO_HELD)
  2802. skb_gro_pull(skb, -ETH_HLEN);
  2803. else if (netif_receive_skb(skb))
  2804. ret = GRO_DROP;
  2805. break;
  2806. case GRO_DROP:
  2807. case GRO_MERGED_FREE:
  2808. napi_reuse_skb(napi, skb);
  2809. break;
  2810. case GRO_MERGED:
  2811. break;
  2812. }
  2813. return ret;
  2814. }
  2815. EXPORT_SYMBOL(napi_frags_finish);
  2816. struct sk_buff *napi_frags_skb(struct napi_struct *napi)
  2817. {
  2818. struct sk_buff *skb = napi->skb;
  2819. struct ethhdr *eth;
  2820. unsigned int hlen;
  2821. unsigned int off;
  2822. napi->skb = NULL;
  2823. skb_reset_mac_header(skb);
  2824. skb_gro_reset_offset(skb);
  2825. off = skb_gro_offset(skb);
  2826. hlen = off + sizeof(*eth);
  2827. eth = skb_gro_header_fast(skb, off);
  2828. if (skb_gro_header_hard(skb, hlen)) {
  2829. eth = skb_gro_header_slow(skb, hlen, off);
  2830. if (unlikely(!eth)) {
  2831. napi_reuse_skb(napi, skb);
  2832. skb = NULL;
  2833. goto out;
  2834. }
  2835. }
  2836. skb_gro_pull(skb, sizeof(*eth));
  2837. /*
  2838. * This works because the only protocols we care about don't require
  2839. * special handling. We'll fix it up properly at the end.
  2840. */
  2841. skb->protocol = eth->h_proto;
  2842. out:
  2843. return skb;
  2844. }
  2845. EXPORT_SYMBOL(napi_frags_skb);
  2846. gro_result_t napi_gro_frags(struct napi_struct *napi)
  2847. {
  2848. struct sk_buff *skb = napi_frags_skb(napi);
  2849. if (!skb)
  2850. return GRO_DROP;
  2851. return napi_frags_finish(napi, skb, __napi_gro_receive(napi, skb));
  2852. }
  2853. EXPORT_SYMBOL(napi_gro_frags);
  2854. /*
  2855. * net_rps_action sends any pending IPI's for rps.
  2856. * Note: called with local irq disabled, but exits with local irq enabled.
  2857. */
  2858. static void net_rps_action_and_irq_enable(struct softnet_data *sd)
  2859. {
  2860. #ifdef CONFIG_RPS
  2861. struct softnet_data *remsd = sd->rps_ipi_list;
  2862. if (remsd) {
  2863. sd->rps_ipi_list = NULL;
  2864. local_irq_enable();
  2865. /* Send pending IPI's to kick RPS processing on remote cpus. */
  2866. while (remsd) {
  2867. struct softnet_data *next = remsd->rps_ipi_next;
  2868. if (cpu_online(remsd->cpu))
  2869. __smp_call_function_single(remsd->cpu,
  2870. &remsd->csd, 0);
  2871. remsd = next;
  2872. }
  2873. } else
  2874. #endif
  2875. local_irq_enable();
  2876. }
  2877. static int process_backlog(struct napi_struct *napi, int quota)
  2878. {
  2879. int work = 0;
  2880. struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
  2881. #ifdef CONFIG_RPS
  2882. /* Check if we have pending ipi, its better to send them now,
  2883. * not waiting net_rx_action() end.
  2884. */
  2885. if (sd->rps_ipi_list) {
  2886. local_irq_disable();
  2887. net_rps_action_and_irq_enable(sd);
  2888. }
  2889. #endif
  2890. napi->weight = weight_p;
  2891. local_irq_disable();
  2892. while (work < quota) {
  2893. struct sk_buff *skb;
  2894. unsigned int qlen;
  2895. while ((skb = __skb_dequeue(&sd->process_queue))) {
  2896. local_irq_enable();
  2897. __netif_receive_skb(skb);
  2898. local_irq_disable();
  2899. input_queue_head_incr(sd);
  2900. if (++work >= quota) {
  2901. local_irq_enable();
  2902. return work;
  2903. }
  2904. }
  2905. rps_lock(sd);
  2906. qlen = skb_queue_len(&sd->input_pkt_queue);
  2907. if (qlen)
  2908. skb_queue_splice_tail_init(&sd->input_pkt_queue,
  2909. &sd->process_queue);
  2910. if (qlen < quota - work) {
  2911. /*
  2912. * Inline a custom version of __napi_complete().
  2913. * only current cpu owns and manipulates this napi,
  2914. * and NAPI_STATE_SCHED is the only possible flag set on backlog.
  2915. * we can use a plain write instead of clear_bit(),
  2916. * and we dont need an smp_mb() memory barrier.
  2917. */
  2918. list_del(&napi->poll_list);
  2919. napi->state = 0;
  2920. quota = work + qlen;
  2921. }
  2922. rps_unlock(sd);
  2923. }
  2924. local_irq_enable();
  2925. return work;
  2926. }
  2927. /**
  2928. * __napi_schedule - schedule for receive
  2929. * @n: entry to schedule
  2930. *
  2931. * The entry's receive function will be scheduled to run
  2932. */
  2933. void __napi_schedule(struct napi_struct *n)
  2934. {
  2935. unsigned long flags;
  2936. local_irq_save(flags);
  2937. ____napi_schedule(&__get_cpu_var(softnet_data), n);
  2938. local_irq_restore(flags);
  2939. }
  2940. EXPORT_SYMBOL(__napi_schedule);
  2941. void __napi_complete(struct napi_struct *n)
  2942. {
  2943. BUG_ON(!test_bit(NAPI_STATE_SCHED, &n->state));
  2944. BUG_ON(n->gro_list);
  2945. list_del(&n->poll_list);
  2946. smp_mb__before_clear_bit();
  2947. clear_bit(NAPI_STATE_SCHED, &n->state);
  2948. }
  2949. EXPORT_SYMBOL(__napi_complete);
  2950. void napi_complete(struct napi_struct *n)
  2951. {
  2952. unsigned long flags;
  2953. /*
  2954. * don't let napi dequeue from the cpu poll list
  2955. * just in case its running on a different cpu
  2956. */
  2957. if (unlikely(test_bit(NAPI_STATE_NPSVC, &n->state)))
  2958. return;
  2959. napi_gro_flush(n);
  2960. local_irq_save(flags);
  2961. __napi_complete(n);
  2962. local_irq_restore(flags);
  2963. }
  2964. EXPORT_SYMBOL(napi_complete);
  2965. void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
  2966. int (*poll)(struct napi_struct *, int), int weight)
  2967. {
  2968. INIT_LIST_HEAD(&napi->poll_list);
  2969. napi->gro_count = 0;
  2970. napi->gro_list = NULL;
  2971. napi->skb = NULL;
  2972. napi->poll = poll;
  2973. napi->weight = weight;
  2974. list_add(&napi->dev_list, &dev->napi_list);
  2975. napi->dev = dev;
  2976. #ifdef CONFIG_NETPOLL
  2977. spin_lock_init(&napi->poll_lock);
  2978. napi->poll_owner = -1;
  2979. #endif
  2980. set_bit(NAPI_STATE_SCHED, &napi->state);
  2981. }
  2982. EXPORT_SYMBOL(netif_napi_add);
  2983. void netif_napi_del(struct napi_struct *napi)
  2984. {
  2985. struct sk_buff *skb, *next;
  2986. list_del_init(&napi->dev_list);
  2987. napi_free_frags(napi);
  2988. for (skb = napi->gro_list; skb; skb = next) {
  2989. next = skb->next;
  2990. skb->next = NULL;
  2991. kfree_skb(skb);
  2992. }
  2993. napi->gro_list = NULL;
  2994. napi->gro_count = 0;
  2995. }
  2996. EXPORT_SYMBOL(netif_napi_del);
  2997. static void net_rx_action(struct softirq_action *h)
  2998. {
  2999. struct softnet_data *sd = &__get_cpu_var(softnet_data);
  3000. unsigned long time_limit = jiffies + 2;
  3001. int budget = netdev_budget;
  3002. void *have;
  3003. local_irq_disable();
  3004. while (!list_empty(&sd->poll_list)) {
  3005. struct napi_struct *n;
  3006. int work, weight;
  3007. /* If softirq window is exhuasted then punt.
  3008. * Allow this to run for 2 jiffies since which will allow
  3009. * an average latency of 1.5/HZ.
  3010. */
  3011. if (unlikely(budget <= 0 || time_after(jiffies, time_limit)))
  3012. goto softnet_break;
  3013. local_irq_enable();
  3014. /* Even though interrupts have been re-enabled, this
  3015. * access is safe because interrupts can only add new
  3016. * entries to the tail of this list, and only ->poll()
  3017. * calls can remove this head entry from the list.
  3018. */
  3019. n = list_first_entry(&sd->poll_list, struct napi_struct, poll_list);
  3020. have = netpoll_poll_lock(n);
  3021. weight = n->weight;
  3022. /* This NAPI_STATE_SCHED test is for avoiding a race
  3023. * with netpoll's poll_napi(). Only the entity which
  3024. * obtains the lock and sees NAPI_STATE_SCHED set will
  3025. * actually make the ->poll() call. Therefore we avoid
  3026. * accidently calling ->poll() when NAPI is not scheduled.
  3027. */
  3028. work = 0;
  3029. if (test_bit(NAPI_STATE_SCHED, &n->state)) {
  3030. work = n->poll(n, weight);
  3031. trace_napi_poll(n);
  3032. }
  3033. WARN_ON_ONCE(work > weight);
  3034. budget -= work;
  3035. local_irq_disable();
  3036. /* Drivers must not modify the NAPI state if they
  3037. * consume the entire weight. In such cases this code
  3038. * still "owns" the NAPI instance and therefore can
  3039. * move the instance around on the list at-will.
  3040. */
  3041. if (unlikely(work == weight)) {
  3042. if (unlikely(napi_disable_pending(n))) {
  3043. local_irq_enable();
  3044. napi_complete(n);
  3045. local_irq_disable();
  3046. } else
  3047. list_move_tail(&n->poll_list, &sd->poll_list);
  3048. }
  3049. netpoll_poll_unlock(have);
  3050. }
  3051. out:
  3052. net_rps_action_and_irq_enable(sd);
  3053. #ifdef CONFIG_NET_DMA
  3054. /*
  3055. * There may not be any more sk_buffs coming right now, so push
  3056. * any pending DMA copies to hardware
  3057. */
  3058. dma_issue_pending_all();
  3059. #endif
  3060. return;
  3061. softnet_break:
  3062. sd->time_squeeze++;
  3063. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  3064. goto out;
  3065. }
  3066. static gifconf_func_t *gifconf_list[NPROTO];
  3067. /**
  3068. * register_gifconf - register a SIOCGIF handler
  3069. * @family: Address family
  3070. * @gifconf: Function handler
  3071. *
  3072. * Register protocol dependent address dumping routines. The handler
  3073. * that is passed must not be freed or reused until it has been replaced
  3074. * by another handler.
  3075. */
  3076. int register_gifconf(unsigned int family, gifconf_func_t *gifconf)
  3077. {
  3078. if (family >= NPROTO)
  3079. return -EINVAL;
  3080. gifconf_list[family] = gifconf;
  3081. return 0;
  3082. }
  3083. EXPORT_SYMBOL(register_gifconf);
  3084. /*
  3085. * Map an interface index to its name (SIOCGIFNAME)
  3086. */
  3087. /*
  3088. * We need this ioctl for efficient implementation of the
  3089. * if_indextoname() function required by the IPv6 API. Without
  3090. * it, we would have to search all the interfaces to find a
  3091. * match. --pb
  3092. */
  3093. static int dev_ifname(struct net *net, struct ifreq __user *arg)
  3094. {
  3095. struct net_device *dev;
  3096. struct ifreq ifr;
  3097. /*
  3098. * Fetch the caller's info block.
  3099. */
  3100. if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
  3101. return -EFAULT;
  3102. rcu_read_lock();
  3103. dev = dev_get_by_index_rcu(net, ifr.ifr_ifindex);
  3104. if (!dev) {
  3105. rcu_read_unlock();
  3106. return -ENODEV;
  3107. }
  3108. strcpy(ifr.ifr_name, dev->name);
  3109. rcu_read_unlock();
  3110. if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
  3111. return -EFAULT;
  3112. return 0;
  3113. }
  3114. /*
  3115. * Perform a SIOCGIFCONF call. This structure will change
  3116. * size eventually, and there is nothing I can do about it.
  3117. * Thus we will need a 'compatibility mode'.
  3118. */
  3119. static int dev_ifconf(struct net *net, char __user *arg)
  3120. {
  3121. struct ifconf ifc;
  3122. struct net_device *dev;
  3123. char __user *pos;
  3124. int len;
  3125. int total;
  3126. int i;
  3127. /*
  3128. * Fetch the caller's info block.
  3129. */
  3130. if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
  3131. return -EFAULT;
  3132. pos = ifc.ifc_buf;
  3133. len = ifc.ifc_len;
  3134. /*
  3135. * Loop over the interfaces, and write an info block for each.
  3136. */
  3137. total = 0;
  3138. for_each_netdev(net, dev) {
  3139. for (i = 0; i < NPROTO; i++) {
  3140. if (gifconf_list[i]) {
  3141. int done;
  3142. if (!pos)
  3143. done = gifconf_list[i](dev, NULL, 0);
  3144. else
  3145. done = gifconf_list[i](dev, pos + total,
  3146. len - total);
  3147. if (done < 0)
  3148. return -EFAULT;
  3149. total += done;
  3150. }
  3151. }
  3152. }
  3153. /*
  3154. * All done. Write the updated control block back to the caller.
  3155. */
  3156. ifc.ifc_len = total;
  3157. /*
  3158. * Both BSD and Solaris return 0 here, so we do too.
  3159. */
  3160. return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
  3161. }
  3162. #ifdef CONFIG_PROC_FS
  3163. /*
  3164. * This is invoked by the /proc filesystem handler to display a device
  3165. * in detail.
  3166. */
  3167. void *dev_seq_start(struct seq_file *seq, loff_t *pos)
  3168. __acquires(RCU)
  3169. {
  3170. struct net *net = seq_file_net(seq);
  3171. loff_t off;
  3172. struct net_device *dev;
  3173. rcu_read_lock();
  3174. if (!*pos)
  3175. return SEQ_START_TOKEN;
  3176. off = 1;
  3177. for_each_netdev_rcu(net, dev)
  3178. if (off++ == *pos)
  3179. return dev;
  3180. return NULL;
  3181. }
  3182. void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  3183. {
  3184. struct net_device *dev = (v == SEQ_START_TOKEN) ?
  3185. first_net_device(seq_file_net(seq)) :
  3186. next_net_device((struct net_device *)v);
  3187. ++*pos;
  3188. return rcu_dereference(dev);
  3189. }
  3190. void dev_seq_stop(struct seq_file *seq, void *v)
  3191. __releases(RCU)
  3192. {
  3193. rcu_read_unlock();
  3194. }
  3195. static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
  3196. {
  3197. struct rtnl_link_stats64 temp;
  3198. const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
  3199. seq_printf(seq, "%6s: %7llu %7llu %4llu %4llu %4llu %5llu %10llu %9llu "
  3200. "%8llu %7llu %4llu %4llu %4llu %5llu %7llu %10llu\n",
  3201. dev->name, stats->rx_bytes, stats->rx_packets,
  3202. stats->rx_errors,
  3203. stats->rx_dropped + stats->rx_missed_errors,
  3204. stats->rx_fifo_errors,
  3205. stats->rx_length_errors + stats->rx_over_errors +
  3206. stats->rx_crc_errors + stats->rx_frame_errors,
  3207. stats->rx_compressed, stats->multicast,
  3208. stats->tx_bytes, stats->tx_packets,
  3209. stats->tx_errors, stats->tx_dropped,
  3210. stats->tx_fifo_errors, stats->collisions,
  3211. stats->tx_carrier_errors +
  3212. stats->tx_aborted_errors +
  3213. stats->tx_window_errors +
  3214. stats->tx_heartbeat_errors,
  3215. stats->tx_compressed);
  3216. }
  3217. /*
  3218. * Called from the PROCfs module. This now uses the new arbitrary sized
  3219. * /proc/net interface to create /proc/net/dev
  3220. */
  3221. static int dev_seq_show(struct seq_file *seq, void *v)
  3222. {
  3223. if (v == SEQ_START_TOKEN)
  3224. seq_puts(seq, "Inter-| Receive "
  3225. " | Transmit\n"
  3226. " face |bytes packets errs drop fifo frame "
  3227. "compressed multicast|bytes packets errs "
  3228. "drop fifo colls carrier compressed\n");
  3229. else
  3230. dev_seq_printf_stats(seq, v);
  3231. return 0;
  3232. }
  3233. static struct softnet_data *softnet_get_online(loff_t *pos)
  3234. {
  3235. struct softnet_data *sd = NULL;
  3236. while (*pos < nr_cpu_ids)
  3237. if (cpu_online(*pos)) {
  3238. sd = &per_cpu(softnet_data, *pos);
  3239. break;
  3240. } else
  3241. ++*pos;
  3242. return sd;
  3243. }
  3244. static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
  3245. {
  3246. return softnet_get_online(pos);
  3247. }
  3248. static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  3249. {
  3250. ++*pos;
  3251. return softnet_get_online(pos);
  3252. }
  3253. static void softnet_seq_stop(struct seq_file *seq, void *v)
  3254. {
  3255. }
  3256. static int softnet_seq_show(struct seq_file *seq, void *v)
  3257. {
  3258. struct softnet_data *sd = v;
  3259. seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
  3260. sd->processed, sd->dropped, sd->time_squeeze, 0,
  3261. 0, 0, 0, 0, /* was fastroute */
  3262. sd->cpu_collision, sd->received_rps);
  3263. return 0;
  3264. }
  3265. static const struct seq_operations dev_seq_ops = {
  3266. .start = dev_seq_start,
  3267. .next = dev_seq_next,
  3268. .stop = dev_seq_stop,
  3269. .show = dev_seq_show,
  3270. };
  3271. static int dev_seq_open(struct inode *inode, struct file *file)
  3272. {
  3273. return seq_open_net(inode, file, &dev_seq_ops,
  3274. sizeof(struct seq_net_private));
  3275. }
  3276. static const struct file_operations dev_seq_fops = {
  3277. .owner = THIS_MODULE,
  3278. .open = dev_seq_open,
  3279. .read = seq_read,
  3280. .llseek = seq_lseek,
  3281. .release = seq_release_net,
  3282. };
  3283. static const struct seq_operations softnet_seq_ops = {
  3284. .start = softnet_seq_start,
  3285. .next = softnet_seq_next,
  3286. .stop = softnet_seq_stop,
  3287. .show = softnet_seq_show,
  3288. };
  3289. static int softnet_seq_open(struct inode *inode, struct file *file)
  3290. {
  3291. return seq_open(file, &softnet_seq_ops);
  3292. }
  3293. static const struct file_operations softnet_seq_fops = {
  3294. .owner = THIS_MODULE,
  3295. .open = softnet_seq_open,
  3296. .read = seq_read,
  3297. .llseek = seq_lseek,
  3298. .release = seq_release,
  3299. };
  3300. static void *ptype_get_idx(loff_t pos)
  3301. {
  3302. struct packet_type *pt = NULL;
  3303. loff_t i = 0;
  3304. int t;
  3305. list_for_each_entry_rcu(pt, &ptype_all, list) {
  3306. if (i == pos)
  3307. return pt;
  3308. ++i;
  3309. }
  3310. for (t = 0; t < PTYPE_HASH_SIZE; t++) {
  3311. list_for_each_entry_rcu(pt, &ptype_base[t], list) {
  3312. if (i == pos)
  3313. return pt;
  3314. ++i;
  3315. }
  3316. }
  3317. return NULL;
  3318. }
  3319. static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
  3320. __acquires(RCU)
  3321. {
  3322. rcu_read_lock();
  3323. return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN;
  3324. }
  3325. static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  3326. {
  3327. struct packet_type *pt;
  3328. struct list_head *nxt;
  3329. int hash;
  3330. ++*pos;
  3331. if (v == SEQ_START_TOKEN)
  3332. return ptype_get_idx(0);
  3333. pt = v;
  3334. nxt = pt->list.next;
  3335. if (pt->type == htons(ETH_P_ALL)) {
  3336. if (nxt != &ptype_all)
  3337. goto found;
  3338. hash = 0;
  3339. nxt = ptype_base[0].next;
  3340. } else
  3341. hash = ntohs(pt->type) & PTYPE_HASH_MASK;
  3342. while (nxt == &ptype_base[hash]) {
  3343. if (++hash >= PTYPE_HASH_SIZE)
  3344. return NULL;
  3345. nxt = ptype_base[hash].next;
  3346. }
  3347. found:
  3348. return list_entry(nxt, struct packet_type, list);
  3349. }
  3350. static void ptype_seq_stop(struct seq_file *seq, void *v)
  3351. __releases(RCU)
  3352. {
  3353. rcu_read_unlock();
  3354. }
  3355. static int ptype_seq_show(struct seq_file *seq, void *v)
  3356. {
  3357. struct packet_type *pt = v;
  3358. if (v == SEQ_START_TOKEN)
  3359. seq_puts(seq, "Type Device Function\n");
  3360. else if (pt->dev == NULL || dev_net(pt->dev) == seq_file_net(seq)) {
  3361. if (pt->type == htons(ETH_P_ALL))
  3362. seq_puts(seq, "ALL ");
  3363. else
  3364. seq_printf(seq, "%04x", ntohs(pt->type));
  3365. seq_printf(seq, " %-8s %pF\n",
  3366. pt->dev ? pt->dev->name : "", pt->func);
  3367. }
  3368. return 0;
  3369. }
  3370. static const struct seq_operations ptype_seq_ops = {
  3371. .start = ptype_seq_start,
  3372. .next = ptype_seq_next,
  3373. .stop = ptype_seq_stop,
  3374. .show = ptype_seq_show,
  3375. };
  3376. static int ptype_seq_open(struct inode *inode, struct file *file)
  3377. {
  3378. return seq_open_net(inode, file, &ptype_seq_ops,
  3379. sizeof(struct seq_net_private));
  3380. }
  3381. static const struct file_operations ptype_seq_fops = {
  3382. .owner = THIS_MODULE,
  3383. .open = ptype_seq_open,
  3384. .read = seq_read,
  3385. .llseek = seq_lseek,
  3386. .release = seq_release_net,
  3387. };
  3388. static int __net_init dev_proc_net_init(struct net *net)
  3389. {
  3390. int rc = -ENOMEM;
  3391. if (!proc_net_fops_create(net, "dev", S_IRUGO, &dev_seq_fops))
  3392. goto out;
  3393. if (!proc_net_fops_create(net, "softnet_stat", S_IRUGO, &softnet_seq_fops))
  3394. goto out_dev;
  3395. if (!proc_net_fops_create(net, "ptype", S_IRUGO, &ptype_seq_fops))
  3396. goto out_softnet;
  3397. if (wext_proc_init(net))
  3398. goto out_ptype;
  3399. rc = 0;
  3400. out:
  3401. return rc;
  3402. out_ptype:
  3403. proc_net_remove(net, "ptype");
  3404. out_softnet:
  3405. proc_net_remove(net, "softnet_stat");
  3406. out_dev:
  3407. proc_net_remove(net, "dev");
  3408. goto out;
  3409. }
  3410. static void __net_exit dev_proc_net_exit(struct net *net)
  3411. {
  3412. wext_proc_exit(net);
  3413. proc_net_remove(net, "ptype");
  3414. proc_net_remove(net, "softnet_stat");
  3415. proc_net_remove(net, "dev");
  3416. }
  3417. static struct pernet_operations __net_initdata dev_proc_ops = {
  3418. .init = dev_proc_net_init,
  3419. .exit = dev_proc_net_exit,
  3420. };
  3421. static int __init dev_proc_init(void)
  3422. {
  3423. return register_pernet_subsys(&dev_proc_ops);
  3424. }
  3425. #else
  3426. #define dev_proc_init() 0
  3427. #endif /* CONFIG_PROC_FS */
  3428. /**
  3429. * netdev_set_master - set up master/slave pair
  3430. * @slave: slave device
  3431. * @master: new master device
  3432. *
  3433. * Changes the master device of the slave. Pass %NULL to break the
  3434. * bonding. The caller must hold the RTNL semaphore. On a failure
  3435. * a negative errno code is returned. On success the reference counts
  3436. * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
  3437. * function returns zero.
  3438. */
  3439. int netdev_set_master(struct net_device *slave, struct net_device *master)
  3440. {
  3441. struct net_device *old = slave->master;
  3442. ASSERT_RTNL();
  3443. if (master) {
  3444. if (old)
  3445. return -EBUSY;
  3446. dev_hold(master);
  3447. }
  3448. slave->master = master;
  3449. if (old) {
  3450. synchronize_net();
  3451. dev_put(old);
  3452. }
  3453. if (master)
  3454. slave->flags |= IFF_SLAVE;
  3455. else
  3456. slave->flags &= ~IFF_SLAVE;
  3457. rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
  3458. return 0;
  3459. }
  3460. EXPORT_SYMBOL(netdev_set_master);
  3461. static void dev_change_rx_flags(struct net_device *dev, int flags)
  3462. {
  3463. const struct net_device_ops *ops = dev->netdev_ops;
  3464. if ((dev->flags & IFF_UP) && ops->ndo_change_rx_flags)
  3465. ops->ndo_change_rx_flags(dev, flags);
  3466. }
  3467. static int __dev_set_promiscuity(struct net_device *dev, int inc)
  3468. {
  3469. unsigned short old_flags = dev->flags;
  3470. uid_t uid;
  3471. gid_t gid;
  3472. ASSERT_RTNL();
  3473. dev->flags |= IFF_PROMISC;
  3474. dev->promiscuity += inc;
  3475. if (dev->promiscuity == 0) {
  3476. /*
  3477. * Avoid overflow.
  3478. * If inc causes overflow, untouch promisc and return error.
  3479. */
  3480. if (inc < 0)
  3481. dev->flags &= ~IFF_PROMISC;
  3482. else {
  3483. dev->promiscuity -= inc;
  3484. printk(KERN_WARNING "%s: promiscuity touches roof, "
  3485. "set promiscuity failed, promiscuity feature "
  3486. "of device might be broken.\n", dev->name);
  3487. return -EOVERFLOW;
  3488. }
  3489. }
  3490. if (dev->flags != old_flags) {
  3491. printk(KERN_INFO "device %s %s promiscuous mode\n",
  3492. dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
  3493. "left");
  3494. if (audit_enabled) {
  3495. current_uid_gid(&uid, &gid);
  3496. audit_log(current->audit_context, GFP_ATOMIC,
  3497. AUDIT_ANOM_PROMISCUOUS,
  3498. "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
  3499. dev->name, (dev->flags & IFF_PROMISC),
  3500. (old_flags & IFF_PROMISC),
  3501. audit_get_loginuid(current),
  3502. uid, gid,
  3503. audit_get_sessionid(current));
  3504. }
  3505. dev_change_rx_flags(dev, IFF_PROMISC);
  3506. }
  3507. return 0;
  3508. }
  3509. /**
  3510. * dev_set_promiscuity - update promiscuity count on a device
  3511. * @dev: device
  3512. * @inc: modifier
  3513. *
  3514. * Add or remove promiscuity from a device. While the count in the device
  3515. * remains above zero the interface remains promiscuous. Once it hits zero
  3516. * the device reverts back to normal filtering operation. A negative inc
  3517. * value is used to drop promiscuity on the device.
  3518. * Return 0 if successful or a negative errno code on error.
  3519. */
  3520. int dev_set_promiscuity(struct net_device *dev, int inc)
  3521. {
  3522. unsigned short old_flags = dev->flags;
  3523. int err;
  3524. err = __dev_set_promiscuity(dev, inc);
  3525. if (err < 0)
  3526. return err;
  3527. if (dev->flags != old_flags)
  3528. dev_set_rx_mode(dev);
  3529. return err;
  3530. }
  3531. EXPORT_SYMBOL(dev_set_promiscuity);
  3532. /**
  3533. * dev_set_allmulti - update allmulti count on a device
  3534. * @dev: device
  3535. * @inc: modifier
  3536. *
  3537. * Add or remove reception of all multicast frames to a device. While the
  3538. * count in the device remains above zero the interface remains listening
  3539. * to all interfaces. Once it hits zero the device reverts back to normal
  3540. * filtering operation. A negative @inc value is used to drop the counter
  3541. * when releasing a resource needing all multicasts.
  3542. * Return 0 if successful or a negative errno code on error.
  3543. */
  3544. int dev_set_allmulti(struct net_device *dev, int inc)
  3545. {
  3546. unsigned short old_flags = dev->flags;
  3547. ASSERT_RTNL();
  3548. dev->flags |= IFF_ALLMULTI;
  3549. dev->allmulti += inc;
  3550. if (dev->allmulti == 0) {
  3551. /*
  3552. * Avoid overflow.
  3553. * If inc causes overflow, untouch allmulti and return error.
  3554. */
  3555. if (inc < 0)
  3556. dev->flags &= ~IFF_ALLMULTI;
  3557. else {
  3558. dev->allmulti -= inc;
  3559. printk(KERN_WARNING "%s: allmulti touches roof, "
  3560. "set allmulti failed, allmulti feature of "
  3561. "device might be broken.\n", dev->name);
  3562. return -EOVERFLOW;
  3563. }
  3564. }
  3565. if (dev->flags ^ old_flags) {
  3566. dev_change_rx_flags(dev, IFF_ALLMULTI);
  3567. dev_set_rx_mode(dev);
  3568. }
  3569. return 0;
  3570. }
  3571. EXPORT_SYMBOL(dev_set_allmulti);
  3572. /*
  3573. * Upload unicast and multicast address lists to device and
  3574. * configure RX filtering. When the device doesn't support unicast
  3575. * filtering it is put in promiscuous mode while unicast addresses
  3576. * are present.
  3577. */
  3578. void __dev_set_rx_mode(struct net_device *dev)
  3579. {
  3580. const struct net_device_ops *ops = dev->netdev_ops;
  3581. /* dev_open will call this function so the list will stay sane. */
  3582. if (!(dev->flags&IFF_UP))
  3583. return;
  3584. if (!netif_device_present(dev))
  3585. return;
  3586. if (ops->ndo_set_rx_mode)
  3587. ops->ndo_set_rx_mode(dev);
  3588. else {
  3589. /* Unicast addresses changes may only happen under the rtnl,
  3590. * therefore calling __dev_set_promiscuity here is safe.
  3591. */
  3592. if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
  3593. __dev_set_promiscuity(dev, 1);
  3594. dev->uc_promisc = 1;
  3595. } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
  3596. __dev_set_promiscuity(dev, -1);
  3597. dev->uc_promisc = 0;
  3598. }
  3599. if (ops->ndo_set_multicast_list)
  3600. ops->ndo_set_multicast_list(dev);
  3601. }
  3602. }
  3603. void dev_set_rx_mode(struct net_device *dev)
  3604. {
  3605. netif_addr_lock_bh(dev);
  3606. __dev_set_rx_mode(dev);
  3607. netif_addr_unlock_bh(dev);
  3608. }
  3609. /**
  3610. * dev_get_flags - get flags reported to userspace
  3611. * @dev: device
  3612. *
  3613. * Get the combination of flag bits exported through APIs to userspace.
  3614. */
  3615. unsigned dev_get_flags(const struct net_device *dev)
  3616. {
  3617. unsigned flags;
  3618. flags = (dev->flags & ~(IFF_PROMISC |
  3619. IFF_ALLMULTI |
  3620. IFF_RUNNING |
  3621. IFF_LOWER_UP |
  3622. IFF_DORMANT)) |
  3623. (dev->gflags & (IFF_PROMISC |
  3624. IFF_ALLMULTI));
  3625. if (netif_running(dev)) {
  3626. if (netif_oper_up(dev))
  3627. flags |= IFF_RUNNING;
  3628. if (netif_carrier_ok(dev))
  3629. flags |= IFF_LOWER_UP;
  3630. if (netif_dormant(dev))
  3631. flags |= IFF_DORMANT;
  3632. }
  3633. return flags;
  3634. }
  3635. EXPORT_SYMBOL(dev_get_flags);
  3636. int __dev_change_flags(struct net_device *dev, unsigned int flags)
  3637. {
  3638. int old_flags = dev->flags;
  3639. int ret;
  3640. ASSERT_RTNL();
  3641. /*
  3642. * Set the flags on our device.
  3643. */
  3644. dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
  3645. IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
  3646. IFF_AUTOMEDIA)) |
  3647. (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
  3648. IFF_ALLMULTI));
  3649. /*
  3650. * Load in the correct multicast list now the flags have changed.
  3651. */
  3652. if ((old_flags ^ flags) & IFF_MULTICAST)
  3653. dev_change_rx_flags(dev, IFF_MULTICAST);
  3654. dev_set_rx_mode(dev);
  3655. /*
  3656. * Have we downed the interface. We handle IFF_UP ourselves
  3657. * according to user attempts to set it, rather than blindly
  3658. * setting it.
  3659. */
  3660. ret = 0;
  3661. if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
  3662. ret = ((old_flags & IFF_UP) ? __dev_close : __dev_open)(dev);
  3663. if (!ret)
  3664. dev_set_rx_mode(dev);
  3665. }
  3666. if ((flags ^ dev->gflags) & IFF_PROMISC) {
  3667. int inc = (flags & IFF_PROMISC) ? 1 : -1;
  3668. dev->gflags ^= IFF_PROMISC;
  3669. dev_set_promiscuity(dev, inc);
  3670. }
  3671. /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
  3672. is important. Some (broken) drivers set IFF_PROMISC, when
  3673. IFF_ALLMULTI is requested not asking us and not reporting.
  3674. */
  3675. if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
  3676. int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
  3677. dev->gflags ^= IFF_ALLMULTI;
  3678. dev_set_allmulti(dev, inc);
  3679. }
  3680. return ret;
  3681. }
  3682. void __dev_notify_flags(struct net_device *dev, unsigned int old_flags)
  3683. {
  3684. unsigned int changes = dev->flags ^ old_flags;
  3685. if (changes & IFF_UP) {
  3686. if (dev->flags & IFF_UP)
  3687. call_netdevice_notifiers(NETDEV_UP, dev);
  3688. else
  3689. call_netdevice_notifiers(NETDEV_DOWN, dev);
  3690. }
  3691. if (dev->flags & IFF_UP &&
  3692. (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE)))
  3693. call_netdevice_notifiers(NETDEV_CHANGE, dev);
  3694. }
  3695. /**
  3696. * dev_change_flags - change device settings
  3697. * @dev: device
  3698. * @flags: device state flags
  3699. *
  3700. * Change settings on device based state flags. The flags are
  3701. * in the userspace exported format.
  3702. */
  3703. int dev_change_flags(struct net_device *dev, unsigned flags)
  3704. {
  3705. int ret, changes;
  3706. int old_flags = dev->flags;
  3707. ret = __dev_change_flags(dev, flags);
  3708. if (ret < 0)
  3709. return ret;
  3710. changes = old_flags ^ dev->flags;
  3711. if (changes)
  3712. rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
  3713. __dev_notify_flags(dev, old_flags);
  3714. return ret;
  3715. }
  3716. EXPORT_SYMBOL(dev_change_flags);
  3717. /**
  3718. * dev_set_mtu - Change maximum transfer unit
  3719. * @dev: device
  3720. * @new_mtu: new transfer unit
  3721. *
  3722. * Change the maximum transfer size of the network device.
  3723. */
  3724. int dev_set_mtu(struct net_device *dev, int new_mtu)
  3725. {
  3726. const struct net_device_ops *ops = dev->netdev_ops;
  3727. int err;
  3728. if (new_mtu == dev->mtu)
  3729. return 0;
  3730. /* MTU must be positive. */
  3731. if (new_mtu < 0)
  3732. return -EINVAL;
  3733. if (!netif_device_present(dev))
  3734. return -ENODEV;
  3735. err = 0;
  3736. if (ops->ndo_change_mtu)
  3737. err = ops->ndo_change_mtu(dev, new_mtu);
  3738. else
  3739. dev->mtu = new_mtu;
  3740. if (!err && dev->flags & IFF_UP)
  3741. call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
  3742. return err;
  3743. }
  3744. EXPORT_SYMBOL(dev_set_mtu);
  3745. /**
  3746. * dev_set_mac_address - Change Media Access Control Address
  3747. * @dev: device
  3748. * @sa: new address
  3749. *
  3750. * Change the hardware (MAC) address of the device
  3751. */
  3752. int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
  3753. {
  3754. const struct net_device_ops *ops = dev->netdev_ops;
  3755. int err;
  3756. if (!ops->ndo_set_mac_address)
  3757. return -EOPNOTSUPP;
  3758. if (sa->sa_family != dev->type)
  3759. return -EINVAL;
  3760. if (!netif_device_present(dev))
  3761. return -ENODEV;
  3762. err = ops->ndo_set_mac_address(dev, sa);
  3763. if (!err)
  3764. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  3765. return err;
  3766. }
  3767. EXPORT_SYMBOL(dev_set_mac_address);
  3768. /*
  3769. * Perform the SIOCxIFxxx calls, inside rcu_read_lock()
  3770. */
  3771. static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
  3772. {
  3773. int err;
  3774. struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
  3775. if (!dev)
  3776. return -ENODEV;
  3777. switch (cmd) {
  3778. case SIOCGIFFLAGS: /* Get interface flags */
  3779. ifr->ifr_flags = (short) dev_get_flags(dev);
  3780. return 0;
  3781. case SIOCGIFMETRIC: /* Get the metric on the interface
  3782. (currently unused) */
  3783. ifr->ifr_metric = 0;
  3784. return 0;
  3785. case SIOCGIFMTU: /* Get the MTU of a device */
  3786. ifr->ifr_mtu = dev->mtu;
  3787. return 0;
  3788. case SIOCGIFHWADDR:
  3789. if (!dev->addr_len)
  3790. memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
  3791. else
  3792. memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
  3793. min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
  3794. ifr->ifr_hwaddr.sa_family = dev->type;
  3795. return 0;
  3796. case SIOCGIFSLAVE:
  3797. err = -EINVAL;
  3798. break;
  3799. case SIOCGIFMAP:
  3800. ifr->ifr_map.mem_start = dev->mem_start;
  3801. ifr->ifr_map.mem_end = dev->mem_end;
  3802. ifr->ifr_map.base_addr = dev->base_addr;
  3803. ifr->ifr_map.irq = dev->irq;
  3804. ifr->ifr_map.dma = dev->dma;
  3805. ifr->ifr_map.port = dev->if_port;
  3806. return 0;
  3807. case SIOCGIFINDEX:
  3808. ifr->ifr_ifindex = dev->ifindex;
  3809. return 0;
  3810. case SIOCGIFTXQLEN:
  3811. ifr->ifr_qlen = dev->tx_queue_len;
  3812. return 0;
  3813. default:
  3814. /* dev_ioctl() should ensure this case
  3815. * is never reached
  3816. */
  3817. WARN_ON(1);
  3818. err = -EINVAL;
  3819. break;
  3820. }
  3821. return err;
  3822. }
  3823. /*
  3824. * Perform the SIOCxIFxxx calls, inside rtnl_lock()
  3825. */
  3826. static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
  3827. {
  3828. int err;
  3829. struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
  3830. const struct net_device_ops *ops;
  3831. if (!dev)
  3832. return -ENODEV;
  3833. ops = dev->netdev_ops;
  3834. switch (cmd) {
  3835. case SIOCSIFFLAGS: /* Set interface flags */
  3836. return dev_change_flags(dev, ifr->ifr_flags);
  3837. case SIOCSIFMETRIC: /* Set the metric on the interface
  3838. (currently unused) */
  3839. return -EOPNOTSUPP;
  3840. case SIOCSIFMTU: /* Set the MTU of a device */
  3841. return dev_set_mtu(dev, ifr->ifr_mtu);
  3842. case SIOCSIFHWADDR:
  3843. return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
  3844. case SIOCSIFHWBROADCAST:
  3845. if (ifr->ifr_hwaddr.sa_family != dev->type)
  3846. return -EINVAL;
  3847. memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
  3848. min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
  3849. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  3850. return 0;
  3851. case SIOCSIFMAP:
  3852. if (ops->ndo_set_config) {
  3853. if (!netif_device_present(dev))
  3854. return -ENODEV;
  3855. return ops->ndo_set_config(dev, &ifr->ifr_map);
  3856. }
  3857. return -EOPNOTSUPP;
  3858. case SIOCADDMULTI:
  3859. if ((!ops->ndo_set_multicast_list && !ops->ndo_set_rx_mode) ||
  3860. ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
  3861. return -EINVAL;
  3862. if (!netif_device_present(dev))
  3863. return -ENODEV;
  3864. return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
  3865. case SIOCDELMULTI:
  3866. if ((!ops->ndo_set_multicast_list && !ops->ndo_set_rx_mode) ||
  3867. ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
  3868. return -EINVAL;
  3869. if (!netif_device_present(dev))
  3870. return -ENODEV;
  3871. return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
  3872. case SIOCSIFTXQLEN:
  3873. if (ifr->ifr_qlen < 0)
  3874. return -EINVAL;
  3875. dev->tx_queue_len = ifr->ifr_qlen;
  3876. return 0;
  3877. case SIOCSIFNAME:
  3878. ifr->ifr_newname[IFNAMSIZ-1] = '\0';
  3879. return dev_change_name(dev, ifr->ifr_newname);
  3880. /*
  3881. * Unknown or private ioctl
  3882. */
  3883. default:
  3884. if ((cmd >= SIOCDEVPRIVATE &&
  3885. cmd <= SIOCDEVPRIVATE + 15) ||
  3886. cmd == SIOCBONDENSLAVE ||
  3887. cmd == SIOCBONDRELEASE ||
  3888. cmd == SIOCBONDSETHWADDR ||
  3889. cmd == SIOCBONDSLAVEINFOQUERY ||
  3890. cmd == SIOCBONDINFOQUERY ||
  3891. cmd == SIOCBONDCHANGEACTIVE ||
  3892. cmd == SIOCGMIIPHY ||
  3893. cmd == SIOCGMIIREG ||
  3894. cmd == SIOCSMIIREG ||
  3895. cmd == SIOCBRADDIF ||
  3896. cmd == SIOCBRDELIF ||
  3897. cmd == SIOCSHWTSTAMP ||
  3898. cmd == SIOCWANDEV) {
  3899. err = -EOPNOTSUPP;
  3900. if (ops->ndo_do_ioctl) {
  3901. if (netif_device_present(dev))
  3902. err = ops->ndo_do_ioctl(dev, ifr, cmd);
  3903. else
  3904. err = -ENODEV;
  3905. }
  3906. } else
  3907. err = -EINVAL;
  3908. }
  3909. return err;
  3910. }
  3911. /*
  3912. * This function handles all "interface"-type I/O control requests. The actual
  3913. * 'doing' part of this is dev_ifsioc above.
  3914. */
  3915. /**
  3916. * dev_ioctl - network device ioctl
  3917. * @net: the applicable net namespace
  3918. * @cmd: command to issue
  3919. * @arg: pointer to a struct ifreq in user space
  3920. *
  3921. * Issue ioctl functions to devices. This is normally called by the
  3922. * user space syscall interfaces but can sometimes be useful for
  3923. * other purposes. The return value is the return from the syscall if
  3924. * positive or a negative errno code on error.
  3925. */
  3926. int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg)
  3927. {
  3928. struct ifreq ifr;
  3929. int ret;
  3930. char *colon;
  3931. /* One special case: SIOCGIFCONF takes ifconf argument
  3932. and requires shared lock, because it sleeps writing
  3933. to user space.
  3934. */
  3935. if (cmd == SIOCGIFCONF) {
  3936. rtnl_lock();
  3937. ret = dev_ifconf(net, (char __user *) arg);
  3938. rtnl_unlock();
  3939. return ret;
  3940. }
  3941. if (cmd == SIOCGIFNAME)
  3942. return dev_ifname(net, (struct ifreq __user *)arg);
  3943. if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
  3944. return -EFAULT;
  3945. ifr.ifr_name[IFNAMSIZ-1] = 0;
  3946. colon = strchr(ifr.ifr_name, ':');
  3947. if (colon)
  3948. *colon = 0;
  3949. /*
  3950. * See which interface the caller is talking about.
  3951. */
  3952. switch (cmd) {
  3953. /*
  3954. * These ioctl calls:
  3955. * - can be done by all.
  3956. * - atomic and do not require locking.
  3957. * - return a value
  3958. */
  3959. case SIOCGIFFLAGS:
  3960. case SIOCGIFMETRIC:
  3961. case SIOCGIFMTU:
  3962. case SIOCGIFHWADDR:
  3963. case SIOCGIFSLAVE:
  3964. case SIOCGIFMAP:
  3965. case SIOCGIFINDEX:
  3966. case SIOCGIFTXQLEN:
  3967. dev_load(net, ifr.ifr_name);
  3968. rcu_read_lock();
  3969. ret = dev_ifsioc_locked(net, &ifr, cmd);
  3970. rcu_read_unlock();
  3971. if (!ret) {
  3972. if (colon)
  3973. *colon = ':';
  3974. if (copy_to_user(arg, &ifr,
  3975. sizeof(struct ifreq)))
  3976. ret = -EFAULT;
  3977. }
  3978. return ret;
  3979. case SIOCETHTOOL:
  3980. dev_load(net, ifr.ifr_name);
  3981. rtnl_lock();
  3982. ret = dev_ethtool(net, &ifr);
  3983. rtnl_unlock();
  3984. if (!ret) {
  3985. if (colon)
  3986. *colon = ':';
  3987. if (copy_to_user(arg, &ifr,
  3988. sizeof(struct ifreq)))
  3989. ret = -EFAULT;
  3990. }
  3991. return ret;
  3992. /*
  3993. * These ioctl calls:
  3994. * - require superuser power.
  3995. * - require strict serialization.
  3996. * - return a value
  3997. */
  3998. case SIOCGMIIPHY:
  3999. case SIOCGMIIREG:
  4000. case SIOCSIFNAME:
  4001. if (!capable(CAP_NET_ADMIN))
  4002. return -EPERM;
  4003. dev_load(net, ifr.ifr_name);
  4004. rtnl_lock();
  4005. ret = dev_ifsioc(net, &ifr, cmd);
  4006. rtnl_unlock();
  4007. if (!ret) {
  4008. if (colon)
  4009. *colon = ':';
  4010. if (copy_to_user(arg, &ifr,
  4011. sizeof(struct ifreq)))
  4012. ret = -EFAULT;
  4013. }
  4014. return ret;
  4015. /*
  4016. * These ioctl calls:
  4017. * - require superuser power.
  4018. * - require strict serialization.
  4019. * - do not return a value
  4020. */
  4021. case SIOCSIFFLAGS:
  4022. case SIOCSIFMETRIC:
  4023. case SIOCSIFMTU:
  4024. case SIOCSIFMAP:
  4025. case SIOCSIFHWADDR:
  4026. case SIOCSIFSLAVE:
  4027. case SIOCADDMULTI:
  4028. case SIOCDELMULTI:
  4029. case SIOCSIFHWBROADCAST:
  4030. case SIOCSIFTXQLEN:
  4031. case SIOCSMIIREG:
  4032. case SIOCBONDENSLAVE:
  4033. case SIOCBONDRELEASE:
  4034. case SIOCBONDSETHWADDR:
  4035. case SIOCBONDCHANGEACTIVE:
  4036. case SIOCBRADDIF:
  4037. case SIOCBRDELIF:
  4038. case SIOCSHWTSTAMP:
  4039. if (!capable(CAP_NET_ADMIN))
  4040. return -EPERM;
  4041. /* fall through */
  4042. case SIOCBONDSLAVEINFOQUERY:
  4043. case SIOCBONDINFOQUERY:
  4044. dev_load(net, ifr.ifr_name);
  4045. rtnl_lock();
  4046. ret = dev_ifsioc(net, &ifr, cmd);
  4047. rtnl_unlock();
  4048. return ret;
  4049. case SIOCGIFMEM:
  4050. /* Get the per device memory space. We can add this but
  4051. * currently do not support it */
  4052. case SIOCSIFMEM:
  4053. /* Set the per device memory buffer space.
  4054. * Not applicable in our case */
  4055. case SIOCSIFLINK:
  4056. return -EINVAL;
  4057. /*
  4058. * Unknown or private ioctl.
  4059. */
  4060. default:
  4061. if (cmd == SIOCWANDEV ||
  4062. (cmd >= SIOCDEVPRIVATE &&
  4063. cmd <= SIOCDEVPRIVATE + 15)) {
  4064. dev_load(net, ifr.ifr_name);
  4065. rtnl_lock();
  4066. ret = dev_ifsioc(net, &ifr, cmd);
  4067. rtnl_unlock();
  4068. if (!ret && copy_to_user(arg, &ifr,
  4069. sizeof(struct ifreq)))
  4070. ret = -EFAULT;
  4071. return ret;
  4072. }
  4073. /* Take care of Wireless Extensions */
  4074. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
  4075. return wext_handle_ioctl(net, &ifr, cmd, arg);
  4076. return -EINVAL;
  4077. }
  4078. }
  4079. /**
  4080. * dev_new_index - allocate an ifindex
  4081. * @net: the applicable net namespace
  4082. *
  4083. * Returns a suitable unique value for a new device interface
  4084. * number. The caller must hold the rtnl semaphore or the
  4085. * dev_base_lock to be sure it remains unique.
  4086. */
  4087. static int dev_new_index(struct net *net)
  4088. {
  4089. static int ifindex;
  4090. for (;;) {
  4091. if (++ifindex <= 0)
  4092. ifindex = 1;
  4093. if (!__dev_get_by_index(net, ifindex))
  4094. return ifindex;
  4095. }
  4096. }
  4097. /* Delayed registration/unregisteration */
  4098. static LIST_HEAD(net_todo_list);
  4099. static void net_set_todo(struct net_device *dev)
  4100. {
  4101. list_add_tail(&dev->todo_list, &net_todo_list);
  4102. }
  4103. static void rollback_registered_many(struct list_head *head)
  4104. {
  4105. struct net_device *dev, *tmp;
  4106. BUG_ON(dev_boot_phase);
  4107. ASSERT_RTNL();
  4108. list_for_each_entry_safe(dev, tmp, head, unreg_list) {
  4109. /* Some devices call without registering
  4110. * for initialization unwind. Remove those
  4111. * devices and proceed with the remaining.
  4112. */
  4113. if (dev->reg_state == NETREG_UNINITIALIZED) {
  4114. pr_debug("unregister_netdevice: device %s/%p never "
  4115. "was registered\n", dev->name, dev);
  4116. WARN_ON(1);
  4117. list_del(&dev->unreg_list);
  4118. continue;
  4119. }
  4120. BUG_ON(dev->reg_state != NETREG_REGISTERED);
  4121. /* If device is running, close it first. */
  4122. dev_close(dev);
  4123. /* And unlink it from device chain. */
  4124. unlist_netdevice(dev);
  4125. dev->reg_state = NETREG_UNREGISTERING;
  4126. }
  4127. synchronize_net();
  4128. list_for_each_entry(dev, head, unreg_list) {
  4129. /* Shutdown queueing discipline. */
  4130. dev_shutdown(dev);
  4131. /* Notify protocols, that we are about to destroy
  4132. this device. They should clean all the things.
  4133. */
  4134. call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
  4135. if (!dev->rtnl_link_ops ||
  4136. dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
  4137. rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
  4138. /*
  4139. * Flush the unicast and multicast chains
  4140. */
  4141. dev_uc_flush(dev);
  4142. dev_mc_flush(dev);
  4143. if (dev->netdev_ops->ndo_uninit)
  4144. dev->netdev_ops->ndo_uninit(dev);
  4145. /* Notifier chain MUST detach us from master device. */
  4146. WARN_ON(dev->master);
  4147. /* Remove entries from kobject tree */
  4148. netdev_unregister_kobject(dev);
  4149. }
  4150. /* Process any work delayed until the end of the batch */
  4151. dev = list_first_entry(head, struct net_device, unreg_list);
  4152. call_netdevice_notifiers(NETDEV_UNREGISTER_BATCH, dev);
  4153. synchronize_net();
  4154. list_for_each_entry(dev, head, unreg_list)
  4155. dev_put(dev);
  4156. }
  4157. static void rollback_registered(struct net_device *dev)
  4158. {
  4159. LIST_HEAD(single);
  4160. list_add(&dev->unreg_list, &single);
  4161. rollback_registered_many(&single);
  4162. }
  4163. static void __netdev_init_queue_locks_one(struct net_device *dev,
  4164. struct netdev_queue *dev_queue,
  4165. void *_unused)
  4166. {
  4167. spin_lock_init(&dev_queue->_xmit_lock);
  4168. netdev_set_xmit_lockdep_class(&dev_queue->_xmit_lock, dev->type);
  4169. dev_queue->xmit_lock_owner = -1;
  4170. }
  4171. static void netdev_init_queue_locks(struct net_device *dev)
  4172. {
  4173. netdev_for_each_tx_queue(dev, __netdev_init_queue_locks_one, NULL);
  4174. __netdev_init_queue_locks_one(dev, &dev->rx_queue, NULL);
  4175. }
  4176. unsigned long netdev_fix_features(unsigned long features, const char *name)
  4177. {
  4178. /* Fix illegal SG+CSUM combinations. */
  4179. if ((features & NETIF_F_SG) &&
  4180. !(features & NETIF_F_ALL_CSUM)) {
  4181. if (name)
  4182. printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no "
  4183. "checksum feature.\n", name);
  4184. features &= ~NETIF_F_SG;
  4185. }
  4186. /* TSO requires that SG is present as well. */
  4187. if ((features & NETIF_F_TSO) && !(features & NETIF_F_SG)) {
  4188. if (name)
  4189. printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no "
  4190. "SG feature.\n", name);
  4191. features &= ~NETIF_F_TSO;
  4192. }
  4193. if (features & NETIF_F_UFO) {
  4194. if (!(features & NETIF_F_GEN_CSUM)) {
  4195. if (name)
  4196. printk(KERN_ERR "%s: Dropping NETIF_F_UFO "
  4197. "since no NETIF_F_HW_CSUM feature.\n",
  4198. name);
  4199. features &= ~NETIF_F_UFO;
  4200. }
  4201. if (!(features & NETIF_F_SG)) {
  4202. if (name)
  4203. printk(KERN_ERR "%s: Dropping NETIF_F_UFO "
  4204. "since no NETIF_F_SG feature.\n", name);
  4205. features &= ~NETIF_F_UFO;
  4206. }
  4207. }
  4208. return features;
  4209. }
  4210. EXPORT_SYMBOL(netdev_fix_features);
  4211. /**
  4212. * netif_stacked_transfer_operstate - transfer operstate
  4213. * @rootdev: the root or lower level device to transfer state from
  4214. * @dev: the device to transfer operstate to
  4215. *
  4216. * Transfer operational state from root to device. This is normally
  4217. * called when a stacking relationship exists between the root
  4218. * device and the device(a leaf device).
  4219. */
  4220. void netif_stacked_transfer_operstate(const struct net_device *rootdev,
  4221. struct net_device *dev)
  4222. {
  4223. if (rootdev->operstate == IF_OPER_DORMANT)
  4224. netif_dormant_on(dev);
  4225. else
  4226. netif_dormant_off(dev);
  4227. if (netif_carrier_ok(rootdev)) {
  4228. if (!netif_carrier_ok(dev))
  4229. netif_carrier_on(dev);
  4230. } else {
  4231. if (netif_carrier_ok(dev))
  4232. netif_carrier_off(dev);
  4233. }
  4234. }
  4235. EXPORT_SYMBOL(netif_stacked_transfer_operstate);
  4236. /**
  4237. * register_netdevice - register a network device
  4238. * @dev: device to register
  4239. *
  4240. * Take a completed network device structure and add it to the kernel
  4241. * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
  4242. * chain. 0 is returned on success. A negative errno code is returned
  4243. * on a failure to set up the device, or if the name is a duplicate.
  4244. *
  4245. * Callers must hold the rtnl semaphore. You may want
  4246. * register_netdev() instead of this.
  4247. *
  4248. * BUGS:
  4249. * The locking appears insufficient to guarantee two parallel registers
  4250. * will not get the same name.
  4251. */
  4252. int register_netdevice(struct net_device *dev)
  4253. {
  4254. int ret;
  4255. struct net *net = dev_net(dev);
  4256. BUG_ON(dev_boot_phase);
  4257. ASSERT_RTNL();
  4258. might_sleep();
  4259. /* When net_device's are persistent, this will be fatal. */
  4260. BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
  4261. BUG_ON(!net);
  4262. spin_lock_init(&dev->addr_list_lock);
  4263. netdev_set_addr_lockdep_class(dev);
  4264. netdev_init_queue_locks(dev);
  4265. dev->iflink = -1;
  4266. #ifdef CONFIG_RPS
  4267. if (!dev->num_rx_queues) {
  4268. /*
  4269. * Allocate a single RX queue if driver never called
  4270. * alloc_netdev_mq
  4271. */
  4272. dev->_rx = kzalloc(sizeof(struct netdev_rx_queue), GFP_KERNEL);
  4273. if (!dev->_rx) {
  4274. ret = -ENOMEM;
  4275. goto out;
  4276. }
  4277. dev->_rx->first = dev->_rx;
  4278. atomic_set(&dev->_rx->count, 1);
  4279. dev->num_rx_queues = 1;
  4280. }
  4281. #endif
  4282. /* Init, if this function is available */
  4283. if (dev->netdev_ops->ndo_init) {
  4284. ret = dev->netdev_ops->ndo_init(dev);
  4285. if (ret) {
  4286. if (ret > 0)
  4287. ret = -EIO;
  4288. goto out;
  4289. }
  4290. }
  4291. ret = dev_get_valid_name(dev, dev->name, 0);
  4292. if (ret)
  4293. goto err_uninit;
  4294. dev->ifindex = dev_new_index(net);
  4295. if (dev->iflink == -1)
  4296. dev->iflink = dev->ifindex;
  4297. /* Fix illegal checksum combinations */
  4298. if ((dev->features & NETIF_F_HW_CSUM) &&
  4299. (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
  4300. printk(KERN_NOTICE "%s: mixed HW and IP checksum settings.\n",
  4301. dev->name);
  4302. dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
  4303. }
  4304. if ((dev->features & NETIF_F_NO_CSUM) &&
  4305. (dev->features & (NETIF_F_HW_CSUM|NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
  4306. printk(KERN_NOTICE "%s: mixed no checksumming and other settings.\n",
  4307. dev->name);
  4308. dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM|NETIF_F_HW_CSUM);
  4309. }
  4310. dev->features = netdev_fix_features(dev->features, dev->name);
  4311. /* Enable software GSO if SG is supported. */
  4312. if (dev->features & NETIF_F_SG)
  4313. dev->features |= NETIF_F_GSO;
  4314. ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
  4315. ret = notifier_to_errno(ret);
  4316. if (ret)
  4317. goto err_uninit;
  4318. ret = netdev_register_kobject(dev);
  4319. if (ret)
  4320. goto err_uninit;
  4321. dev->reg_state = NETREG_REGISTERED;
  4322. /*
  4323. * Default initial state at registry is that the
  4324. * device is present.
  4325. */
  4326. set_bit(__LINK_STATE_PRESENT, &dev->state);
  4327. dev_init_scheduler(dev);
  4328. dev_hold(dev);
  4329. list_netdevice(dev);
  4330. /* Notify protocols, that a new device appeared. */
  4331. ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
  4332. ret = notifier_to_errno(ret);
  4333. if (ret) {
  4334. rollback_registered(dev);
  4335. dev->reg_state = NETREG_UNREGISTERED;
  4336. }
  4337. /*
  4338. * Prevent userspace races by waiting until the network
  4339. * device is fully setup before sending notifications.
  4340. */
  4341. if (!dev->rtnl_link_ops ||
  4342. dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
  4343. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
  4344. out:
  4345. return ret;
  4346. err_uninit:
  4347. if (dev->netdev_ops->ndo_uninit)
  4348. dev->netdev_ops->ndo_uninit(dev);
  4349. goto out;
  4350. }
  4351. EXPORT_SYMBOL(register_netdevice);
  4352. /**
  4353. * init_dummy_netdev - init a dummy network device for NAPI
  4354. * @dev: device to init
  4355. *
  4356. * This takes a network device structure and initialize the minimum
  4357. * amount of fields so it can be used to schedule NAPI polls without
  4358. * registering a full blown interface. This is to be used by drivers
  4359. * that need to tie several hardware interfaces to a single NAPI
  4360. * poll scheduler due to HW limitations.
  4361. */
  4362. int init_dummy_netdev(struct net_device *dev)
  4363. {
  4364. /* Clear everything. Note we don't initialize spinlocks
  4365. * are they aren't supposed to be taken by any of the
  4366. * NAPI code and this dummy netdev is supposed to be
  4367. * only ever used for NAPI polls
  4368. */
  4369. memset(dev, 0, sizeof(struct net_device));
  4370. /* make sure we BUG if trying to hit standard
  4371. * register/unregister code path
  4372. */
  4373. dev->reg_state = NETREG_DUMMY;
  4374. /* initialize the ref count */
  4375. atomic_set(&dev->refcnt, 1);
  4376. /* NAPI wants this */
  4377. INIT_LIST_HEAD(&dev->napi_list);
  4378. /* a dummy interface is started by default */
  4379. set_bit(__LINK_STATE_PRESENT, &dev->state);
  4380. set_bit(__LINK_STATE_START, &dev->state);
  4381. return 0;
  4382. }
  4383. EXPORT_SYMBOL_GPL(init_dummy_netdev);
  4384. /**
  4385. * register_netdev - register a network device
  4386. * @dev: device to register
  4387. *
  4388. * Take a completed network device structure and add it to the kernel
  4389. * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
  4390. * chain. 0 is returned on success. A negative errno code is returned
  4391. * on a failure to set up the device, or if the name is a duplicate.
  4392. *
  4393. * This is a wrapper around register_netdevice that takes the rtnl semaphore
  4394. * and expands the device name if you passed a format string to
  4395. * alloc_netdev.
  4396. */
  4397. int register_netdev(struct net_device *dev)
  4398. {
  4399. int err;
  4400. rtnl_lock();
  4401. /*
  4402. * If the name is a format string the caller wants us to do a
  4403. * name allocation.
  4404. */
  4405. if (strchr(dev->name, '%')) {
  4406. err = dev_alloc_name(dev, dev->name);
  4407. if (err < 0)
  4408. goto out;
  4409. }
  4410. err = register_netdevice(dev);
  4411. out:
  4412. rtnl_unlock();
  4413. return err;
  4414. }
  4415. EXPORT_SYMBOL(register_netdev);
  4416. /*
  4417. * netdev_wait_allrefs - wait until all references are gone.
  4418. *
  4419. * This is called when unregistering network devices.
  4420. *
  4421. * Any protocol or device that holds a reference should register
  4422. * for netdevice notification, and cleanup and put back the
  4423. * reference if they receive an UNREGISTER event.
  4424. * We can get stuck here if buggy protocols don't correctly
  4425. * call dev_put.
  4426. */
  4427. static void netdev_wait_allrefs(struct net_device *dev)
  4428. {
  4429. unsigned long rebroadcast_time, warning_time;
  4430. linkwatch_forget_dev(dev);
  4431. rebroadcast_time = warning_time = jiffies;
  4432. while (atomic_read(&dev->refcnt) != 0) {
  4433. if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
  4434. rtnl_lock();
  4435. /* Rebroadcast unregister notification */
  4436. call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
  4437. /* don't resend NETDEV_UNREGISTER_BATCH, _BATCH users
  4438. * should have already handle it the first time */
  4439. if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
  4440. &dev->state)) {
  4441. /* We must not have linkwatch events
  4442. * pending on unregister. If this
  4443. * happens, we simply run the queue
  4444. * unscheduled, resulting in a noop
  4445. * for this device.
  4446. */
  4447. linkwatch_run_queue();
  4448. }
  4449. __rtnl_unlock();
  4450. rebroadcast_time = jiffies;
  4451. }
  4452. msleep(250);
  4453. if (time_after(jiffies, warning_time + 10 * HZ)) {
  4454. printk(KERN_EMERG "unregister_netdevice: "
  4455. "waiting for %s to become free. Usage "
  4456. "count = %d\n",
  4457. dev->name, atomic_read(&dev->refcnt));
  4458. warning_time = jiffies;
  4459. }
  4460. }
  4461. }
  4462. /* The sequence is:
  4463. *
  4464. * rtnl_lock();
  4465. * ...
  4466. * register_netdevice(x1);
  4467. * register_netdevice(x2);
  4468. * ...
  4469. * unregister_netdevice(y1);
  4470. * unregister_netdevice(y2);
  4471. * ...
  4472. * rtnl_unlock();
  4473. * free_netdev(y1);
  4474. * free_netdev(y2);
  4475. *
  4476. * We are invoked by rtnl_unlock().
  4477. * This allows us to deal with problems:
  4478. * 1) We can delete sysfs objects which invoke hotplug
  4479. * without deadlocking with linkwatch via keventd.
  4480. * 2) Since we run with the RTNL semaphore not held, we can sleep
  4481. * safely in order to wait for the netdev refcnt to drop to zero.
  4482. *
  4483. * We must not return until all unregister events added during
  4484. * the interval the lock was held have been completed.
  4485. */
  4486. void netdev_run_todo(void)
  4487. {
  4488. struct list_head list;
  4489. /* Snapshot list, allow later requests */
  4490. list_replace_init(&net_todo_list, &list);
  4491. __rtnl_unlock();
  4492. while (!list_empty(&list)) {
  4493. struct net_device *dev
  4494. = list_first_entry(&list, struct net_device, todo_list);
  4495. list_del(&dev->todo_list);
  4496. if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
  4497. printk(KERN_ERR "network todo '%s' but state %d\n",
  4498. dev->name, dev->reg_state);
  4499. dump_stack();
  4500. continue;
  4501. }
  4502. dev->reg_state = NETREG_UNREGISTERED;
  4503. on_each_cpu(flush_backlog, dev, 1);
  4504. netdev_wait_allrefs(dev);
  4505. /* paranoia */
  4506. BUG_ON(atomic_read(&dev->refcnt));
  4507. WARN_ON(dev->ip_ptr);
  4508. WARN_ON(dev->ip6_ptr);
  4509. WARN_ON(dev->dn_ptr);
  4510. if (dev->destructor)
  4511. dev->destructor(dev);
  4512. /* Free network device */
  4513. kobject_put(&dev->dev.kobj);
  4514. }
  4515. }
  4516. /**
  4517. * dev_txq_stats_fold - fold tx_queues stats
  4518. * @dev: device to get statistics from
  4519. * @stats: struct rtnl_link_stats64 to hold results
  4520. */
  4521. void dev_txq_stats_fold(const struct net_device *dev,
  4522. struct rtnl_link_stats64 *stats)
  4523. {
  4524. u64 tx_bytes = 0, tx_packets = 0, tx_dropped = 0;
  4525. unsigned int i;
  4526. struct netdev_queue *txq;
  4527. for (i = 0; i < dev->num_tx_queues; i++) {
  4528. txq = netdev_get_tx_queue(dev, i);
  4529. spin_lock_bh(&txq->_xmit_lock);
  4530. tx_bytes += txq->tx_bytes;
  4531. tx_packets += txq->tx_packets;
  4532. tx_dropped += txq->tx_dropped;
  4533. spin_unlock_bh(&txq->_xmit_lock);
  4534. }
  4535. if (tx_bytes || tx_packets || tx_dropped) {
  4536. stats->tx_bytes = tx_bytes;
  4537. stats->tx_packets = tx_packets;
  4538. stats->tx_dropped = tx_dropped;
  4539. }
  4540. }
  4541. EXPORT_SYMBOL(dev_txq_stats_fold);
  4542. /* Convert net_device_stats to rtnl_link_stats64. They have the same
  4543. * fields in the same order, with only the type differing.
  4544. */
  4545. static void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
  4546. const struct net_device_stats *netdev_stats)
  4547. {
  4548. #if BITS_PER_LONG == 64
  4549. BUILD_BUG_ON(sizeof(*stats64) != sizeof(*netdev_stats));
  4550. memcpy(stats64, netdev_stats, sizeof(*stats64));
  4551. #else
  4552. size_t i, n = sizeof(*stats64) / sizeof(u64);
  4553. const unsigned long *src = (const unsigned long *)netdev_stats;
  4554. u64 *dst = (u64 *)stats64;
  4555. BUILD_BUG_ON(sizeof(*netdev_stats) / sizeof(unsigned long) !=
  4556. sizeof(*stats64) / sizeof(u64));
  4557. for (i = 0; i < n; i++)
  4558. dst[i] = src[i];
  4559. #endif
  4560. }
  4561. /**
  4562. * dev_get_stats - get network device statistics
  4563. * @dev: device to get statistics from
  4564. * @storage: place to store stats
  4565. *
  4566. * Get network statistics from device. Return @storage.
  4567. * The device driver may provide its own method by setting
  4568. * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
  4569. * otherwise the internal statistics structure is used.
  4570. */
  4571. struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
  4572. struct rtnl_link_stats64 *storage)
  4573. {
  4574. const struct net_device_ops *ops = dev->netdev_ops;
  4575. if (ops->ndo_get_stats64) {
  4576. memset(storage, 0, sizeof(*storage));
  4577. return ops->ndo_get_stats64(dev, storage);
  4578. }
  4579. if (ops->ndo_get_stats) {
  4580. netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
  4581. return storage;
  4582. }
  4583. netdev_stats_to_stats64(storage, &dev->stats);
  4584. dev_txq_stats_fold(dev, storage);
  4585. return storage;
  4586. }
  4587. EXPORT_SYMBOL(dev_get_stats);
  4588. static void netdev_init_one_queue(struct net_device *dev,
  4589. struct netdev_queue *queue,
  4590. void *_unused)
  4591. {
  4592. queue->dev = dev;
  4593. }
  4594. static void netdev_init_queues(struct net_device *dev)
  4595. {
  4596. netdev_init_one_queue(dev, &dev->rx_queue, NULL);
  4597. netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
  4598. spin_lock_init(&dev->tx_global_lock);
  4599. }
  4600. /**
  4601. * alloc_netdev_mq - allocate network device
  4602. * @sizeof_priv: size of private data to allocate space for
  4603. * @name: device name format string
  4604. * @setup: callback to initialize device
  4605. * @queue_count: the number of subqueues to allocate
  4606. *
  4607. * Allocates a struct net_device with private data area for driver use
  4608. * and performs basic initialization. Also allocates subquue structs
  4609. * for each queue on the device at the end of the netdevice.
  4610. */
  4611. struct net_device *alloc_netdev_mq(int sizeof_priv, const char *name,
  4612. void (*setup)(struct net_device *), unsigned int queue_count)
  4613. {
  4614. struct netdev_queue *tx;
  4615. struct net_device *dev;
  4616. size_t alloc_size;
  4617. struct net_device *p;
  4618. #ifdef CONFIG_RPS
  4619. struct netdev_rx_queue *rx;
  4620. int i;
  4621. #endif
  4622. BUG_ON(strlen(name) >= sizeof(dev->name));
  4623. alloc_size = sizeof(struct net_device);
  4624. if (sizeof_priv) {
  4625. /* ensure 32-byte alignment of private area */
  4626. alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
  4627. alloc_size += sizeof_priv;
  4628. }
  4629. /* ensure 32-byte alignment of whole construct */
  4630. alloc_size += NETDEV_ALIGN - 1;
  4631. p = kzalloc(alloc_size, GFP_KERNEL);
  4632. if (!p) {
  4633. printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
  4634. return NULL;
  4635. }
  4636. tx = kcalloc(queue_count, sizeof(struct netdev_queue), GFP_KERNEL);
  4637. if (!tx) {
  4638. printk(KERN_ERR "alloc_netdev: Unable to allocate "
  4639. "tx qdiscs.\n");
  4640. goto free_p;
  4641. }
  4642. #ifdef CONFIG_RPS
  4643. rx = kcalloc(queue_count, sizeof(struct netdev_rx_queue), GFP_KERNEL);
  4644. if (!rx) {
  4645. printk(KERN_ERR "alloc_netdev: Unable to allocate "
  4646. "rx queues.\n");
  4647. goto free_tx;
  4648. }
  4649. atomic_set(&rx->count, queue_count);
  4650. /*
  4651. * Set a pointer to first element in the array which holds the
  4652. * reference count.
  4653. */
  4654. for (i = 0; i < queue_count; i++)
  4655. rx[i].first = rx;
  4656. #endif
  4657. dev = PTR_ALIGN(p, NETDEV_ALIGN);
  4658. dev->padded = (char *)dev - (char *)p;
  4659. if (dev_addr_init(dev))
  4660. goto free_rx;
  4661. dev_mc_init(dev);
  4662. dev_uc_init(dev);
  4663. dev_net_set(dev, &init_net);
  4664. dev->_tx = tx;
  4665. dev->num_tx_queues = queue_count;
  4666. dev->real_num_tx_queues = queue_count;
  4667. #ifdef CONFIG_RPS
  4668. dev->_rx = rx;
  4669. dev->num_rx_queues = queue_count;
  4670. #endif
  4671. dev->gso_max_size = GSO_MAX_SIZE;
  4672. netdev_init_queues(dev);
  4673. INIT_LIST_HEAD(&dev->ethtool_ntuple_list.list);
  4674. dev->ethtool_ntuple_list.count = 0;
  4675. INIT_LIST_HEAD(&dev->napi_list);
  4676. INIT_LIST_HEAD(&dev->unreg_list);
  4677. INIT_LIST_HEAD(&dev->link_watch_list);
  4678. dev->priv_flags = IFF_XMIT_DST_RELEASE;
  4679. setup(dev);
  4680. strcpy(dev->name, name);
  4681. return dev;
  4682. free_rx:
  4683. #ifdef CONFIG_RPS
  4684. kfree(rx);
  4685. free_tx:
  4686. #endif
  4687. kfree(tx);
  4688. free_p:
  4689. kfree(p);
  4690. return NULL;
  4691. }
  4692. EXPORT_SYMBOL(alloc_netdev_mq);
  4693. /**
  4694. * free_netdev - free network device
  4695. * @dev: device
  4696. *
  4697. * This function does the last stage of destroying an allocated device
  4698. * interface. The reference to the device object is released.
  4699. * If this is the last reference then it will be freed.
  4700. */
  4701. void free_netdev(struct net_device *dev)
  4702. {
  4703. struct napi_struct *p, *n;
  4704. release_net(dev_net(dev));
  4705. kfree(dev->_tx);
  4706. /* Flush device addresses */
  4707. dev_addr_flush(dev);
  4708. /* Clear ethtool n-tuple list */
  4709. ethtool_ntuple_flush(dev);
  4710. list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
  4711. netif_napi_del(p);
  4712. /* Compatibility with error handling in drivers */
  4713. if (dev->reg_state == NETREG_UNINITIALIZED) {
  4714. kfree((char *)dev - dev->padded);
  4715. return;
  4716. }
  4717. BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
  4718. dev->reg_state = NETREG_RELEASED;
  4719. /* will free via device release */
  4720. put_device(&dev->dev);
  4721. }
  4722. EXPORT_SYMBOL(free_netdev);
  4723. /**
  4724. * synchronize_net - Synchronize with packet receive processing
  4725. *
  4726. * Wait for packets currently being received to be done.
  4727. * Does not block later packets from starting.
  4728. */
  4729. void synchronize_net(void)
  4730. {
  4731. might_sleep();
  4732. synchronize_rcu();
  4733. }
  4734. EXPORT_SYMBOL(synchronize_net);
  4735. /**
  4736. * unregister_netdevice_queue - remove device from the kernel
  4737. * @dev: device
  4738. * @head: list
  4739. *
  4740. * This function shuts down a device interface and removes it
  4741. * from the kernel tables.
  4742. * If head not NULL, device is queued to be unregistered later.
  4743. *
  4744. * Callers must hold the rtnl semaphore. You may want
  4745. * unregister_netdev() instead of this.
  4746. */
  4747. void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
  4748. {
  4749. ASSERT_RTNL();
  4750. if (head) {
  4751. list_move_tail(&dev->unreg_list, head);
  4752. } else {
  4753. rollback_registered(dev);
  4754. /* Finish processing unregister after unlock */
  4755. net_set_todo(dev);
  4756. }
  4757. }
  4758. EXPORT_SYMBOL(unregister_netdevice_queue);
  4759. /**
  4760. * unregister_netdevice_many - unregister many devices
  4761. * @head: list of devices
  4762. */
  4763. void unregister_netdevice_many(struct list_head *head)
  4764. {
  4765. struct net_device *dev;
  4766. if (!list_empty(head)) {
  4767. rollback_registered_many(head);
  4768. list_for_each_entry(dev, head, unreg_list)
  4769. net_set_todo(dev);
  4770. }
  4771. }
  4772. EXPORT_SYMBOL(unregister_netdevice_many);
  4773. /**
  4774. * unregister_netdev - remove device from the kernel
  4775. * @dev: device
  4776. *
  4777. * This function shuts down a device interface and removes it
  4778. * from the kernel tables.
  4779. *
  4780. * This is just a wrapper for unregister_netdevice that takes
  4781. * the rtnl semaphore. In general you want to use this and not
  4782. * unregister_netdevice.
  4783. */
  4784. void unregister_netdev(struct net_device *dev)
  4785. {
  4786. rtnl_lock();
  4787. unregister_netdevice(dev);
  4788. rtnl_unlock();
  4789. }
  4790. EXPORT_SYMBOL(unregister_netdev);
  4791. /**
  4792. * dev_change_net_namespace - move device to different nethost namespace
  4793. * @dev: device
  4794. * @net: network namespace
  4795. * @pat: If not NULL name pattern to try if the current device name
  4796. * is already taken in the destination network namespace.
  4797. *
  4798. * This function shuts down a device interface and moves it
  4799. * to a new network namespace. On success 0 is returned, on
  4800. * a failure a netagive errno code is returned.
  4801. *
  4802. * Callers must hold the rtnl semaphore.
  4803. */
  4804. int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
  4805. {
  4806. int err;
  4807. ASSERT_RTNL();
  4808. /* Don't allow namespace local devices to be moved. */
  4809. err = -EINVAL;
  4810. if (dev->features & NETIF_F_NETNS_LOCAL)
  4811. goto out;
  4812. /* Ensure the device has been registrered */
  4813. err = -EINVAL;
  4814. if (dev->reg_state != NETREG_REGISTERED)
  4815. goto out;
  4816. /* Get out if there is nothing todo */
  4817. err = 0;
  4818. if (net_eq(dev_net(dev), net))
  4819. goto out;
  4820. /* Pick the destination device name, and ensure
  4821. * we can use it in the destination network namespace.
  4822. */
  4823. err = -EEXIST;
  4824. if (__dev_get_by_name(net, dev->name)) {
  4825. /* We get here if we can't use the current device name */
  4826. if (!pat)
  4827. goto out;
  4828. if (dev_get_valid_name(dev, pat, 1))
  4829. goto out;
  4830. }
  4831. /*
  4832. * And now a mini version of register_netdevice unregister_netdevice.
  4833. */
  4834. /* If device is running close it first. */
  4835. dev_close(dev);
  4836. /* And unlink it from device chain */
  4837. err = -ENODEV;
  4838. unlist_netdevice(dev);
  4839. synchronize_net();
  4840. /* Shutdown queueing discipline. */
  4841. dev_shutdown(dev);
  4842. /* Notify protocols, that we are about to destroy
  4843. this device. They should clean all the things.
  4844. */
  4845. call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
  4846. call_netdevice_notifiers(NETDEV_UNREGISTER_BATCH, dev);
  4847. /*
  4848. * Flush the unicast and multicast chains
  4849. */
  4850. dev_uc_flush(dev);
  4851. dev_mc_flush(dev);
  4852. /* Actually switch the network namespace */
  4853. dev_net_set(dev, net);
  4854. /* If there is an ifindex conflict assign a new one */
  4855. if (__dev_get_by_index(net, dev->ifindex)) {
  4856. int iflink = (dev->iflink == dev->ifindex);
  4857. dev->ifindex = dev_new_index(net);
  4858. if (iflink)
  4859. dev->iflink = dev->ifindex;
  4860. }
  4861. /* Fixup kobjects */
  4862. err = device_rename(&dev->dev, dev->name);
  4863. WARN_ON(err);
  4864. /* Add the device back in the hashes */
  4865. list_netdevice(dev);
  4866. /* Notify protocols, that a new device appeared. */
  4867. call_netdevice_notifiers(NETDEV_REGISTER, dev);
  4868. /*
  4869. * Prevent userspace races by waiting until the network
  4870. * device is fully setup before sending notifications.
  4871. */
  4872. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
  4873. synchronize_net();
  4874. err = 0;
  4875. out:
  4876. return err;
  4877. }
  4878. EXPORT_SYMBOL_GPL(dev_change_net_namespace);
  4879. static int dev_cpu_callback(struct notifier_block *nfb,
  4880. unsigned long action,
  4881. void *ocpu)
  4882. {
  4883. struct sk_buff **list_skb;
  4884. struct sk_buff *skb;
  4885. unsigned int cpu, oldcpu = (unsigned long)ocpu;
  4886. struct softnet_data *sd, *oldsd;
  4887. if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
  4888. return NOTIFY_OK;
  4889. local_irq_disable();
  4890. cpu = smp_processor_id();
  4891. sd = &per_cpu(softnet_data, cpu);
  4892. oldsd = &per_cpu(softnet_data, oldcpu);
  4893. /* Find end of our completion_queue. */
  4894. list_skb = &sd->completion_queue;
  4895. while (*list_skb)
  4896. list_skb = &(*list_skb)->next;
  4897. /* Append completion queue from offline CPU. */
  4898. *list_skb = oldsd->completion_queue;
  4899. oldsd->completion_queue = NULL;
  4900. /* Append output queue from offline CPU. */
  4901. if (oldsd->output_queue) {
  4902. *sd->output_queue_tailp = oldsd->output_queue;
  4903. sd->output_queue_tailp = oldsd->output_queue_tailp;
  4904. oldsd->output_queue = NULL;
  4905. oldsd->output_queue_tailp = &oldsd->output_queue;
  4906. }
  4907. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  4908. local_irq_enable();
  4909. /* Process offline CPU's input_pkt_queue */
  4910. while ((skb = __skb_dequeue(&oldsd->process_queue))) {
  4911. netif_rx(skb);
  4912. input_queue_head_incr(oldsd);
  4913. }
  4914. while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
  4915. netif_rx(skb);
  4916. input_queue_head_incr(oldsd);
  4917. }
  4918. return NOTIFY_OK;
  4919. }
  4920. /**
  4921. * netdev_increment_features - increment feature set by one
  4922. * @all: current feature set
  4923. * @one: new feature set
  4924. * @mask: mask feature set
  4925. *
  4926. * Computes a new feature set after adding a device with feature set
  4927. * @one to the master device with current feature set @all. Will not
  4928. * enable anything that is off in @mask. Returns the new feature set.
  4929. */
  4930. unsigned long netdev_increment_features(unsigned long all, unsigned long one,
  4931. unsigned long mask)
  4932. {
  4933. /* If device needs checksumming, downgrade to it. */
  4934. if (all & NETIF_F_NO_CSUM && !(one & NETIF_F_NO_CSUM))
  4935. all ^= NETIF_F_NO_CSUM | (one & NETIF_F_ALL_CSUM);
  4936. else if (mask & NETIF_F_ALL_CSUM) {
  4937. /* If one device supports v4/v6 checksumming, set for all. */
  4938. if (one & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM) &&
  4939. !(all & NETIF_F_GEN_CSUM)) {
  4940. all &= ~NETIF_F_ALL_CSUM;
  4941. all |= one & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
  4942. }
  4943. /* If one device supports hw checksumming, set for all. */
  4944. if (one & NETIF_F_GEN_CSUM && !(all & NETIF_F_GEN_CSUM)) {
  4945. all &= ~NETIF_F_ALL_CSUM;
  4946. all |= NETIF_F_HW_CSUM;
  4947. }
  4948. }
  4949. one |= NETIF_F_ALL_CSUM;
  4950. one |= all & NETIF_F_ONE_FOR_ALL;
  4951. all &= one | NETIF_F_LLTX | NETIF_F_GSO | NETIF_F_UFO;
  4952. all |= one & mask & NETIF_F_ONE_FOR_ALL;
  4953. return all;
  4954. }
  4955. EXPORT_SYMBOL(netdev_increment_features);
  4956. static struct hlist_head *netdev_create_hash(void)
  4957. {
  4958. int i;
  4959. struct hlist_head *hash;
  4960. hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
  4961. if (hash != NULL)
  4962. for (i = 0; i < NETDEV_HASHENTRIES; i++)
  4963. INIT_HLIST_HEAD(&hash[i]);
  4964. return hash;
  4965. }
  4966. /* Initialize per network namespace state */
  4967. static int __net_init netdev_init(struct net *net)
  4968. {
  4969. INIT_LIST_HEAD(&net->dev_base_head);
  4970. net->dev_name_head = netdev_create_hash();
  4971. if (net->dev_name_head == NULL)
  4972. goto err_name;
  4973. net->dev_index_head = netdev_create_hash();
  4974. if (net->dev_index_head == NULL)
  4975. goto err_idx;
  4976. return 0;
  4977. err_idx:
  4978. kfree(net->dev_name_head);
  4979. err_name:
  4980. return -ENOMEM;
  4981. }
  4982. /**
  4983. * netdev_drivername - network driver for the device
  4984. * @dev: network device
  4985. * @buffer: buffer for resulting name
  4986. * @len: size of buffer
  4987. *
  4988. * Determine network driver for device.
  4989. */
  4990. char *netdev_drivername(const struct net_device *dev, char *buffer, int len)
  4991. {
  4992. const struct device_driver *driver;
  4993. const struct device *parent;
  4994. if (len <= 0 || !buffer)
  4995. return buffer;
  4996. buffer[0] = 0;
  4997. parent = dev->dev.parent;
  4998. if (!parent)
  4999. return buffer;
  5000. driver = parent->driver;
  5001. if (driver && driver->name)
  5002. strlcpy(buffer, driver->name, len);
  5003. return buffer;
  5004. }
  5005. static int __netdev_printk(const char *level, const struct net_device *dev,
  5006. struct va_format *vaf)
  5007. {
  5008. int r;
  5009. if (dev && dev->dev.parent)
  5010. r = dev_printk(level, dev->dev.parent, "%s: %pV",
  5011. netdev_name(dev), vaf);
  5012. else if (dev)
  5013. r = printk("%s%s: %pV", level, netdev_name(dev), vaf);
  5014. else
  5015. r = printk("%s(NULL net_device): %pV", level, vaf);
  5016. return r;
  5017. }
  5018. int netdev_printk(const char *level, const struct net_device *dev,
  5019. const char *format, ...)
  5020. {
  5021. struct va_format vaf;
  5022. va_list args;
  5023. int r;
  5024. va_start(args, format);
  5025. vaf.fmt = format;
  5026. vaf.va = &args;
  5027. r = __netdev_printk(level, dev, &vaf);
  5028. va_end(args);
  5029. return r;
  5030. }
  5031. EXPORT_SYMBOL(netdev_printk);
  5032. #define define_netdev_printk_level(func, level) \
  5033. int func(const struct net_device *dev, const char *fmt, ...) \
  5034. { \
  5035. int r; \
  5036. struct va_format vaf; \
  5037. va_list args; \
  5038. \
  5039. va_start(args, fmt); \
  5040. \
  5041. vaf.fmt = fmt; \
  5042. vaf.va = &args; \
  5043. \
  5044. r = __netdev_printk(level, dev, &vaf); \
  5045. va_end(args); \
  5046. \
  5047. return r; \
  5048. } \
  5049. EXPORT_SYMBOL(func);
  5050. define_netdev_printk_level(netdev_emerg, KERN_EMERG);
  5051. define_netdev_printk_level(netdev_alert, KERN_ALERT);
  5052. define_netdev_printk_level(netdev_crit, KERN_CRIT);
  5053. define_netdev_printk_level(netdev_err, KERN_ERR);
  5054. define_netdev_printk_level(netdev_warn, KERN_WARNING);
  5055. define_netdev_printk_level(netdev_notice, KERN_NOTICE);
  5056. define_netdev_printk_level(netdev_info, KERN_INFO);
  5057. static void __net_exit netdev_exit(struct net *net)
  5058. {
  5059. kfree(net->dev_name_head);
  5060. kfree(net->dev_index_head);
  5061. }
  5062. static struct pernet_operations __net_initdata netdev_net_ops = {
  5063. .init = netdev_init,
  5064. .exit = netdev_exit,
  5065. };
  5066. static void __net_exit default_device_exit(struct net *net)
  5067. {
  5068. struct net_device *dev, *aux;
  5069. /*
  5070. * Push all migratable network devices back to the
  5071. * initial network namespace
  5072. */
  5073. rtnl_lock();
  5074. for_each_netdev_safe(net, dev, aux) {
  5075. int err;
  5076. char fb_name[IFNAMSIZ];
  5077. /* Ignore unmoveable devices (i.e. loopback) */
  5078. if (dev->features & NETIF_F_NETNS_LOCAL)
  5079. continue;
  5080. /* Leave virtual devices for the generic cleanup */
  5081. if (dev->rtnl_link_ops)
  5082. continue;
  5083. /* Push remaing network devices to init_net */
  5084. snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
  5085. err = dev_change_net_namespace(dev, &init_net, fb_name);
  5086. if (err) {
  5087. printk(KERN_EMERG "%s: failed to move %s to init_net: %d\n",
  5088. __func__, dev->name, err);
  5089. BUG();
  5090. }
  5091. }
  5092. rtnl_unlock();
  5093. }
  5094. static void __net_exit default_device_exit_batch(struct list_head *net_list)
  5095. {
  5096. /* At exit all network devices most be removed from a network
  5097. * namespace. Do this in the reverse order of registeration.
  5098. * Do this across as many network namespaces as possible to
  5099. * improve batching efficiency.
  5100. */
  5101. struct net_device *dev;
  5102. struct net *net;
  5103. LIST_HEAD(dev_kill_list);
  5104. rtnl_lock();
  5105. list_for_each_entry(net, net_list, exit_list) {
  5106. for_each_netdev_reverse(net, dev) {
  5107. if (dev->rtnl_link_ops)
  5108. dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
  5109. else
  5110. unregister_netdevice_queue(dev, &dev_kill_list);
  5111. }
  5112. }
  5113. unregister_netdevice_many(&dev_kill_list);
  5114. rtnl_unlock();
  5115. }
  5116. static struct pernet_operations __net_initdata default_device_ops = {
  5117. .exit = default_device_exit,
  5118. .exit_batch = default_device_exit_batch,
  5119. };
  5120. /*
  5121. * Initialize the DEV module. At boot time this walks the device list and
  5122. * unhooks any devices that fail to initialise (normally hardware not
  5123. * present) and leaves us with a valid list of present and active devices.
  5124. *
  5125. */
  5126. /*
  5127. * This is called single threaded during boot, so no need
  5128. * to take the rtnl semaphore.
  5129. */
  5130. static int __init net_dev_init(void)
  5131. {
  5132. int i, rc = -ENOMEM;
  5133. BUG_ON(!dev_boot_phase);
  5134. if (dev_proc_init())
  5135. goto out;
  5136. if (netdev_kobject_init())
  5137. goto out;
  5138. INIT_LIST_HEAD(&ptype_all);
  5139. for (i = 0; i < PTYPE_HASH_SIZE; i++)
  5140. INIT_LIST_HEAD(&ptype_base[i]);
  5141. if (register_pernet_subsys(&netdev_net_ops))
  5142. goto out;
  5143. /*
  5144. * Initialise the packet receive queues.
  5145. */
  5146. for_each_possible_cpu(i) {
  5147. struct softnet_data *sd = &per_cpu(softnet_data, i);
  5148. memset(sd, 0, sizeof(*sd));
  5149. skb_queue_head_init(&sd->input_pkt_queue);
  5150. skb_queue_head_init(&sd->process_queue);
  5151. sd->completion_queue = NULL;
  5152. INIT_LIST_HEAD(&sd->poll_list);
  5153. sd->output_queue = NULL;
  5154. sd->output_queue_tailp = &sd->output_queue;
  5155. #ifdef CONFIG_RPS
  5156. sd->csd.func = rps_trigger_softirq;
  5157. sd->csd.info = sd;
  5158. sd->csd.flags = 0;
  5159. sd->cpu = i;
  5160. #endif
  5161. sd->backlog.poll = process_backlog;
  5162. sd->backlog.weight = weight_p;
  5163. sd->backlog.gro_list = NULL;
  5164. sd->backlog.gro_count = 0;
  5165. }
  5166. dev_boot_phase = 0;
  5167. /* The loopback device is special if any other network devices
  5168. * is present in a network namespace the loopback device must
  5169. * be present. Since we now dynamically allocate and free the
  5170. * loopback device ensure this invariant is maintained by
  5171. * keeping the loopback device as the first device on the
  5172. * list of network devices. Ensuring the loopback devices
  5173. * is the first device that appears and the last network device
  5174. * that disappears.
  5175. */
  5176. if (register_pernet_device(&loopback_net_ops))
  5177. goto out;
  5178. if (register_pernet_device(&default_device_ops))
  5179. goto out;
  5180. open_softirq(NET_TX_SOFTIRQ, net_tx_action);
  5181. open_softirq(NET_RX_SOFTIRQ, net_rx_action);
  5182. hotcpu_notifier(dev_cpu_callback, 0);
  5183. dst_init();
  5184. dev_mcast_init();
  5185. rc = 0;
  5186. out:
  5187. return rc;
  5188. }
  5189. subsys_initcall(net_dev_init);
  5190. static int __init initialize_hashrnd(void)
  5191. {
  5192. get_random_bytes(&hashrnd, sizeof(hashrnd));
  5193. return 0;
  5194. }
  5195. late_initcall_sync(initialize_hashrnd);