mcast.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706
  1. /*
  2. * Multicast support for IPv6
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. /* Changes:
  16. *
  17. * yoshfuji : fix format of router-alert option
  18. * YOSHIFUJI Hideaki @USAGI:
  19. * Fixed source address for MLD message based on
  20. * <draft-ietf-magma-mld-source-05.txt>.
  21. * YOSHIFUJI Hideaki @USAGI:
  22. * - Ignore Queries for invalid addresses.
  23. * - MLD for link-local addresses.
  24. * David L Stevens <dlstevens@us.ibm.com>:
  25. * - MLDv2 support
  26. */
  27. #include <linux/module.h>
  28. #include <linux/errno.h>
  29. #include <linux/types.h>
  30. #include <linux/string.h>
  31. #include <linux/socket.h>
  32. #include <linux/sockios.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/times.h>
  35. #include <linux/net.h>
  36. #include <linux/in.h>
  37. #include <linux/in6.h>
  38. #include <linux/netdevice.h>
  39. #include <linux/if_arp.h>
  40. #include <linux/route.h>
  41. #include <linux/init.h>
  42. #include <linux/proc_fs.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/netfilter.h>
  45. #include <linux/netfilter_ipv6.h>
  46. #include <net/net_namespace.h>
  47. #include <net/sock.h>
  48. #include <net/snmp.h>
  49. #include <net/ipv6.h>
  50. #include <net/protocol.h>
  51. #include <net/if_inet6.h>
  52. #include <net/ndisc.h>
  53. #include <net/addrconf.h>
  54. #include <net/ip6_route.h>
  55. #include <net/inet_common.h>
  56. #include <net/ip6_checksum.h>
  57. /* Set to 3 to get tracing... */
  58. #define MCAST_DEBUG 2
  59. #if MCAST_DEBUG >= 3
  60. #define MDBG(x) printk x
  61. #else
  62. #define MDBG(x)
  63. #endif
  64. /*
  65. * These header formats should be in a separate include file, but icmpv6.h
  66. * doesn't have in6_addr defined in all cases, there is no __u128, and no
  67. * other files reference these.
  68. *
  69. * +-DLS 4/14/03
  70. */
  71. /* Multicast Listener Discovery version 2 headers */
  72. struct mld2_grec {
  73. __u8 grec_type;
  74. __u8 grec_auxwords;
  75. __be16 grec_nsrcs;
  76. struct in6_addr grec_mca;
  77. struct in6_addr grec_src[0];
  78. };
  79. struct mld2_report {
  80. __u8 type;
  81. __u8 resv1;
  82. __sum16 csum;
  83. __be16 resv2;
  84. __be16 ngrec;
  85. struct mld2_grec grec[0];
  86. };
  87. struct mld2_query {
  88. __u8 type;
  89. __u8 code;
  90. __sum16 csum;
  91. __be16 mrc;
  92. __be16 resv1;
  93. struct in6_addr mca;
  94. #if defined(__LITTLE_ENDIAN_BITFIELD)
  95. __u8 qrv:3,
  96. suppress:1,
  97. resv2:4;
  98. #elif defined(__BIG_ENDIAN_BITFIELD)
  99. __u8 resv2:4,
  100. suppress:1,
  101. qrv:3;
  102. #else
  103. #error "Please fix <asm/byteorder.h>"
  104. #endif
  105. __u8 qqic;
  106. __be16 nsrcs;
  107. struct in6_addr srcs[0];
  108. };
  109. static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
  110. /* Big mc list lock for all the sockets */
  111. static DEFINE_RWLOCK(ipv6_sk_mc_lock);
  112. static void igmp6_join_group(struct ifmcaddr6 *ma);
  113. static void igmp6_leave_group(struct ifmcaddr6 *ma);
  114. static void igmp6_timer_handler(unsigned long data);
  115. static void mld_gq_timer_expire(unsigned long data);
  116. static void mld_ifc_timer_expire(unsigned long data);
  117. static void mld_ifc_event(struct inet6_dev *idev);
  118. static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
  119. static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *addr);
  120. static void mld_clear_delrec(struct inet6_dev *idev);
  121. static int sf_setstate(struct ifmcaddr6 *pmc);
  122. static void sf_markstate(struct ifmcaddr6 *pmc);
  123. static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
  124. static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
  125. int sfmode, int sfcount, struct in6_addr *psfsrc,
  126. int delta);
  127. static int ip6_mc_add_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_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
  131. struct inet6_dev *idev);
  132. #define IGMP6_UNSOLICITED_IVAL (10*HZ)
  133. #define MLD_QRV_DEFAULT 2
  134. #define MLD_V1_SEEN(idev) (dev_net((idev)->dev)->ipv6.devconf_all->force_mld_version == 1 || \
  135. (idev)->cnf.force_mld_version == 1 || \
  136. ((idev)->mc_v1_seen && \
  137. time_before(jiffies, (idev)->mc_v1_seen)))
  138. #define MLDV2_MASK(value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value))
  139. #define MLDV2_EXP(thresh, nbmant, nbexp, value) \
  140. ((value) < (thresh) ? (value) : \
  141. ((MLDV2_MASK(value, nbmant) | (1<<(nbmant))) << \
  142. (MLDV2_MASK((value) >> (nbmant), nbexp) + (nbexp))))
  143. #define MLDV2_MRC(value) MLDV2_EXP(0x8000, 12, 3, value)
  144. #define IPV6_MLD_MAX_MSF 64
  145. int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
  146. /*
  147. * socket join on multicast group
  148. */
  149. int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
  150. {
  151. struct net_device *dev = NULL;
  152. struct ipv6_mc_socklist *mc_lst;
  153. struct ipv6_pinfo *np = inet6_sk(sk);
  154. struct net *net = sock_net(sk);
  155. int err;
  156. if (!ipv6_addr_is_multicast(addr))
  157. return -EINVAL;
  158. read_lock_bh(&ipv6_sk_mc_lock);
  159. for (mc_lst=np->ipv6_mc_list; mc_lst; mc_lst=mc_lst->next) {
  160. if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
  161. ipv6_addr_equal(&mc_lst->addr, addr)) {
  162. read_unlock_bh(&ipv6_sk_mc_lock);
  163. return -EADDRINUSE;
  164. }
  165. }
  166. read_unlock_bh(&ipv6_sk_mc_lock);
  167. mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
  168. if (mc_lst == NULL)
  169. return -ENOMEM;
  170. mc_lst->next = NULL;
  171. ipv6_addr_copy(&mc_lst->addr, addr);
  172. if (ifindex == 0) {
  173. struct rt6_info *rt;
  174. rt = rt6_lookup(net, addr, NULL, 0, 0);
  175. if (rt) {
  176. dev = rt->rt6i_dev;
  177. dev_hold(dev);
  178. dst_release(&rt->u.dst);
  179. }
  180. } else
  181. dev = dev_get_by_index(net, ifindex);
  182. if (dev == NULL) {
  183. sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
  184. return -ENODEV;
  185. }
  186. mc_lst->ifindex = dev->ifindex;
  187. mc_lst->sfmode = MCAST_EXCLUDE;
  188. rwlock_init(&mc_lst->sflock);
  189. mc_lst->sflist = NULL;
  190. /*
  191. * now add/increase the group membership on the device
  192. */
  193. err = ipv6_dev_mc_inc(dev, addr);
  194. if (err) {
  195. sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
  196. dev_put(dev);
  197. return err;
  198. }
  199. write_lock_bh(&ipv6_sk_mc_lock);
  200. mc_lst->next = np->ipv6_mc_list;
  201. np->ipv6_mc_list = mc_lst;
  202. write_unlock_bh(&ipv6_sk_mc_lock);
  203. dev_put(dev);
  204. return 0;
  205. }
  206. /*
  207. * socket leave on multicast group
  208. */
  209. int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
  210. {
  211. struct ipv6_pinfo *np = inet6_sk(sk);
  212. struct ipv6_mc_socklist *mc_lst, **lnk;
  213. struct net *net = sock_net(sk);
  214. write_lock_bh(&ipv6_sk_mc_lock);
  215. for (lnk = &np->ipv6_mc_list; (mc_lst = *lnk) !=NULL ; lnk = &mc_lst->next) {
  216. if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
  217. ipv6_addr_equal(&mc_lst->addr, addr)) {
  218. struct net_device *dev;
  219. *lnk = mc_lst->next;
  220. write_unlock_bh(&ipv6_sk_mc_lock);
  221. dev = dev_get_by_index(net, mc_lst->ifindex);
  222. if (dev != NULL) {
  223. struct inet6_dev *idev = in6_dev_get(dev);
  224. (void) ip6_mc_leave_src(sk, mc_lst, idev);
  225. if (idev) {
  226. __ipv6_dev_mc_dec(idev, &mc_lst->addr);
  227. in6_dev_put(idev);
  228. }
  229. dev_put(dev);
  230. } else
  231. (void) ip6_mc_leave_src(sk, mc_lst, NULL);
  232. sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
  233. return 0;
  234. }
  235. }
  236. write_unlock_bh(&ipv6_sk_mc_lock);
  237. return -EADDRNOTAVAIL;
  238. }
  239. static struct inet6_dev *ip6_mc_find_dev(struct net *net,
  240. struct in6_addr *group,
  241. int ifindex)
  242. {
  243. struct net_device *dev = NULL;
  244. struct inet6_dev *idev = NULL;
  245. if (ifindex == 0) {
  246. struct rt6_info *rt;
  247. rt = rt6_lookup(net, group, NULL, 0, 0);
  248. if (rt) {
  249. dev = rt->rt6i_dev;
  250. dev_hold(dev);
  251. dst_release(&rt->u.dst);
  252. }
  253. } else
  254. dev = dev_get_by_index(net, ifindex);
  255. if (!dev)
  256. return NULL;
  257. idev = in6_dev_get(dev);
  258. if (!idev) {
  259. dev_put(dev);
  260. return NULL;
  261. }
  262. read_lock_bh(&idev->lock);
  263. if (idev->dead) {
  264. read_unlock_bh(&idev->lock);
  265. in6_dev_put(idev);
  266. dev_put(dev);
  267. return NULL;
  268. }
  269. return idev;
  270. }
  271. void ipv6_sock_mc_close(struct sock *sk)
  272. {
  273. struct ipv6_pinfo *np = inet6_sk(sk);
  274. struct ipv6_mc_socklist *mc_lst;
  275. struct net *net = sock_net(sk);
  276. write_lock_bh(&ipv6_sk_mc_lock);
  277. while ((mc_lst = np->ipv6_mc_list) != NULL) {
  278. struct net_device *dev;
  279. np->ipv6_mc_list = mc_lst->next;
  280. write_unlock_bh(&ipv6_sk_mc_lock);
  281. dev = dev_get_by_index(net, mc_lst->ifindex);
  282. if (dev) {
  283. struct inet6_dev *idev = in6_dev_get(dev);
  284. (void) ip6_mc_leave_src(sk, mc_lst, idev);
  285. if (idev) {
  286. __ipv6_dev_mc_dec(idev, &mc_lst->addr);
  287. in6_dev_put(idev);
  288. }
  289. dev_put(dev);
  290. } else
  291. (void) ip6_mc_leave_src(sk, mc_lst, NULL);
  292. sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
  293. write_lock_bh(&ipv6_sk_mc_lock);
  294. }
  295. write_unlock_bh(&ipv6_sk_mc_lock);
  296. }
  297. int ip6_mc_source(int add, int omode, struct sock *sk,
  298. struct group_source_req *pgsr)
  299. {
  300. struct in6_addr *source, *group;
  301. struct ipv6_mc_socklist *pmc;
  302. struct net_device *dev;
  303. struct inet6_dev *idev;
  304. struct ipv6_pinfo *inet6 = inet6_sk(sk);
  305. struct ip6_sf_socklist *psl;
  306. struct net *net = sock_net(sk);
  307. int i, j, rv;
  308. int leavegroup = 0;
  309. int pmclocked = 0;
  310. int err;
  311. source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
  312. group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
  313. if (!ipv6_addr_is_multicast(group))
  314. return -EINVAL;
  315. idev = ip6_mc_find_dev(net, group, pgsr->gsr_interface);
  316. if (!idev)
  317. return -ENODEV;
  318. dev = idev->dev;
  319. err = -EADDRNOTAVAIL;
  320. read_lock_bh(&ipv6_sk_mc_lock);
  321. for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
  322. if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
  323. continue;
  324. if (ipv6_addr_equal(&pmc->addr, group))
  325. break;
  326. }
  327. if (!pmc) { /* must have a prior join */
  328. err = -EINVAL;
  329. goto done;
  330. }
  331. /* if a source filter was set, must be the same mode as before */
  332. if (pmc->sflist) {
  333. if (pmc->sfmode != omode) {
  334. err = -EINVAL;
  335. goto done;
  336. }
  337. } else if (pmc->sfmode != omode) {
  338. /* allow mode switches for empty-set filters */
  339. ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
  340. ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
  341. pmc->sfmode = omode;
  342. }
  343. write_lock_bh(&pmc->sflock);
  344. pmclocked = 1;
  345. psl = pmc->sflist;
  346. if (!add) {
  347. if (!psl)
  348. goto done; /* err = -EADDRNOTAVAIL */
  349. rv = !0;
  350. for (i=0; i<psl->sl_count; i++) {
  351. rv = memcmp(&psl->sl_addr[i], source,
  352. sizeof(struct in6_addr));
  353. if (rv == 0)
  354. break;
  355. }
  356. if (rv) /* source not found */
  357. goto done; /* err = -EADDRNOTAVAIL */
  358. /* special case - (INCLUDE, empty) == LEAVE_GROUP */
  359. if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
  360. leavegroup = 1;
  361. goto done;
  362. }
  363. /* update the interface filter */
  364. ip6_mc_del_src(idev, group, omode, 1, source, 1);
  365. for (j=i+1; j<psl->sl_count; j++)
  366. psl->sl_addr[j-1] = psl->sl_addr[j];
  367. psl->sl_count--;
  368. err = 0;
  369. goto done;
  370. }
  371. /* else, add a new source to the filter */
  372. if (psl && psl->sl_count >= sysctl_mld_max_msf) {
  373. err = -ENOBUFS;
  374. goto done;
  375. }
  376. if (!psl || psl->sl_count == psl->sl_max) {
  377. struct ip6_sf_socklist *newpsl;
  378. int count = IP6_SFBLOCK;
  379. if (psl)
  380. count += psl->sl_max;
  381. newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
  382. if (!newpsl) {
  383. err = -ENOBUFS;
  384. goto done;
  385. }
  386. newpsl->sl_max = count;
  387. newpsl->sl_count = count - IP6_SFBLOCK;
  388. if (psl) {
  389. for (i=0; i<psl->sl_count; i++)
  390. newpsl->sl_addr[i] = psl->sl_addr[i];
  391. sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
  392. }
  393. pmc->sflist = psl = newpsl;
  394. }
  395. rv = 1; /* > 0 for insert logic below if sl_count is 0 */
  396. for (i=0; i<psl->sl_count; i++) {
  397. rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
  398. if (rv == 0)
  399. break;
  400. }
  401. if (rv == 0) /* address already there is an error */
  402. goto done;
  403. for (j=psl->sl_count-1; j>=i; j--)
  404. psl->sl_addr[j+1] = psl->sl_addr[j];
  405. psl->sl_addr[i] = *source;
  406. psl->sl_count++;
  407. err = 0;
  408. /* update the interface list */
  409. ip6_mc_add_src(idev, group, omode, 1, source, 1);
  410. done:
  411. if (pmclocked)
  412. write_unlock_bh(&pmc->sflock);
  413. read_unlock_bh(&ipv6_sk_mc_lock);
  414. read_unlock_bh(&idev->lock);
  415. in6_dev_put(idev);
  416. dev_put(dev);
  417. if (leavegroup)
  418. return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
  419. return err;
  420. }
  421. int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
  422. {
  423. struct in6_addr *group;
  424. struct ipv6_mc_socklist *pmc;
  425. struct net_device *dev;
  426. struct inet6_dev *idev;
  427. struct ipv6_pinfo *inet6 = inet6_sk(sk);
  428. struct ip6_sf_socklist *newpsl, *psl;
  429. struct net *net = sock_net(sk);
  430. int leavegroup = 0;
  431. int i, err;
  432. group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
  433. if (!ipv6_addr_is_multicast(group))
  434. return -EINVAL;
  435. if (gsf->gf_fmode != MCAST_INCLUDE &&
  436. gsf->gf_fmode != MCAST_EXCLUDE)
  437. return -EINVAL;
  438. idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
  439. if (!idev)
  440. return -ENODEV;
  441. dev = idev->dev;
  442. err = 0;
  443. read_lock_bh(&ipv6_sk_mc_lock);
  444. if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
  445. leavegroup = 1;
  446. goto done;
  447. }
  448. for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
  449. if (pmc->ifindex != gsf->gf_interface)
  450. continue;
  451. if (ipv6_addr_equal(&pmc->addr, group))
  452. break;
  453. }
  454. if (!pmc) { /* must have a prior join */
  455. err = -EINVAL;
  456. goto done;
  457. }
  458. if (gsf->gf_numsrc) {
  459. newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
  460. GFP_ATOMIC);
  461. if (!newpsl) {
  462. err = -ENOBUFS;
  463. goto done;
  464. }
  465. newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
  466. for (i=0; i<newpsl->sl_count; ++i) {
  467. struct sockaddr_in6 *psin6;
  468. psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
  469. newpsl->sl_addr[i] = psin6->sin6_addr;
  470. }
  471. err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
  472. newpsl->sl_count, newpsl->sl_addr, 0);
  473. if (err) {
  474. sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
  475. goto done;
  476. }
  477. } else {
  478. newpsl = NULL;
  479. (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
  480. }
  481. write_lock_bh(&pmc->sflock);
  482. psl = pmc->sflist;
  483. if (psl) {
  484. (void) ip6_mc_del_src(idev, group, pmc->sfmode,
  485. psl->sl_count, psl->sl_addr, 0);
  486. sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
  487. } else
  488. (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
  489. pmc->sflist = newpsl;
  490. pmc->sfmode = gsf->gf_fmode;
  491. write_unlock_bh(&pmc->sflock);
  492. err = 0;
  493. done:
  494. read_unlock_bh(&ipv6_sk_mc_lock);
  495. read_unlock_bh(&idev->lock);
  496. in6_dev_put(idev);
  497. dev_put(dev);
  498. if (leavegroup)
  499. err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
  500. return err;
  501. }
  502. int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
  503. struct group_filter __user *optval, int __user *optlen)
  504. {
  505. int err, i, count, copycount;
  506. struct in6_addr *group;
  507. struct ipv6_mc_socklist *pmc;
  508. struct inet6_dev *idev;
  509. struct net_device *dev;
  510. struct ipv6_pinfo *inet6 = inet6_sk(sk);
  511. struct ip6_sf_socklist *psl;
  512. struct net *net = sock_net(sk);
  513. group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
  514. if (!ipv6_addr_is_multicast(group))
  515. return -EINVAL;
  516. idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
  517. if (!idev)
  518. return -ENODEV;
  519. dev = idev->dev;
  520. err = -EADDRNOTAVAIL;
  521. /*
  522. * changes to the ipv6_mc_list require the socket lock and
  523. * a read lock on ip6_sk_mc_lock. We have the socket lock,
  524. * so reading the list is safe.
  525. */
  526. for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
  527. if (pmc->ifindex != gsf->gf_interface)
  528. continue;
  529. if (ipv6_addr_equal(group, &pmc->addr))
  530. break;
  531. }
  532. if (!pmc) /* must have a prior join */
  533. goto done;
  534. gsf->gf_fmode = pmc->sfmode;
  535. psl = pmc->sflist;
  536. count = psl ? psl->sl_count : 0;
  537. read_unlock_bh(&idev->lock);
  538. in6_dev_put(idev);
  539. dev_put(dev);
  540. copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
  541. gsf->gf_numsrc = count;
  542. if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
  543. copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
  544. return -EFAULT;
  545. }
  546. /* changes to psl require the socket lock, a read lock on
  547. * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
  548. * have the socket lock, so reading here is safe.
  549. */
  550. for (i=0; i<copycount; i++) {
  551. struct sockaddr_in6 *psin6;
  552. struct sockaddr_storage ss;
  553. psin6 = (struct sockaddr_in6 *)&ss;
  554. memset(&ss, 0, sizeof(ss));
  555. psin6->sin6_family = AF_INET6;
  556. psin6->sin6_addr = psl->sl_addr[i];
  557. if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
  558. return -EFAULT;
  559. }
  560. return 0;
  561. done:
  562. read_unlock_bh(&idev->lock);
  563. in6_dev_put(idev);
  564. dev_put(dev);
  565. return err;
  566. }
  567. int inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
  568. const struct in6_addr *src_addr)
  569. {
  570. struct ipv6_pinfo *np = inet6_sk(sk);
  571. struct ipv6_mc_socklist *mc;
  572. struct ip6_sf_socklist *psl;
  573. int rv = 1;
  574. read_lock(&ipv6_sk_mc_lock);
  575. for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
  576. if (ipv6_addr_equal(&mc->addr, mc_addr))
  577. break;
  578. }
  579. if (!mc) {
  580. read_unlock(&ipv6_sk_mc_lock);
  581. return 1;
  582. }
  583. read_lock(&mc->sflock);
  584. psl = mc->sflist;
  585. if (!psl) {
  586. rv = mc->sfmode == MCAST_EXCLUDE;
  587. } else {
  588. int i;
  589. for (i=0; i<psl->sl_count; i++) {
  590. if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
  591. break;
  592. }
  593. if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
  594. rv = 0;
  595. if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
  596. rv = 0;
  597. }
  598. read_unlock(&mc->sflock);
  599. read_unlock(&ipv6_sk_mc_lock);
  600. return rv;
  601. }
  602. static void ma_put(struct ifmcaddr6 *mc)
  603. {
  604. if (atomic_dec_and_test(&mc->mca_refcnt)) {
  605. in6_dev_put(mc->idev);
  606. kfree(mc);
  607. }
  608. }
  609. static void igmp6_group_added(struct ifmcaddr6 *mc)
  610. {
  611. struct net_device *dev = mc->idev->dev;
  612. char buf[MAX_ADDR_LEN];
  613. spin_lock_bh(&mc->mca_lock);
  614. if (!(mc->mca_flags&MAF_LOADED)) {
  615. mc->mca_flags |= MAF_LOADED;
  616. if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
  617. dev_mc_add(dev, buf, dev->addr_len, 0);
  618. }
  619. spin_unlock_bh(&mc->mca_lock);
  620. if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
  621. return;
  622. if (MLD_V1_SEEN(mc->idev)) {
  623. igmp6_join_group(mc);
  624. return;
  625. }
  626. /* else v2 */
  627. mc->mca_crcount = mc->idev->mc_qrv;
  628. mld_ifc_event(mc->idev);
  629. }
  630. static void igmp6_group_dropped(struct ifmcaddr6 *mc)
  631. {
  632. struct net_device *dev = mc->idev->dev;
  633. char buf[MAX_ADDR_LEN];
  634. spin_lock_bh(&mc->mca_lock);
  635. if (mc->mca_flags&MAF_LOADED) {
  636. mc->mca_flags &= ~MAF_LOADED;
  637. if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
  638. dev_mc_delete(dev, buf, dev->addr_len, 0);
  639. }
  640. if (mc->mca_flags & MAF_NOREPORT)
  641. goto done;
  642. spin_unlock_bh(&mc->mca_lock);
  643. if (!mc->idev->dead)
  644. igmp6_leave_group(mc);
  645. spin_lock_bh(&mc->mca_lock);
  646. if (del_timer(&mc->mca_timer))
  647. atomic_dec(&mc->mca_refcnt);
  648. done:
  649. ip6_mc_clear_src(mc);
  650. spin_unlock_bh(&mc->mca_lock);
  651. }
  652. /*
  653. * deleted ifmcaddr6 manipulation
  654. */
  655. static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
  656. {
  657. struct ifmcaddr6 *pmc;
  658. /* this is an "ifmcaddr6" for convenience; only the fields below
  659. * are actually used. In particular, the refcnt and users are not
  660. * used for management of the delete list. Using the same structure
  661. * for deleted items allows change reports to use common code with
  662. * non-deleted or query-response MCA's.
  663. */
  664. pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
  665. if (!pmc)
  666. return;
  667. spin_lock_bh(&im->mca_lock);
  668. spin_lock_init(&pmc->mca_lock);
  669. pmc->idev = im->idev;
  670. in6_dev_hold(idev);
  671. pmc->mca_addr = im->mca_addr;
  672. pmc->mca_crcount = idev->mc_qrv;
  673. pmc->mca_sfmode = im->mca_sfmode;
  674. if (pmc->mca_sfmode == MCAST_INCLUDE) {
  675. struct ip6_sf_list *psf;
  676. pmc->mca_tomb = im->mca_tomb;
  677. pmc->mca_sources = im->mca_sources;
  678. im->mca_tomb = im->mca_sources = NULL;
  679. for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
  680. psf->sf_crcount = pmc->mca_crcount;
  681. }
  682. spin_unlock_bh(&im->mca_lock);
  683. write_lock_bh(&idev->mc_lock);
  684. pmc->next = idev->mc_tomb;
  685. idev->mc_tomb = pmc;
  686. write_unlock_bh(&idev->mc_lock);
  687. }
  688. static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
  689. {
  690. struct ifmcaddr6 *pmc, *pmc_prev;
  691. struct ip6_sf_list *psf, *psf_next;
  692. write_lock_bh(&idev->mc_lock);
  693. pmc_prev = NULL;
  694. for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
  695. if (ipv6_addr_equal(&pmc->mca_addr, pmca))
  696. break;
  697. pmc_prev = pmc;
  698. }
  699. if (pmc) {
  700. if (pmc_prev)
  701. pmc_prev->next = pmc->next;
  702. else
  703. idev->mc_tomb = pmc->next;
  704. }
  705. write_unlock_bh(&idev->mc_lock);
  706. if (pmc) {
  707. for (psf=pmc->mca_tomb; psf; psf=psf_next) {
  708. psf_next = psf->sf_next;
  709. kfree(psf);
  710. }
  711. in6_dev_put(pmc->idev);
  712. kfree(pmc);
  713. }
  714. }
  715. static void mld_clear_delrec(struct inet6_dev *idev)
  716. {
  717. struct ifmcaddr6 *pmc, *nextpmc;
  718. write_lock_bh(&idev->mc_lock);
  719. pmc = idev->mc_tomb;
  720. idev->mc_tomb = NULL;
  721. write_unlock_bh(&idev->mc_lock);
  722. for (; pmc; pmc = nextpmc) {
  723. nextpmc = pmc->next;
  724. ip6_mc_clear_src(pmc);
  725. in6_dev_put(pmc->idev);
  726. kfree(pmc);
  727. }
  728. /* clear dead sources, too */
  729. read_lock_bh(&idev->lock);
  730. for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
  731. struct ip6_sf_list *psf, *psf_next;
  732. spin_lock_bh(&pmc->mca_lock);
  733. psf = pmc->mca_tomb;
  734. pmc->mca_tomb = NULL;
  735. spin_unlock_bh(&pmc->mca_lock);
  736. for (; psf; psf=psf_next) {
  737. psf_next = psf->sf_next;
  738. kfree(psf);
  739. }
  740. }
  741. read_unlock_bh(&idev->lock);
  742. }
  743. /*
  744. * device multicast group inc (add if not found)
  745. */
  746. int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
  747. {
  748. struct ifmcaddr6 *mc;
  749. struct inet6_dev *idev;
  750. idev = in6_dev_get(dev);
  751. if (idev == NULL)
  752. return -EINVAL;
  753. write_lock_bh(&idev->lock);
  754. if (idev->dead) {
  755. write_unlock_bh(&idev->lock);
  756. in6_dev_put(idev);
  757. return -ENODEV;
  758. }
  759. for (mc = idev->mc_list; mc; mc = mc->next) {
  760. if (ipv6_addr_equal(&mc->mca_addr, addr)) {
  761. mc->mca_users++;
  762. write_unlock_bh(&idev->lock);
  763. ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
  764. NULL, 0);
  765. in6_dev_put(idev);
  766. return 0;
  767. }
  768. }
  769. /*
  770. * not found: create a new one.
  771. */
  772. mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
  773. if (mc == NULL) {
  774. write_unlock_bh(&idev->lock);
  775. in6_dev_put(idev);
  776. return -ENOMEM;
  777. }
  778. setup_timer(&mc->mca_timer, igmp6_timer_handler, (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, const 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, const 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 = icmp6_hdr(skb);
  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, const struct in6_addr *group,
  859. const 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(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
  1001. len -= skb_network_header_len(skb);
  1002. /* Drop queries with not link local source */
  1003. if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
  1004. return -EINVAL;
  1005. idev = in6_dev_get(skb->dev);
  1006. if (idev == NULL)
  1007. return 0;
  1008. hdr = icmp6_hdr(skb);
  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_transport_header(skb);
  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_transport_header(skb);
  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 = icmp6_hdr(skb);
  1117. /* Drop reports with not link local source */
  1118. addr_type = ipv6_addr_type(&ipv6_hdr(skb)->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 net *net = dev_net(dev);
  1202. struct sock *sk = net->ipv6.igmp_sk;
  1203. struct sk_buff *skb;
  1204. struct mld2_report *pmr;
  1205. struct in6_addr addr_buf;
  1206. const struct in6_addr *saddr;
  1207. int err;
  1208. u8 ra[8] = { IPPROTO_ICMPV6, 0,
  1209. IPV6_TLV_ROUTERALERT, 2, 0, 0,
  1210. IPV6_TLV_PADN, 0 };
  1211. /* we assume size > sizeof(ra) here */
  1212. skb = sock_alloc_send_skb(sk, size + LL_ALLOCATED_SPACE(dev), 1, &err);
  1213. if (!skb)
  1214. return NULL;
  1215. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  1216. if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
  1217. /* <draft-ietf-magma-mld-source-05.txt>:
  1218. * use unspecified address as the source address
  1219. * when a valid link-local address is not available.
  1220. */
  1221. saddr = &in6addr_any;
  1222. } else
  1223. saddr = &addr_buf;
  1224. ip6_nd_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
  1225. memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
  1226. skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
  1227. skb_put(skb, sizeof(*pmr));
  1228. pmr = (struct mld2_report *)skb_transport_header(skb);
  1229. pmr->type = ICMPV6_MLD2_REPORT;
  1230. pmr->resv1 = 0;
  1231. pmr->csum = 0;
  1232. pmr->resv2 = 0;
  1233. pmr->ngrec = 0;
  1234. return skb;
  1235. }
  1236. static void mld_sendpack(struct sk_buff *skb)
  1237. {
  1238. struct ipv6hdr *pip6 = ipv6_hdr(skb);
  1239. struct mld2_report *pmr =
  1240. (struct mld2_report *)skb_transport_header(skb);
  1241. int payload_len, mldlen;
  1242. struct inet6_dev *idev = in6_dev_get(skb->dev);
  1243. struct net *net = dev_net(skb->dev);
  1244. int err;
  1245. struct flowi fl;
  1246. IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTREQUESTS);
  1247. payload_len = (skb->tail - skb->network_header) - sizeof(*pip6);
  1248. mldlen = skb->tail - skb->transport_header;
  1249. pip6->payload_len = htons(payload_len);
  1250. pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
  1251. IPPROTO_ICMPV6, csum_partial(skb_transport_header(skb),
  1252. mldlen, 0));
  1253. skb->dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
  1254. if (!skb->dst) {
  1255. err = -ENOMEM;
  1256. goto err_out;
  1257. }
  1258. icmpv6_flow_init(net->ipv6.igmp_sk, &fl, ICMPV6_MLD2_REPORT,
  1259. &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
  1260. skb->dev->ifindex);
  1261. err = xfrm_lookup(&skb->dst, &fl, NULL, 0);
  1262. if (err)
  1263. goto err_out;
  1264. err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
  1265. dst_output);
  1266. out:
  1267. if (!err) {
  1268. ICMP6MSGOUT_INC_STATS_BH(net, idev, ICMPV6_MLD2_REPORT);
  1269. ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
  1270. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTMCASTPKTS);
  1271. } else
  1272. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTDISCARDS);
  1273. if (likely(idev != NULL))
  1274. in6_dev_put(idev);
  1275. return;
  1276. err_out:
  1277. kfree_skb(skb);
  1278. goto out;
  1279. }
  1280. static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
  1281. {
  1282. return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
  1283. }
  1284. static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
  1285. int type, struct mld2_grec **ppgr)
  1286. {
  1287. struct net_device *dev = pmc->idev->dev;
  1288. struct mld2_report *pmr;
  1289. struct mld2_grec *pgr;
  1290. if (!skb)
  1291. skb = mld_newpack(dev, dev->mtu);
  1292. if (!skb)
  1293. return NULL;
  1294. pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
  1295. pgr->grec_type = type;
  1296. pgr->grec_auxwords = 0;
  1297. pgr->grec_nsrcs = 0;
  1298. pgr->grec_mca = pmc->mca_addr; /* structure copy */
  1299. pmr = (struct mld2_report *)skb_transport_header(skb);
  1300. pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
  1301. *ppgr = pgr;
  1302. return skb;
  1303. }
  1304. #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
  1305. skb_tailroom(skb)) : 0)
  1306. static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
  1307. int type, int gdeleted, int sdeleted)
  1308. {
  1309. struct net_device *dev = pmc->idev->dev;
  1310. struct mld2_report *pmr;
  1311. struct mld2_grec *pgr = NULL;
  1312. struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
  1313. int scount, stotal, first, isquery, truncate;
  1314. if (pmc->mca_flags & MAF_NOREPORT)
  1315. return skb;
  1316. isquery = type == MLD2_MODE_IS_INCLUDE ||
  1317. type == MLD2_MODE_IS_EXCLUDE;
  1318. truncate = type == MLD2_MODE_IS_EXCLUDE ||
  1319. type == MLD2_CHANGE_TO_EXCLUDE;
  1320. stotal = scount = 0;
  1321. psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
  1322. if (!*psf_list)
  1323. goto empty_source;
  1324. pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
  1325. /* EX and TO_EX get a fresh packet, if needed */
  1326. if (truncate) {
  1327. if (pmr && pmr->ngrec &&
  1328. AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
  1329. if (skb)
  1330. mld_sendpack(skb);
  1331. skb = mld_newpack(dev, dev->mtu);
  1332. }
  1333. }
  1334. first = 1;
  1335. psf_prev = NULL;
  1336. for (psf=*psf_list; psf; psf=psf_next) {
  1337. struct in6_addr *psrc;
  1338. psf_next = psf->sf_next;
  1339. if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
  1340. psf_prev = psf;
  1341. continue;
  1342. }
  1343. /* clear marks on query responses */
  1344. if (isquery)
  1345. psf->sf_gsresp = 0;
  1346. if (AVAILABLE(skb) < sizeof(*psrc) +
  1347. first*sizeof(struct mld2_grec)) {
  1348. if (truncate && !first)
  1349. break; /* truncate these */
  1350. if (pgr)
  1351. pgr->grec_nsrcs = htons(scount);
  1352. if (skb)
  1353. mld_sendpack(skb);
  1354. skb = mld_newpack(dev, dev->mtu);
  1355. first = 1;
  1356. scount = 0;
  1357. }
  1358. if (first) {
  1359. skb = add_grhead(skb, pmc, type, &pgr);
  1360. first = 0;
  1361. }
  1362. if (!skb)
  1363. return NULL;
  1364. psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
  1365. *psrc = psf->sf_addr;
  1366. scount++; stotal++;
  1367. if ((type == MLD2_ALLOW_NEW_SOURCES ||
  1368. type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
  1369. psf->sf_crcount--;
  1370. if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
  1371. if (psf_prev)
  1372. psf_prev->sf_next = psf->sf_next;
  1373. else
  1374. *psf_list = psf->sf_next;
  1375. kfree(psf);
  1376. continue;
  1377. }
  1378. }
  1379. psf_prev = psf;
  1380. }
  1381. empty_source:
  1382. if (!stotal) {
  1383. if (type == MLD2_ALLOW_NEW_SOURCES ||
  1384. type == MLD2_BLOCK_OLD_SOURCES)
  1385. return skb;
  1386. if (pmc->mca_crcount || isquery) {
  1387. /* make sure we have room for group header */
  1388. if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
  1389. mld_sendpack(skb);
  1390. skb = NULL; /* add_grhead will get a new one */
  1391. }
  1392. skb = add_grhead(skb, pmc, type, &pgr);
  1393. }
  1394. }
  1395. if (pgr)
  1396. pgr->grec_nsrcs = htons(scount);
  1397. if (isquery)
  1398. pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
  1399. return skb;
  1400. }
  1401. static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
  1402. {
  1403. struct sk_buff *skb = NULL;
  1404. int type;
  1405. if (!pmc) {
  1406. read_lock_bh(&idev->lock);
  1407. for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
  1408. if (pmc->mca_flags & MAF_NOREPORT)
  1409. continue;
  1410. spin_lock_bh(&pmc->mca_lock);
  1411. if (pmc->mca_sfcount[MCAST_EXCLUDE])
  1412. type = MLD2_MODE_IS_EXCLUDE;
  1413. else
  1414. type = MLD2_MODE_IS_INCLUDE;
  1415. skb = add_grec(skb, pmc, type, 0, 0);
  1416. spin_unlock_bh(&pmc->mca_lock);
  1417. }
  1418. read_unlock_bh(&idev->lock);
  1419. } else {
  1420. spin_lock_bh(&pmc->mca_lock);
  1421. if (pmc->mca_sfcount[MCAST_EXCLUDE])
  1422. type = MLD2_MODE_IS_EXCLUDE;
  1423. else
  1424. type = MLD2_MODE_IS_INCLUDE;
  1425. skb = add_grec(skb, pmc, type, 0, 0);
  1426. spin_unlock_bh(&pmc->mca_lock);
  1427. }
  1428. if (skb)
  1429. mld_sendpack(skb);
  1430. }
  1431. /*
  1432. * remove zero-count source records from a source filter list
  1433. */
  1434. static void mld_clear_zeros(struct ip6_sf_list **ppsf)
  1435. {
  1436. struct ip6_sf_list *psf_prev, *psf_next, *psf;
  1437. psf_prev = NULL;
  1438. for (psf=*ppsf; psf; psf = psf_next) {
  1439. psf_next = psf->sf_next;
  1440. if (psf->sf_crcount == 0) {
  1441. if (psf_prev)
  1442. psf_prev->sf_next = psf->sf_next;
  1443. else
  1444. *ppsf = psf->sf_next;
  1445. kfree(psf);
  1446. } else
  1447. psf_prev = psf;
  1448. }
  1449. }
  1450. static void mld_send_cr(struct inet6_dev *idev)
  1451. {
  1452. struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
  1453. struct sk_buff *skb = NULL;
  1454. int type, dtype;
  1455. read_lock_bh(&idev->lock);
  1456. write_lock_bh(&idev->mc_lock);
  1457. /* deleted MCA's */
  1458. pmc_prev = NULL;
  1459. for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
  1460. pmc_next = pmc->next;
  1461. if (pmc->mca_sfmode == MCAST_INCLUDE) {
  1462. type = MLD2_BLOCK_OLD_SOURCES;
  1463. dtype = MLD2_BLOCK_OLD_SOURCES;
  1464. skb = add_grec(skb, pmc, type, 1, 0);
  1465. skb = add_grec(skb, pmc, dtype, 1, 1);
  1466. }
  1467. if (pmc->mca_crcount) {
  1468. if (pmc->mca_sfmode == MCAST_EXCLUDE) {
  1469. type = MLD2_CHANGE_TO_INCLUDE;
  1470. skb = add_grec(skb, pmc, type, 1, 0);
  1471. }
  1472. pmc->mca_crcount--;
  1473. if (pmc->mca_crcount == 0) {
  1474. mld_clear_zeros(&pmc->mca_tomb);
  1475. mld_clear_zeros(&pmc->mca_sources);
  1476. }
  1477. }
  1478. if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
  1479. !pmc->mca_sources) {
  1480. if (pmc_prev)
  1481. pmc_prev->next = pmc_next;
  1482. else
  1483. idev->mc_tomb = pmc_next;
  1484. in6_dev_put(pmc->idev);
  1485. kfree(pmc);
  1486. } else
  1487. pmc_prev = pmc;
  1488. }
  1489. write_unlock_bh(&idev->mc_lock);
  1490. /* change recs */
  1491. for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
  1492. spin_lock_bh(&pmc->mca_lock);
  1493. if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
  1494. type = MLD2_BLOCK_OLD_SOURCES;
  1495. dtype = MLD2_ALLOW_NEW_SOURCES;
  1496. } else {
  1497. type = MLD2_ALLOW_NEW_SOURCES;
  1498. dtype = MLD2_BLOCK_OLD_SOURCES;
  1499. }
  1500. skb = add_grec(skb, pmc, type, 0, 0);
  1501. skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
  1502. /* filter mode changes */
  1503. if (pmc->mca_crcount) {
  1504. if (pmc->mca_sfmode == MCAST_EXCLUDE)
  1505. type = MLD2_CHANGE_TO_EXCLUDE;
  1506. else
  1507. type = MLD2_CHANGE_TO_INCLUDE;
  1508. skb = add_grec(skb, pmc, type, 0, 0);
  1509. pmc->mca_crcount--;
  1510. }
  1511. spin_unlock_bh(&pmc->mca_lock);
  1512. }
  1513. read_unlock_bh(&idev->lock);
  1514. if (!skb)
  1515. return;
  1516. (void) mld_sendpack(skb);
  1517. }
  1518. static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
  1519. {
  1520. struct net *net = dev_net(dev);
  1521. struct sock *sk = net->ipv6.igmp_sk;
  1522. struct inet6_dev *idev;
  1523. struct sk_buff *skb;
  1524. struct icmp6hdr *hdr;
  1525. const struct in6_addr *snd_addr, *saddr;
  1526. struct in6_addr *addrp;
  1527. struct in6_addr addr_buf;
  1528. int err, len, payload_len, full_len;
  1529. u8 ra[8] = { IPPROTO_ICMPV6, 0,
  1530. IPV6_TLV_ROUTERALERT, 2, 0, 0,
  1531. IPV6_TLV_PADN, 0 };
  1532. struct flowi fl;
  1533. rcu_read_lock();
  1534. IP6_INC_STATS(net, __in6_dev_get(dev),
  1535. IPSTATS_MIB_OUTREQUESTS);
  1536. rcu_read_unlock();
  1537. if (type == ICMPV6_MGM_REDUCTION)
  1538. snd_addr = &in6addr_linklocal_allrouters;
  1539. else
  1540. snd_addr = addr;
  1541. len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
  1542. payload_len = len + sizeof(ra);
  1543. full_len = sizeof(struct ipv6hdr) + payload_len;
  1544. skb = sock_alloc_send_skb(sk, LL_ALLOCATED_SPACE(dev) + full_len, 1, &err);
  1545. if (skb == NULL) {
  1546. rcu_read_lock();
  1547. IP6_INC_STATS(net, __in6_dev_get(dev),
  1548. IPSTATS_MIB_OUTDISCARDS);
  1549. rcu_read_unlock();
  1550. return;
  1551. }
  1552. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  1553. if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
  1554. /* <draft-ietf-magma-mld-source-05.txt>:
  1555. * use unspecified address as the source address
  1556. * when a valid link-local address is not available.
  1557. */
  1558. saddr = &in6addr_any;
  1559. } else
  1560. saddr = &addr_buf;
  1561. ip6_nd_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
  1562. memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
  1563. hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
  1564. memset(hdr, 0, sizeof(struct icmp6hdr));
  1565. hdr->icmp6_type = type;
  1566. addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
  1567. ipv6_addr_copy(addrp, addr);
  1568. hdr->icmp6_cksum = csum_ipv6_magic(saddr, snd_addr, len,
  1569. IPPROTO_ICMPV6,
  1570. csum_partial((__u8 *) hdr, len, 0));
  1571. idev = in6_dev_get(skb->dev);
  1572. skb->dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
  1573. if (!skb->dst) {
  1574. err = -ENOMEM;
  1575. goto err_out;
  1576. }
  1577. icmpv6_flow_init(sk, &fl, type,
  1578. &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
  1579. skb->dev->ifindex);
  1580. err = xfrm_lookup(&skb->dst, &fl, NULL, 0);
  1581. if (err)
  1582. goto err_out;
  1583. err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
  1584. dst_output);
  1585. out:
  1586. if (!err) {
  1587. ICMP6MSGOUT_INC_STATS(net, idev, type);
  1588. ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
  1589. IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTMCASTPKTS);
  1590. } else
  1591. IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
  1592. if (likely(idev != NULL))
  1593. in6_dev_put(idev);
  1594. return;
  1595. err_out:
  1596. kfree_skb(skb);
  1597. goto out;
  1598. }
  1599. static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
  1600. struct in6_addr *psfsrc)
  1601. {
  1602. struct ip6_sf_list *psf, *psf_prev;
  1603. int rv = 0;
  1604. psf_prev = NULL;
  1605. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  1606. if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
  1607. break;
  1608. psf_prev = psf;
  1609. }
  1610. if (!psf || psf->sf_count[sfmode] == 0) {
  1611. /* source filter not found, or count wrong => bug */
  1612. return -ESRCH;
  1613. }
  1614. psf->sf_count[sfmode]--;
  1615. if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
  1616. struct inet6_dev *idev = pmc->idev;
  1617. /* no more filters for this source */
  1618. if (psf_prev)
  1619. psf_prev->sf_next = psf->sf_next;
  1620. else
  1621. pmc->mca_sources = psf->sf_next;
  1622. if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
  1623. !MLD_V1_SEEN(idev)) {
  1624. psf->sf_crcount = idev->mc_qrv;
  1625. psf->sf_next = pmc->mca_tomb;
  1626. pmc->mca_tomb = psf;
  1627. rv = 1;
  1628. } else
  1629. kfree(psf);
  1630. }
  1631. return rv;
  1632. }
  1633. static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
  1634. int sfmode, int sfcount, struct in6_addr *psfsrc,
  1635. int delta)
  1636. {
  1637. struct ifmcaddr6 *pmc;
  1638. int changerec = 0;
  1639. int i, err;
  1640. if (!idev)
  1641. return -ENODEV;
  1642. read_lock_bh(&idev->lock);
  1643. for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
  1644. if (ipv6_addr_equal(pmca, &pmc->mca_addr))
  1645. break;
  1646. }
  1647. if (!pmc) {
  1648. /* MCA not found?? bug */
  1649. read_unlock_bh(&idev->lock);
  1650. return -ESRCH;
  1651. }
  1652. spin_lock_bh(&pmc->mca_lock);
  1653. sf_markstate(pmc);
  1654. if (!delta) {
  1655. if (!pmc->mca_sfcount[sfmode]) {
  1656. spin_unlock_bh(&pmc->mca_lock);
  1657. read_unlock_bh(&idev->lock);
  1658. return -EINVAL;
  1659. }
  1660. pmc->mca_sfcount[sfmode]--;
  1661. }
  1662. err = 0;
  1663. for (i=0; i<sfcount; i++) {
  1664. int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1665. changerec |= rv > 0;
  1666. if (!err && rv < 0)
  1667. err = rv;
  1668. }
  1669. if (pmc->mca_sfmode == MCAST_EXCLUDE &&
  1670. pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
  1671. pmc->mca_sfcount[MCAST_INCLUDE]) {
  1672. struct ip6_sf_list *psf;
  1673. /* filter mode change */
  1674. pmc->mca_sfmode = MCAST_INCLUDE;
  1675. pmc->mca_crcount = idev->mc_qrv;
  1676. idev->mc_ifc_count = pmc->mca_crcount;
  1677. for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
  1678. psf->sf_crcount = 0;
  1679. mld_ifc_event(pmc->idev);
  1680. } else if (sf_setstate(pmc) || changerec)
  1681. mld_ifc_event(pmc->idev);
  1682. spin_unlock_bh(&pmc->mca_lock);
  1683. read_unlock_bh(&idev->lock);
  1684. return err;
  1685. }
  1686. /*
  1687. * Add multicast single-source filter to the interface list
  1688. */
  1689. static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
  1690. struct in6_addr *psfsrc, int delta)
  1691. {
  1692. struct ip6_sf_list *psf, *psf_prev;
  1693. psf_prev = NULL;
  1694. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  1695. if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
  1696. break;
  1697. psf_prev = psf;
  1698. }
  1699. if (!psf) {
  1700. psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
  1701. if (!psf)
  1702. return -ENOBUFS;
  1703. psf->sf_addr = *psfsrc;
  1704. if (psf_prev) {
  1705. psf_prev->sf_next = psf;
  1706. } else
  1707. pmc->mca_sources = psf;
  1708. }
  1709. psf->sf_count[sfmode]++;
  1710. return 0;
  1711. }
  1712. static void sf_markstate(struct ifmcaddr6 *pmc)
  1713. {
  1714. struct ip6_sf_list *psf;
  1715. int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
  1716. for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
  1717. if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
  1718. psf->sf_oldin = mca_xcount ==
  1719. psf->sf_count[MCAST_EXCLUDE] &&
  1720. !psf->sf_count[MCAST_INCLUDE];
  1721. } else
  1722. psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
  1723. }
  1724. static int sf_setstate(struct ifmcaddr6 *pmc)
  1725. {
  1726. struct ip6_sf_list *psf, *dpsf;
  1727. int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
  1728. int qrv = pmc->idev->mc_qrv;
  1729. int new_in, rv;
  1730. rv = 0;
  1731. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  1732. if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
  1733. new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
  1734. !psf->sf_count[MCAST_INCLUDE];
  1735. } else
  1736. new_in = psf->sf_count[MCAST_INCLUDE] != 0;
  1737. if (new_in) {
  1738. if (!psf->sf_oldin) {
  1739. struct ip6_sf_list *prev = NULL;
  1740. for (dpsf=pmc->mca_tomb; dpsf;
  1741. dpsf=dpsf->sf_next) {
  1742. if (ipv6_addr_equal(&dpsf->sf_addr,
  1743. &psf->sf_addr))
  1744. break;
  1745. prev = dpsf;
  1746. }
  1747. if (dpsf) {
  1748. if (prev)
  1749. prev->sf_next = dpsf->sf_next;
  1750. else
  1751. pmc->mca_tomb = dpsf->sf_next;
  1752. kfree(dpsf);
  1753. }
  1754. psf->sf_crcount = qrv;
  1755. rv++;
  1756. }
  1757. } else if (psf->sf_oldin) {
  1758. psf->sf_crcount = 0;
  1759. /*
  1760. * add or update "delete" records if an active filter
  1761. * is now inactive
  1762. */
  1763. for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
  1764. if (ipv6_addr_equal(&dpsf->sf_addr,
  1765. &psf->sf_addr))
  1766. break;
  1767. if (!dpsf) {
  1768. dpsf = (struct ip6_sf_list *)
  1769. kmalloc(sizeof(*dpsf), GFP_ATOMIC);
  1770. if (!dpsf)
  1771. continue;
  1772. *dpsf = *psf;
  1773. /* pmc->mca_lock held by callers */
  1774. dpsf->sf_next = pmc->mca_tomb;
  1775. pmc->mca_tomb = dpsf;
  1776. }
  1777. dpsf->sf_crcount = qrv;
  1778. rv++;
  1779. }
  1780. }
  1781. return rv;
  1782. }
  1783. /*
  1784. * Add multicast source filter list to the interface list
  1785. */
  1786. static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
  1787. int sfmode, int sfcount, struct in6_addr *psfsrc,
  1788. int delta)
  1789. {
  1790. struct ifmcaddr6 *pmc;
  1791. int isexclude;
  1792. int i, err;
  1793. if (!idev)
  1794. return -ENODEV;
  1795. read_lock_bh(&idev->lock);
  1796. for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
  1797. if (ipv6_addr_equal(pmca, &pmc->mca_addr))
  1798. break;
  1799. }
  1800. if (!pmc) {
  1801. /* MCA not found?? bug */
  1802. read_unlock_bh(&idev->lock);
  1803. return -ESRCH;
  1804. }
  1805. spin_lock_bh(&pmc->mca_lock);
  1806. sf_markstate(pmc);
  1807. isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
  1808. if (!delta)
  1809. pmc->mca_sfcount[sfmode]++;
  1810. err = 0;
  1811. for (i=0; i<sfcount; i++) {
  1812. err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
  1813. if (err)
  1814. break;
  1815. }
  1816. if (err) {
  1817. int j;
  1818. if (!delta)
  1819. pmc->mca_sfcount[sfmode]--;
  1820. for (j=0; j<i; j++)
  1821. (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1822. } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
  1823. struct inet6_dev *idev = pmc->idev;
  1824. struct ip6_sf_list *psf;
  1825. /* filter mode change */
  1826. if (pmc->mca_sfcount[MCAST_EXCLUDE])
  1827. pmc->mca_sfmode = MCAST_EXCLUDE;
  1828. else if (pmc->mca_sfcount[MCAST_INCLUDE])
  1829. pmc->mca_sfmode = MCAST_INCLUDE;
  1830. /* else no filters; keep old mode for reports */
  1831. pmc->mca_crcount = idev->mc_qrv;
  1832. idev->mc_ifc_count = pmc->mca_crcount;
  1833. for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
  1834. psf->sf_crcount = 0;
  1835. mld_ifc_event(idev);
  1836. } else if (sf_setstate(pmc))
  1837. mld_ifc_event(idev);
  1838. spin_unlock_bh(&pmc->mca_lock);
  1839. read_unlock_bh(&idev->lock);
  1840. return err;
  1841. }
  1842. static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
  1843. {
  1844. struct ip6_sf_list *psf, *nextpsf;
  1845. for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
  1846. nextpsf = psf->sf_next;
  1847. kfree(psf);
  1848. }
  1849. pmc->mca_tomb = NULL;
  1850. for (psf=pmc->mca_sources; psf; psf=nextpsf) {
  1851. nextpsf = psf->sf_next;
  1852. kfree(psf);
  1853. }
  1854. pmc->mca_sources = NULL;
  1855. pmc->mca_sfmode = MCAST_EXCLUDE;
  1856. pmc->mca_sfcount[MCAST_INCLUDE] = 0;
  1857. pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
  1858. }
  1859. static void igmp6_join_group(struct ifmcaddr6 *ma)
  1860. {
  1861. unsigned long delay;
  1862. if (ma->mca_flags & MAF_NOREPORT)
  1863. return;
  1864. igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
  1865. delay = net_random() % IGMP6_UNSOLICITED_IVAL;
  1866. spin_lock_bh(&ma->mca_lock);
  1867. if (del_timer(&ma->mca_timer)) {
  1868. atomic_dec(&ma->mca_refcnt);
  1869. delay = ma->mca_timer.expires - jiffies;
  1870. }
  1871. if (!mod_timer(&ma->mca_timer, jiffies + delay))
  1872. atomic_inc(&ma->mca_refcnt);
  1873. ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
  1874. spin_unlock_bh(&ma->mca_lock);
  1875. }
  1876. static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
  1877. struct inet6_dev *idev)
  1878. {
  1879. int err;
  1880. /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
  1881. * so no other readers or writers of iml or its sflist
  1882. */
  1883. if (!iml->sflist) {
  1884. /* any-source empty exclude case */
  1885. return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
  1886. }
  1887. err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
  1888. iml->sflist->sl_count, iml->sflist->sl_addr, 0);
  1889. sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
  1890. iml->sflist = NULL;
  1891. return err;
  1892. }
  1893. static void igmp6_leave_group(struct ifmcaddr6 *ma)
  1894. {
  1895. if (MLD_V1_SEEN(ma->idev)) {
  1896. if (ma->mca_flags & MAF_LAST_REPORTER)
  1897. igmp6_send(&ma->mca_addr, ma->idev->dev,
  1898. ICMPV6_MGM_REDUCTION);
  1899. } else {
  1900. mld_add_delrec(ma->idev, ma);
  1901. mld_ifc_event(ma->idev);
  1902. }
  1903. }
  1904. static void mld_gq_timer_expire(unsigned long data)
  1905. {
  1906. struct inet6_dev *idev = (struct inet6_dev *)data;
  1907. idev->mc_gq_running = 0;
  1908. mld_send_report(idev, NULL);
  1909. __in6_dev_put(idev);
  1910. }
  1911. static void mld_ifc_timer_expire(unsigned long data)
  1912. {
  1913. struct inet6_dev *idev = (struct inet6_dev *)data;
  1914. mld_send_cr(idev);
  1915. if (idev->mc_ifc_count) {
  1916. idev->mc_ifc_count--;
  1917. if (idev->mc_ifc_count)
  1918. mld_ifc_start_timer(idev, idev->mc_maxdelay);
  1919. }
  1920. __in6_dev_put(idev);
  1921. }
  1922. static void mld_ifc_event(struct inet6_dev *idev)
  1923. {
  1924. if (MLD_V1_SEEN(idev))
  1925. return;
  1926. idev->mc_ifc_count = idev->mc_qrv;
  1927. mld_ifc_start_timer(idev, 1);
  1928. }
  1929. static void igmp6_timer_handler(unsigned long data)
  1930. {
  1931. struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
  1932. if (MLD_V1_SEEN(ma->idev))
  1933. igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
  1934. else
  1935. mld_send_report(ma->idev, ma);
  1936. spin_lock(&ma->mca_lock);
  1937. ma->mca_flags |= MAF_LAST_REPORTER;
  1938. ma->mca_flags &= ~MAF_TIMER_RUNNING;
  1939. spin_unlock(&ma->mca_lock);
  1940. ma_put(ma);
  1941. }
  1942. /* Device going down */
  1943. void ipv6_mc_down(struct inet6_dev *idev)
  1944. {
  1945. struct ifmcaddr6 *i;
  1946. /* Withdraw multicast list */
  1947. read_lock_bh(&idev->lock);
  1948. idev->mc_ifc_count = 0;
  1949. if (del_timer(&idev->mc_ifc_timer))
  1950. __in6_dev_put(idev);
  1951. idev->mc_gq_running = 0;
  1952. if (del_timer(&idev->mc_gq_timer))
  1953. __in6_dev_put(idev);
  1954. for (i = idev->mc_list; i; i=i->next)
  1955. igmp6_group_dropped(i);
  1956. read_unlock_bh(&idev->lock);
  1957. mld_clear_delrec(idev);
  1958. }
  1959. /* Device going up */
  1960. void ipv6_mc_up(struct inet6_dev *idev)
  1961. {
  1962. struct ifmcaddr6 *i;
  1963. /* Install multicast list, except for all-nodes (already installed) */
  1964. read_lock_bh(&idev->lock);
  1965. for (i = idev->mc_list; i; i=i->next)
  1966. igmp6_group_added(i);
  1967. read_unlock_bh(&idev->lock);
  1968. }
  1969. /* IPv6 device initialization. */
  1970. void ipv6_mc_init_dev(struct inet6_dev *idev)
  1971. {
  1972. write_lock_bh(&idev->lock);
  1973. rwlock_init(&idev->mc_lock);
  1974. idev->mc_gq_running = 0;
  1975. setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
  1976. (unsigned long)idev);
  1977. idev->mc_tomb = NULL;
  1978. idev->mc_ifc_count = 0;
  1979. setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
  1980. (unsigned long)idev);
  1981. idev->mc_qrv = MLD_QRV_DEFAULT;
  1982. idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
  1983. idev->mc_v1_seen = 0;
  1984. write_unlock_bh(&idev->lock);
  1985. }
  1986. /*
  1987. * Device is about to be destroyed: clean up.
  1988. */
  1989. void ipv6_mc_destroy_dev(struct inet6_dev *idev)
  1990. {
  1991. struct ifmcaddr6 *i;
  1992. /* Deactivate timers */
  1993. ipv6_mc_down(idev);
  1994. /* Delete all-nodes address. */
  1995. /* We cannot call ipv6_dev_mc_dec() directly, our caller in
  1996. * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
  1997. * fail.
  1998. */
  1999. __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
  2000. if (idev->cnf.forwarding)
  2001. __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
  2002. write_lock_bh(&idev->lock);
  2003. while ((i = idev->mc_list) != NULL) {
  2004. idev->mc_list = i->next;
  2005. write_unlock_bh(&idev->lock);
  2006. igmp6_group_dropped(i);
  2007. ma_put(i);
  2008. write_lock_bh(&idev->lock);
  2009. }
  2010. write_unlock_bh(&idev->lock);
  2011. }
  2012. #ifdef CONFIG_PROC_FS
  2013. struct igmp6_mc_iter_state {
  2014. struct seq_net_private p;
  2015. struct net_device *dev;
  2016. struct inet6_dev *idev;
  2017. };
  2018. #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
  2019. static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
  2020. {
  2021. struct ifmcaddr6 *im = NULL;
  2022. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  2023. struct net *net = seq_file_net(seq);
  2024. state->idev = NULL;
  2025. for_each_netdev(net, state->dev) {
  2026. struct inet6_dev *idev;
  2027. idev = in6_dev_get(state->dev);
  2028. if (!idev)
  2029. continue;
  2030. read_lock_bh(&idev->lock);
  2031. im = idev->mc_list;
  2032. if (im) {
  2033. state->idev = idev;
  2034. break;
  2035. }
  2036. read_unlock_bh(&idev->lock);
  2037. in6_dev_put(idev);
  2038. }
  2039. return im;
  2040. }
  2041. static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
  2042. {
  2043. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  2044. im = im->next;
  2045. while (!im) {
  2046. if (likely(state->idev != NULL)) {
  2047. read_unlock_bh(&state->idev->lock);
  2048. in6_dev_put(state->idev);
  2049. }
  2050. state->dev = next_net_device(state->dev);
  2051. if (!state->dev) {
  2052. state->idev = NULL;
  2053. break;
  2054. }
  2055. state->idev = in6_dev_get(state->dev);
  2056. if (!state->idev)
  2057. continue;
  2058. read_lock_bh(&state->idev->lock);
  2059. im = state->idev->mc_list;
  2060. }
  2061. return im;
  2062. }
  2063. static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
  2064. {
  2065. struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
  2066. if (im)
  2067. while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
  2068. --pos;
  2069. return pos ? NULL : im;
  2070. }
  2071. static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
  2072. __acquires(dev_base_lock)
  2073. {
  2074. read_lock(&dev_base_lock);
  2075. return igmp6_mc_get_idx(seq, *pos);
  2076. }
  2077. static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2078. {
  2079. struct ifmcaddr6 *im;
  2080. im = igmp6_mc_get_next(seq, v);
  2081. ++*pos;
  2082. return im;
  2083. }
  2084. static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
  2085. __releases(dev_base_lock)
  2086. {
  2087. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  2088. if (likely(state->idev != NULL)) {
  2089. read_unlock_bh(&state->idev->lock);
  2090. in6_dev_put(state->idev);
  2091. state->idev = NULL;
  2092. }
  2093. state->dev = NULL;
  2094. read_unlock(&dev_base_lock);
  2095. }
  2096. static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
  2097. {
  2098. struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
  2099. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  2100. seq_printf(seq,
  2101. "%-4d %-15s " NIP6_SEQFMT " %5d %08X %ld\n",
  2102. state->dev->ifindex, state->dev->name,
  2103. NIP6(im->mca_addr),
  2104. im->mca_users, im->mca_flags,
  2105. (im->mca_flags&MAF_TIMER_RUNNING) ?
  2106. jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
  2107. return 0;
  2108. }
  2109. static const struct seq_operations igmp6_mc_seq_ops = {
  2110. .start = igmp6_mc_seq_start,
  2111. .next = igmp6_mc_seq_next,
  2112. .stop = igmp6_mc_seq_stop,
  2113. .show = igmp6_mc_seq_show,
  2114. };
  2115. static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
  2116. {
  2117. return seq_open_net(inode, file, &igmp6_mc_seq_ops,
  2118. sizeof(struct igmp6_mc_iter_state));
  2119. }
  2120. static const struct file_operations igmp6_mc_seq_fops = {
  2121. .owner = THIS_MODULE,
  2122. .open = igmp6_mc_seq_open,
  2123. .read = seq_read,
  2124. .llseek = seq_lseek,
  2125. .release = seq_release_net,
  2126. };
  2127. struct igmp6_mcf_iter_state {
  2128. struct seq_net_private p;
  2129. struct net_device *dev;
  2130. struct inet6_dev *idev;
  2131. struct ifmcaddr6 *im;
  2132. };
  2133. #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
  2134. static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
  2135. {
  2136. struct ip6_sf_list *psf = NULL;
  2137. struct ifmcaddr6 *im = NULL;
  2138. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2139. struct net *net = seq_file_net(seq);
  2140. state->idev = NULL;
  2141. state->im = NULL;
  2142. for_each_netdev(net, state->dev) {
  2143. struct inet6_dev *idev;
  2144. idev = in6_dev_get(state->dev);
  2145. if (unlikely(idev == NULL))
  2146. continue;
  2147. read_lock_bh(&idev->lock);
  2148. im = idev->mc_list;
  2149. if (likely(im != NULL)) {
  2150. spin_lock_bh(&im->mca_lock);
  2151. psf = im->mca_sources;
  2152. if (likely(psf != NULL)) {
  2153. state->im = im;
  2154. state->idev = idev;
  2155. break;
  2156. }
  2157. spin_unlock_bh(&im->mca_lock);
  2158. }
  2159. read_unlock_bh(&idev->lock);
  2160. in6_dev_put(idev);
  2161. }
  2162. return psf;
  2163. }
  2164. static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
  2165. {
  2166. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2167. psf = psf->sf_next;
  2168. while (!psf) {
  2169. spin_unlock_bh(&state->im->mca_lock);
  2170. state->im = state->im->next;
  2171. while (!state->im) {
  2172. if (likely(state->idev != NULL)) {
  2173. read_unlock_bh(&state->idev->lock);
  2174. in6_dev_put(state->idev);
  2175. }
  2176. state->dev = next_net_device(state->dev);
  2177. if (!state->dev) {
  2178. state->idev = NULL;
  2179. goto out;
  2180. }
  2181. state->idev = in6_dev_get(state->dev);
  2182. if (!state->idev)
  2183. continue;
  2184. read_lock_bh(&state->idev->lock);
  2185. state->im = state->idev->mc_list;
  2186. }
  2187. if (!state->im)
  2188. break;
  2189. spin_lock_bh(&state->im->mca_lock);
  2190. psf = state->im->mca_sources;
  2191. }
  2192. out:
  2193. return psf;
  2194. }
  2195. static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
  2196. {
  2197. struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
  2198. if (psf)
  2199. while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
  2200. --pos;
  2201. return pos ? NULL : psf;
  2202. }
  2203. static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
  2204. __acquires(dev_base_lock)
  2205. {
  2206. read_lock(&dev_base_lock);
  2207. return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2208. }
  2209. static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2210. {
  2211. struct ip6_sf_list *psf;
  2212. if (v == SEQ_START_TOKEN)
  2213. psf = igmp6_mcf_get_first(seq);
  2214. else
  2215. psf = igmp6_mcf_get_next(seq, v);
  2216. ++*pos;
  2217. return psf;
  2218. }
  2219. static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
  2220. __releases(dev_base_lock)
  2221. {
  2222. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2223. if (likely(state->im != NULL)) {
  2224. spin_unlock_bh(&state->im->mca_lock);
  2225. state->im = NULL;
  2226. }
  2227. if (likely(state->idev != NULL)) {
  2228. read_unlock_bh(&state->idev->lock);
  2229. in6_dev_put(state->idev);
  2230. state->idev = NULL;
  2231. }
  2232. state->dev = NULL;
  2233. read_unlock(&dev_base_lock);
  2234. }
  2235. static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
  2236. {
  2237. struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
  2238. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2239. if (v == SEQ_START_TOKEN) {
  2240. seq_printf(seq,
  2241. "%3s %6s "
  2242. "%32s %32s %6s %6s\n", "Idx",
  2243. "Device", "Multicast Address",
  2244. "Source Address", "INC", "EXC");
  2245. } else {
  2246. seq_printf(seq,
  2247. "%3d %6.6s " NIP6_SEQFMT " " NIP6_SEQFMT " %6lu %6lu\n",
  2248. state->dev->ifindex, state->dev->name,
  2249. NIP6(state->im->mca_addr),
  2250. NIP6(psf->sf_addr),
  2251. psf->sf_count[MCAST_INCLUDE],
  2252. psf->sf_count[MCAST_EXCLUDE]);
  2253. }
  2254. return 0;
  2255. }
  2256. static const struct seq_operations igmp6_mcf_seq_ops = {
  2257. .start = igmp6_mcf_seq_start,
  2258. .next = igmp6_mcf_seq_next,
  2259. .stop = igmp6_mcf_seq_stop,
  2260. .show = igmp6_mcf_seq_show,
  2261. };
  2262. static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
  2263. {
  2264. return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
  2265. sizeof(struct igmp6_mcf_iter_state));
  2266. }
  2267. static const struct file_operations igmp6_mcf_seq_fops = {
  2268. .owner = THIS_MODULE,
  2269. .open = igmp6_mcf_seq_open,
  2270. .read = seq_read,
  2271. .llseek = seq_lseek,
  2272. .release = seq_release_net,
  2273. };
  2274. static int igmp6_proc_init(struct net *net)
  2275. {
  2276. int err;
  2277. err = -ENOMEM;
  2278. if (!proc_net_fops_create(net, "igmp6", S_IRUGO, &igmp6_mc_seq_fops))
  2279. goto out;
  2280. if (!proc_net_fops_create(net, "mcfilter6", S_IRUGO,
  2281. &igmp6_mcf_seq_fops))
  2282. goto out_proc_net_igmp6;
  2283. err = 0;
  2284. out:
  2285. return err;
  2286. out_proc_net_igmp6:
  2287. proc_net_remove(net, "igmp6");
  2288. goto out;
  2289. }
  2290. static void igmp6_proc_exit(struct net *net)
  2291. {
  2292. proc_net_remove(net, "mcfilter6");
  2293. proc_net_remove(net, "igmp6");
  2294. }
  2295. #else
  2296. static int igmp6_proc_init(struct net *net)
  2297. {
  2298. return 0;
  2299. }
  2300. static void igmp6_proc_exit(struct net *net)
  2301. {
  2302. ;
  2303. }
  2304. #endif
  2305. static int igmp6_net_init(struct net *net)
  2306. {
  2307. int err;
  2308. err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
  2309. SOCK_RAW, IPPROTO_ICMPV6, net);
  2310. if (err < 0) {
  2311. printk(KERN_ERR
  2312. "Failed to initialize the IGMP6 control socket (err %d).\n",
  2313. err);
  2314. goto out;
  2315. }
  2316. inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
  2317. err = igmp6_proc_init(net);
  2318. if (err)
  2319. goto out_sock_create;
  2320. out:
  2321. return err;
  2322. out_sock_create:
  2323. inet_ctl_sock_destroy(net->ipv6.igmp_sk);
  2324. goto out;
  2325. }
  2326. static void igmp6_net_exit(struct net *net)
  2327. {
  2328. inet_ctl_sock_destroy(net->ipv6.igmp_sk);
  2329. igmp6_proc_exit(net);
  2330. }
  2331. static struct pernet_operations igmp6_net_ops = {
  2332. .init = igmp6_net_init,
  2333. .exit = igmp6_net_exit,
  2334. };
  2335. int __init igmp6_init(void)
  2336. {
  2337. return register_pernet_subsys(&igmp6_net_ops);
  2338. }
  2339. void igmp6_cleanup(void)
  2340. {
  2341. unregister_pernet_subsys(&igmp6_net_ops);
  2342. }