dev.c 162 KB

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