af_packet.c 96 KB

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