af_packet.c 95 KB

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