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