mcast.c 62 KB

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