dev.c 150 KB

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