mcast.c 62 KB

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