igmp.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487
  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. (u16)csum_fold(skb_checksum(skb, 0, len, 0))) {
  776. in_dev_put(in_dev);
  777. kfree_skb(skb);
  778. return 0;
  779. }
  780. ih = skb->h.igmph;
  781. switch (ih->type) {
  782. case IGMP_HOST_MEMBERSHIP_QUERY:
  783. igmp_heard_query(in_dev, skb, len);
  784. break;
  785. case IGMP_HOST_MEMBERSHIP_REPORT:
  786. case IGMPV2_HOST_MEMBERSHIP_REPORT:
  787. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  788. /* Is it our report looped back? */
  789. if (((struct rtable*)skb->dst)->fl.iif == 0)
  790. break;
  791. igmp_heard_report(in_dev, ih->group);
  792. break;
  793. case IGMP_PIM:
  794. #ifdef CONFIG_IP_PIMSM_V1
  795. in_dev_put(in_dev);
  796. return pim_rcv_v1(skb);
  797. #endif
  798. case IGMP_DVMRP:
  799. case IGMP_TRACE:
  800. case IGMP_HOST_LEAVE_MESSAGE:
  801. case IGMP_MTRACE:
  802. case IGMP_MTRACE_RESP:
  803. break;
  804. default:
  805. NETDEBUG(KERN_DEBUG "New IGMP type=%d, why we do not know about it?\n", ih->type);
  806. }
  807. in_dev_put(in_dev);
  808. kfree_skb(skb);
  809. return 0;
  810. }
  811. #endif
  812. /*
  813. * Add a filter to a device
  814. */
  815. static void ip_mc_filter_add(struct in_device *in_dev, u32 addr)
  816. {
  817. char buf[MAX_ADDR_LEN];
  818. struct net_device *dev = in_dev->dev;
  819. /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
  820. We will get multicast token leakage, when IFF_MULTICAST
  821. is changed. This check should be done in dev->set_multicast_list
  822. routine. Something sort of:
  823. if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
  824. --ANK
  825. */
  826. if (arp_mc_map(addr, buf, dev, 0) == 0)
  827. dev_mc_add(dev,buf,dev->addr_len,0);
  828. }
  829. /*
  830. * Remove a filter from a device
  831. */
  832. static void ip_mc_filter_del(struct in_device *in_dev, u32 addr)
  833. {
  834. char buf[MAX_ADDR_LEN];
  835. struct net_device *dev = in_dev->dev;
  836. if (arp_mc_map(addr, buf, dev, 0) == 0)
  837. dev_mc_delete(dev,buf,dev->addr_len,0);
  838. }
  839. #ifdef CONFIG_IP_MULTICAST
  840. /*
  841. * deleted ip_mc_list manipulation
  842. */
  843. static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
  844. {
  845. struct ip_mc_list *pmc;
  846. /* this is an "ip_mc_list" for convenience; only the fields below
  847. * are actually used. In particular, the refcnt and users are not
  848. * used for management of the delete list. Using the same structure
  849. * for deleted items allows change reports to use common code with
  850. * non-deleted or query-response MCA's.
  851. */
  852. pmc = (struct ip_mc_list *)kmalloc(sizeof(*pmc), GFP_KERNEL);
  853. if (!pmc)
  854. return;
  855. memset(pmc, 0, sizeof(*pmc));
  856. spin_lock_bh(&im->lock);
  857. pmc->interface = im->interface;
  858. in_dev_hold(in_dev);
  859. pmc->multiaddr = im->multiaddr;
  860. pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  861. IGMP_Unsolicited_Report_Count;
  862. pmc->sfmode = im->sfmode;
  863. if (pmc->sfmode == MCAST_INCLUDE) {
  864. struct ip_sf_list *psf;
  865. pmc->tomb = im->tomb;
  866. pmc->sources = im->sources;
  867. im->tomb = im->sources = NULL;
  868. for (psf=pmc->sources; psf; psf=psf->sf_next)
  869. psf->sf_crcount = pmc->crcount;
  870. }
  871. spin_unlock_bh(&im->lock);
  872. spin_lock_bh(&in_dev->mc_tomb_lock);
  873. pmc->next = in_dev->mc_tomb;
  874. in_dev->mc_tomb = pmc;
  875. spin_unlock_bh(&in_dev->mc_tomb_lock);
  876. }
  877. static void igmpv3_del_delrec(struct in_device *in_dev, __u32 multiaddr)
  878. {
  879. struct ip_mc_list *pmc, *pmc_prev;
  880. struct ip_sf_list *psf, *psf_next;
  881. spin_lock_bh(&in_dev->mc_tomb_lock);
  882. pmc_prev = NULL;
  883. for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
  884. if (pmc->multiaddr == multiaddr)
  885. break;
  886. pmc_prev = pmc;
  887. }
  888. if (pmc) {
  889. if (pmc_prev)
  890. pmc_prev->next = pmc->next;
  891. else
  892. in_dev->mc_tomb = pmc->next;
  893. }
  894. spin_unlock_bh(&in_dev->mc_tomb_lock);
  895. if (pmc) {
  896. for (psf=pmc->tomb; psf; psf=psf_next) {
  897. psf_next = psf->sf_next;
  898. kfree(psf);
  899. }
  900. in_dev_put(pmc->interface);
  901. kfree(pmc);
  902. }
  903. }
  904. static void igmpv3_clear_delrec(struct in_device *in_dev)
  905. {
  906. struct ip_mc_list *pmc, *nextpmc;
  907. spin_lock_bh(&in_dev->mc_tomb_lock);
  908. pmc = in_dev->mc_tomb;
  909. in_dev->mc_tomb = NULL;
  910. spin_unlock_bh(&in_dev->mc_tomb_lock);
  911. for (; pmc; pmc = nextpmc) {
  912. nextpmc = pmc->next;
  913. ip_mc_clear_src(pmc);
  914. in_dev_put(pmc->interface);
  915. kfree(pmc);
  916. }
  917. /* clear dead sources, too */
  918. read_lock(&in_dev->mc_list_lock);
  919. for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
  920. struct ip_sf_list *psf, *psf_next;
  921. spin_lock_bh(&pmc->lock);
  922. psf = pmc->tomb;
  923. pmc->tomb = NULL;
  924. spin_unlock_bh(&pmc->lock);
  925. for (; psf; psf=psf_next) {
  926. psf_next = psf->sf_next;
  927. kfree(psf);
  928. }
  929. }
  930. read_unlock(&in_dev->mc_list_lock);
  931. }
  932. #endif
  933. static void igmp_group_dropped(struct ip_mc_list *im)
  934. {
  935. struct in_device *in_dev = im->interface;
  936. #ifdef CONFIG_IP_MULTICAST
  937. int reporter;
  938. #endif
  939. if (im->loaded) {
  940. im->loaded = 0;
  941. ip_mc_filter_del(in_dev, im->multiaddr);
  942. }
  943. #ifdef CONFIG_IP_MULTICAST
  944. if (im->multiaddr == IGMP_ALL_HOSTS)
  945. return;
  946. reporter = im->reporter;
  947. igmp_stop_timer(im);
  948. if (!in_dev->dead) {
  949. if (IGMP_V1_SEEN(in_dev))
  950. goto done;
  951. if (IGMP_V2_SEEN(in_dev)) {
  952. if (reporter)
  953. igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
  954. goto done;
  955. }
  956. /* IGMPv3 */
  957. igmpv3_add_delrec(in_dev, im);
  958. igmp_ifc_event(in_dev);
  959. }
  960. done:
  961. #endif
  962. ip_mc_clear_src(im);
  963. }
  964. static void igmp_group_added(struct ip_mc_list *im)
  965. {
  966. struct in_device *in_dev = im->interface;
  967. if (im->loaded == 0) {
  968. im->loaded = 1;
  969. ip_mc_filter_add(in_dev, im->multiaddr);
  970. }
  971. #ifdef CONFIG_IP_MULTICAST
  972. if (im->multiaddr == IGMP_ALL_HOSTS)
  973. return;
  974. if (in_dev->dead)
  975. return;
  976. if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
  977. spin_lock_bh(&im->lock);
  978. igmp_start_timer(im, IGMP_Initial_Report_Delay);
  979. spin_unlock_bh(&im->lock);
  980. return;
  981. }
  982. /* else, v3 */
  983. im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  984. IGMP_Unsolicited_Report_Count;
  985. igmp_ifc_event(in_dev);
  986. #endif
  987. }
  988. /*
  989. * Multicast list managers
  990. */
  991. /*
  992. * A socket has joined a multicast group on device dev.
  993. */
  994. void ip_mc_inc_group(struct in_device *in_dev, u32 addr)
  995. {
  996. struct ip_mc_list *im;
  997. ASSERT_RTNL();
  998. for (im=in_dev->mc_list; im; im=im->next) {
  999. if (im->multiaddr == addr) {
  1000. im->users++;
  1001. ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
  1002. goto out;
  1003. }
  1004. }
  1005. im = (struct ip_mc_list *)kmalloc(sizeof(*im), GFP_KERNEL);
  1006. if (!im)
  1007. goto out;
  1008. im->users=1;
  1009. im->interface=in_dev;
  1010. in_dev_hold(in_dev);
  1011. im->multiaddr=addr;
  1012. /* initial mode is (EX, empty) */
  1013. im->sfmode = MCAST_EXCLUDE;
  1014. im->sfcount[MCAST_INCLUDE] = 0;
  1015. im->sfcount[MCAST_EXCLUDE] = 1;
  1016. im->sources = NULL;
  1017. im->tomb = NULL;
  1018. im->crcount = 0;
  1019. atomic_set(&im->refcnt, 1);
  1020. spin_lock_init(&im->lock);
  1021. #ifdef CONFIG_IP_MULTICAST
  1022. im->tm_running=0;
  1023. init_timer(&im->timer);
  1024. im->timer.data=(unsigned long)im;
  1025. im->timer.function=&igmp_timer_expire;
  1026. im->unsolicit_count = IGMP_Unsolicited_Report_Count;
  1027. im->reporter = 0;
  1028. im->gsquery = 0;
  1029. #endif
  1030. im->loaded = 0;
  1031. write_lock_bh(&in_dev->mc_list_lock);
  1032. im->next=in_dev->mc_list;
  1033. in_dev->mc_list=im;
  1034. write_unlock_bh(&in_dev->mc_list_lock);
  1035. #ifdef CONFIG_IP_MULTICAST
  1036. igmpv3_del_delrec(in_dev, im->multiaddr);
  1037. #endif
  1038. igmp_group_added(im);
  1039. if (!in_dev->dead)
  1040. ip_rt_multicast_event(in_dev);
  1041. out:
  1042. return;
  1043. }
  1044. /*
  1045. * A socket has left a multicast group on device dev
  1046. */
  1047. void ip_mc_dec_group(struct in_device *in_dev, u32 addr)
  1048. {
  1049. struct ip_mc_list *i, **ip;
  1050. ASSERT_RTNL();
  1051. for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
  1052. if (i->multiaddr==addr) {
  1053. if (--i->users == 0) {
  1054. write_lock_bh(&in_dev->mc_list_lock);
  1055. *ip = i->next;
  1056. write_unlock_bh(&in_dev->mc_list_lock);
  1057. igmp_group_dropped(i);
  1058. if (!in_dev->dead)
  1059. ip_rt_multicast_event(in_dev);
  1060. ip_ma_put(i);
  1061. return;
  1062. }
  1063. break;
  1064. }
  1065. }
  1066. }
  1067. /* Device going down */
  1068. void ip_mc_down(struct in_device *in_dev)
  1069. {
  1070. struct ip_mc_list *i;
  1071. ASSERT_RTNL();
  1072. for (i=in_dev->mc_list; i; i=i->next)
  1073. igmp_group_dropped(i);
  1074. #ifdef CONFIG_IP_MULTICAST
  1075. in_dev->mr_ifc_count = 0;
  1076. if (del_timer(&in_dev->mr_ifc_timer))
  1077. __in_dev_put(in_dev);
  1078. in_dev->mr_gq_running = 0;
  1079. if (del_timer(&in_dev->mr_gq_timer))
  1080. __in_dev_put(in_dev);
  1081. igmpv3_clear_delrec(in_dev);
  1082. #endif
  1083. ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
  1084. }
  1085. void ip_mc_init_dev(struct in_device *in_dev)
  1086. {
  1087. ASSERT_RTNL();
  1088. in_dev->mc_tomb = NULL;
  1089. #ifdef CONFIG_IP_MULTICAST
  1090. in_dev->mr_gq_running = 0;
  1091. init_timer(&in_dev->mr_gq_timer);
  1092. in_dev->mr_gq_timer.data=(unsigned long) in_dev;
  1093. in_dev->mr_gq_timer.function=&igmp_gq_timer_expire;
  1094. in_dev->mr_ifc_count = 0;
  1095. init_timer(&in_dev->mr_ifc_timer);
  1096. in_dev->mr_ifc_timer.data=(unsigned long) in_dev;
  1097. in_dev->mr_ifc_timer.function=&igmp_ifc_timer_expire;
  1098. in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
  1099. #endif
  1100. rwlock_init(&in_dev->mc_list_lock);
  1101. spin_lock_init(&in_dev->mc_tomb_lock);
  1102. }
  1103. /* Device going up */
  1104. void ip_mc_up(struct in_device *in_dev)
  1105. {
  1106. struct ip_mc_list *i;
  1107. ASSERT_RTNL();
  1108. ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
  1109. for (i=in_dev->mc_list; i; i=i->next)
  1110. igmp_group_added(i);
  1111. }
  1112. /*
  1113. * Device is about to be destroyed: clean up.
  1114. */
  1115. void ip_mc_destroy_dev(struct in_device *in_dev)
  1116. {
  1117. struct ip_mc_list *i;
  1118. ASSERT_RTNL();
  1119. /* Deactivate timers */
  1120. ip_mc_down(in_dev);
  1121. write_lock_bh(&in_dev->mc_list_lock);
  1122. while ((i = in_dev->mc_list) != NULL) {
  1123. in_dev->mc_list = i->next;
  1124. write_unlock_bh(&in_dev->mc_list_lock);
  1125. igmp_group_dropped(i);
  1126. ip_ma_put(i);
  1127. write_lock_bh(&in_dev->mc_list_lock);
  1128. }
  1129. write_unlock_bh(&in_dev->mc_list_lock);
  1130. }
  1131. static struct in_device * ip_mc_find_dev(struct ip_mreqn *imr)
  1132. {
  1133. struct flowi fl = { .nl_u = { .ip4_u =
  1134. { .daddr = imr->imr_multiaddr.s_addr } } };
  1135. struct rtable *rt;
  1136. struct net_device *dev = NULL;
  1137. struct in_device *idev = NULL;
  1138. if (imr->imr_ifindex) {
  1139. idev = inetdev_by_index(imr->imr_ifindex);
  1140. if (idev)
  1141. __in_dev_put(idev);
  1142. return idev;
  1143. }
  1144. if (imr->imr_address.s_addr) {
  1145. dev = ip_dev_find(imr->imr_address.s_addr);
  1146. if (!dev)
  1147. return NULL;
  1148. __dev_put(dev);
  1149. }
  1150. if (!dev && !ip_route_output_key(&rt, &fl)) {
  1151. dev = rt->u.dst.dev;
  1152. ip_rt_put(rt);
  1153. }
  1154. if (dev) {
  1155. imr->imr_ifindex = dev->ifindex;
  1156. idev = __in_dev_get_rtnl(dev);
  1157. }
  1158. return idev;
  1159. }
  1160. /*
  1161. * Join a socket to a group
  1162. */
  1163. int sysctl_igmp_max_memberships = IP_MAX_MEMBERSHIPS;
  1164. int sysctl_igmp_max_msf = IP_MAX_MSF;
  1165. static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
  1166. __u32 *psfsrc)
  1167. {
  1168. struct ip_sf_list *psf, *psf_prev;
  1169. int rv = 0;
  1170. psf_prev = NULL;
  1171. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  1172. if (psf->sf_inaddr == *psfsrc)
  1173. break;
  1174. psf_prev = psf;
  1175. }
  1176. if (!psf || psf->sf_count[sfmode] == 0) {
  1177. /* source filter not found, or count wrong => bug */
  1178. return -ESRCH;
  1179. }
  1180. psf->sf_count[sfmode]--;
  1181. if (psf->sf_count[sfmode] == 0) {
  1182. ip_rt_multicast_event(pmc->interface);
  1183. }
  1184. if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
  1185. #ifdef CONFIG_IP_MULTICAST
  1186. struct in_device *in_dev = pmc->interface;
  1187. #endif
  1188. /* no more filters for this source */
  1189. if (psf_prev)
  1190. psf_prev->sf_next = psf->sf_next;
  1191. else
  1192. pmc->sources = psf->sf_next;
  1193. #ifdef CONFIG_IP_MULTICAST
  1194. if (psf->sf_oldin &&
  1195. !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
  1196. psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  1197. IGMP_Unsolicited_Report_Count;
  1198. psf->sf_next = pmc->tomb;
  1199. pmc->tomb = psf;
  1200. rv = 1;
  1201. } else
  1202. #endif
  1203. kfree(psf);
  1204. }
  1205. return rv;
  1206. }
  1207. #ifndef CONFIG_IP_MULTICAST
  1208. #define igmp_ifc_event(x) do { } while (0)
  1209. #endif
  1210. static int ip_mc_del_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
  1211. int sfcount, __u32 *psfsrc, int delta)
  1212. {
  1213. struct ip_mc_list *pmc;
  1214. int changerec = 0;
  1215. int i, err;
  1216. if (!in_dev)
  1217. return -ENODEV;
  1218. read_lock(&in_dev->mc_list_lock);
  1219. for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
  1220. if (*pmca == pmc->multiaddr)
  1221. break;
  1222. }
  1223. if (!pmc) {
  1224. /* MCA not found?? bug */
  1225. read_unlock(&in_dev->mc_list_lock);
  1226. return -ESRCH;
  1227. }
  1228. spin_lock_bh(&pmc->lock);
  1229. read_unlock(&in_dev->mc_list_lock);
  1230. #ifdef CONFIG_IP_MULTICAST
  1231. sf_markstate(pmc);
  1232. #endif
  1233. if (!delta) {
  1234. err = -EINVAL;
  1235. if (!pmc->sfcount[sfmode])
  1236. goto out_unlock;
  1237. pmc->sfcount[sfmode]--;
  1238. }
  1239. err = 0;
  1240. for (i=0; i<sfcount; i++) {
  1241. int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1242. changerec |= rv > 0;
  1243. if (!err && rv < 0)
  1244. err = rv;
  1245. }
  1246. if (pmc->sfmode == MCAST_EXCLUDE &&
  1247. pmc->sfcount[MCAST_EXCLUDE] == 0 &&
  1248. pmc->sfcount[MCAST_INCLUDE]) {
  1249. #ifdef CONFIG_IP_MULTICAST
  1250. struct ip_sf_list *psf;
  1251. #endif
  1252. /* filter mode change */
  1253. pmc->sfmode = MCAST_INCLUDE;
  1254. #ifdef CONFIG_IP_MULTICAST
  1255. pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  1256. IGMP_Unsolicited_Report_Count;
  1257. in_dev->mr_ifc_count = pmc->crcount;
  1258. for (psf=pmc->sources; psf; psf = psf->sf_next)
  1259. psf->sf_crcount = 0;
  1260. igmp_ifc_event(pmc->interface);
  1261. } else if (sf_setstate(pmc) || changerec) {
  1262. igmp_ifc_event(pmc->interface);
  1263. #endif
  1264. }
  1265. out_unlock:
  1266. spin_unlock_bh(&pmc->lock);
  1267. return err;
  1268. }
  1269. /*
  1270. * Add multicast single-source filter to the interface list
  1271. */
  1272. static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
  1273. __u32 *psfsrc, int delta)
  1274. {
  1275. struct ip_sf_list *psf, *psf_prev;
  1276. psf_prev = NULL;
  1277. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  1278. if (psf->sf_inaddr == *psfsrc)
  1279. break;
  1280. psf_prev = psf;
  1281. }
  1282. if (!psf) {
  1283. psf = (struct ip_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
  1284. if (!psf)
  1285. return -ENOBUFS;
  1286. memset(psf, 0, sizeof(*psf));
  1287. psf->sf_inaddr = *psfsrc;
  1288. if (psf_prev) {
  1289. psf_prev->sf_next = psf;
  1290. } else
  1291. pmc->sources = psf;
  1292. }
  1293. psf->sf_count[sfmode]++;
  1294. if (psf->sf_count[sfmode] == 1) {
  1295. ip_rt_multicast_event(pmc->interface);
  1296. }
  1297. return 0;
  1298. }
  1299. #ifdef CONFIG_IP_MULTICAST
  1300. static void sf_markstate(struct ip_mc_list *pmc)
  1301. {
  1302. struct ip_sf_list *psf;
  1303. int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
  1304. for (psf=pmc->sources; psf; psf=psf->sf_next)
  1305. if (pmc->sfcount[MCAST_EXCLUDE]) {
  1306. psf->sf_oldin = mca_xcount ==
  1307. psf->sf_count[MCAST_EXCLUDE] &&
  1308. !psf->sf_count[MCAST_INCLUDE];
  1309. } else
  1310. psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
  1311. }
  1312. static int sf_setstate(struct ip_mc_list *pmc)
  1313. {
  1314. struct ip_sf_list *psf;
  1315. int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
  1316. int qrv = pmc->interface->mr_qrv;
  1317. int new_in, rv;
  1318. rv = 0;
  1319. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  1320. if (pmc->sfcount[MCAST_EXCLUDE]) {
  1321. new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
  1322. !psf->sf_count[MCAST_INCLUDE];
  1323. } else
  1324. new_in = psf->sf_count[MCAST_INCLUDE] != 0;
  1325. if (new_in != psf->sf_oldin) {
  1326. psf->sf_crcount = qrv;
  1327. rv++;
  1328. }
  1329. }
  1330. return rv;
  1331. }
  1332. #endif
  1333. /*
  1334. * Add multicast source filter list to the interface list
  1335. */
  1336. static int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
  1337. int sfcount, __u32 *psfsrc, int delta)
  1338. {
  1339. struct ip_mc_list *pmc;
  1340. int isexclude;
  1341. int i, err;
  1342. if (!in_dev)
  1343. return -ENODEV;
  1344. read_lock(&in_dev->mc_list_lock);
  1345. for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
  1346. if (*pmca == pmc->multiaddr)
  1347. break;
  1348. }
  1349. if (!pmc) {
  1350. /* MCA not found?? bug */
  1351. read_unlock(&in_dev->mc_list_lock);
  1352. return -ESRCH;
  1353. }
  1354. spin_lock_bh(&pmc->lock);
  1355. read_unlock(&in_dev->mc_list_lock);
  1356. #ifdef CONFIG_IP_MULTICAST
  1357. sf_markstate(pmc);
  1358. #endif
  1359. isexclude = pmc->sfmode == MCAST_EXCLUDE;
  1360. if (!delta)
  1361. pmc->sfcount[sfmode]++;
  1362. err = 0;
  1363. for (i=0; i<sfcount; i++) {
  1364. err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
  1365. if (err)
  1366. break;
  1367. }
  1368. if (err) {
  1369. int j;
  1370. pmc->sfcount[sfmode]--;
  1371. for (j=0; j<i; j++)
  1372. (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1373. } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
  1374. #ifdef CONFIG_IP_MULTICAST
  1375. struct in_device *in_dev = pmc->interface;
  1376. struct ip_sf_list *psf;
  1377. #endif
  1378. /* filter mode change */
  1379. if (pmc->sfcount[MCAST_EXCLUDE])
  1380. pmc->sfmode = MCAST_EXCLUDE;
  1381. else if (pmc->sfcount[MCAST_INCLUDE])
  1382. pmc->sfmode = MCAST_INCLUDE;
  1383. #ifdef CONFIG_IP_MULTICAST
  1384. /* else no filters; keep old mode for reports */
  1385. pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  1386. IGMP_Unsolicited_Report_Count;
  1387. in_dev->mr_ifc_count = pmc->crcount;
  1388. for (psf=pmc->sources; psf; psf = psf->sf_next)
  1389. psf->sf_crcount = 0;
  1390. igmp_ifc_event(in_dev);
  1391. } else if (sf_setstate(pmc)) {
  1392. igmp_ifc_event(in_dev);
  1393. #endif
  1394. }
  1395. spin_unlock_bh(&pmc->lock);
  1396. return err;
  1397. }
  1398. static void ip_mc_clear_src(struct ip_mc_list *pmc)
  1399. {
  1400. struct ip_sf_list *psf, *nextpsf;
  1401. for (psf=pmc->tomb; psf; psf=nextpsf) {
  1402. nextpsf = psf->sf_next;
  1403. kfree(psf);
  1404. }
  1405. pmc->tomb = NULL;
  1406. for (psf=pmc->sources; psf; psf=nextpsf) {
  1407. nextpsf = psf->sf_next;
  1408. kfree(psf);
  1409. }
  1410. pmc->sources = NULL;
  1411. pmc->sfmode = MCAST_EXCLUDE;
  1412. pmc->sfcount[MCAST_INCLUDE] = 0;
  1413. pmc->sfcount[MCAST_EXCLUDE] = 1;
  1414. }
  1415. /*
  1416. * Join a multicast group
  1417. */
  1418. int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
  1419. {
  1420. int err;
  1421. u32 addr = imr->imr_multiaddr.s_addr;
  1422. struct ip_mc_socklist *iml=NULL, *i;
  1423. struct in_device *in_dev;
  1424. struct inet_sock *inet = inet_sk(sk);
  1425. int ifindex;
  1426. int count = 0;
  1427. if (!MULTICAST(addr))
  1428. return -EINVAL;
  1429. rtnl_shlock();
  1430. in_dev = ip_mc_find_dev(imr);
  1431. if (!in_dev) {
  1432. iml = NULL;
  1433. err = -ENODEV;
  1434. goto done;
  1435. }
  1436. err = -EADDRINUSE;
  1437. ifindex = imr->imr_ifindex;
  1438. for (i = inet->mc_list; i; i = i->next) {
  1439. if (i->multi.imr_multiaddr.s_addr == addr &&
  1440. i->multi.imr_ifindex == ifindex)
  1441. goto done;
  1442. count++;
  1443. }
  1444. err = -ENOBUFS;
  1445. if (count >= sysctl_igmp_max_memberships)
  1446. goto done;
  1447. iml = (struct ip_mc_socklist *)sock_kmalloc(sk,sizeof(*iml),GFP_KERNEL);
  1448. if (iml == NULL)
  1449. goto done;
  1450. memcpy(&iml->multi, imr, sizeof(*imr));
  1451. iml->next = inet->mc_list;
  1452. iml->sflist = NULL;
  1453. iml->sfmode = MCAST_EXCLUDE;
  1454. inet->mc_list = iml;
  1455. ip_mc_inc_group(in_dev, addr);
  1456. err = 0;
  1457. done:
  1458. rtnl_shunlock();
  1459. return err;
  1460. }
  1461. static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
  1462. struct in_device *in_dev)
  1463. {
  1464. int err;
  1465. if (iml->sflist == 0) {
  1466. /* any-source empty exclude case */
  1467. return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
  1468. iml->sfmode, 0, NULL, 0);
  1469. }
  1470. err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
  1471. iml->sfmode, iml->sflist->sl_count,
  1472. iml->sflist->sl_addr, 0);
  1473. sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
  1474. iml->sflist = NULL;
  1475. return err;
  1476. }
  1477. /*
  1478. * Ask a socket to leave a group.
  1479. */
  1480. int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
  1481. {
  1482. struct inet_sock *inet = inet_sk(sk);
  1483. struct ip_mc_socklist *iml, **imlp;
  1484. struct in_device *in_dev;
  1485. u32 group = imr->imr_multiaddr.s_addr;
  1486. u32 ifindex;
  1487. rtnl_lock();
  1488. in_dev = ip_mc_find_dev(imr);
  1489. if (!in_dev) {
  1490. rtnl_unlock();
  1491. return -ENODEV;
  1492. }
  1493. ifindex = imr->imr_ifindex;
  1494. for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
  1495. if (iml->multi.imr_multiaddr.s_addr == group &&
  1496. iml->multi.imr_ifindex == ifindex) {
  1497. (void) ip_mc_leave_src(sk, iml, in_dev);
  1498. *imlp = iml->next;
  1499. ip_mc_dec_group(in_dev, group);
  1500. rtnl_unlock();
  1501. sock_kfree_s(sk, iml, sizeof(*iml));
  1502. return 0;
  1503. }
  1504. }
  1505. rtnl_unlock();
  1506. return -EADDRNOTAVAIL;
  1507. }
  1508. int ip_mc_source(int add, int omode, struct sock *sk, struct
  1509. ip_mreq_source *mreqs, int ifindex)
  1510. {
  1511. int err;
  1512. struct ip_mreqn imr;
  1513. u32 addr = mreqs->imr_multiaddr;
  1514. struct ip_mc_socklist *pmc;
  1515. struct in_device *in_dev = NULL;
  1516. struct inet_sock *inet = inet_sk(sk);
  1517. struct ip_sf_socklist *psl;
  1518. int leavegroup = 0;
  1519. int i, j, rv;
  1520. if (!MULTICAST(addr))
  1521. return -EINVAL;
  1522. rtnl_shlock();
  1523. imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
  1524. imr.imr_address.s_addr = mreqs->imr_interface;
  1525. imr.imr_ifindex = ifindex;
  1526. in_dev = ip_mc_find_dev(&imr);
  1527. if (!in_dev) {
  1528. err = -ENODEV;
  1529. goto done;
  1530. }
  1531. err = -EADDRNOTAVAIL;
  1532. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1533. if (pmc->multi.imr_multiaddr.s_addr == imr.imr_multiaddr.s_addr
  1534. && pmc->multi.imr_ifindex == imr.imr_ifindex)
  1535. break;
  1536. }
  1537. if (!pmc) { /* must have a prior join */
  1538. err = -EINVAL;
  1539. goto done;
  1540. }
  1541. /* if a source filter was set, must be the same mode as before */
  1542. if (pmc->sflist) {
  1543. if (pmc->sfmode != omode) {
  1544. err = -EINVAL;
  1545. goto done;
  1546. }
  1547. } else if (pmc->sfmode != omode) {
  1548. /* allow mode switches for empty-set filters */
  1549. ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
  1550. ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
  1551. NULL, 0);
  1552. pmc->sfmode = omode;
  1553. }
  1554. psl = pmc->sflist;
  1555. if (!add) {
  1556. if (!psl)
  1557. goto done; /* err = -EADDRNOTAVAIL */
  1558. rv = !0;
  1559. for (i=0; i<psl->sl_count; i++) {
  1560. rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
  1561. sizeof(__u32));
  1562. if (rv == 0)
  1563. break;
  1564. }
  1565. if (rv) /* source not found */
  1566. goto done; /* err = -EADDRNOTAVAIL */
  1567. /* special case - (INCLUDE, empty) == LEAVE_GROUP */
  1568. if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
  1569. leavegroup = 1;
  1570. goto done;
  1571. }
  1572. /* update the interface filter */
  1573. ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
  1574. &mreqs->imr_sourceaddr, 1);
  1575. for (j=i+1; j<psl->sl_count; j++)
  1576. psl->sl_addr[j-1] = psl->sl_addr[j];
  1577. psl->sl_count--;
  1578. err = 0;
  1579. goto done;
  1580. }
  1581. /* else, add a new source to the filter */
  1582. if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
  1583. err = -ENOBUFS;
  1584. goto done;
  1585. }
  1586. if (!psl || psl->sl_count == psl->sl_max) {
  1587. struct ip_sf_socklist *newpsl;
  1588. int count = IP_SFBLOCK;
  1589. if (psl)
  1590. count += psl->sl_max;
  1591. newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
  1592. IP_SFLSIZE(count), GFP_KERNEL);
  1593. if (!newpsl) {
  1594. err = -ENOBUFS;
  1595. goto done;
  1596. }
  1597. newpsl->sl_max = count;
  1598. newpsl->sl_count = count - IP_SFBLOCK;
  1599. if (psl) {
  1600. for (i=0; i<psl->sl_count; i++)
  1601. newpsl->sl_addr[i] = psl->sl_addr[i];
  1602. sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
  1603. }
  1604. pmc->sflist = psl = newpsl;
  1605. }
  1606. rv = 1; /* > 0 for insert logic below if sl_count is 0 */
  1607. for (i=0; i<psl->sl_count; i++) {
  1608. rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
  1609. sizeof(__u32));
  1610. if (rv == 0)
  1611. break;
  1612. }
  1613. if (rv == 0) /* address already there is an error */
  1614. goto done;
  1615. for (j=psl->sl_count-1; j>=i; j--)
  1616. psl->sl_addr[j+1] = psl->sl_addr[j];
  1617. psl->sl_addr[i] = mreqs->imr_sourceaddr;
  1618. psl->sl_count++;
  1619. err = 0;
  1620. /* update the interface list */
  1621. ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
  1622. &mreqs->imr_sourceaddr, 1);
  1623. done:
  1624. rtnl_shunlock();
  1625. if (leavegroup)
  1626. return ip_mc_leave_group(sk, &imr);
  1627. return err;
  1628. }
  1629. int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
  1630. {
  1631. int err = 0;
  1632. struct ip_mreqn imr;
  1633. u32 addr = msf->imsf_multiaddr;
  1634. struct ip_mc_socklist *pmc;
  1635. struct in_device *in_dev;
  1636. struct inet_sock *inet = inet_sk(sk);
  1637. struct ip_sf_socklist *newpsl, *psl;
  1638. int leavegroup = 0;
  1639. if (!MULTICAST(addr))
  1640. return -EINVAL;
  1641. if (msf->imsf_fmode != MCAST_INCLUDE &&
  1642. msf->imsf_fmode != MCAST_EXCLUDE)
  1643. return -EINVAL;
  1644. rtnl_shlock();
  1645. imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
  1646. imr.imr_address.s_addr = msf->imsf_interface;
  1647. imr.imr_ifindex = ifindex;
  1648. in_dev = ip_mc_find_dev(&imr);
  1649. if (!in_dev) {
  1650. err = -ENODEV;
  1651. goto done;
  1652. }
  1653. /* special case - (INCLUDE, empty) == LEAVE_GROUP */
  1654. if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
  1655. leavegroup = 1;
  1656. goto done;
  1657. }
  1658. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1659. if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
  1660. pmc->multi.imr_ifindex == imr.imr_ifindex)
  1661. break;
  1662. }
  1663. if (!pmc) { /* must have a prior join */
  1664. err = -EINVAL;
  1665. goto done;
  1666. }
  1667. if (msf->imsf_numsrc) {
  1668. newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
  1669. IP_SFLSIZE(msf->imsf_numsrc), GFP_KERNEL);
  1670. if (!newpsl) {
  1671. err = -ENOBUFS;
  1672. goto done;
  1673. }
  1674. newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
  1675. memcpy(newpsl->sl_addr, msf->imsf_slist,
  1676. msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
  1677. err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
  1678. msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
  1679. if (err) {
  1680. sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
  1681. goto done;
  1682. }
  1683. } else
  1684. newpsl = NULL;
  1685. psl = pmc->sflist;
  1686. if (psl) {
  1687. (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
  1688. psl->sl_count, psl->sl_addr, 0);
  1689. sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
  1690. } else
  1691. (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
  1692. 0, NULL, 0);
  1693. pmc->sflist = newpsl;
  1694. pmc->sfmode = msf->imsf_fmode;
  1695. err = 0;
  1696. done:
  1697. rtnl_shunlock();
  1698. if (leavegroup)
  1699. err = ip_mc_leave_group(sk, &imr);
  1700. return err;
  1701. }
  1702. int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
  1703. struct ip_msfilter __user *optval, int __user *optlen)
  1704. {
  1705. int err, len, count, copycount;
  1706. struct ip_mreqn imr;
  1707. u32 addr = msf->imsf_multiaddr;
  1708. struct ip_mc_socklist *pmc;
  1709. struct in_device *in_dev;
  1710. struct inet_sock *inet = inet_sk(sk);
  1711. struct ip_sf_socklist *psl;
  1712. if (!MULTICAST(addr))
  1713. return -EINVAL;
  1714. rtnl_shlock();
  1715. imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
  1716. imr.imr_address.s_addr = msf->imsf_interface;
  1717. imr.imr_ifindex = 0;
  1718. in_dev = ip_mc_find_dev(&imr);
  1719. if (!in_dev) {
  1720. err = -ENODEV;
  1721. goto done;
  1722. }
  1723. err = -EADDRNOTAVAIL;
  1724. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1725. if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
  1726. pmc->multi.imr_ifindex == imr.imr_ifindex)
  1727. break;
  1728. }
  1729. if (!pmc) /* must have a prior join */
  1730. goto done;
  1731. msf->imsf_fmode = pmc->sfmode;
  1732. psl = pmc->sflist;
  1733. rtnl_shunlock();
  1734. if (!psl) {
  1735. len = 0;
  1736. count = 0;
  1737. } else {
  1738. count = psl->sl_count;
  1739. }
  1740. copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
  1741. len = copycount * sizeof(psl->sl_addr[0]);
  1742. msf->imsf_numsrc = count;
  1743. if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
  1744. copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
  1745. return -EFAULT;
  1746. }
  1747. if (len &&
  1748. copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
  1749. return -EFAULT;
  1750. return 0;
  1751. done:
  1752. rtnl_shunlock();
  1753. return err;
  1754. }
  1755. int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
  1756. struct group_filter __user *optval, int __user *optlen)
  1757. {
  1758. int err, i, count, copycount;
  1759. struct sockaddr_in *psin;
  1760. u32 addr;
  1761. struct ip_mc_socklist *pmc;
  1762. struct inet_sock *inet = inet_sk(sk);
  1763. struct ip_sf_socklist *psl;
  1764. psin = (struct sockaddr_in *)&gsf->gf_group;
  1765. if (psin->sin_family != AF_INET)
  1766. return -EINVAL;
  1767. addr = psin->sin_addr.s_addr;
  1768. if (!MULTICAST(addr))
  1769. return -EINVAL;
  1770. rtnl_shlock();
  1771. err = -EADDRNOTAVAIL;
  1772. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1773. if (pmc->multi.imr_multiaddr.s_addr == addr &&
  1774. pmc->multi.imr_ifindex == gsf->gf_interface)
  1775. break;
  1776. }
  1777. if (!pmc) /* must have a prior join */
  1778. goto done;
  1779. gsf->gf_fmode = pmc->sfmode;
  1780. psl = pmc->sflist;
  1781. rtnl_shunlock();
  1782. count = psl ? psl->sl_count : 0;
  1783. copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
  1784. gsf->gf_numsrc = count;
  1785. if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
  1786. copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
  1787. return -EFAULT;
  1788. }
  1789. for (i=0; i<copycount; i++) {
  1790. struct sockaddr_in *psin;
  1791. struct sockaddr_storage ss;
  1792. psin = (struct sockaddr_in *)&ss;
  1793. memset(&ss, 0, sizeof(ss));
  1794. psin->sin_family = AF_INET;
  1795. psin->sin_addr.s_addr = psl->sl_addr[i];
  1796. if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
  1797. return -EFAULT;
  1798. }
  1799. return 0;
  1800. done:
  1801. rtnl_shunlock();
  1802. return err;
  1803. }
  1804. /*
  1805. * check if a multicast source filter allows delivery for a given <src,dst,intf>
  1806. */
  1807. int ip_mc_sf_allow(struct sock *sk, u32 loc_addr, u32 rmt_addr, int dif)
  1808. {
  1809. struct inet_sock *inet = inet_sk(sk);
  1810. struct ip_mc_socklist *pmc;
  1811. struct ip_sf_socklist *psl;
  1812. int i;
  1813. if (!MULTICAST(loc_addr))
  1814. return 1;
  1815. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1816. if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
  1817. pmc->multi.imr_ifindex == dif)
  1818. break;
  1819. }
  1820. if (!pmc)
  1821. return 1;
  1822. psl = pmc->sflist;
  1823. if (!psl)
  1824. return pmc->sfmode == MCAST_EXCLUDE;
  1825. for (i=0; i<psl->sl_count; i++) {
  1826. if (psl->sl_addr[i] == rmt_addr)
  1827. break;
  1828. }
  1829. if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
  1830. return 0;
  1831. if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
  1832. return 0;
  1833. return 1;
  1834. }
  1835. /*
  1836. * A socket is closing.
  1837. */
  1838. void ip_mc_drop_socket(struct sock *sk)
  1839. {
  1840. struct inet_sock *inet = inet_sk(sk);
  1841. struct ip_mc_socklist *iml;
  1842. if (inet->mc_list == NULL)
  1843. return;
  1844. rtnl_lock();
  1845. while ((iml = inet->mc_list) != NULL) {
  1846. struct in_device *in_dev;
  1847. inet->mc_list = iml->next;
  1848. if ((in_dev = inetdev_by_index(iml->multi.imr_ifindex)) != NULL) {
  1849. (void) ip_mc_leave_src(sk, iml, in_dev);
  1850. ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
  1851. in_dev_put(in_dev);
  1852. }
  1853. sock_kfree_s(sk, iml, sizeof(*iml));
  1854. }
  1855. rtnl_unlock();
  1856. }
  1857. int ip_check_mc(struct in_device *in_dev, u32 mc_addr, u32 src_addr, u16 proto)
  1858. {
  1859. struct ip_mc_list *im;
  1860. struct ip_sf_list *psf;
  1861. int rv = 0;
  1862. read_lock(&in_dev->mc_list_lock);
  1863. for (im=in_dev->mc_list; im; im=im->next) {
  1864. if (im->multiaddr == mc_addr)
  1865. break;
  1866. }
  1867. if (im && proto == IPPROTO_IGMP) {
  1868. rv = 1;
  1869. } else if (im) {
  1870. if (src_addr) {
  1871. for (psf=im->sources; psf; psf=psf->sf_next) {
  1872. if (psf->sf_inaddr == src_addr)
  1873. break;
  1874. }
  1875. if (psf)
  1876. rv = psf->sf_count[MCAST_INCLUDE] ||
  1877. psf->sf_count[MCAST_EXCLUDE] !=
  1878. im->sfcount[MCAST_EXCLUDE];
  1879. else
  1880. rv = im->sfcount[MCAST_EXCLUDE] != 0;
  1881. } else
  1882. rv = 1; /* unspecified source; tentatively allow */
  1883. }
  1884. read_unlock(&in_dev->mc_list_lock);
  1885. return rv;
  1886. }
  1887. #if defined(CONFIG_PROC_FS)
  1888. struct igmp_mc_iter_state {
  1889. struct net_device *dev;
  1890. struct in_device *in_dev;
  1891. };
  1892. #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
  1893. static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
  1894. {
  1895. struct ip_mc_list *im = NULL;
  1896. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  1897. for (state->dev = dev_base, state->in_dev = NULL;
  1898. state->dev;
  1899. state->dev = state->dev->next) {
  1900. struct in_device *in_dev;
  1901. in_dev = in_dev_get(state->dev);
  1902. if (!in_dev)
  1903. continue;
  1904. read_lock(&in_dev->mc_list_lock);
  1905. im = in_dev->mc_list;
  1906. if (im) {
  1907. state->in_dev = in_dev;
  1908. break;
  1909. }
  1910. read_unlock(&in_dev->mc_list_lock);
  1911. in_dev_put(in_dev);
  1912. }
  1913. return im;
  1914. }
  1915. static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
  1916. {
  1917. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  1918. im = im->next;
  1919. while (!im) {
  1920. if (likely(state->in_dev != NULL)) {
  1921. read_unlock(&state->in_dev->mc_list_lock);
  1922. in_dev_put(state->in_dev);
  1923. }
  1924. state->dev = state->dev->next;
  1925. if (!state->dev) {
  1926. state->in_dev = NULL;
  1927. break;
  1928. }
  1929. state->in_dev = in_dev_get(state->dev);
  1930. if (!state->in_dev)
  1931. continue;
  1932. read_lock(&state->in_dev->mc_list_lock);
  1933. im = state->in_dev->mc_list;
  1934. }
  1935. return im;
  1936. }
  1937. static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
  1938. {
  1939. struct ip_mc_list *im = igmp_mc_get_first(seq);
  1940. if (im)
  1941. while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
  1942. --pos;
  1943. return pos ? NULL : im;
  1944. }
  1945. static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
  1946. {
  1947. read_lock(&dev_base_lock);
  1948. return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1949. }
  1950. static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1951. {
  1952. struct ip_mc_list *im;
  1953. if (v == SEQ_START_TOKEN)
  1954. im = igmp_mc_get_first(seq);
  1955. else
  1956. im = igmp_mc_get_next(seq, v);
  1957. ++*pos;
  1958. return im;
  1959. }
  1960. static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
  1961. {
  1962. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  1963. if (likely(state->in_dev != NULL)) {
  1964. read_unlock(&state->in_dev->mc_list_lock);
  1965. in_dev_put(state->in_dev);
  1966. state->in_dev = NULL;
  1967. }
  1968. state->dev = NULL;
  1969. read_unlock(&dev_base_lock);
  1970. }
  1971. static int igmp_mc_seq_show(struct seq_file *seq, void *v)
  1972. {
  1973. if (v == SEQ_START_TOKEN)
  1974. seq_puts(seq,
  1975. "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
  1976. else {
  1977. struct ip_mc_list *im = (struct ip_mc_list *)v;
  1978. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  1979. char *querier;
  1980. #ifdef CONFIG_IP_MULTICAST
  1981. querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
  1982. IGMP_V2_SEEN(state->in_dev) ? "V2" :
  1983. "V3";
  1984. #else
  1985. querier = "NONE";
  1986. #endif
  1987. if (state->in_dev->mc_list == im) {
  1988. seq_printf(seq, "%d\t%-10s: %5d %7s\n",
  1989. state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
  1990. }
  1991. seq_printf(seq,
  1992. "\t\t\t\t%08lX %5d %d:%08lX\t\t%d\n",
  1993. im->multiaddr, im->users,
  1994. im->tm_running, im->tm_running ?
  1995. jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
  1996. im->reporter);
  1997. }
  1998. return 0;
  1999. }
  2000. static struct seq_operations igmp_mc_seq_ops = {
  2001. .start = igmp_mc_seq_start,
  2002. .next = igmp_mc_seq_next,
  2003. .stop = igmp_mc_seq_stop,
  2004. .show = igmp_mc_seq_show,
  2005. };
  2006. static int igmp_mc_seq_open(struct inode *inode, struct file *file)
  2007. {
  2008. struct seq_file *seq;
  2009. int rc = -ENOMEM;
  2010. struct igmp_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  2011. if (!s)
  2012. goto out;
  2013. rc = seq_open(file, &igmp_mc_seq_ops);
  2014. if (rc)
  2015. goto out_kfree;
  2016. seq = file->private_data;
  2017. seq->private = s;
  2018. memset(s, 0, sizeof(*s));
  2019. out:
  2020. return rc;
  2021. out_kfree:
  2022. kfree(s);
  2023. goto out;
  2024. }
  2025. static struct file_operations igmp_mc_seq_fops = {
  2026. .owner = THIS_MODULE,
  2027. .open = igmp_mc_seq_open,
  2028. .read = seq_read,
  2029. .llseek = seq_lseek,
  2030. .release = seq_release_private,
  2031. };
  2032. struct igmp_mcf_iter_state {
  2033. struct net_device *dev;
  2034. struct in_device *idev;
  2035. struct ip_mc_list *im;
  2036. };
  2037. #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
  2038. static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
  2039. {
  2040. struct ip_sf_list *psf = NULL;
  2041. struct ip_mc_list *im = NULL;
  2042. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2043. for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
  2044. state->dev;
  2045. state->dev = state->dev->next) {
  2046. struct in_device *idev;
  2047. idev = in_dev_get(state->dev);
  2048. if (unlikely(idev == NULL))
  2049. continue;
  2050. read_lock(&idev->mc_list_lock);
  2051. im = idev->mc_list;
  2052. if (likely(im != NULL)) {
  2053. spin_lock_bh(&im->lock);
  2054. psf = im->sources;
  2055. if (likely(psf != NULL)) {
  2056. state->im = im;
  2057. state->idev = idev;
  2058. break;
  2059. }
  2060. spin_unlock_bh(&im->lock);
  2061. }
  2062. read_unlock(&idev->mc_list_lock);
  2063. in_dev_put(idev);
  2064. }
  2065. return psf;
  2066. }
  2067. static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
  2068. {
  2069. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2070. psf = psf->sf_next;
  2071. while (!psf) {
  2072. spin_unlock_bh(&state->im->lock);
  2073. state->im = state->im->next;
  2074. while (!state->im) {
  2075. if (likely(state->idev != NULL)) {
  2076. read_unlock(&state->idev->mc_list_lock);
  2077. in_dev_put(state->idev);
  2078. }
  2079. state->dev = state->dev->next;
  2080. if (!state->dev) {
  2081. state->idev = NULL;
  2082. goto out;
  2083. }
  2084. state->idev = in_dev_get(state->dev);
  2085. if (!state->idev)
  2086. continue;
  2087. read_lock(&state->idev->mc_list_lock);
  2088. state->im = state->idev->mc_list;
  2089. }
  2090. if (!state->im)
  2091. break;
  2092. spin_lock_bh(&state->im->lock);
  2093. psf = state->im->sources;
  2094. }
  2095. out:
  2096. return psf;
  2097. }
  2098. static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
  2099. {
  2100. struct ip_sf_list *psf = igmp_mcf_get_first(seq);
  2101. if (psf)
  2102. while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
  2103. --pos;
  2104. return pos ? NULL : psf;
  2105. }
  2106. static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
  2107. {
  2108. read_lock(&dev_base_lock);
  2109. return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2110. }
  2111. static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2112. {
  2113. struct ip_sf_list *psf;
  2114. if (v == SEQ_START_TOKEN)
  2115. psf = igmp_mcf_get_first(seq);
  2116. else
  2117. psf = igmp_mcf_get_next(seq, v);
  2118. ++*pos;
  2119. return psf;
  2120. }
  2121. static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
  2122. {
  2123. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2124. if (likely(state->im != NULL)) {
  2125. spin_unlock_bh(&state->im->lock);
  2126. state->im = NULL;
  2127. }
  2128. if (likely(state->idev != NULL)) {
  2129. read_unlock(&state->idev->mc_list_lock);
  2130. in_dev_put(state->idev);
  2131. state->idev = NULL;
  2132. }
  2133. state->dev = NULL;
  2134. read_unlock(&dev_base_lock);
  2135. }
  2136. static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
  2137. {
  2138. struct ip_sf_list *psf = (struct ip_sf_list *)v;
  2139. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2140. if (v == SEQ_START_TOKEN) {
  2141. seq_printf(seq,
  2142. "%3s %6s "
  2143. "%10s %10s %6s %6s\n", "Idx",
  2144. "Device", "MCA",
  2145. "SRC", "INC", "EXC");
  2146. } else {
  2147. seq_printf(seq,
  2148. "%3d %6.6s 0x%08x "
  2149. "0x%08x %6lu %6lu\n",
  2150. state->dev->ifindex, state->dev->name,
  2151. ntohl(state->im->multiaddr),
  2152. ntohl(psf->sf_inaddr),
  2153. psf->sf_count[MCAST_INCLUDE],
  2154. psf->sf_count[MCAST_EXCLUDE]);
  2155. }
  2156. return 0;
  2157. }
  2158. static struct seq_operations igmp_mcf_seq_ops = {
  2159. .start = igmp_mcf_seq_start,
  2160. .next = igmp_mcf_seq_next,
  2161. .stop = igmp_mcf_seq_stop,
  2162. .show = igmp_mcf_seq_show,
  2163. };
  2164. static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
  2165. {
  2166. struct seq_file *seq;
  2167. int rc = -ENOMEM;
  2168. struct igmp_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  2169. if (!s)
  2170. goto out;
  2171. rc = seq_open(file, &igmp_mcf_seq_ops);
  2172. if (rc)
  2173. goto out_kfree;
  2174. seq = file->private_data;
  2175. seq->private = s;
  2176. memset(s, 0, sizeof(*s));
  2177. out:
  2178. return rc;
  2179. out_kfree:
  2180. kfree(s);
  2181. goto out;
  2182. }
  2183. static struct file_operations igmp_mcf_seq_fops = {
  2184. .owner = THIS_MODULE,
  2185. .open = igmp_mcf_seq_open,
  2186. .read = seq_read,
  2187. .llseek = seq_lseek,
  2188. .release = seq_release_private,
  2189. };
  2190. int __init igmp_mc_proc_init(void)
  2191. {
  2192. proc_net_fops_create("igmp", S_IRUGO, &igmp_mc_seq_fops);
  2193. proc_net_fops_create("mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
  2194. return 0;
  2195. }
  2196. #endif
  2197. EXPORT_SYMBOL(ip_mc_dec_group);
  2198. EXPORT_SYMBOL(ip_mc_inc_group);
  2199. EXPORT_SYMBOL(ip_mc_join_group);