mcast.c 62 KB

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