af_packet.c 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * PACKET - implements raw packet sockets.
  7. *
  8. * Authors: Ross Biro
  9. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10. * Alan Cox, <gw4pts@gw4pts.ampr.org>
  11. *
  12. * Fixes:
  13. * Alan Cox : verify_area() now used correctly
  14. * Alan Cox : new skbuff lists, look ma no backlogs!
  15. * Alan Cox : tidied skbuff lists.
  16. * Alan Cox : Now uses generic datagram routines I
  17. * added. Also fixed the peek/read crash
  18. * from all old Linux datagram code.
  19. * Alan Cox : Uses the improved datagram code.
  20. * Alan Cox : Added NULL's for socket options.
  21. * Alan Cox : Re-commented the code.
  22. * Alan Cox : Use new kernel side addressing
  23. * Rob Janssen : Correct MTU usage.
  24. * Dave Platt : Counter leaks caused by incorrect
  25. * interrupt locking and some slightly
  26. * dubious gcc output. Can you read
  27. * compiler: it said _VOLATILE_
  28. * Richard Kooijman : Timestamp fixes.
  29. * Alan Cox : New buffers. Use sk->mac.raw.
  30. * Alan Cox : sendmsg/recvmsg support.
  31. * Alan Cox : Protocol setting support
  32. * Alexey Kuznetsov : Untied from IPv4 stack.
  33. * Cyrus Durgin : Fixed kerneld for kmod.
  34. * Michal Ostrowski : Module initialization cleanup.
  35. * Ulises Alonso : Frame number limit removal and
  36. * packet_set_ring memory leak.
  37. * Eric Biederman : Allow for > 8 byte hardware addresses.
  38. * The convention is that longer addresses
  39. * will simply extend the hardware address
  40. * byte arrays at the end of sockaddr_ll
  41. * and packet_mreq.
  42. * Johann Baudy : Added TX RING.
  43. * Chetan Loke : Implemented TPACKET_V3 block abstraction
  44. * layer.
  45. * Copyright (C) 2011, <lokec@ccs.neu.edu>
  46. *
  47. *
  48. * This program is free software; you can redistribute it and/or
  49. * modify it under the terms of the GNU General Public License
  50. * as published by the Free Software Foundation; either version
  51. * 2 of the License, or (at your option) any later version.
  52. *
  53. */
  54. #include <linux/types.h>
  55. #include <linux/mm.h>
  56. #include <linux/capability.h>
  57. #include <linux/fcntl.h>
  58. #include <linux/socket.h>
  59. #include <linux/in.h>
  60. #include <linux/inet.h>
  61. #include <linux/netdevice.h>
  62. #include <linux/if_packet.h>
  63. #include <linux/wireless.h>
  64. #include <linux/kernel.h>
  65. #include <linux/kmod.h>
  66. #include <linux/slab.h>
  67. #include <linux/vmalloc.h>
  68. #include <net/net_namespace.h>
  69. #include <net/ip.h>
  70. #include <net/protocol.h>
  71. #include <linux/skbuff.h>
  72. #include <net/sock.h>
  73. #include <linux/errno.h>
  74. #include <linux/timer.h>
  75. #include <linux/uaccess.h>
  76. #include <asm/ioctls.h>
  77. #include <asm/page.h>
  78. #include <asm/cacheflush.h>
  79. #include <asm/io.h>
  80. #include <linux/proc_fs.h>
  81. #include <linux/seq_file.h>
  82. #include <linux/poll.h>
  83. #include <linux/module.h>
  84. #include <linux/init.h>
  85. #include <linux/mutex.h>
  86. #include <linux/if_vlan.h>
  87. #include <linux/virtio_net.h>
  88. #include <linux/errqueue.h>
  89. #include <linux/net_tstamp.h>
  90. #include <linux/percpu.h>
  91. #ifdef CONFIG_INET
  92. #include <net/inet_common.h>
  93. #endif
  94. #include <linux/bpf.h>
  95. #include <net/compat.h>
  96. #include "internal.h"
  97. /*
  98. Assumptions:
  99. - if device has no dev->hard_header routine, it adds and removes ll header
  100. inside itself. In this case ll header is invisible outside of device,
  101. but higher levels still should reserve dev->hard_header_len.
  102. Some devices are enough clever to reallocate skb, when header
  103. will not fit to reserved space (tunnel), another ones are silly
  104. (PPP).
  105. - packet socket receives packets with pulled ll header,
  106. so that SOCK_RAW should push it back.
  107. On receive:
  108. -----------
  109. Incoming, dev->hard_header!=NULL
  110. mac_header -> ll header
  111. data -> data
  112. Outgoing, dev->hard_header!=NULL
  113. mac_header -> ll header
  114. data -> ll header
  115. Incoming, dev->hard_header==NULL
  116. mac_header -> UNKNOWN position. It is very likely, that it points to ll
  117. header. PPP makes it, that is wrong, because introduce
  118. assymetry between rx and tx paths.
  119. data -> data
  120. Outgoing, dev->hard_header==NULL
  121. mac_header -> data. ll header is still not built!
  122. data -> data
  123. Resume
  124. If dev->hard_header==NULL we are unlikely to restore sensible ll header.
  125. On transmit:
  126. ------------
  127. dev->hard_header != NULL
  128. mac_header -> ll header
  129. data -> ll header
  130. dev->hard_header == NULL (ll header is added by device, we cannot control it)
  131. mac_header -> data
  132. data -> data
  133. We should set nh.raw on output to correct posistion,
  134. packet classifier depends on it.
  135. */
  136. /* Private packet socket structures. */
  137. /* identical to struct packet_mreq except it has
  138. * a longer address field.
  139. */
  140. struct packet_mreq_max {
  141. int mr_ifindex;
  142. unsigned short mr_type;
  143. unsigned short mr_alen;
  144. unsigned char mr_address[MAX_ADDR_LEN];
  145. };
  146. union tpacket_uhdr {
  147. struct tpacket_hdr *h1;
  148. struct tpacket2_hdr *h2;
  149. struct tpacket3_hdr *h3;
  150. void *raw;
  151. };
  152. static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
  153. int closing, int tx_ring);
  154. #define V3_ALIGNMENT (8)
  155. #define BLK_HDR_LEN (ALIGN(sizeof(struct tpacket_block_desc), V3_ALIGNMENT))
  156. #define BLK_PLUS_PRIV(sz_of_priv) \
  157. (BLK_HDR_LEN + ALIGN((sz_of_priv), V3_ALIGNMENT))
  158. #define BLOCK_STATUS(x) ((x)->hdr.bh1.block_status)
  159. #define BLOCK_NUM_PKTS(x) ((x)->hdr.bh1.num_pkts)
  160. #define BLOCK_O2FP(x) ((x)->hdr.bh1.offset_to_first_pkt)
  161. #define BLOCK_LEN(x) ((x)->hdr.bh1.blk_len)
  162. #define BLOCK_SNUM(x) ((x)->hdr.bh1.seq_num)
  163. #define BLOCK_O2PRIV(x) ((x)->offset_to_priv)
  164. #define BLOCK_PRIV(x) ((void *)((char *)(x) + BLOCK_O2PRIV(x)))
  165. struct packet_sock;
  166. static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
  167. struct packet_type *pt, struct net_device *orig_dev);
  168. static void *packet_previous_frame(struct packet_sock *po,
  169. struct packet_ring_buffer *rb,
  170. int status);
  171. static void packet_increment_head(struct packet_ring_buffer *buff);
  172. static int prb_curr_blk_in_use(struct tpacket_block_desc *);
  173. static void *prb_dispatch_next_block(struct tpacket_kbdq_core *,
  174. struct packet_sock *);
  175. static void prb_retire_current_block(struct tpacket_kbdq_core *,
  176. struct packet_sock *, unsigned int status);
  177. static int prb_queue_frozen(struct tpacket_kbdq_core *);
  178. static void prb_open_block(struct tpacket_kbdq_core *,
  179. struct tpacket_block_desc *);
  180. static void prb_retire_rx_blk_timer_expired(unsigned long);
  181. static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *);
  182. static void prb_init_blk_timer(struct packet_sock *,
  183. struct tpacket_kbdq_core *,
  184. void (*func) (unsigned long));
  185. static void prb_fill_rxhash(struct tpacket_kbdq_core *, struct tpacket3_hdr *);
  186. static void prb_clear_rxhash(struct tpacket_kbdq_core *,
  187. struct tpacket3_hdr *);
  188. static void prb_fill_vlan_info(struct tpacket_kbdq_core *,
  189. struct tpacket3_hdr *);
  190. static void packet_flush_mclist(struct sock *sk);
  191. static void packet_pick_tx_queue(struct net_device *dev, struct sk_buff *skb);
  192. struct packet_skb_cb {
  193. union {
  194. struct sockaddr_pkt pkt;
  195. union {
  196. /* Trick: alias skb original length with
  197. * ll.sll_family and ll.protocol in order
  198. * to save room.
  199. */
  200. unsigned int origlen;
  201. struct sockaddr_ll ll;
  202. };
  203. } sa;
  204. };
  205. #define vio_le() virtio_legacy_is_little_endian()
  206. #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
  207. #define GET_PBDQC_FROM_RB(x) ((struct tpacket_kbdq_core *)(&(x)->prb_bdqc))
  208. #define GET_PBLOCK_DESC(x, bid) \
  209. ((struct tpacket_block_desc *)((x)->pkbdq[(bid)].buffer))
  210. #define GET_CURR_PBLOCK_DESC_FROM_CORE(x) \
  211. ((struct tpacket_block_desc *)((x)->pkbdq[(x)->kactive_blk_num].buffer))
  212. #define GET_NEXT_PRB_BLK_NUM(x) \
  213. (((x)->kactive_blk_num < ((x)->knum_blocks-1)) ? \
  214. ((x)->kactive_blk_num+1) : 0)
  215. static void __fanout_unlink(struct sock *sk, struct packet_sock *po);
  216. static void __fanout_link(struct sock *sk, struct packet_sock *po);
  217. static int packet_direct_xmit(struct sk_buff *skb)
  218. {
  219. struct net_device *dev = skb->dev;
  220. struct sk_buff *orig_skb = skb;
  221. struct netdev_queue *txq;
  222. int ret = NETDEV_TX_BUSY;
  223. if (unlikely(!netif_running(dev) ||
  224. !netif_carrier_ok(dev)))
  225. goto drop;
  226. skb = validate_xmit_skb_list(skb, dev);
  227. if (skb != orig_skb)
  228. goto drop;
  229. packet_pick_tx_queue(dev, skb);
  230. txq = skb_get_tx_queue(dev, skb);
  231. local_bh_disable();
  232. HARD_TX_LOCK(dev, txq, smp_processor_id());
  233. if (!netif_xmit_frozen_or_drv_stopped(txq))
  234. ret = netdev_start_xmit(skb, dev, txq, false);
  235. HARD_TX_UNLOCK(dev, txq);
  236. local_bh_enable();
  237. if (!dev_xmit_complete(ret))
  238. kfree_skb(skb);
  239. return ret;
  240. drop:
  241. atomic_long_inc(&dev->tx_dropped);
  242. kfree_skb_list(skb);
  243. return NET_XMIT_DROP;
  244. }
  245. static struct net_device *packet_cached_dev_get(struct packet_sock *po)
  246. {
  247. struct net_device *dev;
  248. rcu_read_lock();
  249. dev = rcu_dereference(po->cached_dev);
  250. if (likely(dev))
  251. dev_hold(dev);
  252. rcu_read_unlock();
  253. return dev;
  254. }
  255. static void packet_cached_dev_assign(struct packet_sock *po,
  256. struct net_device *dev)
  257. {
  258. rcu_assign_pointer(po->cached_dev, dev);
  259. }
  260. static void packet_cached_dev_reset(struct packet_sock *po)
  261. {
  262. RCU_INIT_POINTER(po->cached_dev, NULL);
  263. }
  264. static bool packet_use_direct_xmit(const struct packet_sock *po)
  265. {
  266. return po->xmit == packet_direct_xmit;
  267. }
  268. static u16 __packet_pick_tx_queue(struct net_device *dev, struct sk_buff *skb)
  269. {
  270. return (u16) raw_smp_processor_id() % dev->real_num_tx_queues;
  271. }
  272. static void packet_pick_tx_queue(struct net_device *dev, struct sk_buff *skb)
  273. {
  274. const struct net_device_ops *ops = dev->netdev_ops;
  275. u16 queue_index;
  276. if (ops->ndo_select_queue) {
  277. queue_index = ops->ndo_select_queue(dev, skb, NULL,
  278. __packet_pick_tx_queue);
  279. queue_index = netdev_cap_txqueue(dev, queue_index);
  280. } else {
  281. queue_index = __packet_pick_tx_queue(dev, skb);
  282. }
  283. skb_set_queue_mapping(skb, queue_index);
  284. }
  285. /* __register_prot_hook must be invoked through register_prot_hook
  286. * or from a context in which asynchronous accesses to the packet
  287. * socket is not possible (packet_create()).
  288. */
  289. static void __register_prot_hook(struct sock *sk)
  290. {
  291. struct packet_sock *po = pkt_sk(sk);
  292. if (!po->running) {
  293. if (po->fanout)
  294. __fanout_link(sk, po);
  295. else
  296. dev_add_pack(&po->prot_hook);
  297. sock_hold(sk);
  298. po->running = 1;
  299. }
  300. }
  301. static void register_prot_hook(struct sock *sk)
  302. {
  303. lockdep_assert_held_once(&pkt_sk(sk)->bind_lock);
  304. __register_prot_hook(sk);
  305. }
  306. /* If the sync parameter is true, we will temporarily drop
  307. * the po->bind_lock and do a synchronize_net to make sure no
  308. * asynchronous packet processing paths still refer to the elements
  309. * of po->prot_hook. If the sync parameter is false, it is the
  310. * callers responsibility to take care of this.
  311. */
  312. static void __unregister_prot_hook(struct sock *sk, bool sync)
  313. {
  314. struct packet_sock *po = pkt_sk(sk);
  315. lockdep_assert_held_once(&po->bind_lock);
  316. po->running = 0;
  317. if (po->fanout)
  318. __fanout_unlink(sk, po);
  319. else
  320. __dev_remove_pack(&po->prot_hook);
  321. __sock_put(sk);
  322. if (sync) {
  323. spin_unlock(&po->bind_lock);
  324. synchronize_net();
  325. spin_lock(&po->bind_lock);
  326. }
  327. }
  328. static void unregister_prot_hook(struct sock *sk, bool sync)
  329. {
  330. struct packet_sock *po = pkt_sk(sk);
  331. if (po->running)
  332. __unregister_prot_hook(sk, sync);
  333. }
  334. static inline struct page * __pure pgv_to_page(void *addr)
  335. {
  336. if (is_vmalloc_addr(addr))
  337. return vmalloc_to_page(addr);
  338. return virt_to_page(addr);
  339. }
  340. static void __packet_set_status(struct packet_sock *po, void *frame, int status)
  341. {
  342. union tpacket_uhdr h;
  343. h.raw = frame;
  344. switch (po->tp_version) {
  345. case TPACKET_V1:
  346. h.h1->tp_status = status;
  347. flush_dcache_page(pgv_to_page(&h.h1->tp_status));
  348. break;
  349. case TPACKET_V2:
  350. h.h2->tp_status = status;
  351. flush_dcache_page(pgv_to_page(&h.h2->tp_status));
  352. break;
  353. case TPACKET_V3:
  354. h.h3->tp_status = status;
  355. flush_dcache_page(pgv_to_page(&h.h3->tp_status));
  356. break;
  357. default:
  358. WARN(1, "TPACKET version not supported.\n");
  359. BUG();
  360. }
  361. smp_wmb();
  362. }
  363. static int __packet_get_status(struct packet_sock *po, void *frame)
  364. {
  365. union tpacket_uhdr h;
  366. smp_rmb();
  367. h.raw = frame;
  368. switch (po->tp_version) {
  369. case TPACKET_V1:
  370. flush_dcache_page(pgv_to_page(&h.h1->tp_status));
  371. return h.h1->tp_status;
  372. case TPACKET_V2:
  373. flush_dcache_page(pgv_to_page(&h.h2->tp_status));
  374. return h.h2->tp_status;
  375. case TPACKET_V3:
  376. flush_dcache_page(pgv_to_page(&h.h3->tp_status));
  377. return h.h3->tp_status;
  378. default:
  379. WARN(1, "TPACKET version not supported.\n");
  380. BUG();
  381. return 0;
  382. }
  383. }
  384. static __u32 tpacket_get_timestamp(struct sk_buff *skb, struct timespec *ts,
  385. unsigned int flags)
  386. {
  387. struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
  388. if (shhwtstamps &&
  389. (flags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
  390. ktime_to_timespec_cond(shhwtstamps->hwtstamp, ts))
  391. return TP_STATUS_TS_RAW_HARDWARE;
  392. if (ktime_to_timespec_cond(skb->tstamp, ts))
  393. return TP_STATUS_TS_SOFTWARE;
  394. return 0;
  395. }
  396. static __u32 __packet_set_timestamp(struct packet_sock *po, void *frame,
  397. struct sk_buff *skb)
  398. {
  399. union tpacket_uhdr h;
  400. struct timespec ts;
  401. __u32 ts_status;
  402. if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
  403. return 0;
  404. h.raw = frame;
  405. switch (po->tp_version) {
  406. case TPACKET_V1:
  407. h.h1->tp_sec = ts.tv_sec;
  408. h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
  409. break;
  410. case TPACKET_V2:
  411. h.h2->tp_sec = ts.tv_sec;
  412. h.h2->tp_nsec = ts.tv_nsec;
  413. break;
  414. case TPACKET_V3:
  415. h.h3->tp_sec = ts.tv_sec;
  416. h.h3->tp_nsec = ts.tv_nsec;
  417. break;
  418. default:
  419. WARN(1, "TPACKET version not supported.\n");
  420. BUG();
  421. }
  422. /* one flush is safe, as both fields always lie on the same cacheline */
  423. flush_dcache_page(pgv_to_page(&h.h1->tp_sec));
  424. smp_wmb();
  425. return ts_status;
  426. }
  427. static void *packet_lookup_frame(struct packet_sock *po,
  428. struct packet_ring_buffer *rb,
  429. unsigned int position,
  430. int status)
  431. {
  432. unsigned int pg_vec_pos, frame_offset;
  433. union tpacket_uhdr h;
  434. pg_vec_pos = position / rb->frames_per_block;
  435. frame_offset = position % rb->frames_per_block;
  436. h.raw = rb->pg_vec[pg_vec_pos].buffer +
  437. (frame_offset * rb->frame_size);
  438. if (status != __packet_get_status(po, h.raw))
  439. return NULL;
  440. return h.raw;
  441. }
  442. static void *packet_current_frame(struct packet_sock *po,
  443. struct packet_ring_buffer *rb,
  444. int status)
  445. {
  446. return packet_lookup_frame(po, rb, rb->head, status);
  447. }
  448. static void prb_del_retire_blk_timer(struct tpacket_kbdq_core *pkc)
  449. {
  450. del_timer_sync(&pkc->retire_blk_timer);
  451. }
  452. static void prb_shutdown_retire_blk_timer(struct packet_sock *po,
  453. struct sk_buff_head *rb_queue)
  454. {
  455. struct tpacket_kbdq_core *pkc;
  456. pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
  457. spin_lock_bh(&rb_queue->lock);
  458. pkc->delete_blk_timer = 1;
  459. spin_unlock_bh(&rb_queue->lock);
  460. prb_del_retire_blk_timer(pkc);
  461. }
  462. static void prb_init_blk_timer(struct packet_sock *po,
  463. struct tpacket_kbdq_core *pkc,
  464. void (*func) (unsigned long))
  465. {
  466. init_timer(&pkc->retire_blk_timer);
  467. pkc->retire_blk_timer.data = (long)po;
  468. pkc->retire_blk_timer.function = func;
  469. pkc->retire_blk_timer.expires = jiffies;
  470. }
  471. static void prb_setup_retire_blk_timer(struct packet_sock *po)
  472. {
  473. struct tpacket_kbdq_core *pkc;
  474. pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
  475. prb_init_blk_timer(po, pkc, prb_retire_rx_blk_timer_expired);
  476. }
  477. static int prb_calc_retire_blk_tmo(struct packet_sock *po,
  478. int blk_size_in_bytes)
  479. {
  480. struct net_device *dev;
  481. unsigned int mbits = 0, msec = 0, div = 0, tmo = 0;
  482. struct ethtool_link_ksettings ecmd;
  483. int err;
  484. rtnl_lock();
  485. dev = __dev_get_by_index(sock_net(&po->sk), po->ifindex);
  486. if (unlikely(!dev)) {
  487. rtnl_unlock();
  488. return DEFAULT_PRB_RETIRE_TOV;
  489. }
  490. err = __ethtool_get_link_ksettings(dev, &ecmd);
  491. rtnl_unlock();
  492. if (!err) {
  493. /*
  494. * If the link speed is so slow you don't really
  495. * need to worry about perf anyways
  496. */
  497. if (ecmd.base.speed < SPEED_1000 ||
  498. ecmd.base.speed == SPEED_UNKNOWN) {
  499. return DEFAULT_PRB_RETIRE_TOV;
  500. } else {
  501. msec = 1;
  502. div = ecmd.base.speed / 1000;
  503. }
  504. } else
  505. return DEFAULT_PRB_RETIRE_TOV;
  506. mbits = (blk_size_in_bytes * 8) / (1024 * 1024);
  507. if (div)
  508. mbits /= div;
  509. tmo = mbits * msec;
  510. if (div)
  511. return tmo+1;
  512. return tmo;
  513. }
  514. static void prb_init_ft_ops(struct tpacket_kbdq_core *p1,
  515. union tpacket_req_u *req_u)
  516. {
  517. p1->feature_req_word = req_u->req3.tp_feature_req_word;
  518. }
  519. static void init_prb_bdqc(struct packet_sock *po,
  520. struct packet_ring_buffer *rb,
  521. struct pgv *pg_vec,
  522. union tpacket_req_u *req_u)
  523. {
  524. struct tpacket_kbdq_core *p1 = GET_PBDQC_FROM_RB(rb);
  525. struct tpacket_block_desc *pbd;
  526. memset(p1, 0x0, sizeof(*p1));
  527. p1->knxt_seq_num = 1;
  528. p1->pkbdq = pg_vec;
  529. pbd = (struct tpacket_block_desc *)pg_vec[0].buffer;
  530. p1->pkblk_start = pg_vec[0].buffer;
  531. p1->kblk_size = req_u->req3.tp_block_size;
  532. p1->knum_blocks = req_u->req3.tp_block_nr;
  533. p1->hdrlen = po->tp_hdrlen;
  534. p1->version = po->tp_version;
  535. p1->last_kactive_blk_num = 0;
  536. po->stats.stats3.tp_freeze_q_cnt = 0;
  537. if (req_u->req3.tp_retire_blk_tov)
  538. p1->retire_blk_tov = req_u->req3.tp_retire_blk_tov;
  539. else
  540. p1->retire_blk_tov = prb_calc_retire_blk_tmo(po,
  541. req_u->req3.tp_block_size);
  542. p1->tov_in_jiffies = msecs_to_jiffies(p1->retire_blk_tov);
  543. p1->blk_sizeof_priv = req_u->req3.tp_sizeof_priv;
  544. p1->max_frame_len = p1->kblk_size - BLK_PLUS_PRIV(p1->blk_sizeof_priv);
  545. prb_init_ft_ops(p1, req_u);
  546. prb_setup_retire_blk_timer(po);
  547. prb_open_block(p1, pbd);
  548. }
  549. /* Do NOT update the last_blk_num first.
  550. * Assumes sk_buff_head lock is held.
  551. */
  552. static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *pkc)
  553. {
  554. mod_timer(&pkc->retire_blk_timer,
  555. jiffies + pkc->tov_in_jiffies);
  556. pkc->last_kactive_blk_num = pkc->kactive_blk_num;
  557. }
  558. /*
  559. * Timer logic:
  560. * 1) We refresh the timer only when we open a block.
  561. * By doing this we don't waste cycles refreshing the timer
  562. * on packet-by-packet basis.
  563. *
  564. * With a 1MB block-size, on a 1Gbps line, it will take
  565. * i) ~8 ms to fill a block + ii) memcpy etc.
  566. * In this cut we are not accounting for the memcpy time.
  567. *
  568. * So, if the user sets the 'tmo' to 10ms then the timer
  569. * will never fire while the block is still getting filled
  570. * (which is what we want). However, the user could choose
  571. * to close a block early and that's fine.
  572. *
  573. * But when the timer does fire, we check whether or not to refresh it.
  574. * Since the tmo granularity is in msecs, it is not too expensive
  575. * to refresh the timer, lets say every '8' msecs.
  576. * Either the user can set the 'tmo' or we can derive it based on
  577. * a) line-speed and b) block-size.
  578. * prb_calc_retire_blk_tmo() calculates the tmo.
  579. *
  580. */
  581. static void prb_retire_rx_blk_timer_expired(unsigned long data)
  582. {
  583. struct packet_sock *po = (struct packet_sock *)data;
  584. struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
  585. unsigned int frozen;
  586. struct tpacket_block_desc *pbd;
  587. spin_lock(&po->sk.sk_receive_queue.lock);
  588. frozen = prb_queue_frozen(pkc);
  589. pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
  590. if (unlikely(pkc->delete_blk_timer))
  591. goto out;
  592. /* We only need to plug the race when the block is partially filled.
  593. * tpacket_rcv:
  594. * lock(); increment BLOCK_NUM_PKTS; unlock()
  595. * copy_bits() is in progress ...
  596. * timer fires on other cpu:
  597. * we can't retire the current block because copy_bits
  598. * is in progress.
  599. *
  600. */
  601. if (BLOCK_NUM_PKTS(pbd)) {
  602. while (atomic_read(&pkc->blk_fill_in_prog)) {
  603. /* Waiting for skb_copy_bits to finish... */
  604. cpu_relax();
  605. }
  606. }
  607. if (pkc->last_kactive_blk_num == pkc->kactive_blk_num) {
  608. if (!frozen) {
  609. if (!BLOCK_NUM_PKTS(pbd)) {
  610. /* An empty block. Just refresh the timer. */
  611. goto refresh_timer;
  612. }
  613. prb_retire_current_block(pkc, po, TP_STATUS_BLK_TMO);
  614. if (!prb_dispatch_next_block(pkc, po))
  615. goto refresh_timer;
  616. else
  617. goto out;
  618. } else {
  619. /* Case 1. Queue was frozen because user-space was
  620. * lagging behind.
  621. */
  622. if (prb_curr_blk_in_use(pbd)) {
  623. /*
  624. * Ok, user-space is still behind.
  625. * So just refresh the timer.
  626. */
  627. goto refresh_timer;
  628. } else {
  629. /* Case 2. queue was frozen,user-space caught up,
  630. * now the link went idle && the timer fired.
  631. * We don't have a block to close.So we open this
  632. * block and restart the timer.
  633. * opening a block thaws the queue,restarts timer
  634. * Thawing/timer-refresh is a side effect.
  635. */
  636. prb_open_block(pkc, pbd);
  637. goto out;
  638. }
  639. }
  640. }
  641. refresh_timer:
  642. _prb_refresh_rx_retire_blk_timer(pkc);
  643. out:
  644. spin_unlock(&po->sk.sk_receive_queue.lock);
  645. }
  646. static void prb_flush_block(struct tpacket_kbdq_core *pkc1,
  647. struct tpacket_block_desc *pbd1, __u32 status)
  648. {
  649. /* Flush everything minus the block header */
  650. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
  651. u8 *start, *end;
  652. start = (u8 *)pbd1;
  653. /* Skip the block header(we know header WILL fit in 4K) */
  654. start += PAGE_SIZE;
  655. end = (u8 *)PAGE_ALIGN((unsigned long)pkc1->pkblk_end);
  656. for (; start < end; start += PAGE_SIZE)
  657. flush_dcache_page(pgv_to_page(start));
  658. smp_wmb();
  659. #endif
  660. /* Now update the block status. */
  661. BLOCK_STATUS(pbd1) = status;
  662. /* Flush the block header */
  663. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
  664. start = (u8 *)pbd1;
  665. flush_dcache_page(pgv_to_page(start));
  666. smp_wmb();
  667. #endif
  668. }
  669. /*
  670. * Side effect:
  671. *
  672. * 1) flush the block
  673. * 2) Increment active_blk_num
  674. *
  675. * Note:We DONT refresh the timer on purpose.
  676. * Because almost always the next block will be opened.
  677. */
  678. static void prb_close_block(struct tpacket_kbdq_core *pkc1,
  679. struct tpacket_block_desc *pbd1,
  680. struct packet_sock *po, unsigned int stat)
  681. {
  682. __u32 status = TP_STATUS_USER | stat;
  683. struct tpacket3_hdr *last_pkt;
  684. struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
  685. struct sock *sk = &po->sk;
  686. if (po->stats.stats3.tp_drops)
  687. status |= TP_STATUS_LOSING;
  688. last_pkt = (struct tpacket3_hdr *)pkc1->prev;
  689. last_pkt->tp_next_offset = 0;
  690. /* Get the ts of the last pkt */
  691. if (BLOCK_NUM_PKTS(pbd1)) {
  692. h1->ts_last_pkt.ts_sec = last_pkt->tp_sec;
  693. h1->ts_last_pkt.ts_nsec = last_pkt->tp_nsec;
  694. } else {
  695. /* Ok, we tmo'd - so get the current time.
  696. *
  697. * It shouldn't really happen as we don't close empty
  698. * blocks. See prb_retire_rx_blk_timer_expired().
  699. */
  700. struct timespec ts;
  701. getnstimeofday(&ts);
  702. h1->ts_last_pkt.ts_sec = ts.tv_sec;
  703. h1->ts_last_pkt.ts_nsec = ts.tv_nsec;
  704. }
  705. smp_wmb();
  706. /* Flush the block */
  707. prb_flush_block(pkc1, pbd1, status);
  708. sk->sk_data_ready(sk);
  709. pkc1->kactive_blk_num = GET_NEXT_PRB_BLK_NUM(pkc1);
  710. }
  711. static void prb_thaw_queue(struct tpacket_kbdq_core *pkc)
  712. {
  713. pkc->reset_pending_on_curr_blk = 0;
  714. }
  715. /*
  716. * Side effect of opening a block:
  717. *
  718. * 1) prb_queue is thawed.
  719. * 2) retire_blk_timer is refreshed.
  720. *
  721. */
  722. static void prb_open_block(struct tpacket_kbdq_core *pkc1,
  723. struct tpacket_block_desc *pbd1)
  724. {
  725. struct timespec ts;
  726. struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
  727. smp_rmb();
  728. /* We could have just memset this but we will lose the
  729. * flexibility of making the priv area sticky
  730. */
  731. BLOCK_SNUM(pbd1) = pkc1->knxt_seq_num++;
  732. BLOCK_NUM_PKTS(pbd1) = 0;
  733. BLOCK_LEN(pbd1) = BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
  734. getnstimeofday(&ts);
  735. h1->ts_first_pkt.ts_sec = ts.tv_sec;
  736. h1->ts_first_pkt.ts_nsec = ts.tv_nsec;
  737. pkc1->pkblk_start = (char *)pbd1;
  738. pkc1->nxt_offset = pkc1->pkblk_start + BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
  739. BLOCK_O2FP(pbd1) = (__u32)BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
  740. BLOCK_O2PRIV(pbd1) = BLK_HDR_LEN;
  741. pbd1->version = pkc1->version;
  742. pkc1->prev = pkc1->nxt_offset;
  743. pkc1->pkblk_end = pkc1->pkblk_start + pkc1->kblk_size;
  744. prb_thaw_queue(pkc1);
  745. _prb_refresh_rx_retire_blk_timer(pkc1);
  746. smp_wmb();
  747. }
  748. /*
  749. * Queue freeze logic:
  750. * 1) Assume tp_block_nr = 8 blocks.
  751. * 2) At time 't0', user opens Rx ring.
  752. * 3) Some time past 't0', kernel starts filling blocks starting from 0 .. 7
  753. * 4) user-space is either sleeping or processing block '0'.
  754. * 5) tpacket_rcv is currently filling block '7', since there is no space left,
  755. * it will close block-7,loop around and try to fill block '0'.
  756. * call-flow:
  757. * __packet_lookup_frame_in_block
  758. * prb_retire_current_block()
  759. * prb_dispatch_next_block()
  760. * |->(BLOCK_STATUS == USER) evaluates to true
  761. * 5.1) Since block-0 is currently in-use, we just freeze the queue.
  762. * 6) Now there are two cases:
  763. * 6.1) Link goes idle right after the queue is frozen.
  764. * But remember, the last open_block() refreshed the timer.
  765. * When this timer expires,it will refresh itself so that we can
  766. * re-open block-0 in near future.
  767. * 6.2) Link is busy and keeps on receiving packets. This is a simple
  768. * case and __packet_lookup_frame_in_block will check if block-0
  769. * is free and can now be re-used.
  770. */
  771. static void prb_freeze_queue(struct tpacket_kbdq_core *pkc,
  772. struct packet_sock *po)
  773. {
  774. pkc->reset_pending_on_curr_blk = 1;
  775. po->stats.stats3.tp_freeze_q_cnt++;
  776. }
  777. #define TOTAL_PKT_LEN_INCL_ALIGN(length) (ALIGN((length), V3_ALIGNMENT))
  778. /*
  779. * If the next block is free then we will dispatch it
  780. * and return a good offset.
  781. * Else, we will freeze the queue.
  782. * So, caller must check the return value.
  783. */
  784. static void *prb_dispatch_next_block(struct tpacket_kbdq_core *pkc,
  785. struct packet_sock *po)
  786. {
  787. struct tpacket_block_desc *pbd;
  788. smp_rmb();
  789. /* 1. Get current block num */
  790. pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
  791. /* 2. If this block is currently in_use then freeze the queue */
  792. if (TP_STATUS_USER & BLOCK_STATUS(pbd)) {
  793. prb_freeze_queue(pkc, po);
  794. return NULL;
  795. }
  796. /*
  797. * 3.
  798. * open this block and return the offset where the first packet
  799. * needs to get stored.
  800. */
  801. prb_open_block(pkc, pbd);
  802. return (void *)pkc->nxt_offset;
  803. }
  804. static void prb_retire_current_block(struct tpacket_kbdq_core *pkc,
  805. struct packet_sock *po, unsigned int status)
  806. {
  807. struct tpacket_block_desc *pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
  808. /* retire/close the current block */
  809. if (likely(TP_STATUS_KERNEL == BLOCK_STATUS(pbd))) {
  810. /*
  811. * Plug the case where copy_bits() is in progress on
  812. * cpu-0 and tpacket_rcv() got invoked on cpu-1, didn't
  813. * have space to copy the pkt in the current block and
  814. * called prb_retire_current_block()
  815. *
  816. * We don't need to worry about the TMO case because
  817. * the timer-handler already handled this case.
  818. */
  819. if (!(status & TP_STATUS_BLK_TMO)) {
  820. while (atomic_read(&pkc->blk_fill_in_prog)) {
  821. /* Waiting for skb_copy_bits to finish... */
  822. cpu_relax();
  823. }
  824. }
  825. prb_close_block(pkc, pbd, po, status);
  826. return;
  827. }
  828. }
  829. static int prb_curr_blk_in_use(struct tpacket_block_desc *pbd)
  830. {
  831. return TP_STATUS_USER & BLOCK_STATUS(pbd);
  832. }
  833. static int prb_queue_frozen(struct tpacket_kbdq_core *pkc)
  834. {
  835. return pkc->reset_pending_on_curr_blk;
  836. }
  837. static void prb_clear_blk_fill_status(struct packet_ring_buffer *rb)
  838. __releases(&pkc->blk_fill_in_prog_lock)
  839. {
  840. struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
  841. atomic_dec(&pkc->blk_fill_in_prog);
  842. }
  843. static void prb_fill_rxhash(struct tpacket_kbdq_core *pkc,
  844. struct tpacket3_hdr *ppd)
  845. {
  846. ppd->hv1.tp_rxhash = skb_get_hash(pkc->skb);
  847. }
  848. static void prb_clear_rxhash(struct tpacket_kbdq_core *pkc,
  849. struct tpacket3_hdr *ppd)
  850. {
  851. ppd->hv1.tp_rxhash = 0;
  852. }
  853. static void prb_fill_vlan_info(struct tpacket_kbdq_core *pkc,
  854. struct tpacket3_hdr *ppd)
  855. {
  856. if (skb_vlan_tag_present(pkc->skb)) {
  857. ppd->hv1.tp_vlan_tci = skb_vlan_tag_get(pkc->skb);
  858. ppd->hv1.tp_vlan_tpid = ntohs(pkc->skb->vlan_proto);
  859. ppd->tp_status = TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
  860. } else {
  861. ppd->hv1.tp_vlan_tci = 0;
  862. ppd->hv1.tp_vlan_tpid = 0;
  863. ppd->tp_status = TP_STATUS_AVAILABLE;
  864. }
  865. }
  866. static void prb_run_all_ft_ops(struct tpacket_kbdq_core *pkc,
  867. struct tpacket3_hdr *ppd)
  868. {
  869. ppd->hv1.tp_padding = 0;
  870. prb_fill_vlan_info(pkc, ppd);
  871. if (pkc->feature_req_word & TP_FT_REQ_FILL_RXHASH)
  872. prb_fill_rxhash(pkc, ppd);
  873. else
  874. prb_clear_rxhash(pkc, ppd);
  875. }
  876. static void prb_fill_curr_block(char *curr,
  877. struct tpacket_kbdq_core *pkc,
  878. struct tpacket_block_desc *pbd,
  879. unsigned int len)
  880. __acquires(&pkc->blk_fill_in_prog_lock)
  881. {
  882. struct tpacket3_hdr *ppd;
  883. ppd = (struct tpacket3_hdr *)curr;
  884. ppd->tp_next_offset = TOTAL_PKT_LEN_INCL_ALIGN(len);
  885. pkc->prev = curr;
  886. pkc->nxt_offset += TOTAL_PKT_LEN_INCL_ALIGN(len);
  887. BLOCK_LEN(pbd) += TOTAL_PKT_LEN_INCL_ALIGN(len);
  888. BLOCK_NUM_PKTS(pbd) += 1;
  889. atomic_inc(&pkc->blk_fill_in_prog);
  890. prb_run_all_ft_ops(pkc, ppd);
  891. }
  892. /* Assumes caller has the sk->rx_queue.lock */
  893. static void *__packet_lookup_frame_in_block(struct packet_sock *po,
  894. struct sk_buff *skb,
  895. int status,
  896. unsigned int len
  897. )
  898. {
  899. struct tpacket_kbdq_core *pkc;
  900. struct tpacket_block_desc *pbd;
  901. char *curr, *end;
  902. pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
  903. pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
  904. /* Queue is frozen when user space is lagging behind */
  905. if (prb_queue_frozen(pkc)) {
  906. /*
  907. * Check if that last block which caused the queue to freeze,
  908. * is still in_use by user-space.
  909. */
  910. if (prb_curr_blk_in_use(pbd)) {
  911. /* Can't record this packet */
  912. return NULL;
  913. } else {
  914. /*
  915. * Ok, the block was released by user-space.
  916. * Now let's open that block.
  917. * opening a block also thaws the queue.
  918. * Thawing is a side effect.
  919. */
  920. prb_open_block(pkc, pbd);
  921. }
  922. }
  923. smp_mb();
  924. curr = pkc->nxt_offset;
  925. pkc->skb = skb;
  926. end = (char *)pbd + pkc->kblk_size;
  927. /* first try the current block */
  928. if (curr+TOTAL_PKT_LEN_INCL_ALIGN(len) < end) {
  929. prb_fill_curr_block(curr, pkc, pbd, len);
  930. return (void *)curr;
  931. }
  932. /* Ok, close the current block */
  933. prb_retire_current_block(pkc, po, 0);
  934. /* Now, try to dispatch the next block */
  935. curr = (char *)prb_dispatch_next_block(pkc, po);
  936. if (curr) {
  937. pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
  938. prb_fill_curr_block(curr, pkc, pbd, len);
  939. return (void *)curr;
  940. }
  941. /*
  942. * No free blocks are available.user_space hasn't caught up yet.
  943. * Queue was just frozen and now this packet will get dropped.
  944. */
  945. return NULL;
  946. }
  947. static void *packet_current_rx_frame(struct packet_sock *po,
  948. struct sk_buff *skb,
  949. int status, unsigned int len)
  950. {
  951. char *curr = NULL;
  952. switch (po->tp_version) {
  953. case TPACKET_V1:
  954. case TPACKET_V2:
  955. curr = packet_lookup_frame(po, &po->rx_ring,
  956. po->rx_ring.head, status);
  957. return curr;
  958. case TPACKET_V3:
  959. return __packet_lookup_frame_in_block(po, skb, status, len);
  960. default:
  961. WARN(1, "TPACKET version not supported\n");
  962. BUG();
  963. return NULL;
  964. }
  965. }
  966. static void *prb_lookup_block(struct packet_sock *po,
  967. struct packet_ring_buffer *rb,
  968. unsigned int idx,
  969. int status)
  970. {
  971. struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
  972. struct tpacket_block_desc *pbd = GET_PBLOCK_DESC(pkc, idx);
  973. if (status != BLOCK_STATUS(pbd))
  974. return NULL;
  975. return pbd;
  976. }
  977. static int prb_previous_blk_num(struct packet_ring_buffer *rb)
  978. {
  979. unsigned int prev;
  980. if (rb->prb_bdqc.kactive_blk_num)
  981. prev = rb->prb_bdqc.kactive_blk_num-1;
  982. else
  983. prev = rb->prb_bdqc.knum_blocks-1;
  984. return prev;
  985. }
  986. /* Assumes caller has held the rx_queue.lock */
  987. static void *__prb_previous_block(struct packet_sock *po,
  988. struct packet_ring_buffer *rb,
  989. int status)
  990. {
  991. unsigned int previous = prb_previous_blk_num(rb);
  992. return prb_lookup_block(po, rb, previous, status);
  993. }
  994. static void *packet_previous_rx_frame(struct packet_sock *po,
  995. struct packet_ring_buffer *rb,
  996. int status)
  997. {
  998. if (po->tp_version <= TPACKET_V2)
  999. return packet_previous_frame(po, rb, status);
  1000. return __prb_previous_block(po, rb, status);
  1001. }
  1002. static void packet_increment_rx_head(struct packet_sock *po,
  1003. struct packet_ring_buffer *rb)
  1004. {
  1005. switch (po->tp_version) {
  1006. case TPACKET_V1:
  1007. case TPACKET_V2:
  1008. return packet_increment_head(rb);
  1009. case TPACKET_V3:
  1010. default:
  1011. WARN(1, "TPACKET version not supported.\n");
  1012. BUG();
  1013. return;
  1014. }
  1015. }
  1016. static void *packet_previous_frame(struct packet_sock *po,
  1017. struct packet_ring_buffer *rb,
  1018. int status)
  1019. {
  1020. unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
  1021. return packet_lookup_frame(po, rb, previous, status);
  1022. }
  1023. static void packet_increment_head(struct packet_ring_buffer *buff)
  1024. {
  1025. buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
  1026. }
  1027. static void packet_inc_pending(struct packet_ring_buffer *rb)
  1028. {
  1029. this_cpu_inc(*rb->pending_refcnt);
  1030. }
  1031. static void packet_dec_pending(struct packet_ring_buffer *rb)
  1032. {
  1033. this_cpu_dec(*rb->pending_refcnt);
  1034. }
  1035. static unsigned int packet_read_pending(const struct packet_ring_buffer *rb)
  1036. {
  1037. unsigned int refcnt = 0;
  1038. int cpu;
  1039. /* We don't use pending refcount in rx_ring. */
  1040. if (rb->pending_refcnt == NULL)
  1041. return 0;
  1042. for_each_possible_cpu(cpu)
  1043. refcnt += *per_cpu_ptr(rb->pending_refcnt, cpu);
  1044. return refcnt;
  1045. }
  1046. static int packet_alloc_pending(struct packet_sock *po)
  1047. {
  1048. po->rx_ring.pending_refcnt = NULL;
  1049. po->tx_ring.pending_refcnt = alloc_percpu(unsigned int);
  1050. if (unlikely(po->tx_ring.pending_refcnt == NULL))
  1051. return -ENOBUFS;
  1052. return 0;
  1053. }
  1054. static void packet_free_pending(struct packet_sock *po)
  1055. {
  1056. free_percpu(po->tx_ring.pending_refcnt);
  1057. }
  1058. #define ROOM_POW_OFF 2
  1059. #define ROOM_NONE 0x0
  1060. #define ROOM_LOW 0x1
  1061. #define ROOM_NORMAL 0x2
  1062. static bool __tpacket_has_room(struct packet_sock *po, int pow_off)
  1063. {
  1064. int idx, len;
  1065. len = po->rx_ring.frame_max + 1;
  1066. idx = po->rx_ring.head;
  1067. if (pow_off)
  1068. idx += len >> pow_off;
  1069. if (idx >= len)
  1070. idx -= len;
  1071. return packet_lookup_frame(po, &po->rx_ring, idx, TP_STATUS_KERNEL);
  1072. }
  1073. static bool __tpacket_v3_has_room(struct packet_sock *po, int pow_off)
  1074. {
  1075. int idx, len;
  1076. len = po->rx_ring.prb_bdqc.knum_blocks;
  1077. idx = po->rx_ring.prb_bdqc.kactive_blk_num;
  1078. if (pow_off)
  1079. idx += len >> pow_off;
  1080. if (idx >= len)
  1081. idx -= len;
  1082. return prb_lookup_block(po, &po->rx_ring, idx, TP_STATUS_KERNEL);
  1083. }
  1084. static int __packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
  1085. {
  1086. struct sock *sk = &po->sk;
  1087. int ret = ROOM_NONE;
  1088. if (po->prot_hook.func != tpacket_rcv) {
  1089. int avail = sk->sk_rcvbuf - atomic_read(&sk->sk_rmem_alloc)
  1090. - (skb ? skb->truesize : 0);
  1091. if (avail > (sk->sk_rcvbuf >> ROOM_POW_OFF))
  1092. return ROOM_NORMAL;
  1093. else if (avail > 0)
  1094. return ROOM_LOW;
  1095. else
  1096. return ROOM_NONE;
  1097. }
  1098. if (po->tp_version == TPACKET_V3) {
  1099. if (__tpacket_v3_has_room(po, ROOM_POW_OFF))
  1100. ret = ROOM_NORMAL;
  1101. else if (__tpacket_v3_has_room(po, 0))
  1102. ret = ROOM_LOW;
  1103. } else {
  1104. if (__tpacket_has_room(po, ROOM_POW_OFF))
  1105. ret = ROOM_NORMAL;
  1106. else if (__tpacket_has_room(po, 0))
  1107. ret = ROOM_LOW;
  1108. }
  1109. return ret;
  1110. }
  1111. static int packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
  1112. {
  1113. int ret;
  1114. bool has_room;
  1115. spin_lock_bh(&po->sk.sk_receive_queue.lock);
  1116. ret = __packet_rcv_has_room(po, skb);
  1117. has_room = ret == ROOM_NORMAL;
  1118. if (po->pressure == has_room)
  1119. po->pressure = !has_room;
  1120. spin_unlock_bh(&po->sk.sk_receive_queue.lock);
  1121. return ret;
  1122. }
  1123. static void packet_sock_destruct(struct sock *sk)
  1124. {
  1125. skb_queue_purge(&sk->sk_error_queue);
  1126. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  1127. WARN_ON(refcount_read(&sk->sk_wmem_alloc));
  1128. if (!sock_flag(sk, SOCK_DEAD)) {
  1129. pr_err("Attempt to release alive packet socket: %p\n", sk);
  1130. return;
  1131. }
  1132. sk_refcnt_debug_dec(sk);
  1133. }
  1134. static bool fanout_flow_is_huge(struct packet_sock *po, struct sk_buff *skb)
  1135. {
  1136. u32 *history = po->rollover->history;
  1137. u32 victim, rxhash;
  1138. int i, count = 0;
  1139. rxhash = skb_get_hash(skb);
  1140. for (i = 0; i < ROLLOVER_HLEN; i++)
  1141. if (READ_ONCE(history[i]) == rxhash)
  1142. count++;
  1143. victim = prandom_u32() % ROLLOVER_HLEN;
  1144. /* Avoid dirtying the cache line if possible */
  1145. if (READ_ONCE(history[victim]) != rxhash)
  1146. WRITE_ONCE(history[victim], rxhash);
  1147. return count > (ROLLOVER_HLEN >> 1);
  1148. }
  1149. static unsigned int fanout_demux_hash(struct packet_fanout *f,
  1150. struct sk_buff *skb,
  1151. unsigned int num)
  1152. {
  1153. return reciprocal_scale(__skb_get_hash_symmetric(skb), num);
  1154. }
  1155. static unsigned int fanout_demux_lb(struct packet_fanout *f,
  1156. struct sk_buff *skb,
  1157. unsigned int num)
  1158. {
  1159. unsigned int val = atomic_inc_return(&f->rr_cur);
  1160. return val % num;
  1161. }
  1162. static unsigned int fanout_demux_cpu(struct packet_fanout *f,
  1163. struct sk_buff *skb,
  1164. unsigned int num)
  1165. {
  1166. return smp_processor_id() % num;
  1167. }
  1168. static unsigned int fanout_demux_rnd(struct packet_fanout *f,
  1169. struct sk_buff *skb,
  1170. unsigned int num)
  1171. {
  1172. return prandom_u32_max(num);
  1173. }
  1174. static unsigned int fanout_demux_rollover(struct packet_fanout *f,
  1175. struct sk_buff *skb,
  1176. unsigned int idx, bool try_self,
  1177. unsigned int num)
  1178. {
  1179. struct packet_sock *po, *po_next, *po_skip = NULL;
  1180. unsigned int i, j, room = ROOM_NONE;
  1181. po = pkt_sk(f->arr[idx]);
  1182. if (try_self) {
  1183. room = packet_rcv_has_room(po, skb);
  1184. if (room == ROOM_NORMAL ||
  1185. (room == ROOM_LOW && !fanout_flow_is_huge(po, skb)))
  1186. return idx;
  1187. po_skip = po;
  1188. }
  1189. i = j = min_t(int, po->rollover->sock, num - 1);
  1190. do {
  1191. po_next = pkt_sk(f->arr[i]);
  1192. if (po_next != po_skip && !po_next->pressure &&
  1193. packet_rcv_has_room(po_next, skb) == ROOM_NORMAL) {
  1194. if (i != j)
  1195. po->rollover->sock = i;
  1196. atomic_long_inc(&po->rollover->num);
  1197. if (room == ROOM_LOW)
  1198. atomic_long_inc(&po->rollover->num_huge);
  1199. return i;
  1200. }
  1201. if (++i == num)
  1202. i = 0;
  1203. } while (i != j);
  1204. atomic_long_inc(&po->rollover->num_failed);
  1205. return idx;
  1206. }
  1207. static unsigned int fanout_demux_qm(struct packet_fanout *f,
  1208. struct sk_buff *skb,
  1209. unsigned int num)
  1210. {
  1211. return skb_get_queue_mapping(skb) % num;
  1212. }
  1213. static unsigned int fanout_demux_bpf(struct packet_fanout *f,
  1214. struct sk_buff *skb,
  1215. unsigned int num)
  1216. {
  1217. struct bpf_prog *prog;
  1218. unsigned int ret = 0;
  1219. rcu_read_lock();
  1220. prog = rcu_dereference(f->bpf_prog);
  1221. if (prog)
  1222. ret = bpf_prog_run_clear_cb(prog, skb) % num;
  1223. rcu_read_unlock();
  1224. return ret;
  1225. }
  1226. static bool fanout_has_flag(struct packet_fanout *f, u16 flag)
  1227. {
  1228. return f->flags & (flag >> 8);
  1229. }
  1230. static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
  1231. struct packet_type *pt, struct net_device *orig_dev)
  1232. {
  1233. struct packet_fanout *f = pt->af_packet_priv;
  1234. unsigned int num = READ_ONCE(f->num_members);
  1235. struct net *net = read_pnet(&f->net);
  1236. struct packet_sock *po;
  1237. unsigned int idx;
  1238. if (!net_eq(dev_net(dev), net) || !num) {
  1239. kfree_skb(skb);
  1240. return 0;
  1241. }
  1242. if (fanout_has_flag(f, PACKET_FANOUT_FLAG_DEFRAG)) {
  1243. skb = ip_check_defrag(net, skb, IP_DEFRAG_AF_PACKET);
  1244. if (!skb)
  1245. return 0;
  1246. }
  1247. switch (f->type) {
  1248. case PACKET_FANOUT_HASH:
  1249. default:
  1250. idx = fanout_demux_hash(f, skb, num);
  1251. break;
  1252. case PACKET_FANOUT_LB:
  1253. idx = fanout_demux_lb(f, skb, num);
  1254. break;
  1255. case PACKET_FANOUT_CPU:
  1256. idx = fanout_demux_cpu(f, skb, num);
  1257. break;
  1258. case PACKET_FANOUT_RND:
  1259. idx = fanout_demux_rnd(f, skb, num);
  1260. break;
  1261. case PACKET_FANOUT_QM:
  1262. idx = fanout_demux_qm(f, skb, num);
  1263. break;
  1264. case PACKET_FANOUT_ROLLOVER:
  1265. idx = fanout_demux_rollover(f, skb, 0, false, num);
  1266. break;
  1267. case PACKET_FANOUT_CBPF:
  1268. case PACKET_FANOUT_EBPF:
  1269. idx = fanout_demux_bpf(f, skb, num);
  1270. break;
  1271. }
  1272. if (fanout_has_flag(f, PACKET_FANOUT_FLAG_ROLLOVER))
  1273. idx = fanout_demux_rollover(f, skb, idx, true, num);
  1274. po = pkt_sk(f->arr[idx]);
  1275. return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
  1276. }
  1277. DEFINE_MUTEX(fanout_mutex);
  1278. EXPORT_SYMBOL_GPL(fanout_mutex);
  1279. static LIST_HEAD(fanout_list);
  1280. static u16 fanout_next_id;
  1281. static void __fanout_link(struct sock *sk, struct packet_sock *po)
  1282. {
  1283. struct packet_fanout *f = po->fanout;
  1284. spin_lock(&f->lock);
  1285. f->arr[f->num_members] = sk;
  1286. smp_wmb();
  1287. f->num_members++;
  1288. if (f->num_members == 1)
  1289. dev_add_pack(&f->prot_hook);
  1290. spin_unlock(&f->lock);
  1291. }
  1292. static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
  1293. {
  1294. struct packet_fanout *f = po->fanout;
  1295. int i;
  1296. spin_lock(&f->lock);
  1297. for (i = 0; i < f->num_members; i++) {
  1298. if (f->arr[i] == sk)
  1299. break;
  1300. }
  1301. BUG_ON(i >= f->num_members);
  1302. f->arr[i] = f->arr[f->num_members - 1];
  1303. f->num_members--;
  1304. if (f->num_members == 0)
  1305. __dev_remove_pack(&f->prot_hook);
  1306. spin_unlock(&f->lock);
  1307. }
  1308. static bool match_fanout_group(struct packet_type *ptype, struct sock *sk)
  1309. {
  1310. if (sk->sk_family != PF_PACKET)
  1311. return false;
  1312. return ptype->af_packet_priv == pkt_sk(sk)->fanout;
  1313. }
  1314. static void fanout_init_data(struct packet_fanout *f)
  1315. {
  1316. switch (f->type) {
  1317. case PACKET_FANOUT_LB:
  1318. atomic_set(&f->rr_cur, 0);
  1319. break;
  1320. case PACKET_FANOUT_CBPF:
  1321. case PACKET_FANOUT_EBPF:
  1322. RCU_INIT_POINTER(f->bpf_prog, NULL);
  1323. break;
  1324. }
  1325. }
  1326. static void __fanout_set_data_bpf(struct packet_fanout *f, struct bpf_prog *new)
  1327. {
  1328. struct bpf_prog *old;
  1329. spin_lock(&f->lock);
  1330. old = rcu_dereference_protected(f->bpf_prog, lockdep_is_held(&f->lock));
  1331. rcu_assign_pointer(f->bpf_prog, new);
  1332. spin_unlock(&f->lock);
  1333. if (old) {
  1334. synchronize_net();
  1335. bpf_prog_destroy(old);
  1336. }
  1337. }
  1338. static int fanout_set_data_cbpf(struct packet_sock *po, char __user *data,
  1339. unsigned int len)
  1340. {
  1341. struct bpf_prog *new;
  1342. struct sock_fprog fprog;
  1343. int ret;
  1344. if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
  1345. return -EPERM;
  1346. if (len != sizeof(fprog))
  1347. return -EINVAL;
  1348. if (copy_from_user(&fprog, data, len))
  1349. return -EFAULT;
  1350. ret = bpf_prog_create_from_user(&new, &fprog, NULL, false);
  1351. if (ret)
  1352. return ret;
  1353. __fanout_set_data_bpf(po->fanout, new);
  1354. return 0;
  1355. }
  1356. static int fanout_set_data_ebpf(struct packet_sock *po, char __user *data,
  1357. unsigned int len)
  1358. {
  1359. struct bpf_prog *new;
  1360. u32 fd;
  1361. if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
  1362. return -EPERM;
  1363. if (len != sizeof(fd))
  1364. return -EINVAL;
  1365. if (copy_from_user(&fd, data, len))
  1366. return -EFAULT;
  1367. new = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
  1368. if (IS_ERR(new))
  1369. return PTR_ERR(new);
  1370. __fanout_set_data_bpf(po->fanout, new);
  1371. return 0;
  1372. }
  1373. static int fanout_set_data(struct packet_sock *po, char __user *data,
  1374. unsigned int len)
  1375. {
  1376. switch (po->fanout->type) {
  1377. case PACKET_FANOUT_CBPF:
  1378. return fanout_set_data_cbpf(po, data, len);
  1379. case PACKET_FANOUT_EBPF:
  1380. return fanout_set_data_ebpf(po, data, len);
  1381. default:
  1382. return -EINVAL;
  1383. };
  1384. }
  1385. static void fanout_release_data(struct packet_fanout *f)
  1386. {
  1387. switch (f->type) {
  1388. case PACKET_FANOUT_CBPF:
  1389. case PACKET_FANOUT_EBPF:
  1390. __fanout_set_data_bpf(f, NULL);
  1391. };
  1392. }
  1393. static bool __fanout_id_is_free(struct sock *sk, u16 candidate_id)
  1394. {
  1395. struct packet_fanout *f;
  1396. list_for_each_entry(f, &fanout_list, list) {
  1397. if (f->id == candidate_id &&
  1398. read_pnet(&f->net) == sock_net(sk)) {
  1399. return false;
  1400. }
  1401. }
  1402. return true;
  1403. }
  1404. static bool fanout_find_new_id(struct sock *sk, u16 *new_id)
  1405. {
  1406. u16 id = fanout_next_id;
  1407. do {
  1408. if (__fanout_id_is_free(sk, id)) {
  1409. *new_id = id;
  1410. fanout_next_id = id + 1;
  1411. return true;
  1412. }
  1413. id++;
  1414. } while (id != fanout_next_id);
  1415. return false;
  1416. }
  1417. static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
  1418. {
  1419. struct packet_rollover *rollover = NULL;
  1420. struct packet_sock *po = pkt_sk(sk);
  1421. struct packet_fanout *f, *match;
  1422. u8 type = type_flags & 0xff;
  1423. u8 flags = type_flags >> 8;
  1424. int err;
  1425. switch (type) {
  1426. case PACKET_FANOUT_ROLLOVER:
  1427. if (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)
  1428. return -EINVAL;
  1429. case PACKET_FANOUT_HASH:
  1430. case PACKET_FANOUT_LB:
  1431. case PACKET_FANOUT_CPU:
  1432. case PACKET_FANOUT_RND:
  1433. case PACKET_FANOUT_QM:
  1434. case PACKET_FANOUT_CBPF:
  1435. case PACKET_FANOUT_EBPF:
  1436. break;
  1437. default:
  1438. return -EINVAL;
  1439. }
  1440. mutex_lock(&fanout_mutex);
  1441. err = -EALREADY;
  1442. if (po->fanout)
  1443. goto out;
  1444. if (type == PACKET_FANOUT_ROLLOVER ||
  1445. (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)) {
  1446. err = -ENOMEM;
  1447. rollover = kzalloc(sizeof(*rollover), GFP_KERNEL);
  1448. if (!rollover)
  1449. goto out;
  1450. atomic_long_set(&rollover->num, 0);
  1451. atomic_long_set(&rollover->num_huge, 0);
  1452. atomic_long_set(&rollover->num_failed, 0);
  1453. }
  1454. if (type_flags & PACKET_FANOUT_FLAG_UNIQUEID) {
  1455. if (id != 0) {
  1456. err = -EINVAL;
  1457. goto out;
  1458. }
  1459. if (!fanout_find_new_id(sk, &id)) {
  1460. err = -ENOMEM;
  1461. goto out;
  1462. }
  1463. /* ephemeral flag for the first socket in the group: drop it */
  1464. flags &= ~(PACKET_FANOUT_FLAG_UNIQUEID >> 8);
  1465. }
  1466. match = NULL;
  1467. list_for_each_entry(f, &fanout_list, list) {
  1468. if (f->id == id &&
  1469. read_pnet(&f->net) == sock_net(sk)) {
  1470. match = f;
  1471. break;
  1472. }
  1473. }
  1474. err = -EINVAL;
  1475. if (match && match->flags != flags)
  1476. goto out;
  1477. if (!match) {
  1478. err = -ENOMEM;
  1479. match = kzalloc(sizeof(*match), GFP_KERNEL);
  1480. if (!match)
  1481. goto out;
  1482. write_pnet(&match->net, sock_net(sk));
  1483. match->id = id;
  1484. match->type = type;
  1485. match->flags = flags;
  1486. INIT_LIST_HEAD(&match->list);
  1487. spin_lock_init(&match->lock);
  1488. refcount_set(&match->sk_ref, 0);
  1489. fanout_init_data(match);
  1490. match->prot_hook.type = po->prot_hook.type;
  1491. match->prot_hook.dev = po->prot_hook.dev;
  1492. match->prot_hook.func = packet_rcv_fanout;
  1493. match->prot_hook.af_packet_priv = match;
  1494. match->prot_hook.id_match = match_fanout_group;
  1495. list_add(&match->list, &fanout_list);
  1496. }
  1497. err = -EINVAL;
  1498. spin_lock(&po->bind_lock);
  1499. if (po->running &&
  1500. match->type == type &&
  1501. match->prot_hook.type == po->prot_hook.type &&
  1502. match->prot_hook.dev == po->prot_hook.dev) {
  1503. err = -ENOSPC;
  1504. if (refcount_read(&match->sk_ref) < PACKET_FANOUT_MAX) {
  1505. __dev_remove_pack(&po->prot_hook);
  1506. po->fanout = match;
  1507. po->rollover = rollover;
  1508. rollover = NULL;
  1509. refcount_set(&match->sk_ref, refcount_read(&match->sk_ref) + 1);
  1510. __fanout_link(sk, po);
  1511. err = 0;
  1512. }
  1513. }
  1514. spin_unlock(&po->bind_lock);
  1515. if (err && !refcount_read(&match->sk_ref)) {
  1516. list_del(&match->list);
  1517. kfree(match);
  1518. }
  1519. out:
  1520. kfree(rollover);
  1521. mutex_unlock(&fanout_mutex);
  1522. return err;
  1523. }
  1524. /* If pkt_sk(sk)->fanout->sk_ref is zero, this function removes
  1525. * pkt_sk(sk)->fanout from fanout_list and returns pkt_sk(sk)->fanout.
  1526. * It is the responsibility of the caller to call fanout_release_data() and
  1527. * free the returned packet_fanout (after synchronize_net())
  1528. */
  1529. static struct packet_fanout *fanout_release(struct sock *sk)
  1530. {
  1531. struct packet_sock *po = pkt_sk(sk);
  1532. struct packet_fanout *f;
  1533. mutex_lock(&fanout_mutex);
  1534. f = po->fanout;
  1535. if (f) {
  1536. po->fanout = NULL;
  1537. if (refcount_dec_and_test(&f->sk_ref))
  1538. list_del(&f->list);
  1539. else
  1540. f = NULL;
  1541. }
  1542. mutex_unlock(&fanout_mutex);
  1543. return f;
  1544. }
  1545. static bool packet_extra_vlan_len_allowed(const struct net_device *dev,
  1546. struct sk_buff *skb)
  1547. {
  1548. /* Earlier code assumed this would be a VLAN pkt, double-check
  1549. * this now that we have the actual packet in hand. We can only
  1550. * do this check on Ethernet devices.
  1551. */
  1552. if (unlikely(dev->type != ARPHRD_ETHER))
  1553. return false;
  1554. skb_reset_mac_header(skb);
  1555. return likely(eth_hdr(skb)->h_proto == htons(ETH_P_8021Q));
  1556. }
  1557. static const struct proto_ops packet_ops;
  1558. static const struct proto_ops packet_ops_spkt;
  1559. static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
  1560. struct packet_type *pt, struct net_device *orig_dev)
  1561. {
  1562. struct sock *sk;
  1563. struct sockaddr_pkt *spkt;
  1564. /*
  1565. * When we registered the protocol we saved the socket in the data
  1566. * field for just this event.
  1567. */
  1568. sk = pt->af_packet_priv;
  1569. /*
  1570. * Yank back the headers [hope the device set this
  1571. * right or kerboom...]
  1572. *
  1573. * Incoming packets have ll header pulled,
  1574. * push it back.
  1575. *
  1576. * For outgoing ones skb->data == skb_mac_header(skb)
  1577. * so that this procedure is noop.
  1578. */
  1579. if (skb->pkt_type == PACKET_LOOPBACK)
  1580. goto out;
  1581. if (!net_eq(dev_net(dev), sock_net(sk)))
  1582. goto out;
  1583. skb = skb_share_check(skb, GFP_ATOMIC);
  1584. if (skb == NULL)
  1585. goto oom;
  1586. /* drop any routing info */
  1587. skb_dst_drop(skb);
  1588. /* drop conntrack reference */
  1589. nf_reset(skb);
  1590. spkt = &PACKET_SKB_CB(skb)->sa.pkt;
  1591. skb_push(skb, skb->data - skb_mac_header(skb));
  1592. /*
  1593. * The SOCK_PACKET socket receives _all_ frames.
  1594. */
  1595. spkt->spkt_family = dev->type;
  1596. strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
  1597. spkt->spkt_protocol = skb->protocol;
  1598. /*
  1599. * Charge the memory to the socket. This is done specifically
  1600. * to prevent sockets using all the memory up.
  1601. */
  1602. if (sock_queue_rcv_skb(sk, skb) == 0)
  1603. return 0;
  1604. out:
  1605. kfree_skb(skb);
  1606. oom:
  1607. return 0;
  1608. }
  1609. /*
  1610. * Output a raw packet to a device layer. This bypasses all the other
  1611. * protocol layers and you must therefore supply it with a complete frame
  1612. */
  1613. static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
  1614. size_t len)
  1615. {
  1616. struct sock *sk = sock->sk;
  1617. DECLARE_SOCKADDR(struct sockaddr_pkt *, saddr, msg->msg_name);
  1618. struct sk_buff *skb = NULL;
  1619. struct net_device *dev;
  1620. struct sockcm_cookie sockc;
  1621. __be16 proto = 0;
  1622. int err;
  1623. int extra_len = 0;
  1624. /*
  1625. * Get and verify the address.
  1626. */
  1627. if (saddr) {
  1628. if (msg->msg_namelen < sizeof(struct sockaddr))
  1629. return -EINVAL;
  1630. if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
  1631. proto = saddr->spkt_protocol;
  1632. } else
  1633. return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
  1634. /*
  1635. * Find the device first to size check it
  1636. */
  1637. saddr->spkt_device[sizeof(saddr->spkt_device) - 1] = 0;
  1638. retry:
  1639. rcu_read_lock();
  1640. dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
  1641. err = -ENODEV;
  1642. if (dev == NULL)
  1643. goto out_unlock;
  1644. err = -ENETDOWN;
  1645. if (!(dev->flags & IFF_UP))
  1646. goto out_unlock;
  1647. /*
  1648. * You may not queue a frame bigger than the mtu. This is the lowest level
  1649. * raw protocol and you must do your own fragmentation at this level.
  1650. */
  1651. if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
  1652. if (!netif_supports_nofcs(dev)) {
  1653. err = -EPROTONOSUPPORT;
  1654. goto out_unlock;
  1655. }
  1656. extra_len = 4; /* We're doing our own CRC */
  1657. }
  1658. err = -EMSGSIZE;
  1659. if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN + extra_len)
  1660. goto out_unlock;
  1661. if (!skb) {
  1662. size_t reserved = LL_RESERVED_SPACE(dev);
  1663. int tlen = dev->needed_tailroom;
  1664. unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
  1665. rcu_read_unlock();
  1666. skb = sock_wmalloc(sk, len + reserved + tlen, 0, GFP_KERNEL);
  1667. if (skb == NULL)
  1668. return -ENOBUFS;
  1669. /* FIXME: Save some space for broken drivers that write a hard
  1670. * header at transmission time by themselves. PPP is the notable
  1671. * one here. This should really be fixed at the driver level.
  1672. */
  1673. skb_reserve(skb, reserved);
  1674. skb_reset_network_header(skb);
  1675. /* Try to align data part correctly */
  1676. if (hhlen) {
  1677. skb->data -= hhlen;
  1678. skb->tail -= hhlen;
  1679. if (len < hhlen)
  1680. skb_reset_network_header(skb);
  1681. }
  1682. err = memcpy_from_msg(skb_put(skb, len), msg, len);
  1683. if (err)
  1684. goto out_free;
  1685. goto retry;
  1686. }
  1687. if (!dev_validate_header(dev, skb->data, len)) {
  1688. err = -EINVAL;
  1689. goto out_unlock;
  1690. }
  1691. if (len > (dev->mtu + dev->hard_header_len + extra_len) &&
  1692. !packet_extra_vlan_len_allowed(dev, skb)) {
  1693. err = -EMSGSIZE;
  1694. goto out_unlock;
  1695. }
  1696. sockc.tsflags = sk->sk_tsflags;
  1697. if (msg->msg_controllen) {
  1698. err = sock_cmsg_send(sk, msg, &sockc);
  1699. if (unlikely(err))
  1700. goto out_unlock;
  1701. }
  1702. skb->protocol = proto;
  1703. skb->dev = dev;
  1704. skb->priority = sk->sk_priority;
  1705. skb->mark = sk->sk_mark;
  1706. sock_tx_timestamp(sk, sockc.tsflags, &skb_shinfo(skb)->tx_flags);
  1707. if (unlikely(extra_len == 4))
  1708. skb->no_fcs = 1;
  1709. skb_probe_transport_header(skb, 0);
  1710. dev_queue_xmit(skb);
  1711. rcu_read_unlock();
  1712. return len;
  1713. out_unlock:
  1714. rcu_read_unlock();
  1715. out_free:
  1716. kfree_skb(skb);
  1717. return err;
  1718. }
  1719. static unsigned int run_filter(struct sk_buff *skb,
  1720. const struct sock *sk,
  1721. unsigned int res)
  1722. {
  1723. struct sk_filter *filter;
  1724. rcu_read_lock();
  1725. filter = rcu_dereference(sk->sk_filter);
  1726. if (filter != NULL)
  1727. res = bpf_prog_run_clear_cb(filter->prog, skb);
  1728. rcu_read_unlock();
  1729. return res;
  1730. }
  1731. static int packet_rcv_vnet(struct msghdr *msg, const struct sk_buff *skb,
  1732. size_t *len)
  1733. {
  1734. struct virtio_net_hdr vnet_hdr;
  1735. if (*len < sizeof(vnet_hdr))
  1736. return -EINVAL;
  1737. *len -= sizeof(vnet_hdr);
  1738. if (virtio_net_hdr_from_skb(skb, &vnet_hdr, vio_le(), true, 0))
  1739. return -EINVAL;
  1740. return memcpy_to_msg(msg, (void *)&vnet_hdr, sizeof(vnet_hdr));
  1741. }
  1742. /*
  1743. * This function makes lazy skb cloning in hope that most of packets
  1744. * are discarded by BPF.
  1745. *
  1746. * Note tricky part: we DO mangle shared skb! skb->data, skb->len
  1747. * and skb->cb are mangled. It works because (and until) packets
  1748. * falling here are owned by current CPU. Output packets are cloned
  1749. * by dev_queue_xmit_nit(), input packets are processed by net_bh
  1750. * sequencially, so that if we return skb to original state on exit,
  1751. * we will not harm anyone.
  1752. */
  1753. static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
  1754. struct packet_type *pt, struct net_device *orig_dev)
  1755. {
  1756. struct sock *sk;
  1757. struct sockaddr_ll *sll;
  1758. struct packet_sock *po;
  1759. u8 *skb_head = skb->data;
  1760. int skb_len = skb->len;
  1761. unsigned int snaplen, res;
  1762. bool is_drop_n_account = false;
  1763. if (skb->pkt_type == PACKET_LOOPBACK)
  1764. goto drop;
  1765. sk = pt->af_packet_priv;
  1766. po = pkt_sk(sk);
  1767. if (!net_eq(dev_net(dev), sock_net(sk)))
  1768. goto drop;
  1769. skb->dev = dev;
  1770. if (dev->header_ops) {
  1771. /* The device has an explicit notion of ll header,
  1772. * exported to higher levels.
  1773. *
  1774. * Otherwise, the device hides details of its frame
  1775. * structure, so that corresponding packet head is
  1776. * never delivered to user.
  1777. */
  1778. if (sk->sk_type != SOCK_DGRAM)
  1779. skb_push(skb, skb->data - skb_mac_header(skb));
  1780. else if (skb->pkt_type == PACKET_OUTGOING) {
  1781. /* Special case: outgoing packets have ll header at head */
  1782. skb_pull(skb, skb_network_offset(skb));
  1783. }
  1784. }
  1785. snaplen = skb->len;
  1786. res = run_filter(skb, sk, snaplen);
  1787. if (!res)
  1788. goto drop_n_restore;
  1789. if (snaplen > res)
  1790. snaplen = res;
  1791. if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
  1792. goto drop_n_acct;
  1793. if (skb_shared(skb)) {
  1794. struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
  1795. if (nskb == NULL)
  1796. goto drop_n_acct;
  1797. if (skb_head != skb->data) {
  1798. skb->data = skb_head;
  1799. skb->len = skb_len;
  1800. }
  1801. consume_skb(skb);
  1802. skb = nskb;
  1803. }
  1804. sock_skb_cb_check_size(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8);
  1805. sll = &PACKET_SKB_CB(skb)->sa.ll;
  1806. sll->sll_hatype = dev->type;
  1807. sll->sll_pkttype = skb->pkt_type;
  1808. if (unlikely(po->origdev))
  1809. sll->sll_ifindex = orig_dev->ifindex;
  1810. else
  1811. sll->sll_ifindex = dev->ifindex;
  1812. sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
  1813. /* sll->sll_family and sll->sll_protocol are set in packet_recvmsg().
  1814. * Use their space for storing the original skb length.
  1815. */
  1816. PACKET_SKB_CB(skb)->sa.origlen = skb->len;
  1817. if (pskb_trim(skb, snaplen))
  1818. goto drop_n_acct;
  1819. skb_set_owner_r(skb, sk);
  1820. skb->dev = NULL;
  1821. skb_dst_drop(skb);
  1822. /* drop conntrack reference */
  1823. nf_reset(skb);
  1824. spin_lock(&sk->sk_receive_queue.lock);
  1825. po->stats.stats1.tp_packets++;
  1826. sock_skb_set_dropcount(sk, skb);
  1827. __skb_queue_tail(&sk->sk_receive_queue, skb);
  1828. spin_unlock(&sk->sk_receive_queue.lock);
  1829. sk->sk_data_ready(sk);
  1830. return 0;
  1831. drop_n_acct:
  1832. is_drop_n_account = true;
  1833. spin_lock(&sk->sk_receive_queue.lock);
  1834. po->stats.stats1.tp_drops++;
  1835. atomic_inc(&sk->sk_drops);
  1836. spin_unlock(&sk->sk_receive_queue.lock);
  1837. drop_n_restore:
  1838. if (skb_head != skb->data && skb_shared(skb)) {
  1839. skb->data = skb_head;
  1840. skb->len = skb_len;
  1841. }
  1842. drop:
  1843. if (!is_drop_n_account)
  1844. consume_skb(skb);
  1845. else
  1846. kfree_skb(skb);
  1847. return 0;
  1848. }
  1849. static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
  1850. struct packet_type *pt, struct net_device *orig_dev)
  1851. {
  1852. struct sock *sk;
  1853. struct packet_sock *po;
  1854. struct sockaddr_ll *sll;
  1855. union tpacket_uhdr h;
  1856. u8 *skb_head = skb->data;
  1857. int skb_len = skb->len;
  1858. unsigned int snaplen, res;
  1859. unsigned long status = TP_STATUS_USER;
  1860. unsigned short macoff, hdrlen;
  1861. unsigned int netoff;
  1862. struct sk_buff *copy_skb = NULL;
  1863. struct timespec ts;
  1864. __u32 ts_status;
  1865. bool is_drop_n_account = false;
  1866. unsigned int slot_id = 0;
  1867. bool do_vnet = false;
  1868. /* struct tpacket{2,3}_hdr is aligned to a multiple of TPACKET_ALIGNMENT.
  1869. * We may add members to them until current aligned size without forcing
  1870. * userspace to call getsockopt(..., PACKET_HDRLEN, ...).
  1871. */
  1872. BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h2)) != 32);
  1873. BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h3)) != 48);
  1874. if (skb->pkt_type == PACKET_LOOPBACK)
  1875. goto drop;
  1876. sk = pt->af_packet_priv;
  1877. po = pkt_sk(sk);
  1878. if (!net_eq(dev_net(dev), sock_net(sk)))
  1879. goto drop;
  1880. if (dev->header_ops) {
  1881. if (sk->sk_type != SOCK_DGRAM)
  1882. skb_push(skb, skb->data - skb_mac_header(skb));
  1883. else if (skb->pkt_type == PACKET_OUTGOING) {
  1884. /* Special case: outgoing packets have ll header at head */
  1885. skb_pull(skb, skb_network_offset(skb));
  1886. }
  1887. }
  1888. //gro on: clatd checksum fail patch
  1889. //if is nornal and gro packet, not calculate tcp's checksum
  1890. if (skb->ip_summed == CHECKSUM_UNNECESSARY ||
  1891. (NAPI_GRO_CB(skb)->count > 1 &&
  1892. (skb->dev && skb->dev->features & (1 << NETIF_F_GRO_BIT))))
  1893. status |= TP_STATUS_CSUM_VALID;
  1894. snaplen = skb->len;
  1895. res = run_filter(skb, sk, snaplen);
  1896. if (!res)
  1897. goto drop_n_restore;
  1898. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1899. status |= TP_STATUS_CSUMNOTREADY;
  1900. else if (skb->pkt_type != PACKET_OUTGOING &&
  1901. (skb->ip_summed == CHECKSUM_COMPLETE ||
  1902. skb_csum_unnecessary(skb)))
  1903. status |= TP_STATUS_CSUM_VALID;
  1904. if (snaplen > res)
  1905. snaplen = res;
  1906. if (sk->sk_type == SOCK_DGRAM) {
  1907. macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
  1908. po->tp_reserve;
  1909. } else {
  1910. unsigned int maclen = skb_network_offset(skb);
  1911. netoff = TPACKET_ALIGN(po->tp_hdrlen +
  1912. (maclen < 16 ? 16 : maclen)) +
  1913. po->tp_reserve;
  1914. if (po->has_vnet_hdr) {
  1915. netoff += sizeof(struct virtio_net_hdr);
  1916. do_vnet = true;
  1917. }
  1918. macoff = netoff - maclen;
  1919. }
  1920. if (netoff > USHRT_MAX) {
  1921. spin_lock(&sk->sk_receive_queue.lock);
  1922. po->stats.stats1.tp_drops++;
  1923. spin_unlock(&sk->sk_receive_queue.lock);
  1924. goto drop_n_restore;
  1925. }
  1926. if (po->tp_version <= TPACKET_V2) {
  1927. if (macoff + snaplen > po->rx_ring.frame_size) {
  1928. if (po->copy_thresh &&
  1929. atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
  1930. if (skb_shared(skb)) {
  1931. copy_skb = skb_clone(skb, GFP_ATOMIC);
  1932. } else {
  1933. copy_skb = skb_get(skb);
  1934. skb_head = skb->data;
  1935. }
  1936. if (copy_skb)
  1937. skb_set_owner_r(copy_skb, sk);
  1938. }
  1939. snaplen = po->rx_ring.frame_size - macoff;
  1940. if ((int)snaplen < 0) {
  1941. snaplen = 0;
  1942. do_vnet = false;
  1943. }
  1944. }
  1945. } else if (unlikely(macoff + snaplen >
  1946. GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len)) {
  1947. u32 nval;
  1948. nval = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len - macoff;
  1949. pr_err_once("tpacket_rcv: packet too big, clamped from %u to %u. macoff=%u\n",
  1950. snaplen, nval, macoff);
  1951. snaplen = nval;
  1952. if (unlikely((int)snaplen < 0)) {
  1953. snaplen = 0;
  1954. macoff = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len;
  1955. do_vnet = false;
  1956. }
  1957. }
  1958. spin_lock(&sk->sk_receive_queue.lock);
  1959. h.raw = packet_current_rx_frame(po, skb,
  1960. TP_STATUS_KERNEL, (macoff+snaplen));
  1961. if (!h.raw)
  1962. goto drop_n_account;
  1963. if (po->tp_version <= TPACKET_V2) {
  1964. slot_id = po->rx_ring.head;
  1965. if (test_bit(slot_id, po->rx_ring.rx_owner_map))
  1966. goto drop_n_account;
  1967. __set_bit(slot_id, po->rx_ring.rx_owner_map);
  1968. }
  1969. if (do_vnet &&
  1970. virtio_net_hdr_from_skb(skb, h.raw + macoff -
  1971. sizeof(struct virtio_net_hdr),
  1972. vio_le(), true, 0)) {
  1973. if (po->tp_version == TPACKET_V3)
  1974. prb_clear_blk_fill_status(&po->rx_ring);
  1975. goto drop_n_account;
  1976. }
  1977. if (po->tp_version <= TPACKET_V2) {
  1978. packet_increment_rx_head(po, &po->rx_ring);
  1979. /*
  1980. * LOSING will be reported till you read the stats,
  1981. * because it's COR - Clear On Read.
  1982. * Anyways, moving it for V1/V2 only as V3 doesn't need this
  1983. * at packet level.
  1984. */
  1985. if (po->stats.stats1.tp_drops)
  1986. status |= TP_STATUS_LOSING;
  1987. }
  1988. po->stats.stats1.tp_packets++;
  1989. if (copy_skb) {
  1990. status |= TP_STATUS_COPY;
  1991. __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
  1992. }
  1993. spin_unlock(&sk->sk_receive_queue.lock);
  1994. skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
  1995. if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
  1996. getnstimeofday(&ts);
  1997. status |= ts_status;
  1998. switch (po->tp_version) {
  1999. case TPACKET_V1:
  2000. h.h1->tp_len = skb->len;
  2001. h.h1->tp_snaplen = snaplen;
  2002. h.h1->tp_mac = macoff;
  2003. h.h1->tp_net = netoff;
  2004. h.h1->tp_sec = ts.tv_sec;
  2005. h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
  2006. hdrlen = sizeof(*h.h1);
  2007. break;
  2008. case TPACKET_V2:
  2009. h.h2->tp_len = skb->len;
  2010. h.h2->tp_snaplen = snaplen;
  2011. h.h2->tp_mac = macoff;
  2012. h.h2->tp_net = netoff;
  2013. h.h2->tp_sec = ts.tv_sec;
  2014. h.h2->tp_nsec = ts.tv_nsec;
  2015. if (skb_vlan_tag_present(skb)) {
  2016. h.h2->tp_vlan_tci = skb_vlan_tag_get(skb);
  2017. h.h2->tp_vlan_tpid = ntohs(skb->vlan_proto);
  2018. status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
  2019. } else {
  2020. h.h2->tp_vlan_tci = 0;
  2021. h.h2->tp_vlan_tpid = 0;
  2022. }
  2023. memset(h.h2->tp_padding, 0, sizeof(h.h2->tp_padding));
  2024. hdrlen = sizeof(*h.h2);
  2025. break;
  2026. case TPACKET_V3:
  2027. /* tp_nxt_offset,vlan are already populated above.
  2028. * So DONT clear those fields here
  2029. */
  2030. h.h3->tp_status |= status;
  2031. h.h3->tp_len = skb->len;
  2032. h.h3->tp_snaplen = snaplen;
  2033. h.h3->tp_mac = macoff;
  2034. h.h3->tp_net = netoff;
  2035. h.h3->tp_sec = ts.tv_sec;
  2036. h.h3->tp_nsec = ts.tv_nsec;
  2037. memset(h.h3->tp_padding, 0, sizeof(h.h3->tp_padding));
  2038. hdrlen = sizeof(*h.h3);
  2039. break;
  2040. default:
  2041. BUG();
  2042. }
  2043. sll = h.raw + TPACKET_ALIGN(hdrlen);
  2044. sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
  2045. sll->sll_family = AF_PACKET;
  2046. sll->sll_hatype = dev->type;
  2047. sll->sll_protocol = skb->protocol;
  2048. sll->sll_pkttype = skb->pkt_type;
  2049. if (unlikely(po->origdev))
  2050. sll->sll_ifindex = orig_dev->ifindex;
  2051. else
  2052. sll->sll_ifindex = dev->ifindex;
  2053. smp_mb();
  2054. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
  2055. if (po->tp_version <= TPACKET_V2) {
  2056. u8 *start, *end;
  2057. end = (u8 *) PAGE_ALIGN((unsigned long) h.raw +
  2058. macoff + snaplen);
  2059. for (start = h.raw; start < end; start += PAGE_SIZE)
  2060. flush_dcache_page(pgv_to_page(start));
  2061. }
  2062. smp_wmb();
  2063. #endif
  2064. if (po->tp_version <= TPACKET_V2) {
  2065. spin_lock(&sk->sk_receive_queue.lock);
  2066. __packet_set_status(po, h.raw, status);
  2067. __clear_bit(slot_id, po->rx_ring.rx_owner_map);
  2068. spin_unlock(&sk->sk_receive_queue.lock);
  2069. sk->sk_data_ready(sk);
  2070. } else if (po->tp_version == TPACKET_V3) {
  2071. prb_clear_blk_fill_status(&po->rx_ring);
  2072. }
  2073. drop_n_restore:
  2074. if (skb_head != skb->data && skb_shared(skb)) {
  2075. skb->data = skb_head;
  2076. skb->len = skb_len;
  2077. }
  2078. drop:
  2079. if (!is_drop_n_account)
  2080. consume_skb(skb);
  2081. else
  2082. kfree_skb(skb);
  2083. return 0;
  2084. drop_n_account:
  2085. is_drop_n_account = true;
  2086. po->stats.stats1.tp_drops++;
  2087. spin_unlock(&sk->sk_receive_queue.lock);
  2088. sk->sk_data_ready(sk);
  2089. kfree_skb(copy_skb);
  2090. goto drop_n_restore;
  2091. }
  2092. static void tpacket_destruct_skb(struct sk_buff *skb)
  2093. {
  2094. struct packet_sock *po = pkt_sk(skb->sk);
  2095. if (likely(po->tx_ring.pg_vec)) {
  2096. void *ph;
  2097. __u32 ts;
  2098. ph = skb_zcopy_get_nouarg(skb);
  2099. packet_dec_pending(&po->tx_ring);
  2100. ts = __packet_set_timestamp(po, ph, skb);
  2101. __packet_set_status(po, ph, TP_STATUS_AVAILABLE | ts);
  2102. if (!packet_read_pending(&po->tx_ring))
  2103. complete(&po->skb_completion);
  2104. }
  2105. sock_wfree(skb);
  2106. }
  2107. static void tpacket_set_protocol(const struct net_device *dev,
  2108. struct sk_buff *skb)
  2109. {
  2110. if (dev->type == ARPHRD_ETHER) {
  2111. skb_reset_mac_header(skb);
  2112. skb->protocol = eth_hdr(skb)->h_proto;
  2113. }
  2114. }
  2115. static int __packet_snd_vnet_parse(struct virtio_net_hdr *vnet_hdr, size_t len)
  2116. {
  2117. if ((vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  2118. (__virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
  2119. __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2 >
  2120. __virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len)))
  2121. vnet_hdr->hdr_len = __cpu_to_virtio16(vio_le(),
  2122. __virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
  2123. __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2);
  2124. if (__virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len) > len)
  2125. return -EINVAL;
  2126. return 0;
  2127. }
  2128. static int packet_snd_vnet_parse(struct msghdr *msg, size_t *len,
  2129. struct virtio_net_hdr *vnet_hdr)
  2130. {
  2131. if (*len < sizeof(*vnet_hdr))
  2132. return -EINVAL;
  2133. *len -= sizeof(*vnet_hdr);
  2134. if (!copy_from_iter_full(vnet_hdr, sizeof(*vnet_hdr), &msg->msg_iter))
  2135. return -EFAULT;
  2136. return __packet_snd_vnet_parse(vnet_hdr, *len);
  2137. }
  2138. static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
  2139. void *frame, struct net_device *dev, void *data, int tp_len,
  2140. __be16 proto, unsigned char *addr, int hlen, int copylen,
  2141. const struct sockcm_cookie *sockc)
  2142. {
  2143. union tpacket_uhdr ph;
  2144. int to_write, offset, len, nr_frags, len_max;
  2145. struct socket *sock = po->sk.sk_socket;
  2146. struct page *page;
  2147. int err;
  2148. ph.raw = frame;
  2149. skb->protocol = proto;
  2150. skb->dev = dev;
  2151. skb->priority = po->sk.sk_priority;
  2152. skb->mark = po->sk.sk_mark;
  2153. sock_tx_timestamp(&po->sk, sockc->tsflags, &skb_shinfo(skb)->tx_flags);
  2154. skb_zcopy_set_nouarg(skb, ph.raw);
  2155. skb_reserve(skb, hlen);
  2156. skb_reset_network_header(skb);
  2157. to_write = tp_len;
  2158. if (sock->type == SOCK_DGRAM) {
  2159. err = dev_hard_header(skb, dev, ntohs(proto), addr,
  2160. NULL, tp_len);
  2161. if (unlikely(err < 0))
  2162. return -EINVAL;
  2163. } else if (copylen) {
  2164. int hdrlen = min_t(int, copylen, tp_len);
  2165. skb_push(skb, dev->hard_header_len);
  2166. skb_put(skb, copylen - dev->hard_header_len);
  2167. err = skb_store_bits(skb, 0, data, hdrlen);
  2168. if (unlikely(err))
  2169. return err;
  2170. if (!dev_validate_header(dev, skb->data, hdrlen))
  2171. return -EINVAL;
  2172. if (!skb->protocol)
  2173. tpacket_set_protocol(dev, skb);
  2174. data += hdrlen;
  2175. to_write -= hdrlen;
  2176. }
  2177. offset = offset_in_page(data);
  2178. len_max = PAGE_SIZE - offset;
  2179. len = ((to_write > len_max) ? len_max : to_write);
  2180. skb->data_len = to_write;
  2181. skb->len += to_write;
  2182. skb->truesize += to_write;
  2183. refcount_add(to_write, &po->sk.sk_wmem_alloc);
  2184. while (likely(to_write)) {
  2185. nr_frags = skb_shinfo(skb)->nr_frags;
  2186. if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
  2187. pr_err("Packet exceed the number of skb frags(%lu)\n",
  2188. MAX_SKB_FRAGS);
  2189. return -EFAULT;
  2190. }
  2191. page = pgv_to_page(data);
  2192. data += len;
  2193. flush_dcache_page(page);
  2194. get_page(page);
  2195. skb_fill_page_desc(skb, nr_frags, page, offset, len);
  2196. to_write -= len;
  2197. offset = 0;
  2198. len_max = PAGE_SIZE;
  2199. len = ((to_write > len_max) ? len_max : to_write);
  2200. }
  2201. skb_probe_transport_header(skb, 0);
  2202. return tp_len;
  2203. }
  2204. static int tpacket_parse_header(struct packet_sock *po, void *frame,
  2205. int size_max, void **data)
  2206. {
  2207. union tpacket_uhdr ph;
  2208. int tp_len, off;
  2209. ph.raw = frame;
  2210. switch (po->tp_version) {
  2211. case TPACKET_V3:
  2212. if (ph.h3->tp_next_offset != 0) {
  2213. pr_warn_once("variable sized slot not supported");
  2214. return -EINVAL;
  2215. }
  2216. tp_len = ph.h3->tp_len;
  2217. break;
  2218. case TPACKET_V2:
  2219. tp_len = ph.h2->tp_len;
  2220. break;
  2221. default:
  2222. tp_len = ph.h1->tp_len;
  2223. break;
  2224. }
  2225. if (unlikely(tp_len > size_max)) {
  2226. pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
  2227. return -EMSGSIZE;
  2228. }
  2229. if (unlikely(po->tp_tx_has_off)) {
  2230. int off_min, off_max;
  2231. off_min = po->tp_hdrlen - sizeof(struct sockaddr_ll);
  2232. off_max = po->tx_ring.frame_size - tp_len;
  2233. if (po->sk.sk_type == SOCK_DGRAM) {
  2234. switch (po->tp_version) {
  2235. case TPACKET_V3:
  2236. off = ph.h3->tp_net;
  2237. break;
  2238. case TPACKET_V2:
  2239. off = ph.h2->tp_net;
  2240. break;
  2241. default:
  2242. off = ph.h1->tp_net;
  2243. break;
  2244. }
  2245. } else {
  2246. switch (po->tp_version) {
  2247. case TPACKET_V3:
  2248. off = ph.h3->tp_mac;
  2249. break;
  2250. case TPACKET_V2:
  2251. off = ph.h2->tp_mac;
  2252. break;
  2253. default:
  2254. off = ph.h1->tp_mac;
  2255. break;
  2256. }
  2257. }
  2258. if (unlikely((off < off_min) || (off_max < off)))
  2259. return -EINVAL;
  2260. } else {
  2261. off = po->tp_hdrlen - sizeof(struct sockaddr_ll);
  2262. }
  2263. *data = frame + off;
  2264. return tp_len;
  2265. }
  2266. static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
  2267. {
  2268. struct sk_buff *skb = NULL;
  2269. struct net_device *dev;
  2270. struct virtio_net_hdr *vnet_hdr = NULL;
  2271. struct sockcm_cookie sockc;
  2272. __be16 proto;
  2273. int err, reserve = 0;
  2274. void *ph;
  2275. DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
  2276. bool need_wait = !(msg->msg_flags & MSG_DONTWAIT);
  2277. unsigned char *addr = NULL;
  2278. int tp_len, size_max;
  2279. void *data;
  2280. int len_sum = 0;
  2281. int status = TP_STATUS_AVAILABLE;
  2282. int hlen, tlen, copylen = 0;
  2283. long timeo = 0;
  2284. mutex_lock(&po->pg_vec_lock);
  2285. /* packet_sendmsg() check on tx_ring.pg_vec was lockless,
  2286. * we need to confirm it under protection of pg_vec_lock.
  2287. */
  2288. if (unlikely(!po->tx_ring.pg_vec)) {
  2289. err = -EBUSY;
  2290. goto out;
  2291. }
  2292. if (likely(saddr == NULL)) {
  2293. dev = packet_cached_dev_get(po);
  2294. proto = READ_ONCE(po->num);
  2295. } else {
  2296. err = -EINVAL;
  2297. if (msg->msg_namelen < sizeof(struct sockaddr_ll))
  2298. goto out;
  2299. if (msg->msg_namelen < (saddr->sll_halen
  2300. + offsetof(struct sockaddr_ll,
  2301. sll_addr)))
  2302. goto out;
  2303. proto = saddr->sll_protocol;
  2304. dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
  2305. if (po->sk.sk_socket->type == SOCK_DGRAM) {
  2306. if (dev && msg->msg_namelen < dev->addr_len +
  2307. offsetof(struct sockaddr_ll, sll_addr))
  2308. goto out_put;
  2309. addr = saddr->sll_addr;
  2310. }
  2311. }
  2312. err = -ENXIO;
  2313. if (unlikely(dev == NULL))
  2314. goto out;
  2315. err = -ENETDOWN;
  2316. if (unlikely(!(dev->flags & IFF_UP)))
  2317. goto out_put;
  2318. sockc.tsflags = po->sk.sk_tsflags;
  2319. if (msg->msg_controllen) {
  2320. err = sock_cmsg_send(&po->sk, msg, &sockc);
  2321. if (unlikely(err))
  2322. goto out_put;
  2323. }
  2324. if (po->sk.sk_socket->type == SOCK_RAW)
  2325. reserve = dev->hard_header_len;
  2326. size_max = po->tx_ring.frame_size
  2327. - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
  2328. if ((size_max > dev->mtu + reserve + VLAN_HLEN) && !po->has_vnet_hdr)
  2329. size_max = dev->mtu + reserve + VLAN_HLEN;
  2330. reinit_completion(&po->skb_completion);
  2331. do {
  2332. ph = packet_current_frame(po, &po->tx_ring,
  2333. TP_STATUS_SEND_REQUEST);
  2334. if (unlikely(ph == NULL)) {
  2335. if (need_wait && skb) {
  2336. timeo = sock_sndtimeo(&po->sk, msg->msg_flags & MSG_DONTWAIT);
  2337. timeo = wait_for_completion_interruptible_timeout(&po->skb_completion, timeo);
  2338. if (timeo <= 0) {
  2339. err = !timeo ? -ETIMEDOUT : -ERESTARTSYS;
  2340. goto out_put;
  2341. }
  2342. }
  2343. /* check for additional frames */
  2344. continue;
  2345. }
  2346. skb = NULL;
  2347. tp_len = tpacket_parse_header(po, ph, size_max, &data);
  2348. if (tp_len < 0)
  2349. goto tpacket_error;
  2350. status = TP_STATUS_SEND_REQUEST;
  2351. hlen = LL_RESERVED_SPACE(dev);
  2352. tlen = dev->needed_tailroom;
  2353. if (po->has_vnet_hdr) {
  2354. vnet_hdr = data;
  2355. data += sizeof(*vnet_hdr);
  2356. tp_len -= sizeof(*vnet_hdr);
  2357. if (tp_len < 0 ||
  2358. __packet_snd_vnet_parse(vnet_hdr, tp_len)) {
  2359. tp_len = -EINVAL;
  2360. goto tpacket_error;
  2361. }
  2362. copylen = __virtio16_to_cpu(vio_le(),
  2363. vnet_hdr->hdr_len);
  2364. }
  2365. copylen = max_t(int, copylen, dev->hard_header_len);
  2366. skb = sock_alloc_send_skb(&po->sk,
  2367. hlen + tlen + sizeof(struct sockaddr_ll) +
  2368. (copylen - dev->hard_header_len),
  2369. !need_wait, &err);
  2370. if (unlikely(skb == NULL)) {
  2371. /* we assume the socket was initially writeable ... */
  2372. if (likely(len_sum > 0))
  2373. err = len_sum;
  2374. goto out_status;
  2375. }
  2376. tp_len = tpacket_fill_skb(po, skb, ph, dev, data, tp_len, proto,
  2377. addr, hlen, copylen, &sockc);
  2378. if (likely(tp_len >= 0) &&
  2379. tp_len > dev->mtu + reserve &&
  2380. !po->has_vnet_hdr &&
  2381. !packet_extra_vlan_len_allowed(dev, skb))
  2382. tp_len = -EMSGSIZE;
  2383. if (unlikely(tp_len < 0)) {
  2384. tpacket_error:
  2385. if (po->tp_loss) {
  2386. __packet_set_status(po, ph,
  2387. TP_STATUS_AVAILABLE);
  2388. packet_increment_head(&po->tx_ring);
  2389. kfree_skb(skb);
  2390. continue;
  2391. } else {
  2392. status = TP_STATUS_WRONG_FORMAT;
  2393. err = tp_len;
  2394. goto out_status;
  2395. }
  2396. }
  2397. if (po->has_vnet_hdr) {
  2398. if (virtio_net_hdr_to_skb(skb, vnet_hdr, vio_le())) {
  2399. tp_len = -EINVAL;
  2400. goto tpacket_error;
  2401. }
  2402. virtio_net_hdr_set_proto(skb, vnet_hdr);
  2403. }
  2404. skb->destructor = tpacket_destruct_skb;
  2405. __packet_set_status(po, ph, TP_STATUS_SENDING);
  2406. packet_inc_pending(&po->tx_ring);
  2407. status = TP_STATUS_SEND_REQUEST;
  2408. err = po->xmit(skb);
  2409. if (unlikely(err > 0)) {
  2410. err = net_xmit_errno(err);
  2411. if (err && __packet_get_status(po, ph) ==
  2412. TP_STATUS_AVAILABLE) {
  2413. /* skb was destructed already */
  2414. skb = NULL;
  2415. goto out_status;
  2416. }
  2417. /*
  2418. * skb was dropped but not destructed yet;
  2419. * let's treat it like congestion or err < 0
  2420. */
  2421. err = 0;
  2422. }
  2423. packet_increment_head(&po->tx_ring);
  2424. len_sum += tp_len;
  2425. } while (likely((ph != NULL) ||
  2426. /* Note: packet_read_pending() might be slow if we have
  2427. * to call it as it's per_cpu variable, but in fast-path
  2428. * we already short-circuit the loop with the first
  2429. * condition, and luckily don't have to go that path
  2430. * anyway.
  2431. */
  2432. (need_wait && packet_read_pending(&po->tx_ring))));
  2433. err = len_sum;
  2434. goto out_put;
  2435. out_status:
  2436. __packet_set_status(po, ph, status);
  2437. kfree_skb(skb);
  2438. out_put:
  2439. dev_put(dev);
  2440. out:
  2441. mutex_unlock(&po->pg_vec_lock);
  2442. return err;
  2443. }
  2444. static struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
  2445. size_t reserve, size_t len,
  2446. size_t linear, int noblock,
  2447. int *err)
  2448. {
  2449. struct sk_buff *skb;
  2450. /* Under a page? Don't bother with paged skb. */
  2451. if (prepad + len < PAGE_SIZE || !linear)
  2452. linear = len;
  2453. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  2454. err, 0);
  2455. if (!skb)
  2456. return NULL;
  2457. skb_reserve(skb, reserve);
  2458. skb_put(skb, linear);
  2459. skb->data_len = len - linear;
  2460. skb->len += len - linear;
  2461. return skb;
  2462. }
  2463. static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
  2464. {
  2465. struct sock *sk = sock->sk;
  2466. DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
  2467. struct sk_buff *skb;
  2468. struct net_device *dev;
  2469. __be16 proto;
  2470. unsigned char *addr = NULL;
  2471. int err, reserve = 0;
  2472. struct sockcm_cookie sockc;
  2473. struct virtio_net_hdr vnet_hdr = { 0 };
  2474. int offset = 0;
  2475. struct packet_sock *po = pkt_sk(sk);
  2476. bool has_vnet_hdr = false;
  2477. int hlen, tlen, linear;
  2478. int extra_len = 0;
  2479. /*
  2480. * Get and verify the address.
  2481. */
  2482. if (likely(saddr == NULL)) {
  2483. dev = packet_cached_dev_get(po);
  2484. proto = READ_ONCE(po->num);
  2485. } else {
  2486. err = -EINVAL;
  2487. if (msg->msg_namelen < sizeof(struct sockaddr_ll))
  2488. goto out;
  2489. if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
  2490. goto out;
  2491. proto = saddr->sll_protocol;
  2492. dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
  2493. if (sock->type == SOCK_DGRAM) {
  2494. if (dev && msg->msg_namelen < dev->addr_len +
  2495. offsetof(struct sockaddr_ll, sll_addr))
  2496. goto out_unlock;
  2497. addr = saddr->sll_addr;
  2498. }
  2499. }
  2500. err = -ENXIO;
  2501. if (unlikely(dev == NULL))
  2502. goto out_unlock;
  2503. err = -ENETDOWN;
  2504. if (unlikely(!(dev->flags & IFF_UP)))
  2505. goto out_unlock;
  2506. sockc.tsflags = sk->sk_tsflags;
  2507. sockc.mark = sk->sk_mark;
  2508. if (msg->msg_controllen) {
  2509. err = sock_cmsg_send(sk, msg, &sockc);
  2510. if (unlikely(err))
  2511. goto out_unlock;
  2512. }
  2513. if (sock->type == SOCK_RAW)
  2514. reserve = dev->hard_header_len;
  2515. if (po->has_vnet_hdr) {
  2516. err = packet_snd_vnet_parse(msg, &len, &vnet_hdr);
  2517. if (err)
  2518. goto out_unlock;
  2519. has_vnet_hdr = true;
  2520. }
  2521. if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
  2522. if (!netif_supports_nofcs(dev)) {
  2523. err = -EPROTONOSUPPORT;
  2524. goto out_unlock;
  2525. }
  2526. extra_len = 4; /* We're doing our own CRC */
  2527. }
  2528. err = -EMSGSIZE;
  2529. if (!vnet_hdr.gso_type &&
  2530. (len > dev->mtu + reserve + VLAN_HLEN + extra_len))
  2531. goto out_unlock;
  2532. err = -ENOBUFS;
  2533. hlen = LL_RESERVED_SPACE(dev);
  2534. tlen = dev->needed_tailroom;
  2535. linear = __virtio16_to_cpu(vio_le(), vnet_hdr.hdr_len);
  2536. linear = max(linear, min_t(int, len, dev->hard_header_len));
  2537. skb = packet_alloc_skb(sk, hlen + tlen, hlen, len, linear,
  2538. msg->msg_flags & MSG_DONTWAIT, &err);
  2539. if (skb == NULL)
  2540. goto out_unlock;
  2541. skb_reset_network_header(skb);
  2542. err = -EINVAL;
  2543. if (sock->type == SOCK_DGRAM) {
  2544. offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len);
  2545. if (unlikely(offset < 0))
  2546. goto out_free;
  2547. } else if (reserve) {
  2548. skb_reserve(skb, -reserve);
  2549. if (len < reserve)
  2550. skb_reset_network_header(skb);
  2551. }
  2552. /* Returns -EFAULT on error */
  2553. err = skb_copy_datagram_from_iter(skb, offset, &msg->msg_iter, len);
  2554. if (err)
  2555. goto out_free;
  2556. if (sock->type == SOCK_RAW &&
  2557. !dev_validate_header(dev, skb->data, len)) {
  2558. err = -EINVAL;
  2559. goto out_free;
  2560. }
  2561. sock_tx_timestamp(sk, sockc.tsflags, &skb_shinfo(skb)->tx_flags);
  2562. if (!vnet_hdr.gso_type && (len > dev->mtu + reserve + extra_len) &&
  2563. !packet_extra_vlan_len_allowed(dev, skb)) {
  2564. err = -EMSGSIZE;
  2565. goto out_free;
  2566. }
  2567. skb->protocol = proto;
  2568. skb->dev = dev;
  2569. skb->priority = sk->sk_priority;
  2570. skb->mark = sockc.mark;
  2571. if (has_vnet_hdr) {
  2572. err = virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le());
  2573. if (err)
  2574. goto out_free;
  2575. len += sizeof(vnet_hdr);
  2576. virtio_net_hdr_set_proto(skb, &vnet_hdr);
  2577. }
  2578. skb_probe_transport_header(skb, reserve);
  2579. if (unlikely(extra_len == 4))
  2580. skb->no_fcs = 1;
  2581. err = po->xmit(skb);
  2582. if (err > 0 && (err = net_xmit_errno(err)) != 0)
  2583. goto out_unlock;
  2584. dev_put(dev);
  2585. return len;
  2586. out_free:
  2587. kfree_skb(skb);
  2588. out_unlock:
  2589. if (dev)
  2590. dev_put(dev);
  2591. out:
  2592. return err;
  2593. }
  2594. static int packet_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  2595. {
  2596. struct sock *sk = sock->sk;
  2597. struct packet_sock *po = pkt_sk(sk);
  2598. if (po->tx_ring.pg_vec)
  2599. return tpacket_snd(po, msg);
  2600. else
  2601. return packet_snd(sock, msg, len);
  2602. }
  2603. /*
  2604. * Close a PACKET socket. This is fairly simple. We immediately go
  2605. * to 'closed' state and remove our protocol entry in the device list.
  2606. */
  2607. static int packet_release(struct socket *sock)
  2608. {
  2609. struct sock *sk = sock->sk;
  2610. struct packet_sock *po;
  2611. struct packet_fanout *f;
  2612. struct net *net;
  2613. union tpacket_req_u req_u;
  2614. if (!sk)
  2615. return 0;
  2616. net = sock_net(sk);
  2617. po = pkt_sk(sk);
  2618. mutex_lock(&net->packet.sklist_lock);
  2619. sk_del_node_init_rcu(sk);
  2620. mutex_unlock(&net->packet.sklist_lock);
  2621. preempt_disable();
  2622. sock_prot_inuse_add(net, sk->sk_prot, -1);
  2623. preempt_enable();
  2624. spin_lock(&po->bind_lock);
  2625. unregister_prot_hook(sk, false);
  2626. packet_cached_dev_reset(po);
  2627. if (po->prot_hook.dev) {
  2628. dev_put(po->prot_hook.dev);
  2629. po->prot_hook.dev = NULL;
  2630. }
  2631. spin_unlock(&po->bind_lock);
  2632. packet_flush_mclist(sk);
  2633. lock_sock(sk);
  2634. if (po->rx_ring.pg_vec) {
  2635. memset(&req_u, 0, sizeof(req_u));
  2636. packet_set_ring(sk, &req_u, 1, 0);
  2637. }
  2638. if (po->tx_ring.pg_vec) {
  2639. memset(&req_u, 0, sizeof(req_u));
  2640. packet_set_ring(sk, &req_u, 1, 1);
  2641. }
  2642. release_sock(sk);
  2643. f = fanout_release(sk);
  2644. synchronize_net();
  2645. if (f) {
  2646. kfree(po->rollover);
  2647. fanout_release_data(f);
  2648. kfree(f);
  2649. }
  2650. /*
  2651. * Now the socket is dead. No more input will appear.
  2652. */
  2653. sock_orphan(sk);
  2654. sock->sk = NULL;
  2655. /* Purge queues */
  2656. skb_queue_purge(&sk->sk_receive_queue);
  2657. packet_free_pending(po);
  2658. sk_refcnt_debug_release(sk);
  2659. sock_put(sk);
  2660. return 0;
  2661. }
  2662. /*
  2663. * Attach a packet hook.
  2664. */
  2665. static int packet_do_bind(struct sock *sk, const char *name, int ifindex,
  2666. __be16 proto)
  2667. {
  2668. struct packet_sock *po = pkt_sk(sk);
  2669. struct net_device *dev_curr;
  2670. __be16 proto_curr;
  2671. bool need_rehook;
  2672. struct net_device *dev = NULL;
  2673. int ret = 0;
  2674. bool unlisted = false;
  2675. lock_sock(sk);
  2676. spin_lock(&po->bind_lock);
  2677. rcu_read_lock();
  2678. if (po->fanout) {
  2679. ret = -EINVAL;
  2680. goto out_unlock;
  2681. }
  2682. if (name) {
  2683. dev = dev_get_by_name_rcu(sock_net(sk), name);
  2684. if (!dev) {
  2685. ret = -ENODEV;
  2686. goto out_unlock;
  2687. }
  2688. } else if (ifindex) {
  2689. dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
  2690. if (!dev) {
  2691. ret = -ENODEV;
  2692. goto out_unlock;
  2693. }
  2694. }
  2695. if (dev)
  2696. dev_hold(dev);
  2697. proto_curr = po->prot_hook.type;
  2698. dev_curr = po->prot_hook.dev;
  2699. need_rehook = proto_curr != proto || dev_curr != dev;
  2700. if (need_rehook) {
  2701. if (po->running) {
  2702. rcu_read_unlock();
  2703. /* prevents packet_notifier() from calling
  2704. * register_prot_hook()
  2705. */
  2706. WRITE_ONCE(po->num, 0);
  2707. __unregister_prot_hook(sk, true);
  2708. rcu_read_lock();
  2709. dev_curr = po->prot_hook.dev;
  2710. if (dev)
  2711. unlisted = !dev_get_by_index_rcu(sock_net(sk),
  2712. dev->ifindex);
  2713. }
  2714. BUG_ON(po->running);
  2715. WRITE_ONCE(po->num, proto);
  2716. po->prot_hook.type = proto;
  2717. if (unlikely(unlisted)) {
  2718. dev_put(dev);
  2719. po->prot_hook.dev = NULL;
  2720. WRITE_ONCE(po->ifindex, -1);
  2721. packet_cached_dev_reset(po);
  2722. } else {
  2723. po->prot_hook.dev = dev;
  2724. WRITE_ONCE(po->ifindex, dev ? dev->ifindex : 0);
  2725. packet_cached_dev_assign(po, dev);
  2726. }
  2727. }
  2728. if (dev_curr)
  2729. dev_put(dev_curr);
  2730. if (proto == 0 || !need_rehook)
  2731. goto out_unlock;
  2732. if (!unlisted && (!dev || (dev->flags & IFF_UP))) {
  2733. register_prot_hook(sk);
  2734. } else {
  2735. sk->sk_err = ENETDOWN;
  2736. if (!sock_flag(sk, SOCK_DEAD))
  2737. sk->sk_error_report(sk);
  2738. }
  2739. out_unlock:
  2740. rcu_read_unlock();
  2741. spin_unlock(&po->bind_lock);
  2742. release_sock(sk);
  2743. return ret;
  2744. }
  2745. /*
  2746. * Bind a packet socket to a device
  2747. */
  2748. static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
  2749. int addr_len)
  2750. {
  2751. struct sock *sk = sock->sk;
  2752. char name[sizeof(uaddr->sa_data) + 1];
  2753. /*
  2754. * Check legality
  2755. */
  2756. if (addr_len != sizeof(struct sockaddr))
  2757. return -EINVAL;
  2758. /* uaddr->sa_data comes from the userspace, it's not guaranteed to be
  2759. * zero-terminated.
  2760. */
  2761. memcpy(name, uaddr->sa_data, sizeof(uaddr->sa_data));
  2762. name[sizeof(uaddr->sa_data)] = 0;
  2763. return packet_do_bind(sk, name, 0, pkt_sk(sk)->num);
  2764. }
  2765. static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  2766. {
  2767. struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
  2768. struct sock *sk = sock->sk;
  2769. /*
  2770. * Check legality
  2771. */
  2772. if (addr_len < sizeof(struct sockaddr_ll))
  2773. return -EINVAL;
  2774. if (sll->sll_family != AF_PACKET)
  2775. return -EINVAL;
  2776. return packet_do_bind(sk, NULL, sll->sll_ifindex,
  2777. sll->sll_protocol ? : pkt_sk(sk)->num);
  2778. }
  2779. static struct proto packet_proto = {
  2780. .name = "PACKET",
  2781. .owner = THIS_MODULE,
  2782. .obj_size = sizeof(struct packet_sock),
  2783. };
  2784. /*
  2785. * Create a packet of type SOCK_PACKET.
  2786. */
  2787. static int packet_create(struct net *net, struct socket *sock, int protocol,
  2788. int kern)
  2789. {
  2790. struct sock *sk;
  2791. struct packet_sock *po;
  2792. __be16 proto = (__force __be16)protocol; /* weird, but documented */
  2793. int err;
  2794. if (!ns_capable(net->user_ns, CAP_NET_RAW))
  2795. return -EPERM;
  2796. if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
  2797. sock->type != SOCK_PACKET)
  2798. return -ESOCKTNOSUPPORT;
  2799. sock->state = SS_UNCONNECTED;
  2800. err = -ENOBUFS;
  2801. sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto, kern);
  2802. if (sk == NULL)
  2803. goto out;
  2804. sock->ops = &packet_ops;
  2805. if (sock->type == SOCK_PACKET)
  2806. sock->ops = &packet_ops_spkt;
  2807. sock_init_data(sock, sk);
  2808. po = pkt_sk(sk);
  2809. init_completion(&po->skb_completion);
  2810. sk->sk_family = PF_PACKET;
  2811. po->num = proto;
  2812. po->xmit = dev_queue_xmit;
  2813. err = packet_alloc_pending(po);
  2814. if (err)
  2815. goto out2;
  2816. packet_cached_dev_reset(po);
  2817. sk->sk_destruct = packet_sock_destruct;
  2818. sk_refcnt_debug_inc(sk);
  2819. /*
  2820. * Attach a protocol block
  2821. */
  2822. spin_lock_init(&po->bind_lock);
  2823. mutex_init(&po->pg_vec_lock);
  2824. po->rollover = NULL;
  2825. po->prot_hook.func = packet_rcv;
  2826. if (sock->type == SOCK_PACKET)
  2827. po->prot_hook.func = packet_rcv_spkt;
  2828. po->prot_hook.af_packet_priv = sk;
  2829. if (proto) {
  2830. po->prot_hook.type = proto;
  2831. __register_prot_hook(sk);
  2832. }
  2833. mutex_lock(&net->packet.sklist_lock);
  2834. sk_add_node_tail_rcu(sk, &net->packet.sklist);
  2835. mutex_unlock(&net->packet.sklist_lock);
  2836. preempt_disable();
  2837. sock_prot_inuse_add(net, &packet_proto, 1);
  2838. preempt_enable();
  2839. return 0;
  2840. out2:
  2841. sk_free(sk);
  2842. out:
  2843. return err;
  2844. }
  2845. /*
  2846. * Pull a packet from our receive queue and hand it to the user.
  2847. * If necessary we block.
  2848. */
  2849. static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  2850. int flags)
  2851. {
  2852. struct sock *sk = sock->sk;
  2853. struct sk_buff *skb;
  2854. int copied, err;
  2855. int vnet_hdr_len = 0;
  2856. unsigned int origlen = 0;
  2857. err = -EINVAL;
  2858. if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
  2859. goto out;
  2860. #if 0
  2861. /* What error should we return now? EUNATTACH? */
  2862. if (pkt_sk(sk)->ifindex < 0)
  2863. return -ENODEV;
  2864. #endif
  2865. if (flags & MSG_ERRQUEUE) {
  2866. err = sock_recv_errqueue(sk, msg, len,
  2867. SOL_PACKET, PACKET_TX_TIMESTAMP);
  2868. goto out;
  2869. }
  2870. /*
  2871. * Call the generic datagram receiver. This handles all sorts
  2872. * of horrible races and re-entrancy so we can forget about it
  2873. * in the protocol layers.
  2874. *
  2875. * Now it will return ENETDOWN, if device have just gone down,
  2876. * but then it will block.
  2877. */
  2878. skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
  2879. /*
  2880. * An error occurred so return it. Because skb_recv_datagram()
  2881. * handles the blocking we don't see and worry about blocking
  2882. * retries.
  2883. */
  2884. if (skb == NULL)
  2885. goto out;
  2886. if (pkt_sk(sk)->pressure)
  2887. packet_rcv_has_room(pkt_sk(sk), NULL);
  2888. if (pkt_sk(sk)->has_vnet_hdr) {
  2889. err = packet_rcv_vnet(msg, skb, &len);
  2890. if (err)
  2891. goto out_free;
  2892. vnet_hdr_len = sizeof(struct virtio_net_hdr);
  2893. }
  2894. /* You lose any data beyond the buffer you gave. If it worries
  2895. * a user program they can ask the device for its MTU
  2896. * anyway.
  2897. */
  2898. copied = skb->len;
  2899. if (copied > len) {
  2900. copied = len;
  2901. msg->msg_flags |= MSG_TRUNC;
  2902. }
  2903. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  2904. if (err)
  2905. goto out_free;
  2906. if (sock->type != SOCK_PACKET) {
  2907. struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
  2908. /* Original length was stored in sockaddr_ll fields */
  2909. origlen = PACKET_SKB_CB(skb)->sa.origlen;
  2910. sll->sll_family = AF_PACKET;
  2911. sll->sll_protocol = skb->protocol;
  2912. }
  2913. sock_recv_ts_and_drops(msg, sk, skb);
  2914. if (msg->msg_name) {
  2915. int copy_len;
  2916. /* If the address length field is there to be filled
  2917. * in, we fill it in now.
  2918. */
  2919. if (sock->type == SOCK_PACKET) {
  2920. __sockaddr_check_size(sizeof(struct sockaddr_pkt));
  2921. msg->msg_namelen = sizeof(struct sockaddr_pkt);
  2922. copy_len = msg->msg_namelen;
  2923. } else {
  2924. struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
  2925. msg->msg_namelen = sll->sll_halen +
  2926. offsetof(struct sockaddr_ll, sll_addr);
  2927. copy_len = msg->msg_namelen;
  2928. if (msg->msg_namelen < sizeof(struct sockaddr_ll)) {
  2929. memset(msg->msg_name +
  2930. offsetof(struct sockaddr_ll, sll_addr),
  2931. 0, sizeof(sll->sll_addr));
  2932. msg->msg_namelen = sizeof(struct sockaddr_ll);
  2933. }
  2934. }
  2935. memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa, copy_len);
  2936. }
  2937. if (pkt_sk(sk)->auxdata) {
  2938. struct tpacket_auxdata aux;
  2939. aux.tp_status = TP_STATUS_USER;
  2940. if (skb->ip_summed == CHECKSUM_PARTIAL)
  2941. aux.tp_status |= TP_STATUS_CSUMNOTREADY;
  2942. else if (skb->pkt_type != PACKET_OUTGOING &&
  2943. (skb->ip_summed == CHECKSUM_COMPLETE ||
  2944. skb_csum_unnecessary(skb)))
  2945. aux.tp_status |= TP_STATUS_CSUM_VALID;
  2946. aux.tp_len = origlen;
  2947. aux.tp_snaplen = skb->len;
  2948. aux.tp_mac = 0;
  2949. aux.tp_net = skb_network_offset(skb);
  2950. if (skb_vlan_tag_present(skb)) {
  2951. aux.tp_vlan_tci = skb_vlan_tag_get(skb);
  2952. aux.tp_vlan_tpid = ntohs(skb->vlan_proto);
  2953. aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
  2954. } else {
  2955. aux.tp_vlan_tci = 0;
  2956. aux.tp_vlan_tpid = 0;
  2957. }
  2958. put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
  2959. }
  2960. /*
  2961. * Free or return the buffer as appropriate. Again this
  2962. * hides all the races and re-entrancy issues from us.
  2963. */
  2964. err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
  2965. out_free:
  2966. skb_free_datagram(sk, skb);
  2967. out:
  2968. return err;
  2969. }
  2970. static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
  2971. int *uaddr_len, int peer)
  2972. {
  2973. struct net_device *dev;
  2974. struct sock *sk = sock->sk;
  2975. if (peer)
  2976. return -EOPNOTSUPP;
  2977. uaddr->sa_family = AF_PACKET;
  2978. memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data));
  2979. rcu_read_lock();
  2980. dev = dev_get_by_index_rcu(sock_net(sk), READ_ONCE(pkt_sk(sk)->ifindex));
  2981. if (dev)
  2982. strlcpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data));
  2983. rcu_read_unlock();
  2984. *uaddr_len = sizeof(*uaddr);
  2985. return 0;
  2986. }
  2987. static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
  2988. int *uaddr_len, int peer)
  2989. {
  2990. struct net_device *dev;
  2991. struct sock *sk = sock->sk;
  2992. struct packet_sock *po = pkt_sk(sk);
  2993. DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
  2994. int ifindex;
  2995. if (peer)
  2996. return -EOPNOTSUPP;
  2997. ifindex = READ_ONCE(po->ifindex);
  2998. sll->sll_family = AF_PACKET;
  2999. sll->sll_ifindex = ifindex;
  3000. sll->sll_protocol = READ_ONCE(po->num);
  3001. sll->sll_pkttype = 0;
  3002. rcu_read_lock();
  3003. dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
  3004. if (dev) {
  3005. sll->sll_hatype = dev->type;
  3006. sll->sll_halen = dev->addr_len;
  3007. memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
  3008. } else {
  3009. sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
  3010. sll->sll_halen = 0;
  3011. }
  3012. rcu_read_unlock();
  3013. *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
  3014. return 0;
  3015. }
  3016. static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
  3017. int what)
  3018. {
  3019. switch (i->type) {
  3020. case PACKET_MR_MULTICAST:
  3021. if (i->alen != dev->addr_len)
  3022. return -EINVAL;
  3023. if (what > 0)
  3024. return dev_mc_add(dev, i->addr);
  3025. else
  3026. return dev_mc_del(dev, i->addr);
  3027. break;
  3028. case PACKET_MR_PROMISC:
  3029. return dev_set_promiscuity(dev, what);
  3030. case PACKET_MR_ALLMULTI:
  3031. return dev_set_allmulti(dev, what);
  3032. case PACKET_MR_UNICAST:
  3033. if (i->alen != dev->addr_len)
  3034. return -EINVAL;
  3035. if (what > 0)
  3036. return dev_uc_add(dev, i->addr);
  3037. else
  3038. return dev_uc_del(dev, i->addr);
  3039. break;
  3040. default:
  3041. break;
  3042. }
  3043. return 0;
  3044. }
  3045. static void packet_dev_mclist_delete(struct net_device *dev,
  3046. struct packet_mclist **mlp)
  3047. {
  3048. struct packet_mclist *ml;
  3049. while ((ml = *mlp) != NULL) {
  3050. if (ml->ifindex == dev->ifindex) {
  3051. packet_dev_mc(dev, ml, -1);
  3052. *mlp = ml->next;
  3053. kfree(ml);
  3054. } else
  3055. mlp = &ml->next;
  3056. }
  3057. }
  3058. static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
  3059. {
  3060. struct packet_sock *po = pkt_sk(sk);
  3061. struct packet_mclist *ml, *i;
  3062. struct net_device *dev;
  3063. int err;
  3064. rtnl_lock();
  3065. err = -ENODEV;
  3066. dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
  3067. if (!dev)
  3068. goto done;
  3069. err = -EINVAL;
  3070. if (mreq->mr_alen > dev->addr_len)
  3071. goto done;
  3072. err = -ENOBUFS;
  3073. i = kmalloc(sizeof(*i), GFP_KERNEL);
  3074. if (i == NULL)
  3075. goto done;
  3076. err = 0;
  3077. for (ml = po->mclist; ml; ml = ml->next) {
  3078. if (ml->ifindex == mreq->mr_ifindex &&
  3079. ml->type == mreq->mr_type &&
  3080. ml->alen == mreq->mr_alen &&
  3081. memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
  3082. ml->count++;
  3083. /* Free the new element ... */
  3084. kfree(i);
  3085. goto done;
  3086. }
  3087. }
  3088. i->type = mreq->mr_type;
  3089. i->ifindex = mreq->mr_ifindex;
  3090. i->alen = mreq->mr_alen;
  3091. memcpy(i->addr, mreq->mr_address, i->alen);
  3092. memset(i->addr + i->alen, 0, sizeof(i->addr) - i->alen);
  3093. i->count = 1;
  3094. i->next = po->mclist;
  3095. po->mclist = i;
  3096. err = packet_dev_mc(dev, i, 1);
  3097. if (err) {
  3098. po->mclist = i->next;
  3099. kfree(i);
  3100. }
  3101. done:
  3102. rtnl_unlock();
  3103. return err;
  3104. }
  3105. static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
  3106. {
  3107. struct packet_mclist *ml, **mlp;
  3108. rtnl_lock();
  3109. for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
  3110. if (ml->ifindex == mreq->mr_ifindex &&
  3111. ml->type == mreq->mr_type &&
  3112. ml->alen == mreq->mr_alen &&
  3113. memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
  3114. if (--ml->count == 0) {
  3115. struct net_device *dev;
  3116. *mlp = ml->next;
  3117. dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
  3118. if (dev)
  3119. packet_dev_mc(dev, ml, -1);
  3120. kfree(ml);
  3121. }
  3122. break;
  3123. }
  3124. }
  3125. rtnl_unlock();
  3126. return 0;
  3127. }
  3128. static void packet_flush_mclist(struct sock *sk)
  3129. {
  3130. struct packet_sock *po = pkt_sk(sk);
  3131. struct packet_mclist *ml;
  3132. if (!po->mclist)
  3133. return;
  3134. rtnl_lock();
  3135. while ((ml = po->mclist) != NULL) {
  3136. struct net_device *dev;
  3137. po->mclist = ml->next;
  3138. dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
  3139. if (dev != NULL)
  3140. packet_dev_mc(dev, ml, -1);
  3141. kfree(ml);
  3142. }
  3143. rtnl_unlock();
  3144. }
  3145. static int
  3146. packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
  3147. {
  3148. struct sock *sk = sock->sk;
  3149. struct packet_sock *po = pkt_sk(sk);
  3150. int ret;
  3151. if (level != SOL_PACKET)
  3152. return -ENOPROTOOPT;
  3153. switch (optname) {
  3154. case PACKET_ADD_MEMBERSHIP:
  3155. case PACKET_DROP_MEMBERSHIP:
  3156. {
  3157. struct packet_mreq_max mreq;
  3158. int len = optlen;
  3159. memset(&mreq, 0, sizeof(mreq));
  3160. if (len < sizeof(struct packet_mreq))
  3161. return -EINVAL;
  3162. if (len > sizeof(mreq))
  3163. len = sizeof(mreq);
  3164. if (copy_from_user(&mreq, optval, len))
  3165. return -EFAULT;
  3166. if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
  3167. return -EINVAL;
  3168. if (optname == PACKET_ADD_MEMBERSHIP)
  3169. ret = packet_mc_add(sk, &mreq);
  3170. else
  3171. ret = packet_mc_drop(sk, &mreq);
  3172. return ret;
  3173. }
  3174. case PACKET_RX_RING:
  3175. case PACKET_TX_RING:
  3176. {
  3177. union tpacket_req_u req_u;
  3178. int len;
  3179. lock_sock(sk);
  3180. switch (po->tp_version) {
  3181. case TPACKET_V1:
  3182. case TPACKET_V2:
  3183. len = sizeof(req_u.req);
  3184. break;
  3185. case TPACKET_V3:
  3186. default:
  3187. len = sizeof(req_u.req3);
  3188. break;
  3189. }
  3190. if (optlen < len) {
  3191. ret = -EINVAL;
  3192. } else {
  3193. if (copy_from_user(&req_u.req, optval, len))
  3194. ret = -EFAULT;
  3195. else
  3196. ret = packet_set_ring(sk, &req_u, 0,
  3197. optname == PACKET_TX_RING);
  3198. }
  3199. release_sock(sk);
  3200. return ret;
  3201. }
  3202. case PACKET_COPY_THRESH:
  3203. {
  3204. int val;
  3205. if (optlen != sizeof(val))
  3206. return -EINVAL;
  3207. if (copy_from_user(&val, optval, sizeof(val)))
  3208. return -EFAULT;
  3209. pkt_sk(sk)->copy_thresh = val;
  3210. return 0;
  3211. }
  3212. case PACKET_VERSION:
  3213. {
  3214. int val;
  3215. if (optlen != sizeof(val))
  3216. return -EINVAL;
  3217. if (copy_from_user(&val, optval, sizeof(val)))
  3218. return -EFAULT;
  3219. switch (val) {
  3220. case TPACKET_V1:
  3221. case TPACKET_V2:
  3222. case TPACKET_V3:
  3223. break;
  3224. default:
  3225. return -EINVAL;
  3226. }
  3227. lock_sock(sk);
  3228. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
  3229. ret = -EBUSY;
  3230. } else {
  3231. po->tp_version = val;
  3232. ret = 0;
  3233. }
  3234. release_sock(sk);
  3235. return ret;
  3236. }
  3237. case PACKET_RESERVE:
  3238. {
  3239. unsigned int val;
  3240. if (optlen != sizeof(val))
  3241. return -EINVAL;
  3242. if (copy_from_user(&val, optval, sizeof(val)))
  3243. return -EFAULT;
  3244. if (val > INT_MAX)
  3245. return -EINVAL;
  3246. lock_sock(sk);
  3247. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
  3248. ret = -EBUSY;
  3249. } else {
  3250. po->tp_reserve = val;
  3251. ret = 0;
  3252. }
  3253. release_sock(sk);
  3254. return ret;
  3255. }
  3256. case PACKET_LOSS:
  3257. {
  3258. unsigned int val;
  3259. if (optlen != sizeof(val))
  3260. return -EINVAL;
  3261. if (copy_from_user(&val, optval, sizeof(val)))
  3262. return -EFAULT;
  3263. lock_sock(sk);
  3264. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
  3265. ret = -EBUSY;
  3266. } else {
  3267. po->tp_loss = !!val;
  3268. ret = 0;
  3269. }
  3270. release_sock(sk);
  3271. return ret;
  3272. }
  3273. case PACKET_AUXDATA:
  3274. {
  3275. int val;
  3276. if (optlen < sizeof(val))
  3277. return -EINVAL;
  3278. if (copy_from_user(&val, optval, sizeof(val)))
  3279. return -EFAULT;
  3280. lock_sock(sk);
  3281. po->auxdata = !!val;
  3282. release_sock(sk);
  3283. return 0;
  3284. }
  3285. case PACKET_ORIGDEV:
  3286. {
  3287. int val;
  3288. if (optlen < sizeof(val))
  3289. return -EINVAL;
  3290. if (copy_from_user(&val, optval, sizeof(val)))
  3291. return -EFAULT;
  3292. lock_sock(sk);
  3293. po->origdev = !!val;
  3294. release_sock(sk);
  3295. return 0;
  3296. }
  3297. case PACKET_VNET_HDR:
  3298. {
  3299. int val;
  3300. if (sock->type != SOCK_RAW)
  3301. return -EINVAL;
  3302. if (optlen < sizeof(val))
  3303. return -EINVAL;
  3304. if (copy_from_user(&val, optval, sizeof(val)))
  3305. return -EFAULT;
  3306. lock_sock(sk);
  3307. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
  3308. ret = -EBUSY;
  3309. } else {
  3310. po->has_vnet_hdr = !!val;
  3311. ret = 0;
  3312. }
  3313. release_sock(sk);
  3314. return ret;
  3315. }
  3316. case PACKET_TIMESTAMP:
  3317. {
  3318. int val;
  3319. if (optlen != sizeof(val))
  3320. return -EINVAL;
  3321. if (copy_from_user(&val, optval, sizeof(val)))
  3322. return -EFAULT;
  3323. po->tp_tstamp = val;
  3324. return 0;
  3325. }
  3326. case PACKET_FANOUT:
  3327. {
  3328. int val;
  3329. if (optlen != sizeof(val))
  3330. return -EINVAL;
  3331. if (copy_from_user(&val, optval, sizeof(val)))
  3332. return -EFAULT;
  3333. return fanout_add(sk, val & 0xffff, val >> 16);
  3334. }
  3335. case PACKET_FANOUT_DATA:
  3336. {
  3337. if (!po->fanout)
  3338. return -EINVAL;
  3339. return fanout_set_data(po, optval, optlen);
  3340. }
  3341. case PACKET_TX_HAS_OFF:
  3342. {
  3343. unsigned int val;
  3344. if (optlen != sizeof(val))
  3345. return -EINVAL;
  3346. if (copy_from_user(&val, optval, sizeof(val)))
  3347. return -EFAULT;
  3348. lock_sock(sk);
  3349. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
  3350. ret = -EBUSY;
  3351. } else {
  3352. po->tp_tx_has_off = !!val;
  3353. ret = 0;
  3354. }
  3355. release_sock(sk);
  3356. return 0;
  3357. }
  3358. case PACKET_QDISC_BYPASS:
  3359. {
  3360. int val;
  3361. if (optlen != sizeof(val))
  3362. return -EINVAL;
  3363. if (copy_from_user(&val, optval, sizeof(val)))
  3364. return -EFAULT;
  3365. po->xmit = val ? packet_direct_xmit : dev_queue_xmit;
  3366. return 0;
  3367. }
  3368. default:
  3369. return -ENOPROTOOPT;
  3370. }
  3371. }
  3372. static int packet_getsockopt(struct socket *sock, int level, int optname,
  3373. char __user *optval, int __user *optlen)
  3374. {
  3375. int len;
  3376. int val, lv = sizeof(val);
  3377. struct sock *sk = sock->sk;
  3378. struct packet_sock *po = pkt_sk(sk);
  3379. void *data = &val;
  3380. union tpacket_stats_u st;
  3381. struct tpacket_rollover_stats rstats;
  3382. if (level != SOL_PACKET)
  3383. return -ENOPROTOOPT;
  3384. if (get_user(len, optlen))
  3385. return -EFAULT;
  3386. if (len < 0)
  3387. return -EINVAL;
  3388. switch (optname) {
  3389. case PACKET_STATISTICS:
  3390. spin_lock_bh(&sk->sk_receive_queue.lock);
  3391. memcpy(&st, &po->stats, sizeof(st));
  3392. memset(&po->stats, 0, sizeof(po->stats));
  3393. spin_unlock_bh(&sk->sk_receive_queue.lock);
  3394. if (po->tp_version == TPACKET_V3) {
  3395. lv = sizeof(struct tpacket_stats_v3);
  3396. st.stats3.tp_packets += st.stats3.tp_drops;
  3397. data = &st.stats3;
  3398. } else {
  3399. lv = sizeof(struct tpacket_stats);
  3400. st.stats1.tp_packets += st.stats1.tp_drops;
  3401. data = &st.stats1;
  3402. }
  3403. break;
  3404. case PACKET_AUXDATA:
  3405. val = po->auxdata;
  3406. break;
  3407. case PACKET_ORIGDEV:
  3408. val = po->origdev;
  3409. break;
  3410. case PACKET_VNET_HDR:
  3411. val = po->has_vnet_hdr;
  3412. break;
  3413. case PACKET_VERSION:
  3414. val = po->tp_version;
  3415. break;
  3416. case PACKET_HDRLEN:
  3417. if (len > sizeof(int))
  3418. len = sizeof(int);
  3419. if (len < sizeof(int))
  3420. return -EINVAL;
  3421. if (copy_from_user(&val, optval, len))
  3422. return -EFAULT;
  3423. switch (val) {
  3424. case TPACKET_V1:
  3425. val = sizeof(struct tpacket_hdr);
  3426. break;
  3427. case TPACKET_V2:
  3428. val = sizeof(struct tpacket2_hdr);
  3429. break;
  3430. case TPACKET_V3:
  3431. val = sizeof(struct tpacket3_hdr);
  3432. break;
  3433. default:
  3434. return -EINVAL;
  3435. }
  3436. break;
  3437. case PACKET_RESERVE:
  3438. val = po->tp_reserve;
  3439. break;
  3440. case PACKET_LOSS:
  3441. val = po->tp_loss;
  3442. break;
  3443. case PACKET_TIMESTAMP:
  3444. val = po->tp_tstamp;
  3445. break;
  3446. case PACKET_FANOUT:
  3447. val = (po->fanout ?
  3448. ((u32)po->fanout->id |
  3449. ((u32)po->fanout->type << 16) |
  3450. ((u32)po->fanout->flags << 24)) :
  3451. 0);
  3452. break;
  3453. case PACKET_ROLLOVER_STATS:
  3454. if (!po->rollover)
  3455. return -EINVAL;
  3456. rstats.tp_all = atomic_long_read(&po->rollover->num);
  3457. rstats.tp_huge = atomic_long_read(&po->rollover->num_huge);
  3458. rstats.tp_failed = atomic_long_read(&po->rollover->num_failed);
  3459. data = &rstats;
  3460. lv = sizeof(rstats);
  3461. break;
  3462. case PACKET_TX_HAS_OFF:
  3463. val = po->tp_tx_has_off;
  3464. break;
  3465. case PACKET_QDISC_BYPASS:
  3466. val = packet_use_direct_xmit(po);
  3467. break;
  3468. default:
  3469. return -ENOPROTOOPT;
  3470. }
  3471. if (len > lv)
  3472. len = lv;
  3473. if (put_user(len, optlen))
  3474. return -EFAULT;
  3475. if (copy_to_user(optval, data, len))
  3476. return -EFAULT;
  3477. return 0;
  3478. }
  3479. #ifdef CONFIG_COMPAT
  3480. static int compat_packet_setsockopt(struct socket *sock, int level, int optname,
  3481. char __user *optval, unsigned int optlen)
  3482. {
  3483. struct packet_sock *po = pkt_sk(sock->sk);
  3484. if (level != SOL_PACKET)
  3485. return -ENOPROTOOPT;
  3486. if (optname == PACKET_FANOUT_DATA &&
  3487. po->fanout && po->fanout->type == PACKET_FANOUT_CBPF) {
  3488. optval = (char __user *)get_compat_bpf_fprog(optval);
  3489. if (!optval)
  3490. return -EFAULT;
  3491. optlen = sizeof(struct sock_fprog);
  3492. }
  3493. return packet_setsockopt(sock, level, optname, optval, optlen);
  3494. }
  3495. #endif
  3496. static int packet_notifier(struct notifier_block *this,
  3497. unsigned long msg, void *ptr)
  3498. {
  3499. struct sock *sk;
  3500. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  3501. struct net *net = dev_net(dev);
  3502. rcu_read_lock();
  3503. sk_for_each_rcu(sk, &net->packet.sklist) {
  3504. struct packet_sock *po = pkt_sk(sk);
  3505. switch (msg) {
  3506. case NETDEV_UNREGISTER:
  3507. if (po->mclist)
  3508. packet_dev_mclist_delete(dev, &po->mclist);
  3509. /* fallthrough */
  3510. case NETDEV_DOWN:
  3511. if (dev->ifindex == po->ifindex) {
  3512. spin_lock(&po->bind_lock);
  3513. if (po->running) {
  3514. __unregister_prot_hook(sk, false);
  3515. sk->sk_err = ENETDOWN;
  3516. if (!sock_flag(sk, SOCK_DEAD))
  3517. sk->sk_error_report(sk);
  3518. }
  3519. if (msg == NETDEV_UNREGISTER) {
  3520. packet_cached_dev_reset(po);
  3521. WRITE_ONCE(po->ifindex, -1);
  3522. if (po->prot_hook.dev)
  3523. dev_put(po->prot_hook.dev);
  3524. po->prot_hook.dev = NULL;
  3525. }
  3526. spin_unlock(&po->bind_lock);
  3527. }
  3528. break;
  3529. case NETDEV_UP:
  3530. if (dev->ifindex == po->ifindex) {
  3531. spin_lock(&po->bind_lock);
  3532. if (po->num)
  3533. register_prot_hook(sk);
  3534. spin_unlock(&po->bind_lock);
  3535. }
  3536. break;
  3537. }
  3538. }
  3539. rcu_read_unlock();
  3540. return NOTIFY_DONE;
  3541. }
  3542. static int packet_ioctl(struct socket *sock, unsigned int cmd,
  3543. unsigned long arg)
  3544. {
  3545. struct sock *sk = sock->sk;
  3546. switch (cmd) {
  3547. case SIOCOUTQ:
  3548. {
  3549. int amount = sk_wmem_alloc_get(sk);
  3550. return put_user(amount, (int __user *)arg);
  3551. }
  3552. case SIOCINQ:
  3553. {
  3554. struct sk_buff *skb;
  3555. int amount = 0;
  3556. spin_lock_bh(&sk->sk_receive_queue.lock);
  3557. skb = skb_peek(&sk->sk_receive_queue);
  3558. if (skb)
  3559. amount = skb->len;
  3560. spin_unlock_bh(&sk->sk_receive_queue.lock);
  3561. return put_user(amount, (int __user *)arg);
  3562. }
  3563. case SIOCGSTAMP:
  3564. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  3565. case SIOCGSTAMPNS:
  3566. return sock_get_timestampns(sk, (struct timespec __user *)arg);
  3567. #ifdef CONFIG_INET
  3568. case SIOCADDRT:
  3569. case SIOCDELRT:
  3570. case SIOCDARP:
  3571. case SIOCGARP:
  3572. case SIOCSARP:
  3573. case SIOCGIFADDR:
  3574. case SIOCSIFADDR:
  3575. case SIOCGIFBRDADDR:
  3576. case SIOCSIFBRDADDR:
  3577. case SIOCGIFNETMASK:
  3578. case SIOCSIFNETMASK:
  3579. case SIOCGIFDSTADDR:
  3580. case SIOCSIFDSTADDR:
  3581. case SIOCSIFFLAGS:
  3582. return inet_dgram_ops.ioctl(sock, cmd, arg);
  3583. #endif
  3584. default:
  3585. return -ENOIOCTLCMD;
  3586. }
  3587. return 0;
  3588. }
  3589. static unsigned int packet_poll(struct file *file, struct socket *sock,
  3590. poll_table *wait)
  3591. {
  3592. struct sock *sk = sock->sk;
  3593. struct packet_sock *po = pkt_sk(sk);
  3594. unsigned int mask = datagram_poll(file, sock, wait);
  3595. spin_lock_bh(&sk->sk_receive_queue.lock);
  3596. if (po->rx_ring.pg_vec) {
  3597. if (!packet_previous_rx_frame(po, &po->rx_ring,
  3598. TP_STATUS_KERNEL))
  3599. mask |= POLLIN | POLLRDNORM;
  3600. }
  3601. if (po->pressure && __packet_rcv_has_room(po, NULL) == ROOM_NORMAL)
  3602. po->pressure = 0;
  3603. spin_unlock_bh(&sk->sk_receive_queue.lock);
  3604. spin_lock_bh(&sk->sk_write_queue.lock);
  3605. if (po->tx_ring.pg_vec) {
  3606. if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
  3607. mask |= POLLOUT | POLLWRNORM;
  3608. }
  3609. spin_unlock_bh(&sk->sk_write_queue.lock);
  3610. return mask;
  3611. }
  3612. /* Dirty? Well, I still did not learn better way to account
  3613. * for user mmaps.
  3614. */
  3615. static void packet_mm_open(struct vm_area_struct *vma)
  3616. {
  3617. struct file *file = vma->vm_file;
  3618. struct socket *sock = file->private_data;
  3619. struct sock *sk = sock->sk;
  3620. if (sk)
  3621. atomic_inc(&pkt_sk(sk)->mapped);
  3622. }
  3623. static void packet_mm_close(struct vm_area_struct *vma)
  3624. {
  3625. struct file *file = vma->vm_file;
  3626. struct socket *sock = file->private_data;
  3627. struct sock *sk = sock->sk;
  3628. if (sk)
  3629. atomic_dec(&pkt_sk(sk)->mapped);
  3630. }
  3631. static const struct vm_operations_struct packet_mmap_ops = {
  3632. .open = packet_mm_open,
  3633. .close = packet_mm_close,
  3634. };
  3635. static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
  3636. unsigned int len)
  3637. {
  3638. int i;
  3639. for (i = 0; i < len; i++) {
  3640. if (likely(pg_vec[i].buffer)) {
  3641. if (is_vmalloc_addr(pg_vec[i].buffer))
  3642. vfree(pg_vec[i].buffer);
  3643. else
  3644. free_pages((unsigned long)pg_vec[i].buffer,
  3645. order);
  3646. pg_vec[i].buffer = NULL;
  3647. }
  3648. }
  3649. kfree(pg_vec);
  3650. }
  3651. static char *alloc_one_pg_vec_page(unsigned long order)
  3652. {
  3653. char *buffer;
  3654. gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
  3655. __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
  3656. buffer = (char *) __get_free_pages(gfp_flags, order);
  3657. if (buffer)
  3658. return buffer;
  3659. /* __get_free_pages failed, fall back to vmalloc */
  3660. buffer = vzalloc((1 << order) * PAGE_SIZE);
  3661. if (buffer)
  3662. return buffer;
  3663. /* vmalloc failed, lets dig into swap here */
  3664. gfp_flags &= ~__GFP_NORETRY;
  3665. buffer = (char *) __get_free_pages(gfp_flags, order);
  3666. if (buffer)
  3667. return buffer;
  3668. /* complete and utter failure */
  3669. return NULL;
  3670. }
  3671. static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
  3672. {
  3673. unsigned int block_nr = req->tp_block_nr;
  3674. struct pgv *pg_vec;
  3675. int i;
  3676. pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL | __GFP_NOWARN);
  3677. if (unlikely(!pg_vec))
  3678. goto out;
  3679. for (i = 0; i < block_nr; i++) {
  3680. pg_vec[i].buffer = alloc_one_pg_vec_page(order);
  3681. if (unlikely(!pg_vec[i].buffer))
  3682. goto out_free_pgvec;
  3683. }
  3684. out:
  3685. return pg_vec;
  3686. out_free_pgvec:
  3687. free_pg_vec(pg_vec, order, block_nr);
  3688. pg_vec = NULL;
  3689. goto out;
  3690. }
  3691. static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
  3692. int closing, int tx_ring)
  3693. {
  3694. struct pgv *pg_vec = NULL;
  3695. struct packet_sock *po = pkt_sk(sk);
  3696. unsigned long *rx_owner_map = NULL;
  3697. int was_running, order = 0;
  3698. struct packet_ring_buffer *rb;
  3699. struct sk_buff_head *rb_queue;
  3700. __be16 num;
  3701. int err = -EINVAL;
  3702. /* Added to avoid minimal code churn */
  3703. struct tpacket_req *req = &req_u->req;
  3704. rb = tx_ring ? &po->tx_ring : &po->rx_ring;
  3705. rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
  3706. err = -EBUSY;
  3707. if (!closing) {
  3708. if (atomic_read(&po->mapped))
  3709. goto out;
  3710. if (packet_read_pending(rb))
  3711. goto out;
  3712. }
  3713. if (req->tp_block_nr) {
  3714. unsigned int min_frame_size;
  3715. /* Sanity tests and some calculations */
  3716. err = -EBUSY;
  3717. if (unlikely(rb->pg_vec))
  3718. goto out;
  3719. switch (po->tp_version) {
  3720. case TPACKET_V1:
  3721. po->tp_hdrlen = TPACKET_HDRLEN;
  3722. break;
  3723. case TPACKET_V2:
  3724. po->tp_hdrlen = TPACKET2_HDRLEN;
  3725. break;
  3726. case TPACKET_V3:
  3727. po->tp_hdrlen = TPACKET3_HDRLEN;
  3728. break;
  3729. }
  3730. err = -EINVAL;
  3731. if (unlikely((int)req->tp_block_size <= 0))
  3732. goto out;
  3733. if (unlikely(!PAGE_ALIGNED(req->tp_block_size)))
  3734. goto out;
  3735. min_frame_size = po->tp_hdrlen + po->tp_reserve;
  3736. if (po->tp_version >= TPACKET_V3 &&
  3737. req->tp_block_size <
  3738. BLK_PLUS_PRIV((u64)req_u->req3.tp_sizeof_priv) + min_frame_size)
  3739. goto out;
  3740. if (unlikely(req->tp_frame_size < min_frame_size))
  3741. goto out;
  3742. if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
  3743. goto out;
  3744. rb->frames_per_block = req->tp_block_size / req->tp_frame_size;
  3745. if (unlikely(rb->frames_per_block == 0))
  3746. goto out;
  3747. if (unlikely(rb->frames_per_block > UINT_MAX / req->tp_block_nr))
  3748. goto out;
  3749. if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
  3750. req->tp_frame_nr))
  3751. goto out;
  3752. err = -ENOMEM;
  3753. order = get_order(req->tp_block_size);
  3754. pg_vec = alloc_pg_vec(req, order);
  3755. if (unlikely(!pg_vec))
  3756. goto out;
  3757. switch (po->tp_version) {
  3758. case TPACKET_V3:
  3759. /* Block transmit is not supported yet */
  3760. if (!tx_ring) {
  3761. init_prb_bdqc(po, rb, pg_vec, req_u);
  3762. } else {
  3763. struct tpacket_req3 *req3 = &req_u->req3;
  3764. if (req3->tp_retire_blk_tov ||
  3765. req3->tp_sizeof_priv ||
  3766. req3->tp_feature_req_word) {
  3767. err = -EINVAL;
  3768. goto out_free_pg_vec;
  3769. }
  3770. }
  3771. break;
  3772. default:
  3773. if (!tx_ring) {
  3774. rx_owner_map = bitmap_alloc(req->tp_frame_nr,
  3775. GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
  3776. if (!rx_owner_map)
  3777. goto out_free_pg_vec;
  3778. }
  3779. break;
  3780. }
  3781. }
  3782. /* Done */
  3783. else {
  3784. err = -EINVAL;
  3785. if (unlikely(req->tp_frame_nr))
  3786. goto out;
  3787. }
  3788. /* Detach socket from network */
  3789. spin_lock(&po->bind_lock);
  3790. was_running = po->running;
  3791. num = po->num;
  3792. if (was_running) {
  3793. WRITE_ONCE(po->num, 0);
  3794. __unregister_prot_hook(sk, false);
  3795. }
  3796. spin_unlock(&po->bind_lock);
  3797. synchronize_net();
  3798. err = -EBUSY;
  3799. mutex_lock(&po->pg_vec_lock);
  3800. if (closing || atomic_read(&po->mapped) == 0) {
  3801. err = 0;
  3802. spin_lock_bh(&rb_queue->lock);
  3803. swap(rb->pg_vec, pg_vec);
  3804. if (po->tp_version <= TPACKET_V2)
  3805. swap(rb->rx_owner_map, rx_owner_map);
  3806. rb->frame_max = (req->tp_frame_nr - 1);
  3807. rb->head = 0;
  3808. rb->frame_size = req->tp_frame_size;
  3809. spin_unlock_bh(&rb_queue->lock);
  3810. swap(rb->pg_vec_order, order);
  3811. swap(rb->pg_vec_len, req->tp_block_nr);
  3812. rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
  3813. po->prot_hook.func = (po->rx_ring.pg_vec) ?
  3814. tpacket_rcv : packet_rcv;
  3815. skb_queue_purge(rb_queue);
  3816. if (atomic_read(&po->mapped))
  3817. pr_err("packet_mmap: vma is busy: %d\n",
  3818. atomic_read(&po->mapped));
  3819. }
  3820. mutex_unlock(&po->pg_vec_lock);
  3821. spin_lock(&po->bind_lock);
  3822. if (was_running) {
  3823. WRITE_ONCE(po->num, num);
  3824. register_prot_hook(sk);
  3825. }
  3826. spin_unlock(&po->bind_lock);
  3827. if (pg_vec && (po->tp_version > TPACKET_V2)) {
  3828. /* Because we don't support block-based V3 on tx-ring */
  3829. if (!tx_ring)
  3830. prb_shutdown_retire_blk_timer(po, rb_queue);
  3831. }
  3832. out_free_pg_vec:
  3833. bitmap_free(rx_owner_map);
  3834. if (pg_vec)
  3835. free_pg_vec(pg_vec, order, req->tp_block_nr);
  3836. out:
  3837. return err;
  3838. }
  3839. static int packet_mmap(struct file *file, struct socket *sock,
  3840. struct vm_area_struct *vma)
  3841. {
  3842. struct sock *sk = sock->sk;
  3843. struct packet_sock *po = pkt_sk(sk);
  3844. unsigned long size, expected_size;
  3845. struct packet_ring_buffer *rb;
  3846. unsigned long start;
  3847. int err = -EINVAL;
  3848. int i;
  3849. if (vma->vm_pgoff)
  3850. return -EINVAL;
  3851. mutex_lock(&po->pg_vec_lock);
  3852. expected_size = 0;
  3853. for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
  3854. if (rb->pg_vec) {
  3855. expected_size += rb->pg_vec_len
  3856. * rb->pg_vec_pages
  3857. * PAGE_SIZE;
  3858. }
  3859. }
  3860. if (expected_size == 0)
  3861. goto out;
  3862. size = vma->vm_end - vma->vm_start;
  3863. if (size != expected_size)
  3864. goto out;
  3865. start = vma->vm_start;
  3866. for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
  3867. if (rb->pg_vec == NULL)
  3868. continue;
  3869. for (i = 0; i < rb->pg_vec_len; i++) {
  3870. struct page *page;
  3871. void *kaddr = rb->pg_vec[i].buffer;
  3872. int pg_num;
  3873. for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
  3874. page = pgv_to_page(kaddr);
  3875. err = vm_insert_page(vma, start, page);
  3876. if (unlikely(err))
  3877. goto out;
  3878. start += PAGE_SIZE;
  3879. kaddr += PAGE_SIZE;
  3880. }
  3881. }
  3882. }
  3883. atomic_inc(&po->mapped);
  3884. vma->vm_ops = &packet_mmap_ops;
  3885. err = 0;
  3886. out:
  3887. mutex_unlock(&po->pg_vec_lock);
  3888. return err;
  3889. }
  3890. static const struct proto_ops packet_ops_spkt = {
  3891. .family = PF_PACKET,
  3892. .owner = THIS_MODULE,
  3893. .release = packet_release,
  3894. .bind = packet_bind_spkt,
  3895. .connect = sock_no_connect,
  3896. .socketpair = sock_no_socketpair,
  3897. .accept = sock_no_accept,
  3898. .getname = packet_getname_spkt,
  3899. .poll = datagram_poll,
  3900. .ioctl = packet_ioctl,
  3901. .listen = sock_no_listen,
  3902. .shutdown = sock_no_shutdown,
  3903. .setsockopt = sock_no_setsockopt,
  3904. .getsockopt = sock_no_getsockopt,
  3905. .sendmsg = packet_sendmsg_spkt,
  3906. .recvmsg = packet_recvmsg,
  3907. .mmap = sock_no_mmap,
  3908. .sendpage = sock_no_sendpage,
  3909. };
  3910. static const struct proto_ops packet_ops = {
  3911. .family = PF_PACKET,
  3912. .owner = THIS_MODULE,
  3913. .release = packet_release,
  3914. .bind = packet_bind,
  3915. .connect = sock_no_connect,
  3916. .socketpair = sock_no_socketpair,
  3917. .accept = sock_no_accept,
  3918. .getname = packet_getname,
  3919. .poll = packet_poll,
  3920. .ioctl = packet_ioctl,
  3921. .listen = sock_no_listen,
  3922. .shutdown = sock_no_shutdown,
  3923. .setsockopt = packet_setsockopt,
  3924. .getsockopt = packet_getsockopt,
  3925. #ifdef CONFIG_COMPAT
  3926. .compat_setsockopt = compat_packet_setsockopt,
  3927. #endif
  3928. .sendmsg = packet_sendmsg,
  3929. .recvmsg = packet_recvmsg,
  3930. .mmap = packet_mmap,
  3931. .sendpage = sock_no_sendpage,
  3932. };
  3933. static const struct net_proto_family packet_family_ops = {
  3934. .family = PF_PACKET,
  3935. .create = packet_create,
  3936. .owner = THIS_MODULE,
  3937. };
  3938. static struct notifier_block packet_netdev_notifier = {
  3939. .notifier_call = packet_notifier,
  3940. };
  3941. #ifdef CONFIG_PROC_FS
  3942. static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
  3943. __acquires(RCU)
  3944. {
  3945. struct net *net = seq_file_net(seq);
  3946. rcu_read_lock();
  3947. return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
  3948. }
  3949. static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  3950. {
  3951. struct net *net = seq_file_net(seq);
  3952. return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
  3953. }
  3954. static void packet_seq_stop(struct seq_file *seq, void *v)
  3955. __releases(RCU)
  3956. {
  3957. rcu_read_unlock();
  3958. }
  3959. static int packet_seq_show(struct seq_file *seq, void *v)
  3960. {
  3961. if (v == SEQ_START_TOKEN)
  3962. seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
  3963. else {
  3964. struct sock *s = sk_entry(v);
  3965. const struct packet_sock *po = pkt_sk(s);
  3966. seq_printf(seq,
  3967. "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
  3968. s,
  3969. refcount_read(&s->sk_refcnt),
  3970. s->sk_type,
  3971. ntohs(READ_ONCE(po->num)),
  3972. READ_ONCE(po->ifindex),
  3973. po->running,
  3974. atomic_read(&s->sk_rmem_alloc),
  3975. from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)),
  3976. sock_i_ino(s));
  3977. }
  3978. return 0;
  3979. }
  3980. static const struct seq_operations packet_seq_ops = {
  3981. .start = packet_seq_start,
  3982. .next = packet_seq_next,
  3983. .stop = packet_seq_stop,
  3984. .show = packet_seq_show,
  3985. };
  3986. static int packet_seq_open(struct inode *inode, struct file *file)
  3987. {
  3988. return seq_open_net(inode, file, &packet_seq_ops,
  3989. sizeof(struct seq_net_private));
  3990. }
  3991. static const struct file_operations packet_seq_fops = {
  3992. .owner = THIS_MODULE,
  3993. .open = packet_seq_open,
  3994. .read = seq_read,
  3995. .llseek = seq_lseek,
  3996. .release = seq_release_net,
  3997. };
  3998. #endif
  3999. static int __net_init packet_net_init(struct net *net)
  4000. {
  4001. mutex_init(&net->packet.sklist_lock);
  4002. INIT_HLIST_HEAD(&net->packet.sklist);
  4003. if (!proc_create("packet", 0, net->proc_net, &packet_seq_fops))
  4004. return -ENOMEM;
  4005. return 0;
  4006. }
  4007. static void __net_exit packet_net_exit(struct net *net)
  4008. {
  4009. remove_proc_entry("packet", net->proc_net);
  4010. }
  4011. static struct pernet_operations packet_net_ops = {
  4012. .init = packet_net_init,
  4013. .exit = packet_net_exit,
  4014. };
  4015. static void __exit packet_exit(void)
  4016. {
  4017. unregister_netdevice_notifier(&packet_netdev_notifier);
  4018. unregister_pernet_subsys(&packet_net_ops);
  4019. sock_unregister(PF_PACKET);
  4020. proto_unregister(&packet_proto);
  4021. }
  4022. static int __init packet_init(void)
  4023. {
  4024. int rc;
  4025. rc = proto_register(&packet_proto, 0);
  4026. if (rc)
  4027. goto out;
  4028. rc = sock_register(&packet_family_ops);
  4029. if (rc)
  4030. goto out_proto;
  4031. rc = register_pernet_subsys(&packet_net_ops);
  4032. if (rc)
  4033. goto out_sock;
  4034. rc = register_netdevice_notifier(&packet_netdev_notifier);
  4035. if (rc)
  4036. goto out_pernet;
  4037. return 0;
  4038. out_pernet:
  4039. unregister_pernet_subsys(&packet_net_ops);
  4040. out_sock:
  4041. sock_unregister(PF_PACKET);
  4042. out_proto:
  4043. proto_unregister(&packet_proto);
  4044. out:
  4045. return rc;
  4046. }
  4047. module_init(packet_init);
  4048. module_exit(packet_exit);
  4049. MODULE_LICENSE("GPL");
  4050. MODULE_ALIAS_NETPROTO(PF_PACKET);