af_packet.c 90 KB

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