dev.c 145 KB

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