af_packet.c 92 KB

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