af_packet.c 96 KB

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