dev.c 143 KB

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