af_packet.c 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872
  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. if (po->tp_tx_has_off) {
  1618. int off_min, off_max, off;
  1619. off_min = po->tp_hdrlen - sizeof(struct sockaddr_ll);
  1620. off_max = po->tx_ring.frame_size - tp_len;
  1621. if (sock->type == SOCK_DGRAM) {
  1622. switch (po->tp_version) {
  1623. case TPACKET_V2:
  1624. off = ph.h2->tp_net;
  1625. break;
  1626. default:
  1627. off = ph.h1->tp_net;
  1628. break;
  1629. }
  1630. } else {
  1631. switch (po->tp_version) {
  1632. case TPACKET_V2:
  1633. off = ph.h2->tp_mac;
  1634. break;
  1635. default:
  1636. off = ph.h1->tp_mac;
  1637. break;
  1638. }
  1639. }
  1640. if (unlikely((off < off_min) || (off_max < off)))
  1641. return -EINVAL;
  1642. data = ph.raw + off;
  1643. } else {
  1644. data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll);
  1645. }
  1646. to_write = tp_len;
  1647. if (sock->type == SOCK_DGRAM) {
  1648. err = dev_hard_header(skb, dev, ntohs(proto), addr,
  1649. NULL, tp_len);
  1650. if (unlikely(err < 0))
  1651. return -EINVAL;
  1652. } else if (dev->hard_header_len) {
  1653. /* net device doesn't like empty head */
  1654. if (unlikely(tp_len <= dev->hard_header_len)) {
  1655. pr_err("packet size is too short (%d < %d)\n",
  1656. tp_len, dev->hard_header_len);
  1657. return -EINVAL;
  1658. }
  1659. skb_push(skb, dev->hard_header_len);
  1660. err = skb_store_bits(skb, 0, data,
  1661. dev->hard_header_len);
  1662. if (unlikely(err))
  1663. return err;
  1664. data += dev->hard_header_len;
  1665. to_write -= dev->hard_header_len;
  1666. }
  1667. offset = offset_in_page(data);
  1668. len_max = PAGE_SIZE - offset;
  1669. len = ((to_write > len_max) ? len_max : to_write);
  1670. skb->data_len = to_write;
  1671. skb->len += to_write;
  1672. skb->truesize += to_write;
  1673. atomic_add(to_write, &po->sk.sk_wmem_alloc);
  1674. while (likely(to_write)) {
  1675. nr_frags = skb_shinfo(skb)->nr_frags;
  1676. if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
  1677. pr_err("Packet exceed the number of skb frags(%lu)\n",
  1678. MAX_SKB_FRAGS);
  1679. return -EFAULT;
  1680. }
  1681. page = pgv_to_page(data);
  1682. data += len;
  1683. flush_dcache_page(page);
  1684. get_page(page);
  1685. skb_fill_page_desc(skb, nr_frags, page, offset, len);
  1686. to_write -= len;
  1687. offset = 0;
  1688. len_max = PAGE_SIZE;
  1689. len = ((to_write > len_max) ? len_max : to_write);
  1690. }
  1691. return tp_len;
  1692. }
  1693. static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
  1694. {
  1695. struct sk_buff *skb;
  1696. struct net_device *dev;
  1697. __be16 proto;
  1698. bool need_rls_dev = false;
  1699. int err, reserve = 0;
  1700. void *ph;
  1701. struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
  1702. int tp_len, size_max;
  1703. unsigned char *addr;
  1704. int len_sum = 0;
  1705. int status = TP_STATUS_AVAILABLE;
  1706. int hlen, tlen;
  1707. mutex_lock(&po->pg_vec_lock);
  1708. if (saddr == NULL) {
  1709. dev = po->prot_hook.dev;
  1710. proto = po->num;
  1711. addr = NULL;
  1712. } else {
  1713. err = -EINVAL;
  1714. if (msg->msg_namelen < sizeof(struct sockaddr_ll))
  1715. goto out;
  1716. if (msg->msg_namelen < (saddr->sll_halen
  1717. + offsetof(struct sockaddr_ll,
  1718. sll_addr)))
  1719. goto out;
  1720. proto = saddr->sll_protocol;
  1721. addr = saddr->sll_addr;
  1722. dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
  1723. need_rls_dev = true;
  1724. }
  1725. err = -ENXIO;
  1726. if (unlikely(dev == NULL))
  1727. goto out;
  1728. reserve = dev->hard_header_len;
  1729. err = -ENETDOWN;
  1730. if (unlikely(!(dev->flags & IFF_UP)))
  1731. goto out_put;
  1732. size_max = po->tx_ring.frame_size
  1733. - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
  1734. if (size_max > dev->mtu + reserve)
  1735. size_max = dev->mtu + reserve;
  1736. do {
  1737. ph = packet_current_frame(po, &po->tx_ring,
  1738. TP_STATUS_SEND_REQUEST);
  1739. if (unlikely(ph == NULL)) {
  1740. schedule();
  1741. continue;
  1742. }
  1743. status = TP_STATUS_SEND_REQUEST;
  1744. hlen = LL_RESERVED_SPACE(dev);
  1745. tlen = dev->needed_tailroom;
  1746. skb = sock_alloc_send_skb(&po->sk,
  1747. hlen + tlen + sizeof(struct sockaddr_ll),
  1748. 0, &err);
  1749. if (unlikely(skb == NULL))
  1750. goto out_status;
  1751. tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto,
  1752. addr, hlen);
  1753. if (unlikely(tp_len < 0)) {
  1754. if (po->tp_loss) {
  1755. __packet_set_status(po, ph,
  1756. TP_STATUS_AVAILABLE);
  1757. packet_increment_head(&po->tx_ring);
  1758. kfree_skb(skb);
  1759. continue;
  1760. } else {
  1761. status = TP_STATUS_WRONG_FORMAT;
  1762. err = tp_len;
  1763. goto out_status;
  1764. }
  1765. }
  1766. skb->destructor = tpacket_destruct_skb;
  1767. __packet_set_status(po, ph, TP_STATUS_SENDING);
  1768. atomic_inc(&po->tx_ring.pending);
  1769. status = TP_STATUS_SEND_REQUEST;
  1770. err = dev_queue_xmit(skb);
  1771. if (unlikely(err > 0)) {
  1772. err = net_xmit_errno(err);
  1773. if (err && __packet_get_status(po, ph) ==
  1774. TP_STATUS_AVAILABLE) {
  1775. /* skb was destructed already */
  1776. skb = NULL;
  1777. goto out_status;
  1778. }
  1779. /*
  1780. * skb was dropped but not destructed yet;
  1781. * let's treat it like congestion or err < 0
  1782. */
  1783. err = 0;
  1784. }
  1785. packet_increment_head(&po->tx_ring);
  1786. len_sum += tp_len;
  1787. } while (likely((ph != NULL) ||
  1788. ((!(msg->msg_flags & MSG_DONTWAIT)) &&
  1789. (atomic_read(&po->tx_ring.pending))))
  1790. );
  1791. err = len_sum;
  1792. goto out_put;
  1793. out_status:
  1794. __packet_set_status(po, ph, status);
  1795. kfree_skb(skb);
  1796. out_put:
  1797. if (need_rls_dev)
  1798. dev_put(dev);
  1799. out:
  1800. mutex_unlock(&po->pg_vec_lock);
  1801. return err;
  1802. }
  1803. static struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
  1804. size_t reserve, size_t len,
  1805. size_t linear, int noblock,
  1806. int *err)
  1807. {
  1808. struct sk_buff *skb;
  1809. /* Under a page? Don't bother with paged skb. */
  1810. if (prepad + len < PAGE_SIZE || !linear)
  1811. linear = len;
  1812. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  1813. err);
  1814. if (!skb)
  1815. return NULL;
  1816. skb_reserve(skb, reserve);
  1817. skb_put(skb, linear);
  1818. skb->data_len = len - linear;
  1819. skb->len += len - linear;
  1820. return skb;
  1821. }
  1822. static int packet_snd(struct socket *sock,
  1823. struct msghdr *msg, size_t len)
  1824. {
  1825. struct sock *sk = sock->sk;
  1826. struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
  1827. struct sk_buff *skb;
  1828. struct net_device *dev;
  1829. __be16 proto;
  1830. bool need_rls_dev = false;
  1831. unsigned char *addr;
  1832. int err, reserve = 0;
  1833. struct virtio_net_hdr vnet_hdr = { 0 };
  1834. int offset = 0;
  1835. int vnet_hdr_len;
  1836. struct packet_sock *po = pkt_sk(sk);
  1837. unsigned short gso_type = 0;
  1838. int hlen, tlen;
  1839. int extra_len = 0;
  1840. /*
  1841. * Get and verify the address.
  1842. */
  1843. if (saddr == NULL) {
  1844. dev = po->prot_hook.dev;
  1845. proto = po->num;
  1846. addr = NULL;
  1847. } else {
  1848. err = -EINVAL;
  1849. if (msg->msg_namelen < sizeof(struct sockaddr_ll))
  1850. goto out;
  1851. if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
  1852. goto out;
  1853. proto = saddr->sll_protocol;
  1854. addr = saddr->sll_addr;
  1855. dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
  1856. need_rls_dev = true;
  1857. }
  1858. err = -ENXIO;
  1859. if (dev == NULL)
  1860. goto out_unlock;
  1861. if (sock->type == SOCK_RAW)
  1862. reserve = dev->hard_header_len;
  1863. err = -ENETDOWN;
  1864. if (!(dev->flags & IFF_UP))
  1865. goto out_unlock;
  1866. if (po->has_vnet_hdr) {
  1867. vnet_hdr_len = sizeof(vnet_hdr);
  1868. err = -EINVAL;
  1869. if (len < vnet_hdr_len)
  1870. goto out_unlock;
  1871. len -= vnet_hdr_len;
  1872. err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov,
  1873. vnet_hdr_len);
  1874. if (err < 0)
  1875. goto out_unlock;
  1876. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  1877. (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
  1878. vnet_hdr.hdr_len))
  1879. vnet_hdr.hdr_len = vnet_hdr.csum_start +
  1880. vnet_hdr.csum_offset + 2;
  1881. err = -EINVAL;
  1882. if (vnet_hdr.hdr_len > len)
  1883. goto out_unlock;
  1884. if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  1885. switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  1886. case VIRTIO_NET_HDR_GSO_TCPV4:
  1887. gso_type = SKB_GSO_TCPV4;
  1888. break;
  1889. case VIRTIO_NET_HDR_GSO_TCPV6:
  1890. gso_type = SKB_GSO_TCPV6;
  1891. break;
  1892. case VIRTIO_NET_HDR_GSO_UDP:
  1893. gso_type = SKB_GSO_UDP;
  1894. break;
  1895. default:
  1896. goto out_unlock;
  1897. }
  1898. if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  1899. gso_type |= SKB_GSO_TCP_ECN;
  1900. if (vnet_hdr.gso_size == 0)
  1901. goto out_unlock;
  1902. }
  1903. }
  1904. if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
  1905. if (!netif_supports_nofcs(dev)) {
  1906. err = -EPROTONOSUPPORT;
  1907. goto out_unlock;
  1908. }
  1909. extra_len = 4; /* We're doing our own CRC */
  1910. }
  1911. err = -EMSGSIZE;
  1912. if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN + extra_len))
  1913. goto out_unlock;
  1914. err = -ENOBUFS;
  1915. hlen = LL_RESERVED_SPACE(dev);
  1916. tlen = dev->needed_tailroom;
  1917. skb = packet_alloc_skb(sk, hlen + tlen, hlen, len, vnet_hdr.hdr_len,
  1918. msg->msg_flags & MSG_DONTWAIT, &err);
  1919. if (skb == NULL)
  1920. goto out_unlock;
  1921. skb_set_network_header(skb, reserve);
  1922. err = -EINVAL;
  1923. if (sock->type == SOCK_DGRAM &&
  1924. (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0)
  1925. goto out_free;
  1926. /* Returns -EFAULT on error */
  1927. err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len);
  1928. if (err)
  1929. goto out_free;
  1930. err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
  1931. if (err < 0)
  1932. goto out_free;
  1933. if (!gso_type && (len > dev->mtu + reserve + extra_len)) {
  1934. /* Earlier code assumed this would be a VLAN pkt,
  1935. * double-check this now that we have the actual
  1936. * packet in hand.
  1937. */
  1938. struct ethhdr *ehdr;
  1939. skb_reset_mac_header(skb);
  1940. ehdr = eth_hdr(skb);
  1941. if (ehdr->h_proto != htons(ETH_P_8021Q)) {
  1942. err = -EMSGSIZE;
  1943. goto out_free;
  1944. }
  1945. }
  1946. skb->protocol = proto;
  1947. skb->dev = dev;
  1948. skb->priority = sk->sk_priority;
  1949. skb->mark = sk->sk_mark;
  1950. if (po->has_vnet_hdr) {
  1951. if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  1952. if (!skb_partial_csum_set(skb, vnet_hdr.csum_start,
  1953. vnet_hdr.csum_offset)) {
  1954. err = -EINVAL;
  1955. goto out_free;
  1956. }
  1957. }
  1958. skb_shinfo(skb)->gso_size = vnet_hdr.gso_size;
  1959. skb_shinfo(skb)->gso_type = gso_type;
  1960. /* Header must be checked, and gso_segs computed. */
  1961. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  1962. skb_shinfo(skb)->gso_segs = 0;
  1963. len += vnet_hdr_len;
  1964. }
  1965. if (unlikely(extra_len == 4))
  1966. skb->no_fcs = 1;
  1967. /*
  1968. * Now send it
  1969. */
  1970. err = dev_queue_xmit(skb);
  1971. if (err > 0 && (err = net_xmit_errno(err)) != 0)
  1972. goto out_unlock;
  1973. if (need_rls_dev)
  1974. dev_put(dev);
  1975. return len;
  1976. out_free:
  1977. kfree_skb(skb);
  1978. out_unlock:
  1979. if (dev && need_rls_dev)
  1980. dev_put(dev);
  1981. out:
  1982. return err;
  1983. }
  1984. static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
  1985. struct msghdr *msg, size_t len)
  1986. {
  1987. struct sock *sk = sock->sk;
  1988. struct packet_sock *po = pkt_sk(sk);
  1989. if (po->tx_ring.pg_vec)
  1990. return tpacket_snd(po, msg);
  1991. else
  1992. return packet_snd(sock, msg, len);
  1993. }
  1994. /*
  1995. * Close a PACKET socket. This is fairly simple. We immediately go
  1996. * to 'closed' state and remove our protocol entry in the device list.
  1997. */
  1998. static int packet_release(struct socket *sock)
  1999. {
  2000. struct sock *sk = sock->sk;
  2001. struct packet_sock *po;
  2002. struct net *net;
  2003. union tpacket_req_u req_u;
  2004. if (!sk)
  2005. return 0;
  2006. net = sock_net(sk);
  2007. po = pkt_sk(sk);
  2008. mutex_lock(&net->packet.sklist_lock);
  2009. sk_del_node_init_rcu(sk);
  2010. mutex_unlock(&net->packet.sklist_lock);
  2011. preempt_disable();
  2012. sock_prot_inuse_add(net, sk->sk_prot, -1);
  2013. preempt_enable();
  2014. spin_lock(&po->bind_lock);
  2015. unregister_prot_hook(sk, false);
  2016. if (po->prot_hook.dev) {
  2017. dev_put(po->prot_hook.dev);
  2018. po->prot_hook.dev = NULL;
  2019. }
  2020. spin_unlock(&po->bind_lock);
  2021. packet_flush_mclist(sk);
  2022. if (po->rx_ring.pg_vec) {
  2023. memset(&req_u, 0, sizeof(req_u));
  2024. packet_set_ring(sk, &req_u, 1, 0);
  2025. }
  2026. if (po->tx_ring.pg_vec) {
  2027. memset(&req_u, 0, sizeof(req_u));
  2028. packet_set_ring(sk, &req_u, 1, 1);
  2029. }
  2030. fanout_release(sk);
  2031. synchronize_net();
  2032. /*
  2033. * Now the socket is dead. No more input will appear.
  2034. */
  2035. sock_orphan(sk);
  2036. sock->sk = NULL;
  2037. /* Purge queues */
  2038. skb_queue_purge(&sk->sk_receive_queue);
  2039. sk_refcnt_debug_release(sk);
  2040. sock_put(sk);
  2041. return 0;
  2042. }
  2043. /*
  2044. * Attach a packet hook.
  2045. */
  2046. static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
  2047. {
  2048. struct packet_sock *po = pkt_sk(sk);
  2049. if (po->fanout) {
  2050. if (dev)
  2051. dev_put(dev);
  2052. return -EINVAL;
  2053. }
  2054. lock_sock(sk);
  2055. spin_lock(&po->bind_lock);
  2056. unregister_prot_hook(sk, true);
  2057. po->num = protocol;
  2058. po->prot_hook.type = protocol;
  2059. if (po->prot_hook.dev)
  2060. dev_put(po->prot_hook.dev);
  2061. po->prot_hook.dev = dev;
  2062. po->ifindex = dev ? dev->ifindex : 0;
  2063. if (protocol == 0)
  2064. goto out_unlock;
  2065. if (!dev || (dev->flags & IFF_UP)) {
  2066. register_prot_hook(sk);
  2067. } else {
  2068. sk->sk_err = ENETDOWN;
  2069. if (!sock_flag(sk, SOCK_DEAD))
  2070. sk->sk_error_report(sk);
  2071. }
  2072. out_unlock:
  2073. spin_unlock(&po->bind_lock);
  2074. release_sock(sk);
  2075. return 0;
  2076. }
  2077. /*
  2078. * Bind a packet socket to a device
  2079. */
  2080. static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
  2081. int addr_len)
  2082. {
  2083. struct sock *sk = sock->sk;
  2084. char name[15];
  2085. struct net_device *dev;
  2086. int err = -ENODEV;
  2087. /*
  2088. * Check legality
  2089. */
  2090. if (addr_len != sizeof(struct sockaddr))
  2091. return -EINVAL;
  2092. strlcpy(name, uaddr->sa_data, sizeof(name));
  2093. dev = dev_get_by_name(sock_net(sk), name);
  2094. if (dev)
  2095. err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
  2096. return err;
  2097. }
  2098. static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  2099. {
  2100. struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
  2101. struct sock *sk = sock->sk;
  2102. struct net_device *dev = NULL;
  2103. int err;
  2104. /*
  2105. * Check legality
  2106. */
  2107. if (addr_len < sizeof(struct sockaddr_ll))
  2108. return -EINVAL;
  2109. if (sll->sll_family != AF_PACKET)
  2110. return -EINVAL;
  2111. if (sll->sll_ifindex) {
  2112. err = -ENODEV;
  2113. dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
  2114. if (dev == NULL)
  2115. goto out;
  2116. }
  2117. err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
  2118. out:
  2119. return err;
  2120. }
  2121. static struct proto packet_proto = {
  2122. .name = "PACKET",
  2123. .owner = THIS_MODULE,
  2124. .obj_size = sizeof(struct packet_sock),
  2125. };
  2126. /*
  2127. * Create a packet of type SOCK_PACKET.
  2128. */
  2129. static int packet_create(struct net *net, struct socket *sock, int protocol,
  2130. int kern)
  2131. {
  2132. struct sock *sk;
  2133. struct packet_sock *po;
  2134. __be16 proto = (__force __be16)protocol; /* weird, but documented */
  2135. int err;
  2136. if (!ns_capable(net->user_ns, CAP_NET_RAW))
  2137. return -EPERM;
  2138. if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
  2139. sock->type != SOCK_PACKET)
  2140. return -ESOCKTNOSUPPORT;
  2141. sock->state = SS_UNCONNECTED;
  2142. err = -ENOBUFS;
  2143. sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto);
  2144. if (sk == NULL)
  2145. goto out;
  2146. sock->ops = &packet_ops;
  2147. if (sock->type == SOCK_PACKET)
  2148. sock->ops = &packet_ops_spkt;
  2149. sock_init_data(sock, sk);
  2150. po = pkt_sk(sk);
  2151. sk->sk_family = PF_PACKET;
  2152. po->num = proto;
  2153. sk->sk_destruct = packet_sock_destruct;
  2154. sk_refcnt_debug_inc(sk);
  2155. /*
  2156. * Attach a protocol block
  2157. */
  2158. spin_lock_init(&po->bind_lock);
  2159. mutex_init(&po->pg_vec_lock);
  2160. po->prot_hook.func = packet_rcv;
  2161. if (sock->type == SOCK_PACKET)
  2162. po->prot_hook.func = packet_rcv_spkt;
  2163. po->prot_hook.af_packet_priv = sk;
  2164. if (proto) {
  2165. po->prot_hook.type = proto;
  2166. register_prot_hook(sk);
  2167. }
  2168. mutex_lock(&net->packet.sklist_lock);
  2169. sk_add_node_rcu(sk, &net->packet.sklist);
  2170. mutex_unlock(&net->packet.sklist_lock);
  2171. preempt_disable();
  2172. sock_prot_inuse_add(net, &packet_proto, 1);
  2173. preempt_enable();
  2174. return 0;
  2175. out:
  2176. return err;
  2177. }
  2178. static int packet_recv_error(struct sock *sk, struct msghdr *msg, int len)
  2179. {
  2180. struct sock_exterr_skb *serr;
  2181. struct sk_buff *skb, *skb2;
  2182. int copied, err;
  2183. err = -EAGAIN;
  2184. skb = skb_dequeue(&sk->sk_error_queue);
  2185. if (skb == NULL)
  2186. goto out;
  2187. copied = skb->len;
  2188. if (copied > len) {
  2189. msg->msg_flags |= MSG_TRUNC;
  2190. copied = len;
  2191. }
  2192. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  2193. if (err)
  2194. goto out_free_skb;
  2195. sock_recv_timestamp(msg, sk, skb);
  2196. serr = SKB_EXT_ERR(skb);
  2197. put_cmsg(msg, SOL_PACKET, PACKET_TX_TIMESTAMP,
  2198. sizeof(serr->ee), &serr->ee);
  2199. msg->msg_flags |= MSG_ERRQUEUE;
  2200. err = copied;
  2201. /* Reset and regenerate socket error */
  2202. spin_lock_bh(&sk->sk_error_queue.lock);
  2203. sk->sk_err = 0;
  2204. if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
  2205. sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
  2206. spin_unlock_bh(&sk->sk_error_queue.lock);
  2207. sk->sk_error_report(sk);
  2208. } else
  2209. spin_unlock_bh(&sk->sk_error_queue.lock);
  2210. out_free_skb:
  2211. kfree_skb(skb);
  2212. out:
  2213. return err;
  2214. }
  2215. /*
  2216. * Pull a packet from our receive queue and hand it to the user.
  2217. * If necessary we block.
  2218. */
  2219. static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
  2220. struct msghdr *msg, size_t len, int flags)
  2221. {
  2222. struct sock *sk = sock->sk;
  2223. struct sk_buff *skb;
  2224. int copied, err;
  2225. struct sockaddr_ll *sll;
  2226. int vnet_hdr_len = 0;
  2227. err = -EINVAL;
  2228. if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
  2229. goto out;
  2230. #if 0
  2231. /* What error should we return now? EUNATTACH? */
  2232. if (pkt_sk(sk)->ifindex < 0)
  2233. return -ENODEV;
  2234. #endif
  2235. if (flags & MSG_ERRQUEUE) {
  2236. err = packet_recv_error(sk, msg, len);
  2237. goto out;
  2238. }
  2239. /*
  2240. * Call the generic datagram receiver. This handles all sorts
  2241. * of horrible races and re-entrancy so we can forget about it
  2242. * in the protocol layers.
  2243. *
  2244. * Now it will return ENETDOWN, if device have just gone down,
  2245. * but then it will block.
  2246. */
  2247. skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
  2248. /*
  2249. * An error occurred so return it. Because skb_recv_datagram()
  2250. * handles the blocking we don't see and worry about blocking
  2251. * retries.
  2252. */
  2253. if (skb == NULL)
  2254. goto out;
  2255. if (pkt_sk(sk)->has_vnet_hdr) {
  2256. struct virtio_net_hdr vnet_hdr = { 0 };
  2257. err = -EINVAL;
  2258. vnet_hdr_len = sizeof(vnet_hdr);
  2259. if (len < vnet_hdr_len)
  2260. goto out_free;
  2261. len -= vnet_hdr_len;
  2262. if (skb_is_gso(skb)) {
  2263. struct skb_shared_info *sinfo = skb_shinfo(skb);
  2264. /* This is a hint as to how much should be linear. */
  2265. vnet_hdr.hdr_len = skb_headlen(skb);
  2266. vnet_hdr.gso_size = sinfo->gso_size;
  2267. if (sinfo->gso_type & SKB_GSO_TCPV4)
  2268. vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  2269. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  2270. vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  2271. else if (sinfo->gso_type & SKB_GSO_UDP)
  2272. vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  2273. else if (sinfo->gso_type & SKB_GSO_FCOE)
  2274. goto out_free;
  2275. else
  2276. BUG();
  2277. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  2278. vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  2279. } else
  2280. vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  2281. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2282. vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  2283. vnet_hdr.csum_start = skb_checksum_start_offset(skb);
  2284. vnet_hdr.csum_offset = skb->csum_offset;
  2285. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  2286. vnet_hdr.flags = VIRTIO_NET_HDR_F_DATA_VALID;
  2287. } /* else everything is zero */
  2288. err = memcpy_toiovec(msg->msg_iov, (void *)&vnet_hdr,
  2289. vnet_hdr_len);
  2290. if (err < 0)
  2291. goto out_free;
  2292. }
  2293. /*
  2294. * If the address length field is there to be filled in, we fill
  2295. * it in now.
  2296. */
  2297. sll = &PACKET_SKB_CB(skb)->sa.ll;
  2298. if (sock->type == SOCK_PACKET)
  2299. msg->msg_namelen = sizeof(struct sockaddr_pkt);
  2300. else
  2301. msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
  2302. /*
  2303. * You lose any data beyond the buffer you gave. If it worries a
  2304. * user program they can ask the device for its MTU anyway.
  2305. */
  2306. copied = skb->len;
  2307. if (copied > len) {
  2308. copied = len;
  2309. msg->msg_flags |= MSG_TRUNC;
  2310. }
  2311. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  2312. if (err)
  2313. goto out_free;
  2314. sock_recv_ts_and_drops(msg, sk, skb);
  2315. if (msg->msg_name)
  2316. memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
  2317. msg->msg_namelen);
  2318. if (pkt_sk(sk)->auxdata) {
  2319. struct tpacket_auxdata aux;
  2320. aux.tp_status = TP_STATUS_USER;
  2321. if (skb->ip_summed == CHECKSUM_PARTIAL)
  2322. aux.tp_status |= TP_STATUS_CSUMNOTREADY;
  2323. aux.tp_len = PACKET_SKB_CB(skb)->origlen;
  2324. aux.tp_snaplen = skb->len;
  2325. aux.tp_mac = 0;
  2326. aux.tp_net = skb_network_offset(skb);
  2327. if (vlan_tx_tag_present(skb)) {
  2328. aux.tp_vlan_tci = vlan_tx_tag_get(skb);
  2329. aux.tp_status |= TP_STATUS_VLAN_VALID;
  2330. } else {
  2331. aux.tp_vlan_tci = 0;
  2332. }
  2333. aux.tp_padding = 0;
  2334. put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
  2335. }
  2336. /*
  2337. * Free or return the buffer as appropriate. Again this
  2338. * hides all the races and re-entrancy issues from us.
  2339. */
  2340. err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
  2341. out_free:
  2342. skb_free_datagram(sk, skb);
  2343. out:
  2344. return err;
  2345. }
  2346. static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
  2347. int *uaddr_len, int peer)
  2348. {
  2349. struct net_device *dev;
  2350. struct sock *sk = sock->sk;
  2351. if (peer)
  2352. return -EOPNOTSUPP;
  2353. uaddr->sa_family = AF_PACKET;
  2354. rcu_read_lock();
  2355. dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
  2356. if (dev)
  2357. strncpy(uaddr->sa_data, dev->name, 14);
  2358. else
  2359. memset(uaddr->sa_data, 0, 14);
  2360. rcu_read_unlock();
  2361. *uaddr_len = sizeof(*uaddr);
  2362. return 0;
  2363. }
  2364. static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
  2365. int *uaddr_len, int peer)
  2366. {
  2367. struct net_device *dev;
  2368. struct sock *sk = sock->sk;
  2369. struct packet_sock *po = pkt_sk(sk);
  2370. DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
  2371. if (peer)
  2372. return -EOPNOTSUPP;
  2373. sll->sll_family = AF_PACKET;
  2374. sll->sll_ifindex = po->ifindex;
  2375. sll->sll_protocol = po->num;
  2376. sll->sll_pkttype = 0;
  2377. rcu_read_lock();
  2378. dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
  2379. if (dev) {
  2380. sll->sll_hatype = dev->type;
  2381. sll->sll_halen = dev->addr_len;
  2382. memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
  2383. } else {
  2384. sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
  2385. sll->sll_halen = 0;
  2386. }
  2387. rcu_read_unlock();
  2388. *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
  2389. return 0;
  2390. }
  2391. static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
  2392. int what)
  2393. {
  2394. switch (i->type) {
  2395. case PACKET_MR_MULTICAST:
  2396. if (i->alen != dev->addr_len)
  2397. return -EINVAL;
  2398. if (what > 0)
  2399. return dev_mc_add(dev, i->addr);
  2400. else
  2401. return dev_mc_del(dev, i->addr);
  2402. break;
  2403. case PACKET_MR_PROMISC:
  2404. return dev_set_promiscuity(dev, what);
  2405. break;
  2406. case PACKET_MR_ALLMULTI:
  2407. return dev_set_allmulti(dev, what);
  2408. break;
  2409. case PACKET_MR_UNICAST:
  2410. if (i->alen != dev->addr_len)
  2411. return -EINVAL;
  2412. if (what > 0)
  2413. return dev_uc_add(dev, i->addr);
  2414. else
  2415. return dev_uc_del(dev, i->addr);
  2416. break;
  2417. default:
  2418. break;
  2419. }
  2420. return 0;
  2421. }
  2422. static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
  2423. {
  2424. for ( ; i; i = i->next) {
  2425. if (i->ifindex == dev->ifindex)
  2426. packet_dev_mc(dev, i, what);
  2427. }
  2428. }
  2429. static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
  2430. {
  2431. struct packet_sock *po = pkt_sk(sk);
  2432. struct packet_mclist *ml, *i;
  2433. struct net_device *dev;
  2434. int err;
  2435. rtnl_lock();
  2436. err = -ENODEV;
  2437. dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
  2438. if (!dev)
  2439. goto done;
  2440. err = -EINVAL;
  2441. if (mreq->mr_alen > dev->addr_len)
  2442. goto done;
  2443. err = -ENOBUFS;
  2444. i = kmalloc(sizeof(*i), GFP_KERNEL);
  2445. if (i == NULL)
  2446. goto done;
  2447. err = 0;
  2448. for (ml = po->mclist; ml; ml = 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. ml->count++;
  2454. /* Free the new element ... */
  2455. kfree(i);
  2456. goto done;
  2457. }
  2458. }
  2459. i->type = mreq->mr_type;
  2460. i->ifindex = mreq->mr_ifindex;
  2461. i->alen = mreq->mr_alen;
  2462. memcpy(i->addr, mreq->mr_address, i->alen);
  2463. i->count = 1;
  2464. i->next = po->mclist;
  2465. po->mclist = i;
  2466. err = packet_dev_mc(dev, i, 1);
  2467. if (err) {
  2468. po->mclist = i->next;
  2469. kfree(i);
  2470. }
  2471. done:
  2472. rtnl_unlock();
  2473. return err;
  2474. }
  2475. static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
  2476. {
  2477. struct packet_mclist *ml, **mlp;
  2478. rtnl_lock();
  2479. for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
  2480. if (ml->ifindex == mreq->mr_ifindex &&
  2481. ml->type == mreq->mr_type &&
  2482. ml->alen == mreq->mr_alen &&
  2483. memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
  2484. if (--ml->count == 0) {
  2485. struct net_device *dev;
  2486. *mlp = ml->next;
  2487. dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
  2488. if (dev)
  2489. packet_dev_mc(dev, ml, -1);
  2490. kfree(ml);
  2491. }
  2492. rtnl_unlock();
  2493. return 0;
  2494. }
  2495. }
  2496. rtnl_unlock();
  2497. return -EADDRNOTAVAIL;
  2498. }
  2499. static void packet_flush_mclist(struct sock *sk)
  2500. {
  2501. struct packet_sock *po = pkt_sk(sk);
  2502. struct packet_mclist *ml;
  2503. if (!po->mclist)
  2504. return;
  2505. rtnl_lock();
  2506. while ((ml = po->mclist) != NULL) {
  2507. struct net_device *dev;
  2508. po->mclist = ml->next;
  2509. dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
  2510. if (dev != NULL)
  2511. packet_dev_mc(dev, ml, -1);
  2512. kfree(ml);
  2513. }
  2514. rtnl_unlock();
  2515. }
  2516. static int
  2517. packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
  2518. {
  2519. struct sock *sk = sock->sk;
  2520. struct packet_sock *po = pkt_sk(sk);
  2521. int ret;
  2522. if (level != SOL_PACKET)
  2523. return -ENOPROTOOPT;
  2524. switch (optname) {
  2525. case PACKET_ADD_MEMBERSHIP:
  2526. case PACKET_DROP_MEMBERSHIP:
  2527. {
  2528. struct packet_mreq_max mreq;
  2529. int len = optlen;
  2530. memset(&mreq, 0, sizeof(mreq));
  2531. if (len < sizeof(struct packet_mreq))
  2532. return -EINVAL;
  2533. if (len > sizeof(mreq))
  2534. len = sizeof(mreq);
  2535. if (copy_from_user(&mreq, optval, len))
  2536. return -EFAULT;
  2537. if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
  2538. return -EINVAL;
  2539. if (optname == PACKET_ADD_MEMBERSHIP)
  2540. ret = packet_mc_add(sk, &mreq);
  2541. else
  2542. ret = packet_mc_drop(sk, &mreq);
  2543. return ret;
  2544. }
  2545. case PACKET_RX_RING:
  2546. case PACKET_TX_RING:
  2547. {
  2548. union tpacket_req_u req_u;
  2549. int len;
  2550. switch (po->tp_version) {
  2551. case TPACKET_V1:
  2552. case TPACKET_V2:
  2553. len = sizeof(req_u.req);
  2554. break;
  2555. case TPACKET_V3:
  2556. default:
  2557. len = sizeof(req_u.req3);
  2558. break;
  2559. }
  2560. if (optlen < len)
  2561. return -EINVAL;
  2562. if (pkt_sk(sk)->has_vnet_hdr)
  2563. return -EINVAL;
  2564. if (copy_from_user(&req_u.req, optval, len))
  2565. return -EFAULT;
  2566. return packet_set_ring(sk, &req_u, 0,
  2567. optname == PACKET_TX_RING);
  2568. }
  2569. case PACKET_COPY_THRESH:
  2570. {
  2571. int val;
  2572. if (optlen != sizeof(val))
  2573. return -EINVAL;
  2574. if (copy_from_user(&val, optval, sizeof(val)))
  2575. return -EFAULT;
  2576. pkt_sk(sk)->copy_thresh = val;
  2577. return 0;
  2578. }
  2579. case PACKET_VERSION:
  2580. {
  2581. 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. switch (val) {
  2589. case TPACKET_V1:
  2590. case TPACKET_V2:
  2591. case TPACKET_V3:
  2592. po->tp_version = val;
  2593. return 0;
  2594. default:
  2595. return -EINVAL;
  2596. }
  2597. }
  2598. case PACKET_RESERVE:
  2599. {
  2600. unsigned int val;
  2601. if (optlen != sizeof(val))
  2602. return -EINVAL;
  2603. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  2604. return -EBUSY;
  2605. if (copy_from_user(&val, optval, sizeof(val)))
  2606. return -EFAULT;
  2607. po->tp_reserve = val;
  2608. return 0;
  2609. }
  2610. case PACKET_LOSS:
  2611. {
  2612. unsigned int val;
  2613. if (optlen != sizeof(val))
  2614. return -EINVAL;
  2615. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  2616. return -EBUSY;
  2617. if (copy_from_user(&val, optval, sizeof(val)))
  2618. return -EFAULT;
  2619. po->tp_loss = !!val;
  2620. return 0;
  2621. }
  2622. case PACKET_AUXDATA:
  2623. {
  2624. int val;
  2625. if (optlen < sizeof(val))
  2626. return -EINVAL;
  2627. if (copy_from_user(&val, optval, sizeof(val)))
  2628. return -EFAULT;
  2629. po->auxdata = !!val;
  2630. return 0;
  2631. }
  2632. case PACKET_ORIGDEV:
  2633. {
  2634. int val;
  2635. if (optlen < sizeof(val))
  2636. return -EINVAL;
  2637. if (copy_from_user(&val, optval, sizeof(val)))
  2638. return -EFAULT;
  2639. po->origdev = !!val;
  2640. return 0;
  2641. }
  2642. case PACKET_VNET_HDR:
  2643. {
  2644. int val;
  2645. if (sock->type != SOCK_RAW)
  2646. return -EINVAL;
  2647. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  2648. return -EBUSY;
  2649. if (optlen < sizeof(val))
  2650. return -EINVAL;
  2651. if (copy_from_user(&val, optval, sizeof(val)))
  2652. return -EFAULT;
  2653. po->has_vnet_hdr = !!val;
  2654. return 0;
  2655. }
  2656. case PACKET_TIMESTAMP:
  2657. {
  2658. int val;
  2659. if (optlen != sizeof(val))
  2660. return -EINVAL;
  2661. if (copy_from_user(&val, optval, sizeof(val)))
  2662. return -EFAULT;
  2663. po->tp_tstamp = val;
  2664. return 0;
  2665. }
  2666. case PACKET_FANOUT:
  2667. {
  2668. int val;
  2669. if (optlen != sizeof(val))
  2670. return -EINVAL;
  2671. if (copy_from_user(&val, optval, sizeof(val)))
  2672. return -EFAULT;
  2673. return fanout_add(sk, val & 0xffff, val >> 16);
  2674. }
  2675. case PACKET_TX_HAS_OFF:
  2676. {
  2677. unsigned int val;
  2678. if (optlen != sizeof(val))
  2679. return -EINVAL;
  2680. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  2681. return -EBUSY;
  2682. if (copy_from_user(&val, optval, sizeof(val)))
  2683. return -EFAULT;
  2684. po->tp_tx_has_off = !!val;
  2685. return 0;
  2686. }
  2687. default:
  2688. return -ENOPROTOOPT;
  2689. }
  2690. }
  2691. static int packet_getsockopt(struct socket *sock, int level, int optname,
  2692. char __user *optval, int __user *optlen)
  2693. {
  2694. int len;
  2695. int val, lv = sizeof(val);
  2696. struct sock *sk = sock->sk;
  2697. struct packet_sock *po = pkt_sk(sk);
  2698. void *data = &val;
  2699. struct tpacket_stats st;
  2700. union tpacket_stats_u st_u;
  2701. if (level != SOL_PACKET)
  2702. return -ENOPROTOOPT;
  2703. if (get_user(len, optlen))
  2704. return -EFAULT;
  2705. if (len < 0)
  2706. return -EINVAL;
  2707. switch (optname) {
  2708. case PACKET_STATISTICS:
  2709. spin_lock_bh(&sk->sk_receive_queue.lock);
  2710. if (po->tp_version == TPACKET_V3) {
  2711. lv = sizeof(struct tpacket_stats_v3);
  2712. memcpy(&st_u.stats3, &po->stats,
  2713. sizeof(struct tpacket_stats));
  2714. st_u.stats3.tp_freeze_q_cnt =
  2715. po->stats_u.stats3.tp_freeze_q_cnt;
  2716. st_u.stats3.tp_packets += po->stats.tp_drops;
  2717. data = &st_u.stats3;
  2718. } else {
  2719. lv = sizeof(struct tpacket_stats);
  2720. st = po->stats;
  2721. st.tp_packets += st.tp_drops;
  2722. data = &st;
  2723. }
  2724. memset(&po->stats, 0, sizeof(st));
  2725. spin_unlock_bh(&sk->sk_receive_queue.lock);
  2726. break;
  2727. case PACKET_AUXDATA:
  2728. val = po->auxdata;
  2729. break;
  2730. case PACKET_ORIGDEV:
  2731. val = po->origdev;
  2732. break;
  2733. case PACKET_VNET_HDR:
  2734. val = po->has_vnet_hdr;
  2735. break;
  2736. case PACKET_VERSION:
  2737. val = po->tp_version;
  2738. break;
  2739. case PACKET_HDRLEN:
  2740. if (len > sizeof(int))
  2741. len = sizeof(int);
  2742. if (copy_from_user(&val, optval, len))
  2743. return -EFAULT;
  2744. switch (val) {
  2745. case TPACKET_V1:
  2746. val = sizeof(struct tpacket_hdr);
  2747. break;
  2748. case TPACKET_V2:
  2749. val = sizeof(struct tpacket2_hdr);
  2750. break;
  2751. case TPACKET_V3:
  2752. val = sizeof(struct tpacket3_hdr);
  2753. break;
  2754. default:
  2755. return -EINVAL;
  2756. }
  2757. break;
  2758. case PACKET_RESERVE:
  2759. val = po->tp_reserve;
  2760. break;
  2761. case PACKET_LOSS:
  2762. val = po->tp_loss;
  2763. break;
  2764. case PACKET_TIMESTAMP:
  2765. val = po->tp_tstamp;
  2766. break;
  2767. case PACKET_FANOUT:
  2768. val = (po->fanout ?
  2769. ((u32)po->fanout->id |
  2770. ((u32)po->fanout->type << 16)) :
  2771. 0);
  2772. break;
  2773. case PACKET_TX_HAS_OFF:
  2774. val = po->tp_tx_has_off;
  2775. break;
  2776. default:
  2777. return -ENOPROTOOPT;
  2778. }
  2779. if (len > lv)
  2780. len = lv;
  2781. if (put_user(len, optlen))
  2782. return -EFAULT;
  2783. if (copy_to_user(optval, data, len))
  2784. return -EFAULT;
  2785. return 0;
  2786. }
  2787. static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
  2788. {
  2789. struct sock *sk;
  2790. struct net_device *dev = data;
  2791. struct net *net = dev_net(dev);
  2792. rcu_read_lock();
  2793. sk_for_each_rcu(sk, &net->packet.sklist) {
  2794. struct packet_sock *po = pkt_sk(sk);
  2795. switch (msg) {
  2796. case NETDEV_UNREGISTER:
  2797. if (po->mclist)
  2798. packet_dev_mclist(dev, po->mclist, -1);
  2799. /* fallthrough */
  2800. case NETDEV_DOWN:
  2801. if (dev->ifindex == po->ifindex) {
  2802. spin_lock(&po->bind_lock);
  2803. if (po->running) {
  2804. __unregister_prot_hook(sk, false);
  2805. sk->sk_err = ENETDOWN;
  2806. if (!sock_flag(sk, SOCK_DEAD))
  2807. sk->sk_error_report(sk);
  2808. }
  2809. if (msg == NETDEV_UNREGISTER) {
  2810. po->ifindex = -1;
  2811. if (po->prot_hook.dev)
  2812. dev_put(po->prot_hook.dev);
  2813. po->prot_hook.dev = NULL;
  2814. }
  2815. spin_unlock(&po->bind_lock);
  2816. }
  2817. break;
  2818. case NETDEV_UP:
  2819. if (dev->ifindex == po->ifindex) {
  2820. spin_lock(&po->bind_lock);
  2821. if (po->num)
  2822. register_prot_hook(sk);
  2823. spin_unlock(&po->bind_lock);
  2824. }
  2825. break;
  2826. }
  2827. }
  2828. rcu_read_unlock();
  2829. return NOTIFY_DONE;
  2830. }
  2831. static int packet_ioctl(struct socket *sock, unsigned int cmd,
  2832. unsigned long arg)
  2833. {
  2834. struct sock *sk = sock->sk;
  2835. switch (cmd) {
  2836. case SIOCOUTQ:
  2837. {
  2838. int amount = sk_wmem_alloc_get(sk);
  2839. return put_user(amount, (int __user *)arg);
  2840. }
  2841. case SIOCINQ:
  2842. {
  2843. struct sk_buff *skb;
  2844. int amount = 0;
  2845. spin_lock_bh(&sk->sk_receive_queue.lock);
  2846. skb = skb_peek(&sk->sk_receive_queue);
  2847. if (skb)
  2848. amount = skb->len;
  2849. spin_unlock_bh(&sk->sk_receive_queue.lock);
  2850. return put_user(amount, (int __user *)arg);
  2851. }
  2852. case SIOCGSTAMP:
  2853. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  2854. case SIOCGSTAMPNS:
  2855. return sock_get_timestampns(sk, (struct timespec __user *)arg);
  2856. #ifdef CONFIG_INET
  2857. case SIOCADDRT:
  2858. case SIOCDELRT:
  2859. case SIOCDARP:
  2860. case SIOCGARP:
  2861. case SIOCSARP:
  2862. case SIOCGIFADDR:
  2863. case SIOCSIFADDR:
  2864. case SIOCGIFBRDADDR:
  2865. case SIOCSIFBRDADDR:
  2866. case SIOCGIFNETMASK:
  2867. case SIOCSIFNETMASK:
  2868. case SIOCGIFDSTADDR:
  2869. case SIOCSIFDSTADDR:
  2870. case SIOCSIFFLAGS:
  2871. return inet_dgram_ops.ioctl(sock, cmd, arg);
  2872. #endif
  2873. default:
  2874. return -ENOIOCTLCMD;
  2875. }
  2876. return 0;
  2877. }
  2878. static unsigned int packet_poll(struct file *file, struct socket *sock,
  2879. poll_table *wait)
  2880. {
  2881. struct sock *sk = sock->sk;
  2882. struct packet_sock *po = pkt_sk(sk);
  2883. unsigned int mask = datagram_poll(file, sock, wait);
  2884. spin_lock_bh(&sk->sk_receive_queue.lock);
  2885. if (po->rx_ring.pg_vec) {
  2886. if (!packet_previous_rx_frame(po, &po->rx_ring,
  2887. TP_STATUS_KERNEL))
  2888. mask |= POLLIN | POLLRDNORM;
  2889. }
  2890. spin_unlock_bh(&sk->sk_receive_queue.lock);
  2891. spin_lock_bh(&sk->sk_write_queue.lock);
  2892. if (po->tx_ring.pg_vec) {
  2893. if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
  2894. mask |= POLLOUT | POLLWRNORM;
  2895. }
  2896. spin_unlock_bh(&sk->sk_write_queue.lock);
  2897. return mask;
  2898. }
  2899. /* Dirty? Well, I still did not learn better way to account
  2900. * for user mmaps.
  2901. */
  2902. static void packet_mm_open(struct vm_area_struct *vma)
  2903. {
  2904. struct file *file = vma->vm_file;
  2905. struct socket *sock = file->private_data;
  2906. struct sock *sk = sock->sk;
  2907. if (sk)
  2908. atomic_inc(&pkt_sk(sk)->mapped);
  2909. }
  2910. static void packet_mm_close(struct vm_area_struct *vma)
  2911. {
  2912. struct file *file = vma->vm_file;
  2913. struct socket *sock = file->private_data;
  2914. struct sock *sk = sock->sk;
  2915. if (sk)
  2916. atomic_dec(&pkt_sk(sk)->mapped);
  2917. }
  2918. static const struct vm_operations_struct packet_mmap_ops = {
  2919. .open = packet_mm_open,
  2920. .close = packet_mm_close,
  2921. };
  2922. static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
  2923. unsigned int len)
  2924. {
  2925. int i;
  2926. for (i = 0; i < len; i++) {
  2927. if (likely(pg_vec[i].buffer)) {
  2928. if (is_vmalloc_addr(pg_vec[i].buffer))
  2929. vfree(pg_vec[i].buffer);
  2930. else
  2931. free_pages((unsigned long)pg_vec[i].buffer,
  2932. order);
  2933. pg_vec[i].buffer = NULL;
  2934. }
  2935. }
  2936. kfree(pg_vec);
  2937. }
  2938. static char *alloc_one_pg_vec_page(unsigned long order)
  2939. {
  2940. char *buffer = NULL;
  2941. gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
  2942. __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
  2943. buffer = (char *) __get_free_pages(gfp_flags, order);
  2944. if (buffer)
  2945. return buffer;
  2946. /*
  2947. * __get_free_pages failed, fall back to vmalloc
  2948. */
  2949. buffer = vzalloc((1 << order) * PAGE_SIZE);
  2950. if (buffer)
  2951. return buffer;
  2952. /*
  2953. * vmalloc failed, lets dig into swap here
  2954. */
  2955. gfp_flags &= ~__GFP_NORETRY;
  2956. buffer = (char *)__get_free_pages(gfp_flags, order);
  2957. if (buffer)
  2958. return buffer;
  2959. /*
  2960. * complete and utter failure
  2961. */
  2962. return NULL;
  2963. }
  2964. static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
  2965. {
  2966. unsigned int block_nr = req->tp_block_nr;
  2967. struct pgv *pg_vec;
  2968. int i;
  2969. pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
  2970. if (unlikely(!pg_vec))
  2971. goto out;
  2972. for (i = 0; i < block_nr; i++) {
  2973. pg_vec[i].buffer = alloc_one_pg_vec_page(order);
  2974. if (unlikely(!pg_vec[i].buffer))
  2975. goto out_free_pgvec;
  2976. }
  2977. out:
  2978. return pg_vec;
  2979. out_free_pgvec:
  2980. free_pg_vec(pg_vec, order, block_nr);
  2981. pg_vec = NULL;
  2982. goto out;
  2983. }
  2984. static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
  2985. int closing, int tx_ring)
  2986. {
  2987. struct pgv *pg_vec = NULL;
  2988. struct packet_sock *po = pkt_sk(sk);
  2989. int was_running, order = 0;
  2990. struct packet_ring_buffer *rb;
  2991. struct sk_buff_head *rb_queue;
  2992. __be16 num;
  2993. int err = -EINVAL;
  2994. /* Added to avoid minimal code churn */
  2995. struct tpacket_req *req = &req_u->req;
  2996. /* Opening a Tx-ring is NOT supported in TPACKET_V3 */
  2997. if (!closing && tx_ring && (po->tp_version > TPACKET_V2)) {
  2998. WARN(1, "Tx-ring is not supported.\n");
  2999. goto out;
  3000. }
  3001. rb = tx_ring ? &po->tx_ring : &po->rx_ring;
  3002. rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
  3003. err = -EBUSY;
  3004. if (!closing) {
  3005. if (atomic_read(&po->mapped))
  3006. goto out;
  3007. if (atomic_read(&rb->pending))
  3008. goto out;
  3009. }
  3010. if (req->tp_block_nr) {
  3011. /* Sanity tests and some calculations */
  3012. err = -EBUSY;
  3013. if (unlikely(rb->pg_vec))
  3014. goto out;
  3015. switch (po->tp_version) {
  3016. case TPACKET_V1:
  3017. po->tp_hdrlen = TPACKET_HDRLEN;
  3018. break;
  3019. case TPACKET_V2:
  3020. po->tp_hdrlen = TPACKET2_HDRLEN;
  3021. break;
  3022. case TPACKET_V3:
  3023. po->tp_hdrlen = TPACKET3_HDRLEN;
  3024. break;
  3025. }
  3026. err = -EINVAL;
  3027. if (unlikely((int)req->tp_block_size <= 0))
  3028. goto out;
  3029. if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
  3030. goto out;
  3031. if (unlikely(req->tp_frame_size < po->tp_hdrlen +
  3032. po->tp_reserve))
  3033. goto out;
  3034. if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
  3035. goto out;
  3036. rb->frames_per_block = req->tp_block_size/req->tp_frame_size;
  3037. if (unlikely(rb->frames_per_block <= 0))
  3038. goto out;
  3039. if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
  3040. req->tp_frame_nr))
  3041. goto out;
  3042. err = -ENOMEM;
  3043. order = get_order(req->tp_block_size);
  3044. pg_vec = alloc_pg_vec(req, order);
  3045. if (unlikely(!pg_vec))
  3046. goto out;
  3047. switch (po->tp_version) {
  3048. case TPACKET_V3:
  3049. /* Transmit path is not supported. We checked
  3050. * it above but just being paranoid
  3051. */
  3052. if (!tx_ring)
  3053. init_prb_bdqc(po, rb, pg_vec, req_u, tx_ring);
  3054. break;
  3055. default:
  3056. break;
  3057. }
  3058. }
  3059. /* Done */
  3060. else {
  3061. err = -EINVAL;
  3062. if (unlikely(req->tp_frame_nr))
  3063. goto out;
  3064. }
  3065. lock_sock(sk);
  3066. /* Detach socket from network */
  3067. spin_lock(&po->bind_lock);
  3068. was_running = po->running;
  3069. num = po->num;
  3070. if (was_running) {
  3071. po->num = 0;
  3072. __unregister_prot_hook(sk, false);
  3073. }
  3074. spin_unlock(&po->bind_lock);
  3075. synchronize_net();
  3076. err = -EBUSY;
  3077. mutex_lock(&po->pg_vec_lock);
  3078. if (closing || atomic_read(&po->mapped) == 0) {
  3079. err = 0;
  3080. spin_lock_bh(&rb_queue->lock);
  3081. swap(rb->pg_vec, pg_vec);
  3082. rb->frame_max = (req->tp_frame_nr - 1);
  3083. rb->head = 0;
  3084. rb->frame_size = req->tp_frame_size;
  3085. spin_unlock_bh(&rb_queue->lock);
  3086. swap(rb->pg_vec_order, order);
  3087. swap(rb->pg_vec_len, req->tp_block_nr);
  3088. rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
  3089. po->prot_hook.func = (po->rx_ring.pg_vec) ?
  3090. tpacket_rcv : packet_rcv;
  3091. skb_queue_purge(rb_queue);
  3092. if (atomic_read(&po->mapped))
  3093. pr_err("packet_mmap: vma is busy: %d\n",
  3094. atomic_read(&po->mapped));
  3095. }
  3096. mutex_unlock(&po->pg_vec_lock);
  3097. spin_lock(&po->bind_lock);
  3098. if (was_running) {
  3099. po->num = num;
  3100. register_prot_hook(sk);
  3101. }
  3102. spin_unlock(&po->bind_lock);
  3103. if (closing && (po->tp_version > TPACKET_V2)) {
  3104. /* Because we don't support block-based V3 on tx-ring */
  3105. if (!tx_ring)
  3106. prb_shutdown_retire_blk_timer(po, tx_ring, rb_queue);
  3107. }
  3108. release_sock(sk);
  3109. if (pg_vec)
  3110. free_pg_vec(pg_vec, order, req->tp_block_nr);
  3111. out:
  3112. return err;
  3113. }
  3114. static int packet_mmap(struct file *file, struct socket *sock,
  3115. struct vm_area_struct *vma)
  3116. {
  3117. struct sock *sk = sock->sk;
  3118. struct packet_sock *po = pkt_sk(sk);
  3119. unsigned long size, expected_size;
  3120. struct packet_ring_buffer *rb;
  3121. unsigned long start;
  3122. int err = -EINVAL;
  3123. int i;
  3124. if (vma->vm_pgoff)
  3125. return -EINVAL;
  3126. mutex_lock(&po->pg_vec_lock);
  3127. expected_size = 0;
  3128. for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
  3129. if (rb->pg_vec) {
  3130. expected_size += rb->pg_vec_len
  3131. * rb->pg_vec_pages
  3132. * PAGE_SIZE;
  3133. }
  3134. }
  3135. if (expected_size == 0)
  3136. goto out;
  3137. size = vma->vm_end - vma->vm_start;
  3138. if (size != expected_size)
  3139. goto out;
  3140. start = vma->vm_start;
  3141. for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
  3142. if (rb->pg_vec == NULL)
  3143. continue;
  3144. for (i = 0; i < rb->pg_vec_len; i++) {
  3145. struct page *page;
  3146. void *kaddr = rb->pg_vec[i].buffer;
  3147. int pg_num;
  3148. for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
  3149. page = pgv_to_page(kaddr);
  3150. err = vm_insert_page(vma, start, page);
  3151. if (unlikely(err))
  3152. goto out;
  3153. start += PAGE_SIZE;
  3154. kaddr += PAGE_SIZE;
  3155. }
  3156. }
  3157. }
  3158. atomic_inc(&po->mapped);
  3159. vma->vm_ops = &packet_mmap_ops;
  3160. err = 0;
  3161. out:
  3162. mutex_unlock(&po->pg_vec_lock);
  3163. return err;
  3164. }
  3165. static const struct proto_ops packet_ops_spkt = {
  3166. .family = PF_PACKET,
  3167. .owner = THIS_MODULE,
  3168. .release = packet_release,
  3169. .bind = packet_bind_spkt,
  3170. .connect = sock_no_connect,
  3171. .socketpair = sock_no_socketpair,
  3172. .accept = sock_no_accept,
  3173. .getname = packet_getname_spkt,
  3174. .poll = datagram_poll,
  3175. .ioctl = packet_ioctl,
  3176. .listen = sock_no_listen,
  3177. .shutdown = sock_no_shutdown,
  3178. .setsockopt = sock_no_setsockopt,
  3179. .getsockopt = sock_no_getsockopt,
  3180. .sendmsg = packet_sendmsg_spkt,
  3181. .recvmsg = packet_recvmsg,
  3182. .mmap = sock_no_mmap,
  3183. .sendpage = sock_no_sendpage,
  3184. };
  3185. static const struct proto_ops packet_ops = {
  3186. .family = PF_PACKET,
  3187. .owner = THIS_MODULE,
  3188. .release = packet_release,
  3189. .bind = packet_bind,
  3190. .connect = sock_no_connect,
  3191. .socketpair = sock_no_socketpair,
  3192. .accept = sock_no_accept,
  3193. .getname = packet_getname,
  3194. .poll = packet_poll,
  3195. .ioctl = packet_ioctl,
  3196. .listen = sock_no_listen,
  3197. .shutdown = sock_no_shutdown,
  3198. .setsockopt = packet_setsockopt,
  3199. .getsockopt = packet_getsockopt,
  3200. .sendmsg = packet_sendmsg,
  3201. .recvmsg = packet_recvmsg,
  3202. .mmap = packet_mmap,
  3203. .sendpage = sock_no_sendpage,
  3204. };
  3205. static const struct net_proto_family packet_family_ops = {
  3206. .family = PF_PACKET,
  3207. .create = packet_create,
  3208. .owner = THIS_MODULE,
  3209. };
  3210. static struct notifier_block packet_netdev_notifier = {
  3211. .notifier_call = packet_notifier,
  3212. };
  3213. #ifdef CONFIG_PROC_FS
  3214. static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
  3215. __acquires(RCU)
  3216. {
  3217. struct net *net = seq_file_net(seq);
  3218. rcu_read_lock();
  3219. return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
  3220. }
  3221. static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  3222. {
  3223. struct net *net = seq_file_net(seq);
  3224. return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
  3225. }
  3226. static void packet_seq_stop(struct seq_file *seq, void *v)
  3227. __releases(RCU)
  3228. {
  3229. rcu_read_unlock();
  3230. }
  3231. static int packet_seq_show(struct seq_file *seq, void *v)
  3232. {
  3233. if (v == SEQ_START_TOKEN)
  3234. seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
  3235. else {
  3236. struct sock *s = sk_entry(v);
  3237. const struct packet_sock *po = pkt_sk(s);
  3238. seq_printf(seq,
  3239. "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
  3240. s,
  3241. atomic_read(&s->sk_refcnt),
  3242. s->sk_type,
  3243. ntohs(po->num),
  3244. po->ifindex,
  3245. po->running,
  3246. atomic_read(&s->sk_rmem_alloc),
  3247. from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)),
  3248. sock_i_ino(s));
  3249. }
  3250. return 0;
  3251. }
  3252. static const struct seq_operations packet_seq_ops = {
  3253. .start = packet_seq_start,
  3254. .next = packet_seq_next,
  3255. .stop = packet_seq_stop,
  3256. .show = packet_seq_show,
  3257. };
  3258. static int packet_seq_open(struct inode *inode, struct file *file)
  3259. {
  3260. return seq_open_net(inode, file, &packet_seq_ops,
  3261. sizeof(struct seq_net_private));
  3262. }
  3263. static const struct file_operations packet_seq_fops = {
  3264. .owner = THIS_MODULE,
  3265. .open = packet_seq_open,
  3266. .read = seq_read,
  3267. .llseek = seq_lseek,
  3268. .release = seq_release_net,
  3269. };
  3270. #endif
  3271. static int __net_init packet_net_init(struct net *net)
  3272. {
  3273. mutex_init(&net->packet.sklist_lock);
  3274. INIT_HLIST_HEAD(&net->packet.sklist);
  3275. if (!proc_create("packet", 0, net->proc_net, &packet_seq_fops))
  3276. return -ENOMEM;
  3277. return 0;
  3278. }
  3279. static void __net_exit packet_net_exit(struct net *net)
  3280. {
  3281. remove_proc_entry("packet", net->proc_net);
  3282. }
  3283. static struct pernet_operations packet_net_ops = {
  3284. .init = packet_net_init,
  3285. .exit = packet_net_exit,
  3286. };
  3287. static void __exit packet_exit(void)
  3288. {
  3289. unregister_netdevice_notifier(&packet_netdev_notifier);
  3290. unregister_pernet_subsys(&packet_net_ops);
  3291. sock_unregister(PF_PACKET);
  3292. proto_unregister(&packet_proto);
  3293. }
  3294. static int __init packet_init(void)
  3295. {
  3296. int rc = proto_register(&packet_proto, 0);
  3297. if (rc != 0)
  3298. goto out;
  3299. sock_register(&packet_family_ops);
  3300. register_pernet_subsys(&packet_net_ops);
  3301. register_netdevice_notifier(&packet_netdev_notifier);
  3302. out:
  3303. return rc;
  3304. }
  3305. module_init(packet_init);
  3306. module_exit(packet_exit);
  3307. MODULE_LICENSE("GPL");
  3308. MODULE_ALIAS_NETPROTO(PF_PACKET);