dev.c 128 KB

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