igmp.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102
  1. /*
  2. * Linux NET3: Internet Group Management Protocol [IGMP]
  3. *
  4. * This code implements the IGMP protocol as defined in RFC1112. There has
  5. * been a further revision of this protocol since which is now supported.
  6. *
  7. * If you have trouble with this module be careful what gcc you have used,
  8. * the older version didn't come out right using gcc 2.5.8, the newer one
  9. * seems to fall out with gcc 2.6.2.
  10. *
  11. * Authors:
  12. * Alan Cox <alan@lxorguk.ukuu.org.uk>
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. *
  19. * Fixes:
  20. *
  21. * Alan Cox : Added lots of __inline__ to optimise
  22. * the memory usage of all the tiny little
  23. * functions.
  24. * Alan Cox : Dumped the header building experiment.
  25. * Alan Cox : Minor tweaks ready for multicast routing
  26. * and extended IGMP protocol.
  27. * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
  28. * writes utterly bogus code otherwise (sigh)
  29. * fixed IGMP loopback to behave in the manner
  30. * desired by mrouted, fixed the fact it has been
  31. * broken since 1.3.6 and cleaned up a few minor
  32. * points.
  33. *
  34. * Chih-Jen Chang : Tried to revise IGMP to Version 2
  35. * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
  36. * The enhancements are mainly based on Steve Deering's
  37. * ipmulti-3.5 source code.
  38. * Chih-Jen Chang : Added the igmp_get_mrouter_info and
  39. * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
  40. * the mrouted version on that device.
  41. * Chih-Jen Chang : Added the max_resp_time parameter to
  42. * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
  43. * to identify the multicast router version
  44. * and do what the IGMP version 2 specified.
  45. * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
  46. * Tsu-Sheng Tsao if the specified time expired.
  47. * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
  48. * Alan Cox : Use GFP_ATOMIC in the right places.
  49. * Christian Daudt : igmp timer wasn't set for local group
  50. * memberships but was being deleted,
  51. * which caused a "del_timer() called
  52. * from %p with timer not initialized\n"
  53. * message (960131).
  54. * Christian Daudt : removed del_timer from
  55. * igmp_timer_expire function (960205).
  56. * Christian Daudt : igmp_heard_report now only calls
  57. * igmp_timer_expire if tm->running is
  58. * true (960216).
  59. * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
  60. * igmp_heard_query never trigger. Expiry
  61. * miscalculation fixed in igmp_heard_query
  62. * and random() made to return unsigned to
  63. * prevent negative expiry times.
  64. * Alexey Kuznetsov: Wrong group leaving behaviour, backport
  65. * fix from pending 2.1.x patches.
  66. * Alan Cox: Forget to enable FDDI support earlier.
  67. * Alexey Kuznetsov: Fixed leaving groups on device down.
  68. * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
  69. * David L Stevens: IGMPv3 support, with help from
  70. * Vinay Kulkarni
  71. */
  72. #include <linux/module.h>
  73. #include <linux/slab.h>
  74. #include <linux/uaccess.h>
  75. #include <linux/types.h>
  76. #include <linux/kernel.h>
  77. #include <linux/jiffies.h>
  78. #include <linux/string.h>
  79. #include <linux/socket.h>
  80. #include <linux/sockios.h>
  81. #include <linux/in.h>
  82. #include <linux/inet.h>
  83. #include <linux/netdevice.h>
  84. #include <linux/skbuff.h>
  85. #include <linux/inetdevice.h>
  86. #include <linux/igmp.h>
  87. #include <linux/if_arp.h>
  88. #include <linux/rtnetlink.h>
  89. #include <linux/times.h>
  90. #include <linux/pkt_sched.h>
  91. #include <linux/byteorder/generic.h>
  92. #include <net/net_namespace.h>
  93. #include <net/arp.h>
  94. #include <net/ip.h>
  95. #include <net/protocol.h>
  96. #include <net/route.h>
  97. #include <net/sock.h>
  98. #include <net/checksum.h>
  99. #include <net/inet_common.h>
  100. #include <linux/netfilter_ipv4.h>
  101. #ifdef CONFIG_IP_MROUTE
  102. #include <linux/mroute.h>
  103. #endif
  104. #ifdef CONFIG_PROC_FS
  105. #include <linux/proc_fs.h>
  106. #include <linux/seq_file.h>
  107. #endif
  108. #ifdef CONFIG_IP_MULTICAST
  109. /* Parameter names and values are taken from igmp-v2-06 draft */
  110. #define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
  111. #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
  112. #define IGMP_QUERY_INTERVAL (125*HZ)
  113. #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
  114. #define IGMP_INITIAL_REPORT_DELAY (1)
  115. /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
  116. * IGMP specs require to report membership immediately after
  117. * joining a group, but we delay the first report by a
  118. * small interval. It seems more natural and still does not
  119. * contradict to specs provided this delay is small enough.
  120. */
  121. #define IGMP_V1_SEEN(in_dev) \
  122. (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
  123. IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
  124. ((in_dev)->mr_v1_seen && \
  125. time_before(jiffies, (in_dev)->mr_v1_seen)))
  126. #define IGMP_V2_SEEN(in_dev) \
  127. (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
  128. IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
  129. ((in_dev)->mr_v2_seen && \
  130. time_before(jiffies, (in_dev)->mr_v2_seen)))
  131. static int unsolicited_report_interval(struct in_device *in_dev)
  132. {
  133. int interval_ms, interval_jiffies;
  134. if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
  135. interval_ms = IN_DEV_CONF_GET(
  136. in_dev,
  137. IGMPV2_UNSOLICITED_REPORT_INTERVAL);
  138. else /* v3 */
  139. interval_ms = IN_DEV_CONF_GET(
  140. in_dev,
  141. IGMPV3_UNSOLICITED_REPORT_INTERVAL);
  142. interval_jiffies = msecs_to_jiffies(interval_ms);
  143. /* _timer functions can't handle a delay of 0 jiffies so ensure
  144. * we always return a positive value.
  145. */
  146. if (interval_jiffies <= 0)
  147. interval_jiffies = 1;
  148. return interval_jiffies;
  149. }
  150. static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
  151. static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im);
  152. static void igmpv3_clear_delrec(struct in_device *in_dev);
  153. static int sf_setstate(struct ip_mc_list *pmc);
  154. static void sf_markstate(struct ip_mc_list *pmc);
  155. #endif
  156. static void ip_mc_clear_src(struct ip_mc_list *pmc);
  157. static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
  158. int sfcount, __be32 *psfsrc, int delta);
  159. static void ip_ma_put(struct ip_mc_list *im)
  160. {
  161. if (refcount_dec_and_test(&im->refcnt)) {
  162. in_dev_put(im->interface);
  163. kfree_rcu(im, rcu);
  164. }
  165. }
  166. #define for_each_pmc_rcu(in_dev, pmc) \
  167. for (pmc = rcu_dereference(in_dev->mc_list); \
  168. pmc != NULL; \
  169. pmc = rcu_dereference(pmc->next_rcu))
  170. #define for_each_pmc_rtnl(in_dev, pmc) \
  171. for (pmc = rtnl_dereference(in_dev->mc_list); \
  172. pmc != NULL; \
  173. pmc = rtnl_dereference(pmc->next_rcu))
  174. static void ip_sf_list_clear_all(struct ip_sf_list *psf)
  175. {
  176. struct ip_sf_list *next;
  177. while (psf) {
  178. next = psf->sf_next;
  179. kfree(psf);
  180. psf = next;
  181. }
  182. }
  183. #ifdef CONFIG_IP_MULTICAST
  184. /*
  185. * Timer management
  186. */
  187. static void igmp_stop_timer(struct ip_mc_list *im)
  188. {
  189. spin_lock_bh(&im->lock);
  190. if (del_timer(&im->timer))
  191. refcount_dec(&im->refcnt);
  192. im->tm_running = 0;
  193. im->reporter = 0;
  194. im->unsolicit_count = 0;
  195. spin_unlock_bh(&im->lock);
  196. }
  197. /* It must be called with locked im->lock */
  198. static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
  199. {
  200. int tv = prandom_u32() % max_delay;
  201. im->tm_running = 1;
  202. if (!mod_timer(&im->timer, jiffies+tv+2))
  203. refcount_inc(&im->refcnt);
  204. }
  205. static void igmp_gq_start_timer(struct in_device *in_dev)
  206. {
  207. int tv = prandom_u32() % in_dev->mr_maxdelay;
  208. unsigned long exp = jiffies + tv + 2;
  209. if (in_dev->mr_gq_running &&
  210. time_after_eq(exp, (in_dev->mr_gq_timer).expires))
  211. return;
  212. in_dev->mr_gq_running = 1;
  213. if (!mod_timer(&in_dev->mr_gq_timer, exp))
  214. in_dev_hold(in_dev);
  215. }
  216. static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
  217. {
  218. int tv = prandom_u32() % delay;
  219. if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
  220. in_dev_hold(in_dev);
  221. }
  222. static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
  223. {
  224. spin_lock_bh(&im->lock);
  225. im->unsolicit_count = 0;
  226. if (del_timer(&im->timer)) {
  227. if ((long)(im->timer.expires-jiffies) < max_delay) {
  228. add_timer(&im->timer);
  229. im->tm_running = 1;
  230. spin_unlock_bh(&im->lock);
  231. return;
  232. }
  233. refcount_dec(&im->refcnt);
  234. }
  235. igmp_start_timer(im, max_delay);
  236. spin_unlock_bh(&im->lock);
  237. }
  238. /*
  239. * Send an IGMP report.
  240. */
  241. #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
  242. static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
  243. int gdeleted, int sdeleted)
  244. {
  245. switch (type) {
  246. case IGMPV3_MODE_IS_INCLUDE:
  247. case IGMPV3_MODE_IS_EXCLUDE:
  248. if (gdeleted || sdeleted)
  249. return 0;
  250. if (!(pmc->gsquery && !psf->sf_gsresp)) {
  251. if (pmc->sfmode == MCAST_INCLUDE)
  252. return 1;
  253. /* don't include if this source is excluded
  254. * in all filters
  255. */
  256. if (psf->sf_count[MCAST_INCLUDE])
  257. return type == IGMPV3_MODE_IS_INCLUDE;
  258. return pmc->sfcount[MCAST_EXCLUDE] ==
  259. psf->sf_count[MCAST_EXCLUDE];
  260. }
  261. return 0;
  262. case IGMPV3_CHANGE_TO_INCLUDE:
  263. if (gdeleted || sdeleted)
  264. return 0;
  265. return psf->sf_count[MCAST_INCLUDE] != 0;
  266. case IGMPV3_CHANGE_TO_EXCLUDE:
  267. if (gdeleted || sdeleted)
  268. return 0;
  269. if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
  270. psf->sf_count[MCAST_INCLUDE])
  271. return 0;
  272. return pmc->sfcount[MCAST_EXCLUDE] ==
  273. psf->sf_count[MCAST_EXCLUDE];
  274. case IGMPV3_ALLOW_NEW_SOURCES:
  275. if (gdeleted || !psf->sf_crcount)
  276. return 0;
  277. return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
  278. case IGMPV3_BLOCK_OLD_SOURCES:
  279. if (pmc->sfmode == MCAST_INCLUDE)
  280. return gdeleted || (psf->sf_crcount && sdeleted);
  281. return psf->sf_crcount && !gdeleted && !sdeleted;
  282. }
  283. return 0;
  284. }
  285. static int
  286. igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
  287. {
  288. struct ip_sf_list *psf;
  289. int scount = 0;
  290. for (psf = pmc->sources; psf; psf = psf->sf_next) {
  291. if (!is_in(pmc, psf, type, gdeleted, sdeleted))
  292. continue;
  293. scount++;
  294. }
  295. return scount;
  296. }
  297. /* source address selection per RFC 3376 section 4.2.13 */
  298. static __be32 igmpv3_get_srcaddr(struct net_device *dev,
  299. const struct flowi4 *fl4)
  300. {
  301. struct in_device *in_dev = __in_dev_get_rcu(dev);
  302. if (!in_dev)
  303. return htonl(INADDR_ANY);
  304. for_ifa(in_dev) {
  305. if (fl4->saddr == ifa->ifa_local)
  306. return fl4->saddr;
  307. } endfor_ifa(in_dev);
  308. return htonl(INADDR_ANY);
  309. }
  310. static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
  311. {
  312. struct sk_buff *skb;
  313. struct rtable *rt;
  314. struct iphdr *pip;
  315. struct igmpv3_report *pig;
  316. struct net *net = dev_net(dev);
  317. struct flowi4 fl4;
  318. int hlen = LL_RESERVED_SPACE(dev);
  319. int tlen = dev->needed_tailroom;
  320. unsigned int size = mtu;
  321. while (1) {
  322. skb = alloc_skb(size + hlen + tlen,
  323. GFP_ATOMIC | __GFP_NOWARN);
  324. if (skb)
  325. break;
  326. size >>= 1;
  327. if (size < 256)
  328. return NULL;
  329. }
  330. skb->priority = TC_PRIO_CONTROL;
  331. rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
  332. 0, 0,
  333. IPPROTO_IGMP, 0, dev->ifindex);
  334. if (IS_ERR(rt)) {
  335. kfree_skb(skb);
  336. return NULL;
  337. }
  338. skb_dst_set(skb, &rt->dst);
  339. skb->dev = dev;
  340. skb_reserve(skb, hlen);
  341. skb_tailroom_reserve(skb, mtu, tlen);
  342. skb_reset_network_header(skb);
  343. pip = ip_hdr(skb);
  344. skb_put(skb, sizeof(struct iphdr) + 4);
  345. pip->version = 4;
  346. pip->ihl = (sizeof(struct iphdr)+4)>>2;
  347. pip->tos = 0xc0;
  348. pip->frag_off = htons(IP_DF);
  349. pip->ttl = 1;
  350. pip->daddr = fl4.daddr;
  351. rcu_read_lock();
  352. pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
  353. rcu_read_unlock();
  354. pip->protocol = IPPROTO_IGMP;
  355. pip->tot_len = 0; /* filled in later */
  356. ip_select_ident(net, skb, NULL);
  357. ((u8 *)&pip[1])[0] = IPOPT_RA;
  358. ((u8 *)&pip[1])[1] = 4;
  359. ((u8 *)&pip[1])[2] = 0;
  360. ((u8 *)&pip[1])[3] = 0;
  361. skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
  362. skb_put(skb, sizeof(*pig));
  363. pig = igmpv3_report_hdr(skb);
  364. pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
  365. pig->resv1 = 0;
  366. pig->csum = 0;
  367. pig->resv2 = 0;
  368. pig->ngrec = 0;
  369. return skb;
  370. }
  371. static int igmpv3_sendpack(struct sk_buff *skb)
  372. {
  373. struct igmphdr *pig = igmp_hdr(skb);
  374. const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
  375. pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
  376. return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
  377. }
  378. static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
  379. {
  380. return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
  381. }
  382. static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
  383. int type, struct igmpv3_grec **ppgr, unsigned int mtu)
  384. {
  385. struct net_device *dev = pmc->interface->dev;
  386. struct igmpv3_report *pih;
  387. struct igmpv3_grec *pgr;
  388. if (!skb) {
  389. skb = igmpv3_newpack(dev, mtu);
  390. if (!skb)
  391. return NULL;
  392. }
  393. pgr = skb_put(skb, sizeof(struct igmpv3_grec));
  394. pgr->grec_type = type;
  395. pgr->grec_auxwords = 0;
  396. pgr->grec_nsrcs = 0;
  397. pgr->grec_mca = pmc->multiaddr;
  398. pih = igmpv3_report_hdr(skb);
  399. pih->ngrec = htons(ntohs(pih->ngrec)+1);
  400. *ppgr = pgr;
  401. return skb;
  402. }
  403. #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
  404. static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
  405. int type, int gdeleted, int sdeleted)
  406. {
  407. struct net_device *dev = pmc->interface->dev;
  408. struct net *net = dev_net(dev);
  409. struct igmpv3_report *pih;
  410. struct igmpv3_grec *pgr = NULL;
  411. struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
  412. int scount, stotal, first, isquery, truncate;
  413. unsigned int mtu;
  414. if (pmc->multiaddr == IGMP_ALL_HOSTS)
  415. return skb;
  416. if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
  417. return skb;
  418. mtu = READ_ONCE(dev->mtu);
  419. if (mtu < IPV4_MIN_MTU)
  420. return skb;
  421. isquery = type == IGMPV3_MODE_IS_INCLUDE ||
  422. type == IGMPV3_MODE_IS_EXCLUDE;
  423. truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
  424. type == IGMPV3_CHANGE_TO_EXCLUDE;
  425. stotal = scount = 0;
  426. psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
  427. if (!*psf_list)
  428. goto empty_source;
  429. pih = skb ? igmpv3_report_hdr(skb) : NULL;
  430. /* EX and TO_EX get a fresh packet, if needed */
  431. if (truncate) {
  432. if (pih && pih->ngrec &&
  433. AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
  434. if (skb)
  435. igmpv3_sendpack(skb);
  436. skb = igmpv3_newpack(dev, mtu);
  437. }
  438. }
  439. first = 1;
  440. psf_prev = NULL;
  441. for (psf = *psf_list; psf; psf = psf_next) {
  442. __be32 *psrc;
  443. psf_next = psf->sf_next;
  444. if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
  445. psf_prev = psf;
  446. continue;
  447. }
  448. /* Based on RFC3376 5.1. Should not send source-list change
  449. * records when there is a filter mode change.
  450. */
  451. if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
  452. (!gdeleted && pmc->crcount)) &&
  453. (type == IGMPV3_ALLOW_NEW_SOURCES ||
  454. type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
  455. goto decrease_sf_crcount;
  456. /* clear marks on query responses */
  457. if (isquery)
  458. psf->sf_gsresp = 0;
  459. if (AVAILABLE(skb) < sizeof(__be32) +
  460. first*sizeof(struct igmpv3_grec)) {
  461. if (truncate && !first)
  462. break; /* truncate these */
  463. if (pgr)
  464. pgr->grec_nsrcs = htons(scount);
  465. if (skb)
  466. igmpv3_sendpack(skb);
  467. skb = igmpv3_newpack(dev, mtu);
  468. first = 1;
  469. scount = 0;
  470. }
  471. if (first) {
  472. skb = add_grhead(skb, pmc, type, &pgr, mtu);
  473. first = 0;
  474. }
  475. if (!skb)
  476. return NULL;
  477. psrc = skb_put(skb, sizeof(__be32));
  478. *psrc = psf->sf_inaddr;
  479. scount++; stotal++;
  480. if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
  481. type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
  482. decrease_sf_crcount:
  483. psf->sf_crcount--;
  484. if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
  485. if (psf_prev)
  486. psf_prev->sf_next = psf->sf_next;
  487. else
  488. *psf_list = psf->sf_next;
  489. kfree(psf);
  490. continue;
  491. }
  492. }
  493. psf_prev = psf;
  494. }
  495. empty_source:
  496. if (!stotal) {
  497. if (type == IGMPV3_ALLOW_NEW_SOURCES ||
  498. type == IGMPV3_BLOCK_OLD_SOURCES)
  499. return skb;
  500. if (pmc->crcount || isquery) {
  501. /* make sure we have room for group header */
  502. if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
  503. igmpv3_sendpack(skb);
  504. skb = NULL; /* add_grhead will get a new one */
  505. }
  506. skb = add_grhead(skb, pmc, type, &pgr, mtu);
  507. }
  508. }
  509. if (pgr)
  510. pgr->grec_nsrcs = htons(scount);
  511. if (isquery)
  512. pmc->gsquery = 0; /* clear query state on report */
  513. return skb;
  514. }
  515. static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
  516. {
  517. struct sk_buff *skb = NULL;
  518. struct net *net = dev_net(in_dev->dev);
  519. int type;
  520. if (!pmc) {
  521. rcu_read_lock();
  522. for_each_pmc_rcu(in_dev, pmc) {
  523. if (pmc->multiaddr == IGMP_ALL_HOSTS)
  524. continue;
  525. if (ipv4_is_local_multicast(pmc->multiaddr) &&
  526. !net->ipv4.sysctl_igmp_llm_reports)
  527. continue;
  528. spin_lock_bh(&pmc->lock);
  529. if (pmc->sfcount[MCAST_EXCLUDE])
  530. type = IGMPV3_MODE_IS_EXCLUDE;
  531. else
  532. type = IGMPV3_MODE_IS_INCLUDE;
  533. skb = add_grec(skb, pmc, type, 0, 0);
  534. spin_unlock_bh(&pmc->lock);
  535. }
  536. rcu_read_unlock();
  537. } else {
  538. spin_lock_bh(&pmc->lock);
  539. if (pmc->sfcount[MCAST_EXCLUDE])
  540. type = IGMPV3_MODE_IS_EXCLUDE;
  541. else
  542. type = IGMPV3_MODE_IS_INCLUDE;
  543. skb = add_grec(skb, pmc, type, 0, 0);
  544. spin_unlock_bh(&pmc->lock);
  545. }
  546. if (!skb)
  547. return 0;
  548. return igmpv3_sendpack(skb);
  549. }
  550. /*
  551. * remove zero-count source records from a source filter list
  552. */
  553. static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
  554. {
  555. struct ip_sf_list *psf_prev, *psf_next, *psf;
  556. psf_prev = NULL;
  557. for (psf = *ppsf; psf; psf = psf_next) {
  558. psf_next = psf->sf_next;
  559. if (psf->sf_crcount == 0) {
  560. if (psf_prev)
  561. psf_prev->sf_next = psf->sf_next;
  562. else
  563. *ppsf = psf->sf_next;
  564. kfree(psf);
  565. } else
  566. psf_prev = psf;
  567. }
  568. }
  569. static void kfree_pmc(struct ip_mc_list *pmc)
  570. {
  571. ip_sf_list_clear_all(pmc->sources);
  572. ip_sf_list_clear_all(pmc->tomb);
  573. kfree(pmc);
  574. }
  575. static void igmpv3_send_cr(struct in_device *in_dev)
  576. {
  577. struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
  578. struct sk_buff *skb = NULL;
  579. int type, dtype;
  580. rcu_read_lock();
  581. spin_lock_bh(&in_dev->mc_tomb_lock);
  582. /* deleted MCA's */
  583. pmc_prev = NULL;
  584. for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
  585. pmc_next = pmc->next;
  586. if (pmc->sfmode == MCAST_INCLUDE) {
  587. type = IGMPV3_BLOCK_OLD_SOURCES;
  588. dtype = IGMPV3_BLOCK_OLD_SOURCES;
  589. skb = add_grec(skb, pmc, type, 1, 0);
  590. skb = add_grec(skb, pmc, dtype, 1, 1);
  591. }
  592. if (pmc->crcount) {
  593. if (pmc->sfmode == MCAST_EXCLUDE) {
  594. type = IGMPV3_CHANGE_TO_INCLUDE;
  595. skb = add_grec(skb, pmc, type, 1, 0);
  596. }
  597. pmc->crcount--;
  598. if (pmc->crcount == 0) {
  599. igmpv3_clear_zeros(&pmc->tomb);
  600. igmpv3_clear_zeros(&pmc->sources);
  601. }
  602. }
  603. if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
  604. if (pmc_prev)
  605. pmc_prev->next = pmc_next;
  606. else
  607. in_dev->mc_tomb = pmc_next;
  608. in_dev_put(pmc->interface);
  609. kfree_pmc(pmc);
  610. } else
  611. pmc_prev = pmc;
  612. }
  613. spin_unlock_bh(&in_dev->mc_tomb_lock);
  614. /* change recs */
  615. for_each_pmc_rcu(in_dev, pmc) {
  616. spin_lock_bh(&pmc->lock);
  617. if (pmc->sfcount[MCAST_EXCLUDE]) {
  618. type = IGMPV3_BLOCK_OLD_SOURCES;
  619. dtype = IGMPV3_ALLOW_NEW_SOURCES;
  620. } else {
  621. type = IGMPV3_ALLOW_NEW_SOURCES;
  622. dtype = IGMPV3_BLOCK_OLD_SOURCES;
  623. }
  624. skb = add_grec(skb, pmc, type, 0, 0);
  625. skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
  626. /* filter mode changes */
  627. if (pmc->crcount) {
  628. if (pmc->sfmode == MCAST_EXCLUDE)
  629. type = IGMPV3_CHANGE_TO_EXCLUDE;
  630. else
  631. type = IGMPV3_CHANGE_TO_INCLUDE;
  632. skb = add_grec(skb, pmc, type, 0, 0);
  633. pmc->crcount--;
  634. }
  635. spin_unlock_bh(&pmc->lock);
  636. }
  637. rcu_read_unlock();
  638. if (!skb)
  639. return;
  640. (void) igmpv3_sendpack(skb);
  641. }
  642. static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
  643. int type)
  644. {
  645. struct sk_buff *skb;
  646. struct iphdr *iph;
  647. struct igmphdr *ih;
  648. struct rtable *rt;
  649. struct net_device *dev = in_dev->dev;
  650. struct net *net = dev_net(dev);
  651. __be32 group = pmc ? pmc->multiaddr : 0;
  652. struct flowi4 fl4;
  653. __be32 dst;
  654. int hlen, tlen;
  655. if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
  656. return igmpv3_send_report(in_dev, pmc);
  657. if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
  658. return 0;
  659. if (type == IGMP_HOST_LEAVE_MESSAGE)
  660. dst = IGMP_ALL_ROUTER;
  661. else
  662. dst = group;
  663. rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
  664. 0, 0,
  665. IPPROTO_IGMP, 0, dev->ifindex);
  666. if (IS_ERR(rt))
  667. return -1;
  668. hlen = LL_RESERVED_SPACE(dev);
  669. tlen = dev->needed_tailroom;
  670. skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
  671. if (!skb) {
  672. ip_rt_put(rt);
  673. return -1;
  674. }
  675. skb->priority = TC_PRIO_CONTROL;
  676. skb_dst_set(skb, &rt->dst);
  677. skb_reserve(skb, hlen);
  678. skb_reset_network_header(skb);
  679. iph = ip_hdr(skb);
  680. skb_put(skb, sizeof(struct iphdr) + 4);
  681. iph->version = 4;
  682. iph->ihl = (sizeof(struct iphdr)+4)>>2;
  683. iph->tos = 0xc0;
  684. iph->frag_off = htons(IP_DF);
  685. iph->ttl = 1;
  686. iph->daddr = dst;
  687. iph->saddr = fl4.saddr;
  688. iph->protocol = IPPROTO_IGMP;
  689. ip_select_ident(net, skb, NULL);
  690. ((u8 *)&iph[1])[0] = IPOPT_RA;
  691. ((u8 *)&iph[1])[1] = 4;
  692. ((u8 *)&iph[1])[2] = 0;
  693. ((u8 *)&iph[1])[3] = 0;
  694. ih = skb_put(skb, sizeof(struct igmphdr));
  695. ih->type = type;
  696. ih->code = 0;
  697. ih->csum = 0;
  698. ih->group = group;
  699. ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
  700. return ip_local_out(net, skb->sk, skb);
  701. }
  702. static void igmp_gq_timer_expire(struct timer_list *t)
  703. {
  704. struct in_device *in_dev = from_timer(in_dev, t, mr_gq_timer);
  705. in_dev->mr_gq_running = 0;
  706. igmpv3_send_report(in_dev, NULL);
  707. in_dev_put(in_dev);
  708. }
  709. static void igmp_ifc_timer_expire(struct timer_list *t)
  710. {
  711. struct in_device *in_dev = from_timer(in_dev, t, mr_ifc_timer);
  712. igmpv3_send_cr(in_dev);
  713. if (in_dev->mr_ifc_count) {
  714. in_dev->mr_ifc_count--;
  715. igmp_ifc_start_timer(in_dev,
  716. unsolicited_report_interval(in_dev));
  717. }
  718. in_dev_put(in_dev);
  719. }
  720. static void igmp_ifc_event(struct in_device *in_dev)
  721. {
  722. struct net *net = dev_net(in_dev->dev);
  723. if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
  724. return;
  725. in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
  726. igmp_ifc_start_timer(in_dev, 1);
  727. }
  728. static void igmp_timer_expire(struct timer_list *t)
  729. {
  730. struct ip_mc_list *im = from_timer(im, t, timer);
  731. struct in_device *in_dev = im->interface;
  732. spin_lock(&im->lock);
  733. im->tm_running = 0;
  734. if (im->unsolicit_count && --im->unsolicit_count)
  735. igmp_start_timer(im, unsolicited_report_interval(in_dev));
  736. im->reporter = 1;
  737. spin_unlock(&im->lock);
  738. if (IGMP_V1_SEEN(in_dev))
  739. igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
  740. else if (IGMP_V2_SEEN(in_dev))
  741. igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
  742. else
  743. igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
  744. ip_ma_put(im);
  745. }
  746. /* mark EXCLUDE-mode sources */
  747. static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
  748. {
  749. struct ip_sf_list *psf;
  750. int i, scount;
  751. scount = 0;
  752. for (psf = pmc->sources; psf; psf = psf->sf_next) {
  753. if (scount == nsrcs)
  754. break;
  755. for (i = 0; i < nsrcs; i++) {
  756. /* skip inactive filters */
  757. if (psf->sf_count[MCAST_INCLUDE] ||
  758. pmc->sfcount[MCAST_EXCLUDE] !=
  759. psf->sf_count[MCAST_EXCLUDE])
  760. break;
  761. if (srcs[i] == psf->sf_inaddr) {
  762. scount++;
  763. break;
  764. }
  765. }
  766. }
  767. pmc->gsquery = 0;
  768. if (scount == nsrcs) /* all sources excluded */
  769. return 0;
  770. return 1;
  771. }
  772. static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
  773. {
  774. struct ip_sf_list *psf;
  775. int i, scount;
  776. if (pmc->sfmode == MCAST_EXCLUDE)
  777. return igmp_xmarksources(pmc, nsrcs, srcs);
  778. /* mark INCLUDE-mode sources */
  779. scount = 0;
  780. for (psf = pmc->sources; psf; psf = psf->sf_next) {
  781. if (scount == nsrcs)
  782. break;
  783. for (i = 0; i < nsrcs; i++)
  784. if (srcs[i] == psf->sf_inaddr) {
  785. psf->sf_gsresp = 1;
  786. scount++;
  787. break;
  788. }
  789. }
  790. if (!scount) {
  791. pmc->gsquery = 0;
  792. return 0;
  793. }
  794. pmc->gsquery = 1;
  795. return 1;
  796. }
  797. /* return true if packet was dropped */
  798. static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
  799. {
  800. struct ip_mc_list *im;
  801. struct net *net = dev_net(in_dev->dev);
  802. /* Timers are only set for non-local groups */
  803. if (group == IGMP_ALL_HOSTS)
  804. return false;
  805. if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
  806. return false;
  807. rcu_read_lock();
  808. for_each_pmc_rcu(in_dev, im) {
  809. if (im->multiaddr == group) {
  810. igmp_stop_timer(im);
  811. break;
  812. }
  813. }
  814. rcu_read_unlock();
  815. return false;
  816. }
  817. /* return true if packet was dropped */
  818. static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
  819. int len)
  820. {
  821. struct igmphdr *ih = igmp_hdr(skb);
  822. struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
  823. struct ip_mc_list *im;
  824. __be32 group = ih->group;
  825. int max_delay;
  826. int mark = 0;
  827. struct net *net = dev_net(in_dev->dev);
  828. if (len == 8) {
  829. if (ih->code == 0) {
  830. /* Alas, old v1 router presents here. */
  831. max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
  832. in_dev->mr_v1_seen = jiffies +
  833. (in_dev->mr_qrv * in_dev->mr_qi) +
  834. in_dev->mr_qri;
  835. group = 0;
  836. } else {
  837. /* v2 router present */
  838. max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
  839. in_dev->mr_v2_seen = jiffies +
  840. (in_dev->mr_qrv * in_dev->mr_qi) +
  841. in_dev->mr_qri;
  842. }
  843. /* cancel the interface change timer */
  844. in_dev->mr_ifc_count = 0;
  845. if (del_timer(&in_dev->mr_ifc_timer))
  846. __in_dev_put(in_dev);
  847. /* clear deleted report items */
  848. igmpv3_clear_delrec(in_dev);
  849. } else if (len < 12) {
  850. return true; /* ignore bogus packet; freed by caller */
  851. } else if (IGMP_V1_SEEN(in_dev)) {
  852. /* This is a v3 query with v1 queriers present */
  853. max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
  854. group = 0;
  855. } else if (IGMP_V2_SEEN(in_dev)) {
  856. /* this is a v3 query with v2 queriers present;
  857. * Interpretation of the max_delay code is problematic here.
  858. * A real v2 host would use ih_code directly, while v3 has a
  859. * different encoding. We use the v3 encoding as more likely
  860. * to be intended in a v3 query.
  861. */
  862. max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
  863. if (!max_delay)
  864. max_delay = 1; /* can't mod w/ 0 */
  865. } else { /* v3 */
  866. if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
  867. return true;
  868. ih3 = igmpv3_query_hdr(skb);
  869. if (ih3->nsrcs) {
  870. if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
  871. + ntohs(ih3->nsrcs)*sizeof(__be32)))
  872. return true;
  873. ih3 = igmpv3_query_hdr(skb);
  874. }
  875. max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
  876. if (!max_delay)
  877. max_delay = 1; /* can't mod w/ 0 */
  878. in_dev->mr_maxdelay = max_delay;
  879. /* RFC3376, 4.1.6. QRV and 4.1.7. QQIC, when the most recently
  880. * received value was zero, use the default or statically
  881. * configured value.
  882. */
  883. in_dev->mr_qrv = ih3->qrv ?: net->ipv4.sysctl_igmp_qrv;
  884. in_dev->mr_qi = IGMPV3_QQIC(ih3->qqic)*HZ ?: IGMP_QUERY_INTERVAL;
  885. /* RFC3376, 8.3. Query Response Interval:
  886. * The number of seconds represented by the [Query Response
  887. * Interval] must be less than the [Query Interval].
  888. */
  889. if (in_dev->mr_qri >= in_dev->mr_qi)
  890. in_dev->mr_qri = (in_dev->mr_qi/HZ - 1)*HZ;
  891. if (!group) { /* general query */
  892. if (ih3->nsrcs)
  893. return true; /* no sources allowed */
  894. igmp_gq_start_timer(in_dev);
  895. return false;
  896. }
  897. /* mark sources to include, if group & source-specific */
  898. mark = ih3->nsrcs != 0;
  899. }
  900. /*
  901. * - Start the timers in all of our membership records
  902. * that the query applies to for the interface on
  903. * which the query arrived excl. those that belong
  904. * to a "local" group (224.0.0.X)
  905. * - For timers already running check if they need to
  906. * be reset.
  907. * - Use the igmp->igmp_code field as the maximum
  908. * delay possible
  909. */
  910. rcu_read_lock();
  911. for_each_pmc_rcu(in_dev, im) {
  912. int changed;
  913. if (group && group != im->multiaddr)
  914. continue;
  915. if (im->multiaddr == IGMP_ALL_HOSTS)
  916. continue;
  917. if (ipv4_is_local_multicast(im->multiaddr) &&
  918. !net->ipv4.sysctl_igmp_llm_reports)
  919. continue;
  920. spin_lock_bh(&im->lock);
  921. if (im->tm_running)
  922. im->gsquery = im->gsquery && mark;
  923. else
  924. im->gsquery = mark;
  925. changed = !im->gsquery ||
  926. igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
  927. spin_unlock_bh(&im->lock);
  928. if (changed)
  929. igmp_mod_timer(im, max_delay);
  930. }
  931. rcu_read_unlock();
  932. return false;
  933. }
  934. /* called in rcu_read_lock() section */
  935. int igmp_rcv(struct sk_buff *skb)
  936. {
  937. /* This basically follows the spec line by line -- see RFC1112 */
  938. struct igmphdr *ih;
  939. struct net_device *dev = skb->dev;
  940. struct in_device *in_dev;
  941. int len = skb->len;
  942. bool dropped = true;
  943. if (netif_is_l3_master(dev)) {
  944. dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif);
  945. if (!dev)
  946. goto drop;
  947. }
  948. in_dev = __in_dev_get_rcu(dev);
  949. if (!in_dev)
  950. goto drop;
  951. if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
  952. goto drop;
  953. if (skb_checksum_simple_validate(skb))
  954. goto drop;
  955. ih = igmp_hdr(skb);
  956. switch (ih->type) {
  957. case IGMP_HOST_MEMBERSHIP_QUERY:
  958. dropped = igmp_heard_query(in_dev, skb, len);
  959. break;
  960. case IGMP_HOST_MEMBERSHIP_REPORT:
  961. case IGMPV2_HOST_MEMBERSHIP_REPORT:
  962. /* Is it our report looped back? */
  963. if (rt_is_output_route(skb_rtable(skb)))
  964. break;
  965. /* don't rely on MC router hearing unicast reports */
  966. if (skb->pkt_type == PACKET_MULTICAST ||
  967. skb->pkt_type == PACKET_BROADCAST)
  968. dropped = igmp_heard_report(in_dev, ih->group);
  969. break;
  970. case IGMP_PIM:
  971. #ifdef CONFIG_IP_PIMSM_V1
  972. return pim_rcv_v1(skb);
  973. #endif
  974. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  975. case IGMP_DVMRP:
  976. case IGMP_TRACE:
  977. case IGMP_HOST_LEAVE_MESSAGE:
  978. case IGMP_MTRACE:
  979. case IGMP_MTRACE_RESP:
  980. break;
  981. default:
  982. break;
  983. }
  984. drop:
  985. if (dropped)
  986. kfree_skb(skb);
  987. else
  988. consume_skb(skb);
  989. return 0;
  990. }
  991. #endif
  992. /*
  993. * Add a filter to a device
  994. */
  995. static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
  996. {
  997. char buf[MAX_ADDR_LEN];
  998. struct net_device *dev = in_dev->dev;
  999. /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
  1000. We will get multicast token leakage, when IFF_MULTICAST
  1001. is changed. This check should be done in ndo_set_rx_mode
  1002. routine. Something sort of:
  1003. if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
  1004. --ANK
  1005. */
  1006. if (arp_mc_map(addr, buf, dev, 0) == 0)
  1007. dev_mc_add(dev, buf);
  1008. }
  1009. /*
  1010. * Remove a filter from a device
  1011. */
  1012. static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
  1013. {
  1014. char buf[MAX_ADDR_LEN];
  1015. struct net_device *dev = in_dev->dev;
  1016. if (arp_mc_map(addr, buf, dev, 0) == 0)
  1017. dev_mc_del(dev, buf);
  1018. }
  1019. #ifdef CONFIG_IP_MULTICAST
  1020. /*
  1021. * deleted ip_mc_list manipulation
  1022. */
  1023. static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
  1024. {
  1025. struct ip_mc_list *pmc;
  1026. struct net *net = dev_net(in_dev->dev);
  1027. /* this is an "ip_mc_list" for convenience; only the fields below
  1028. * are actually used. In particular, the refcnt and users are not
  1029. * used for management of the delete list. Using the same structure
  1030. * for deleted items allows change reports to use common code with
  1031. * non-deleted or query-response MCA's.
  1032. */
  1033. pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
  1034. if (!pmc)
  1035. return;
  1036. spin_lock_init(&pmc->lock);
  1037. spin_lock_bh(&im->lock);
  1038. pmc->interface = im->interface;
  1039. in_dev_hold(in_dev);
  1040. pmc->multiaddr = im->multiaddr;
  1041. pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
  1042. pmc->sfmode = im->sfmode;
  1043. if (pmc->sfmode == MCAST_INCLUDE) {
  1044. struct ip_sf_list *psf;
  1045. pmc->tomb = im->tomb;
  1046. pmc->sources = im->sources;
  1047. im->tomb = im->sources = NULL;
  1048. for (psf = pmc->sources; psf; psf = psf->sf_next)
  1049. psf->sf_crcount = pmc->crcount;
  1050. }
  1051. spin_unlock_bh(&im->lock);
  1052. spin_lock_bh(&in_dev->mc_tomb_lock);
  1053. pmc->next = in_dev->mc_tomb;
  1054. in_dev->mc_tomb = pmc;
  1055. spin_unlock_bh(&in_dev->mc_tomb_lock);
  1056. }
  1057. /*
  1058. * restore ip_mc_list deleted records
  1059. */
  1060. static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
  1061. {
  1062. struct ip_mc_list *pmc, *pmc_prev;
  1063. struct ip_sf_list *psf;
  1064. struct net *net = dev_net(in_dev->dev);
  1065. __be32 multiaddr = im->multiaddr;
  1066. spin_lock_bh(&in_dev->mc_tomb_lock);
  1067. pmc_prev = NULL;
  1068. for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
  1069. if (pmc->multiaddr == multiaddr)
  1070. break;
  1071. pmc_prev = pmc;
  1072. }
  1073. if (pmc) {
  1074. if (pmc_prev)
  1075. pmc_prev->next = pmc->next;
  1076. else
  1077. in_dev->mc_tomb = pmc->next;
  1078. }
  1079. spin_unlock_bh(&in_dev->mc_tomb_lock);
  1080. spin_lock_bh(&im->lock);
  1081. if (pmc) {
  1082. im->interface = pmc->interface;
  1083. if (im->sfmode == MCAST_INCLUDE) {
  1084. swap(im->tomb, pmc->tomb);
  1085. swap(im->sources, pmc->sources);
  1086. for (psf = im->sources; psf; psf = psf->sf_next)
  1087. psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
  1088. } else {
  1089. im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
  1090. }
  1091. in_dev_put(pmc->interface);
  1092. kfree_pmc(pmc);
  1093. }
  1094. spin_unlock_bh(&im->lock);
  1095. }
  1096. /*
  1097. * flush ip_mc_list deleted records
  1098. */
  1099. static void igmpv3_clear_delrec(struct in_device *in_dev)
  1100. {
  1101. struct ip_mc_list *pmc, *nextpmc;
  1102. spin_lock_bh(&in_dev->mc_tomb_lock);
  1103. pmc = in_dev->mc_tomb;
  1104. in_dev->mc_tomb = NULL;
  1105. spin_unlock_bh(&in_dev->mc_tomb_lock);
  1106. for (; pmc; pmc = nextpmc) {
  1107. nextpmc = pmc->next;
  1108. ip_mc_clear_src(pmc);
  1109. in_dev_put(pmc->interface);
  1110. kfree_pmc(pmc);
  1111. }
  1112. /* clear dead sources, too */
  1113. rcu_read_lock();
  1114. for_each_pmc_rcu(in_dev, pmc) {
  1115. struct ip_sf_list *psf;
  1116. spin_lock_bh(&pmc->lock);
  1117. psf = pmc->tomb;
  1118. pmc->tomb = NULL;
  1119. spin_unlock_bh(&pmc->lock);
  1120. ip_sf_list_clear_all(psf);
  1121. }
  1122. rcu_read_unlock();
  1123. }
  1124. #endif
  1125. static void igmp_group_dropped(struct ip_mc_list *im)
  1126. {
  1127. struct in_device *in_dev = im->interface;
  1128. #ifdef CONFIG_IP_MULTICAST
  1129. struct net *net = dev_net(in_dev->dev);
  1130. int reporter;
  1131. #endif
  1132. if (im->loaded) {
  1133. im->loaded = 0;
  1134. ip_mc_filter_del(in_dev, im->multiaddr);
  1135. }
  1136. #ifdef CONFIG_IP_MULTICAST
  1137. if (im->multiaddr == IGMP_ALL_HOSTS)
  1138. return;
  1139. if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
  1140. return;
  1141. reporter = im->reporter;
  1142. igmp_stop_timer(im);
  1143. if (!in_dev->dead) {
  1144. if (IGMP_V1_SEEN(in_dev))
  1145. return;
  1146. if (IGMP_V2_SEEN(in_dev)) {
  1147. if (reporter)
  1148. igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
  1149. return;
  1150. }
  1151. /* IGMPv3 */
  1152. igmpv3_add_delrec(in_dev, im);
  1153. igmp_ifc_event(in_dev);
  1154. }
  1155. #endif
  1156. }
  1157. static void igmp_group_added(struct ip_mc_list *im)
  1158. {
  1159. struct in_device *in_dev = im->interface;
  1160. #ifdef CONFIG_IP_MULTICAST
  1161. struct net *net = dev_net(in_dev->dev);
  1162. #endif
  1163. if (im->loaded == 0) {
  1164. im->loaded = 1;
  1165. ip_mc_filter_add(in_dev, im->multiaddr);
  1166. }
  1167. #ifdef CONFIG_IP_MULTICAST
  1168. if (im->multiaddr == IGMP_ALL_HOSTS)
  1169. return;
  1170. if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
  1171. return;
  1172. if (in_dev->dead)
  1173. return;
  1174. im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
  1175. if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
  1176. spin_lock_bh(&im->lock);
  1177. igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
  1178. spin_unlock_bh(&im->lock);
  1179. return;
  1180. }
  1181. /* else, v3 */
  1182. /* Based on RFC3376 5.1, for newly added INCLUDE SSM, we should
  1183. * not send filter-mode change record as the mode should be from
  1184. * IN() to IN(A).
  1185. */
  1186. if (im->sfmode == MCAST_EXCLUDE)
  1187. im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
  1188. igmp_ifc_event(in_dev);
  1189. #endif
  1190. }
  1191. /*
  1192. * Multicast list managers
  1193. */
  1194. static u32 ip_mc_hash(const struct ip_mc_list *im)
  1195. {
  1196. return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
  1197. }
  1198. static void ip_mc_hash_add(struct in_device *in_dev,
  1199. struct ip_mc_list *im)
  1200. {
  1201. struct ip_mc_list __rcu **mc_hash;
  1202. u32 hash;
  1203. mc_hash = rtnl_dereference(in_dev->mc_hash);
  1204. if (mc_hash) {
  1205. hash = ip_mc_hash(im);
  1206. im->next_hash = mc_hash[hash];
  1207. rcu_assign_pointer(mc_hash[hash], im);
  1208. return;
  1209. }
  1210. /* do not use a hash table for small number of items */
  1211. if (in_dev->mc_count < 4)
  1212. return;
  1213. mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
  1214. GFP_KERNEL);
  1215. if (!mc_hash)
  1216. return;
  1217. for_each_pmc_rtnl(in_dev, im) {
  1218. hash = ip_mc_hash(im);
  1219. im->next_hash = mc_hash[hash];
  1220. RCU_INIT_POINTER(mc_hash[hash], im);
  1221. }
  1222. rcu_assign_pointer(in_dev->mc_hash, mc_hash);
  1223. }
  1224. static void ip_mc_hash_remove(struct in_device *in_dev,
  1225. struct ip_mc_list *im)
  1226. {
  1227. struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
  1228. struct ip_mc_list *aux;
  1229. if (!mc_hash)
  1230. return;
  1231. mc_hash += ip_mc_hash(im);
  1232. while ((aux = rtnl_dereference(*mc_hash)) != im)
  1233. mc_hash = &aux->next_hash;
  1234. *mc_hash = im->next_hash;
  1235. }
  1236. /*
  1237. * A socket has joined a multicast group on device dev.
  1238. */
  1239. static void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
  1240. unsigned int mode)
  1241. {
  1242. struct ip_mc_list *im;
  1243. ASSERT_RTNL();
  1244. for_each_pmc_rtnl(in_dev, im) {
  1245. if (im->multiaddr == addr) {
  1246. im->users++;
  1247. ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0);
  1248. goto out;
  1249. }
  1250. }
  1251. im = kzalloc(sizeof(*im), GFP_KERNEL);
  1252. if (!im)
  1253. goto out;
  1254. im->users = 1;
  1255. im->interface = in_dev;
  1256. in_dev_hold(in_dev);
  1257. im->multiaddr = addr;
  1258. /* initial mode is (EX, empty) */
  1259. im->sfmode = mode;
  1260. im->sfcount[mode] = 1;
  1261. refcount_set(&im->refcnt, 1);
  1262. spin_lock_init(&im->lock);
  1263. #ifdef CONFIG_IP_MULTICAST
  1264. timer_setup(&im->timer, igmp_timer_expire, 0);
  1265. #endif
  1266. im->next_rcu = in_dev->mc_list;
  1267. in_dev->mc_count++;
  1268. rcu_assign_pointer(in_dev->mc_list, im);
  1269. ip_mc_hash_add(in_dev, im);
  1270. #ifdef CONFIG_IP_MULTICAST
  1271. igmpv3_del_delrec(in_dev, im);
  1272. #endif
  1273. igmp_group_added(im);
  1274. if (!in_dev->dead)
  1275. ip_rt_multicast_event(in_dev);
  1276. out:
  1277. return;
  1278. }
  1279. void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
  1280. {
  1281. __ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE);
  1282. }
  1283. EXPORT_SYMBOL(ip_mc_inc_group);
  1284. static int ip_mc_check_iphdr(struct sk_buff *skb)
  1285. {
  1286. const struct iphdr *iph;
  1287. unsigned int len;
  1288. unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
  1289. if (!pskb_may_pull(skb, offset))
  1290. return -EINVAL;
  1291. iph = ip_hdr(skb);
  1292. if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
  1293. return -EINVAL;
  1294. offset += ip_hdrlen(skb) - sizeof(*iph);
  1295. if (!pskb_may_pull(skb, offset))
  1296. return -EINVAL;
  1297. iph = ip_hdr(skb);
  1298. if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
  1299. return -EINVAL;
  1300. len = skb_network_offset(skb) + ntohs(iph->tot_len);
  1301. if (skb->len < len || len < offset)
  1302. return -EINVAL;
  1303. skb_set_transport_header(skb, offset);
  1304. return 0;
  1305. }
  1306. static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
  1307. {
  1308. unsigned int len = skb_transport_offset(skb);
  1309. len += sizeof(struct igmpv3_report);
  1310. return pskb_may_pull(skb, len) ? 0 : -EINVAL;
  1311. }
  1312. static int ip_mc_check_igmp_query(struct sk_buff *skb)
  1313. {
  1314. unsigned int len = skb_transport_offset(skb);
  1315. len += sizeof(struct igmphdr);
  1316. if (skb->len < len)
  1317. return -EINVAL;
  1318. /* IGMPv{1,2}? */
  1319. if (skb->len != len) {
  1320. /* or IGMPv3? */
  1321. len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
  1322. if (skb->len < len || !pskb_may_pull(skb, len))
  1323. return -EINVAL;
  1324. }
  1325. /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
  1326. * all-systems destination addresses (224.0.0.1) for general queries
  1327. */
  1328. if (!igmp_hdr(skb)->group &&
  1329. ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
  1330. return -EINVAL;
  1331. return 0;
  1332. }
  1333. static int ip_mc_check_igmp_msg(struct sk_buff *skb)
  1334. {
  1335. switch (igmp_hdr(skb)->type) {
  1336. case IGMP_HOST_LEAVE_MESSAGE:
  1337. case IGMP_HOST_MEMBERSHIP_REPORT:
  1338. case IGMPV2_HOST_MEMBERSHIP_REPORT:
  1339. /* fall through */
  1340. return 0;
  1341. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  1342. return ip_mc_check_igmp_reportv3(skb);
  1343. case IGMP_HOST_MEMBERSHIP_QUERY:
  1344. return ip_mc_check_igmp_query(skb);
  1345. default:
  1346. return -ENOMSG;
  1347. }
  1348. }
  1349. static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
  1350. {
  1351. return skb_checksum_simple_validate(skb);
  1352. }
  1353. static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
  1354. {
  1355. struct sk_buff *skb_chk;
  1356. unsigned int transport_len;
  1357. unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
  1358. int ret = -EINVAL;
  1359. transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
  1360. skb_chk = skb_checksum_trimmed(skb, transport_len,
  1361. ip_mc_validate_checksum);
  1362. if (!skb_chk)
  1363. goto err;
  1364. if (!pskb_may_pull(skb_chk, len))
  1365. goto err;
  1366. ret = ip_mc_check_igmp_msg(skb_chk);
  1367. if (ret)
  1368. goto err;
  1369. if (skb_trimmed)
  1370. *skb_trimmed = skb_chk;
  1371. /* free now unneeded clone */
  1372. else if (skb_chk != skb)
  1373. kfree_skb(skb_chk);
  1374. ret = 0;
  1375. err:
  1376. if (ret && skb_chk && skb_chk != skb)
  1377. kfree_skb(skb_chk);
  1378. return ret;
  1379. }
  1380. /**
  1381. * ip_mc_check_igmp - checks whether this is a sane IGMP packet
  1382. * @skb: the skb to validate
  1383. * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
  1384. *
  1385. * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
  1386. * skb transport header accordingly and returns zero.
  1387. *
  1388. * -EINVAL: A broken packet was detected, i.e. it violates some internet
  1389. * standard
  1390. * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
  1391. * -ENOMEM: A memory allocation failure happened.
  1392. *
  1393. * Optionally, an skb pointer might be provided via skb_trimmed (or set it
  1394. * to NULL): After parsing an IGMP packet successfully it will point to
  1395. * an skb which has its tail aligned to the IP packet end. This might
  1396. * either be the originally provided skb or a trimmed, cloned version if
  1397. * the skb frame had data beyond the IP packet. A cloned skb allows us
  1398. * to leave the original skb and its full frame unchanged (which might be
  1399. * desirable for layer 2 frame jugglers).
  1400. *
  1401. * Caller needs to set the skb network header and free any returned skb if it
  1402. * differs from the provided skb.
  1403. */
  1404. int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
  1405. {
  1406. int ret = ip_mc_check_iphdr(skb);
  1407. if (ret < 0)
  1408. return ret;
  1409. if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
  1410. return -ENOMSG;
  1411. return __ip_mc_check_igmp(skb, skb_trimmed);
  1412. }
  1413. EXPORT_SYMBOL(ip_mc_check_igmp);
  1414. /*
  1415. * Resend IGMP JOIN report; used by netdev notifier.
  1416. */
  1417. static void ip_mc_rejoin_groups(struct in_device *in_dev)
  1418. {
  1419. #ifdef CONFIG_IP_MULTICAST
  1420. struct ip_mc_list *im;
  1421. int type;
  1422. struct net *net = dev_net(in_dev->dev);
  1423. ASSERT_RTNL();
  1424. for_each_pmc_rtnl(in_dev, im) {
  1425. if (im->multiaddr == IGMP_ALL_HOSTS)
  1426. continue;
  1427. if (ipv4_is_local_multicast(im->multiaddr) &&
  1428. !net->ipv4.sysctl_igmp_llm_reports)
  1429. continue;
  1430. /* a failover is happening and switches
  1431. * must be notified immediately
  1432. */
  1433. if (IGMP_V1_SEEN(in_dev))
  1434. type = IGMP_HOST_MEMBERSHIP_REPORT;
  1435. else if (IGMP_V2_SEEN(in_dev))
  1436. type = IGMPV2_HOST_MEMBERSHIP_REPORT;
  1437. else
  1438. type = IGMPV3_HOST_MEMBERSHIP_REPORT;
  1439. igmp_send_report(in_dev, im, type);
  1440. }
  1441. #endif
  1442. }
  1443. /*
  1444. * A socket has left a multicast group on device dev
  1445. */
  1446. void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
  1447. {
  1448. struct ip_mc_list *i;
  1449. struct ip_mc_list __rcu **ip;
  1450. ASSERT_RTNL();
  1451. for (ip = &in_dev->mc_list;
  1452. (i = rtnl_dereference(*ip)) != NULL;
  1453. ip = &i->next_rcu) {
  1454. if (i->multiaddr == addr) {
  1455. if (--i->users == 0) {
  1456. ip_mc_hash_remove(in_dev, i);
  1457. *ip = i->next_rcu;
  1458. in_dev->mc_count--;
  1459. igmp_group_dropped(i);
  1460. ip_mc_clear_src(i);
  1461. if (!in_dev->dead)
  1462. ip_rt_multicast_event(in_dev);
  1463. ip_ma_put(i);
  1464. return;
  1465. }
  1466. break;
  1467. }
  1468. }
  1469. }
  1470. EXPORT_SYMBOL(ip_mc_dec_group);
  1471. /* Device changing type */
  1472. void ip_mc_unmap(struct in_device *in_dev)
  1473. {
  1474. struct ip_mc_list *pmc;
  1475. ASSERT_RTNL();
  1476. for_each_pmc_rtnl(in_dev, pmc)
  1477. igmp_group_dropped(pmc);
  1478. }
  1479. void ip_mc_remap(struct in_device *in_dev)
  1480. {
  1481. struct ip_mc_list *pmc;
  1482. ASSERT_RTNL();
  1483. for_each_pmc_rtnl(in_dev, pmc) {
  1484. #ifdef CONFIG_IP_MULTICAST
  1485. igmpv3_del_delrec(in_dev, pmc);
  1486. #endif
  1487. igmp_group_added(pmc);
  1488. }
  1489. }
  1490. /* Device going down */
  1491. void ip_mc_down(struct in_device *in_dev)
  1492. {
  1493. struct ip_mc_list *pmc;
  1494. ASSERT_RTNL();
  1495. for_each_pmc_rtnl(in_dev, pmc)
  1496. igmp_group_dropped(pmc);
  1497. #ifdef CONFIG_IP_MULTICAST
  1498. in_dev->mr_ifc_count = 0;
  1499. if (del_timer(&in_dev->mr_ifc_timer))
  1500. __in_dev_put(in_dev);
  1501. in_dev->mr_gq_running = 0;
  1502. if (del_timer(&in_dev->mr_gq_timer))
  1503. __in_dev_put(in_dev);
  1504. #endif
  1505. ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
  1506. }
  1507. #ifdef CONFIG_IP_MULTICAST
  1508. static void ip_mc_reset(struct in_device *in_dev)
  1509. {
  1510. struct net *net = dev_net(in_dev->dev);
  1511. in_dev->mr_qi = IGMP_QUERY_INTERVAL;
  1512. in_dev->mr_qri = IGMP_QUERY_RESPONSE_INTERVAL;
  1513. in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
  1514. }
  1515. #else
  1516. static void ip_mc_reset(struct in_device *in_dev)
  1517. {
  1518. }
  1519. #endif
  1520. void ip_mc_init_dev(struct in_device *in_dev)
  1521. {
  1522. ASSERT_RTNL();
  1523. #ifdef CONFIG_IP_MULTICAST
  1524. timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0);
  1525. timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0);
  1526. #endif
  1527. ip_mc_reset(in_dev);
  1528. spin_lock_init(&in_dev->mc_tomb_lock);
  1529. }
  1530. /* Device going up */
  1531. void ip_mc_up(struct in_device *in_dev)
  1532. {
  1533. struct ip_mc_list *pmc;
  1534. ASSERT_RTNL();
  1535. ip_mc_reset(in_dev);
  1536. ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
  1537. for_each_pmc_rtnl(in_dev, pmc) {
  1538. #ifdef CONFIG_IP_MULTICAST
  1539. igmpv3_del_delrec(in_dev, pmc);
  1540. #endif
  1541. igmp_group_added(pmc);
  1542. }
  1543. }
  1544. /*
  1545. * Device is about to be destroyed: clean up.
  1546. */
  1547. void ip_mc_destroy_dev(struct in_device *in_dev)
  1548. {
  1549. struct ip_mc_list *i;
  1550. ASSERT_RTNL();
  1551. /* Deactivate timers */
  1552. ip_mc_down(in_dev);
  1553. #ifdef CONFIG_IP_MULTICAST
  1554. igmpv3_clear_delrec(in_dev);
  1555. #endif
  1556. while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
  1557. in_dev->mc_list = i->next_rcu;
  1558. in_dev->mc_count--;
  1559. ip_ma_put(i);
  1560. }
  1561. }
  1562. /* RTNL is locked */
  1563. static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
  1564. {
  1565. struct net_device *dev = NULL;
  1566. struct in_device *idev = NULL;
  1567. if (imr->imr_ifindex) {
  1568. idev = inetdev_by_index(net, imr->imr_ifindex);
  1569. return idev;
  1570. }
  1571. if (imr->imr_address.s_addr) {
  1572. dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
  1573. if (!dev)
  1574. return NULL;
  1575. }
  1576. if (!dev) {
  1577. struct rtable *rt = ip_route_output(net,
  1578. imr->imr_multiaddr.s_addr,
  1579. 0, 0, 0);
  1580. if (!IS_ERR(rt)) {
  1581. dev = rt->dst.dev;
  1582. ip_rt_put(rt);
  1583. }
  1584. }
  1585. if (dev) {
  1586. imr->imr_ifindex = dev->ifindex;
  1587. idev = __in_dev_get_rtnl(dev);
  1588. }
  1589. return idev;
  1590. }
  1591. /*
  1592. * Join a socket to a group
  1593. */
  1594. static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
  1595. __be32 *psfsrc)
  1596. {
  1597. struct ip_sf_list *psf, *psf_prev;
  1598. int rv = 0;
  1599. psf_prev = NULL;
  1600. for (psf = pmc->sources; psf; psf = psf->sf_next) {
  1601. if (psf->sf_inaddr == *psfsrc)
  1602. break;
  1603. psf_prev = psf;
  1604. }
  1605. if (!psf || psf->sf_count[sfmode] == 0) {
  1606. /* source filter not found, or count wrong => bug */
  1607. return -ESRCH;
  1608. }
  1609. psf->sf_count[sfmode]--;
  1610. if (psf->sf_count[sfmode] == 0) {
  1611. ip_rt_multicast_event(pmc->interface);
  1612. }
  1613. if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
  1614. #ifdef CONFIG_IP_MULTICAST
  1615. struct in_device *in_dev = pmc->interface;
  1616. struct net *net = dev_net(in_dev->dev);
  1617. #endif
  1618. /* no more filters for this source */
  1619. if (psf_prev)
  1620. psf_prev->sf_next = psf->sf_next;
  1621. else
  1622. pmc->sources = psf->sf_next;
  1623. #ifdef CONFIG_IP_MULTICAST
  1624. if (psf->sf_oldin &&
  1625. !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
  1626. psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
  1627. psf->sf_next = pmc->tomb;
  1628. pmc->tomb = psf;
  1629. rv = 1;
  1630. } else
  1631. #endif
  1632. kfree(psf);
  1633. }
  1634. return rv;
  1635. }
  1636. #ifndef CONFIG_IP_MULTICAST
  1637. #define igmp_ifc_event(x) do { } while (0)
  1638. #endif
  1639. static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
  1640. int sfcount, __be32 *psfsrc, int delta)
  1641. {
  1642. struct ip_mc_list *pmc;
  1643. int changerec = 0;
  1644. int i, err;
  1645. if (!in_dev)
  1646. return -ENODEV;
  1647. rcu_read_lock();
  1648. for_each_pmc_rcu(in_dev, pmc) {
  1649. if (*pmca == pmc->multiaddr)
  1650. break;
  1651. }
  1652. if (!pmc) {
  1653. /* MCA not found?? bug */
  1654. rcu_read_unlock();
  1655. return -ESRCH;
  1656. }
  1657. spin_lock_bh(&pmc->lock);
  1658. rcu_read_unlock();
  1659. #ifdef CONFIG_IP_MULTICAST
  1660. sf_markstate(pmc);
  1661. #endif
  1662. if (!delta) {
  1663. err = -EINVAL;
  1664. if (!pmc->sfcount[sfmode])
  1665. goto out_unlock;
  1666. pmc->sfcount[sfmode]--;
  1667. }
  1668. err = 0;
  1669. for (i = 0; i < sfcount; i++) {
  1670. int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1671. changerec |= rv > 0;
  1672. if (!err && rv < 0)
  1673. err = rv;
  1674. }
  1675. if (pmc->sfmode == MCAST_EXCLUDE &&
  1676. pmc->sfcount[MCAST_EXCLUDE] == 0 &&
  1677. pmc->sfcount[MCAST_INCLUDE]) {
  1678. #ifdef CONFIG_IP_MULTICAST
  1679. struct ip_sf_list *psf;
  1680. struct net *net = dev_net(in_dev->dev);
  1681. #endif
  1682. /* filter mode change */
  1683. pmc->sfmode = MCAST_INCLUDE;
  1684. #ifdef CONFIG_IP_MULTICAST
  1685. pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
  1686. in_dev->mr_ifc_count = pmc->crcount;
  1687. for (psf = pmc->sources; psf; psf = psf->sf_next)
  1688. psf->sf_crcount = 0;
  1689. igmp_ifc_event(pmc->interface);
  1690. } else if (sf_setstate(pmc) || changerec) {
  1691. igmp_ifc_event(pmc->interface);
  1692. #endif
  1693. }
  1694. out_unlock:
  1695. spin_unlock_bh(&pmc->lock);
  1696. return err;
  1697. }
  1698. /*
  1699. * Add multicast single-source filter to the interface list
  1700. */
  1701. static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
  1702. __be32 *psfsrc)
  1703. {
  1704. struct ip_sf_list *psf, *psf_prev;
  1705. psf_prev = NULL;
  1706. for (psf = pmc->sources; psf; psf = psf->sf_next) {
  1707. if (psf->sf_inaddr == *psfsrc)
  1708. break;
  1709. psf_prev = psf;
  1710. }
  1711. if (!psf) {
  1712. psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
  1713. if (!psf)
  1714. return -ENOBUFS;
  1715. psf->sf_inaddr = *psfsrc;
  1716. if (psf_prev) {
  1717. psf_prev->sf_next = psf;
  1718. } else
  1719. pmc->sources = psf;
  1720. }
  1721. psf->sf_count[sfmode]++;
  1722. if (psf->sf_count[sfmode] == 1) {
  1723. ip_rt_multicast_event(pmc->interface);
  1724. }
  1725. return 0;
  1726. }
  1727. #ifdef CONFIG_IP_MULTICAST
  1728. static void sf_markstate(struct ip_mc_list *pmc)
  1729. {
  1730. struct ip_sf_list *psf;
  1731. int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
  1732. for (psf = pmc->sources; psf; psf = psf->sf_next)
  1733. if (pmc->sfcount[MCAST_EXCLUDE]) {
  1734. psf->sf_oldin = mca_xcount ==
  1735. psf->sf_count[MCAST_EXCLUDE] &&
  1736. !psf->sf_count[MCAST_INCLUDE];
  1737. } else
  1738. psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
  1739. }
  1740. static int sf_setstate(struct ip_mc_list *pmc)
  1741. {
  1742. struct ip_sf_list *psf, *dpsf;
  1743. int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
  1744. int qrv = pmc->interface->mr_qrv;
  1745. int new_in, rv;
  1746. rv = 0;
  1747. for (psf = pmc->sources; psf; psf = psf->sf_next) {
  1748. if (pmc->sfcount[MCAST_EXCLUDE]) {
  1749. new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
  1750. !psf->sf_count[MCAST_INCLUDE];
  1751. } else
  1752. new_in = psf->sf_count[MCAST_INCLUDE] != 0;
  1753. if (new_in) {
  1754. if (!psf->sf_oldin) {
  1755. struct ip_sf_list *prev = NULL;
  1756. for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
  1757. if (dpsf->sf_inaddr == psf->sf_inaddr)
  1758. break;
  1759. prev = dpsf;
  1760. }
  1761. if (dpsf) {
  1762. if (prev)
  1763. prev->sf_next = dpsf->sf_next;
  1764. else
  1765. pmc->tomb = dpsf->sf_next;
  1766. kfree(dpsf);
  1767. }
  1768. psf->sf_crcount = qrv;
  1769. rv++;
  1770. }
  1771. } else if (psf->sf_oldin) {
  1772. psf->sf_crcount = 0;
  1773. /*
  1774. * add or update "delete" records if an active filter
  1775. * is now inactive
  1776. */
  1777. for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
  1778. if (dpsf->sf_inaddr == psf->sf_inaddr)
  1779. break;
  1780. if (!dpsf) {
  1781. dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
  1782. if (!dpsf)
  1783. continue;
  1784. *dpsf = *psf;
  1785. /* pmc->lock held by callers */
  1786. dpsf->sf_next = pmc->tomb;
  1787. pmc->tomb = dpsf;
  1788. }
  1789. dpsf->sf_crcount = qrv;
  1790. rv++;
  1791. }
  1792. }
  1793. return rv;
  1794. }
  1795. #endif
  1796. /*
  1797. * Add multicast source filter list to the interface list
  1798. */
  1799. static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
  1800. int sfcount, __be32 *psfsrc, int delta)
  1801. {
  1802. struct ip_mc_list *pmc;
  1803. int isexclude;
  1804. int i, err;
  1805. if (!in_dev)
  1806. return -ENODEV;
  1807. rcu_read_lock();
  1808. for_each_pmc_rcu(in_dev, pmc) {
  1809. if (*pmca == pmc->multiaddr)
  1810. break;
  1811. }
  1812. if (!pmc) {
  1813. /* MCA not found?? bug */
  1814. rcu_read_unlock();
  1815. return -ESRCH;
  1816. }
  1817. spin_lock_bh(&pmc->lock);
  1818. rcu_read_unlock();
  1819. #ifdef CONFIG_IP_MULTICAST
  1820. sf_markstate(pmc);
  1821. #endif
  1822. isexclude = pmc->sfmode == MCAST_EXCLUDE;
  1823. if (!delta)
  1824. pmc->sfcount[sfmode]++;
  1825. err = 0;
  1826. for (i = 0; i < sfcount; i++) {
  1827. err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
  1828. if (err)
  1829. break;
  1830. }
  1831. if (err) {
  1832. int j;
  1833. if (!delta)
  1834. pmc->sfcount[sfmode]--;
  1835. for (j = 0; j < i; j++)
  1836. (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
  1837. } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
  1838. #ifdef CONFIG_IP_MULTICAST
  1839. struct ip_sf_list *psf;
  1840. struct net *net = dev_net(pmc->interface->dev);
  1841. in_dev = pmc->interface;
  1842. #endif
  1843. /* filter mode change */
  1844. if (pmc->sfcount[MCAST_EXCLUDE])
  1845. pmc->sfmode = MCAST_EXCLUDE;
  1846. else if (pmc->sfcount[MCAST_INCLUDE])
  1847. pmc->sfmode = MCAST_INCLUDE;
  1848. #ifdef CONFIG_IP_MULTICAST
  1849. /* else no filters; keep old mode for reports */
  1850. pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
  1851. in_dev->mr_ifc_count = pmc->crcount;
  1852. for (psf = pmc->sources; psf; psf = psf->sf_next)
  1853. psf->sf_crcount = 0;
  1854. igmp_ifc_event(in_dev);
  1855. } else if (sf_setstate(pmc)) {
  1856. igmp_ifc_event(in_dev);
  1857. #endif
  1858. }
  1859. spin_unlock_bh(&pmc->lock);
  1860. return err;
  1861. }
  1862. static void ip_mc_clear_src(struct ip_mc_list *pmc)
  1863. {
  1864. struct ip_sf_list *tomb, *sources;
  1865. spin_lock_bh(&pmc->lock);
  1866. tomb = pmc->tomb;
  1867. pmc->tomb = NULL;
  1868. sources = pmc->sources;
  1869. pmc->sources = NULL;
  1870. pmc->sfmode = MCAST_EXCLUDE;
  1871. pmc->sfcount[MCAST_INCLUDE] = 0;
  1872. pmc->sfcount[MCAST_EXCLUDE] = 1;
  1873. spin_unlock_bh(&pmc->lock);
  1874. ip_sf_list_clear_all(tomb);
  1875. ip_sf_list_clear_all(sources);
  1876. }
  1877. /* Join a multicast group
  1878. */
  1879. static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr,
  1880. unsigned int mode)
  1881. {
  1882. __be32 addr = imr->imr_multiaddr.s_addr;
  1883. struct ip_mc_socklist *iml, *i;
  1884. struct in_device *in_dev;
  1885. struct inet_sock *inet = inet_sk(sk);
  1886. struct net *net = sock_net(sk);
  1887. int ifindex;
  1888. int count = 0;
  1889. int err;
  1890. ASSERT_RTNL();
  1891. if (!ipv4_is_multicast(addr))
  1892. return -EINVAL;
  1893. in_dev = ip_mc_find_dev(net, imr);
  1894. if (!in_dev) {
  1895. err = -ENODEV;
  1896. goto done;
  1897. }
  1898. err = -EADDRINUSE;
  1899. ifindex = imr->imr_ifindex;
  1900. for_each_pmc_rtnl(inet, i) {
  1901. if (i->multi.imr_multiaddr.s_addr == addr &&
  1902. i->multi.imr_ifindex == ifindex)
  1903. goto done;
  1904. count++;
  1905. }
  1906. err = -ENOBUFS;
  1907. if (count >= net->ipv4.sysctl_igmp_max_memberships)
  1908. goto done;
  1909. iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
  1910. if (!iml)
  1911. goto done;
  1912. memcpy(&iml->multi, imr, sizeof(*imr));
  1913. iml->next_rcu = inet->mc_list;
  1914. iml->sflist = NULL;
  1915. iml->sfmode = mode;
  1916. rcu_assign_pointer(inet->mc_list, iml);
  1917. __ip_mc_inc_group(in_dev, addr, mode);
  1918. err = 0;
  1919. done:
  1920. return err;
  1921. }
  1922. /* Join ASM (Any-Source Multicast) group
  1923. */
  1924. int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
  1925. {
  1926. return __ip_mc_join_group(sk, imr, MCAST_EXCLUDE);
  1927. }
  1928. EXPORT_SYMBOL(ip_mc_join_group);
  1929. /* Join SSM (Source-Specific Multicast) group
  1930. */
  1931. int ip_mc_join_group_ssm(struct sock *sk, struct ip_mreqn *imr,
  1932. unsigned int mode)
  1933. {
  1934. return __ip_mc_join_group(sk, imr, mode);
  1935. }
  1936. static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
  1937. struct in_device *in_dev)
  1938. {
  1939. struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
  1940. int err;
  1941. if (!psf) {
  1942. /* any-source empty exclude case */
  1943. return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
  1944. iml->sfmode, 0, NULL, 0);
  1945. }
  1946. err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
  1947. iml->sfmode, psf->sl_count, psf->sl_addr, 0);
  1948. RCU_INIT_POINTER(iml->sflist, NULL);
  1949. /* decrease mem now to avoid the memleak warning */
  1950. atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
  1951. kfree_rcu(psf, rcu);
  1952. return err;
  1953. }
  1954. int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
  1955. {
  1956. struct inet_sock *inet = inet_sk(sk);
  1957. struct ip_mc_socklist *iml;
  1958. struct ip_mc_socklist __rcu **imlp;
  1959. struct in_device *in_dev;
  1960. struct net *net = sock_net(sk);
  1961. __be32 group = imr->imr_multiaddr.s_addr;
  1962. u32 ifindex;
  1963. int ret = -EADDRNOTAVAIL;
  1964. ASSERT_RTNL();
  1965. in_dev = ip_mc_find_dev(net, imr);
  1966. if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
  1967. ret = -ENODEV;
  1968. goto out;
  1969. }
  1970. ifindex = imr->imr_ifindex;
  1971. for (imlp = &inet->mc_list;
  1972. (iml = rtnl_dereference(*imlp)) != NULL;
  1973. imlp = &iml->next_rcu) {
  1974. if (iml->multi.imr_multiaddr.s_addr != group)
  1975. continue;
  1976. if (ifindex) {
  1977. if (iml->multi.imr_ifindex != ifindex)
  1978. continue;
  1979. } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
  1980. iml->multi.imr_address.s_addr)
  1981. continue;
  1982. (void) ip_mc_leave_src(sk, iml, in_dev);
  1983. *imlp = iml->next_rcu;
  1984. if (in_dev)
  1985. ip_mc_dec_group(in_dev, group);
  1986. /* decrease mem now to avoid the memleak warning */
  1987. atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
  1988. kfree_rcu(iml, rcu);
  1989. return 0;
  1990. }
  1991. out:
  1992. return ret;
  1993. }
  1994. EXPORT_SYMBOL(ip_mc_leave_group);
  1995. int ip_mc_source(int add, int omode, struct sock *sk, struct
  1996. ip_mreq_source *mreqs, int ifindex)
  1997. {
  1998. int err;
  1999. struct ip_mreqn imr;
  2000. __be32 addr = mreqs->imr_multiaddr;
  2001. struct ip_mc_socklist *pmc;
  2002. struct in_device *in_dev = NULL;
  2003. struct inet_sock *inet = inet_sk(sk);
  2004. struct ip_sf_socklist *psl;
  2005. struct net *net = sock_net(sk);
  2006. int leavegroup = 0;
  2007. int i, j, rv;
  2008. if (!ipv4_is_multicast(addr))
  2009. return -EINVAL;
  2010. ASSERT_RTNL();
  2011. imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
  2012. imr.imr_address.s_addr = mreqs->imr_interface;
  2013. imr.imr_ifindex = ifindex;
  2014. in_dev = ip_mc_find_dev(net, &imr);
  2015. if (!in_dev) {
  2016. err = -ENODEV;
  2017. goto done;
  2018. }
  2019. err = -EADDRNOTAVAIL;
  2020. for_each_pmc_rtnl(inet, pmc) {
  2021. if ((pmc->multi.imr_multiaddr.s_addr ==
  2022. imr.imr_multiaddr.s_addr) &&
  2023. (pmc->multi.imr_ifindex == imr.imr_ifindex))
  2024. break;
  2025. }
  2026. if (!pmc) { /* must have a prior join */
  2027. err = -EINVAL;
  2028. goto done;
  2029. }
  2030. /* if a source filter was set, must be the same mode as before */
  2031. if (pmc->sflist) {
  2032. if (pmc->sfmode != omode) {
  2033. err = -EINVAL;
  2034. goto done;
  2035. }
  2036. } else if (pmc->sfmode != omode) {
  2037. /* allow mode switches for empty-set filters */
  2038. ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
  2039. ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
  2040. NULL, 0);
  2041. pmc->sfmode = omode;
  2042. }
  2043. psl = rtnl_dereference(pmc->sflist);
  2044. if (!add) {
  2045. if (!psl)
  2046. goto done; /* err = -EADDRNOTAVAIL */
  2047. rv = !0;
  2048. for (i = 0; i < psl->sl_count; i++) {
  2049. rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
  2050. sizeof(__be32));
  2051. if (rv == 0)
  2052. break;
  2053. }
  2054. if (rv) /* source not found */
  2055. goto done; /* err = -EADDRNOTAVAIL */
  2056. /* special case - (INCLUDE, empty) == LEAVE_GROUP */
  2057. if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
  2058. leavegroup = 1;
  2059. goto done;
  2060. }
  2061. /* update the interface filter */
  2062. ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
  2063. &mreqs->imr_sourceaddr, 1);
  2064. for (j = i+1; j < psl->sl_count; j++)
  2065. psl->sl_addr[j-1] = psl->sl_addr[j];
  2066. psl->sl_count--;
  2067. err = 0;
  2068. goto done;
  2069. }
  2070. /* else, add a new source to the filter */
  2071. if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
  2072. err = -ENOBUFS;
  2073. goto done;
  2074. }
  2075. if (!psl || psl->sl_count == psl->sl_max) {
  2076. struct ip_sf_socklist *newpsl;
  2077. int count = IP_SFBLOCK;
  2078. if (psl)
  2079. count += psl->sl_max;
  2080. newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
  2081. if (!newpsl) {
  2082. err = -ENOBUFS;
  2083. goto done;
  2084. }
  2085. newpsl->sl_max = count;
  2086. newpsl->sl_count = count - IP_SFBLOCK;
  2087. if (psl) {
  2088. for (i = 0; i < psl->sl_count; i++)
  2089. newpsl->sl_addr[i] = psl->sl_addr[i];
  2090. /* decrease mem now to avoid the memleak warning */
  2091. atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
  2092. kfree_rcu(psl, rcu);
  2093. }
  2094. rcu_assign_pointer(pmc->sflist, newpsl);
  2095. psl = newpsl;
  2096. }
  2097. rv = 1; /* > 0 for insert logic below if sl_count is 0 */
  2098. for (i = 0; i < psl->sl_count; i++) {
  2099. rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
  2100. sizeof(__be32));
  2101. if (rv == 0)
  2102. break;
  2103. }
  2104. if (rv == 0) /* address already there is an error */
  2105. goto done;
  2106. for (j = psl->sl_count-1; j >= i; j--)
  2107. psl->sl_addr[j+1] = psl->sl_addr[j];
  2108. psl->sl_addr[i] = mreqs->imr_sourceaddr;
  2109. psl->sl_count++;
  2110. err = 0;
  2111. /* update the interface list */
  2112. ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
  2113. &mreqs->imr_sourceaddr, 1);
  2114. done:
  2115. if (leavegroup)
  2116. err = ip_mc_leave_group(sk, &imr);
  2117. return err;
  2118. }
  2119. int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
  2120. {
  2121. int err = 0;
  2122. struct ip_mreqn imr;
  2123. __be32 addr = msf->imsf_multiaddr;
  2124. struct ip_mc_socklist *pmc;
  2125. struct in_device *in_dev;
  2126. struct inet_sock *inet = inet_sk(sk);
  2127. struct ip_sf_socklist *newpsl, *psl;
  2128. struct net *net = sock_net(sk);
  2129. int leavegroup = 0;
  2130. if (!ipv4_is_multicast(addr))
  2131. return -EINVAL;
  2132. if (msf->imsf_fmode != MCAST_INCLUDE &&
  2133. msf->imsf_fmode != MCAST_EXCLUDE)
  2134. return -EINVAL;
  2135. ASSERT_RTNL();
  2136. imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
  2137. imr.imr_address.s_addr = msf->imsf_interface;
  2138. imr.imr_ifindex = ifindex;
  2139. in_dev = ip_mc_find_dev(net, &imr);
  2140. if (!in_dev) {
  2141. err = -ENODEV;
  2142. goto done;
  2143. }
  2144. /* special case - (INCLUDE, empty) == LEAVE_GROUP */
  2145. if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
  2146. leavegroup = 1;
  2147. goto done;
  2148. }
  2149. for_each_pmc_rtnl(inet, pmc) {
  2150. if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
  2151. pmc->multi.imr_ifindex == imr.imr_ifindex)
  2152. break;
  2153. }
  2154. if (!pmc) { /* must have a prior join */
  2155. err = -EINVAL;
  2156. goto done;
  2157. }
  2158. if (msf->imsf_numsrc) {
  2159. newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
  2160. GFP_KERNEL);
  2161. if (!newpsl) {
  2162. err = -ENOBUFS;
  2163. goto done;
  2164. }
  2165. newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
  2166. memcpy(newpsl->sl_addr, msf->imsf_slist,
  2167. msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
  2168. err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
  2169. msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
  2170. if (err) {
  2171. sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
  2172. goto done;
  2173. }
  2174. } else {
  2175. newpsl = NULL;
  2176. (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
  2177. msf->imsf_fmode, 0, NULL, 0);
  2178. }
  2179. psl = rtnl_dereference(pmc->sflist);
  2180. if (psl) {
  2181. (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
  2182. psl->sl_count, psl->sl_addr, 0);
  2183. /* decrease mem now to avoid the memleak warning */
  2184. atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
  2185. kfree_rcu(psl, rcu);
  2186. } else
  2187. (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
  2188. 0, NULL, 0);
  2189. rcu_assign_pointer(pmc->sflist, newpsl);
  2190. pmc->sfmode = msf->imsf_fmode;
  2191. err = 0;
  2192. done:
  2193. if (leavegroup)
  2194. err = ip_mc_leave_group(sk, &imr);
  2195. return err;
  2196. }
  2197. int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
  2198. struct ip_msfilter __user *optval, int __user *optlen)
  2199. {
  2200. int err, len, count, copycount;
  2201. struct ip_mreqn imr;
  2202. __be32 addr = msf->imsf_multiaddr;
  2203. struct ip_mc_socklist *pmc;
  2204. struct in_device *in_dev;
  2205. struct inet_sock *inet = inet_sk(sk);
  2206. struct ip_sf_socklist *psl;
  2207. struct net *net = sock_net(sk);
  2208. ASSERT_RTNL();
  2209. if (!ipv4_is_multicast(addr))
  2210. return -EINVAL;
  2211. imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
  2212. imr.imr_address.s_addr = msf->imsf_interface;
  2213. imr.imr_ifindex = 0;
  2214. in_dev = ip_mc_find_dev(net, &imr);
  2215. if (!in_dev) {
  2216. err = -ENODEV;
  2217. goto done;
  2218. }
  2219. err = -EADDRNOTAVAIL;
  2220. for_each_pmc_rtnl(inet, pmc) {
  2221. if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
  2222. pmc->multi.imr_ifindex == imr.imr_ifindex)
  2223. break;
  2224. }
  2225. if (!pmc) /* must have a prior join */
  2226. goto done;
  2227. msf->imsf_fmode = pmc->sfmode;
  2228. psl = rtnl_dereference(pmc->sflist);
  2229. if (!psl) {
  2230. len = 0;
  2231. count = 0;
  2232. } else {
  2233. count = psl->sl_count;
  2234. }
  2235. copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
  2236. len = copycount * sizeof(psl->sl_addr[0]);
  2237. msf->imsf_numsrc = count;
  2238. if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
  2239. copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
  2240. return -EFAULT;
  2241. }
  2242. if (len &&
  2243. copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
  2244. return -EFAULT;
  2245. return 0;
  2246. done:
  2247. return err;
  2248. }
  2249. int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
  2250. struct group_filter __user *optval, int __user *optlen)
  2251. {
  2252. int err, i, count, copycount;
  2253. struct sockaddr_in *psin;
  2254. __be32 addr;
  2255. struct ip_mc_socklist *pmc;
  2256. struct inet_sock *inet = inet_sk(sk);
  2257. struct ip_sf_socklist *psl;
  2258. ASSERT_RTNL();
  2259. psin = (struct sockaddr_in *)&gsf->gf_group;
  2260. if (psin->sin_family != AF_INET)
  2261. return -EINVAL;
  2262. addr = psin->sin_addr.s_addr;
  2263. if (!ipv4_is_multicast(addr))
  2264. return -EINVAL;
  2265. err = -EADDRNOTAVAIL;
  2266. for_each_pmc_rtnl(inet, pmc) {
  2267. if (pmc->multi.imr_multiaddr.s_addr == addr &&
  2268. pmc->multi.imr_ifindex == gsf->gf_interface)
  2269. break;
  2270. }
  2271. if (!pmc) /* must have a prior join */
  2272. goto done;
  2273. gsf->gf_fmode = pmc->sfmode;
  2274. psl = rtnl_dereference(pmc->sflist);
  2275. count = psl ? psl->sl_count : 0;
  2276. copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
  2277. gsf->gf_numsrc = count;
  2278. if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
  2279. copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
  2280. return -EFAULT;
  2281. }
  2282. for (i = 0; i < copycount; i++) {
  2283. struct sockaddr_storage ss;
  2284. psin = (struct sockaddr_in *)&ss;
  2285. memset(&ss, 0, sizeof(ss));
  2286. psin->sin_family = AF_INET;
  2287. psin->sin_addr.s_addr = psl->sl_addr[i];
  2288. if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
  2289. return -EFAULT;
  2290. }
  2291. return 0;
  2292. done:
  2293. return err;
  2294. }
  2295. /*
  2296. * check if a multicast source filter allows delivery for a given <src,dst,intf>
  2297. */
  2298. int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
  2299. int dif, int sdif)
  2300. {
  2301. struct inet_sock *inet = inet_sk(sk);
  2302. struct ip_mc_socklist *pmc;
  2303. struct ip_sf_socklist *psl;
  2304. int i;
  2305. int ret;
  2306. ret = 1;
  2307. if (!ipv4_is_multicast(loc_addr))
  2308. goto out;
  2309. rcu_read_lock();
  2310. for_each_pmc_rcu(inet, pmc) {
  2311. if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
  2312. (pmc->multi.imr_ifindex == dif ||
  2313. (sdif && pmc->multi.imr_ifindex == sdif)))
  2314. break;
  2315. }
  2316. ret = inet->mc_all;
  2317. if (!pmc)
  2318. goto unlock;
  2319. psl = rcu_dereference(pmc->sflist);
  2320. ret = (pmc->sfmode == MCAST_EXCLUDE);
  2321. if (!psl)
  2322. goto unlock;
  2323. for (i = 0; i < psl->sl_count; i++) {
  2324. if (psl->sl_addr[i] == rmt_addr)
  2325. break;
  2326. }
  2327. ret = 0;
  2328. if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
  2329. goto unlock;
  2330. if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
  2331. goto unlock;
  2332. ret = 1;
  2333. unlock:
  2334. rcu_read_unlock();
  2335. out:
  2336. return ret;
  2337. }
  2338. /*
  2339. * A socket is closing.
  2340. */
  2341. void ip_mc_drop_socket(struct sock *sk)
  2342. {
  2343. struct inet_sock *inet = inet_sk(sk);
  2344. struct ip_mc_socklist *iml;
  2345. struct net *net = sock_net(sk);
  2346. if (!inet->mc_list)
  2347. return;
  2348. rtnl_lock();
  2349. while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
  2350. struct in_device *in_dev;
  2351. inet->mc_list = iml->next_rcu;
  2352. in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
  2353. (void) ip_mc_leave_src(sk, iml, in_dev);
  2354. if (in_dev)
  2355. ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
  2356. /* decrease mem now to avoid the memleak warning */
  2357. atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
  2358. kfree_rcu(iml, rcu);
  2359. }
  2360. rtnl_unlock();
  2361. }
  2362. /* called with rcu_read_lock() */
  2363. int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
  2364. {
  2365. struct ip_mc_list *im;
  2366. struct ip_mc_list __rcu **mc_hash;
  2367. struct ip_sf_list *psf;
  2368. int rv = 0;
  2369. mc_hash = rcu_dereference(in_dev->mc_hash);
  2370. if (mc_hash) {
  2371. u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
  2372. for (im = rcu_dereference(mc_hash[hash]);
  2373. im != NULL;
  2374. im = rcu_dereference(im->next_hash)) {
  2375. if (im->multiaddr == mc_addr)
  2376. break;
  2377. }
  2378. } else {
  2379. for_each_pmc_rcu(in_dev, im) {
  2380. if (im->multiaddr == mc_addr)
  2381. break;
  2382. }
  2383. }
  2384. if (im && proto == IPPROTO_IGMP) {
  2385. rv = 1;
  2386. } else if (im) {
  2387. if (src_addr) {
  2388. for (psf = im->sources; psf; psf = psf->sf_next) {
  2389. if (psf->sf_inaddr == src_addr)
  2390. break;
  2391. }
  2392. if (psf)
  2393. rv = psf->sf_count[MCAST_INCLUDE] ||
  2394. psf->sf_count[MCAST_EXCLUDE] !=
  2395. im->sfcount[MCAST_EXCLUDE];
  2396. else
  2397. rv = im->sfcount[MCAST_EXCLUDE] != 0;
  2398. } else
  2399. rv = 1; /* unspecified source; tentatively allow */
  2400. }
  2401. return rv;
  2402. }
  2403. #if defined(CONFIG_PROC_FS)
  2404. struct igmp_mc_iter_state {
  2405. struct seq_net_private p;
  2406. struct net_device *dev;
  2407. struct in_device *in_dev;
  2408. };
  2409. #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
  2410. static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
  2411. {
  2412. struct net *net = seq_file_net(seq);
  2413. struct ip_mc_list *im = NULL;
  2414. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2415. state->in_dev = NULL;
  2416. for_each_netdev_rcu(net, state->dev) {
  2417. struct in_device *in_dev;
  2418. in_dev = __in_dev_get_rcu(state->dev);
  2419. if (!in_dev)
  2420. continue;
  2421. im = rcu_dereference(in_dev->mc_list);
  2422. if (im) {
  2423. state->in_dev = in_dev;
  2424. break;
  2425. }
  2426. }
  2427. return im;
  2428. }
  2429. static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
  2430. {
  2431. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2432. im = rcu_dereference(im->next_rcu);
  2433. while (!im) {
  2434. state->dev = next_net_device_rcu(state->dev);
  2435. if (!state->dev) {
  2436. state->in_dev = NULL;
  2437. break;
  2438. }
  2439. state->in_dev = __in_dev_get_rcu(state->dev);
  2440. if (!state->in_dev)
  2441. continue;
  2442. im = rcu_dereference(state->in_dev->mc_list);
  2443. }
  2444. return im;
  2445. }
  2446. static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
  2447. {
  2448. struct ip_mc_list *im = igmp_mc_get_first(seq);
  2449. if (im)
  2450. while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
  2451. --pos;
  2452. return pos ? NULL : im;
  2453. }
  2454. static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
  2455. __acquires(rcu)
  2456. {
  2457. rcu_read_lock();
  2458. return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2459. }
  2460. static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2461. {
  2462. struct ip_mc_list *im;
  2463. if (v == SEQ_START_TOKEN)
  2464. im = igmp_mc_get_first(seq);
  2465. else
  2466. im = igmp_mc_get_next(seq, v);
  2467. ++*pos;
  2468. return im;
  2469. }
  2470. static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
  2471. __releases(rcu)
  2472. {
  2473. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2474. state->in_dev = NULL;
  2475. state->dev = NULL;
  2476. rcu_read_unlock();
  2477. }
  2478. static int igmp_mc_seq_show(struct seq_file *seq, void *v)
  2479. {
  2480. if (v == SEQ_START_TOKEN)
  2481. seq_puts(seq,
  2482. "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
  2483. else {
  2484. struct ip_mc_list *im = (struct ip_mc_list *)v;
  2485. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2486. char *querier;
  2487. long delta;
  2488. #ifdef CONFIG_IP_MULTICAST
  2489. querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
  2490. IGMP_V2_SEEN(state->in_dev) ? "V2" :
  2491. "V3";
  2492. #else
  2493. querier = "NONE";
  2494. #endif
  2495. if (rcu_access_pointer(state->in_dev->mc_list) == im) {
  2496. seq_printf(seq, "%d\t%-10s: %5d %7s\n",
  2497. state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
  2498. }
  2499. delta = im->timer.expires - jiffies;
  2500. seq_printf(seq,
  2501. "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
  2502. im->multiaddr, im->users,
  2503. im->tm_running,
  2504. im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
  2505. im->reporter);
  2506. }
  2507. return 0;
  2508. }
  2509. static const struct seq_operations igmp_mc_seq_ops = {
  2510. .start = igmp_mc_seq_start,
  2511. .next = igmp_mc_seq_next,
  2512. .stop = igmp_mc_seq_stop,
  2513. .show = igmp_mc_seq_show,
  2514. };
  2515. struct igmp_mcf_iter_state {
  2516. struct seq_net_private p;
  2517. struct net_device *dev;
  2518. struct in_device *idev;
  2519. struct ip_mc_list *im;
  2520. };
  2521. #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
  2522. static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
  2523. {
  2524. struct net *net = seq_file_net(seq);
  2525. struct ip_sf_list *psf = NULL;
  2526. struct ip_mc_list *im = NULL;
  2527. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2528. state->idev = NULL;
  2529. state->im = NULL;
  2530. for_each_netdev_rcu(net, state->dev) {
  2531. struct in_device *idev;
  2532. idev = __in_dev_get_rcu(state->dev);
  2533. if (unlikely(!idev))
  2534. continue;
  2535. im = rcu_dereference(idev->mc_list);
  2536. if (likely(im)) {
  2537. spin_lock_bh(&im->lock);
  2538. psf = im->sources;
  2539. if (likely(psf)) {
  2540. state->im = im;
  2541. state->idev = idev;
  2542. break;
  2543. }
  2544. spin_unlock_bh(&im->lock);
  2545. }
  2546. }
  2547. return psf;
  2548. }
  2549. static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
  2550. {
  2551. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2552. psf = psf->sf_next;
  2553. while (!psf) {
  2554. spin_unlock_bh(&state->im->lock);
  2555. state->im = state->im->next;
  2556. while (!state->im) {
  2557. state->dev = next_net_device_rcu(state->dev);
  2558. if (!state->dev) {
  2559. state->idev = NULL;
  2560. goto out;
  2561. }
  2562. state->idev = __in_dev_get_rcu(state->dev);
  2563. if (!state->idev)
  2564. continue;
  2565. state->im = rcu_dereference(state->idev->mc_list);
  2566. }
  2567. if (!state->im)
  2568. break;
  2569. spin_lock_bh(&state->im->lock);
  2570. psf = state->im->sources;
  2571. }
  2572. out:
  2573. return psf;
  2574. }
  2575. static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
  2576. {
  2577. struct ip_sf_list *psf = igmp_mcf_get_first(seq);
  2578. if (psf)
  2579. while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
  2580. --pos;
  2581. return pos ? NULL : psf;
  2582. }
  2583. static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
  2584. __acquires(rcu)
  2585. {
  2586. rcu_read_lock();
  2587. return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2588. }
  2589. static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2590. {
  2591. struct ip_sf_list *psf;
  2592. if (v == SEQ_START_TOKEN)
  2593. psf = igmp_mcf_get_first(seq);
  2594. else
  2595. psf = igmp_mcf_get_next(seq, v);
  2596. ++*pos;
  2597. return psf;
  2598. }
  2599. static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
  2600. __releases(rcu)
  2601. {
  2602. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2603. if (likely(state->im)) {
  2604. spin_unlock_bh(&state->im->lock);
  2605. state->im = NULL;
  2606. }
  2607. state->idev = NULL;
  2608. state->dev = NULL;
  2609. rcu_read_unlock();
  2610. }
  2611. static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
  2612. {
  2613. struct ip_sf_list *psf = (struct ip_sf_list *)v;
  2614. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2615. if (v == SEQ_START_TOKEN) {
  2616. seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
  2617. } else {
  2618. seq_printf(seq,
  2619. "%3d %6.6s 0x%08x "
  2620. "0x%08x %6lu %6lu\n",
  2621. state->dev->ifindex, state->dev->name,
  2622. ntohl(state->im->multiaddr),
  2623. ntohl(psf->sf_inaddr),
  2624. psf->sf_count[MCAST_INCLUDE],
  2625. psf->sf_count[MCAST_EXCLUDE]);
  2626. }
  2627. return 0;
  2628. }
  2629. static const struct seq_operations igmp_mcf_seq_ops = {
  2630. .start = igmp_mcf_seq_start,
  2631. .next = igmp_mcf_seq_next,
  2632. .stop = igmp_mcf_seq_stop,
  2633. .show = igmp_mcf_seq_show,
  2634. };
  2635. static int __net_init igmp_net_init(struct net *net)
  2636. {
  2637. struct proc_dir_entry *pde;
  2638. int err;
  2639. pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops,
  2640. sizeof(struct igmp_mc_iter_state));
  2641. if (!pde)
  2642. goto out_igmp;
  2643. pde = proc_create_net("mcfilter", 0444, net->proc_net,
  2644. &igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state));
  2645. if (!pde)
  2646. goto out_mcfilter;
  2647. err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
  2648. SOCK_DGRAM, 0, net);
  2649. if (err < 0) {
  2650. pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
  2651. err);
  2652. goto out_sock;
  2653. }
  2654. return 0;
  2655. out_sock:
  2656. remove_proc_entry("mcfilter", net->proc_net);
  2657. out_mcfilter:
  2658. remove_proc_entry("igmp", net->proc_net);
  2659. out_igmp:
  2660. return -ENOMEM;
  2661. }
  2662. static void __net_exit igmp_net_exit(struct net *net)
  2663. {
  2664. remove_proc_entry("mcfilter", net->proc_net);
  2665. remove_proc_entry("igmp", net->proc_net);
  2666. inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
  2667. }
  2668. static struct pernet_operations igmp_net_ops = {
  2669. .init = igmp_net_init,
  2670. .exit = igmp_net_exit,
  2671. };
  2672. #endif
  2673. static int igmp_netdev_event(struct notifier_block *this,
  2674. unsigned long event, void *ptr)
  2675. {
  2676. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  2677. struct in_device *in_dev;
  2678. switch (event) {
  2679. case NETDEV_RESEND_IGMP:
  2680. in_dev = __in_dev_get_rtnl(dev);
  2681. if (in_dev)
  2682. ip_mc_rejoin_groups(in_dev);
  2683. break;
  2684. default:
  2685. break;
  2686. }
  2687. return NOTIFY_DONE;
  2688. }
  2689. static struct notifier_block igmp_notifier = {
  2690. .notifier_call = igmp_netdev_event,
  2691. };
  2692. int __init igmp_mc_init(void)
  2693. {
  2694. #if defined(CONFIG_PROC_FS)
  2695. int err;
  2696. err = register_pernet_subsys(&igmp_net_ops);
  2697. if (err)
  2698. return err;
  2699. err = register_netdevice_notifier(&igmp_notifier);
  2700. if (err)
  2701. goto reg_notif_fail;
  2702. return 0;
  2703. reg_notif_fail:
  2704. unregister_pernet_subsys(&igmp_net_ops);
  2705. return err;
  2706. #else
  2707. return register_netdevice_notifier(&igmp_notifier);
  2708. #endif
  2709. }