af_packet.c 94 KB

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