af_packet.c 98 KB

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