dev.c 160 KB

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