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