igmp.c 59 KB

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