dev.c 107 KB

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