dev.c 143 KB

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