igmp.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595
  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/config.h>
  75. #include <linux/module.h>
  76. #include <asm/uaccess.h>
  77. #include <asm/system.h>
  78. #include <linux/types.h>
  79. #include <linux/kernel.h>
  80. #include <linux/jiffies.h>
  81. #include <linux/string.h>
  82. #include <linux/socket.h>
  83. #include <linux/sockios.h>
  84. #include <linux/in.h>
  85. #include <linux/inet.h>
  86. #include <linux/netdevice.h>
  87. #include <linux/skbuff.h>
  88. #include <linux/inetdevice.h>
  89. #include <linux/igmp.h>
  90. #include <linux/if_arp.h>
  91. #include <linux/rtnetlink.h>
  92. #include <linux/times.h>
  93. #include <net/arp.h>
  94. #include <net/ip.h>
  95. #include <net/protocol.h>
  96. #include <net/route.h>
  97. #include <net/sock.h>
  98. #include <net/checksum.h>
  99. #include <linux/netfilter_ipv4.h>
  100. #ifdef CONFIG_IP_MROUTE
  101. #include <linux/mroute.h>
  102. #endif
  103. #ifdef CONFIG_PROC_FS
  104. #include <linux/proc_fs.h>
  105. #include <linux/seq_file.h>
  106. #endif
  107. #define IP_MAX_MEMBERSHIPS 20
  108. #define IP_MAX_MSF 10
  109. #ifdef CONFIG_IP_MULTICAST
  110. /* Parameter names and values are taken from igmp-v2-06 draft */
  111. #define IGMP_V1_Router_Present_Timeout (400*HZ)
  112. #define IGMP_V2_Router_Present_Timeout (400*HZ)
  113. #define IGMP_Unsolicited_Report_Interval (10*HZ)
  114. #define IGMP_Query_Response_Interval (10*HZ)
  115. #define IGMP_Unsolicited_Report_Count 2
  116. #define IGMP_Initial_Report_Delay (1)
  117. /* IGMP_Initial_Report_Delay is not from IGMP specs!
  118. * IGMP specs require to report membership immediately after
  119. * joining a group, but we delay the first report by a
  120. * small interval. It seems more natural and still does not
  121. * contradict to specs provided this delay is small enough.
  122. */
  123. #define IGMP_V1_SEEN(in_dev) (ipv4_devconf.force_igmp_version == 1 || \
  124. (in_dev)->cnf.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) (ipv4_devconf.force_igmp_version == 2 || \
  128. (in_dev)->cnf.force_igmp_version == 2 || \
  129. ((in_dev)->mr_v2_seen && \
  130. time_before(jiffies, (in_dev)->mr_v2_seen)))
  131. static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
  132. static void igmpv3_del_delrec(struct in_device *in_dev, __u32 multiaddr);
  133. static void igmpv3_clear_delrec(struct in_device *in_dev);
  134. static int sf_setstate(struct ip_mc_list *pmc);
  135. static void sf_markstate(struct ip_mc_list *pmc);
  136. #endif
  137. static void ip_mc_clear_src(struct ip_mc_list *pmc);
  138. static int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
  139. int sfcount, __u32 *psfsrc, int delta);
  140. static void ip_ma_put(struct ip_mc_list *im)
  141. {
  142. if (atomic_dec_and_test(&im->refcnt)) {
  143. in_dev_put(im->interface);
  144. kfree(im);
  145. }
  146. }
  147. #ifdef CONFIG_IP_MULTICAST
  148. /*
  149. * Timer management
  150. */
  151. static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
  152. {
  153. spin_lock_bh(&im->lock);
  154. if (del_timer(&im->timer))
  155. atomic_dec(&im->refcnt);
  156. im->tm_running=0;
  157. im->reporter = 0;
  158. im->unsolicit_count = 0;
  159. spin_unlock_bh(&im->lock);
  160. }
  161. /* It must be called with locked im->lock */
  162. static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
  163. {
  164. int tv=net_random() % max_delay;
  165. im->tm_running=1;
  166. if (!mod_timer(&im->timer, jiffies+tv+2))
  167. atomic_inc(&im->refcnt);
  168. }
  169. static void igmp_gq_start_timer(struct in_device *in_dev)
  170. {
  171. int tv = net_random() % in_dev->mr_maxdelay;
  172. in_dev->mr_gq_running = 1;
  173. if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
  174. in_dev_hold(in_dev);
  175. }
  176. static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
  177. {
  178. int tv = net_random() % delay;
  179. if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
  180. in_dev_hold(in_dev);
  181. }
  182. static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
  183. {
  184. spin_lock_bh(&im->lock);
  185. im->unsolicit_count = 0;
  186. if (del_timer(&im->timer)) {
  187. if ((long)(im->timer.expires-jiffies) < max_delay) {
  188. add_timer(&im->timer);
  189. im->tm_running=1;
  190. spin_unlock_bh(&im->lock);
  191. return;
  192. }
  193. atomic_dec(&im->refcnt);
  194. }
  195. igmp_start_timer(im, max_delay);
  196. spin_unlock_bh(&im->lock);
  197. }
  198. /*
  199. * Send an IGMP report.
  200. */
  201. #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
  202. static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
  203. int gdeleted, int sdeleted)
  204. {
  205. switch (type) {
  206. case IGMPV3_MODE_IS_INCLUDE:
  207. case IGMPV3_MODE_IS_EXCLUDE:
  208. if (gdeleted || sdeleted)
  209. return 0;
  210. if (!(pmc->gsquery && !psf->sf_gsresp)) {
  211. if (pmc->sfmode == MCAST_INCLUDE)
  212. return 1;
  213. /* don't include if this source is excluded
  214. * in all filters
  215. */
  216. if (psf->sf_count[MCAST_INCLUDE])
  217. return type == IGMPV3_MODE_IS_INCLUDE;
  218. return pmc->sfcount[MCAST_EXCLUDE] ==
  219. psf->sf_count[MCAST_EXCLUDE];
  220. }
  221. return 0;
  222. case IGMPV3_CHANGE_TO_INCLUDE:
  223. if (gdeleted || sdeleted)
  224. return 0;
  225. return psf->sf_count[MCAST_INCLUDE] != 0;
  226. case IGMPV3_CHANGE_TO_EXCLUDE:
  227. if (gdeleted || sdeleted)
  228. return 0;
  229. if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
  230. psf->sf_count[MCAST_INCLUDE])
  231. return 0;
  232. return pmc->sfcount[MCAST_EXCLUDE] ==
  233. psf->sf_count[MCAST_EXCLUDE];
  234. case IGMPV3_ALLOW_NEW_SOURCES:
  235. if (gdeleted || !psf->sf_crcount)
  236. return 0;
  237. return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
  238. case IGMPV3_BLOCK_OLD_SOURCES:
  239. if (pmc->sfmode == MCAST_INCLUDE)
  240. return gdeleted || (psf->sf_crcount && sdeleted);
  241. return psf->sf_crcount && !gdeleted && !sdeleted;
  242. }
  243. return 0;
  244. }
  245. static int
  246. igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
  247. {
  248. struct ip_sf_list *psf;
  249. int scount = 0;
  250. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  251. if (!is_in(pmc, psf, type, gdeleted, sdeleted))
  252. continue;
  253. scount++;
  254. }
  255. return scount;
  256. }
  257. static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
  258. {
  259. struct sk_buff *skb;
  260. struct rtable *rt;
  261. struct iphdr *pip;
  262. struct igmpv3_report *pig;
  263. skb = alloc_skb(size + LL_RESERVED_SPACE(dev), GFP_ATOMIC);
  264. if (skb == NULL)
  265. return NULL;
  266. {
  267. struct flowi fl = { .oif = dev->ifindex,
  268. .nl_u = { .ip4_u = {
  269. .daddr = IGMPV3_ALL_MCR } },
  270. .proto = IPPROTO_IGMP };
  271. if (ip_route_output_key(&rt, &fl)) {
  272. kfree_skb(skb);
  273. return NULL;
  274. }
  275. }
  276. if (rt->rt_src == 0) {
  277. kfree_skb(skb);
  278. ip_rt_put(rt);
  279. return NULL;
  280. }
  281. skb->dst = &rt->u.dst;
  282. skb->dev = dev;
  283. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  284. skb->nh.iph = pip =(struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
  285. pip->version = 4;
  286. pip->ihl = (sizeof(struct iphdr)+4)>>2;
  287. pip->tos = 0xc0;
  288. pip->frag_off = htons(IP_DF);
  289. pip->ttl = 1;
  290. pip->daddr = rt->rt_dst;
  291. pip->saddr = rt->rt_src;
  292. pip->protocol = IPPROTO_IGMP;
  293. pip->tot_len = 0; /* filled in later */
  294. ip_select_ident(pip, &rt->u.dst, NULL);
  295. ((u8*)&pip[1])[0] = IPOPT_RA;
  296. ((u8*)&pip[1])[1] = 4;
  297. ((u8*)&pip[1])[2] = 0;
  298. ((u8*)&pip[1])[3] = 0;
  299. pig =(struct igmpv3_report *)skb_put(skb, sizeof(*pig));
  300. skb->h.igmph = (struct igmphdr *)pig;
  301. pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
  302. pig->resv1 = 0;
  303. pig->csum = 0;
  304. pig->resv2 = 0;
  305. pig->ngrec = 0;
  306. return skb;
  307. }
  308. static int igmpv3_sendpack(struct sk_buff *skb)
  309. {
  310. struct iphdr *pip = skb->nh.iph;
  311. struct igmphdr *pig = skb->h.igmph;
  312. int iplen, igmplen;
  313. iplen = skb->tail - (unsigned char *)skb->nh.iph;
  314. pip->tot_len = htons(iplen);
  315. ip_send_check(pip);
  316. igmplen = skb->tail - (unsigned char *)skb->h.igmph;
  317. pig->csum = ip_compute_csum((void *)skb->h.igmph, igmplen);
  318. return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, skb->dev,
  319. dst_output);
  320. }
  321. static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
  322. {
  323. return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc,type,gdel,sdel);
  324. }
  325. static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
  326. int type, struct igmpv3_grec **ppgr)
  327. {
  328. struct net_device *dev = pmc->interface->dev;
  329. struct igmpv3_report *pih;
  330. struct igmpv3_grec *pgr;
  331. if (!skb)
  332. skb = igmpv3_newpack(dev, dev->mtu);
  333. if (!skb)
  334. return NULL;
  335. pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
  336. pgr->grec_type = type;
  337. pgr->grec_auxwords = 0;
  338. pgr->grec_nsrcs = 0;
  339. pgr->grec_mca = pmc->multiaddr;
  340. pih = (struct igmpv3_report *)skb->h.igmph;
  341. pih->ngrec = htons(ntohs(pih->ngrec)+1);
  342. *ppgr = pgr;
  343. return skb;
  344. }
  345. #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
  346. skb_tailroom(skb)) : 0)
  347. static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
  348. int type, int gdeleted, int sdeleted)
  349. {
  350. struct net_device *dev = pmc->interface->dev;
  351. struct igmpv3_report *pih;
  352. struct igmpv3_grec *pgr = NULL;
  353. struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
  354. int scount, stotal, first, isquery, truncate;
  355. if (pmc->multiaddr == IGMP_ALL_HOSTS)
  356. return skb;
  357. isquery = type == IGMPV3_MODE_IS_INCLUDE ||
  358. type == IGMPV3_MODE_IS_EXCLUDE;
  359. truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
  360. type == IGMPV3_CHANGE_TO_EXCLUDE;
  361. stotal = scount = 0;
  362. psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
  363. if (!*psf_list)
  364. goto empty_source;
  365. pih = skb ? (struct igmpv3_report *)skb->h.igmph : NULL;
  366. /* EX and TO_EX get a fresh packet, if needed */
  367. if (truncate) {
  368. if (pih && pih->ngrec &&
  369. AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
  370. if (skb)
  371. igmpv3_sendpack(skb);
  372. skb = igmpv3_newpack(dev, dev->mtu);
  373. }
  374. }
  375. first = 1;
  376. psf_prev = NULL;
  377. for (psf=*psf_list; psf; psf=psf_next) {
  378. u32 *psrc;
  379. psf_next = psf->sf_next;
  380. if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
  381. psf_prev = psf;
  382. continue;
  383. }
  384. /* clear marks on query responses */
  385. if (isquery)
  386. psf->sf_gsresp = 0;
  387. if (AVAILABLE(skb) < sizeof(u32) +
  388. first*sizeof(struct igmpv3_grec)) {
  389. if (truncate && !first)
  390. break; /* truncate these */
  391. if (pgr)
  392. pgr->grec_nsrcs = htons(scount);
  393. if (skb)
  394. igmpv3_sendpack(skb);
  395. skb = igmpv3_newpack(dev, dev->mtu);
  396. first = 1;
  397. scount = 0;
  398. }
  399. if (first) {
  400. skb = add_grhead(skb, pmc, type, &pgr);
  401. first = 0;
  402. }
  403. psrc = (u32 *)skb_put(skb, sizeof(u32));
  404. *psrc = psf->sf_inaddr;
  405. scount++; stotal++;
  406. if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
  407. type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
  408. psf->sf_crcount--;
  409. if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
  410. if (psf_prev)
  411. psf_prev->sf_next = psf->sf_next;
  412. else
  413. *psf_list = psf->sf_next;
  414. kfree(psf);
  415. continue;
  416. }
  417. }
  418. psf_prev = psf;
  419. }
  420. empty_source:
  421. if (!stotal) {
  422. if (type == IGMPV3_ALLOW_NEW_SOURCES ||
  423. type == IGMPV3_BLOCK_OLD_SOURCES)
  424. return skb;
  425. if (pmc->crcount || isquery) {
  426. /* make sure we have room for group header */
  427. if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
  428. igmpv3_sendpack(skb);
  429. skb = NULL; /* add_grhead will get a new one */
  430. }
  431. skb = add_grhead(skb, pmc, type, &pgr);
  432. }
  433. }
  434. if (pgr)
  435. pgr->grec_nsrcs = htons(scount);
  436. if (isquery)
  437. pmc->gsquery = 0; /* clear query state on report */
  438. return skb;
  439. }
  440. static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
  441. {
  442. struct sk_buff *skb = NULL;
  443. int type;
  444. if (!pmc) {
  445. read_lock(&in_dev->mc_list_lock);
  446. for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
  447. if (pmc->multiaddr == IGMP_ALL_HOSTS)
  448. continue;
  449. spin_lock_bh(&pmc->lock);
  450. if (pmc->sfcount[MCAST_EXCLUDE])
  451. type = IGMPV3_MODE_IS_EXCLUDE;
  452. else
  453. type = IGMPV3_MODE_IS_INCLUDE;
  454. skb = add_grec(skb, pmc, type, 0, 0);
  455. spin_unlock_bh(&pmc->lock);
  456. }
  457. read_unlock(&in_dev->mc_list_lock);
  458. } else {
  459. spin_lock_bh(&pmc->lock);
  460. if (pmc->sfcount[MCAST_EXCLUDE])
  461. type = IGMPV3_MODE_IS_EXCLUDE;
  462. else
  463. type = IGMPV3_MODE_IS_INCLUDE;
  464. skb = add_grec(skb, pmc, type, 0, 0);
  465. spin_unlock_bh(&pmc->lock);
  466. }
  467. if (!skb)
  468. return 0;
  469. return igmpv3_sendpack(skb);
  470. }
  471. /*
  472. * remove zero-count source records from a source filter list
  473. */
  474. static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
  475. {
  476. struct ip_sf_list *psf_prev, *psf_next, *psf;
  477. psf_prev = NULL;
  478. for (psf=*ppsf; psf; psf = psf_next) {
  479. psf_next = psf->sf_next;
  480. if (psf->sf_crcount == 0) {
  481. if (psf_prev)
  482. psf_prev->sf_next = psf->sf_next;
  483. else
  484. *ppsf = psf->sf_next;
  485. kfree(psf);
  486. } else
  487. psf_prev = psf;
  488. }
  489. }
  490. static void igmpv3_send_cr(struct in_device *in_dev)
  491. {
  492. struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
  493. struct sk_buff *skb = NULL;
  494. int type, dtype;
  495. read_lock(&in_dev->mc_list_lock);
  496. spin_lock_bh(&in_dev->mc_tomb_lock);
  497. /* deleted MCA's */
  498. pmc_prev = NULL;
  499. for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
  500. pmc_next = pmc->next;
  501. if (pmc->sfmode == MCAST_INCLUDE) {
  502. type = IGMPV3_BLOCK_OLD_SOURCES;
  503. dtype = IGMPV3_BLOCK_OLD_SOURCES;
  504. skb = add_grec(skb, pmc, type, 1, 0);
  505. skb = add_grec(skb, pmc, dtype, 1, 1);
  506. }
  507. if (pmc->crcount) {
  508. if (pmc->sfmode == MCAST_EXCLUDE) {
  509. type = IGMPV3_CHANGE_TO_INCLUDE;
  510. skb = add_grec(skb, pmc, type, 1, 0);
  511. }
  512. pmc->crcount--;
  513. if (pmc->crcount == 0) {
  514. igmpv3_clear_zeros(&pmc->tomb);
  515. igmpv3_clear_zeros(&pmc->sources);
  516. }
  517. }
  518. if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
  519. if (pmc_prev)
  520. pmc_prev->next = pmc_next;
  521. else
  522. in_dev->mc_tomb = pmc_next;
  523. in_dev_put(pmc->interface);
  524. kfree(pmc);
  525. } else
  526. pmc_prev = pmc;
  527. }
  528. spin_unlock_bh(&in_dev->mc_tomb_lock);
  529. /* change recs */
  530. for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
  531. spin_lock_bh(&pmc->lock);
  532. if (pmc->sfcount[MCAST_EXCLUDE]) {
  533. type = IGMPV3_BLOCK_OLD_SOURCES;
  534. dtype = IGMPV3_ALLOW_NEW_SOURCES;
  535. } else {
  536. type = IGMPV3_ALLOW_NEW_SOURCES;
  537. dtype = IGMPV3_BLOCK_OLD_SOURCES;
  538. }
  539. skb = add_grec(skb, pmc, type, 0, 0);
  540. skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
  541. /* filter mode changes */
  542. if (pmc->crcount) {
  543. if (pmc->sfmode == MCAST_EXCLUDE)
  544. type = IGMPV3_CHANGE_TO_EXCLUDE;
  545. else
  546. type = IGMPV3_CHANGE_TO_INCLUDE;
  547. skb = add_grec(skb, pmc, type, 0, 0);
  548. pmc->crcount--;
  549. }
  550. spin_unlock_bh(&pmc->lock);
  551. }
  552. read_unlock(&in_dev->mc_list_lock);
  553. if (!skb)
  554. return;
  555. (void) igmpv3_sendpack(skb);
  556. }
  557. static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
  558. int type)
  559. {
  560. struct sk_buff *skb;
  561. struct iphdr *iph;
  562. struct igmphdr *ih;
  563. struct rtable *rt;
  564. struct net_device *dev = in_dev->dev;
  565. u32 group = pmc ? pmc->multiaddr : 0;
  566. u32 dst;
  567. if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
  568. return igmpv3_send_report(in_dev, pmc);
  569. else if (type == IGMP_HOST_LEAVE_MESSAGE)
  570. dst = IGMP_ALL_ROUTER;
  571. else
  572. dst = group;
  573. {
  574. struct flowi fl = { .oif = dev->ifindex,
  575. .nl_u = { .ip4_u = { .daddr = dst } },
  576. .proto = IPPROTO_IGMP };
  577. if (ip_route_output_key(&rt, &fl))
  578. return -1;
  579. }
  580. if (rt->rt_src == 0) {
  581. ip_rt_put(rt);
  582. return -1;
  583. }
  584. skb=alloc_skb(IGMP_SIZE+LL_RESERVED_SPACE(dev), GFP_ATOMIC);
  585. if (skb == NULL) {
  586. ip_rt_put(rt);
  587. return -1;
  588. }
  589. skb->dst = &rt->u.dst;
  590. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  591. skb->nh.iph = iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
  592. iph->version = 4;
  593. iph->ihl = (sizeof(struct iphdr)+4)>>2;
  594. iph->tos = 0xc0;
  595. iph->frag_off = htons(IP_DF);
  596. iph->ttl = 1;
  597. iph->daddr = dst;
  598. iph->saddr = rt->rt_src;
  599. iph->protocol = IPPROTO_IGMP;
  600. iph->tot_len = htons(IGMP_SIZE);
  601. ip_select_ident(iph, &rt->u.dst, NULL);
  602. ((u8*)&iph[1])[0] = IPOPT_RA;
  603. ((u8*)&iph[1])[1] = 4;
  604. ((u8*)&iph[1])[2] = 0;
  605. ((u8*)&iph[1])[3] = 0;
  606. ip_send_check(iph);
  607. ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
  608. ih->type=type;
  609. ih->code=0;
  610. ih->csum=0;
  611. ih->group=group;
  612. ih->csum=ip_compute_csum((void *)ih, sizeof(struct igmphdr));
  613. return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
  614. dst_output);
  615. }
  616. static void igmp_gq_timer_expire(unsigned long data)
  617. {
  618. struct in_device *in_dev = (struct in_device *)data;
  619. in_dev->mr_gq_running = 0;
  620. igmpv3_send_report(in_dev, NULL);
  621. __in_dev_put(in_dev);
  622. }
  623. static void igmp_ifc_timer_expire(unsigned long data)
  624. {
  625. struct in_device *in_dev = (struct in_device *)data;
  626. igmpv3_send_cr(in_dev);
  627. if (in_dev->mr_ifc_count) {
  628. in_dev->mr_ifc_count--;
  629. igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
  630. }
  631. __in_dev_put(in_dev);
  632. }
  633. static void igmp_ifc_event(struct in_device *in_dev)
  634. {
  635. if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
  636. return;
  637. in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
  638. IGMP_Unsolicited_Report_Count;
  639. igmp_ifc_start_timer(in_dev, 1);
  640. }
  641. static void igmp_timer_expire(unsigned long data)
  642. {
  643. struct ip_mc_list *im=(struct ip_mc_list *)data;
  644. struct in_device *in_dev = im->interface;
  645. spin_lock(&im->lock);
  646. im->tm_running=0;
  647. if (im->unsolicit_count) {
  648. im->unsolicit_count--;
  649. igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
  650. }
  651. im->reporter = 1;
  652. spin_unlock(&im->lock);
  653. if (IGMP_V1_SEEN(in_dev))
  654. igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
  655. else if (IGMP_V2_SEEN(in_dev))
  656. igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
  657. else
  658. igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
  659. ip_ma_put(im);
  660. }
  661. /* mark EXCLUDE-mode sources */
  662. static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __u32 *srcs)
  663. {
  664. struct ip_sf_list *psf;
  665. int i, scount;
  666. scount = 0;
  667. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  668. if (scount == nsrcs)
  669. break;
  670. for (i=0; i<nsrcs; i++) {
  671. /* skip inactive filters */
  672. if (pmc->sfcount[MCAST_INCLUDE] ||
  673. pmc->sfcount[MCAST_EXCLUDE] !=
  674. psf->sf_count[MCAST_EXCLUDE])
  675. continue;
  676. if (srcs[i] == psf->sf_inaddr) {
  677. scount++;
  678. break;
  679. }
  680. }
  681. }
  682. pmc->gsquery = 0;
  683. if (scount == nsrcs) /* all sources excluded */
  684. return 0;
  685. return 1;
  686. }
  687. static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __u32 *srcs)
  688. {
  689. struct ip_sf_list *psf;
  690. int i, scount;
  691. if (pmc->sfmode == MCAST_EXCLUDE)
  692. return igmp_xmarksources(pmc, nsrcs, srcs);
  693. /* mark INCLUDE-mode sources */
  694. scount = 0;
  695. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  696. if (scount == nsrcs)
  697. break;
  698. for (i=0; i<nsrcs; i++)
  699. if (srcs[i] == psf->sf_inaddr) {
  700. psf->sf_gsresp = 1;
  701. scount++;
  702. break;
  703. }
  704. }
  705. if (!scount) {
  706. pmc->gsquery = 0;
  707. return 0;
  708. }
  709. pmc->gsquery = 1;
  710. return 1;
  711. }
  712. static void igmp_heard_report(struct in_device *in_dev, u32 group)
  713. {
  714. struct ip_mc_list *im;
  715. /* Timers are only set for non-local groups */
  716. if (group == IGMP_ALL_HOSTS)
  717. return;
  718. read_lock(&in_dev->mc_list_lock);
  719. for (im=in_dev->mc_list; im!=NULL; im=im->next) {
  720. if (im->multiaddr == group) {
  721. igmp_stop_timer(im);
  722. break;
  723. }
  724. }
  725. read_unlock(&in_dev->mc_list_lock);
  726. }
  727. static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
  728. int len)
  729. {
  730. struct igmphdr *ih = skb->h.igmph;
  731. struct igmpv3_query *ih3 = (struct igmpv3_query *)ih;
  732. struct ip_mc_list *im;
  733. u32 group = ih->group;
  734. int max_delay;
  735. int mark = 0;
  736. if (len == 8) {
  737. if (ih->code == 0) {
  738. /* Alas, old v1 router presents here. */
  739. max_delay = IGMP_Query_Response_Interval;
  740. in_dev->mr_v1_seen = jiffies +
  741. IGMP_V1_Router_Present_Timeout;
  742. group = 0;
  743. } else {
  744. /* v2 router present */
  745. max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
  746. in_dev->mr_v2_seen = jiffies +
  747. IGMP_V2_Router_Present_Timeout;
  748. }
  749. /* cancel the interface change timer */
  750. in_dev->mr_ifc_count = 0;
  751. if (del_timer(&in_dev->mr_ifc_timer))
  752. __in_dev_put(in_dev);
  753. /* clear deleted report items */
  754. igmpv3_clear_delrec(in_dev);
  755. } else if (len < 12) {
  756. return; /* ignore bogus packet; freed by caller */
  757. } else { /* v3 */
  758. if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
  759. return;
  760. ih3 = (struct igmpv3_query *) skb->h.raw;
  761. if (ih3->nsrcs) {
  762. if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
  763. + ntohs(ih3->nsrcs)*sizeof(__u32)))
  764. return;
  765. ih3 = (struct igmpv3_query *) skb->h.raw;
  766. }
  767. max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
  768. if (!max_delay)
  769. max_delay = 1; /* can't mod w/ 0 */
  770. in_dev->mr_maxdelay = max_delay;
  771. if (ih3->qrv)
  772. in_dev->mr_qrv = ih3->qrv;
  773. if (!group) { /* general query */
  774. if (ih3->nsrcs)
  775. return; /* no sources allowed */
  776. igmp_gq_start_timer(in_dev);
  777. return;
  778. }
  779. /* mark sources to include, if group & source-specific */
  780. mark = ih3->nsrcs != 0;
  781. }
  782. /*
  783. * - Start the timers in all of our membership records
  784. * that the query applies to for the interface on
  785. * which the query arrived excl. those that belong
  786. * to a "local" group (224.0.0.X)
  787. * - For timers already running check if they need to
  788. * be reset.
  789. * - Use the igmp->igmp_code field as the maximum
  790. * delay possible
  791. */
  792. read_lock(&in_dev->mc_list_lock);
  793. for (im=in_dev->mc_list; im!=NULL; im=im->next) {
  794. int changed;
  795. if (group && group != im->multiaddr)
  796. continue;
  797. if (im->multiaddr == IGMP_ALL_HOSTS)
  798. continue;
  799. spin_lock_bh(&im->lock);
  800. if (im->tm_running)
  801. im->gsquery = im->gsquery && mark;
  802. else
  803. im->gsquery = mark;
  804. changed = !im->gsquery ||
  805. igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
  806. spin_unlock_bh(&im->lock);
  807. if (changed)
  808. igmp_mod_timer(im, max_delay);
  809. }
  810. read_unlock(&in_dev->mc_list_lock);
  811. }
  812. int igmp_rcv(struct sk_buff *skb)
  813. {
  814. /* This basically follows the spec line by line -- see RFC1112 */
  815. struct igmphdr *ih;
  816. struct in_device *in_dev = in_dev_get(skb->dev);
  817. int len = skb->len;
  818. if (in_dev==NULL) {
  819. kfree_skb(skb);
  820. return 0;
  821. }
  822. if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
  823. goto drop;
  824. switch (skb->ip_summed) {
  825. case CHECKSUM_HW:
  826. if (!(u16)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 = skb->h.igmph;
  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. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  842. /* Is it our report looped back? */
  843. if (((struct rtable*)skb->dst)->fl.iif == 0)
  844. break;
  845. /* don't rely on MC router hearing unicast reports */
  846. if (skb->pkt_type == PACKET_MULTICAST ||
  847. skb->pkt_type == PACKET_BROADCAST)
  848. igmp_heard_report(in_dev, ih->group);
  849. break;
  850. case IGMP_PIM:
  851. #ifdef CONFIG_IP_PIMSM_V1
  852. in_dev_put(in_dev);
  853. return pim_rcv_v1(skb);
  854. #endif
  855. case IGMP_DVMRP:
  856. case IGMP_TRACE:
  857. case IGMP_HOST_LEAVE_MESSAGE:
  858. case IGMP_MTRACE:
  859. case IGMP_MTRACE_RESP:
  860. break;
  861. default:
  862. break;
  863. }
  864. drop:
  865. in_dev_put(in_dev);
  866. kfree_skb(skb);
  867. return 0;
  868. }
  869. #endif
  870. /*
  871. * Add a filter to a device
  872. */
  873. static void ip_mc_filter_add(struct in_device *in_dev, u32 addr)
  874. {
  875. char buf[MAX_ADDR_LEN];
  876. struct net_device *dev = in_dev->dev;
  877. /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
  878. We will get multicast token leakage, when IFF_MULTICAST
  879. is changed. This check should be done in dev->set_multicast_list
  880. routine. Something sort of:
  881. if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
  882. --ANK
  883. */
  884. if (arp_mc_map(addr, buf, dev, 0) == 0)
  885. dev_mc_add(dev,buf,dev->addr_len,0);
  886. }
  887. /*
  888. * Remove a filter from a device
  889. */
  890. static void ip_mc_filter_del(struct in_device *in_dev, u32 addr)
  891. {
  892. char buf[MAX_ADDR_LEN];
  893. struct net_device *dev = in_dev->dev;
  894. if (arp_mc_map(addr, buf, dev, 0) == 0)
  895. dev_mc_delete(dev,buf,dev->addr_len,0);
  896. }
  897. #ifdef CONFIG_IP_MULTICAST
  898. /*
  899. * deleted ip_mc_list manipulation
  900. */
  901. static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
  902. {
  903. struct ip_mc_list *pmc;
  904. /* this is an "ip_mc_list" for convenience; only the fields below
  905. * are actually used. In particular, the refcnt and users are not
  906. * used for management of the delete list. Using the same structure
  907. * for deleted items allows change reports to use common code with
  908. * non-deleted or query-response MCA's.
  909. */
  910. pmc = kmalloc(sizeof(*pmc), GFP_KERNEL);
  911. if (!pmc)
  912. return;
  913. memset(pmc, 0, sizeof(*pmc));
  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, __u32 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, u32 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, u32 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 = IP_MAX_MEMBERSHIPS;
  1222. int sysctl_igmp_max_msf = IP_MAX_MSF;
  1223. static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
  1224. __u32 *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, __u32 *pmca, int sfmode,
  1269. int sfcount, __u32 *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. __u32 *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 = kmalloc(sizeof(*psf), GFP_ATOMIC);
  1342. if (!psf)
  1343. return -ENOBUFS;
  1344. memset(psf, 0, sizeof(*psf));
  1345. psf->sf_inaddr = *psfsrc;
  1346. if (psf_prev) {
  1347. psf_prev->sf_next = psf;
  1348. } else
  1349. pmc->sources = psf;
  1350. }
  1351. psf->sf_count[sfmode]++;
  1352. if (psf->sf_count[sfmode] == 1) {
  1353. ip_rt_multicast_event(pmc->interface);
  1354. }
  1355. return 0;
  1356. }
  1357. #ifdef CONFIG_IP_MULTICAST
  1358. static void sf_markstate(struct ip_mc_list *pmc)
  1359. {
  1360. struct ip_sf_list *psf;
  1361. int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
  1362. for (psf=pmc->sources; psf; psf=psf->sf_next)
  1363. if (pmc->sfcount[MCAST_EXCLUDE]) {
  1364. psf->sf_oldin = mca_xcount ==
  1365. psf->sf_count[MCAST_EXCLUDE] &&
  1366. !psf->sf_count[MCAST_INCLUDE];
  1367. } else
  1368. psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
  1369. }
  1370. static int sf_setstate(struct ip_mc_list *pmc)
  1371. {
  1372. struct ip_sf_list *psf, *dpsf;
  1373. int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
  1374. int qrv = pmc->interface->mr_qrv;
  1375. int new_in, rv;
  1376. rv = 0;
  1377. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  1378. if (pmc->sfcount[MCAST_EXCLUDE]) {
  1379. new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
  1380. !psf->sf_count[MCAST_INCLUDE];
  1381. } else
  1382. new_in = psf->sf_count[MCAST_INCLUDE] != 0;
  1383. if (new_in) {
  1384. if (!psf->sf_oldin) {
  1385. struct ip_sf_list *prev = 0;
  1386. for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
  1387. if (dpsf->sf_inaddr == psf->sf_inaddr)
  1388. break;
  1389. prev = dpsf;
  1390. }
  1391. if (dpsf) {
  1392. if (prev)
  1393. prev->sf_next = dpsf->sf_next;
  1394. else
  1395. pmc->tomb = dpsf->sf_next;
  1396. kfree(dpsf);
  1397. }
  1398. psf->sf_crcount = qrv;
  1399. rv++;
  1400. }
  1401. } else if (psf->sf_oldin) {
  1402. psf->sf_crcount = 0;
  1403. /*
  1404. * add or update "delete" records if an active filter
  1405. * is now inactive
  1406. */
  1407. for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
  1408. if (dpsf->sf_inaddr == psf->sf_inaddr)
  1409. break;
  1410. if (!dpsf) {
  1411. dpsf = (struct ip_sf_list *)
  1412. kmalloc(sizeof(*dpsf), GFP_ATOMIC);
  1413. if (!dpsf)
  1414. continue;
  1415. *dpsf = *psf;
  1416. /* pmc->lock held by callers */
  1417. dpsf->sf_next = pmc->tomb;
  1418. pmc->tomb = dpsf;
  1419. }
  1420. dpsf->sf_crcount = qrv;
  1421. rv++;
  1422. }
  1423. }
  1424. return rv;
  1425. }
  1426. #endif
  1427. /*
  1428. * Add multicast source filter list to the interface list
  1429. */
  1430. static int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
  1431. int sfcount, __u32 *psfsrc, int delta)
  1432. {
  1433. struct ip_mc_list *pmc;
  1434. int isexclude;
  1435. int i, err;
  1436. if (!in_dev)
  1437. return -ENODEV;
  1438. read_lock(&in_dev->mc_list_lock);
  1439. for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
  1440. if (*pmca == pmc->multiaddr)
  1441. break;
  1442. }
  1443. if (!pmc) {
  1444. /* MCA not found?? bug */
  1445. read_unlock(&in_dev->mc_list_lock);
  1446. return -ESRCH;
  1447. }
  1448. spin_lock_bh(&pmc->lock);
  1449. read_unlock(&in_dev->mc_list_lock);
  1450. #ifdef CONFIG_IP_MULTICAST
  1451. sf_markstate(pmc);
  1452. #endif
  1453. isexclude = pmc->sfmode == MCAST_EXCLUDE;
  1454. if (!delta)
  1455. pmc->sfcount[sfmode]++;
  1456. err = 0;
  1457. for (i=0; i<sfcount; i++) {
  1458. err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
  1459. if (err)
  1460. break;
  1461. }
  1462. if (err) {
  1463. int j;
  1464. pmc->sfcount[sfmode]--;
  1465. for (j=0; j<i; j++)
  1466. (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1467. } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
  1468. #ifdef CONFIG_IP_MULTICAST
  1469. struct in_device *in_dev = pmc->interface;
  1470. struct ip_sf_list *psf;
  1471. #endif
  1472. /* filter mode change */
  1473. if (pmc->sfcount[MCAST_EXCLUDE])
  1474. pmc->sfmode = MCAST_EXCLUDE;
  1475. else if (pmc->sfcount[MCAST_INCLUDE])
  1476. pmc->sfmode = MCAST_INCLUDE;
  1477. #ifdef CONFIG_IP_MULTICAST
  1478. /* else no filters; keep old mode for reports */
  1479. pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  1480. IGMP_Unsolicited_Report_Count;
  1481. in_dev->mr_ifc_count = pmc->crcount;
  1482. for (psf=pmc->sources; psf; psf = psf->sf_next)
  1483. psf->sf_crcount = 0;
  1484. igmp_ifc_event(in_dev);
  1485. } else if (sf_setstate(pmc)) {
  1486. igmp_ifc_event(in_dev);
  1487. #endif
  1488. }
  1489. spin_unlock_bh(&pmc->lock);
  1490. return err;
  1491. }
  1492. static void ip_mc_clear_src(struct ip_mc_list *pmc)
  1493. {
  1494. struct ip_sf_list *psf, *nextpsf;
  1495. for (psf=pmc->tomb; psf; psf=nextpsf) {
  1496. nextpsf = psf->sf_next;
  1497. kfree(psf);
  1498. }
  1499. pmc->tomb = NULL;
  1500. for (psf=pmc->sources; psf; psf=nextpsf) {
  1501. nextpsf = psf->sf_next;
  1502. kfree(psf);
  1503. }
  1504. pmc->sources = NULL;
  1505. pmc->sfmode = MCAST_EXCLUDE;
  1506. pmc->sfcount[MCAST_INCLUDE] = 0;
  1507. pmc->sfcount[MCAST_EXCLUDE] = 1;
  1508. }
  1509. /*
  1510. * Join a multicast group
  1511. */
  1512. int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
  1513. {
  1514. int err;
  1515. u32 addr = imr->imr_multiaddr.s_addr;
  1516. struct ip_mc_socklist *iml=NULL, *i;
  1517. struct in_device *in_dev;
  1518. struct inet_sock *inet = inet_sk(sk);
  1519. int ifindex;
  1520. int count = 0;
  1521. if (!MULTICAST(addr))
  1522. return -EINVAL;
  1523. rtnl_shlock();
  1524. in_dev = ip_mc_find_dev(imr);
  1525. if (!in_dev) {
  1526. iml = NULL;
  1527. err = -ENODEV;
  1528. goto done;
  1529. }
  1530. err = -EADDRINUSE;
  1531. ifindex = imr->imr_ifindex;
  1532. for (i = inet->mc_list; i; i = i->next) {
  1533. if (i->multi.imr_multiaddr.s_addr == addr &&
  1534. i->multi.imr_ifindex == ifindex)
  1535. goto done;
  1536. count++;
  1537. }
  1538. err = -ENOBUFS;
  1539. if (count >= sysctl_igmp_max_memberships)
  1540. goto done;
  1541. iml = sock_kmalloc(sk,sizeof(*iml),GFP_KERNEL);
  1542. if (iml == NULL)
  1543. goto done;
  1544. memcpy(&iml->multi, imr, sizeof(*imr));
  1545. iml->next = inet->mc_list;
  1546. iml->sflist = NULL;
  1547. iml->sfmode = MCAST_EXCLUDE;
  1548. inet->mc_list = iml;
  1549. ip_mc_inc_group(in_dev, addr);
  1550. err = 0;
  1551. done:
  1552. rtnl_shunlock();
  1553. return err;
  1554. }
  1555. static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
  1556. struct in_device *in_dev)
  1557. {
  1558. int err;
  1559. if (iml->sflist == 0) {
  1560. /* any-source empty exclude case */
  1561. return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
  1562. iml->sfmode, 0, NULL, 0);
  1563. }
  1564. err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
  1565. iml->sfmode, iml->sflist->sl_count,
  1566. iml->sflist->sl_addr, 0);
  1567. sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
  1568. iml->sflist = NULL;
  1569. return err;
  1570. }
  1571. /*
  1572. * Ask a socket to leave a group.
  1573. */
  1574. int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
  1575. {
  1576. struct inet_sock *inet = inet_sk(sk);
  1577. struct ip_mc_socklist *iml, **imlp;
  1578. struct in_device *in_dev;
  1579. u32 group = imr->imr_multiaddr.s_addr;
  1580. u32 ifindex;
  1581. rtnl_lock();
  1582. in_dev = ip_mc_find_dev(imr);
  1583. if (!in_dev) {
  1584. rtnl_unlock();
  1585. return -ENODEV;
  1586. }
  1587. ifindex = imr->imr_ifindex;
  1588. for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
  1589. if (iml->multi.imr_multiaddr.s_addr == group &&
  1590. iml->multi.imr_ifindex == ifindex) {
  1591. (void) ip_mc_leave_src(sk, iml, in_dev);
  1592. *imlp = iml->next;
  1593. ip_mc_dec_group(in_dev, group);
  1594. rtnl_unlock();
  1595. sock_kfree_s(sk, iml, sizeof(*iml));
  1596. return 0;
  1597. }
  1598. }
  1599. rtnl_unlock();
  1600. return -EADDRNOTAVAIL;
  1601. }
  1602. int ip_mc_source(int add, int omode, struct sock *sk, struct
  1603. ip_mreq_source *mreqs, int ifindex)
  1604. {
  1605. int err;
  1606. struct ip_mreqn imr;
  1607. u32 addr = mreqs->imr_multiaddr;
  1608. struct ip_mc_socklist *pmc;
  1609. struct in_device *in_dev = NULL;
  1610. struct inet_sock *inet = inet_sk(sk);
  1611. struct ip_sf_socklist *psl;
  1612. int leavegroup = 0;
  1613. int i, j, rv;
  1614. if (!MULTICAST(addr))
  1615. return -EINVAL;
  1616. rtnl_shlock();
  1617. imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
  1618. imr.imr_address.s_addr = mreqs->imr_interface;
  1619. imr.imr_ifindex = ifindex;
  1620. in_dev = ip_mc_find_dev(&imr);
  1621. if (!in_dev) {
  1622. err = -ENODEV;
  1623. goto done;
  1624. }
  1625. err = -EADDRNOTAVAIL;
  1626. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1627. if (pmc->multi.imr_multiaddr.s_addr == imr.imr_multiaddr.s_addr
  1628. && pmc->multi.imr_ifindex == imr.imr_ifindex)
  1629. break;
  1630. }
  1631. if (!pmc) { /* must have a prior join */
  1632. err = -EINVAL;
  1633. goto done;
  1634. }
  1635. /* if a source filter was set, must be the same mode as before */
  1636. if (pmc->sflist) {
  1637. if (pmc->sfmode != omode) {
  1638. err = -EINVAL;
  1639. goto done;
  1640. }
  1641. } else if (pmc->sfmode != omode) {
  1642. /* allow mode switches for empty-set filters */
  1643. ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
  1644. ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
  1645. NULL, 0);
  1646. pmc->sfmode = omode;
  1647. }
  1648. psl = pmc->sflist;
  1649. if (!add) {
  1650. if (!psl)
  1651. goto done; /* err = -EADDRNOTAVAIL */
  1652. rv = !0;
  1653. for (i=0; i<psl->sl_count; i++) {
  1654. rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
  1655. sizeof(__u32));
  1656. if (rv == 0)
  1657. break;
  1658. }
  1659. if (rv) /* source not found */
  1660. goto done; /* err = -EADDRNOTAVAIL */
  1661. /* special case - (INCLUDE, empty) == LEAVE_GROUP */
  1662. if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
  1663. leavegroup = 1;
  1664. goto done;
  1665. }
  1666. /* update the interface filter */
  1667. ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
  1668. &mreqs->imr_sourceaddr, 1);
  1669. for (j=i+1; j<psl->sl_count; j++)
  1670. psl->sl_addr[j-1] = psl->sl_addr[j];
  1671. psl->sl_count--;
  1672. err = 0;
  1673. goto done;
  1674. }
  1675. /* else, add a new source to the filter */
  1676. if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
  1677. err = -ENOBUFS;
  1678. goto done;
  1679. }
  1680. if (!psl || psl->sl_count == psl->sl_max) {
  1681. struct ip_sf_socklist *newpsl;
  1682. int count = IP_SFBLOCK;
  1683. if (psl)
  1684. count += psl->sl_max;
  1685. newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
  1686. if (!newpsl) {
  1687. err = -ENOBUFS;
  1688. goto done;
  1689. }
  1690. newpsl->sl_max = count;
  1691. newpsl->sl_count = count - IP_SFBLOCK;
  1692. if (psl) {
  1693. for (i=0; i<psl->sl_count; i++)
  1694. newpsl->sl_addr[i] = psl->sl_addr[i];
  1695. sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
  1696. }
  1697. pmc->sflist = psl = newpsl;
  1698. }
  1699. rv = 1; /* > 0 for insert logic below if sl_count is 0 */
  1700. for (i=0; i<psl->sl_count; i++) {
  1701. rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
  1702. sizeof(__u32));
  1703. if (rv == 0)
  1704. break;
  1705. }
  1706. if (rv == 0) /* address already there is an error */
  1707. goto done;
  1708. for (j=psl->sl_count-1; j>=i; j--)
  1709. psl->sl_addr[j+1] = psl->sl_addr[j];
  1710. psl->sl_addr[i] = mreqs->imr_sourceaddr;
  1711. psl->sl_count++;
  1712. err = 0;
  1713. /* update the interface list */
  1714. ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
  1715. &mreqs->imr_sourceaddr, 1);
  1716. done:
  1717. rtnl_shunlock();
  1718. if (leavegroup)
  1719. return ip_mc_leave_group(sk, &imr);
  1720. return err;
  1721. }
  1722. int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
  1723. {
  1724. int err = 0;
  1725. struct ip_mreqn imr;
  1726. u32 addr = msf->imsf_multiaddr;
  1727. struct ip_mc_socklist *pmc;
  1728. struct in_device *in_dev;
  1729. struct inet_sock *inet = inet_sk(sk);
  1730. struct ip_sf_socklist *newpsl, *psl;
  1731. int leavegroup = 0;
  1732. if (!MULTICAST(addr))
  1733. return -EINVAL;
  1734. if (msf->imsf_fmode != MCAST_INCLUDE &&
  1735. msf->imsf_fmode != MCAST_EXCLUDE)
  1736. return -EINVAL;
  1737. rtnl_shlock();
  1738. imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
  1739. imr.imr_address.s_addr = msf->imsf_interface;
  1740. imr.imr_ifindex = ifindex;
  1741. in_dev = ip_mc_find_dev(&imr);
  1742. if (!in_dev) {
  1743. err = -ENODEV;
  1744. goto done;
  1745. }
  1746. /* special case - (INCLUDE, empty) == LEAVE_GROUP */
  1747. if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
  1748. leavegroup = 1;
  1749. goto done;
  1750. }
  1751. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1752. if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
  1753. pmc->multi.imr_ifindex == imr.imr_ifindex)
  1754. break;
  1755. }
  1756. if (!pmc) { /* must have a prior join */
  1757. err = -EINVAL;
  1758. goto done;
  1759. }
  1760. if (msf->imsf_numsrc) {
  1761. newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
  1762. GFP_KERNEL);
  1763. if (!newpsl) {
  1764. err = -ENOBUFS;
  1765. goto done;
  1766. }
  1767. newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
  1768. memcpy(newpsl->sl_addr, msf->imsf_slist,
  1769. msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
  1770. err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
  1771. msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
  1772. if (err) {
  1773. sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
  1774. goto done;
  1775. }
  1776. } else {
  1777. newpsl = NULL;
  1778. (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
  1779. msf->imsf_fmode, 0, NULL, 0);
  1780. }
  1781. psl = pmc->sflist;
  1782. if (psl) {
  1783. (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
  1784. psl->sl_count, psl->sl_addr, 0);
  1785. sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
  1786. } else
  1787. (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
  1788. 0, NULL, 0);
  1789. pmc->sflist = newpsl;
  1790. pmc->sfmode = msf->imsf_fmode;
  1791. err = 0;
  1792. done:
  1793. rtnl_shunlock();
  1794. if (leavegroup)
  1795. err = ip_mc_leave_group(sk, &imr);
  1796. return err;
  1797. }
  1798. int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
  1799. struct ip_msfilter __user *optval, int __user *optlen)
  1800. {
  1801. int err, len, count, copycount;
  1802. struct ip_mreqn imr;
  1803. u32 addr = msf->imsf_multiaddr;
  1804. struct ip_mc_socklist *pmc;
  1805. struct in_device *in_dev;
  1806. struct inet_sock *inet = inet_sk(sk);
  1807. struct ip_sf_socklist *psl;
  1808. if (!MULTICAST(addr))
  1809. return -EINVAL;
  1810. rtnl_shlock();
  1811. imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
  1812. imr.imr_address.s_addr = msf->imsf_interface;
  1813. imr.imr_ifindex = 0;
  1814. in_dev = ip_mc_find_dev(&imr);
  1815. if (!in_dev) {
  1816. err = -ENODEV;
  1817. goto done;
  1818. }
  1819. err = -EADDRNOTAVAIL;
  1820. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1821. if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
  1822. pmc->multi.imr_ifindex == imr.imr_ifindex)
  1823. break;
  1824. }
  1825. if (!pmc) /* must have a prior join */
  1826. goto done;
  1827. msf->imsf_fmode = pmc->sfmode;
  1828. psl = pmc->sflist;
  1829. rtnl_shunlock();
  1830. if (!psl) {
  1831. len = 0;
  1832. count = 0;
  1833. } else {
  1834. count = psl->sl_count;
  1835. }
  1836. copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
  1837. len = copycount * sizeof(psl->sl_addr[0]);
  1838. msf->imsf_numsrc = count;
  1839. if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
  1840. copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
  1841. return -EFAULT;
  1842. }
  1843. if (len &&
  1844. copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
  1845. return -EFAULT;
  1846. return 0;
  1847. done:
  1848. rtnl_shunlock();
  1849. return err;
  1850. }
  1851. int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
  1852. struct group_filter __user *optval, int __user *optlen)
  1853. {
  1854. int err, i, count, copycount;
  1855. struct sockaddr_in *psin;
  1856. u32 addr;
  1857. struct ip_mc_socklist *pmc;
  1858. struct inet_sock *inet = inet_sk(sk);
  1859. struct ip_sf_socklist *psl;
  1860. psin = (struct sockaddr_in *)&gsf->gf_group;
  1861. if (psin->sin_family != AF_INET)
  1862. return -EINVAL;
  1863. addr = psin->sin_addr.s_addr;
  1864. if (!MULTICAST(addr))
  1865. return -EINVAL;
  1866. rtnl_shlock();
  1867. err = -EADDRNOTAVAIL;
  1868. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1869. if (pmc->multi.imr_multiaddr.s_addr == addr &&
  1870. pmc->multi.imr_ifindex == gsf->gf_interface)
  1871. break;
  1872. }
  1873. if (!pmc) /* must have a prior join */
  1874. goto done;
  1875. gsf->gf_fmode = pmc->sfmode;
  1876. psl = pmc->sflist;
  1877. rtnl_shunlock();
  1878. count = psl ? psl->sl_count : 0;
  1879. copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
  1880. gsf->gf_numsrc = count;
  1881. if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
  1882. copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
  1883. return -EFAULT;
  1884. }
  1885. for (i=0; i<copycount; i++) {
  1886. struct sockaddr_in *psin;
  1887. struct sockaddr_storage ss;
  1888. psin = (struct sockaddr_in *)&ss;
  1889. memset(&ss, 0, sizeof(ss));
  1890. psin->sin_family = AF_INET;
  1891. psin->sin_addr.s_addr = psl->sl_addr[i];
  1892. if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
  1893. return -EFAULT;
  1894. }
  1895. return 0;
  1896. done:
  1897. rtnl_shunlock();
  1898. return err;
  1899. }
  1900. /*
  1901. * check if a multicast source filter allows delivery for a given <src,dst,intf>
  1902. */
  1903. int ip_mc_sf_allow(struct sock *sk, u32 loc_addr, u32 rmt_addr, int dif)
  1904. {
  1905. struct inet_sock *inet = inet_sk(sk);
  1906. struct ip_mc_socklist *pmc;
  1907. struct ip_sf_socklist *psl;
  1908. int i;
  1909. if (!MULTICAST(loc_addr))
  1910. return 1;
  1911. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1912. if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
  1913. pmc->multi.imr_ifindex == dif)
  1914. break;
  1915. }
  1916. if (!pmc)
  1917. return 1;
  1918. psl = pmc->sflist;
  1919. if (!psl)
  1920. return pmc->sfmode == MCAST_EXCLUDE;
  1921. for (i=0; i<psl->sl_count; i++) {
  1922. if (psl->sl_addr[i] == rmt_addr)
  1923. break;
  1924. }
  1925. if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
  1926. return 0;
  1927. if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
  1928. return 0;
  1929. return 1;
  1930. }
  1931. /*
  1932. * A socket is closing.
  1933. */
  1934. void ip_mc_drop_socket(struct sock *sk)
  1935. {
  1936. struct inet_sock *inet = inet_sk(sk);
  1937. struct ip_mc_socklist *iml;
  1938. if (inet->mc_list == NULL)
  1939. return;
  1940. rtnl_lock();
  1941. while ((iml = inet->mc_list) != NULL) {
  1942. struct in_device *in_dev;
  1943. inet->mc_list = iml->next;
  1944. if ((in_dev = inetdev_by_index(iml->multi.imr_ifindex)) != NULL) {
  1945. (void) ip_mc_leave_src(sk, iml, in_dev);
  1946. ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
  1947. in_dev_put(in_dev);
  1948. }
  1949. sock_kfree_s(sk, iml, sizeof(*iml));
  1950. }
  1951. rtnl_unlock();
  1952. }
  1953. int ip_check_mc(struct in_device *in_dev, u32 mc_addr, u32 src_addr, u16 proto)
  1954. {
  1955. struct ip_mc_list *im;
  1956. struct ip_sf_list *psf;
  1957. int rv = 0;
  1958. read_lock(&in_dev->mc_list_lock);
  1959. for (im=in_dev->mc_list; im; im=im->next) {
  1960. if (im->multiaddr == mc_addr)
  1961. break;
  1962. }
  1963. if (im && proto == IPPROTO_IGMP) {
  1964. rv = 1;
  1965. } else if (im) {
  1966. if (src_addr) {
  1967. for (psf=im->sources; psf; psf=psf->sf_next) {
  1968. if (psf->sf_inaddr == src_addr)
  1969. break;
  1970. }
  1971. if (psf)
  1972. rv = psf->sf_count[MCAST_INCLUDE] ||
  1973. psf->sf_count[MCAST_EXCLUDE] !=
  1974. im->sfcount[MCAST_EXCLUDE];
  1975. else
  1976. rv = im->sfcount[MCAST_EXCLUDE] != 0;
  1977. } else
  1978. rv = 1; /* unspecified source; tentatively allow */
  1979. }
  1980. read_unlock(&in_dev->mc_list_lock);
  1981. return rv;
  1982. }
  1983. #if defined(CONFIG_PROC_FS)
  1984. struct igmp_mc_iter_state {
  1985. struct net_device *dev;
  1986. struct in_device *in_dev;
  1987. };
  1988. #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
  1989. static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
  1990. {
  1991. struct ip_mc_list *im = NULL;
  1992. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  1993. for (state->dev = dev_base, state->in_dev = NULL;
  1994. state->dev;
  1995. state->dev = state->dev->next) {
  1996. struct in_device *in_dev;
  1997. in_dev = in_dev_get(state->dev);
  1998. if (!in_dev)
  1999. continue;
  2000. read_lock(&in_dev->mc_list_lock);
  2001. im = in_dev->mc_list;
  2002. if (im) {
  2003. state->in_dev = in_dev;
  2004. break;
  2005. }
  2006. read_unlock(&in_dev->mc_list_lock);
  2007. in_dev_put(in_dev);
  2008. }
  2009. return im;
  2010. }
  2011. static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
  2012. {
  2013. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2014. im = im->next;
  2015. while (!im) {
  2016. if (likely(state->in_dev != NULL)) {
  2017. read_unlock(&state->in_dev->mc_list_lock);
  2018. in_dev_put(state->in_dev);
  2019. }
  2020. state->dev = state->dev->next;
  2021. if (!state->dev) {
  2022. state->in_dev = NULL;
  2023. break;
  2024. }
  2025. state->in_dev = in_dev_get(state->dev);
  2026. if (!state->in_dev)
  2027. continue;
  2028. read_lock(&state->in_dev->mc_list_lock);
  2029. im = state->in_dev->mc_list;
  2030. }
  2031. return im;
  2032. }
  2033. static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
  2034. {
  2035. struct ip_mc_list *im = igmp_mc_get_first(seq);
  2036. if (im)
  2037. while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
  2038. --pos;
  2039. return pos ? NULL : im;
  2040. }
  2041. static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
  2042. {
  2043. read_lock(&dev_base_lock);
  2044. return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2045. }
  2046. static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2047. {
  2048. struct ip_mc_list *im;
  2049. if (v == SEQ_START_TOKEN)
  2050. im = igmp_mc_get_first(seq);
  2051. else
  2052. im = igmp_mc_get_next(seq, v);
  2053. ++*pos;
  2054. return im;
  2055. }
  2056. static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
  2057. {
  2058. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2059. if (likely(state->in_dev != NULL)) {
  2060. read_unlock(&state->in_dev->mc_list_lock);
  2061. in_dev_put(state->in_dev);
  2062. state->in_dev = NULL;
  2063. }
  2064. state->dev = NULL;
  2065. read_unlock(&dev_base_lock);
  2066. }
  2067. static int igmp_mc_seq_show(struct seq_file *seq, void *v)
  2068. {
  2069. if (v == SEQ_START_TOKEN)
  2070. seq_puts(seq,
  2071. "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
  2072. else {
  2073. struct ip_mc_list *im = (struct ip_mc_list *)v;
  2074. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2075. char *querier;
  2076. #ifdef CONFIG_IP_MULTICAST
  2077. querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
  2078. IGMP_V2_SEEN(state->in_dev) ? "V2" :
  2079. "V3";
  2080. #else
  2081. querier = "NONE";
  2082. #endif
  2083. if (state->in_dev->mc_list == im) {
  2084. seq_printf(seq, "%d\t%-10s: %5d %7s\n",
  2085. state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
  2086. }
  2087. seq_printf(seq,
  2088. "\t\t\t\t%08lX %5d %d:%08lX\t\t%d\n",
  2089. im->multiaddr, im->users,
  2090. im->tm_running, im->tm_running ?
  2091. jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
  2092. im->reporter);
  2093. }
  2094. return 0;
  2095. }
  2096. static struct seq_operations igmp_mc_seq_ops = {
  2097. .start = igmp_mc_seq_start,
  2098. .next = igmp_mc_seq_next,
  2099. .stop = igmp_mc_seq_stop,
  2100. .show = igmp_mc_seq_show,
  2101. };
  2102. static int igmp_mc_seq_open(struct inode *inode, struct file *file)
  2103. {
  2104. struct seq_file *seq;
  2105. int rc = -ENOMEM;
  2106. struct igmp_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  2107. if (!s)
  2108. goto out;
  2109. rc = seq_open(file, &igmp_mc_seq_ops);
  2110. if (rc)
  2111. goto out_kfree;
  2112. seq = file->private_data;
  2113. seq->private = s;
  2114. memset(s, 0, sizeof(*s));
  2115. out:
  2116. return rc;
  2117. out_kfree:
  2118. kfree(s);
  2119. goto out;
  2120. }
  2121. static struct file_operations igmp_mc_seq_fops = {
  2122. .owner = THIS_MODULE,
  2123. .open = igmp_mc_seq_open,
  2124. .read = seq_read,
  2125. .llseek = seq_lseek,
  2126. .release = seq_release_private,
  2127. };
  2128. struct igmp_mcf_iter_state {
  2129. struct net_device *dev;
  2130. struct in_device *idev;
  2131. struct ip_mc_list *im;
  2132. };
  2133. #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
  2134. static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
  2135. {
  2136. struct ip_sf_list *psf = NULL;
  2137. struct ip_mc_list *im = NULL;
  2138. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2139. for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
  2140. state->dev;
  2141. state->dev = state->dev->next) {
  2142. struct in_device *idev;
  2143. idev = in_dev_get(state->dev);
  2144. if (unlikely(idev == NULL))
  2145. continue;
  2146. read_lock(&idev->mc_list_lock);
  2147. im = idev->mc_list;
  2148. if (likely(im != NULL)) {
  2149. spin_lock_bh(&im->lock);
  2150. psf = im->sources;
  2151. if (likely(psf != NULL)) {
  2152. state->im = im;
  2153. state->idev = idev;
  2154. break;
  2155. }
  2156. spin_unlock_bh(&im->lock);
  2157. }
  2158. read_unlock(&idev->mc_list_lock);
  2159. in_dev_put(idev);
  2160. }
  2161. return psf;
  2162. }
  2163. static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
  2164. {
  2165. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2166. psf = psf->sf_next;
  2167. while (!psf) {
  2168. spin_unlock_bh(&state->im->lock);
  2169. state->im = state->im->next;
  2170. while (!state->im) {
  2171. if (likely(state->idev != NULL)) {
  2172. read_unlock(&state->idev->mc_list_lock);
  2173. in_dev_put(state->idev);
  2174. }
  2175. state->dev = state->dev->next;
  2176. if (!state->dev) {
  2177. state->idev = NULL;
  2178. goto out;
  2179. }
  2180. state->idev = in_dev_get(state->dev);
  2181. if (!state->idev)
  2182. continue;
  2183. read_lock(&state->idev->mc_list_lock);
  2184. state->im = state->idev->mc_list;
  2185. }
  2186. if (!state->im)
  2187. break;
  2188. spin_lock_bh(&state->im->lock);
  2189. psf = state->im->sources;
  2190. }
  2191. out:
  2192. return psf;
  2193. }
  2194. static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
  2195. {
  2196. struct ip_sf_list *psf = igmp_mcf_get_first(seq);
  2197. if (psf)
  2198. while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
  2199. --pos;
  2200. return pos ? NULL : psf;
  2201. }
  2202. static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
  2203. {
  2204. read_lock(&dev_base_lock);
  2205. return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2206. }
  2207. static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2208. {
  2209. struct ip_sf_list *psf;
  2210. if (v == SEQ_START_TOKEN)
  2211. psf = igmp_mcf_get_first(seq);
  2212. else
  2213. psf = igmp_mcf_get_next(seq, v);
  2214. ++*pos;
  2215. return psf;
  2216. }
  2217. static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
  2218. {
  2219. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2220. if (likely(state->im != NULL)) {
  2221. spin_unlock_bh(&state->im->lock);
  2222. state->im = NULL;
  2223. }
  2224. if (likely(state->idev != NULL)) {
  2225. read_unlock(&state->idev->mc_list_lock);
  2226. in_dev_put(state->idev);
  2227. state->idev = NULL;
  2228. }
  2229. state->dev = NULL;
  2230. read_unlock(&dev_base_lock);
  2231. }
  2232. static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
  2233. {
  2234. struct ip_sf_list *psf = (struct ip_sf_list *)v;
  2235. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2236. if (v == SEQ_START_TOKEN) {
  2237. seq_printf(seq,
  2238. "%3s %6s "
  2239. "%10s %10s %6s %6s\n", "Idx",
  2240. "Device", "MCA",
  2241. "SRC", "INC", "EXC");
  2242. } else {
  2243. seq_printf(seq,
  2244. "%3d %6.6s 0x%08x "
  2245. "0x%08x %6lu %6lu\n",
  2246. state->dev->ifindex, state->dev->name,
  2247. ntohl(state->im->multiaddr),
  2248. ntohl(psf->sf_inaddr),
  2249. psf->sf_count[MCAST_INCLUDE],
  2250. psf->sf_count[MCAST_EXCLUDE]);
  2251. }
  2252. return 0;
  2253. }
  2254. static struct seq_operations igmp_mcf_seq_ops = {
  2255. .start = igmp_mcf_seq_start,
  2256. .next = igmp_mcf_seq_next,
  2257. .stop = igmp_mcf_seq_stop,
  2258. .show = igmp_mcf_seq_show,
  2259. };
  2260. static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
  2261. {
  2262. struct seq_file *seq;
  2263. int rc = -ENOMEM;
  2264. struct igmp_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  2265. if (!s)
  2266. goto out;
  2267. rc = seq_open(file, &igmp_mcf_seq_ops);
  2268. if (rc)
  2269. goto out_kfree;
  2270. seq = file->private_data;
  2271. seq->private = s;
  2272. memset(s, 0, sizeof(*s));
  2273. out:
  2274. return rc;
  2275. out_kfree:
  2276. kfree(s);
  2277. goto out;
  2278. }
  2279. static struct file_operations igmp_mcf_seq_fops = {
  2280. .owner = THIS_MODULE,
  2281. .open = igmp_mcf_seq_open,
  2282. .read = seq_read,
  2283. .llseek = seq_lseek,
  2284. .release = seq_release_private,
  2285. };
  2286. int __init igmp_mc_proc_init(void)
  2287. {
  2288. proc_net_fops_create("igmp", S_IRUGO, &igmp_mc_seq_fops);
  2289. proc_net_fops_create("mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
  2290. return 0;
  2291. }
  2292. #endif
  2293. EXPORT_SYMBOL(ip_mc_dec_group);
  2294. EXPORT_SYMBOL(ip_mc_inc_group);
  2295. EXPORT_SYMBOL(ip_mc_join_group);