dev.c 104 KB

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