mcast.c 62 KB

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