mcast.c 62 KB

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