af_packet.c 90 KB

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