igmp.c 63 KB

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