igmp.c 59 KB

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