af_packet.c 94 KB

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