af_packet.c 107 KB

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