af_packet.c 97 KB

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