af_packet.c 107 KB

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