af_packet.c 91 KB

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