af_packet.c 107 KB

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