mcast.c 60 KB

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