igmp.c 61 KB

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