mcast.c 77 KB

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