mcast.c 72 KB

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