mcast.c 59 KB

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