dev.c 138 KB

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