af_packet.c 109 KB

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