af_packet.c 95 KB

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