mcast.c 72 KB

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