mcast.c 59 KB

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