af_packet.c 106 KB

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