mcast.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952
  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/slab.h>
  45. #include <linux/pkt_sched.h>
  46. #include <net/mld.h>
  47. #include <linux/netfilter.h>
  48. #include <linux/netfilter_ipv6.h>
  49. #include <net/net_namespace.h>
  50. #include <net/sock.h>
  51. #include <net/snmp.h>
  52. #include <net/ipv6.h>
  53. #include <net/protocol.h>
  54. #include <net/if_inet6.h>
  55. #include <net/ndisc.h>
  56. #include <net/addrconf.h>
  57. #include <net/ip6_route.h>
  58. #include <net/inet_common.h>
  59. #include <net/ip6_checksum.h>
  60. /* Ensure that we have struct in6_addr aligned on 32bit word. */
  61. static void *__mld2_query_bugs[] __attribute__((__unused__)) = {
  62. BUILD_BUG_ON_NULL(offsetof(struct mld2_query, mld2q_srcs) % 4),
  63. BUILD_BUG_ON_NULL(offsetof(struct mld2_report, mld2r_grec) % 4),
  64. BUILD_BUG_ON_NULL(offsetof(struct mld2_grec, grec_mca) % 4)
  65. };
  66. static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
  67. static void igmp6_join_group(struct ifmcaddr6 *ma);
  68. static void igmp6_leave_group(struct ifmcaddr6 *ma);
  69. static void igmp6_timer_handler(unsigned long data);
  70. static void mld_gq_timer_expire(unsigned long data);
  71. static void mld_ifc_timer_expire(unsigned long data);
  72. static void mld_ifc_event(struct inet6_dev *idev);
  73. static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
  74. static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *addr);
  75. static void mld_clear_delrec(struct inet6_dev *idev);
  76. static bool mld_in_v1_mode(const struct inet6_dev *idev);
  77. static int sf_setstate(struct ifmcaddr6 *pmc);
  78. static void sf_markstate(struct ifmcaddr6 *pmc);
  79. static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
  80. static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
  81. int sfmode, int sfcount, const struct in6_addr *psfsrc,
  82. int delta);
  83. static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
  84. int sfmode, int sfcount, const struct in6_addr *psfsrc,
  85. int delta);
  86. static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
  87. struct inet6_dev *idev);
  88. #define MLD_QRV_DEFAULT 2
  89. /* RFC3810, 9.2. Query Interval */
  90. #define MLD_QI_DEFAULT (125 * HZ)
  91. /* RFC3810, 9.3. Query Response Interval */
  92. #define MLD_QRI_DEFAULT (10 * HZ)
  93. /* RFC3810, 8.1 Query Version Distinctions */
  94. #define MLD_V1_QUERY_LEN 24
  95. #define MLD_V2_QUERY_LEN_MIN 28
  96. #define IPV6_MLD_MAX_MSF 64
  97. int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
  98. int sysctl_mld_qrv __read_mostly = MLD_QRV_DEFAULT;
  99. /*
  100. * socket join on multicast group
  101. */
  102. #define for_each_pmc_rcu(np, pmc) \
  103. for (pmc = rcu_dereference(np->ipv6_mc_list); \
  104. pmc != NULL; \
  105. pmc = rcu_dereference(pmc->next))
  106. static int unsolicited_report_interval(struct inet6_dev *idev)
  107. {
  108. int iv;
  109. if (mld_in_v1_mode(idev))
  110. iv = idev->cnf.mldv1_unsolicited_report_interval;
  111. else
  112. iv = idev->cnf.mldv2_unsolicited_report_interval;
  113. return iv > 0 ? iv : 1;
  114. }
  115. int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
  116. {
  117. struct net_device *dev = NULL;
  118. struct ipv6_mc_socklist *mc_lst;
  119. struct ipv6_pinfo *np = inet6_sk(sk);
  120. struct net *net = sock_net(sk);
  121. int err;
  122. ASSERT_RTNL();
  123. if (!ipv6_addr_is_multicast(addr))
  124. return -EINVAL;
  125. rcu_read_lock();
  126. for_each_pmc_rcu(np, mc_lst) {
  127. if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
  128. ipv6_addr_equal(&mc_lst->addr, addr)) {
  129. rcu_read_unlock();
  130. return -EADDRINUSE;
  131. }
  132. }
  133. rcu_read_unlock();
  134. mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
  135. if (!mc_lst)
  136. return -ENOMEM;
  137. mc_lst->next = NULL;
  138. mc_lst->addr = *addr;
  139. if (ifindex == 0) {
  140. struct rt6_info *rt;
  141. rt = rt6_lookup(net, addr, NULL, 0, 0);
  142. if (rt) {
  143. dev = rt->dst.dev;
  144. ip6_rt_put(rt);
  145. }
  146. } else
  147. dev = __dev_get_by_index(net, ifindex);
  148. if (!dev) {
  149. sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
  150. return -ENODEV;
  151. }
  152. mc_lst->ifindex = dev->ifindex;
  153. mc_lst->sfmode = MCAST_EXCLUDE;
  154. rwlock_init(&mc_lst->sflock);
  155. mc_lst->sflist = NULL;
  156. /*
  157. * now add/increase the group membership on the device
  158. */
  159. err = ipv6_dev_mc_inc(dev, addr);
  160. if (err) {
  161. sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
  162. return err;
  163. }
  164. mc_lst->next = np->ipv6_mc_list;
  165. rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
  166. return 0;
  167. }
  168. EXPORT_SYMBOL(ipv6_sock_mc_join);
  169. /*
  170. * socket leave on multicast group
  171. */
  172. int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
  173. {
  174. struct ipv6_pinfo *np = inet6_sk(sk);
  175. struct ipv6_mc_socklist *mc_lst;
  176. struct ipv6_mc_socklist __rcu **lnk;
  177. struct net *net = sock_net(sk);
  178. ASSERT_RTNL();
  179. if (!ipv6_addr_is_multicast(addr))
  180. return -EINVAL;
  181. for (lnk = &np->ipv6_mc_list;
  182. (mc_lst = rtnl_dereference(*lnk)) != NULL;
  183. lnk = &mc_lst->next) {
  184. if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
  185. ipv6_addr_equal(&mc_lst->addr, addr)) {
  186. struct net_device *dev;
  187. *lnk = mc_lst->next;
  188. dev = __dev_get_by_index(net, mc_lst->ifindex);
  189. if (dev) {
  190. struct inet6_dev *idev = __in6_dev_get(dev);
  191. (void) ip6_mc_leave_src(sk, mc_lst, idev);
  192. if (idev)
  193. __ipv6_dev_mc_dec(idev, &mc_lst->addr);
  194. } else
  195. (void) ip6_mc_leave_src(sk, mc_lst, NULL);
  196. atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
  197. kfree_rcu(mc_lst, rcu);
  198. return 0;
  199. }
  200. }
  201. return -EADDRNOTAVAIL;
  202. }
  203. EXPORT_SYMBOL(ipv6_sock_mc_drop);
  204. /* called with rcu_read_lock() */
  205. static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net,
  206. const struct in6_addr *group,
  207. int ifindex)
  208. {
  209. struct net_device *dev = NULL;
  210. struct inet6_dev *idev = NULL;
  211. if (ifindex == 0) {
  212. struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, 0);
  213. if (rt) {
  214. dev = rt->dst.dev;
  215. ip6_rt_put(rt);
  216. }
  217. } else
  218. dev = dev_get_by_index_rcu(net, ifindex);
  219. if (!dev)
  220. return NULL;
  221. idev = __in6_dev_get(dev);
  222. if (!idev)
  223. return NULL;
  224. read_lock_bh(&idev->lock);
  225. if (idev->dead) {
  226. read_unlock_bh(&idev->lock);
  227. return NULL;
  228. }
  229. return idev;
  230. }
  231. void ipv6_sock_mc_close(struct sock *sk)
  232. {
  233. struct ipv6_pinfo *np = inet6_sk(sk);
  234. struct ipv6_mc_socklist *mc_lst;
  235. struct net *net = sock_net(sk);
  236. if (!rcu_access_pointer(np->ipv6_mc_list))
  237. return;
  238. rtnl_lock();
  239. while ((mc_lst = rtnl_dereference(np->ipv6_mc_list)) != NULL) {
  240. struct net_device *dev;
  241. np->ipv6_mc_list = mc_lst->next;
  242. dev = __dev_get_by_index(net, mc_lst->ifindex);
  243. if (dev) {
  244. struct inet6_dev *idev = __in6_dev_get(dev);
  245. (void) ip6_mc_leave_src(sk, mc_lst, idev);
  246. if (idev)
  247. __ipv6_dev_mc_dec(idev, &mc_lst->addr);
  248. } else
  249. (void) ip6_mc_leave_src(sk, mc_lst, NULL);
  250. atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
  251. kfree_rcu(mc_lst, rcu);
  252. }
  253. rtnl_unlock();
  254. }
  255. int ip6_mc_source(int add, int omode, struct sock *sk,
  256. struct group_source_req *pgsr)
  257. {
  258. struct in6_addr *source, *group;
  259. struct ipv6_mc_socklist *pmc;
  260. struct inet6_dev *idev;
  261. struct ipv6_pinfo *inet6 = inet6_sk(sk);
  262. struct ip6_sf_socklist *psl;
  263. struct net *net = sock_net(sk);
  264. int i, j, rv;
  265. int leavegroup = 0;
  266. int pmclocked = 0;
  267. int err;
  268. source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
  269. group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
  270. if (!ipv6_addr_is_multicast(group))
  271. return -EINVAL;
  272. rcu_read_lock();
  273. idev = ip6_mc_find_dev_rcu(net, group, pgsr->gsr_interface);
  274. if (!idev) {
  275. rcu_read_unlock();
  276. return -ENODEV;
  277. }
  278. err = -EADDRNOTAVAIL;
  279. for_each_pmc_rcu(inet6, pmc) {
  280. if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
  281. continue;
  282. if (ipv6_addr_equal(&pmc->addr, group))
  283. break;
  284. }
  285. if (!pmc) { /* must have a prior join */
  286. err = -EINVAL;
  287. goto done;
  288. }
  289. /* if a source filter was set, must be the same mode as before */
  290. if (pmc->sflist) {
  291. if (pmc->sfmode != omode) {
  292. err = -EINVAL;
  293. goto done;
  294. }
  295. } else if (pmc->sfmode != omode) {
  296. /* allow mode switches for empty-set filters */
  297. ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
  298. ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
  299. pmc->sfmode = omode;
  300. }
  301. write_lock(&pmc->sflock);
  302. pmclocked = 1;
  303. psl = pmc->sflist;
  304. if (!add) {
  305. if (!psl)
  306. goto done; /* err = -EADDRNOTAVAIL */
  307. rv = !0;
  308. for (i = 0; i < psl->sl_count; i++) {
  309. rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
  310. if (rv == 0)
  311. break;
  312. }
  313. if (rv) /* source not found */
  314. goto done; /* err = -EADDRNOTAVAIL */
  315. /* special case - (INCLUDE, empty) == LEAVE_GROUP */
  316. if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
  317. leavegroup = 1;
  318. goto done;
  319. }
  320. /* update the interface filter */
  321. ip6_mc_del_src(idev, group, omode, 1, source, 1);
  322. for (j = i+1; j < psl->sl_count; j++)
  323. psl->sl_addr[j-1] = psl->sl_addr[j];
  324. psl->sl_count--;
  325. err = 0;
  326. goto done;
  327. }
  328. /* else, add a new source to the filter */
  329. if (psl && psl->sl_count >= sysctl_mld_max_msf) {
  330. err = -ENOBUFS;
  331. goto done;
  332. }
  333. if (!psl || psl->sl_count == psl->sl_max) {
  334. struct ip6_sf_socklist *newpsl;
  335. int count = IP6_SFBLOCK;
  336. if (psl)
  337. count += psl->sl_max;
  338. newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
  339. if (!newpsl) {
  340. err = -ENOBUFS;
  341. goto done;
  342. }
  343. newpsl->sl_max = count;
  344. newpsl->sl_count = count - IP6_SFBLOCK;
  345. if (psl) {
  346. for (i = 0; i < psl->sl_count; i++)
  347. newpsl->sl_addr[i] = psl->sl_addr[i];
  348. sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
  349. }
  350. pmc->sflist = psl = newpsl;
  351. }
  352. rv = 1; /* > 0 for insert logic below if sl_count is 0 */
  353. for (i = 0; i < psl->sl_count; i++) {
  354. rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
  355. if (rv == 0) /* There is an error in the address. */
  356. goto done;
  357. }
  358. for (j = psl->sl_count-1; j >= i; j--)
  359. psl->sl_addr[j+1] = psl->sl_addr[j];
  360. psl->sl_addr[i] = *source;
  361. psl->sl_count++;
  362. err = 0;
  363. /* update the interface list */
  364. ip6_mc_add_src(idev, group, omode, 1, source, 1);
  365. done:
  366. if (pmclocked)
  367. write_unlock(&pmc->sflock);
  368. read_unlock_bh(&idev->lock);
  369. rcu_read_unlock();
  370. if (leavegroup)
  371. err = ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
  372. return err;
  373. }
  374. int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
  375. {
  376. const struct in6_addr *group;
  377. struct ipv6_mc_socklist *pmc;
  378. struct inet6_dev *idev;
  379. struct ipv6_pinfo *inet6 = inet6_sk(sk);
  380. struct ip6_sf_socklist *newpsl, *psl;
  381. struct net *net = sock_net(sk);
  382. int leavegroup = 0;
  383. int i, err;
  384. group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
  385. if (!ipv6_addr_is_multicast(group))
  386. return -EINVAL;
  387. if (gsf->gf_fmode != MCAST_INCLUDE &&
  388. gsf->gf_fmode != MCAST_EXCLUDE)
  389. return -EINVAL;
  390. rcu_read_lock();
  391. idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
  392. if (!idev) {
  393. rcu_read_unlock();
  394. return -ENODEV;
  395. }
  396. err = 0;
  397. if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
  398. leavegroup = 1;
  399. goto done;
  400. }
  401. for_each_pmc_rcu(inet6, pmc) {
  402. if (pmc->ifindex != gsf->gf_interface)
  403. continue;
  404. if (ipv6_addr_equal(&pmc->addr, group))
  405. break;
  406. }
  407. if (!pmc) { /* must have a prior join */
  408. err = -EINVAL;
  409. goto done;
  410. }
  411. if (gsf->gf_numsrc) {
  412. newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
  413. GFP_ATOMIC);
  414. if (!newpsl) {
  415. err = -ENOBUFS;
  416. goto done;
  417. }
  418. newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
  419. for (i = 0; i < newpsl->sl_count; ++i) {
  420. struct sockaddr_in6 *psin6;
  421. psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
  422. newpsl->sl_addr[i] = psin6->sin6_addr;
  423. }
  424. err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
  425. newpsl->sl_count, newpsl->sl_addr, 0);
  426. if (err) {
  427. sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
  428. goto done;
  429. }
  430. } else {
  431. newpsl = NULL;
  432. (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
  433. }
  434. write_lock(&pmc->sflock);
  435. psl = pmc->sflist;
  436. if (psl) {
  437. (void) ip6_mc_del_src(idev, group, pmc->sfmode,
  438. psl->sl_count, psl->sl_addr, 0);
  439. sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
  440. } else
  441. (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
  442. pmc->sflist = newpsl;
  443. pmc->sfmode = gsf->gf_fmode;
  444. write_unlock(&pmc->sflock);
  445. err = 0;
  446. done:
  447. read_unlock_bh(&idev->lock);
  448. rcu_read_unlock();
  449. if (leavegroup)
  450. err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
  451. return err;
  452. }
  453. int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
  454. struct group_filter __user *optval, int __user *optlen)
  455. {
  456. int err, i, count, copycount;
  457. const struct in6_addr *group;
  458. struct ipv6_mc_socklist *pmc;
  459. struct inet6_dev *idev;
  460. struct ipv6_pinfo *inet6 = inet6_sk(sk);
  461. struct ip6_sf_socklist *psl;
  462. struct net *net = sock_net(sk);
  463. group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
  464. if (!ipv6_addr_is_multicast(group))
  465. return -EINVAL;
  466. rcu_read_lock();
  467. idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
  468. if (!idev) {
  469. rcu_read_unlock();
  470. return -ENODEV;
  471. }
  472. err = -EADDRNOTAVAIL;
  473. /* changes to the ipv6_mc_list require the socket lock and
  474. * rtnl lock. We have the socket lock and rcu read lock,
  475. * so reading the list is safe.
  476. */
  477. for_each_pmc_rcu(inet6, pmc) {
  478. if (pmc->ifindex != gsf->gf_interface)
  479. continue;
  480. if (ipv6_addr_equal(group, &pmc->addr))
  481. break;
  482. }
  483. if (!pmc) /* must have a prior join */
  484. goto done;
  485. gsf->gf_fmode = pmc->sfmode;
  486. psl = pmc->sflist;
  487. count = psl ? psl->sl_count : 0;
  488. read_unlock_bh(&idev->lock);
  489. rcu_read_unlock();
  490. copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
  491. gsf->gf_numsrc = count;
  492. if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
  493. copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
  494. return -EFAULT;
  495. }
  496. /* changes to psl require the socket lock, and a write lock
  497. * on pmc->sflock. We have the socket lock so reading here is safe.
  498. */
  499. for (i = 0; i < copycount; i++) {
  500. struct sockaddr_in6 *psin6;
  501. struct sockaddr_storage ss;
  502. psin6 = (struct sockaddr_in6 *)&ss;
  503. memset(&ss, 0, sizeof(ss));
  504. psin6->sin6_family = AF_INET6;
  505. psin6->sin6_addr = psl->sl_addr[i];
  506. if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
  507. return -EFAULT;
  508. }
  509. return 0;
  510. done:
  511. read_unlock_bh(&idev->lock);
  512. rcu_read_unlock();
  513. return err;
  514. }
  515. bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
  516. const struct in6_addr *src_addr)
  517. {
  518. struct ipv6_pinfo *np = inet6_sk(sk);
  519. struct ipv6_mc_socklist *mc;
  520. struct ip6_sf_socklist *psl;
  521. bool rv = true;
  522. rcu_read_lock();
  523. for_each_pmc_rcu(np, mc) {
  524. if (ipv6_addr_equal(&mc->addr, mc_addr))
  525. break;
  526. }
  527. if (!mc) {
  528. rcu_read_unlock();
  529. return true;
  530. }
  531. read_lock(&mc->sflock);
  532. psl = mc->sflist;
  533. if (!psl) {
  534. rv = mc->sfmode == MCAST_EXCLUDE;
  535. } else {
  536. int i;
  537. for (i = 0; i < psl->sl_count; i++) {
  538. if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
  539. break;
  540. }
  541. if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
  542. rv = false;
  543. if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
  544. rv = false;
  545. }
  546. read_unlock(&mc->sflock);
  547. rcu_read_unlock();
  548. return rv;
  549. }
  550. static void igmp6_group_added(struct ifmcaddr6 *mc)
  551. {
  552. struct net_device *dev = mc->idev->dev;
  553. char buf[MAX_ADDR_LEN];
  554. if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
  555. IPV6_ADDR_SCOPE_LINKLOCAL)
  556. return;
  557. spin_lock_bh(&mc->mca_lock);
  558. if (!(mc->mca_flags&MAF_LOADED)) {
  559. mc->mca_flags |= MAF_LOADED;
  560. if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
  561. dev_mc_add(dev, buf);
  562. }
  563. spin_unlock_bh(&mc->mca_lock);
  564. if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
  565. return;
  566. if (mld_in_v1_mode(mc->idev)) {
  567. igmp6_join_group(mc);
  568. return;
  569. }
  570. /* else v2 */
  571. mc->mca_crcount = mc->idev->mc_qrv;
  572. mld_ifc_event(mc->idev);
  573. }
  574. static void igmp6_group_dropped(struct ifmcaddr6 *mc)
  575. {
  576. struct net_device *dev = mc->idev->dev;
  577. char buf[MAX_ADDR_LEN];
  578. if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
  579. IPV6_ADDR_SCOPE_LINKLOCAL)
  580. return;
  581. spin_lock_bh(&mc->mca_lock);
  582. if (mc->mca_flags&MAF_LOADED) {
  583. mc->mca_flags &= ~MAF_LOADED;
  584. if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
  585. dev_mc_del(dev, buf);
  586. }
  587. if (mc->mca_flags & MAF_NOREPORT)
  588. goto done;
  589. spin_unlock_bh(&mc->mca_lock);
  590. if (!mc->idev->dead)
  591. igmp6_leave_group(mc);
  592. spin_lock_bh(&mc->mca_lock);
  593. if (del_timer(&mc->mca_timer))
  594. atomic_dec(&mc->mca_refcnt);
  595. done:
  596. ip6_mc_clear_src(mc);
  597. spin_unlock_bh(&mc->mca_lock);
  598. }
  599. /*
  600. * deleted ifmcaddr6 manipulation
  601. */
  602. static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
  603. {
  604. struct ifmcaddr6 *pmc;
  605. /* this is an "ifmcaddr6" for convenience; only the fields below
  606. * are actually used. In particular, the refcnt and users are not
  607. * used for management of the delete list. Using the same structure
  608. * for deleted items allows change reports to use common code with
  609. * non-deleted or query-response MCA's.
  610. */
  611. pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
  612. if (!pmc)
  613. return;
  614. spin_lock_bh(&im->mca_lock);
  615. spin_lock_init(&pmc->mca_lock);
  616. pmc->idev = im->idev;
  617. in6_dev_hold(idev);
  618. pmc->mca_addr = im->mca_addr;
  619. pmc->mca_crcount = idev->mc_qrv;
  620. pmc->mca_sfmode = im->mca_sfmode;
  621. if (pmc->mca_sfmode == MCAST_INCLUDE) {
  622. struct ip6_sf_list *psf;
  623. pmc->mca_tomb = im->mca_tomb;
  624. pmc->mca_sources = im->mca_sources;
  625. im->mca_tomb = im->mca_sources = NULL;
  626. for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
  627. psf->sf_crcount = pmc->mca_crcount;
  628. }
  629. spin_unlock_bh(&im->mca_lock);
  630. spin_lock_bh(&idev->mc_lock);
  631. pmc->next = idev->mc_tomb;
  632. idev->mc_tomb = pmc;
  633. spin_unlock_bh(&idev->mc_lock);
  634. }
  635. static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *pmca)
  636. {
  637. struct ifmcaddr6 *pmc, *pmc_prev;
  638. struct ip6_sf_list *psf, *psf_next;
  639. spin_lock_bh(&idev->mc_lock);
  640. pmc_prev = NULL;
  641. for (pmc = idev->mc_tomb; pmc; pmc = pmc->next) {
  642. if (ipv6_addr_equal(&pmc->mca_addr, pmca))
  643. break;
  644. pmc_prev = pmc;
  645. }
  646. if (pmc) {
  647. if (pmc_prev)
  648. pmc_prev->next = pmc->next;
  649. else
  650. idev->mc_tomb = pmc->next;
  651. }
  652. spin_unlock_bh(&idev->mc_lock);
  653. if (pmc) {
  654. for (psf = pmc->mca_tomb; psf; psf = psf_next) {
  655. psf_next = psf->sf_next;
  656. kfree(psf);
  657. }
  658. in6_dev_put(pmc->idev);
  659. kfree(pmc);
  660. }
  661. }
  662. static void mld_clear_delrec(struct inet6_dev *idev)
  663. {
  664. struct ifmcaddr6 *pmc, *nextpmc;
  665. spin_lock_bh(&idev->mc_lock);
  666. pmc = idev->mc_tomb;
  667. idev->mc_tomb = NULL;
  668. spin_unlock_bh(&idev->mc_lock);
  669. for (; pmc; pmc = nextpmc) {
  670. nextpmc = pmc->next;
  671. ip6_mc_clear_src(pmc);
  672. in6_dev_put(pmc->idev);
  673. kfree(pmc);
  674. }
  675. /* clear dead sources, too */
  676. read_lock_bh(&idev->lock);
  677. for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
  678. struct ip6_sf_list *psf, *psf_next;
  679. spin_lock_bh(&pmc->mca_lock);
  680. psf = pmc->mca_tomb;
  681. pmc->mca_tomb = NULL;
  682. spin_unlock_bh(&pmc->mca_lock);
  683. for (; psf; psf = psf_next) {
  684. psf_next = psf->sf_next;
  685. kfree(psf);
  686. }
  687. }
  688. read_unlock_bh(&idev->lock);
  689. }
  690. static void mca_get(struct ifmcaddr6 *mc)
  691. {
  692. atomic_inc(&mc->mca_refcnt);
  693. }
  694. static void ma_put(struct ifmcaddr6 *mc)
  695. {
  696. if (atomic_dec_and_test(&mc->mca_refcnt)) {
  697. in6_dev_put(mc->idev);
  698. kfree(mc);
  699. }
  700. }
  701. static struct ifmcaddr6 *mca_alloc(struct inet6_dev *idev,
  702. const struct in6_addr *addr)
  703. {
  704. struct ifmcaddr6 *mc;
  705. mc = kzalloc(sizeof(*mc), GFP_ATOMIC);
  706. if (!mc)
  707. return NULL;
  708. setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
  709. mc->mca_addr = *addr;
  710. mc->idev = idev; /* reference taken by caller */
  711. mc->mca_users = 1;
  712. /* mca_stamp should be updated upon changes */
  713. mc->mca_cstamp = mc->mca_tstamp = jiffies;
  714. atomic_set(&mc->mca_refcnt, 1);
  715. spin_lock_init(&mc->mca_lock);
  716. /* initial mode is (EX, empty) */
  717. mc->mca_sfmode = MCAST_EXCLUDE;
  718. mc->mca_sfcount[MCAST_EXCLUDE] = 1;
  719. if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
  720. IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
  721. mc->mca_flags |= MAF_NOREPORT;
  722. return mc;
  723. }
  724. /*
  725. * device multicast group inc (add if not found)
  726. */
  727. int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
  728. {
  729. struct ifmcaddr6 *mc;
  730. struct inet6_dev *idev;
  731. ASSERT_RTNL();
  732. /* we need to take a reference on idev */
  733. idev = in6_dev_get(dev);
  734. if (!idev)
  735. return -EINVAL;
  736. write_lock_bh(&idev->lock);
  737. if (idev->dead) {
  738. write_unlock_bh(&idev->lock);
  739. in6_dev_put(idev);
  740. return -ENODEV;
  741. }
  742. for (mc = idev->mc_list; mc; mc = mc->next) {
  743. if (ipv6_addr_equal(&mc->mca_addr, addr)) {
  744. mc->mca_users++;
  745. write_unlock_bh(&idev->lock);
  746. ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
  747. NULL, 0);
  748. in6_dev_put(idev);
  749. return 0;
  750. }
  751. }
  752. mc = mca_alloc(idev, addr);
  753. if (!mc) {
  754. write_unlock_bh(&idev->lock);
  755. in6_dev_put(idev);
  756. return -ENOMEM;
  757. }
  758. mc->next = idev->mc_list;
  759. idev->mc_list = mc;
  760. /* Hold this for the code below before we unlock,
  761. * it is already exposed via idev->mc_list.
  762. */
  763. mca_get(mc);
  764. write_unlock_bh(&idev->lock);
  765. mld_del_delrec(idev, &mc->mca_addr);
  766. igmp6_group_added(mc);
  767. ma_put(mc);
  768. return 0;
  769. }
  770. /*
  771. * device multicast group del
  772. */
  773. int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
  774. {
  775. struct ifmcaddr6 *ma, **map;
  776. ASSERT_RTNL();
  777. write_lock_bh(&idev->lock);
  778. for (map = &idev->mc_list; (ma = *map) != NULL; map = &ma->next) {
  779. if (ipv6_addr_equal(&ma->mca_addr, addr)) {
  780. if (--ma->mca_users == 0) {
  781. *map = ma->next;
  782. write_unlock_bh(&idev->lock);
  783. igmp6_group_dropped(ma);
  784. ma_put(ma);
  785. return 0;
  786. }
  787. write_unlock_bh(&idev->lock);
  788. return 0;
  789. }
  790. }
  791. write_unlock_bh(&idev->lock);
  792. return -ENOENT;
  793. }
  794. int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
  795. {
  796. struct inet6_dev *idev;
  797. int err;
  798. ASSERT_RTNL();
  799. idev = __in6_dev_get(dev);
  800. if (!idev)
  801. err = -ENODEV;
  802. else
  803. err = __ipv6_dev_mc_dec(idev, addr);
  804. return err;
  805. }
  806. /*
  807. * check if the interface/address pair is valid
  808. */
  809. bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
  810. const struct in6_addr *src_addr)
  811. {
  812. struct inet6_dev *idev;
  813. struct ifmcaddr6 *mc;
  814. bool rv = false;
  815. rcu_read_lock();
  816. idev = __in6_dev_get(dev);
  817. if (idev) {
  818. read_lock_bh(&idev->lock);
  819. for (mc = idev->mc_list; mc; mc = mc->next) {
  820. if (ipv6_addr_equal(&mc->mca_addr, group))
  821. break;
  822. }
  823. if (mc) {
  824. if (src_addr && !ipv6_addr_any(src_addr)) {
  825. struct ip6_sf_list *psf;
  826. spin_lock_bh(&mc->mca_lock);
  827. for (psf = mc->mca_sources; psf; psf = psf->sf_next) {
  828. if (ipv6_addr_equal(&psf->sf_addr, src_addr))
  829. break;
  830. }
  831. if (psf)
  832. rv = psf->sf_count[MCAST_INCLUDE] ||
  833. psf->sf_count[MCAST_EXCLUDE] !=
  834. mc->mca_sfcount[MCAST_EXCLUDE];
  835. else
  836. rv = mc->mca_sfcount[MCAST_EXCLUDE] != 0;
  837. spin_unlock_bh(&mc->mca_lock);
  838. } else
  839. rv = true; /* don't filter unspecified source */
  840. }
  841. read_unlock_bh(&idev->lock);
  842. }
  843. rcu_read_unlock();
  844. return rv;
  845. }
  846. static void mld_gq_start_timer(struct inet6_dev *idev)
  847. {
  848. unsigned long tv = prandom_u32() % idev->mc_maxdelay;
  849. idev->mc_gq_running = 1;
  850. if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
  851. in6_dev_hold(idev);
  852. }
  853. static void mld_gq_stop_timer(struct inet6_dev *idev)
  854. {
  855. idev->mc_gq_running = 0;
  856. if (del_timer(&idev->mc_gq_timer))
  857. __in6_dev_put(idev);
  858. }
  859. static void mld_ifc_start_timer(struct inet6_dev *idev, unsigned long delay)
  860. {
  861. unsigned long tv = prandom_u32() % delay;
  862. if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
  863. in6_dev_hold(idev);
  864. }
  865. static void mld_ifc_stop_timer(struct inet6_dev *idev)
  866. {
  867. idev->mc_ifc_count = 0;
  868. if (del_timer(&idev->mc_ifc_timer))
  869. __in6_dev_put(idev);
  870. }
  871. static void mld_dad_start_timer(struct inet6_dev *idev, unsigned long delay)
  872. {
  873. unsigned long tv = prandom_u32() % delay;
  874. if (!mod_timer(&idev->mc_dad_timer, jiffies+tv+2))
  875. in6_dev_hold(idev);
  876. }
  877. static void mld_dad_stop_timer(struct inet6_dev *idev)
  878. {
  879. if (del_timer(&idev->mc_dad_timer))
  880. __in6_dev_put(idev);
  881. }
  882. /*
  883. * IGMP handling (alias multicast ICMPv6 messages)
  884. */
  885. static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
  886. {
  887. unsigned long delay = resptime;
  888. /* Do not start timer for these addresses */
  889. if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
  890. IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
  891. return;
  892. if (del_timer(&ma->mca_timer)) {
  893. atomic_dec(&ma->mca_refcnt);
  894. delay = ma->mca_timer.expires - jiffies;
  895. }
  896. if (delay >= resptime)
  897. delay = prandom_u32() % resptime;
  898. ma->mca_timer.expires = jiffies + delay;
  899. if (!mod_timer(&ma->mca_timer, jiffies + delay))
  900. atomic_inc(&ma->mca_refcnt);
  901. ma->mca_flags |= MAF_TIMER_RUNNING;
  902. }
  903. /* mark EXCLUDE-mode sources */
  904. static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
  905. const struct in6_addr *srcs)
  906. {
  907. struct ip6_sf_list *psf;
  908. int i, scount;
  909. scount = 0;
  910. for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
  911. if (scount == nsrcs)
  912. break;
  913. for (i = 0; i < nsrcs; i++) {
  914. /* skip inactive filters */
  915. if (psf->sf_count[MCAST_INCLUDE] ||
  916. pmc->mca_sfcount[MCAST_EXCLUDE] !=
  917. psf->sf_count[MCAST_EXCLUDE])
  918. break;
  919. if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
  920. scount++;
  921. break;
  922. }
  923. }
  924. }
  925. pmc->mca_flags &= ~MAF_GSQUERY;
  926. if (scount == nsrcs) /* all sources excluded */
  927. return false;
  928. return true;
  929. }
  930. static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
  931. const struct in6_addr *srcs)
  932. {
  933. struct ip6_sf_list *psf;
  934. int i, scount;
  935. if (pmc->mca_sfmode == MCAST_EXCLUDE)
  936. return mld_xmarksources(pmc, nsrcs, srcs);
  937. /* mark INCLUDE-mode sources */
  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. if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
  944. psf->sf_gsresp = 1;
  945. scount++;
  946. break;
  947. }
  948. }
  949. }
  950. if (!scount) {
  951. pmc->mca_flags &= ~MAF_GSQUERY;
  952. return false;
  953. }
  954. pmc->mca_flags |= MAF_GSQUERY;
  955. return true;
  956. }
  957. static int mld_force_mld_version(const struct inet6_dev *idev)
  958. {
  959. /* Normally, both are 0 here. If enforcement to a particular is
  960. * being used, individual device enforcement will have a lower
  961. * precedence over 'all' device (.../conf/all/force_mld_version).
  962. */
  963. if (dev_net(idev->dev)->ipv6.devconf_all->force_mld_version != 0)
  964. return dev_net(idev->dev)->ipv6.devconf_all->force_mld_version;
  965. else
  966. return idev->cnf.force_mld_version;
  967. }
  968. static bool mld_in_v2_mode_only(const struct inet6_dev *idev)
  969. {
  970. return mld_force_mld_version(idev) == 2;
  971. }
  972. static bool mld_in_v1_mode_only(const struct inet6_dev *idev)
  973. {
  974. return mld_force_mld_version(idev) == 1;
  975. }
  976. static bool mld_in_v1_mode(const struct inet6_dev *idev)
  977. {
  978. if (mld_in_v2_mode_only(idev))
  979. return false;
  980. if (mld_in_v1_mode_only(idev))
  981. return true;
  982. if (idev->mc_v1_seen && time_before(jiffies, idev->mc_v1_seen))
  983. return true;
  984. return false;
  985. }
  986. static void mld_set_v1_mode(struct inet6_dev *idev)
  987. {
  988. /* RFC3810, relevant sections:
  989. * - 9.1. Robustness Variable
  990. * - 9.2. Query Interval
  991. * - 9.3. Query Response Interval
  992. * - 9.12. Older Version Querier Present Timeout
  993. */
  994. unsigned long switchback;
  995. switchback = (idev->mc_qrv * idev->mc_qi) + idev->mc_qri;
  996. idev->mc_v1_seen = jiffies + switchback;
  997. }
  998. static void mld_update_qrv(struct inet6_dev *idev,
  999. const struct mld2_query *mlh2)
  1000. {
  1001. /* RFC3810, relevant sections:
  1002. * - 5.1.8. QRV (Querier's Robustness Variable)
  1003. * - 9.1. Robustness Variable
  1004. */
  1005. /* The value of the Robustness Variable MUST NOT be zero,
  1006. * and SHOULD NOT be one. Catch this here if we ever run
  1007. * into such a case in future.
  1008. */
  1009. const int min_qrv = min(MLD_QRV_DEFAULT, sysctl_mld_qrv);
  1010. WARN_ON(idev->mc_qrv == 0);
  1011. if (mlh2->mld2q_qrv > 0)
  1012. idev->mc_qrv = mlh2->mld2q_qrv;
  1013. if (unlikely(idev->mc_qrv < min_qrv)) {
  1014. net_warn_ratelimited("IPv6: MLD: clamping QRV from %u to %u!\n",
  1015. idev->mc_qrv, min_qrv);
  1016. idev->mc_qrv = min_qrv;
  1017. }
  1018. }
  1019. static void mld_update_qi(struct inet6_dev *idev,
  1020. const struct mld2_query *mlh2)
  1021. {
  1022. /* RFC3810, relevant sections:
  1023. * - 5.1.9. QQIC (Querier's Query Interval Code)
  1024. * - 9.2. Query Interval
  1025. * - 9.12. Older Version Querier Present Timeout
  1026. * (the [Query Interval] in the last Query received)
  1027. */
  1028. unsigned long mc_qqi;
  1029. if (mlh2->mld2q_qqic < 128) {
  1030. mc_qqi = mlh2->mld2q_qqic;
  1031. } else {
  1032. unsigned long mc_man, mc_exp;
  1033. mc_exp = MLDV2_QQIC_EXP(mlh2->mld2q_qqic);
  1034. mc_man = MLDV2_QQIC_MAN(mlh2->mld2q_qqic);
  1035. mc_qqi = (mc_man | 0x10) << (mc_exp + 3);
  1036. }
  1037. idev->mc_qi = mc_qqi * HZ;
  1038. }
  1039. static void mld_update_qri(struct inet6_dev *idev,
  1040. const struct mld2_query *mlh2)
  1041. {
  1042. /* RFC3810, relevant sections:
  1043. * - 5.1.3. Maximum Response Code
  1044. * - 9.3. Query Response Interval
  1045. */
  1046. idev->mc_qri = msecs_to_jiffies(mldv2_mrc(mlh2));
  1047. }
  1048. static int mld_process_v1(struct inet6_dev *idev, struct mld_msg *mld,
  1049. unsigned long *max_delay, bool v1_query)
  1050. {
  1051. unsigned long mldv1_md;
  1052. /* Ignore v1 queries */
  1053. if (mld_in_v2_mode_only(idev))
  1054. return -EINVAL;
  1055. mldv1_md = ntohs(mld->mld_maxdelay);
  1056. /* When in MLDv1 fallback and a MLDv2 router start-up being
  1057. * unaware of current MLDv1 operation, the MRC == MRD mapping
  1058. * only works when the exponential algorithm is not being
  1059. * used (as MLDv1 is unaware of such things).
  1060. *
  1061. * According to the RFC author, the MLDv2 implementations
  1062. * he's aware of all use a MRC < 32768 on start up queries.
  1063. *
  1064. * Thus, should we *ever* encounter something else larger
  1065. * than that, just assume the maximum possible within our
  1066. * reach.
  1067. */
  1068. if (!v1_query)
  1069. mldv1_md = min(mldv1_md, MLDV1_MRD_MAX_COMPAT);
  1070. *max_delay = max(msecs_to_jiffies(mldv1_md), 1UL);
  1071. /* MLDv1 router present: we need to go into v1 mode *only*
  1072. * when an MLDv1 query is received as per section 9.12. of
  1073. * RFC3810! And we know from RFC2710 section 3.7 that MLDv1
  1074. * queries MUST be of exactly 24 octets.
  1075. */
  1076. if (v1_query)
  1077. mld_set_v1_mode(idev);
  1078. /* cancel MLDv2 report timer */
  1079. mld_gq_stop_timer(idev);
  1080. /* cancel the interface change timer */
  1081. mld_ifc_stop_timer(idev);
  1082. /* clear deleted report items */
  1083. mld_clear_delrec(idev);
  1084. return 0;
  1085. }
  1086. static int mld_process_v2(struct inet6_dev *idev, struct mld2_query *mld,
  1087. unsigned long *max_delay)
  1088. {
  1089. *max_delay = max(msecs_to_jiffies(mldv2_mrc(mld)), 1UL);
  1090. mld_update_qrv(idev, mld);
  1091. mld_update_qi(idev, mld);
  1092. mld_update_qri(idev, mld);
  1093. idev->mc_maxdelay = *max_delay;
  1094. return 0;
  1095. }
  1096. /* called with rcu_read_lock() */
  1097. int igmp6_event_query(struct sk_buff *skb)
  1098. {
  1099. struct mld2_query *mlh2 = NULL;
  1100. struct ifmcaddr6 *ma;
  1101. const struct in6_addr *group;
  1102. unsigned long max_delay;
  1103. struct inet6_dev *idev;
  1104. struct mld_msg *mld;
  1105. int group_type;
  1106. int mark = 0;
  1107. int len, err;
  1108. if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
  1109. return -EINVAL;
  1110. /* compute payload length excluding extension headers */
  1111. len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
  1112. len -= skb_network_header_len(skb);
  1113. /* RFC3810 6.2
  1114. * Upon reception of an MLD message that contains a Query, the node
  1115. * checks if the source address of the message is a valid link-local
  1116. * address, if the Hop Limit is set to 1, and if the Router Alert
  1117. * option is present in the Hop-By-Hop Options header of the IPv6
  1118. * packet. If any of these checks fails, the packet is dropped.
  1119. */
  1120. if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL) ||
  1121. ipv6_hdr(skb)->hop_limit != 1 ||
  1122. !(IP6CB(skb)->flags & IP6SKB_ROUTERALERT) ||
  1123. IP6CB(skb)->ra != htons(IPV6_OPT_ROUTERALERT_MLD))
  1124. return -EINVAL;
  1125. idev = __in6_dev_get(skb->dev);
  1126. if (!idev)
  1127. return 0;
  1128. mld = (struct mld_msg *)icmp6_hdr(skb);
  1129. group = &mld->mld_mca;
  1130. group_type = ipv6_addr_type(group);
  1131. if (group_type != IPV6_ADDR_ANY &&
  1132. !(group_type&IPV6_ADDR_MULTICAST))
  1133. return -EINVAL;
  1134. if (len < MLD_V1_QUERY_LEN) {
  1135. return -EINVAL;
  1136. } else if (len == MLD_V1_QUERY_LEN || mld_in_v1_mode(idev)) {
  1137. err = mld_process_v1(idev, mld, &max_delay,
  1138. len == MLD_V1_QUERY_LEN);
  1139. if (err < 0)
  1140. return err;
  1141. } else if (len >= MLD_V2_QUERY_LEN_MIN) {
  1142. int srcs_offset = sizeof(struct mld2_query) -
  1143. sizeof(struct icmp6hdr);
  1144. if (!pskb_may_pull(skb, srcs_offset))
  1145. return -EINVAL;
  1146. mlh2 = (struct mld2_query *)skb_transport_header(skb);
  1147. err = mld_process_v2(idev, mlh2, &max_delay);
  1148. if (err < 0)
  1149. return err;
  1150. if (group_type == IPV6_ADDR_ANY) { /* general query */
  1151. if (mlh2->mld2q_nsrcs)
  1152. return -EINVAL; /* no sources allowed */
  1153. mld_gq_start_timer(idev);
  1154. return 0;
  1155. }
  1156. /* mark sources to include, if group & source-specific */
  1157. if (mlh2->mld2q_nsrcs != 0) {
  1158. if (!pskb_may_pull(skb, srcs_offset +
  1159. ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
  1160. return -EINVAL;
  1161. mlh2 = (struct mld2_query *)skb_transport_header(skb);
  1162. mark = 1;
  1163. }
  1164. } else {
  1165. return -EINVAL;
  1166. }
  1167. read_lock_bh(&idev->lock);
  1168. if (group_type == IPV6_ADDR_ANY) {
  1169. for (ma = idev->mc_list; ma; ma = ma->next) {
  1170. spin_lock_bh(&ma->mca_lock);
  1171. igmp6_group_queried(ma, max_delay);
  1172. spin_unlock_bh(&ma->mca_lock);
  1173. }
  1174. } else {
  1175. for (ma = idev->mc_list; ma; ma = ma->next) {
  1176. if (!ipv6_addr_equal(group, &ma->mca_addr))
  1177. continue;
  1178. spin_lock_bh(&ma->mca_lock);
  1179. if (ma->mca_flags & MAF_TIMER_RUNNING) {
  1180. /* gsquery <- gsquery && mark */
  1181. if (!mark)
  1182. ma->mca_flags &= ~MAF_GSQUERY;
  1183. } else {
  1184. /* gsquery <- mark */
  1185. if (mark)
  1186. ma->mca_flags |= MAF_GSQUERY;
  1187. else
  1188. ma->mca_flags &= ~MAF_GSQUERY;
  1189. }
  1190. if (!(ma->mca_flags & MAF_GSQUERY) ||
  1191. mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
  1192. igmp6_group_queried(ma, max_delay);
  1193. spin_unlock_bh(&ma->mca_lock);
  1194. break;
  1195. }
  1196. }
  1197. read_unlock_bh(&idev->lock);
  1198. return 0;
  1199. }
  1200. /* called with rcu_read_lock() */
  1201. int igmp6_event_report(struct sk_buff *skb)
  1202. {
  1203. struct ifmcaddr6 *ma;
  1204. struct inet6_dev *idev;
  1205. struct mld_msg *mld;
  1206. int addr_type;
  1207. /* Our own report looped back. Ignore it. */
  1208. if (skb->pkt_type == PACKET_LOOPBACK)
  1209. return 0;
  1210. /* send our report if the MC router may not have heard this report */
  1211. if (skb->pkt_type != PACKET_MULTICAST &&
  1212. skb->pkt_type != PACKET_BROADCAST)
  1213. return 0;
  1214. if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
  1215. return -EINVAL;
  1216. mld = (struct mld_msg *)icmp6_hdr(skb);
  1217. /* Drop reports with not link local source */
  1218. addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
  1219. if (addr_type != IPV6_ADDR_ANY &&
  1220. !(addr_type&IPV6_ADDR_LINKLOCAL))
  1221. return -EINVAL;
  1222. idev = __in6_dev_get(skb->dev);
  1223. if (!idev)
  1224. return -ENODEV;
  1225. /*
  1226. * Cancel the timer for this group
  1227. */
  1228. read_lock_bh(&idev->lock);
  1229. for (ma = idev->mc_list; ma; ma = ma->next) {
  1230. if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
  1231. spin_lock(&ma->mca_lock);
  1232. if (del_timer(&ma->mca_timer))
  1233. atomic_dec(&ma->mca_refcnt);
  1234. ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
  1235. spin_unlock(&ma->mca_lock);
  1236. break;
  1237. }
  1238. }
  1239. read_unlock_bh(&idev->lock);
  1240. return 0;
  1241. }
  1242. static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
  1243. int gdeleted, int sdeleted)
  1244. {
  1245. switch (type) {
  1246. case MLD2_MODE_IS_INCLUDE:
  1247. case MLD2_MODE_IS_EXCLUDE:
  1248. if (gdeleted || sdeleted)
  1249. return false;
  1250. if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
  1251. if (pmc->mca_sfmode == MCAST_INCLUDE)
  1252. return true;
  1253. /* don't include if this source is excluded
  1254. * in all filters
  1255. */
  1256. if (psf->sf_count[MCAST_INCLUDE])
  1257. return type == MLD2_MODE_IS_INCLUDE;
  1258. return pmc->mca_sfcount[MCAST_EXCLUDE] ==
  1259. psf->sf_count[MCAST_EXCLUDE];
  1260. }
  1261. return false;
  1262. case MLD2_CHANGE_TO_INCLUDE:
  1263. if (gdeleted || sdeleted)
  1264. return false;
  1265. return psf->sf_count[MCAST_INCLUDE] != 0;
  1266. case MLD2_CHANGE_TO_EXCLUDE:
  1267. if (gdeleted || sdeleted)
  1268. return false;
  1269. if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
  1270. psf->sf_count[MCAST_INCLUDE])
  1271. return false;
  1272. return pmc->mca_sfcount[MCAST_EXCLUDE] ==
  1273. psf->sf_count[MCAST_EXCLUDE];
  1274. case MLD2_ALLOW_NEW_SOURCES:
  1275. if (gdeleted || !psf->sf_crcount)
  1276. return false;
  1277. return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
  1278. case MLD2_BLOCK_OLD_SOURCES:
  1279. if (pmc->mca_sfmode == MCAST_INCLUDE)
  1280. return gdeleted || (psf->sf_crcount && sdeleted);
  1281. return psf->sf_crcount && !gdeleted && !sdeleted;
  1282. }
  1283. return false;
  1284. }
  1285. static int
  1286. mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
  1287. {
  1288. struct ip6_sf_list *psf;
  1289. int scount = 0;
  1290. for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
  1291. if (!is_in(pmc, psf, type, gdeleted, sdeleted))
  1292. continue;
  1293. scount++;
  1294. }
  1295. return scount;
  1296. }
  1297. static void ip6_mc_hdr(struct sock *sk, struct sk_buff *skb,
  1298. struct net_device *dev,
  1299. const struct in6_addr *saddr,
  1300. const struct in6_addr *daddr,
  1301. int proto, int len)
  1302. {
  1303. struct ipv6hdr *hdr;
  1304. skb->protocol = htons(ETH_P_IPV6);
  1305. skb->dev = dev;
  1306. skb_reset_network_header(skb);
  1307. skb_put(skb, sizeof(struct ipv6hdr));
  1308. hdr = ipv6_hdr(skb);
  1309. ip6_flow_hdr(hdr, 0, 0);
  1310. hdr->payload_len = htons(len);
  1311. hdr->nexthdr = proto;
  1312. hdr->hop_limit = inet6_sk(sk)->hop_limit;
  1313. hdr->saddr = *saddr;
  1314. hdr->daddr = *daddr;
  1315. }
  1316. static struct sk_buff *mld_newpack(struct inet6_dev *idev, unsigned int mtu)
  1317. {
  1318. struct net_device *dev = idev->dev;
  1319. struct net *net = dev_net(dev);
  1320. struct sock *sk = net->ipv6.igmp_sk;
  1321. struct sk_buff *skb;
  1322. struct mld2_report *pmr;
  1323. struct in6_addr addr_buf;
  1324. const struct in6_addr *saddr;
  1325. int hlen = LL_RESERVED_SPACE(dev);
  1326. int tlen = dev->needed_tailroom;
  1327. unsigned int size = mtu + hlen + tlen;
  1328. int err;
  1329. u8 ra[8] = { IPPROTO_ICMPV6, 0,
  1330. IPV6_TLV_ROUTERALERT, 2, 0, 0,
  1331. IPV6_TLV_PADN, 0 };
  1332. /* we assume size > sizeof(ra) here */
  1333. /* limit our allocations to order-0 page */
  1334. size = min_t(int, size, SKB_MAX_ORDER(0, 0));
  1335. skb = sock_alloc_send_skb(sk, size, 1, &err);
  1336. if (!skb)
  1337. return NULL;
  1338. skb->priority = TC_PRIO_CONTROL;
  1339. skb->reserved_tailroom = skb_end_offset(skb) -
  1340. min(mtu, skb_end_offset(skb));
  1341. skb_reserve(skb, hlen);
  1342. if (__ipv6_get_lladdr(idev, &addr_buf, IFA_F_TENTATIVE)) {
  1343. /* <draft-ietf-magma-mld-source-05.txt>:
  1344. * use unspecified address as the source address
  1345. * when a valid link-local address is not available.
  1346. */
  1347. saddr = &in6addr_any;
  1348. } else
  1349. saddr = &addr_buf;
  1350. ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
  1351. memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
  1352. skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
  1353. skb_put(skb, sizeof(*pmr));
  1354. pmr = (struct mld2_report *)skb_transport_header(skb);
  1355. pmr->mld2r_type = ICMPV6_MLD2_REPORT;
  1356. pmr->mld2r_resv1 = 0;
  1357. pmr->mld2r_cksum = 0;
  1358. pmr->mld2r_resv2 = 0;
  1359. pmr->mld2r_ngrec = 0;
  1360. return skb;
  1361. }
  1362. static void mld_sendpack(struct sk_buff *skb)
  1363. {
  1364. struct ipv6hdr *pip6 = ipv6_hdr(skb);
  1365. struct mld2_report *pmr =
  1366. (struct mld2_report *)skb_transport_header(skb);
  1367. int payload_len, mldlen;
  1368. struct inet6_dev *idev;
  1369. struct net *net = dev_net(skb->dev);
  1370. int err;
  1371. struct flowi6 fl6;
  1372. struct dst_entry *dst;
  1373. rcu_read_lock();
  1374. idev = __in6_dev_get(skb->dev);
  1375. IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
  1376. payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) -
  1377. sizeof(*pip6);
  1378. mldlen = skb_tail_pointer(skb) - skb_transport_header(skb);
  1379. pip6->payload_len = htons(payload_len);
  1380. pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
  1381. IPPROTO_ICMPV6,
  1382. csum_partial(skb_transport_header(skb),
  1383. mldlen, 0));
  1384. icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
  1385. &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
  1386. skb->dev->ifindex);
  1387. dst = icmp6_dst_alloc(skb->dev, &fl6);
  1388. err = 0;
  1389. if (IS_ERR(dst)) {
  1390. err = PTR_ERR(dst);
  1391. dst = NULL;
  1392. }
  1393. skb_dst_set(skb, dst);
  1394. if (err)
  1395. goto err_out;
  1396. payload_len = skb->len;
  1397. err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
  1398. net->ipv6.igmp_sk, skb, NULL, skb->dev,
  1399. dst_output_sk);
  1400. out:
  1401. if (!err) {
  1402. ICMP6MSGOUT_INC_STATS(net, idev, ICMPV6_MLD2_REPORT);
  1403. ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
  1404. IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, payload_len);
  1405. } else {
  1406. IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
  1407. }
  1408. rcu_read_unlock();
  1409. return;
  1410. err_out:
  1411. kfree_skb(skb);
  1412. goto out;
  1413. }
  1414. static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
  1415. {
  1416. return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
  1417. }
  1418. static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
  1419. int type, struct mld2_grec **ppgr)
  1420. {
  1421. struct net_device *dev = pmc->idev->dev;
  1422. struct mld2_report *pmr;
  1423. struct mld2_grec *pgr;
  1424. if (!skb)
  1425. skb = mld_newpack(pmc->idev, dev->mtu);
  1426. if (!skb)
  1427. return NULL;
  1428. pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
  1429. pgr->grec_type = type;
  1430. pgr->grec_auxwords = 0;
  1431. pgr->grec_nsrcs = 0;
  1432. pgr->grec_mca = pmc->mca_addr; /* structure copy */
  1433. pmr = (struct mld2_report *)skb_transport_header(skb);
  1434. pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
  1435. *ppgr = pgr;
  1436. return skb;
  1437. }
  1438. #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
  1439. static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
  1440. int type, int gdeleted, int sdeleted, int crsend)
  1441. {
  1442. struct inet6_dev *idev = pmc->idev;
  1443. struct net_device *dev = idev->dev;
  1444. struct mld2_report *pmr;
  1445. struct mld2_grec *pgr = NULL;
  1446. struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
  1447. int scount, stotal, first, isquery, truncate;
  1448. if (pmc->mca_flags & MAF_NOREPORT)
  1449. return skb;
  1450. isquery = type == MLD2_MODE_IS_INCLUDE ||
  1451. type == MLD2_MODE_IS_EXCLUDE;
  1452. truncate = type == MLD2_MODE_IS_EXCLUDE ||
  1453. type == MLD2_CHANGE_TO_EXCLUDE;
  1454. stotal = scount = 0;
  1455. psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
  1456. if (!*psf_list)
  1457. goto empty_source;
  1458. pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
  1459. /* EX and TO_EX get a fresh packet, if needed */
  1460. if (truncate) {
  1461. if (pmr && pmr->mld2r_ngrec &&
  1462. AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
  1463. if (skb)
  1464. mld_sendpack(skb);
  1465. skb = mld_newpack(idev, dev->mtu);
  1466. }
  1467. }
  1468. first = 1;
  1469. psf_prev = NULL;
  1470. for (psf = *psf_list; psf; psf = psf_next) {
  1471. struct in6_addr *psrc;
  1472. psf_next = psf->sf_next;
  1473. if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
  1474. psf_prev = psf;
  1475. continue;
  1476. }
  1477. /* clear marks on query responses */
  1478. if (isquery)
  1479. psf->sf_gsresp = 0;
  1480. if (AVAILABLE(skb) < sizeof(*psrc) +
  1481. first*sizeof(struct mld2_grec)) {
  1482. if (truncate && !first)
  1483. break; /* truncate these */
  1484. if (pgr)
  1485. pgr->grec_nsrcs = htons(scount);
  1486. if (skb)
  1487. mld_sendpack(skb);
  1488. skb = mld_newpack(idev, dev->mtu);
  1489. first = 1;
  1490. scount = 0;
  1491. }
  1492. if (first) {
  1493. skb = add_grhead(skb, pmc, type, &pgr);
  1494. first = 0;
  1495. }
  1496. if (!skb)
  1497. return NULL;
  1498. psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
  1499. *psrc = psf->sf_addr;
  1500. scount++; stotal++;
  1501. if ((type == MLD2_ALLOW_NEW_SOURCES ||
  1502. type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
  1503. psf->sf_crcount--;
  1504. if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
  1505. if (psf_prev)
  1506. psf_prev->sf_next = psf->sf_next;
  1507. else
  1508. *psf_list = psf->sf_next;
  1509. kfree(psf);
  1510. continue;
  1511. }
  1512. }
  1513. psf_prev = psf;
  1514. }
  1515. empty_source:
  1516. if (!stotal) {
  1517. if (type == MLD2_ALLOW_NEW_SOURCES ||
  1518. type == MLD2_BLOCK_OLD_SOURCES)
  1519. return skb;
  1520. if (pmc->mca_crcount || isquery || crsend) {
  1521. /* make sure we have room for group header */
  1522. if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
  1523. mld_sendpack(skb);
  1524. skb = NULL; /* add_grhead will get a new one */
  1525. }
  1526. skb = add_grhead(skb, pmc, type, &pgr);
  1527. }
  1528. }
  1529. if (pgr)
  1530. pgr->grec_nsrcs = htons(scount);
  1531. if (isquery)
  1532. pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
  1533. return skb;
  1534. }
  1535. static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
  1536. {
  1537. struct sk_buff *skb = NULL;
  1538. int type;
  1539. read_lock_bh(&idev->lock);
  1540. if (!pmc) {
  1541. for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
  1542. if (pmc->mca_flags & MAF_NOREPORT)
  1543. continue;
  1544. spin_lock_bh(&pmc->mca_lock);
  1545. if (pmc->mca_sfcount[MCAST_EXCLUDE])
  1546. type = MLD2_MODE_IS_EXCLUDE;
  1547. else
  1548. type = MLD2_MODE_IS_INCLUDE;
  1549. skb = add_grec(skb, pmc, type, 0, 0, 0);
  1550. spin_unlock_bh(&pmc->mca_lock);
  1551. }
  1552. } else {
  1553. spin_lock_bh(&pmc->mca_lock);
  1554. if (pmc->mca_sfcount[MCAST_EXCLUDE])
  1555. type = MLD2_MODE_IS_EXCLUDE;
  1556. else
  1557. type = MLD2_MODE_IS_INCLUDE;
  1558. skb = add_grec(skb, pmc, type, 0, 0, 0);
  1559. spin_unlock_bh(&pmc->mca_lock);
  1560. }
  1561. read_unlock_bh(&idev->lock);
  1562. if (skb)
  1563. mld_sendpack(skb);
  1564. }
  1565. /*
  1566. * remove zero-count source records from a source filter list
  1567. */
  1568. static void mld_clear_zeros(struct ip6_sf_list **ppsf)
  1569. {
  1570. struct ip6_sf_list *psf_prev, *psf_next, *psf;
  1571. psf_prev = NULL;
  1572. for (psf = *ppsf; psf; psf = psf_next) {
  1573. psf_next = psf->sf_next;
  1574. if (psf->sf_crcount == 0) {
  1575. if (psf_prev)
  1576. psf_prev->sf_next = psf->sf_next;
  1577. else
  1578. *ppsf = psf->sf_next;
  1579. kfree(psf);
  1580. } else
  1581. psf_prev = psf;
  1582. }
  1583. }
  1584. static void mld_send_cr(struct inet6_dev *idev)
  1585. {
  1586. struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
  1587. struct sk_buff *skb = NULL;
  1588. int type, dtype;
  1589. read_lock_bh(&idev->lock);
  1590. spin_lock(&idev->mc_lock);
  1591. /* deleted MCA's */
  1592. pmc_prev = NULL;
  1593. for (pmc = idev->mc_tomb; pmc; pmc = pmc_next) {
  1594. pmc_next = pmc->next;
  1595. if (pmc->mca_sfmode == MCAST_INCLUDE) {
  1596. type = MLD2_BLOCK_OLD_SOURCES;
  1597. dtype = MLD2_BLOCK_OLD_SOURCES;
  1598. skb = add_grec(skb, pmc, type, 1, 0, 0);
  1599. skb = add_grec(skb, pmc, dtype, 1, 1, 0);
  1600. }
  1601. if (pmc->mca_crcount) {
  1602. if (pmc->mca_sfmode == MCAST_EXCLUDE) {
  1603. type = MLD2_CHANGE_TO_INCLUDE;
  1604. skb = add_grec(skb, pmc, type, 1, 0, 0);
  1605. }
  1606. pmc->mca_crcount--;
  1607. if (pmc->mca_crcount == 0) {
  1608. mld_clear_zeros(&pmc->mca_tomb);
  1609. mld_clear_zeros(&pmc->mca_sources);
  1610. }
  1611. }
  1612. if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
  1613. !pmc->mca_sources) {
  1614. if (pmc_prev)
  1615. pmc_prev->next = pmc_next;
  1616. else
  1617. idev->mc_tomb = pmc_next;
  1618. in6_dev_put(pmc->idev);
  1619. kfree(pmc);
  1620. } else
  1621. pmc_prev = pmc;
  1622. }
  1623. spin_unlock(&idev->mc_lock);
  1624. /* change recs */
  1625. for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
  1626. spin_lock_bh(&pmc->mca_lock);
  1627. if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
  1628. type = MLD2_BLOCK_OLD_SOURCES;
  1629. dtype = MLD2_ALLOW_NEW_SOURCES;
  1630. } else {
  1631. type = MLD2_ALLOW_NEW_SOURCES;
  1632. dtype = MLD2_BLOCK_OLD_SOURCES;
  1633. }
  1634. skb = add_grec(skb, pmc, type, 0, 0, 0);
  1635. skb = add_grec(skb, pmc, dtype, 0, 1, 0); /* deleted sources */
  1636. /* filter mode changes */
  1637. if (pmc->mca_crcount) {
  1638. if (pmc->mca_sfmode == MCAST_EXCLUDE)
  1639. type = MLD2_CHANGE_TO_EXCLUDE;
  1640. else
  1641. type = MLD2_CHANGE_TO_INCLUDE;
  1642. skb = add_grec(skb, pmc, type, 0, 0, 0);
  1643. pmc->mca_crcount--;
  1644. }
  1645. spin_unlock_bh(&pmc->mca_lock);
  1646. }
  1647. read_unlock_bh(&idev->lock);
  1648. if (!skb)
  1649. return;
  1650. (void) mld_sendpack(skb);
  1651. }
  1652. static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
  1653. {
  1654. struct net *net = dev_net(dev);
  1655. struct sock *sk = net->ipv6.igmp_sk;
  1656. struct inet6_dev *idev;
  1657. struct sk_buff *skb;
  1658. struct mld_msg *hdr;
  1659. const struct in6_addr *snd_addr, *saddr;
  1660. struct in6_addr addr_buf;
  1661. int hlen = LL_RESERVED_SPACE(dev);
  1662. int tlen = dev->needed_tailroom;
  1663. int err, len, payload_len, full_len;
  1664. u8 ra[8] = { IPPROTO_ICMPV6, 0,
  1665. IPV6_TLV_ROUTERALERT, 2, 0, 0,
  1666. IPV6_TLV_PADN, 0 };
  1667. struct flowi6 fl6;
  1668. struct dst_entry *dst;
  1669. if (type == ICMPV6_MGM_REDUCTION)
  1670. snd_addr = &in6addr_linklocal_allrouters;
  1671. else
  1672. snd_addr = addr;
  1673. len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
  1674. payload_len = len + sizeof(ra);
  1675. full_len = sizeof(struct ipv6hdr) + payload_len;
  1676. rcu_read_lock();
  1677. IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
  1678. IPSTATS_MIB_OUT, full_len);
  1679. rcu_read_unlock();
  1680. skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
  1681. if (!skb) {
  1682. rcu_read_lock();
  1683. IP6_INC_STATS(net, __in6_dev_get(dev),
  1684. IPSTATS_MIB_OUTDISCARDS);
  1685. rcu_read_unlock();
  1686. return;
  1687. }
  1688. skb->priority = TC_PRIO_CONTROL;
  1689. skb_reserve(skb, hlen);
  1690. if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
  1691. /* <draft-ietf-magma-mld-source-05.txt>:
  1692. * use unspecified address as the source address
  1693. * when a valid link-local address is not available.
  1694. */
  1695. saddr = &in6addr_any;
  1696. } else
  1697. saddr = &addr_buf;
  1698. ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
  1699. memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
  1700. hdr = (struct mld_msg *) skb_put(skb, sizeof(struct mld_msg));
  1701. memset(hdr, 0, sizeof(struct mld_msg));
  1702. hdr->mld_type = type;
  1703. hdr->mld_mca = *addr;
  1704. hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
  1705. IPPROTO_ICMPV6,
  1706. csum_partial(hdr, len, 0));
  1707. rcu_read_lock();
  1708. idev = __in6_dev_get(skb->dev);
  1709. icmpv6_flow_init(sk, &fl6, type,
  1710. &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
  1711. skb->dev->ifindex);
  1712. dst = icmp6_dst_alloc(skb->dev, &fl6);
  1713. if (IS_ERR(dst)) {
  1714. err = PTR_ERR(dst);
  1715. goto err_out;
  1716. }
  1717. skb_dst_set(skb, dst);
  1718. err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, sk, skb,
  1719. NULL, skb->dev, dst_output_sk);
  1720. out:
  1721. if (!err) {
  1722. ICMP6MSGOUT_INC_STATS(net, idev, type);
  1723. ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
  1724. IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, full_len);
  1725. } else
  1726. IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
  1727. rcu_read_unlock();
  1728. return;
  1729. err_out:
  1730. kfree_skb(skb);
  1731. goto out;
  1732. }
  1733. static void mld_send_initial_cr(struct inet6_dev *idev)
  1734. {
  1735. struct sk_buff *skb;
  1736. struct ifmcaddr6 *pmc;
  1737. int type;
  1738. if (mld_in_v1_mode(idev))
  1739. return;
  1740. skb = NULL;
  1741. read_lock_bh(&idev->lock);
  1742. for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
  1743. spin_lock_bh(&pmc->mca_lock);
  1744. if (pmc->mca_sfcount[MCAST_EXCLUDE])
  1745. type = MLD2_CHANGE_TO_EXCLUDE;
  1746. else
  1747. type = MLD2_CHANGE_TO_INCLUDE;
  1748. skb = add_grec(skb, pmc, type, 0, 0, 1);
  1749. spin_unlock_bh(&pmc->mca_lock);
  1750. }
  1751. read_unlock_bh(&idev->lock);
  1752. if (skb)
  1753. mld_sendpack(skb);
  1754. }
  1755. void ipv6_mc_dad_complete(struct inet6_dev *idev)
  1756. {
  1757. idev->mc_dad_count = idev->mc_qrv;
  1758. if (idev->mc_dad_count) {
  1759. mld_send_initial_cr(idev);
  1760. idev->mc_dad_count--;
  1761. if (idev->mc_dad_count)
  1762. mld_dad_start_timer(idev, idev->mc_maxdelay);
  1763. }
  1764. }
  1765. static void mld_dad_timer_expire(unsigned long data)
  1766. {
  1767. struct inet6_dev *idev = (struct inet6_dev *)data;
  1768. mld_send_initial_cr(idev);
  1769. if (idev->mc_dad_count) {
  1770. idev->mc_dad_count--;
  1771. if (idev->mc_dad_count)
  1772. mld_dad_start_timer(idev, idev->mc_maxdelay);
  1773. }
  1774. in6_dev_put(idev);
  1775. }
  1776. static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
  1777. const struct in6_addr *psfsrc)
  1778. {
  1779. struct ip6_sf_list *psf, *psf_prev;
  1780. int rv = 0;
  1781. psf_prev = NULL;
  1782. for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
  1783. if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
  1784. break;
  1785. psf_prev = psf;
  1786. }
  1787. if (!psf || psf->sf_count[sfmode] == 0) {
  1788. /* source filter not found, or count wrong => bug */
  1789. return -ESRCH;
  1790. }
  1791. psf->sf_count[sfmode]--;
  1792. if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
  1793. struct inet6_dev *idev = pmc->idev;
  1794. /* no more filters for this source */
  1795. if (psf_prev)
  1796. psf_prev->sf_next = psf->sf_next;
  1797. else
  1798. pmc->mca_sources = psf->sf_next;
  1799. if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
  1800. !mld_in_v1_mode(idev)) {
  1801. psf->sf_crcount = idev->mc_qrv;
  1802. psf->sf_next = pmc->mca_tomb;
  1803. pmc->mca_tomb = psf;
  1804. rv = 1;
  1805. } else
  1806. kfree(psf);
  1807. }
  1808. return rv;
  1809. }
  1810. static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
  1811. int sfmode, int sfcount, const struct in6_addr *psfsrc,
  1812. int delta)
  1813. {
  1814. struct ifmcaddr6 *pmc;
  1815. int changerec = 0;
  1816. int i, err;
  1817. if (!idev)
  1818. return -ENODEV;
  1819. read_lock_bh(&idev->lock);
  1820. for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
  1821. if (ipv6_addr_equal(pmca, &pmc->mca_addr))
  1822. break;
  1823. }
  1824. if (!pmc) {
  1825. /* MCA not found?? bug */
  1826. read_unlock_bh(&idev->lock);
  1827. return -ESRCH;
  1828. }
  1829. spin_lock_bh(&pmc->mca_lock);
  1830. sf_markstate(pmc);
  1831. if (!delta) {
  1832. if (!pmc->mca_sfcount[sfmode]) {
  1833. spin_unlock_bh(&pmc->mca_lock);
  1834. read_unlock_bh(&idev->lock);
  1835. return -EINVAL;
  1836. }
  1837. pmc->mca_sfcount[sfmode]--;
  1838. }
  1839. err = 0;
  1840. for (i = 0; i < sfcount; i++) {
  1841. int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1842. changerec |= rv > 0;
  1843. if (!err && rv < 0)
  1844. err = rv;
  1845. }
  1846. if (pmc->mca_sfmode == MCAST_EXCLUDE &&
  1847. pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
  1848. pmc->mca_sfcount[MCAST_INCLUDE]) {
  1849. struct ip6_sf_list *psf;
  1850. /* filter mode change */
  1851. pmc->mca_sfmode = MCAST_INCLUDE;
  1852. pmc->mca_crcount = idev->mc_qrv;
  1853. idev->mc_ifc_count = pmc->mca_crcount;
  1854. for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
  1855. psf->sf_crcount = 0;
  1856. mld_ifc_event(pmc->idev);
  1857. } else if (sf_setstate(pmc) || changerec)
  1858. mld_ifc_event(pmc->idev);
  1859. spin_unlock_bh(&pmc->mca_lock);
  1860. read_unlock_bh(&idev->lock);
  1861. return err;
  1862. }
  1863. /*
  1864. * Add multicast single-source filter to the interface list
  1865. */
  1866. static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
  1867. const struct in6_addr *psfsrc)
  1868. {
  1869. struct ip6_sf_list *psf, *psf_prev;
  1870. psf_prev = NULL;
  1871. for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
  1872. if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
  1873. break;
  1874. psf_prev = psf;
  1875. }
  1876. if (!psf) {
  1877. psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
  1878. if (!psf)
  1879. return -ENOBUFS;
  1880. psf->sf_addr = *psfsrc;
  1881. if (psf_prev) {
  1882. psf_prev->sf_next = psf;
  1883. } else
  1884. pmc->mca_sources = psf;
  1885. }
  1886. psf->sf_count[sfmode]++;
  1887. return 0;
  1888. }
  1889. static void sf_markstate(struct ifmcaddr6 *pmc)
  1890. {
  1891. struct ip6_sf_list *psf;
  1892. int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
  1893. for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
  1894. if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
  1895. psf->sf_oldin = mca_xcount ==
  1896. psf->sf_count[MCAST_EXCLUDE] &&
  1897. !psf->sf_count[MCAST_INCLUDE];
  1898. } else
  1899. psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
  1900. }
  1901. static int sf_setstate(struct ifmcaddr6 *pmc)
  1902. {
  1903. struct ip6_sf_list *psf, *dpsf;
  1904. int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
  1905. int qrv = pmc->idev->mc_qrv;
  1906. int new_in, rv;
  1907. rv = 0;
  1908. for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
  1909. if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
  1910. new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
  1911. !psf->sf_count[MCAST_INCLUDE];
  1912. } else
  1913. new_in = psf->sf_count[MCAST_INCLUDE] != 0;
  1914. if (new_in) {
  1915. if (!psf->sf_oldin) {
  1916. struct ip6_sf_list *prev = NULL;
  1917. for (dpsf = pmc->mca_tomb; dpsf;
  1918. dpsf = dpsf->sf_next) {
  1919. if (ipv6_addr_equal(&dpsf->sf_addr,
  1920. &psf->sf_addr))
  1921. break;
  1922. prev = dpsf;
  1923. }
  1924. if (dpsf) {
  1925. if (prev)
  1926. prev->sf_next = dpsf->sf_next;
  1927. else
  1928. pmc->mca_tomb = dpsf->sf_next;
  1929. kfree(dpsf);
  1930. }
  1931. psf->sf_crcount = qrv;
  1932. rv++;
  1933. }
  1934. } else if (psf->sf_oldin) {
  1935. psf->sf_crcount = 0;
  1936. /*
  1937. * add or update "delete" records if an active filter
  1938. * is now inactive
  1939. */
  1940. for (dpsf = pmc->mca_tomb; dpsf; dpsf = dpsf->sf_next)
  1941. if (ipv6_addr_equal(&dpsf->sf_addr,
  1942. &psf->sf_addr))
  1943. break;
  1944. if (!dpsf) {
  1945. dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
  1946. if (!dpsf)
  1947. continue;
  1948. *dpsf = *psf;
  1949. /* pmc->mca_lock held by callers */
  1950. dpsf->sf_next = pmc->mca_tomb;
  1951. pmc->mca_tomb = dpsf;
  1952. }
  1953. dpsf->sf_crcount = qrv;
  1954. rv++;
  1955. }
  1956. }
  1957. return rv;
  1958. }
  1959. /*
  1960. * Add multicast source filter list to the interface list
  1961. */
  1962. static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
  1963. int sfmode, int sfcount, const struct in6_addr *psfsrc,
  1964. int delta)
  1965. {
  1966. struct ifmcaddr6 *pmc;
  1967. int isexclude;
  1968. int i, err;
  1969. if (!idev)
  1970. return -ENODEV;
  1971. read_lock_bh(&idev->lock);
  1972. for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
  1973. if (ipv6_addr_equal(pmca, &pmc->mca_addr))
  1974. break;
  1975. }
  1976. if (!pmc) {
  1977. /* MCA not found?? bug */
  1978. read_unlock_bh(&idev->lock);
  1979. return -ESRCH;
  1980. }
  1981. spin_lock_bh(&pmc->mca_lock);
  1982. sf_markstate(pmc);
  1983. isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
  1984. if (!delta)
  1985. pmc->mca_sfcount[sfmode]++;
  1986. err = 0;
  1987. for (i = 0; i < sfcount; i++) {
  1988. err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
  1989. if (err)
  1990. break;
  1991. }
  1992. if (err) {
  1993. int j;
  1994. if (!delta)
  1995. pmc->mca_sfcount[sfmode]--;
  1996. for (j = 0; j < i; j++)
  1997. ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
  1998. } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
  1999. struct ip6_sf_list *psf;
  2000. /* filter mode change */
  2001. if (pmc->mca_sfcount[MCAST_EXCLUDE])
  2002. pmc->mca_sfmode = MCAST_EXCLUDE;
  2003. else if (pmc->mca_sfcount[MCAST_INCLUDE])
  2004. pmc->mca_sfmode = MCAST_INCLUDE;
  2005. /* else no filters; keep old mode for reports */
  2006. pmc->mca_crcount = idev->mc_qrv;
  2007. idev->mc_ifc_count = pmc->mca_crcount;
  2008. for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
  2009. psf->sf_crcount = 0;
  2010. mld_ifc_event(idev);
  2011. } else if (sf_setstate(pmc))
  2012. mld_ifc_event(idev);
  2013. spin_unlock_bh(&pmc->mca_lock);
  2014. read_unlock_bh(&idev->lock);
  2015. return err;
  2016. }
  2017. static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
  2018. {
  2019. struct ip6_sf_list *psf, *nextpsf;
  2020. for (psf = pmc->mca_tomb; psf; psf = nextpsf) {
  2021. nextpsf = psf->sf_next;
  2022. kfree(psf);
  2023. }
  2024. pmc->mca_tomb = NULL;
  2025. for (psf = pmc->mca_sources; psf; psf = nextpsf) {
  2026. nextpsf = psf->sf_next;
  2027. kfree(psf);
  2028. }
  2029. pmc->mca_sources = NULL;
  2030. pmc->mca_sfmode = MCAST_EXCLUDE;
  2031. pmc->mca_sfcount[MCAST_INCLUDE] = 0;
  2032. pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
  2033. }
  2034. static void igmp6_join_group(struct ifmcaddr6 *ma)
  2035. {
  2036. unsigned long delay;
  2037. if (ma->mca_flags & MAF_NOREPORT)
  2038. return;
  2039. igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
  2040. delay = prandom_u32() % unsolicited_report_interval(ma->idev);
  2041. spin_lock_bh(&ma->mca_lock);
  2042. if (del_timer(&ma->mca_timer)) {
  2043. atomic_dec(&ma->mca_refcnt);
  2044. delay = ma->mca_timer.expires - jiffies;
  2045. }
  2046. if (!mod_timer(&ma->mca_timer, jiffies + delay))
  2047. atomic_inc(&ma->mca_refcnt);
  2048. ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
  2049. spin_unlock_bh(&ma->mca_lock);
  2050. }
  2051. static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
  2052. struct inet6_dev *idev)
  2053. {
  2054. int err;
  2055. /* callers have the socket lock and rtnl lock
  2056. * so no other readers or writers of iml or its sflist
  2057. */
  2058. if (!iml->sflist) {
  2059. /* any-source empty exclude case */
  2060. return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
  2061. }
  2062. err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
  2063. iml->sflist->sl_count, iml->sflist->sl_addr, 0);
  2064. sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
  2065. iml->sflist = NULL;
  2066. return err;
  2067. }
  2068. static void igmp6_leave_group(struct ifmcaddr6 *ma)
  2069. {
  2070. if (mld_in_v1_mode(ma->idev)) {
  2071. if (ma->mca_flags & MAF_LAST_REPORTER)
  2072. igmp6_send(&ma->mca_addr, ma->idev->dev,
  2073. ICMPV6_MGM_REDUCTION);
  2074. } else {
  2075. mld_add_delrec(ma->idev, ma);
  2076. mld_ifc_event(ma->idev);
  2077. }
  2078. }
  2079. static void mld_gq_timer_expire(unsigned long data)
  2080. {
  2081. struct inet6_dev *idev = (struct inet6_dev *)data;
  2082. idev->mc_gq_running = 0;
  2083. mld_send_report(idev, NULL);
  2084. in6_dev_put(idev);
  2085. }
  2086. static void mld_ifc_timer_expire(unsigned long data)
  2087. {
  2088. struct inet6_dev *idev = (struct inet6_dev *)data;
  2089. mld_send_cr(idev);
  2090. if (idev->mc_ifc_count) {
  2091. idev->mc_ifc_count--;
  2092. if (idev->mc_ifc_count)
  2093. mld_ifc_start_timer(idev, idev->mc_maxdelay);
  2094. }
  2095. in6_dev_put(idev);
  2096. }
  2097. static void mld_ifc_event(struct inet6_dev *idev)
  2098. {
  2099. if (mld_in_v1_mode(idev))
  2100. return;
  2101. idev->mc_ifc_count = idev->mc_qrv;
  2102. mld_ifc_start_timer(idev, 1);
  2103. }
  2104. static void igmp6_timer_handler(unsigned long data)
  2105. {
  2106. struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
  2107. if (mld_in_v1_mode(ma->idev))
  2108. igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
  2109. else
  2110. mld_send_report(ma->idev, ma);
  2111. spin_lock(&ma->mca_lock);
  2112. ma->mca_flags |= MAF_LAST_REPORTER;
  2113. ma->mca_flags &= ~MAF_TIMER_RUNNING;
  2114. spin_unlock(&ma->mca_lock);
  2115. ma_put(ma);
  2116. }
  2117. /* Device changing type */
  2118. void ipv6_mc_unmap(struct inet6_dev *idev)
  2119. {
  2120. struct ifmcaddr6 *i;
  2121. /* Install multicast list, except for all-nodes (already installed) */
  2122. read_lock_bh(&idev->lock);
  2123. for (i = idev->mc_list; i; i = i->next)
  2124. igmp6_group_dropped(i);
  2125. read_unlock_bh(&idev->lock);
  2126. }
  2127. void ipv6_mc_remap(struct inet6_dev *idev)
  2128. {
  2129. ipv6_mc_up(idev);
  2130. }
  2131. /* Device going down */
  2132. void ipv6_mc_down(struct inet6_dev *idev)
  2133. {
  2134. struct ifmcaddr6 *i;
  2135. /* Withdraw multicast list */
  2136. read_lock_bh(&idev->lock);
  2137. mld_ifc_stop_timer(idev);
  2138. mld_gq_stop_timer(idev);
  2139. mld_dad_stop_timer(idev);
  2140. for (i = idev->mc_list; i; i = i->next)
  2141. igmp6_group_dropped(i);
  2142. read_unlock_bh(&idev->lock);
  2143. mld_clear_delrec(idev);
  2144. }
  2145. static void ipv6_mc_reset(struct inet6_dev *idev)
  2146. {
  2147. idev->mc_qrv = sysctl_mld_qrv;
  2148. idev->mc_qi = MLD_QI_DEFAULT;
  2149. idev->mc_qri = MLD_QRI_DEFAULT;
  2150. idev->mc_v1_seen = 0;
  2151. idev->mc_maxdelay = unsolicited_report_interval(idev);
  2152. }
  2153. /* Device going up */
  2154. void ipv6_mc_up(struct inet6_dev *idev)
  2155. {
  2156. struct ifmcaddr6 *i;
  2157. /* Install multicast list, except for all-nodes (already installed) */
  2158. read_lock_bh(&idev->lock);
  2159. ipv6_mc_reset(idev);
  2160. for (i = idev->mc_list; i; i = i->next)
  2161. igmp6_group_added(i);
  2162. read_unlock_bh(&idev->lock);
  2163. }
  2164. /* IPv6 device initialization. */
  2165. void ipv6_mc_init_dev(struct inet6_dev *idev)
  2166. {
  2167. write_lock_bh(&idev->lock);
  2168. spin_lock_init(&idev->mc_lock);
  2169. idev->mc_gq_running = 0;
  2170. setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
  2171. (unsigned long)idev);
  2172. idev->mc_tomb = NULL;
  2173. idev->mc_ifc_count = 0;
  2174. setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
  2175. (unsigned long)idev);
  2176. setup_timer(&idev->mc_dad_timer, mld_dad_timer_expire,
  2177. (unsigned long)idev);
  2178. ipv6_mc_reset(idev);
  2179. write_unlock_bh(&idev->lock);
  2180. }
  2181. /*
  2182. * Device is about to be destroyed: clean up.
  2183. */
  2184. void ipv6_mc_destroy_dev(struct inet6_dev *idev)
  2185. {
  2186. struct ifmcaddr6 *i;
  2187. /* Deactivate timers */
  2188. ipv6_mc_down(idev);
  2189. /* Delete all-nodes address. */
  2190. /* We cannot call ipv6_dev_mc_dec() directly, our caller in
  2191. * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
  2192. * fail.
  2193. */
  2194. __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
  2195. if (idev->cnf.forwarding)
  2196. __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
  2197. write_lock_bh(&idev->lock);
  2198. while ((i = idev->mc_list) != NULL) {
  2199. idev->mc_list = i->next;
  2200. write_unlock_bh(&idev->lock);
  2201. igmp6_group_dropped(i);
  2202. ma_put(i);
  2203. write_lock_bh(&idev->lock);
  2204. }
  2205. write_unlock_bh(&idev->lock);
  2206. }
  2207. #ifdef CONFIG_PROC_FS
  2208. struct igmp6_mc_iter_state {
  2209. struct seq_net_private p;
  2210. struct net_device *dev;
  2211. struct inet6_dev *idev;
  2212. };
  2213. #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
  2214. static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
  2215. {
  2216. struct ifmcaddr6 *im = NULL;
  2217. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  2218. struct net *net = seq_file_net(seq);
  2219. state->idev = NULL;
  2220. for_each_netdev_rcu(net, state->dev) {
  2221. struct inet6_dev *idev;
  2222. idev = __in6_dev_get(state->dev);
  2223. if (!idev)
  2224. continue;
  2225. read_lock_bh(&idev->lock);
  2226. im = idev->mc_list;
  2227. if (im) {
  2228. state->idev = idev;
  2229. break;
  2230. }
  2231. read_unlock_bh(&idev->lock);
  2232. }
  2233. return im;
  2234. }
  2235. static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
  2236. {
  2237. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  2238. im = im->next;
  2239. while (!im) {
  2240. if (likely(state->idev))
  2241. read_unlock_bh(&state->idev->lock);
  2242. state->dev = next_net_device_rcu(state->dev);
  2243. if (!state->dev) {
  2244. state->idev = NULL;
  2245. break;
  2246. }
  2247. state->idev = __in6_dev_get(state->dev);
  2248. if (!state->idev)
  2249. continue;
  2250. read_lock_bh(&state->idev->lock);
  2251. im = state->idev->mc_list;
  2252. }
  2253. return im;
  2254. }
  2255. static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
  2256. {
  2257. struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
  2258. if (im)
  2259. while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
  2260. --pos;
  2261. return pos ? NULL : im;
  2262. }
  2263. static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
  2264. __acquires(RCU)
  2265. {
  2266. rcu_read_lock();
  2267. return igmp6_mc_get_idx(seq, *pos);
  2268. }
  2269. static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2270. {
  2271. struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
  2272. ++*pos;
  2273. return im;
  2274. }
  2275. static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
  2276. __releases(RCU)
  2277. {
  2278. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  2279. if (likely(state->idev)) {
  2280. read_unlock_bh(&state->idev->lock);
  2281. state->idev = NULL;
  2282. }
  2283. state->dev = NULL;
  2284. rcu_read_unlock();
  2285. }
  2286. static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
  2287. {
  2288. struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
  2289. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  2290. seq_printf(seq,
  2291. "%-4d %-15s %pi6 %5d %08X %ld\n",
  2292. state->dev->ifindex, state->dev->name,
  2293. &im->mca_addr,
  2294. im->mca_users, im->mca_flags,
  2295. (im->mca_flags&MAF_TIMER_RUNNING) ?
  2296. jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
  2297. return 0;
  2298. }
  2299. static const struct seq_operations igmp6_mc_seq_ops = {
  2300. .start = igmp6_mc_seq_start,
  2301. .next = igmp6_mc_seq_next,
  2302. .stop = igmp6_mc_seq_stop,
  2303. .show = igmp6_mc_seq_show,
  2304. };
  2305. static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
  2306. {
  2307. return seq_open_net(inode, file, &igmp6_mc_seq_ops,
  2308. sizeof(struct igmp6_mc_iter_state));
  2309. }
  2310. static const struct file_operations igmp6_mc_seq_fops = {
  2311. .owner = THIS_MODULE,
  2312. .open = igmp6_mc_seq_open,
  2313. .read = seq_read,
  2314. .llseek = seq_lseek,
  2315. .release = seq_release_net,
  2316. };
  2317. struct igmp6_mcf_iter_state {
  2318. struct seq_net_private p;
  2319. struct net_device *dev;
  2320. struct inet6_dev *idev;
  2321. struct ifmcaddr6 *im;
  2322. };
  2323. #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
  2324. static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
  2325. {
  2326. struct ip6_sf_list *psf = NULL;
  2327. struct ifmcaddr6 *im = NULL;
  2328. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2329. struct net *net = seq_file_net(seq);
  2330. state->idev = NULL;
  2331. state->im = NULL;
  2332. for_each_netdev_rcu(net, state->dev) {
  2333. struct inet6_dev *idev;
  2334. idev = __in6_dev_get(state->dev);
  2335. if (unlikely(idev == NULL))
  2336. continue;
  2337. read_lock_bh(&idev->lock);
  2338. im = idev->mc_list;
  2339. if (likely(im)) {
  2340. spin_lock_bh(&im->mca_lock);
  2341. psf = im->mca_sources;
  2342. if (likely(psf)) {
  2343. state->im = im;
  2344. state->idev = idev;
  2345. break;
  2346. }
  2347. spin_unlock_bh(&im->mca_lock);
  2348. }
  2349. read_unlock_bh(&idev->lock);
  2350. }
  2351. return psf;
  2352. }
  2353. static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
  2354. {
  2355. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2356. psf = psf->sf_next;
  2357. while (!psf) {
  2358. spin_unlock_bh(&state->im->mca_lock);
  2359. state->im = state->im->next;
  2360. while (!state->im) {
  2361. if (likely(state->idev))
  2362. read_unlock_bh(&state->idev->lock);
  2363. state->dev = next_net_device_rcu(state->dev);
  2364. if (!state->dev) {
  2365. state->idev = NULL;
  2366. goto out;
  2367. }
  2368. state->idev = __in6_dev_get(state->dev);
  2369. if (!state->idev)
  2370. continue;
  2371. read_lock_bh(&state->idev->lock);
  2372. state->im = state->idev->mc_list;
  2373. }
  2374. if (!state->im)
  2375. break;
  2376. spin_lock_bh(&state->im->mca_lock);
  2377. psf = state->im->mca_sources;
  2378. }
  2379. out:
  2380. return psf;
  2381. }
  2382. static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
  2383. {
  2384. struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
  2385. if (psf)
  2386. while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
  2387. --pos;
  2388. return pos ? NULL : psf;
  2389. }
  2390. static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
  2391. __acquires(RCU)
  2392. {
  2393. rcu_read_lock();
  2394. return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2395. }
  2396. static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2397. {
  2398. struct ip6_sf_list *psf;
  2399. if (v == SEQ_START_TOKEN)
  2400. psf = igmp6_mcf_get_first(seq);
  2401. else
  2402. psf = igmp6_mcf_get_next(seq, v);
  2403. ++*pos;
  2404. return psf;
  2405. }
  2406. static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
  2407. __releases(RCU)
  2408. {
  2409. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2410. if (likely(state->im)) {
  2411. spin_unlock_bh(&state->im->mca_lock);
  2412. state->im = NULL;
  2413. }
  2414. if (likely(state->idev)) {
  2415. read_unlock_bh(&state->idev->lock);
  2416. state->idev = NULL;
  2417. }
  2418. state->dev = NULL;
  2419. rcu_read_unlock();
  2420. }
  2421. static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
  2422. {
  2423. struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
  2424. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2425. if (v == SEQ_START_TOKEN) {
  2426. seq_puts(seq, "Idx Device Multicast Address Source Address INC EXC\n");
  2427. } else {
  2428. seq_printf(seq,
  2429. "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
  2430. state->dev->ifindex, state->dev->name,
  2431. &state->im->mca_addr,
  2432. &psf->sf_addr,
  2433. psf->sf_count[MCAST_INCLUDE],
  2434. psf->sf_count[MCAST_EXCLUDE]);
  2435. }
  2436. return 0;
  2437. }
  2438. static const struct seq_operations igmp6_mcf_seq_ops = {
  2439. .start = igmp6_mcf_seq_start,
  2440. .next = igmp6_mcf_seq_next,
  2441. .stop = igmp6_mcf_seq_stop,
  2442. .show = igmp6_mcf_seq_show,
  2443. };
  2444. static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
  2445. {
  2446. return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
  2447. sizeof(struct igmp6_mcf_iter_state));
  2448. }
  2449. static const struct file_operations igmp6_mcf_seq_fops = {
  2450. .owner = THIS_MODULE,
  2451. .open = igmp6_mcf_seq_open,
  2452. .read = seq_read,
  2453. .llseek = seq_lseek,
  2454. .release = seq_release_net,
  2455. };
  2456. static int __net_init igmp6_proc_init(struct net *net)
  2457. {
  2458. int err;
  2459. err = -ENOMEM;
  2460. if (!proc_create("igmp6", S_IRUGO, net->proc_net, &igmp6_mc_seq_fops))
  2461. goto out;
  2462. if (!proc_create("mcfilter6", S_IRUGO, net->proc_net,
  2463. &igmp6_mcf_seq_fops))
  2464. goto out_proc_net_igmp6;
  2465. err = 0;
  2466. out:
  2467. return err;
  2468. out_proc_net_igmp6:
  2469. remove_proc_entry("igmp6", net->proc_net);
  2470. goto out;
  2471. }
  2472. static void __net_exit igmp6_proc_exit(struct net *net)
  2473. {
  2474. remove_proc_entry("mcfilter6", net->proc_net);
  2475. remove_proc_entry("igmp6", net->proc_net);
  2476. }
  2477. #else
  2478. static inline int igmp6_proc_init(struct net *net)
  2479. {
  2480. return 0;
  2481. }
  2482. static inline void igmp6_proc_exit(struct net *net)
  2483. {
  2484. }
  2485. #endif
  2486. static int __net_init igmp6_net_init(struct net *net)
  2487. {
  2488. int err;
  2489. err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
  2490. SOCK_RAW, IPPROTO_ICMPV6, net);
  2491. if (err < 0) {
  2492. pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
  2493. err);
  2494. goto out;
  2495. }
  2496. inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
  2497. err = inet_ctl_sock_create(&net->ipv6.mc_autojoin_sk, PF_INET6,
  2498. SOCK_RAW, IPPROTO_ICMPV6, net);
  2499. if (err < 0) {
  2500. pr_err("Failed to initialize the IGMP6 autojoin socket (err %d)\n",
  2501. err);
  2502. goto out_sock_create;
  2503. }
  2504. err = igmp6_proc_init(net);
  2505. if (err)
  2506. goto out_sock_create_autojoin;
  2507. return 0;
  2508. out_sock_create_autojoin:
  2509. inet_ctl_sock_destroy(net->ipv6.mc_autojoin_sk);
  2510. out_sock_create:
  2511. inet_ctl_sock_destroy(net->ipv6.igmp_sk);
  2512. out:
  2513. return err;
  2514. }
  2515. static void __net_exit igmp6_net_exit(struct net *net)
  2516. {
  2517. inet_ctl_sock_destroy(net->ipv6.igmp_sk);
  2518. inet_ctl_sock_destroy(net->ipv6.mc_autojoin_sk);
  2519. igmp6_proc_exit(net);
  2520. }
  2521. static struct pernet_operations igmp6_net_ops = {
  2522. .init = igmp6_net_init,
  2523. .exit = igmp6_net_exit,
  2524. };
  2525. int __init igmp6_init(void)
  2526. {
  2527. return register_pernet_subsys(&igmp6_net_ops);
  2528. }
  2529. void igmp6_cleanup(void)
  2530. {
  2531. unregister_pernet_subsys(&igmp6_net_ops);
  2532. }