af_packet.c 97 KB

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