igmp.c 62 KB

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