igmp.c 73 KB

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