mcast.c 62 KB

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