af_packet.c 92 KB

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