mcast.c 63 KB

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