af_packet.c 96 KB

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