af_packet.c 90 KB

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