igmp.c 62 KB

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