dev.c 86 KB

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