igmp.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683
  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->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->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->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->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. /* called in rcu_read_lock() section */
  814. int igmp_rcv(struct sk_buff *skb)
  815. {
  816. /* This basically follows the spec line by line -- see RFC1112 */
  817. struct igmphdr *ih;
  818. struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
  819. int len = skb->len;
  820. if (in_dev == NULL)
  821. goto drop;
  822. if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
  823. goto drop;
  824. switch (skb->ip_summed) {
  825. case CHECKSUM_COMPLETE:
  826. if (!csum_fold(skb->csum))
  827. break;
  828. /* fall through */
  829. case CHECKSUM_NONE:
  830. skb->csum = 0;
  831. if (__skb_checksum_complete(skb))
  832. goto drop;
  833. }
  834. ih = igmp_hdr(skb);
  835. switch (ih->type) {
  836. case IGMP_HOST_MEMBERSHIP_QUERY:
  837. igmp_heard_query(in_dev, skb, len);
  838. break;
  839. case IGMP_HOST_MEMBERSHIP_REPORT:
  840. case IGMPV2_HOST_MEMBERSHIP_REPORT:
  841. /* Is it our report looped back? */
  842. if (skb_rtable(skb)->fl.iif == 0)
  843. break;
  844. /* don't rely on MC router hearing unicast reports */
  845. if (skb->pkt_type == PACKET_MULTICAST ||
  846. skb->pkt_type == PACKET_BROADCAST)
  847. igmp_heard_report(in_dev, ih->group);
  848. break;
  849. case IGMP_PIM:
  850. #ifdef CONFIG_IP_PIMSM_V1
  851. 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:
  864. kfree_skb(skb);
  865. return 0;
  866. }
  867. #endif
  868. /*
  869. * Add a filter to a device
  870. */
  871. static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
  872. {
  873. char buf[MAX_ADDR_LEN];
  874. struct net_device *dev = in_dev->dev;
  875. /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
  876. We will get multicast token leakage, when IFF_MULTICAST
  877. is changed. This check should be done in dev->set_multicast_list
  878. routine. Something sort of:
  879. if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
  880. --ANK
  881. */
  882. if (arp_mc_map(addr, buf, dev, 0) == 0)
  883. dev_mc_add(dev, buf);
  884. }
  885. /*
  886. * Remove a filter from a device
  887. */
  888. static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
  889. {
  890. char buf[MAX_ADDR_LEN];
  891. struct net_device *dev = in_dev->dev;
  892. if (arp_mc_map(addr, buf, dev, 0) == 0)
  893. dev_mc_del(dev, buf);
  894. }
  895. #ifdef CONFIG_IP_MULTICAST
  896. /*
  897. * deleted ip_mc_list manipulation
  898. */
  899. static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
  900. {
  901. struct ip_mc_list *pmc;
  902. /* this is an "ip_mc_list" for convenience; only the fields below
  903. * are actually used. In particular, the refcnt and users are not
  904. * used for management of the delete list. Using the same structure
  905. * for deleted items allows change reports to use common code with
  906. * non-deleted or query-response MCA's.
  907. */
  908. pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
  909. if (!pmc)
  910. return;
  911. spin_lock_bh(&im->lock);
  912. pmc->interface = im->interface;
  913. in_dev_hold(in_dev);
  914. pmc->multiaddr = im->multiaddr;
  915. pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  916. IGMP_Unsolicited_Report_Count;
  917. pmc->sfmode = im->sfmode;
  918. if (pmc->sfmode == MCAST_INCLUDE) {
  919. struct ip_sf_list *psf;
  920. pmc->tomb = im->tomb;
  921. pmc->sources = im->sources;
  922. im->tomb = im->sources = NULL;
  923. for (psf=pmc->sources; psf; psf=psf->sf_next)
  924. psf->sf_crcount = pmc->crcount;
  925. }
  926. spin_unlock_bh(&im->lock);
  927. spin_lock_bh(&in_dev->mc_tomb_lock);
  928. pmc->next = in_dev->mc_tomb;
  929. in_dev->mc_tomb = pmc;
  930. spin_unlock_bh(&in_dev->mc_tomb_lock);
  931. }
  932. static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
  933. {
  934. struct ip_mc_list *pmc, *pmc_prev;
  935. struct ip_sf_list *psf, *psf_next;
  936. spin_lock_bh(&in_dev->mc_tomb_lock);
  937. pmc_prev = NULL;
  938. for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
  939. if (pmc->multiaddr == multiaddr)
  940. break;
  941. pmc_prev = pmc;
  942. }
  943. if (pmc) {
  944. if (pmc_prev)
  945. pmc_prev->next = pmc->next;
  946. else
  947. in_dev->mc_tomb = pmc->next;
  948. }
  949. spin_unlock_bh(&in_dev->mc_tomb_lock);
  950. if (pmc) {
  951. for (psf=pmc->tomb; psf; psf=psf_next) {
  952. psf_next = psf->sf_next;
  953. kfree(psf);
  954. }
  955. in_dev_put(pmc->interface);
  956. kfree(pmc);
  957. }
  958. }
  959. static void igmpv3_clear_delrec(struct in_device *in_dev)
  960. {
  961. struct ip_mc_list *pmc, *nextpmc;
  962. spin_lock_bh(&in_dev->mc_tomb_lock);
  963. pmc = in_dev->mc_tomb;
  964. in_dev->mc_tomb = NULL;
  965. spin_unlock_bh(&in_dev->mc_tomb_lock);
  966. for (; pmc; pmc = nextpmc) {
  967. nextpmc = pmc->next;
  968. ip_mc_clear_src(pmc);
  969. in_dev_put(pmc->interface);
  970. kfree(pmc);
  971. }
  972. /* clear dead sources, too */
  973. read_lock(&in_dev->mc_list_lock);
  974. for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
  975. struct ip_sf_list *psf, *psf_next;
  976. spin_lock_bh(&pmc->lock);
  977. psf = pmc->tomb;
  978. pmc->tomb = NULL;
  979. spin_unlock_bh(&pmc->lock);
  980. for (; psf; psf=psf_next) {
  981. psf_next = psf->sf_next;
  982. kfree(psf);
  983. }
  984. }
  985. read_unlock(&in_dev->mc_list_lock);
  986. }
  987. #endif
  988. static void igmp_group_dropped(struct ip_mc_list *im)
  989. {
  990. struct in_device *in_dev = im->interface;
  991. #ifdef CONFIG_IP_MULTICAST
  992. int reporter;
  993. #endif
  994. if (im->loaded) {
  995. im->loaded = 0;
  996. ip_mc_filter_del(in_dev, im->multiaddr);
  997. }
  998. #ifdef CONFIG_IP_MULTICAST
  999. if (im->multiaddr == IGMP_ALL_HOSTS)
  1000. return;
  1001. reporter = im->reporter;
  1002. igmp_stop_timer(im);
  1003. if (!in_dev->dead) {
  1004. if (IGMP_V1_SEEN(in_dev))
  1005. goto done;
  1006. if (IGMP_V2_SEEN(in_dev)) {
  1007. if (reporter)
  1008. igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
  1009. goto done;
  1010. }
  1011. /* IGMPv3 */
  1012. igmpv3_add_delrec(in_dev, im);
  1013. igmp_ifc_event(in_dev);
  1014. }
  1015. done:
  1016. #endif
  1017. ip_mc_clear_src(im);
  1018. }
  1019. static void igmp_group_added(struct ip_mc_list *im)
  1020. {
  1021. struct in_device *in_dev = im->interface;
  1022. if (im->loaded == 0) {
  1023. im->loaded = 1;
  1024. ip_mc_filter_add(in_dev, im->multiaddr);
  1025. }
  1026. #ifdef CONFIG_IP_MULTICAST
  1027. if (im->multiaddr == IGMP_ALL_HOSTS)
  1028. return;
  1029. if (in_dev->dead)
  1030. return;
  1031. if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
  1032. spin_lock_bh(&im->lock);
  1033. igmp_start_timer(im, IGMP_Initial_Report_Delay);
  1034. spin_unlock_bh(&im->lock);
  1035. return;
  1036. }
  1037. /* else, v3 */
  1038. im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  1039. IGMP_Unsolicited_Report_Count;
  1040. igmp_ifc_event(in_dev);
  1041. #endif
  1042. }
  1043. /*
  1044. * Multicast list managers
  1045. */
  1046. /*
  1047. * A socket has joined a multicast group on device dev.
  1048. */
  1049. void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
  1050. {
  1051. struct ip_mc_list *im;
  1052. ASSERT_RTNL();
  1053. for (im=in_dev->mc_list; im; im=im->next) {
  1054. if (im->multiaddr == addr) {
  1055. im->users++;
  1056. ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
  1057. goto out;
  1058. }
  1059. }
  1060. im = kmalloc(sizeof(*im), GFP_KERNEL);
  1061. if (!im)
  1062. goto out;
  1063. im->users = 1;
  1064. im->interface = in_dev;
  1065. in_dev_hold(in_dev);
  1066. im->multiaddr = addr;
  1067. /* initial mode is (EX, empty) */
  1068. im->sfmode = MCAST_EXCLUDE;
  1069. im->sfcount[MCAST_INCLUDE] = 0;
  1070. im->sfcount[MCAST_EXCLUDE] = 1;
  1071. im->sources = NULL;
  1072. im->tomb = NULL;
  1073. im->crcount = 0;
  1074. atomic_set(&im->refcnt, 1);
  1075. spin_lock_init(&im->lock);
  1076. #ifdef CONFIG_IP_MULTICAST
  1077. im->tm_running = 0;
  1078. setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
  1079. im->unsolicit_count = IGMP_Unsolicited_Report_Count;
  1080. im->reporter = 0;
  1081. im->gsquery = 0;
  1082. #endif
  1083. im->loaded = 0;
  1084. write_lock_bh(&in_dev->mc_list_lock);
  1085. im->next = in_dev->mc_list;
  1086. in_dev->mc_list = im;
  1087. in_dev->mc_count++;
  1088. write_unlock_bh(&in_dev->mc_list_lock);
  1089. #ifdef CONFIG_IP_MULTICAST
  1090. igmpv3_del_delrec(in_dev, im->multiaddr);
  1091. #endif
  1092. igmp_group_added(im);
  1093. if (!in_dev->dead)
  1094. ip_rt_multicast_event(in_dev);
  1095. out:
  1096. return;
  1097. }
  1098. /*
  1099. * Resend IGMP JOIN report; used for bonding.
  1100. */
  1101. void ip_mc_rejoin_group(struct ip_mc_list *im)
  1102. {
  1103. #ifdef CONFIG_IP_MULTICAST
  1104. struct in_device *in_dev = im->interface;
  1105. if (im->multiaddr == IGMP_ALL_HOSTS)
  1106. return;
  1107. if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
  1108. igmp_mod_timer(im, IGMP_Initial_Report_Delay);
  1109. return;
  1110. }
  1111. /* else, v3 */
  1112. im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  1113. IGMP_Unsolicited_Report_Count;
  1114. igmp_ifc_event(in_dev);
  1115. #endif
  1116. }
  1117. /*
  1118. * A socket has left a multicast group on device dev
  1119. */
  1120. void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
  1121. {
  1122. struct ip_mc_list *i, **ip;
  1123. ASSERT_RTNL();
  1124. for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
  1125. if (i->multiaddr == addr) {
  1126. if (--i->users == 0) {
  1127. write_lock_bh(&in_dev->mc_list_lock);
  1128. *ip = i->next;
  1129. in_dev->mc_count--;
  1130. write_unlock_bh(&in_dev->mc_list_lock);
  1131. igmp_group_dropped(i);
  1132. if (!in_dev->dead)
  1133. ip_rt_multicast_event(in_dev);
  1134. ip_ma_put(i);
  1135. return;
  1136. }
  1137. break;
  1138. }
  1139. }
  1140. }
  1141. /* Device changing type */
  1142. void ip_mc_unmap(struct in_device *in_dev)
  1143. {
  1144. struct ip_mc_list *i;
  1145. ASSERT_RTNL();
  1146. for (i = in_dev->mc_list; i; i = i->next)
  1147. igmp_group_dropped(i);
  1148. }
  1149. void ip_mc_remap(struct in_device *in_dev)
  1150. {
  1151. struct ip_mc_list *i;
  1152. ASSERT_RTNL();
  1153. for (i = in_dev->mc_list; i; i = i->next)
  1154. igmp_group_added(i);
  1155. }
  1156. /* Device going down */
  1157. void ip_mc_down(struct in_device *in_dev)
  1158. {
  1159. struct ip_mc_list *i;
  1160. ASSERT_RTNL();
  1161. for (i=in_dev->mc_list; i; i=i->next)
  1162. igmp_group_dropped(i);
  1163. #ifdef CONFIG_IP_MULTICAST
  1164. in_dev->mr_ifc_count = 0;
  1165. if (del_timer(&in_dev->mr_ifc_timer))
  1166. __in_dev_put(in_dev);
  1167. in_dev->mr_gq_running = 0;
  1168. if (del_timer(&in_dev->mr_gq_timer))
  1169. __in_dev_put(in_dev);
  1170. igmpv3_clear_delrec(in_dev);
  1171. #endif
  1172. ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
  1173. }
  1174. void ip_mc_init_dev(struct in_device *in_dev)
  1175. {
  1176. ASSERT_RTNL();
  1177. in_dev->mc_tomb = NULL;
  1178. #ifdef CONFIG_IP_MULTICAST
  1179. in_dev->mr_gq_running = 0;
  1180. setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
  1181. (unsigned long)in_dev);
  1182. in_dev->mr_ifc_count = 0;
  1183. in_dev->mc_count = 0;
  1184. setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
  1185. (unsigned long)in_dev);
  1186. in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
  1187. #endif
  1188. rwlock_init(&in_dev->mc_list_lock);
  1189. spin_lock_init(&in_dev->mc_tomb_lock);
  1190. }
  1191. /* Device going up */
  1192. void ip_mc_up(struct in_device *in_dev)
  1193. {
  1194. struct ip_mc_list *i;
  1195. ASSERT_RTNL();
  1196. ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
  1197. for (i=in_dev->mc_list; i; i=i->next)
  1198. igmp_group_added(i);
  1199. }
  1200. /*
  1201. * Device is about to be destroyed: clean up.
  1202. */
  1203. void ip_mc_destroy_dev(struct in_device *in_dev)
  1204. {
  1205. struct ip_mc_list *i;
  1206. ASSERT_RTNL();
  1207. /* Deactivate timers */
  1208. ip_mc_down(in_dev);
  1209. write_lock_bh(&in_dev->mc_list_lock);
  1210. while ((i = in_dev->mc_list) != NULL) {
  1211. in_dev->mc_list = i->next;
  1212. in_dev->mc_count--;
  1213. write_unlock_bh(&in_dev->mc_list_lock);
  1214. igmp_group_dropped(i);
  1215. ip_ma_put(i);
  1216. write_lock_bh(&in_dev->mc_list_lock);
  1217. }
  1218. write_unlock_bh(&in_dev->mc_list_lock);
  1219. }
  1220. static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
  1221. {
  1222. struct flowi fl = { .nl_u = { .ip4_u =
  1223. { .daddr = imr->imr_multiaddr.s_addr } } };
  1224. struct rtable *rt;
  1225. struct net_device *dev = NULL;
  1226. struct in_device *idev = NULL;
  1227. if (imr->imr_ifindex) {
  1228. idev = inetdev_by_index(net, imr->imr_ifindex);
  1229. if (idev)
  1230. __in_dev_put(idev);
  1231. return idev;
  1232. }
  1233. if (imr->imr_address.s_addr) {
  1234. dev = ip_dev_find(net, imr->imr_address.s_addr);
  1235. if (!dev)
  1236. return NULL;
  1237. dev_put(dev);
  1238. }
  1239. if (!dev && !ip_route_output_key(net, &rt, &fl)) {
  1240. dev = rt->dst.dev;
  1241. ip_rt_put(rt);
  1242. }
  1243. if (dev) {
  1244. imr->imr_ifindex = dev->ifindex;
  1245. idev = __in_dev_get_rtnl(dev);
  1246. }
  1247. return idev;
  1248. }
  1249. /*
  1250. * Join a socket to a group
  1251. */
  1252. int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
  1253. int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
  1254. static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
  1255. __be32 *psfsrc)
  1256. {
  1257. struct ip_sf_list *psf, *psf_prev;
  1258. int rv = 0;
  1259. psf_prev = NULL;
  1260. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  1261. if (psf->sf_inaddr == *psfsrc)
  1262. break;
  1263. psf_prev = psf;
  1264. }
  1265. if (!psf || psf->sf_count[sfmode] == 0) {
  1266. /* source filter not found, or count wrong => bug */
  1267. return -ESRCH;
  1268. }
  1269. psf->sf_count[sfmode]--;
  1270. if (psf->sf_count[sfmode] == 0) {
  1271. ip_rt_multicast_event(pmc->interface);
  1272. }
  1273. if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
  1274. #ifdef CONFIG_IP_MULTICAST
  1275. struct in_device *in_dev = pmc->interface;
  1276. #endif
  1277. /* no more filters for this source */
  1278. if (psf_prev)
  1279. psf_prev->sf_next = psf->sf_next;
  1280. else
  1281. pmc->sources = psf->sf_next;
  1282. #ifdef CONFIG_IP_MULTICAST
  1283. if (psf->sf_oldin &&
  1284. !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
  1285. psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  1286. IGMP_Unsolicited_Report_Count;
  1287. psf->sf_next = pmc->tomb;
  1288. pmc->tomb = psf;
  1289. rv = 1;
  1290. } else
  1291. #endif
  1292. kfree(psf);
  1293. }
  1294. return rv;
  1295. }
  1296. #ifndef CONFIG_IP_MULTICAST
  1297. #define igmp_ifc_event(x) do { } while (0)
  1298. #endif
  1299. static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
  1300. int sfcount, __be32 *psfsrc, int delta)
  1301. {
  1302. struct ip_mc_list *pmc;
  1303. int changerec = 0;
  1304. int i, err;
  1305. if (!in_dev)
  1306. return -ENODEV;
  1307. read_lock(&in_dev->mc_list_lock);
  1308. for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
  1309. if (*pmca == pmc->multiaddr)
  1310. break;
  1311. }
  1312. if (!pmc) {
  1313. /* MCA not found?? bug */
  1314. read_unlock(&in_dev->mc_list_lock);
  1315. return -ESRCH;
  1316. }
  1317. spin_lock_bh(&pmc->lock);
  1318. read_unlock(&in_dev->mc_list_lock);
  1319. #ifdef CONFIG_IP_MULTICAST
  1320. sf_markstate(pmc);
  1321. #endif
  1322. if (!delta) {
  1323. err = -EINVAL;
  1324. if (!pmc->sfcount[sfmode])
  1325. goto out_unlock;
  1326. pmc->sfcount[sfmode]--;
  1327. }
  1328. err = 0;
  1329. for (i=0; i<sfcount; i++) {
  1330. int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1331. changerec |= rv > 0;
  1332. if (!err && rv < 0)
  1333. err = rv;
  1334. }
  1335. if (pmc->sfmode == MCAST_EXCLUDE &&
  1336. pmc->sfcount[MCAST_EXCLUDE] == 0 &&
  1337. pmc->sfcount[MCAST_INCLUDE]) {
  1338. #ifdef CONFIG_IP_MULTICAST
  1339. struct ip_sf_list *psf;
  1340. #endif
  1341. /* filter mode change */
  1342. pmc->sfmode = MCAST_INCLUDE;
  1343. #ifdef CONFIG_IP_MULTICAST
  1344. pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  1345. IGMP_Unsolicited_Report_Count;
  1346. in_dev->mr_ifc_count = pmc->crcount;
  1347. for (psf=pmc->sources; psf; psf = psf->sf_next)
  1348. psf->sf_crcount = 0;
  1349. igmp_ifc_event(pmc->interface);
  1350. } else if (sf_setstate(pmc) || changerec) {
  1351. igmp_ifc_event(pmc->interface);
  1352. #endif
  1353. }
  1354. out_unlock:
  1355. spin_unlock_bh(&pmc->lock);
  1356. return err;
  1357. }
  1358. /*
  1359. * Add multicast single-source filter to the interface list
  1360. */
  1361. static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
  1362. __be32 *psfsrc, int delta)
  1363. {
  1364. struct ip_sf_list *psf, *psf_prev;
  1365. psf_prev = NULL;
  1366. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  1367. if (psf->sf_inaddr == *psfsrc)
  1368. break;
  1369. psf_prev = psf;
  1370. }
  1371. if (!psf) {
  1372. psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
  1373. if (!psf)
  1374. return -ENOBUFS;
  1375. psf->sf_inaddr = *psfsrc;
  1376. if (psf_prev) {
  1377. psf_prev->sf_next = psf;
  1378. } else
  1379. pmc->sources = psf;
  1380. }
  1381. psf->sf_count[sfmode]++;
  1382. if (psf->sf_count[sfmode] == 1) {
  1383. ip_rt_multicast_event(pmc->interface);
  1384. }
  1385. return 0;
  1386. }
  1387. #ifdef CONFIG_IP_MULTICAST
  1388. static void sf_markstate(struct ip_mc_list *pmc)
  1389. {
  1390. struct ip_sf_list *psf;
  1391. int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
  1392. for (psf=pmc->sources; psf; psf=psf->sf_next)
  1393. if (pmc->sfcount[MCAST_EXCLUDE]) {
  1394. psf->sf_oldin = mca_xcount ==
  1395. psf->sf_count[MCAST_EXCLUDE] &&
  1396. !psf->sf_count[MCAST_INCLUDE];
  1397. } else
  1398. psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
  1399. }
  1400. static int sf_setstate(struct ip_mc_list *pmc)
  1401. {
  1402. struct ip_sf_list *psf, *dpsf;
  1403. int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
  1404. int qrv = pmc->interface->mr_qrv;
  1405. int new_in, rv;
  1406. rv = 0;
  1407. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  1408. if (pmc->sfcount[MCAST_EXCLUDE]) {
  1409. new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
  1410. !psf->sf_count[MCAST_INCLUDE];
  1411. } else
  1412. new_in = psf->sf_count[MCAST_INCLUDE] != 0;
  1413. if (new_in) {
  1414. if (!psf->sf_oldin) {
  1415. struct ip_sf_list *prev = NULL;
  1416. for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
  1417. if (dpsf->sf_inaddr == psf->sf_inaddr)
  1418. break;
  1419. prev = dpsf;
  1420. }
  1421. if (dpsf) {
  1422. if (prev)
  1423. prev->sf_next = dpsf->sf_next;
  1424. else
  1425. pmc->tomb = dpsf->sf_next;
  1426. kfree(dpsf);
  1427. }
  1428. psf->sf_crcount = qrv;
  1429. rv++;
  1430. }
  1431. } else if (psf->sf_oldin) {
  1432. psf->sf_crcount = 0;
  1433. /*
  1434. * add or update "delete" records if an active filter
  1435. * is now inactive
  1436. */
  1437. for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
  1438. if (dpsf->sf_inaddr == psf->sf_inaddr)
  1439. break;
  1440. if (!dpsf) {
  1441. dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
  1442. if (!dpsf)
  1443. continue;
  1444. *dpsf = *psf;
  1445. /* pmc->lock held by callers */
  1446. dpsf->sf_next = pmc->tomb;
  1447. pmc->tomb = dpsf;
  1448. }
  1449. dpsf->sf_crcount = qrv;
  1450. rv++;
  1451. }
  1452. }
  1453. return rv;
  1454. }
  1455. #endif
  1456. /*
  1457. * Add multicast source filter list to the interface list
  1458. */
  1459. static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
  1460. int sfcount, __be32 *psfsrc, int delta)
  1461. {
  1462. struct ip_mc_list *pmc;
  1463. int isexclude;
  1464. int i, err;
  1465. if (!in_dev)
  1466. return -ENODEV;
  1467. read_lock(&in_dev->mc_list_lock);
  1468. for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
  1469. if (*pmca == pmc->multiaddr)
  1470. break;
  1471. }
  1472. if (!pmc) {
  1473. /* MCA not found?? bug */
  1474. read_unlock(&in_dev->mc_list_lock);
  1475. return -ESRCH;
  1476. }
  1477. spin_lock_bh(&pmc->lock);
  1478. read_unlock(&in_dev->mc_list_lock);
  1479. #ifdef CONFIG_IP_MULTICAST
  1480. sf_markstate(pmc);
  1481. #endif
  1482. isexclude = pmc->sfmode == MCAST_EXCLUDE;
  1483. if (!delta)
  1484. pmc->sfcount[sfmode]++;
  1485. err = 0;
  1486. for (i=0; i<sfcount; i++) {
  1487. err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
  1488. if (err)
  1489. break;
  1490. }
  1491. if (err) {
  1492. int j;
  1493. pmc->sfcount[sfmode]--;
  1494. for (j=0; j<i; j++)
  1495. (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1496. } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
  1497. #ifdef CONFIG_IP_MULTICAST
  1498. struct ip_sf_list *psf;
  1499. in_dev = pmc->interface;
  1500. #endif
  1501. /* filter mode change */
  1502. if (pmc->sfcount[MCAST_EXCLUDE])
  1503. pmc->sfmode = MCAST_EXCLUDE;
  1504. else if (pmc->sfcount[MCAST_INCLUDE])
  1505. pmc->sfmode = MCAST_INCLUDE;
  1506. #ifdef CONFIG_IP_MULTICAST
  1507. /* else no filters; keep old mode for reports */
  1508. pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  1509. IGMP_Unsolicited_Report_Count;
  1510. in_dev->mr_ifc_count = pmc->crcount;
  1511. for (psf=pmc->sources; psf; psf = psf->sf_next)
  1512. psf->sf_crcount = 0;
  1513. igmp_ifc_event(in_dev);
  1514. } else if (sf_setstate(pmc)) {
  1515. igmp_ifc_event(in_dev);
  1516. #endif
  1517. }
  1518. spin_unlock_bh(&pmc->lock);
  1519. return err;
  1520. }
  1521. static void ip_mc_clear_src(struct ip_mc_list *pmc)
  1522. {
  1523. struct ip_sf_list *psf, *nextpsf;
  1524. for (psf=pmc->tomb; psf; psf=nextpsf) {
  1525. nextpsf = psf->sf_next;
  1526. kfree(psf);
  1527. }
  1528. pmc->tomb = NULL;
  1529. for (psf=pmc->sources; psf; psf=nextpsf) {
  1530. nextpsf = psf->sf_next;
  1531. kfree(psf);
  1532. }
  1533. pmc->sources = NULL;
  1534. pmc->sfmode = MCAST_EXCLUDE;
  1535. pmc->sfcount[MCAST_INCLUDE] = 0;
  1536. pmc->sfcount[MCAST_EXCLUDE] = 1;
  1537. }
  1538. /*
  1539. * Join a multicast group
  1540. */
  1541. int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
  1542. {
  1543. int err;
  1544. __be32 addr = imr->imr_multiaddr.s_addr;
  1545. struct ip_mc_socklist *iml = NULL, *i;
  1546. struct in_device *in_dev;
  1547. struct inet_sock *inet = inet_sk(sk);
  1548. struct net *net = sock_net(sk);
  1549. int ifindex;
  1550. int count = 0;
  1551. if (!ipv4_is_multicast(addr))
  1552. return -EINVAL;
  1553. rtnl_lock();
  1554. in_dev = ip_mc_find_dev(net, imr);
  1555. if (!in_dev) {
  1556. iml = NULL;
  1557. err = -ENODEV;
  1558. goto done;
  1559. }
  1560. err = -EADDRINUSE;
  1561. ifindex = imr->imr_ifindex;
  1562. for (i = inet->mc_list; i; i = i->next) {
  1563. if (i->multi.imr_multiaddr.s_addr == addr &&
  1564. i->multi.imr_ifindex == ifindex)
  1565. goto done;
  1566. count++;
  1567. }
  1568. err = -ENOBUFS;
  1569. if (count >= sysctl_igmp_max_memberships)
  1570. goto done;
  1571. iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
  1572. if (iml == NULL)
  1573. goto done;
  1574. memcpy(&iml->multi, imr, sizeof(*imr));
  1575. iml->next = inet->mc_list;
  1576. iml->sflist = NULL;
  1577. iml->sfmode = MCAST_EXCLUDE;
  1578. rcu_assign_pointer(inet->mc_list, iml);
  1579. ip_mc_inc_group(in_dev, addr);
  1580. err = 0;
  1581. done:
  1582. rtnl_unlock();
  1583. return err;
  1584. }
  1585. static void ip_sf_socklist_reclaim(struct rcu_head *rp)
  1586. {
  1587. struct ip_sf_socklist *psf;
  1588. psf = container_of(rp, struct ip_sf_socklist, rcu);
  1589. /* sk_omem_alloc should have been decreased by the caller*/
  1590. kfree(psf);
  1591. }
  1592. static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
  1593. struct in_device *in_dev)
  1594. {
  1595. struct ip_sf_socklist *psf = iml->sflist;
  1596. int err;
  1597. if (psf == NULL) {
  1598. /* any-source empty exclude case */
  1599. return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
  1600. iml->sfmode, 0, NULL, 0);
  1601. }
  1602. err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
  1603. iml->sfmode, psf->sl_count, psf->sl_addr, 0);
  1604. rcu_assign_pointer(iml->sflist, NULL);
  1605. /* decrease mem now to avoid the memleak warning */
  1606. atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
  1607. call_rcu(&psf->rcu, ip_sf_socklist_reclaim);
  1608. return err;
  1609. }
  1610. static void ip_mc_socklist_reclaim(struct rcu_head *rp)
  1611. {
  1612. struct ip_mc_socklist *iml;
  1613. iml = container_of(rp, struct ip_mc_socklist, rcu);
  1614. /* sk_omem_alloc should have been decreased by the caller*/
  1615. kfree(iml);
  1616. }
  1617. /*
  1618. * Ask a socket to leave a group.
  1619. */
  1620. int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
  1621. {
  1622. struct inet_sock *inet = inet_sk(sk);
  1623. struct ip_mc_socklist *iml, **imlp;
  1624. struct in_device *in_dev;
  1625. struct net *net = sock_net(sk);
  1626. __be32 group = imr->imr_multiaddr.s_addr;
  1627. u32 ifindex;
  1628. int ret = -EADDRNOTAVAIL;
  1629. rtnl_lock();
  1630. in_dev = ip_mc_find_dev(net, imr);
  1631. ifindex = imr->imr_ifindex;
  1632. for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
  1633. if (iml->multi.imr_multiaddr.s_addr != group)
  1634. continue;
  1635. if (ifindex) {
  1636. if (iml->multi.imr_ifindex != ifindex)
  1637. continue;
  1638. } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
  1639. iml->multi.imr_address.s_addr)
  1640. continue;
  1641. (void) ip_mc_leave_src(sk, iml, in_dev);
  1642. rcu_assign_pointer(*imlp, iml->next);
  1643. if (in_dev)
  1644. ip_mc_dec_group(in_dev, group);
  1645. rtnl_unlock();
  1646. /* decrease mem now to avoid the memleak warning */
  1647. atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
  1648. call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
  1649. return 0;
  1650. }
  1651. if (!in_dev)
  1652. ret = -ENODEV;
  1653. rtnl_unlock();
  1654. return ret;
  1655. }
  1656. int ip_mc_source(int add, int omode, struct sock *sk, struct
  1657. ip_mreq_source *mreqs, int ifindex)
  1658. {
  1659. int err;
  1660. struct ip_mreqn imr;
  1661. __be32 addr = mreqs->imr_multiaddr;
  1662. struct ip_mc_socklist *pmc;
  1663. struct in_device *in_dev = NULL;
  1664. struct inet_sock *inet = inet_sk(sk);
  1665. struct ip_sf_socklist *psl;
  1666. struct net *net = sock_net(sk);
  1667. int leavegroup = 0;
  1668. int i, j, rv;
  1669. if (!ipv4_is_multicast(addr))
  1670. return -EINVAL;
  1671. rtnl_lock();
  1672. imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
  1673. imr.imr_address.s_addr = mreqs->imr_interface;
  1674. imr.imr_ifindex = ifindex;
  1675. in_dev = ip_mc_find_dev(net, &imr);
  1676. if (!in_dev) {
  1677. err = -ENODEV;
  1678. goto done;
  1679. }
  1680. err = -EADDRNOTAVAIL;
  1681. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1682. if ((pmc->multi.imr_multiaddr.s_addr ==
  1683. imr.imr_multiaddr.s_addr) &&
  1684. (pmc->multi.imr_ifindex == imr.imr_ifindex))
  1685. break;
  1686. }
  1687. if (!pmc) { /* must have a prior join */
  1688. err = -EINVAL;
  1689. goto done;
  1690. }
  1691. /* if a source filter was set, must be the same mode as before */
  1692. if (pmc->sflist) {
  1693. if (pmc->sfmode != omode) {
  1694. err = -EINVAL;
  1695. goto done;
  1696. }
  1697. } else if (pmc->sfmode != omode) {
  1698. /* allow mode switches for empty-set filters */
  1699. ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
  1700. ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
  1701. NULL, 0);
  1702. pmc->sfmode = omode;
  1703. }
  1704. psl = pmc->sflist;
  1705. if (!add) {
  1706. if (!psl)
  1707. goto done; /* err = -EADDRNOTAVAIL */
  1708. rv = !0;
  1709. for (i=0; i<psl->sl_count; i++) {
  1710. rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
  1711. sizeof(__be32));
  1712. if (rv == 0)
  1713. break;
  1714. }
  1715. if (rv) /* source not found */
  1716. goto done; /* err = -EADDRNOTAVAIL */
  1717. /* special case - (INCLUDE, empty) == LEAVE_GROUP */
  1718. if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
  1719. leavegroup = 1;
  1720. goto done;
  1721. }
  1722. /* update the interface filter */
  1723. ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
  1724. &mreqs->imr_sourceaddr, 1);
  1725. for (j=i+1; j<psl->sl_count; j++)
  1726. psl->sl_addr[j-1] = psl->sl_addr[j];
  1727. psl->sl_count--;
  1728. err = 0;
  1729. goto done;
  1730. }
  1731. /* else, add a new source to the filter */
  1732. if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
  1733. err = -ENOBUFS;
  1734. goto done;
  1735. }
  1736. if (!psl || psl->sl_count == psl->sl_max) {
  1737. struct ip_sf_socklist *newpsl;
  1738. int count = IP_SFBLOCK;
  1739. if (psl)
  1740. count += psl->sl_max;
  1741. newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
  1742. if (!newpsl) {
  1743. err = -ENOBUFS;
  1744. goto done;
  1745. }
  1746. newpsl->sl_max = count;
  1747. newpsl->sl_count = count - IP_SFBLOCK;
  1748. if (psl) {
  1749. for (i=0; i<psl->sl_count; i++)
  1750. newpsl->sl_addr[i] = psl->sl_addr[i];
  1751. /* decrease mem now to avoid the memleak warning */
  1752. atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
  1753. call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
  1754. }
  1755. rcu_assign_pointer(pmc->sflist, newpsl);
  1756. psl = newpsl;
  1757. }
  1758. rv = 1; /* > 0 for insert logic below if sl_count is 0 */
  1759. for (i=0; i<psl->sl_count; i++) {
  1760. rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
  1761. sizeof(__be32));
  1762. if (rv == 0)
  1763. break;
  1764. }
  1765. if (rv == 0) /* address already there is an error */
  1766. goto done;
  1767. for (j=psl->sl_count-1; j>=i; j--)
  1768. psl->sl_addr[j+1] = psl->sl_addr[j];
  1769. psl->sl_addr[i] = mreqs->imr_sourceaddr;
  1770. psl->sl_count++;
  1771. err = 0;
  1772. /* update the interface list */
  1773. ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
  1774. &mreqs->imr_sourceaddr, 1);
  1775. done:
  1776. rtnl_unlock();
  1777. if (leavegroup)
  1778. return ip_mc_leave_group(sk, &imr);
  1779. return err;
  1780. }
  1781. int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
  1782. {
  1783. int err = 0;
  1784. struct ip_mreqn imr;
  1785. __be32 addr = msf->imsf_multiaddr;
  1786. struct ip_mc_socklist *pmc;
  1787. struct in_device *in_dev;
  1788. struct inet_sock *inet = inet_sk(sk);
  1789. struct ip_sf_socklist *newpsl, *psl;
  1790. struct net *net = sock_net(sk);
  1791. int leavegroup = 0;
  1792. if (!ipv4_is_multicast(addr))
  1793. return -EINVAL;
  1794. if (msf->imsf_fmode != MCAST_INCLUDE &&
  1795. msf->imsf_fmode != MCAST_EXCLUDE)
  1796. return -EINVAL;
  1797. rtnl_lock();
  1798. imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
  1799. imr.imr_address.s_addr = msf->imsf_interface;
  1800. imr.imr_ifindex = ifindex;
  1801. in_dev = ip_mc_find_dev(net, &imr);
  1802. if (!in_dev) {
  1803. err = -ENODEV;
  1804. goto done;
  1805. }
  1806. /* special case - (INCLUDE, empty) == LEAVE_GROUP */
  1807. if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
  1808. leavegroup = 1;
  1809. goto done;
  1810. }
  1811. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1812. if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
  1813. pmc->multi.imr_ifindex == imr.imr_ifindex)
  1814. break;
  1815. }
  1816. if (!pmc) { /* must have a prior join */
  1817. err = -EINVAL;
  1818. goto done;
  1819. }
  1820. if (msf->imsf_numsrc) {
  1821. newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
  1822. GFP_KERNEL);
  1823. if (!newpsl) {
  1824. err = -ENOBUFS;
  1825. goto done;
  1826. }
  1827. newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
  1828. memcpy(newpsl->sl_addr, msf->imsf_slist,
  1829. msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
  1830. err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
  1831. msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
  1832. if (err) {
  1833. sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
  1834. goto done;
  1835. }
  1836. } else {
  1837. newpsl = NULL;
  1838. (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
  1839. msf->imsf_fmode, 0, NULL, 0);
  1840. }
  1841. psl = pmc->sflist;
  1842. if (psl) {
  1843. (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
  1844. psl->sl_count, psl->sl_addr, 0);
  1845. /* decrease mem now to avoid the memleak warning */
  1846. atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
  1847. call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
  1848. } else
  1849. (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
  1850. 0, NULL, 0);
  1851. rcu_assign_pointer(pmc->sflist, newpsl);
  1852. pmc->sfmode = msf->imsf_fmode;
  1853. err = 0;
  1854. done:
  1855. rtnl_unlock();
  1856. if (leavegroup)
  1857. err = ip_mc_leave_group(sk, &imr);
  1858. return err;
  1859. }
  1860. int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
  1861. struct ip_msfilter __user *optval, int __user *optlen)
  1862. {
  1863. int err, len, count, copycount;
  1864. struct ip_mreqn imr;
  1865. __be32 addr = msf->imsf_multiaddr;
  1866. struct ip_mc_socklist *pmc;
  1867. struct in_device *in_dev;
  1868. struct inet_sock *inet = inet_sk(sk);
  1869. struct ip_sf_socklist *psl;
  1870. struct net *net = sock_net(sk);
  1871. if (!ipv4_is_multicast(addr))
  1872. return -EINVAL;
  1873. rtnl_lock();
  1874. imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
  1875. imr.imr_address.s_addr = msf->imsf_interface;
  1876. imr.imr_ifindex = 0;
  1877. in_dev = ip_mc_find_dev(net, &imr);
  1878. if (!in_dev) {
  1879. err = -ENODEV;
  1880. goto done;
  1881. }
  1882. err = -EADDRNOTAVAIL;
  1883. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1884. if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
  1885. pmc->multi.imr_ifindex == imr.imr_ifindex)
  1886. break;
  1887. }
  1888. if (!pmc) /* must have a prior join */
  1889. goto done;
  1890. msf->imsf_fmode = pmc->sfmode;
  1891. psl = pmc->sflist;
  1892. rtnl_unlock();
  1893. if (!psl) {
  1894. len = 0;
  1895. count = 0;
  1896. } else {
  1897. count = psl->sl_count;
  1898. }
  1899. copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
  1900. len = copycount * sizeof(psl->sl_addr[0]);
  1901. msf->imsf_numsrc = count;
  1902. if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
  1903. copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
  1904. return -EFAULT;
  1905. }
  1906. if (len &&
  1907. copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
  1908. return -EFAULT;
  1909. return 0;
  1910. done:
  1911. rtnl_unlock();
  1912. return err;
  1913. }
  1914. int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
  1915. struct group_filter __user *optval, int __user *optlen)
  1916. {
  1917. int err, i, count, copycount;
  1918. struct sockaddr_in *psin;
  1919. __be32 addr;
  1920. struct ip_mc_socklist *pmc;
  1921. struct inet_sock *inet = inet_sk(sk);
  1922. struct ip_sf_socklist *psl;
  1923. psin = (struct sockaddr_in *)&gsf->gf_group;
  1924. if (psin->sin_family != AF_INET)
  1925. return -EINVAL;
  1926. addr = psin->sin_addr.s_addr;
  1927. if (!ipv4_is_multicast(addr))
  1928. return -EINVAL;
  1929. rtnl_lock();
  1930. err = -EADDRNOTAVAIL;
  1931. for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
  1932. if (pmc->multi.imr_multiaddr.s_addr == addr &&
  1933. pmc->multi.imr_ifindex == gsf->gf_interface)
  1934. break;
  1935. }
  1936. if (!pmc) /* must have a prior join */
  1937. goto done;
  1938. gsf->gf_fmode = pmc->sfmode;
  1939. psl = pmc->sflist;
  1940. rtnl_unlock();
  1941. count = psl ? psl->sl_count : 0;
  1942. copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
  1943. gsf->gf_numsrc = count;
  1944. if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
  1945. copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
  1946. return -EFAULT;
  1947. }
  1948. for (i=0; i<copycount; i++) {
  1949. struct sockaddr_storage ss;
  1950. psin = (struct sockaddr_in *)&ss;
  1951. memset(&ss, 0, sizeof(ss));
  1952. psin->sin_family = AF_INET;
  1953. psin->sin_addr.s_addr = psl->sl_addr[i];
  1954. if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
  1955. return -EFAULT;
  1956. }
  1957. return 0;
  1958. done:
  1959. rtnl_unlock();
  1960. return err;
  1961. }
  1962. /*
  1963. * check if a multicast source filter allows delivery for a given <src,dst,intf>
  1964. */
  1965. int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
  1966. {
  1967. struct inet_sock *inet = inet_sk(sk);
  1968. struct ip_mc_socklist *pmc;
  1969. struct ip_sf_socklist *psl;
  1970. int i;
  1971. int ret;
  1972. ret = 1;
  1973. if (!ipv4_is_multicast(loc_addr))
  1974. goto out;
  1975. rcu_read_lock();
  1976. for (pmc=rcu_dereference(inet->mc_list); pmc; pmc=rcu_dereference(pmc->next)) {
  1977. if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
  1978. pmc->multi.imr_ifindex == dif)
  1979. break;
  1980. }
  1981. ret = inet->mc_all;
  1982. if (!pmc)
  1983. goto unlock;
  1984. psl = pmc->sflist;
  1985. ret = (pmc->sfmode == MCAST_EXCLUDE);
  1986. if (!psl)
  1987. goto unlock;
  1988. for (i=0; i<psl->sl_count; i++) {
  1989. if (psl->sl_addr[i] == rmt_addr)
  1990. break;
  1991. }
  1992. ret = 0;
  1993. if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
  1994. goto unlock;
  1995. if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
  1996. goto unlock;
  1997. ret = 1;
  1998. unlock:
  1999. rcu_read_unlock();
  2000. out:
  2001. return ret;
  2002. }
  2003. /*
  2004. * A socket is closing.
  2005. */
  2006. void ip_mc_drop_socket(struct sock *sk)
  2007. {
  2008. struct inet_sock *inet = inet_sk(sk);
  2009. struct ip_mc_socklist *iml;
  2010. struct net *net = sock_net(sk);
  2011. if (inet->mc_list == NULL)
  2012. return;
  2013. rtnl_lock();
  2014. while ((iml = inet->mc_list) != NULL) {
  2015. struct in_device *in_dev;
  2016. rcu_assign_pointer(inet->mc_list, iml->next);
  2017. in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
  2018. (void) ip_mc_leave_src(sk, iml, in_dev);
  2019. if (in_dev != NULL) {
  2020. ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
  2021. in_dev_put(in_dev);
  2022. }
  2023. /* decrease mem now to avoid the memleak warning */
  2024. atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
  2025. call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
  2026. }
  2027. rtnl_unlock();
  2028. }
  2029. int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
  2030. {
  2031. struct ip_mc_list *im;
  2032. struct ip_sf_list *psf;
  2033. int rv = 0;
  2034. read_lock(&in_dev->mc_list_lock);
  2035. for (im=in_dev->mc_list; im; im=im->next) {
  2036. if (im->multiaddr == mc_addr)
  2037. break;
  2038. }
  2039. if (im && proto == IPPROTO_IGMP) {
  2040. rv = 1;
  2041. } else if (im) {
  2042. if (src_addr) {
  2043. for (psf=im->sources; psf; psf=psf->sf_next) {
  2044. if (psf->sf_inaddr == src_addr)
  2045. break;
  2046. }
  2047. if (psf)
  2048. rv = psf->sf_count[MCAST_INCLUDE] ||
  2049. psf->sf_count[MCAST_EXCLUDE] !=
  2050. im->sfcount[MCAST_EXCLUDE];
  2051. else
  2052. rv = im->sfcount[MCAST_EXCLUDE] != 0;
  2053. } else
  2054. rv = 1; /* unspecified source; tentatively allow */
  2055. }
  2056. read_unlock(&in_dev->mc_list_lock);
  2057. return rv;
  2058. }
  2059. #if defined(CONFIG_PROC_FS)
  2060. struct igmp_mc_iter_state {
  2061. struct seq_net_private p;
  2062. struct net_device *dev;
  2063. struct in_device *in_dev;
  2064. };
  2065. #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
  2066. static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
  2067. {
  2068. struct net *net = seq_file_net(seq);
  2069. struct ip_mc_list *im = NULL;
  2070. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2071. state->in_dev = NULL;
  2072. for_each_netdev_rcu(net, state->dev) {
  2073. struct in_device *in_dev;
  2074. in_dev = __in_dev_get_rcu(state->dev);
  2075. if (!in_dev)
  2076. continue;
  2077. read_lock(&in_dev->mc_list_lock);
  2078. im = in_dev->mc_list;
  2079. if (im) {
  2080. state->in_dev = in_dev;
  2081. break;
  2082. }
  2083. read_unlock(&in_dev->mc_list_lock);
  2084. }
  2085. return im;
  2086. }
  2087. static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
  2088. {
  2089. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2090. im = im->next;
  2091. while (!im) {
  2092. if (likely(state->in_dev != NULL))
  2093. read_unlock(&state->in_dev->mc_list_lock);
  2094. state->dev = next_net_device_rcu(state->dev);
  2095. if (!state->dev) {
  2096. state->in_dev = NULL;
  2097. break;
  2098. }
  2099. state->in_dev = __in_dev_get_rcu(state->dev);
  2100. if (!state->in_dev)
  2101. continue;
  2102. read_lock(&state->in_dev->mc_list_lock);
  2103. im = state->in_dev->mc_list;
  2104. }
  2105. return im;
  2106. }
  2107. static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
  2108. {
  2109. struct ip_mc_list *im = igmp_mc_get_first(seq);
  2110. if (im)
  2111. while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
  2112. --pos;
  2113. return pos ? NULL : im;
  2114. }
  2115. static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
  2116. __acquires(rcu)
  2117. {
  2118. rcu_read_lock();
  2119. return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2120. }
  2121. static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2122. {
  2123. struct ip_mc_list *im;
  2124. if (v == SEQ_START_TOKEN)
  2125. im = igmp_mc_get_first(seq);
  2126. else
  2127. im = igmp_mc_get_next(seq, v);
  2128. ++*pos;
  2129. return im;
  2130. }
  2131. static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
  2132. __releases(rcu)
  2133. {
  2134. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2135. if (likely(state->in_dev != NULL)) {
  2136. read_unlock(&state->in_dev->mc_list_lock);
  2137. state->in_dev = NULL;
  2138. }
  2139. state->dev = NULL;
  2140. rcu_read_unlock();
  2141. }
  2142. static int igmp_mc_seq_show(struct seq_file *seq, void *v)
  2143. {
  2144. if (v == SEQ_START_TOKEN)
  2145. seq_puts(seq,
  2146. "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
  2147. else {
  2148. struct ip_mc_list *im = (struct ip_mc_list *)v;
  2149. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2150. char *querier;
  2151. #ifdef CONFIG_IP_MULTICAST
  2152. querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
  2153. IGMP_V2_SEEN(state->in_dev) ? "V2" :
  2154. "V3";
  2155. #else
  2156. querier = "NONE";
  2157. #endif
  2158. if (state->in_dev->mc_list == im) {
  2159. seq_printf(seq, "%d\t%-10s: %5d %7s\n",
  2160. state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
  2161. }
  2162. seq_printf(seq,
  2163. "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
  2164. im->multiaddr, im->users,
  2165. im->tm_running, im->tm_running ?
  2166. jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
  2167. im->reporter);
  2168. }
  2169. return 0;
  2170. }
  2171. static const struct seq_operations igmp_mc_seq_ops = {
  2172. .start = igmp_mc_seq_start,
  2173. .next = igmp_mc_seq_next,
  2174. .stop = igmp_mc_seq_stop,
  2175. .show = igmp_mc_seq_show,
  2176. };
  2177. static int igmp_mc_seq_open(struct inode *inode, struct file *file)
  2178. {
  2179. return seq_open_net(inode, file, &igmp_mc_seq_ops,
  2180. sizeof(struct igmp_mc_iter_state));
  2181. }
  2182. static const struct file_operations igmp_mc_seq_fops = {
  2183. .owner = THIS_MODULE,
  2184. .open = igmp_mc_seq_open,
  2185. .read = seq_read,
  2186. .llseek = seq_lseek,
  2187. .release = seq_release_net,
  2188. };
  2189. struct igmp_mcf_iter_state {
  2190. struct seq_net_private p;
  2191. struct net_device *dev;
  2192. struct in_device *idev;
  2193. struct ip_mc_list *im;
  2194. };
  2195. #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
  2196. static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
  2197. {
  2198. struct net *net = seq_file_net(seq);
  2199. struct ip_sf_list *psf = NULL;
  2200. struct ip_mc_list *im = NULL;
  2201. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2202. state->idev = NULL;
  2203. state->im = NULL;
  2204. for_each_netdev_rcu(net, state->dev) {
  2205. struct in_device *idev;
  2206. idev = __in_dev_get_rcu(state->dev);
  2207. if (unlikely(idev == NULL))
  2208. continue;
  2209. read_lock(&idev->mc_list_lock);
  2210. im = idev->mc_list;
  2211. if (likely(im != NULL)) {
  2212. spin_lock_bh(&im->lock);
  2213. psf = im->sources;
  2214. if (likely(psf != NULL)) {
  2215. state->im = im;
  2216. state->idev = idev;
  2217. break;
  2218. }
  2219. spin_unlock_bh(&im->lock);
  2220. }
  2221. read_unlock(&idev->mc_list_lock);
  2222. }
  2223. return psf;
  2224. }
  2225. static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
  2226. {
  2227. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2228. psf = psf->sf_next;
  2229. while (!psf) {
  2230. spin_unlock_bh(&state->im->lock);
  2231. state->im = state->im->next;
  2232. while (!state->im) {
  2233. if (likely(state->idev != NULL))
  2234. read_unlock(&state->idev->mc_list_lock);
  2235. state->dev = next_net_device_rcu(state->dev);
  2236. if (!state->dev) {
  2237. state->idev = NULL;
  2238. goto out;
  2239. }
  2240. state->idev = __in_dev_get_rcu(state->dev);
  2241. if (!state->idev)
  2242. continue;
  2243. read_lock(&state->idev->mc_list_lock);
  2244. state->im = state->idev->mc_list;
  2245. }
  2246. if (!state->im)
  2247. break;
  2248. spin_lock_bh(&state->im->lock);
  2249. psf = state->im->sources;
  2250. }
  2251. out:
  2252. return psf;
  2253. }
  2254. static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
  2255. {
  2256. struct ip_sf_list *psf = igmp_mcf_get_first(seq);
  2257. if (psf)
  2258. while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
  2259. --pos;
  2260. return pos ? NULL : psf;
  2261. }
  2262. static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
  2263. __acquires(rcu)
  2264. {
  2265. rcu_read_lock();
  2266. return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2267. }
  2268. static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2269. {
  2270. struct ip_sf_list *psf;
  2271. if (v == SEQ_START_TOKEN)
  2272. psf = igmp_mcf_get_first(seq);
  2273. else
  2274. psf = igmp_mcf_get_next(seq, v);
  2275. ++*pos;
  2276. return psf;
  2277. }
  2278. static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
  2279. __releases(rcu)
  2280. {
  2281. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2282. if (likely(state->im != NULL)) {
  2283. spin_unlock_bh(&state->im->lock);
  2284. state->im = NULL;
  2285. }
  2286. if (likely(state->idev != NULL)) {
  2287. read_unlock(&state->idev->mc_list_lock);
  2288. state->idev = NULL;
  2289. }
  2290. state->dev = NULL;
  2291. rcu_read_unlock();
  2292. }
  2293. static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
  2294. {
  2295. struct ip_sf_list *psf = (struct ip_sf_list *)v;
  2296. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2297. if (v == SEQ_START_TOKEN) {
  2298. seq_printf(seq,
  2299. "%3s %6s "
  2300. "%10s %10s %6s %6s\n", "Idx",
  2301. "Device", "MCA",
  2302. "SRC", "INC", "EXC");
  2303. } else {
  2304. seq_printf(seq,
  2305. "%3d %6.6s 0x%08x "
  2306. "0x%08x %6lu %6lu\n",
  2307. state->dev->ifindex, state->dev->name,
  2308. ntohl(state->im->multiaddr),
  2309. ntohl(psf->sf_inaddr),
  2310. psf->sf_count[MCAST_INCLUDE],
  2311. psf->sf_count[MCAST_EXCLUDE]);
  2312. }
  2313. return 0;
  2314. }
  2315. static const struct seq_operations igmp_mcf_seq_ops = {
  2316. .start = igmp_mcf_seq_start,
  2317. .next = igmp_mcf_seq_next,
  2318. .stop = igmp_mcf_seq_stop,
  2319. .show = igmp_mcf_seq_show,
  2320. };
  2321. static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
  2322. {
  2323. return seq_open_net(inode, file, &igmp_mcf_seq_ops,
  2324. sizeof(struct igmp_mcf_iter_state));
  2325. }
  2326. static const struct file_operations igmp_mcf_seq_fops = {
  2327. .owner = THIS_MODULE,
  2328. .open = igmp_mcf_seq_open,
  2329. .read = seq_read,
  2330. .llseek = seq_lseek,
  2331. .release = seq_release_net,
  2332. };
  2333. static int __net_init igmp_net_init(struct net *net)
  2334. {
  2335. struct proc_dir_entry *pde;
  2336. pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
  2337. if (!pde)
  2338. goto out_igmp;
  2339. pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
  2340. if (!pde)
  2341. goto out_mcfilter;
  2342. return 0;
  2343. out_mcfilter:
  2344. proc_net_remove(net, "igmp");
  2345. out_igmp:
  2346. return -ENOMEM;
  2347. }
  2348. static void __net_exit igmp_net_exit(struct net *net)
  2349. {
  2350. proc_net_remove(net, "mcfilter");
  2351. proc_net_remove(net, "igmp");
  2352. }
  2353. static struct pernet_operations igmp_net_ops = {
  2354. .init = igmp_net_init,
  2355. .exit = igmp_net_exit,
  2356. };
  2357. int __init igmp_mc_proc_init(void)
  2358. {
  2359. return register_pernet_subsys(&igmp_net_ops);
  2360. }
  2361. #endif
  2362. EXPORT_SYMBOL(ip_mc_dec_group);
  2363. EXPORT_SYMBOL(ip_mc_inc_group);
  2364. EXPORT_SYMBOL(ip_mc_join_group);
  2365. EXPORT_SYMBOL(ip_mc_rejoin_group);