igmp.c 61 KB

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