af_packet.c 91 KB

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