igmp.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462
  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(printk(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(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_EXCLUDE] = 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. rtnl_lock();
  1485. for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
  1486. if (iml->multi.imr_multiaddr.s_addr==imr->imr_multiaddr.s_addr &&
  1487. iml->multi.imr_address.s_addr==imr->imr_address.s_addr &&
  1488. (!imr->imr_ifindex || iml->multi.imr_ifindex==imr->imr_ifindex)) {
  1489. struct in_device *in_dev;
  1490. in_dev = inetdev_by_index(iml->multi.imr_ifindex);
  1491. if (in_dev)
  1492. (void) ip_mc_leave_src(sk, iml, in_dev);
  1493. *imlp = iml->next;
  1494. if (in_dev) {
  1495. ip_mc_dec_group(in_dev, imr->imr_multiaddr.s_addr);
  1496. in_dev_put(in_dev);
  1497. }
  1498. rtnl_unlock();
  1499. sock_kfree_s(sk, iml, sizeof(*iml));
  1500. return 0;
  1501. }
  1502. }
  1503. rtnl_unlock();
  1504. return -EADDRNOTAVAIL;
  1505. }
  1506. int ip_mc_source(int add, int omode, struct sock *sk, struct
  1507. ip_mreq_source *mreqs, int ifindex)
  1508. {
  1509. int err;
  1510. struct ip_mreqn imr;
  1511. u32 addr = mreqs->imr_multiaddr;
  1512. struct ip_mc_socklist *pmc;
  1513. struct in_device *in_dev = NULL;
  1514. struct inet_sock *inet = inet_sk(sk);
  1515. struct ip_sf_socklist *psl;
  1516. int i, j, rv;
  1517. if (!MULTICAST(addr))
  1518. return -EINVAL;
  1519. rtnl_shlock();
  1520. imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
  1521. imr.imr_address.s_addr = mreqs->imr_interface;
  1522. imr.imr_ifindex = ifindex;
  1523. in_dev = ip_mc_find_dev(&imr);
  1524. if (!in_dev) {
  1525. err = -ENODEV;
  1526. goto done;
  1527. }
  1528. err = -EADDRNOTAVAIL;
  1529. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1530. if (pmc->multi.imr_multiaddr.s_addr == imr.imr_multiaddr.s_addr
  1531. && pmc->multi.imr_ifindex == imr.imr_ifindex)
  1532. break;
  1533. }
  1534. if (!pmc) /* must have a prior join */
  1535. goto done;
  1536. /* if a source filter was set, must be the same mode as before */
  1537. if (pmc->sflist) {
  1538. if (pmc->sfmode != omode)
  1539. goto done;
  1540. } else if (pmc->sfmode != omode) {
  1541. /* allow mode switches for empty-set filters */
  1542. ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
  1543. ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
  1544. NULL, 0);
  1545. pmc->sfmode = omode;
  1546. }
  1547. psl = pmc->sflist;
  1548. if (!add) {
  1549. if (!psl)
  1550. goto done;
  1551. rv = !0;
  1552. for (i=0; i<psl->sl_count; i++) {
  1553. rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
  1554. sizeof(__u32));
  1555. if (rv == 0)
  1556. break;
  1557. }
  1558. if (rv) /* source not found */
  1559. goto done;
  1560. /* update the interface filter */
  1561. ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
  1562. &mreqs->imr_sourceaddr, 1);
  1563. for (j=i+1; j<psl->sl_count; j++)
  1564. psl->sl_addr[j-1] = psl->sl_addr[j];
  1565. psl->sl_count--;
  1566. err = 0;
  1567. goto done;
  1568. }
  1569. /* else, add a new source to the filter */
  1570. if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
  1571. err = -ENOBUFS;
  1572. goto done;
  1573. }
  1574. if (!psl || psl->sl_count == psl->sl_max) {
  1575. struct ip_sf_socklist *newpsl;
  1576. int count = IP_SFBLOCK;
  1577. if (psl)
  1578. count += psl->sl_max;
  1579. newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
  1580. IP_SFLSIZE(count), GFP_KERNEL);
  1581. if (!newpsl) {
  1582. err = -ENOBUFS;
  1583. goto done;
  1584. }
  1585. newpsl->sl_max = count;
  1586. newpsl->sl_count = count - IP_SFBLOCK;
  1587. if (psl) {
  1588. for (i=0; i<psl->sl_count; i++)
  1589. newpsl->sl_addr[i] = psl->sl_addr[i];
  1590. sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
  1591. }
  1592. pmc->sflist = psl = newpsl;
  1593. }
  1594. rv = 1; /* > 0 for insert logic below if sl_count is 0 */
  1595. for (i=0; i<psl->sl_count; i++) {
  1596. rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
  1597. sizeof(__u32));
  1598. if (rv == 0)
  1599. break;
  1600. }
  1601. if (rv == 0) /* address already there is an error */
  1602. goto done;
  1603. for (j=psl->sl_count-1; j>=i; j--)
  1604. psl->sl_addr[j+1] = psl->sl_addr[j];
  1605. psl->sl_addr[i] = mreqs->imr_sourceaddr;
  1606. psl->sl_count++;
  1607. err = 0;
  1608. /* update the interface list */
  1609. ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
  1610. &mreqs->imr_sourceaddr, 1);
  1611. done:
  1612. rtnl_shunlock();
  1613. return err;
  1614. }
  1615. int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
  1616. {
  1617. int err;
  1618. struct ip_mreqn imr;
  1619. u32 addr = msf->imsf_multiaddr;
  1620. struct ip_mc_socklist *pmc;
  1621. struct in_device *in_dev;
  1622. struct inet_sock *inet = inet_sk(sk);
  1623. struct ip_sf_socklist *newpsl, *psl;
  1624. if (!MULTICAST(addr))
  1625. return -EINVAL;
  1626. if (msf->imsf_fmode != MCAST_INCLUDE &&
  1627. msf->imsf_fmode != MCAST_EXCLUDE)
  1628. return -EINVAL;
  1629. rtnl_shlock();
  1630. imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
  1631. imr.imr_address.s_addr = msf->imsf_interface;
  1632. imr.imr_ifindex = ifindex;
  1633. in_dev = ip_mc_find_dev(&imr);
  1634. if (!in_dev) {
  1635. err = -ENODEV;
  1636. goto done;
  1637. }
  1638. err = -EADDRNOTAVAIL;
  1639. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1640. if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
  1641. pmc->multi.imr_ifindex == imr.imr_ifindex)
  1642. break;
  1643. }
  1644. if (!pmc) /* must have a prior join */
  1645. goto done;
  1646. if (msf->imsf_numsrc) {
  1647. newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
  1648. IP_SFLSIZE(msf->imsf_numsrc), GFP_KERNEL);
  1649. if (!newpsl) {
  1650. err = -ENOBUFS;
  1651. goto done;
  1652. }
  1653. newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
  1654. memcpy(newpsl->sl_addr, msf->imsf_slist,
  1655. msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
  1656. err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
  1657. msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
  1658. if (err) {
  1659. sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
  1660. goto done;
  1661. }
  1662. } else
  1663. newpsl = NULL;
  1664. psl = pmc->sflist;
  1665. if (psl) {
  1666. (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
  1667. psl->sl_count, psl->sl_addr, 0);
  1668. sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
  1669. } else
  1670. (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
  1671. 0, NULL, 0);
  1672. pmc->sflist = newpsl;
  1673. pmc->sfmode = msf->imsf_fmode;
  1674. done:
  1675. rtnl_shunlock();
  1676. return err;
  1677. }
  1678. int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
  1679. struct ip_msfilter __user *optval, int __user *optlen)
  1680. {
  1681. int err, len, count, copycount;
  1682. struct ip_mreqn imr;
  1683. u32 addr = msf->imsf_multiaddr;
  1684. struct ip_mc_socklist *pmc;
  1685. struct in_device *in_dev;
  1686. struct inet_sock *inet = inet_sk(sk);
  1687. struct ip_sf_socklist *psl;
  1688. if (!MULTICAST(addr))
  1689. return -EINVAL;
  1690. rtnl_shlock();
  1691. imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
  1692. imr.imr_address.s_addr = msf->imsf_interface;
  1693. imr.imr_ifindex = 0;
  1694. in_dev = ip_mc_find_dev(&imr);
  1695. if (!in_dev) {
  1696. err = -ENODEV;
  1697. goto done;
  1698. }
  1699. err = -EADDRNOTAVAIL;
  1700. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1701. if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
  1702. pmc->multi.imr_ifindex == imr.imr_ifindex)
  1703. break;
  1704. }
  1705. if (!pmc) /* must have a prior join */
  1706. goto done;
  1707. msf->imsf_fmode = pmc->sfmode;
  1708. psl = pmc->sflist;
  1709. rtnl_shunlock();
  1710. if (!psl) {
  1711. len = 0;
  1712. count = 0;
  1713. } else {
  1714. count = psl->sl_count;
  1715. }
  1716. copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
  1717. len = copycount * sizeof(psl->sl_addr[0]);
  1718. msf->imsf_numsrc = count;
  1719. if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
  1720. copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
  1721. return -EFAULT;
  1722. }
  1723. if (len &&
  1724. copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
  1725. return -EFAULT;
  1726. return 0;
  1727. done:
  1728. rtnl_shunlock();
  1729. return err;
  1730. }
  1731. int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
  1732. struct group_filter __user *optval, int __user *optlen)
  1733. {
  1734. int err, i, count, copycount;
  1735. struct sockaddr_in *psin;
  1736. u32 addr;
  1737. struct ip_mc_socklist *pmc;
  1738. struct inet_sock *inet = inet_sk(sk);
  1739. struct ip_sf_socklist *psl;
  1740. psin = (struct sockaddr_in *)&gsf->gf_group;
  1741. if (psin->sin_family != AF_INET)
  1742. return -EINVAL;
  1743. addr = psin->sin_addr.s_addr;
  1744. if (!MULTICAST(addr))
  1745. return -EINVAL;
  1746. rtnl_shlock();
  1747. err = -EADDRNOTAVAIL;
  1748. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1749. if (pmc->multi.imr_multiaddr.s_addr == addr &&
  1750. pmc->multi.imr_ifindex == gsf->gf_interface)
  1751. break;
  1752. }
  1753. if (!pmc) /* must have a prior join */
  1754. goto done;
  1755. gsf->gf_fmode = pmc->sfmode;
  1756. psl = pmc->sflist;
  1757. rtnl_shunlock();
  1758. count = psl ? psl->sl_count : 0;
  1759. copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
  1760. gsf->gf_numsrc = count;
  1761. if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
  1762. copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
  1763. return -EFAULT;
  1764. }
  1765. for (i=0; i<copycount; i++) {
  1766. struct sockaddr_in *psin;
  1767. struct sockaddr_storage ss;
  1768. psin = (struct sockaddr_in *)&ss;
  1769. memset(&ss, 0, sizeof(ss));
  1770. psin->sin_family = AF_INET;
  1771. psin->sin_addr.s_addr = psl->sl_addr[i];
  1772. if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
  1773. return -EFAULT;
  1774. }
  1775. return 0;
  1776. done:
  1777. rtnl_shunlock();
  1778. return err;
  1779. }
  1780. /*
  1781. * check if a multicast source filter allows delivery for a given <src,dst,intf>
  1782. */
  1783. int ip_mc_sf_allow(struct sock *sk, u32 loc_addr, u32 rmt_addr, int dif)
  1784. {
  1785. struct inet_sock *inet = inet_sk(sk);
  1786. struct ip_mc_socklist *pmc;
  1787. struct ip_sf_socklist *psl;
  1788. int i;
  1789. if (!MULTICAST(loc_addr))
  1790. return 1;
  1791. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1792. if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
  1793. pmc->multi.imr_ifindex == dif)
  1794. break;
  1795. }
  1796. if (!pmc)
  1797. return 1;
  1798. psl = pmc->sflist;
  1799. if (!psl)
  1800. return pmc->sfmode == MCAST_EXCLUDE;
  1801. for (i=0; i<psl->sl_count; i++) {
  1802. if (psl->sl_addr[i] == rmt_addr)
  1803. break;
  1804. }
  1805. if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
  1806. return 0;
  1807. if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
  1808. return 0;
  1809. return 1;
  1810. }
  1811. /*
  1812. * A socket is closing.
  1813. */
  1814. void ip_mc_drop_socket(struct sock *sk)
  1815. {
  1816. struct inet_sock *inet = inet_sk(sk);
  1817. struct ip_mc_socklist *iml;
  1818. if (inet->mc_list == NULL)
  1819. return;
  1820. rtnl_lock();
  1821. while ((iml = inet->mc_list) != NULL) {
  1822. struct in_device *in_dev;
  1823. inet->mc_list = iml->next;
  1824. if ((in_dev = inetdev_by_index(iml->multi.imr_ifindex)) != NULL) {
  1825. (void) ip_mc_leave_src(sk, iml, in_dev);
  1826. ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
  1827. in_dev_put(in_dev);
  1828. }
  1829. sock_kfree_s(sk, iml, sizeof(*iml));
  1830. }
  1831. rtnl_unlock();
  1832. }
  1833. int ip_check_mc(struct in_device *in_dev, u32 mc_addr, u32 src_addr, u16 proto)
  1834. {
  1835. struct ip_mc_list *im;
  1836. struct ip_sf_list *psf;
  1837. int rv = 0;
  1838. read_lock(&in_dev->mc_list_lock);
  1839. for (im=in_dev->mc_list; im; im=im->next) {
  1840. if (im->multiaddr == mc_addr)
  1841. break;
  1842. }
  1843. if (im && proto == IPPROTO_IGMP) {
  1844. rv = 1;
  1845. } else if (im) {
  1846. if (src_addr) {
  1847. for (psf=im->sources; psf; psf=psf->sf_next) {
  1848. if (psf->sf_inaddr == src_addr)
  1849. break;
  1850. }
  1851. if (psf)
  1852. rv = psf->sf_count[MCAST_INCLUDE] ||
  1853. psf->sf_count[MCAST_EXCLUDE] !=
  1854. im->sfcount[MCAST_EXCLUDE];
  1855. else
  1856. rv = im->sfcount[MCAST_EXCLUDE] != 0;
  1857. } else
  1858. rv = 1; /* unspecified source; tentatively allow */
  1859. }
  1860. read_unlock(&in_dev->mc_list_lock);
  1861. return rv;
  1862. }
  1863. #if defined(CONFIG_PROC_FS)
  1864. struct igmp_mc_iter_state {
  1865. struct net_device *dev;
  1866. struct in_device *in_dev;
  1867. };
  1868. #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
  1869. static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
  1870. {
  1871. struct ip_mc_list *im = NULL;
  1872. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  1873. for (state->dev = dev_base, state->in_dev = NULL;
  1874. state->dev;
  1875. state->dev = state->dev->next) {
  1876. struct in_device *in_dev;
  1877. in_dev = in_dev_get(state->dev);
  1878. if (!in_dev)
  1879. continue;
  1880. read_lock(&in_dev->mc_list_lock);
  1881. im = in_dev->mc_list;
  1882. if (im) {
  1883. state->in_dev = in_dev;
  1884. break;
  1885. }
  1886. read_unlock(&in_dev->mc_list_lock);
  1887. in_dev_put(in_dev);
  1888. }
  1889. return im;
  1890. }
  1891. static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
  1892. {
  1893. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  1894. im = im->next;
  1895. while (!im) {
  1896. if (likely(state->in_dev != NULL)) {
  1897. read_unlock(&state->in_dev->mc_list_lock);
  1898. in_dev_put(state->in_dev);
  1899. }
  1900. state->dev = state->dev->next;
  1901. if (!state->dev) {
  1902. state->in_dev = NULL;
  1903. break;
  1904. }
  1905. state->in_dev = in_dev_get(state->dev);
  1906. if (!state->in_dev)
  1907. continue;
  1908. read_lock(&state->in_dev->mc_list_lock);
  1909. im = state->in_dev->mc_list;
  1910. }
  1911. return im;
  1912. }
  1913. static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
  1914. {
  1915. struct ip_mc_list *im = igmp_mc_get_first(seq);
  1916. if (im)
  1917. while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
  1918. --pos;
  1919. return pos ? NULL : im;
  1920. }
  1921. static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
  1922. {
  1923. read_lock(&dev_base_lock);
  1924. return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1925. }
  1926. static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1927. {
  1928. struct ip_mc_list *im;
  1929. if (v == SEQ_START_TOKEN)
  1930. im = igmp_mc_get_first(seq);
  1931. else
  1932. im = igmp_mc_get_next(seq, v);
  1933. ++*pos;
  1934. return im;
  1935. }
  1936. static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
  1937. {
  1938. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  1939. if (likely(state->in_dev != NULL)) {
  1940. read_unlock(&state->in_dev->mc_list_lock);
  1941. in_dev_put(state->in_dev);
  1942. state->in_dev = NULL;
  1943. }
  1944. state->dev = NULL;
  1945. read_unlock(&dev_base_lock);
  1946. }
  1947. static int igmp_mc_seq_show(struct seq_file *seq, void *v)
  1948. {
  1949. if (v == SEQ_START_TOKEN)
  1950. seq_puts(seq,
  1951. "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
  1952. else {
  1953. struct ip_mc_list *im = (struct ip_mc_list *)v;
  1954. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  1955. char *querier;
  1956. #ifdef CONFIG_IP_MULTICAST
  1957. querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
  1958. IGMP_V2_SEEN(state->in_dev) ? "V2" :
  1959. "V3";
  1960. #else
  1961. querier = "NONE";
  1962. #endif
  1963. if (state->in_dev->mc_list == im) {
  1964. seq_printf(seq, "%d\t%-10s: %5d %7s\n",
  1965. state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
  1966. }
  1967. seq_printf(seq,
  1968. "\t\t\t\t%08lX %5d %d:%08lX\t\t%d\n",
  1969. im->multiaddr, im->users,
  1970. im->tm_running, im->tm_running ?
  1971. jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
  1972. im->reporter);
  1973. }
  1974. return 0;
  1975. }
  1976. static struct seq_operations igmp_mc_seq_ops = {
  1977. .start = igmp_mc_seq_start,
  1978. .next = igmp_mc_seq_next,
  1979. .stop = igmp_mc_seq_stop,
  1980. .show = igmp_mc_seq_show,
  1981. };
  1982. static int igmp_mc_seq_open(struct inode *inode, struct file *file)
  1983. {
  1984. struct seq_file *seq;
  1985. int rc = -ENOMEM;
  1986. struct igmp_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  1987. if (!s)
  1988. goto out;
  1989. rc = seq_open(file, &igmp_mc_seq_ops);
  1990. if (rc)
  1991. goto out_kfree;
  1992. seq = file->private_data;
  1993. seq->private = s;
  1994. memset(s, 0, sizeof(*s));
  1995. out:
  1996. return rc;
  1997. out_kfree:
  1998. kfree(s);
  1999. goto out;
  2000. }
  2001. static struct file_operations igmp_mc_seq_fops = {
  2002. .owner = THIS_MODULE,
  2003. .open = igmp_mc_seq_open,
  2004. .read = seq_read,
  2005. .llseek = seq_lseek,
  2006. .release = seq_release_private,
  2007. };
  2008. struct igmp_mcf_iter_state {
  2009. struct net_device *dev;
  2010. struct in_device *idev;
  2011. struct ip_mc_list *im;
  2012. };
  2013. #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
  2014. static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
  2015. {
  2016. struct ip_sf_list *psf = NULL;
  2017. struct ip_mc_list *im = NULL;
  2018. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2019. for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
  2020. state->dev;
  2021. state->dev = state->dev->next) {
  2022. struct in_device *idev;
  2023. idev = in_dev_get(state->dev);
  2024. if (unlikely(idev == NULL))
  2025. continue;
  2026. read_lock(&idev->mc_list_lock);
  2027. im = idev->mc_list;
  2028. if (likely(im != NULL)) {
  2029. spin_lock_bh(&im->lock);
  2030. psf = im->sources;
  2031. if (likely(psf != NULL)) {
  2032. state->im = im;
  2033. state->idev = idev;
  2034. break;
  2035. }
  2036. spin_unlock_bh(&im->lock);
  2037. }
  2038. read_unlock(&idev->mc_list_lock);
  2039. in_dev_put(idev);
  2040. }
  2041. return psf;
  2042. }
  2043. static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
  2044. {
  2045. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2046. psf = psf->sf_next;
  2047. while (!psf) {
  2048. spin_unlock_bh(&state->im->lock);
  2049. state->im = state->im->next;
  2050. while (!state->im) {
  2051. if (likely(state->idev != NULL)) {
  2052. read_unlock(&state->idev->mc_list_lock);
  2053. in_dev_put(state->idev);
  2054. }
  2055. state->dev = state->dev->next;
  2056. if (!state->dev) {
  2057. state->idev = NULL;
  2058. goto out;
  2059. }
  2060. state->idev = in_dev_get(state->dev);
  2061. if (!state->idev)
  2062. continue;
  2063. read_lock(&state->idev->mc_list_lock);
  2064. state->im = state->idev->mc_list;
  2065. }
  2066. if (!state->im)
  2067. break;
  2068. spin_lock_bh(&state->im->lock);
  2069. psf = state->im->sources;
  2070. }
  2071. out:
  2072. return psf;
  2073. }
  2074. static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
  2075. {
  2076. struct ip_sf_list *psf = igmp_mcf_get_first(seq);
  2077. if (psf)
  2078. while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
  2079. --pos;
  2080. return pos ? NULL : psf;
  2081. }
  2082. static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
  2083. {
  2084. read_lock(&dev_base_lock);
  2085. return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2086. }
  2087. static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2088. {
  2089. struct ip_sf_list *psf;
  2090. if (v == SEQ_START_TOKEN)
  2091. psf = igmp_mcf_get_first(seq);
  2092. else
  2093. psf = igmp_mcf_get_next(seq, v);
  2094. ++*pos;
  2095. return psf;
  2096. }
  2097. static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
  2098. {
  2099. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2100. if (likely(state->im != NULL)) {
  2101. spin_unlock_bh(&state->im->lock);
  2102. state->im = NULL;
  2103. }
  2104. if (likely(state->idev != NULL)) {
  2105. read_unlock(&state->idev->mc_list_lock);
  2106. in_dev_put(state->idev);
  2107. state->idev = NULL;
  2108. }
  2109. state->dev = NULL;
  2110. read_unlock(&dev_base_lock);
  2111. }
  2112. static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
  2113. {
  2114. struct ip_sf_list *psf = (struct ip_sf_list *)v;
  2115. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2116. if (v == SEQ_START_TOKEN) {
  2117. seq_printf(seq,
  2118. "%3s %6s "
  2119. "%10s %10s %6s %6s\n", "Idx",
  2120. "Device", "MCA",
  2121. "SRC", "INC", "EXC");
  2122. } else {
  2123. seq_printf(seq,
  2124. "%3d %6.6s 0x%08x "
  2125. "0x%08x %6lu %6lu\n",
  2126. state->dev->ifindex, state->dev->name,
  2127. ntohl(state->im->multiaddr),
  2128. ntohl(psf->sf_inaddr),
  2129. psf->sf_count[MCAST_INCLUDE],
  2130. psf->sf_count[MCAST_EXCLUDE]);
  2131. }
  2132. return 0;
  2133. }
  2134. static struct seq_operations igmp_mcf_seq_ops = {
  2135. .start = igmp_mcf_seq_start,
  2136. .next = igmp_mcf_seq_next,
  2137. .stop = igmp_mcf_seq_stop,
  2138. .show = igmp_mcf_seq_show,
  2139. };
  2140. static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
  2141. {
  2142. struct seq_file *seq;
  2143. int rc = -ENOMEM;
  2144. struct igmp_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  2145. if (!s)
  2146. goto out;
  2147. rc = seq_open(file, &igmp_mcf_seq_ops);
  2148. if (rc)
  2149. goto out_kfree;
  2150. seq = file->private_data;
  2151. seq->private = s;
  2152. memset(s, 0, sizeof(*s));
  2153. out:
  2154. return rc;
  2155. out_kfree:
  2156. kfree(s);
  2157. goto out;
  2158. }
  2159. static struct file_operations igmp_mcf_seq_fops = {
  2160. .owner = THIS_MODULE,
  2161. .open = igmp_mcf_seq_open,
  2162. .read = seq_read,
  2163. .llseek = seq_lseek,
  2164. .release = seq_release_private,
  2165. };
  2166. int __init igmp_mc_proc_init(void)
  2167. {
  2168. proc_net_fops_create("igmp", S_IRUGO, &igmp_mc_seq_fops);
  2169. proc_net_fops_create("mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
  2170. return 0;
  2171. }
  2172. #endif
  2173. EXPORT_SYMBOL(ip_mc_dec_group);
  2174. EXPORT_SYMBOL(ip_mc_inc_group);
  2175. EXPORT_SYMBOL(ip_mc_join_group);