igmp.c 64 KB

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