af_packet.c 91 KB

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