mcast.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642
  1. /*
  2. * Multicast support for IPv6
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * $Id: mcast.c,v 1.40 2002/02/08 03:57:19 davem Exp $
  9. *
  10. * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. /* Changes:
  18. *
  19. * yoshfuji : fix format of router-alert option
  20. * YOSHIFUJI Hideaki @USAGI:
  21. * Fixed source address for MLD message based on
  22. * <draft-ietf-magma-mld-source-05.txt>.
  23. * YOSHIFUJI Hideaki @USAGI:
  24. * - Ignore Queries for invalid addresses.
  25. * - MLD for link-local addresses.
  26. * David L Stevens <dlstevens@us.ibm.com>:
  27. * - MLDv2 support
  28. */
  29. #include <linux/config.h>
  30. #include <linux/module.h>
  31. #include <linux/errno.h>
  32. #include <linux/types.h>
  33. #include <linux/string.h>
  34. #include <linux/socket.h>
  35. #include <linux/sockios.h>
  36. #include <linux/jiffies.h>
  37. #include <linux/times.h>
  38. #include <linux/net.h>
  39. #include <linux/in.h>
  40. #include <linux/in6.h>
  41. #include <linux/netdevice.h>
  42. #include <linux/if_arp.h>
  43. #include <linux/route.h>
  44. #include <linux/init.h>
  45. #include <linux/proc_fs.h>
  46. #include <linux/seq_file.h>
  47. #include <linux/netfilter.h>
  48. #include <linux/netfilter_ipv6.h>
  49. #include <net/sock.h>
  50. #include <net/snmp.h>
  51. #include <net/ipv6.h>
  52. #include <net/protocol.h>
  53. #include <net/if_inet6.h>
  54. #include <net/ndisc.h>
  55. #include <net/addrconf.h>
  56. #include <net/ip6_route.h>
  57. #include <net/ip6_checksum.h>
  58. /* Set to 3 to get tracing... */
  59. #define MCAST_DEBUG 2
  60. #if MCAST_DEBUG >= 3
  61. #define MDBG(x) printk x
  62. #else
  63. #define MDBG(x)
  64. #endif
  65. /*
  66. * These header formats should be in a separate include file, but icmpv6.h
  67. * doesn't have in6_addr defined in all cases, there is no __u128, and no
  68. * other files reference these.
  69. *
  70. * +-DLS 4/14/03
  71. */
  72. /* Multicast Listener Discovery version 2 headers */
  73. struct mld2_grec {
  74. __u8 grec_type;
  75. __u8 grec_auxwords;
  76. __u16 grec_nsrcs;
  77. struct in6_addr grec_mca;
  78. struct in6_addr grec_src[0];
  79. };
  80. struct mld2_report {
  81. __u8 type;
  82. __u8 resv1;
  83. __u16 csum;
  84. __u16 resv2;
  85. __u16 ngrec;
  86. struct mld2_grec grec[0];
  87. };
  88. struct mld2_query {
  89. __u8 type;
  90. __u8 code;
  91. __u16 csum;
  92. __u16 mrc;
  93. __u16 resv1;
  94. struct in6_addr mca;
  95. #if defined(__LITTLE_ENDIAN_BITFIELD)
  96. __u8 qrv:3,
  97. suppress:1,
  98. resv2:4;
  99. #elif defined(__BIG_ENDIAN_BITFIELD)
  100. __u8 resv2:4,
  101. suppress:1,
  102. qrv:3;
  103. #else
  104. #error "Please fix <asm/byteorder.h>"
  105. #endif
  106. __u8 qqic;
  107. __u16 nsrcs;
  108. struct in6_addr srcs[0];
  109. };
  110. static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
  111. /* Big mc list lock for all the sockets */
  112. static DEFINE_RWLOCK(ipv6_sk_mc_lock);
  113. static struct socket *igmp6_socket;
  114. int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr);
  115. static void igmp6_join_group(struct ifmcaddr6 *ma);
  116. static void igmp6_leave_group(struct ifmcaddr6 *ma);
  117. static void igmp6_timer_handler(unsigned long data);
  118. static void mld_gq_timer_expire(unsigned long data);
  119. static void mld_ifc_timer_expire(unsigned long data);
  120. static void mld_ifc_event(struct inet6_dev *idev);
  121. static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
  122. static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *addr);
  123. static void mld_clear_delrec(struct inet6_dev *idev);
  124. static int sf_setstate(struct ifmcaddr6 *pmc);
  125. static void sf_markstate(struct ifmcaddr6 *pmc);
  126. static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
  127. static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
  128. int sfmode, int sfcount, struct in6_addr *psfsrc,
  129. int delta);
  130. static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
  131. int sfmode, int sfcount, struct in6_addr *psfsrc,
  132. int delta);
  133. static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
  134. struct inet6_dev *idev);
  135. #define IGMP6_UNSOLICITED_IVAL (10*HZ)
  136. #define MLD_QRV_DEFAULT 2
  137. #define MLD_V1_SEEN(idev) (ipv6_devconf.force_mld_version == 1 || \
  138. (idev)->cnf.force_mld_version == 1 || \
  139. ((idev)->mc_v1_seen && \
  140. time_before(jiffies, (idev)->mc_v1_seen)))
  141. #define MLDV2_MASK(value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value))
  142. #define MLDV2_EXP(thresh, nbmant, nbexp, value) \
  143. ((value) < (thresh) ? (value) : \
  144. ((MLDV2_MASK(value, nbmant) | (1<<(nbmant))) << \
  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 = IPV6_MLD_MAX_MSF;
  150. /*
  151. * socket join on multicast group
  152. */
  153. int ipv6_sock_mc_join(struct sock *sk, int ifindex, struct in6_addr *addr)
  154. {
  155. struct net_device *dev = NULL;
  156. struct ipv6_mc_socklist *mc_lst;
  157. struct ipv6_pinfo *np = inet6_sk(sk);
  158. int err;
  159. if (!ipv6_addr_is_multicast(addr))
  160. return -EINVAL;
  161. read_lock_bh(&ipv6_sk_mc_lock);
  162. for (mc_lst=np->ipv6_mc_list; mc_lst; mc_lst=mc_lst->next) {
  163. if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
  164. ipv6_addr_equal(&mc_lst->addr, addr)) {
  165. read_unlock_bh(&ipv6_sk_mc_lock);
  166. return -EADDRINUSE;
  167. }
  168. }
  169. read_unlock_bh(&ipv6_sk_mc_lock);
  170. mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
  171. if (mc_lst == NULL)
  172. return -ENOMEM;
  173. mc_lst->next = NULL;
  174. ipv6_addr_copy(&mc_lst->addr, addr);
  175. if (ifindex == 0) {
  176. struct rt6_info *rt;
  177. rt = rt6_lookup(addr, NULL, 0, 0);
  178. if (rt) {
  179. dev = rt->rt6i_dev;
  180. dev_hold(dev);
  181. dst_release(&rt->u.dst);
  182. }
  183. } else
  184. dev = dev_get_by_index(ifindex);
  185. if (dev == NULL) {
  186. sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
  187. return -ENODEV;
  188. }
  189. mc_lst->ifindex = dev->ifindex;
  190. mc_lst->sfmode = MCAST_EXCLUDE;
  191. mc_lst->sflock = RW_LOCK_UNLOCKED;
  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(mc_lst->ifindex)) != NULL) {
  224. struct inet6_dev *idev = in6_dev_get(dev);
  225. if (idev) {
  226. (void) ip6_mc_leave_src(sk,mc_lst,idev);
  227. __ipv6_dev_mc_dec(idev, &mc_lst->addr);
  228. in6_dev_put(idev);
  229. }
  230. dev_put(dev);
  231. }
  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. if (idev) {
  282. (void) ip6_mc_leave_src(sk, mc_lst, idev);
  283. __ipv6_dev_mc_dec(idev, &mc_lst->addr);
  284. in6_dev_put(idev);
  285. }
  286. dev_put(dev);
  287. }
  288. sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
  289. write_lock_bh(&ipv6_sk_mc_lock);
  290. }
  291. write_unlock_bh(&ipv6_sk_mc_lock);
  292. }
  293. int ip6_mc_source(int add, int omode, struct sock *sk,
  294. struct group_source_req *pgsr)
  295. {
  296. struct in6_addr *source, *group;
  297. struct ipv6_mc_socklist *pmc;
  298. struct net_device *dev;
  299. struct inet6_dev *idev;
  300. struct ipv6_pinfo *inet6 = inet6_sk(sk);
  301. struct ip6_sf_socklist *psl;
  302. int i, j, rv;
  303. int leavegroup = 0;
  304. int pmclocked = 0;
  305. int err;
  306. if (pgsr->gsr_group.ss_family != AF_INET6 ||
  307. pgsr->gsr_source.ss_family != AF_INET6)
  308. return -EINVAL;
  309. source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
  310. group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
  311. if (!ipv6_addr_is_multicast(group))
  312. return -EINVAL;
  313. idev = ip6_mc_find_dev(group, pgsr->gsr_interface);
  314. if (!idev)
  315. return -ENODEV;
  316. dev = idev->dev;
  317. err = -EADDRNOTAVAIL;
  318. read_lock_bh(&ipv6_sk_mc_lock);
  319. for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
  320. if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
  321. continue;
  322. if (ipv6_addr_equal(&pmc->addr, group))
  323. break;
  324. }
  325. if (!pmc) { /* must have a prior join */
  326. err = -EINVAL;
  327. goto done;
  328. }
  329. /* if a source filter was set, must be the same mode as before */
  330. if (pmc->sflist) {
  331. if (pmc->sfmode != omode) {
  332. err = -EINVAL;
  333. goto done;
  334. }
  335. } else if (pmc->sfmode != omode) {
  336. /* allow mode switches for empty-set filters */
  337. ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
  338. ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
  339. pmc->sfmode = omode;
  340. }
  341. write_lock_bh(&pmc->sflock);
  342. pmclocked = 1;
  343. psl = pmc->sflist;
  344. if (!add) {
  345. if (!psl)
  346. goto done; /* err = -EADDRNOTAVAIL */
  347. rv = !0;
  348. for (i=0; i<psl->sl_count; i++) {
  349. rv = memcmp(&psl->sl_addr[i], source,
  350. sizeof(struct in6_addr));
  351. if (rv == 0)
  352. break;
  353. }
  354. if (rv) /* source not found */
  355. goto done; /* err = -EADDRNOTAVAIL */
  356. /* special case - (INCLUDE, empty) == LEAVE_GROUP */
  357. if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
  358. leavegroup = 1;
  359. goto done;
  360. }
  361. /* update the interface filter */
  362. ip6_mc_del_src(idev, group, omode, 1, source, 1);
  363. for (j=i+1; j<psl->sl_count; j++)
  364. psl->sl_addr[j-1] = psl->sl_addr[j];
  365. psl->sl_count--;
  366. err = 0;
  367. goto done;
  368. }
  369. /* else, add a new source to the filter */
  370. if (psl && psl->sl_count >= sysctl_mld_max_msf) {
  371. err = -ENOBUFS;
  372. goto done;
  373. }
  374. if (!psl || psl->sl_count == psl->sl_max) {
  375. struct ip6_sf_socklist *newpsl;
  376. int count = IP6_SFBLOCK;
  377. if (psl)
  378. count += psl->sl_max;
  379. newpsl = (struct ip6_sf_socklist *)sock_kmalloc(sk,
  380. 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 = (struct ip6_sf_socklist *)sock_kmalloc(sk,
  458. IP6_SFLSIZE(gsf->gf_numsrc), 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 = (struct ifmcaddr6 *)kmalloc(sizeof(*pmc), GFP_ATOMIC);
  662. if (!pmc)
  663. return;
  664. memset(pmc, 0, sizeof(*pmc));
  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 = kmalloc(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. memset(mc, 0, sizeof(struct ifmcaddr6));
  777. init_timer(&mc->mca_timer);
  778. mc->mca_timer.function = igmp6_timer_handler;
  779. mc->mca_timer.data = (unsigned long) mc;
  780. ipv6_addr_copy(&mc->mca_addr, addr);
  781. mc->idev = idev;
  782. mc->mca_users = 1;
  783. /* mca_stamp should be updated upon changes */
  784. mc->mca_cstamp = mc->mca_tstamp = jiffies;
  785. atomic_set(&mc->mca_refcnt, 2);
  786. spin_lock_init(&mc->mca_lock);
  787. /* initial mode is (EX, empty) */
  788. mc->mca_sfmode = MCAST_EXCLUDE;
  789. mc->mca_sfcount[MCAST_EXCLUDE] = 1;
  790. if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
  791. IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
  792. mc->mca_flags |= MAF_NOREPORT;
  793. mc->next = idev->mc_list;
  794. idev->mc_list = mc;
  795. write_unlock_bh(&idev->lock);
  796. mld_del_delrec(idev, &mc->mca_addr);
  797. igmp6_group_added(mc);
  798. ma_put(mc);
  799. return 0;
  800. }
  801. /*
  802. * device multicast group del
  803. */
  804. int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr)
  805. {
  806. struct ifmcaddr6 *ma, **map;
  807. write_lock_bh(&idev->lock);
  808. for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
  809. if (ipv6_addr_equal(&ma->mca_addr, addr)) {
  810. if (--ma->mca_users == 0) {
  811. *map = ma->next;
  812. write_unlock_bh(&idev->lock);
  813. igmp6_group_dropped(ma);
  814. ma_put(ma);
  815. return 0;
  816. }
  817. write_unlock_bh(&idev->lock);
  818. return 0;
  819. }
  820. }
  821. write_unlock_bh(&idev->lock);
  822. return -ENOENT;
  823. }
  824. int ipv6_dev_mc_dec(struct net_device *dev, struct in6_addr *addr)
  825. {
  826. struct inet6_dev *idev = in6_dev_get(dev);
  827. int err;
  828. if (!idev)
  829. return -ENODEV;
  830. err = __ipv6_dev_mc_dec(idev, addr);
  831. in6_dev_put(idev);
  832. return err;
  833. }
  834. /*
  835. * identify MLD packets for MLD filter exceptions
  836. */
  837. int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
  838. {
  839. struct icmp6hdr *pic;
  840. if (nexthdr != IPPROTO_ICMPV6)
  841. return 0;
  842. if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
  843. return 0;
  844. pic = (struct icmp6hdr *)skb->h.raw;
  845. switch (pic->icmp6_type) {
  846. case ICMPV6_MGM_QUERY:
  847. case ICMPV6_MGM_REPORT:
  848. case ICMPV6_MGM_REDUCTION:
  849. case ICMPV6_MLD2_REPORT:
  850. return 1;
  851. default:
  852. break;
  853. }
  854. return 0;
  855. }
  856. /*
  857. * check if the interface/address pair is valid
  858. */
  859. int ipv6_chk_mcast_addr(struct net_device *dev, struct in6_addr *group,
  860. struct in6_addr *src_addr)
  861. {
  862. struct inet6_dev *idev;
  863. struct ifmcaddr6 *mc;
  864. int rv = 0;
  865. idev = in6_dev_get(dev);
  866. if (idev) {
  867. read_lock_bh(&idev->lock);
  868. for (mc = idev->mc_list; mc; mc=mc->next) {
  869. if (ipv6_addr_equal(&mc->mca_addr, group))
  870. break;
  871. }
  872. if (mc) {
  873. if (src_addr && !ipv6_addr_any(src_addr)) {
  874. struct ip6_sf_list *psf;
  875. spin_lock_bh(&mc->mca_lock);
  876. for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
  877. if (ipv6_addr_equal(&psf->sf_addr, src_addr))
  878. break;
  879. }
  880. if (psf)
  881. rv = psf->sf_count[MCAST_INCLUDE] ||
  882. psf->sf_count[MCAST_EXCLUDE] !=
  883. mc->mca_sfcount[MCAST_EXCLUDE];
  884. else
  885. rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
  886. spin_unlock_bh(&mc->mca_lock);
  887. } else
  888. rv = 1; /* don't filter unspecified source */
  889. }
  890. read_unlock_bh(&idev->lock);
  891. in6_dev_put(idev);
  892. }
  893. return rv;
  894. }
  895. static void mld_gq_start_timer(struct inet6_dev *idev)
  896. {
  897. int tv = net_random() % idev->mc_maxdelay;
  898. idev->mc_gq_running = 1;
  899. if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
  900. in6_dev_hold(idev);
  901. }
  902. static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
  903. {
  904. int tv = net_random() % delay;
  905. if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
  906. in6_dev_hold(idev);
  907. }
  908. /*
  909. * IGMP handling (alias multicast ICMPv6 messages)
  910. */
  911. static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
  912. {
  913. unsigned long delay = resptime;
  914. /* Do not start timer for these addresses */
  915. if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
  916. IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
  917. return;
  918. if (del_timer(&ma->mca_timer)) {
  919. atomic_dec(&ma->mca_refcnt);
  920. delay = ma->mca_timer.expires - jiffies;
  921. }
  922. if (delay >= resptime) {
  923. if (resptime)
  924. delay = net_random() % resptime;
  925. else
  926. delay = 1;
  927. }
  928. ma->mca_timer.expires = jiffies + delay;
  929. if (!mod_timer(&ma->mca_timer, jiffies + delay))
  930. atomic_inc(&ma->mca_refcnt);
  931. ma->mca_flags |= MAF_TIMER_RUNNING;
  932. }
  933. /* mark EXCLUDE-mode sources */
  934. static int mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
  935. struct in6_addr *srcs)
  936. {
  937. struct ip6_sf_list *psf;
  938. int i, scount;
  939. scount = 0;
  940. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  941. if (scount == nsrcs)
  942. break;
  943. for (i=0; i<nsrcs; i++) {
  944. /* skip inactive filters */
  945. if (pmc->mca_sfcount[MCAST_INCLUDE] ||
  946. pmc->mca_sfcount[MCAST_EXCLUDE] !=
  947. psf->sf_count[MCAST_EXCLUDE])
  948. continue;
  949. if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
  950. scount++;
  951. break;
  952. }
  953. }
  954. }
  955. pmc->mca_flags &= ~MAF_GSQUERY;
  956. if (scount == nsrcs) /* all sources excluded */
  957. return 0;
  958. return 1;
  959. }
  960. static int mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
  961. struct in6_addr *srcs)
  962. {
  963. struct ip6_sf_list *psf;
  964. int i, scount;
  965. if (pmc->mca_sfmode == MCAST_EXCLUDE)
  966. return mld_xmarksources(pmc, nsrcs, srcs);
  967. /* mark INCLUDE-mode sources */
  968. scount = 0;
  969. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  970. if (scount == nsrcs)
  971. break;
  972. for (i=0; i<nsrcs; i++) {
  973. if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
  974. psf->sf_gsresp = 1;
  975. scount++;
  976. break;
  977. }
  978. }
  979. }
  980. if (!scount) {
  981. pmc->mca_flags &= ~MAF_GSQUERY;
  982. return 0;
  983. }
  984. pmc->mca_flags |= MAF_GSQUERY;
  985. return 1;
  986. }
  987. int igmp6_event_query(struct sk_buff *skb)
  988. {
  989. struct mld2_query *mlh2 = NULL;
  990. struct ifmcaddr6 *ma;
  991. struct in6_addr *group;
  992. unsigned long max_delay;
  993. struct inet6_dev *idev;
  994. struct icmp6hdr *hdr;
  995. int group_type;
  996. int mark = 0;
  997. int len;
  998. if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
  999. return -EINVAL;
  1000. /* compute payload length excluding extension headers */
  1001. len = ntohs(skb->nh.ipv6h->payload_len) + sizeof(struct ipv6hdr);
  1002. len -= (char *)skb->h.raw - (char *)skb->nh.ipv6h;
  1003. /* Drop queries with not link local source */
  1004. if (!(ipv6_addr_type(&skb->nh.ipv6h->saddr)&IPV6_ADDR_LINKLOCAL))
  1005. return -EINVAL;
  1006. idev = in6_dev_get(skb->dev);
  1007. if (idev == NULL)
  1008. return 0;
  1009. hdr = (struct icmp6hdr *) skb->h.raw;
  1010. group = (struct in6_addr *) (hdr + 1);
  1011. group_type = ipv6_addr_type(group);
  1012. if (group_type != IPV6_ADDR_ANY &&
  1013. !(group_type&IPV6_ADDR_MULTICAST)) {
  1014. in6_dev_put(idev);
  1015. return -EINVAL;
  1016. }
  1017. if (len == 24) {
  1018. int switchback;
  1019. /* MLDv1 router present */
  1020. /* Translate milliseconds to jiffies */
  1021. max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
  1022. switchback = (idev->mc_qrv + 1) * max_delay;
  1023. idev->mc_v1_seen = jiffies + switchback;
  1024. /* cancel the interface change timer */
  1025. idev->mc_ifc_count = 0;
  1026. if (del_timer(&idev->mc_ifc_timer))
  1027. __in6_dev_put(idev);
  1028. /* clear deleted report items */
  1029. mld_clear_delrec(idev);
  1030. } else if (len >= 28) {
  1031. int srcs_offset = sizeof(struct mld2_query) -
  1032. sizeof(struct icmp6hdr);
  1033. if (!pskb_may_pull(skb, srcs_offset)) {
  1034. in6_dev_put(idev);
  1035. return -EINVAL;
  1036. }
  1037. mlh2 = (struct mld2_query *) skb->h.raw;
  1038. max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
  1039. if (!max_delay)
  1040. max_delay = 1;
  1041. idev->mc_maxdelay = max_delay;
  1042. if (mlh2->qrv)
  1043. idev->mc_qrv = mlh2->qrv;
  1044. if (group_type == IPV6_ADDR_ANY) { /* general query */
  1045. if (mlh2->nsrcs) {
  1046. in6_dev_put(idev);
  1047. return -EINVAL; /* no sources allowed */
  1048. }
  1049. mld_gq_start_timer(idev);
  1050. in6_dev_put(idev);
  1051. return 0;
  1052. }
  1053. /* mark sources to include, if group & source-specific */
  1054. if (mlh2->nsrcs != 0) {
  1055. if (!pskb_may_pull(skb, srcs_offset +
  1056. ntohs(mlh2->nsrcs) * sizeof(struct in6_addr))) {
  1057. in6_dev_put(idev);
  1058. return -EINVAL;
  1059. }
  1060. mlh2 = (struct mld2_query *) skb->h.raw;
  1061. mark = 1;
  1062. }
  1063. } else {
  1064. in6_dev_put(idev);
  1065. return -EINVAL;
  1066. }
  1067. read_lock_bh(&idev->lock);
  1068. if (group_type == IPV6_ADDR_ANY) {
  1069. for (ma = idev->mc_list; ma; ma=ma->next) {
  1070. spin_lock_bh(&ma->mca_lock);
  1071. igmp6_group_queried(ma, max_delay);
  1072. spin_unlock_bh(&ma->mca_lock);
  1073. }
  1074. } else {
  1075. for (ma = idev->mc_list; ma; ma=ma->next) {
  1076. if (group_type != IPV6_ADDR_ANY &&
  1077. !ipv6_addr_equal(group, &ma->mca_addr))
  1078. continue;
  1079. spin_lock_bh(&ma->mca_lock);
  1080. if (ma->mca_flags & MAF_TIMER_RUNNING) {
  1081. /* gsquery <- gsquery && mark */
  1082. if (!mark)
  1083. ma->mca_flags &= ~MAF_GSQUERY;
  1084. } else {
  1085. /* gsquery <- mark */
  1086. if (mark)
  1087. ma->mca_flags |= MAF_GSQUERY;
  1088. else
  1089. ma->mca_flags &= ~MAF_GSQUERY;
  1090. }
  1091. if (!(ma->mca_flags & MAF_GSQUERY) ||
  1092. mld_marksources(ma, ntohs(mlh2->nsrcs), mlh2->srcs))
  1093. igmp6_group_queried(ma, max_delay);
  1094. spin_unlock_bh(&ma->mca_lock);
  1095. if (group_type != IPV6_ADDR_ANY)
  1096. break;
  1097. }
  1098. }
  1099. read_unlock_bh(&idev->lock);
  1100. in6_dev_put(idev);
  1101. return 0;
  1102. }
  1103. int igmp6_event_report(struct sk_buff *skb)
  1104. {
  1105. struct ifmcaddr6 *ma;
  1106. struct in6_addr *addrp;
  1107. struct inet6_dev *idev;
  1108. struct icmp6hdr *hdr;
  1109. int addr_type;
  1110. /* Our own report looped back. Ignore it. */
  1111. if (skb->pkt_type == PACKET_LOOPBACK)
  1112. return 0;
  1113. /* send our report if the MC router may not have heard this report */
  1114. if (skb->pkt_type != PACKET_MULTICAST &&
  1115. skb->pkt_type != PACKET_BROADCAST)
  1116. return 0;
  1117. if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
  1118. return -EINVAL;
  1119. hdr = (struct icmp6hdr*) skb->h.raw;
  1120. /* Drop reports with not link local source */
  1121. addr_type = ipv6_addr_type(&skb->nh.ipv6h->saddr);
  1122. if (addr_type != IPV6_ADDR_ANY &&
  1123. !(addr_type&IPV6_ADDR_LINKLOCAL))
  1124. return -EINVAL;
  1125. addrp = (struct in6_addr *) (hdr + 1);
  1126. idev = in6_dev_get(skb->dev);
  1127. if (idev == NULL)
  1128. return -ENODEV;
  1129. /*
  1130. * Cancel the timer for this group
  1131. */
  1132. read_lock_bh(&idev->lock);
  1133. for (ma = idev->mc_list; ma; ma=ma->next) {
  1134. if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
  1135. spin_lock(&ma->mca_lock);
  1136. if (del_timer(&ma->mca_timer))
  1137. atomic_dec(&ma->mca_refcnt);
  1138. ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
  1139. spin_unlock(&ma->mca_lock);
  1140. break;
  1141. }
  1142. }
  1143. read_unlock_bh(&idev->lock);
  1144. in6_dev_put(idev);
  1145. return 0;
  1146. }
  1147. static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
  1148. int gdeleted, int sdeleted)
  1149. {
  1150. switch (type) {
  1151. case MLD2_MODE_IS_INCLUDE:
  1152. case MLD2_MODE_IS_EXCLUDE:
  1153. if (gdeleted || sdeleted)
  1154. return 0;
  1155. if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
  1156. if (pmc->mca_sfmode == MCAST_INCLUDE)
  1157. return 1;
  1158. /* don't include if this source is excluded
  1159. * in all filters
  1160. */
  1161. if (psf->sf_count[MCAST_INCLUDE])
  1162. return 0;
  1163. return pmc->mca_sfcount[MCAST_EXCLUDE] ==
  1164. psf->sf_count[MCAST_EXCLUDE];
  1165. }
  1166. return 0;
  1167. case MLD2_CHANGE_TO_INCLUDE:
  1168. if (gdeleted || sdeleted)
  1169. return 0;
  1170. return psf->sf_count[MCAST_INCLUDE] != 0;
  1171. case MLD2_CHANGE_TO_EXCLUDE:
  1172. if (gdeleted || sdeleted)
  1173. return 0;
  1174. if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
  1175. psf->sf_count[MCAST_INCLUDE])
  1176. return 0;
  1177. return pmc->mca_sfcount[MCAST_EXCLUDE] ==
  1178. psf->sf_count[MCAST_EXCLUDE];
  1179. case MLD2_ALLOW_NEW_SOURCES:
  1180. if (gdeleted || !psf->sf_crcount)
  1181. return 0;
  1182. return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
  1183. case MLD2_BLOCK_OLD_SOURCES:
  1184. if (pmc->mca_sfmode == MCAST_INCLUDE)
  1185. return gdeleted || (psf->sf_crcount && sdeleted);
  1186. return psf->sf_crcount && !gdeleted && !sdeleted;
  1187. }
  1188. return 0;
  1189. }
  1190. static int
  1191. mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
  1192. {
  1193. struct ip6_sf_list *psf;
  1194. int scount = 0;
  1195. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  1196. if (!is_in(pmc, psf, type, gdeleted, sdeleted))
  1197. continue;
  1198. scount++;
  1199. }
  1200. return scount;
  1201. }
  1202. static struct sk_buff *mld_newpack(struct net_device *dev, int size)
  1203. {
  1204. struct sock *sk = igmp6_socket->sk;
  1205. struct sk_buff *skb;
  1206. struct mld2_report *pmr;
  1207. struct in6_addr addr_buf;
  1208. int err;
  1209. u8 ra[8] = { IPPROTO_ICMPV6, 0,
  1210. IPV6_TLV_ROUTERALERT, 2, 0, 0,
  1211. IPV6_TLV_PADN, 0 };
  1212. /* we assume size > sizeof(ra) here */
  1213. skb = sock_alloc_send_skb(sk, size + LL_RESERVED_SPACE(dev), 1, &err);
  1214. if (skb == 0)
  1215. return NULL;
  1216. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  1217. if (ipv6_get_lladdr(dev, &addr_buf)) {
  1218. /* <draft-ietf-magma-mld-source-05.txt>:
  1219. * use unspecified address as the source address
  1220. * when a valid link-local address is not available.
  1221. */
  1222. memset(&addr_buf, 0, sizeof(addr_buf));
  1223. }
  1224. ip6_nd_hdr(sk, skb, dev, &addr_buf, &mld2_all_mcr, NEXTHDR_HOP, 0);
  1225. memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
  1226. pmr =(struct mld2_report *)skb_put(skb, sizeof(*pmr));
  1227. skb->h.raw = (unsigned char *)pmr;
  1228. pmr->type = ICMPV6_MLD2_REPORT;
  1229. pmr->resv1 = 0;
  1230. pmr->csum = 0;
  1231. pmr->resv2 = 0;
  1232. pmr->ngrec = 0;
  1233. return skb;
  1234. }
  1235. static inline int mld_dev_queue_xmit2(struct sk_buff *skb)
  1236. {
  1237. struct net_device *dev = skb->dev;
  1238. if (dev->hard_header) {
  1239. unsigned char ha[MAX_ADDR_LEN];
  1240. int err;
  1241. ndisc_mc_map(&skb->nh.ipv6h->daddr, ha, dev, 1);
  1242. err = dev->hard_header(skb, dev, ETH_P_IPV6, ha, NULL, skb->len);
  1243. if (err < 0) {
  1244. kfree_skb(skb);
  1245. return err;
  1246. }
  1247. }
  1248. return dev_queue_xmit(skb);
  1249. }
  1250. static inline int mld_dev_queue_xmit(struct sk_buff *skb)
  1251. {
  1252. return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dev,
  1253. mld_dev_queue_xmit2);
  1254. }
  1255. static void mld_sendpack(struct sk_buff *skb)
  1256. {
  1257. struct ipv6hdr *pip6 = skb->nh.ipv6h;
  1258. struct mld2_report *pmr = (struct mld2_report *)skb->h.raw;
  1259. int payload_len, mldlen;
  1260. struct inet6_dev *idev = in6_dev_get(skb->dev);
  1261. int err;
  1262. IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
  1263. payload_len = skb->tail - (unsigned char *)skb->nh.ipv6h -
  1264. sizeof(struct ipv6hdr);
  1265. mldlen = skb->tail - skb->h.raw;
  1266. pip6->payload_len = htons(payload_len);
  1267. pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
  1268. IPPROTO_ICMPV6, csum_partial(skb->h.raw, mldlen, 0));
  1269. err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
  1270. mld_dev_queue_xmit);
  1271. if (!err) {
  1272. ICMP6_INC_STATS(idev,ICMP6_MIB_OUTMSGS);
  1273. IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
  1274. } else
  1275. IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
  1276. if (likely(idev != NULL))
  1277. in6_dev_put(idev);
  1278. }
  1279. static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
  1280. {
  1281. return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
  1282. }
  1283. static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
  1284. int type, struct mld2_grec **ppgr)
  1285. {
  1286. struct net_device *dev = pmc->idev->dev;
  1287. struct mld2_report *pmr;
  1288. struct mld2_grec *pgr;
  1289. if (!skb)
  1290. skb = mld_newpack(dev, dev->mtu);
  1291. if (!skb)
  1292. return NULL;
  1293. pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
  1294. pgr->grec_type = type;
  1295. pgr->grec_auxwords = 0;
  1296. pgr->grec_nsrcs = 0;
  1297. pgr->grec_mca = pmc->mca_addr; /* structure copy */
  1298. pmr = (struct mld2_report *)skb->h.raw;
  1299. pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
  1300. *ppgr = pgr;
  1301. return skb;
  1302. }
  1303. #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
  1304. skb_tailroom(skb)) : 0)
  1305. static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
  1306. int type, int gdeleted, int sdeleted)
  1307. {
  1308. struct net_device *dev = pmc->idev->dev;
  1309. struct mld2_report *pmr;
  1310. struct mld2_grec *pgr = NULL;
  1311. struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
  1312. int scount, stotal, first, isquery, truncate;
  1313. if (pmc->mca_flags & MAF_NOREPORT)
  1314. return skb;
  1315. isquery = type == MLD2_MODE_IS_INCLUDE ||
  1316. type == MLD2_MODE_IS_EXCLUDE;
  1317. truncate = type == MLD2_MODE_IS_EXCLUDE ||
  1318. type == MLD2_CHANGE_TO_EXCLUDE;
  1319. stotal = scount = 0;
  1320. psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
  1321. if (!*psf_list)
  1322. goto empty_source;
  1323. pmr = skb ? (struct mld2_report *)skb->h.raw : NULL;
  1324. /* EX and TO_EX get a fresh packet, if needed */
  1325. if (truncate) {
  1326. if (pmr && pmr->ngrec &&
  1327. AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
  1328. if (skb)
  1329. mld_sendpack(skb);
  1330. skb = mld_newpack(dev, dev->mtu);
  1331. }
  1332. }
  1333. first = 1;
  1334. psf_prev = NULL;
  1335. for (psf=*psf_list; psf; psf=psf_next) {
  1336. struct in6_addr *psrc;
  1337. psf_next = psf->sf_next;
  1338. if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
  1339. psf_prev = psf;
  1340. continue;
  1341. }
  1342. /* clear marks on query responses */
  1343. if (isquery)
  1344. psf->sf_gsresp = 0;
  1345. if (AVAILABLE(skb) < sizeof(*psrc) +
  1346. first*sizeof(struct mld2_grec)) {
  1347. if (truncate && !first)
  1348. break; /* truncate these */
  1349. if (pgr)
  1350. pgr->grec_nsrcs = htons(scount);
  1351. if (skb)
  1352. mld_sendpack(skb);
  1353. skb = mld_newpack(dev, dev->mtu);
  1354. first = 1;
  1355. scount = 0;
  1356. }
  1357. if (first) {
  1358. skb = add_grhead(skb, pmc, type, &pgr);
  1359. first = 0;
  1360. }
  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. IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
  1530. snd_addr = addr;
  1531. if (type == ICMPV6_MGM_REDUCTION) {
  1532. snd_addr = &all_routers;
  1533. ipv6_addr_all_routers(&all_routers);
  1534. }
  1535. len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
  1536. payload_len = len + sizeof(ra);
  1537. full_len = sizeof(struct ipv6hdr) + payload_len;
  1538. skb = sock_alloc_send_skb(sk, LL_RESERVED_SPACE(dev) + full_len, 1, &err);
  1539. if (skb == NULL) {
  1540. IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
  1541. return;
  1542. }
  1543. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  1544. if (ipv6_get_lladdr(dev, &addr_buf)) {
  1545. /* <draft-ietf-magma-mld-source-05.txt>:
  1546. * use unspecified address as the source address
  1547. * when a valid link-local address is not available.
  1548. */
  1549. memset(&addr_buf, 0, sizeof(addr_buf));
  1550. }
  1551. ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len);
  1552. memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
  1553. hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
  1554. memset(hdr, 0, sizeof(struct icmp6hdr));
  1555. hdr->icmp6_type = type;
  1556. addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
  1557. ipv6_addr_copy(addrp, addr);
  1558. hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len,
  1559. IPPROTO_ICMPV6,
  1560. csum_partial((__u8 *) hdr, len, 0));
  1561. idev = in6_dev_get(skb->dev);
  1562. err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
  1563. mld_dev_queue_xmit);
  1564. if (!err) {
  1565. if (type == ICMPV6_MGM_REDUCTION)
  1566. ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBREDUCTIONS);
  1567. else
  1568. ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBRESPONSES);
  1569. ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
  1570. IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
  1571. } else
  1572. IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
  1573. if (likely(idev != NULL))
  1574. in6_dev_put(idev);
  1575. return;
  1576. }
  1577. static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
  1578. struct in6_addr *psfsrc)
  1579. {
  1580. struct ip6_sf_list *psf, *psf_prev;
  1581. int rv = 0;
  1582. psf_prev = NULL;
  1583. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  1584. if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
  1585. break;
  1586. psf_prev = psf;
  1587. }
  1588. if (!psf || psf->sf_count[sfmode] == 0) {
  1589. /* source filter not found, or count wrong => bug */
  1590. return -ESRCH;
  1591. }
  1592. psf->sf_count[sfmode]--;
  1593. if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
  1594. struct inet6_dev *idev = pmc->idev;
  1595. /* no more filters for this source */
  1596. if (psf_prev)
  1597. psf_prev->sf_next = psf->sf_next;
  1598. else
  1599. pmc->mca_sources = psf->sf_next;
  1600. if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
  1601. !MLD_V1_SEEN(idev)) {
  1602. psf->sf_crcount = idev->mc_qrv;
  1603. psf->sf_next = pmc->mca_tomb;
  1604. pmc->mca_tomb = psf;
  1605. rv = 1;
  1606. } else
  1607. kfree(psf);
  1608. }
  1609. return rv;
  1610. }
  1611. static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
  1612. int sfmode, int sfcount, struct in6_addr *psfsrc,
  1613. int delta)
  1614. {
  1615. struct ifmcaddr6 *pmc;
  1616. int changerec = 0;
  1617. int i, err;
  1618. if (!idev)
  1619. return -ENODEV;
  1620. read_lock_bh(&idev->lock);
  1621. for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
  1622. if (ipv6_addr_equal(pmca, &pmc->mca_addr))
  1623. break;
  1624. }
  1625. if (!pmc) {
  1626. /* MCA not found?? bug */
  1627. read_unlock_bh(&idev->lock);
  1628. return -ESRCH;
  1629. }
  1630. spin_lock_bh(&pmc->mca_lock);
  1631. sf_markstate(pmc);
  1632. if (!delta) {
  1633. if (!pmc->mca_sfcount[sfmode]) {
  1634. spin_unlock_bh(&pmc->mca_lock);
  1635. read_unlock_bh(&idev->lock);
  1636. return -EINVAL;
  1637. }
  1638. pmc->mca_sfcount[sfmode]--;
  1639. }
  1640. err = 0;
  1641. for (i=0; i<sfcount; i++) {
  1642. int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1643. changerec |= rv > 0;
  1644. if (!err && rv < 0)
  1645. err = rv;
  1646. }
  1647. if (pmc->mca_sfmode == MCAST_EXCLUDE &&
  1648. pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
  1649. pmc->mca_sfcount[MCAST_INCLUDE]) {
  1650. struct ip6_sf_list *psf;
  1651. /* filter mode change */
  1652. pmc->mca_sfmode = MCAST_INCLUDE;
  1653. pmc->mca_crcount = idev->mc_qrv;
  1654. idev->mc_ifc_count = pmc->mca_crcount;
  1655. for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
  1656. psf->sf_crcount = 0;
  1657. mld_ifc_event(pmc->idev);
  1658. } else if (sf_setstate(pmc) || changerec)
  1659. mld_ifc_event(pmc->idev);
  1660. spin_unlock_bh(&pmc->mca_lock);
  1661. read_unlock_bh(&idev->lock);
  1662. return err;
  1663. }
  1664. /*
  1665. * Add multicast single-source filter to the interface list
  1666. */
  1667. static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
  1668. struct in6_addr *psfsrc, int delta)
  1669. {
  1670. struct ip6_sf_list *psf, *psf_prev;
  1671. psf_prev = NULL;
  1672. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  1673. if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
  1674. break;
  1675. psf_prev = psf;
  1676. }
  1677. if (!psf) {
  1678. psf = (struct ip6_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
  1679. if (!psf)
  1680. return -ENOBUFS;
  1681. memset(psf, 0, sizeof(*psf));
  1682. psf->sf_addr = *psfsrc;
  1683. if (psf_prev) {
  1684. psf_prev->sf_next = psf;
  1685. } else
  1686. pmc->mca_sources = psf;
  1687. }
  1688. psf->sf_count[sfmode]++;
  1689. return 0;
  1690. }
  1691. static void sf_markstate(struct ifmcaddr6 *pmc)
  1692. {
  1693. struct ip6_sf_list *psf;
  1694. int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
  1695. for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
  1696. if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
  1697. psf->sf_oldin = mca_xcount ==
  1698. psf->sf_count[MCAST_EXCLUDE] &&
  1699. !psf->sf_count[MCAST_INCLUDE];
  1700. } else
  1701. psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
  1702. }
  1703. static int sf_setstate(struct ifmcaddr6 *pmc)
  1704. {
  1705. struct ip6_sf_list *psf;
  1706. int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
  1707. int qrv = pmc->idev->mc_qrv;
  1708. int new_in, rv;
  1709. rv = 0;
  1710. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  1711. if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
  1712. new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
  1713. !psf->sf_count[MCAST_INCLUDE];
  1714. } else
  1715. new_in = psf->sf_count[MCAST_INCLUDE] != 0;
  1716. if (new_in != psf->sf_oldin) {
  1717. psf->sf_crcount = qrv;
  1718. rv++;
  1719. }
  1720. }
  1721. return rv;
  1722. }
  1723. /*
  1724. * Add multicast source filter list to the interface list
  1725. */
  1726. static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
  1727. int sfmode, int sfcount, struct in6_addr *psfsrc,
  1728. int delta)
  1729. {
  1730. struct ifmcaddr6 *pmc;
  1731. int isexclude;
  1732. int i, err;
  1733. if (!idev)
  1734. return -ENODEV;
  1735. read_lock_bh(&idev->lock);
  1736. for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
  1737. if (ipv6_addr_equal(pmca, &pmc->mca_addr))
  1738. break;
  1739. }
  1740. if (!pmc) {
  1741. /* MCA not found?? bug */
  1742. read_unlock_bh(&idev->lock);
  1743. return -ESRCH;
  1744. }
  1745. spin_lock_bh(&pmc->mca_lock);
  1746. sf_markstate(pmc);
  1747. isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
  1748. if (!delta)
  1749. pmc->mca_sfcount[sfmode]++;
  1750. err = 0;
  1751. for (i=0; i<sfcount; i++) {
  1752. err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
  1753. if (err)
  1754. break;
  1755. }
  1756. if (err) {
  1757. int j;
  1758. if (!delta)
  1759. pmc->mca_sfcount[sfmode]--;
  1760. for (j=0; j<i; j++)
  1761. (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1762. } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
  1763. struct inet6_dev *idev = pmc->idev;
  1764. struct ip6_sf_list *psf;
  1765. /* filter mode change */
  1766. if (pmc->mca_sfcount[MCAST_EXCLUDE])
  1767. pmc->mca_sfmode = MCAST_EXCLUDE;
  1768. else if (pmc->mca_sfcount[MCAST_INCLUDE])
  1769. pmc->mca_sfmode = MCAST_INCLUDE;
  1770. /* else no filters; keep old mode for reports */
  1771. pmc->mca_crcount = idev->mc_qrv;
  1772. idev->mc_ifc_count = pmc->mca_crcount;
  1773. for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
  1774. psf->sf_crcount = 0;
  1775. mld_ifc_event(idev);
  1776. } else if (sf_setstate(pmc))
  1777. mld_ifc_event(idev);
  1778. spin_unlock_bh(&pmc->mca_lock);
  1779. read_unlock_bh(&idev->lock);
  1780. return err;
  1781. }
  1782. static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
  1783. {
  1784. struct ip6_sf_list *psf, *nextpsf;
  1785. for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
  1786. nextpsf = psf->sf_next;
  1787. kfree(psf);
  1788. }
  1789. pmc->mca_tomb = NULL;
  1790. for (psf=pmc->mca_sources; psf; psf=nextpsf) {
  1791. nextpsf = psf->sf_next;
  1792. kfree(psf);
  1793. }
  1794. pmc->mca_sources = NULL;
  1795. pmc->mca_sfmode = MCAST_EXCLUDE;
  1796. pmc->mca_sfcount[MCAST_INCLUDE] = 0;
  1797. pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
  1798. }
  1799. static void igmp6_join_group(struct ifmcaddr6 *ma)
  1800. {
  1801. unsigned long delay;
  1802. if (ma->mca_flags & MAF_NOREPORT)
  1803. return;
  1804. igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
  1805. delay = net_random() % IGMP6_UNSOLICITED_IVAL;
  1806. spin_lock_bh(&ma->mca_lock);
  1807. if (del_timer(&ma->mca_timer)) {
  1808. atomic_dec(&ma->mca_refcnt);
  1809. delay = ma->mca_timer.expires - jiffies;
  1810. }
  1811. if (!mod_timer(&ma->mca_timer, jiffies + delay))
  1812. atomic_inc(&ma->mca_refcnt);
  1813. ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
  1814. spin_unlock_bh(&ma->mca_lock);
  1815. }
  1816. static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
  1817. struct inet6_dev *idev)
  1818. {
  1819. int err;
  1820. /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
  1821. * so no other readers or writers of iml or its sflist
  1822. */
  1823. if (iml->sflist == 0) {
  1824. /* any-source empty exclude case */
  1825. return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
  1826. }
  1827. err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
  1828. iml->sflist->sl_count, iml->sflist->sl_addr, 0);
  1829. sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
  1830. iml->sflist = NULL;
  1831. return err;
  1832. }
  1833. static void igmp6_leave_group(struct ifmcaddr6 *ma)
  1834. {
  1835. if (MLD_V1_SEEN(ma->idev)) {
  1836. if (ma->mca_flags & MAF_LAST_REPORTER)
  1837. igmp6_send(&ma->mca_addr, ma->idev->dev,
  1838. ICMPV6_MGM_REDUCTION);
  1839. } else {
  1840. mld_add_delrec(ma->idev, ma);
  1841. mld_ifc_event(ma->idev);
  1842. }
  1843. }
  1844. static void mld_gq_timer_expire(unsigned long data)
  1845. {
  1846. struct inet6_dev *idev = (struct inet6_dev *)data;
  1847. idev->mc_gq_running = 0;
  1848. mld_send_report(idev, NULL);
  1849. __in6_dev_put(idev);
  1850. }
  1851. static void mld_ifc_timer_expire(unsigned long data)
  1852. {
  1853. struct inet6_dev *idev = (struct inet6_dev *)data;
  1854. mld_send_cr(idev);
  1855. if (idev->mc_ifc_count) {
  1856. idev->mc_ifc_count--;
  1857. if (idev->mc_ifc_count)
  1858. mld_ifc_start_timer(idev, idev->mc_maxdelay);
  1859. }
  1860. __in6_dev_put(idev);
  1861. }
  1862. static void mld_ifc_event(struct inet6_dev *idev)
  1863. {
  1864. if (MLD_V1_SEEN(idev))
  1865. return;
  1866. idev->mc_ifc_count = idev->mc_qrv;
  1867. mld_ifc_start_timer(idev, 1);
  1868. }
  1869. static void igmp6_timer_handler(unsigned long data)
  1870. {
  1871. struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
  1872. if (MLD_V1_SEEN(ma->idev))
  1873. igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
  1874. else
  1875. mld_send_report(ma->idev, ma);
  1876. spin_lock(&ma->mca_lock);
  1877. ma->mca_flags |= MAF_LAST_REPORTER;
  1878. ma->mca_flags &= ~MAF_TIMER_RUNNING;
  1879. spin_unlock(&ma->mca_lock);
  1880. ma_put(ma);
  1881. }
  1882. /* Device going down */
  1883. void ipv6_mc_down(struct inet6_dev *idev)
  1884. {
  1885. struct ifmcaddr6 *i;
  1886. /* Withdraw multicast list */
  1887. read_lock_bh(&idev->lock);
  1888. idev->mc_ifc_count = 0;
  1889. if (del_timer(&idev->mc_ifc_timer))
  1890. __in6_dev_put(idev);
  1891. idev->mc_gq_running = 0;
  1892. if (del_timer(&idev->mc_gq_timer))
  1893. __in6_dev_put(idev);
  1894. for (i = idev->mc_list; i; i=i->next)
  1895. igmp6_group_dropped(i);
  1896. read_unlock_bh(&idev->lock);
  1897. mld_clear_delrec(idev);
  1898. }
  1899. /* Device going up */
  1900. void ipv6_mc_up(struct inet6_dev *idev)
  1901. {
  1902. struct ifmcaddr6 *i;
  1903. /* Install multicast list, except for all-nodes (already installed) */
  1904. read_lock_bh(&idev->lock);
  1905. for (i = idev->mc_list; i; i=i->next)
  1906. igmp6_group_added(i);
  1907. read_unlock_bh(&idev->lock);
  1908. }
  1909. /* IPv6 device initialization. */
  1910. void ipv6_mc_init_dev(struct inet6_dev *idev)
  1911. {
  1912. struct in6_addr maddr;
  1913. write_lock_bh(&idev->lock);
  1914. rwlock_init(&idev->mc_lock);
  1915. idev->mc_gq_running = 0;
  1916. init_timer(&idev->mc_gq_timer);
  1917. idev->mc_gq_timer.data = (unsigned long) idev;
  1918. idev->mc_gq_timer.function = &mld_gq_timer_expire;
  1919. idev->mc_tomb = NULL;
  1920. idev->mc_ifc_count = 0;
  1921. init_timer(&idev->mc_ifc_timer);
  1922. idev->mc_ifc_timer.data = (unsigned long) idev;
  1923. idev->mc_ifc_timer.function = &mld_ifc_timer_expire;
  1924. idev->mc_qrv = MLD_QRV_DEFAULT;
  1925. idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
  1926. idev->mc_v1_seen = 0;
  1927. write_unlock_bh(&idev->lock);
  1928. /* Add all-nodes address. */
  1929. ipv6_addr_all_nodes(&maddr);
  1930. ipv6_dev_mc_inc(idev->dev, &maddr);
  1931. }
  1932. /*
  1933. * Device is about to be destroyed: clean up.
  1934. */
  1935. void ipv6_mc_destroy_dev(struct inet6_dev *idev)
  1936. {
  1937. struct ifmcaddr6 *i;
  1938. struct in6_addr maddr;
  1939. /* Deactivate timers */
  1940. ipv6_mc_down(idev);
  1941. /* Delete all-nodes address. */
  1942. ipv6_addr_all_nodes(&maddr);
  1943. /* We cannot call ipv6_dev_mc_dec() directly, our caller in
  1944. * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
  1945. * fail.
  1946. */
  1947. __ipv6_dev_mc_dec(idev, &maddr);
  1948. if (idev->cnf.forwarding) {
  1949. ipv6_addr_all_routers(&maddr);
  1950. __ipv6_dev_mc_dec(idev, &maddr);
  1951. }
  1952. write_lock_bh(&idev->lock);
  1953. while ((i = idev->mc_list) != NULL) {
  1954. idev->mc_list = i->next;
  1955. write_unlock_bh(&idev->lock);
  1956. igmp6_group_dropped(i);
  1957. ma_put(i);
  1958. write_lock_bh(&idev->lock);
  1959. }
  1960. write_unlock_bh(&idev->lock);
  1961. }
  1962. #ifdef CONFIG_PROC_FS
  1963. struct igmp6_mc_iter_state {
  1964. struct net_device *dev;
  1965. struct inet6_dev *idev;
  1966. };
  1967. #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
  1968. static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
  1969. {
  1970. struct ifmcaddr6 *im = NULL;
  1971. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  1972. for (state->dev = dev_base, state->idev = NULL;
  1973. state->dev;
  1974. state->dev = state->dev->next) {
  1975. struct inet6_dev *idev;
  1976. idev = in6_dev_get(state->dev);
  1977. if (!idev)
  1978. continue;
  1979. read_lock_bh(&idev->lock);
  1980. im = idev->mc_list;
  1981. if (im) {
  1982. state->idev = idev;
  1983. break;
  1984. }
  1985. read_unlock_bh(&idev->lock);
  1986. in6_dev_put(idev);
  1987. }
  1988. return im;
  1989. }
  1990. static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
  1991. {
  1992. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  1993. im = im->next;
  1994. while (!im) {
  1995. if (likely(state->idev != NULL)) {
  1996. read_unlock_bh(&state->idev->lock);
  1997. in6_dev_put(state->idev);
  1998. }
  1999. state->dev = state->dev->next;
  2000. if (!state->dev) {
  2001. state->idev = NULL;
  2002. break;
  2003. }
  2004. state->idev = in6_dev_get(state->dev);
  2005. if (!state->idev)
  2006. continue;
  2007. read_lock_bh(&state->idev->lock);
  2008. im = state->idev->mc_list;
  2009. }
  2010. return im;
  2011. }
  2012. static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
  2013. {
  2014. struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
  2015. if (im)
  2016. while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
  2017. --pos;
  2018. return pos ? NULL : im;
  2019. }
  2020. static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
  2021. {
  2022. read_lock(&dev_base_lock);
  2023. return igmp6_mc_get_idx(seq, *pos);
  2024. }
  2025. static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2026. {
  2027. struct ifmcaddr6 *im;
  2028. im = igmp6_mc_get_next(seq, v);
  2029. ++*pos;
  2030. return im;
  2031. }
  2032. static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
  2033. {
  2034. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  2035. if (likely(state->idev != NULL)) {
  2036. read_unlock_bh(&state->idev->lock);
  2037. in6_dev_put(state->idev);
  2038. state->idev = NULL;
  2039. }
  2040. state->dev = NULL;
  2041. read_unlock(&dev_base_lock);
  2042. }
  2043. static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
  2044. {
  2045. struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
  2046. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  2047. seq_printf(seq,
  2048. "%-4d %-15s %04x%04x%04x%04x%04x%04x%04x%04x %5d %08X %ld\n",
  2049. state->dev->ifindex, state->dev->name,
  2050. NIP6(im->mca_addr),
  2051. im->mca_users, im->mca_flags,
  2052. (im->mca_flags&MAF_TIMER_RUNNING) ?
  2053. jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
  2054. return 0;
  2055. }
  2056. static struct seq_operations igmp6_mc_seq_ops = {
  2057. .start = igmp6_mc_seq_start,
  2058. .next = igmp6_mc_seq_next,
  2059. .stop = igmp6_mc_seq_stop,
  2060. .show = igmp6_mc_seq_show,
  2061. };
  2062. static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
  2063. {
  2064. struct seq_file *seq;
  2065. int rc = -ENOMEM;
  2066. struct igmp6_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  2067. if (!s)
  2068. goto out;
  2069. rc = seq_open(file, &igmp6_mc_seq_ops);
  2070. if (rc)
  2071. goto out_kfree;
  2072. seq = file->private_data;
  2073. seq->private = s;
  2074. memset(s, 0, sizeof(*s));
  2075. out:
  2076. return rc;
  2077. out_kfree:
  2078. kfree(s);
  2079. goto out;
  2080. }
  2081. static struct file_operations igmp6_mc_seq_fops = {
  2082. .owner = THIS_MODULE,
  2083. .open = igmp6_mc_seq_open,
  2084. .read = seq_read,
  2085. .llseek = seq_lseek,
  2086. .release = seq_release_private,
  2087. };
  2088. struct igmp6_mcf_iter_state {
  2089. struct net_device *dev;
  2090. struct inet6_dev *idev;
  2091. struct ifmcaddr6 *im;
  2092. };
  2093. #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
  2094. static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
  2095. {
  2096. struct ip6_sf_list *psf = NULL;
  2097. struct ifmcaddr6 *im = NULL;
  2098. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2099. for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
  2100. state->dev;
  2101. state->dev = state->dev->next) {
  2102. struct inet6_dev *idev;
  2103. idev = in6_dev_get(state->dev);
  2104. if (unlikely(idev == NULL))
  2105. continue;
  2106. read_lock_bh(&idev->lock);
  2107. im = idev->mc_list;
  2108. if (likely(im != NULL)) {
  2109. spin_lock_bh(&im->mca_lock);
  2110. psf = im->mca_sources;
  2111. if (likely(psf != NULL)) {
  2112. state->im = im;
  2113. state->idev = idev;
  2114. break;
  2115. }
  2116. spin_unlock_bh(&im->mca_lock);
  2117. }
  2118. read_unlock_bh(&idev->lock);
  2119. in6_dev_put(idev);
  2120. }
  2121. return psf;
  2122. }
  2123. static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
  2124. {
  2125. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2126. psf = psf->sf_next;
  2127. while (!psf) {
  2128. spin_unlock_bh(&state->im->mca_lock);
  2129. state->im = state->im->next;
  2130. while (!state->im) {
  2131. if (likely(state->idev != NULL)) {
  2132. read_unlock_bh(&state->idev->lock);
  2133. in6_dev_put(state->idev);
  2134. }
  2135. state->dev = state->dev->next;
  2136. if (!state->dev) {
  2137. state->idev = NULL;
  2138. goto out;
  2139. }
  2140. state->idev = in6_dev_get(state->dev);
  2141. if (!state->idev)
  2142. continue;
  2143. read_lock_bh(&state->idev->lock);
  2144. state->im = state->idev->mc_list;
  2145. }
  2146. if (!state->im)
  2147. break;
  2148. spin_lock_bh(&state->im->mca_lock);
  2149. psf = state->im->mca_sources;
  2150. }
  2151. out:
  2152. return psf;
  2153. }
  2154. static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
  2155. {
  2156. struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
  2157. if (psf)
  2158. while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
  2159. --pos;
  2160. return pos ? NULL : psf;
  2161. }
  2162. static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
  2163. {
  2164. read_lock(&dev_base_lock);
  2165. return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2166. }
  2167. static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2168. {
  2169. struct ip6_sf_list *psf;
  2170. if (v == SEQ_START_TOKEN)
  2171. psf = igmp6_mcf_get_first(seq);
  2172. else
  2173. psf = igmp6_mcf_get_next(seq, v);
  2174. ++*pos;
  2175. return psf;
  2176. }
  2177. static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
  2178. {
  2179. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2180. if (likely(state->im != NULL)) {
  2181. spin_unlock_bh(&state->im->mca_lock);
  2182. state->im = NULL;
  2183. }
  2184. if (likely(state->idev != NULL)) {
  2185. read_unlock_bh(&state->idev->lock);
  2186. in6_dev_put(state->idev);
  2187. state->idev = NULL;
  2188. }
  2189. state->dev = NULL;
  2190. read_unlock(&dev_base_lock);
  2191. }
  2192. static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
  2193. {
  2194. struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
  2195. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2196. if (v == SEQ_START_TOKEN) {
  2197. seq_printf(seq,
  2198. "%3s %6s "
  2199. "%32s %32s %6s %6s\n", "Idx",
  2200. "Device", "Multicast Address",
  2201. "Source Address", "INC", "EXC");
  2202. } else {
  2203. seq_printf(seq,
  2204. "%3d %6.6s "
  2205. "%04x%04x%04x%04x%04x%04x%04x%04x "
  2206. "%04x%04x%04x%04x%04x%04x%04x%04x "
  2207. "%6lu %6lu\n",
  2208. state->dev->ifindex, state->dev->name,
  2209. NIP6(state->im->mca_addr),
  2210. NIP6(psf->sf_addr),
  2211. psf->sf_count[MCAST_INCLUDE],
  2212. psf->sf_count[MCAST_EXCLUDE]);
  2213. }
  2214. return 0;
  2215. }
  2216. static struct seq_operations igmp6_mcf_seq_ops = {
  2217. .start = igmp6_mcf_seq_start,
  2218. .next = igmp6_mcf_seq_next,
  2219. .stop = igmp6_mcf_seq_stop,
  2220. .show = igmp6_mcf_seq_show,
  2221. };
  2222. static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
  2223. {
  2224. struct seq_file *seq;
  2225. int rc = -ENOMEM;
  2226. struct igmp6_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  2227. if (!s)
  2228. goto out;
  2229. rc = seq_open(file, &igmp6_mcf_seq_ops);
  2230. if (rc)
  2231. goto out_kfree;
  2232. seq = file->private_data;
  2233. seq->private = s;
  2234. memset(s, 0, sizeof(*s));
  2235. out:
  2236. return rc;
  2237. out_kfree:
  2238. kfree(s);
  2239. goto out;
  2240. }
  2241. static struct file_operations igmp6_mcf_seq_fops = {
  2242. .owner = THIS_MODULE,
  2243. .open = igmp6_mcf_seq_open,
  2244. .read = seq_read,
  2245. .llseek = seq_lseek,
  2246. .release = seq_release_private,
  2247. };
  2248. #endif
  2249. int __init igmp6_init(struct net_proto_family *ops)
  2250. {
  2251. struct ipv6_pinfo *np;
  2252. struct sock *sk;
  2253. int err;
  2254. err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &igmp6_socket);
  2255. if (err < 0) {
  2256. printk(KERN_ERR
  2257. "Failed to initialize the IGMP6 control socket (err %d).\n",
  2258. err);
  2259. igmp6_socket = NULL; /* For safety. */
  2260. return err;
  2261. }
  2262. sk = igmp6_socket->sk;
  2263. sk->sk_allocation = GFP_ATOMIC;
  2264. sk->sk_prot->unhash(sk);
  2265. np = inet6_sk(sk);
  2266. np->hop_limit = 1;
  2267. #ifdef CONFIG_PROC_FS
  2268. proc_net_fops_create("igmp6", S_IRUGO, &igmp6_mc_seq_fops);
  2269. proc_net_fops_create("mcfilter6", S_IRUGO, &igmp6_mcf_seq_fops);
  2270. #endif
  2271. return 0;
  2272. }
  2273. void igmp6_cleanup(void)
  2274. {
  2275. sock_release(igmp6_socket);
  2276. igmp6_socket = NULL; /* for safety */
  2277. #ifdef CONFIG_PROC_FS
  2278. proc_net_remove("mcfilter6");
  2279. proc_net_remove("igmp6");
  2280. #endif
  2281. }