mcast.c 63 KB

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