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