af_packet.c 103 KB

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