af_packet.c 91 KB

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