igmp.c 73 KB

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