af_packet.c 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577
  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 (u16) raw_smp_processor_id() % dev->real_num_tx_queues;
  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. sockc.tsflags = sk->sk_tsflags;
  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. sock_tx_timestamp(sk, sockc.tsflags, &skb_shinfo(skb)->tx_flags);
  1668. if (unlikely(extra_len == 4))
  1669. skb->no_fcs = 1;
  1670. skb_probe_transport_header(skb, 0);
  1671. dev_queue_xmit(skb);
  1672. rcu_read_unlock();
  1673. return len;
  1674. out_unlock:
  1675. rcu_read_unlock();
  1676. out_free:
  1677. kfree_skb(skb);
  1678. return err;
  1679. }
  1680. static unsigned int run_filter(struct sk_buff *skb,
  1681. const struct sock *sk,
  1682. unsigned int res)
  1683. {
  1684. struct sk_filter *filter;
  1685. rcu_read_lock();
  1686. filter = rcu_dereference(sk->sk_filter);
  1687. if (filter != NULL)
  1688. res = bpf_prog_run_clear_cb(filter->prog, skb);
  1689. rcu_read_unlock();
  1690. return res;
  1691. }
  1692. static int packet_rcv_vnet(struct msghdr *msg, const struct sk_buff *skb,
  1693. size_t *len)
  1694. {
  1695. struct virtio_net_hdr vnet_hdr;
  1696. if (*len < sizeof(vnet_hdr))
  1697. return -EINVAL;
  1698. *len -= sizeof(vnet_hdr);
  1699. if (virtio_net_hdr_from_skb(skb, &vnet_hdr, vio_le(), true, 0))
  1700. return -EINVAL;
  1701. return memcpy_to_msg(msg, (void *)&vnet_hdr, sizeof(vnet_hdr));
  1702. }
  1703. /*
  1704. * This function makes lazy skb cloning in hope that most of packets
  1705. * are discarded by BPF.
  1706. *
  1707. * Note tricky part: we DO mangle shared skb! skb->data, skb->len
  1708. * and skb->cb are mangled. It works because (and until) packets
  1709. * falling here are owned by current CPU. Output packets are cloned
  1710. * by dev_queue_xmit_nit(), input packets are processed by net_bh
  1711. * sequencially, so that if we return skb to original state on exit,
  1712. * we will not harm anyone.
  1713. */
  1714. static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
  1715. struct packet_type *pt, struct net_device *orig_dev)
  1716. {
  1717. struct sock *sk;
  1718. struct sockaddr_ll *sll;
  1719. struct packet_sock *po;
  1720. u8 *skb_head = skb->data;
  1721. int skb_len = skb->len;
  1722. unsigned int snaplen, res;
  1723. bool is_drop_n_account = false;
  1724. if (skb->pkt_type == PACKET_LOOPBACK)
  1725. goto drop;
  1726. sk = pt->af_packet_priv;
  1727. po = pkt_sk(sk);
  1728. if (!net_eq(dev_net(dev), sock_net(sk)))
  1729. goto drop;
  1730. skb->dev = dev;
  1731. if (dev->header_ops) {
  1732. /* The device has an explicit notion of ll header,
  1733. * exported to higher levels.
  1734. *
  1735. * Otherwise, the device hides details of its frame
  1736. * structure, so that corresponding packet head is
  1737. * never delivered to user.
  1738. */
  1739. if (sk->sk_type != SOCK_DGRAM)
  1740. skb_push(skb, skb->data - skb_mac_header(skb));
  1741. else if (skb->pkt_type == PACKET_OUTGOING) {
  1742. /* Special case: outgoing packets have ll header at head */
  1743. skb_pull(skb, skb_network_offset(skb));
  1744. }
  1745. }
  1746. snaplen = skb->len;
  1747. res = run_filter(skb, sk, snaplen);
  1748. if (!res)
  1749. goto drop_n_restore;
  1750. if (snaplen > res)
  1751. snaplen = res;
  1752. if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
  1753. goto drop_n_acct;
  1754. if (skb_shared(skb)) {
  1755. struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
  1756. if (nskb == NULL)
  1757. goto drop_n_acct;
  1758. if (skb_head != skb->data) {
  1759. skb->data = skb_head;
  1760. skb->len = skb_len;
  1761. }
  1762. consume_skb(skb);
  1763. skb = nskb;
  1764. }
  1765. sock_skb_cb_check_size(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8);
  1766. sll = &PACKET_SKB_CB(skb)->sa.ll;
  1767. sll->sll_hatype = dev->type;
  1768. sll->sll_pkttype = skb->pkt_type;
  1769. if (unlikely(po->origdev))
  1770. sll->sll_ifindex = orig_dev->ifindex;
  1771. else
  1772. sll->sll_ifindex = dev->ifindex;
  1773. sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
  1774. /* sll->sll_family and sll->sll_protocol are set in packet_recvmsg().
  1775. * Use their space for storing the original skb length.
  1776. */
  1777. PACKET_SKB_CB(skb)->sa.origlen = skb->len;
  1778. if (pskb_trim(skb, snaplen))
  1779. goto drop_n_acct;
  1780. skb_set_owner_r(skb, sk);
  1781. skb->dev = NULL;
  1782. skb_dst_drop(skb);
  1783. /* drop conntrack reference */
  1784. nf_reset(skb);
  1785. spin_lock(&sk->sk_receive_queue.lock);
  1786. po->stats.stats1.tp_packets++;
  1787. sock_skb_set_dropcount(sk, skb);
  1788. __skb_queue_tail(&sk->sk_receive_queue, skb);
  1789. spin_unlock(&sk->sk_receive_queue.lock);
  1790. sk->sk_data_ready(sk);
  1791. return 0;
  1792. drop_n_acct:
  1793. is_drop_n_account = true;
  1794. spin_lock(&sk->sk_receive_queue.lock);
  1795. po->stats.stats1.tp_drops++;
  1796. atomic_inc(&sk->sk_drops);
  1797. spin_unlock(&sk->sk_receive_queue.lock);
  1798. drop_n_restore:
  1799. if (skb_head != skb->data && skb_shared(skb)) {
  1800. skb->data = skb_head;
  1801. skb->len = skb_len;
  1802. }
  1803. drop:
  1804. if (!is_drop_n_account)
  1805. consume_skb(skb);
  1806. else
  1807. kfree_skb(skb);
  1808. return 0;
  1809. }
  1810. static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
  1811. struct packet_type *pt, struct net_device *orig_dev)
  1812. {
  1813. struct sock *sk;
  1814. struct packet_sock *po;
  1815. struct sockaddr_ll *sll;
  1816. union tpacket_uhdr h;
  1817. u8 *skb_head = skb->data;
  1818. int skb_len = skb->len;
  1819. unsigned int snaplen, res;
  1820. unsigned long status = TP_STATUS_USER;
  1821. unsigned short macoff, netoff, hdrlen;
  1822. struct sk_buff *copy_skb = NULL;
  1823. struct timespec ts;
  1824. __u32 ts_status;
  1825. bool is_drop_n_account = false;
  1826. bool do_vnet = false;
  1827. /* struct tpacket{2,3}_hdr is aligned to a multiple of TPACKET_ALIGNMENT.
  1828. * We may add members to them until current aligned size without forcing
  1829. * userspace to call getsockopt(..., PACKET_HDRLEN, ...).
  1830. */
  1831. BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h2)) != 32);
  1832. BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h3)) != 48);
  1833. if (skb->pkt_type == PACKET_LOOPBACK)
  1834. goto drop;
  1835. sk = pt->af_packet_priv;
  1836. po = pkt_sk(sk);
  1837. if (!net_eq(dev_net(dev), sock_net(sk)))
  1838. goto drop;
  1839. if (dev->header_ops) {
  1840. if (sk->sk_type != SOCK_DGRAM)
  1841. skb_push(skb, skb->data - skb_mac_header(skb));
  1842. else if (skb->pkt_type == PACKET_OUTGOING) {
  1843. /* Special case: outgoing packets have ll header at head */
  1844. skb_pull(skb, skb_network_offset(skb));
  1845. }
  1846. }
  1847. snaplen = skb->len;
  1848. res = run_filter(skb, sk, snaplen);
  1849. if (!res)
  1850. goto drop_n_restore;
  1851. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1852. status |= TP_STATUS_CSUMNOTREADY;
  1853. else if (skb->pkt_type != PACKET_OUTGOING &&
  1854. (skb->ip_summed == CHECKSUM_COMPLETE ||
  1855. skb_csum_unnecessary(skb)))
  1856. status |= TP_STATUS_CSUM_VALID;
  1857. if (snaplen > res)
  1858. snaplen = res;
  1859. if (sk->sk_type == SOCK_DGRAM) {
  1860. macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
  1861. po->tp_reserve;
  1862. } else {
  1863. unsigned int maclen = skb_network_offset(skb);
  1864. netoff = TPACKET_ALIGN(po->tp_hdrlen +
  1865. (maclen < 16 ? 16 : maclen)) +
  1866. po->tp_reserve;
  1867. if (po->has_vnet_hdr) {
  1868. netoff += sizeof(struct virtio_net_hdr);
  1869. do_vnet = true;
  1870. }
  1871. macoff = netoff - maclen;
  1872. }
  1873. if (po->tp_version <= TPACKET_V2) {
  1874. if (macoff + snaplen > po->rx_ring.frame_size) {
  1875. if (po->copy_thresh &&
  1876. atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
  1877. if (skb_shared(skb)) {
  1878. copy_skb = skb_clone(skb, GFP_ATOMIC);
  1879. } else {
  1880. copy_skb = skb_get(skb);
  1881. skb_head = skb->data;
  1882. }
  1883. if (copy_skb)
  1884. skb_set_owner_r(copy_skb, sk);
  1885. }
  1886. snaplen = po->rx_ring.frame_size - macoff;
  1887. if ((int)snaplen < 0) {
  1888. snaplen = 0;
  1889. do_vnet = false;
  1890. }
  1891. }
  1892. } else if (unlikely(macoff + snaplen >
  1893. GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len)) {
  1894. u32 nval;
  1895. nval = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len - macoff;
  1896. pr_err_once("tpacket_rcv: packet too big, clamped from %u to %u. macoff=%u\n",
  1897. snaplen, nval, macoff);
  1898. snaplen = nval;
  1899. if (unlikely((int)snaplen < 0)) {
  1900. snaplen = 0;
  1901. macoff = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len;
  1902. do_vnet = false;
  1903. }
  1904. }
  1905. spin_lock(&sk->sk_receive_queue.lock);
  1906. h.raw = packet_current_rx_frame(po, skb,
  1907. TP_STATUS_KERNEL, (macoff+snaplen));
  1908. if (!h.raw)
  1909. goto drop_n_account;
  1910. if (po->tp_version <= TPACKET_V2) {
  1911. packet_increment_rx_head(po, &po->rx_ring);
  1912. /*
  1913. * LOSING will be reported till you read the stats,
  1914. * because it's COR - Clear On Read.
  1915. * Anyways, moving it for V1/V2 only as V3 doesn't need this
  1916. * at packet level.
  1917. */
  1918. if (po->stats.stats1.tp_drops)
  1919. status |= TP_STATUS_LOSING;
  1920. }
  1921. po->stats.stats1.tp_packets++;
  1922. if (copy_skb) {
  1923. status |= TP_STATUS_COPY;
  1924. __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
  1925. }
  1926. spin_unlock(&sk->sk_receive_queue.lock);
  1927. if (do_vnet) {
  1928. if (virtio_net_hdr_from_skb(skb, h.raw + macoff -
  1929. sizeof(struct virtio_net_hdr),
  1930. vio_le(), true, 0)) {
  1931. spin_lock(&sk->sk_receive_queue.lock);
  1932. goto drop_n_account;
  1933. }
  1934. }
  1935. skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
  1936. if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
  1937. getnstimeofday(&ts);
  1938. status |= ts_status;
  1939. switch (po->tp_version) {
  1940. case TPACKET_V1:
  1941. h.h1->tp_len = skb->len;
  1942. h.h1->tp_snaplen = snaplen;
  1943. h.h1->tp_mac = macoff;
  1944. h.h1->tp_net = netoff;
  1945. h.h1->tp_sec = ts.tv_sec;
  1946. h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
  1947. hdrlen = sizeof(*h.h1);
  1948. break;
  1949. case TPACKET_V2:
  1950. h.h2->tp_len = skb->len;
  1951. h.h2->tp_snaplen = snaplen;
  1952. h.h2->tp_mac = macoff;
  1953. h.h2->tp_net = netoff;
  1954. h.h2->tp_sec = ts.tv_sec;
  1955. h.h2->tp_nsec = ts.tv_nsec;
  1956. if (skb_vlan_tag_present(skb)) {
  1957. h.h2->tp_vlan_tci = skb_vlan_tag_get(skb);
  1958. h.h2->tp_vlan_tpid = ntohs(skb->vlan_proto);
  1959. status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
  1960. } else {
  1961. h.h2->tp_vlan_tci = 0;
  1962. h.h2->tp_vlan_tpid = 0;
  1963. }
  1964. memset(h.h2->tp_padding, 0, sizeof(h.h2->tp_padding));
  1965. hdrlen = sizeof(*h.h2);
  1966. break;
  1967. case TPACKET_V3:
  1968. /* tp_nxt_offset,vlan are already populated above.
  1969. * So DONT clear those fields here
  1970. */
  1971. h.h3->tp_status |= status;
  1972. h.h3->tp_len = skb->len;
  1973. h.h3->tp_snaplen = snaplen;
  1974. h.h3->tp_mac = macoff;
  1975. h.h3->tp_net = netoff;
  1976. h.h3->tp_sec = ts.tv_sec;
  1977. h.h3->tp_nsec = ts.tv_nsec;
  1978. memset(h.h3->tp_padding, 0, sizeof(h.h3->tp_padding));
  1979. hdrlen = sizeof(*h.h3);
  1980. break;
  1981. default:
  1982. BUG();
  1983. }
  1984. sll = h.raw + TPACKET_ALIGN(hdrlen);
  1985. sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
  1986. sll->sll_family = AF_PACKET;
  1987. sll->sll_hatype = dev->type;
  1988. sll->sll_protocol = skb->protocol;
  1989. sll->sll_pkttype = skb->pkt_type;
  1990. if (unlikely(po->origdev))
  1991. sll->sll_ifindex = orig_dev->ifindex;
  1992. else
  1993. sll->sll_ifindex = dev->ifindex;
  1994. smp_mb();
  1995. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
  1996. if (po->tp_version <= TPACKET_V2) {
  1997. u8 *start, *end;
  1998. end = (u8 *) PAGE_ALIGN((unsigned long) h.raw +
  1999. macoff + snaplen);
  2000. for (start = h.raw; start < end; start += PAGE_SIZE)
  2001. flush_dcache_page(pgv_to_page(start));
  2002. }
  2003. smp_wmb();
  2004. #endif
  2005. if (po->tp_version <= TPACKET_V2) {
  2006. __packet_set_status(po, h.raw, status);
  2007. sk->sk_data_ready(sk);
  2008. } else {
  2009. prb_clear_blk_fill_status(&po->rx_ring);
  2010. }
  2011. drop_n_restore:
  2012. if (skb_head != skb->data && skb_shared(skb)) {
  2013. skb->data = skb_head;
  2014. skb->len = skb_len;
  2015. }
  2016. drop:
  2017. if (!is_drop_n_account)
  2018. consume_skb(skb);
  2019. else
  2020. kfree_skb(skb);
  2021. return 0;
  2022. drop_n_account:
  2023. is_drop_n_account = true;
  2024. po->stats.stats1.tp_drops++;
  2025. spin_unlock(&sk->sk_receive_queue.lock);
  2026. sk->sk_data_ready(sk);
  2027. kfree_skb(copy_skb);
  2028. goto drop_n_restore;
  2029. }
  2030. static void tpacket_destruct_skb(struct sk_buff *skb)
  2031. {
  2032. struct packet_sock *po = pkt_sk(skb->sk);
  2033. if (likely(po->tx_ring.pg_vec)) {
  2034. void *ph;
  2035. __u32 ts;
  2036. ph = skb_shinfo(skb)->destructor_arg;
  2037. packet_dec_pending(&po->tx_ring);
  2038. ts = __packet_set_timestamp(po, ph, skb);
  2039. __packet_set_status(po, ph, TP_STATUS_AVAILABLE | ts);
  2040. }
  2041. sock_wfree(skb);
  2042. }
  2043. static void tpacket_set_protocol(const struct net_device *dev,
  2044. struct sk_buff *skb)
  2045. {
  2046. if (dev->type == ARPHRD_ETHER) {
  2047. skb_reset_mac_header(skb);
  2048. skb->protocol = eth_hdr(skb)->h_proto;
  2049. }
  2050. }
  2051. static int __packet_snd_vnet_parse(struct virtio_net_hdr *vnet_hdr, size_t len)
  2052. {
  2053. if ((vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  2054. (__virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
  2055. __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2 >
  2056. __virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len)))
  2057. vnet_hdr->hdr_len = __cpu_to_virtio16(vio_le(),
  2058. __virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
  2059. __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2);
  2060. if (__virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len) > len)
  2061. return -EINVAL;
  2062. return 0;
  2063. }
  2064. static int packet_snd_vnet_parse(struct msghdr *msg, size_t *len,
  2065. struct virtio_net_hdr *vnet_hdr)
  2066. {
  2067. if (*len < sizeof(*vnet_hdr))
  2068. return -EINVAL;
  2069. *len -= sizeof(*vnet_hdr);
  2070. if (!copy_from_iter_full(vnet_hdr, sizeof(*vnet_hdr), &msg->msg_iter))
  2071. return -EFAULT;
  2072. return __packet_snd_vnet_parse(vnet_hdr, *len);
  2073. }
  2074. static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
  2075. void *frame, struct net_device *dev, void *data, int tp_len,
  2076. __be16 proto, unsigned char *addr, int hlen, int copylen,
  2077. const struct sockcm_cookie *sockc)
  2078. {
  2079. union tpacket_uhdr ph;
  2080. int to_write, offset, len, nr_frags, len_max;
  2081. struct socket *sock = po->sk.sk_socket;
  2082. struct page *page;
  2083. int err;
  2084. ph.raw = frame;
  2085. skb->protocol = proto;
  2086. skb->dev = dev;
  2087. skb->priority = po->sk.sk_priority;
  2088. skb->mark = po->sk.sk_mark;
  2089. sock_tx_timestamp(&po->sk, sockc->tsflags, &skb_shinfo(skb)->tx_flags);
  2090. skb_shinfo(skb)->destructor_arg = ph.raw;
  2091. skb_reserve(skb, hlen);
  2092. skb_reset_network_header(skb);
  2093. to_write = tp_len;
  2094. if (sock->type == SOCK_DGRAM) {
  2095. err = dev_hard_header(skb, dev, ntohs(proto), addr,
  2096. NULL, tp_len);
  2097. if (unlikely(err < 0))
  2098. return -EINVAL;
  2099. } else if (copylen) {
  2100. int hdrlen = min_t(int, copylen, tp_len);
  2101. skb_push(skb, dev->hard_header_len);
  2102. skb_put(skb, copylen - dev->hard_header_len);
  2103. err = skb_store_bits(skb, 0, data, hdrlen);
  2104. if (unlikely(err))
  2105. return err;
  2106. if (!dev_validate_header(dev, skb->data, hdrlen))
  2107. return -EINVAL;
  2108. if (!skb->protocol)
  2109. tpacket_set_protocol(dev, skb);
  2110. data += hdrlen;
  2111. to_write -= hdrlen;
  2112. }
  2113. offset = offset_in_page(data);
  2114. len_max = PAGE_SIZE - offset;
  2115. len = ((to_write > len_max) ? len_max : to_write);
  2116. skb->data_len = to_write;
  2117. skb->len += to_write;
  2118. skb->truesize += to_write;
  2119. refcount_add(to_write, &po->sk.sk_wmem_alloc);
  2120. while (likely(to_write)) {
  2121. nr_frags = skb_shinfo(skb)->nr_frags;
  2122. if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
  2123. pr_err("Packet exceed the number of skb frags(%lu)\n",
  2124. MAX_SKB_FRAGS);
  2125. return -EFAULT;
  2126. }
  2127. page = pgv_to_page(data);
  2128. data += len;
  2129. flush_dcache_page(page);
  2130. get_page(page);
  2131. skb_fill_page_desc(skb, nr_frags, page, offset, len);
  2132. to_write -= len;
  2133. offset = 0;
  2134. len_max = PAGE_SIZE;
  2135. len = ((to_write > len_max) ? len_max : to_write);
  2136. }
  2137. skb_probe_transport_header(skb, 0);
  2138. return tp_len;
  2139. }
  2140. static int tpacket_parse_header(struct packet_sock *po, void *frame,
  2141. int size_max, void **data)
  2142. {
  2143. union tpacket_uhdr ph;
  2144. int tp_len, off;
  2145. ph.raw = frame;
  2146. switch (po->tp_version) {
  2147. case TPACKET_V3:
  2148. if (ph.h3->tp_next_offset != 0) {
  2149. pr_warn_once("variable sized slot not supported");
  2150. return -EINVAL;
  2151. }
  2152. tp_len = ph.h3->tp_len;
  2153. break;
  2154. case TPACKET_V2:
  2155. tp_len = ph.h2->tp_len;
  2156. break;
  2157. default:
  2158. tp_len = ph.h1->tp_len;
  2159. break;
  2160. }
  2161. if (unlikely(tp_len > size_max)) {
  2162. pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
  2163. return -EMSGSIZE;
  2164. }
  2165. if (unlikely(po->tp_tx_has_off)) {
  2166. int off_min, off_max;
  2167. off_min = po->tp_hdrlen - sizeof(struct sockaddr_ll);
  2168. off_max = po->tx_ring.frame_size - tp_len;
  2169. if (po->sk.sk_type == SOCK_DGRAM) {
  2170. switch (po->tp_version) {
  2171. case TPACKET_V3:
  2172. off = ph.h3->tp_net;
  2173. break;
  2174. case TPACKET_V2:
  2175. off = ph.h2->tp_net;
  2176. break;
  2177. default:
  2178. off = ph.h1->tp_net;
  2179. break;
  2180. }
  2181. } else {
  2182. switch (po->tp_version) {
  2183. case TPACKET_V3:
  2184. off = ph.h3->tp_mac;
  2185. break;
  2186. case TPACKET_V2:
  2187. off = ph.h2->tp_mac;
  2188. break;
  2189. default:
  2190. off = ph.h1->tp_mac;
  2191. break;
  2192. }
  2193. }
  2194. if (unlikely((off < off_min) || (off_max < off)))
  2195. return -EINVAL;
  2196. } else {
  2197. off = po->tp_hdrlen - sizeof(struct sockaddr_ll);
  2198. }
  2199. *data = frame + off;
  2200. return tp_len;
  2201. }
  2202. static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
  2203. {
  2204. struct sk_buff *skb;
  2205. struct net_device *dev;
  2206. struct virtio_net_hdr *vnet_hdr = NULL;
  2207. struct sockcm_cookie sockc;
  2208. __be16 proto;
  2209. int err, reserve = 0;
  2210. void *ph;
  2211. DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
  2212. bool need_wait = !(msg->msg_flags & MSG_DONTWAIT);
  2213. int tp_len, size_max;
  2214. unsigned char *addr;
  2215. void *data;
  2216. int len_sum = 0;
  2217. int status = TP_STATUS_AVAILABLE;
  2218. int hlen, tlen, copylen = 0;
  2219. mutex_lock(&po->pg_vec_lock);
  2220. if (likely(saddr == NULL)) {
  2221. dev = packet_cached_dev_get(po);
  2222. proto = po->num;
  2223. addr = NULL;
  2224. } else {
  2225. err = -EINVAL;
  2226. if (msg->msg_namelen < sizeof(struct sockaddr_ll))
  2227. goto out;
  2228. if (msg->msg_namelen < (saddr->sll_halen
  2229. + offsetof(struct sockaddr_ll,
  2230. sll_addr)))
  2231. goto out;
  2232. proto = saddr->sll_protocol;
  2233. addr = saddr->sll_addr;
  2234. dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
  2235. }
  2236. err = -ENXIO;
  2237. if (unlikely(dev == NULL))
  2238. goto out;
  2239. err = -ENETDOWN;
  2240. if (unlikely(!(dev->flags & IFF_UP)))
  2241. goto out_put;
  2242. sockc.tsflags = po->sk.sk_tsflags;
  2243. if (msg->msg_controllen) {
  2244. err = sock_cmsg_send(&po->sk, msg, &sockc);
  2245. if (unlikely(err))
  2246. goto out_put;
  2247. }
  2248. if (po->sk.sk_socket->type == SOCK_RAW)
  2249. reserve = dev->hard_header_len;
  2250. size_max = po->tx_ring.frame_size
  2251. - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
  2252. if ((size_max > dev->mtu + reserve + VLAN_HLEN) && !po->has_vnet_hdr)
  2253. size_max = dev->mtu + reserve + VLAN_HLEN;
  2254. do {
  2255. ph = packet_current_frame(po, &po->tx_ring,
  2256. TP_STATUS_SEND_REQUEST);
  2257. if (unlikely(ph == NULL)) {
  2258. if (need_wait && need_resched())
  2259. schedule();
  2260. continue;
  2261. }
  2262. skb = NULL;
  2263. tp_len = tpacket_parse_header(po, ph, size_max, &data);
  2264. if (tp_len < 0)
  2265. goto tpacket_error;
  2266. status = TP_STATUS_SEND_REQUEST;
  2267. hlen = LL_RESERVED_SPACE(dev);
  2268. tlen = dev->needed_tailroom;
  2269. if (po->has_vnet_hdr) {
  2270. vnet_hdr = data;
  2271. data += sizeof(*vnet_hdr);
  2272. tp_len -= sizeof(*vnet_hdr);
  2273. if (tp_len < 0 ||
  2274. __packet_snd_vnet_parse(vnet_hdr, tp_len)) {
  2275. tp_len = -EINVAL;
  2276. goto tpacket_error;
  2277. }
  2278. copylen = __virtio16_to_cpu(vio_le(),
  2279. vnet_hdr->hdr_len);
  2280. }
  2281. copylen = max_t(int, copylen, dev->hard_header_len);
  2282. skb = sock_alloc_send_skb(&po->sk,
  2283. hlen + tlen + sizeof(struct sockaddr_ll) +
  2284. (copylen - dev->hard_header_len),
  2285. !need_wait, &err);
  2286. if (unlikely(skb == NULL)) {
  2287. /* we assume the socket was initially writeable ... */
  2288. if (likely(len_sum > 0))
  2289. err = len_sum;
  2290. goto out_status;
  2291. }
  2292. tp_len = tpacket_fill_skb(po, skb, ph, dev, data, tp_len, proto,
  2293. addr, hlen, copylen, &sockc);
  2294. if (likely(tp_len >= 0) &&
  2295. tp_len > dev->mtu + reserve &&
  2296. !po->has_vnet_hdr &&
  2297. !packet_extra_vlan_len_allowed(dev, skb))
  2298. tp_len = -EMSGSIZE;
  2299. if (unlikely(tp_len < 0)) {
  2300. tpacket_error:
  2301. if (po->tp_loss) {
  2302. __packet_set_status(po, ph,
  2303. TP_STATUS_AVAILABLE);
  2304. packet_increment_head(&po->tx_ring);
  2305. kfree_skb(skb);
  2306. continue;
  2307. } else {
  2308. status = TP_STATUS_WRONG_FORMAT;
  2309. err = tp_len;
  2310. goto out_status;
  2311. }
  2312. }
  2313. if (po->has_vnet_hdr && virtio_net_hdr_to_skb(skb, vnet_hdr,
  2314. vio_le())) {
  2315. tp_len = -EINVAL;
  2316. goto tpacket_error;
  2317. }
  2318. skb->destructor = tpacket_destruct_skb;
  2319. __packet_set_status(po, ph, TP_STATUS_SENDING);
  2320. packet_inc_pending(&po->tx_ring);
  2321. status = TP_STATUS_SEND_REQUEST;
  2322. err = po->xmit(skb);
  2323. if (unlikely(err > 0)) {
  2324. err = net_xmit_errno(err);
  2325. if (err && __packet_get_status(po, ph) ==
  2326. TP_STATUS_AVAILABLE) {
  2327. /* skb was destructed already */
  2328. skb = NULL;
  2329. goto out_status;
  2330. }
  2331. /*
  2332. * skb was dropped but not destructed yet;
  2333. * let's treat it like congestion or err < 0
  2334. */
  2335. err = 0;
  2336. }
  2337. packet_increment_head(&po->tx_ring);
  2338. len_sum += tp_len;
  2339. } while (likely((ph != NULL) ||
  2340. /* Note: packet_read_pending() might be slow if we have
  2341. * to call it as it's per_cpu variable, but in fast-path
  2342. * we already short-circuit the loop with the first
  2343. * condition, and luckily don't have to go that path
  2344. * anyway.
  2345. */
  2346. (need_wait && packet_read_pending(&po->tx_ring))));
  2347. err = len_sum;
  2348. goto out_put;
  2349. out_status:
  2350. __packet_set_status(po, ph, status);
  2351. kfree_skb(skb);
  2352. out_put:
  2353. dev_put(dev);
  2354. out:
  2355. mutex_unlock(&po->pg_vec_lock);
  2356. return err;
  2357. }
  2358. static struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
  2359. size_t reserve, size_t len,
  2360. size_t linear, int noblock,
  2361. int *err)
  2362. {
  2363. struct sk_buff *skb;
  2364. /* Under a page? Don't bother with paged skb. */
  2365. if (prepad + len < PAGE_SIZE || !linear)
  2366. linear = len;
  2367. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  2368. err, 0);
  2369. if (!skb)
  2370. return NULL;
  2371. skb_reserve(skb, reserve);
  2372. skb_put(skb, linear);
  2373. skb->data_len = len - linear;
  2374. skb->len += len - linear;
  2375. return skb;
  2376. }
  2377. static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
  2378. {
  2379. struct sock *sk = sock->sk;
  2380. DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
  2381. struct sk_buff *skb;
  2382. struct net_device *dev;
  2383. __be16 proto;
  2384. unsigned char *addr;
  2385. int err, reserve = 0;
  2386. struct sockcm_cookie sockc;
  2387. struct virtio_net_hdr vnet_hdr = { 0 };
  2388. int offset = 0;
  2389. struct packet_sock *po = pkt_sk(sk);
  2390. bool has_vnet_hdr = false;
  2391. int hlen, tlen, linear;
  2392. int extra_len = 0;
  2393. /*
  2394. * Get and verify the address.
  2395. */
  2396. if (likely(saddr == NULL)) {
  2397. dev = packet_cached_dev_get(po);
  2398. proto = po->num;
  2399. addr = NULL;
  2400. } else {
  2401. err = -EINVAL;
  2402. if (msg->msg_namelen < sizeof(struct sockaddr_ll))
  2403. goto out;
  2404. if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
  2405. goto out;
  2406. proto = saddr->sll_protocol;
  2407. addr = saddr->sll_addr;
  2408. dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
  2409. }
  2410. err = -ENXIO;
  2411. if (unlikely(dev == NULL))
  2412. goto out_unlock;
  2413. err = -ENETDOWN;
  2414. if (unlikely(!(dev->flags & IFF_UP)))
  2415. goto out_unlock;
  2416. sockc.tsflags = sk->sk_tsflags;
  2417. sockc.mark = sk->sk_mark;
  2418. if (msg->msg_controllen) {
  2419. err = sock_cmsg_send(sk, msg, &sockc);
  2420. if (unlikely(err))
  2421. goto out_unlock;
  2422. }
  2423. if (sock->type == SOCK_RAW)
  2424. reserve = dev->hard_header_len;
  2425. if (po->has_vnet_hdr) {
  2426. err = packet_snd_vnet_parse(msg, &len, &vnet_hdr);
  2427. if (err)
  2428. goto out_unlock;
  2429. has_vnet_hdr = true;
  2430. }
  2431. if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
  2432. if (!netif_supports_nofcs(dev)) {
  2433. err = -EPROTONOSUPPORT;
  2434. goto out_unlock;
  2435. }
  2436. extra_len = 4; /* We're doing our own CRC */
  2437. }
  2438. err = -EMSGSIZE;
  2439. if (!vnet_hdr.gso_type &&
  2440. (len > dev->mtu + reserve + VLAN_HLEN + extra_len))
  2441. goto out_unlock;
  2442. err = -ENOBUFS;
  2443. hlen = LL_RESERVED_SPACE(dev);
  2444. tlen = dev->needed_tailroom;
  2445. linear = __virtio16_to_cpu(vio_le(), vnet_hdr.hdr_len);
  2446. linear = max(linear, min_t(int, len, dev->hard_header_len));
  2447. skb = packet_alloc_skb(sk, hlen + tlen, hlen, len, linear,
  2448. msg->msg_flags & MSG_DONTWAIT, &err);
  2449. if (skb == NULL)
  2450. goto out_unlock;
  2451. skb_reset_network_header(skb);
  2452. err = -EINVAL;
  2453. if (sock->type == SOCK_DGRAM) {
  2454. offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len);
  2455. if (unlikely(offset < 0))
  2456. goto out_free;
  2457. } else if (reserve) {
  2458. skb_reserve(skb, -reserve);
  2459. }
  2460. /* Returns -EFAULT on error */
  2461. err = skb_copy_datagram_from_iter(skb, offset, &msg->msg_iter, len);
  2462. if (err)
  2463. goto out_free;
  2464. if (sock->type == SOCK_RAW &&
  2465. !dev_validate_header(dev, skb->data, len)) {
  2466. err = -EINVAL;
  2467. goto out_free;
  2468. }
  2469. sock_tx_timestamp(sk, sockc.tsflags, &skb_shinfo(skb)->tx_flags);
  2470. if (!vnet_hdr.gso_type && (len > dev->mtu + reserve + extra_len) &&
  2471. !packet_extra_vlan_len_allowed(dev, skb)) {
  2472. err = -EMSGSIZE;
  2473. goto out_free;
  2474. }
  2475. skb->protocol = proto;
  2476. skb->dev = dev;
  2477. skb->priority = sk->sk_priority;
  2478. skb->mark = sockc.mark;
  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_mask(struct socket *sock, __poll_t events)
  3484. {
  3485. struct sock *sk = sock->sk;
  3486. struct packet_sock *po = pkt_sk(sk);
  3487. __poll_t mask = datagram_poll_mask(sock, events);
  3488. spin_lock_bh(&sk->sk_receive_queue.lock);
  3489. if (po->rx_ring.pg_vec) {
  3490. if (!packet_previous_rx_frame(po, &po->rx_ring,
  3491. TP_STATUS_KERNEL))
  3492. mask |= EPOLLIN | EPOLLRDNORM;
  3493. }
  3494. if (po->pressure && __packet_rcv_has_room(po, NULL) == ROOM_NORMAL)
  3495. po->pressure = 0;
  3496. spin_unlock_bh(&sk->sk_receive_queue.lock);
  3497. spin_lock_bh(&sk->sk_write_queue.lock);
  3498. if (po->tx_ring.pg_vec) {
  3499. if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
  3500. mask |= EPOLLOUT | EPOLLWRNORM;
  3501. }
  3502. spin_unlock_bh(&sk->sk_write_queue.lock);
  3503. return mask;
  3504. }
  3505. /* Dirty? Well, I still did not learn better way to account
  3506. * for user mmaps.
  3507. */
  3508. static void packet_mm_open(struct vm_area_struct *vma)
  3509. {
  3510. struct file *file = vma->vm_file;
  3511. struct socket *sock = file->private_data;
  3512. struct sock *sk = sock->sk;
  3513. if (sk)
  3514. atomic_inc(&pkt_sk(sk)->mapped);
  3515. }
  3516. static void packet_mm_close(struct vm_area_struct *vma)
  3517. {
  3518. struct file *file = vma->vm_file;
  3519. struct socket *sock = file->private_data;
  3520. struct sock *sk = sock->sk;
  3521. if (sk)
  3522. atomic_dec(&pkt_sk(sk)->mapped);
  3523. }
  3524. static const struct vm_operations_struct packet_mmap_ops = {
  3525. .open = packet_mm_open,
  3526. .close = packet_mm_close,
  3527. };
  3528. static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
  3529. unsigned int len)
  3530. {
  3531. int i;
  3532. for (i = 0; i < len; i++) {
  3533. if (likely(pg_vec[i].buffer)) {
  3534. if (is_vmalloc_addr(pg_vec[i].buffer))
  3535. vfree(pg_vec[i].buffer);
  3536. else
  3537. free_pages((unsigned long)pg_vec[i].buffer,
  3538. order);
  3539. pg_vec[i].buffer = NULL;
  3540. }
  3541. }
  3542. kfree(pg_vec);
  3543. }
  3544. static char *alloc_one_pg_vec_page(unsigned long order)
  3545. {
  3546. char *buffer;
  3547. gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
  3548. __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
  3549. buffer = (char *) __get_free_pages(gfp_flags, order);
  3550. if (buffer)
  3551. return buffer;
  3552. /* __get_free_pages failed, fall back to vmalloc */
  3553. buffer = vzalloc(array_size((1 << order), PAGE_SIZE));
  3554. if (buffer)
  3555. return buffer;
  3556. /* vmalloc failed, lets dig into swap here */
  3557. gfp_flags &= ~__GFP_NORETRY;
  3558. buffer = (char *) __get_free_pages(gfp_flags, order);
  3559. if (buffer)
  3560. return buffer;
  3561. /* complete and utter failure */
  3562. return NULL;
  3563. }
  3564. static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
  3565. {
  3566. unsigned int block_nr = req->tp_block_nr;
  3567. struct pgv *pg_vec;
  3568. int i;
  3569. pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
  3570. if (unlikely(!pg_vec))
  3571. goto out;
  3572. for (i = 0; i < block_nr; i++) {
  3573. pg_vec[i].buffer = alloc_one_pg_vec_page(order);
  3574. if (unlikely(!pg_vec[i].buffer))
  3575. goto out_free_pgvec;
  3576. }
  3577. out:
  3578. return pg_vec;
  3579. out_free_pgvec:
  3580. free_pg_vec(pg_vec, order, block_nr);
  3581. pg_vec = NULL;
  3582. goto out;
  3583. }
  3584. static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
  3585. int closing, int tx_ring)
  3586. {
  3587. struct pgv *pg_vec = NULL;
  3588. struct packet_sock *po = pkt_sk(sk);
  3589. int was_running, order = 0;
  3590. struct packet_ring_buffer *rb;
  3591. struct sk_buff_head *rb_queue;
  3592. __be16 num;
  3593. int err = -EINVAL;
  3594. /* Added to avoid minimal code churn */
  3595. struct tpacket_req *req = &req_u->req;
  3596. rb = tx_ring ? &po->tx_ring : &po->rx_ring;
  3597. rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
  3598. err = -EBUSY;
  3599. if (!closing) {
  3600. if (atomic_read(&po->mapped))
  3601. goto out;
  3602. if (packet_read_pending(rb))
  3603. goto out;
  3604. }
  3605. if (req->tp_block_nr) {
  3606. /* Sanity tests and some calculations */
  3607. err = -EBUSY;
  3608. if (unlikely(rb->pg_vec))
  3609. goto out;
  3610. switch (po->tp_version) {
  3611. case TPACKET_V1:
  3612. po->tp_hdrlen = TPACKET_HDRLEN;
  3613. break;
  3614. case TPACKET_V2:
  3615. po->tp_hdrlen = TPACKET2_HDRLEN;
  3616. break;
  3617. case TPACKET_V3:
  3618. po->tp_hdrlen = TPACKET3_HDRLEN;
  3619. break;
  3620. }
  3621. err = -EINVAL;
  3622. if (unlikely((int)req->tp_block_size <= 0))
  3623. goto out;
  3624. if (unlikely(!PAGE_ALIGNED(req->tp_block_size)))
  3625. goto out;
  3626. if (po->tp_version >= TPACKET_V3 &&
  3627. req->tp_block_size <=
  3628. BLK_PLUS_PRIV((u64)req_u->req3.tp_sizeof_priv) + sizeof(struct tpacket3_hdr))
  3629. goto out;
  3630. if (unlikely(req->tp_frame_size < po->tp_hdrlen +
  3631. po->tp_reserve))
  3632. goto out;
  3633. if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
  3634. goto out;
  3635. rb->frames_per_block = req->tp_block_size / req->tp_frame_size;
  3636. if (unlikely(rb->frames_per_block == 0))
  3637. goto out;
  3638. if (unlikely(req->tp_block_size > UINT_MAX / req->tp_block_nr))
  3639. goto out;
  3640. if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
  3641. req->tp_frame_nr))
  3642. goto out;
  3643. err = -ENOMEM;
  3644. order = get_order(req->tp_block_size);
  3645. pg_vec = alloc_pg_vec(req, order);
  3646. if (unlikely(!pg_vec))
  3647. goto out;
  3648. switch (po->tp_version) {
  3649. case TPACKET_V3:
  3650. /* Block transmit is not supported yet */
  3651. if (!tx_ring) {
  3652. init_prb_bdqc(po, rb, pg_vec, req_u);
  3653. } else {
  3654. struct tpacket_req3 *req3 = &req_u->req3;
  3655. if (req3->tp_retire_blk_tov ||
  3656. req3->tp_sizeof_priv ||
  3657. req3->tp_feature_req_word) {
  3658. err = -EINVAL;
  3659. goto out;
  3660. }
  3661. }
  3662. break;
  3663. default:
  3664. break;
  3665. }
  3666. }
  3667. /* Done */
  3668. else {
  3669. err = -EINVAL;
  3670. if (unlikely(req->tp_frame_nr))
  3671. goto out;
  3672. }
  3673. /* Detach socket from network */
  3674. spin_lock(&po->bind_lock);
  3675. was_running = po->running;
  3676. num = po->num;
  3677. if (was_running) {
  3678. po->num = 0;
  3679. __unregister_prot_hook(sk, false);
  3680. }
  3681. spin_unlock(&po->bind_lock);
  3682. synchronize_net();
  3683. err = -EBUSY;
  3684. mutex_lock(&po->pg_vec_lock);
  3685. if (closing || atomic_read(&po->mapped) == 0) {
  3686. err = 0;
  3687. spin_lock_bh(&rb_queue->lock);
  3688. swap(rb->pg_vec, pg_vec);
  3689. rb->frame_max = (req->tp_frame_nr - 1);
  3690. rb->head = 0;
  3691. rb->frame_size = req->tp_frame_size;
  3692. spin_unlock_bh(&rb_queue->lock);
  3693. swap(rb->pg_vec_order, order);
  3694. swap(rb->pg_vec_len, req->tp_block_nr);
  3695. rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
  3696. po->prot_hook.func = (po->rx_ring.pg_vec) ?
  3697. tpacket_rcv : packet_rcv;
  3698. skb_queue_purge(rb_queue);
  3699. if (atomic_read(&po->mapped))
  3700. pr_err("packet_mmap: vma is busy: %d\n",
  3701. atomic_read(&po->mapped));
  3702. }
  3703. mutex_unlock(&po->pg_vec_lock);
  3704. spin_lock(&po->bind_lock);
  3705. if (was_running) {
  3706. po->num = num;
  3707. register_prot_hook(sk);
  3708. }
  3709. spin_unlock(&po->bind_lock);
  3710. if (pg_vec && (po->tp_version > TPACKET_V2)) {
  3711. /* Because we don't support block-based V3 on tx-ring */
  3712. if (!tx_ring)
  3713. prb_shutdown_retire_blk_timer(po, rb_queue);
  3714. }
  3715. if (pg_vec)
  3716. free_pg_vec(pg_vec, order, req->tp_block_nr);
  3717. out:
  3718. return err;
  3719. }
  3720. static int packet_mmap(struct file *file, struct socket *sock,
  3721. struct vm_area_struct *vma)
  3722. {
  3723. struct sock *sk = sock->sk;
  3724. struct packet_sock *po = pkt_sk(sk);
  3725. unsigned long size, expected_size;
  3726. struct packet_ring_buffer *rb;
  3727. unsigned long start;
  3728. int err = -EINVAL;
  3729. int i;
  3730. if (vma->vm_pgoff)
  3731. return -EINVAL;
  3732. mutex_lock(&po->pg_vec_lock);
  3733. expected_size = 0;
  3734. for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
  3735. if (rb->pg_vec) {
  3736. expected_size += rb->pg_vec_len
  3737. * rb->pg_vec_pages
  3738. * PAGE_SIZE;
  3739. }
  3740. }
  3741. if (expected_size == 0)
  3742. goto out;
  3743. size = vma->vm_end - vma->vm_start;
  3744. if (size != expected_size)
  3745. goto out;
  3746. start = vma->vm_start;
  3747. for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
  3748. if (rb->pg_vec == NULL)
  3749. continue;
  3750. for (i = 0; i < rb->pg_vec_len; i++) {
  3751. struct page *page;
  3752. void *kaddr = rb->pg_vec[i].buffer;
  3753. int pg_num;
  3754. for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
  3755. page = pgv_to_page(kaddr);
  3756. err = vm_insert_page(vma, start, page);
  3757. if (unlikely(err))
  3758. goto out;
  3759. start += PAGE_SIZE;
  3760. kaddr += PAGE_SIZE;
  3761. }
  3762. }
  3763. }
  3764. atomic_inc(&po->mapped);
  3765. vma->vm_ops = &packet_mmap_ops;
  3766. err = 0;
  3767. out:
  3768. mutex_unlock(&po->pg_vec_lock);
  3769. return err;
  3770. }
  3771. static const struct proto_ops packet_ops_spkt = {
  3772. .family = PF_PACKET,
  3773. .owner = THIS_MODULE,
  3774. .release = packet_release,
  3775. .bind = packet_bind_spkt,
  3776. .connect = sock_no_connect,
  3777. .socketpair = sock_no_socketpair,
  3778. .accept = sock_no_accept,
  3779. .getname = packet_getname_spkt,
  3780. .poll_mask = datagram_poll_mask,
  3781. .ioctl = packet_ioctl,
  3782. .listen = sock_no_listen,
  3783. .shutdown = sock_no_shutdown,
  3784. .setsockopt = sock_no_setsockopt,
  3785. .getsockopt = sock_no_getsockopt,
  3786. .sendmsg = packet_sendmsg_spkt,
  3787. .recvmsg = packet_recvmsg,
  3788. .mmap = sock_no_mmap,
  3789. .sendpage = sock_no_sendpage,
  3790. };
  3791. static const struct proto_ops packet_ops = {
  3792. .family = PF_PACKET,
  3793. .owner = THIS_MODULE,
  3794. .release = packet_release,
  3795. .bind = packet_bind,
  3796. .connect = sock_no_connect,
  3797. .socketpair = sock_no_socketpair,
  3798. .accept = sock_no_accept,
  3799. .getname = packet_getname,
  3800. .poll_mask = packet_poll_mask,
  3801. .ioctl = packet_ioctl,
  3802. .listen = sock_no_listen,
  3803. .shutdown = sock_no_shutdown,
  3804. .setsockopt = packet_setsockopt,
  3805. .getsockopt = packet_getsockopt,
  3806. #ifdef CONFIG_COMPAT
  3807. .compat_setsockopt = compat_packet_setsockopt,
  3808. #endif
  3809. .sendmsg = packet_sendmsg,
  3810. .recvmsg = packet_recvmsg,
  3811. .mmap = packet_mmap,
  3812. .sendpage = sock_no_sendpage,
  3813. };
  3814. static const struct net_proto_family packet_family_ops = {
  3815. .family = PF_PACKET,
  3816. .create = packet_create,
  3817. .owner = THIS_MODULE,
  3818. };
  3819. static struct notifier_block packet_netdev_notifier = {
  3820. .notifier_call = packet_notifier,
  3821. };
  3822. #ifdef CONFIG_PROC_FS
  3823. static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
  3824. __acquires(RCU)
  3825. {
  3826. struct net *net = seq_file_net(seq);
  3827. rcu_read_lock();
  3828. return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
  3829. }
  3830. static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  3831. {
  3832. struct net *net = seq_file_net(seq);
  3833. return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
  3834. }
  3835. static void packet_seq_stop(struct seq_file *seq, void *v)
  3836. __releases(RCU)
  3837. {
  3838. rcu_read_unlock();
  3839. }
  3840. static int packet_seq_show(struct seq_file *seq, void *v)
  3841. {
  3842. if (v == SEQ_START_TOKEN)
  3843. seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
  3844. else {
  3845. struct sock *s = sk_entry(v);
  3846. const struct packet_sock *po = pkt_sk(s);
  3847. seq_printf(seq,
  3848. "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
  3849. s,
  3850. refcount_read(&s->sk_refcnt),
  3851. s->sk_type,
  3852. ntohs(po->num),
  3853. po->ifindex,
  3854. po->running,
  3855. atomic_read(&s->sk_rmem_alloc),
  3856. from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)),
  3857. sock_i_ino(s));
  3858. }
  3859. return 0;
  3860. }
  3861. static const struct seq_operations packet_seq_ops = {
  3862. .start = packet_seq_start,
  3863. .next = packet_seq_next,
  3864. .stop = packet_seq_stop,
  3865. .show = packet_seq_show,
  3866. };
  3867. #endif
  3868. static int __net_init packet_net_init(struct net *net)
  3869. {
  3870. mutex_init(&net->packet.sklist_lock);
  3871. INIT_HLIST_HEAD(&net->packet.sklist);
  3872. if (!proc_create_net("packet", 0, net->proc_net, &packet_seq_ops,
  3873. sizeof(struct seq_net_private)))
  3874. return -ENOMEM;
  3875. return 0;
  3876. }
  3877. static void __net_exit packet_net_exit(struct net *net)
  3878. {
  3879. remove_proc_entry("packet", net->proc_net);
  3880. WARN_ON_ONCE(!hlist_empty(&net->packet.sklist));
  3881. }
  3882. static struct pernet_operations packet_net_ops = {
  3883. .init = packet_net_init,
  3884. .exit = packet_net_exit,
  3885. };
  3886. static void __exit packet_exit(void)
  3887. {
  3888. unregister_netdevice_notifier(&packet_netdev_notifier);
  3889. unregister_pernet_subsys(&packet_net_ops);
  3890. sock_unregister(PF_PACKET);
  3891. proto_unregister(&packet_proto);
  3892. }
  3893. static int __init packet_init(void)
  3894. {
  3895. int rc = proto_register(&packet_proto, 0);
  3896. if (rc != 0)
  3897. goto out;
  3898. sock_register(&packet_family_ops);
  3899. register_pernet_subsys(&packet_net_ops);
  3900. register_netdevice_notifier(&packet_netdev_notifier);
  3901. out:
  3902. return rc;
  3903. }
  3904. module_init(packet_init);
  3905. module_exit(packet_exit);
  3906. MODULE_LICENSE("GPL");
  3907. MODULE_ALIAS_NETPROTO(PF_PACKET);