mcast.c 62 KB

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