mcast.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682
  1. /*
  2. * Multicast support for IPv6
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. /* Changes:
  16. *
  17. * yoshfuji : fix format of router-alert option
  18. * YOSHIFUJI Hideaki @USAGI:
  19. * Fixed source address for MLD message based on
  20. * <draft-ietf-magma-mld-source-05.txt>.
  21. * YOSHIFUJI Hideaki @USAGI:
  22. * - Ignore Queries for invalid addresses.
  23. * - MLD for link-local addresses.
  24. * David L Stevens <dlstevens@us.ibm.com>:
  25. * - MLDv2 support
  26. */
  27. #include <linux/module.h>
  28. #include <linux/errno.h>
  29. #include <linux/types.h>
  30. #include <linux/string.h>
  31. #include <linux/socket.h>
  32. #include <linux/sockios.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/times.h>
  35. #include <linux/net.h>
  36. #include <linux/in.h>
  37. #include <linux/in6.h>
  38. #include <linux/netdevice.h>
  39. #include <linux/if_arp.h>
  40. #include <linux/route.h>
  41. #include <linux/init.h>
  42. #include <linux/proc_fs.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/slab.h>
  45. #include <net/mld.h>
  46. #include <linux/netfilter.h>
  47. #include <linux/netfilter_ipv6.h>
  48. #include <net/net_namespace.h>
  49. #include <net/sock.h>
  50. #include <net/snmp.h>
  51. #include <net/ipv6.h>
  52. #include <net/protocol.h>
  53. #include <net/if_inet6.h>
  54. #include <net/ndisc.h>
  55. #include <net/addrconf.h>
  56. #include <net/ip6_route.h>
  57. #include <net/inet_common.h>
  58. #include <net/ip6_checksum.h>
  59. /* Set to 3 to get tracing... */
  60. #define MCAST_DEBUG 2
  61. #if MCAST_DEBUG >= 3
  62. #define MDBG(x) printk x
  63. #else
  64. #define MDBG(x)
  65. #endif
  66. /* Ensure that we have struct in6_addr aligned on 32bit word. */
  67. static void *__mld2_query_bugs[] __attribute__((__unused__)) = {
  68. BUILD_BUG_ON_NULL(offsetof(struct mld2_query, mld2q_srcs) % 4),
  69. BUILD_BUG_ON_NULL(offsetof(struct mld2_report, mld2r_grec) % 4),
  70. BUILD_BUG_ON_NULL(offsetof(struct mld2_grec, grec_mca) % 4)
  71. };
  72. static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
  73. /* Big mc list lock for all the sockets */
  74. static DEFINE_SPINLOCK(ipv6_sk_mc_lock);
  75. static void igmp6_join_group(struct ifmcaddr6 *ma);
  76. static void igmp6_leave_group(struct ifmcaddr6 *ma);
  77. static void igmp6_timer_handler(unsigned long data);
  78. static void mld_gq_timer_expire(unsigned long data);
  79. static void mld_ifc_timer_expire(unsigned long data);
  80. static void mld_ifc_event(struct inet6_dev *idev);
  81. static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
  82. static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *addr);
  83. static void mld_clear_delrec(struct inet6_dev *idev);
  84. static int sf_setstate(struct ifmcaddr6 *pmc);
  85. static void sf_markstate(struct ifmcaddr6 *pmc);
  86. static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
  87. static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
  88. int sfmode, int sfcount, struct in6_addr *psfsrc,
  89. int delta);
  90. static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
  91. int sfmode, int sfcount, struct in6_addr *psfsrc,
  92. int delta);
  93. static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
  94. struct inet6_dev *idev);
  95. #define IGMP6_UNSOLICITED_IVAL (10*HZ)
  96. #define MLD_QRV_DEFAULT 2
  97. #define MLD_V1_SEEN(idev) (dev_net((idev)->dev)->ipv6.devconf_all->force_mld_version == 1 || \
  98. (idev)->cnf.force_mld_version == 1 || \
  99. ((idev)->mc_v1_seen && \
  100. time_before(jiffies, (idev)->mc_v1_seen)))
  101. #define IPV6_MLD_MAX_MSF 64
  102. int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
  103. /*
  104. * socket join on multicast group
  105. */
  106. #define for_each_pmc_rcu(np, pmc) \
  107. for (pmc = rcu_dereference(np->ipv6_mc_list); \
  108. pmc != NULL; \
  109. pmc = rcu_dereference(pmc->next))
  110. int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
  111. {
  112. struct net_device *dev = NULL;
  113. struct ipv6_mc_socklist *mc_lst;
  114. struct ipv6_pinfo *np = inet6_sk(sk);
  115. struct net *net = sock_net(sk);
  116. int err;
  117. if (!ipv6_addr_is_multicast(addr))
  118. return -EINVAL;
  119. rcu_read_lock();
  120. for_each_pmc_rcu(np, mc_lst) {
  121. if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
  122. ipv6_addr_equal(&mc_lst->addr, addr)) {
  123. rcu_read_unlock();
  124. return -EADDRINUSE;
  125. }
  126. }
  127. rcu_read_unlock();
  128. mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
  129. if (mc_lst == NULL)
  130. return -ENOMEM;
  131. mc_lst->next = NULL;
  132. ipv6_addr_copy(&mc_lst->addr, addr);
  133. rcu_read_lock();
  134. if (ifindex == 0) {
  135. struct rt6_info *rt;
  136. rt = rt6_lookup(net, addr, NULL, 0, 0);
  137. if (rt) {
  138. dev = rt->rt6i_dev;
  139. dst_release(&rt->dst);
  140. }
  141. } else
  142. dev = dev_get_by_index_rcu(net, ifindex);
  143. if (dev == NULL) {
  144. rcu_read_unlock();
  145. sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
  146. return -ENODEV;
  147. }
  148. mc_lst->ifindex = dev->ifindex;
  149. mc_lst->sfmode = MCAST_EXCLUDE;
  150. rwlock_init(&mc_lst->sflock);
  151. mc_lst->sflist = NULL;
  152. /*
  153. * now add/increase the group membership on the device
  154. */
  155. err = ipv6_dev_mc_inc(dev, addr);
  156. if (err) {
  157. rcu_read_unlock();
  158. sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
  159. return err;
  160. }
  161. spin_lock(&ipv6_sk_mc_lock);
  162. mc_lst->next = np->ipv6_mc_list;
  163. rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
  164. spin_unlock(&ipv6_sk_mc_lock);
  165. rcu_read_unlock();
  166. return 0;
  167. }
  168. static void ipv6_mc_socklist_reclaim(struct rcu_head *head)
  169. {
  170. kfree(container_of(head, struct ipv6_mc_socklist, rcu));
  171. }
  172. /*
  173. * socket leave on multicast group
  174. */
  175. int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
  176. {
  177. struct ipv6_pinfo *np = inet6_sk(sk);
  178. struct ipv6_mc_socklist *mc_lst;
  179. struct ipv6_mc_socklist __rcu **lnk;
  180. struct net *net = sock_net(sk);
  181. spin_lock(&ipv6_sk_mc_lock);
  182. for (lnk = &np->ipv6_mc_list;
  183. (mc_lst = rcu_dereference_protected(*lnk,
  184. lockdep_is_held(&ipv6_sk_mc_lock))) !=NULL ;
  185. lnk = &mc_lst->next) {
  186. if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
  187. ipv6_addr_equal(&mc_lst->addr, addr)) {
  188. struct net_device *dev;
  189. *lnk = mc_lst->next;
  190. spin_unlock(&ipv6_sk_mc_lock);
  191. rcu_read_lock();
  192. dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
  193. if (dev != NULL) {
  194. struct inet6_dev *idev = __in6_dev_get(dev);
  195. (void) ip6_mc_leave_src(sk, mc_lst, idev);
  196. if (idev)
  197. __ipv6_dev_mc_dec(idev, &mc_lst->addr);
  198. } else
  199. (void) ip6_mc_leave_src(sk, mc_lst, NULL);
  200. rcu_read_unlock();
  201. atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
  202. call_rcu(&mc_lst->rcu, ipv6_mc_socklist_reclaim);
  203. return 0;
  204. }
  205. }
  206. spin_unlock(&ipv6_sk_mc_lock);
  207. return -EADDRNOTAVAIL;
  208. }
  209. /* called with rcu_read_lock() */
  210. static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net,
  211. struct in6_addr *group,
  212. int ifindex)
  213. {
  214. struct net_device *dev = NULL;
  215. struct inet6_dev *idev = NULL;
  216. if (ifindex == 0) {
  217. struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, 0);
  218. if (rt) {
  219. dev = rt->rt6i_dev;
  220. dev_hold(dev);
  221. dst_release(&rt->dst);
  222. }
  223. } else
  224. dev = dev_get_by_index_rcu(net, ifindex);
  225. if (!dev)
  226. return NULL;
  227. idev = __in6_dev_get(dev);
  228. if (!idev)
  229. return NULL;
  230. read_lock_bh(&idev->lock);
  231. if (idev->dead) {
  232. read_unlock_bh(&idev->lock);
  233. return NULL;
  234. }
  235. return idev;
  236. }
  237. void ipv6_sock_mc_close(struct sock *sk)
  238. {
  239. struct ipv6_pinfo *np = inet6_sk(sk);
  240. struct ipv6_mc_socklist *mc_lst;
  241. struct net *net = sock_net(sk);
  242. spin_lock(&ipv6_sk_mc_lock);
  243. while ((mc_lst = rcu_dereference_protected(np->ipv6_mc_list,
  244. lockdep_is_held(&ipv6_sk_mc_lock))) != NULL) {
  245. struct net_device *dev;
  246. np->ipv6_mc_list = mc_lst->next;
  247. spin_unlock(&ipv6_sk_mc_lock);
  248. rcu_read_lock();
  249. dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
  250. if (dev) {
  251. struct inet6_dev *idev = __in6_dev_get(dev);
  252. (void) ip6_mc_leave_src(sk, mc_lst, idev);
  253. if (idev)
  254. __ipv6_dev_mc_dec(idev, &mc_lst->addr);
  255. } else
  256. (void) ip6_mc_leave_src(sk, mc_lst, NULL);
  257. rcu_read_unlock();
  258. atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
  259. call_rcu(&mc_lst->rcu, ipv6_mc_socklist_reclaim);
  260. spin_lock(&ipv6_sk_mc_lock);
  261. }
  262. spin_unlock(&ipv6_sk_mc_lock);
  263. }
  264. int ip6_mc_source(int add, int omode, struct sock *sk,
  265. struct group_source_req *pgsr)
  266. {
  267. struct in6_addr *source, *group;
  268. struct ipv6_mc_socklist *pmc;
  269. struct net_device *dev;
  270. struct inet6_dev *idev;
  271. struct ipv6_pinfo *inet6 = inet6_sk(sk);
  272. struct ip6_sf_socklist *psl;
  273. struct net *net = sock_net(sk);
  274. int i, j, rv;
  275. int leavegroup = 0;
  276. int pmclocked = 0;
  277. int err;
  278. source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
  279. group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
  280. if (!ipv6_addr_is_multicast(group))
  281. return -EINVAL;
  282. rcu_read_lock();
  283. idev = ip6_mc_find_dev_rcu(net, group, pgsr->gsr_interface);
  284. if (!idev) {
  285. rcu_read_unlock();
  286. return -ENODEV;
  287. }
  288. dev = idev->dev;
  289. err = -EADDRNOTAVAIL;
  290. for_each_pmc_rcu(inet6, pmc) {
  291. if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
  292. continue;
  293. if (ipv6_addr_equal(&pmc->addr, group))
  294. break;
  295. }
  296. if (!pmc) { /* must have a prior join */
  297. err = -EINVAL;
  298. goto done;
  299. }
  300. /* if a source filter was set, must be the same mode as before */
  301. if (pmc->sflist) {
  302. if (pmc->sfmode != omode) {
  303. err = -EINVAL;
  304. goto done;
  305. }
  306. } else if (pmc->sfmode != omode) {
  307. /* allow mode switches for empty-set filters */
  308. ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
  309. ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
  310. pmc->sfmode = omode;
  311. }
  312. write_lock(&pmc->sflock);
  313. pmclocked = 1;
  314. psl = pmc->sflist;
  315. if (!add) {
  316. if (!psl)
  317. goto done; /* err = -EADDRNOTAVAIL */
  318. rv = !0;
  319. for (i=0; i<psl->sl_count; i++) {
  320. rv = memcmp(&psl->sl_addr[i], source,
  321. sizeof(struct in6_addr));
  322. if (rv == 0)
  323. break;
  324. }
  325. if (rv) /* source not found */
  326. goto done; /* err = -EADDRNOTAVAIL */
  327. /* special case - (INCLUDE, empty) == LEAVE_GROUP */
  328. if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
  329. leavegroup = 1;
  330. goto done;
  331. }
  332. /* update the interface filter */
  333. ip6_mc_del_src(idev, group, omode, 1, source, 1);
  334. for (j=i+1; j<psl->sl_count; j++)
  335. psl->sl_addr[j-1] = psl->sl_addr[j];
  336. psl->sl_count--;
  337. err = 0;
  338. goto done;
  339. }
  340. /* else, add a new source to the filter */
  341. if (psl && psl->sl_count >= sysctl_mld_max_msf) {
  342. err = -ENOBUFS;
  343. goto done;
  344. }
  345. if (!psl || psl->sl_count == psl->sl_max) {
  346. struct ip6_sf_socklist *newpsl;
  347. int count = IP6_SFBLOCK;
  348. if (psl)
  349. count += psl->sl_max;
  350. newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
  351. if (!newpsl) {
  352. err = -ENOBUFS;
  353. goto done;
  354. }
  355. newpsl->sl_max = count;
  356. newpsl->sl_count = count - IP6_SFBLOCK;
  357. if (psl) {
  358. for (i=0; i<psl->sl_count; i++)
  359. newpsl->sl_addr[i] = psl->sl_addr[i];
  360. sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
  361. }
  362. pmc->sflist = psl = newpsl;
  363. }
  364. rv = 1; /* > 0 for insert logic below if sl_count is 0 */
  365. for (i=0; i<psl->sl_count; i++) {
  366. rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
  367. if (rv == 0)
  368. break;
  369. }
  370. if (rv == 0) /* address already there is an error */
  371. goto done;
  372. for (j=psl->sl_count-1; j>=i; j--)
  373. psl->sl_addr[j+1] = psl->sl_addr[j];
  374. psl->sl_addr[i] = *source;
  375. psl->sl_count++;
  376. err = 0;
  377. /* update the interface list */
  378. ip6_mc_add_src(idev, group, omode, 1, source, 1);
  379. done:
  380. if (pmclocked)
  381. write_unlock(&pmc->sflock);
  382. read_unlock_bh(&idev->lock);
  383. rcu_read_unlock();
  384. if (leavegroup)
  385. return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
  386. return err;
  387. }
  388. int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
  389. {
  390. struct in6_addr *group;
  391. struct ipv6_mc_socklist *pmc;
  392. struct net_device *dev;
  393. struct inet6_dev *idev;
  394. struct ipv6_pinfo *inet6 = inet6_sk(sk);
  395. struct ip6_sf_socklist *newpsl, *psl;
  396. struct net *net = sock_net(sk);
  397. int leavegroup = 0;
  398. int i, err;
  399. group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
  400. if (!ipv6_addr_is_multicast(group))
  401. return -EINVAL;
  402. if (gsf->gf_fmode != MCAST_INCLUDE &&
  403. gsf->gf_fmode != MCAST_EXCLUDE)
  404. return -EINVAL;
  405. rcu_read_lock();
  406. idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
  407. if (!idev) {
  408. rcu_read_unlock();
  409. return -ENODEV;
  410. }
  411. dev = idev->dev;
  412. err = 0;
  413. if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
  414. leavegroup = 1;
  415. goto done;
  416. }
  417. for_each_pmc_rcu(inet6, pmc) {
  418. if (pmc->ifindex != gsf->gf_interface)
  419. continue;
  420. if (ipv6_addr_equal(&pmc->addr, group))
  421. break;
  422. }
  423. if (!pmc) { /* must have a prior join */
  424. err = -EINVAL;
  425. goto done;
  426. }
  427. if (gsf->gf_numsrc) {
  428. newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
  429. GFP_ATOMIC);
  430. if (!newpsl) {
  431. err = -ENOBUFS;
  432. goto done;
  433. }
  434. newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
  435. for (i=0; i<newpsl->sl_count; ++i) {
  436. struct sockaddr_in6 *psin6;
  437. psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
  438. newpsl->sl_addr[i] = psin6->sin6_addr;
  439. }
  440. err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
  441. newpsl->sl_count, newpsl->sl_addr, 0);
  442. if (err) {
  443. sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
  444. goto done;
  445. }
  446. } else {
  447. newpsl = NULL;
  448. (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
  449. }
  450. write_lock(&pmc->sflock);
  451. psl = pmc->sflist;
  452. if (psl) {
  453. (void) ip6_mc_del_src(idev, group, pmc->sfmode,
  454. psl->sl_count, psl->sl_addr, 0);
  455. sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
  456. } else
  457. (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
  458. pmc->sflist = newpsl;
  459. pmc->sfmode = gsf->gf_fmode;
  460. write_unlock(&pmc->sflock);
  461. err = 0;
  462. done:
  463. read_unlock_bh(&idev->lock);
  464. rcu_read_unlock();
  465. if (leavegroup)
  466. err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
  467. return err;
  468. }
  469. int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
  470. struct group_filter __user *optval, int __user *optlen)
  471. {
  472. int err, i, count, copycount;
  473. struct in6_addr *group;
  474. struct ipv6_mc_socklist *pmc;
  475. struct inet6_dev *idev;
  476. struct net_device *dev;
  477. struct ipv6_pinfo *inet6 = inet6_sk(sk);
  478. struct ip6_sf_socklist *psl;
  479. struct net *net = sock_net(sk);
  480. group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
  481. if (!ipv6_addr_is_multicast(group))
  482. return -EINVAL;
  483. rcu_read_lock();
  484. idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
  485. if (!idev) {
  486. rcu_read_unlock();
  487. return -ENODEV;
  488. }
  489. dev = idev->dev;
  490. err = -EADDRNOTAVAIL;
  491. /*
  492. * changes to the ipv6_mc_list require the socket lock and
  493. * a read lock on ip6_sk_mc_lock. We have the socket lock,
  494. * so reading the list is safe.
  495. */
  496. for_each_pmc_rcu(inet6, pmc) {
  497. if (pmc->ifindex != gsf->gf_interface)
  498. continue;
  499. if (ipv6_addr_equal(group, &pmc->addr))
  500. break;
  501. }
  502. if (!pmc) /* must have a prior join */
  503. goto done;
  504. gsf->gf_fmode = pmc->sfmode;
  505. psl = pmc->sflist;
  506. count = psl ? psl->sl_count : 0;
  507. read_unlock_bh(&idev->lock);
  508. rcu_read_unlock();
  509. copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
  510. gsf->gf_numsrc = count;
  511. if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
  512. copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
  513. return -EFAULT;
  514. }
  515. /* changes to psl require the socket lock, a read lock on
  516. * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
  517. * have the socket lock, so reading here is safe.
  518. */
  519. for (i=0; i<copycount; i++) {
  520. struct sockaddr_in6 *psin6;
  521. struct sockaddr_storage ss;
  522. psin6 = (struct sockaddr_in6 *)&ss;
  523. memset(&ss, 0, sizeof(ss));
  524. psin6->sin6_family = AF_INET6;
  525. psin6->sin6_addr = psl->sl_addr[i];
  526. if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
  527. return -EFAULT;
  528. }
  529. return 0;
  530. done:
  531. read_unlock_bh(&idev->lock);
  532. rcu_read_unlock();
  533. return err;
  534. }
  535. int inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
  536. const struct in6_addr *src_addr)
  537. {
  538. struct ipv6_pinfo *np = inet6_sk(sk);
  539. struct ipv6_mc_socklist *mc;
  540. struct ip6_sf_socklist *psl;
  541. int rv = 1;
  542. rcu_read_lock();
  543. for_each_pmc_rcu(np, mc) {
  544. if (ipv6_addr_equal(&mc->addr, mc_addr))
  545. break;
  546. }
  547. if (!mc) {
  548. rcu_read_unlock();
  549. return 1;
  550. }
  551. read_lock(&mc->sflock);
  552. psl = mc->sflist;
  553. if (!psl) {
  554. rv = mc->sfmode == MCAST_EXCLUDE;
  555. } else {
  556. int i;
  557. for (i=0; i<psl->sl_count; i++) {
  558. if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
  559. break;
  560. }
  561. if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
  562. rv = 0;
  563. if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
  564. rv = 0;
  565. }
  566. read_unlock(&mc->sflock);
  567. rcu_read_unlock();
  568. return rv;
  569. }
  570. static void ma_put(struct ifmcaddr6 *mc)
  571. {
  572. if (atomic_dec_and_test(&mc->mca_refcnt)) {
  573. in6_dev_put(mc->idev);
  574. kfree(mc);
  575. }
  576. }
  577. static void igmp6_group_added(struct ifmcaddr6 *mc)
  578. {
  579. struct net_device *dev = mc->idev->dev;
  580. char buf[MAX_ADDR_LEN];
  581. spin_lock_bh(&mc->mca_lock);
  582. if (!(mc->mca_flags&MAF_LOADED)) {
  583. mc->mca_flags |= MAF_LOADED;
  584. if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
  585. dev_mc_add(dev, buf);
  586. }
  587. spin_unlock_bh(&mc->mca_lock);
  588. if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
  589. return;
  590. if (MLD_V1_SEEN(mc->idev)) {
  591. igmp6_join_group(mc);
  592. return;
  593. }
  594. /* else v2 */
  595. mc->mca_crcount = mc->idev->mc_qrv;
  596. mld_ifc_event(mc->idev);
  597. }
  598. static void igmp6_group_dropped(struct ifmcaddr6 *mc)
  599. {
  600. struct net_device *dev = mc->idev->dev;
  601. char buf[MAX_ADDR_LEN];
  602. spin_lock_bh(&mc->mca_lock);
  603. if (mc->mca_flags&MAF_LOADED) {
  604. mc->mca_flags &= ~MAF_LOADED;
  605. if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
  606. dev_mc_del(dev, buf);
  607. }
  608. if (mc->mca_flags & MAF_NOREPORT)
  609. goto done;
  610. spin_unlock_bh(&mc->mca_lock);
  611. if (!mc->idev->dead)
  612. igmp6_leave_group(mc);
  613. spin_lock_bh(&mc->mca_lock);
  614. if (del_timer(&mc->mca_timer))
  615. atomic_dec(&mc->mca_refcnt);
  616. done:
  617. ip6_mc_clear_src(mc);
  618. spin_unlock_bh(&mc->mca_lock);
  619. }
  620. /*
  621. * deleted ifmcaddr6 manipulation
  622. */
  623. static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
  624. {
  625. struct ifmcaddr6 *pmc;
  626. /* this is an "ifmcaddr6" for convenience; only the fields below
  627. * are actually used. In particular, the refcnt and users are not
  628. * used for management of the delete list. Using the same structure
  629. * for deleted items allows change reports to use common code with
  630. * non-deleted or query-response MCA's.
  631. */
  632. pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
  633. if (!pmc)
  634. return;
  635. spin_lock_bh(&im->mca_lock);
  636. spin_lock_init(&pmc->mca_lock);
  637. pmc->idev = im->idev;
  638. in6_dev_hold(idev);
  639. pmc->mca_addr = im->mca_addr;
  640. pmc->mca_crcount = idev->mc_qrv;
  641. pmc->mca_sfmode = im->mca_sfmode;
  642. if (pmc->mca_sfmode == MCAST_INCLUDE) {
  643. struct ip6_sf_list *psf;
  644. pmc->mca_tomb = im->mca_tomb;
  645. pmc->mca_sources = im->mca_sources;
  646. im->mca_tomb = im->mca_sources = NULL;
  647. for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
  648. psf->sf_crcount = pmc->mca_crcount;
  649. }
  650. spin_unlock_bh(&im->mca_lock);
  651. spin_lock_bh(&idev->mc_lock);
  652. pmc->next = idev->mc_tomb;
  653. idev->mc_tomb = pmc;
  654. spin_unlock_bh(&idev->mc_lock);
  655. }
  656. static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
  657. {
  658. struct ifmcaddr6 *pmc, *pmc_prev;
  659. struct ip6_sf_list *psf, *psf_next;
  660. spin_lock_bh(&idev->mc_lock);
  661. pmc_prev = NULL;
  662. for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
  663. if (ipv6_addr_equal(&pmc->mca_addr, pmca))
  664. break;
  665. pmc_prev = pmc;
  666. }
  667. if (pmc) {
  668. if (pmc_prev)
  669. pmc_prev->next = pmc->next;
  670. else
  671. idev->mc_tomb = pmc->next;
  672. }
  673. spin_unlock_bh(&idev->mc_lock);
  674. if (pmc) {
  675. for (psf=pmc->mca_tomb; psf; psf=psf_next) {
  676. psf_next = psf->sf_next;
  677. kfree(psf);
  678. }
  679. in6_dev_put(pmc->idev);
  680. kfree(pmc);
  681. }
  682. }
  683. static void mld_clear_delrec(struct inet6_dev *idev)
  684. {
  685. struct ifmcaddr6 *pmc, *nextpmc;
  686. spin_lock_bh(&idev->mc_lock);
  687. pmc = idev->mc_tomb;
  688. idev->mc_tomb = NULL;
  689. spin_unlock_bh(&idev->mc_lock);
  690. for (; pmc; pmc = nextpmc) {
  691. nextpmc = pmc->next;
  692. ip6_mc_clear_src(pmc);
  693. in6_dev_put(pmc->idev);
  694. kfree(pmc);
  695. }
  696. /* clear dead sources, too */
  697. read_lock_bh(&idev->lock);
  698. for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
  699. struct ip6_sf_list *psf, *psf_next;
  700. spin_lock_bh(&pmc->mca_lock);
  701. psf = pmc->mca_tomb;
  702. pmc->mca_tomb = NULL;
  703. spin_unlock_bh(&pmc->mca_lock);
  704. for (; psf; psf=psf_next) {
  705. psf_next = psf->sf_next;
  706. kfree(psf);
  707. }
  708. }
  709. read_unlock_bh(&idev->lock);
  710. }
  711. /*
  712. * device multicast group inc (add if not found)
  713. */
  714. int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
  715. {
  716. struct ifmcaddr6 *mc;
  717. struct inet6_dev *idev;
  718. /* we need to take a reference on idev */
  719. idev = in6_dev_get(dev);
  720. if (idev == NULL)
  721. return -EINVAL;
  722. write_lock_bh(&idev->lock);
  723. if (idev->dead) {
  724. write_unlock_bh(&idev->lock);
  725. in6_dev_put(idev);
  726. return -ENODEV;
  727. }
  728. for (mc = idev->mc_list; mc; mc = mc->next) {
  729. if (ipv6_addr_equal(&mc->mca_addr, addr)) {
  730. mc->mca_users++;
  731. write_unlock_bh(&idev->lock);
  732. ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
  733. NULL, 0);
  734. in6_dev_put(idev);
  735. return 0;
  736. }
  737. }
  738. /*
  739. * not found: create a new one.
  740. */
  741. mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
  742. if (mc == NULL) {
  743. write_unlock_bh(&idev->lock);
  744. in6_dev_put(idev);
  745. return -ENOMEM;
  746. }
  747. setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
  748. ipv6_addr_copy(&mc->mca_addr, addr);
  749. mc->idev = idev; /* (reference taken) */
  750. mc->mca_users = 1;
  751. /* mca_stamp should be updated upon changes */
  752. mc->mca_cstamp = mc->mca_tstamp = jiffies;
  753. atomic_set(&mc->mca_refcnt, 2);
  754. spin_lock_init(&mc->mca_lock);
  755. /* initial mode is (EX, empty) */
  756. mc->mca_sfmode = MCAST_EXCLUDE;
  757. mc->mca_sfcount[MCAST_EXCLUDE] = 1;
  758. if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
  759. IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
  760. mc->mca_flags |= MAF_NOREPORT;
  761. mc->next = idev->mc_list;
  762. idev->mc_list = mc;
  763. write_unlock_bh(&idev->lock);
  764. mld_del_delrec(idev, &mc->mca_addr);
  765. igmp6_group_added(mc);
  766. ma_put(mc);
  767. return 0;
  768. }
  769. /*
  770. * device multicast group del
  771. */
  772. int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
  773. {
  774. struct ifmcaddr6 *ma, **map;
  775. write_lock_bh(&idev->lock);
  776. for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
  777. if (ipv6_addr_equal(&ma->mca_addr, addr)) {
  778. if (--ma->mca_users == 0) {
  779. *map = ma->next;
  780. write_unlock_bh(&idev->lock);
  781. igmp6_group_dropped(ma);
  782. ma_put(ma);
  783. return 0;
  784. }
  785. write_unlock_bh(&idev->lock);
  786. return 0;
  787. }
  788. }
  789. write_unlock_bh(&idev->lock);
  790. return -ENOENT;
  791. }
  792. int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
  793. {
  794. struct inet6_dev *idev;
  795. int err;
  796. rcu_read_lock();
  797. idev = __in6_dev_get(dev);
  798. if (!idev)
  799. err = -ENODEV;
  800. else
  801. err = __ipv6_dev_mc_dec(idev, addr);
  802. rcu_read_unlock();
  803. return err;
  804. }
  805. /*
  806. * identify MLD packets for MLD filter exceptions
  807. */
  808. int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
  809. {
  810. struct icmp6hdr *pic;
  811. if (nexthdr != IPPROTO_ICMPV6)
  812. return 0;
  813. if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
  814. return 0;
  815. pic = icmp6_hdr(skb);
  816. switch (pic->icmp6_type) {
  817. case ICMPV6_MGM_QUERY:
  818. case ICMPV6_MGM_REPORT:
  819. case ICMPV6_MGM_REDUCTION:
  820. case ICMPV6_MLD2_REPORT:
  821. return 1;
  822. default:
  823. break;
  824. }
  825. return 0;
  826. }
  827. /*
  828. * check if the interface/address pair is valid
  829. */
  830. int ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
  831. const struct in6_addr *src_addr)
  832. {
  833. struct inet6_dev *idev;
  834. struct ifmcaddr6 *mc;
  835. int rv = 0;
  836. rcu_read_lock();
  837. idev = __in6_dev_get(dev);
  838. if (idev) {
  839. read_lock_bh(&idev->lock);
  840. for (mc = idev->mc_list; mc; mc=mc->next) {
  841. if (ipv6_addr_equal(&mc->mca_addr, group))
  842. break;
  843. }
  844. if (mc) {
  845. if (src_addr && !ipv6_addr_any(src_addr)) {
  846. struct ip6_sf_list *psf;
  847. spin_lock_bh(&mc->mca_lock);
  848. for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
  849. if (ipv6_addr_equal(&psf->sf_addr, src_addr))
  850. break;
  851. }
  852. if (psf)
  853. rv = psf->sf_count[MCAST_INCLUDE] ||
  854. psf->sf_count[MCAST_EXCLUDE] !=
  855. mc->mca_sfcount[MCAST_EXCLUDE];
  856. else
  857. rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
  858. spin_unlock_bh(&mc->mca_lock);
  859. } else
  860. rv = 1; /* don't filter unspecified source */
  861. }
  862. read_unlock_bh(&idev->lock);
  863. }
  864. rcu_read_unlock();
  865. return rv;
  866. }
  867. static void mld_gq_start_timer(struct inet6_dev *idev)
  868. {
  869. int tv = net_random() % idev->mc_maxdelay;
  870. idev->mc_gq_running = 1;
  871. if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
  872. in6_dev_hold(idev);
  873. }
  874. static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
  875. {
  876. int tv = net_random() % delay;
  877. if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
  878. in6_dev_hold(idev);
  879. }
  880. /*
  881. * IGMP handling (alias multicast ICMPv6 messages)
  882. */
  883. static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
  884. {
  885. unsigned long delay = resptime;
  886. /* Do not start timer for these addresses */
  887. if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
  888. IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
  889. return;
  890. if (del_timer(&ma->mca_timer)) {
  891. atomic_dec(&ma->mca_refcnt);
  892. delay = ma->mca_timer.expires - jiffies;
  893. }
  894. if (delay >= resptime) {
  895. if (resptime)
  896. delay = net_random() % resptime;
  897. else
  898. delay = 1;
  899. }
  900. ma->mca_timer.expires = jiffies + delay;
  901. if (!mod_timer(&ma->mca_timer, jiffies + delay))
  902. atomic_inc(&ma->mca_refcnt);
  903. ma->mca_flags |= MAF_TIMER_RUNNING;
  904. }
  905. /* mark EXCLUDE-mode sources */
  906. static int mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
  907. struct in6_addr *srcs)
  908. {
  909. struct ip6_sf_list *psf;
  910. int i, scount;
  911. scount = 0;
  912. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  913. if (scount == nsrcs)
  914. break;
  915. for (i=0; i<nsrcs; i++) {
  916. /* skip inactive filters */
  917. if (pmc->mca_sfcount[MCAST_INCLUDE] ||
  918. pmc->mca_sfcount[MCAST_EXCLUDE] !=
  919. psf->sf_count[MCAST_EXCLUDE])
  920. continue;
  921. if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
  922. scount++;
  923. break;
  924. }
  925. }
  926. }
  927. pmc->mca_flags &= ~MAF_GSQUERY;
  928. if (scount == nsrcs) /* all sources excluded */
  929. return 0;
  930. return 1;
  931. }
  932. static int mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
  933. struct in6_addr *srcs)
  934. {
  935. struct ip6_sf_list *psf;
  936. int i, scount;
  937. if (pmc->mca_sfmode == MCAST_EXCLUDE)
  938. return mld_xmarksources(pmc, nsrcs, srcs);
  939. /* mark INCLUDE-mode sources */
  940. scount = 0;
  941. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  942. if (scount == nsrcs)
  943. break;
  944. for (i=0; i<nsrcs; i++) {
  945. if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
  946. psf->sf_gsresp = 1;
  947. scount++;
  948. break;
  949. }
  950. }
  951. }
  952. if (!scount) {
  953. pmc->mca_flags &= ~MAF_GSQUERY;
  954. return 0;
  955. }
  956. pmc->mca_flags |= MAF_GSQUERY;
  957. return 1;
  958. }
  959. /* called with rcu_read_lock() */
  960. int igmp6_event_query(struct sk_buff *skb)
  961. {
  962. struct mld2_query *mlh2 = NULL;
  963. struct ifmcaddr6 *ma;
  964. struct in6_addr *group;
  965. unsigned long max_delay;
  966. struct inet6_dev *idev;
  967. struct mld_msg *mld;
  968. int group_type;
  969. int mark = 0;
  970. int len;
  971. if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
  972. return -EINVAL;
  973. /* compute payload length excluding extension headers */
  974. len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
  975. len -= skb_network_header_len(skb);
  976. /* Drop queries with not link local source */
  977. if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
  978. return -EINVAL;
  979. idev = __in6_dev_get(skb->dev);
  980. if (idev == NULL)
  981. return 0;
  982. mld = (struct mld_msg *)icmp6_hdr(skb);
  983. group = &mld->mld_mca;
  984. group_type = ipv6_addr_type(group);
  985. if (group_type != IPV6_ADDR_ANY &&
  986. !(group_type&IPV6_ADDR_MULTICAST))
  987. return -EINVAL;
  988. if (len == 24) {
  989. int switchback;
  990. /* MLDv1 router present */
  991. /* Translate milliseconds to jiffies */
  992. max_delay = (ntohs(mld->mld_maxdelay)*HZ)/1000;
  993. switchback = (idev->mc_qrv + 1) * max_delay;
  994. idev->mc_v1_seen = jiffies + switchback;
  995. /* cancel the interface change timer */
  996. idev->mc_ifc_count = 0;
  997. if (del_timer(&idev->mc_ifc_timer))
  998. __in6_dev_put(idev);
  999. /* clear deleted report items */
  1000. mld_clear_delrec(idev);
  1001. } else if (len >= 28) {
  1002. int srcs_offset = sizeof(struct mld2_query) -
  1003. sizeof(struct icmp6hdr);
  1004. if (!pskb_may_pull(skb, srcs_offset))
  1005. return -EINVAL;
  1006. mlh2 = (struct mld2_query *)skb_transport_header(skb);
  1007. max_delay = (MLDV2_MRC(ntohs(mlh2->mld2q_mrc))*HZ)/1000;
  1008. if (!max_delay)
  1009. max_delay = 1;
  1010. idev->mc_maxdelay = max_delay;
  1011. if (mlh2->mld2q_qrv)
  1012. idev->mc_qrv = mlh2->mld2q_qrv;
  1013. if (group_type == IPV6_ADDR_ANY) { /* general query */
  1014. if (mlh2->mld2q_nsrcs)
  1015. return -EINVAL; /* no sources allowed */
  1016. mld_gq_start_timer(idev);
  1017. return 0;
  1018. }
  1019. /* mark sources to include, if group & source-specific */
  1020. if (mlh2->mld2q_nsrcs != 0) {
  1021. if (!pskb_may_pull(skb, srcs_offset +
  1022. ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
  1023. return -EINVAL;
  1024. mlh2 = (struct mld2_query *)skb_transport_header(skb);
  1025. mark = 1;
  1026. }
  1027. } else
  1028. return -EINVAL;
  1029. read_lock_bh(&idev->lock);
  1030. if (group_type == IPV6_ADDR_ANY) {
  1031. for (ma = idev->mc_list; ma; ma=ma->next) {
  1032. spin_lock_bh(&ma->mca_lock);
  1033. igmp6_group_queried(ma, max_delay);
  1034. spin_unlock_bh(&ma->mca_lock);
  1035. }
  1036. } else {
  1037. for (ma = idev->mc_list; ma; ma=ma->next) {
  1038. if (!ipv6_addr_equal(group, &ma->mca_addr))
  1039. continue;
  1040. spin_lock_bh(&ma->mca_lock);
  1041. if (ma->mca_flags & MAF_TIMER_RUNNING) {
  1042. /* gsquery <- gsquery && mark */
  1043. if (!mark)
  1044. ma->mca_flags &= ~MAF_GSQUERY;
  1045. } else {
  1046. /* gsquery <- mark */
  1047. if (mark)
  1048. ma->mca_flags |= MAF_GSQUERY;
  1049. else
  1050. ma->mca_flags &= ~MAF_GSQUERY;
  1051. }
  1052. if (!(ma->mca_flags & MAF_GSQUERY) ||
  1053. mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
  1054. igmp6_group_queried(ma, max_delay);
  1055. spin_unlock_bh(&ma->mca_lock);
  1056. break;
  1057. }
  1058. }
  1059. read_unlock_bh(&idev->lock);
  1060. return 0;
  1061. }
  1062. /* called with rcu_read_lock() */
  1063. int igmp6_event_report(struct sk_buff *skb)
  1064. {
  1065. struct ifmcaddr6 *ma;
  1066. struct inet6_dev *idev;
  1067. struct mld_msg *mld;
  1068. int addr_type;
  1069. /* Our own report looped back. Ignore it. */
  1070. if (skb->pkt_type == PACKET_LOOPBACK)
  1071. return 0;
  1072. /* send our report if the MC router may not have heard this report */
  1073. if (skb->pkt_type != PACKET_MULTICAST &&
  1074. skb->pkt_type != PACKET_BROADCAST)
  1075. return 0;
  1076. if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
  1077. return -EINVAL;
  1078. mld = (struct mld_msg *)icmp6_hdr(skb);
  1079. /* Drop reports with not link local source */
  1080. addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
  1081. if (addr_type != IPV6_ADDR_ANY &&
  1082. !(addr_type&IPV6_ADDR_LINKLOCAL))
  1083. return -EINVAL;
  1084. idev = __in6_dev_get(skb->dev);
  1085. if (idev == NULL)
  1086. return -ENODEV;
  1087. /*
  1088. * Cancel the timer for this group
  1089. */
  1090. read_lock_bh(&idev->lock);
  1091. for (ma = idev->mc_list; ma; ma=ma->next) {
  1092. if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
  1093. spin_lock(&ma->mca_lock);
  1094. if (del_timer(&ma->mca_timer))
  1095. atomic_dec(&ma->mca_refcnt);
  1096. ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
  1097. spin_unlock(&ma->mca_lock);
  1098. break;
  1099. }
  1100. }
  1101. read_unlock_bh(&idev->lock);
  1102. return 0;
  1103. }
  1104. static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
  1105. int gdeleted, int sdeleted)
  1106. {
  1107. switch (type) {
  1108. case MLD2_MODE_IS_INCLUDE:
  1109. case MLD2_MODE_IS_EXCLUDE:
  1110. if (gdeleted || sdeleted)
  1111. return 0;
  1112. if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
  1113. if (pmc->mca_sfmode == MCAST_INCLUDE)
  1114. return 1;
  1115. /* don't include if this source is excluded
  1116. * in all filters
  1117. */
  1118. if (psf->sf_count[MCAST_INCLUDE])
  1119. return type == MLD2_MODE_IS_INCLUDE;
  1120. return pmc->mca_sfcount[MCAST_EXCLUDE] ==
  1121. psf->sf_count[MCAST_EXCLUDE];
  1122. }
  1123. return 0;
  1124. case MLD2_CHANGE_TO_INCLUDE:
  1125. if (gdeleted || sdeleted)
  1126. return 0;
  1127. return psf->sf_count[MCAST_INCLUDE] != 0;
  1128. case MLD2_CHANGE_TO_EXCLUDE:
  1129. if (gdeleted || sdeleted)
  1130. return 0;
  1131. if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
  1132. psf->sf_count[MCAST_INCLUDE])
  1133. return 0;
  1134. return pmc->mca_sfcount[MCAST_EXCLUDE] ==
  1135. psf->sf_count[MCAST_EXCLUDE];
  1136. case MLD2_ALLOW_NEW_SOURCES:
  1137. if (gdeleted || !psf->sf_crcount)
  1138. return 0;
  1139. return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
  1140. case MLD2_BLOCK_OLD_SOURCES:
  1141. if (pmc->mca_sfmode == MCAST_INCLUDE)
  1142. return gdeleted || (psf->sf_crcount && sdeleted);
  1143. return psf->sf_crcount && !gdeleted && !sdeleted;
  1144. }
  1145. return 0;
  1146. }
  1147. static int
  1148. mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
  1149. {
  1150. struct ip6_sf_list *psf;
  1151. int scount = 0;
  1152. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  1153. if (!is_in(pmc, psf, type, gdeleted, sdeleted))
  1154. continue;
  1155. scount++;
  1156. }
  1157. return scount;
  1158. }
  1159. static struct sk_buff *mld_newpack(struct net_device *dev, int size)
  1160. {
  1161. struct net *net = dev_net(dev);
  1162. struct sock *sk = net->ipv6.igmp_sk;
  1163. struct sk_buff *skb;
  1164. struct mld2_report *pmr;
  1165. struct in6_addr addr_buf;
  1166. const struct in6_addr *saddr;
  1167. int err;
  1168. u8 ra[8] = { IPPROTO_ICMPV6, 0,
  1169. IPV6_TLV_ROUTERALERT, 2, 0, 0,
  1170. IPV6_TLV_PADN, 0 };
  1171. /* we assume size > sizeof(ra) here */
  1172. size += LL_ALLOCATED_SPACE(dev);
  1173. /* limit our allocations to order-0 page */
  1174. size = min_t(int, size, SKB_MAX_ORDER(0, 0));
  1175. skb = sock_alloc_send_skb(sk, size, 1, &err);
  1176. if (!skb)
  1177. return NULL;
  1178. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  1179. if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
  1180. /* <draft-ietf-magma-mld-source-05.txt>:
  1181. * use unspecified address as the source address
  1182. * when a valid link-local address is not available.
  1183. */
  1184. saddr = &in6addr_any;
  1185. } else
  1186. saddr = &addr_buf;
  1187. ip6_nd_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
  1188. memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
  1189. skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
  1190. skb_put(skb, sizeof(*pmr));
  1191. pmr = (struct mld2_report *)skb_transport_header(skb);
  1192. pmr->mld2r_type = ICMPV6_MLD2_REPORT;
  1193. pmr->mld2r_resv1 = 0;
  1194. pmr->mld2r_cksum = 0;
  1195. pmr->mld2r_resv2 = 0;
  1196. pmr->mld2r_ngrec = 0;
  1197. return skb;
  1198. }
  1199. static void mld_sendpack(struct sk_buff *skb)
  1200. {
  1201. struct ipv6hdr *pip6 = ipv6_hdr(skb);
  1202. struct mld2_report *pmr =
  1203. (struct mld2_report *)skb_transport_header(skb);
  1204. int payload_len, mldlen;
  1205. struct inet6_dev *idev;
  1206. struct net *net = dev_net(skb->dev);
  1207. int err;
  1208. struct flowi fl;
  1209. struct dst_entry *dst;
  1210. rcu_read_lock();
  1211. idev = __in6_dev_get(skb->dev);
  1212. IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
  1213. payload_len = (skb->tail - skb->network_header) - sizeof(*pip6);
  1214. mldlen = skb->tail - skb->transport_header;
  1215. pip6->payload_len = htons(payload_len);
  1216. pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
  1217. IPPROTO_ICMPV6,
  1218. csum_partial(skb_transport_header(skb),
  1219. mldlen, 0));
  1220. dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
  1221. if (!dst) {
  1222. err = -ENOMEM;
  1223. goto err_out;
  1224. }
  1225. icmpv6_flow_init(net->ipv6.igmp_sk, &fl, ICMPV6_MLD2_REPORT,
  1226. &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
  1227. skb->dev->ifindex);
  1228. err = xfrm_lookup(net, &dst, &fl, NULL, 0);
  1229. skb_dst_set(skb, dst);
  1230. if (err)
  1231. goto err_out;
  1232. payload_len = skb->len;
  1233. err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
  1234. dst_output);
  1235. out:
  1236. if (!err) {
  1237. ICMP6MSGOUT_INC_STATS_BH(net, idev, ICMPV6_MLD2_REPORT);
  1238. ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
  1239. IP6_UPD_PO_STATS_BH(net, idev, IPSTATS_MIB_OUTMCAST, payload_len);
  1240. } else
  1241. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTDISCARDS);
  1242. rcu_read_unlock();
  1243. return;
  1244. err_out:
  1245. kfree_skb(skb);
  1246. goto out;
  1247. }
  1248. static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
  1249. {
  1250. return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
  1251. }
  1252. static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
  1253. int type, struct mld2_grec **ppgr)
  1254. {
  1255. struct net_device *dev = pmc->idev->dev;
  1256. struct mld2_report *pmr;
  1257. struct mld2_grec *pgr;
  1258. if (!skb)
  1259. skb = mld_newpack(dev, dev->mtu);
  1260. if (!skb)
  1261. return NULL;
  1262. pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
  1263. pgr->grec_type = type;
  1264. pgr->grec_auxwords = 0;
  1265. pgr->grec_nsrcs = 0;
  1266. pgr->grec_mca = pmc->mca_addr; /* structure copy */
  1267. pmr = (struct mld2_report *)skb_transport_header(skb);
  1268. pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
  1269. *ppgr = pgr;
  1270. return skb;
  1271. }
  1272. #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
  1273. skb_tailroom(skb)) : 0)
  1274. static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
  1275. int type, int gdeleted, int sdeleted)
  1276. {
  1277. struct net_device *dev = pmc->idev->dev;
  1278. struct mld2_report *pmr;
  1279. struct mld2_grec *pgr = NULL;
  1280. struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
  1281. int scount, stotal, first, isquery, truncate;
  1282. if (pmc->mca_flags & MAF_NOREPORT)
  1283. return skb;
  1284. isquery = type == MLD2_MODE_IS_INCLUDE ||
  1285. type == MLD2_MODE_IS_EXCLUDE;
  1286. truncate = type == MLD2_MODE_IS_EXCLUDE ||
  1287. type == MLD2_CHANGE_TO_EXCLUDE;
  1288. stotal = scount = 0;
  1289. psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
  1290. if (!*psf_list)
  1291. goto empty_source;
  1292. pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
  1293. /* EX and TO_EX get a fresh packet, if needed */
  1294. if (truncate) {
  1295. if (pmr && pmr->mld2r_ngrec &&
  1296. AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
  1297. if (skb)
  1298. mld_sendpack(skb);
  1299. skb = mld_newpack(dev, dev->mtu);
  1300. }
  1301. }
  1302. first = 1;
  1303. psf_prev = NULL;
  1304. for (psf=*psf_list; psf; psf=psf_next) {
  1305. struct in6_addr *psrc;
  1306. psf_next = psf->sf_next;
  1307. if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
  1308. psf_prev = psf;
  1309. continue;
  1310. }
  1311. /* clear marks on query responses */
  1312. if (isquery)
  1313. psf->sf_gsresp = 0;
  1314. if (AVAILABLE(skb) < sizeof(*psrc) +
  1315. first*sizeof(struct mld2_grec)) {
  1316. if (truncate && !first)
  1317. break; /* truncate these */
  1318. if (pgr)
  1319. pgr->grec_nsrcs = htons(scount);
  1320. if (skb)
  1321. mld_sendpack(skb);
  1322. skb = mld_newpack(dev, dev->mtu);
  1323. first = 1;
  1324. scount = 0;
  1325. }
  1326. if (first) {
  1327. skb = add_grhead(skb, pmc, type, &pgr);
  1328. first = 0;
  1329. }
  1330. if (!skb)
  1331. return NULL;
  1332. psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
  1333. *psrc = psf->sf_addr;
  1334. scount++; stotal++;
  1335. if ((type == MLD2_ALLOW_NEW_SOURCES ||
  1336. type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
  1337. psf->sf_crcount--;
  1338. if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
  1339. if (psf_prev)
  1340. psf_prev->sf_next = psf->sf_next;
  1341. else
  1342. *psf_list = psf->sf_next;
  1343. kfree(psf);
  1344. continue;
  1345. }
  1346. }
  1347. psf_prev = psf;
  1348. }
  1349. empty_source:
  1350. if (!stotal) {
  1351. if (type == MLD2_ALLOW_NEW_SOURCES ||
  1352. type == MLD2_BLOCK_OLD_SOURCES)
  1353. return skb;
  1354. if (pmc->mca_crcount || isquery) {
  1355. /* make sure we have room for group header */
  1356. if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
  1357. mld_sendpack(skb);
  1358. skb = NULL; /* add_grhead will get a new one */
  1359. }
  1360. skb = add_grhead(skb, pmc, type, &pgr);
  1361. }
  1362. }
  1363. if (pgr)
  1364. pgr->grec_nsrcs = htons(scount);
  1365. if (isquery)
  1366. pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
  1367. return skb;
  1368. }
  1369. static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
  1370. {
  1371. struct sk_buff *skb = NULL;
  1372. int type;
  1373. if (!pmc) {
  1374. read_lock_bh(&idev->lock);
  1375. for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
  1376. if (pmc->mca_flags & MAF_NOREPORT)
  1377. continue;
  1378. spin_lock_bh(&pmc->mca_lock);
  1379. if (pmc->mca_sfcount[MCAST_EXCLUDE])
  1380. type = MLD2_MODE_IS_EXCLUDE;
  1381. else
  1382. type = MLD2_MODE_IS_INCLUDE;
  1383. skb = add_grec(skb, pmc, type, 0, 0);
  1384. spin_unlock_bh(&pmc->mca_lock);
  1385. }
  1386. read_unlock_bh(&idev->lock);
  1387. } else {
  1388. spin_lock_bh(&pmc->mca_lock);
  1389. if (pmc->mca_sfcount[MCAST_EXCLUDE])
  1390. type = MLD2_MODE_IS_EXCLUDE;
  1391. else
  1392. type = MLD2_MODE_IS_INCLUDE;
  1393. skb = add_grec(skb, pmc, type, 0, 0);
  1394. spin_unlock_bh(&pmc->mca_lock);
  1395. }
  1396. if (skb)
  1397. mld_sendpack(skb);
  1398. }
  1399. /*
  1400. * remove zero-count source records from a source filter list
  1401. */
  1402. static void mld_clear_zeros(struct ip6_sf_list **ppsf)
  1403. {
  1404. struct ip6_sf_list *psf_prev, *psf_next, *psf;
  1405. psf_prev = NULL;
  1406. for (psf=*ppsf; psf; psf = psf_next) {
  1407. psf_next = psf->sf_next;
  1408. if (psf->sf_crcount == 0) {
  1409. if (psf_prev)
  1410. psf_prev->sf_next = psf->sf_next;
  1411. else
  1412. *ppsf = psf->sf_next;
  1413. kfree(psf);
  1414. } else
  1415. psf_prev = psf;
  1416. }
  1417. }
  1418. static void mld_send_cr(struct inet6_dev *idev)
  1419. {
  1420. struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
  1421. struct sk_buff *skb = NULL;
  1422. int type, dtype;
  1423. read_lock_bh(&idev->lock);
  1424. spin_lock(&idev->mc_lock);
  1425. /* deleted MCA's */
  1426. pmc_prev = NULL;
  1427. for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
  1428. pmc_next = pmc->next;
  1429. if (pmc->mca_sfmode == MCAST_INCLUDE) {
  1430. type = MLD2_BLOCK_OLD_SOURCES;
  1431. dtype = MLD2_BLOCK_OLD_SOURCES;
  1432. skb = add_grec(skb, pmc, type, 1, 0);
  1433. skb = add_grec(skb, pmc, dtype, 1, 1);
  1434. }
  1435. if (pmc->mca_crcount) {
  1436. if (pmc->mca_sfmode == MCAST_EXCLUDE) {
  1437. type = MLD2_CHANGE_TO_INCLUDE;
  1438. skb = add_grec(skb, pmc, type, 1, 0);
  1439. }
  1440. pmc->mca_crcount--;
  1441. if (pmc->mca_crcount == 0) {
  1442. mld_clear_zeros(&pmc->mca_tomb);
  1443. mld_clear_zeros(&pmc->mca_sources);
  1444. }
  1445. }
  1446. if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
  1447. !pmc->mca_sources) {
  1448. if (pmc_prev)
  1449. pmc_prev->next = pmc_next;
  1450. else
  1451. idev->mc_tomb = pmc_next;
  1452. in6_dev_put(pmc->idev);
  1453. kfree(pmc);
  1454. } else
  1455. pmc_prev = pmc;
  1456. }
  1457. spin_unlock(&idev->mc_lock);
  1458. /* change recs */
  1459. for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
  1460. spin_lock_bh(&pmc->mca_lock);
  1461. if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
  1462. type = MLD2_BLOCK_OLD_SOURCES;
  1463. dtype = MLD2_ALLOW_NEW_SOURCES;
  1464. } else {
  1465. type = MLD2_ALLOW_NEW_SOURCES;
  1466. dtype = MLD2_BLOCK_OLD_SOURCES;
  1467. }
  1468. skb = add_grec(skb, pmc, type, 0, 0);
  1469. skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
  1470. /* filter mode changes */
  1471. if (pmc->mca_crcount) {
  1472. if (pmc->mca_sfmode == MCAST_EXCLUDE)
  1473. type = MLD2_CHANGE_TO_EXCLUDE;
  1474. else
  1475. type = MLD2_CHANGE_TO_INCLUDE;
  1476. skb = add_grec(skb, pmc, type, 0, 0);
  1477. pmc->mca_crcount--;
  1478. }
  1479. spin_unlock_bh(&pmc->mca_lock);
  1480. }
  1481. read_unlock_bh(&idev->lock);
  1482. if (!skb)
  1483. return;
  1484. (void) mld_sendpack(skb);
  1485. }
  1486. static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
  1487. {
  1488. struct net *net = dev_net(dev);
  1489. struct sock *sk = net->ipv6.igmp_sk;
  1490. struct inet6_dev *idev;
  1491. struct sk_buff *skb;
  1492. struct mld_msg *hdr;
  1493. const struct in6_addr *snd_addr, *saddr;
  1494. struct in6_addr addr_buf;
  1495. int err, len, payload_len, full_len;
  1496. u8 ra[8] = { IPPROTO_ICMPV6, 0,
  1497. IPV6_TLV_ROUTERALERT, 2, 0, 0,
  1498. IPV6_TLV_PADN, 0 };
  1499. struct flowi fl;
  1500. struct dst_entry *dst;
  1501. if (type == ICMPV6_MGM_REDUCTION)
  1502. snd_addr = &in6addr_linklocal_allrouters;
  1503. else
  1504. snd_addr = addr;
  1505. len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
  1506. payload_len = len + sizeof(ra);
  1507. full_len = sizeof(struct ipv6hdr) + payload_len;
  1508. rcu_read_lock();
  1509. IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
  1510. IPSTATS_MIB_OUT, full_len);
  1511. rcu_read_unlock();
  1512. skb = sock_alloc_send_skb(sk, LL_ALLOCATED_SPACE(dev) + full_len, 1, &err);
  1513. if (skb == NULL) {
  1514. rcu_read_lock();
  1515. IP6_INC_STATS(net, __in6_dev_get(dev),
  1516. IPSTATS_MIB_OUTDISCARDS);
  1517. rcu_read_unlock();
  1518. return;
  1519. }
  1520. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  1521. if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
  1522. /* <draft-ietf-magma-mld-source-05.txt>:
  1523. * use unspecified address as the source address
  1524. * when a valid link-local address is not available.
  1525. */
  1526. saddr = &in6addr_any;
  1527. } else
  1528. saddr = &addr_buf;
  1529. ip6_nd_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
  1530. memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
  1531. hdr = (struct mld_msg *) skb_put(skb, sizeof(struct mld_msg));
  1532. memset(hdr, 0, sizeof(struct mld_msg));
  1533. hdr->mld_type = type;
  1534. ipv6_addr_copy(&hdr->mld_mca, addr);
  1535. hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
  1536. IPPROTO_ICMPV6,
  1537. csum_partial(hdr, len, 0));
  1538. rcu_read_lock();
  1539. idev = __in6_dev_get(skb->dev);
  1540. dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
  1541. if (!dst) {
  1542. err = -ENOMEM;
  1543. goto err_out;
  1544. }
  1545. icmpv6_flow_init(sk, &fl, type,
  1546. &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
  1547. skb->dev->ifindex);
  1548. err = xfrm_lookup(net, &dst, &fl, NULL, 0);
  1549. if (err)
  1550. goto err_out;
  1551. skb_dst_set(skb, dst);
  1552. err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
  1553. dst_output);
  1554. out:
  1555. if (!err) {
  1556. ICMP6MSGOUT_INC_STATS(net, idev, type);
  1557. ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
  1558. IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, full_len);
  1559. } else
  1560. IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
  1561. rcu_read_unlock();
  1562. return;
  1563. err_out:
  1564. kfree_skb(skb);
  1565. goto out;
  1566. }
  1567. static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
  1568. struct in6_addr *psfsrc)
  1569. {
  1570. struct ip6_sf_list *psf, *psf_prev;
  1571. int rv = 0;
  1572. psf_prev = NULL;
  1573. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  1574. if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
  1575. break;
  1576. psf_prev = psf;
  1577. }
  1578. if (!psf || psf->sf_count[sfmode] == 0) {
  1579. /* source filter not found, or count wrong => bug */
  1580. return -ESRCH;
  1581. }
  1582. psf->sf_count[sfmode]--;
  1583. if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
  1584. struct inet6_dev *idev = pmc->idev;
  1585. /* no more filters for this source */
  1586. if (psf_prev)
  1587. psf_prev->sf_next = psf->sf_next;
  1588. else
  1589. pmc->mca_sources = psf->sf_next;
  1590. if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
  1591. !MLD_V1_SEEN(idev)) {
  1592. psf->sf_crcount = idev->mc_qrv;
  1593. psf->sf_next = pmc->mca_tomb;
  1594. pmc->mca_tomb = psf;
  1595. rv = 1;
  1596. } else
  1597. kfree(psf);
  1598. }
  1599. return rv;
  1600. }
  1601. static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
  1602. int sfmode, int sfcount, struct in6_addr *psfsrc,
  1603. int delta)
  1604. {
  1605. struct ifmcaddr6 *pmc;
  1606. int changerec = 0;
  1607. int i, err;
  1608. if (!idev)
  1609. return -ENODEV;
  1610. read_lock_bh(&idev->lock);
  1611. for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
  1612. if (ipv6_addr_equal(pmca, &pmc->mca_addr))
  1613. break;
  1614. }
  1615. if (!pmc) {
  1616. /* MCA not found?? bug */
  1617. read_unlock_bh(&idev->lock);
  1618. return -ESRCH;
  1619. }
  1620. spin_lock_bh(&pmc->mca_lock);
  1621. sf_markstate(pmc);
  1622. if (!delta) {
  1623. if (!pmc->mca_sfcount[sfmode]) {
  1624. spin_unlock_bh(&pmc->mca_lock);
  1625. read_unlock_bh(&idev->lock);
  1626. return -EINVAL;
  1627. }
  1628. pmc->mca_sfcount[sfmode]--;
  1629. }
  1630. err = 0;
  1631. for (i=0; i<sfcount; i++) {
  1632. int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1633. changerec |= rv > 0;
  1634. if (!err && rv < 0)
  1635. err = rv;
  1636. }
  1637. if (pmc->mca_sfmode == MCAST_EXCLUDE &&
  1638. pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
  1639. pmc->mca_sfcount[MCAST_INCLUDE]) {
  1640. struct ip6_sf_list *psf;
  1641. /* filter mode change */
  1642. pmc->mca_sfmode = MCAST_INCLUDE;
  1643. pmc->mca_crcount = idev->mc_qrv;
  1644. idev->mc_ifc_count = pmc->mca_crcount;
  1645. for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
  1646. psf->sf_crcount = 0;
  1647. mld_ifc_event(pmc->idev);
  1648. } else if (sf_setstate(pmc) || changerec)
  1649. mld_ifc_event(pmc->idev);
  1650. spin_unlock_bh(&pmc->mca_lock);
  1651. read_unlock_bh(&idev->lock);
  1652. return err;
  1653. }
  1654. /*
  1655. * Add multicast single-source filter to the interface list
  1656. */
  1657. static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
  1658. struct in6_addr *psfsrc, int delta)
  1659. {
  1660. struct ip6_sf_list *psf, *psf_prev;
  1661. psf_prev = NULL;
  1662. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  1663. if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
  1664. break;
  1665. psf_prev = psf;
  1666. }
  1667. if (!psf) {
  1668. psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
  1669. if (!psf)
  1670. return -ENOBUFS;
  1671. psf->sf_addr = *psfsrc;
  1672. if (psf_prev) {
  1673. psf_prev->sf_next = psf;
  1674. } else
  1675. pmc->mca_sources = psf;
  1676. }
  1677. psf->sf_count[sfmode]++;
  1678. return 0;
  1679. }
  1680. static void sf_markstate(struct ifmcaddr6 *pmc)
  1681. {
  1682. struct ip6_sf_list *psf;
  1683. int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
  1684. for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
  1685. if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
  1686. psf->sf_oldin = mca_xcount ==
  1687. psf->sf_count[MCAST_EXCLUDE] &&
  1688. !psf->sf_count[MCAST_INCLUDE];
  1689. } else
  1690. psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
  1691. }
  1692. static int sf_setstate(struct ifmcaddr6 *pmc)
  1693. {
  1694. struct ip6_sf_list *psf, *dpsf;
  1695. int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
  1696. int qrv = pmc->idev->mc_qrv;
  1697. int new_in, rv;
  1698. rv = 0;
  1699. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  1700. if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
  1701. new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
  1702. !psf->sf_count[MCAST_INCLUDE];
  1703. } else
  1704. new_in = psf->sf_count[MCAST_INCLUDE] != 0;
  1705. if (new_in) {
  1706. if (!psf->sf_oldin) {
  1707. struct ip6_sf_list *prev = NULL;
  1708. for (dpsf=pmc->mca_tomb; dpsf;
  1709. dpsf=dpsf->sf_next) {
  1710. if (ipv6_addr_equal(&dpsf->sf_addr,
  1711. &psf->sf_addr))
  1712. break;
  1713. prev = dpsf;
  1714. }
  1715. if (dpsf) {
  1716. if (prev)
  1717. prev->sf_next = dpsf->sf_next;
  1718. else
  1719. pmc->mca_tomb = dpsf->sf_next;
  1720. kfree(dpsf);
  1721. }
  1722. psf->sf_crcount = qrv;
  1723. rv++;
  1724. }
  1725. } else if (psf->sf_oldin) {
  1726. psf->sf_crcount = 0;
  1727. /*
  1728. * add or update "delete" records if an active filter
  1729. * is now inactive
  1730. */
  1731. for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
  1732. if (ipv6_addr_equal(&dpsf->sf_addr,
  1733. &psf->sf_addr))
  1734. break;
  1735. if (!dpsf) {
  1736. dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
  1737. if (!dpsf)
  1738. continue;
  1739. *dpsf = *psf;
  1740. /* pmc->mca_lock held by callers */
  1741. dpsf->sf_next = pmc->mca_tomb;
  1742. pmc->mca_tomb = dpsf;
  1743. }
  1744. dpsf->sf_crcount = qrv;
  1745. rv++;
  1746. }
  1747. }
  1748. return rv;
  1749. }
  1750. /*
  1751. * Add multicast source filter list to the interface list
  1752. */
  1753. static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
  1754. int sfmode, int sfcount, struct in6_addr *psfsrc,
  1755. int delta)
  1756. {
  1757. struct ifmcaddr6 *pmc;
  1758. int isexclude;
  1759. int i, err;
  1760. if (!idev)
  1761. return -ENODEV;
  1762. read_lock_bh(&idev->lock);
  1763. for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
  1764. if (ipv6_addr_equal(pmca, &pmc->mca_addr))
  1765. break;
  1766. }
  1767. if (!pmc) {
  1768. /* MCA not found?? bug */
  1769. read_unlock_bh(&idev->lock);
  1770. return -ESRCH;
  1771. }
  1772. spin_lock_bh(&pmc->mca_lock);
  1773. sf_markstate(pmc);
  1774. isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
  1775. if (!delta)
  1776. pmc->mca_sfcount[sfmode]++;
  1777. err = 0;
  1778. for (i=0; i<sfcount; i++) {
  1779. err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
  1780. if (err)
  1781. break;
  1782. }
  1783. if (err) {
  1784. int j;
  1785. if (!delta)
  1786. pmc->mca_sfcount[sfmode]--;
  1787. for (j=0; j<i; j++)
  1788. (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1789. } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
  1790. struct ip6_sf_list *psf;
  1791. /* filter mode change */
  1792. if (pmc->mca_sfcount[MCAST_EXCLUDE])
  1793. pmc->mca_sfmode = MCAST_EXCLUDE;
  1794. else if (pmc->mca_sfcount[MCAST_INCLUDE])
  1795. pmc->mca_sfmode = MCAST_INCLUDE;
  1796. /* else no filters; keep old mode for reports */
  1797. pmc->mca_crcount = idev->mc_qrv;
  1798. idev->mc_ifc_count = pmc->mca_crcount;
  1799. for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
  1800. psf->sf_crcount = 0;
  1801. mld_ifc_event(idev);
  1802. } else if (sf_setstate(pmc))
  1803. mld_ifc_event(idev);
  1804. spin_unlock_bh(&pmc->mca_lock);
  1805. read_unlock_bh(&idev->lock);
  1806. return err;
  1807. }
  1808. static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
  1809. {
  1810. struct ip6_sf_list *psf, *nextpsf;
  1811. for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
  1812. nextpsf = psf->sf_next;
  1813. kfree(psf);
  1814. }
  1815. pmc->mca_tomb = NULL;
  1816. for (psf=pmc->mca_sources; psf; psf=nextpsf) {
  1817. nextpsf = psf->sf_next;
  1818. kfree(psf);
  1819. }
  1820. pmc->mca_sources = NULL;
  1821. pmc->mca_sfmode = MCAST_EXCLUDE;
  1822. pmc->mca_sfcount[MCAST_INCLUDE] = 0;
  1823. pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
  1824. }
  1825. static void igmp6_join_group(struct ifmcaddr6 *ma)
  1826. {
  1827. unsigned long delay;
  1828. if (ma->mca_flags & MAF_NOREPORT)
  1829. return;
  1830. igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
  1831. delay = net_random() % IGMP6_UNSOLICITED_IVAL;
  1832. spin_lock_bh(&ma->mca_lock);
  1833. if (del_timer(&ma->mca_timer)) {
  1834. atomic_dec(&ma->mca_refcnt);
  1835. delay = ma->mca_timer.expires - jiffies;
  1836. }
  1837. if (!mod_timer(&ma->mca_timer, jiffies + delay))
  1838. atomic_inc(&ma->mca_refcnt);
  1839. ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
  1840. spin_unlock_bh(&ma->mca_lock);
  1841. }
  1842. static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
  1843. struct inet6_dev *idev)
  1844. {
  1845. int err;
  1846. /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
  1847. * so no other readers or writers of iml or its sflist
  1848. */
  1849. if (!iml->sflist) {
  1850. /* any-source empty exclude case */
  1851. return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
  1852. }
  1853. err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
  1854. iml->sflist->sl_count, iml->sflist->sl_addr, 0);
  1855. sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
  1856. iml->sflist = NULL;
  1857. return err;
  1858. }
  1859. static void igmp6_leave_group(struct ifmcaddr6 *ma)
  1860. {
  1861. if (MLD_V1_SEEN(ma->idev)) {
  1862. if (ma->mca_flags & MAF_LAST_REPORTER)
  1863. igmp6_send(&ma->mca_addr, ma->idev->dev,
  1864. ICMPV6_MGM_REDUCTION);
  1865. } else {
  1866. mld_add_delrec(ma->idev, ma);
  1867. mld_ifc_event(ma->idev);
  1868. }
  1869. }
  1870. static void mld_gq_timer_expire(unsigned long data)
  1871. {
  1872. struct inet6_dev *idev = (struct inet6_dev *)data;
  1873. idev->mc_gq_running = 0;
  1874. mld_send_report(idev, NULL);
  1875. __in6_dev_put(idev);
  1876. }
  1877. static void mld_ifc_timer_expire(unsigned long data)
  1878. {
  1879. struct inet6_dev *idev = (struct inet6_dev *)data;
  1880. mld_send_cr(idev);
  1881. if (idev->mc_ifc_count) {
  1882. idev->mc_ifc_count--;
  1883. if (idev->mc_ifc_count)
  1884. mld_ifc_start_timer(idev, idev->mc_maxdelay);
  1885. }
  1886. __in6_dev_put(idev);
  1887. }
  1888. static void mld_ifc_event(struct inet6_dev *idev)
  1889. {
  1890. if (MLD_V1_SEEN(idev))
  1891. return;
  1892. idev->mc_ifc_count = idev->mc_qrv;
  1893. mld_ifc_start_timer(idev, 1);
  1894. }
  1895. static void igmp6_timer_handler(unsigned long data)
  1896. {
  1897. struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
  1898. if (MLD_V1_SEEN(ma->idev))
  1899. igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
  1900. else
  1901. mld_send_report(ma->idev, ma);
  1902. spin_lock(&ma->mca_lock);
  1903. ma->mca_flags |= MAF_LAST_REPORTER;
  1904. ma->mca_flags &= ~MAF_TIMER_RUNNING;
  1905. spin_unlock(&ma->mca_lock);
  1906. ma_put(ma);
  1907. }
  1908. /* Device changing type */
  1909. void ipv6_mc_unmap(struct inet6_dev *idev)
  1910. {
  1911. struct ifmcaddr6 *i;
  1912. /* Install multicast list, except for all-nodes (already installed) */
  1913. read_lock_bh(&idev->lock);
  1914. for (i = idev->mc_list; i; i = i->next)
  1915. igmp6_group_dropped(i);
  1916. read_unlock_bh(&idev->lock);
  1917. }
  1918. void ipv6_mc_remap(struct inet6_dev *idev)
  1919. {
  1920. ipv6_mc_up(idev);
  1921. }
  1922. /* Device going down */
  1923. void ipv6_mc_down(struct inet6_dev *idev)
  1924. {
  1925. struct ifmcaddr6 *i;
  1926. /* Withdraw multicast list */
  1927. read_lock_bh(&idev->lock);
  1928. idev->mc_ifc_count = 0;
  1929. if (del_timer(&idev->mc_ifc_timer))
  1930. __in6_dev_put(idev);
  1931. idev->mc_gq_running = 0;
  1932. if (del_timer(&idev->mc_gq_timer))
  1933. __in6_dev_put(idev);
  1934. for (i = idev->mc_list; i; i=i->next)
  1935. igmp6_group_dropped(i);
  1936. read_unlock_bh(&idev->lock);
  1937. mld_clear_delrec(idev);
  1938. }
  1939. /* Device going up */
  1940. void ipv6_mc_up(struct inet6_dev *idev)
  1941. {
  1942. struct ifmcaddr6 *i;
  1943. /* Install multicast list, except for all-nodes (already installed) */
  1944. read_lock_bh(&idev->lock);
  1945. for (i = idev->mc_list; i; i=i->next)
  1946. igmp6_group_added(i);
  1947. read_unlock_bh(&idev->lock);
  1948. }
  1949. /* IPv6 device initialization. */
  1950. void ipv6_mc_init_dev(struct inet6_dev *idev)
  1951. {
  1952. write_lock_bh(&idev->lock);
  1953. spin_lock_init(&idev->mc_lock);
  1954. idev->mc_gq_running = 0;
  1955. setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
  1956. (unsigned long)idev);
  1957. idev->mc_tomb = NULL;
  1958. idev->mc_ifc_count = 0;
  1959. setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
  1960. (unsigned long)idev);
  1961. idev->mc_qrv = MLD_QRV_DEFAULT;
  1962. idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
  1963. idev->mc_v1_seen = 0;
  1964. write_unlock_bh(&idev->lock);
  1965. }
  1966. /*
  1967. * Device is about to be destroyed: clean up.
  1968. */
  1969. void ipv6_mc_destroy_dev(struct inet6_dev *idev)
  1970. {
  1971. struct ifmcaddr6 *i;
  1972. /* Deactivate timers */
  1973. ipv6_mc_down(idev);
  1974. /* Delete all-nodes address. */
  1975. /* We cannot call ipv6_dev_mc_dec() directly, our caller in
  1976. * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
  1977. * fail.
  1978. */
  1979. __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
  1980. if (idev->cnf.forwarding)
  1981. __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
  1982. write_lock_bh(&idev->lock);
  1983. while ((i = idev->mc_list) != NULL) {
  1984. idev->mc_list = i->next;
  1985. write_unlock_bh(&idev->lock);
  1986. igmp6_group_dropped(i);
  1987. ma_put(i);
  1988. write_lock_bh(&idev->lock);
  1989. }
  1990. write_unlock_bh(&idev->lock);
  1991. }
  1992. #ifdef CONFIG_PROC_FS
  1993. struct igmp6_mc_iter_state {
  1994. struct seq_net_private p;
  1995. struct net_device *dev;
  1996. struct inet6_dev *idev;
  1997. };
  1998. #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
  1999. static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
  2000. {
  2001. struct ifmcaddr6 *im = NULL;
  2002. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  2003. struct net *net = seq_file_net(seq);
  2004. state->idev = NULL;
  2005. for_each_netdev_rcu(net, state->dev) {
  2006. struct inet6_dev *idev;
  2007. idev = __in6_dev_get(state->dev);
  2008. if (!idev)
  2009. continue;
  2010. read_lock_bh(&idev->lock);
  2011. im = idev->mc_list;
  2012. if (im) {
  2013. state->idev = idev;
  2014. break;
  2015. }
  2016. read_unlock_bh(&idev->lock);
  2017. }
  2018. return im;
  2019. }
  2020. static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
  2021. {
  2022. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  2023. im = im->next;
  2024. while (!im) {
  2025. if (likely(state->idev != NULL))
  2026. read_unlock_bh(&state->idev->lock);
  2027. state->dev = next_net_device_rcu(state->dev);
  2028. if (!state->dev) {
  2029. state->idev = NULL;
  2030. break;
  2031. }
  2032. state->idev = __in6_dev_get(state->dev);
  2033. if (!state->idev)
  2034. continue;
  2035. read_lock_bh(&state->idev->lock);
  2036. im = state->idev->mc_list;
  2037. }
  2038. return im;
  2039. }
  2040. static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
  2041. {
  2042. struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
  2043. if (im)
  2044. while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
  2045. --pos;
  2046. return pos ? NULL : im;
  2047. }
  2048. static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
  2049. __acquires(RCU)
  2050. {
  2051. rcu_read_lock();
  2052. return igmp6_mc_get_idx(seq, *pos);
  2053. }
  2054. static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2055. {
  2056. struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
  2057. ++*pos;
  2058. return im;
  2059. }
  2060. static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
  2061. __releases(RCU)
  2062. {
  2063. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  2064. if (likely(state->idev != NULL)) {
  2065. read_unlock_bh(&state->idev->lock);
  2066. state->idev = NULL;
  2067. }
  2068. state->dev = NULL;
  2069. rcu_read_unlock();
  2070. }
  2071. static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
  2072. {
  2073. struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
  2074. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  2075. seq_printf(seq,
  2076. "%-4d %-15s %pi6 %5d %08X %ld\n",
  2077. state->dev->ifindex, state->dev->name,
  2078. &im->mca_addr,
  2079. im->mca_users, im->mca_flags,
  2080. (im->mca_flags&MAF_TIMER_RUNNING) ?
  2081. jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
  2082. return 0;
  2083. }
  2084. static const struct seq_operations igmp6_mc_seq_ops = {
  2085. .start = igmp6_mc_seq_start,
  2086. .next = igmp6_mc_seq_next,
  2087. .stop = igmp6_mc_seq_stop,
  2088. .show = igmp6_mc_seq_show,
  2089. };
  2090. static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
  2091. {
  2092. return seq_open_net(inode, file, &igmp6_mc_seq_ops,
  2093. sizeof(struct igmp6_mc_iter_state));
  2094. }
  2095. static const struct file_operations igmp6_mc_seq_fops = {
  2096. .owner = THIS_MODULE,
  2097. .open = igmp6_mc_seq_open,
  2098. .read = seq_read,
  2099. .llseek = seq_lseek,
  2100. .release = seq_release_net,
  2101. };
  2102. struct igmp6_mcf_iter_state {
  2103. struct seq_net_private p;
  2104. struct net_device *dev;
  2105. struct inet6_dev *idev;
  2106. struct ifmcaddr6 *im;
  2107. };
  2108. #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
  2109. static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
  2110. {
  2111. struct ip6_sf_list *psf = NULL;
  2112. struct ifmcaddr6 *im = NULL;
  2113. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2114. struct net *net = seq_file_net(seq);
  2115. state->idev = NULL;
  2116. state->im = NULL;
  2117. for_each_netdev_rcu(net, state->dev) {
  2118. struct inet6_dev *idev;
  2119. idev = __in6_dev_get(state->dev);
  2120. if (unlikely(idev == NULL))
  2121. continue;
  2122. read_lock_bh(&idev->lock);
  2123. im = idev->mc_list;
  2124. if (likely(im != NULL)) {
  2125. spin_lock_bh(&im->mca_lock);
  2126. psf = im->mca_sources;
  2127. if (likely(psf != NULL)) {
  2128. state->im = im;
  2129. state->idev = idev;
  2130. break;
  2131. }
  2132. spin_unlock_bh(&im->mca_lock);
  2133. }
  2134. read_unlock_bh(&idev->lock);
  2135. }
  2136. return psf;
  2137. }
  2138. static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
  2139. {
  2140. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2141. psf = psf->sf_next;
  2142. while (!psf) {
  2143. spin_unlock_bh(&state->im->mca_lock);
  2144. state->im = state->im->next;
  2145. while (!state->im) {
  2146. if (likely(state->idev != NULL))
  2147. read_unlock_bh(&state->idev->lock);
  2148. state->dev = next_net_device_rcu(state->dev);
  2149. if (!state->dev) {
  2150. state->idev = NULL;
  2151. goto out;
  2152. }
  2153. state->idev = __in6_dev_get(state->dev);
  2154. if (!state->idev)
  2155. continue;
  2156. read_lock_bh(&state->idev->lock);
  2157. state->im = state->idev->mc_list;
  2158. }
  2159. if (!state->im)
  2160. break;
  2161. spin_lock_bh(&state->im->mca_lock);
  2162. psf = state->im->mca_sources;
  2163. }
  2164. out:
  2165. return psf;
  2166. }
  2167. static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
  2168. {
  2169. struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
  2170. if (psf)
  2171. while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
  2172. --pos;
  2173. return pos ? NULL : psf;
  2174. }
  2175. static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
  2176. __acquires(RCU)
  2177. {
  2178. rcu_read_lock();
  2179. return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2180. }
  2181. static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2182. {
  2183. struct ip6_sf_list *psf;
  2184. if (v == SEQ_START_TOKEN)
  2185. psf = igmp6_mcf_get_first(seq);
  2186. else
  2187. psf = igmp6_mcf_get_next(seq, v);
  2188. ++*pos;
  2189. return psf;
  2190. }
  2191. static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
  2192. __releases(RCU)
  2193. {
  2194. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2195. if (likely(state->im != NULL)) {
  2196. spin_unlock_bh(&state->im->mca_lock);
  2197. state->im = NULL;
  2198. }
  2199. if (likely(state->idev != NULL)) {
  2200. read_unlock_bh(&state->idev->lock);
  2201. state->idev = NULL;
  2202. }
  2203. state->dev = NULL;
  2204. rcu_read_unlock();
  2205. }
  2206. static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
  2207. {
  2208. struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
  2209. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2210. if (v == SEQ_START_TOKEN) {
  2211. seq_printf(seq,
  2212. "%3s %6s "
  2213. "%32s %32s %6s %6s\n", "Idx",
  2214. "Device", "Multicast Address",
  2215. "Source Address", "INC", "EXC");
  2216. } else {
  2217. seq_printf(seq,
  2218. "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
  2219. state->dev->ifindex, state->dev->name,
  2220. &state->im->mca_addr,
  2221. &psf->sf_addr,
  2222. psf->sf_count[MCAST_INCLUDE],
  2223. psf->sf_count[MCAST_EXCLUDE]);
  2224. }
  2225. return 0;
  2226. }
  2227. static const struct seq_operations igmp6_mcf_seq_ops = {
  2228. .start = igmp6_mcf_seq_start,
  2229. .next = igmp6_mcf_seq_next,
  2230. .stop = igmp6_mcf_seq_stop,
  2231. .show = igmp6_mcf_seq_show,
  2232. };
  2233. static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
  2234. {
  2235. return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
  2236. sizeof(struct igmp6_mcf_iter_state));
  2237. }
  2238. static const struct file_operations igmp6_mcf_seq_fops = {
  2239. .owner = THIS_MODULE,
  2240. .open = igmp6_mcf_seq_open,
  2241. .read = seq_read,
  2242. .llseek = seq_lseek,
  2243. .release = seq_release_net,
  2244. };
  2245. static int __net_init igmp6_proc_init(struct net *net)
  2246. {
  2247. int err;
  2248. err = -ENOMEM;
  2249. if (!proc_net_fops_create(net, "igmp6", S_IRUGO, &igmp6_mc_seq_fops))
  2250. goto out;
  2251. if (!proc_net_fops_create(net, "mcfilter6", S_IRUGO,
  2252. &igmp6_mcf_seq_fops))
  2253. goto out_proc_net_igmp6;
  2254. err = 0;
  2255. out:
  2256. return err;
  2257. out_proc_net_igmp6:
  2258. proc_net_remove(net, "igmp6");
  2259. goto out;
  2260. }
  2261. static void __net_exit igmp6_proc_exit(struct net *net)
  2262. {
  2263. proc_net_remove(net, "mcfilter6");
  2264. proc_net_remove(net, "igmp6");
  2265. }
  2266. #else
  2267. static inline int igmp6_proc_init(struct net *net)
  2268. {
  2269. return 0;
  2270. }
  2271. static inline void igmp6_proc_exit(struct net *net)
  2272. {
  2273. }
  2274. #endif
  2275. static int __net_init igmp6_net_init(struct net *net)
  2276. {
  2277. int err;
  2278. err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
  2279. SOCK_RAW, IPPROTO_ICMPV6, net);
  2280. if (err < 0) {
  2281. printk(KERN_ERR
  2282. "Failed to initialize the IGMP6 control socket (err %d).\n",
  2283. err);
  2284. goto out;
  2285. }
  2286. inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
  2287. err = igmp6_proc_init(net);
  2288. if (err)
  2289. goto out_sock_create;
  2290. out:
  2291. return err;
  2292. out_sock_create:
  2293. inet_ctl_sock_destroy(net->ipv6.igmp_sk);
  2294. goto out;
  2295. }
  2296. static void __net_exit igmp6_net_exit(struct net *net)
  2297. {
  2298. inet_ctl_sock_destroy(net->ipv6.igmp_sk);
  2299. igmp6_proc_exit(net);
  2300. }
  2301. static struct pernet_operations igmp6_net_ops = {
  2302. .init = igmp6_net_init,
  2303. .exit = igmp6_net_exit,
  2304. };
  2305. int __init igmp6_init(void)
  2306. {
  2307. return register_pernet_subsys(&igmp6_net_ops);
  2308. }
  2309. void igmp6_cleanup(void)
  2310. {
  2311. unregister_pernet_subsys(&igmp6_net_ops);
  2312. }