mcast.c 63 KB

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