dev.c 158 KB

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