af_packet.c 95 KB

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