af_packet.c 96 KB

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