af_packet.c 91 KB

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