mcast.c 58 KB

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