dev.c 96 KB

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