mcast.c 63 KB

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