igmp.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690
  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 <linux/slab.h>
  74. #include <asm/uaccess.h>
  75. #include <asm/system.h>
  76. #include <linux/types.h>
  77. #include <linux/kernel.h>
  78. #include <linux/jiffies.h>
  79. #include <linux/string.h>
  80. #include <linux/socket.h>
  81. #include <linux/sockios.h>
  82. #include <linux/in.h>
  83. #include <linux/inet.h>
  84. #include <linux/netdevice.h>
  85. #include <linux/skbuff.h>
  86. #include <linux/inetdevice.h>
  87. #include <linux/igmp.h>
  88. #include <linux/if_arp.h>
  89. #include <linux/rtnetlink.h>
  90. #include <linux/times.h>
  91. #include <net/net_namespace.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) \
  123. (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
  124. IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
  125. ((in_dev)->mr_v1_seen && \
  126. time_before(jiffies, (in_dev)->mr_v1_seen)))
  127. #define IGMP_V2_SEEN(in_dev) \
  128. (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
  129. IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
  130. ((in_dev)->mr_v2_seen && \
  131. time_before(jiffies, (in_dev)->mr_v2_seen)))
  132. static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
  133. static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
  134. static void igmpv3_clear_delrec(struct in_device *in_dev);
  135. static int sf_setstate(struct ip_mc_list *pmc);
  136. static void sf_markstate(struct ip_mc_list *pmc);
  137. #endif
  138. static void ip_mc_clear_src(struct ip_mc_list *pmc);
  139. static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
  140. int sfcount, __be32 *psfsrc, int delta);
  141. static void ip_ma_put(struct ip_mc_list *im)
  142. {
  143. if (atomic_dec_and_test(&im->refcnt)) {
  144. in_dev_put(im->interface);
  145. kfree(im);
  146. }
  147. }
  148. #ifdef CONFIG_IP_MULTICAST
  149. /*
  150. * Timer management
  151. */
  152. static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
  153. {
  154. spin_lock_bh(&im->lock);
  155. if (del_timer(&im->timer))
  156. atomic_dec(&im->refcnt);
  157. im->tm_running = 0;
  158. im->reporter = 0;
  159. im->unsolicit_count = 0;
  160. spin_unlock_bh(&im->lock);
  161. }
  162. /* It must be called with locked im->lock */
  163. static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
  164. {
  165. int tv = net_random() % max_delay;
  166. im->tm_running = 1;
  167. if (!mod_timer(&im->timer, jiffies+tv+2))
  168. atomic_inc(&im->refcnt);
  169. }
  170. static void igmp_gq_start_timer(struct in_device *in_dev)
  171. {
  172. int tv = net_random() % in_dev->mr_maxdelay;
  173. in_dev->mr_gq_running = 1;
  174. if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
  175. in_dev_hold(in_dev);
  176. }
  177. static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
  178. {
  179. int tv = net_random() % delay;
  180. if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
  181. in_dev_hold(in_dev);
  182. }
  183. static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
  184. {
  185. spin_lock_bh(&im->lock);
  186. im->unsolicit_count = 0;
  187. if (del_timer(&im->timer)) {
  188. if ((long)(im->timer.expires-jiffies) < max_delay) {
  189. add_timer(&im->timer);
  190. im->tm_running = 1;
  191. spin_unlock_bh(&im->lock);
  192. return;
  193. }
  194. atomic_dec(&im->refcnt);
  195. }
  196. igmp_start_timer(im, max_delay);
  197. spin_unlock_bh(&im->lock);
  198. }
  199. /*
  200. * Send an IGMP report.
  201. */
  202. #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
  203. static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
  204. int gdeleted, int sdeleted)
  205. {
  206. switch (type) {
  207. case IGMPV3_MODE_IS_INCLUDE:
  208. case IGMPV3_MODE_IS_EXCLUDE:
  209. if (gdeleted || sdeleted)
  210. return 0;
  211. if (!(pmc->gsquery && !psf->sf_gsresp)) {
  212. if (pmc->sfmode == MCAST_INCLUDE)
  213. return 1;
  214. /* don't include if this source is excluded
  215. * in all filters
  216. */
  217. if (psf->sf_count[MCAST_INCLUDE])
  218. return type == IGMPV3_MODE_IS_INCLUDE;
  219. return pmc->sfcount[MCAST_EXCLUDE] ==
  220. psf->sf_count[MCAST_EXCLUDE];
  221. }
  222. return 0;
  223. case IGMPV3_CHANGE_TO_INCLUDE:
  224. if (gdeleted || sdeleted)
  225. return 0;
  226. return psf->sf_count[MCAST_INCLUDE] != 0;
  227. case IGMPV3_CHANGE_TO_EXCLUDE:
  228. if (gdeleted || sdeleted)
  229. return 0;
  230. if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
  231. psf->sf_count[MCAST_INCLUDE])
  232. return 0;
  233. return pmc->sfcount[MCAST_EXCLUDE] ==
  234. psf->sf_count[MCAST_EXCLUDE];
  235. case IGMPV3_ALLOW_NEW_SOURCES:
  236. if (gdeleted || !psf->sf_crcount)
  237. return 0;
  238. return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
  239. case IGMPV3_BLOCK_OLD_SOURCES:
  240. if (pmc->sfmode == MCAST_INCLUDE)
  241. return gdeleted || (psf->sf_crcount && sdeleted);
  242. return psf->sf_crcount && !gdeleted && !sdeleted;
  243. }
  244. return 0;
  245. }
  246. static int
  247. igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
  248. {
  249. struct ip_sf_list *psf;
  250. int scount = 0;
  251. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  252. if (!is_in(pmc, psf, type, gdeleted, sdeleted))
  253. continue;
  254. scount++;
  255. }
  256. return scount;
  257. }
  258. static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
  259. {
  260. struct sk_buff *skb;
  261. struct rtable *rt;
  262. struct iphdr *pip;
  263. struct igmpv3_report *pig;
  264. struct net *net = dev_net(dev);
  265. skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
  266. if (skb == NULL)
  267. return NULL;
  268. {
  269. struct flowi fl = { .oif = dev->ifindex,
  270. .nl_u = { .ip4_u = {
  271. .daddr = IGMPV3_ALL_MCR } },
  272. .proto = IPPROTO_IGMP };
  273. if (ip_route_output_key(net, &rt, &fl)) {
  274. kfree_skb(skb);
  275. return NULL;
  276. }
  277. }
  278. if (rt->rt_src == 0) {
  279. kfree_skb(skb);
  280. ip_rt_put(rt);
  281. return NULL;
  282. }
  283. skb_dst_set(skb, &rt->dst);
  284. skb->dev = dev;
  285. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  286. skb_reset_network_header(skb);
  287. pip = ip_hdr(skb);
  288. skb_put(skb, sizeof(struct iphdr) + 4);
  289. pip->version = 4;
  290. pip->ihl = (sizeof(struct iphdr)+4)>>2;
  291. pip->tos = 0xc0;
  292. pip->frag_off = htons(IP_DF);
  293. pip->ttl = 1;
  294. pip->daddr = rt->rt_dst;
  295. pip->saddr = rt->rt_src;
  296. pip->protocol = IPPROTO_IGMP;
  297. pip->tot_len = 0; /* filled in later */
  298. ip_select_ident(pip, &rt->dst, NULL);
  299. ((u8*)&pip[1])[0] = IPOPT_RA;
  300. ((u8*)&pip[1])[1] = 4;
  301. ((u8*)&pip[1])[2] = 0;
  302. ((u8*)&pip[1])[3] = 0;
  303. skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
  304. skb_put(skb, sizeof(*pig));
  305. pig = igmpv3_report_hdr(skb);
  306. pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
  307. pig->resv1 = 0;
  308. pig->csum = 0;
  309. pig->resv2 = 0;
  310. pig->ngrec = 0;
  311. return skb;
  312. }
  313. static int igmpv3_sendpack(struct sk_buff *skb)
  314. {
  315. struct igmphdr *pig = igmp_hdr(skb);
  316. const int igmplen = skb->tail - skb->transport_header;
  317. pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
  318. return ip_local_out(skb);
  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 = igmpv3_report_hdr(skb);
  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 ? igmpv3_report_hdr(skb) : 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. struct net *net = dev_net(dev);
  567. __be32 group = pmc ? pmc->multiaddr : 0;
  568. __be32 dst;
  569. if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
  570. return igmpv3_send_report(in_dev, pmc);
  571. else if (type == IGMP_HOST_LEAVE_MESSAGE)
  572. dst = IGMP_ALL_ROUTER;
  573. else
  574. dst = group;
  575. {
  576. struct flowi fl = { .oif = dev->ifindex,
  577. .nl_u = { .ip4_u = { .daddr = dst } },
  578. .proto = IPPROTO_IGMP };
  579. if (ip_route_output_key(net, &rt, &fl))
  580. return -1;
  581. }
  582. if (rt->rt_src == 0) {
  583. ip_rt_put(rt);
  584. return -1;
  585. }
  586. skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
  587. if (skb == NULL) {
  588. ip_rt_put(rt);
  589. return -1;
  590. }
  591. skb_dst_set(skb, &rt->dst);
  592. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  593. skb_reset_network_header(skb);
  594. iph = ip_hdr(skb);
  595. skb_put(skb, sizeof(struct iphdr) + 4);
  596. iph->version = 4;
  597. iph->ihl = (sizeof(struct iphdr)+4)>>2;
  598. iph->tos = 0xc0;
  599. iph->frag_off = htons(IP_DF);
  600. iph->ttl = 1;
  601. iph->daddr = dst;
  602. iph->saddr = rt->rt_src;
  603. iph->protocol = IPPROTO_IGMP;
  604. ip_select_ident(iph, &rt->dst, NULL);
  605. ((u8*)&iph[1])[0] = IPOPT_RA;
  606. ((u8*)&iph[1])[1] = 4;
  607. ((u8*)&iph[1])[2] = 0;
  608. ((u8*)&iph[1])[3] = 0;
  609. ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
  610. ih->type = type;
  611. ih->code = 0;
  612. ih->csum = 0;
  613. ih->group = group;
  614. ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
  615. return ip_local_out(skb);
  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 = igmp_hdr(skb);
  732. struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
  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 if (IGMP_V1_SEEN(in_dev)) {
  759. /* This is a v3 query with v1 queriers present */
  760. max_delay = IGMP_Query_Response_Interval;
  761. group = 0;
  762. } else if (IGMP_V2_SEEN(in_dev)) {
  763. /* this is a v3 query with v2 queriers present;
  764. * Interpretation of the max_delay code is problematic here.
  765. * A real v2 host would use ih_code directly, while v3 has a
  766. * different encoding. We use the v3 encoding as more likely
  767. * to be intended in a v3 query.
  768. */
  769. max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
  770. } else { /* v3 */
  771. if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
  772. return;
  773. ih3 = igmpv3_query_hdr(skb);
  774. if (ih3->nsrcs) {
  775. if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
  776. + ntohs(ih3->nsrcs)*sizeof(__be32)))
  777. return;
  778. ih3 = igmpv3_query_hdr(skb);
  779. }
  780. max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
  781. if (!max_delay)
  782. max_delay = 1; /* can't mod w/ 0 */
  783. in_dev->mr_maxdelay = max_delay;
  784. if (ih3->qrv)
  785. in_dev->mr_qrv = ih3->qrv;
  786. if (!group) { /* general query */
  787. if (ih3->nsrcs)
  788. return; /* no sources allowed */
  789. igmp_gq_start_timer(in_dev);
  790. return;
  791. }
  792. /* mark sources to include, if group & source-specific */
  793. mark = ih3->nsrcs != 0;
  794. }
  795. /*
  796. * - Start the timers in all of our membership records
  797. * that the query applies to for the interface on
  798. * which the query arrived excl. those that belong
  799. * to a "local" group (224.0.0.X)
  800. * - For timers already running check if they need to
  801. * be reset.
  802. * - Use the igmp->igmp_code field as the maximum
  803. * delay possible
  804. */
  805. read_lock(&in_dev->mc_list_lock);
  806. for (im=in_dev->mc_list; im!=NULL; im=im->next) {
  807. int changed;
  808. if (group && group != im->multiaddr)
  809. continue;
  810. if (im->multiaddr == IGMP_ALL_HOSTS)
  811. continue;
  812. spin_lock_bh(&im->lock);
  813. if (im->tm_running)
  814. im->gsquery = im->gsquery && mark;
  815. else
  816. im->gsquery = mark;
  817. changed = !im->gsquery ||
  818. igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
  819. spin_unlock_bh(&im->lock);
  820. if (changed)
  821. igmp_mod_timer(im, max_delay);
  822. }
  823. read_unlock(&in_dev->mc_list_lock);
  824. }
  825. /* called in rcu_read_lock() section */
  826. int igmp_rcv(struct sk_buff *skb)
  827. {
  828. /* This basically follows the spec line by line -- see RFC1112 */
  829. struct igmphdr *ih;
  830. struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
  831. int len = skb->len;
  832. if (in_dev == NULL)
  833. goto drop;
  834. if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
  835. goto drop;
  836. switch (skb->ip_summed) {
  837. case CHECKSUM_COMPLETE:
  838. if (!csum_fold(skb->csum))
  839. break;
  840. /* fall through */
  841. case CHECKSUM_NONE:
  842. skb->csum = 0;
  843. if (__skb_checksum_complete(skb))
  844. goto drop;
  845. }
  846. ih = igmp_hdr(skb);
  847. switch (ih->type) {
  848. case IGMP_HOST_MEMBERSHIP_QUERY:
  849. igmp_heard_query(in_dev, skb, len);
  850. break;
  851. case IGMP_HOST_MEMBERSHIP_REPORT:
  852. case IGMPV2_HOST_MEMBERSHIP_REPORT:
  853. /* Is it our report looped back? */
  854. if (skb_rtable(skb)->fl.iif == 0)
  855. break;
  856. /* don't rely on MC router hearing unicast reports */
  857. if (skb->pkt_type == PACKET_MULTICAST ||
  858. skb->pkt_type == PACKET_BROADCAST)
  859. igmp_heard_report(in_dev, ih->group);
  860. break;
  861. case IGMP_PIM:
  862. #ifdef CONFIG_IP_PIMSM_V1
  863. return pim_rcv_v1(skb);
  864. #endif
  865. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  866. case IGMP_DVMRP:
  867. case IGMP_TRACE:
  868. case IGMP_HOST_LEAVE_MESSAGE:
  869. case IGMP_MTRACE:
  870. case IGMP_MTRACE_RESP:
  871. break;
  872. default:
  873. break;
  874. }
  875. drop:
  876. kfree_skb(skb);
  877. return 0;
  878. }
  879. #endif
  880. /*
  881. * Add a filter to a device
  882. */
  883. static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
  884. {
  885. char buf[MAX_ADDR_LEN];
  886. struct net_device *dev = in_dev->dev;
  887. /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
  888. We will get multicast token leakage, when IFF_MULTICAST
  889. is changed. This check should be done in dev->set_multicast_list
  890. routine. Something sort of:
  891. if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
  892. --ANK
  893. */
  894. if (arp_mc_map(addr, buf, dev, 0) == 0)
  895. dev_mc_add(dev, buf);
  896. }
  897. /*
  898. * Remove a filter from a device
  899. */
  900. static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
  901. {
  902. char buf[MAX_ADDR_LEN];
  903. struct net_device *dev = in_dev->dev;
  904. if (arp_mc_map(addr, buf, dev, 0) == 0)
  905. dev_mc_del(dev, buf);
  906. }
  907. #ifdef CONFIG_IP_MULTICAST
  908. /*
  909. * deleted ip_mc_list manipulation
  910. */
  911. static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
  912. {
  913. struct ip_mc_list *pmc;
  914. /* this is an "ip_mc_list" for convenience; only the fields below
  915. * are actually used. In particular, the refcnt and users are not
  916. * used for management of the delete list. Using the same structure
  917. * for deleted items allows change reports to use common code with
  918. * non-deleted or query-response MCA's.
  919. */
  920. pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
  921. if (!pmc)
  922. return;
  923. spin_lock_bh(&im->lock);
  924. pmc->interface = im->interface;
  925. in_dev_hold(in_dev);
  926. pmc->multiaddr = im->multiaddr;
  927. pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  928. IGMP_Unsolicited_Report_Count;
  929. pmc->sfmode = im->sfmode;
  930. if (pmc->sfmode == MCAST_INCLUDE) {
  931. struct ip_sf_list *psf;
  932. pmc->tomb = im->tomb;
  933. pmc->sources = im->sources;
  934. im->tomb = im->sources = NULL;
  935. for (psf=pmc->sources; psf; psf=psf->sf_next)
  936. psf->sf_crcount = pmc->crcount;
  937. }
  938. spin_unlock_bh(&im->lock);
  939. spin_lock_bh(&in_dev->mc_tomb_lock);
  940. pmc->next = in_dev->mc_tomb;
  941. in_dev->mc_tomb = pmc;
  942. spin_unlock_bh(&in_dev->mc_tomb_lock);
  943. }
  944. static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
  945. {
  946. struct ip_mc_list *pmc, *pmc_prev;
  947. struct ip_sf_list *psf, *psf_next;
  948. spin_lock_bh(&in_dev->mc_tomb_lock);
  949. pmc_prev = NULL;
  950. for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
  951. if (pmc->multiaddr == multiaddr)
  952. break;
  953. pmc_prev = pmc;
  954. }
  955. if (pmc) {
  956. if (pmc_prev)
  957. pmc_prev->next = pmc->next;
  958. else
  959. in_dev->mc_tomb = pmc->next;
  960. }
  961. spin_unlock_bh(&in_dev->mc_tomb_lock);
  962. if (pmc) {
  963. for (psf=pmc->tomb; psf; psf=psf_next) {
  964. psf_next = psf->sf_next;
  965. kfree(psf);
  966. }
  967. in_dev_put(pmc->interface);
  968. kfree(pmc);
  969. }
  970. }
  971. static void igmpv3_clear_delrec(struct in_device *in_dev)
  972. {
  973. struct ip_mc_list *pmc, *nextpmc;
  974. spin_lock_bh(&in_dev->mc_tomb_lock);
  975. pmc = in_dev->mc_tomb;
  976. in_dev->mc_tomb = NULL;
  977. spin_unlock_bh(&in_dev->mc_tomb_lock);
  978. for (; pmc; pmc = nextpmc) {
  979. nextpmc = pmc->next;
  980. ip_mc_clear_src(pmc);
  981. in_dev_put(pmc->interface);
  982. kfree(pmc);
  983. }
  984. /* clear dead sources, too */
  985. read_lock(&in_dev->mc_list_lock);
  986. for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
  987. struct ip_sf_list *psf, *psf_next;
  988. spin_lock_bh(&pmc->lock);
  989. psf = pmc->tomb;
  990. pmc->tomb = NULL;
  991. spin_unlock_bh(&pmc->lock);
  992. for (; psf; psf=psf_next) {
  993. psf_next = psf->sf_next;
  994. kfree(psf);
  995. }
  996. }
  997. read_unlock(&in_dev->mc_list_lock);
  998. }
  999. #endif
  1000. static void igmp_group_dropped(struct ip_mc_list *im)
  1001. {
  1002. struct in_device *in_dev = im->interface;
  1003. #ifdef CONFIG_IP_MULTICAST
  1004. int reporter;
  1005. #endif
  1006. if (im->loaded) {
  1007. im->loaded = 0;
  1008. ip_mc_filter_del(in_dev, im->multiaddr);
  1009. }
  1010. #ifdef CONFIG_IP_MULTICAST
  1011. if (im->multiaddr == IGMP_ALL_HOSTS)
  1012. return;
  1013. reporter = im->reporter;
  1014. igmp_stop_timer(im);
  1015. if (!in_dev->dead) {
  1016. if (IGMP_V1_SEEN(in_dev))
  1017. goto done;
  1018. if (IGMP_V2_SEEN(in_dev)) {
  1019. if (reporter)
  1020. igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
  1021. goto done;
  1022. }
  1023. /* IGMPv3 */
  1024. igmpv3_add_delrec(in_dev, im);
  1025. igmp_ifc_event(in_dev);
  1026. }
  1027. done:
  1028. #endif
  1029. ip_mc_clear_src(im);
  1030. }
  1031. static void igmp_group_added(struct ip_mc_list *im)
  1032. {
  1033. struct in_device *in_dev = im->interface;
  1034. if (im->loaded == 0) {
  1035. im->loaded = 1;
  1036. ip_mc_filter_add(in_dev, im->multiaddr);
  1037. }
  1038. #ifdef CONFIG_IP_MULTICAST
  1039. if (im->multiaddr == IGMP_ALL_HOSTS)
  1040. return;
  1041. if (in_dev->dead)
  1042. return;
  1043. if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
  1044. spin_lock_bh(&im->lock);
  1045. igmp_start_timer(im, IGMP_Initial_Report_Delay);
  1046. spin_unlock_bh(&im->lock);
  1047. return;
  1048. }
  1049. /* else, v3 */
  1050. im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  1051. IGMP_Unsolicited_Report_Count;
  1052. igmp_ifc_event(in_dev);
  1053. #endif
  1054. }
  1055. /*
  1056. * Multicast list managers
  1057. */
  1058. /*
  1059. * A socket has joined a multicast group on device dev.
  1060. */
  1061. void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
  1062. {
  1063. struct ip_mc_list *im;
  1064. ASSERT_RTNL();
  1065. for (im=in_dev->mc_list; im; im=im->next) {
  1066. if (im->multiaddr == addr) {
  1067. im->users++;
  1068. ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
  1069. goto out;
  1070. }
  1071. }
  1072. im = kmalloc(sizeof(*im), GFP_KERNEL);
  1073. if (!im)
  1074. goto out;
  1075. im->users = 1;
  1076. im->interface = in_dev;
  1077. in_dev_hold(in_dev);
  1078. im->multiaddr = addr;
  1079. /* initial mode is (EX, empty) */
  1080. im->sfmode = MCAST_EXCLUDE;
  1081. im->sfcount[MCAST_INCLUDE] = 0;
  1082. im->sfcount[MCAST_EXCLUDE] = 1;
  1083. im->sources = NULL;
  1084. im->tomb = NULL;
  1085. im->crcount = 0;
  1086. atomic_set(&im->refcnt, 1);
  1087. spin_lock_init(&im->lock);
  1088. #ifdef CONFIG_IP_MULTICAST
  1089. im->tm_running = 0;
  1090. setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
  1091. im->unsolicit_count = IGMP_Unsolicited_Report_Count;
  1092. im->reporter = 0;
  1093. im->gsquery = 0;
  1094. #endif
  1095. im->loaded = 0;
  1096. write_lock_bh(&in_dev->mc_list_lock);
  1097. im->next = in_dev->mc_list;
  1098. in_dev->mc_list = im;
  1099. in_dev->mc_count++;
  1100. write_unlock_bh(&in_dev->mc_list_lock);
  1101. #ifdef CONFIG_IP_MULTICAST
  1102. igmpv3_del_delrec(in_dev, im->multiaddr);
  1103. #endif
  1104. igmp_group_added(im);
  1105. if (!in_dev->dead)
  1106. ip_rt_multicast_event(in_dev);
  1107. out:
  1108. return;
  1109. }
  1110. EXPORT_SYMBOL(ip_mc_inc_group);
  1111. /*
  1112. * Resend IGMP JOIN report; used for bonding.
  1113. */
  1114. void ip_mc_rejoin_group(struct ip_mc_list *im)
  1115. {
  1116. #ifdef CONFIG_IP_MULTICAST
  1117. struct in_device *in_dev = im->interface;
  1118. if (im->multiaddr == IGMP_ALL_HOSTS)
  1119. return;
  1120. /* a failover is happening and switches
  1121. * must be notified immediately */
  1122. if (IGMP_V1_SEEN(in_dev))
  1123. igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
  1124. else if (IGMP_V2_SEEN(in_dev))
  1125. igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
  1126. else
  1127. igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
  1128. #endif
  1129. }
  1130. EXPORT_SYMBOL(ip_mc_rejoin_group);
  1131. /*
  1132. * A socket has left a multicast group on device dev
  1133. */
  1134. void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
  1135. {
  1136. struct ip_mc_list *i, **ip;
  1137. ASSERT_RTNL();
  1138. for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
  1139. if (i->multiaddr == addr) {
  1140. if (--i->users == 0) {
  1141. write_lock_bh(&in_dev->mc_list_lock);
  1142. *ip = i->next;
  1143. in_dev->mc_count--;
  1144. write_unlock_bh(&in_dev->mc_list_lock);
  1145. igmp_group_dropped(i);
  1146. if (!in_dev->dead)
  1147. ip_rt_multicast_event(in_dev);
  1148. ip_ma_put(i);
  1149. return;
  1150. }
  1151. break;
  1152. }
  1153. }
  1154. }
  1155. EXPORT_SYMBOL(ip_mc_dec_group);
  1156. /* Device changing type */
  1157. void ip_mc_unmap(struct in_device *in_dev)
  1158. {
  1159. struct ip_mc_list *i;
  1160. ASSERT_RTNL();
  1161. for (i = in_dev->mc_list; i; i = i->next)
  1162. igmp_group_dropped(i);
  1163. }
  1164. void ip_mc_remap(struct in_device *in_dev)
  1165. {
  1166. struct ip_mc_list *i;
  1167. ASSERT_RTNL();
  1168. for (i = in_dev->mc_list; i; i = i->next)
  1169. igmp_group_added(i);
  1170. }
  1171. /* Device going down */
  1172. void ip_mc_down(struct in_device *in_dev)
  1173. {
  1174. struct ip_mc_list *i;
  1175. ASSERT_RTNL();
  1176. for (i=in_dev->mc_list; i; i=i->next)
  1177. igmp_group_dropped(i);
  1178. #ifdef CONFIG_IP_MULTICAST
  1179. in_dev->mr_ifc_count = 0;
  1180. if (del_timer(&in_dev->mr_ifc_timer))
  1181. __in_dev_put(in_dev);
  1182. in_dev->mr_gq_running = 0;
  1183. if (del_timer(&in_dev->mr_gq_timer))
  1184. __in_dev_put(in_dev);
  1185. igmpv3_clear_delrec(in_dev);
  1186. #endif
  1187. ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
  1188. }
  1189. void ip_mc_init_dev(struct in_device *in_dev)
  1190. {
  1191. ASSERT_RTNL();
  1192. in_dev->mc_tomb = NULL;
  1193. #ifdef CONFIG_IP_MULTICAST
  1194. in_dev->mr_gq_running = 0;
  1195. setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
  1196. (unsigned long)in_dev);
  1197. in_dev->mr_ifc_count = 0;
  1198. in_dev->mc_count = 0;
  1199. setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
  1200. (unsigned long)in_dev);
  1201. in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
  1202. #endif
  1203. rwlock_init(&in_dev->mc_list_lock);
  1204. spin_lock_init(&in_dev->mc_tomb_lock);
  1205. }
  1206. /* Device going up */
  1207. void ip_mc_up(struct in_device *in_dev)
  1208. {
  1209. struct ip_mc_list *i;
  1210. ASSERT_RTNL();
  1211. ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
  1212. for (i=in_dev->mc_list; i; i=i->next)
  1213. igmp_group_added(i);
  1214. }
  1215. /*
  1216. * Device is about to be destroyed: clean up.
  1217. */
  1218. void ip_mc_destroy_dev(struct in_device *in_dev)
  1219. {
  1220. struct ip_mc_list *i;
  1221. ASSERT_RTNL();
  1222. /* Deactivate timers */
  1223. ip_mc_down(in_dev);
  1224. write_lock_bh(&in_dev->mc_list_lock);
  1225. while ((i = in_dev->mc_list) != NULL) {
  1226. in_dev->mc_list = i->next;
  1227. in_dev->mc_count--;
  1228. write_unlock_bh(&in_dev->mc_list_lock);
  1229. igmp_group_dropped(i);
  1230. ip_ma_put(i);
  1231. write_lock_bh(&in_dev->mc_list_lock);
  1232. }
  1233. write_unlock_bh(&in_dev->mc_list_lock);
  1234. }
  1235. /* RTNL is locked */
  1236. static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
  1237. {
  1238. struct flowi fl = { .nl_u = { .ip4_u =
  1239. { .daddr = imr->imr_multiaddr.s_addr } } };
  1240. struct rtable *rt;
  1241. struct net_device *dev = NULL;
  1242. struct in_device *idev = NULL;
  1243. if (imr->imr_ifindex) {
  1244. idev = inetdev_by_index(net, imr->imr_ifindex);
  1245. return idev;
  1246. }
  1247. if (imr->imr_address.s_addr) {
  1248. dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
  1249. if (!dev)
  1250. return NULL;
  1251. }
  1252. if (!dev && !ip_route_output_key(net, &rt, &fl)) {
  1253. dev = rt->dst.dev;
  1254. ip_rt_put(rt);
  1255. }
  1256. if (dev) {
  1257. imr->imr_ifindex = dev->ifindex;
  1258. idev = __in_dev_get_rtnl(dev);
  1259. }
  1260. return idev;
  1261. }
  1262. /*
  1263. * Join a socket to a group
  1264. */
  1265. int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
  1266. int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
  1267. static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
  1268. __be32 *psfsrc)
  1269. {
  1270. struct ip_sf_list *psf, *psf_prev;
  1271. int rv = 0;
  1272. psf_prev = NULL;
  1273. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  1274. if (psf->sf_inaddr == *psfsrc)
  1275. break;
  1276. psf_prev = psf;
  1277. }
  1278. if (!psf || psf->sf_count[sfmode] == 0) {
  1279. /* source filter not found, or count wrong => bug */
  1280. return -ESRCH;
  1281. }
  1282. psf->sf_count[sfmode]--;
  1283. if (psf->sf_count[sfmode] == 0) {
  1284. ip_rt_multicast_event(pmc->interface);
  1285. }
  1286. if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
  1287. #ifdef CONFIG_IP_MULTICAST
  1288. struct in_device *in_dev = pmc->interface;
  1289. #endif
  1290. /* no more filters for this source */
  1291. if (psf_prev)
  1292. psf_prev->sf_next = psf->sf_next;
  1293. else
  1294. pmc->sources = psf->sf_next;
  1295. #ifdef CONFIG_IP_MULTICAST
  1296. if (psf->sf_oldin &&
  1297. !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
  1298. psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  1299. IGMP_Unsolicited_Report_Count;
  1300. psf->sf_next = pmc->tomb;
  1301. pmc->tomb = psf;
  1302. rv = 1;
  1303. } else
  1304. #endif
  1305. kfree(psf);
  1306. }
  1307. return rv;
  1308. }
  1309. #ifndef CONFIG_IP_MULTICAST
  1310. #define igmp_ifc_event(x) do { } while (0)
  1311. #endif
  1312. static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
  1313. int sfcount, __be32 *psfsrc, int delta)
  1314. {
  1315. struct ip_mc_list *pmc;
  1316. int changerec = 0;
  1317. int i, err;
  1318. if (!in_dev)
  1319. return -ENODEV;
  1320. read_lock(&in_dev->mc_list_lock);
  1321. for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
  1322. if (*pmca == pmc->multiaddr)
  1323. break;
  1324. }
  1325. if (!pmc) {
  1326. /* MCA not found?? bug */
  1327. read_unlock(&in_dev->mc_list_lock);
  1328. return -ESRCH;
  1329. }
  1330. spin_lock_bh(&pmc->lock);
  1331. read_unlock(&in_dev->mc_list_lock);
  1332. #ifdef CONFIG_IP_MULTICAST
  1333. sf_markstate(pmc);
  1334. #endif
  1335. if (!delta) {
  1336. err = -EINVAL;
  1337. if (!pmc->sfcount[sfmode])
  1338. goto out_unlock;
  1339. pmc->sfcount[sfmode]--;
  1340. }
  1341. err = 0;
  1342. for (i=0; i<sfcount; i++) {
  1343. int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1344. changerec |= rv > 0;
  1345. if (!err && rv < 0)
  1346. err = rv;
  1347. }
  1348. if (pmc->sfmode == MCAST_EXCLUDE &&
  1349. pmc->sfcount[MCAST_EXCLUDE] == 0 &&
  1350. pmc->sfcount[MCAST_INCLUDE]) {
  1351. #ifdef CONFIG_IP_MULTICAST
  1352. struct ip_sf_list *psf;
  1353. #endif
  1354. /* filter mode change */
  1355. pmc->sfmode = MCAST_INCLUDE;
  1356. #ifdef CONFIG_IP_MULTICAST
  1357. pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  1358. IGMP_Unsolicited_Report_Count;
  1359. in_dev->mr_ifc_count = pmc->crcount;
  1360. for (psf=pmc->sources; psf; psf = psf->sf_next)
  1361. psf->sf_crcount = 0;
  1362. igmp_ifc_event(pmc->interface);
  1363. } else if (sf_setstate(pmc) || changerec) {
  1364. igmp_ifc_event(pmc->interface);
  1365. #endif
  1366. }
  1367. out_unlock:
  1368. spin_unlock_bh(&pmc->lock);
  1369. return err;
  1370. }
  1371. /*
  1372. * Add multicast single-source filter to the interface list
  1373. */
  1374. static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
  1375. __be32 *psfsrc, int delta)
  1376. {
  1377. struct ip_sf_list *psf, *psf_prev;
  1378. psf_prev = NULL;
  1379. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  1380. if (psf->sf_inaddr == *psfsrc)
  1381. break;
  1382. psf_prev = psf;
  1383. }
  1384. if (!psf) {
  1385. psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
  1386. if (!psf)
  1387. return -ENOBUFS;
  1388. psf->sf_inaddr = *psfsrc;
  1389. if (psf_prev) {
  1390. psf_prev->sf_next = psf;
  1391. } else
  1392. pmc->sources = psf;
  1393. }
  1394. psf->sf_count[sfmode]++;
  1395. if (psf->sf_count[sfmode] == 1) {
  1396. ip_rt_multicast_event(pmc->interface);
  1397. }
  1398. return 0;
  1399. }
  1400. #ifdef CONFIG_IP_MULTICAST
  1401. static void sf_markstate(struct ip_mc_list *pmc)
  1402. {
  1403. struct ip_sf_list *psf;
  1404. int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
  1405. for (psf=pmc->sources; psf; psf=psf->sf_next)
  1406. if (pmc->sfcount[MCAST_EXCLUDE]) {
  1407. psf->sf_oldin = mca_xcount ==
  1408. psf->sf_count[MCAST_EXCLUDE] &&
  1409. !psf->sf_count[MCAST_INCLUDE];
  1410. } else
  1411. psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
  1412. }
  1413. static int sf_setstate(struct ip_mc_list *pmc)
  1414. {
  1415. struct ip_sf_list *psf, *dpsf;
  1416. int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
  1417. int qrv = pmc->interface->mr_qrv;
  1418. int new_in, rv;
  1419. rv = 0;
  1420. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  1421. if (pmc->sfcount[MCAST_EXCLUDE]) {
  1422. new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
  1423. !psf->sf_count[MCAST_INCLUDE];
  1424. } else
  1425. new_in = psf->sf_count[MCAST_INCLUDE] != 0;
  1426. if (new_in) {
  1427. if (!psf->sf_oldin) {
  1428. struct ip_sf_list *prev = NULL;
  1429. for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
  1430. if (dpsf->sf_inaddr == psf->sf_inaddr)
  1431. break;
  1432. prev = dpsf;
  1433. }
  1434. if (dpsf) {
  1435. if (prev)
  1436. prev->sf_next = dpsf->sf_next;
  1437. else
  1438. pmc->tomb = dpsf->sf_next;
  1439. kfree(dpsf);
  1440. }
  1441. psf->sf_crcount = qrv;
  1442. rv++;
  1443. }
  1444. } else if (psf->sf_oldin) {
  1445. psf->sf_crcount = 0;
  1446. /*
  1447. * add or update "delete" records if an active filter
  1448. * is now inactive
  1449. */
  1450. for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
  1451. if (dpsf->sf_inaddr == psf->sf_inaddr)
  1452. break;
  1453. if (!dpsf) {
  1454. dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
  1455. if (!dpsf)
  1456. continue;
  1457. *dpsf = *psf;
  1458. /* pmc->lock held by callers */
  1459. dpsf->sf_next = pmc->tomb;
  1460. pmc->tomb = dpsf;
  1461. }
  1462. dpsf->sf_crcount = qrv;
  1463. rv++;
  1464. }
  1465. }
  1466. return rv;
  1467. }
  1468. #endif
  1469. /*
  1470. * Add multicast source filter list to the interface list
  1471. */
  1472. static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
  1473. int sfcount, __be32 *psfsrc, int delta)
  1474. {
  1475. struct ip_mc_list *pmc;
  1476. int isexclude;
  1477. int i, err;
  1478. if (!in_dev)
  1479. return -ENODEV;
  1480. read_lock(&in_dev->mc_list_lock);
  1481. for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
  1482. if (*pmca == pmc->multiaddr)
  1483. break;
  1484. }
  1485. if (!pmc) {
  1486. /* MCA not found?? bug */
  1487. read_unlock(&in_dev->mc_list_lock);
  1488. return -ESRCH;
  1489. }
  1490. spin_lock_bh(&pmc->lock);
  1491. read_unlock(&in_dev->mc_list_lock);
  1492. #ifdef CONFIG_IP_MULTICAST
  1493. sf_markstate(pmc);
  1494. #endif
  1495. isexclude = pmc->sfmode == MCAST_EXCLUDE;
  1496. if (!delta)
  1497. pmc->sfcount[sfmode]++;
  1498. err = 0;
  1499. for (i=0; i<sfcount; i++) {
  1500. err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
  1501. if (err)
  1502. break;
  1503. }
  1504. if (err) {
  1505. int j;
  1506. pmc->sfcount[sfmode]--;
  1507. for (j=0; j<i; j++)
  1508. (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1509. } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
  1510. #ifdef CONFIG_IP_MULTICAST
  1511. struct ip_sf_list *psf;
  1512. in_dev = pmc->interface;
  1513. #endif
  1514. /* filter mode change */
  1515. if (pmc->sfcount[MCAST_EXCLUDE])
  1516. pmc->sfmode = MCAST_EXCLUDE;
  1517. else if (pmc->sfcount[MCAST_INCLUDE])
  1518. pmc->sfmode = MCAST_INCLUDE;
  1519. #ifdef CONFIG_IP_MULTICAST
  1520. /* else no filters; keep old mode for reports */
  1521. pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  1522. IGMP_Unsolicited_Report_Count;
  1523. in_dev->mr_ifc_count = pmc->crcount;
  1524. for (psf=pmc->sources; psf; psf = psf->sf_next)
  1525. psf->sf_crcount = 0;
  1526. igmp_ifc_event(in_dev);
  1527. } else if (sf_setstate(pmc)) {
  1528. igmp_ifc_event(in_dev);
  1529. #endif
  1530. }
  1531. spin_unlock_bh(&pmc->lock);
  1532. return err;
  1533. }
  1534. static void ip_mc_clear_src(struct ip_mc_list *pmc)
  1535. {
  1536. struct ip_sf_list *psf, *nextpsf;
  1537. for (psf=pmc->tomb; psf; psf=nextpsf) {
  1538. nextpsf = psf->sf_next;
  1539. kfree(psf);
  1540. }
  1541. pmc->tomb = NULL;
  1542. for (psf=pmc->sources; psf; psf=nextpsf) {
  1543. nextpsf = psf->sf_next;
  1544. kfree(psf);
  1545. }
  1546. pmc->sources = NULL;
  1547. pmc->sfmode = MCAST_EXCLUDE;
  1548. pmc->sfcount[MCAST_INCLUDE] = 0;
  1549. pmc->sfcount[MCAST_EXCLUDE] = 1;
  1550. }
  1551. /*
  1552. * Join a multicast group
  1553. */
  1554. int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
  1555. {
  1556. int err;
  1557. __be32 addr = imr->imr_multiaddr.s_addr;
  1558. struct ip_mc_socklist *iml = NULL, *i;
  1559. struct in_device *in_dev;
  1560. struct inet_sock *inet = inet_sk(sk);
  1561. struct net *net = sock_net(sk);
  1562. int ifindex;
  1563. int count = 0;
  1564. if (!ipv4_is_multicast(addr))
  1565. return -EINVAL;
  1566. rtnl_lock();
  1567. in_dev = ip_mc_find_dev(net, imr);
  1568. if (!in_dev) {
  1569. iml = NULL;
  1570. err = -ENODEV;
  1571. goto done;
  1572. }
  1573. err = -EADDRINUSE;
  1574. ifindex = imr->imr_ifindex;
  1575. for (i = inet->mc_list; i; i = i->next) {
  1576. if (i->multi.imr_multiaddr.s_addr == addr &&
  1577. i->multi.imr_ifindex == ifindex)
  1578. goto done;
  1579. count++;
  1580. }
  1581. err = -ENOBUFS;
  1582. if (count >= sysctl_igmp_max_memberships)
  1583. goto done;
  1584. iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
  1585. if (iml == NULL)
  1586. goto done;
  1587. memcpy(&iml->multi, imr, sizeof(*imr));
  1588. iml->next = inet->mc_list;
  1589. iml->sflist = NULL;
  1590. iml->sfmode = MCAST_EXCLUDE;
  1591. rcu_assign_pointer(inet->mc_list, iml);
  1592. ip_mc_inc_group(in_dev, addr);
  1593. err = 0;
  1594. done:
  1595. rtnl_unlock();
  1596. return err;
  1597. }
  1598. EXPORT_SYMBOL(ip_mc_join_group);
  1599. static void ip_sf_socklist_reclaim(struct rcu_head *rp)
  1600. {
  1601. struct ip_sf_socklist *psf;
  1602. psf = container_of(rp, struct ip_sf_socklist, rcu);
  1603. /* sk_omem_alloc should have been decreased by the caller*/
  1604. kfree(psf);
  1605. }
  1606. static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
  1607. struct in_device *in_dev)
  1608. {
  1609. struct ip_sf_socklist *psf = iml->sflist;
  1610. int err;
  1611. if (psf == NULL) {
  1612. /* any-source empty exclude case */
  1613. return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
  1614. iml->sfmode, 0, NULL, 0);
  1615. }
  1616. err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
  1617. iml->sfmode, psf->sl_count, psf->sl_addr, 0);
  1618. rcu_assign_pointer(iml->sflist, NULL);
  1619. /* decrease mem now to avoid the memleak warning */
  1620. atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
  1621. call_rcu(&psf->rcu, ip_sf_socklist_reclaim);
  1622. return err;
  1623. }
  1624. static void ip_mc_socklist_reclaim(struct rcu_head *rp)
  1625. {
  1626. struct ip_mc_socklist *iml;
  1627. iml = container_of(rp, struct ip_mc_socklist, rcu);
  1628. /* sk_omem_alloc should have been decreased by the caller*/
  1629. kfree(iml);
  1630. }
  1631. /*
  1632. * Ask a socket to leave a group.
  1633. */
  1634. int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
  1635. {
  1636. struct inet_sock *inet = inet_sk(sk);
  1637. struct ip_mc_socklist *iml, **imlp;
  1638. struct in_device *in_dev;
  1639. struct net *net = sock_net(sk);
  1640. __be32 group = imr->imr_multiaddr.s_addr;
  1641. u32 ifindex;
  1642. int ret = -EADDRNOTAVAIL;
  1643. rtnl_lock();
  1644. in_dev = ip_mc_find_dev(net, imr);
  1645. ifindex = imr->imr_ifindex;
  1646. for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
  1647. if (iml->multi.imr_multiaddr.s_addr != group)
  1648. continue;
  1649. if (ifindex) {
  1650. if (iml->multi.imr_ifindex != ifindex)
  1651. continue;
  1652. } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
  1653. iml->multi.imr_address.s_addr)
  1654. continue;
  1655. (void) ip_mc_leave_src(sk, iml, in_dev);
  1656. rcu_assign_pointer(*imlp, iml->next);
  1657. if (in_dev)
  1658. ip_mc_dec_group(in_dev, group);
  1659. rtnl_unlock();
  1660. /* decrease mem now to avoid the memleak warning */
  1661. atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
  1662. call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
  1663. return 0;
  1664. }
  1665. if (!in_dev)
  1666. ret = -ENODEV;
  1667. rtnl_unlock();
  1668. return ret;
  1669. }
  1670. int ip_mc_source(int add, int omode, struct sock *sk, struct
  1671. ip_mreq_source *mreqs, int ifindex)
  1672. {
  1673. int err;
  1674. struct ip_mreqn imr;
  1675. __be32 addr = mreqs->imr_multiaddr;
  1676. struct ip_mc_socklist *pmc;
  1677. struct in_device *in_dev = NULL;
  1678. struct inet_sock *inet = inet_sk(sk);
  1679. struct ip_sf_socklist *psl;
  1680. struct net *net = sock_net(sk);
  1681. int leavegroup = 0;
  1682. int i, j, rv;
  1683. if (!ipv4_is_multicast(addr))
  1684. return -EINVAL;
  1685. rtnl_lock();
  1686. imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
  1687. imr.imr_address.s_addr = mreqs->imr_interface;
  1688. imr.imr_ifindex = ifindex;
  1689. in_dev = ip_mc_find_dev(net, &imr);
  1690. if (!in_dev) {
  1691. err = -ENODEV;
  1692. goto done;
  1693. }
  1694. err = -EADDRNOTAVAIL;
  1695. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1696. if ((pmc->multi.imr_multiaddr.s_addr ==
  1697. imr.imr_multiaddr.s_addr) &&
  1698. (pmc->multi.imr_ifindex == imr.imr_ifindex))
  1699. break;
  1700. }
  1701. if (!pmc) { /* must have a prior join */
  1702. err = -EINVAL;
  1703. goto done;
  1704. }
  1705. /* if a source filter was set, must be the same mode as before */
  1706. if (pmc->sflist) {
  1707. if (pmc->sfmode != omode) {
  1708. err = -EINVAL;
  1709. goto done;
  1710. }
  1711. } else if (pmc->sfmode != omode) {
  1712. /* allow mode switches for empty-set filters */
  1713. ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
  1714. ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
  1715. NULL, 0);
  1716. pmc->sfmode = omode;
  1717. }
  1718. psl = pmc->sflist;
  1719. if (!add) {
  1720. if (!psl)
  1721. goto done; /* err = -EADDRNOTAVAIL */
  1722. rv = !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) /* source not found */
  1730. goto done; /* err = -EADDRNOTAVAIL */
  1731. /* special case - (INCLUDE, empty) == LEAVE_GROUP */
  1732. if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
  1733. leavegroup = 1;
  1734. goto done;
  1735. }
  1736. /* update the interface filter */
  1737. ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
  1738. &mreqs->imr_sourceaddr, 1);
  1739. for (j=i+1; j<psl->sl_count; j++)
  1740. psl->sl_addr[j-1] = psl->sl_addr[j];
  1741. psl->sl_count--;
  1742. err = 0;
  1743. goto done;
  1744. }
  1745. /* else, add a new source to the filter */
  1746. if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
  1747. err = -ENOBUFS;
  1748. goto done;
  1749. }
  1750. if (!psl || psl->sl_count == psl->sl_max) {
  1751. struct ip_sf_socklist *newpsl;
  1752. int count = IP_SFBLOCK;
  1753. if (psl)
  1754. count += psl->sl_max;
  1755. newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
  1756. if (!newpsl) {
  1757. err = -ENOBUFS;
  1758. goto done;
  1759. }
  1760. newpsl->sl_max = count;
  1761. newpsl->sl_count = count - IP_SFBLOCK;
  1762. if (psl) {
  1763. for (i=0; i<psl->sl_count; i++)
  1764. newpsl->sl_addr[i] = psl->sl_addr[i];
  1765. /* decrease mem now to avoid the memleak warning */
  1766. atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
  1767. call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
  1768. }
  1769. rcu_assign_pointer(pmc->sflist, newpsl);
  1770. psl = newpsl;
  1771. }
  1772. rv = 1; /* > 0 for insert logic below if sl_count is 0 */
  1773. for (i=0; i<psl->sl_count; i++) {
  1774. rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
  1775. sizeof(__be32));
  1776. if (rv == 0)
  1777. break;
  1778. }
  1779. if (rv == 0) /* address already there is an error */
  1780. goto done;
  1781. for (j=psl->sl_count-1; j>=i; j--)
  1782. psl->sl_addr[j+1] = psl->sl_addr[j];
  1783. psl->sl_addr[i] = mreqs->imr_sourceaddr;
  1784. psl->sl_count++;
  1785. err = 0;
  1786. /* update the interface list */
  1787. ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
  1788. &mreqs->imr_sourceaddr, 1);
  1789. done:
  1790. rtnl_unlock();
  1791. if (leavegroup)
  1792. return ip_mc_leave_group(sk, &imr);
  1793. return err;
  1794. }
  1795. int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
  1796. {
  1797. int err = 0;
  1798. struct ip_mreqn imr;
  1799. __be32 addr = msf->imsf_multiaddr;
  1800. struct ip_mc_socklist *pmc;
  1801. struct in_device *in_dev;
  1802. struct inet_sock *inet = inet_sk(sk);
  1803. struct ip_sf_socklist *newpsl, *psl;
  1804. struct net *net = sock_net(sk);
  1805. int leavegroup = 0;
  1806. if (!ipv4_is_multicast(addr))
  1807. return -EINVAL;
  1808. if (msf->imsf_fmode != MCAST_INCLUDE &&
  1809. msf->imsf_fmode != MCAST_EXCLUDE)
  1810. return -EINVAL;
  1811. rtnl_lock();
  1812. imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
  1813. imr.imr_address.s_addr = msf->imsf_interface;
  1814. imr.imr_ifindex = ifindex;
  1815. in_dev = ip_mc_find_dev(net, &imr);
  1816. if (!in_dev) {
  1817. err = -ENODEV;
  1818. goto done;
  1819. }
  1820. /* special case - (INCLUDE, empty) == LEAVE_GROUP */
  1821. if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
  1822. leavegroup = 1;
  1823. goto done;
  1824. }
  1825. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1826. if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
  1827. pmc->multi.imr_ifindex == imr.imr_ifindex)
  1828. break;
  1829. }
  1830. if (!pmc) { /* must have a prior join */
  1831. err = -EINVAL;
  1832. goto done;
  1833. }
  1834. if (msf->imsf_numsrc) {
  1835. newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
  1836. GFP_KERNEL);
  1837. if (!newpsl) {
  1838. err = -ENOBUFS;
  1839. goto done;
  1840. }
  1841. newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
  1842. memcpy(newpsl->sl_addr, msf->imsf_slist,
  1843. msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
  1844. err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
  1845. msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
  1846. if (err) {
  1847. sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
  1848. goto done;
  1849. }
  1850. } else {
  1851. newpsl = NULL;
  1852. (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
  1853. msf->imsf_fmode, 0, NULL, 0);
  1854. }
  1855. psl = pmc->sflist;
  1856. if (psl) {
  1857. (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
  1858. psl->sl_count, psl->sl_addr, 0);
  1859. /* decrease mem now to avoid the memleak warning */
  1860. atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
  1861. call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
  1862. } else
  1863. (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
  1864. 0, NULL, 0);
  1865. rcu_assign_pointer(pmc->sflist, newpsl);
  1866. pmc->sfmode = msf->imsf_fmode;
  1867. err = 0;
  1868. done:
  1869. rtnl_unlock();
  1870. if (leavegroup)
  1871. err = ip_mc_leave_group(sk, &imr);
  1872. return err;
  1873. }
  1874. int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
  1875. struct ip_msfilter __user *optval, int __user *optlen)
  1876. {
  1877. int err, len, count, copycount;
  1878. struct ip_mreqn imr;
  1879. __be32 addr = msf->imsf_multiaddr;
  1880. struct ip_mc_socklist *pmc;
  1881. struct in_device *in_dev;
  1882. struct inet_sock *inet = inet_sk(sk);
  1883. struct ip_sf_socklist *psl;
  1884. struct net *net = sock_net(sk);
  1885. if (!ipv4_is_multicast(addr))
  1886. return -EINVAL;
  1887. rtnl_lock();
  1888. imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
  1889. imr.imr_address.s_addr = msf->imsf_interface;
  1890. imr.imr_ifindex = 0;
  1891. in_dev = ip_mc_find_dev(net, &imr);
  1892. if (!in_dev) {
  1893. err = -ENODEV;
  1894. goto done;
  1895. }
  1896. err = -EADDRNOTAVAIL;
  1897. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1898. if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
  1899. pmc->multi.imr_ifindex == imr.imr_ifindex)
  1900. break;
  1901. }
  1902. if (!pmc) /* must have a prior join */
  1903. goto done;
  1904. msf->imsf_fmode = pmc->sfmode;
  1905. psl = pmc->sflist;
  1906. rtnl_unlock();
  1907. if (!psl) {
  1908. len = 0;
  1909. count = 0;
  1910. } else {
  1911. count = psl->sl_count;
  1912. }
  1913. copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
  1914. len = copycount * sizeof(psl->sl_addr[0]);
  1915. msf->imsf_numsrc = count;
  1916. if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
  1917. copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
  1918. return -EFAULT;
  1919. }
  1920. if (len &&
  1921. copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
  1922. return -EFAULT;
  1923. return 0;
  1924. done:
  1925. rtnl_unlock();
  1926. return err;
  1927. }
  1928. int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
  1929. struct group_filter __user *optval, int __user *optlen)
  1930. {
  1931. int err, i, count, copycount;
  1932. struct sockaddr_in *psin;
  1933. __be32 addr;
  1934. struct ip_mc_socklist *pmc;
  1935. struct inet_sock *inet = inet_sk(sk);
  1936. struct ip_sf_socklist *psl;
  1937. psin = (struct sockaddr_in *)&gsf->gf_group;
  1938. if (psin->sin_family != AF_INET)
  1939. return -EINVAL;
  1940. addr = psin->sin_addr.s_addr;
  1941. if (!ipv4_is_multicast(addr))
  1942. return -EINVAL;
  1943. rtnl_lock();
  1944. err = -EADDRNOTAVAIL;
  1945. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1946. if (pmc->multi.imr_multiaddr.s_addr == addr &&
  1947. pmc->multi.imr_ifindex == gsf->gf_interface)
  1948. break;
  1949. }
  1950. if (!pmc) /* must have a prior join */
  1951. goto done;
  1952. gsf->gf_fmode = pmc->sfmode;
  1953. psl = pmc->sflist;
  1954. rtnl_unlock();
  1955. count = psl ? psl->sl_count : 0;
  1956. copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
  1957. gsf->gf_numsrc = count;
  1958. if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
  1959. copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
  1960. return -EFAULT;
  1961. }
  1962. for (i=0; i<copycount; i++) {
  1963. struct sockaddr_storage ss;
  1964. psin = (struct sockaddr_in *)&ss;
  1965. memset(&ss, 0, sizeof(ss));
  1966. psin->sin_family = AF_INET;
  1967. psin->sin_addr.s_addr = psl->sl_addr[i];
  1968. if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
  1969. return -EFAULT;
  1970. }
  1971. return 0;
  1972. done:
  1973. rtnl_unlock();
  1974. return err;
  1975. }
  1976. /*
  1977. * check if a multicast source filter allows delivery for a given <src,dst,intf>
  1978. */
  1979. int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
  1980. {
  1981. struct inet_sock *inet = inet_sk(sk);
  1982. struct ip_mc_socklist *pmc;
  1983. struct ip_sf_socklist *psl;
  1984. int i;
  1985. int ret;
  1986. ret = 1;
  1987. if (!ipv4_is_multicast(loc_addr))
  1988. goto out;
  1989. rcu_read_lock();
  1990. for (pmc=rcu_dereference(inet->mc_list); pmc; pmc=rcu_dereference(pmc->next)) {
  1991. if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
  1992. pmc->multi.imr_ifindex == dif)
  1993. break;
  1994. }
  1995. ret = inet->mc_all;
  1996. if (!pmc)
  1997. goto unlock;
  1998. psl = pmc->sflist;
  1999. ret = (pmc->sfmode == MCAST_EXCLUDE);
  2000. if (!psl)
  2001. goto unlock;
  2002. for (i=0; i<psl->sl_count; i++) {
  2003. if (psl->sl_addr[i] == rmt_addr)
  2004. break;
  2005. }
  2006. ret = 0;
  2007. if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
  2008. goto unlock;
  2009. if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
  2010. goto unlock;
  2011. ret = 1;
  2012. unlock:
  2013. rcu_read_unlock();
  2014. out:
  2015. return ret;
  2016. }
  2017. /*
  2018. * A socket is closing.
  2019. */
  2020. void ip_mc_drop_socket(struct sock *sk)
  2021. {
  2022. struct inet_sock *inet = inet_sk(sk);
  2023. struct ip_mc_socklist *iml;
  2024. struct net *net = sock_net(sk);
  2025. if (inet->mc_list == NULL)
  2026. return;
  2027. rtnl_lock();
  2028. while ((iml = inet->mc_list) != NULL) {
  2029. struct in_device *in_dev;
  2030. rcu_assign_pointer(inet->mc_list, iml->next);
  2031. in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
  2032. (void) ip_mc_leave_src(sk, iml, in_dev);
  2033. if (in_dev != NULL)
  2034. ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
  2035. /* decrease mem now to avoid the memleak warning */
  2036. atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
  2037. call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
  2038. }
  2039. rtnl_unlock();
  2040. }
  2041. int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
  2042. {
  2043. struct ip_mc_list *im;
  2044. struct ip_sf_list *psf;
  2045. int rv = 0;
  2046. read_lock(&in_dev->mc_list_lock);
  2047. for (im=in_dev->mc_list; im; im=im->next) {
  2048. if (im->multiaddr == mc_addr)
  2049. break;
  2050. }
  2051. if (im && proto == IPPROTO_IGMP) {
  2052. rv = 1;
  2053. } else if (im) {
  2054. if (src_addr) {
  2055. for (psf=im->sources; psf; psf=psf->sf_next) {
  2056. if (psf->sf_inaddr == src_addr)
  2057. break;
  2058. }
  2059. if (psf)
  2060. rv = psf->sf_count[MCAST_INCLUDE] ||
  2061. psf->sf_count[MCAST_EXCLUDE] !=
  2062. im->sfcount[MCAST_EXCLUDE];
  2063. else
  2064. rv = im->sfcount[MCAST_EXCLUDE] != 0;
  2065. } else
  2066. rv = 1; /* unspecified source; tentatively allow */
  2067. }
  2068. read_unlock(&in_dev->mc_list_lock);
  2069. return rv;
  2070. }
  2071. #if defined(CONFIG_PROC_FS)
  2072. struct igmp_mc_iter_state {
  2073. struct seq_net_private p;
  2074. struct net_device *dev;
  2075. struct in_device *in_dev;
  2076. };
  2077. #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
  2078. static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
  2079. {
  2080. struct net *net = seq_file_net(seq);
  2081. struct ip_mc_list *im = NULL;
  2082. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2083. state->in_dev = NULL;
  2084. for_each_netdev_rcu(net, state->dev) {
  2085. struct in_device *in_dev;
  2086. in_dev = __in_dev_get_rcu(state->dev);
  2087. if (!in_dev)
  2088. continue;
  2089. read_lock(&in_dev->mc_list_lock);
  2090. im = in_dev->mc_list;
  2091. if (im) {
  2092. state->in_dev = in_dev;
  2093. break;
  2094. }
  2095. read_unlock(&in_dev->mc_list_lock);
  2096. }
  2097. return im;
  2098. }
  2099. static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
  2100. {
  2101. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2102. im = im->next;
  2103. while (!im) {
  2104. if (likely(state->in_dev != NULL))
  2105. read_unlock(&state->in_dev->mc_list_lock);
  2106. state->dev = next_net_device_rcu(state->dev);
  2107. if (!state->dev) {
  2108. state->in_dev = NULL;
  2109. break;
  2110. }
  2111. state->in_dev = __in_dev_get_rcu(state->dev);
  2112. if (!state->in_dev)
  2113. continue;
  2114. read_lock(&state->in_dev->mc_list_lock);
  2115. im = state->in_dev->mc_list;
  2116. }
  2117. return im;
  2118. }
  2119. static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
  2120. {
  2121. struct ip_mc_list *im = igmp_mc_get_first(seq);
  2122. if (im)
  2123. while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
  2124. --pos;
  2125. return pos ? NULL : im;
  2126. }
  2127. static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
  2128. __acquires(rcu)
  2129. {
  2130. rcu_read_lock();
  2131. return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2132. }
  2133. static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2134. {
  2135. struct ip_mc_list *im;
  2136. if (v == SEQ_START_TOKEN)
  2137. im = igmp_mc_get_first(seq);
  2138. else
  2139. im = igmp_mc_get_next(seq, v);
  2140. ++*pos;
  2141. return im;
  2142. }
  2143. static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
  2144. __releases(rcu)
  2145. {
  2146. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2147. if (likely(state->in_dev != NULL)) {
  2148. read_unlock(&state->in_dev->mc_list_lock);
  2149. state->in_dev = NULL;
  2150. }
  2151. state->dev = NULL;
  2152. rcu_read_unlock();
  2153. }
  2154. static int igmp_mc_seq_show(struct seq_file *seq, void *v)
  2155. {
  2156. if (v == SEQ_START_TOKEN)
  2157. seq_puts(seq,
  2158. "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
  2159. else {
  2160. struct ip_mc_list *im = (struct ip_mc_list *)v;
  2161. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2162. char *querier;
  2163. #ifdef CONFIG_IP_MULTICAST
  2164. querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
  2165. IGMP_V2_SEEN(state->in_dev) ? "V2" :
  2166. "V3";
  2167. #else
  2168. querier = "NONE";
  2169. #endif
  2170. if (state->in_dev->mc_list == im) {
  2171. seq_printf(seq, "%d\t%-10s: %5d %7s\n",
  2172. state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
  2173. }
  2174. seq_printf(seq,
  2175. "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
  2176. im->multiaddr, im->users,
  2177. im->tm_running, im->tm_running ?
  2178. jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
  2179. im->reporter);
  2180. }
  2181. return 0;
  2182. }
  2183. static const struct seq_operations igmp_mc_seq_ops = {
  2184. .start = igmp_mc_seq_start,
  2185. .next = igmp_mc_seq_next,
  2186. .stop = igmp_mc_seq_stop,
  2187. .show = igmp_mc_seq_show,
  2188. };
  2189. static int igmp_mc_seq_open(struct inode *inode, struct file *file)
  2190. {
  2191. return seq_open_net(inode, file, &igmp_mc_seq_ops,
  2192. sizeof(struct igmp_mc_iter_state));
  2193. }
  2194. static const struct file_operations igmp_mc_seq_fops = {
  2195. .owner = THIS_MODULE,
  2196. .open = igmp_mc_seq_open,
  2197. .read = seq_read,
  2198. .llseek = seq_lseek,
  2199. .release = seq_release_net,
  2200. };
  2201. struct igmp_mcf_iter_state {
  2202. struct seq_net_private p;
  2203. struct net_device *dev;
  2204. struct in_device *idev;
  2205. struct ip_mc_list *im;
  2206. };
  2207. #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
  2208. static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
  2209. {
  2210. struct net *net = seq_file_net(seq);
  2211. struct ip_sf_list *psf = NULL;
  2212. struct ip_mc_list *im = NULL;
  2213. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2214. state->idev = NULL;
  2215. state->im = NULL;
  2216. for_each_netdev_rcu(net, state->dev) {
  2217. struct in_device *idev;
  2218. idev = __in_dev_get_rcu(state->dev);
  2219. if (unlikely(idev == NULL))
  2220. continue;
  2221. read_lock(&idev->mc_list_lock);
  2222. im = idev->mc_list;
  2223. if (likely(im != NULL)) {
  2224. spin_lock_bh(&im->lock);
  2225. psf = im->sources;
  2226. if (likely(psf != NULL)) {
  2227. state->im = im;
  2228. state->idev = idev;
  2229. break;
  2230. }
  2231. spin_unlock_bh(&im->lock);
  2232. }
  2233. read_unlock(&idev->mc_list_lock);
  2234. }
  2235. return psf;
  2236. }
  2237. static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
  2238. {
  2239. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2240. psf = psf->sf_next;
  2241. while (!psf) {
  2242. spin_unlock_bh(&state->im->lock);
  2243. state->im = state->im->next;
  2244. while (!state->im) {
  2245. if (likely(state->idev != NULL))
  2246. read_unlock(&state->idev->mc_list_lock);
  2247. state->dev = next_net_device_rcu(state->dev);
  2248. if (!state->dev) {
  2249. state->idev = NULL;
  2250. goto out;
  2251. }
  2252. state->idev = __in_dev_get_rcu(state->dev);
  2253. if (!state->idev)
  2254. continue;
  2255. read_lock(&state->idev->mc_list_lock);
  2256. state->im = state->idev->mc_list;
  2257. }
  2258. if (!state->im)
  2259. break;
  2260. spin_lock_bh(&state->im->lock);
  2261. psf = state->im->sources;
  2262. }
  2263. out:
  2264. return psf;
  2265. }
  2266. static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
  2267. {
  2268. struct ip_sf_list *psf = igmp_mcf_get_first(seq);
  2269. if (psf)
  2270. while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
  2271. --pos;
  2272. return pos ? NULL : psf;
  2273. }
  2274. static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
  2275. __acquires(rcu)
  2276. {
  2277. rcu_read_lock();
  2278. return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2279. }
  2280. static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2281. {
  2282. struct ip_sf_list *psf;
  2283. if (v == SEQ_START_TOKEN)
  2284. psf = igmp_mcf_get_first(seq);
  2285. else
  2286. psf = igmp_mcf_get_next(seq, v);
  2287. ++*pos;
  2288. return psf;
  2289. }
  2290. static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
  2291. __releases(rcu)
  2292. {
  2293. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2294. if (likely(state->im != NULL)) {
  2295. spin_unlock_bh(&state->im->lock);
  2296. state->im = NULL;
  2297. }
  2298. if (likely(state->idev != NULL)) {
  2299. read_unlock(&state->idev->mc_list_lock);
  2300. state->idev = NULL;
  2301. }
  2302. state->dev = NULL;
  2303. rcu_read_unlock();
  2304. }
  2305. static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
  2306. {
  2307. struct ip_sf_list *psf = (struct ip_sf_list *)v;
  2308. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2309. if (v == SEQ_START_TOKEN) {
  2310. seq_printf(seq,
  2311. "%3s %6s "
  2312. "%10s %10s %6s %6s\n", "Idx",
  2313. "Device", "MCA",
  2314. "SRC", "INC", "EXC");
  2315. } else {
  2316. seq_printf(seq,
  2317. "%3d %6.6s 0x%08x "
  2318. "0x%08x %6lu %6lu\n",
  2319. state->dev->ifindex, state->dev->name,
  2320. ntohl(state->im->multiaddr),
  2321. ntohl(psf->sf_inaddr),
  2322. psf->sf_count[MCAST_INCLUDE],
  2323. psf->sf_count[MCAST_EXCLUDE]);
  2324. }
  2325. return 0;
  2326. }
  2327. static const struct seq_operations igmp_mcf_seq_ops = {
  2328. .start = igmp_mcf_seq_start,
  2329. .next = igmp_mcf_seq_next,
  2330. .stop = igmp_mcf_seq_stop,
  2331. .show = igmp_mcf_seq_show,
  2332. };
  2333. static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
  2334. {
  2335. return seq_open_net(inode, file, &igmp_mcf_seq_ops,
  2336. sizeof(struct igmp_mcf_iter_state));
  2337. }
  2338. static const struct file_operations igmp_mcf_seq_fops = {
  2339. .owner = THIS_MODULE,
  2340. .open = igmp_mcf_seq_open,
  2341. .read = seq_read,
  2342. .llseek = seq_lseek,
  2343. .release = seq_release_net,
  2344. };
  2345. static int __net_init igmp_net_init(struct net *net)
  2346. {
  2347. struct proc_dir_entry *pde;
  2348. pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
  2349. if (!pde)
  2350. goto out_igmp;
  2351. pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
  2352. if (!pde)
  2353. goto out_mcfilter;
  2354. return 0;
  2355. out_mcfilter:
  2356. proc_net_remove(net, "igmp");
  2357. out_igmp:
  2358. return -ENOMEM;
  2359. }
  2360. static void __net_exit igmp_net_exit(struct net *net)
  2361. {
  2362. proc_net_remove(net, "mcfilter");
  2363. proc_net_remove(net, "igmp");
  2364. }
  2365. static struct pernet_operations igmp_net_ops = {
  2366. .init = igmp_net_init,
  2367. .exit = igmp_net_exit,
  2368. };
  2369. int __init igmp_mc_proc_init(void)
  2370. {
  2371. return register_pernet_subsys(&igmp_net_ops);
  2372. }
  2373. #endif