af_packet.c 106 KB

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