af_packet.c 100 KB

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