igmp.c 61 KB

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