igmp.c 66 KB

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