dev.c 136 KB

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