dev.c 159 KB

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