igmp.c 64 KB

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