dev.c 153 KB

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