af_packet.c 91 KB

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