dev.c 152 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293
  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(struct net_device *dev)
  1061. {
  1062. const struct net_device_ops *ops = dev->netdev_ops;
  1063. ASSERT_RTNL();
  1064. might_sleep();
  1065. /*
  1066. * Tell people we are going down, so that they can
  1067. * prepare to death, when device is still operating.
  1068. */
  1069. call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
  1070. clear_bit(__LINK_STATE_START, &dev->state);
  1071. /* Synchronize to scheduled poll. We cannot touch poll list,
  1072. * it can be even on different cpu. So just clear netif_running().
  1073. *
  1074. * dev->stop() will invoke napi_disable() on all of it's
  1075. * napi_struct instances on this device.
  1076. */
  1077. smp_mb__after_clear_bit(); /* Commit netif_running(). */
  1078. dev_deactivate(dev);
  1079. /*
  1080. * Call the device specific close. This cannot fail.
  1081. * Only if device is UP
  1082. *
  1083. * We allow it to be called even after a DETACH hot-plug
  1084. * event.
  1085. */
  1086. if (ops->ndo_stop)
  1087. ops->ndo_stop(dev);
  1088. /*
  1089. * Device is now down.
  1090. */
  1091. dev->flags &= ~IFF_UP;
  1092. /*
  1093. * Shutdown NET_DMA
  1094. */
  1095. net_dmaengine_put();
  1096. return 0;
  1097. }
  1098. /**
  1099. * dev_close - shutdown an interface.
  1100. * @dev: device to shutdown
  1101. *
  1102. * This function moves an active device into down state. A
  1103. * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
  1104. * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
  1105. * chain.
  1106. */
  1107. int dev_close(struct net_device *dev)
  1108. {
  1109. if (!(dev->flags & IFF_UP))
  1110. return 0;
  1111. __dev_close(dev);
  1112. /*
  1113. * Tell people we are down
  1114. */
  1115. rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
  1116. call_netdevice_notifiers(NETDEV_DOWN, dev);
  1117. return 0;
  1118. }
  1119. EXPORT_SYMBOL(dev_close);
  1120. /**
  1121. * dev_disable_lro - disable Large Receive Offload on a device
  1122. * @dev: device
  1123. *
  1124. * Disable Large Receive Offload (LRO) on a net device. Must be
  1125. * called under RTNL. This is needed if received packets may be
  1126. * forwarded to another interface.
  1127. */
  1128. void dev_disable_lro(struct net_device *dev)
  1129. {
  1130. if (dev->ethtool_ops && dev->ethtool_ops->get_flags &&
  1131. dev->ethtool_ops->set_flags) {
  1132. u32 flags = dev->ethtool_ops->get_flags(dev);
  1133. if (flags & ETH_FLAG_LRO) {
  1134. flags &= ~ETH_FLAG_LRO;
  1135. dev->ethtool_ops->set_flags(dev, flags);
  1136. }
  1137. }
  1138. WARN_ON(dev->features & NETIF_F_LRO);
  1139. }
  1140. EXPORT_SYMBOL(dev_disable_lro);
  1141. static int dev_boot_phase = 1;
  1142. /*
  1143. * Device change register/unregister. These are not inline or static
  1144. * as we export them to the world.
  1145. */
  1146. /**
  1147. * register_netdevice_notifier - register a network notifier block
  1148. * @nb: notifier
  1149. *
  1150. * Register a notifier to be called when network device events occur.
  1151. * The notifier passed is linked into the kernel structures and must
  1152. * not be reused until it has been unregistered. A negative errno code
  1153. * is returned on a failure.
  1154. *
  1155. * When registered all registration and up events are replayed
  1156. * to the new notifier to allow device to have a race free
  1157. * view of the network device list.
  1158. */
  1159. int register_netdevice_notifier(struct notifier_block *nb)
  1160. {
  1161. struct net_device *dev;
  1162. struct net_device *last;
  1163. struct net *net;
  1164. int err;
  1165. rtnl_lock();
  1166. err = raw_notifier_chain_register(&netdev_chain, nb);
  1167. if (err)
  1168. goto unlock;
  1169. if (dev_boot_phase)
  1170. goto unlock;
  1171. for_each_net(net) {
  1172. for_each_netdev(net, dev) {
  1173. err = nb->notifier_call(nb, NETDEV_REGISTER, dev);
  1174. err = notifier_to_errno(err);
  1175. if (err)
  1176. goto rollback;
  1177. if (!(dev->flags & IFF_UP))
  1178. continue;
  1179. nb->notifier_call(nb, NETDEV_UP, dev);
  1180. }
  1181. }
  1182. unlock:
  1183. rtnl_unlock();
  1184. return err;
  1185. rollback:
  1186. last = dev;
  1187. for_each_net(net) {
  1188. for_each_netdev(net, dev) {
  1189. if (dev == last)
  1190. break;
  1191. if (dev->flags & IFF_UP) {
  1192. nb->notifier_call(nb, NETDEV_GOING_DOWN, dev);
  1193. nb->notifier_call(nb, NETDEV_DOWN, dev);
  1194. }
  1195. nb->notifier_call(nb, NETDEV_UNREGISTER, dev);
  1196. nb->notifier_call(nb, NETDEV_UNREGISTER_BATCH, dev);
  1197. }
  1198. }
  1199. raw_notifier_chain_unregister(&netdev_chain, nb);
  1200. goto unlock;
  1201. }
  1202. EXPORT_SYMBOL(register_netdevice_notifier);
  1203. /**
  1204. * unregister_netdevice_notifier - unregister a network notifier block
  1205. * @nb: notifier
  1206. *
  1207. * Unregister a notifier previously registered by
  1208. * register_netdevice_notifier(). The notifier is unlinked into the
  1209. * kernel structures and may then be reused. A negative errno code
  1210. * is returned on a failure.
  1211. */
  1212. int unregister_netdevice_notifier(struct notifier_block *nb)
  1213. {
  1214. int err;
  1215. rtnl_lock();
  1216. err = raw_notifier_chain_unregister(&netdev_chain, nb);
  1217. rtnl_unlock();
  1218. return err;
  1219. }
  1220. EXPORT_SYMBOL(unregister_netdevice_notifier);
  1221. /**
  1222. * call_netdevice_notifiers - call all network notifier blocks
  1223. * @val: value passed unmodified to notifier function
  1224. * @dev: net_device pointer passed unmodified to notifier function
  1225. *
  1226. * Call all network notifier blocks. Parameters and return value
  1227. * are as for raw_notifier_call_chain().
  1228. */
  1229. int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
  1230. {
  1231. ASSERT_RTNL();
  1232. return raw_notifier_call_chain(&netdev_chain, val, dev);
  1233. }
  1234. /* When > 0 there are consumers of rx skb time stamps */
  1235. static atomic_t netstamp_needed = ATOMIC_INIT(0);
  1236. void net_enable_timestamp(void)
  1237. {
  1238. atomic_inc(&netstamp_needed);
  1239. }
  1240. EXPORT_SYMBOL(net_enable_timestamp);
  1241. void net_disable_timestamp(void)
  1242. {
  1243. atomic_dec(&netstamp_needed);
  1244. }
  1245. EXPORT_SYMBOL(net_disable_timestamp);
  1246. static inline void net_timestamp_set(struct sk_buff *skb)
  1247. {
  1248. if (atomic_read(&netstamp_needed))
  1249. __net_timestamp(skb);
  1250. else
  1251. skb->tstamp.tv64 = 0;
  1252. }
  1253. static inline void net_timestamp_check(struct sk_buff *skb)
  1254. {
  1255. if (!skb->tstamp.tv64 && atomic_read(&netstamp_needed))
  1256. __net_timestamp(skb);
  1257. }
  1258. /**
  1259. * dev_forward_skb - loopback an skb to another netif
  1260. *
  1261. * @dev: destination network device
  1262. * @skb: buffer to forward
  1263. *
  1264. * return values:
  1265. * NET_RX_SUCCESS (no congestion)
  1266. * NET_RX_DROP (packet was dropped, but freed)
  1267. *
  1268. * dev_forward_skb can be used for injecting an skb from the
  1269. * start_xmit function of one device into the receive queue
  1270. * of another device.
  1271. *
  1272. * The receiving device may be in another namespace, so
  1273. * we have to clear all information in the skb that could
  1274. * impact namespace isolation.
  1275. */
  1276. int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
  1277. {
  1278. skb_orphan(skb);
  1279. nf_reset(skb);
  1280. if (unlikely(!(dev->flags & IFF_UP) ||
  1281. (skb->len > (dev->mtu + dev->hard_header_len + VLAN_HLEN)))) {
  1282. atomic_long_inc(&dev->rx_dropped);
  1283. kfree_skb(skb);
  1284. return NET_RX_DROP;
  1285. }
  1286. skb_set_dev(skb, dev);
  1287. skb->tstamp.tv64 = 0;
  1288. skb->pkt_type = PACKET_HOST;
  1289. skb->protocol = eth_type_trans(skb, dev);
  1290. return netif_rx(skb);
  1291. }
  1292. EXPORT_SYMBOL_GPL(dev_forward_skb);
  1293. /*
  1294. * Support routine. Sends outgoing frames to any network
  1295. * taps currently in use.
  1296. */
  1297. static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
  1298. {
  1299. struct packet_type *ptype;
  1300. #ifdef CONFIG_NET_CLS_ACT
  1301. if (!(skb->tstamp.tv64 && (G_TC_FROM(skb->tc_verd) & AT_INGRESS)))
  1302. net_timestamp_set(skb);
  1303. #else
  1304. net_timestamp_set(skb);
  1305. #endif
  1306. rcu_read_lock();
  1307. list_for_each_entry_rcu(ptype, &ptype_all, list) {
  1308. /* Never send packets back to the socket
  1309. * they originated from - MvS (miquels@drinkel.ow.org)
  1310. */
  1311. if ((ptype->dev == dev || !ptype->dev) &&
  1312. (ptype->af_packet_priv == NULL ||
  1313. (struct sock *)ptype->af_packet_priv != skb->sk)) {
  1314. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  1315. if (!skb2)
  1316. break;
  1317. /* skb->nh should be correctly
  1318. set by sender, so that the second statement is
  1319. just protection against buggy protocols.
  1320. */
  1321. skb_reset_mac_header(skb2);
  1322. if (skb_network_header(skb2) < skb2->data ||
  1323. skb2->network_header > skb2->tail) {
  1324. if (net_ratelimit())
  1325. printk(KERN_CRIT "protocol %04x is "
  1326. "buggy, dev %s\n",
  1327. ntohs(skb2->protocol),
  1328. dev->name);
  1329. skb_reset_network_header(skb2);
  1330. }
  1331. skb2->transport_header = skb2->network_header;
  1332. skb2->pkt_type = PACKET_OUTGOING;
  1333. ptype->func(skb2, skb->dev, ptype, skb->dev);
  1334. }
  1335. }
  1336. rcu_read_unlock();
  1337. }
  1338. /*
  1339. * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
  1340. * greater then real_num_tx_queues stale skbs on the qdisc must be flushed.
  1341. */
  1342. int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
  1343. {
  1344. int rc;
  1345. if (txq < 1 || txq > dev->num_tx_queues)
  1346. return -EINVAL;
  1347. if (dev->reg_state == NETREG_REGISTERED) {
  1348. ASSERT_RTNL();
  1349. rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
  1350. txq);
  1351. if (rc)
  1352. return rc;
  1353. if (txq < dev->real_num_tx_queues)
  1354. qdisc_reset_all_tx_gt(dev, txq);
  1355. }
  1356. dev->real_num_tx_queues = txq;
  1357. return 0;
  1358. }
  1359. EXPORT_SYMBOL(netif_set_real_num_tx_queues);
  1360. #ifdef CONFIG_RPS
  1361. /**
  1362. * netif_set_real_num_rx_queues - set actual number of RX queues used
  1363. * @dev: Network device
  1364. * @rxq: Actual number of RX queues
  1365. *
  1366. * This must be called either with the rtnl_lock held or before
  1367. * registration of the net device. Returns 0 on success, or a
  1368. * negative error code. If called before registration, it always
  1369. * succeeds.
  1370. */
  1371. int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
  1372. {
  1373. int rc;
  1374. if (rxq < 1 || rxq > dev->num_rx_queues)
  1375. return -EINVAL;
  1376. if (dev->reg_state == NETREG_REGISTERED) {
  1377. ASSERT_RTNL();
  1378. rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
  1379. rxq);
  1380. if (rc)
  1381. return rc;
  1382. }
  1383. dev->real_num_rx_queues = rxq;
  1384. return 0;
  1385. }
  1386. EXPORT_SYMBOL(netif_set_real_num_rx_queues);
  1387. #endif
  1388. static inline void __netif_reschedule(struct Qdisc *q)
  1389. {
  1390. struct softnet_data *sd;
  1391. unsigned long flags;
  1392. local_irq_save(flags);
  1393. sd = &__get_cpu_var(softnet_data);
  1394. q->next_sched = NULL;
  1395. *sd->output_queue_tailp = q;
  1396. sd->output_queue_tailp = &q->next_sched;
  1397. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  1398. local_irq_restore(flags);
  1399. }
  1400. void __netif_schedule(struct Qdisc *q)
  1401. {
  1402. if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
  1403. __netif_reschedule(q);
  1404. }
  1405. EXPORT_SYMBOL(__netif_schedule);
  1406. void dev_kfree_skb_irq(struct sk_buff *skb)
  1407. {
  1408. if (atomic_dec_and_test(&skb->users)) {
  1409. struct softnet_data *sd;
  1410. unsigned long flags;
  1411. local_irq_save(flags);
  1412. sd = &__get_cpu_var(softnet_data);
  1413. skb->next = sd->completion_queue;
  1414. sd->completion_queue = skb;
  1415. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  1416. local_irq_restore(flags);
  1417. }
  1418. }
  1419. EXPORT_SYMBOL(dev_kfree_skb_irq);
  1420. void dev_kfree_skb_any(struct sk_buff *skb)
  1421. {
  1422. if (in_irq() || irqs_disabled())
  1423. dev_kfree_skb_irq(skb);
  1424. else
  1425. dev_kfree_skb(skb);
  1426. }
  1427. EXPORT_SYMBOL(dev_kfree_skb_any);
  1428. /**
  1429. * netif_device_detach - mark device as removed
  1430. * @dev: network device
  1431. *
  1432. * Mark device as removed from system and therefore no longer available.
  1433. */
  1434. void netif_device_detach(struct net_device *dev)
  1435. {
  1436. if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
  1437. netif_running(dev)) {
  1438. netif_tx_stop_all_queues(dev);
  1439. }
  1440. }
  1441. EXPORT_SYMBOL(netif_device_detach);
  1442. /**
  1443. * netif_device_attach - mark device as attached
  1444. * @dev: network device
  1445. *
  1446. * Mark device as attached from system and restart if needed.
  1447. */
  1448. void netif_device_attach(struct net_device *dev)
  1449. {
  1450. if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
  1451. netif_running(dev)) {
  1452. netif_tx_wake_all_queues(dev);
  1453. __netdev_watchdog_up(dev);
  1454. }
  1455. }
  1456. EXPORT_SYMBOL(netif_device_attach);
  1457. static bool can_checksum_protocol(unsigned long features, __be16 protocol)
  1458. {
  1459. return ((features & NETIF_F_NO_CSUM) ||
  1460. ((features & NETIF_F_V4_CSUM) &&
  1461. protocol == htons(ETH_P_IP)) ||
  1462. ((features & NETIF_F_V6_CSUM) &&
  1463. protocol == htons(ETH_P_IPV6)) ||
  1464. ((features & NETIF_F_FCOE_CRC) &&
  1465. protocol == htons(ETH_P_FCOE)));
  1466. }
  1467. static bool dev_can_checksum(struct net_device *dev, struct sk_buff *skb)
  1468. {
  1469. __be16 protocol = skb->protocol;
  1470. int features = dev->features;
  1471. if (vlan_tx_tag_present(skb)) {
  1472. features &= dev->vlan_features;
  1473. } else if (protocol == htons(ETH_P_8021Q)) {
  1474. struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
  1475. protocol = veh->h_vlan_encapsulated_proto;
  1476. features &= dev->vlan_features;
  1477. }
  1478. return can_checksum_protocol(features, protocol);
  1479. }
  1480. /**
  1481. * skb_dev_set -- assign a new device to a buffer
  1482. * @skb: buffer for the new device
  1483. * @dev: network device
  1484. *
  1485. * If an skb is owned by a device already, we have to reset
  1486. * all data private to the namespace a device belongs to
  1487. * before assigning it a new device.
  1488. */
  1489. #ifdef CONFIG_NET_NS
  1490. void skb_set_dev(struct sk_buff *skb, struct net_device *dev)
  1491. {
  1492. skb_dst_drop(skb);
  1493. if (skb->dev && !net_eq(dev_net(skb->dev), dev_net(dev))) {
  1494. secpath_reset(skb);
  1495. nf_reset(skb);
  1496. skb_init_secmark(skb);
  1497. skb->mark = 0;
  1498. skb->priority = 0;
  1499. skb->nf_trace = 0;
  1500. skb->ipvs_property = 0;
  1501. #ifdef CONFIG_NET_SCHED
  1502. skb->tc_index = 0;
  1503. #endif
  1504. }
  1505. skb->dev = dev;
  1506. }
  1507. EXPORT_SYMBOL(skb_set_dev);
  1508. #endif /* CONFIG_NET_NS */
  1509. /*
  1510. * Invalidate hardware checksum when packet is to be mangled, and
  1511. * complete checksum manually on outgoing path.
  1512. */
  1513. int skb_checksum_help(struct sk_buff *skb)
  1514. {
  1515. __wsum csum;
  1516. int ret = 0, offset;
  1517. if (skb->ip_summed == CHECKSUM_COMPLETE)
  1518. goto out_set_summed;
  1519. if (unlikely(skb_shinfo(skb)->gso_size)) {
  1520. /* Let GSO fix up the checksum. */
  1521. goto out_set_summed;
  1522. }
  1523. offset = skb->csum_start - skb_headroom(skb);
  1524. BUG_ON(offset >= skb_headlen(skb));
  1525. csum = skb_checksum(skb, offset, skb->len - offset, 0);
  1526. offset += skb->csum_offset;
  1527. BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
  1528. if (skb_cloned(skb) &&
  1529. !skb_clone_writable(skb, offset + sizeof(__sum16))) {
  1530. ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  1531. if (ret)
  1532. goto out;
  1533. }
  1534. *(__sum16 *)(skb->data + offset) = csum_fold(csum);
  1535. out_set_summed:
  1536. skb->ip_summed = CHECKSUM_NONE;
  1537. out:
  1538. return ret;
  1539. }
  1540. EXPORT_SYMBOL(skb_checksum_help);
  1541. /**
  1542. * skb_gso_segment - Perform segmentation on skb.
  1543. * @skb: buffer to segment
  1544. * @features: features for the output path (see dev->features)
  1545. *
  1546. * This function segments the given skb and returns a list of segments.
  1547. *
  1548. * It may return NULL if the skb requires no segmentation. This is
  1549. * only possible when GSO is used for verifying header integrity.
  1550. */
  1551. struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
  1552. {
  1553. struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
  1554. struct packet_type *ptype;
  1555. __be16 type = skb->protocol;
  1556. int vlan_depth = ETH_HLEN;
  1557. int err;
  1558. while (type == htons(ETH_P_8021Q)) {
  1559. struct vlan_hdr *vh;
  1560. if (unlikely(!pskb_may_pull(skb, vlan_depth + VLAN_HLEN)))
  1561. return ERR_PTR(-EINVAL);
  1562. vh = (struct vlan_hdr *)(skb->data + vlan_depth);
  1563. type = vh->h_vlan_encapsulated_proto;
  1564. vlan_depth += VLAN_HLEN;
  1565. }
  1566. skb_reset_mac_header(skb);
  1567. skb->mac_len = skb->network_header - skb->mac_header;
  1568. __skb_pull(skb, skb->mac_len);
  1569. if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
  1570. struct net_device *dev = skb->dev;
  1571. struct ethtool_drvinfo info = {};
  1572. if (dev && dev->ethtool_ops && dev->ethtool_ops->get_drvinfo)
  1573. dev->ethtool_ops->get_drvinfo(dev, &info);
  1574. WARN(1, "%s: caps=(0x%lx, 0x%lx) len=%d data_len=%d ip_summed=%d\n",
  1575. info.driver, dev ? dev->features : 0L,
  1576. skb->sk ? skb->sk->sk_route_caps : 0L,
  1577. skb->len, skb->data_len, skb->ip_summed);
  1578. if (skb_header_cloned(skb) &&
  1579. (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  1580. return ERR_PTR(err);
  1581. }
  1582. rcu_read_lock();
  1583. list_for_each_entry_rcu(ptype,
  1584. &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
  1585. if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
  1586. if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
  1587. err = ptype->gso_send_check(skb);
  1588. segs = ERR_PTR(err);
  1589. if (err || skb_gso_ok(skb, features))
  1590. break;
  1591. __skb_push(skb, (skb->data -
  1592. skb_network_header(skb)));
  1593. }
  1594. segs = ptype->gso_segment(skb, features);
  1595. break;
  1596. }
  1597. }
  1598. rcu_read_unlock();
  1599. __skb_push(skb, skb->data - skb_mac_header(skb));
  1600. return segs;
  1601. }
  1602. EXPORT_SYMBOL(skb_gso_segment);
  1603. /* Take action when hardware reception checksum errors are detected. */
  1604. #ifdef CONFIG_BUG
  1605. void netdev_rx_csum_fault(struct net_device *dev)
  1606. {
  1607. if (net_ratelimit()) {
  1608. printk(KERN_ERR "%s: hw csum failure.\n",
  1609. dev ? dev->name : "<unknown>");
  1610. dump_stack();
  1611. }
  1612. }
  1613. EXPORT_SYMBOL(netdev_rx_csum_fault);
  1614. #endif
  1615. /* Actually, we should eliminate this check as soon as we know, that:
  1616. * 1. IOMMU is present and allows to map all the memory.
  1617. * 2. No high memory really exists on this machine.
  1618. */
  1619. static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
  1620. {
  1621. #ifdef CONFIG_HIGHMEM
  1622. int i;
  1623. if (!(dev->features & NETIF_F_HIGHDMA)) {
  1624. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1625. if (PageHighMem(skb_shinfo(skb)->frags[i].page))
  1626. return 1;
  1627. }
  1628. if (PCI_DMA_BUS_IS_PHYS) {
  1629. struct device *pdev = dev->dev.parent;
  1630. if (!pdev)
  1631. return 0;
  1632. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1633. dma_addr_t addr = page_to_phys(skb_shinfo(skb)->frags[i].page);
  1634. if (!pdev->dma_mask || addr + PAGE_SIZE - 1 > *pdev->dma_mask)
  1635. return 1;
  1636. }
  1637. }
  1638. #endif
  1639. return 0;
  1640. }
  1641. struct dev_gso_cb {
  1642. void (*destructor)(struct sk_buff *skb);
  1643. };
  1644. #define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
  1645. static void dev_gso_skb_destructor(struct sk_buff *skb)
  1646. {
  1647. struct dev_gso_cb *cb;
  1648. do {
  1649. struct sk_buff *nskb = skb->next;
  1650. skb->next = nskb->next;
  1651. nskb->next = NULL;
  1652. kfree_skb(nskb);
  1653. } while (skb->next);
  1654. cb = DEV_GSO_CB(skb);
  1655. if (cb->destructor)
  1656. cb->destructor(skb);
  1657. }
  1658. /**
  1659. * dev_gso_segment - Perform emulated hardware segmentation on skb.
  1660. * @skb: buffer to segment
  1661. *
  1662. * This function segments the given skb and stores the list of segments
  1663. * in skb->next.
  1664. */
  1665. static int dev_gso_segment(struct sk_buff *skb)
  1666. {
  1667. struct net_device *dev = skb->dev;
  1668. struct sk_buff *segs;
  1669. int features = dev->features & ~(illegal_highdma(dev, skb) ?
  1670. NETIF_F_SG : 0);
  1671. segs = skb_gso_segment(skb, features);
  1672. /* Verifying header integrity only. */
  1673. if (!segs)
  1674. return 0;
  1675. if (IS_ERR(segs))
  1676. return PTR_ERR(segs);
  1677. skb->next = segs;
  1678. DEV_GSO_CB(skb)->destructor = skb->destructor;
  1679. skb->destructor = dev_gso_skb_destructor;
  1680. return 0;
  1681. }
  1682. /*
  1683. * Try to orphan skb early, right before transmission by the device.
  1684. * We cannot orphan skb if tx timestamp is requested or the sk-reference
  1685. * is needed on driver level for other reasons, e.g. see net/can/raw.c
  1686. */
  1687. static inline void skb_orphan_try(struct sk_buff *skb)
  1688. {
  1689. struct sock *sk = skb->sk;
  1690. if (sk && !skb_shinfo(skb)->tx_flags) {
  1691. /* skb_tx_hash() wont be able to get sk.
  1692. * We copy sk_hash into skb->rxhash
  1693. */
  1694. if (!skb->rxhash)
  1695. skb->rxhash = sk->sk_hash;
  1696. skb_orphan(skb);
  1697. }
  1698. }
  1699. int netif_get_vlan_features(struct sk_buff *skb, struct net_device *dev)
  1700. {
  1701. __be16 protocol = skb->protocol;
  1702. if (protocol == htons(ETH_P_8021Q)) {
  1703. struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
  1704. protocol = veh->h_vlan_encapsulated_proto;
  1705. } else if (!skb->vlan_tci)
  1706. return dev->features;
  1707. if (protocol != htons(ETH_P_8021Q))
  1708. return dev->features & dev->vlan_features;
  1709. else
  1710. return 0;
  1711. }
  1712. EXPORT_SYMBOL(netif_get_vlan_features);
  1713. /*
  1714. * Returns true if either:
  1715. * 1. skb has frag_list and the device doesn't support FRAGLIST, or
  1716. * 2. skb is fragmented and the device does not support SG, or if
  1717. * at least one of fragments is in highmem and device does not
  1718. * support DMA from it.
  1719. */
  1720. static inline int skb_needs_linearize(struct sk_buff *skb,
  1721. struct net_device *dev)
  1722. {
  1723. if (skb_is_nonlinear(skb)) {
  1724. int features = dev->features;
  1725. if (vlan_tx_tag_present(skb))
  1726. features &= dev->vlan_features;
  1727. return (skb_has_frag_list(skb) &&
  1728. !(features & NETIF_F_FRAGLIST)) ||
  1729. (skb_shinfo(skb)->nr_frags &&
  1730. (!(features & NETIF_F_SG) ||
  1731. illegal_highdma(dev, skb)));
  1732. }
  1733. return 0;
  1734. }
  1735. int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
  1736. struct netdev_queue *txq)
  1737. {
  1738. const struct net_device_ops *ops = dev->netdev_ops;
  1739. int rc = NETDEV_TX_OK;
  1740. if (likely(!skb->next)) {
  1741. /*
  1742. * If device doesnt need skb->dst, release it right now while
  1743. * its hot in this cpu cache
  1744. */
  1745. if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
  1746. skb_dst_drop(skb);
  1747. if (!list_empty(&ptype_all))
  1748. dev_queue_xmit_nit(skb, dev);
  1749. skb_orphan_try(skb);
  1750. if (vlan_tx_tag_present(skb) &&
  1751. !(dev->features & NETIF_F_HW_VLAN_TX)) {
  1752. skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
  1753. if (unlikely(!skb))
  1754. goto out;
  1755. skb->vlan_tci = 0;
  1756. }
  1757. if (netif_needs_gso(dev, skb)) {
  1758. if (unlikely(dev_gso_segment(skb)))
  1759. goto out_kfree_skb;
  1760. if (skb->next)
  1761. goto gso;
  1762. } else {
  1763. if (skb_needs_linearize(skb, dev) &&
  1764. __skb_linearize(skb))
  1765. goto out_kfree_skb;
  1766. /* If packet is not checksummed and device does not
  1767. * support checksumming for this protocol, complete
  1768. * checksumming here.
  1769. */
  1770. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1771. skb_set_transport_header(skb, skb->csum_start -
  1772. skb_headroom(skb));
  1773. if (!dev_can_checksum(dev, skb) &&
  1774. skb_checksum_help(skb))
  1775. goto out_kfree_skb;
  1776. }
  1777. }
  1778. rc = ops->ndo_start_xmit(skb, dev);
  1779. trace_net_dev_xmit(skb, rc);
  1780. if (rc == NETDEV_TX_OK)
  1781. txq_trans_update(txq);
  1782. return rc;
  1783. }
  1784. gso:
  1785. do {
  1786. struct sk_buff *nskb = skb->next;
  1787. skb->next = nskb->next;
  1788. nskb->next = NULL;
  1789. /*
  1790. * If device doesnt need nskb->dst, release it right now while
  1791. * its hot in this cpu cache
  1792. */
  1793. if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
  1794. skb_dst_drop(nskb);
  1795. rc = ops->ndo_start_xmit(nskb, dev);
  1796. trace_net_dev_xmit(nskb, rc);
  1797. if (unlikely(rc != NETDEV_TX_OK)) {
  1798. if (rc & ~NETDEV_TX_MASK)
  1799. goto out_kfree_gso_skb;
  1800. nskb->next = skb->next;
  1801. skb->next = nskb;
  1802. return rc;
  1803. }
  1804. txq_trans_update(txq);
  1805. if (unlikely(netif_tx_queue_stopped(txq) && skb->next))
  1806. return NETDEV_TX_BUSY;
  1807. } while (skb->next);
  1808. out_kfree_gso_skb:
  1809. if (likely(skb->next == NULL))
  1810. skb->destructor = DEV_GSO_CB(skb)->destructor;
  1811. out_kfree_skb:
  1812. kfree_skb(skb);
  1813. out:
  1814. return rc;
  1815. }
  1816. static u32 hashrnd __read_mostly;
  1817. /*
  1818. * Returns a Tx hash based on the given packet descriptor a Tx queues' number
  1819. * to be used as a distribution range.
  1820. */
  1821. u16 __skb_tx_hash(const struct net_device *dev, const struct sk_buff *skb,
  1822. unsigned int num_tx_queues)
  1823. {
  1824. u32 hash;
  1825. if (skb_rx_queue_recorded(skb)) {
  1826. hash = skb_get_rx_queue(skb);
  1827. while (unlikely(hash >= num_tx_queues))
  1828. hash -= num_tx_queues;
  1829. return hash;
  1830. }
  1831. if (skb->sk && skb->sk->sk_hash)
  1832. hash = skb->sk->sk_hash;
  1833. else
  1834. hash = (__force u16) skb->protocol ^ skb->rxhash;
  1835. hash = jhash_1word(hash, hashrnd);
  1836. return (u16) (((u64) hash * num_tx_queues) >> 32);
  1837. }
  1838. EXPORT_SYMBOL(__skb_tx_hash);
  1839. static inline u16 dev_cap_txqueue(struct net_device *dev, u16 queue_index)
  1840. {
  1841. if (unlikely(queue_index >= dev->real_num_tx_queues)) {
  1842. if (net_ratelimit()) {
  1843. pr_warning("%s selects TX queue %d, but "
  1844. "real number of TX queues is %d\n",
  1845. dev->name, queue_index, dev->real_num_tx_queues);
  1846. }
  1847. return 0;
  1848. }
  1849. return queue_index;
  1850. }
  1851. static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
  1852. {
  1853. #ifdef CONFIG_XPS
  1854. struct xps_dev_maps *dev_maps;
  1855. struct xps_map *map;
  1856. int queue_index = -1;
  1857. rcu_read_lock();
  1858. dev_maps = rcu_dereference(dev->xps_maps);
  1859. if (dev_maps) {
  1860. map = rcu_dereference(
  1861. dev_maps->cpu_map[raw_smp_processor_id()]);
  1862. if (map) {
  1863. if (map->len == 1)
  1864. queue_index = map->queues[0];
  1865. else {
  1866. u32 hash;
  1867. if (skb->sk && skb->sk->sk_hash)
  1868. hash = skb->sk->sk_hash;
  1869. else
  1870. hash = (__force u16) skb->protocol ^
  1871. skb->rxhash;
  1872. hash = jhash_1word(hash, hashrnd);
  1873. queue_index = map->queues[
  1874. ((u64)hash * map->len) >> 32];
  1875. }
  1876. if (unlikely(queue_index >= dev->real_num_tx_queues))
  1877. queue_index = -1;
  1878. }
  1879. }
  1880. rcu_read_unlock();
  1881. return queue_index;
  1882. #else
  1883. return -1;
  1884. #endif
  1885. }
  1886. static struct netdev_queue *dev_pick_tx(struct net_device *dev,
  1887. struct sk_buff *skb)
  1888. {
  1889. int queue_index;
  1890. const struct net_device_ops *ops = dev->netdev_ops;
  1891. if (dev->real_num_tx_queues == 1)
  1892. queue_index = 0;
  1893. else if (ops->ndo_select_queue) {
  1894. queue_index = ops->ndo_select_queue(dev, skb);
  1895. queue_index = dev_cap_txqueue(dev, queue_index);
  1896. } else {
  1897. struct sock *sk = skb->sk;
  1898. queue_index = sk_tx_queue_get(sk);
  1899. if (queue_index < 0 || skb->ooo_okay ||
  1900. queue_index >= dev->real_num_tx_queues) {
  1901. int old_index = queue_index;
  1902. queue_index = get_xps_queue(dev, skb);
  1903. if (queue_index < 0)
  1904. queue_index = skb_tx_hash(dev, skb);
  1905. if (queue_index != old_index && sk) {
  1906. struct dst_entry *dst =
  1907. rcu_dereference_check(sk->sk_dst_cache, 1);
  1908. if (dst && skb_dst(skb) == dst)
  1909. sk_tx_queue_set(sk, queue_index);
  1910. }
  1911. }
  1912. }
  1913. skb_set_queue_mapping(skb, queue_index);
  1914. return netdev_get_tx_queue(dev, queue_index);
  1915. }
  1916. static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
  1917. struct net_device *dev,
  1918. struct netdev_queue *txq)
  1919. {
  1920. spinlock_t *root_lock = qdisc_lock(q);
  1921. bool contended = qdisc_is_running(q);
  1922. int rc;
  1923. /*
  1924. * Heuristic to force contended enqueues to serialize on a
  1925. * separate lock before trying to get qdisc main lock.
  1926. * This permits __QDISC_STATE_RUNNING owner to get the lock more often
  1927. * and dequeue packets faster.
  1928. */
  1929. if (unlikely(contended))
  1930. spin_lock(&q->busylock);
  1931. spin_lock(root_lock);
  1932. if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
  1933. kfree_skb(skb);
  1934. rc = NET_XMIT_DROP;
  1935. } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
  1936. qdisc_run_begin(q)) {
  1937. /*
  1938. * This is a work-conserving queue; there are no old skbs
  1939. * waiting to be sent out; and the qdisc is not running -
  1940. * xmit the skb directly.
  1941. */
  1942. if (!(dev->priv_flags & IFF_XMIT_DST_RELEASE))
  1943. skb_dst_force(skb);
  1944. __qdisc_update_bstats(q, skb->len);
  1945. if (sch_direct_xmit(skb, q, dev, txq, root_lock)) {
  1946. if (unlikely(contended)) {
  1947. spin_unlock(&q->busylock);
  1948. contended = false;
  1949. }
  1950. __qdisc_run(q);
  1951. } else
  1952. qdisc_run_end(q);
  1953. rc = NET_XMIT_SUCCESS;
  1954. } else {
  1955. skb_dst_force(skb);
  1956. rc = qdisc_enqueue_root(skb, q);
  1957. if (qdisc_run_begin(q)) {
  1958. if (unlikely(contended)) {
  1959. spin_unlock(&q->busylock);
  1960. contended = false;
  1961. }
  1962. __qdisc_run(q);
  1963. }
  1964. }
  1965. spin_unlock(root_lock);
  1966. if (unlikely(contended))
  1967. spin_unlock(&q->busylock);
  1968. return rc;
  1969. }
  1970. static DEFINE_PER_CPU(int, xmit_recursion);
  1971. #define RECURSION_LIMIT 10
  1972. /**
  1973. * dev_queue_xmit - transmit a buffer
  1974. * @skb: buffer to transmit
  1975. *
  1976. * Queue a buffer for transmission to a network device. The caller must
  1977. * have set the device and priority and built the buffer before calling
  1978. * this function. The function can be called from an interrupt.
  1979. *
  1980. * A negative errno code is returned on a failure. A success does not
  1981. * guarantee the frame will be transmitted as it may be dropped due
  1982. * to congestion or traffic shaping.
  1983. *
  1984. * -----------------------------------------------------------------------------------
  1985. * I notice this method can also return errors from the queue disciplines,
  1986. * including NET_XMIT_DROP, which is a positive value. So, errors can also
  1987. * be positive.
  1988. *
  1989. * Regardless of the return value, the skb is consumed, so it is currently
  1990. * difficult to retry a send to this method. (You can bump the ref count
  1991. * before sending to hold a reference for retry if you are careful.)
  1992. *
  1993. * When calling this method, interrupts MUST be enabled. This is because
  1994. * the BH enable code must have IRQs enabled so that it will not deadlock.
  1995. * --BLG
  1996. */
  1997. int dev_queue_xmit(struct sk_buff *skb)
  1998. {
  1999. struct net_device *dev = skb->dev;
  2000. struct netdev_queue *txq;
  2001. struct Qdisc *q;
  2002. int rc = -ENOMEM;
  2003. /* Disable soft irqs for various locks below. Also
  2004. * stops preemption for RCU.
  2005. */
  2006. rcu_read_lock_bh();
  2007. txq = dev_pick_tx(dev, skb);
  2008. q = rcu_dereference_bh(txq->qdisc);
  2009. #ifdef CONFIG_NET_CLS_ACT
  2010. skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_EGRESS);
  2011. #endif
  2012. trace_net_dev_queue(skb);
  2013. if (q->enqueue) {
  2014. rc = __dev_xmit_skb(skb, q, dev, txq);
  2015. goto out;
  2016. }
  2017. /* The device has no queue. Common case for software devices:
  2018. loopback, all the sorts of tunnels...
  2019. Really, it is unlikely that netif_tx_lock protection is necessary
  2020. here. (f.e. loopback and IP tunnels are clean ignoring statistics
  2021. counters.)
  2022. However, it is possible, that they rely on protection
  2023. made by us here.
  2024. Check this and shot the lock. It is not prone from deadlocks.
  2025. Either shot noqueue qdisc, it is even simpler 8)
  2026. */
  2027. if (dev->flags & IFF_UP) {
  2028. int cpu = smp_processor_id(); /* ok because BHs are off */
  2029. if (txq->xmit_lock_owner != cpu) {
  2030. if (__this_cpu_read(xmit_recursion) > RECURSION_LIMIT)
  2031. goto recursion_alert;
  2032. HARD_TX_LOCK(dev, txq, cpu);
  2033. if (!netif_tx_queue_stopped(txq)) {
  2034. __this_cpu_inc(xmit_recursion);
  2035. rc = dev_hard_start_xmit(skb, dev, txq);
  2036. __this_cpu_dec(xmit_recursion);
  2037. if (dev_xmit_complete(rc)) {
  2038. HARD_TX_UNLOCK(dev, txq);
  2039. goto out;
  2040. }
  2041. }
  2042. HARD_TX_UNLOCK(dev, txq);
  2043. if (net_ratelimit())
  2044. printk(KERN_CRIT "Virtual device %s asks to "
  2045. "queue packet!\n", dev->name);
  2046. } else {
  2047. /* Recursion is detected! It is possible,
  2048. * unfortunately
  2049. */
  2050. recursion_alert:
  2051. if (net_ratelimit())
  2052. printk(KERN_CRIT "Dead loop on virtual device "
  2053. "%s, fix it urgently!\n", dev->name);
  2054. }
  2055. }
  2056. rc = -ENETDOWN;
  2057. rcu_read_unlock_bh();
  2058. kfree_skb(skb);
  2059. return rc;
  2060. out:
  2061. rcu_read_unlock_bh();
  2062. return rc;
  2063. }
  2064. EXPORT_SYMBOL(dev_queue_xmit);
  2065. /*=======================================================================
  2066. Receiver routines
  2067. =======================================================================*/
  2068. int netdev_max_backlog __read_mostly = 1000;
  2069. int netdev_tstamp_prequeue __read_mostly = 1;
  2070. int netdev_budget __read_mostly = 300;
  2071. int weight_p __read_mostly = 64; /* old backlog weight */
  2072. /* Called with irq disabled */
  2073. static inline void ____napi_schedule(struct softnet_data *sd,
  2074. struct napi_struct *napi)
  2075. {
  2076. list_add_tail(&napi->poll_list, &sd->poll_list);
  2077. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  2078. }
  2079. /*
  2080. * __skb_get_rxhash: calculate a flow hash based on src/dst addresses
  2081. * and src/dst port numbers. Returns a non-zero hash number on success
  2082. * and 0 on failure.
  2083. */
  2084. __u32 __skb_get_rxhash(struct sk_buff *skb)
  2085. {
  2086. int nhoff, hash = 0, poff;
  2087. struct ipv6hdr *ip6;
  2088. struct iphdr *ip;
  2089. u8 ip_proto;
  2090. u32 addr1, addr2, ihl;
  2091. union {
  2092. u32 v32;
  2093. u16 v16[2];
  2094. } ports;
  2095. nhoff = skb_network_offset(skb);
  2096. switch (skb->protocol) {
  2097. case __constant_htons(ETH_P_IP):
  2098. if (!pskb_may_pull(skb, sizeof(*ip) + nhoff))
  2099. goto done;
  2100. ip = (struct iphdr *) (skb->data + nhoff);
  2101. if (ip->frag_off & htons(IP_MF | IP_OFFSET))
  2102. ip_proto = 0;
  2103. else
  2104. ip_proto = ip->protocol;
  2105. addr1 = (__force u32) ip->saddr;
  2106. addr2 = (__force u32) ip->daddr;
  2107. ihl = ip->ihl;
  2108. break;
  2109. case __constant_htons(ETH_P_IPV6):
  2110. if (!pskb_may_pull(skb, sizeof(*ip6) + nhoff))
  2111. goto done;
  2112. ip6 = (struct ipv6hdr *) (skb->data + nhoff);
  2113. ip_proto = ip6->nexthdr;
  2114. addr1 = (__force u32) ip6->saddr.s6_addr32[3];
  2115. addr2 = (__force u32) ip6->daddr.s6_addr32[3];
  2116. ihl = (40 >> 2);
  2117. break;
  2118. default:
  2119. goto done;
  2120. }
  2121. ports.v32 = 0;
  2122. poff = proto_ports_offset(ip_proto);
  2123. if (poff >= 0) {
  2124. nhoff += ihl * 4 + poff;
  2125. if (pskb_may_pull(skb, nhoff + 4)) {
  2126. ports.v32 = * (__force u32 *) (skb->data + nhoff);
  2127. if (ports.v16[1] < ports.v16[0])
  2128. swap(ports.v16[0], ports.v16[1]);
  2129. }
  2130. }
  2131. /* get a consistent hash (same value on both flow directions) */
  2132. if (addr2 < addr1)
  2133. swap(addr1, addr2);
  2134. hash = jhash_3words(addr1, addr2, ports.v32, hashrnd);
  2135. if (!hash)
  2136. hash = 1;
  2137. done:
  2138. return hash;
  2139. }
  2140. EXPORT_SYMBOL(__skb_get_rxhash);
  2141. #ifdef CONFIG_RPS
  2142. /* One global table that all flow-based protocols share. */
  2143. struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
  2144. EXPORT_SYMBOL(rps_sock_flow_table);
  2145. /*
  2146. * get_rps_cpu is called from netif_receive_skb and returns the target
  2147. * CPU from the RPS map of the receiving queue for a given skb.
  2148. * rcu_read_lock must be held on entry.
  2149. */
  2150. static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
  2151. struct rps_dev_flow **rflowp)
  2152. {
  2153. struct netdev_rx_queue *rxqueue;
  2154. struct rps_map *map;
  2155. struct rps_dev_flow_table *flow_table;
  2156. struct rps_sock_flow_table *sock_flow_table;
  2157. int cpu = -1;
  2158. u16 tcpu;
  2159. if (skb_rx_queue_recorded(skb)) {
  2160. u16 index = skb_get_rx_queue(skb);
  2161. if (unlikely(index >= dev->real_num_rx_queues)) {
  2162. WARN_ONCE(dev->real_num_rx_queues > 1,
  2163. "%s received packet on queue %u, but number "
  2164. "of RX queues is %u\n",
  2165. dev->name, index, dev->real_num_rx_queues);
  2166. goto done;
  2167. }
  2168. rxqueue = dev->_rx + index;
  2169. } else
  2170. rxqueue = dev->_rx;
  2171. map = rcu_dereference(rxqueue->rps_map);
  2172. if (map) {
  2173. if (map->len == 1) {
  2174. tcpu = map->cpus[0];
  2175. if (cpu_online(tcpu))
  2176. cpu = tcpu;
  2177. goto done;
  2178. }
  2179. } else if (!rcu_dereference_raw(rxqueue->rps_flow_table)) {
  2180. goto done;
  2181. }
  2182. skb_reset_network_header(skb);
  2183. if (!skb_get_rxhash(skb))
  2184. goto done;
  2185. flow_table = rcu_dereference(rxqueue->rps_flow_table);
  2186. sock_flow_table = rcu_dereference(rps_sock_flow_table);
  2187. if (flow_table && sock_flow_table) {
  2188. u16 next_cpu;
  2189. struct rps_dev_flow *rflow;
  2190. rflow = &flow_table->flows[skb->rxhash & flow_table->mask];
  2191. tcpu = rflow->cpu;
  2192. next_cpu = sock_flow_table->ents[skb->rxhash &
  2193. sock_flow_table->mask];
  2194. /*
  2195. * If the desired CPU (where last recvmsg was done) is
  2196. * different from current CPU (one in the rx-queue flow
  2197. * table entry), switch if one of the following holds:
  2198. * - Current CPU is unset (equal to RPS_NO_CPU).
  2199. * - Current CPU is offline.
  2200. * - The current CPU's queue tail has advanced beyond the
  2201. * last packet that was enqueued using this table entry.
  2202. * This guarantees that all previous packets for the flow
  2203. * have been dequeued, thus preserving in order delivery.
  2204. */
  2205. if (unlikely(tcpu != next_cpu) &&
  2206. (tcpu == RPS_NO_CPU || !cpu_online(tcpu) ||
  2207. ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
  2208. rflow->last_qtail)) >= 0)) {
  2209. tcpu = rflow->cpu = next_cpu;
  2210. if (tcpu != RPS_NO_CPU)
  2211. rflow->last_qtail = per_cpu(softnet_data,
  2212. tcpu).input_queue_head;
  2213. }
  2214. if (tcpu != RPS_NO_CPU && cpu_online(tcpu)) {
  2215. *rflowp = rflow;
  2216. cpu = tcpu;
  2217. goto done;
  2218. }
  2219. }
  2220. if (map) {
  2221. tcpu = map->cpus[((u64) skb->rxhash * map->len) >> 32];
  2222. if (cpu_online(tcpu)) {
  2223. cpu = tcpu;
  2224. goto done;
  2225. }
  2226. }
  2227. done:
  2228. return cpu;
  2229. }
  2230. /* Called from hardirq (IPI) context */
  2231. static void rps_trigger_softirq(void *data)
  2232. {
  2233. struct softnet_data *sd = data;
  2234. ____napi_schedule(sd, &sd->backlog);
  2235. sd->received_rps++;
  2236. }
  2237. #endif /* CONFIG_RPS */
  2238. /*
  2239. * Check if this softnet_data structure is another cpu one
  2240. * If yes, queue it to our IPI list and return 1
  2241. * If no, return 0
  2242. */
  2243. static int rps_ipi_queued(struct softnet_data *sd)
  2244. {
  2245. #ifdef CONFIG_RPS
  2246. struct softnet_data *mysd = &__get_cpu_var(softnet_data);
  2247. if (sd != mysd) {
  2248. sd->rps_ipi_next = mysd->rps_ipi_list;
  2249. mysd->rps_ipi_list = sd;
  2250. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  2251. return 1;
  2252. }
  2253. #endif /* CONFIG_RPS */
  2254. return 0;
  2255. }
  2256. /*
  2257. * enqueue_to_backlog is called to queue an skb to a per CPU backlog
  2258. * queue (may be a remote CPU queue).
  2259. */
  2260. static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
  2261. unsigned int *qtail)
  2262. {
  2263. struct softnet_data *sd;
  2264. unsigned long flags;
  2265. sd = &per_cpu(softnet_data, cpu);
  2266. local_irq_save(flags);
  2267. rps_lock(sd);
  2268. if (skb_queue_len(&sd->input_pkt_queue) <= netdev_max_backlog) {
  2269. if (skb_queue_len(&sd->input_pkt_queue)) {
  2270. enqueue:
  2271. __skb_queue_tail(&sd->input_pkt_queue, skb);
  2272. input_queue_tail_incr_save(sd, qtail);
  2273. rps_unlock(sd);
  2274. local_irq_restore(flags);
  2275. return NET_RX_SUCCESS;
  2276. }
  2277. /* Schedule NAPI for backlog device
  2278. * We can use non atomic operation since we own the queue lock
  2279. */
  2280. if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
  2281. if (!rps_ipi_queued(sd))
  2282. ____napi_schedule(sd, &sd->backlog);
  2283. }
  2284. goto enqueue;
  2285. }
  2286. sd->dropped++;
  2287. rps_unlock(sd);
  2288. local_irq_restore(flags);
  2289. atomic_long_inc(&skb->dev->rx_dropped);
  2290. kfree_skb(skb);
  2291. return NET_RX_DROP;
  2292. }
  2293. /**
  2294. * netif_rx - post buffer to the network code
  2295. * @skb: buffer to post
  2296. *
  2297. * This function receives a packet from a device driver and queues it for
  2298. * the upper (protocol) levels to process. It always succeeds. The buffer
  2299. * may be dropped during processing for congestion control or by the
  2300. * protocol layers.
  2301. *
  2302. * return values:
  2303. * NET_RX_SUCCESS (no congestion)
  2304. * NET_RX_DROP (packet was dropped)
  2305. *
  2306. */
  2307. int netif_rx(struct sk_buff *skb)
  2308. {
  2309. int ret;
  2310. /* if netpoll wants it, pretend we never saw it */
  2311. if (netpoll_rx(skb))
  2312. return NET_RX_DROP;
  2313. if (netdev_tstamp_prequeue)
  2314. net_timestamp_check(skb);
  2315. trace_netif_rx(skb);
  2316. #ifdef CONFIG_RPS
  2317. {
  2318. struct rps_dev_flow voidflow, *rflow = &voidflow;
  2319. int cpu;
  2320. preempt_disable();
  2321. rcu_read_lock();
  2322. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  2323. if (cpu < 0)
  2324. cpu = smp_processor_id();
  2325. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  2326. rcu_read_unlock();
  2327. preempt_enable();
  2328. }
  2329. #else
  2330. {
  2331. unsigned int qtail;
  2332. ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
  2333. put_cpu();
  2334. }
  2335. #endif
  2336. return ret;
  2337. }
  2338. EXPORT_SYMBOL(netif_rx);
  2339. int netif_rx_ni(struct sk_buff *skb)
  2340. {
  2341. int err;
  2342. preempt_disable();
  2343. err = netif_rx(skb);
  2344. if (local_softirq_pending())
  2345. do_softirq();
  2346. preempt_enable();
  2347. return err;
  2348. }
  2349. EXPORT_SYMBOL(netif_rx_ni);
  2350. static void net_tx_action(struct softirq_action *h)
  2351. {
  2352. struct softnet_data *sd = &__get_cpu_var(softnet_data);
  2353. if (sd->completion_queue) {
  2354. struct sk_buff *clist;
  2355. local_irq_disable();
  2356. clist = sd->completion_queue;
  2357. sd->completion_queue = NULL;
  2358. local_irq_enable();
  2359. while (clist) {
  2360. struct sk_buff *skb = clist;
  2361. clist = clist->next;
  2362. WARN_ON(atomic_read(&skb->users));
  2363. trace_kfree_skb(skb, net_tx_action);
  2364. __kfree_skb(skb);
  2365. }
  2366. }
  2367. if (sd->output_queue) {
  2368. struct Qdisc *head;
  2369. local_irq_disable();
  2370. head = sd->output_queue;
  2371. sd->output_queue = NULL;
  2372. sd->output_queue_tailp = &sd->output_queue;
  2373. local_irq_enable();
  2374. while (head) {
  2375. struct Qdisc *q = head;
  2376. spinlock_t *root_lock;
  2377. head = head->next_sched;
  2378. root_lock = qdisc_lock(q);
  2379. if (spin_trylock(root_lock)) {
  2380. smp_mb__before_clear_bit();
  2381. clear_bit(__QDISC_STATE_SCHED,
  2382. &q->state);
  2383. qdisc_run(q);
  2384. spin_unlock(root_lock);
  2385. } else {
  2386. if (!test_bit(__QDISC_STATE_DEACTIVATED,
  2387. &q->state)) {
  2388. __netif_reschedule(q);
  2389. } else {
  2390. smp_mb__before_clear_bit();
  2391. clear_bit(__QDISC_STATE_SCHED,
  2392. &q->state);
  2393. }
  2394. }
  2395. }
  2396. }
  2397. }
  2398. static inline int deliver_skb(struct sk_buff *skb,
  2399. struct packet_type *pt_prev,
  2400. struct net_device *orig_dev)
  2401. {
  2402. atomic_inc(&skb->users);
  2403. return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
  2404. }
  2405. #if (defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)) && \
  2406. (defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE))
  2407. /* This hook is defined here for ATM LANE */
  2408. int (*br_fdb_test_addr_hook)(struct net_device *dev,
  2409. unsigned char *addr) __read_mostly;
  2410. EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
  2411. #endif
  2412. #ifdef CONFIG_NET_CLS_ACT
  2413. /* TODO: Maybe we should just force sch_ingress to be compiled in
  2414. * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
  2415. * a compare and 2 stores extra right now if we dont have it on
  2416. * but have CONFIG_NET_CLS_ACT
  2417. * NOTE: This doesnt stop any functionality; if you dont have
  2418. * the ingress scheduler, you just cant add policies on ingress.
  2419. *
  2420. */
  2421. static int ing_filter(struct sk_buff *skb, struct netdev_queue *rxq)
  2422. {
  2423. struct net_device *dev = skb->dev;
  2424. u32 ttl = G_TC_RTTL(skb->tc_verd);
  2425. int result = TC_ACT_OK;
  2426. struct Qdisc *q;
  2427. if (unlikely(MAX_RED_LOOP < ttl++)) {
  2428. if (net_ratelimit())
  2429. pr_warning( "Redir loop detected Dropping packet (%d->%d)\n",
  2430. skb->skb_iif, dev->ifindex);
  2431. return TC_ACT_SHOT;
  2432. }
  2433. skb->tc_verd = SET_TC_RTTL(skb->tc_verd, ttl);
  2434. skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_INGRESS);
  2435. q = rxq->qdisc;
  2436. if (q != &noop_qdisc) {
  2437. spin_lock(qdisc_lock(q));
  2438. if (likely(!test_bit(__QDISC_STATE_DEACTIVATED, &q->state)))
  2439. result = qdisc_enqueue_root(skb, q);
  2440. spin_unlock(qdisc_lock(q));
  2441. }
  2442. return result;
  2443. }
  2444. static inline struct sk_buff *handle_ing(struct sk_buff *skb,
  2445. struct packet_type **pt_prev,
  2446. int *ret, struct net_device *orig_dev)
  2447. {
  2448. struct netdev_queue *rxq = rcu_dereference(skb->dev->ingress_queue);
  2449. if (!rxq || rxq->qdisc == &noop_qdisc)
  2450. goto out;
  2451. if (*pt_prev) {
  2452. *ret = deliver_skb(skb, *pt_prev, orig_dev);
  2453. *pt_prev = NULL;
  2454. }
  2455. switch (ing_filter(skb, rxq)) {
  2456. case TC_ACT_SHOT:
  2457. case TC_ACT_STOLEN:
  2458. kfree_skb(skb);
  2459. return NULL;
  2460. }
  2461. out:
  2462. skb->tc_verd = 0;
  2463. return skb;
  2464. }
  2465. #endif
  2466. /**
  2467. * netdev_rx_handler_register - register receive handler
  2468. * @dev: device to register a handler for
  2469. * @rx_handler: receive handler to register
  2470. * @rx_handler_data: data pointer that is used by rx handler
  2471. *
  2472. * Register a receive hander for a device. This handler will then be
  2473. * called from __netif_receive_skb. A negative errno code is returned
  2474. * on a failure.
  2475. *
  2476. * The caller must hold the rtnl_mutex.
  2477. */
  2478. int netdev_rx_handler_register(struct net_device *dev,
  2479. rx_handler_func_t *rx_handler,
  2480. void *rx_handler_data)
  2481. {
  2482. ASSERT_RTNL();
  2483. if (dev->rx_handler)
  2484. return -EBUSY;
  2485. rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
  2486. rcu_assign_pointer(dev->rx_handler, rx_handler);
  2487. return 0;
  2488. }
  2489. EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
  2490. /**
  2491. * netdev_rx_handler_unregister - unregister receive handler
  2492. * @dev: device to unregister a handler from
  2493. *
  2494. * Unregister a receive hander from a device.
  2495. *
  2496. * The caller must hold the rtnl_mutex.
  2497. */
  2498. void netdev_rx_handler_unregister(struct net_device *dev)
  2499. {
  2500. ASSERT_RTNL();
  2501. rcu_assign_pointer(dev->rx_handler, NULL);
  2502. rcu_assign_pointer(dev->rx_handler_data, NULL);
  2503. }
  2504. EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
  2505. static inline void skb_bond_set_mac_by_master(struct sk_buff *skb,
  2506. struct net_device *master)
  2507. {
  2508. if (skb->pkt_type == PACKET_HOST) {
  2509. u16 *dest = (u16 *) eth_hdr(skb)->h_dest;
  2510. memcpy(dest, master->dev_addr, ETH_ALEN);
  2511. }
  2512. }
  2513. /* On bonding slaves other than the currently active slave, suppress
  2514. * duplicates except for 802.3ad ETH_P_SLOW, alb non-mcast/bcast, and
  2515. * ARP on active-backup slaves with arp_validate enabled.
  2516. */
  2517. int __skb_bond_should_drop(struct sk_buff *skb, struct net_device *master)
  2518. {
  2519. struct net_device *dev = skb->dev;
  2520. if (master->priv_flags & IFF_MASTER_ARPMON)
  2521. dev->last_rx = jiffies;
  2522. if ((master->priv_flags & IFF_MASTER_ALB) &&
  2523. (master->priv_flags & IFF_BRIDGE_PORT)) {
  2524. /* Do address unmangle. The local destination address
  2525. * will be always the one master has. Provides the right
  2526. * functionality in a bridge.
  2527. */
  2528. skb_bond_set_mac_by_master(skb, master);
  2529. }
  2530. if (dev->priv_flags & IFF_SLAVE_INACTIVE) {
  2531. if ((dev->priv_flags & IFF_SLAVE_NEEDARP) &&
  2532. skb->protocol == __cpu_to_be16(ETH_P_ARP))
  2533. return 0;
  2534. if (master->priv_flags & IFF_MASTER_ALB) {
  2535. if (skb->pkt_type != PACKET_BROADCAST &&
  2536. skb->pkt_type != PACKET_MULTICAST)
  2537. return 0;
  2538. }
  2539. if (master->priv_flags & IFF_MASTER_8023AD &&
  2540. skb->protocol == __cpu_to_be16(ETH_P_SLOW))
  2541. return 0;
  2542. return 1;
  2543. }
  2544. return 0;
  2545. }
  2546. EXPORT_SYMBOL(__skb_bond_should_drop);
  2547. static int __netif_receive_skb(struct sk_buff *skb)
  2548. {
  2549. struct packet_type *ptype, *pt_prev;
  2550. rx_handler_func_t *rx_handler;
  2551. struct net_device *orig_dev;
  2552. struct net_device *master;
  2553. struct net_device *null_or_orig;
  2554. struct net_device *orig_or_bond;
  2555. int ret = NET_RX_DROP;
  2556. __be16 type;
  2557. if (!netdev_tstamp_prequeue)
  2558. net_timestamp_check(skb);
  2559. trace_netif_receive_skb(skb);
  2560. /* if we've gotten here through NAPI, check netpoll */
  2561. if (netpoll_receive_skb(skb))
  2562. return NET_RX_DROP;
  2563. if (!skb->skb_iif)
  2564. skb->skb_iif = skb->dev->ifindex;
  2565. /*
  2566. * bonding note: skbs received on inactive slaves should only
  2567. * be delivered to pkt handlers that are exact matches. Also
  2568. * the deliver_no_wcard flag will be set. If packet handlers
  2569. * are sensitive to duplicate packets these skbs will need to
  2570. * be dropped at the handler.
  2571. */
  2572. null_or_orig = NULL;
  2573. orig_dev = skb->dev;
  2574. master = ACCESS_ONCE(orig_dev->master);
  2575. if (skb->deliver_no_wcard)
  2576. null_or_orig = orig_dev;
  2577. else if (master) {
  2578. if (skb_bond_should_drop(skb, master)) {
  2579. skb->deliver_no_wcard = 1;
  2580. null_or_orig = orig_dev; /* deliver only exact match */
  2581. } else
  2582. skb->dev = master;
  2583. }
  2584. __this_cpu_inc(softnet_data.processed);
  2585. skb_reset_network_header(skb);
  2586. skb_reset_transport_header(skb);
  2587. skb->mac_len = skb->network_header - skb->mac_header;
  2588. pt_prev = NULL;
  2589. rcu_read_lock();
  2590. #ifdef CONFIG_NET_CLS_ACT
  2591. if (skb->tc_verd & TC_NCLS) {
  2592. skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
  2593. goto ncls;
  2594. }
  2595. #endif
  2596. list_for_each_entry_rcu(ptype, &ptype_all, list) {
  2597. if (ptype->dev == null_or_orig || ptype->dev == skb->dev ||
  2598. ptype->dev == orig_dev) {
  2599. if (pt_prev)
  2600. ret = deliver_skb(skb, pt_prev, orig_dev);
  2601. pt_prev = ptype;
  2602. }
  2603. }
  2604. #ifdef CONFIG_NET_CLS_ACT
  2605. skb = handle_ing(skb, &pt_prev, &ret, orig_dev);
  2606. if (!skb)
  2607. goto out;
  2608. ncls:
  2609. #endif
  2610. /* Handle special case of bridge or macvlan */
  2611. rx_handler = rcu_dereference(skb->dev->rx_handler);
  2612. if (rx_handler) {
  2613. if (pt_prev) {
  2614. ret = deliver_skb(skb, pt_prev, orig_dev);
  2615. pt_prev = NULL;
  2616. }
  2617. skb = rx_handler(skb);
  2618. if (!skb)
  2619. goto out;
  2620. }
  2621. if (vlan_tx_tag_present(skb)) {
  2622. if (pt_prev) {
  2623. ret = deliver_skb(skb, pt_prev, orig_dev);
  2624. pt_prev = NULL;
  2625. }
  2626. if (vlan_hwaccel_do_receive(&skb)) {
  2627. ret = __netif_receive_skb(skb);
  2628. goto out;
  2629. } else if (unlikely(!skb))
  2630. goto out;
  2631. }
  2632. /*
  2633. * Make sure frames received on VLAN interfaces stacked on
  2634. * bonding interfaces still make their way to any base bonding
  2635. * device that may have registered for a specific ptype. The
  2636. * handler may have to adjust skb->dev and orig_dev.
  2637. */
  2638. orig_or_bond = orig_dev;
  2639. if ((skb->dev->priv_flags & IFF_802_1Q_VLAN) &&
  2640. (vlan_dev_real_dev(skb->dev)->priv_flags & IFF_BONDING)) {
  2641. orig_or_bond = vlan_dev_real_dev(skb->dev);
  2642. }
  2643. type = skb->protocol;
  2644. list_for_each_entry_rcu(ptype,
  2645. &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
  2646. if (ptype->type == type && (ptype->dev == null_or_orig ||
  2647. ptype->dev == skb->dev || ptype->dev == orig_dev ||
  2648. ptype->dev == orig_or_bond)) {
  2649. if (pt_prev)
  2650. ret = deliver_skb(skb, pt_prev, orig_dev);
  2651. pt_prev = ptype;
  2652. }
  2653. }
  2654. if (pt_prev) {
  2655. ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
  2656. } else {
  2657. atomic_long_inc(&skb->dev->rx_dropped);
  2658. kfree_skb(skb);
  2659. /* Jamal, now you will not able to escape explaining
  2660. * me how you were going to use this. :-)
  2661. */
  2662. ret = NET_RX_DROP;
  2663. }
  2664. out:
  2665. rcu_read_unlock();
  2666. return ret;
  2667. }
  2668. /**
  2669. * netif_receive_skb - process receive buffer from network
  2670. * @skb: buffer to process
  2671. *
  2672. * netif_receive_skb() is the main receive data processing function.
  2673. * It always succeeds. The buffer may be dropped during processing
  2674. * for congestion control or by the protocol layers.
  2675. *
  2676. * This function may only be called from softirq context and interrupts
  2677. * should be enabled.
  2678. *
  2679. * Return values (usually ignored):
  2680. * NET_RX_SUCCESS: no congestion
  2681. * NET_RX_DROP: packet was dropped
  2682. */
  2683. int netif_receive_skb(struct sk_buff *skb)
  2684. {
  2685. if (netdev_tstamp_prequeue)
  2686. net_timestamp_check(skb);
  2687. if (skb_defer_rx_timestamp(skb))
  2688. return NET_RX_SUCCESS;
  2689. #ifdef CONFIG_RPS
  2690. {
  2691. struct rps_dev_flow voidflow, *rflow = &voidflow;
  2692. int cpu, ret;
  2693. rcu_read_lock();
  2694. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  2695. if (cpu >= 0) {
  2696. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  2697. rcu_read_unlock();
  2698. } else {
  2699. rcu_read_unlock();
  2700. ret = __netif_receive_skb(skb);
  2701. }
  2702. return ret;
  2703. }
  2704. #else
  2705. return __netif_receive_skb(skb);
  2706. #endif
  2707. }
  2708. EXPORT_SYMBOL(netif_receive_skb);
  2709. /* Network device is going away, flush any packets still pending
  2710. * Called with irqs disabled.
  2711. */
  2712. static void flush_backlog(void *arg)
  2713. {
  2714. struct net_device *dev = arg;
  2715. struct softnet_data *sd = &__get_cpu_var(softnet_data);
  2716. struct sk_buff *skb, *tmp;
  2717. rps_lock(sd);
  2718. skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
  2719. if (skb->dev == dev) {
  2720. __skb_unlink(skb, &sd->input_pkt_queue);
  2721. kfree_skb(skb);
  2722. input_queue_head_incr(sd);
  2723. }
  2724. }
  2725. rps_unlock(sd);
  2726. skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
  2727. if (skb->dev == dev) {
  2728. __skb_unlink(skb, &sd->process_queue);
  2729. kfree_skb(skb);
  2730. input_queue_head_incr(sd);
  2731. }
  2732. }
  2733. }
  2734. static int napi_gro_complete(struct sk_buff *skb)
  2735. {
  2736. struct packet_type *ptype;
  2737. __be16 type = skb->protocol;
  2738. struct list_head *head = &ptype_base[ntohs(type) & PTYPE_HASH_MASK];
  2739. int err = -ENOENT;
  2740. if (NAPI_GRO_CB(skb)->count == 1) {
  2741. skb_shinfo(skb)->gso_size = 0;
  2742. goto out;
  2743. }
  2744. rcu_read_lock();
  2745. list_for_each_entry_rcu(ptype, head, list) {
  2746. if (ptype->type != type || ptype->dev || !ptype->gro_complete)
  2747. continue;
  2748. err = ptype->gro_complete(skb);
  2749. break;
  2750. }
  2751. rcu_read_unlock();
  2752. if (err) {
  2753. WARN_ON(&ptype->list == head);
  2754. kfree_skb(skb);
  2755. return NET_RX_SUCCESS;
  2756. }
  2757. out:
  2758. return netif_receive_skb(skb);
  2759. }
  2760. inline void napi_gro_flush(struct napi_struct *napi)
  2761. {
  2762. struct sk_buff *skb, *next;
  2763. for (skb = napi->gro_list; skb; skb = next) {
  2764. next = skb->next;
  2765. skb->next = NULL;
  2766. napi_gro_complete(skb);
  2767. }
  2768. napi->gro_count = 0;
  2769. napi->gro_list = NULL;
  2770. }
  2771. EXPORT_SYMBOL(napi_gro_flush);
  2772. enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
  2773. {
  2774. struct sk_buff **pp = NULL;
  2775. struct packet_type *ptype;
  2776. __be16 type = skb->protocol;
  2777. struct list_head *head = &ptype_base[ntohs(type) & PTYPE_HASH_MASK];
  2778. int same_flow;
  2779. int mac_len;
  2780. enum gro_result ret;
  2781. if (!(skb->dev->features & NETIF_F_GRO) || netpoll_rx_on(skb))
  2782. goto normal;
  2783. if (skb_is_gso(skb) || skb_has_frag_list(skb))
  2784. goto normal;
  2785. rcu_read_lock();
  2786. list_for_each_entry_rcu(ptype, head, list) {
  2787. if (ptype->type != type || ptype->dev || !ptype->gro_receive)
  2788. continue;
  2789. skb_set_network_header(skb, skb_gro_offset(skb));
  2790. mac_len = skb->network_header - skb->mac_header;
  2791. skb->mac_len = mac_len;
  2792. NAPI_GRO_CB(skb)->same_flow = 0;
  2793. NAPI_GRO_CB(skb)->flush = 0;
  2794. NAPI_GRO_CB(skb)->free = 0;
  2795. pp = ptype->gro_receive(&napi->gro_list, skb);
  2796. break;
  2797. }
  2798. rcu_read_unlock();
  2799. if (&ptype->list == head)
  2800. goto normal;
  2801. same_flow = NAPI_GRO_CB(skb)->same_flow;
  2802. ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
  2803. if (pp) {
  2804. struct sk_buff *nskb = *pp;
  2805. *pp = nskb->next;
  2806. nskb->next = NULL;
  2807. napi_gro_complete(nskb);
  2808. napi->gro_count--;
  2809. }
  2810. if (same_flow)
  2811. goto ok;
  2812. if (NAPI_GRO_CB(skb)->flush || napi->gro_count >= MAX_GRO_SKBS)
  2813. goto normal;
  2814. napi->gro_count++;
  2815. NAPI_GRO_CB(skb)->count = 1;
  2816. skb_shinfo(skb)->gso_size = skb_gro_len(skb);
  2817. skb->next = napi->gro_list;
  2818. napi->gro_list = skb;
  2819. ret = GRO_HELD;
  2820. pull:
  2821. if (skb_headlen(skb) < skb_gro_offset(skb)) {
  2822. int grow = skb_gro_offset(skb) - skb_headlen(skb);
  2823. BUG_ON(skb->end - skb->tail < grow);
  2824. memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
  2825. skb->tail += grow;
  2826. skb->data_len -= grow;
  2827. skb_shinfo(skb)->frags[0].page_offset += grow;
  2828. skb_shinfo(skb)->frags[0].size -= grow;
  2829. if (unlikely(!skb_shinfo(skb)->frags[0].size)) {
  2830. put_page(skb_shinfo(skb)->frags[0].page);
  2831. memmove(skb_shinfo(skb)->frags,
  2832. skb_shinfo(skb)->frags + 1,
  2833. --skb_shinfo(skb)->nr_frags * sizeof(skb_frag_t));
  2834. }
  2835. }
  2836. ok:
  2837. return ret;
  2838. normal:
  2839. ret = GRO_NORMAL;
  2840. goto pull;
  2841. }
  2842. EXPORT_SYMBOL(dev_gro_receive);
  2843. static inline gro_result_t
  2844. __napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
  2845. {
  2846. struct sk_buff *p;
  2847. for (p = napi->gro_list; p; p = p->next) {
  2848. unsigned long diffs;
  2849. diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
  2850. diffs |= p->vlan_tci ^ skb->vlan_tci;
  2851. diffs |= compare_ether_header(skb_mac_header(p),
  2852. skb_gro_mac_header(skb));
  2853. NAPI_GRO_CB(p)->same_flow = !diffs;
  2854. NAPI_GRO_CB(p)->flush = 0;
  2855. }
  2856. return dev_gro_receive(napi, skb);
  2857. }
  2858. gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
  2859. {
  2860. switch (ret) {
  2861. case GRO_NORMAL:
  2862. if (netif_receive_skb(skb))
  2863. ret = GRO_DROP;
  2864. break;
  2865. case GRO_DROP:
  2866. case GRO_MERGED_FREE:
  2867. kfree_skb(skb);
  2868. break;
  2869. case GRO_HELD:
  2870. case GRO_MERGED:
  2871. break;
  2872. }
  2873. return ret;
  2874. }
  2875. EXPORT_SYMBOL(napi_skb_finish);
  2876. void skb_gro_reset_offset(struct sk_buff *skb)
  2877. {
  2878. NAPI_GRO_CB(skb)->data_offset = 0;
  2879. NAPI_GRO_CB(skb)->frag0 = NULL;
  2880. NAPI_GRO_CB(skb)->frag0_len = 0;
  2881. if (skb->mac_header == skb->tail &&
  2882. !PageHighMem(skb_shinfo(skb)->frags[0].page)) {
  2883. NAPI_GRO_CB(skb)->frag0 =
  2884. page_address(skb_shinfo(skb)->frags[0].page) +
  2885. skb_shinfo(skb)->frags[0].page_offset;
  2886. NAPI_GRO_CB(skb)->frag0_len = skb_shinfo(skb)->frags[0].size;
  2887. }
  2888. }
  2889. EXPORT_SYMBOL(skb_gro_reset_offset);
  2890. gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
  2891. {
  2892. skb_gro_reset_offset(skb);
  2893. return napi_skb_finish(__napi_gro_receive(napi, skb), skb);
  2894. }
  2895. EXPORT_SYMBOL(napi_gro_receive);
  2896. static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
  2897. {
  2898. __skb_pull(skb, skb_headlen(skb));
  2899. skb_reserve(skb, NET_IP_ALIGN - skb_headroom(skb));
  2900. skb->vlan_tci = 0;
  2901. napi->skb = skb;
  2902. }
  2903. struct sk_buff *napi_get_frags(struct napi_struct *napi)
  2904. {
  2905. struct sk_buff *skb = napi->skb;
  2906. if (!skb) {
  2907. skb = netdev_alloc_skb_ip_align(napi->dev, GRO_MAX_HEAD);
  2908. if (skb)
  2909. napi->skb = skb;
  2910. }
  2911. return skb;
  2912. }
  2913. EXPORT_SYMBOL(napi_get_frags);
  2914. gro_result_t napi_frags_finish(struct napi_struct *napi, struct sk_buff *skb,
  2915. gro_result_t ret)
  2916. {
  2917. switch (ret) {
  2918. case GRO_NORMAL:
  2919. case GRO_HELD:
  2920. skb->protocol = eth_type_trans(skb, skb->dev);
  2921. if (ret == GRO_HELD)
  2922. skb_gro_pull(skb, -ETH_HLEN);
  2923. else if (netif_receive_skb(skb))
  2924. ret = GRO_DROP;
  2925. break;
  2926. case GRO_DROP:
  2927. case GRO_MERGED_FREE:
  2928. napi_reuse_skb(napi, skb);
  2929. break;
  2930. case GRO_MERGED:
  2931. break;
  2932. }
  2933. return ret;
  2934. }
  2935. EXPORT_SYMBOL(napi_frags_finish);
  2936. struct sk_buff *napi_frags_skb(struct napi_struct *napi)
  2937. {
  2938. struct sk_buff *skb = napi->skb;
  2939. struct ethhdr *eth;
  2940. unsigned int hlen;
  2941. unsigned int off;
  2942. napi->skb = NULL;
  2943. skb_reset_mac_header(skb);
  2944. skb_gro_reset_offset(skb);
  2945. off = skb_gro_offset(skb);
  2946. hlen = off + sizeof(*eth);
  2947. eth = skb_gro_header_fast(skb, off);
  2948. if (skb_gro_header_hard(skb, hlen)) {
  2949. eth = skb_gro_header_slow(skb, hlen, off);
  2950. if (unlikely(!eth)) {
  2951. napi_reuse_skb(napi, skb);
  2952. skb = NULL;
  2953. goto out;
  2954. }
  2955. }
  2956. skb_gro_pull(skb, sizeof(*eth));
  2957. /*
  2958. * This works because the only protocols we care about don't require
  2959. * special handling. We'll fix it up properly at the end.
  2960. */
  2961. skb->protocol = eth->h_proto;
  2962. out:
  2963. return skb;
  2964. }
  2965. EXPORT_SYMBOL(napi_frags_skb);
  2966. gro_result_t napi_gro_frags(struct napi_struct *napi)
  2967. {
  2968. struct sk_buff *skb = napi_frags_skb(napi);
  2969. if (!skb)
  2970. return GRO_DROP;
  2971. return napi_frags_finish(napi, skb, __napi_gro_receive(napi, skb));
  2972. }
  2973. EXPORT_SYMBOL(napi_gro_frags);
  2974. /*
  2975. * net_rps_action sends any pending IPI's for rps.
  2976. * Note: called with local irq disabled, but exits with local irq enabled.
  2977. */
  2978. static void net_rps_action_and_irq_enable(struct softnet_data *sd)
  2979. {
  2980. #ifdef CONFIG_RPS
  2981. struct softnet_data *remsd = sd->rps_ipi_list;
  2982. if (remsd) {
  2983. sd->rps_ipi_list = NULL;
  2984. local_irq_enable();
  2985. /* Send pending IPI's to kick RPS processing on remote cpus. */
  2986. while (remsd) {
  2987. struct softnet_data *next = remsd->rps_ipi_next;
  2988. if (cpu_online(remsd->cpu))
  2989. __smp_call_function_single(remsd->cpu,
  2990. &remsd->csd, 0);
  2991. remsd = next;
  2992. }
  2993. } else
  2994. #endif
  2995. local_irq_enable();
  2996. }
  2997. static int process_backlog(struct napi_struct *napi, int quota)
  2998. {
  2999. int work = 0;
  3000. struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
  3001. #ifdef CONFIG_RPS
  3002. /* Check if we have pending ipi, its better to send them now,
  3003. * not waiting net_rx_action() end.
  3004. */
  3005. if (sd->rps_ipi_list) {
  3006. local_irq_disable();
  3007. net_rps_action_and_irq_enable(sd);
  3008. }
  3009. #endif
  3010. napi->weight = weight_p;
  3011. local_irq_disable();
  3012. while (work < quota) {
  3013. struct sk_buff *skb;
  3014. unsigned int qlen;
  3015. while ((skb = __skb_dequeue(&sd->process_queue))) {
  3016. local_irq_enable();
  3017. __netif_receive_skb(skb);
  3018. local_irq_disable();
  3019. input_queue_head_incr(sd);
  3020. if (++work >= quota) {
  3021. local_irq_enable();
  3022. return work;
  3023. }
  3024. }
  3025. rps_lock(sd);
  3026. qlen = skb_queue_len(&sd->input_pkt_queue);
  3027. if (qlen)
  3028. skb_queue_splice_tail_init(&sd->input_pkt_queue,
  3029. &sd->process_queue);
  3030. if (qlen < quota - work) {
  3031. /*
  3032. * Inline a custom version of __napi_complete().
  3033. * only current cpu owns and manipulates this napi,
  3034. * and NAPI_STATE_SCHED is the only possible flag set on backlog.
  3035. * we can use a plain write instead of clear_bit(),
  3036. * and we dont need an smp_mb() memory barrier.
  3037. */
  3038. list_del(&napi->poll_list);
  3039. napi->state = 0;
  3040. quota = work + qlen;
  3041. }
  3042. rps_unlock(sd);
  3043. }
  3044. local_irq_enable();
  3045. return work;
  3046. }
  3047. /**
  3048. * __napi_schedule - schedule for receive
  3049. * @n: entry to schedule
  3050. *
  3051. * The entry's receive function will be scheduled to run
  3052. */
  3053. void __napi_schedule(struct napi_struct *n)
  3054. {
  3055. unsigned long flags;
  3056. local_irq_save(flags);
  3057. ____napi_schedule(&__get_cpu_var(softnet_data), n);
  3058. local_irq_restore(flags);
  3059. }
  3060. EXPORT_SYMBOL(__napi_schedule);
  3061. void __napi_complete(struct napi_struct *n)
  3062. {
  3063. BUG_ON(!test_bit(NAPI_STATE_SCHED, &n->state));
  3064. BUG_ON(n->gro_list);
  3065. list_del(&n->poll_list);
  3066. smp_mb__before_clear_bit();
  3067. clear_bit(NAPI_STATE_SCHED, &n->state);
  3068. }
  3069. EXPORT_SYMBOL(__napi_complete);
  3070. void napi_complete(struct napi_struct *n)
  3071. {
  3072. unsigned long flags;
  3073. /*
  3074. * don't let napi dequeue from the cpu poll list
  3075. * just in case its running on a different cpu
  3076. */
  3077. if (unlikely(test_bit(NAPI_STATE_NPSVC, &n->state)))
  3078. return;
  3079. napi_gro_flush(n);
  3080. local_irq_save(flags);
  3081. __napi_complete(n);
  3082. local_irq_restore(flags);
  3083. }
  3084. EXPORT_SYMBOL(napi_complete);
  3085. void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
  3086. int (*poll)(struct napi_struct *, int), int weight)
  3087. {
  3088. INIT_LIST_HEAD(&napi->poll_list);
  3089. napi->gro_count = 0;
  3090. napi->gro_list = NULL;
  3091. napi->skb = NULL;
  3092. napi->poll = poll;
  3093. napi->weight = weight;
  3094. list_add(&napi->dev_list, &dev->napi_list);
  3095. napi->dev = dev;
  3096. #ifdef CONFIG_NETPOLL
  3097. spin_lock_init(&napi->poll_lock);
  3098. napi->poll_owner = -1;
  3099. #endif
  3100. set_bit(NAPI_STATE_SCHED, &napi->state);
  3101. }
  3102. EXPORT_SYMBOL(netif_napi_add);
  3103. void netif_napi_del(struct napi_struct *napi)
  3104. {
  3105. struct sk_buff *skb, *next;
  3106. list_del_init(&napi->dev_list);
  3107. napi_free_frags(napi);
  3108. for (skb = napi->gro_list; skb; skb = next) {
  3109. next = skb->next;
  3110. skb->next = NULL;
  3111. kfree_skb(skb);
  3112. }
  3113. napi->gro_list = NULL;
  3114. napi->gro_count = 0;
  3115. }
  3116. EXPORT_SYMBOL(netif_napi_del);
  3117. static void net_rx_action(struct softirq_action *h)
  3118. {
  3119. struct softnet_data *sd = &__get_cpu_var(softnet_data);
  3120. unsigned long time_limit = jiffies + 2;
  3121. int budget = netdev_budget;
  3122. void *have;
  3123. local_irq_disable();
  3124. while (!list_empty(&sd->poll_list)) {
  3125. struct napi_struct *n;
  3126. int work, weight;
  3127. /* If softirq window is exhuasted then punt.
  3128. * Allow this to run for 2 jiffies since which will allow
  3129. * an average latency of 1.5/HZ.
  3130. */
  3131. if (unlikely(budget <= 0 || time_after(jiffies, time_limit)))
  3132. goto softnet_break;
  3133. local_irq_enable();
  3134. /* Even though interrupts have been re-enabled, this
  3135. * access is safe because interrupts can only add new
  3136. * entries to the tail of this list, and only ->poll()
  3137. * calls can remove this head entry from the list.
  3138. */
  3139. n = list_first_entry(&sd->poll_list, struct napi_struct, poll_list);
  3140. have = netpoll_poll_lock(n);
  3141. weight = n->weight;
  3142. /* This NAPI_STATE_SCHED test is for avoiding a race
  3143. * with netpoll's poll_napi(). Only the entity which
  3144. * obtains the lock and sees NAPI_STATE_SCHED set will
  3145. * actually make the ->poll() call. Therefore we avoid
  3146. * accidently calling ->poll() when NAPI is not scheduled.
  3147. */
  3148. work = 0;
  3149. if (test_bit(NAPI_STATE_SCHED, &n->state)) {
  3150. work = n->poll(n, weight);
  3151. trace_napi_poll(n);
  3152. }
  3153. WARN_ON_ONCE(work > weight);
  3154. budget -= work;
  3155. local_irq_disable();
  3156. /* Drivers must not modify the NAPI state if they
  3157. * consume the entire weight. In such cases this code
  3158. * still "owns" the NAPI instance and therefore can
  3159. * move the instance around on the list at-will.
  3160. */
  3161. if (unlikely(work == weight)) {
  3162. if (unlikely(napi_disable_pending(n))) {
  3163. local_irq_enable();
  3164. napi_complete(n);
  3165. local_irq_disable();
  3166. } else
  3167. list_move_tail(&n->poll_list, &sd->poll_list);
  3168. }
  3169. netpoll_poll_unlock(have);
  3170. }
  3171. out:
  3172. net_rps_action_and_irq_enable(sd);
  3173. #ifdef CONFIG_NET_DMA
  3174. /*
  3175. * There may not be any more sk_buffs coming right now, so push
  3176. * any pending DMA copies to hardware
  3177. */
  3178. dma_issue_pending_all();
  3179. #endif
  3180. return;
  3181. softnet_break:
  3182. sd->time_squeeze++;
  3183. __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  3184. goto out;
  3185. }
  3186. static gifconf_func_t *gifconf_list[NPROTO];
  3187. /**
  3188. * register_gifconf - register a SIOCGIF handler
  3189. * @family: Address family
  3190. * @gifconf: Function handler
  3191. *
  3192. * Register protocol dependent address dumping routines. The handler
  3193. * that is passed must not be freed or reused until it has been replaced
  3194. * by another handler.
  3195. */
  3196. int register_gifconf(unsigned int family, gifconf_func_t *gifconf)
  3197. {
  3198. if (family >= NPROTO)
  3199. return -EINVAL;
  3200. gifconf_list[family] = gifconf;
  3201. return 0;
  3202. }
  3203. EXPORT_SYMBOL(register_gifconf);
  3204. /*
  3205. * Map an interface index to its name (SIOCGIFNAME)
  3206. */
  3207. /*
  3208. * We need this ioctl for efficient implementation of the
  3209. * if_indextoname() function required by the IPv6 API. Without
  3210. * it, we would have to search all the interfaces to find a
  3211. * match. --pb
  3212. */
  3213. static int dev_ifname(struct net *net, struct ifreq __user *arg)
  3214. {
  3215. struct net_device *dev;
  3216. struct ifreq ifr;
  3217. /*
  3218. * Fetch the caller's info block.
  3219. */
  3220. if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
  3221. return -EFAULT;
  3222. rcu_read_lock();
  3223. dev = dev_get_by_index_rcu(net, ifr.ifr_ifindex);
  3224. if (!dev) {
  3225. rcu_read_unlock();
  3226. return -ENODEV;
  3227. }
  3228. strcpy(ifr.ifr_name, dev->name);
  3229. rcu_read_unlock();
  3230. if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
  3231. return -EFAULT;
  3232. return 0;
  3233. }
  3234. /*
  3235. * Perform a SIOCGIFCONF call. This structure will change
  3236. * size eventually, and there is nothing I can do about it.
  3237. * Thus we will need a 'compatibility mode'.
  3238. */
  3239. static int dev_ifconf(struct net *net, char __user *arg)
  3240. {
  3241. struct ifconf ifc;
  3242. struct net_device *dev;
  3243. char __user *pos;
  3244. int len;
  3245. int total;
  3246. int i;
  3247. /*
  3248. * Fetch the caller's info block.
  3249. */
  3250. if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
  3251. return -EFAULT;
  3252. pos = ifc.ifc_buf;
  3253. len = ifc.ifc_len;
  3254. /*
  3255. * Loop over the interfaces, and write an info block for each.
  3256. */
  3257. total = 0;
  3258. for_each_netdev(net, dev) {
  3259. for (i = 0; i < NPROTO; i++) {
  3260. if (gifconf_list[i]) {
  3261. int done;
  3262. if (!pos)
  3263. done = gifconf_list[i](dev, NULL, 0);
  3264. else
  3265. done = gifconf_list[i](dev, pos + total,
  3266. len - total);
  3267. if (done < 0)
  3268. return -EFAULT;
  3269. total += done;
  3270. }
  3271. }
  3272. }
  3273. /*
  3274. * All done. Write the updated control block back to the caller.
  3275. */
  3276. ifc.ifc_len = total;
  3277. /*
  3278. * Both BSD and Solaris return 0 here, so we do too.
  3279. */
  3280. return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
  3281. }
  3282. #ifdef CONFIG_PROC_FS
  3283. /*
  3284. * This is invoked by the /proc filesystem handler to display a device
  3285. * in detail.
  3286. */
  3287. void *dev_seq_start(struct seq_file *seq, loff_t *pos)
  3288. __acquires(RCU)
  3289. {
  3290. struct net *net = seq_file_net(seq);
  3291. loff_t off;
  3292. struct net_device *dev;
  3293. rcu_read_lock();
  3294. if (!*pos)
  3295. return SEQ_START_TOKEN;
  3296. off = 1;
  3297. for_each_netdev_rcu(net, dev)
  3298. if (off++ == *pos)
  3299. return dev;
  3300. return NULL;
  3301. }
  3302. void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  3303. {
  3304. struct net_device *dev = (v == SEQ_START_TOKEN) ?
  3305. first_net_device(seq_file_net(seq)) :
  3306. next_net_device((struct net_device *)v);
  3307. ++*pos;
  3308. return rcu_dereference(dev);
  3309. }
  3310. void dev_seq_stop(struct seq_file *seq, void *v)
  3311. __releases(RCU)
  3312. {
  3313. rcu_read_unlock();
  3314. }
  3315. static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
  3316. {
  3317. struct rtnl_link_stats64 temp;
  3318. const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
  3319. seq_printf(seq, "%6s: %7llu %7llu %4llu %4llu %4llu %5llu %10llu %9llu "
  3320. "%8llu %7llu %4llu %4llu %4llu %5llu %7llu %10llu\n",
  3321. dev->name, stats->rx_bytes, stats->rx_packets,
  3322. stats->rx_errors,
  3323. stats->rx_dropped + stats->rx_missed_errors,
  3324. stats->rx_fifo_errors,
  3325. stats->rx_length_errors + stats->rx_over_errors +
  3326. stats->rx_crc_errors + stats->rx_frame_errors,
  3327. stats->rx_compressed, stats->multicast,
  3328. stats->tx_bytes, stats->tx_packets,
  3329. stats->tx_errors, stats->tx_dropped,
  3330. stats->tx_fifo_errors, stats->collisions,
  3331. stats->tx_carrier_errors +
  3332. stats->tx_aborted_errors +
  3333. stats->tx_window_errors +
  3334. stats->tx_heartbeat_errors,
  3335. stats->tx_compressed);
  3336. }
  3337. /*
  3338. * Called from the PROCfs module. This now uses the new arbitrary sized
  3339. * /proc/net interface to create /proc/net/dev
  3340. */
  3341. static int dev_seq_show(struct seq_file *seq, void *v)
  3342. {
  3343. if (v == SEQ_START_TOKEN)
  3344. seq_puts(seq, "Inter-| Receive "
  3345. " | Transmit\n"
  3346. " face |bytes packets errs drop fifo frame "
  3347. "compressed multicast|bytes packets errs "
  3348. "drop fifo colls carrier compressed\n");
  3349. else
  3350. dev_seq_printf_stats(seq, v);
  3351. return 0;
  3352. }
  3353. static struct softnet_data *softnet_get_online(loff_t *pos)
  3354. {
  3355. struct softnet_data *sd = NULL;
  3356. while (*pos < nr_cpu_ids)
  3357. if (cpu_online(*pos)) {
  3358. sd = &per_cpu(softnet_data, *pos);
  3359. break;
  3360. } else
  3361. ++*pos;
  3362. return sd;
  3363. }
  3364. static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
  3365. {
  3366. return softnet_get_online(pos);
  3367. }
  3368. static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  3369. {
  3370. ++*pos;
  3371. return softnet_get_online(pos);
  3372. }
  3373. static void softnet_seq_stop(struct seq_file *seq, void *v)
  3374. {
  3375. }
  3376. static int softnet_seq_show(struct seq_file *seq, void *v)
  3377. {
  3378. struct softnet_data *sd = v;
  3379. seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
  3380. sd->processed, sd->dropped, sd->time_squeeze, 0,
  3381. 0, 0, 0, 0, /* was fastroute */
  3382. sd->cpu_collision, sd->received_rps);
  3383. return 0;
  3384. }
  3385. static const struct seq_operations dev_seq_ops = {
  3386. .start = dev_seq_start,
  3387. .next = dev_seq_next,
  3388. .stop = dev_seq_stop,
  3389. .show = dev_seq_show,
  3390. };
  3391. static int dev_seq_open(struct inode *inode, struct file *file)
  3392. {
  3393. return seq_open_net(inode, file, &dev_seq_ops,
  3394. sizeof(struct seq_net_private));
  3395. }
  3396. static const struct file_operations dev_seq_fops = {
  3397. .owner = THIS_MODULE,
  3398. .open = dev_seq_open,
  3399. .read = seq_read,
  3400. .llseek = seq_lseek,
  3401. .release = seq_release_net,
  3402. };
  3403. static const struct seq_operations softnet_seq_ops = {
  3404. .start = softnet_seq_start,
  3405. .next = softnet_seq_next,
  3406. .stop = softnet_seq_stop,
  3407. .show = softnet_seq_show,
  3408. };
  3409. static int softnet_seq_open(struct inode *inode, struct file *file)
  3410. {
  3411. return seq_open(file, &softnet_seq_ops);
  3412. }
  3413. static const struct file_operations softnet_seq_fops = {
  3414. .owner = THIS_MODULE,
  3415. .open = softnet_seq_open,
  3416. .read = seq_read,
  3417. .llseek = seq_lseek,
  3418. .release = seq_release,
  3419. };
  3420. static void *ptype_get_idx(loff_t pos)
  3421. {
  3422. struct packet_type *pt = NULL;
  3423. loff_t i = 0;
  3424. int t;
  3425. list_for_each_entry_rcu(pt, &ptype_all, list) {
  3426. if (i == pos)
  3427. return pt;
  3428. ++i;
  3429. }
  3430. for (t = 0; t < PTYPE_HASH_SIZE; t++) {
  3431. list_for_each_entry_rcu(pt, &ptype_base[t], list) {
  3432. if (i == pos)
  3433. return pt;
  3434. ++i;
  3435. }
  3436. }
  3437. return NULL;
  3438. }
  3439. static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
  3440. __acquires(RCU)
  3441. {
  3442. rcu_read_lock();
  3443. return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN;
  3444. }
  3445. static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  3446. {
  3447. struct packet_type *pt;
  3448. struct list_head *nxt;
  3449. int hash;
  3450. ++*pos;
  3451. if (v == SEQ_START_TOKEN)
  3452. return ptype_get_idx(0);
  3453. pt = v;
  3454. nxt = pt->list.next;
  3455. if (pt->type == htons(ETH_P_ALL)) {
  3456. if (nxt != &ptype_all)
  3457. goto found;
  3458. hash = 0;
  3459. nxt = ptype_base[0].next;
  3460. } else
  3461. hash = ntohs(pt->type) & PTYPE_HASH_MASK;
  3462. while (nxt == &ptype_base[hash]) {
  3463. if (++hash >= PTYPE_HASH_SIZE)
  3464. return NULL;
  3465. nxt = ptype_base[hash].next;
  3466. }
  3467. found:
  3468. return list_entry(nxt, struct packet_type, list);
  3469. }
  3470. static void ptype_seq_stop(struct seq_file *seq, void *v)
  3471. __releases(RCU)
  3472. {
  3473. rcu_read_unlock();
  3474. }
  3475. static int ptype_seq_show(struct seq_file *seq, void *v)
  3476. {
  3477. struct packet_type *pt = v;
  3478. if (v == SEQ_START_TOKEN)
  3479. seq_puts(seq, "Type Device Function\n");
  3480. else if (pt->dev == NULL || dev_net(pt->dev) == seq_file_net(seq)) {
  3481. if (pt->type == htons(ETH_P_ALL))
  3482. seq_puts(seq, "ALL ");
  3483. else
  3484. seq_printf(seq, "%04x", ntohs(pt->type));
  3485. seq_printf(seq, " %-8s %pF\n",
  3486. pt->dev ? pt->dev->name : "", pt->func);
  3487. }
  3488. return 0;
  3489. }
  3490. static const struct seq_operations ptype_seq_ops = {
  3491. .start = ptype_seq_start,
  3492. .next = ptype_seq_next,
  3493. .stop = ptype_seq_stop,
  3494. .show = ptype_seq_show,
  3495. };
  3496. static int ptype_seq_open(struct inode *inode, struct file *file)
  3497. {
  3498. return seq_open_net(inode, file, &ptype_seq_ops,
  3499. sizeof(struct seq_net_private));
  3500. }
  3501. static const struct file_operations ptype_seq_fops = {
  3502. .owner = THIS_MODULE,
  3503. .open = ptype_seq_open,
  3504. .read = seq_read,
  3505. .llseek = seq_lseek,
  3506. .release = seq_release_net,
  3507. };
  3508. static int __net_init dev_proc_net_init(struct net *net)
  3509. {
  3510. int rc = -ENOMEM;
  3511. if (!proc_net_fops_create(net, "dev", S_IRUGO, &dev_seq_fops))
  3512. goto out;
  3513. if (!proc_net_fops_create(net, "softnet_stat", S_IRUGO, &softnet_seq_fops))
  3514. goto out_dev;
  3515. if (!proc_net_fops_create(net, "ptype", S_IRUGO, &ptype_seq_fops))
  3516. goto out_softnet;
  3517. if (wext_proc_init(net))
  3518. goto out_ptype;
  3519. rc = 0;
  3520. out:
  3521. return rc;
  3522. out_ptype:
  3523. proc_net_remove(net, "ptype");
  3524. out_softnet:
  3525. proc_net_remove(net, "softnet_stat");
  3526. out_dev:
  3527. proc_net_remove(net, "dev");
  3528. goto out;
  3529. }
  3530. static void __net_exit dev_proc_net_exit(struct net *net)
  3531. {
  3532. wext_proc_exit(net);
  3533. proc_net_remove(net, "ptype");
  3534. proc_net_remove(net, "softnet_stat");
  3535. proc_net_remove(net, "dev");
  3536. }
  3537. static struct pernet_operations __net_initdata dev_proc_ops = {
  3538. .init = dev_proc_net_init,
  3539. .exit = dev_proc_net_exit,
  3540. };
  3541. static int __init dev_proc_init(void)
  3542. {
  3543. return register_pernet_subsys(&dev_proc_ops);
  3544. }
  3545. #else
  3546. #define dev_proc_init() 0
  3547. #endif /* CONFIG_PROC_FS */
  3548. /**
  3549. * netdev_set_master - set up master/slave pair
  3550. * @slave: slave device
  3551. * @master: new master device
  3552. *
  3553. * Changes the master device of the slave. Pass %NULL to break the
  3554. * bonding. The caller must hold the RTNL semaphore. On a failure
  3555. * a negative errno code is returned. On success the reference counts
  3556. * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
  3557. * function returns zero.
  3558. */
  3559. int netdev_set_master(struct net_device *slave, struct net_device *master)
  3560. {
  3561. struct net_device *old = slave->master;
  3562. ASSERT_RTNL();
  3563. if (master) {
  3564. if (old)
  3565. return -EBUSY;
  3566. dev_hold(master);
  3567. }
  3568. slave->master = master;
  3569. if (old) {
  3570. synchronize_net();
  3571. dev_put(old);
  3572. }
  3573. if (master)
  3574. slave->flags |= IFF_SLAVE;
  3575. else
  3576. slave->flags &= ~IFF_SLAVE;
  3577. rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
  3578. return 0;
  3579. }
  3580. EXPORT_SYMBOL(netdev_set_master);
  3581. static void dev_change_rx_flags(struct net_device *dev, int flags)
  3582. {
  3583. const struct net_device_ops *ops = dev->netdev_ops;
  3584. if ((dev->flags & IFF_UP) && ops->ndo_change_rx_flags)
  3585. ops->ndo_change_rx_flags(dev, flags);
  3586. }
  3587. static int __dev_set_promiscuity(struct net_device *dev, int inc)
  3588. {
  3589. unsigned short old_flags = dev->flags;
  3590. uid_t uid;
  3591. gid_t gid;
  3592. ASSERT_RTNL();
  3593. dev->flags |= IFF_PROMISC;
  3594. dev->promiscuity += inc;
  3595. if (dev->promiscuity == 0) {
  3596. /*
  3597. * Avoid overflow.
  3598. * If inc causes overflow, untouch promisc and return error.
  3599. */
  3600. if (inc < 0)
  3601. dev->flags &= ~IFF_PROMISC;
  3602. else {
  3603. dev->promiscuity -= inc;
  3604. printk(KERN_WARNING "%s: promiscuity touches roof, "
  3605. "set promiscuity failed, promiscuity feature "
  3606. "of device might be broken.\n", dev->name);
  3607. return -EOVERFLOW;
  3608. }
  3609. }
  3610. if (dev->flags != old_flags) {
  3611. printk(KERN_INFO "device %s %s promiscuous mode\n",
  3612. dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
  3613. "left");
  3614. if (audit_enabled) {
  3615. current_uid_gid(&uid, &gid);
  3616. audit_log(current->audit_context, GFP_ATOMIC,
  3617. AUDIT_ANOM_PROMISCUOUS,
  3618. "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
  3619. dev->name, (dev->flags & IFF_PROMISC),
  3620. (old_flags & IFF_PROMISC),
  3621. audit_get_loginuid(current),
  3622. uid, gid,
  3623. audit_get_sessionid(current));
  3624. }
  3625. dev_change_rx_flags(dev, IFF_PROMISC);
  3626. }
  3627. return 0;
  3628. }
  3629. /**
  3630. * dev_set_promiscuity - update promiscuity count on a device
  3631. * @dev: device
  3632. * @inc: modifier
  3633. *
  3634. * Add or remove promiscuity from a device. While the count in the device
  3635. * remains above zero the interface remains promiscuous. Once it hits zero
  3636. * the device reverts back to normal filtering operation. A negative inc
  3637. * value is used to drop promiscuity on the device.
  3638. * Return 0 if successful or a negative errno code on error.
  3639. */
  3640. int dev_set_promiscuity(struct net_device *dev, int inc)
  3641. {
  3642. unsigned short old_flags = dev->flags;
  3643. int err;
  3644. err = __dev_set_promiscuity(dev, inc);
  3645. if (err < 0)
  3646. return err;
  3647. if (dev->flags != old_flags)
  3648. dev_set_rx_mode(dev);
  3649. return err;
  3650. }
  3651. EXPORT_SYMBOL(dev_set_promiscuity);
  3652. /**
  3653. * dev_set_allmulti - update allmulti count on a device
  3654. * @dev: device
  3655. * @inc: modifier
  3656. *
  3657. * Add or remove reception of all multicast frames to a device. While the
  3658. * count in the device remains above zero the interface remains listening
  3659. * to all interfaces. Once it hits zero the device reverts back to normal
  3660. * filtering operation. A negative @inc value is used to drop the counter
  3661. * when releasing a resource needing all multicasts.
  3662. * Return 0 if successful or a negative errno code on error.
  3663. */
  3664. int dev_set_allmulti(struct net_device *dev, int inc)
  3665. {
  3666. unsigned short old_flags = dev->flags;
  3667. ASSERT_RTNL();
  3668. dev->flags |= IFF_ALLMULTI;
  3669. dev->allmulti += inc;
  3670. if (dev->allmulti == 0) {
  3671. /*
  3672. * Avoid overflow.
  3673. * If inc causes overflow, untouch allmulti and return error.
  3674. */
  3675. if (inc < 0)
  3676. dev->flags &= ~IFF_ALLMULTI;
  3677. else {
  3678. dev->allmulti -= inc;
  3679. printk(KERN_WARNING "%s: allmulti touches roof, "
  3680. "set allmulti failed, allmulti feature of "
  3681. "device might be broken.\n", dev->name);
  3682. return -EOVERFLOW;
  3683. }
  3684. }
  3685. if (dev->flags ^ old_flags) {
  3686. dev_change_rx_flags(dev, IFF_ALLMULTI);
  3687. dev_set_rx_mode(dev);
  3688. }
  3689. return 0;
  3690. }
  3691. EXPORT_SYMBOL(dev_set_allmulti);
  3692. /*
  3693. * Upload unicast and multicast address lists to device and
  3694. * configure RX filtering. When the device doesn't support unicast
  3695. * filtering it is put in promiscuous mode while unicast addresses
  3696. * are present.
  3697. */
  3698. void __dev_set_rx_mode(struct net_device *dev)
  3699. {
  3700. const struct net_device_ops *ops = dev->netdev_ops;
  3701. /* dev_open will call this function so the list will stay sane. */
  3702. if (!(dev->flags&IFF_UP))
  3703. return;
  3704. if (!netif_device_present(dev))
  3705. return;
  3706. if (ops->ndo_set_rx_mode)
  3707. ops->ndo_set_rx_mode(dev);
  3708. else {
  3709. /* Unicast addresses changes may only happen under the rtnl,
  3710. * therefore calling __dev_set_promiscuity here is safe.
  3711. */
  3712. if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
  3713. __dev_set_promiscuity(dev, 1);
  3714. dev->uc_promisc = 1;
  3715. } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
  3716. __dev_set_promiscuity(dev, -1);
  3717. dev->uc_promisc = 0;
  3718. }
  3719. if (ops->ndo_set_multicast_list)
  3720. ops->ndo_set_multicast_list(dev);
  3721. }
  3722. }
  3723. void dev_set_rx_mode(struct net_device *dev)
  3724. {
  3725. netif_addr_lock_bh(dev);
  3726. __dev_set_rx_mode(dev);
  3727. netif_addr_unlock_bh(dev);
  3728. }
  3729. /**
  3730. * dev_get_flags - get flags reported to userspace
  3731. * @dev: device
  3732. *
  3733. * Get the combination of flag bits exported through APIs to userspace.
  3734. */
  3735. unsigned dev_get_flags(const struct net_device *dev)
  3736. {
  3737. unsigned flags;
  3738. flags = (dev->flags & ~(IFF_PROMISC |
  3739. IFF_ALLMULTI |
  3740. IFF_RUNNING |
  3741. IFF_LOWER_UP |
  3742. IFF_DORMANT)) |
  3743. (dev->gflags & (IFF_PROMISC |
  3744. IFF_ALLMULTI));
  3745. if (netif_running(dev)) {
  3746. if (netif_oper_up(dev))
  3747. flags |= IFF_RUNNING;
  3748. if (netif_carrier_ok(dev))
  3749. flags |= IFF_LOWER_UP;
  3750. if (netif_dormant(dev))
  3751. flags |= IFF_DORMANT;
  3752. }
  3753. return flags;
  3754. }
  3755. EXPORT_SYMBOL(dev_get_flags);
  3756. int __dev_change_flags(struct net_device *dev, unsigned int flags)
  3757. {
  3758. int old_flags = dev->flags;
  3759. int ret;
  3760. ASSERT_RTNL();
  3761. /*
  3762. * Set the flags on our device.
  3763. */
  3764. dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
  3765. IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
  3766. IFF_AUTOMEDIA)) |
  3767. (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
  3768. IFF_ALLMULTI));
  3769. /*
  3770. * Load in the correct multicast list now the flags have changed.
  3771. */
  3772. if ((old_flags ^ flags) & IFF_MULTICAST)
  3773. dev_change_rx_flags(dev, IFF_MULTICAST);
  3774. dev_set_rx_mode(dev);
  3775. /*
  3776. * Have we downed the interface. We handle IFF_UP ourselves
  3777. * according to user attempts to set it, rather than blindly
  3778. * setting it.
  3779. */
  3780. ret = 0;
  3781. if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
  3782. ret = ((old_flags & IFF_UP) ? __dev_close : __dev_open)(dev);
  3783. if (!ret)
  3784. dev_set_rx_mode(dev);
  3785. }
  3786. if ((flags ^ dev->gflags) & IFF_PROMISC) {
  3787. int inc = (flags & IFF_PROMISC) ? 1 : -1;
  3788. dev->gflags ^= IFF_PROMISC;
  3789. dev_set_promiscuity(dev, inc);
  3790. }
  3791. /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
  3792. is important. Some (broken) drivers set IFF_PROMISC, when
  3793. IFF_ALLMULTI is requested not asking us and not reporting.
  3794. */
  3795. if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
  3796. int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
  3797. dev->gflags ^= IFF_ALLMULTI;
  3798. dev_set_allmulti(dev, inc);
  3799. }
  3800. return ret;
  3801. }
  3802. void __dev_notify_flags(struct net_device *dev, unsigned int old_flags)
  3803. {
  3804. unsigned int changes = dev->flags ^ old_flags;
  3805. if (changes & IFF_UP) {
  3806. if (dev->flags & IFF_UP)
  3807. call_netdevice_notifiers(NETDEV_UP, dev);
  3808. else
  3809. call_netdevice_notifiers(NETDEV_DOWN, dev);
  3810. }
  3811. if (dev->flags & IFF_UP &&
  3812. (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE)))
  3813. call_netdevice_notifiers(NETDEV_CHANGE, dev);
  3814. }
  3815. /**
  3816. * dev_change_flags - change device settings
  3817. * @dev: device
  3818. * @flags: device state flags
  3819. *
  3820. * Change settings on device based state flags. The flags are
  3821. * in the userspace exported format.
  3822. */
  3823. int dev_change_flags(struct net_device *dev, unsigned flags)
  3824. {
  3825. int ret, changes;
  3826. int old_flags = dev->flags;
  3827. ret = __dev_change_flags(dev, flags);
  3828. if (ret < 0)
  3829. return ret;
  3830. changes = old_flags ^ dev->flags;
  3831. if (changes)
  3832. rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
  3833. __dev_notify_flags(dev, old_flags);
  3834. return ret;
  3835. }
  3836. EXPORT_SYMBOL(dev_change_flags);
  3837. /**
  3838. * dev_set_mtu - Change maximum transfer unit
  3839. * @dev: device
  3840. * @new_mtu: new transfer unit
  3841. *
  3842. * Change the maximum transfer size of the network device.
  3843. */
  3844. int dev_set_mtu(struct net_device *dev, int new_mtu)
  3845. {
  3846. const struct net_device_ops *ops = dev->netdev_ops;
  3847. int err;
  3848. if (new_mtu == dev->mtu)
  3849. return 0;
  3850. /* MTU must be positive. */
  3851. if (new_mtu < 0)
  3852. return -EINVAL;
  3853. if (!netif_device_present(dev))
  3854. return -ENODEV;
  3855. err = 0;
  3856. if (ops->ndo_change_mtu)
  3857. err = ops->ndo_change_mtu(dev, new_mtu);
  3858. else
  3859. dev->mtu = new_mtu;
  3860. if (!err && dev->flags & IFF_UP)
  3861. call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
  3862. return err;
  3863. }
  3864. EXPORT_SYMBOL(dev_set_mtu);
  3865. /**
  3866. * dev_set_mac_address - Change Media Access Control Address
  3867. * @dev: device
  3868. * @sa: new address
  3869. *
  3870. * Change the hardware (MAC) address of the device
  3871. */
  3872. int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
  3873. {
  3874. const struct net_device_ops *ops = dev->netdev_ops;
  3875. int err;
  3876. if (!ops->ndo_set_mac_address)
  3877. return -EOPNOTSUPP;
  3878. if (sa->sa_family != dev->type)
  3879. return -EINVAL;
  3880. if (!netif_device_present(dev))
  3881. return -ENODEV;
  3882. err = ops->ndo_set_mac_address(dev, sa);
  3883. if (!err)
  3884. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  3885. return err;
  3886. }
  3887. EXPORT_SYMBOL(dev_set_mac_address);
  3888. /*
  3889. * Perform the SIOCxIFxxx calls, inside rcu_read_lock()
  3890. */
  3891. static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
  3892. {
  3893. int err;
  3894. struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
  3895. if (!dev)
  3896. return -ENODEV;
  3897. switch (cmd) {
  3898. case SIOCGIFFLAGS: /* Get interface flags */
  3899. ifr->ifr_flags = (short) dev_get_flags(dev);
  3900. return 0;
  3901. case SIOCGIFMETRIC: /* Get the metric on the interface
  3902. (currently unused) */
  3903. ifr->ifr_metric = 0;
  3904. return 0;
  3905. case SIOCGIFMTU: /* Get the MTU of a device */
  3906. ifr->ifr_mtu = dev->mtu;
  3907. return 0;
  3908. case SIOCGIFHWADDR:
  3909. if (!dev->addr_len)
  3910. memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
  3911. else
  3912. memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
  3913. min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
  3914. ifr->ifr_hwaddr.sa_family = dev->type;
  3915. return 0;
  3916. case SIOCGIFSLAVE:
  3917. err = -EINVAL;
  3918. break;
  3919. case SIOCGIFMAP:
  3920. ifr->ifr_map.mem_start = dev->mem_start;
  3921. ifr->ifr_map.mem_end = dev->mem_end;
  3922. ifr->ifr_map.base_addr = dev->base_addr;
  3923. ifr->ifr_map.irq = dev->irq;
  3924. ifr->ifr_map.dma = dev->dma;
  3925. ifr->ifr_map.port = dev->if_port;
  3926. return 0;
  3927. case SIOCGIFINDEX:
  3928. ifr->ifr_ifindex = dev->ifindex;
  3929. return 0;
  3930. case SIOCGIFTXQLEN:
  3931. ifr->ifr_qlen = dev->tx_queue_len;
  3932. return 0;
  3933. default:
  3934. /* dev_ioctl() should ensure this case
  3935. * is never reached
  3936. */
  3937. WARN_ON(1);
  3938. err = -EINVAL;
  3939. break;
  3940. }
  3941. return err;
  3942. }
  3943. /*
  3944. * Perform the SIOCxIFxxx calls, inside rtnl_lock()
  3945. */
  3946. static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
  3947. {
  3948. int err;
  3949. struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
  3950. const struct net_device_ops *ops;
  3951. if (!dev)
  3952. return -ENODEV;
  3953. ops = dev->netdev_ops;
  3954. switch (cmd) {
  3955. case SIOCSIFFLAGS: /* Set interface flags */
  3956. return dev_change_flags(dev, ifr->ifr_flags);
  3957. case SIOCSIFMETRIC: /* Set the metric on the interface
  3958. (currently unused) */
  3959. return -EOPNOTSUPP;
  3960. case SIOCSIFMTU: /* Set the MTU of a device */
  3961. return dev_set_mtu(dev, ifr->ifr_mtu);
  3962. case SIOCSIFHWADDR:
  3963. return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
  3964. case SIOCSIFHWBROADCAST:
  3965. if (ifr->ifr_hwaddr.sa_family != dev->type)
  3966. return -EINVAL;
  3967. memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
  3968. min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
  3969. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  3970. return 0;
  3971. case SIOCSIFMAP:
  3972. if (ops->ndo_set_config) {
  3973. if (!netif_device_present(dev))
  3974. return -ENODEV;
  3975. return ops->ndo_set_config(dev, &ifr->ifr_map);
  3976. }
  3977. return -EOPNOTSUPP;
  3978. case SIOCADDMULTI:
  3979. if ((!ops->ndo_set_multicast_list && !ops->ndo_set_rx_mode) ||
  3980. ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
  3981. return -EINVAL;
  3982. if (!netif_device_present(dev))
  3983. return -ENODEV;
  3984. return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
  3985. case SIOCDELMULTI:
  3986. if ((!ops->ndo_set_multicast_list && !ops->ndo_set_rx_mode) ||
  3987. ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
  3988. return -EINVAL;
  3989. if (!netif_device_present(dev))
  3990. return -ENODEV;
  3991. return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
  3992. case SIOCSIFTXQLEN:
  3993. if (ifr->ifr_qlen < 0)
  3994. return -EINVAL;
  3995. dev->tx_queue_len = ifr->ifr_qlen;
  3996. return 0;
  3997. case SIOCSIFNAME:
  3998. ifr->ifr_newname[IFNAMSIZ-1] = '\0';
  3999. return dev_change_name(dev, ifr->ifr_newname);
  4000. /*
  4001. * Unknown or private ioctl
  4002. */
  4003. default:
  4004. if ((cmd >= SIOCDEVPRIVATE &&
  4005. cmd <= SIOCDEVPRIVATE + 15) ||
  4006. cmd == SIOCBONDENSLAVE ||
  4007. cmd == SIOCBONDRELEASE ||
  4008. cmd == SIOCBONDSETHWADDR ||
  4009. cmd == SIOCBONDSLAVEINFOQUERY ||
  4010. cmd == SIOCBONDINFOQUERY ||
  4011. cmd == SIOCBONDCHANGEACTIVE ||
  4012. cmd == SIOCGMIIPHY ||
  4013. cmd == SIOCGMIIREG ||
  4014. cmd == SIOCSMIIREG ||
  4015. cmd == SIOCBRADDIF ||
  4016. cmd == SIOCBRDELIF ||
  4017. cmd == SIOCSHWTSTAMP ||
  4018. cmd == SIOCWANDEV) {
  4019. err = -EOPNOTSUPP;
  4020. if (ops->ndo_do_ioctl) {
  4021. if (netif_device_present(dev))
  4022. err = ops->ndo_do_ioctl(dev, ifr, cmd);
  4023. else
  4024. err = -ENODEV;
  4025. }
  4026. } else
  4027. err = -EINVAL;
  4028. }
  4029. return err;
  4030. }
  4031. /*
  4032. * This function handles all "interface"-type I/O control requests. The actual
  4033. * 'doing' part of this is dev_ifsioc above.
  4034. */
  4035. /**
  4036. * dev_ioctl - network device ioctl
  4037. * @net: the applicable net namespace
  4038. * @cmd: command to issue
  4039. * @arg: pointer to a struct ifreq in user space
  4040. *
  4041. * Issue ioctl functions to devices. This is normally called by the
  4042. * user space syscall interfaces but can sometimes be useful for
  4043. * other purposes. The return value is the return from the syscall if
  4044. * positive or a negative errno code on error.
  4045. */
  4046. int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg)
  4047. {
  4048. struct ifreq ifr;
  4049. int ret;
  4050. char *colon;
  4051. /* One special case: SIOCGIFCONF takes ifconf argument
  4052. and requires shared lock, because it sleeps writing
  4053. to user space.
  4054. */
  4055. if (cmd == SIOCGIFCONF) {
  4056. rtnl_lock();
  4057. ret = dev_ifconf(net, (char __user *) arg);
  4058. rtnl_unlock();
  4059. return ret;
  4060. }
  4061. if (cmd == SIOCGIFNAME)
  4062. return dev_ifname(net, (struct ifreq __user *)arg);
  4063. if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
  4064. return -EFAULT;
  4065. ifr.ifr_name[IFNAMSIZ-1] = 0;
  4066. colon = strchr(ifr.ifr_name, ':');
  4067. if (colon)
  4068. *colon = 0;
  4069. /*
  4070. * See which interface the caller is talking about.
  4071. */
  4072. switch (cmd) {
  4073. /*
  4074. * These ioctl calls:
  4075. * - can be done by all.
  4076. * - atomic and do not require locking.
  4077. * - return a value
  4078. */
  4079. case SIOCGIFFLAGS:
  4080. case SIOCGIFMETRIC:
  4081. case SIOCGIFMTU:
  4082. case SIOCGIFHWADDR:
  4083. case SIOCGIFSLAVE:
  4084. case SIOCGIFMAP:
  4085. case SIOCGIFINDEX:
  4086. case SIOCGIFTXQLEN:
  4087. dev_load(net, ifr.ifr_name);
  4088. rcu_read_lock();
  4089. ret = dev_ifsioc_locked(net, &ifr, cmd);
  4090. rcu_read_unlock();
  4091. if (!ret) {
  4092. if (colon)
  4093. *colon = ':';
  4094. if (copy_to_user(arg, &ifr,
  4095. sizeof(struct ifreq)))
  4096. ret = -EFAULT;
  4097. }
  4098. return ret;
  4099. case SIOCETHTOOL:
  4100. dev_load(net, ifr.ifr_name);
  4101. rtnl_lock();
  4102. ret = dev_ethtool(net, &ifr);
  4103. rtnl_unlock();
  4104. if (!ret) {
  4105. if (colon)
  4106. *colon = ':';
  4107. if (copy_to_user(arg, &ifr,
  4108. sizeof(struct ifreq)))
  4109. ret = -EFAULT;
  4110. }
  4111. return ret;
  4112. /*
  4113. * These ioctl calls:
  4114. * - require superuser power.
  4115. * - require strict serialization.
  4116. * - return a value
  4117. */
  4118. case SIOCGMIIPHY:
  4119. case SIOCGMIIREG:
  4120. case SIOCSIFNAME:
  4121. if (!capable(CAP_NET_ADMIN))
  4122. return -EPERM;
  4123. dev_load(net, ifr.ifr_name);
  4124. rtnl_lock();
  4125. ret = dev_ifsioc(net, &ifr, cmd);
  4126. rtnl_unlock();
  4127. if (!ret) {
  4128. if (colon)
  4129. *colon = ':';
  4130. if (copy_to_user(arg, &ifr,
  4131. sizeof(struct ifreq)))
  4132. ret = -EFAULT;
  4133. }
  4134. return ret;
  4135. /*
  4136. * These ioctl calls:
  4137. * - require superuser power.
  4138. * - require strict serialization.
  4139. * - do not return a value
  4140. */
  4141. case SIOCSIFFLAGS:
  4142. case SIOCSIFMETRIC:
  4143. case SIOCSIFMTU:
  4144. case SIOCSIFMAP:
  4145. case SIOCSIFHWADDR:
  4146. case SIOCSIFSLAVE:
  4147. case SIOCADDMULTI:
  4148. case SIOCDELMULTI:
  4149. case SIOCSIFHWBROADCAST:
  4150. case SIOCSIFTXQLEN:
  4151. case SIOCSMIIREG:
  4152. case SIOCBONDENSLAVE:
  4153. case SIOCBONDRELEASE:
  4154. case SIOCBONDSETHWADDR:
  4155. case SIOCBONDCHANGEACTIVE:
  4156. case SIOCBRADDIF:
  4157. case SIOCBRDELIF:
  4158. case SIOCSHWTSTAMP:
  4159. if (!capable(CAP_NET_ADMIN))
  4160. return -EPERM;
  4161. /* fall through */
  4162. case SIOCBONDSLAVEINFOQUERY:
  4163. case SIOCBONDINFOQUERY:
  4164. dev_load(net, ifr.ifr_name);
  4165. rtnl_lock();
  4166. ret = dev_ifsioc(net, &ifr, cmd);
  4167. rtnl_unlock();
  4168. return ret;
  4169. case SIOCGIFMEM:
  4170. /* Get the per device memory space. We can add this but
  4171. * currently do not support it */
  4172. case SIOCSIFMEM:
  4173. /* Set the per device memory buffer space.
  4174. * Not applicable in our case */
  4175. case SIOCSIFLINK:
  4176. return -EINVAL;
  4177. /*
  4178. * Unknown or private ioctl.
  4179. */
  4180. default:
  4181. if (cmd == SIOCWANDEV ||
  4182. (cmd >= SIOCDEVPRIVATE &&
  4183. cmd <= SIOCDEVPRIVATE + 15)) {
  4184. dev_load(net, ifr.ifr_name);
  4185. rtnl_lock();
  4186. ret = dev_ifsioc(net, &ifr, cmd);
  4187. rtnl_unlock();
  4188. if (!ret && copy_to_user(arg, &ifr,
  4189. sizeof(struct ifreq)))
  4190. ret = -EFAULT;
  4191. return ret;
  4192. }
  4193. /* Take care of Wireless Extensions */
  4194. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
  4195. return wext_handle_ioctl(net, &ifr, cmd, arg);
  4196. return -EINVAL;
  4197. }
  4198. }
  4199. /**
  4200. * dev_new_index - allocate an ifindex
  4201. * @net: the applicable net namespace
  4202. *
  4203. * Returns a suitable unique value for a new device interface
  4204. * number. The caller must hold the rtnl semaphore or the
  4205. * dev_base_lock to be sure it remains unique.
  4206. */
  4207. static int dev_new_index(struct net *net)
  4208. {
  4209. static int ifindex;
  4210. for (;;) {
  4211. if (++ifindex <= 0)
  4212. ifindex = 1;
  4213. if (!__dev_get_by_index(net, ifindex))
  4214. return ifindex;
  4215. }
  4216. }
  4217. /* Delayed registration/unregisteration */
  4218. static LIST_HEAD(net_todo_list);
  4219. static void net_set_todo(struct net_device *dev)
  4220. {
  4221. list_add_tail(&dev->todo_list, &net_todo_list);
  4222. }
  4223. static void rollback_registered_many(struct list_head *head)
  4224. {
  4225. struct net_device *dev, *tmp;
  4226. BUG_ON(dev_boot_phase);
  4227. ASSERT_RTNL();
  4228. list_for_each_entry_safe(dev, tmp, head, unreg_list) {
  4229. /* Some devices call without registering
  4230. * for initialization unwind. Remove those
  4231. * devices and proceed with the remaining.
  4232. */
  4233. if (dev->reg_state == NETREG_UNINITIALIZED) {
  4234. pr_debug("unregister_netdevice: device %s/%p never "
  4235. "was registered\n", dev->name, dev);
  4236. WARN_ON(1);
  4237. list_del(&dev->unreg_list);
  4238. continue;
  4239. }
  4240. BUG_ON(dev->reg_state != NETREG_REGISTERED);
  4241. /* If device is running, close it first. */
  4242. dev_close(dev);
  4243. /* And unlink it from device chain. */
  4244. unlist_netdevice(dev);
  4245. dev->reg_state = NETREG_UNREGISTERING;
  4246. }
  4247. synchronize_net();
  4248. list_for_each_entry(dev, head, unreg_list) {
  4249. /* Shutdown queueing discipline. */
  4250. dev_shutdown(dev);
  4251. /* Notify protocols, that we are about to destroy
  4252. this device. They should clean all the things.
  4253. */
  4254. call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
  4255. if (!dev->rtnl_link_ops ||
  4256. dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
  4257. rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
  4258. /*
  4259. * Flush the unicast and multicast chains
  4260. */
  4261. dev_uc_flush(dev);
  4262. dev_mc_flush(dev);
  4263. if (dev->netdev_ops->ndo_uninit)
  4264. dev->netdev_ops->ndo_uninit(dev);
  4265. /* Notifier chain MUST detach us from master device. */
  4266. WARN_ON(dev->master);
  4267. /* Remove entries from kobject tree */
  4268. netdev_unregister_kobject(dev);
  4269. }
  4270. /* Process any work delayed until the end of the batch */
  4271. dev = list_first_entry(head, struct net_device, unreg_list);
  4272. call_netdevice_notifiers(NETDEV_UNREGISTER_BATCH, dev);
  4273. rcu_barrier();
  4274. list_for_each_entry(dev, head, unreg_list)
  4275. dev_put(dev);
  4276. }
  4277. static void rollback_registered(struct net_device *dev)
  4278. {
  4279. LIST_HEAD(single);
  4280. list_add(&dev->unreg_list, &single);
  4281. rollback_registered_many(&single);
  4282. }
  4283. unsigned long netdev_fix_features(unsigned long features, const char *name)
  4284. {
  4285. /* Fix illegal SG+CSUM combinations. */
  4286. if ((features & NETIF_F_SG) &&
  4287. !(features & NETIF_F_ALL_CSUM)) {
  4288. if (name)
  4289. printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no "
  4290. "checksum feature.\n", name);
  4291. features &= ~NETIF_F_SG;
  4292. }
  4293. /* TSO requires that SG is present as well. */
  4294. if ((features & NETIF_F_TSO) && !(features & NETIF_F_SG)) {
  4295. if (name)
  4296. printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no "
  4297. "SG feature.\n", name);
  4298. features &= ~NETIF_F_TSO;
  4299. }
  4300. if (features & NETIF_F_UFO) {
  4301. /* maybe split UFO into V4 and V6? */
  4302. if (!((features & NETIF_F_GEN_CSUM) ||
  4303. (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
  4304. == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
  4305. if (name)
  4306. printk(KERN_ERR "%s: Dropping NETIF_F_UFO "
  4307. "since no checksum offload features.\n",
  4308. name);
  4309. features &= ~NETIF_F_UFO;
  4310. }
  4311. if (!(features & NETIF_F_SG)) {
  4312. if (name)
  4313. printk(KERN_ERR "%s: Dropping NETIF_F_UFO "
  4314. "since no NETIF_F_SG feature.\n", name);
  4315. features &= ~NETIF_F_UFO;
  4316. }
  4317. }
  4318. return features;
  4319. }
  4320. EXPORT_SYMBOL(netdev_fix_features);
  4321. /**
  4322. * netif_stacked_transfer_operstate - transfer operstate
  4323. * @rootdev: the root or lower level device to transfer state from
  4324. * @dev: the device to transfer operstate to
  4325. *
  4326. * Transfer operational state from root to device. This is normally
  4327. * called when a stacking relationship exists between the root
  4328. * device and the device(a leaf device).
  4329. */
  4330. void netif_stacked_transfer_operstate(const struct net_device *rootdev,
  4331. struct net_device *dev)
  4332. {
  4333. if (rootdev->operstate == IF_OPER_DORMANT)
  4334. netif_dormant_on(dev);
  4335. else
  4336. netif_dormant_off(dev);
  4337. if (netif_carrier_ok(rootdev)) {
  4338. if (!netif_carrier_ok(dev))
  4339. netif_carrier_on(dev);
  4340. } else {
  4341. if (netif_carrier_ok(dev))
  4342. netif_carrier_off(dev);
  4343. }
  4344. }
  4345. EXPORT_SYMBOL(netif_stacked_transfer_operstate);
  4346. #ifdef CONFIG_RPS
  4347. static int netif_alloc_rx_queues(struct net_device *dev)
  4348. {
  4349. unsigned int i, count = dev->num_rx_queues;
  4350. struct netdev_rx_queue *rx;
  4351. BUG_ON(count < 1);
  4352. rx = kcalloc(count, sizeof(struct netdev_rx_queue), GFP_KERNEL);
  4353. if (!rx) {
  4354. pr_err("netdev: Unable to allocate %u rx queues.\n", count);
  4355. return -ENOMEM;
  4356. }
  4357. dev->_rx = rx;
  4358. for (i = 0; i < count; i++)
  4359. rx[i].dev = dev;
  4360. return 0;
  4361. }
  4362. #endif
  4363. static void netdev_init_one_queue(struct net_device *dev,
  4364. struct netdev_queue *queue, void *_unused)
  4365. {
  4366. /* Initialize queue lock */
  4367. spin_lock_init(&queue->_xmit_lock);
  4368. netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
  4369. queue->xmit_lock_owner = -1;
  4370. netdev_queue_numa_node_write(queue, -1);
  4371. queue->dev = dev;
  4372. }
  4373. static int netif_alloc_netdev_queues(struct net_device *dev)
  4374. {
  4375. unsigned int count = dev->num_tx_queues;
  4376. struct netdev_queue *tx;
  4377. BUG_ON(count < 1);
  4378. tx = kcalloc(count, sizeof(struct netdev_queue), GFP_KERNEL);
  4379. if (!tx) {
  4380. pr_err("netdev: Unable to allocate %u tx queues.\n",
  4381. count);
  4382. return -ENOMEM;
  4383. }
  4384. dev->_tx = tx;
  4385. netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
  4386. spin_lock_init(&dev->tx_global_lock);
  4387. return 0;
  4388. }
  4389. /**
  4390. * register_netdevice - register a network device
  4391. * @dev: device to register
  4392. *
  4393. * Take a completed network device structure and add it to the kernel
  4394. * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
  4395. * chain. 0 is returned on success. A negative errno code is returned
  4396. * on a failure to set up the device, or if the name is a duplicate.
  4397. *
  4398. * Callers must hold the rtnl semaphore. You may want
  4399. * register_netdev() instead of this.
  4400. *
  4401. * BUGS:
  4402. * The locking appears insufficient to guarantee two parallel registers
  4403. * will not get the same name.
  4404. */
  4405. int register_netdevice(struct net_device *dev)
  4406. {
  4407. int ret;
  4408. struct net *net = dev_net(dev);
  4409. BUG_ON(dev_boot_phase);
  4410. ASSERT_RTNL();
  4411. might_sleep();
  4412. /* When net_device's are persistent, this will be fatal. */
  4413. BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
  4414. BUG_ON(!net);
  4415. spin_lock_init(&dev->addr_list_lock);
  4416. netdev_set_addr_lockdep_class(dev);
  4417. dev->iflink = -1;
  4418. /* Init, if this function is available */
  4419. if (dev->netdev_ops->ndo_init) {
  4420. ret = dev->netdev_ops->ndo_init(dev);
  4421. if (ret) {
  4422. if (ret > 0)
  4423. ret = -EIO;
  4424. goto out;
  4425. }
  4426. }
  4427. ret = dev_get_valid_name(dev, dev->name, 0);
  4428. if (ret)
  4429. goto err_uninit;
  4430. dev->ifindex = dev_new_index(net);
  4431. if (dev->iflink == -1)
  4432. dev->iflink = dev->ifindex;
  4433. /* Fix illegal checksum combinations */
  4434. if ((dev->features & NETIF_F_HW_CSUM) &&
  4435. (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
  4436. printk(KERN_NOTICE "%s: mixed HW and IP checksum settings.\n",
  4437. dev->name);
  4438. dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
  4439. }
  4440. if ((dev->features & NETIF_F_NO_CSUM) &&
  4441. (dev->features & (NETIF_F_HW_CSUM|NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
  4442. printk(KERN_NOTICE "%s: mixed no checksumming and other settings.\n",
  4443. dev->name);
  4444. dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM|NETIF_F_HW_CSUM);
  4445. }
  4446. dev->features = netdev_fix_features(dev->features, dev->name);
  4447. /* Enable software GSO if SG is supported. */
  4448. if (dev->features & NETIF_F_SG)
  4449. dev->features |= NETIF_F_GSO;
  4450. /* Enable GRO and NETIF_F_HIGHDMA for vlans by default,
  4451. * vlan_dev_init() will do the dev->features check, so these features
  4452. * are enabled only if supported by underlying device.
  4453. */
  4454. dev->vlan_features |= (NETIF_F_GRO | NETIF_F_HIGHDMA);
  4455. ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
  4456. ret = notifier_to_errno(ret);
  4457. if (ret)
  4458. goto err_uninit;
  4459. ret = netdev_register_kobject(dev);
  4460. if (ret)
  4461. goto err_uninit;
  4462. dev->reg_state = NETREG_REGISTERED;
  4463. /*
  4464. * Default initial state at registry is that the
  4465. * device is present.
  4466. */
  4467. set_bit(__LINK_STATE_PRESENT, &dev->state);
  4468. dev_init_scheduler(dev);
  4469. dev_hold(dev);
  4470. list_netdevice(dev);
  4471. /* Notify protocols, that a new device appeared. */
  4472. ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
  4473. ret = notifier_to_errno(ret);
  4474. if (ret) {
  4475. rollback_registered(dev);
  4476. dev->reg_state = NETREG_UNREGISTERED;
  4477. }
  4478. /*
  4479. * Prevent userspace races by waiting until the network
  4480. * device is fully setup before sending notifications.
  4481. */
  4482. if (!dev->rtnl_link_ops ||
  4483. dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
  4484. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
  4485. out:
  4486. return ret;
  4487. err_uninit:
  4488. if (dev->netdev_ops->ndo_uninit)
  4489. dev->netdev_ops->ndo_uninit(dev);
  4490. goto out;
  4491. }
  4492. EXPORT_SYMBOL(register_netdevice);
  4493. /**
  4494. * init_dummy_netdev - init a dummy network device for NAPI
  4495. * @dev: device to init
  4496. *
  4497. * This takes a network device structure and initialize the minimum
  4498. * amount of fields so it can be used to schedule NAPI polls without
  4499. * registering a full blown interface. This is to be used by drivers
  4500. * that need to tie several hardware interfaces to a single NAPI
  4501. * poll scheduler due to HW limitations.
  4502. */
  4503. int init_dummy_netdev(struct net_device *dev)
  4504. {
  4505. /* Clear everything. Note we don't initialize spinlocks
  4506. * are they aren't supposed to be taken by any of the
  4507. * NAPI code and this dummy netdev is supposed to be
  4508. * only ever used for NAPI polls
  4509. */
  4510. memset(dev, 0, sizeof(struct net_device));
  4511. /* make sure we BUG if trying to hit standard
  4512. * register/unregister code path
  4513. */
  4514. dev->reg_state = NETREG_DUMMY;
  4515. /* NAPI wants this */
  4516. INIT_LIST_HEAD(&dev->napi_list);
  4517. /* a dummy interface is started by default */
  4518. set_bit(__LINK_STATE_PRESENT, &dev->state);
  4519. set_bit(__LINK_STATE_START, &dev->state);
  4520. /* Note : We dont allocate pcpu_refcnt for dummy devices,
  4521. * because users of this 'device' dont need to change
  4522. * its refcount.
  4523. */
  4524. return 0;
  4525. }
  4526. EXPORT_SYMBOL_GPL(init_dummy_netdev);
  4527. /**
  4528. * register_netdev - register a network device
  4529. * @dev: device to register
  4530. *
  4531. * Take a completed network device structure and add it to the kernel
  4532. * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
  4533. * chain. 0 is returned on success. A negative errno code is returned
  4534. * on a failure to set up the device, or if the name is a duplicate.
  4535. *
  4536. * This is a wrapper around register_netdevice that takes the rtnl semaphore
  4537. * and expands the device name if you passed a format string to
  4538. * alloc_netdev.
  4539. */
  4540. int register_netdev(struct net_device *dev)
  4541. {
  4542. int err;
  4543. rtnl_lock();
  4544. /*
  4545. * If the name is a format string the caller wants us to do a
  4546. * name allocation.
  4547. */
  4548. if (strchr(dev->name, '%')) {
  4549. err = dev_alloc_name(dev, dev->name);
  4550. if (err < 0)
  4551. goto out;
  4552. }
  4553. err = register_netdevice(dev);
  4554. out:
  4555. rtnl_unlock();
  4556. return err;
  4557. }
  4558. EXPORT_SYMBOL(register_netdev);
  4559. int netdev_refcnt_read(const struct net_device *dev)
  4560. {
  4561. int i, refcnt = 0;
  4562. for_each_possible_cpu(i)
  4563. refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
  4564. return refcnt;
  4565. }
  4566. EXPORT_SYMBOL(netdev_refcnt_read);
  4567. /*
  4568. * netdev_wait_allrefs - wait until all references are gone.
  4569. *
  4570. * This is called when unregistering network devices.
  4571. *
  4572. * Any protocol or device that holds a reference should register
  4573. * for netdevice notification, and cleanup and put back the
  4574. * reference if they receive an UNREGISTER event.
  4575. * We can get stuck here if buggy protocols don't correctly
  4576. * call dev_put.
  4577. */
  4578. static void netdev_wait_allrefs(struct net_device *dev)
  4579. {
  4580. unsigned long rebroadcast_time, warning_time;
  4581. int refcnt;
  4582. linkwatch_forget_dev(dev);
  4583. rebroadcast_time = warning_time = jiffies;
  4584. refcnt = netdev_refcnt_read(dev);
  4585. while (refcnt != 0) {
  4586. if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
  4587. rtnl_lock();
  4588. /* Rebroadcast unregister notification */
  4589. call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
  4590. /* don't resend NETDEV_UNREGISTER_BATCH, _BATCH users
  4591. * should have already handle it the first time */
  4592. if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
  4593. &dev->state)) {
  4594. /* We must not have linkwatch events
  4595. * pending on unregister. If this
  4596. * happens, we simply run the queue
  4597. * unscheduled, resulting in a noop
  4598. * for this device.
  4599. */
  4600. linkwatch_run_queue();
  4601. }
  4602. __rtnl_unlock();
  4603. rebroadcast_time = jiffies;
  4604. }
  4605. msleep(250);
  4606. refcnt = netdev_refcnt_read(dev);
  4607. if (time_after(jiffies, warning_time + 10 * HZ)) {
  4608. printk(KERN_EMERG "unregister_netdevice: "
  4609. "waiting for %s to become free. Usage "
  4610. "count = %d\n",
  4611. dev->name, refcnt);
  4612. warning_time = jiffies;
  4613. }
  4614. }
  4615. }
  4616. /* The sequence is:
  4617. *
  4618. * rtnl_lock();
  4619. * ...
  4620. * register_netdevice(x1);
  4621. * register_netdevice(x2);
  4622. * ...
  4623. * unregister_netdevice(y1);
  4624. * unregister_netdevice(y2);
  4625. * ...
  4626. * rtnl_unlock();
  4627. * free_netdev(y1);
  4628. * free_netdev(y2);
  4629. *
  4630. * We are invoked by rtnl_unlock().
  4631. * This allows us to deal with problems:
  4632. * 1) We can delete sysfs objects which invoke hotplug
  4633. * without deadlocking with linkwatch via keventd.
  4634. * 2) Since we run with the RTNL semaphore not held, we can sleep
  4635. * safely in order to wait for the netdev refcnt to drop to zero.
  4636. *
  4637. * We must not return until all unregister events added during
  4638. * the interval the lock was held have been completed.
  4639. */
  4640. void netdev_run_todo(void)
  4641. {
  4642. struct list_head list;
  4643. /* Snapshot list, allow later requests */
  4644. list_replace_init(&net_todo_list, &list);
  4645. __rtnl_unlock();
  4646. while (!list_empty(&list)) {
  4647. struct net_device *dev
  4648. = list_first_entry(&list, struct net_device, todo_list);
  4649. list_del(&dev->todo_list);
  4650. if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
  4651. printk(KERN_ERR "network todo '%s' but state %d\n",
  4652. dev->name, dev->reg_state);
  4653. dump_stack();
  4654. continue;
  4655. }
  4656. dev->reg_state = NETREG_UNREGISTERED;
  4657. on_each_cpu(flush_backlog, dev, 1);
  4658. netdev_wait_allrefs(dev);
  4659. /* paranoia */
  4660. BUG_ON(netdev_refcnt_read(dev));
  4661. WARN_ON(rcu_dereference_raw(dev->ip_ptr));
  4662. WARN_ON(rcu_dereference_raw(dev->ip6_ptr));
  4663. WARN_ON(dev->dn_ptr);
  4664. if (dev->destructor)
  4665. dev->destructor(dev);
  4666. /* Free network device */
  4667. kobject_put(&dev->dev.kobj);
  4668. }
  4669. }
  4670. /**
  4671. * dev_txq_stats_fold - fold tx_queues stats
  4672. * @dev: device to get statistics from
  4673. * @stats: struct rtnl_link_stats64 to hold results
  4674. */
  4675. void dev_txq_stats_fold(const struct net_device *dev,
  4676. struct rtnl_link_stats64 *stats)
  4677. {
  4678. u64 tx_bytes = 0, tx_packets = 0, tx_dropped = 0;
  4679. unsigned int i;
  4680. struct netdev_queue *txq;
  4681. for (i = 0; i < dev->num_tx_queues; i++) {
  4682. txq = netdev_get_tx_queue(dev, i);
  4683. spin_lock_bh(&txq->_xmit_lock);
  4684. tx_bytes += txq->tx_bytes;
  4685. tx_packets += txq->tx_packets;
  4686. tx_dropped += txq->tx_dropped;
  4687. spin_unlock_bh(&txq->_xmit_lock);
  4688. }
  4689. if (tx_bytes || tx_packets || tx_dropped) {
  4690. stats->tx_bytes = tx_bytes;
  4691. stats->tx_packets = tx_packets;
  4692. stats->tx_dropped = tx_dropped;
  4693. }
  4694. }
  4695. EXPORT_SYMBOL(dev_txq_stats_fold);
  4696. /* Convert net_device_stats to rtnl_link_stats64. They have the same
  4697. * fields in the same order, with only the type differing.
  4698. */
  4699. static void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
  4700. const struct net_device_stats *netdev_stats)
  4701. {
  4702. #if BITS_PER_LONG == 64
  4703. BUILD_BUG_ON(sizeof(*stats64) != sizeof(*netdev_stats));
  4704. memcpy(stats64, netdev_stats, sizeof(*stats64));
  4705. #else
  4706. size_t i, n = sizeof(*stats64) / sizeof(u64);
  4707. const unsigned long *src = (const unsigned long *)netdev_stats;
  4708. u64 *dst = (u64 *)stats64;
  4709. BUILD_BUG_ON(sizeof(*netdev_stats) / sizeof(unsigned long) !=
  4710. sizeof(*stats64) / sizeof(u64));
  4711. for (i = 0; i < n; i++)
  4712. dst[i] = src[i];
  4713. #endif
  4714. }
  4715. /**
  4716. * dev_get_stats - get network device statistics
  4717. * @dev: device to get statistics from
  4718. * @storage: place to store stats
  4719. *
  4720. * Get network statistics from device. Return @storage.
  4721. * The device driver may provide its own method by setting
  4722. * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
  4723. * otherwise the internal statistics structure is used.
  4724. */
  4725. struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
  4726. struct rtnl_link_stats64 *storage)
  4727. {
  4728. const struct net_device_ops *ops = dev->netdev_ops;
  4729. if (ops->ndo_get_stats64) {
  4730. memset(storage, 0, sizeof(*storage));
  4731. ops->ndo_get_stats64(dev, storage);
  4732. } else if (ops->ndo_get_stats) {
  4733. netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
  4734. } else {
  4735. netdev_stats_to_stats64(storage, &dev->stats);
  4736. dev_txq_stats_fold(dev, storage);
  4737. }
  4738. storage->rx_dropped += atomic_long_read(&dev->rx_dropped);
  4739. return storage;
  4740. }
  4741. EXPORT_SYMBOL(dev_get_stats);
  4742. struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
  4743. {
  4744. struct netdev_queue *queue = dev_ingress_queue(dev);
  4745. #ifdef CONFIG_NET_CLS_ACT
  4746. if (queue)
  4747. return queue;
  4748. queue = kzalloc(sizeof(*queue), GFP_KERNEL);
  4749. if (!queue)
  4750. return NULL;
  4751. netdev_init_one_queue(dev, queue, NULL);
  4752. queue->qdisc = &noop_qdisc;
  4753. queue->qdisc_sleeping = &noop_qdisc;
  4754. rcu_assign_pointer(dev->ingress_queue, queue);
  4755. #endif
  4756. return queue;
  4757. }
  4758. /**
  4759. * alloc_netdev_mq - allocate network device
  4760. * @sizeof_priv: size of private data to allocate space for
  4761. * @name: device name format string
  4762. * @setup: callback to initialize device
  4763. * @queue_count: the number of subqueues to allocate
  4764. *
  4765. * Allocates a struct net_device with private data area for driver use
  4766. * and performs basic initialization. Also allocates subquue structs
  4767. * for each queue on the device at the end of the netdevice.
  4768. */
  4769. struct net_device *alloc_netdev_mq(int sizeof_priv, const char *name,
  4770. void (*setup)(struct net_device *), unsigned int queue_count)
  4771. {
  4772. struct net_device *dev;
  4773. size_t alloc_size;
  4774. struct net_device *p;
  4775. BUG_ON(strlen(name) >= sizeof(dev->name));
  4776. if (queue_count < 1) {
  4777. pr_err("alloc_netdev: Unable to allocate device "
  4778. "with zero queues.\n");
  4779. return NULL;
  4780. }
  4781. alloc_size = sizeof(struct net_device);
  4782. if (sizeof_priv) {
  4783. /* ensure 32-byte alignment of private area */
  4784. alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
  4785. alloc_size += sizeof_priv;
  4786. }
  4787. /* ensure 32-byte alignment of whole construct */
  4788. alloc_size += NETDEV_ALIGN - 1;
  4789. p = kzalloc(alloc_size, GFP_KERNEL);
  4790. if (!p) {
  4791. printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
  4792. return NULL;
  4793. }
  4794. dev = PTR_ALIGN(p, NETDEV_ALIGN);
  4795. dev->padded = (char *)dev - (char *)p;
  4796. dev->pcpu_refcnt = alloc_percpu(int);
  4797. if (!dev->pcpu_refcnt)
  4798. goto free_p;
  4799. if (dev_addr_init(dev))
  4800. goto free_pcpu;
  4801. dev_mc_init(dev);
  4802. dev_uc_init(dev);
  4803. dev_net_set(dev, &init_net);
  4804. dev->num_tx_queues = queue_count;
  4805. dev->real_num_tx_queues = queue_count;
  4806. if (netif_alloc_netdev_queues(dev))
  4807. goto free_pcpu;
  4808. #ifdef CONFIG_RPS
  4809. dev->num_rx_queues = queue_count;
  4810. dev->real_num_rx_queues = queue_count;
  4811. if (netif_alloc_rx_queues(dev))
  4812. goto free_pcpu;
  4813. #endif
  4814. dev->gso_max_size = GSO_MAX_SIZE;
  4815. INIT_LIST_HEAD(&dev->ethtool_ntuple_list.list);
  4816. dev->ethtool_ntuple_list.count = 0;
  4817. INIT_LIST_HEAD(&dev->napi_list);
  4818. INIT_LIST_HEAD(&dev->unreg_list);
  4819. INIT_LIST_HEAD(&dev->link_watch_list);
  4820. dev->priv_flags = IFF_XMIT_DST_RELEASE;
  4821. setup(dev);
  4822. strcpy(dev->name, name);
  4823. return dev;
  4824. free_pcpu:
  4825. free_percpu(dev->pcpu_refcnt);
  4826. kfree(dev->_tx);
  4827. #ifdef CONFIG_RPS
  4828. kfree(dev->_rx);
  4829. #endif
  4830. free_p:
  4831. kfree(p);
  4832. return NULL;
  4833. }
  4834. EXPORT_SYMBOL(alloc_netdev_mq);
  4835. /**
  4836. * free_netdev - free network device
  4837. * @dev: device
  4838. *
  4839. * This function does the last stage of destroying an allocated device
  4840. * interface. The reference to the device object is released.
  4841. * If this is the last reference then it will be freed.
  4842. */
  4843. void free_netdev(struct net_device *dev)
  4844. {
  4845. struct napi_struct *p, *n;
  4846. release_net(dev_net(dev));
  4847. kfree(dev->_tx);
  4848. #ifdef CONFIG_RPS
  4849. kfree(dev->_rx);
  4850. #endif
  4851. kfree(rcu_dereference_raw(dev->ingress_queue));
  4852. /* Flush device addresses */
  4853. dev_addr_flush(dev);
  4854. /* Clear ethtool n-tuple list */
  4855. ethtool_ntuple_flush(dev);
  4856. list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
  4857. netif_napi_del(p);
  4858. free_percpu(dev->pcpu_refcnt);
  4859. dev->pcpu_refcnt = NULL;
  4860. /* Compatibility with error handling in drivers */
  4861. if (dev->reg_state == NETREG_UNINITIALIZED) {
  4862. kfree((char *)dev - dev->padded);
  4863. return;
  4864. }
  4865. BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
  4866. dev->reg_state = NETREG_RELEASED;
  4867. /* will free via device release */
  4868. put_device(&dev->dev);
  4869. }
  4870. EXPORT_SYMBOL(free_netdev);
  4871. /**
  4872. * synchronize_net - Synchronize with packet receive processing
  4873. *
  4874. * Wait for packets currently being received to be done.
  4875. * Does not block later packets from starting.
  4876. */
  4877. void synchronize_net(void)
  4878. {
  4879. might_sleep();
  4880. synchronize_rcu();
  4881. }
  4882. EXPORT_SYMBOL(synchronize_net);
  4883. /**
  4884. * unregister_netdevice_queue - remove device from the kernel
  4885. * @dev: device
  4886. * @head: list
  4887. *
  4888. * This function shuts down a device interface and removes it
  4889. * from the kernel tables.
  4890. * If head not NULL, device is queued to be unregistered later.
  4891. *
  4892. * Callers must hold the rtnl semaphore. You may want
  4893. * unregister_netdev() instead of this.
  4894. */
  4895. void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
  4896. {
  4897. ASSERT_RTNL();
  4898. if (head) {
  4899. list_move_tail(&dev->unreg_list, head);
  4900. } else {
  4901. rollback_registered(dev);
  4902. /* Finish processing unregister after unlock */
  4903. net_set_todo(dev);
  4904. }
  4905. }
  4906. EXPORT_SYMBOL(unregister_netdevice_queue);
  4907. /**
  4908. * unregister_netdevice_many - unregister many devices
  4909. * @head: list of devices
  4910. */
  4911. void unregister_netdevice_many(struct list_head *head)
  4912. {
  4913. struct net_device *dev;
  4914. if (!list_empty(head)) {
  4915. rollback_registered_many(head);
  4916. list_for_each_entry(dev, head, unreg_list)
  4917. net_set_todo(dev);
  4918. }
  4919. }
  4920. EXPORT_SYMBOL(unregister_netdevice_many);
  4921. /**
  4922. * unregister_netdev - remove device from the kernel
  4923. * @dev: device
  4924. *
  4925. * This function shuts down a device interface and removes it
  4926. * from the kernel tables.
  4927. *
  4928. * This is just a wrapper for unregister_netdevice that takes
  4929. * the rtnl semaphore. In general you want to use this and not
  4930. * unregister_netdevice.
  4931. */
  4932. void unregister_netdev(struct net_device *dev)
  4933. {
  4934. rtnl_lock();
  4935. unregister_netdevice(dev);
  4936. rtnl_unlock();
  4937. }
  4938. EXPORT_SYMBOL(unregister_netdev);
  4939. /**
  4940. * dev_change_net_namespace - move device to different nethost namespace
  4941. * @dev: device
  4942. * @net: network namespace
  4943. * @pat: If not NULL name pattern to try if the current device name
  4944. * is already taken in the destination network namespace.
  4945. *
  4946. * This function shuts down a device interface and moves it
  4947. * to a new network namespace. On success 0 is returned, on
  4948. * a failure a netagive errno code is returned.
  4949. *
  4950. * Callers must hold the rtnl semaphore.
  4951. */
  4952. int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
  4953. {
  4954. int err;
  4955. ASSERT_RTNL();
  4956. /* Don't allow namespace local devices to be moved. */
  4957. err = -EINVAL;
  4958. if (dev->features & NETIF_F_NETNS_LOCAL)
  4959. goto out;
  4960. /* Ensure the device has been registrered */
  4961. err = -EINVAL;
  4962. if (dev->reg_state != NETREG_REGISTERED)
  4963. goto out;
  4964. /* Get out if there is nothing todo */
  4965. err = 0;
  4966. if (net_eq(dev_net(dev), net))
  4967. goto out;
  4968. /* Pick the destination device name, and ensure
  4969. * we can use it in the destination network namespace.
  4970. */
  4971. err = -EEXIST;
  4972. if (__dev_get_by_name(net, dev->name)) {
  4973. /* We get here if we can't use the current device name */
  4974. if (!pat)
  4975. goto out;
  4976. if (dev_get_valid_name(dev, pat, 1))
  4977. goto out;
  4978. }
  4979. /*
  4980. * And now a mini version of register_netdevice unregister_netdevice.
  4981. */
  4982. /* If device is running close it first. */
  4983. dev_close(dev);
  4984. /* And unlink it from device chain */
  4985. err = -ENODEV;
  4986. unlist_netdevice(dev);
  4987. synchronize_net();
  4988. /* Shutdown queueing discipline. */
  4989. dev_shutdown(dev);
  4990. /* Notify protocols, that we are about to destroy
  4991. this device. They should clean all the things.
  4992. Note that dev->reg_state stays at NETREG_REGISTERED.
  4993. This is wanted because this way 8021q and macvlan know
  4994. the device is just moving and can keep their slaves up.
  4995. */
  4996. call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
  4997. call_netdevice_notifiers(NETDEV_UNREGISTER_BATCH, dev);
  4998. /*
  4999. * Flush the unicast and multicast chains
  5000. */
  5001. dev_uc_flush(dev);
  5002. dev_mc_flush(dev);
  5003. /* Actually switch the network namespace */
  5004. dev_net_set(dev, net);
  5005. /* If there is an ifindex conflict assign a new one */
  5006. if (__dev_get_by_index(net, dev->ifindex)) {
  5007. int iflink = (dev->iflink == dev->ifindex);
  5008. dev->ifindex = dev_new_index(net);
  5009. if (iflink)
  5010. dev->iflink = dev->ifindex;
  5011. }
  5012. /* Fixup kobjects */
  5013. err = device_rename(&dev->dev, dev->name);
  5014. WARN_ON(err);
  5015. /* Add the device back in the hashes */
  5016. list_netdevice(dev);
  5017. /* Notify protocols, that a new device appeared. */
  5018. call_netdevice_notifiers(NETDEV_REGISTER, dev);
  5019. /*
  5020. * Prevent userspace races by waiting until the network
  5021. * device is fully setup before sending notifications.
  5022. */
  5023. rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
  5024. synchronize_net();
  5025. err = 0;
  5026. out:
  5027. return err;
  5028. }
  5029. EXPORT_SYMBOL_GPL(dev_change_net_namespace);
  5030. static int dev_cpu_callback(struct notifier_block *nfb,
  5031. unsigned long action,
  5032. void *ocpu)
  5033. {
  5034. struct sk_buff **list_skb;
  5035. struct sk_buff *skb;
  5036. unsigned int cpu, oldcpu = (unsigned long)ocpu;
  5037. struct softnet_data *sd, *oldsd;
  5038. if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
  5039. return NOTIFY_OK;
  5040. local_irq_disable();
  5041. cpu = smp_processor_id();
  5042. sd = &per_cpu(softnet_data, cpu);
  5043. oldsd = &per_cpu(softnet_data, oldcpu);
  5044. /* Find end of our completion_queue. */
  5045. list_skb = &sd->completion_queue;
  5046. while (*list_skb)
  5047. list_skb = &(*list_skb)->next;
  5048. /* Append completion queue from offline CPU. */
  5049. *list_skb = oldsd->completion_queue;
  5050. oldsd->completion_queue = NULL;
  5051. /* Append output queue from offline CPU. */
  5052. if (oldsd->output_queue) {
  5053. *sd->output_queue_tailp = oldsd->output_queue;
  5054. sd->output_queue_tailp = oldsd->output_queue_tailp;
  5055. oldsd->output_queue = NULL;
  5056. oldsd->output_queue_tailp = &oldsd->output_queue;
  5057. }
  5058. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  5059. local_irq_enable();
  5060. /* Process offline CPU's input_pkt_queue */
  5061. while ((skb = __skb_dequeue(&oldsd->process_queue))) {
  5062. netif_rx(skb);
  5063. input_queue_head_incr(oldsd);
  5064. }
  5065. while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
  5066. netif_rx(skb);
  5067. input_queue_head_incr(oldsd);
  5068. }
  5069. return NOTIFY_OK;
  5070. }
  5071. /**
  5072. * netdev_increment_features - increment feature set by one
  5073. * @all: current feature set
  5074. * @one: new feature set
  5075. * @mask: mask feature set
  5076. *
  5077. * Computes a new feature set after adding a device with feature set
  5078. * @one to the master device with current feature set @all. Will not
  5079. * enable anything that is off in @mask. Returns the new feature set.
  5080. */
  5081. unsigned long netdev_increment_features(unsigned long all, unsigned long one,
  5082. unsigned long mask)
  5083. {
  5084. /* If device needs checksumming, downgrade to it. */
  5085. if (all & NETIF_F_NO_CSUM && !(one & NETIF_F_NO_CSUM))
  5086. all ^= NETIF_F_NO_CSUM | (one & NETIF_F_ALL_CSUM);
  5087. else if (mask & NETIF_F_ALL_CSUM) {
  5088. /* If one device supports v4/v6 checksumming, set for all. */
  5089. if (one & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM) &&
  5090. !(all & NETIF_F_GEN_CSUM)) {
  5091. all &= ~NETIF_F_ALL_CSUM;
  5092. all |= one & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
  5093. }
  5094. /* If one device supports hw checksumming, set for all. */
  5095. if (one & NETIF_F_GEN_CSUM && !(all & NETIF_F_GEN_CSUM)) {
  5096. all &= ~NETIF_F_ALL_CSUM;
  5097. all |= NETIF_F_HW_CSUM;
  5098. }
  5099. }
  5100. one |= NETIF_F_ALL_CSUM;
  5101. one |= all & NETIF_F_ONE_FOR_ALL;
  5102. all &= one | NETIF_F_LLTX | NETIF_F_GSO | NETIF_F_UFO;
  5103. all |= one & mask & NETIF_F_ONE_FOR_ALL;
  5104. return all;
  5105. }
  5106. EXPORT_SYMBOL(netdev_increment_features);
  5107. static struct hlist_head *netdev_create_hash(void)
  5108. {
  5109. int i;
  5110. struct hlist_head *hash;
  5111. hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
  5112. if (hash != NULL)
  5113. for (i = 0; i < NETDEV_HASHENTRIES; i++)
  5114. INIT_HLIST_HEAD(&hash[i]);
  5115. return hash;
  5116. }
  5117. /* Initialize per network namespace state */
  5118. static int __net_init netdev_init(struct net *net)
  5119. {
  5120. INIT_LIST_HEAD(&net->dev_base_head);
  5121. net->dev_name_head = netdev_create_hash();
  5122. if (net->dev_name_head == NULL)
  5123. goto err_name;
  5124. net->dev_index_head = netdev_create_hash();
  5125. if (net->dev_index_head == NULL)
  5126. goto err_idx;
  5127. return 0;
  5128. err_idx:
  5129. kfree(net->dev_name_head);
  5130. err_name:
  5131. return -ENOMEM;
  5132. }
  5133. /**
  5134. * netdev_drivername - network driver for the device
  5135. * @dev: network device
  5136. * @buffer: buffer for resulting name
  5137. * @len: size of buffer
  5138. *
  5139. * Determine network driver for device.
  5140. */
  5141. char *netdev_drivername(const struct net_device *dev, char *buffer, int len)
  5142. {
  5143. const struct device_driver *driver;
  5144. const struct device *parent;
  5145. if (len <= 0 || !buffer)
  5146. return buffer;
  5147. buffer[0] = 0;
  5148. parent = dev->dev.parent;
  5149. if (!parent)
  5150. return buffer;
  5151. driver = parent->driver;
  5152. if (driver && driver->name)
  5153. strlcpy(buffer, driver->name, len);
  5154. return buffer;
  5155. }
  5156. static int __netdev_printk(const char *level, const struct net_device *dev,
  5157. struct va_format *vaf)
  5158. {
  5159. int r;
  5160. if (dev && dev->dev.parent)
  5161. r = dev_printk(level, dev->dev.parent, "%s: %pV",
  5162. netdev_name(dev), vaf);
  5163. else if (dev)
  5164. r = printk("%s%s: %pV", level, netdev_name(dev), vaf);
  5165. else
  5166. r = printk("%s(NULL net_device): %pV", level, vaf);
  5167. return r;
  5168. }
  5169. int netdev_printk(const char *level, const struct net_device *dev,
  5170. const char *format, ...)
  5171. {
  5172. struct va_format vaf;
  5173. va_list args;
  5174. int r;
  5175. va_start(args, format);
  5176. vaf.fmt = format;
  5177. vaf.va = &args;
  5178. r = __netdev_printk(level, dev, &vaf);
  5179. va_end(args);
  5180. return r;
  5181. }
  5182. EXPORT_SYMBOL(netdev_printk);
  5183. #define define_netdev_printk_level(func, level) \
  5184. int func(const struct net_device *dev, const char *fmt, ...) \
  5185. { \
  5186. int r; \
  5187. struct va_format vaf; \
  5188. va_list args; \
  5189. \
  5190. va_start(args, fmt); \
  5191. \
  5192. vaf.fmt = fmt; \
  5193. vaf.va = &args; \
  5194. \
  5195. r = __netdev_printk(level, dev, &vaf); \
  5196. va_end(args); \
  5197. \
  5198. return r; \
  5199. } \
  5200. EXPORT_SYMBOL(func);
  5201. define_netdev_printk_level(netdev_emerg, KERN_EMERG);
  5202. define_netdev_printk_level(netdev_alert, KERN_ALERT);
  5203. define_netdev_printk_level(netdev_crit, KERN_CRIT);
  5204. define_netdev_printk_level(netdev_err, KERN_ERR);
  5205. define_netdev_printk_level(netdev_warn, KERN_WARNING);
  5206. define_netdev_printk_level(netdev_notice, KERN_NOTICE);
  5207. define_netdev_printk_level(netdev_info, KERN_INFO);
  5208. static void __net_exit netdev_exit(struct net *net)
  5209. {
  5210. kfree(net->dev_name_head);
  5211. kfree(net->dev_index_head);
  5212. }
  5213. static struct pernet_operations __net_initdata netdev_net_ops = {
  5214. .init = netdev_init,
  5215. .exit = netdev_exit,
  5216. };
  5217. static void __net_exit default_device_exit(struct net *net)
  5218. {
  5219. struct net_device *dev, *aux;
  5220. /*
  5221. * Push all migratable network devices back to the
  5222. * initial network namespace
  5223. */
  5224. rtnl_lock();
  5225. for_each_netdev_safe(net, dev, aux) {
  5226. int err;
  5227. char fb_name[IFNAMSIZ];
  5228. /* Ignore unmoveable devices (i.e. loopback) */
  5229. if (dev->features & NETIF_F_NETNS_LOCAL)
  5230. continue;
  5231. /* Leave virtual devices for the generic cleanup */
  5232. if (dev->rtnl_link_ops)
  5233. continue;
  5234. /* Push remaing network devices to init_net */
  5235. snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
  5236. err = dev_change_net_namespace(dev, &init_net, fb_name);
  5237. if (err) {
  5238. printk(KERN_EMERG "%s: failed to move %s to init_net: %d\n",
  5239. __func__, dev->name, err);
  5240. BUG();
  5241. }
  5242. }
  5243. rtnl_unlock();
  5244. }
  5245. static void __net_exit default_device_exit_batch(struct list_head *net_list)
  5246. {
  5247. /* At exit all network devices most be removed from a network
  5248. * namespace. Do this in the reverse order of registeration.
  5249. * Do this across as many network namespaces as possible to
  5250. * improve batching efficiency.
  5251. */
  5252. struct net_device *dev;
  5253. struct net *net;
  5254. LIST_HEAD(dev_kill_list);
  5255. rtnl_lock();
  5256. list_for_each_entry(net, net_list, exit_list) {
  5257. for_each_netdev_reverse(net, dev) {
  5258. if (dev->rtnl_link_ops)
  5259. dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
  5260. else
  5261. unregister_netdevice_queue(dev, &dev_kill_list);
  5262. }
  5263. }
  5264. unregister_netdevice_many(&dev_kill_list);
  5265. rtnl_unlock();
  5266. }
  5267. static struct pernet_operations __net_initdata default_device_ops = {
  5268. .exit = default_device_exit,
  5269. .exit_batch = default_device_exit_batch,
  5270. };
  5271. /*
  5272. * Initialize the DEV module. At boot time this walks the device list and
  5273. * unhooks any devices that fail to initialise (normally hardware not
  5274. * present) and leaves us with a valid list of present and active devices.
  5275. *
  5276. */
  5277. /*
  5278. * This is called single threaded during boot, so no need
  5279. * to take the rtnl semaphore.
  5280. */
  5281. static int __init net_dev_init(void)
  5282. {
  5283. int i, rc = -ENOMEM;
  5284. BUG_ON(!dev_boot_phase);
  5285. if (dev_proc_init())
  5286. goto out;
  5287. if (netdev_kobject_init())
  5288. goto out;
  5289. INIT_LIST_HEAD(&ptype_all);
  5290. for (i = 0; i < PTYPE_HASH_SIZE; i++)
  5291. INIT_LIST_HEAD(&ptype_base[i]);
  5292. if (register_pernet_subsys(&netdev_net_ops))
  5293. goto out;
  5294. /*
  5295. * Initialise the packet receive queues.
  5296. */
  5297. for_each_possible_cpu(i) {
  5298. struct softnet_data *sd = &per_cpu(softnet_data, i);
  5299. memset(sd, 0, sizeof(*sd));
  5300. skb_queue_head_init(&sd->input_pkt_queue);
  5301. skb_queue_head_init(&sd->process_queue);
  5302. sd->completion_queue = NULL;
  5303. INIT_LIST_HEAD(&sd->poll_list);
  5304. sd->output_queue = NULL;
  5305. sd->output_queue_tailp = &sd->output_queue;
  5306. #ifdef CONFIG_RPS
  5307. sd->csd.func = rps_trigger_softirq;
  5308. sd->csd.info = sd;
  5309. sd->csd.flags = 0;
  5310. sd->cpu = i;
  5311. #endif
  5312. sd->backlog.poll = process_backlog;
  5313. sd->backlog.weight = weight_p;
  5314. sd->backlog.gro_list = NULL;
  5315. sd->backlog.gro_count = 0;
  5316. }
  5317. dev_boot_phase = 0;
  5318. /* The loopback device is special if any other network devices
  5319. * is present in a network namespace the loopback device must
  5320. * be present. Since we now dynamically allocate and free the
  5321. * loopback device ensure this invariant is maintained by
  5322. * keeping the loopback device as the first device on the
  5323. * list of network devices. Ensuring the loopback devices
  5324. * is the first device that appears and the last network device
  5325. * that disappears.
  5326. */
  5327. if (register_pernet_device(&loopback_net_ops))
  5328. goto out;
  5329. if (register_pernet_device(&default_device_ops))
  5330. goto out;
  5331. open_softirq(NET_TX_SOFTIRQ, net_tx_action);
  5332. open_softirq(NET_RX_SOFTIRQ, net_rx_action);
  5333. hotcpu_notifier(dev_cpu_callback, 0);
  5334. dst_init();
  5335. dev_mcast_init();
  5336. rc = 0;
  5337. out:
  5338. return rc;
  5339. }
  5340. subsys_initcall(net_dev_init);
  5341. static int __init initialize_hashrnd(void)
  5342. {
  5343. get_random_bytes(&hashrnd, sizeof(hashrnd));
  5344. return 0;
  5345. }
  5346. late_initcall_sync(initialize_hashrnd);