dev.c 147 KB

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