af_packet.c 90 KB

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