igmp.c 59 KB

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