af_packet.c 91 KB

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