dev.c 136 KB

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