igmp.c 71 KB

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