igmp.c 62 KB

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