igmp.c 62 KB

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