af_packet.c 91 KB

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