igmp.c 73 KB

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