igmp.c 66 KB

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