dev.c 140 KB

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