af_packet.c 91 KB

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