mcast.c 59 KB

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