igmp.c 63 KB

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