igmp.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800
  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 <linux/types.h>
  76. #include <linux/kernel.h>
  77. #include <linux/jiffies.h>
  78. #include <linux/string.h>
  79. #include <linux/socket.h>
  80. #include <linux/sockios.h>
  81. #include <linux/in.h>
  82. #include <linux/inet.h>
  83. #include <linux/netdevice.h>
  84. #include <linux/skbuff.h>
  85. #include <linux/inetdevice.h>
  86. #include <linux/igmp.h>
  87. #include <linux/if_arp.h>
  88. #include <linux/rtnetlink.h>
  89. #include <linux/times.h>
  90. #include <linux/pkt_sched.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 <net/inet_common.h>
  99. #include <linux/netfilter_ipv4.h>
  100. #ifdef CONFIG_IP_MROUTE
  101. #include <linux/mroute.h>
  102. #endif
  103. #ifdef CONFIG_PROC_FS
  104. #include <linux/proc_fs.h>
  105. #include <linux/seq_file.h>
  106. #endif
  107. #define IP_MAX_MEMBERSHIPS 20
  108. #define IP_MAX_MSF 10
  109. #ifdef CONFIG_IP_MULTICAST
  110. /* Parameter names and values are taken from igmp-v2-06 draft */
  111. #define IGMP_V1_ROUTER_PRESENT_TIMEOUT (400*HZ)
  112. #define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ)
  113. #define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
  114. #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
  115. #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
  116. #define IGMP_QUERY_ROBUSTNESS_VARIABLE 2
  117. #define IGMP_INITIAL_REPORT_DELAY (1)
  118. /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
  119. * IGMP specs require to report membership immediately after
  120. * joining a group, but we delay the first report by a
  121. * small interval. It seems more natural and still does not
  122. * contradict to specs provided this delay is small enough.
  123. */
  124. #define IGMP_V1_SEEN(in_dev) \
  125. (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
  126. IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
  127. ((in_dev)->mr_v1_seen && \
  128. time_before(jiffies, (in_dev)->mr_v1_seen)))
  129. #define IGMP_V2_SEEN(in_dev) \
  130. (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
  131. IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
  132. ((in_dev)->mr_v2_seen && \
  133. time_before(jiffies, (in_dev)->mr_v2_seen)))
  134. static int unsolicited_report_interval(struct in_device *in_dev)
  135. {
  136. int interval_ms, interval_jiffies;
  137. if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
  138. interval_ms = IN_DEV_CONF_GET(
  139. in_dev,
  140. IGMPV2_UNSOLICITED_REPORT_INTERVAL);
  141. else /* v3 */
  142. interval_ms = IN_DEV_CONF_GET(
  143. in_dev,
  144. IGMPV3_UNSOLICITED_REPORT_INTERVAL);
  145. interval_jiffies = msecs_to_jiffies(interval_ms);
  146. /* _timer functions can't handle a delay of 0 jiffies so ensure
  147. * we always return a positive value.
  148. */
  149. if (interval_jiffies <= 0)
  150. interval_jiffies = 1;
  151. return interval_jiffies;
  152. }
  153. static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
  154. static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
  155. static void igmpv3_clear_delrec(struct in_device *in_dev);
  156. static int sf_setstate(struct ip_mc_list *pmc);
  157. static void sf_markstate(struct ip_mc_list *pmc);
  158. #endif
  159. static void ip_mc_clear_src(struct ip_mc_list *pmc);
  160. static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
  161. int sfcount, __be32 *psfsrc, int delta);
  162. static void ip_ma_put(struct ip_mc_list *im)
  163. {
  164. if (atomic_dec_and_test(&im->refcnt)) {
  165. in_dev_put(im->interface);
  166. kfree_rcu(im, rcu);
  167. }
  168. }
  169. #define for_each_pmc_rcu(in_dev, pmc) \
  170. for (pmc = rcu_dereference(in_dev->mc_list); \
  171. pmc != NULL; \
  172. pmc = rcu_dereference(pmc->next_rcu))
  173. #define for_each_pmc_rtnl(in_dev, pmc) \
  174. for (pmc = rtnl_dereference(in_dev->mc_list); \
  175. pmc != NULL; \
  176. pmc = rtnl_dereference(pmc->next_rcu))
  177. #ifdef CONFIG_IP_MULTICAST
  178. /*
  179. * Timer management
  180. */
  181. static void igmp_stop_timer(struct ip_mc_list *im)
  182. {
  183. spin_lock_bh(&im->lock);
  184. if (del_timer(&im->timer))
  185. atomic_dec(&im->refcnt);
  186. im->tm_running = 0;
  187. im->reporter = 0;
  188. im->unsolicit_count = 0;
  189. spin_unlock_bh(&im->lock);
  190. }
  191. /* It must be called with locked im->lock */
  192. static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
  193. {
  194. int tv = prandom_u32() % max_delay;
  195. im->tm_running = 1;
  196. if (!mod_timer(&im->timer, jiffies+tv+2))
  197. atomic_inc(&im->refcnt);
  198. }
  199. static void igmp_gq_start_timer(struct in_device *in_dev)
  200. {
  201. int tv = prandom_u32() % in_dev->mr_maxdelay;
  202. in_dev->mr_gq_running = 1;
  203. if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
  204. in_dev_hold(in_dev);
  205. }
  206. static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
  207. {
  208. int tv = prandom_u32() % delay;
  209. if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
  210. in_dev_hold(in_dev);
  211. }
  212. static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
  213. {
  214. spin_lock_bh(&im->lock);
  215. im->unsolicit_count = 0;
  216. if (del_timer(&im->timer)) {
  217. if ((long)(im->timer.expires-jiffies) < max_delay) {
  218. add_timer(&im->timer);
  219. im->tm_running = 1;
  220. spin_unlock_bh(&im->lock);
  221. return;
  222. }
  223. atomic_dec(&im->refcnt);
  224. }
  225. igmp_start_timer(im, max_delay);
  226. spin_unlock_bh(&im->lock);
  227. }
  228. /*
  229. * Send an IGMP report.
  230. */
  231. #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
  232. static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
  233. int gdeleted, int sdeleted)
  234. {
  235. switch (type) {
  236. case IGMPV3_MODE_IS_INCLUDE:
  237. case IGMPV3_MODE_IS_EXCLUDE:
  238. if (gdeleted || sdeleted)
  239. return 0;
  240. if (!(pmc->gsquery && !psf->sf_gsresp)) {
  241. if (pmc->sfmode == MCAST_INCLUDE)
  242. return 1;
  243. /* don't include if this source is excluded
  244. * in all filters
  245. */
  246. if (psf->sf_count[MCAST_INCLUDE])
  247. return type == IGMPV3_MODE_IS_INCLUDE;
  248. return pmc->sfcount[MCAST_EXCLUDE] ==
  249. psf->sf_count[MCAST_EXCLUDE];
  250. }
  251. return 0;
  252. case IGMPV3_CHANGE_TO_INCLUDE:
  253. if (gdeleted || sdeleted)
  254. return 0;
  255. return psf->sf_count[MCAST_INCLUDE] != 0;
  256. case IGMPV3_CHANGE_TO_EXCLUDE:
  257. if (gdeleted || sdeleted)
  258. return 0;
  259. if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
  260. psf->sf_count[MCAST_INCLUDE])
  261. return 0;
  262. return pmc->sfcount[MCAST_EXCLUDE] ==
  263. psf->sf_count[MCAST_EXCLUDE];
  264. case IGMPV3_ALLOW_NEW_SOURCES:
  265. if (gdeleted || !psf->sf_crcount)
  266. return 0;
  267. return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
  268. case IGMPV3_BLOCK_OLD_SOURCES:
  269. if (pmc->sfmode == MCAST_INCLUDE)
  270. return gdeleted || (psf->sf_crcount && sdeleted);
  271. return psf->sf_crcount && !gdeleted && !sdeleted;
  272. }
  273. return 0;
  274. }
  275. static int
  276. igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
  277. {
  278. struct ip_sf_list *psf;
  279. int scount = 0;
  280. for (psf = pmc->sources; psf; psf = psf->sf_next) {
  281. if (!is_in(pmc, psf, type, gdeleted, sdeleted))
  282. continue;
  283. scount++;
  284. }
  285. return scount;
  286. }
  287. static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
  288. {
  289. struct sk_buff *skb;
  290. struct rtable *rt;
  291. struct iphdr *pip;
  292. struct igmpv3_report *pig;
  293. struct net *net = dev_net(dev);
  294. struct flowi4 fl4;
  295. int hlen = LL_RESERVED_SPACE(dev);
  296. int tlen = dev->needed_tailroom;
  297. unsigned int size = mtu;
  298. while (1) {
  299. skb = alloc_skb(size + hlen + tlen,
  300. GFP_ATOMIC | __GFP_NOWARN);
  301. if (skb)
  302. break;
  303. size >>= 1;
  304. if (size < 256)
  305. return NULL;
  306. }
  307. skb->priority = TC_PRIO_CONTROL;
  308. rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
  309. 0, 0,
  310. IPPROTO_IGMP, 0, dev->ifindex);
  311. if (IS_ERR(rt)) {
  312. kfree_skb(skb);
  313. return NULL;
  314. }
  315. skb_dst_set(skb, &rt->dst);
  316. skb->dev = dev;
  317. skb->reserved_tailroom = skb_end_offset(skb) -
  318. min(mtu, skb_end_offset(skb));
  319. skb_reserve(skb, hlen);
  320. skb_reset_network_header(skb);
  321. pip = ip_hdr(skb);
  322. skb_put(skb, sizeof(struct iphdr) + 4);
  323. pip->version = 4;
  324. pip->ihl = (sizeof(struct iphdr)+4)>>2;
  325. pip->tos = 0xc0;
  326. pip->frag_off = htons(IP_DF);
  327. pip->ttl = 1;
  328. pip->daddr = fl4.daddr;
  329. pip->saddr = fl4.saddr;
  330. pip->protocol = IPPROTO_IGMP;
  331. pip->tot_len = 0; /* filled in later */
  332. ip_select_ident(skb, NULL);
  333. ((u8 *)&pip[1])[0] = IPOPT_RA;
  334. ((u8 *)&pip[1])[1] = 4;
  335. ((u8 *)&pip[1])[2] = 0;
  336. ((u8 *)&pip[1])[3] = 0;
  337. skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
  338. skb_put(skb, sizeof(*pig));
  339. pig = igmpv3_report_hdr(skb);
  340. pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
  341. pig->resv1 = 0;
  342. pig->csum = 0;
  343. pig->resv2 = 0;
  344. pig->ngrec = 0;
  345. return skb;
  346. }
  347. static int igmpv3_sendpack(struct sk_buff *skb)
  348. {
  349. struct igmphdr *pig = igmp_hdr(skb);
  350. const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
  351. pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
  352. return ip_local_out(skb);
  353. }
  354. static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
  355. {
  356. return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
  357. }
  358. static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
  359. int type, struct igmpv3_grec **ppgr)
  360. {
  361. struct net_device *dev = pmc->interface->dev;
  362. struct igmpv3_report *pih;
  363. struct igmpv3_grec *pgr;
  364. if (!skb)
  365. skb = igmpv3_newpack(dev, dev->mtu);
  366. if (!skb)
  367. return NULL;
  368. pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
  369. pgr->grec_type = type;
  370. pgr->grec_auxwords = 0;
  371. pgr->grec_nsrcs = 0;
  372. pgr->grec_mca = pmc->multiaddr;
  373. pih = igmpv3_report_hdr(skb);
  374. pih->ngrec = htons(ntohs(pih->ngrec)+1);
  375. *ppgr = pgr;
  376. return skb;
  377. }
  378. #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
  379. static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
  380. int type, int gdeleted, int sdeleted)
  381. {
  382. struct net_device *dev = pmc->interface->dev;
  383. struct igmpv3_report *pih;
  384. struct igmpv3_grec *pgr = NULL;
  385. struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
  386. int scount, stotal, first, isquery, truncate;
  387. if (pmc->multiaddr == IGMP_ALL_HOSTS)
  388. return skb;
  389. isquery = type == IGMPV3_MODE_IS_INCLUDE ||
  390. type == IGMPV3_MODE_IS_EXCLUDE;
  391. truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
  392. type == IGMPV3_CHANGE_TO_EXCLUDE;
  393. stotal = scount = 0;
  394. psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
  395. if (!*psf_list)
  396. goto empty_source;
  397. pih = skb ? igmpv3_report_hdr(skb) : NULL;
  398. /* EX and TO_EX get a fresh packet, if needed */
  399. if (truncate) {
  400. if (pih && pih->ngrec &&
  401. AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
  402. if (skb)
  403. igmpv3_sendpack(skb);
  404. skb = igmpv3_newpack(dev, dev->mtu);
  405. }
  406. }
  407. first = 1;
  408. psf_prev = NULL;
  409. for (psf = *psf_list; psf; psf = psf_next) {
  410. __be32 *psrc;
  411. psf_next = psf->sf_next;
  412. if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
  413. psf_prev = psf;
  414. continue;
  415. }
  416. /* clear marks on query responses */
  417. if (isquery)
  418. psf->sf_gsresp = 0;
  419. if (AVAILABLE(skb) < sizeof(__be32) +
  420. first*sizeof(struct igmpv3_grec)) {
  421. if (truncate && !first)
  422. break; /* truncate these */
  423. if (pgr)
  424. pgr->grec_nsrcs = htons(scount);
  425. if (skb)
  426. igmpv3_sendpack(skb);
  427. skb = igmpv3_newpack(dev, dev->mtu);
  428. first = 1;
  429. scount = 0;
  430. }
  431. if (first) {
  432. skb = add_grhead(skb, pmc, type, &pgr);
  433. first = 0;
  434. }
  435. if (!skb)
  436. return NULL;
  437. psrc = (__be32 *)skb_put(skb, sizeof(__be32));
  438. *psrc = psf->sf_inaddr;
  439. scount++; stotal++;
  440. if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
  441. type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
  442. psf->sf_crcount--;
  443. if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
  444. if (psf_prev)
  445. psf_prev->sf_next = psf->sf_next;
  446. else
  447. *psf_list = psf->sf_next;
  448. kfree(psf);
  449. continue;
  450. }
  451. }
  452. psf_prev = psf;
  453. }
  454. empty_source:
  455. if (!stotal) {
  456. if (type == IGMPV3_ALLOW_NEW_SOURCES ||
  457. type == IGMPV3_BLOCK_OLD_SOURCES)
  458. return skb;
  459. if (pmc->crcount || isquery) {
  460. /* make sure we have room for group header */
  461. if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
  462. igmpv3_sendpack(skb);
  463. skb = NULL; /* add_grhead will get a new one */
  464. }
  465. skb = add_grhead(skb, pmc, type, &pgr);
  466. }
  467. }
  468. if (pgr)
  469. pgr->grec_nsrcs = htons(scount);
  470. if (isquery)
  471. pmc->gsquery = 0; /* clear query state on report */
  472. return skb;
  473. }
  474. static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
  475. {
  476. struct sk_buff *skb = NULL;
  477. int type;
  478. if (!pmc) {
  479. rcu_read_lock();
  480. for_each_pmc_rcu(in_dev, pmc) {
  481. if (pmc->multiaddr == IGMP_ALL_HOSTS)
  482. continue;
  483. spin_lock_bh(&pmc->lock);
  484. if (pmc->sfcount[MCAST_EXCLUDE])
  485. type = IGMPV3_MODE_IS_EXCLUDE;
  486. else
  487. type = IGMPV3_MODE_IS_INCLUDE;
  488. skb = add_grec(skb, pmc, type, 0, 0);
  489. spin_unlock_bh(&pmc->lock);
  490. }
  491. rcu_read_unlock();
  492. } else {
  493. spin_lock_bh(&pmc->lock);
  494. if (pmc->sfcount[MCAST_EXCLUDE])
  495. type = IGMPV3_MODE_IS_EXCLUDE;
  496. else
  497. type = IGMPV3_MODE_IS_INCLUDE;
  498. skb = add_grec(skb, pmc, type, 0, 0);
  499. spin_unlock_bh(&pmc->lock);
  500. }
  501. if (!skb)
  502. return 0;
  503. return igmpv3_sendpack(skb);
  504. }
  505. /*
  506. * remove zero-count source records from a source filter list
  507. */
  508. static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
  509. {
  510. struct ip_sf_list *psf_prev, *psf_next, *psf;
  511. psf_prev = NULL;
  512. for (psf = *ppsf; psf; psf = psf_next) {
  513. psf_next = psf->sf_next;
  514. if (psf->sf_crcount == 0) {
  515. if (psf_prev)
  516. psf_prev->sf_next = psf->sf_next;
  517. else
  518. *ppsf = psf->sf_next;
  519. kfree(psf);
  520. } else
  521. psf_prev = psf;
  522. }
  523. }
  524. static void igmpv3_send_cr(struct in_device *in_dev)
  525. {
  526. struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
  527. struct sk_buff *skb = NULL;
  528. int type, dtype;
  529. rcu_read_lock();
  530. spin_lock_bh(&in_dev->mc_tomb_lock);
  531. /* deleted MCA's */
  532. pmc_prev = NULL;
  533. for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
  534. pmc_next = pmc->next;
  535. if (pmc->sfmode == MCAST_INCLUDE) {
  536. type = IGMPV3_BLOCK_OLD_SOURCES;
  537. dtype = IGMPV3_BLOCK_OLD_SOURCES;
  538. skb = add_grec(skb, pmc, type, 1, 0);
  539. skb = add_grec(skb, pmc, dtype, 1, 1);
  540. }
  541. if (pmc->crcount) {
  542. if (pmc->sfmode == MCAST_EXCLUDE) {
  543. type = IGMPV3_CHANGE_TO_INCLUDE;
  544. skb = add_grec(skb, pmc, type, 1, 0);
  545. }
  546. pmc->crcount--;
  547. if (pmc->crcount == 0) {
  548. igmpv3_clear_zeros(&pmc->tomb);
  549. igmpv3_clear_zeros(&pmc->sources);
  550. }
  551. }
  552. if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
  553. if (pmc_prev)
  554. pmc_prev->next = pmc_next;
  555. else
  556. in_dev->mc_tomb = pmc_next;
  557. in_dev_put(pmc->interface);
  558. kfree(pmc);
  559. } else
  560. pmc_prev = pmc;
  561. }
  562. spin_unlock_bh(&in_dev->mc_tomb_lock);
  563. /* change recs */
  564. for_each_pmc_rcu(in_dev, pmc) {
  565. spin_lock_bh(&pmc->lock);
  566. if (pmc->sfcount[MCAST_EXCLUDE]) {
  567. type = IGMPV3_BLOCK_OLD_SOURCES;
  568. dtype = IGMPV3_ALLOW_NEW_SOURCES;
  569. } else {
  570. type = IGMPV3_ALLOW_NEW_SOURCES;
  571. dtype = IGMPV3_BLOCK_OLD_SOURCES;
  572. }
  573. skb = add_grec(skb, pmc, type, 0, 0);
  574. skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
  575. /* filter mode changes */
  576. if (pmc->crcount) {
  577. if (pmc->sfmode == MCAST_EXCLUDE)
  578. type = IGMPV3_CHANGE_TO_EXCLUDE;
  579. else
  580. type = IGMPV3_CHANGE_TO_INCLUDE;
  581. skb = add_grec(skb, pmc, type, 0, 0);
  582. pmc->crcount--;
  583. }
  584. spin_unlock_bh(&pmc->lock);
  585. }
  586. rcu_read_unlock();
  587. if (!skb)
  588. return;
  589. (void) igmpv3_sendpack(skb);
  590. }
  591. static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
  592. int type)
  593. {
  594. struct sk_buff *skb;
  595. struct iphdr *iph;
  596. struct igmphdr *ih;
  597. struct rtable *rt;
  598. struct net_device *dev = in_dev->dev;
  599. struct net *net = dev_net(dev);
  600. __be32 group = pmc ? pmc->multiaddr : 0;
  601. struct flowi4 fl4;
  602. __be32 dst;
  603. int hlen, tlen;
  604. if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
  605. return igmpv3_send_report(in_dev, pmc);
  606. else if (type == IGMP_HOST_LEAVE_MESSAGE)
  607. dst = IGMP_ALL_ROUTER;
  608. else
  609. dst = group;
  610. rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
  611. 0, 0,
  612. IPPROTO_IGMP, 0, dev->ifindex);
  613. if (IS_ERR(rt))
  614. return -1;
  615. hlen = LL_RESERVED_SPACE(dev);
  616. tlen = dev->needed_tailroom;
  617. skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
  618. if (skb == NULL) {
  619. ip_rt_put(rt);
  620. return -1;
  621. }
  622. skb->priority = TC_PRIO_CONTROL;
  623. skb_dst_set(skb, &rt->dst);
  624. skb_reserve(skb, hlen);
  625. skb_reset_network_header(skb);
  626. iph = ip_hdr(skb);
  627. skb_put(skb, sizeof(struct iphdr) + 4);
  628. iph->version = 4;
  629. iph->ihl = (sizeof(struct iphdr)+4)>>2;
  630. iph->tos = 0xc0;
  631. iph->frag_off = htons(IP_DF);
  632. iph->ttl = 1;
  633. iph->daddr = dst;
  634. iph->saddr = fl4.saddr;
  635. iph->protocol = IPPROTO_IGMP;
  636. ip_select_ident(skb, NULL);
  637. ((u8 *)&iph[1])[0] = IPOPT_RA;
  638. ((u8 *)&iph[1])[1] = 4;
  639. ((u8 *)&iph[1])[2] = 0;
  640. ((u8 *)&iph[1])[3] = 0;
  641. ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
  642. ih->type = type;
  643. ih->code = 0;
  644. ih->csum = 0;
  645. ih->group = group;
  646. ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
  647. return ip_local_out(skb);
  648. }
  649. static void igmp_gq_timer_expire(unsigned long data)
  650. {
  651. struct in_device *in_dev = (struct in_device *)data;
  652. in_dev->mr_gq_running = 0;
  653. igmpv3_send_report(in_dev, NULL);
  654. in_dev_put(in_dev);
  655. }
  656. static void igmp_ifc_timer_expire(unsigned long data)
  657. {
  658. struct in_device *in_dev = (struct in_device *)data;
  659. igmpv3_send_cr(in_dev);
  660. if (in_dev->mr_ifc_count) {
  661. in_dev->mr_ifc_count--;
  662. igmp_ifc_start_timer(in_dev,
  663. unsolicited_report_interval(in_dev));
  664. }
  665. in_dev_put(in_dev);
  666. }
  667. static void igmp_ifc_event(struct in_device *in_dev)
  668. {
  669. if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
  670. return;
  671. in_dev->mr_ifc_count = in_dev->mr_qrv ?: sysctl_igmp_qrv;
  672. igmp_ifc_start_timer(in_dev, 1);
  673. }
  674. static void igmp_timer_expire(unsigned long data)
  675. {
  676. struct ip_mc_list *im = (struct ip_mc_list *)data;
  677. struct in_device *in_dev = im->interface;
  678. spin_lock(&im->lock);
  679. im->tm_running = 0;
  680. if (im->unsolicit_count) {
  681. im->unsolicit_count--;
  682. igmp_start_timer(im, unsolicited_report_interval(in_dev));
  683. }
  684. im->reporter = 1;
  685. spin_unlock(&im->lock);
  686. if (IGMP_V1_SEEN(in_dev))
  687. igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
  688. else if (IGMP_V2_SEEN(in_dev))
  689. igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
  690. else
  691. igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
  692. ip_ma_put(im);
  693. }
  694. /* mark EXCLUDE-mode sources */
  695. static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
  696. {
  697. struct ip_sf_list *psf;
  698. int i, scount;
  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. /* skip inactive filters */
  705. if (psf->sf_count[MCAST_INCLUDE] ||
  706. pmc->sfcount[MCAST_EXCLUDE] !=
  707. psf->sf_count[MCAST_EXCLUDE])
  708. break;
  709. if (srcs[i] == psf->sf_inaddr) {
  710. scount++;
  711. break;
  712. }
  713. }
  714. }
  715. pmc->gsquery = 0;
  716. if (scount == nsrcs) /* all sources excluded */
  717. return 0;
  718. return 1;
  719. }
  720. static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
  721. {
  722. struct ip_sf_list *psf;
  723. int i, scount;
  724. if (pmc->sfmode == MCAST_EXCLUDE)
  725. return igmp_xmarksources(pmc, nsrcs, srcs);
  726. /* mark INCLUDE-mode sources */
  727. scount = 0;
  728. for (psf = pmc->sources; psf; psf = psf->sf_next) {
  729. if (scount == nsrcs)
  730. break;
  731. for (i = 0; i < nsrcs; i++)
  732. if (srcs[i] == psf->sf_inaddr) {
  733. psf->sf_gsresp = 1;
  734. scount++;
  735. break;
  736. }
  737. }
  738. if (!scount) {
  739. pmc->gsquery = 0;
  740. return 0;
  741. }
  742. pmc->gsquery = 1;
  743. return 1;
  744. }
  745. /* return true if packet was dropped */
  746. static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
  747. {
  748. struct ip_mc_list *im;
  749. /* Timers are only set for non-local groups */
  750. if (group == IGMP_ALL_HOSTS)
  751. return false;
  752. rcu_read_lock();
  753. for_each_pmc_rcu(in_dev, im) {
  754. if (im->multiaddr == group) {
  755. igmp_stop_timer(im);
  756. break;
  757. }
  758. }
  759. rcu_read_unlock();
  760. return false;
  761. }
  762. /* return true if packet was dropped */
  763. static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
  764. int len)
  765. {
  766. struct igmphdr *ih = igmp_hdr(skb);
  767. struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
  768. struct ip_mc_list *im;
  769. __be32 group = ih->group;
  770. int max_delay;
  771. int mark = 0;
  772. if (len == 8) {
  773. if (ih->code == 0) {
  774. /* Alas, old v1 router presents here. */
  775. max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
  776. in_dev->mr_v1_seen = jiffies +
  777. IGMP_V1_ROUTER_PRESENT_TIMEOUT;
  778. group = 0;
  779. } else {
  780. /* v2 router present */
  781. max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
  782. in_dev->mr_v2_seen = jiffies +
  783. IGMP_V2_ROUTER_PRESENT_TIMEOUT;
  784. }
  785. /* cancel the interface change timer */
  786. in_dev->mr_ifc_count = 0;
  787. if (del_timer(&in_dev->mr_ifc_timer))
  788. __in_dev_put(in_dev);
  789. /* clear deleted report items */
  790. igmpv3_clear_delrec(in_dev);
  791. } else if (len < 12) {
  792. return true; /* ignore bogus packet; freed by caller */
  793. } else if (IGMP_V1_SEEN(in_dev)) {
  794. /* This is a v3 query with v1 queriers present */
  795. max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
  796. group = 0;
  797. } else if (IGMP_V2_SEEN(in_dev)) {
  798. /* this is a v3 query with v2 queriers present;
  799. * Interpretation of the max_delay code is problematic here.
  800. * A real v2 host would use ih_code directly, while v3 has a
  801. * different encoding. We use the v3 encoding as more likely
  802. * to be intended in a v3 query.
  803. */
  804. max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
  805. if (!max_delay)
  806. max_delay = 1; /* can't mod w/ 0 */
  807. } else { /* v3 */
  808. if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
  809. return true;
  810. ih3 = igmpv3_query_hdr(skb);
  811. if (ih3->nsrcs) {
  812. if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
  813. + ntohs(ih3->nsrcs)*sizeof(__be32)))
  814. return true;
  815. ih3 = igmpv3_query_hdr(skb);
  816. }
  817. max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
  818. if (!max_delay)
  819. max_delay = 1; /* can't mod w/ 0 */
  820. in_dev->mr_maxdelay = max_delay;
  821. if (ih3->qrv)
  822. in_dev->mr_qrv = ih3->qrv;
  823. if (!group) { /* general query */
  824. if (ih3->nsrcs)
  825. return true; /* no sources allowed */
  826. igmp_gq_start_timer(in_dev);
  827. return false;
  828. }
  829. /* mark sources to include, if group & source-specific */
  830. mark = ih3->nsrcs != 0;
  831. }
  832. /*
  833. * - Start the timers in all of our membership records
  834. * that the query applies to for the interface on
  835. * which the query arrived excl. those that belong
  836. * to a "local" group (224.0.0.X)
  837. * - For timers already running check if they need to
  838. * be reset.
  839. * - Use the igmp->igmp_code field as the maximum
  840. * delay possible
  841. */
  842. rcu_read_lock();
  843. for_each_pmc_rcu(in_dev, im) {
  844. int changed;
  845. if (group && group != im->multiaddr)
  846. continue;
  847. if (im->multiaddr == IGMP_ALL_HOSTS)
  848. continue;
  849. spin_lock_bh(&im->lock);
  850. if (im->tm_running)
  851. im->gsquery = im->gsquery && mark;
  852. else
  853. im->gsquery = mark;
  854. changed = !im->gsquery ||
  855. igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
  856. spin_unlock_bh(&im->lock);
  857. if (changed)
  858. igmp_mod_timer(im, max_delay);
  859. }
  860. rcu_read_unlock();
  861. return false;
  862. }
  863. /* called in rcu_read_lock() section */
  864. int igmp_rcv(struct sk_buff *skb)
  865. {
  866. /* This basically follows the spec line by line -- see RFC1112 */
  867. struct igmphdr *ih;
  868. struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
  869. int len = skb->len;
  870. bool dropped = true;
  871. if (in_dev == NULL)
  872. goto drop;
  873. if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
  874. goto drop;
  875. if (skb_checksum_simple_validate(skb))
  876. goto drop;
  877. ih = igmp_hdr(skb);
  878. switch (ih->type) {
  879. case IGMP_HOST_MEMBERSHIP_QUERY:
  880. dropped = igmp_heard_query(in_dev, skb, len);
  881. break;
  882. case IGMP_HOST_MEMBERSHIP_REPORT:
  883. case IGMPV2_HOST_MEMBERSHIP_REPORT:
  884. /* Is it our report looped back? */
  885. if (rt_is_output_route(skb_rtable(skb)))
  886. break;
  887. /* don't rely on MC router hearing unicast reports */
  888. if (skb->pkt_type == PACKET_MULTICAST ||
  889. skb->pkt_type == PACKET_BROADCAST)
  890. dropped = igmp_heard_report(in_dev, ih->group);
  891. break;
  892. case IGMP_PIM:
  893. #ifdef CONFIG_IP_PIMSM_V1
  894. return pim_rcv_v1(skb);
  895. #endif
  896. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  897. case IGMP_DVMRP:
  898. case IGMP_TRACE:
  899. case IGMP_HOST_LEAVE_MESSAGE:
  900. case IGMP_MTRACE:
  901. case IGMP_MTRACE_RESP:
  902. break;
  903. default:
  904. break;
  905. }
  906. drop:
  907. if (dropped)
  908. kfree_skb(skb);
  909. else
  910. consume_skb(skb);
  911. return 0;
  912. }
  913. #endif
  914. /*
  915. * Add a filter to a device
  916. */
  917. static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
  918. {
  919. char buf[MAX_ADDR_LEN];
  920. struct net_device *dev = in_dev->dev;
  921. /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
  922. We will get multicast token leakage, when IFF_MULTICAST
  923. is changed. This check should be done in ndo_set_rx_mode
  924. routine. Something sort of:
  925. if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
  926. --ANK
  927. */
  928. if (arp_mc_map(addr, buf, dev, 0) == 0)
  929. dev_mc_add(dev, buf);
  930. }
  931. /*
  932. * Remove a filter from a device
  933. */
  934. static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
  935. {
  936. char buf[MAX_ADDR_LEN];
  937. struct net_device *dev = in_dev->dev;
  938. if (arp_mc_map(addr, buf, dev, 0) == 0)
  939. dev_mc_del(dev, buf);
  940. }
  941. #ifdef CONFIG_IP_MULTICAST
  942. /*
  943. * deleted ip_mc_list manipulation
  944. */
  945. static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
  946. {
  947. struct ip_mc_list *pmc;
  948. /* this is an "ip_mc_list" for convenience; only the fields below
  949. * are actually used. In particular, the refcnt and users are not
  950. * used for management of the delete list. Using the same structure
  951. * for deleted items allows change reports to use common code with
  952. * non-deleted or query-response MCA's.
  953. */
  954. pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
  955. if (!pmc)
  956. return;
  957. spin_lock_bh(&im->lock);
  958. pmc->interface = im->interface;
  959. in_dev_hold(in_dev);
  960. pmc->multiaddr = im->multiaddr;
  961. pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
  962. pmc->sfmode = im->sfmode;
  963. if (pmc->sfmode == MCAST_INCLUDE) {
  964. struct ip_sf_list *psf;
  965. pmc->tomb = im->tomb;
  966. pmc->sources = im->sources;
  967. im->tomb = im->sources = NULL;
  968. for (psf = pmc->sources; psf; psf = psf->sf_next)
  969. psf->sf_crcount = pmc->crcount;
  970. }
  971. spin_unlock_bh(&im->lock);
  972. spin_lock_bh(&in_dev->mc_tomb_lock);
  973. pmc->next = in_dev->mc_tomb;
  974. in_dev->mc_tomb = pmc;
  975. spin_unlock_bh(&in_dev->mc_tomb_lock);
  976. }
  977. static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
  978. {
  979. struct ip_mc_list *pmc, *pmc_prev;
  980. struct ip_sf_list *psf, *psf_next;
  981. spin_lock_bh(&in_dev->mc_tomb_lock);
  982. pmc_prev = NULL;
  983. for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
  984. if (pmc->multiaddr == multiaddr)
  985. break;
  986. pmc_prev = pmc;
  987. }
  988. if (pmc) {
  989. if (pmc_prev)
  990. pmc_prev->next = pmc->next;
  991. else
  992. in_dev->mc_tomb = pmc->next;
  993. }
  994. spin_unlock_bh(&in_dev->mc_tomb_lock);
  995. if (pmc) {
  996. for (psf = pmc->tomb; psf; psf = psf_next) {
  997. psf_next = psf->sf_next;
  998. kfree(psf);
  999. }
  1000. in_dev_put(pmc->interface);
  1001. kfree(pmc);
  1002. }
  1003. }
  1004. static void igmpv3_clear_delrec(struct in_device *in_dev)
  1005. {
  1006. struct ip_mc_list *pmc, *nextpmc;
  1007. spin_lock_bh(&in_dev->mc_tomb_lock);
  1008. pmc = in_dev->mc_tomb;
  1009. in_dev->mc_tomb = NULL;
  1010. spin_unlock_bh(&in_dev->mc_tomb_lock);
  1011. for (; pmc; pmc = nextpmc) {
  1012. nextpmc = pmc->next;
  1013. ip_mc_clear_src(pmc);
  1014. in_dev_put(pmc->interface);
  1015. kfree(pmc);
  1016. }
  1017. /* clear dead sources, too */
  1018. rcu_read_lock();
  1019. for_each_pmc_rcu(in_dev, pmc) {
  1020. struct ip_sf_list *psf, *psf_next;
  1021. spin_lock_bh(&pmc->lock);
  1022. psf = pmc->tomb;
  1023. pmc->tomb = NULL;
  1024. spin_unlock_bh(&pmc->lock);
  1025. for (; psf; psf = psf_next) {
  1026. psf_next = psf->sf_next;
  1027. kfree(psf);
  1028. }
  1029. }
  1030. rcu_read_unlock();
  1031. }
  1032. #endif
  1033. static void igmp_group_dropped(struct ip_mc_list *im)
  1034. {
  1035. struct in_device *in_dev = im->interface;
  1036. #ifdef CONFIG_IP_MULTICAST
  1037. int reporter;
  1038. #endif
  1039. if (im->loaded) {
  1040. im->loaded = 0;
  1041. ip_mc_filter_del(in_dev, im->multiaddr);
  1042. }
  1043. #ifdef CONFIG_IP_MULTICAST
  1044. if (im->multiaddr == IGMP_ALL_HOSTS)
  1045. return;
  1046. reporter = im->reporter;
  1047. igmp_stop_timer(im);
  1048. if (!in_dev->dead) {
  1049. if (IGMP_V1_SEEN(in_dev))
  1050. return;
  1051. if (IGMP_V2_SEEN(in_dev)) {
  1052. if (reporter)
  1053. igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
  1054. return;
  1055. }
  1056. /* IGMPv3 */
  1057. igmpv3_add_delrec(in_dev, im);
  1058. igmp_ifc_event(in_dev);
  1059. }
  1060. #endif
  1061. }
  1062. static void igmp_group_added(struct ip_mc_list *im)
  1063. {
  1064. struct in_device *in_dev = im->interface;
  1065. if (im->loaded == 0) {
  1066. im->loaded = 1;
  1067. ip_mc_filter_add(in_dev, im->multiaddr);
  1068. }
  1069. #ifdef CONFIG_IP_MULTICAST
  1070. if (im->multiaddr == IGMP_ALL_HOSTS)
  1071. return;
  1072. if (in_dev->dead)
  1073. return;
  1074. if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
  1075. spin_lock_bh(&im->lock);
  1076. igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
  1077. spin_unlock_bh(&im->lock);
  1078. return;
  1079. }
  1080. /* else, v3 */
  1081. im->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
  1082. igmp_ifc_event(in_dev);
  1083. #endif
  1084. }
  1085. /*
  1086. * Multicast list managers
  1087. */
  1088. static u32 ip_mc_hash(const struct ip_mc_list *im)
  1089. {
  1090. return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
  1091. }
  1092. static void ip_mc_hash_add(struct in_device *in_dev,
  1093. struct ip_mc_list *im)
  1094. {
  1095. struct ip_mc_list __rcu **mc_hash;
  1096. u32 hash;
  1097. mc_hash = rtnl_dereference(in_dev->mc_hash);
  1098. if (mc_hash) {
  1099. hash = ip_mc_hash(im);
  1100. im->next_hash = mc_hash[hash];
  1101. rcu_assign_pointer(mc_hash[hash], im);
  1102. return;
  1103. }
  1104. /* do not use a hash table for small number of items */
  1105. if (in_dev->mc_count < 4)
  1106. return;
  1107. mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
  1108. GFP_KERNEL);
  1109. if (!mc_hash)
  1110. return;
  1111. for_each_pmc_rtnl(in_dev, im) {
  1112. hash = ip_mc_hash(im);
  1113. im->next_hash = mc_hash[hash];
  1114. RCU_INIT_POINTER(mc_hash[hash], im);
  1115. }
  1116. rcu_assign_pointer(in_dev->mc_hash, mc_hash);
  1117. }
  1118. static void ip_mc_hash_remove(struct in_device *in_dev,
  1119. struct ip_mc_list *im)
  1120. {
  1121. struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
  1122. struct ip_mc_list *aux;
  1123. if (!mc_hash)
  1124. return;
  1125. mc_hash += ip_mc_hash(im);
  1126. while ((aux = rtnl_dereference(*mc_hash)) != im)
  1127. mc_hash = &aux->next_hash;
  1128. *mc_hash = im->next_hash;
  1129. }
  1130. /*
  1131. * A socket has joined a multicast group on device dev.
  1132. */
  1133. void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
  1134. {
  1135. struct ip_mc_list *im;
  1136. ASSERT_RTNL();
  1137. for_each_pmc_rtnl(in_dev, im) {
  1138. if (im->multiaddr == addr) {
  1139. im->users++;
  1140. ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
  1141. goto out;
  1142. }
  1143. }
  1144. im = kzalloc(sizeof(*im), GFP_KERNEL);
  1145. if (!im)
  1146. goto out;
  1147. im->users = 1;
  1148. im->interface = in_dev;
  1149. in_dev_hold(in_dev);
  1150. im->multiaddr = addr;
  1151. /* initial mode is (EX, empty) */
  1152. im->sfmode = MCAST_EXCLUDE;
  1153. im->sfcount[MCAST_EXCLUDE] = 1;
  1154. atomic_set(&im->refcnt, 1);
  1155. spin_lock_init(&im->lock);
  1156. #ifdef CONFIG_IP_MULTICAST
  1157. setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im);
  1158. im->unsolicit_count = sysctl_igmp_qrv;
  1159. #endif
  1160. im->next_rcu = in_dev->mc_list;
  1161. in_dev->mc_count++;
  1162. rcu_assign_pointer(in_dev->mc_list, im);
  1163. ip_mc_hash_add(in_dev, im);
  1164. #ifdef CONFIG_IP_MULTICAST
  1165. igmpv3_del_delrec(in_dev, im->multiaddr);
  1166. #endif
  1167. igmp_group_added(im);
  1168. if (!in_dev->dead)
  1169. ip_rt_multicast_event(in_dev);
  1170. out:
  1171. return;
  1172. }
  1173. EXPORT_SYMBOL(ip_mc_inc_group);
  1174. /*
  1175. * Resend IGMP JOIN report; used by netdev notifier.
  1176. */
  1177. static void ip_mc_rejoin_groups(struct in_device *in_dev)
  1178. {
  1179. #ifdef CONFIG_IP_MULTICAST
  1180. struct ip_mc_list *im;
  1181. int type;
  1182. ASSERT_RTNL();
  1183. for_each_pmc_rtnl(in_dev, im) {
  1184. if (im->multiaddr == IGMP_ALL_HOSTS)
  1185. continue;
  1186. /* a failover is happening and switches
  1187. * must be notified immediately
  1188. */
  1189. if (IGMP_V1_SEEN(in_dev))
  1190. type = IGMP_HOST_MEMBERSHIP_REPORT;
  1191. else if (IGMP_V2_SEEN(in_dev))
  1192. type = IGMPV2_HOST_MEMBERSHIP_REPORT;
  1193. else
  1194. type = IGMPV3_HOST_MEMBERSHIP_REPORT;
  1195. igmp_send_report(in_dev, im, type);
  1196. }
  1197. #endif
  1198. }
  1199. /*
  1200. * A socket has left a multicast group on device dev
  1201. */
  1202. void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
  1203. {
  1204. struct ip_mc_list *i;
  1205. struct ip_mc_list __rcu **ip;
  1206. ASSERT_RTNL();
  1207. for (ip = &in_dev->mc_list;
  1208. (i = rtnl_dereference(*ip)) != NULL;
  1209. ip = &i->next_rcu) {
  1210. if (i->multiaddr == addr) {
  1211. if (--i->users == 0) {
  1212. ip_mc_hash_remove(in_dev, i);
  1213. *ip = i->next_rcu;
  1214. in_dev->mc_count--;
  1215. igmp_group_dropped(i);
  1216. ip_mc_clear_src(i);
  1217. if (!in_dev->dead)
  1218. ip_rt_multicast_event(in_dev);
  1219. ip_ma_put(i);
  1220. return;
  1221. }
  1222. break;
  1223. }
  1224. }
  1225. }
  1226. EXPORT_SYMBOL(ip_mc_dec_group);
  1227. /* Device changing type */
  1228. void ip_mc_unmap(struct in_device *in_dev)
  1229. {
  1230. struct ip_mc_list *pmc;
  1231. ASSERT_RTNL();
  1232. for_each_pmc_rtnl(in_dev, pmc)
  1233. igmp_group_dropped(pmc);
  1234. }
  1235. void ip_mc_remap(struct in_device *in_dev)
  1236. {
  1237. struct ip_mc_list *pmc;
  1238. ASSERT_RTNL();
  1239. for_each_pmc_rtnl(in_dev, pmc)
  1240. igmp_group_added(pmc);
  1241. }
  1242. /* Device going down */
  1243. void ip_mc_down(struct in_device *in_dev)
  1244. {
  1245. struct ip_mc_list *pmc;
  1246. ASSERT_RTNL();
  1247. for_each_pmc_rtnl(in_dev, pmc)
  1248. igmp_group_dropped(pmc);
  1249. #ifdef CONFIG_IP_MULTICAST
  1250. in_dev->mr_ifc_count = 0;
  1251. if (del_timer(&in_dev->mr_ifc_timer))
  1252. __in_dev_put(in_dev);
  1253. in_dev->mr_gq_running = 0;
  1254. if (del_timer(&in_dev->mr_gq_timer))
  1255. __in_dev_put(in_dev);
  1256. igmpv3_clear_delrec(in_dev);
  1257. #endif
  1258. ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
  1259. }
  1260. void ip_mc_init_dev(struct in_device *in_dev)
  1261. {
  1262. ASSERT_RTNL();
  1263. #ifdef CONFIG_IP_MULTICAST
  1264. setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
  1265. (unsigned long)in_dev);
  1266. setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
  1267. (unsigned long)in_dev);
  1268. in_dev->mr_qrv = sysctl_igmp_qrv;
  1269. #endif
  1270. spin_lock_init(&in_dev->mc_tomb_lock);
  1271. }
  1272. /* Device going up */
  1273. void ip_mc_up(struct in_device *in_dev)
  1274. {
  1275. struct ip_mc_list *pmc;
  1276. ASSERT_RTNL();
  1277. #ifdef CONFIG_IP_MULTICAST
  1278. in_dev->mr_qrv = sysctl_igmp_qrv;
  1279. #endif
  1280. ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
  1281. for_each_pmc_rtnl(in_dev, pmc)
  1282. igmp_group_added(pmc);
  1283. }
  1284. /*
  1285. * Device is about to be destroyed: clean up.
  1286. */
  1287. void ip_mc_destroy_dev(struct in_device *in_dev)
  1288. {
  1289. struct ip_mc_list *i;
  1290. ASSERT_RTNL();
  1291. /* Deactivate timers */
  1292. ip_mc_down(in_dev);
  1293. while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
  1294. in_dev->mc_list = i->next_rcu;
  1295. in_dev->mc_count--;
  1296. /* We've dropped the groups in ip_mc_down already */
  1297. ip_mc_clear_src(i);
  1298. ip_ma_put(i);
  1299. }
  1300. }
  1301. /* RTNL is locked */
  1302. static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
  1303. {
  1304. struct net_device *dev = NULL;
  1305. struct in_device *idev = NULL;
  1306. if (imr->imr_ifindex) {
  1307. idev = inetdev_by_index(net, imr->imr_ifindex);
  1308. return idev;
  1309. }
  1310. if (imr->imr_address.s_addr) {
  1311. dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
  1312. if (!dev)
  1313. return NULL;
  1314. }
  1315. if (!dev) {
  1316. struct rtable *rt = ip_route_output(net,
  1317. imr->imr_multiaddr.s_addr,
  1318. 0, 0, 0);
  1319. if (!IS_ERR(rt)) {
  1320. dev = rt->dst.dev;
  1321. ip_rt_put(rt);
  1322. }
  1323. }
  1324. if (dev) {
  1325. imr->imr_ifindex = dev->ifindex;
  1326. idev = __in_dev_get_rtnl(dev);
  1327. }
  1328. return idev;
  1329. }
  1330. /*
  1331. * Join a socket to a group
  1332. */
  1333. int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
  1334. int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
  1335. #ifdef CONFIG_IP_MULTICAST
  1336. int sysctl_igmp_qrv __read_mostly = IGMP_QUERY_ROBUSTNESS_VARIABLE;
  1337. #endif
  1338. static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
  1339. __be32 *psfsrc)
  1340. {
  1341. struct ip_sf_list *psf, *psf_prev;
  1342. int rv = 0;
  1343. psf_prev = NULL;
  1344. for (psf = pmc->sources; psf; psf = psf->sf_next) {
  1345. if (psf->sf_inaddr == *psfsrc)
  1346. break;
  1347. psf_prev = psf;
  1348. }
  1349. if (!psf || psf->sf_count[sfmode] == 0) {
  1350. /* source filter not found, or count wrong => bug */
  1351. return -ESRCH;
  1352. }
  1353. psf->sf_count[sfmode]--;
  1354. if (psf->sf_count[sfmode] == 0) {
  1355. ip_rt_multicast_event(pmc->interface);
  1356. }
  1357. if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
  1358. #ifdef CONFIG_IP_MULTICAST
  1359. struct in_device *in_dev = pmc->interface;
  1360. #endif
  1361. /* no more filters for this source */
  1362. if (psf_prev)
  1363. psf_prev->sf_next = psf->sf_next;
  1364. else
  1365. pmc->sources = psf->sf_next;
  1366. #ifdef CONFIG_IP_MULTICAST
  1367. if (psf->sf_oldin &&
  1368. !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
  1369. psf->sf_crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
  1370. psf->sf_next = pmc->tomb;
  1371. pmc->tomb = psf;
  1372. rv = 1;
  1373. } else
  1374. #endif
  1375. kfree(psf);
  1376. }
  1377. return rv;
  1378. }
  1379. #ifndef CONFIG_IP_MULTICAST
  1380. #define igmp_ifc_event(x) do { } while (0)
  1381. #endif
  1382. static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
  1383. int sfcount, __be32 *psfsrc, int delta)
  1384. {
  1385. struct ip_mc_list *pmc;
  1386. int changerec = 0;
  1387. int i, err;
  1388. if (!in_dev)
  1389. return -ENODEV;
  1390. rcu_read_lock();
  1391. for_each_pmc_rcu(in_dev, pmc) {
  1392. if (*pmca == pmc->multiaddr)
  1393. break;
  1394. }
  1395. if (!pmc) {
  1396. /* MCA not found?? bug */
  1397. rcu_read_unlock();
  1398. return -ESRCH;
  1399. }
  1400. spin_lock_bh(&pmc->lock);
  1401. rcu_read_unlock();
  1402. #ifdef CONFIG_IP_MULTICAST
  1403. sf_markstate(pmc);
  1404. #endif
  1405. if (!delta) {
  1406. err = -EINVAL;
  1407. if (!pmc->sfcount[sfmode])
  1408. goto out_unlock;
  1409. pmc->sfcount[sfmode]--;
  1410. }
  1411. err = 0;
  1412. for (i = 0; i < sfcount; i++) {
  1413. int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1414. changerec |= rv > 0;
  1415. if (!err && rv < 0)
  1416. err = rv;
  1417. }
  1418. if (pmc->sfmode == MCAST_EXCLUDE &&
  1419. pmc->sfcount[MCAST_EXCLUDE] == 0 &&
  1420. pmc->sfcount[MCAST_INCLUDE]) {
  1421. #ifdef CONFIG_IP_MULTICAST
  1422. struct ip_sf_list *psf;
  1423. #endif
  1424. /* filter mode change */
  1425. pmc->sfmode = MCAST_INCLUDE;
  1426. #ifdef CONFIG_IP_MULTICAST
  1427. pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
  1428. in_dev->mr_ifc_count = pmc->crcount;
  1429. for (psf = pmc->sources; psf; psf = psf->sf_next)
  1430. psf->sf_crcount = 0;
  1431. igmp_ifc_event(pmc->interface);
  1432. } else if (sf_setstate(pmc) || changerec) {
  1433. igmp_ifc_event(pmc->interface);
  1434. #endif
  1435. }
  1436. out_unlock:
  1437. spin_unlock_bh(&pmc->lock);
  1438. return err;
  1439. }
  1440. /*
  1441. * Add multicast single-source filter to the interface list
  1442. */
  1443. static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
  1444. __be32 *psfsrc)
  1445. {
  1446. struct ip_sf_list *psf, *psf_prev;
  1447. psf_prev = NULL;
  1448. for (psf = pmc->sources; psf; psf = psf->sf_next) {
  1449. if (psf->sf_inaddr == *psfsrc)
  1450. break;
  1451. psf_prev = psf;
  1452. }
  1453. if (!psf) {
  1454. psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
  1455. if (!psf)
  1456. return -ENOBUFS;
  1457. psf->sf_inaddr = *psfsrc;
  1458. if (psf_prev) {
  1459. psf_prev->sf_next = psf;
  1460. } else
  1461. pmc->sources = psf;
  1462. }
  1463. psf->sf_count[sfmode]++;
  1464. if (psf->sf_count[sfmode] == 1) {
  1465. ip_rt_multicast_event(pmc->interface);
  1466. }
  1467. return 0;
  1468. }
  1469. #ifdef CONFIG_IP_MULTICAST
  1470. static void sf_markstate(struct ip_mc_list *pmc)
  1471. {
  1472. struct ip_sf_list *psf;
  1473. int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
  1474. for (psf = pmc->sources; psf; psf = psf->sf_next)
  1475. if (pmc->sfcount[MCAST_EXCLUDE]) {
  1476. psf->sf_oldin = mca_xcount ==
  1477. psf->sf_count[MCAST_EXCLUDE] &&
  1478. !psf->sf_count[MCAST_INCLUDE];
  1479. } else
  1480. psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
  1481. }
  1482. static int sf_setstate(struct ip_mc_list *pmc)
  1483. {
  1484. struct ip_sf_list *psf, *dpsf;
  1485. int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
  1486. int qrv = pmc->interface->mr_qrv;
  1487. int new_in, rv;
  1488. rv = 0;
  1489. for (psf = pmc->sources; psf; psf = psf->sf_next) {
  1490. if (pmc->sfcount[MCAST_EXCLUDE]) {
  1491. new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
  1492. !psf->sf_count[MCAST_INCLUDE];
  1493. } else
  1494. new_in = psf->sf_count[MCAST_INCLUDE] != 0;
  1495. if (new_in) {
  1496. if (!psf->sf_oldin) {
  1497. struct ip_sf_list *prev = NULL;
  1498. for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
  1499. if (dpsf->sf_inaddr == psf->sf_inaddr)
  1500. break;
  1501. prev = dpsf;
  1502. }
  1503. if (dpsf) {
  1504. if (prev)
  1505. prev->sf_next = dpsf->sf_next;
  1506. else
  1507. pmc->tomb = dpsf->sf_next;
  1508. kfree(dpsf);
  1509. }
  1510. psf->sf_crcount = qrv;
  1511. rv++;
  1512. }
  1513. } else if (psf->sf_oldin) {
  1514. psf->sf_crcount = 0;
  1515. /*
  1516. * add or update "delete" records if an active filter
  1517. * is now inactive
  1518. */
  1519. for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
  1520. if (dpsf->sf_inaddr == psf->sf_inaddr)
  1521. break;
  1522. if (!dpsf) {
  1523. dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
  1524. if (!dpsf)
  1525. continue;
  1526. *dpsf = *psf;
  1527. /* pmc->lock held by callers */
  1528. dpsf->sf_next = pmc->tomb;
  1529. pmc->tomb = dpsf;
  1530. }
  1531. dpsf->sf_crcount = qrv;
  1532. rv++;
  1533. }
  1534. }
  1535. return rv;
  1536. }
  1537. #endif
  1538. /*
  1539. * Add multicast source filter list to the interface list
  1540. */
  1541. static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
  1542. int sfcount, __be32 *psfsrc, int delta)
  1543. {
  1544. struct ip_mc_list *pmc;
  1545. int isexclude;
  1546. int i, err;
  1547. if (!in_dev)
  1548. return -ENODEV;
  1549. rcu_read_lock();
  1550. for_each_pmc_rcu(in_dev, pmc) {
  1551. if (*pmca == pmc->multiaddr)
  1552. break;
  1553. }
  1554. if (!pmc) {
  1555. /* MCA not found?? bug */
  1556. rcu_read_unlock();
  1557. return -ESRCH;
  1558. }
  1559. spin_lock_bh(&pmc->lock);
  1560. rcu_read_unlock();
  1561. #ifdef CONFIG_IP_MULTICAST
  1562. sf_markstate(pmc);
  1563. #endif
  1564. isexclude = pmc->sfmode == MCAST_EXCLUDE;
  1565. if (!delta)
  1566. pmc->sfcount[sfmode]++;
  1567. err = 0;
  1568. for (i = 0; i < sfcount; i++) {
  1569. err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
  1570. if (err)
  1571. break;
  1572. }
  1573. if (err) {
  1574. int j;
  1575. if (!delta)
  1576. pmc->sfcount[sfmode]--;
  1577. for (j = 0; j < i; j++)
  1578. (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
  1579. } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
  1580. #ifdef CONFIG_IP_MULTICAST
  1581. struct ip_sf_list *psf;
  1582. in_dev = pmc->interface;
  1583. #endif
  1584. /* filter mode change */
  1585. if (pmc->sfcount[MCAST_EXCLUDE])
  1586. pmc->sfmode = MCAST_EXCLUDE;
  1587. else if (pmc->sfcount[MCAST_INCLUDE])
  1588. pmc->sfmode = MCAST_INCLUDE;
  1589. #ifdef CONFIG_IP_MULTICAST
  1590. /* else no filters; keep old mode for reports */
  1591. pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv;
  1592. in_dev->mr_ifc_count = pmc->crcount;
  1593. for (psf = pmc->sources; psf; psf = psf->sf_next)
  1594. psf->sf_crcount = 0;
  1595. igmp_ifc_event(in_dev);
  1596. } else if (sf_setstate(pmc)) {
  1597. igmp_ifc_event(in_dev);
  1598. #endif
  1599. }
  1600. spin_unlock_bh(&pmc->lock);
  1601. return err;
  1602. }
  1603. static void ip_mc_clear_src(struct ip_mc_list *pmc)
  1604. {
  1605. struct ip_sf_list *psf, *nextpsf;
  1606. for (psf = pmc->tomb; psf; psf = nextpsf) {
  1607. nextpsf = psf->sf_next;
  1608. kfree(psf);
  1609. }
  1610. pmc->tomb = NULL;
  1611. for (psf = pmc->sources; psf; psf = nextpsf) {
  1612. nextpsf = psf->sf_next;
  1613. kfree(psf);
  1614. }
  1615. pmc->sources = NULL;
  1616. pmc->sfmode = MCAST_EXCLUDE;
  1617. pmc->sfcount[MCAST_INCLUDE] = 0;
  1618. pmc->sfcount[MCAST_EXCLUDE] = 1;
  1619. }
  1620. /* Join a multicast group
  1621. */
  1622. int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
  1623. {
  1624. __be32 addr = imr->imr_multiaddr.s_addr;
  1625. struct ip_mc_socklist *iml, *i;
  1626. struct in_device *in_dev;
  1627. struct inet_sock *inet = inet_sk(sk);
  1628. struct net *net = sock_net(sk);
  1629. int ifindex;
  1630. int count = 0;
  1631. int err;
  1632. ASSERT_RTNL();
  1633. if (!ipv4_is_multicast(addr))
  1634. return -EINVAL;
  1635. in_dev = ip_mc_find_dev(net, imr);
  1636. if (!in_dev) {
  1637. err = -ENODEV;
  1638. goto done;
  1639. }
  1640. err = -EADDRINUSE;
  1641. ifindex = imr->imr_ifindex;
  1642. for_each_pmc_rtnl(inet, i) {
  1643. if (i->multi.imr_multiaddr.s_addr == addr &&
  1644. i->multi.imr_ifindex == ifindex)
  1645. goto done;
  1646. count++;
  1647. }
  1648. err = -ENOBUFS;
  1649. if (count >= sysctl_igmp_max_memberships)
  1650. goto done;
  1651. iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
  1652. if (iml == NULL)
  1653. goto done;
  1654. memcpy(&iml->multi, imr, sizeof(*imr));
  1655. iml->next_rcu = inet->mc_list;
  1656. iml->sflist = NULL;
  1657. iml->sfmode = MCAST_EXCLUDE;
  1658. rcu_assign_pointer(inet->mc_list, iml);
  1659. ip_mc_inc_group(in_dev, addr);
  1660. err = 0;
  1661. done:
  1662. return err;
  1663. }
  1664. EXPORT_SYMBOL(ip_mc_join_group);
  1665. static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
  1666. struct in_device *in_dev)
  1667. {
  1668. struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
  1669. int err;
  1670. if (psf == NULL) {
  1671. /* any-source empty exclude case */
  1672. return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
  1673. iml->sfmode, 0, NULL, 0);
  1674. }
  1675. err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
  1676. iml->sfmode, psf->sl_count, psf->sl_addr, 0);
  1677. RCU_INIT_POINTER(iml->sflist, NULL);
  1678. /* decrease mem now to avoid the memleak warning */
  1679. atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
  1680. kfree_rcu(psf, rcu);
  1681. return err;
  1682. }
  1683. int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
  1684. {
  1685. struct inet_sock *inet = inet_sk(sk);
  1686. struct ip_mc_socklist *iml;
  1687. struct ip_mc_socklist __rcu **imlp;
  1688. struct in_device *in_dev;
  1689. struct net *net = sock_net(sk);
  1690. __be32 group = imr->imr_multiaddr.s_addr;
  1691. u32 ifindex;
  1692. int ret = -EADDRNOTAVAIL;
  1693. ASSERT_RTNL();
  1694. in_dev = ip_mc_find_dev(net, imr);
  1695. if (!in_dev) {
  1696. ret = -ENODEV;
  1697. goto out;
  1698. }
  1699. ifindex = imr->imr_ifindex;
  1700. for (imlp = &inet->mc_list;
  1701. (iml = rtnl_dereference(*imlp)) != NULL;
  1702. imlp = &iml->next_rcu) {
  1703. if (iml->multi.imr_multiaddr.s_addr != group)
  1704. continue;
  1705. if (ifindex) {
  1706. if (iml->multi.imr_ifindex != ifindex)
  1707. continue;
  1708. } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
  1709. iml->multi.imr_address.s_addr)
  1710. continue;
  1711. (void) ip_mc_leave_src(sk, iml, in_dev);
  1712. *imlp = iml->next_rcu;
  1713. ip_mc_dec_group(in_dev, group);
  1714. /* decrease mem now to avoid the memleak warning */
  1715. atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
  1716. kfree_rcu(iml, rcu);
  1717. return 0;
  1718. }
  1719. out:
  1720. return ret;
  1721. }
  1722. EXPORT_SYMBOL(ip_mc_leave_group);
  1723. int ip_mc_source(int add, int omode, struct sock *sk, struct
  1724. ip_mreq_source *mreqs, int ifindex)
  1725. {
  1726. int err;
  1727. struct ip_mreqn imr;
  1728. __be32 addr = mreqs->imr_multiaddr;
  1729. struct ip_mc_socklist *pmc;
  1730. struct in_device *in_dev = NULL;
  1731. struct inet_sock *inet = inet_sk(sk);
  1732. struct ip_sf_socklist *psl;
  1733. struct net *net = sock_net(sk);
  1734. int leavegroup = 0;
  1735. int i, j, rv;
  1736. if (!ipv4_is_multicast(addr))
  1737. return -EINVAL;
  1738. ASSERT_RTNL();
  1739. imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
  1740. imr.imr_address.s_addr = mreqs->imr_interface;
  1741. imr.imr_ifindex = ifindex;
  1742. in_dev = ip_mc_find_dev(net, &imr);
  1743. if (!in_dev) {
  1744. err = -ENODEV;
  1745. goto done;
  1746. }
  1747. err = -EADDRNOTAVAIL;
  1748. for_each_pmc_rtnl(inet, pmc) {
  1749. if ((pmc->multi.imr_multiaddr.s_addr ==
  1750. imr.imr_multiaddr.s_addr) &&
  1751. (pmc->multi.imr_ifindex == imr.imr_ifindex))
  1752. break;
  1753. }
  1754. if (!pmc) { /* must have a prior join */
  1755. err = -EINVAL;
  1756. goto done;
  1757. }
  1758. /* if a source filter was set, must be the same mode as before */
  1759. if (pmc->sflist) {
  1760. if (pmc->sfmode != omode) {
  1761. err = -EINVAL;
  1762. goto done;
  1763. }
  1764. } else if (pmc->sfmode != omode) {
  1765. /* allow mode switches for empty-set filters */
  1766. ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
  1767. ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
  1768. NULL, 0);
  1769. pmc->sfmode = omode;
  1770. }
  1771. psl = rtnl_dereference(pmc->sflist);
  1772. if (!add) {
  1773. if (!psl)
  1774. goto done; /* err = -EADDRNOTAVAIL */
  1775. rv = !0;
  1776. for (i = 0; i < psl->sl_count; i++) {
  1777. rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
  1778. sizeof(__be32));
  1779. if (rv == 0)
  1780. break;
  1781. }
  1782. if (rv) /* source not found */
  1783. goto done; /* err = -EADDRNOTAVAIL */
  1784. /* special case - (INCLUDE, empty) == LEAVE_GROUP */
  1785. if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
  1786. leavegroup = 1;
  1787. goto done;
  1788. }
  1789. /* update the interface filter */
  1790. ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
  1791. &mreqs->imr_sourceaddr, 1);
  1792. for (j = i+1; j < psl->sl_count; j++)
  1793. psl->sl_addr[j-1] = psl->sl_addr[j];
  1794. psl->sl_count--;
  1795. err = 0;
  1796. goto done;
  1797. }
  1798. /* else, add a new source to the filter */
  1799. if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
  1800. err = -ENOBUFS;
  1801. goto done;
  1802. }
  1803. if (!psl || psl->sl_count == psl->sl_max) {
  1804. struct ip_sf_socklist *newpsl;
  1805. int count = IP_SFBLOCK;
  1806. if (psl)
  1807. count += psl->sl_max;
  1808. newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
  1809. if (!newpsl) {
  1810. err = -ENOBUFS;
  1811. goto done;
  1812. }
  1813. newpsl->sl_max = count;
  1814. newpsl->sl_count = count - IP_SFBLOCK;
  1815. if (psl) {
  1816. for (i = 0; i < psl->sl_count; i++)
  1817. newpsl->sl_addr[i] = psl->sl_addr[i];
  1818. /* decrease mem now to avoid the memleak warning */
  1819. atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
  1820. kfree_rcu(psl, rcu);
  1821. }
  1822. rcu_assign_pointer(pmc->sflist, newpsl);
  1823. psl = newpsl;
  1824. }
  1825. rv = 1; /* > 0 for insert logic below if sl_count is 0 */
  1826. for (i = 0; i < psl->sl_count; i++) {
  1827. rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
  1828. sizeof(__be32));
  1829. if (rv == 0)
  1830. break;
  1831. }
  1832. if (rv == 0) /* address already there is an error */
  1833. goto done;
  1834. for (j = psl->sl_count-1; j >= i; j--)
  1835. psl->sl_addr[j+1] = psl->sl_addr[j];
  1836. psl->sl_addr[i] = mreqs->imr_sourceaddr;
  1837. psl->sl_count++;
  1838. err = 0;
  1839. /* update the interface list */
  1840. ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
  1841. &mreqs->imr_sourceaddr, 1);
  1842. done:
  1843. if (leavegroup)
  1844. err = ip_mc_leave_group(sk, &imr);
  1845. return err;
  1846. }
  1847. int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
  1848. {
  1849. int err = 0;
  1850. struct ip_mreqn imr;
  1851. __be32 addr = msf->imsf_multiaddr;
  1852. struct ip_mc_socklist *pmc;
  1853. struct in_device *in_dev;
  1854. struct inet_sock *inet = inet_sk(sk);
  1855. struct ip_sf_socklist *newpsl, *psl;
  1856. struct net *net = sock_net(sk);
  1857. int leavegroup = 0;
  1858. if (!ipv4_is_multicast(addr))
  1859. return -EINVAL;
  1860. if (msf->imsf_fmode != MCAST_INCLUDE &&
  1861. msf->imsf_fmode != MCAST_EXCLUDE)
  1862. return -EINVAL;
  1863. ASSERT_RTNL();
  1864. imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
  1865. imr.imr_address.s_addr = msf->imsf_interface;
  1866. imr.imr_ifindex = ifindex;
  1867. in_dev = ip_mc_find_dev(net, &imr);
  1868. if (!in_dev) {
  1869. err = -ENODEV;
  1870. goto done;
  1871. }
  1872. /* special case - (INCLUDE, empty) == LEAVE_GROUP */
  1873. if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
  1874. leavegroup = 1;
  1875. goto done;
  1876. }
  1877. for_each_pmc_rtnl(inet, pmc) {
  1878. if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
  1879. pmc->multi.imr_ifindex == imr.imr_ifindex)
  1880. break;
  1881. }
  1882. if (!pmc) { /* must have a prior join */
  1883. err = -EINVAL;
  1884. goto done;
  1885. }
  1886. if (msf->imsf_numsrc) {
  1887. newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
  1888. GFP_KERNEL);
  1889. if (!newpsl) {
  1890. err = -ENOBUFS;
  1891. goto done;
  1892. }
  1893. newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
  1894. memcpy(newpsl->sl_addr, msf->imsf_slist,
  1895. msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
  1896. err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
  1897. msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
  1898. if (err) {
  1899. sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
  1900. goto done;
  1901. }
  1902. } else {
  1903. newpsl = NULL;
  1904. (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
  1905. msf->imsf_fmode, 0, NULL, 0);
  1906. }
  1907. psl = rtnl_dereference(pmc->sflist);
  1908. if (psl) {
  1909. (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
  1910. psl->sl_count, psl->sl_addr, 0);
  1911. /* decrease mem now to avoid the memleak warning */
  1912. atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
  1913. kfree_rcu(psl, rcu);
  1914. } else
  1915. (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
  1916. 0, NULL, 0);
  1917. rcu_assign_pointer(pmc->sflist, newpsl);
  1918. pmc->sfmode = msf->imsf_fmode;
  1919. err = 0;
  1920. done:
  1921. if (leavegroup)
  1922. err = ip_mc_leave_group(sk, &imr);
  1923. return err;
  1924. }
  1925. int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
  1926. struct ip_msfilter __user *optval, int __user *optlen)
  1927. {
  1928. int err, len, count, copycount;
  1929. struct ip_mreqn imr;
  1930. __be32 addr = msf->imsf_multiaddr;
  1931. struct ip_mc_socklist *pmc;
  1932. struct in_device *in_dev;
  1933. struct inet_sock *inet = inet_sk(sk);
  1934. struct ip_sf_socklist *psl;
  1935. struct net *net = sock_net(sk);
  1936. if (!ipv4_is_multicast(addr))
  1937. return -EINVAL;
  1938. rtnl_lock();
  1939. imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
  1940. imr.imr_address.s_addr = msf->imsf_interface;
  1941. imr.imr_ifindex = 0;
  1942. in_dev = ip_mc_find_dev(net, &imr);
  1943. if (!in_dev) {
  1944. err = -ENODEV;
  1945. goto done;
  1946. }
  1947. err = -EADDRNOTAVAIL;
  1948. for_each_pmc_rtnl(inet, pmc) {
  1949. if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
  1950. pmc->multi.imr_ifindex == imr.imr_ifindex)
  1951. break;
  1952. }
  1953. if (!pmc) /* must have a prior join */
  1954. goto done;
  1955. msf->imsf_fmode = pmc->sfmode;
  1956. psl = rtnl_dereference(pmc->sflist);
  1957. rtnl_unlock();
  1958. if (!psl) {
  1959. len = 0;
  1960. count = 0;
  1961. } else {
  1962. count = psl->sl_count;
  1963. }
  1964. copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
  1965. len = copycount * sizeof(psl->sl_addr[0]);
  1966. msf->imsf_numsrc = count;
  1967. if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
  1968. copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
  1969. return -EFAULT;
  1970. }
  1971. if (len &&
  1972. copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
  1973. return -EFAULT;
  1974. return 0;
  1975. done:
  1976. rtnl_unlock();
  1977. return err;
  1978. }
  1979. int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
  1980. struct group_filter __user *optval, int __user *optlen)
  1981. {
  1982. int err, i, count, copycount;
  1983. struct sockaddr_in *psin;
  1984. __be32 addr;
  1985. struct ip_mc_socklist *pmc;
  1986. struct inet_sock *inet = inet_sk(sk);
  1987. struct ip_sf_socklist *psl;
  1988. psin = (struct sockaddr_in *)&gsf->gf_group;
  1989. if (psin->sin_family != AF_INET)
  1990. return -EINVAL;
  1991. addr = psin->sin_addr.s_addr;
  1992. if (!ipv4_is_multicast(addr))
  1993. return -EINVAL;
  1994. rtnl_lock();
  1995. err = -EADDRNOTAVAIL;
  1996. for_each_pmc_rtnl(inet, pmc) {
  1997. if (pmc->multi.imr_multiaddr.s_addr == addr &&
  1998. pmc->multi.imr_ifindex == gsf->gf_interface)
  1999. break;
  2000. }
  2001. if (!pmc) /* must have a prior join */
  2002. goto done;
  2003. gsf->gf_fmode = pmc->sfmode;
  2004. psl = rtnl_dereference(pmc->sflist);
  2005. rtnl_unlock();
  2006. count = psl ? psl->sl_count : 0;
  2007. copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
  2008. gsf->gf_numsrc = count;
  2009. if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
  2010. copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
  2011. return -EFAULT;
  2012. }
  2013. for (i = 0; i < copycount; i++) {
  2014. struct sockaddr_storage ss;
  2015. psin = (struct sockaddr_in *)&ss;
  2016. memset(&ss, 0, sizeof(ss));
  2017. psin->sin_family = AF_INET;
  2018. psin->sin_addr.s_addr = psl->sl_addr[i];
  2019. if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
  2020. return -EFAULT;
  2021. }
  2022. return 0;
  2023. done:
  2024. rtnl_unlock();
  2025. return err;
  2026. }
  2027. /*
  2028. * check if a multicast source filter allows delivery for a given <src,dst,intf>
  2029. */
  2030. int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
  2031. {
  2032. struct inet_sock *inet = inet_sk(sk);
  2033. struct ip_mc_socklist *pmc;
  2034. struct ip_sf_socklist *psl;
  2035. int i;
  2036. int ret;
  2037. ret = 1;
  2038. if (!ipv4_is_multicast(loc_addr))
  2039. goto out;
  2040. rcu_read_lock();
  2041. for_each_pmc_rcu(inet, pmc) {
  2042. if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
  2043. pmc->multi.imr_ifindex == dif)
  2044. break;
  2045. }
  2046. ret = inet->mc_all;
  2047. if (!pmc)
  2048. goto unlock;
  2049. psl = rcu_dereference(pmc->sflist);
  2050. ret = (pmc->sfmode == MCAST_EXCLUDE);
  2051. if (!psl)
  2052. goto unlock;
  2053. for (i = 0; i < psl->sl_count; i++) {
  2054. if (psl->sl_addr[i] == rmt_addr)
  2055. break;
  2056. }
  2057. ret = 0;
  2058. if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
  2059. goto unlock;
  2060. if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
  2061. goto unlock;
  2062. ret = 1;
  2063. unlock:
  2064. rcu_read_unlock();
  2065. out:
  2066. return ret;
  2067. }
  2068. /*
  2069. * A socket is closing.
  2070. */
  2071. void ip_mc_drop_socket(struct sock *sk)
  2072. {
  2073. struct inet_sock *inet = inet_sk(sk);
  2074. struct ip_mc_socklist *iml;
  2075. struct net *net = sock_net(sk);
  2076. if (inet->mc_list == NULL)
  2077. return;
  2078. rtnl_lock();
  2079. while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
  2080. struct in_device *in_dev;
  2081. inet->mc_list = iml->next_rcu;
  2082. in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
  2083. (void) ip_mc_leave_src(sk, iml, in_dev);
  2084. if (in_dev != NULL)
  2085. ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
  2086. /* decrease mem now to avoid the memleak warning */
  2087. atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
  2088. kfree_rcu(iml, rcu);
  2089. }
  2090. rtnl_unlock();
  2091. }
  2092. /* called with rcu_read_lock() */
  2093. int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
  2094. {
  2095. struct ip_mc_list *im;
  2096. struct ip_mc_list __rcu **mc_hash;
  2097. struct ip_sf_list *psf;
  2098. int rv = 0;
  2099. mc_hash = rcu_dereference(in_dev->mc_hash);
  2100. if (mc_hash) {
  2101. u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
  2102. for (im = rcu_dereference(mc_hash[hash]);
  2103. im != NULL;
  2104. im = rcu_dereference(im->next_hash)) {
  2105. if (im->multiaddr == mc_addr)
  2106. break;
  2107. }
  2108. } else {
  2109. for_each_pmc_rcu(in_dev, im) {
  2110. if (im->multiaddr == mc_addr)
  2111. break;
  2112. }
  2113. }
  2114. if (im && proto == IPPROTO_IGMP) {
  2115. rv = 1;
  2116. } else if (im) {
  2117. if (src_addr) {
  2118. for (psf = im->sources; psf; psf = psf->sf_next) {
  2119. if (psf->sf_inaddr == src_addr)
  2120. break;
  2121. }
  2122. if (psf)
  2123. rv = psf->sf_count[MCAST_INCLUDE] ||
  2124. psf->sf_count[MCAST_EXCLUDE] !=
  2125. im->sfcount[MCAST_EXCLUDE];
  2126. else
  2127. rv = im->sfcount[MCAST_EXCLUDE] != 0;
  2128. } else
  2129. rv = 1; /* unspecified source; tentatively allow */
  2130. }
  2131. return rv;
  2132. }
  2133. #if defined(CONFIG_PROC_FS)
  2134. struct igmp_mc_iter_state {
  2135. struct seq_net_private p;
  2136. struct net_device *dev;
  2137. struct in_device *in_dev;
  2138. };
  2139. #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
  2140. static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
  2141. {
  2142. struct net *net = seq_file_net(seq);
  2143. struct ip_mc_list *im = NULL;
  2144. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2145. state->in_dev = NULL;
  2146. for_each_netdev_rcu(net, state->dev) {
  2147. struct in_device *in_dev;
  2148. in_dev = __in_dev_get_rcu(state->dev);
  2149. if (!in_dev)
  2150. continue;
  2151. im = rcu_dereference(in_dev->mc_list);
  2152. if (im) {
  2153. state->in_dev = in_dev;
  2154. break;
  2155. }
  2156. }
  2157. return im;
  2158. }
  2159. static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
  2160. {
  2161. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2162. im = rcu_dereference(im->next_rcu);
  2163. while (!im) {
  2164. state->dev = next_net_device_rcu(state->dev);
  2165. if (!state->dev) {
  2166. state->in_dev = NULL;
  2167. break;
  2168. }
  2169. state->in_dev = __in_dev_get_rcu(state->dev);
  2170. if (!state->in_dev)
  2171. continue;
  2172. im = rcu_dereference(state->in_dev->mc_list);
  2173. }
  2174. return im;
  2175. }
  2176. static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
  2177. {
  2178. struct ip_mc_list *im = igmp_mc_get_first(seq);
  2179. if (im)
  2180. while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
  2181. --pos;
  2182. return pos ? NULL : im;
  2183. }
  2184. static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
  2185. __acquires(rcu)
  2186. {
  2187. rcu_read_lock();
  2188. return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2189. }
  2190. static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2191. {
  2192. struct ip_mc_list *im;
  2193. if (v == SEQ_START_TOKEN)
  2194. im = igmp_mc_get_first(seq);
  2195. else
  2196. im = igmp_mc_get_next(seq, v);
  2197. ++*pos;
  2198. return im;
  2199. }
  2200. static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
  2201. __releases(rcu)
  2202. {
  2203. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2204. state->in_dev = NULL;
  2205. state->dev = NULL;
  2206. rcu_read_unlock();
  2207. }
  2208. static int igmp_mc_seq_show(struct seq_file *seq, void *v)
  2209. {
  2210. if (v == SEQ_START_TOKEN)
  2211. seq_puts(seq,
  2212. "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
  2213. else {
  2214. struct ip_mc_list *im = (struct ip_mc_list *)v;
  2215. struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
  2216. char *querier;
  2217. long delta;
  2218. #ifdef CONFIG_IP_MULTICAST
  2219. querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
  2220. IGMP_V2_SEEN(state->in_dev) ? "V2" :
  2221. "V3";
  2222. #else
  2223. querier = "NONE";
  2224. #endif
  2225. if (rcu_access_pointer(state->in_dev->mc_list) == im) {
  2226. seq_printf(seq, "%d\t%-10s: %5d %7s\n",
  2227. state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
  2228. }
  2229. delta = im->timer.expires - jiffies;
  2230. seq_printf(seq,
  2231. "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
  2232. im->multiaddr, im->users,
  2233. im->tm_running,
  2234. im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
  2235. im->reporter);
  2236. }
  2237. return 0;
  2238. }
  2239. static const struct seq_operations igmp_mc_seq_ops = {
  2240. .start = igmp_mc_seq_start,
  2241. .next = igmp_mc_seq_next,
  2242. .stop = igmp_mc_seq_stop,
  2243. .show = igmp_mc_seq_show,
  2244. };
  2245. static int igmp_mc_seq_open(struct inode *inode, struct file *file)
  2246. {
  2247. return seq_open_net(inode, file, &igmp_mc_seq_ops,
  2248. sizeof(struct igmp_mc_iter_state));
  2249. }
  2250. static const struct file_operations igmp_mc_seq_fops = {
  2251. .owner = THIS_MODULE,
  2252. .open = igmp_mc_seq_open,
  2253. .read = seq_read,
  2254. .llseek = seq_lseek,
  2255. .release = seq_release_net,
  2256. };
  2257. struct igmp_mcf_iter_state {
  2258. struct seq_net_private p;
  2259. struct net_device *dev;
  2260. struct in_device *idev;
  2261. struct ip_mc_list *im;
  2262. };
  2263. #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
  2264. static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
  2265. {
  2266. struct net *net = seq_file_net(seq);
  2267. struct ip_sf_list *psf = NULL;
  2268. struct ip_mc_list *im = NULL;
  2269. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2270. state->idev = NULL;
  2271. state->im = NULL;
  2272. for_each_netdev_rcu(net, state->dev) {
  2273. struct in_device *idev;
  2274. idev = __in_dev_get_rcu(state->dev);
  2275. if (unlikely(idev == NULL))
  2276. continue;
  2277. im = rcu_dereference(idev->mc_list);
  2278. if (likely(im != NULL)) {
  2279. spin_lock_bh(&im->lock);
  2280. psf = im->sources;
  2281. if (likely(psf != NULL)) {
  2282. state->im = im;
  2283. state->idev = idev;
  2284. break;
  2285. }
  2286. spin_unlock_bh(&im->lock);
  2287. }
  2288. }
  2289. return psf;
  2290. }
  2291. static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
  2292. {
  2293. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2294. psf = psf->sf_next;
  2295. while (!psf) {
  2296. spin_unlock_bh(&state->im->lock);
  2297. state->im = state->im->next;
  2298. while (!state->im) {
  2299. state->dev = next_net_device_rcu(state->dev);
  2300. if (!state->dev) {
  2301. state->idev = NULL;
  2302. goto out;
  2303. }
  2304. state->idev = __in_dev_get_rcu(state->dev);
  2305. if (!state->idev)
  2306. continue;
  2307. state->im = rcu_dereference(state->idev->mc_list);
  2308. }
  2309. if (!state->im)
  2310. break;
  2311. spin_lock_bh(&state->im->lock);
  2312. psf = state->im->sources;
  2313. }
  2314. out:
  2315. return psf;
  2316. }
  2317. static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
  2318. {
  2319. struct ip_sf_list *psf = igmp_mcf_get_first(seq);
  2320. if (psf)
  2321. while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
  2322. --pos;
  2323. return pos ? NULL : psf;
  2324. }
  2325. static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
  2326. __acquires(rcu)
  2327. {
  2328. rcu_read_lock();
  2329. return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2330. }
  2331. static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2332. {
  2333. struct ip_sf_list *psf;
  2334. if (v == SEQ_START_TOKEN)
  2335. psf = igmp_mcf_get_first(seq);
  2336. else
  2337. psf = igmp_mcf_get_next(seq, v);
  2338. ++*pos;
  2339. return psf;
  2340. }
  2341. static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
  2342. __releases(rcu)
  2343. {
  2344. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2345. if (likely(state->im != NULL)) {
  2346. spin_unlock_bh(&state->im->lock);
  2347. state->im = NULL;
  2348. }
  2349. state->idev = NULL;
  2350. state->dev = NULL;
  2351. rcu_read_unlock();
  2352. }
  2353. static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
  2354. {
  2355. struct ip_sf_list *psf = (struct ip_sf_list *)v;
  2356. struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
  2357. if (v == SEQ_START_TOKEN) {
  2358. seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
  2359. } else {
  2360. seq_printf(seq,
  2361. "%3d %6.6s 0x%08x "
  2362. "0x%08x %6lu %6lu\n",
  2363. state->dev->ifindex, state->dev->name,
  2364. ntohl(state->im->multiaddr),
  2365. ntohl(psf->sf_inaddr),
  2366. psf->sf_count[MCAST_INCLUDE],
  2367. psf->sf_count[MCAST_EXCLUDE]);
  2368. }
  2369. return 0;
  2370. }
  2371. static const struct seq_operations igmp_mcf_seq_ops = {
  2372. .start = igmp_mcf_seq_start,
  2373. .next = igmp_mcf_seq_next,
  2374. .stop = igmp_mcf_seq_stop,
  2375. .show = igmp_mcf_seq_show,
  2376. };
  2377. static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
  2378. {
  2379. return seq_open_net(inode, file, &igmp_mcf_seq_ops,
  2380. sizeof(struct igmp_mcf_iter_state));
  2381. }
  2382. static const struct file_operations igmp_mcf_seq_fops = {
  2383. .owner = THIS_MODULE,
  2384. .open = igmp_mcf_seq_open,
  2385. .read = seq_read,
  2386. .llseek = seq_lseek,
  2387. .release = seq_release_net,
  2388. };
  2389. static int __net_init igmp_net_init(struct net *net)
  2390. {
  2391. struct proc_dir_entry *pde;
  2392. int err;
  2393. pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
  2394. if (!pde)
  2395. goto out_igmp;
  2396. pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
  2397. &igmp_mcf_seq_fops);
  2398. if (!pde)
  2399. goto out_mcfilter;
  2400. err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
  2401. SOCK_DGRAM, 0, net);
  2402. if (err < 0) {
  2403. pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
  2404. err);
  2405. goto out_sock;
  2406. }
  2407. return 0;
  2408. out_sock:
  2409. remove_proc_entry("mcfilter", net->proc_net);
  2410. out_mcfilter:
  2411. remove_proc_entry("igmp", net->proc_net);
  2412. out_igmp:
  2413. return -ENOMEM;
  2414. }
  2415. static void __net_exit igmp_net_exit(struct net *net)
  2416. {
  2417. remove_proc_entry("mcfilter", net->proc_net);
  2418. remove_proc_entry("igmp", net->proc_net);
  2419. inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
  2420. }
  2421. static struct pernet_operations igmp_net_ops = {
  2422. .init = igmp_net_init,
  2423. .exit = igmp_net_exit,
  2424. };
  2425. #endif
  2426. static int igmp_netdev_event(struct notifier_block *this,
  2427. unsigned long event, void *ptr)
  2428. {
  2429. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  2430. struct in_device *in_dev;
  2431. switch (event) {
  2432. case NETDEV_RESEND_IGMP:
  2433. in_dev = __in_dev_get_rtnl(dev);
  2434. if (in_dev)
  2435. ip_mc_rejoin_groups(in_dev);
  2436. break;
  2437. default:
  2438. break;
  2439. }
  2440. return NOTIFY_DONE;
  2441. }
  2442. static struct notifier_block igmp_notifier = {
  2443. .notifier_call = igmp_netdev_event,
  2444. };
  2445. int __init igmp_mc_init(void)
  2446. {
  2447. #if defined(CONFIG_PROC_FS)
  2448. int err;
  2449. err = register_pernet_subsys(&igmp_net_ops);
  2450. if (err)
  2451. return err;
  2452. err = register_netdevice_notifier(&igmp_notifier);
  2453. if (err)
  2454. goto reg_notif_fail;
  2455. return 0;
  2456. reg_notif_fail:
  2457. unregister_pernet_subsys(&igmp_net_ops);
  2458. return err;
  2459. #else
  2460. return register_netdevice_notifier(&igmp_notifier);
  2461. #endif
  2462. }