dev.c 128 KB

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