af_packet.c 92 KB

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