af_packet.c 91 KB

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