dev.c 153 KB

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