dev.c 158 KB

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