dev.c 162 KB

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