mcast.c 62 KB

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