af_packet.c 89 KB

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