igmp.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662
  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_rcu(im, rcu);
  146. }
  147. }
  148. #define for_each_pmc_rcu(in_dev, pmc) \
  149. for (pmc = rcu_dereference(in_dev->mc_list); \
  150. pmc != NULL; \
  151. pmc = rcu_dereference(pmc->next_rcu))
  152. #define for_each_pmc_rtnl(in_dev, pmc) \
  153. for (pmc = rtnl_dereference(in_dev->mc_list); \
  154. pmc != NULL; \
  155. pmc = rtnl_dereference(pmc->next_rcu))
  156. #ifdef CONFIG_IP_MULTICAST
  157. /*
  158. * Timer management
  159. */
  160. static void igmp_stop_timer(struct ip_mc_list *im)
  161. {
  162. spin_lock_bh(&im->lock);
  163. if (del_timer(&im->timer))
  164. atomic_dec(&im->refcnt);
  165. im->tm_running = 0;
  166. im->reporter = 0;
  167. im->unsolicit_count = 0;
  168. spin_unlock_bh(&im->lock);
  169. }
  170. /* It must be called with locked im->lock */
  171. static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
  172. {
  173. int tv = net_random() % max_delay;
  174. im->tm_running = 1;
  175. if (!mod_timer(&im->timer, jiffies+tv+2))
  176. atomic_inc(&im->refcnt);
  177. }
  178. static void igmp_gq_start_timer(struct in_device *in_dev)
  179. {
  180. int tv = net_random() % in_dev->mr_maxdelay;
  181. in_dev->mr_gq_running = 1;
  182. if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
  183. in_dev_hold(in_dev);
  184. }
  185. static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
  186. {
  187. int tv = net_random() % delay;
  188. if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
  189. in_dev_hold(in_dev);
  190. }
  191. static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
  192. {
  193. spin_lock_bh(&im->lock);
  194. im->unsolicit_count = 0;
  195. if (del_timer(&im->timer)) {
  196. if ((long)(im->timer.expires-jiffies) < max_delay) {
  197. add_timer(&im->timer);
  198. im->tm_running = 1;
  199. spin_unlock_bh(&im->lock);
  200. return;
  201. }
  202. atomic_dec(&im->refcnt);
  203. }
  204. igmp_start_timer(im, max_delay);
  205. spin_unlock_bh(&im->lock);
  206. }
  207. /*
  208. * Send an IGMP report.
  209. */
  210. #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
  211. static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
  212. int gdeleted, int sdeleted)
  213. {
  214. switch (type) {
  215. case IGMPV3_MODE_IS_INCLUDE:
  216. case IGMPV3_MODE_IS_EXCLUDE:
  217. if (gdeleted || sdeleted)
  218. return 0;
  219. if (!(pmc->gsquery && !psf->sf_gsresp)) {
  220. if (pmc->sfmode == MCAST_INCLUDE)
  221. return 1;
  222. /* don't include if this source is excluded
  223. * in all filters
  224. */
  225. if (psf->sf_count[MCAST_INCLUDE])
  226. return type == IGMPV3_MODE_IS_INCLUDE;
  227. return pmc->sfcount[MCAST_EXCLUDE] ==
  228. psf->sf_count[MCAST_EXCLUDE];
  229. }
  230. return 0;
  231. case IGMPV3_CHANGE_TO_INCLUDE:
  232. if (gdeleted || sdeleted)
  233. return 0;
  234. return psf->sf_count[MCAST_INCLUDE] != 0;
  235. case IGMPV3_CHANGE_TO_EXCLUDE:
  236. if (gdeleted || sdeleted)
  237. return 0;
  238. if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
  239. psf->sf_count[MCAST_INCLUDE])
  240. return 0;
  241. return pmc->sfcount[MCAST_EXCLUDE] ==
  242. psf->sf_count[MCAST_EXCLUDE];
  243. case IGMPV3_ALLOW_NEW_SOURCES:
  244. if (gdeleted || !psf->sf_crcount)
  245. return 0;
  246. return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
  247. case IGMPV3_BLOCK_OLD_SOURCES:
  248. if (pmc->sfmode == MCAST_INCLUDE)
  249. return gdeleted || (psf->sf_crcount && sdeleted);
  250. return psf->sf_crcount && !gdeleted && !sdeleted;
  251. }
  252. return 0;
  253. }
  254. static int
  255. igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
  256. {
  257. struct ip_sf_list *psf;
  258. int scount = 0;
  259. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  260. if (!is_in(pmc, psf, type, gdeleted, sdeleted))
  261. continue;
  262. scount++;
  263. }
  264. return scount;
  265. }
  266. #define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb))
  267. static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
  268. {
  269. struct sk_buff *skb;
  270. struct rtable *rt;
  271. struct iphdr *pip;
  272. struct igmpv3_report *pig;
  273. struct net *net = dev_net(dev);
  274. struct flowi4 fl4;
  275. while (1) {
  276. skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev),
  277. GFP_ATOMIC | __GFP_NOWARN);
  278. if (skb)
  279. break;
  280. size >>= 1;
  281. if (size < 256)
  282. return NULL;
  283. }
  284. igmp_skb_size(skb) = size;
  285. rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
  286. 0, 0,
  287. IPPROTO_IGMP, 0, dev->ifindex);
  288. if (IS_ERR(rt)) {
  289. kfree_skb(skb);
  290. return NULL;
  291. }
  292. skb_dst_set(skb, &rt->dst);
  293. skb->dev = dev;
  294. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  295. skb_reset_network_header(skb);
  296. pip = ip_hdr(skb);
  297. skb_put(skb, sizeof(struct iphdr) + 4);
  298. pip->version = 4;
  299. pip->ihl = (sizeof(struct iphdr)+4)>>2;
  300. pip->tos = 0xc0;
  301. pip->frag_off = htons(IP_DF);
  302. pip->ttl = 1;
  303. pip->daddr = fl4.daddr;
  304. pip->saddr = fl4.saddr;
  305. pip->protocol = IPPROTO_IGMP;
  306. pip->tot_len = 0; /* filled in later */
  307. ip_select_ident(pip, &rt->dst, NULL);
  308. ((u8*)&pip[1])[0] = IPOPT_RA;
  309. ((u8*)&pip[1])[1] = 4;
  310. ((u8*)&pip[1])[2] = 0;
  311. ((u8*)&pip[1])[3] = 0;
  312. skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
  313. skb_put(skb, sizeof(*pig));
  314. pig = igmpv3_report_hdr(skb);
  315. pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
  316. pig->resv1 = 0;
  317. pig->csum = 0;
  318. pig->resv2 = 0;
  319. pig->ngrec = 0;
  320. return skb;
  321. }
  322. static int igmpv3_sendpack(struct sk_buff *skb)
  323. {
  324. struct igmphdr *pig = igmp_hdr(skb);
  325. const int igmplen = skb->tail - skb->transport_header;
  326. pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
  327. return ip_local_out(skb);
  328. }
  329. static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
  330. {
  331. return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
  332. }
  333. static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
  334. int type, struct igmpv3_grec **ppgr)
  335. {
  336. struct net_device *dev = pmc->interface->dev;
  337. struct igmpv3_report *pih;
  338. struct igmpv3_grec *pgr;
  339. if (!skb)
  340. skb = igmpv3_newpack(dev, dev->mtu);
  341. if (!skb)
  342. return NULL;
  343. pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
  344. pgr->grec_type = type;
  345. pgr->grec_auxwords = 0;
  346. pgr->grec_nsrcs = 0;
  347. pgr->grec_mca = pmc->multiaddr;
  348. pih = igmpv3_report_hdr(skb);
  349. pih->ngrec = htons(ntohs(pih->ngrec)+1);
  350. *ppgr = pgr;
  351. return skb;
  352. }
  353. #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
  354. skb_tailroom(skb)) : 0)
  355. static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
  356. int type, int gdeleted, int sdeleted)
  357. {
  358. struct net_device *dev = pmc->interface->dev;
  359. struct igmpv3_report *pih;
  360. struct igmpv3_grec *pgr = NULL;
  361. struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
  362. int scount, stotal, first, isquery, truncate;
  363. if (pmc->multiaddr == IGMP_ALL_HOSTS)
  364. return skb;
  365. isquery = type == IGMPV3_MODE_IS_INCLUDE ||
  366. type == IGMPV3_MODE_IS_EXCLUDE;
  367. truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
  368. type == IGMPV3_CHANGE_TO_EXCLUDE;
  369. stotal = scount = 0;
  370. psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
  371. if (!*psf_list)
  372. goto empty_source;
  373. pih = skb ? igmpv3_report_hdr(skb) : NULL;
  374. /* EX and TO_EX get a fresh packet, if needed */
  375. if (truncate) {
  376. if (pih && pih->ngrec &&
  377. AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
  378. if (skb)
  379. igmpv3_sendpack(skb);
  380. skb = igmpv3_newpack(dev, dev->mtu);
  381. }
  382. }
  383. first = 1;
  384. psf_prev = NULL;
  385. for (psf=*psf_list; psf; psf=psf_next) {
  386. __be32 *psrc;
  387. psf_next = psf->sf_next;
  388. if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
  389. psf_prev = psf;
  390. continue;
  391. }
  392. /* clear marks on query responses */
  393. if (isquery)
  394. psf->sf_gsresp = 0;
  395. if (AVAILABLE(skb) < sizeof(__be32) +
  396. first*sizeof(struct igmpv3_grec)) {
  397. if (truncate && !first)
  398. break; /* truncate these */
  399. if (pgr)
  400. pgr->grec_nsrcs = htons(scount);
  401. if (skb)
  402. igmpv3_sendpack(skb);
  403. skb = igmpv3_newpack(dev, dev->mtu);
  404. first = 1;
  405. scount = 0;
  406. }
  407. if (first) {
  408. skb = add_grhead(skb, pmc, type, &pgr);
  409. first = 0;
  410. }
  411. if (!skb)
  412. return NULL;
  413. psrc = (__be32 *)skb_put(skb, sizeof(__be32));
  414. *psrc = psf->sf_inaddr;
  415. scount++; stotal++;
  416. if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
  417. type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
  418. psf->sf_crcount--;
  419. if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
  420. if (psf_prev)
  421. psf_prev->sf_next = psf->sf_next;
  422. else
  423. *psf_list = psf->sf_next;
  424. kfree(psf);
  425. continue;
  426. }
  427. }
  428. psf_prev = psf;
  429. }
  430. empty_source:
  431. if (!stotal) {
  432. if (type == IGMPV3_ALLOW_NEW_SOURCES ||
  433. type == IGMPV3_BLOCK_OLD_SOURCES)
  434. return skb;
  435. if (pmc->crcount || isquery) {
  436. /* make sure we have room for group header */
  437. if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
  438. igmpv3_sendpack(skb);
  439. skb = NULL; /* add_grhead will get a new one */
  440. }
  441. skb = add_grhead(skb, pmc, type, &pgr);
  442. }
  443. }
  444. if (pgr)
  445. pgr->grec_nsrcs = htons(scount);
  446. if (isquery)
  447. pmc->gsquery = 0; /* clear query state on report */
  448. return skb;
  449. }
  450. static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
  451. {
  452. struct sk_buff *skb = NULL;
  453. int type;
  454. if (!pmc) {
  455. rcu_read_lock();
  456. for_each_pmc_rcu(in_dev, pmc) {
  457. if (pmc->multiaddr == IGMP_ALL_HOSTS)
  458. continue;
  459. spin_lock_bh(&pmc->lock);
  460. if (pmc->sfcount[MCAST_EXCLUDE])
  461. type = IGMPV3_MODE_IS_EXCLUDE;
  462. else
  463. type = IGMPV3_MODE_IS_INCLUDE;
  464. skb = add_grec(skb, pmc, type, 0, 0);
  465. spin_unlock_bh(&pmc->lock);
  466. }
  467. rcu_read_unlock();
  468. } else {
  469. spin_lock_bh(&pmc->lock);
  470. if (pmc->sfcount[MCAST_EXCLUDE])
  471. type = IGMPV3_MODE_IS_EXCLUDE;
  472. else
  473. type = IGMPV3_MODE_IS_INCLUDE;
  474. skb = add_grec(skb, pmc, type, 0, 0);
  475. spin_unlock_bh(&pmc->lock);
  476. }
  477. if (!skb)
  478. return 0;
  479. return igmpv3_sendpack(skb);
  480. }
  481. /*
  482. * remove zero-count source records from a source filter list
  483. */
  484. static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
  485. {
  486. struct ip_sf_list *psf_prev, *psf_next, *psf;
  487. psf_prev = NULL;
  488. for (psf=*ppsf; psf; psf = psf_next) {
  489. psf_next = psf->sf_next;
  490. if (psf->sf_crcount == 0) {
  491. if (psf_prev)
  492. psf_prev->sf_next = psf->sf_next;
  493. else
  494. *ppsf = psf->sf_next;
  495. kfree(psf);
  496. } else
  497. psf_prev = psf;
  498. }
  499. }
  500. static void igmpv3_send_cr(struct in_device *in_dev)
  501. {
  502. struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
  503. struct sk_buff *skb = NULL;
  504. int type, dtype;
  505. rcu_read_lock();
  506. spin_lock_bh(&in_dev->mc_tomb_lock);
  507. /* deleted MCA's */
  508. pmc_prev = NULL;
  509. for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
  510. pmc_next = pmc->next;
  511. if (pmc->sfmode == MCAST_INCLUDE) {
  512. type = IGMPV3_BLOCK_OLD_SOURCES;
  513. dtype = IGMPV3_BLOCK_OLD_SOURCES;
  514. skb = add_grec(skb, pmc, type, 1, 0);
  515. skb = add_grec(skb, pmc, dtype, 1, 1);
  516. }
  517. if (pmc->crcount) {
  518. if (pmc->sfmode == MCAST_EXCLUDE) {
  519. type = IGMPV3_CHANGE_TO_INCLUDE;
  520. skb = add_grec(skb, pmc, type, 1, 0);
  521. }
  522. pmc->crcount--;
  523. if (pmc->crcount == 0) {
  524. igmpv3_clear_zeros(&pmc->tomb);
  525. igmpv3_clear_zeros(&pmc->sources);
  526. }
  527. }
  528. if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
  529. if (pmc_prev)
  530. pmc_prev->next = pmc_next;
  531. else
  532. in_dev->mc_tomb = pmc_next;
  533. in_dev_put(pmc->interface);
  534. kfree(pmc);
  535. } else
  536. pmc_prev = pmc;
  537. }
  538. spin_unlock_bh(&in_dev->mc_tomb_lock);
  539. /* change recs */
  540. for_each_pmc_rcu(in_dev, pmc) {
  541. spin_lock_bh(&pmc->lock);
  542. if (pmc->sfcount[MCAST_EXCLUDE]) {
  543. type = IGMPV3_BLOCK_OLD_SOURCES;
  544. dtype = IGMPV3_ALLOW_NEW_SOURCES;
  545. } else {
  546. type = IGMPV3_ALLOW_NEW_SOURCES;
  547. dtype = IGMPV3_BLOCK_OLD_SOURCES;
  548. }
  549. skb = add_grec(skb, pmc, type, 0, 0);
  550. skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
  551. /* filter mode changes */
  552. if (pmc->crcount) {
  553. if (pmc->sfmode == MCAST_EXCLUDE)
  554. type = IGMPV3_CHANGE_TO_EXCLUDE;
  555. else
  556. type = IGMPV3_CHANGE_TO_INCLUDE;
  557. skb = add_grec(skb, pmc, type, 0, 0);
  558. pmc->crcount--;
  559. }
  560. spin_unlock_bh(&pmc->lock);
  561. }
  562. rcu_read_unlock();
  563. if (!skb)
  564. return;
  565. (void) igmpv3_sendpack(skb);
  566. }
  567. static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
  568. int type)
  569. {
  570. struct sk_buff *skb;
  571. struct iphdr *iph;
  572. struct igmphdr *ih;
  573. struct rtable *rt;
  574. struct net_device *dev = in_dev->dev;
  575. struct net *net = dev_net(dev);
  576. __be32 group = pmc ? pmc->multiaddr : 0;
  577. struct flowi4 fl4;
  578. __be32 dst;
  579. if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
  580. return igmpv3_send_report(in_dev, pmc);
  581. else if (type == IGMP_HOST_LEAVE_MESSAGE)
  582. dst = IGMP_ALL_ROUTER;
  583. else
  584. dst = group;
  585. rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
  586. 0, 0,
  587. IPPROTO_IGMP, 0, dev->ifindex);
  588. if (IS_ERR(rt))
  589. return -1;
  590. skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
  591. if (skb == NULL) {
  592. ip_rt_put(rt);
  593. return -1;
  594. }
  595. skb_dst_set(skb, &rt->dst);
  596. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  597. skb_reset_network_header(skb);
  598. iph = ip_hdr(skb);
  599. skb_put(skb, sizeof(struct iphdr) + 4);
  600. iph->version = 4;
  601. iph->ihl = (sizeof(struct iphdr)+4)>>2;
  602. iph->tos = 0xc0;
  603. iph->frag_off = htons(IP_DF);
  604. iph->ttl = 1;
  605. iph->daddr = dst;
  606. iph->saddr = fl4.saddr;
  607. iph->protocol = IPPROTO_IGMP;
  608. ip_select_ident(iph, &rt->dst, NULL);
  609. ((u8*)&iph[1])[0] = IPOPT_RA;
  610. ((u8*)&iph[1])[1] = 4;
  611. ((u8*)&iph[1])[2] = 0;
  612. ((u8*)&iph[1])[3] = 0;
  613. ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
  614. ih->type = type;
  615. ih->code = 0;
  616. ih->csum = 0;
  617. ih->group = group;
  618. ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
  619. return ip_local_out(skb);
  620. }
  621. static void igmp_gq_timer_expire(unsigned long data)
  622. {
  623. struct in_device *in_dev = (struct in_device *)data;
  624. in_dev->mr_gq_running = 0;
  625. igmpv3_send_report(in_dev, NULL);
  626. __in_dev_put(in_dev);
  627. }
  628. static void igmp_ifc_timer_expire(unsigned long data)
  629. {
  630. struct in_device *in_dev = (struct in_device *)data;
  631. igmpv3_send_cr(in_dev);
  632. if (in_dev->mr_ifc_count) {
  633. in_dev->mr_ifc_count--;
  634. igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
  635. }
  636. __in_dev_put(in_dev);
  637. }
  638. static void igmp_ifc_event(struct in_device *in_dev)
  639. {
  640. if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
  641. return;
  642. in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
  643. IGMP_Unsolicited_Report_Count;
  644. igmp_ifc_start_timer(in_dev, 1);
  645. }
  646. static void igmp_timer_expire(unsigned long data)
  647. {
  648. struct ip_mc_list *im=(struct ip_mc_list *)data;
  649. struct in_device *in_dev = im->interface;
  650. spin_lock(&im->lock);
  651. im->tm_running = 0;
  652. if (im->unsolicit_count) {
  653. im->unsolicit_count--;
  654. igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
  655. }
  656. im->reporter = 1;
  657. spin_unlock(&im->lock);
  658. if (IGMP_V1_SEEN(in_dev))
  659. igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
  660. else if (IGMP_V2_SEEN(in_dev))
  661. igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
  662. else
  663. igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
  664. ip_ma_put(im);
  665. }
  666. /* mark EXCLUDE-mode sources */
  667. static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
  668. {
  669. struct ip_sf_list *psf;
  670. int i, scount;
  671. scount = 0;
  672. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  673. if (scount == nsrcs)
  674. break;
  675. for (i=0; i<nsrcs; i++) {
  676. /* skip inactive filters */
  677. if (psf->sf_count[MCAST_INCLUDE] ||
  678. pmc->sfcount[MCAST_EXCLUDE] !=
  679. psf->sf_count[MCAST_EXCLUDE])
  680. continue;
  681. if (srcs[i] == psf->sf_inaddr) {
  682. scount++;
  683. break;
  684. }
  685. }
  686. }
  687. pmc->gsquery = 0;
  688. if (scount == nsrcs) /* all sources excluded */
  689. return 0;
  690. return 1;
  691. }
  692. static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
  693. {
  694. struct ip_sf_list *psf;
  695. int i, scount;
  696. if (pmc->sfmode == MCAST_EXCLUDE)
  697. return igmp_xmarksources(pmc, nsrcs, srcs);
  698. /* mark INCLUDE-mode sources */
  699. scount = 0;
  700. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  701. if (scount == nsrcs)
  702. break;
  703. for (i=0; i<nsrcs; i++)
  704. if (srcs[i] == psf->sf_inaddr) {
  705. psf->sf_gsresp = 1;
  706. scount++;
  707. break;
  708. }
  709. }
  710. if (!scount) {
  711. pmc->gsquery = 0;
  712. return 0;
  713. }
  714. pmc->gsquery = 1;
  715. return 1;
  716. }
  717. static void igmp_heard_report(struct in_device *in_dev, __be32 group)
  718. {
  719. struct ip_mc_list *im;
  720. /* Timers are only set for non-local groups */
  721. if (group == IGMP_ALL_HOSTS)
  722. return;
  723. rcu_read_lock();
  724. for_each_pmc_rcu(in_dev, im) {
  725. if (im->multiaddr == group) {
  726. igmp_stop_timer(im);
  727. break;
  728. }
  729. }
  730. rcu_read_unlock();
  731. }
  732. static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
  733. int len)
  734. {
  735. struct igmphdr *ih = igmp_hdr(skb);
  736. struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
  737. struct ip_mc_list *im;
  738. __be32 group = ih->group;
  739. int max_delay;
  740. int mark = 0;
  741. if (len == 8) {
  742. if (ih->code == 0) {
  743. /* Alas, old v1 router presents here. */
  744. max_delay = IGMP_Query_Response_Interval;
  745. in_dev->mr_v1_seen = jiffies +
  746. IGMP_V1_Router_Present_Timeout;
  747. group = 0;
  748. } else {
  749. /* v2 router present */
  750. max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
  751. in_dev->mr_v2_seen = jiffies +
  752. IGMP_V2_Router_Present_Timeout;
  753. }
  754. /* cancel the interface change timer */
  755. in_dev->mr_ifc_count = 0;
  756. if (del_timer(&in_dev->mr_ifc_timer))
  757. __in_dev_put(in_dev);
  758. /* clear deleted report items */
  759. igmpv3_clear_delrec(in_dev);
  760. } else if (len < 12) {
  761. return; /* ignore bogus packet; freed by caller */
  762. } else if (IGMP_V1_SEEN(in_dev)) {
  763. /* This is a v3 query with v1 queriers present */
  764. max_delay = IGMP_Query_Response_Interval;
  765. group = 0;
  766. } else if (IGMP_V2_SEEN(in_dev)) {
  767. /* this is a v3 query with v2 queriers present;
  768. * Interpretation of the max_delay code is problematic here.
  769. * A real v2 host would use ih_code directly, while v3 has a
  770. * different encoding. We use the v3 encoding as more likely
  771. * to be intended in a v3 query.
  772. */
  773. max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
  774. if (!max_delay)
  775. max_delay = 1; /* can't mod w/ 0 */
  776. } else { /* v3 */
  777. if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
  778. return;
  779. ih3 = igmpv3_query_hdr(skb);
  780. if (ih3->nsrcs) {
  781. if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
  782. + ntohs(ih3->nsrcs)*sizeof(__be32)))
  783. return;
  784. ih3 = igmpv3_query_hdr(skb);
  785. }
  786. max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
  787. if (!max_delay)
  788. max_delay = 1; /* can't mod w/ 0 */
  789. in_dev->mr_maxdelay = max_delay;
  790. if (ih3->qrv)
  791. in_dev->mr_qrv = ih3->qrv;
  792. if (!group) { /* general query */
  793. if (ih3->nsrcs)
  794. return; /* no sources allowed */
  795. igmp_gq_start_timer(in_dev);
  796. return;
  797. }
  798. /* mark sources to include, if group & source-specific */
  799. mark = ih3->nsrcs != 0;
  800. }
  801. /*
  802. * - Start the timers in all of our membership records
  803. * that the query applies to for the interface on
  804. * which the query arrived excl. those that belong
  805. * to a "local" group (224.0.0.X)
  806. * - For timers already running check if they need to
  807. * be reset.
  808. * - Use the igmp->igmp_code field as the maximum
  809. * delay possible
  810. */
  811. rcu_read_lock();
  812. for_each_pmc_rcu(in_dev, im) {
  813. int changed;
  814. if (group && group != im->multiaddr)
  815. continue;
  816. if (im->multiaddr == IGMP_ALL_HOSTS)
  817. continue;
  818. spin_lock_bh(&im->lock);
  819. if (im->tm_running)
  820. im->gsquery = im->gsquery && mark;
  821. else
  822. im->gsquery = mark;
  823. changed = !im->gsquery ||
  824. igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
  825. spin_unlock_bh(&im->lock);
  826. if (changed)
  827. igmp_mod_timer(im, max_delay);
  828. }
  829. rcu_read_unlock();
  830. }
  831. /* called in rcu_read_lock() section */
  832. int igmp_rcv(struct sk_buff *skb)
  833. {
  834. /* This basically follows the spec line by line -- see RFC1112 */
  835. struct igmphdr *ih;
  836. struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
  837. int len = skb->len;
  838. if (in_dev == NULL)
  839. goto drop;
  840. if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
  841. goto drop;
  842. switch (skb->ip_summed) {
  843. case CHECKSUM_COMPLETE:
  844. if (!csum_fold(skb->csum))
  845. break;
  846. /* fall through */
  847. case CHECKSUM_NONE:
  848. skb->csum = 0;
  849. if (__skb_checksum_complete(skb))
  850. goto drop;
  851. }
  852. ih = igmp_hdr(skb);
  853. switch (ih->type) {
  854. case IGMP_HOST_MEMBERSHIP_QUERY:
  855. igmp_heard_query(in_dev, skb, len);
  856. break;
  857. case IGMP_HOST_MEMBERSHIP_REPORT:
  858. case IGMPV2_HOST_MEMBERSHIP_REPORT:
  859. /* Is it our report looped back? */
  860. if (rt_is_output_route(skb_rtable(skb)))
  861. break;
  862. /* don't rely on MC router hearing unicast reports */
  863. if (skb->pkt_type == PACKET_MULTICAST ||
  864. skb->pkt_type == PACKET_BROADCAST)
  865. igmp_heard_report(in_dev, ih->group);
  866. break;
  867. case IGMP_PIM:
  868. #ifdef CONFIG_IP_PIMSM_V1
  869. return pim_rcv_v1(skb);
  870. #endif
  871. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  872. case IGMP_DVMRP:
  873. case IGMP_TRACE:
  874. case IGMP_HOST_LEAVE_MESSAGE:
  875. case IGMP_MTRACE:
  876. case IGMP_MTRACE_RESP:
  877. break;
  878. default:
  879. break;
  880. }
  881. drop:
  882. kfree_skb(skb);
  883. return 0;
  884. }
  885. #endif
  886. /*
  887. * Add a filter to a device
  888. */
  889. static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
  890. {
  891. char buf[MAX_ADDR_LEN];
  892. struct net_device *dev = in_dev->dev;
  893. /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
  894. We will get multicast token leakage, when IFF_MULTICAST
  895. is changed. This check should be done in dev->set_multicast_list
  896. routine. Something sort of:
  897. if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
  898. --ANK
  899. */
  900. if (arp_mc_map(addr, buf, dev, 0) == 0)
  901. dev_mc_add(dev, buf);
  902. }
  903. /*
  904. * Remove a filter from a device
  905. */
  906. static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
  907. {
  908. char buf[MAX_ADDR_LEN];
  909. struct net_device *dev = in_dev->dev;
  910. if (arp_mc_map(addr, buf, dev, 0) == 0)
  911. dev_mc_del(dev, buf);
  912. }
  913. #ifdef CONFIG_IP_MULTICAST
  914. /*
  915. * deleted ip_mc_list manipulation
  916. */
  917. static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
  918. {
  919. struct ip_mc_list *pmc;
  920. /* this is an "ip_mc_list" for convenience; only the fields below
  921. * are actually used. In particular, the refcnt and users are not
  922. * used for management of the delete list. Using the same structure
  923. * for deleted items allows change reports to use common code with
  924. * non-deleted or query-response MCA's.
  925. */
  926. pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
  927. if (!pmc)
  928. return;
  929. spin_lock_bh(&im->lock);
  930. pmc->interface = im->interface;
  931. in_dev_hold(in_dev);
  932. pmc->multiaddr = im->multiaddr;
  933. pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  934. IGMP_Unsolicited_Report_Count;
  935. pmc->sfmode = im->sfmode;
  936. if (pmc->sfmode == MCAST_INCLUDE) {
  937. struct ip_sf_list *psf;
  938. pmc->tomb = im->tomb;
  939. pmc->sources = im->sources;
  940. im->tomb = im->sources = NULL;
  941. for (psf=pmc->sources; psf; psf=psf->sf_next)
  942. psf->sf_crcount = pmc->crcount;
  943. }
  944. spin_unlock_bh(&im->lock);
  945. spin_lock_bh(&in_dev->mc_tomb_lock);
  946. pmc->next = in_dev->mc_tomb;
  947. in_dev->mc_tomb = pmc;
  948. spin_unlock_bh(&in_dev->mc_tomb_lock);
  949. }
  950. static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
  951. {
  952. struct ip_mc_list *pmc, *pmc_prev;
  953. struct ip_sf_list *psf, *psf_next;
  954. spin_lock_bh(&in_dev->mc_tomb_lock);
  955. pmc_prev = NULL;
  956. for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
  957. if (pmc->multiaddr == multiaddr)
  958. break;
  959. pmc_prev = pmc;
  960. }
  961. if (pmc) {
  962. if (pmc_prev)
  963. pmc_prev->next = pmc->next;
  964. else
  965. in_dev->mc_tomb = pmc->next;
  966. }
  967. spin_unlock_bh(&in_dev->mc_tomb_lock);
  968. if (pmc) {
  969. for (psf=pmc->tomb; psf; psf=psf_next) {
  970. psf_next = psf->sf_next;
  971. kfree(psf);
  972. }
  973. in_dev_put(pmc->interface);
  974. kfree(pmc);
  975. }
  976. }
  977. static void igmpv3_clear_delrec(struct in_device *in_dev)
  978. {
  979. struct ip_mc_list *pmc, *nextpmc;
  980. spin_lock_bh(&in_dev->mc_tomb_lock);
  981. pmc = in_dev->mc_tomb;
  982. in_dev->mc_tomb = NULL;
  983. spin_unlock_bh(&in_dev->mc_tomb_lock);
  984. for (; pmc; pmc = nextpmc) {
  985. nextpmc = pmc->next;
  986. ip_mc_clear_src(pmc);
  987. in_dev_put(pmc->interface);
  988. kfree(pmc);
  989. }
  990. /* clear dead sources, too */
  991. rcu_read_lock();
  992. for_each_pmc_rcu(in_dev, pmc) {
  993. struct ip_sf_list *psf, *psf_next;
  994. spin_lock_bh(&pmc->lock);
  995. psf = pmc->tomb;
  996. pmc->tomb = NULL;
  997. spin_unlock_bh(&pmc->lock);
  998. for (; psf; psf=psf_next) {
  999. psf_next = psf->sf_next;
  1000. kfree(psf);
  1001. }
  1002. }
  1003. rcu_read_unlock();
  1004. }
  1005. #endif
  1006. static void igmp_group_dropped(struct ip_mc_list *im)
  1007. {
  1008. struct in_device *in_dev = im->interface;
  1009. #ifdef CONFIG_IP_MULTICAST
  1010. int reporter;
  1011. #endif
  1012. if (im->loaded) {
  1013. im->loaded = 0;
  1014. ip_mc_filter_del(in_dev, im->multiaddr);
  1015. }
  1016. #ifdef CONFIG_IP_MULTICAST
  1017. if (im->multiaddr == IGMP_ALL_HOSTS)
  1018. return;
  1019. reporter = im->reporter;
  1020. igmp_stop_timer(im);
  1021. if (!in_dev->dead) {
  1022. if (IGMP_V1_SEEN(in_dev))
  1023. return;
  1024. if (IGMP_V2_SEEN(in_dev)) {
  1025. if (reporter)
  1026. igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
  1027. return;
  1028. }
  1029. /* IGMPv3 */
  1030. igmpv3_add_delrec(in_dev, im);
  1031. igmp_ifc_event(in_dev);
  1032. }
  1033. #endif
  1034. }
  1035. static void igmp_group_added(struct ip_mc_list *im)
  1036. {
  1037. struct in_device *in_dev = im->interface;
  1038. if (im->loaded == 0) {
  1039. im->loaded = 1;
  1040. ip_mc_filter_add(in_dev, im->multiaddr);
  1041. }
  1042. #ifdef CONFIG_IP_MULTICAST
  1043. if (im->multiaddr == IGMP_ALL_HOSTS)
  1044. return;
  1045. if (in_dev->dead)
  1046. return;
  1047. if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
  1048. spin_lock_bh(&im->lock);
  1049. igmp_start_timer(im, IGMP_Initial_Report_Delay);
  1050. spin_unlock_bh(&im->lock);
  1051. return;
  1052. }
  1053. /* else, v3 */
  1054. im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  1055. IGMP_Unsolicited_Report_Count;
  1056. igmp_ifc_event(in_dev);
  1057. #endif
  1058. }
  1059. /*
  1060. * Multicast list managers
  1061. */
  1062. /*
  1063. * A socket has joined a multicast group on device dev.
  1064. */
  1065. void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
  1066. {
  1067. struct ip_mc_list *im;
  1068. ASSERT_RTNL();
  1069. for_each_pmc_rtnl(in_dev, im) {
  1070. if (im->multiaddr == addr) {
  1071. im->users++;
  1072. ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
  1073. goto out;
  1074. }
  1075. }
  1076. im = kzalloc(sizeof(*im), GFP_KERNEL);
  1077. if (!im)
  1078. goto out;
  1079. im->users = 1;
  1080. im->interface = in_dev;
  1081. in_dev_hold(in_dev);
  1082. im->multiaddr = addr;
  1083. /* initial mode is (EX, empty) */
  1084. im->sfmode = MCAST_EXCLUDE;
  1085. im->sfcount[MCAST_EXCLUDE] = 1;
  1086. atomic_set(&im->refcnt, 1);
  1087. spin_lock_init(&im->lock);
  1088. #ifdef CONFIG_IP_MULTICAST
  1089. setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
  1090. im->unsolicit_count = IGMP_Unsolicited_Report_Count;
  1091. #endif
  1092. im->next_rcu = in_dev->mc_list;
  1093. in_dev->mc_count++;
  1094. rcu_assign_pointer(in_dev->mc_list, im);
  1095. #ifdef CONFIG_IP_MULTICAST
  1096. igmpv3_del_delrec(in_dev, im->multiaddr);
  1097. #endif
  1098. igmp_group_added(im);
  1099. if (!in_dev->dead)
  1100. ip_rt_multicast_event(in_dev);
  1101. out:
  1102. return;
  1103. }
  1104. EXPORT_SYMBOL(ip_mc_inc_group);
  1105. /*
  1106. * Resend IGMP JOIN report; used for bonding.
  1107. * Called with rcu_read_lock()
  1108. */
  1109. void ip_mc_rejoin_groups(struct in_device *in_dev)
  1110. {
  1111. #ifdef CONFIG_IP_MULTICAST
  1112. struct ip_mc_list *im;
  1113. int type;
  1114. for_each_pmc_rcu(in_dev, im) {
  1115. if (im->multiaddr == IGMP_ALL_HOSTS)
  1116. continue;
  1117. /* a failover is happening and switches
  1118. * must be notified immediately
  1119. */
  1120. if (IGMP_V1_SEEN(in_dev))
  1121. type = IGMP_HOST_MEMBERSHIP_REPORT;
  1122. else if (IGMP_V2_SEEN(in_dev))
  1123. type = IGMPV2_HOST_MEMBERSHIP_REPORT;
  1124. else
  1125. type = IGMPV3_HOST_MEMBERSHIP_REPORT;
  1126. igmp_send_report(in_dev, im, type);
  1127. }
  1128. #endif
  1129. }
  1130. EXPORT_SYMBOL(ip_mc_rejoin_groups);
  1131. /*
  1132. * A socket has left a multicast group on device dev
  1133. */
  1134. void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
  1135. {
  1136. struct ip_mc_list *i;
  1137. struct ip_mc_list __rcu **ip;
  1138. ASSERT_RTNL();
  1139. for (ip = &in_dev->mc_list;
  1140. (i = rtnl_dereference(*ip)) != NULL;
  1141. ip = &i->next_rcu) {
  1142. if (i->multiaddr == addr) {
  1143. if (--i->users == 0) {
  1144. *ip = i->next_rcu;
  1145. in_dev->mc_count--;
  1146. igmp_group_dropped(i);
  1147. ip_mc_clear_src(i);
  1148. if (!in_dev->dead)
  1149. ip_rt_multicast_event(in_dev);
  1150. ip_ma_put(i);
  1151. return;
  1152. }
  1153. break;
  1154. }
  1155. }
  1156. }
  1157. EXPORT_SYMBOL(ip_mc_dec_group);
  1158. /* Device changing type */
  1159. void ip_mc_unmap(struct in_device *in_dev)
  1160. {
  1161. struct ip_mc_list *pmc;
  1162. ASSERT_RTNL();
  1163. for_each_pmc_rtnl(in_dev, pmc)
  1164. igmp_group_dropped(pmc);
  1165. }
  1166. void ip_mc_remap(struct in_device *in_dev)
  1167. {
  1168. struct ip_mc_list *pmc;
  1169. ASSERT_RTNL();
  1170. for_each_pmc_rtnl(in_dev, pmc)
  1171. igmp_group_added(pmc);
  1172. }
  1173. /* Device going down */
  1174. void ip_mc_down(struct in_device *in_dev)
  1175. {
  1176. struct ip_mc_list *pmc;
  1177. ASSERT_RTNL();
  1178. for_each_pmc_rtnl(in_dev, pmc)
  1179. igmp_group_dropped(pmc);
  1180. #ifdef CONFIG_IP_MULTICAST
  1181. in_dev->mr_ifc_count = 0;
  1182. if (del_timer(&in_dev->mr_ifc_timer))
  1183. __in_dev_put(in_dev);
  1184. in_dev->mr_gq_running = 0;
  1185. if (del_timer(&in_dev->mr_gq_timer))
  1186. __in_dev_put(in_dev);
  1187. igmpv3_clear_delrec(in_dev);
  1188. #endif
  1189. ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
  1190. }
  1191. void ip_mc_init_dev(struct in_device *in_dev)
  1192. {
  1193. ASSERT_RTNL();
  1194. in_dev->mc_tomb = NULL;
  1195. #ifdef CONFIG_IP_MULTICAST
  1196. in_dev->mr_gq_running = 0;
  1197. setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
  1198. (unsigned long)in_dev);
  1199. in_dev->mr_ifc_count = 0;
  1200. in_dev->mc_count = 0;
  1201. setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
  1202. (unsigned long)in_dev);
  1203. in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
  1204. #endif
  1205. spin_lock_init(&in_dev->mc_tomb_lock);
  1206. }
  1207. /* Device going up */
  1208. void ip_mc_up(struct in_device *in_dev)
  1209. {
  1210. struct ip_mc_list *pmc;
  1211. ASSERT_RTNL();
  1212. ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
  1213. for_each_pmc_rtnl(in_dev, pmc)
  1214. igmp_group_added(pmc);
  1215. }
  1216. /*
  1217. * Device is about to be destroyed: clean up.
  1218. */
  1219. void ip_mc_destroy_dev(struct in_device *in_dev)
  1220. {
  1221. struct ip_mc_list *i;
  1222. ASSERT_RTNL();
  1223. /* Deactivate timers */
  1224. ip_mc_down(in_dev);
  1225. while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
  1226. in_dev->mc_list = i->next_rcu;
  1227. in_dev->mc_count--;
  1228. /* We've dropped the groups in ip_mc_down already */
  1229. ip_mc_clear_src(i);
  1230. ip_ma_put(i);
  1231. }
  1232. }
  1233. /* RTNL is locked */
  1234. static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
  1235. {
  1236. struct net_device *dev = NULL;
  1237. struct in_device *idev = NULL;
  1238. if (imr->imr_ifindex) {
  1239. idev = inetdev_by_index(net, imr->imr_ifindex);
  1240. return idev;
  1241. }
  1242. if (imr->imr_address.s_addr) {
  1243. dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
  1244. if (!dev)
  1245. return NULL;
  1246. }
  1247. if (!dev) {
  1248. struct rtable *rt = ip_route_output(net,
  1249. imr->imr_multiaddr.s_addr,
  1250. 0, 0, 0);
  1251. if (!IS_ERR(rt)) {
  1252. dev = rt->dst.dev;
  1253. ip_rt_put(rt);
  1254. }
  1255. }
  1256. if (dev) {
  1257. imr->imr_ifindex = dev->ifindex;
  1258. idev = __in_dev_get_rtnl(dev);
  1259. }
  1260. return idev;
  1261. }
  1262. /*
  1263. * Join a socket to a group
  1264. */
  1265. int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
  1266. int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
  1267. static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
  1268. __be32 *psfsrc)
  1269. {
  1270. struct ip_sf_list *psf, *psf_prev;
  1271. int rv = 0;
  1272. psf_prev = NULL;
  1273. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  1274. if (psf->sf_inaddr == *psfsrc)
  1275. break;
  1276. psf_prev = psf;
  1277. }
  1278. if (!psf || psf->sf_count[sfmode] == 0) {
  1279. /* source filter not found, or count wrong => bug */
  1280. return -ESRCH;
  1281. }
  1282. psf->sf_count[sfmode]--;
  1283. if (psf->sf_count[sfmode] == 0) {
  1284. ip_rt_multicast_event(pmc->interface);
  1285. }
  1286. if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
  1287. #ifdef CONFIG_IP_MULTICAST
  1288. struct in_device *in_dev = pmc->interface;
  1289. #endif
  1290. /* no more filters for this source */
  1291. if (psf_prev)
  1292. psf_prev->sf_next = psf->sf_next;
  1293. else
  1294. pmc->sources = psf->sf_next;
  1295. #ifdef CONFIG_IP_MULTICAST
  1296. if (psf->sf_oldin &&
  1297. !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
  1298. psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  1299. IGMP_Unsolicited_Report_Count;
  1300. psf->sf_next = pmc->tomb;
  1301. pmc->tomb = psf;
  1302. rv = 1;
  1303. } else
  1304. #endif
  1305. kfree(psf);
  1306. }
  1307. return rv;
  1308. }
  1309. #ifndef CONFIG_IP_MULTICAST
  1310. #define igmp_ifc_event(x) do { } while (0)
  1311. #endif
  1312. static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
  1313. int sfcount, __be32 *psfsrc, int delta)
  1314. {
  1315. struct ip_mc_list *pmc;
  1316. int changerec = 0;
  1317. int i, err;
  1318. if (!in_dev)
  1319. return -ENODEV;
  1320. rcu_read_lock();
  1321. for_each_pmc_rcu(in_dev, pmc) {
  1322. if (*pmca == pmc->multiaddr)
  1323. break;
  1324. }
  1325. if (!pmc) {
  1326. /* MCA not found?? bug */
  1327. rcu_read_unlock();
  1328. return -ESRCH;
  1329. }
  1330. spin_lock_bh(&pmc->lock);
  1331. rcu_read_unlock();
  1332. #ifdef CONFIG_IP_MULTICAST
  1333. sf_markstate(pmc);
  1334. #endif
  1335. if (!delta) {
  1336. err = -EINVAL;
  1337. if (!pmc->sfcount[sfmode])
  1338. goto out_unlock;
  1339. pmc->sfcount[sfmode]--;
  1340. }
  1341. err = 0;
  1342. for (i=0; i<sfcount; i++) {
  1343. int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1344. changerec |= rv > 0;
  1345. if (!err && rv < 0)
  1346. err = rv;
  1347. }
  1348. if (pmc->sfmode == MCAST_EXCLUDE &&
  1349. pmc->sfcount[MCAST_EXCLUDE] == 0 &&
  1350. pmc->sfcount[MCAST_INCLUDE]) {
  1351. #ifdef CONFIG_IP_MULTICAST
  1352. struct ip_sf_list *psf;
  1353. #endif
  1354. /* filter mode change */
  1355. pmc->sfmode = MCAST_INCLUDE;
  1356. #ifdef CONFIG_IP_MULTICAST
  1357. pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  1358. IGMP_Unsolicited_Report_Count;
  1359. in_dev->mr_ifc_count = pmc->crcount;
  1360. for (psf=pmc->sources; psf; psf = psf->sf_next)
  1361. psf->sf_crcount = 0;
  1362. igmp_ifc_event(pmc->interface);
  1363. } else if (sf_setstate(pmc) || changerec) {
  1364. igmp_ifc_event(pmc->interface);
  1365. #endif
  1366. }
  1367. out_unlock:
  1368. spin_unlock_bh(&pmc->lock);
  1369. return err;
  1370. }
  1371. /*
  1372. * Add multicast single-source filter to the interface list
  1373. */
  1374. static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
  1375. __be32 *psfsrc, int delta)
  1376. {
  1377. struct ip_sf_list *psf, *psf_prev;
  1378. psf_prev = NULL;
  1379. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  1380. if (psf->sf_inaddr == *psfsrc)
  1381. break;
  1382. psf_prev = psf;
  1383. }
  1384. if (!psf) {
  1385. psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
  1386. if (!psf)
  1387. return -ENOBUFS;
  1388. psf->sf_inaddr = *psfsrc;
  1389. if (psf_prev) {
  1390. psf_prev->sf_next = psf;
  1391. } else
  1392. pmc->sources = psf;
  1393. }
  1394. psf->sf_count[sfmode]++;
  1395. if (psf->sf_count[sfmode] == 1) {
  1396. ip_rt_multicast_event(pmc->interface);
  1397. }
  1398. return 0;
  1399. }
  1400. #ifdef CONFIG_IP_MULTICAST
  1401. static void sf_markstate(struct ip_mc_list *pmc)
  1402. {
  1403. struct ip_sf_list *psf;
  1404. int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
  1405. for (psf=pmc->sources; psf; psf=psf->sf_next)
  1406. if (pmc->sfcount[MCAST_EXCLUDE]) {
  1407. psf->sf_oldin = mca_xcount ==
  1408. psf->sf_count[MCAST_EXCLUDE] &&
  1409. !psf->sf_count[MCAST_INCLUDE];
  1410. } else
  1411. psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
  1412. }
  1413. static int sf_setstate(struct ip_mc_list *pmc)
  1414. {
  1415. struct ip_sf_list *psf, *dpsf;
  1416. int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
  1417. int qrv = pmc->interface->mr_qrv;
  1418. int new_in, rv;
  1419. rv = 0;
  1420. for (psf=pmc->sources; psf; psf=psf->sf_next) {
  1421. if (pmc->sfcount[MCAST_EXCLUDE]) {
  1422. new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
  1423. !psf->sf_count[MCAST_INCLUDE];
  1424. } else
  1425. new_in = psf->sf_count[MCAST_INCLUDE] != 0;
  1426. if (new_in) {
  1427. if (!psf->sf_oldin) {
  1428. struct ip_sf_list *prev = NULL;
  1429. for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
  1430. if (dpsf->sf_inaddr == psf->sf_inaddr)
  1431. break;
  1432. prev = dpsf;
  1433. }
  1434. if (dpsf) {
  1435. if (prev)
  1436. prev->sf_next = dpsf->sf_next;
  1437. else
  1438. pmc->tomb = dpsf->sf_next;
  1439. kfree(dpsf);
  1440. }
  1441. psf->sf_crcount = qrv;
  1442. rv++;
  1443. }
  1444. } else if (psf->sf_oldin) {
  1445. psf->sf_crcount = 0;
  1446. /*
  1447. * add or update "delete" records if an active filter
  1448. * is now inactive
  1449. */
  1450. for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
  1451. if (dpsf->sf_inaddr == psf->sf_inaddr)
  1452. break;
  1453. if (!dpsf) {
  1454. dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
  1455. if (!dpsf)
  1456. continue;
  1457. *dpsf = *psf;
  1458. /* pmc->lock held by callers */
  1459. dpsf->sf_next = pmc->tomb;
  1460. pmc->tomb = dpsf;
  1461. }
  1462. dpsf->sf_crcount = qrv;
  1463. rv++;
  1464. }
  1465. }
  1466. return rv;
  1467. }
  1468. #endif
  1469. /*
  1470. * Add multicast source filter list to the interface list
  1471. */
  1472. static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
  1473. int sfcount, __be32 *psfsrc, int delta)
  1474. {
  1475. struct ip_mc_list *pmc;
  1476. int isexclude;
  1477. int i, err;
  1478. if (!in_dev)
  1479. return -ENODEV;
  1480. rcu_read_lock();
  1481. for_each_pmc_rcu(in_dev, pmc) {
  1482. if (*pmca == pmc->multiaddr)
  1483. break;
  1484. }
  1485. if (!pmc) {
  1486. /* MCA not found?? bug */
  1487. rcu_read_unlock();
  1488. return -ESRCH;
  1489. }
  1490. spin_lock_bh(&pmc->lock);
  1491. rcu_read_unlock();
  1492. #ifdef CONFIG_IP_MULTICAST
  1493. sf_markstate(pmc);
  1494. #endif
  1495. isexclude = pmc->sfmode == MCAST_EXCLUDE;
  1496. if (!delta)
  1497. pmc->sfcount[sfmode]++;
  1498. err = 0;
  1499. for (i=0; i<sfcount; i++) {
  1500. err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
  1501. if (err)
  1502. break;
  1503. }
  1504. if (err) {
  1505. int j;
  1506. pmc->sfcount[sfmode]--;
  1507. for (j=0; j<i; j++)
  1508. (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
  1509. } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
  1510. #ifdef CONFIG_IP_MULTICAST
  1511. struct ip_sf_list *psf;
  1512. in_dev = pmc->interface;
  1513. #endif
  1514. /* filter mode change */
  1515. if (pmc->sfcount[MCAST_EXCLUDE])
  1516. pmc->sfmode = MCAST_EXCLUDE;
  1517. else if (pmc->sfcount[MCAST_INCLUDE])
  1518. pmc->sfmode = MCAST_INCLUDE;
  1519. #ifdef CONFIG_IP_MULTICAST
  1520. /* else no filters; keep old mode for reports */
  1521. pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
  1522. IGMP_Unsolicited_Report_Count;
  1523. in_dev->mr_ifc_count = pmc->crcount;
  1524. for (psf=pmc->sources; psf; psf = psf->sf_next)
  1525. psf->sf_crcount = 0;
  1526. igmp_ifc_event(in_dev);
  1527. } else if (sf_setstate(pmc)) {
  1528. igmp_ifc_event(in_dev);
  1529. #endif
  1530. }
  1531. spin_unlock_bh(&pmc->lock);
  1532. return err;
  1533. }
  1534. static void ip_mc_clear_src(struct ip_mc_list *pmc)
  1535. {
  1536. struct ip_sf_list *psf, *nextpsf;
  1537. for (psf=pmc->tomb; psf; psf=nextpsf) {
  1538. nextpsf = psf->sf_next;
  1539. kfree(psf);
  1540. }
  1541. pmc->tomb = NULL;
  1542. for (psf=pmc->sources; psf; psf=nextpsf) {
  1543. nextpsf = psf->sf_next;
  1544. kfree(psf);
  1545. }
  1546. pmc->sources = NULL;
  1547. pmc->sfmode = MCAST_EXCLUDE;
  1548. pmc->sfcount[MCAST_INCLUDE] = 0;
  1549. pmc->sfcount[MCAST_EXCLUDE] = 1;
  1550. }
  1551. /*
  1552. * Join a multicast group
  1553. */
  1554. int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
  1555. {
  1556. int err;
  1557. __be32 addr = imr->imr_multiaddr.s_addr;
  1558. struct ip_mc_socklist *iml = NULL, *i;
  1559. struct in_device *in_dev;
  1560. struct inet_sock *inet = inet_sk(sk);
  1561. struct net *net = sock_net(sk);
  1562. int ifindex;
  1563. int count = 0;
  1564. if (!ipv4_is_multicast(addr))
  1565. return -EINVAL;
  1566. rtnl_lock();
  1567. in_dev = ip_mc_find_dev(net, imr);
  1568. if (!in_dev) {
  1569. iml = NULL;
  1570. err = -ENODEV;
  1571. goto done;
  1572. }
  1573. err = -EADDRINUSE;
  1574. ifindex = imr->imr_ifindex;
  1575. for_each_pmc_rtnl(inet, i) {
  1576. if (i->multi.imr_multiaddr.s_addr == addr &&
  1577. i->multi.imr_ifindex == ifindex)
  1578. goto done;
  1579. count++;
  1580. }
  1581. err = -ENOBUFS;
  1582. if (count >= sysctl_igmp_max_memberships)
  1583. goto done;
  1584. iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
  1585. if (iml == NULL)
  1586. goto done;
  1587. memcpy(&iml->multi, imr, sizeof(*imr));
  1588. iml->next_rcu = inet->mc_list;
  1589. iml->sflist = NULL;
  1590. iml->sfmode = MCAST_EXCLUDE;
  1591. rcu_assign_pointer(inet->mc_list, iml);
  1592. ip_mc_inc_group(in_dev, addr);
  1593. err = 0;
  1594. done:
  1595. rtnl_unlock();
  1596. return err;
  1597. }
  1598. EXPORT_SYMBOL(ip_mc_join_group);
  1599. static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
  1600. struct in_device *in_dev)
  1601. {
  1602. struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
  1603. int err;
  1604. if (psf == NULL) {
  1605. /* any-source empty exclude case */
  1606. return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
  1607. iml->sfmode, 0, NULL, 0);
  1608. }
  1609. err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
  1610. iml->sfmode, psf->sl_count, psf->sl_addr, 0);
  1611. rcu_assign_pointer(iml->sflist, NULL);
  1612. /* decrease mem now to avoid the memleak warning */
  1613. atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
  1614. kfree_rcu(psf, rcu);
  1615. return err;
  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;
  1624. struct ip_mc_socklist __rcu **imlp;
  1625. struct in_device *in_dev;
  1626. struct net *net = sock_net(sk);
  1627. __be32 group = imr->imr_multiaddr.s_addr;
  1628. u32 ifindex;
  1629. int ret = -EADDRNOTAVAIL;
  1630. rtnl_lock();
  1631. in_dev = ip_mc_find_dev(net, imr);
  1632. ifindex = imr->imr_ifindex;
  1633. for (imlp = &inet->mc_list;
  1634. (iml = rtnl_dereference(*imlp)) != NULL;
  1635. imlp = &iml->next_rcu) {
  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. *imlp = iml->next_rcu;
  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. kfree_rcu(iml, rcu);
  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_each_pmc_rtnl(inet, pmc) {
  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 = rtnl_dereference(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. kfree_rcu(psl, rcu);
  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_each_pmc_rtnl(inet, pmc) {
  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 = rtnl_dereference(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. kfree_rcu(psl, rcu);
  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_each_pmc_rtnl(inet, pmc) {
  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 = rtnl_dereference(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_each_pmc_rtnl(inet, pmc) {
  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 = rtnl_dereference(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_each_pmc_rcu(inet, pmc) {
  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 = rcu_dereference(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 = rtnl_dereference(inet->mc_list)) != NULL) {
  2018. struct in_device *in_dev;
  2019. inet->mc_list = iml->next_rcu;
  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. /* decrease mem now to avoid the memleak warning */
  2025. atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
  2026. kfree_rcu(iml, rcu);
  2027. }
  2028. rtnl_unlock();
  2029. }
  2030. /* called with rcu_read_lock() */
  2031. int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
  2032. {
  2033. struct ip_mc_list *im;
  2034. struct ip_sf_list *psf;
  2035. int rv = 0;
  2036. for_each_pmc_rcu(in_dev, im) {
  2037. if (im->multiaddr == mc_addr)
  2038. break;
  2039. }
  2040. if (im && proto == IPPROTO_IGMP) {
  2041. rv = 1;
  2042. } else if (im) {
  2043. if (src_addr) {
  2044. for (psf=im->sources; psf; psf=psf->sf_next) {
  2045. if (psf->sf_inaddr == src_addr)
  2046. break;
  2047. }
  2048. if (psf)
  2049. rv = psf->sf_count[MCAST_INCLUDE] ||
  2050. psf->sf_count[MCAST_EXCLUDE] !=
  2051. im->sfcount[MCAST_EXCLUDE];
  2052. else
  2053. rv = im->sfcount[MCAST_EXCLUDE] != 0;
  2054. } else
  2055. rv = 1; /* unspecified source; tentatively allow */
  2056. }
  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. im = rcu_dereference(in_dev->mc_list);
  2078. if (im) {
  2079. state->in_dev = in_dev;
  2080. break;
  2081. }
  2082. }
  2083. return im;
  2084. }
  2085. static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
  2086. {
  2087. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2088. im = rcu_dereference(im->next_rcu);
  2089. while (!im) {
  2090. state->dev = next_net_device_rcu(state->dev);
  2091. if (!state->dev) {
  2092. state->in_dev = NULL;
  2093. break;
  2094. }
  2095. state->in_dev = __in_dev_get_rcu(state->dev);
  2096. if (!state->in_dev)
  2097. continue;
  2098. im = rcu_dereference(state->in_dev->mc_list);
  2099. }
  2100. return im;
  2101. }
  2102. static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
  2103. {
  2104. struct ip_mc_list *im = igmp_mc_get_first(seq);
  2105. if (im)
  2106. while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
  2107. --pos;
  2108. return pos ? NULL : im;
  2109. }
  2110. static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
  2111. __acquires(rcu)
  2112. {
  2113. rcu_read_lock();
  2114. return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2115. }
  2116. static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2117. {
  2118. struct ip_mc_list *im;
  2119. if (v == SEQ_START_TOKEN)
  2120. im = igmp_mc_get_first(seq);
  2121. else
  2122. im = igmp_mc_get_next(seq, v);
  2123. ++*pos;
  2124. return im;
  2125. }
  2126. static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
  2127. __releases(rcu)
  2128. {
  2129. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2130. state->in_dev = NULL;
  2131. state->dev = NULL;
  2132. rcu_read_unlock();
  2133. }
  2134. static int igmp_mc_seq_show(struct seq_file *seq, void *v)
  2135. {
  2136. if (v == SEQ_START_TOKEN)
  2137. seq_puts(seq,
  2138. "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
  2139. else {
  2140. struct ip_mc_list *im = (struct ip_mc_list *)v;
  2141. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2142. char *querier;
  2143. #ifdef CONFIG_IP_MULTICAST
  2144. querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
  2145. IGMP_V2_SEEN(state->in_dev) ? "V2" :
  2146. "V3";
  2147. #else
  2148. querier = "NONE";
  2149. #endif
  2150. if (rcu_dereference(state->in_dev->mc_list) == im) {
  2151. seq_printf(seq, "%d\t%-10s: %5d %7s\n",
  2152. state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
  2153. }
  2154. seq_printf(seq,
  2155. "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
  2156. im->multiaddr, im->users,
  2157. im->tm_running, im->tm_running ?
  2158. jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
  2159. im->reporter);
  2160. }
  2161. return 0;
  2162. }
  2163. static const struct seq_operations igmp_mc_seq_ops = {
  2164. .start = igmp_mc_seq_start,
  2165. .next = igmp_mc_seq_next,
  2166. .stop = igmp_mc_seq_stop,
  2167. .show = igmp_mc_seq_show,
  2168. };
  2169. static int igmp_mc_seq_open(struct inode *inode, struct file *file)
  2170. {
  2171. return seq_open_net(inode, file, &igmp_mc_seq_ops,
  2172. sizeof(struct igmp_mc_iter_state));
  2173. }
  2174. static const struct file_operations igmp_mc_seq_fops = {
  2175. .owner = THIS_MODULE,
  2176. .open = igmp_mc_seq_open,
  2177. .read = seq_read,
  2178. .llseek = seq_lseek,
  2179. .release = seq_release_net,
  2180. };
  2181. struct igmp_mcf_iter_state {
  2182. struct seq_net_private p;
  2183. struct net_device *dev;
  2184. struct in_device *idev;
  2185. struct ip_mc_list *im;
  2186. };
  2187. #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
  2188. static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
  2189. {
  2190. struct net *net = seq_file_net(seq);
  2191. struct ip_sf_list *psf = NULL;
  2192. struct ip_mc_list *im = NULL;
  2193. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2194. state->idev = NULL;
  2195. state->im = NULL;
  2196. for_each_netdev_rcu(net, state->dev) {
  2197. struct in_device *idev;
  2198. idev = __in_dev_get_rcu(state->dev);
  2199. if (unlikely(idev == NULL))
  2200. continue;
  2201. im = rcu_dereference(idev->mc_list);
  2202. if (likely(im != NULL)) {
  2203. spin_lock_bh(&im->lock);
  2204. psf = im->sources;
  2205. if (likely(psf != NULL)) {
  2206. state->im = im;
  2207. state->idev = idev;
  2208. break;
  2209. }
  2210. spin_unlock_bh(&im->lock);
  2211. }
  2212. }
  2213. return psf;
  2214. }
  2215. static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
  2216. {
  2217. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2218. psf = psf->sf_next;
  2219. while (!psf) {
  2220. spin_unlock_bh(&state->im->lock);
  2221. state->im = state->im->next;
  2222. while (!state->im) {
  2223. state->dev = next_net_device_rcu(state->dev);
  2224. if (!state->dev) {
  2225. state->idev = NULL;
  2226. goto out;
  2227. }
  2228. state->idev = __in_dev_get_rcu(state->dev);
  2229. if (!state->idev)
  2230. continue;
  2231. state->im = rcu_dereference(state->idev->mc_list);
  2232. }
  2233. if (!state->im)
  2234. break;
  2235. spin_lock_bh(&state->im->lock);
  2236. psf = state->im->sources;
  2237. }
  2238. out:
  2239. return psf;
  2240. }
  2241. static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
  2242. {
  2243. struct ip_sf_list *psf = igmp_mcf_get_first(seq);
  2244. if (psf)
  2245. while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
  2246. --pos;
  2247. return pos ? NULL : psf;
  2248. }
  2249. static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
  2250. __acquires(rcu)
  2251. {
  2252. rcu_read_lock();
  2253. return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2254. }
  2255. static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2256. {
  2257. struct ip_sf_list *psf;
  2258. if (v == SEQ_START_TOKEN)
  2259. psf = igmp_mcf_get_first(seq);
  2260. else
  2261. psf = igmp_mcf_get_next(seq, v);
  2262. ++*pos;
  2263. return psf;
  2264. }
  2265. static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
  2266. __releases(rcu)
  2267. {
  2268. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2269. if (likely(state->im != NULL)) {
  2270. spin_unlock_bh(&state->im->lock);
  2271. state->im = NULL;
  2272. }
  2273. state->idev = NULL;
  2274. state->dev = NULL;
  2275. rcu_read_unlock();
  2276. }
  2277. static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
  2278. {
  2279. struct ip_sf_list *psf = (struct ip_sf_list *)v;
  2280. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2281. if (v == SEQ_START_TOKEN) {
  2282. seq_printf(seq,
  2283. "%3s %6s "
  2284. "%10s %10s %6s %6s\n", "Idx",
  2285. "Device", "MCA",
  2286. "SRC", "INC", "EXC");
  2287. } else {
  2288. seq_printf(seq,
  2289. "%3d %6.6s 0x%08x "
  2290. "0x%08x %6lu %6lu\n",
  2291. state->dev->ifindex, state->dev->name,
  2292. ntohl(state->im->multiaddr),
  2293. ntohl(psf->sf_inaddr),
  2294. psf->sf_count[MCAST_INCLUDE],
  2295. psf->sf_count[MCAST_EXCLUDE]);
  2296. }
  2297. return 0;
  2298. }
  2299. static const struct seq_operations igmp_mcf_seq_ops = {
  2300. .start = igmp_mcf_seq_start,
  2301. .next = igmp_mcf_seq_next,
  2302. .stop = igmp_mcf_seq_stop,
  2303. .show = igmp_mcf_seq_show,
  2304. };
  2305. static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
  2306. {
  2307. return seq_open_net(inode, file, &igmp_mcf_seq_ops,
  2308. sizeof(struct igmp_mcf_iter_state));
  2309. }
  2310. static const struct file_operations igmp_mcf_seq_fops = {
  2311. .owner = THIS_MODULE,
  2312. .open = igmp_mcf_seq_open,
  2313. .read = seq_read,
  2314. .llseek = seq_lseek,
  2315. .release = seq_release_net,
  2316. };
  2317. static int __net_init igmp_net_init(struct net *net)
  2318. {
  2319. struct proc_dir_entry *pde;
  2320. pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
  2321. if (!pde)
  2322. goto out_igmp;
  2323. pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
  2324. if (!pde)
  2325. goto out_mcfilter;
  2326. return 0;
  2327. out_mcfilter:
  2328. proc_net_remove(net, "igmp");
  2329. out_igmp:
  2330. return -ENOMEM;
  2331. }
  2332. static void __net_exit igmp_net_exit(struct net *net)
  2333. {
  2334. proc_net_remove(net, "mcfilter");
  2335. proc_net_remove(net, "igmp");
  2336. }
  2337. static struct pernet_operations igmp_net_ops = {
  2338. .init = igmp_net_init,
  2339. .exit = igmp_net_exit,
  2340. };
  2341. int __init igmp_mc_proc_init(void)
  2342. {
  2343. return register_pernet_subsys(&igmp_net_ops);
  2344. }
  2345. #endif