mcast.c 60 KB

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