dev.c 108 KB

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