dev.c 119 KB

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