mcast.c 71 KB

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