af_packet.c 106 KB

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