af_packet.c 92 KB

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