mcast.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717
  1. /*
  2. * Multicast support for IPv6
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. /* Changes:
  16. *
  17. * yoshfuji : fix format of router-alert option
  18. * YOSHIFUJI Hideaki @USAGI:
  19. * Fixed source address for MLD message based on
  20. * <draft-ietf-magma-mld-source-05.txt>.
  21. * YOSHIFUJI Hideaki @USAGI:
  22. * - Ignore Queries for invalid addresses.
  23. * - MLD for link-local addresses.
  24. * David L Stevens <dlstevens@us.ibm.com>:
  25. * - MLDv2 support
  26. */
  27. #include <linux/module.h>
  28. #include <linux/errno.h>
  29. #include <linux/types.h>
  30. #include <linux/string.h>
  31. #include <linux/socket.h>
  32. #include <linux/sockios.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/times.h>
  35. #include <linux/net.h>
  36. #include <linux/in.h>
  37. #include <linux/in6.h>
  38. #include <linux/netdevice.h>
  39. #include <linux/if_arp.h>
  40. #include <linux/route.h>
  41. #include <linux/init.h>
  42. #include <linux/proc_fs.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/netfilter.h>
  45. #include <linux/netfilter_ipv6.h>
  46. #include <net/net_namespace.h>
  47. #include <net/sock.h>
  48. #include <net/snmp.h>
  49. #include <net/ipv6.h>
  50. #include <net/protocol.h>
  51. #include <net/if_inet6.h>
  52. #include <net/ndisc.h>
  53. #include <net/addrconf.h>
  54. #include <net/ip6_route.h>
  55. #include <net/inet_common.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 void igmp6_join_group(struct ifmcaddr6 *ma);
  113. static void igmp6_leave_group(struct ifmcaddr6 *ma);
  114. static void igmp6_timer_handler(unsigned long data);
  115. static void mld_gq_timer_expire(unsigned long data);
  116. static void mld_ifc_timer_expire(unsigned long data);
  117. static void mld_ifc_event(struct inet6_dev *idev);
  118. static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
  119. static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *addr);
  120. static void mld_clear_delrec(struct inet6_dev *idev);
  121. static int sf_setstate(struct ifmcaddr6 *pmc);
  122. static void sf_markstate(struct ifmcaddr6 *pmc);
  123. static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
  124. static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
  125. int sfmode, int sfcount, struct in6_addr *psfsrc,
  126. int delta);
  127. static int ip6_mc_add_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_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
  131. struct inet6_dev *idev);
  132. #define IGMP6_UNSOLICITED_IVAL (10*HZ)
  133. #define MLD_QRV_DEFAULT 2
  134. #define MLD_V1_SEEN(idev) (dev_net((idev)->dev)->ipv6.devconf_all->force_mld_version == 1 || \
  135. (idev)->cnf.force_mld_version == 1 || \
  136. ((idev)->mc_v1_seen && \
  137. time_before(jiffies, (idev)->mc_v1_seen)))
  138. #define MLDV2_MASK(value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value))
  139. #define MLDV2_EXP(thresh, nbmant, nbexp, value) \
  140. ((value) < (thresh) ? (value) : \
  141. ((MLDV2_MASK(value, nbmant) | (1<<(nbmant))) << \
  142. (MLDV2_MASK((value) >> (nbmant), nbexp) + (nbexp))))
  143. #define MLDV2_MRC(value) MLDV2_EXP(0x8000, 12, 3, value)
  144. #define IPV6_MLD_MAX_MSF 64
  145. int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
  146. /*
  147. * socket join on multicast group
  148. */
  149. int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
  150. {
  151. struct net_device *dev = NULL;
  152. struct ipv6_mc_socklist *mc_lst;
  153. struct ipv6_pinfo *np = inet6_sk(sk);
  154. struct net *net = sock_net(sk);
  155. int err;
  156. if (!ipv6_addr_is_multicast(addr))
  157. return -EINVAL;
  158. read_lock_bh(&ipv6_sk_mc_lock);
  159. for (mc_lst=np->ipv6_mc_list; mc_lst; mc_lst=mc_lst->next) {
  160. if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
  161. ipv6_addr_equal(&mc_lst->addr, addr)) {
  162. read_unlock_bh(&ipv6_sk_mc_lock);
  163. return -EADDRINUSE;
  164. }
  165. }
  166. read_unlock_bh(&ipv6_sk_mc_lock);
  167. mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
  168. if (mc_lst == NULL)
  169. return -ENOMEM;
  170. mc_lst->next = NULL;
  171. ipv6_addr_copy(&mc_lst->addr, addr);
  172. if (ifindex == 0) {
  173. struct rt6_info *rt;
  174. rt = rt6_lookup(net, addr, NULL, 0, 0);
  175. if (rt) {
  176. dev = rt->rt6i_dev;
  177. dev_hold(dev);
  178. dst_release(&rt->u.dst);
  179. }
  180. } else
  181. dev = dev_get_by_index(net, ifindex);
  182. if (dev == NULL) {
  183. sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
  184. return -ENODEV;
  185. }
  186. mc_lst->ifindex = dev->ifindex;
  187. mc_lst->sfmode = MCAST_EXCLUDE;
  188. rwlock_init(&mc_lst->sflock);
  189. mc_lst->sflist = NULL;
  190. /*
  191. * now add/increase the group membership on the device
  192. */
  193. err = ipv6_dev_mc_inc(dev, addr);
  194. if (err) {
  195. sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
  196. dev_put(dev);
  197. return err;
  198. }
  199. write_lock_bh(&ipv6_sk_mc_lock);
  200. mc_lst->next = np->ipv6_mc_list;
  201. np->ipv6_mc_list = mc_lst;
  202. write_unlock_bh(&ipv6_sk_mc_lock);
  203. dev_put(dev);
  204. return 0;
  205. }
  206. /*
  207. * socket leave on multicast group
  208. */
  209. int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
  210. {
  211. struct ipv6_pinfo *np = inet6_sk(sk);
  212. struct ipv6_mc_socklist *mc_lst, **lnk;
  213. struct net *net = sock_net(sk);
  214. write_lock_bh(&ipv6_sk_mc_lock);
  215. for (lnk = &np->ipv6_mc_list; (mc_lst = *lnk) !=NULL ; lnk = &mc_lst->next) {
  216. if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
  217. ipv6_addr_equal(&mc_lst->addr, addr)) {
  218. struct net_device *dev;
  219. *lnk = mc_lst->next;
  220. write_unlock_bh(&ipv6_sk_mc_lock);
  221. dev = dev_get_by_index(net, mc_lst->ifindex);
  222. if (dev != 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 net *net,
  240. struct in6_addr *group,
  241. int ifindex)
  242. {
  243. struct net_device *dev = NULL;
  244. struct inet6_dev *idev = NULL;
  245. if (ifindex == 0) {
  246. struct rt6_info *rt;
  247. rt = rt6_lookup(net, group, NULL, 0, 0);
  248. if (rt) {
  249. dev = rt->rt6i_dev;
  250. dev_hold(dev);
  251. dst_release(&rt->u.dst);
  252. }
  253. } else
  254. dev = dev_get_by_index(net, ifindex);
  255. if (!dev)
  256. goto nodev;
  257. idev = in6_dev_get(dev);
  258. if (!idev)
  259. goto release;
  260. read_lock_bh(&idev->lock);
  261. if (idev->dead)
  262. goto unlock_release;
  263. return idev;
  264. unlock_release:
  265. read_unlock_bh(&idev->lock);
  266. in6_dev_put(idev);
  267. release:
  268. dev_put(dev);
  269. nodev:
  270. return NULL;
  271. }
  272. void ipv6_sock_mc_close(struct sock *sk)
  273. {
  274. struct ipv6_pinfo *np = inet6_sk(sk);
  275. struct ipv6_mc_socklist *mc_lst;
  276. struct net *net = sock_net(sk);
  277. write_lock_bh(&ipv6_sk_mc_lock);
  278. while ((mc_lst = np->ipv6_mc_list) != NULL) {
  279. struct net_device *dev;
  280. np->ipv6_mc_list = mc_lst->next;
  281. write_unlock_bh(&ipv6_sk_mc_lock);
  282. dev = dev_get_by_index(net, mc_lst->ifindex);
  283. if (dev) {
  284. struct inet6_dev *idev = in6_dev_get(dev);
  285. (void) ip6_mc_leave_src(sk, mc_lst, idev);
  286. if (idev) {
  287. __ipv6_dev_mc_dec(idev, &mc_lst->addr);
  288. in6_dev_put(idev);
  289. }
  290. dev_put(dev);
  291. } else
  292. (void) ip6_mc_leave_src(sk, mc_lst, NULL);
  293. sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
  294. write_lock_bh(&ipv6_sk_mc_lock);
  295. }
  296. write_unlock_bh(&ipv6_sk_mc_lock);
  297. }
  298. int ip6_mc_source(int add, int omode, struct sock *sk,
  299. struct group_source_req *pgsr)
  300. {
  301. struct in6_addr *source, *group;
  302. struct ipv6_mc_socklist *pmc;
  303. struct net_device *dev;
  304. struct inet6_dev *idev;
  305. struct ipv6_pinfo *inet6 = inet6_sk(sk);
  306. struct ip6_sf_socklist *psl;
  307. struct net *net = sock_net(sk);
  308. int i, j, rv;
  309. int leavegroup = 0;
  310. int pmclocked = 0;
  311. int err;
  312. source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
  313. group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
  314. if (!ipv6_addr_is_multicast(group))
  315. return -EINVAL;
  316. idev = ip6_mc_find_dev(net, group, pgsr->gsr_interface);
  317. if (!idev)
  318. return -ENODEV;
  319. dev = idev->dev;
  320. err = -EADDRNOTAVAIL;
  321. read_lock_bh(&ipv6_sk_mc_lock);
  322. for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
  323. if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
  324. continue;
  325. if (ipv6_addr_equal(&pmc->addr, group))
  326. break;
  327. }
  328. if (!pmc) { /* must have a prior join */
  329. err = -EINVAL;
  330. goto done;
  331. }
  332. /* if a source filter was set, must be the same mode as before */
  333. if (pmc->sflist) {
  334. if (pmc->sfmode != omode) {
  335. err = -EINVAL;
  336. goto done;
  337. }
  338. } else if (pmc->sfmode != omode) {
  339. /* allow mode switches for empty-set filters */
  340. ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
  341. ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
  342. pmc->sfmode = omode;
  343. }
  344. write_lock_bh(&pmc->sflock);
  345. pmclocked = 1;
  346. psl = pmc->sflist;
  347. if (!add) {
  348. if (!psl)
  349. goto done; /* err = -EADDRNOTAVAIL */
  350. rv = !0;
  351. for (i=0; i<psl->sl_count; i++) {
  352. rv = memcmp(&psl->sl_addr[i], source,
  353. sizeof(struct in6_addr));
  354. if (rv == 0)
  355. break;
  356. }
  357. if (rv) /* source not found */
  358. goto done; /* err = -EADDRNOTAVAIL */
  359. /* special case - (INCLUDE, empty) == LEAVE_GROUP */
  360. if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
  361. leavegroup = 1;
  362. goto done;
  363. }
  364. /* update the interface filter */
  365. ip6_mc_del_src(idev, group, omode, 1, source, 1);
  366. for (j=i+1; j<psl->sl_count; j++)
  367. psl->sl_addr[j-1] = psl->sl_addr[j];
  368. psl->sl_count--;
  369. err = 0;
  370. goto done;
  371. }
  372. /* else, add a new source to the filter */
  373. if (psl && psl->sl_count >= sysctl_mld_max_msf) {
  374. err = -ENOBUFS;
  375. goto done;
  376. }
  377. if (!psl || psl->sl_count == psl->sl_max) {
  378. struct ip6_sf_socklist *newpsl;
  379. int count = IP6_SFBLOCK;
  380. if (psl)
  381. count += psl->sl_max;
  382. newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
  383. if (!newpsl) {
  384. err = -ENOBUFS;
  385. goto done;
  386. }
  387. newpsl->sl_max = count;
  388. newpsl->sl_count = count - IP6_SFBLOCK;
  389. if (psl) {
  390. for (i=0; i<psl->sl_count; i++)
  391. newpsl->sl_addr[i] = psl->sl_addr[i];
  392. sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
  393. }
  394. pmc->sflist = psl = newpsl;
  395. }
  396. rv = 1; /* > 0 for insert logic below if sl_count is 0 */
  397. for (i=0; i<psl->sl_count; i++) {
  398. rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
  399. if (rv == 0)
  400. break;
  401. }
  402. if (rv == 0) /* address already there is an error */
  403. goto done;
  404. for (j=psl->sl_count-1; j>=i; j--)
  405. psl->sl_addr[j+1] = psl->sl_addr[j];
  406. psl->sl_addr[i] = *source;
  407. psl->sl_count++;
  408. err = 0;
  409. /* update the interface list */
  410. ip6_mc_add_src(idev, group, omode, 1, source, 1);
  411. done:
  412. if (pmclocked)
  413. write_unlock_bh(&pmc->sflock);
  414. read_unlock_bh(&ipv6_sk_mc_lock);
  415. read_unlock_bh(&idev->lock);
  416. in6_dev_put(idev);
  417. dev_put(dev);
  418. if (leavegroup)
  419. return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
  420. return err;
  421. }
  422. int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
  423. {
  424. struct in6_addr *group;
  425. struct ipv6_mc_socklist *pmc;
  426. struct net_device *dev;
  427. struct inet6_dev *idev;
  428. struct ipv6_pinfo *inet6 = inet6_sk(sk);
  429. struct ip6_sf_socklist *newpsl, *psl;
  430. struct net *net = sock_net(sk);
  431. int leavegroup = 0;
  432. int i, err;
  433. group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
  434. if (!ipv6_addr_is_multicast(group))
  435. return -EINVAL;
  436. if (gsf->gf_fmode != MCAST_INCLUDE &&
  437. gsf->gf_fmode != MCAST_EXCLUDE)
  438. return -EINVAL;
  439. idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
  440. if (!idev)
  441. return -ENODEV;
  442. dev = idev->dev;
  443. err = 0;
  444. read_lock_bh(&ipv6_sk_mc_lock);
  445. if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
  446. leavegroup = 1;
  447. goto done;
  448. }
  449. for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
  450. if (pmc->ifindex != gsf->gf_interface)
  451. continue;
  452. if (ipv6_addr_equal(&pmc->addr, group))
  453. break;
  454. }
  455. if (!pmc) { /* must have a prior join */
  456. err = -EINVAL;
  457. goto done;
  458. }
  459. if (gsf->gf_numsrc) {
  460. newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
  461. GFP_ATOMIC);
  462. if (!newpsl) {
  463. err = -ENOBUFS;
  464. goto done;
  465. }
  466. newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
  467. for (i=0; i<newpsl->sl_count; ++i) {
  468. struct sockaddr_in6 *psin6;
  469. psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
  470. newpsl->sl_addr[i] = psin6->sin6_addr;
  471. }
  472. err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
  473. newpsl->sl_count, newpsl->sl_addr, 0);
  474. if (err) {
  475. sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
  476. goto done;
  477. }
  478. } else {
  479. newpsl = NULL;
  480. (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
  481. }
  482. write_lock_bh(&pmc->sflock);
  483. psl = pmc->sflist;
  484. if (psl) {
  485. (void) ip6_mc_del_src(idev, group, pmc->sfmode,
  486. psl->sl_count, psl->sl_addr, 0);
  487. sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
  488. } else
  489. (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
  490. pmc->sflist = newpsl;
  491. pmc->sfmode = gsf->gf_fmode;
  492. write_unlock_bh(&pmc->sflock);
  493. err = 0;
  494. done:
  495. read_unlock_bh(&ipv6_sk_mc_lock);
  496. read_unlock_bh(&idev->lock);
  497. in6_dev_put(idev);
  498. dev_put(dev);
  499. if (leavegroup)
  500. err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
  501. return err;
  502. }
  503. int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
  504. struct group_filter __user *optval, int __user *optlen)
  505. {
  506. int err, i, count, copycount;
  507. struct in6_addr *group;
  508. struct ipv6_mc_socklist *pmc;
  509. struct inet6_dev *idev;
  510. struct net_device *dev;
  511. struct ipv6_pinfo *inet6 = inet6_sk(sk);
  512. struct ip6_sf_socklist *psl;
  513. struct net *net = sock_net(sk);
  514. group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
  515. if (!ipv6_addr_is_multicast(group))
  516. return -EINVAL;
  517. idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
  518. if (!idev)
  519. return -ENODEV;
  520. dev = idev->dev;
  521. err = -EADDRNOTAVAIL;
  522. /*
  523. * changes to the ipv6_mc_list require the socket lock and
  524. * a read lock on ip6_sk_mc_lock. We have the socket lock,
  525. * so reading the list is safe.
  526. */
  527. for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
  528. if (pmc->ifindex != gsf->gf_interface)
  529. continue;
  530. if (ipv6_addr_equal(group, &pmc->addr))
  531. break;
  532. }
  533. if (!pmc) /* must have a prior join */
  534. goto done;
  535. gsf->gf_fmode = pmc->sfmode;
  536. psl = pmc->sflist;
  537. count = psl ? psl->sl_count : 0;
  538. read_unlock_bh(&idev->lock);
  539. in6_dev_put(idev);
  540. dev_put(dev);
  541. copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
  542. gsf->gf_numsrc = count;
  543. if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
  544. copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
  545. return -EFAULT;
  546. }
  547. /* changes to psl require the socket lock, a read lock on
  548. * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
  549. * have the socket lock, so reading here is safe.
  550. */
  551. for (i=0; i<copycount; i++) {
  552. struct sockaddr_in6 *psin6;
  553. struct sockaddr_storage ss;
  554. psin6 = (struct sockaddr_in6 *)&ss;
  555. memset(&ss, 0, sizeof(ss));
  556. psin6->sin6_family = AF_INET6;
  557. psin6->sin6_addr = psl->sl_addr[i];
  558. if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
  559. return -EFAULT;
  560. }
  561. return 0;
  562. done:
  563. read_unlock_bh(&idev->lock);
  564. in6_dev_put(idev);
  565. dev_put(dev);
  566. return err;
  567. }
  568. int inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
  569. const struct in6_addr *src_addr)
  570. {
  571. struct ipv6_pinfo *np = inet6_sk(sk);
  572. struct ipv6_mc_socklist *mc;
  573. struct ip6_sf_socklist *psl;
  574. int rv = 1;
  575. read_lock(&ipv6_sk_mc_lock);
  576. for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
  577. if (ipv6_addr_equal(&mc->addr, mc_addr))
  578. break;
  579. }
  580. if (!mc) {
  581. read_unlock(&ipv6_sk_mc_lock);
  582. return 1;
  583. }
  584. read_lock(&mc->sflock);
  585. psl = mc->sflist;
  586. if (!psl) {
  587. rv = mc->sfmode == MCAST_EXCLUDE;
  588. } else {
  589. int i;
  590. for (i=0; i<psl->sl_count; i++) {
  591. if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
  592. break;
  593. }
  594. if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
  595. rv = 0;
  596. if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
  597. rv = 0;
  598. }
  599. read_unlock(&mc->sflock);
  600. read_unlock(&ipv6_sk_mc_lock);
  601. return rv;
  602. }
  603. static void ma_put(struct ifmcaddr6 *mc)
  604. {
  605. if (atomic_dec_and_test(&mc->mca_refcnt)) {
  606. in6_dev_put(mc->idev);
  607. kfree(mc);
  608. }
  609. }
  610. static void igmp6_group_added(struct ifmcaddr6 *mc)
  611. {
  612. struct net_device *dev = mc->idev->dev;
  613. char buf[MAX_ADDR_LEN];
  614. spin_lock_bh(&mc->mca_lock);
  615. if (!(mc->mca_flags&MAF_LOADED)) {
  616. mc->mca_flags |= MAF_LOADED;
  617. if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
  618. dev_mc_add(dev, buf, dev->addr_len, 0);
  619. }
  620. spin_unlock_bh(&mc->mca_lock);
  621. if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
  622. return;
  623. if (MLD_V1_SEEN(mc->idev)) {
  624. igmp6_join_group(mc);
  625. return;
  626. }
  627. /* else v2 */
  628. mc->mca_crcount = mc->idev->mc_qrv;
  629. mld_ifc_event(mc->idev);
  630. }
  631. static void igmp6_group_dropped(struct ifmcaddr6 *mc)
  632. {
  633. struct net_device *dev = mc->idev->dev;
  634. char buf[MAX_ADDR_LEN];
  635. spin_lock_bh(&mc->mca_lock);
  636. if (mc->mca_flags&MAF_LOADED) {
  637. mc->mca_flags &= ~MAF_LOADED;
  638. if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
  639. dev_mc_delete(dev, buf, dev->addr_len, 0);
  640. }
  641. if (mc->mca_flags & MAF_NOREPORT)
  642. goto done;
  643. spin_unlock_bh(&mc->mca_lock);
  644. if (!mc->idev->dead)
  645. igmp6_leave_group(mc);
  646. spin_lock_bh(&mc->mca_lock);
  647. if (del_timer(&mc->mca_timer))
  648. atomic_dec(&mc->mca_refcnt);
  649. done:
  650. ip6_mc_clear_src(mc);
  651. spin_unlock_bh(&mc->mca_lock);
  652. }
  653. /*
  654. * deleted ifmcaddr6 manipulation
  655. */
  656. static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
  657. {
  658. struct ifmcaddr6 *pmc;
  659. /* this is an "ifmcaddr6" for convenience; only the fields below
  660. * are actually used. In particular, the refcnt and users are not
  661. * used for management of the delete list. Using the same structure
  662. * for deleted items allows change reports to use common code with
  663. * non-deleted or query-response MCA's.
  664. */
  665. pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
  666. if (!pmc)
  667. return;
  668. spin_lock_bh(&im->mca_lock);
  669. spin_lock_init(&pmc->mca_lock);
  670. pmc->idev = im->idev;
  671. in6_dev_hold(idev);
  672. pmc->mca_addr = im->mca_addr;
  673. pmc->mca_crcount = idev->mc_qrv;
  674. pmc->mca_sfmode = im->mca_sfmode;
  675. if (pmc->mca_sfmode == MCAST_INCLUDE) {
  676. struct ip6_sf_list *psf;
  677. pmc->mca_tomb = im->mca_tomb;
  678. pmc->mca_sources = im->mca_sources;
  679. im->mca_tomb = im->mca_sources = NULL;
  680. for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
  681. psf->sf_crcount = pmc->mca_crcount;
  682. }
  683. spin_unlock_bh(&im->mca_lock);
  684. write_lock_bh(&idev->mc_lock);
  685. pmc->next = idev->mc_tomb;
  686. idev->mc_tomb = pmc;
  687. write_unlock_bh(&idev->mc_lock);
  688. }
  689. static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
  690. {
  691. struct ifmcaddr6 *pmc, *pmc_prev;
  692. struct ip6_sf_list *psf, *psf_next;
  693. write_lock_bh(&idev->mc_lock);
  694. pmc_prev = NULL;
  695. for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
  696. if (ipv6_addr_equal(&pmc->mca_addr, pmca))
  697. break;
  698. pmc_prev = pmc;
  699. }
  700. if (pmc) {
  701. if (pmc_prev)
  702. pmc_prev->next = pmc->next;
  703. else
  704. idev->mc_tomb = pmc->next;
  705. }
  706. write_unlock_bh(&idev->mc_lock);
  707. if (pmc) {
  708. for (psf=pmc->mca_tomb; psf; psf=psf_next) {
  709. psf_next = psf->sf_next;
  710. kfree(psf);
  711. }
  712. in6_dev_put(pmc->idev);
  713. kfree(pmc);
  714. }
  715. }
  716. static void mld_clear_delrec(struct inet6_dev *idev)
  717. {
  718. struct ifmcaddr6 *pmc, *nextpmc;
  719. write_lock_bh(&idev->mc_lock);
  720. pmc = idev->mc_tomb;
  721. idev->mc_tomb = NULL;
  722. write_unlock_bh(&idev->mc_lock);
  723. for (; pmc; pmc = nextpmc) {
  724. nextpmc = pmc->next;
  725. ip6_mc_clear_src(pmc);
  726. in6_dev_put(pmc->idev);
  727. kfree(pmc);
  728. }
  729. /* clear dead sources, too */
  730. read_lock_bh(&idev->lock);
  731. for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
  732. struct ip6_sf_list *psf, *psf_next;
  733. spin_lock_bh(&pmc->mca_lock);
  734. psf = pmc->mca_tomb;
  735. pmc->mca_tomb = NULL;
  736. spin_unlock_bh(&pmc->mca_lock);
  737. for (; psf; psf=psf_next) {
  738. psf_next = psf->sf_next;
  739. kfree(psf);
  740. }
  741. }
  742. read_unlock_bh(&idev->lock);
  743. }
  744. /*
  745. * device multicast group inc (add if not found)
  746. */
  747. int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
  748. {
  749. struct ifmcaddr6 *mc;
  750. struct inet6_dev *idev;
  751. idev = in6_dev_get(dev);
  752. if (idev == NULL)
  753. return -EINVAL;
  754. write_lock_bh(&idev->lock);
  755. if (idev->dead) {
  756. write_unlock_bh(&idev->lock);
  757. in6_dev_put(idev);
  758. return -ENODEV;
  759. }
  760. for (mc = idev->mc_list; mc; mc = mc->next) {
  761. if (ipv6_addr_equal(&mc->mca_addr, addr)) {
  762. mc->mca_users++;
  763. write_unlock_bh(&idev->lock);
  764. ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
  765. NULL, 0);
  766. in6_dev_put(idev);
  767. return 0;
  768. }
  769. }
  770. /*
  771. * not found: create a new one.
  772. */
  773. mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
  774. if (mc == NULL) {
  775. write_unlock_bh(&idev->lock);
  776. in6_dev_put(idev);
  777. return -ENOMEM;
  778. }
  779. setup_timer(&mc->mca_timer, igmp6_timer_handler, (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, const 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, const 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 = icmp6_hdr(skb);
  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, const struct in6_addr *group,
  860. const 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(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
  1002. len -= skb_network_header_len(skb);
  1003. /* Drop queries with not link local source */
  1004. if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
  1005. return -EINVAL;
  1006. idev = in6_dev_get(skb->dev);
  1007. if (idev == NULL)
  1008. return 0;
  1009. hdr = icmp6_hdr(skb);
  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_transport_header(skb);
  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_transport_header(skb);
  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 (!ipv6_addr_equal(group, &ma->mca_addr))
  1077. continue;
  1078. spin_lock_bh(&ma->mca_lock);
  1079. if (ma->mca_flags & MAF_TIMER_RUNNING) {
  1080. /* gsquery <- gsquery && mark */
  1081. if (!mark)
  1082. ma->mca_flags &= ~MAF_GSQUERY;
  1083. } else {
  1084. /* gsquery <- mark */
  1085. if (mark)
  1086. ma->mca_flags |= MAF_GSQUERY;
  1087. else
  1088. ma->mca_flags &= ~MAF_GSQUERY;
  1089. }
  1090. if (!(ma->mca_flags & MAF_GSQUERY) ||
  1091. mld_marksources(ma, ntohs(mlh2->nsrcs), mlh2->srcs))
  1092. igmp6_group_queried(ma, max_delay);
  1093. spin_unlock_bh(&ma->mca_lock);
  1094. break;
  1095. }
  1096. }
  1097. read_unlock_bh(&idev->lock);
  1098. in6_dev_put(idev);
  1099. return 0;
  1100. }
  1101. int igmp6_event_report(struct sk_buff *skb)
  1102. {
  1103. struct ifmcaddr6 *ma;
  1104. struct in6_addr *addrp;
  1105. struct inet6_dev *idev;
  1106. struct icmp6hdr *hdr;
  1107. int addr_type;
  1108. /* Our own report looped back. Ignore it. */
  1109. if (skb->pkt_type == PACKET_LOOPBACK)
  1110. return 0;
  1111. /* send our report if the MC router may not have heard this report */
  1112. if (skb->pkt_type != PACKET_MULTICAST &&
  1113. skb->pkt_type != PACKET_BROADCAST)
  1114. return 0;
  1115. if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
  1116. return -EINVAL;
  1117. hdr = icmp6_hdr(skb);
  1118. /* Drop reports with not link local source */
  1119. addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
  1120. if (addr_type != IPV6_ADDR_ANY &&
  1121. !(addr_type&IPV6_ADDR_LINKLOCAL))
  1122. return -EINVAL;
  1123. addrp = (struct in6_addr *) (hdr + 1);
  1124. idev = in6_dev_get(skb->dev);
  1125. if (idev == NULL)
  1126. return -ENODEV;
  1127. /*
  1128. * Cancel the timer for this group
  1129. */
  1130. read_lock_bh(&idev->lock);
  1131. for (ma = idev->mc_list; ma; ma=ma->next) {
  1132. if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
  1133. spin_lock(&ma->mca_lock);
  1134. if (del_timer(&ma->mca_timer))
  1135. atomic_dec(&ma->mca_refcnt);
  1136. ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
  1137. spin_unlock(&ma->mca_lock);
  1138. break;
  1139. }
  1140. }
  1141. read_unlock_bh(&idev->lock);
  1142. in6_dev_put(idev);
  1143. return 0;
  1144. }
  1145. static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
  1146. int gdeleted, int sdeleted)
  1147. {
  1148. switch (type) {
  1149. case MLD2_MODE_IS_INCLUDE:
  1150. case MLD2_MODE_IS_EXCLUDE:
  1151. if (gdeleted || sdeleted)
  1152. return 0;
  1153. if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
  1154. if (pmc->mca_sfmode == MCAST_INCLUDE)
  1155. return 1;
  1156. /* don't include if this source is excluded
  1157. * in all filters
  1158. */
  1159. if (psf->sf_count[MCAST_INCLUDE])
  1160. return type == MLD2_MODE_IS_INCLUDE;
  1161. return pmc->mca_sfcount[MCAST_EXCLUDE] ==
  1162. psf->sf_count[MCAST_EXCLUDE];
  1163. }
  1164. return 0;
  1165. case MLD2_CHANGE_TO_INCLUDE:
  1166. if (gdeleted || sdeleted)
  1167. return 0;
  1168. return psf->sf_count[MCAST_INCLUDE] != 0;
  1169. case MLD2_CHANGE_TO_EXCLUDE:
  1170. if (gdeleted || sdeleted)
  1171. return 0;
  1172. if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
  1173. psf->sf_count[MCAST_INCLUDE])
  1174. return 0;
  1175. return pmc->mca_sfcount[MCAST_EXCLUDE] ==
  1176. psf->sf_count[MCAST_EXCLUDE];
  1177. case MLD2_ALLOW_NEW_SOURCES:
  1178. if (gdeleted || !psf->sf_crcount)
  1179. return 0;
  1180. return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
  1181. case MLD2_BLOCK_OLD_SOURCES:
  1182. if (pmc->mca_sfmode == MCAST_INCLUDE)
  1183. return gdeleted || (psf->sf_crcount && sdeleted);
  1184. return psf->sf_crcount && !gdeleted && !sdeleted;
  1185. }
  1186. return 0;
  1187. }
  1188. static int
  1189. mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
  1190. {
  1191. struct ip6_sf_list *psf;
  1192. int scount = 0;
  1193. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  1194. if (!is_in(pmc, psf, type, gdeleted, sdeleted))
  1195. continue;
  1196. scount++;
  1197. }
  1198. return scount;
  1199. }
  1200. static struct sk_buff *mld_newpack(struct net_device *dev, int size)
  1201. {
  1202. struct net *net = dev_net(dev);
  1203. struct sock *sk = net->ipv6.igmp_sk;
  1204. struct sk_buff *skb;
  1205. struct mld2_report *pmr;
  1206. struct in6_addr addr_buf;
  1207. const struct in6_addr *saddr;
  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_ALLOCATED_SPACE(dev), 1, &err);
  1214. if (!skb)
  1215. return NULL;
  1216. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  1217. if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
  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. saddr = &in6addr_any;
  1223. } else
  1224. saddr = &addr_buf;
  1225. ip6_nd_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
  1226. memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
  1227. skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
  1228. skb_put(skb, sizeof(*pmr));
  1229. pmr = (struct mld2_report *)skb_transport_header(skb);
  1230. pmr->type = ICMPV6_MLD2_REPORT;
  1231. pmr->resv1 = 0;
  1232. pmr->csum = 0;
  1233. pmr->resv2 = 0;
  1234. pmr->ngrec = 0;
  1235. return skb;
  1236. }
  1237. static void mld_sendpack(struct sk_buff *skb)
  1238. {
  1239. struct ipv6hdr *pip6 = ipv6_hdr(skb);
  1240. struct mld2_report *pmr =
  1241. (struct mld2_report *)skb_transport_header(skb);
  1242. int payload_len, mldlen;
  1243. struct inet6_dev *idev = in6_dev_get(skb->dev);
  1244. struct net *net = dev_net(skb->dev);
  1245. int err;
  1246. struct flowi fl;
  1247. struct dst_entry *dst;
  1248. IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
  1249. payload_len = (skb->tail - skb->network_header) - sizeof(*pip6);
  1250. mldlen = skb->tail - skb->transport_header;
  1251. pip6->payload_len = htons(payload_len);
  1252. pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
  1253. IPPROTO_ICMPV6, csum_partial(skb_transport_header(skb),
  1254. mldlen, 0));
  1255. dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
  1256. if (!dst) {
  1257. err = -ENOMEM;
  1258. goto err_out;
  1259. }
  1260. icmpv6_flow_init(net->ipv6.igmp_sk, &fl, ICMPV6_MLD2_REPORT,
  1261. &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
  1262. skb->dev->ifindex);
  1263. err = xfrm_lookup(net, &dst, &fl, NULL, 0);
  1264. skb_dst_set(skb, dst);
  1265. if (err)
  1266. goto err_out;
  1267. payload_len = skb->len;
  1268. err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
  1269. dst_output);
  1270. out:
  1271. if (!err) {
  1272. ICMP6MSGOUT_INC_STATS_BH(net, idev, ICMPV6_MLD2_REPORT);
  1273. ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
  1274. IP6_UPD_PO_STATS_BH(net, idev, IPSTATS_MIB_OUTMCAST, payload_len);
  1275. } else
  1276. IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTDISCARDS);
  1277. if (likely(idev != NULL))
  1278. in6_dev_put(idev);
  1279. return;
  1280. err_out:
  1281. kfree_skb(skb);
  1282. goto out;
  1283. }
  1284. static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
  1285. {
  1286. return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
  1287. }
  1288. static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
  1289. int type, struct mld2_grec **ppgr)
  1290. {
  1291. struct net_device *dev = pmc->idev->dev;
  1292. struct mld2_report *pmr;
  1293. struct mld2_grec *pgr;
  1294. if (!skb)
  1295. skb = mld_newpack(dev, dev->mtu);
  1296. if (!skb)
  1297. return NULL;
  1298. pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
  1299. pgr->grec_type = type;
  1300. pgr->grec_auxwords = 0;
  1301. pgr->grec_nsrcs = 0;
  1302. pgr->grec_mca = pmc->mca_addr; /* structure copy */
  1303. pmr = (struct mld2_report *)skb_transport_header(skb);
  1304. pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
  1305. *ppgr = pgr;
  1306. return skb;
  1307. }
  1308. #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
  1309. skb_tailroom(skb)) : 0)
  1310. static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
  1311. int type, int gdeleted, int sdeleted)
  1312. {
  1313. struct net_device *dev = pmc->idev->dev;
  1314. struct mld2_report *pmr;
  1315. struct mld2_grec *pgr = NULL;
  1316. struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
  1317. int scount, stotal, first, isquery, truncate;
  1318. if (pmc->mca_flags & MAF_NOREPORT)
  1319. return skb;
  1320. isquery = type == MLD2_MODE_IS_INCLUDE ||
  1321. type == MLD2_MODE_IS_EXCLUDE;
  1322. truncate = type == MLD2_MODE_IS_EXCLUDE ||
  1323. type == MLD2_CHANGE_TO_EXCLUDE;
  1324. stotal = scount = 0;
  1325. psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
  1326. if (!*psf_list)
  1327. goto empty_source;
  1328. pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
  1329. /* EX and TO_EX get a fresh packet, if needed */
  1330. if (truncate) {
  1331. if (pmr && pmr->ngrec &&
  1332. AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
  1333. if (skb)
  1334. mld_sendpack(skb);
  1335. skb = mld_newpack(dev, dev->mtu);
  1336. }
  1337. }
  1338. first = 1;
  1339. psf_prev = NULL;
  1340. for (psf=*psf_list; psf; psf=psf_next) {
  1341. struct in6_addr *psrc;
  1342. psf_next = psf->sf_next;
  1343. if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
  1344. psf_prev = psf;
  1345. continue;
  1346. }
  1347. /* clear marks on query responses */
  1348. if (isquery)
  1349. psf->sf_gsresp = 0;
  1350. if (AVAILABLE(skb) < sizeof(*psrc) +
  1351. first*sizeof(struct mld2_grec)) {
  1352. if (truncate && !first)
  1353. break; /* truncate these */
  1354. if (pgr)
  1355. pgr->grec_nsrcs = htons(scount);
  1356. if (skb)
  1357. mld_sendpack(skb);
  1358. skb = mld_newpack(dev, dev->mtu);
  1359. first = 1;
  1360. scount = 0;
  1361. }
  1362. if (first) {
  1363. skb = add_grhead(skb, pmc, type, &pgr);
  1364. first = 0;
  1365. }
  1366. if (!skb)
  1367. return NULL;
  1368. psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
  1369. *psrc = psf->sf_addr;
  1370. scount++; stotal++;
  1371. if ((type == MLD2_ALLOW_NEW_SOURCES ||
  1372. type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
  1373. psf->sf_crcount--;
  1374. if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
  1375. if (psf_prev)
  1376. psf_prev->sf_next = psf->sf_next;
  1377. else
  1378. *psf_list = psf->sf_next;
  1379. kfree(psf);
  1380. continue;
  1381. }
  1382. }
  1383. psf_prev = psf;
  1384. }
  1385. empty_source:
  1386. if (!stotal) {
  1387. if (type == MLD2_ALLOW_NEW_SOURCES ||
  1388. type == MLD2_BLOCK_OLD_SOURCES)
  1389. return skb;
  1390. if (pmc->mca_crcount || isquery) {
  1391. /* make sure we have room for group header */
  1392. if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
  1393. mld_sendpack(skb);
  1394. skb = NULL; /* add_grhead will get a new one */
  1395. }
  1396. skb = add_grhead(skb, pmc, type, &pgr);
  1397. }
  1398. }
  1399. if (pgr)
  1400. pgr->grec_nsrcs = htons(scount);
  1401. if (isquery)
  1402. pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
  1403. return skb;
  1404. }
  1405. static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
  1406. {
  1407. struct sk_buff *skb = NULL;
  1408. int type;
  1409. if (!pmc) {
  1410. read_lock_bh(&idev->lock);
  1411. for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
  1412. if (pmc->mca_flags & MAF_NOREPORT)
  1413. continue;
  1414. spin_lock_bh(&pmc->mca_lock);
  1415. if (pmc->mca_sfcount[MCAST_EXCLUDE])
  1416. type = MLD2_MODE_IS_EXCLUDE;
  1417. else
  1418. type = MLD2_MODE_IS_INCLUDE;
  1419. skb = add_grec(skb, pmc, type, 0, 0);
  1420. spin_unlock_bh(&pmc->mca_lock);
  1421. }
  1422. read_unlock_bh(&idev->lock);
  1423. } else {
  1424. spin_lock_bh(&pmc->mca_lock);
  1425. if (pmc->mca_sfcount[MCAST_EXCLUDE])
  1426. type = MLD2_MODE_IS_EXCLUDE;
  1427. else
  1428. type = MLD2_MODE_IS_INCLUDE;
  1429. skb = add_grec(skb, pmc, type, 0, 0);
  1430. spin_unlock_bh(&pmc->mca_lock);
  1431. }
  1432. if (skb)
  1433. mld_sendpack(skb);
  1434. }
  1435. /*
  1436. * remove zero-count source records from a source filter list
  1437. */
  1438. static void mld_clear_zeros(struct ip6_sf_list **ppsf)
  1439. {
  1440. struct ip6_sf_list *psf_prev, *psf_next, *psf;
  1441. psf_prev = NULL;
  1442. for (psf=*ppsf; psf; psf = psf_next) {
  1443. psf_next = psf->sf_next;
  1444. if (psf->sf_crcount == 0) {
  1445. if (psf_prev)
  1446. psf_prev->sf_next = psf->sf_next;
  1447. else
  1448. *ppsf = psf->sf_next;
  1449. kfree(psf);
  1450. } else
  1451. psf_prev = psf;
  1452. }
  1453. }
  1454. static void mld_send_cr(struct inet6_dev *idev)
  1455. {
  1456. struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
  1457. struct sk_buff *skb = NULL;
  1458. int type, dtype;
  1459. read_lock_bh(&idev->lock);
  1460. write_lock_bh(&idev->mc_lock);
  1461. /* deleted MCA's */
  1462. pmc_prev = NULL;
  1463. for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
  1464. pmc_next = pmc->next;
  1465. if (pmc->mca_sfmode == MCAST_INCLUDE) {
  1466. type = MLD2_BLOCK_OLD_SOURCES;
  1467. dtype = MLD2_BLOCK_OLD_SOURCES;
  1468. skb = add_grec(skb, pmc, type, 1, 0);
  1469. skb = add_grec(skb, pmc, dtype, 1, 1);
  1470. }
  1471. if (pmc->mca_crcount) {
  1472. if (pmc->mca_sfmode == MCAST_EXCLUDE) {
  1473. type = MLD2_CHANGE_TO_INCLUDE;
  1474. skb = add_grec(skb, pmc, type, 1, 0);
  1475. }
  1476. pmc->mca_crcount--;
  1477. if (pmc->mca_crcount == 0) {
  1478. mld_clear_zeros(&pmc->mca_tomb);
  1479. mld_clear_zeros(&pmc->mca_sources);
  1480. }
  1481. }
  1482. if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
  1483. !pmc->mca_sources) {
  1484. if (pmc_prev)
  1485. pmc_prev->next = pmc_next;
  1486. else
  1487. idev->mc_tomb = pmc_next;
  1488. in6_dev_put(pmc->idev);
  1489. kfree(pmc);
  1490. } else
  1491. pmc_prev = pmc;
  1492. }
  1493. write_unlock_bh(&idev->mc_lock);
  1494. /* change recs */
  1495. for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
  1496. spin_lock_bh(&pmc->mca_lock);
  1497. if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
  1498. type = MLD2_BLOCK_OLD_SOURCES;
  1499. dtype = MLD2_ALLOW_NEW_SOURCES;
  1500. } else {
  1501. type = MLD2_ALLOW_NEW_SOURCES;
  1502. dtype = MLD2_BLOCK_OLD_SOURCES;
  1503. }
  1504. skb = add_grec(skb, pmc, type, 0, 0);
  1505. skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
  1506. /* filter mode changes */
  1507. if (pmc->mca_crcount) {
  1508. if (pmc->mca_sfmode == MCAST_EXCLUDE)
  1509. type = MLD2_CHANGE_TO_EXCLUDE;
  1510. else
  1511. type = MLD2_CHANGE_TO_INCLUDE;
  1512. skb = add_grec(skb, pmc, type, 0, 0);
  1513. pmc->mca_crcount--;
  1514. }
  1515. spin_unlock_bh(&pmc->mca_lock);
  1516. }
  1517. read_unlock_bh(&idev->lock);
  1518. if (!skb)
  1519. return;
  1520. (void) mld_sendpack(skb);
  1521. }
  1522. static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
  1523. {
  1524. struct net *net = dev_net(dev);
  1525. struct sock *sk = net->ipv6.igmp_sk;
  1526. struct inet6_dev *idev;
  1527. struct sk_buff *skb;
  1528. struct icmp6hdr *hdr;
  1529. const struct in6_addr *snd_addr, *saddr;
  1530. struct in6_addr *addrp;
  1531. struct in6_addr addr_buf;
  1532. int err, len, payload_len, full_len;
  1533. u8 ra[8] = { IPPROTO_ICMPV6, 0,
  1534. IPV6_TLV_ROUTERALERT, 2, 0, 0,
  1535. IPV6_TLV_PADN, 0 };
  1536. struct flowi fl;
  1537. struct dst_entry *dst;
  1538. if (type == ICMPV6_MGM_REDUCTION)
  1539. snd_addr = &in6addr_linklocal_allrouters;
  1540. else
  1541. snd_addr = addr;
  1542. len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
  1543. payload_len = len + sizeof(ra);
  1544. full_len = sizeof(struct ipv6hdr) + payload_len;
  1545. rcu_read_lock();
  1546. IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
  1547. IPSTATS_MIB_OUT, full_len);
  1548. rcu_read_unlock();
  1549. skb = sock_alloc_send_skb(sk, LL_ALLOCATED_SPACE(dev) + full_len, 1, &err);
  1550. if (skb == NULL) {
  1551. rcu_read_lock();
  1552. IP6_INC_STATS(net, __in6_dev_get(dev),
  1553. IPSTATS_MIB_OUTDISCARDS);
  1554. rcu_read_unlock();
  1555. return;
  1556. }
  1557. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  1558. if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
  1559. /* <draft-ietf-magma-mld-source-05.txt>:
  1560. * use unspecified address as the source address
  1561. * when a valid link-local address is not available.
  1562. */
  1563. saddr = &in6addr_any;
  1564. } else
  1565. saddr = &addr_buf;
  1566. ip6_nd_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
  1567. memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
  1568. hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
  1569. memset(hdr, 0, sizeof(struct icmp6hdr));
  1570. hdr->icmp6_type = type;
  1571. addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
  1572. ipv6_addr_copy(addrp, addr);
  1573. hdr->icmp6_cksum = csum_ipv6_magic(saddr, snd_addr, len,
  1574. IPPROTO_ICMPV6,
  1575. csum_partial(hdr, len, 0));
  1576. idev = in6_dev_get(skb->dev);
  1577. dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
  1578. if (!dst) {
  1579. err = -ENOMEM;
  1580. goto err_out;
  1581. }
  1582. icmpv6_flow_init(sk, &fl, type,
  1583. &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
  1584. skb->dev->ifindex);
  1585. err = xfrm_lookup(net, &dst, &fl, NULL, 0);
  1586. if (err)
  1587. goto err_out;
  1588. skb_dst_set(skb, dst);
  1589. err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
  1590. dst_output);
  1591. out:
  1592. if (!err) {
  1593. ICMP6MSGOUT_INC_STATS(net, idev, type);
  1594. ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
  1595. IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, full_len);
  1596. } else
  1597. IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
  1598. if (likely(idev != NULL))
  1599. in6_dev_put(idev);
  1600. return;
  1601. err_out:
  1602. kfree_skb(skb);
  1603. goto out;
  1604. }
  1605. static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
  1606. struct in6_addr *psfsrc)
  1607. {
  1608. struct ip6_sf_list *psf, *psf_prev;
  1609. int rv = 0;
  1610. psf_prev = NULL;
  1611. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  1612. if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
  1613. break;
  1614. psf_prev = psf;
  1615. }
  1616. if (!psf || psf->sf_count[sfmode] == 0) {
  1617. /* source filter not found, or count wrong => bug */
  1618. return -ESRCH;
  1619. }
  1620. psf->sf_count[sfmode]--;
  1621. if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
  1622. struct inet6_dev *idev = pmc->idev;
  1623. /* no more filters for this source */
  1624. if (psf_prev)
  1625. psf_prev->sf_next = psf->sf_next;
  1626. else
  1627. pmc->mca_sources = psf->sf_next;
  1628. if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
  1629. !MLD_V1_SEEN(idev)) {
  1630. psf->sf_crcount = idev->mc_qrv;
  1631. psf->sf_next = pmc->mca_tomb;
  1632. pmc->mca_tomb = psf;
  1633. rv = 1;
  1634. } else
  1635. kfree(psf);
  1636. }
  1637. return rv;
  1638. }
  1639. static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
  1640. int sfmode, int sfcount, struct in6_addr *psfsrc,
  1641. int delta)
  1642. {
  1643. struct ifmcaddr6 *pmc;
  1644. int changerec = 0;
  1645. int i, err;
  1646. if (!idev)
  1647. return -ENODEV;
  1648. read_lock_bh(&idev->lock);
  1649. for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
  1650. if (ipv6_addr_equal(pmca, &pmc->mca_addr))
  1651. break;
  1652. }
  1653. if (!pmc) {
  1654. /* MCA not found?? bug */
  1655. read_unlock_bh(&idev->lock);
  1656. return -ESRCH;
  1657. }
  1658. spin_lock_bh(&pmc->mca_lock);
  1659. sf_markstate(pmc);
  1660. if (!delta) {
  1661. if (!pmc->mca_sfcount[sfmode]) {
  1662. spin_unlock_bh(&pmc->mca_lock);
  1663. read_unlock_bh(&idev->lock);
  1664. return -EINVAL;
  1665. }
  1666. pmc->mca_sfcount[sfmode]--;
  1667. }
  1668. err = 0;
  1669. for (i=0; i<sfcount; i++) {
  1670. int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1671. changerec |= rv > 0;
  1672. if (!err && rv < 0)
  1673. err = rv;
  1674. }
  1675. if (pmc->mca_sfmode == MCAST_EXCLUDE &&
  1676. pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
  1677. pmc->mca_sfcount[MCAST_INCLUDE]) {
  1678. struct ip6_sf_list *psf;
  1679. /* filter mode change */
  1680. pmc->mca_sfmode = MCAST_INCLUDE;
  1681. pmc->mca_crcount = idev->mc_qrv;
  1682. idev->mc_ifc_count = pmc->mca_crcount;
  1683. for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
  1684. psf->sf_crcount = 0;
  1685. mld_ifc_event(pmc->idev);
  1686. } else if (sf_setstate(pmc) || changerec)
  1687. mld_ifc_event(pmc->idev);
  1688. spin_unlock_bh(&pmc->mca_lock);
  1689. read_unlock_bh(&idev->lock);
  1690. return err;
  1691. }
  1692. /*
  1693. * Add multicast single-source filter to the interface list
  1694. */
  1695. static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
  1696. struct in6_addr *psfsrc, int delta)
  1697. {
  1698. struct ip6_sf_list *psf, *psf_prev;
  1699. psf_prev = NULL;
  1700. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  1701. if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
  1702. break;
  1703. psf_prev = psf;
  1704. }
  1705. if (!psf) {
  1706. psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
  1707. if (!psf)
  1708. return -ENOBUFS;
  1709. psf->sf_addr = *psfsrc;
  1710. if (psf_prev) {
  1711. psf_prev->sf_next = psf;
  1712. } else
  1713. pmc->mca_sources = psf;
  1714. }
  1715. psf->sf_count[sfmode]++;
  1716. return 0;
  1717. }
  1718. static void sf_markstate(struct ifmcaddr6 *pmc)
  1719. {
  1720. struct ip6_sf_list *psf;
  1721. int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
  1722. for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
  1723. if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
  1724. psf->sf_oldin = mca_xcount ==
  1725. psf->sf_count[MCAST_EXCLUDE] &&
  1726. !psf->sf_count[MCAST_INCLUDE];
  1727. } else
  1728. psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
  1729. }
  1730. static int sf_setstate(struct ifmcaddr6 *pmc)
  1731. {
  1732. struct ip6_sf_list *psf, *dpsf;
  1733. int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
  1734. int qrv = pmc->idev->mc_qrv;
  1735. int new_in, rv;
  1736. rv = 0;
  1737. for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
  1738. if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
  1739. new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
  1740. !psf->sf_count[MCAST_INCLUDE];
  1741. } else
  1742. new_in = psf->sf_count[MCAST_INCLUDE] != 0;
  1743. if (new_in) {
  1744. if (!psf->sf_oldin) {
  1745. struct ip6_sf_list *prev = NULL;
  1746. for (dpsf=pmc->mca_tomb; dpsf;
  1747. dpsf=dpsf->sf_next) {
  1748. if (ipv6_addr_equal(&dpsf->sf_addr,
  1749. &psf->sf_addr))
  1750. break;
  1751. prev = dpsf;
  1752. }
  1753. if (dpsf) {
  1754. if (prev)
  1755. prev->sf_next = dpsf->sf_next;
  1756. else
  1757. pmc->mca_tomb = dpsf->sf_next;
  1758. kfree(dpsf);
  1759. }
  1760. psf->sf_crcount = qrv;
  1761. rv++;
  1762. }
  1763. } else if (psf->sf_oldin) {
  1764. psf->sf_crcount = 0;
  1765. /*
  1766. * add or update "delete" records if an active filter
  1767. * is now inactive
  1768. */
  1769. for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
  1770. if (ipv6_addr_equal(&dpsf->sf_addr,
  1771. &psf->sf_addr))
  1772. break;
  1773. if (!dpsf) {
  1774. dpsf = (struct ip6_sf_list *)
  1775. kmalloc(sizeof(*dpsf), GFP_ATOMIC);
  1776. if (!dpsf)
  1777. continue;
  1778. *dpsf = *psf;
  1779. /* pmc->mca_lock held by callers */
  1780. dpsf->sf_next = pmc->mca_tomb;
  1781. pmc->mca_tomb = dpsf;
  1782. }
  1783. dpsf->sf_crcount = qrv;
  1784. rv++;
  1785. }
  1786. }
  1787. return rv;
  1788. }
  1789. /*
  1790. * Add multicast source filter list to the interface list
  1791. */
  1792. static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
  1793. int sfmode, int sfcount, struct in6_addr *psfsrc,
  1794. int delta)
  1795. {
  1796. struct ifmcaddr6 *pmc;
  1797. int isexclude;
  1798. int i, err;
  1799. if (!idev)
  1800. return -ENODEV;
  1801. read_lock_bh(&idev->lock);
  1802. for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
  1803. if (ipv6_addr_equal(pmca, &pmc->mca_addr))
  1804. break;
  1805. }
  1806. if (!pmc) {
  1807. /* MCA not found?? bug */
  1808. read_unlock_bh(&idev->lock);
  1809. return -ESRCH;
  1810. }
  1811. spin_lock_bh(&pmc->mca_lock);
  1812. sf_markstate(pmc);
  1813. isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
  1814. if (!delta)
  1815. pmc->mca_sfcount[sfmode]++;
  1816. err = 0;
  1817. for (i=0; i<sfcount; i++) {
  1818. err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
  1819. if (err)
  1820. break;
  1821. }
  1822. if (err) {
  1823. int j;
  1824. if (!delta)
  1825. pmc->mca_sfcount[sfmode]--;
  1826. for (j=0; j<i; j++)
  1827. (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
  1828. } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
  1829. struct inet6_dev *idev = pmc->idev;
  1830. struct ip6_sf_list *psf;
  1831. /* filter mode change */
  1832. if (pmc->mca_sfcount[MCAST_EXCLUDE])
  1833. pmc->mca_sfmode = MCAST_EXCLUDE;
  1834. else if (pmc->mca_sfcount[MCAST_INCLUDE])
  1835. pmc->mca_sfmode = MCAST_INCLUDE;
  1836. /* else no filters; keep old mode for reports */
  1837. pmc->mca_crcount = idev->mc_qrv;
  1838. idev->mc_ifc_count = pmc->mca_crcount;
  1839. for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
  1840. psf->sf_crcount = 0;
  1841. mld_ifc_event(idev);
  1842. } else if (sf_setstate(pmc))
  1843. mld_ifc_event(idev);
  1844. spin_unlock_bh(&pmc->mca_lock);
  1845. read_unlock_bh(&idev->lock);
  1846. return err;
  1847. }
  1848. static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
  1849. {
  1850. struct ip6_sf_list *psf, *nextpsf;
  1851. for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
  1852. nextpsf = psf->sf_next;
  1853. kfree(psf);
  1854. }
  1855. pmc->mca_tomb = NULL;
  1856. for (psf=pmc->mca_sources; psf; psf=nextpsf) {
  1857. nextpsf = psf->sf_next;
  1858. kfree(psf);
  1859. }
  1860. pmc->mca_sources = NULL;
  1861. pmc->mca_sfmode = MCAST_EXCLUDE;
  1862. pmc->mca_sfcount[MCAST_INCLUDE] = 0;
  1863. pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
  1864. }
  1865. static void igmp6_join_group(struct ifmcaddr6 *ma)
  1866. {
  1867. unsigned long delay;
  1868. if (ma->mca_flags & MAF_NOREPORT)
  1869. return;
  1870. igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
  1871. delay = net_random() % IGMP6_UNSOLICITED_IVAL;
  1872. spin_lock_bh(&ma->mca_lock);
  1873. if (del_timer(&ma->mca_timer)) {
  1874. atomic_dec(&ma->mca_refcnt);
  1875. delay = ma->mca_timer.expires - jiffies;
  1876. }
  1877. if (!mod_timer(&ma->mca_timer, jiffies + delay))
  1878. atomic_inc(&ma->mca_refcnt);
  1879. ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
  1880. spin_unlock_bh(&ma->mca_lock);
  1881. }
  1882. static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
  1883. struct inet6_dev *idev)
  1884. {
  1885. int err;
  1886. /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
  1887. * so no other readers or writers of iml or its sflist
  1888. */
  1889. if (!iml->sflist) {
  1890. /* any-source empty exclude case */
  1891. return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
  1892. }
  1893. err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
  1894. iml->sflist->sl_count, iml->sflist->sl_addr, 0);
  1895. sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
  1896. iml->sflist = NULL;
  1897. return err;
  1898. }
  1899. static void igmp6_leave_group(struct ifmcaddr6 *ma)
  1900. {
  1901. if (MLD_V1_SEEN(ma->idev)) {
  1902. if (ma->mca_flags & MAF_LAST_REPORTER)
  1903. igmp6_send(&ma->mca_addr, ma->idev->dev,
  1904. ICMPV6_MGM_REDUCTION);
  1905. } else {
  1906. mld_add_delrec(ma->idev, ma);
  1907. mld_ifc_event(ma->idev);
  1908. }
  1909. }
  1910. static void mld_gq_timer_expire(unsigned long data)
  1911. {
  1912. struct inet6_dev *idev = (struct inet6_dev *)data;
  1913. idev->mc_gq_running = 0;
  1914. mld_send_report(idev, NULL);
  1915. __in6_dev_put(idev);
  1916. }
  1917. static void mld_ifc_timer_expire(unsigned long data)
  1918. {
  1919. struct inet6_dev *idev = (struct inet6_dev *)data;
  1920. mld_send_cr(idev);
  1921. if (idev->mc_ifc_count) {
  1922. idev->mc_ifc_count--;
  1923. if (idev->mc_ifc_count)
  1924. mld_ifc_start_timer(idev, idev->mc_maxdelay);
  1925. }
  1926. __in6_dev_put(idev);
  1927. }
  1928. static void mld_ifc_event(struct inet6_dev *idev)
  1929. {
  1930. if (MLD_V1_SEEN(idev))
  1931. return;
  1932. idev->mc_ifc_count = idev->mc_qrv;
  1933. mld_ifc_start_timer(idev, 1);
  1934. }
  1935. static void igmp6_timer_handler(unsigned long data)
  1936. {
  1937. struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
  1938. if (MLD_V1_SEEN(ma->idev))
  1939. igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
  1940. else
  1941. mld_send_report(ma->idev, ma);
  1942. spin_lock(&ma->mca_lock);
  1943. ma->mca_flags |= MAF_LAST_REPORTER;
  1944. ma->mca_flags &= ~MAF_TIMER_RUNNING;
  1945. spin_unlock(&ma->mca_lock);
  1946. ma_put(ma);
  1947. }
  1948. /* Device going down */
  1949. void ipv6_mc_down(struct inet6_dev *idev)
  1950. {
  1951. struct ifmcaddr6 *i;
  1952. /* Withdraw multicast list */
  1953. read_lock_bh(&idev->lock);
  1954. idev->mc_ifc_count = 0;
  1955. if (del_timer(&idev->mc_ifc_timer))
  1956. __in6_dev_put(idev);
  1957. idev->mc_gq_running = 0;
  1958. if (del_timer(&idev->mc_gq_timer))
  1959. __in6_dev_put(idev);
  1960. for (i = idev->mc_list; i; i=i->next)
  1961. igmp6_group_dropped(i);
  1962. read_unlock_bh(&idev->lock);
  1963. mld_clear_delrec(idev);
  1964. }
  1965. /* Device going up */
  1966. void ipv6_mc_up(struct inet6_dev *idev)
  1967. {
  1968. struct ifmcaddr6 *i;
  1969. /* Install multicast list, except for all-nodes (already installed) */
  1970. read_lock_bh(&idev->lock);
  1971. for (i = idev->mc_list; i; i=i->next)
  1972. igmp6_group_added(i);
  1973. read_unlock_bh(&idev->lock);
  1974. }
  1975. /* IPv6 device initialization. */
  1976. void ipv6_mc_init_dev(struct inet6_dev *idev)
  1977. {
  1978. write_lock_bh(&idev->lock);
  1979. rwlock_init(&idev->mc_lock);
  1980. idev->mc_gq_running = 0;
  1981. setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
  1982. (unsigned long)idev);
  1983. idev->mc_tomb = NULL;
  1984. idev->mc_ifc_count = 0;
  1985. setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
  1986. (unsigned long)idev);
  1987. idev->mc_qrv = MLD_QRV_DEFAULT;
  1988. idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
  1989. idev->mc_v1_seen = 0;
  1990. write_unlock_bh(&idev->lock);
  1991. }
  1992. /*
  1993. * Device is about to be destroyed: clean up.
  1994. */
  1995. void ipv6_mc_destroy_dev(struct inet6_dev *idev)
  1996. {
  1997. struct ifmcaddr6 *i;
  1998. /* Deactivate timers */
  1999. ipv6_mc_down(idev);
  2000. /* Delete all-nodes address. */
  2001. /* We cannot call ipv6_dev_mc_dec() directly, our caller in
  2002. * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
  2003. * fail.
  2004. */
  2005. __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
  2006. if (idev->cnf.forwarding)
  2007. __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
  2008. write_lock_bh(&idev->lock);
  2009. while ((i = idev->mc_list) != NULL) {
  2010. idev->mc_list = i->next;
  2011. write_unlock_bh(&idev->lock);
  2012. igmp6_group_dropped(i);
  2013. ma_put(i);
  2014. write_lock_bh(&idev->lock);
  2015. }
  2016. write_unlock_bh(&idev->lock);
  2017. }
  2018. #ifdef CONFIG_PROC_FS
  2019. struct igmp6_mc_iter_state {
  2020. struct seq_net_private p;
  2021. struct net_device *dev;
  2022. struct inet6_dev *idev;
  2023. };
  2024. #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
  2025. static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
  2026. {
  2027. struct ifmcaddr6 *im = NULL;
  2028. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  2029. struct net *net = seq_file_net(seq);
  2030. state->idev = NULL;
  2031. for_each_netdev(net, state->dev) {
  2032. struct inet6_dev *idev;
  2033. idev = in6_dev_get(state->dev);
  2034. if (!idev)
  2035. continue;
  2036. read_lock_bh(&idev->lock);
  2037. im = idev->mc_list;
  2038. if (im) {
  2039. state->idev = idev;
  2040. break;
  2041. }
  2042. read_unlock_bh(&idev->lock);
  2043. in6_dev_put(idev);
  2044. }
  2045. return im;
  2046. }
  2047. static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
  2048. {
  2049. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  2050. im = im->next;
  2051. while (!im) {
  2052. if (likely(state->idev != NULL)) {
  2053. read_unlock_bh(&state->idev->lock);
  2054. in6_dev_put(state->idev);
  2055. }
  2056. state->dev = next_net_device(state->dev);
  2057. if (!state->dev) {
  2058. state->idev = NULL;
  2059. break;
  2060. }
  2061. state->idev = in6_dev_get(state->dev);
  2062. if (!state->idev)
  2063. continue;
  2064. read_lock_bh(&state->idev->lock);
  2065. im = state->idev->mc_list;
  2066. }
  2067. return im;
  2068. }
  2069. static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
  2070. {
  2071. struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
  2072. if (im)
  2073. while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
  2074. --pos;
  2075. return pos ? NULL : im;
  2076. }
  2077. static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
  2078. __acquires(dev_base_lock)
  2079. {
  2080. read_lock(&dev_base_lock);
  2081. return igmp6_mc_get_idx(seq, *pos);
  2082. }
  2083. static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2084. {
  2085. struct ifmcaddr6 *im;
  2086. im = igmp6_mc_get_next(seq, v);
  2087. ++*pos;
  2088. return im;
  2089. }
  2090. static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
  2091. __releases(dev_base_lock)
  2092. {
  2093. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  2094. if (likely(state->idev != NULL)) {
  2095. read_unlock_bh(&state->idev->lock);
  2096. in6_dev_put(state->idev);
  2097. state->idev = NULL;
  2098. }
  2099. state->dev = NULL;
  2100. read_unlock(&dev_base_lock);
  2101. }
  2102. static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
  2103. {
  2104. struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
  2105. struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
  2106. seq_printf(seq,
  2107. "%-4d %-15s %pi6 %5d %08X %ld\n",
  2108. state->dev->ifindex, state->dev->name,
  2109. &im->mca_addr,
  2110. im->mca_users, im->mca_flags,
  2111. (im->mca_flags&MAF_TIMER_RUNNING) ?
  2112. jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
  2113. return 0;
  2114. }
  2115. static const struct seq_operations igmp6_mc_seq_ops = {
  2116. .start = igmp6_mc_seq_start,
  2117. .next = igmp6_mc_seq_next,
  2118. .stop = igmp6_mc_seq_stop,
  2119. .show = igmp6_mc_seq_show,
  2120. };
  2121. static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
  2122. {
  2123. return seq_open_net(inode, file, &igmp6_mc_seq_ops,
  2124. sizeof(struct igmp6_mc_iter_state));
  2125. }
  2126. static const struct file_operations igmp6_mc_seq_fops = {
  2127. .owner = THIS_MODULE,
  2128. .open = igmp6_mc_seq_open,
  2129. .read = seq_read,
  2130. .llseek = seq_lseek,
  2131. .release = seq_release_net,
  2132. };
  2133. struct igmp6_mcf_iter_state {
  2134. struct seq_net_private p;
  2135. struct net_device *dev;
  2136. struct inet6_dev *idev;
  2137. struct ifmcaddr6 *im;
  2138. };
  2139. #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
  2140. static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
  2141. {
  2142. struct ip6_sf_list *psf = NULL;
  2143. struct ifmcaddr6 *im = NULL;
  2144. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2145. struct net *net = seq_file_net(seq);
  2146. state->idev = NULL;
  2147. state->im = NULL;
  2148. for_each_netdev(net, state->dev) {
  2149. struct inet6_dev *idev;
  2150. idev = in6_dev_get(state->dev);
  2151. if (unlikely(idev == NULL))
  2152. continue;
  2153. read_lock_bh(&idev->lock);
  2154. im = idev->mc_list;
  2155. if (likely(im != NULL)) {
  2156. spin_lock_bh(&im->mca_lock);
  2157. psf = im->mca_sources;
  2158. if (likely(psf != NULL)) {
  2159. state->im = im;
  2160. state->idev = idev;
  2161. break;
  2162. }
  2163. spin_unlock_bh(&im->mca_lock);
  2164. }
  2165. read_unlock_bh(&idev->lock);
  2166. in6_dev_put(idev);
  2167. }
  2168. return psf;
  2169. }
  2170. static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
  2171. {
  2172. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2173. psf = psf->sf_next;
  2174. while (!psf) {
  2175. spin_unlock_bh(&state->im->mca_lock);
  2176. state->im = state->im->next;
  2177. while (!state->im) {
  2178. if (likely(state->idev != NULL)) {
  2179. read_unlock_bh(&state->idev->lock);
  2180. in6_dev_put(state->idev);
  2181. }
  2182. state->dev = next_net_device(state->dev);
  2183. if (!state->dev) {
  2184. state->idev = NULL;
  2185. goto out;
  2186. }
  2187. state->idev = in6_dev_get(state->dev);
  2188. if (!state->idev)
  2189. continue;
  2190. read_lock_bh(&state->idev->lock);
  2191. state->im = state->idev->mc_list;
  2192. }
  2193. if (!state->im)
  2194. break;
  2195. spin_lock_bh(&state->im->mca_lock);
  2196. psf = state->im->mca_sources;
  2197. }
  2198. out:
  2199. return psf;
  2200. }
  2201. static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
  2202. {
  2203. struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
  2204. if (psf)
  2205. while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
  2206. --pos;
  2207. return pos ? NULL : psf;
  2208. }
  2209. static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
  2210. __acquires(dev_base_lock)
  2211. {
  2212. read_lock(&dev_base_lock);
  2213. return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2214. }
  2215. static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2216. {
  2217. struct ip6_sf_list *psf;
  2218. if (v == SEQ_START_TOKEN)
  2219. psf = igmp6_mcf_get_first(seq);
  2220. else
  2221. psf = igmp6_mcf_get_next(seq, v);
  2222. ++*pos;
  2223. return psf;
  2224. }
  2225. static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
  2226. __releases(dev_base_lock)
  2227. {
  2228. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2229. if (likely(state->im != NULL)) {
  2230. spin_unlock_bh(&state->im->mca_lock);
  2231. state->im = NULL;
  2232. }
  2233. if (likely(state->idev != NULL)) {
  2234. read_unlock_bh(&state->idev->lock);
  2235. in6_dev_put(state->idev);
  2236. state->idev = NULL;
  2237. }
  2238. state->dev = NULL;
  2239. read_unlock(&dev_base_lock);
  2240. }
  2241. static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
  2242. {
  2243. struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
  2244. struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
  2245. if (v == SEQ_START_TOKEN) {
  2246. seq_printf(seq,
  2247. "%3s %6s "
  2248. "%32s %32s %6s %6s\n", "Idx",
  2249. "Device", "Multicast Address",
  2250. "Source Address", "INC", "EXC");
  2251. } else {
  2252. seq_printf(seq,
  2253. "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
  2254. state->dev->ifindex, state->dev->name,
  2255. &state->im->mca_addr,
  2256. &psf->sf_addr,
  2257. psf->sf_count[MCAST_INCLUDE],
  2258. psf->sf_count[MCAST_EXCLUDE]);
  2259. }
  2260. return 0;
  2261. }
  2262. static const struct seq_operations igmp6_mcf_seq_ops = {
  2263. .start = igmp6_mcf_seq_start,
  2264. .next = igmp6_mcf_seq_next,
  2265. .stop = igmp6_mcf_seq_stop,
  2266. .show = igmp6_mcf_seq_show,
  2267. };
  2268. static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
  2269. {
  2270. return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
  2271. sizeof(struct igmp6_mcf_iter_state));
  2272. }
  2273. static const struct file_operations igmp6_mcf_seq_fops = {
  2274. .owner = THIS_MODULE,
  2275. .open = igmp6_mcf_seq_open,
  2276. .read = seq_read,
  2277. .llseek = seq_lseek,
  2278. .release = seq_release_net,
  2279. };
  2280. static int igmp6_proc_init(struct net *net)
  2281. {
  2282. int err;
  2283. err = -ENOMEM;
  2284. if (!proc_net_fops_create(net, "igmp6", S_IRUGO, &igmp6_mc_seq_fops))
  2285. goto out;
  2286. if (!proc_net_fops_create(net, "mcfilter6", S_IRUGO,
  2287. &igmp6_mcf_seq_fops))
  2288. goto out_proc_net_igmp6;
  2289. err = 0;
  2290. out:
  2291. return err;
  2292. out_proc_net_igmp6:
  2293. proc_net_remove(net, "igmp6");
  2294. goto out;
  2295. }
  2296. static void igmp6_proc_exit(struct net *net)
  2297. {
  2298. proc_net_remove(net, "mcfilter6");
  2299. proc_net_remove(net, "igmp6");
  2300. }
  2301. #else
  2302. static int igmp6_proc_init(struct net *net)
  2303. {
  2304. return 0;
  2305. }
  2306. static void igmp6_proc_exit(struct net *net)
  2307. {
  2308. ;
  2309. }
  2310. #endif
  2311. static int igmp6_net_init(struct net *net)
  2312. {
  2313. int err;
  2314. err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
  2315. SOCK_RAW, IPPROTO_ICMPV6, net);
  2316. if (err < 0) {
  2317. printk(KERN_ERR
  2318. "Failed to initialize the IGMP6 control socket (err %d).\n",
  2319. err);
  2320. goto out;
  2321. }
  2322. inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
  2323. err = igmp6_proc_init(net);
  2324. if (err)
  2325. goto out_sock_create;
  2326. out:
  2327. return err;
  2328. out_sock_create:
  2329. inet_ctl_sock_destroy(net->ipv6.igmp_sk);
  2330. goto out;
  2331. }
  2332. static void igmp6_net_exit(struct net *net)
  2333. {
  2334. inet_ctl_sock_destroy(net->ipv6.igmp_sk);
  2335. igmp6_proc_exit(net);
  2336. }
  2337. static struct pernet_operations igmp6_net_ops = {
  2338. .init = igmp6_net_init,
  2339. .exit = igmp6_net_exit,
  2340. };
  2341. int __init igmp6_init(void)
  2342. {
  2343. return register_pernet_subsys(&igmp6_net_ops);
  2344. }
  2345. void igmp6_cleanup(void)
  2346. {
  2347. unregister_pernet_subsys(&igmp6_net_ops);
  2348. }