mcast.c 59 KB

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