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