af_packet.c 93 KB

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