dev.c 175 KB

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