igmp.c 61 KB

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