Lines Matching +full:im +full:-
1 // SPDX-License-Identifier: GPL-2.0-or-later
30 * Chih-Jen Chang : Tried to revise IGMP to Version 2
31 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
33 * ipmulti-3.5 source code.
34 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
35 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
37 * Chih-Jen Chang : Added the max_resp_time parameter to
38 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
41 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
42 * Tsu-Sheng Tsao if the specified time expired.
53 * igmp_timer_expire if tm->running is
64 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
108 /* Parameter names and values are taken from igmp-v2-06 draft */
123 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
125 ((in_dev)->mr_v1_seen && \
126 time_before(jiffies, (in_dev)->mr_v1_seen)))
128 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
130 ((in_dev)->mr_v2_seen && \
131 time_before(jiffies, (in_dev)->mr_v2_seen)))
156 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im,
158 static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im);
167 static void ip_ma_put(struct ip_mc_list *im) in ip_ma_put() argument
169 if (refcount_dec_and_test(&im->refcnt)) { in ip_ma_put()
170 in_dev_put(im->interface); in ip_ma_put()
171 kfree_rcu(im, rcu); in ip_ma_put()
176 for (pmc = rcu_dereference(in_dev->mc_list); \
178 pmc = rcu_dereference(pmc->next_rcu))
181 for (pmc = rtnl_dereference(in_dev->mc_list); \
183 pmc = rtnl_dereference(pmc->next_rcu))
190 next = psf->sf_next; in ip_sf_list_clear_all()
202 static void igmp_stop_timer(struct ip_mc_list *im) in igmp_stop_timer() argument
204 spin_lock_bh(&im->lock); in igmp_stop_timer()
205 if (del_timer(&im->timer)) in igmp_stop_timer()
206 refcount_dec(&im->refcnt); in igmp_stop_timer()
207 im->tm_running = 0; in igmp_stop_timer()
208 im->reporter = 0; in igmp_stop_timer()
209 im->unsolicit_count = 0; in igmp_stop_timer()
210 spin_unlock_bh(&im->lock); in igmp_stop_timer()
213 /* It must be called with locked im->lock */
214 static void igmp_start_timer(struct ip_mc_list *im, int max_delay) in igmp_start_timer() argument
218 im->tm_running = 1; in igmp_start_timer()
219 if (!mod_timer(&im->timer, jiffies+tv+2)) in igmp_start_timer()
220 refcount_inc(&im->refcnt); in igmp_start_timer()
225 int tv = prandom_u32_max(in_dev->mr_maxdelay); in igmp_gq_start_timer()
228 if (in_dev->mr_gq_running && in igmp_gq_start_timer()
229 time_after_eq(exp, (in_dev->mr_gq_timer).expires)) in igmp_gq_start_timer()
232 in_dev->mr_gq_running = 1; in igmp_gq_start_timer()
233 if (!mod_timer(&in_dev->mr_gq_timer, exp)) in igmp_gq_start_timer()
241 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2)) in igmp_ifc_start_timer()
245 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay) in igmp_mod_timer() argument
247 spin_lock_bh(&im->lock); in igmp_mod_timer()
248 im->unsolicit_count = 0; in igmp_mod_timer()
249 if (del_timer(&im->timer)) { in igmp_mod_timer()
250 if ((long)(im->timer.expires-jiffies) < max_delay) { in igmp_mod_timer()
251 add_timer(&im->timer); in igmp_mod_timer()
252 im->tm_running = 1; in igmp_mod_timer()
253 spin_unlock_bh(&im->lock); in igmp_mod_timer()
256 refcount_dec(&im->refcnt); in igmp_mod_timer()
258 igmp_start_timer(im, max_delay); in igmp_mod_timer()
259 spin_unlock_bh(&im->lock); in igmp_mod_timer()
278 if (!(pmc->gsquery && !psf->sf_gsresp)) { in is_in()
279 if (pmc->sfmode == MCAST_INCLUDE) in is_in()
284 if (psf->sf_count[MCAST_INCLUDE]) in is_in()
286 return pmc->sfcount[MCAST_EXCLUDE] == in is_in()
287 psf->sf_count[MCAST_EXCLUDE]; in is_in()
293 return psf->sf_count[MCAST_INCLUDE] != 0; in is_in()
297 if (pmc->sfcount[MCAST_EXCLUDE] == 0 || in is_in()
298 psf->sf_count[MCAST_INCLUDE]) in is_in()
300 return pmc->sfcount[MCAST_EXCLUDE] == in is_in()
301 psf->sf_count[MCAST_EXCLUDE]; in is_in()
303 if (gdeleted || !psf->sf_crcount) in is_in()
305 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted; in is_in()
307 if (pmc->sfmode == MCAST_INCLUDE) in is_in()
308 return gdeleted || (psf->sf_crcount && sdeleted); in is_in()
309 return psf->sf_crcount && !gdeleted && !sdeleted; in is_in()
320 for (psf = pmc->sources; psf; psf = psf->sf_next) { in igmp_scount()
339 if (fl4->saddr == ifa->ifa_local) in igmpv3_get_srcaddr()
340 return fl4->saddr; in igmpv3_get_srcaddr()
355 int tlen = dev->needed_tailroom; in igmpv3_newpack()
367 skb->priority = TC_PRIO_CONTROL; in igmpv3_newpack()
371 IPPROTO_IGMP, 0, dev->ifindex); in igmpv3_newpack()
377 skb_dst_set(skb, &rt->dst); in igmpv3_newpack()
378 skb->dev = dev; in igmpv3_newpack()
387 pip->version = 4; in igmpv3_newpack()
388 pip->ihl = (sizeof(struct iphdr)+4)>>2; in igmpv3_newpack()
389 pip->tos = 0xc0; in igmpv3_newpack()
390 pip->frag_off = htons(IP_DF); in igmpv3_newpack()
391 pip->ttl = 1; in igmpv3_newpack()
392 pip->daddr = fl4.daddr; in igmpv3_newpack()
395 pip->saddr = igmpv3_get_srcaddr(dev, &fl4); in igmpv3_newpack()
398 pip->protocol = IPPROTO_IGMP; in igmpv3_newpack()
399 pip->tot_len = 0; /* filled in later */ in igmpv3_newpack()
406 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4; in igmpv3_newpack()
409 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT; in igmpv3_newpack()
410 pig->resv1 = 0; in igmpv3_newpack()
411 pig->csum = 0; in igmpv3_newpack()
412 pig->resv2 = 0; in igmpv3_newpack()
413 pig->ngrec = 0; in igmpv3_newpack()
420 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb); in igmpv3_sendpack()
422 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen); in igmpv3_sendpack()
424 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb); in igmpv3_sendpack()
435 struct net_device *dev = pmc->interface->dev; in add_grhead()
445 pgr->grec_type = type; in add_grhead()
446 pgr->grec_auxwords = 0; in add_grhead()
447 pgr->grec_nsrcs = 0; in add_grhead()
448 pgr->grec_mca = pmc->multiaddr; in add_grhead()
450 pih->ngrec = htons(ntohs(pih->ngrec)+1); in add_grhead()
460 struct net_device *dev = pmc->interface->dev; in add_grec()
468 if (pmc->multiaddr == IGMP_ALL_HOSTS) in add_grec()
470 if (ipv4_is_local_multicast(pmc->multiaddr) && in add_grec()
471 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports)) in add_grec()
474 mtu = READ_ONCE(dev->mtu); in add_grec()
485 psf_list = sdeleted ? &pmc->tomb : &pmc->sources; in add_grec()
494 if (pih && pih->ngrec && in add_grec()
506 psf_next = psf->sf_next; in add_grec()
513 /* Based on RFC3376 5.1. Should not send source-list change in add_grec()
516 if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) || in add_grec()
517 (!gdeleted && pmc->crcount)) && in add_grec()
519 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) in add_grec()
524 psf->sf_gsresp = 0; in add_grec()
531 pgr->grec_nsrcs = htons(scount); in add_grec()
545 *psrc = psf->sf_inaddr; in add_grec()
548 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) { in add_grec()
550 psf->sf_crcount--; in add_grec()
551 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) { in add_grec()
553 psf_prev->sf_next = psf->sf_next; in add_grec()
555 *psf_list = psf->sf_next; in add_grec()
568 if (pmc->crcount || isquery) { in add_grec()
578 pgr->grec_nsrcs = htons(scount); in add_grec()
581 pmc->gsquery = 0; /* clear query state on report */ in add_grec()
588 struct net *net = dev_net(in_dev->dev); in igmpv3_send_report()
594 if (pmc->multiaddr == IGMP_ALL_HOSTS) in igmpv3_send_report()
596 if (ipv4_is_local_multicast(pmc->multiaddr) && in igmpv3_send_report()
597 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports)) in igmpv3_send_report()
599 spin_lock_bh(&pmc->lock); in igmpv3_send_report()
600 if (pmc->sfcount[MCAST_EXCLUDE]) in igmpv3_send_report()
605 spin_unlock_bh(&pmc->lock); in igmpv3_send_report()
609 spin_lock_bh(&pmc->lock); in igmpv3_send_report()
610 if (pmc->sfcount[MCAST_EXCLUDE]) in igmpv3_send_report()
615 spin_unlock_bh(&pmc->lock); in igmpv3_send_report()
623 * remove zero-count source records from a source filter list
631 psf_next = psf->sf_next; in igmpv3_clear_zeros()
632 if (psf->sf_crcount == 0) { in igmpv3_clear_zeros()
634 psf_prev->sf_next = psf->sf_next; in igmpv3_clear_zeros()
636 *ppsf = psf->sf_next; in igmpv3_clear_zeros()
645 ip_sf_list_clear_all(pmc->sources); in kfree_pmc()
646 ip_sf_list_clear_all(pmc->tomb); in kfree_pmc()
657 spin_lock_bh(&in_dev->mc_tomb_lock); in igmpv3_send_cr()
661 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) { in igmpv3_send_cr()
662 pmc_next = pmc->next; in igmpv3_send_cr()
663 if (pmc->sfmode == MCAST_INCLUDE) { in igmpv3_send_cr()
669 if (pmc->crcount) { in igmpv3_send_cr()
670 if (pmc->sfmode == MCAST_EXCLUDE) { in igmpv3_send_cr()
674 pmc->crcount--; in igmpv3_send_cr()
675 if (pmc->crcount == 0) { in igmpv3_send_cr()
676 igmpv3_clear_zeros(&pmc->tomb); in igmpv3_send_cr()
677 igmpv3_clear_zeros(&pmc->sources); in igmpv3_send_cr()
680 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) { in igmpv3_send_cr()
682 pmc_prev->next = pmc_next; in igmpv3_send_cr()
684 in_dev->mc_tomb = pmc_next; in igmpv3_send_cr()
685 in_dev_put(pmc->interface); in igmpv3_send_cr()
690 spin_unlock_bh(&in_dev->mc_tomb_lock); in igmpv3_send_cr()
694 spin_lock_bh(&pmc->lock); in igmpv3_send_cr()
695 if (pmc->sfcount[MCAST_EXCLUDE]) { in igmpv3_send_cr()
706 if (pmc->crcount) { in igmpv3_send_cr()
707 if (pmc->sfmode == MCAST_EXCLUDE) in igmpv3_send_cr()
712 pmc->crcount--; in igmpv3_send_cr()
714 spin_unlock_bh(&pmc->lock); in igmpv3_send_cr()
730 struct net_device *dev = in_dev->dev; in igmp_send_report()
732 __be32 group = pmc ? pmc->multiaddr : 0; in igmp_send_report()
741 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports)) in igmp_send_report()
751 IPPROTO_IGMP, 0, dev->ifindex); in igmp_send_report()
753 return -1; in igmp_send_report()
756 tlen = dev->needed_tailroom; in igmp_send_report()
760 return -1; in igmp_send_report()
762 skb->priority = TC_PRIO_CONTROL; in igmp_send_report()
764 skb_dst_set(skb, &rt->dst); in igmp_send_report()
772 iph->version = 4; in igmp_send_report()
773 iph->ihl = (sizeof(struct iphdr)+4)>>2; in igmp_send_report()
774 iph->tos = 0xc0; in igmp_send_report()
775 iph->frag_off = htons(IP_DF); in igmp_send_report()
776 iph->ttl = 1; in igmp_send_report()
777 iph->daddr = dst; in igmp_send_report()
778 iph->saddr = fl4.saddr; in igmp_send_report()
779 iph->protocol = IPPROTO_IGMP; in igmp_send_report()
787 ih->type = type; in igmp_send_report()
788 ih->code = 0; in igmp_send_report()
789 ih->csum = 0; in igmp_send_report()
790 ih->group = group; in igmp_send_report()
791 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); in igmp_send_report()
793 return ip_local_out(net, skb->sk, skb); in igmp_send_report()
800 in_dev->mr_gq_running = 0; in igmp_gq_timer_expire()
812 mr_ifc_count = READ_ONCE(in_dev->mr_ifc_count); in igmp_ifc_timer_expire()
815 if (cmpxchg(&in_dev->mr_ifc_count, in igmp_ifc_timer_expire()
817 mr_ifc_count - 1) != mr_ifc_count) in igmp_ifc_timer_expire()
827 struct net *net = dev_net(in_dev->dev); in igmp_ifc_event()
830 WRITE_ONCE(in_dev->mr_ifc_count, in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv)); in igmp_ifc_event()
837 struct ip_mc_list *im = from_timer(im, t, timer); in igmp_timer_expire() local
838 struct in_device *in_dev = im->interface; in igmp_timer_expire()
840 spin_lock(&im->lock); in igmp_timer_expire()
841 im->tm_running = 0; in igmp_timer_expire()
843 if (im->unsolicit_count && --im->unsolicit_count) in igmp_timer_expire()
844 igmp_start_timer(im, unsolicited_report_interval(in_dev)); in igmp_timer_expire()
846 im->reporter = 1; in igmp_timer_expire()
847 spin_unlock(&im->lock); in igmp_timer_expire()
850 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT); in igmp_timer_expire()
852 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT); in igmp_timer_expire()
854 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT); in igmp_timer_expire()
856 ip_ma_put(im); in igmp_timer_expire()
859 /* mark EXCLUDE-mode sources */
866 for (psf = pmc->sources; psf; psf = psf->sf_next) { in igmp_xmarksources()
871 if (psf->sf_count[MCAST_INCLUDE] || in igmp_xmarksources()
872 pmc->sfcount[MCAST_EXCLUDE] != in igmp_xmarksources()
873 psf->sf_count[MCAST_EXCLUDE]) in igmp_xmarksources()
875 if (srcs[i] == psf->sf_inaddr) { in igmp_xmarksources()
881 pmc->gsquery = 0; in igmp_xmarksources()
892 if (pmc->sfmode == MCAST_EXCLUDE) in igmp_marksources()
895 /* mark INCLUDE-mode sources */ in igmp_marksources()
897 for (psf = pmc->sources; psf; psf = psf->sf_next) { in igmp_marksources()
901 if (srcs[i] == psf->sf_inaddr) { in igmp_marksources()
902 psf->sf_gsresp = 1; in igmp_marksources()
908 pmc->gsquery = 0; in igmp_marksources()
911 pmc->gsquery = 1; in igmp_marksources()
918 struct ip_mc_list *im; in igmp_heard_report() local
919 struct net *net = dev_net(in_dev->dev); in igmp_heard_report()
921 /* Timers are only set for non-local groups */ in igmp_heard_report()
926 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports)) in igmp_heard_report()
930 for_each_pmc_rcu(in_dev, im) { in igmp_heard_report()
931 if (im->multiaddr == group) { in igmp_heard_report()
932 igmp_stop_timer(im); in igmp_heard_report()
946 struct ip_mc_list *im; in igmp_heard_query() local
947 __be32 group = ih->group; in igmp_heard_query()
950 struct net *net = dev_net(in_dev->dev); in igmp_heard_query()
954 if (ih->code == 0) { in igmp_heard_query()
958 in_dev->mr_v1_seen = jiffies + in igmp_heard_query()
959 (in_dev->mr_qrv * in_dev->mr_qi) + in igmp_heard_query()
960 in_dev->mr_qri; in igmp_heard_query()
964 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE); in igmp_heard_query()
965 in_dev->mr_v2_seen = jiffies + in igmp_heard_query()
966 (in_dev->mr_qrv * in_dev->mr_qi) + in igmp_heard_query()
967 in_dev->mr_qri; in igmp_heard_query()
970 WRITE_ONCE(in_dev->mr_ifc_count, 0); in igmp_heard_query()
971 if (del_timer(&in_dev->mr_ifc_timer)) in igmp_heard_query()
988 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); in igmp_heard_query()
996 if (ih3->nsrcs) { in igmp_heard_query()
998 + ntohs(ih3->nsrcs)*sizeof(__be32))) in igmp_heard_query()
1003 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); in igmp_heard_query()
1006 in_dev->mr_maxdelay = max_delay; in igmp_heard_query()
1012 in_dev->mr_qrv = ih3->qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv); in igmp_heard_query()
1013 in_dev->mr_qi = IGMPV3_QQIC(ih3->qqic)*HZ ?: IGMP_QUERY_INTERVAL; in igmp_heard_query()
1019 if (in_dev->mr_qri >= in_dev->mr_qi) in igmp_heard_query()
1020 in_dev->mr_qri = (in_dev->mr_qi/HZ - 1)*HZ; in igmp_heard_query()
1023 if (ih3->nsrcs) in igmp_heard_query()
1028 /* mark sources to include, if group & source-specific */ in igmp_heard_query()
1029 mark = ih3->nsrcs != 0; in igmp_heard_query()
1033 * - Start the timers in all of our membership records in igmp_heard_query()
1037 * - For timers already running check if they need to in igmp_heard_query()
1039 * - Use the igmp->igmp_code field as the maximum in igmp_heard_query()
1043 for_each_pmc_rcu(in_dev, im) { in igmp_heard_query()
1046 if (group && group != im->multiaddr) in igmp_heard_query()
1048 if (im->multiaddr == IGMP_ALL_HOSTS) in igmp_heard_query()
1050 if (ipv4_is_local_multicast(im->multiaddr) && in igmp_heard_query()
1051 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports)) in igmp_heard_query()
1053 spin_lock_bh(&im->lock); in igmp_heard_query()
1054 if (im->tm_running) in igmp_heard_query()
1055 im->gsquery = im->gsquery && mark; in igmp_heard_query()
1057 im->gsquery = mark; in igmp_heard_query()
1058 changed = !im->gsquery || in igmp_heard_query()
1059 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs); in igmp_heard_query()
1060 spin_unlock_bh(&im->lock); in igmp_heard_query()
1062 igmp_mod_timer(im, max_delay); in igmp_heard_query()
1071 /* This basically follows the spec line by line -- see RFC1112 */ in igmp_rcv()
1073 struct net_device *dev = skb->dev; in igmp_rcv()
1075 int len = skb->len; in igmp_rcv()
1079 dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif); in igmp_rcv()
1095 switch (ih->type) { in igmp_rcv()
1105 if (skb->pkt_type == PACKET_MULTICAST || in igmp_rcv()
1106 skb->pkt_type == PACKET_BROADCAST) in igmp_rcv()
1107 dropped = igmp_heard_report(in_dev, ih->group); in igmp_rcv()
1142 struct net_device *dev = in_dev->dev; in ip_mc_filter_add()
1144 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG. in ip_mc_filter_add()
1148 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; } in ip_mc_filter_add()
1149 --ANK in ip_mc_filter_add()
1162 struct net_device *dev = in_dev->dev; in ip_mc_filter_del()
1172 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im, in igmpv3_add_delrec() argument
1176 struct net *net = dev_net(in_dev->dev); in igmpv3_add_delrec()
1182 * non-deleted or query-response MCA's. in igmpv3_add_delrec()
1187 spin_lock_init(&pmc->lock); in igmpv3_add_delrec()
1188 spin_lock_bh(&im->lock); in igmpv3_add_delrec()
1189 pmc->interface = im->interface; in igmpv3_add_delrec()
1191 pmc->multiaddr = im->multiaddr; in igmpv3_add_delrec()
1192 pmc->crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv); in igmpv3_add_delrec()
1193 pmc->sfmode = im->sfmode; in igmpv3_add_delrec()
1194 if (pmc->sfmode == MCAST_INCLUDE) { in igmpv3_add_delrec()
1197 pmc->tomb = im->tomb; in igmpv3_add_delrec()
1198 pmc->sources = im->sources; in igmpv3_add_delrec()
1199 im->tomb = im->sources = NULL; in igmpv3_add_delrec()
1200 for (psf = pmc->sources; psf; psf = psf->sf_next) in igmpv3_add_delrec()
1201 psf->sf_crcount = pmc->crcount; in igmpv3_add_delrec()
1203 spin_unlock_bh(&im->lock); in igmpv3_add_delrec()
1205 spin_lock_bh(&in_dev->mc_tomb_lock); in igmpv3_add_delrec()
1206 pmc->next = in_dev->mc_tomb; in igmpv3_add_delrec()
1207 in_dev->mc_tomb = pmc; in igmpv3_add_delrec()
1208 spin_unlock_bh(&in_dev->mc_tomb_lock); in igmpv3_add_delrec()
1214 static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im) in igmpv3_del_delrec() argument
1218 struct net *net = dev_net(in_dev->dev); in igmpv3_del_delrec()
1219 __be32 multiaddr = im->multiaddr; in igmpv3_del_delrec()
1221 spin_lock_bh(&in_dev->mc_tomb_lock); in igmpv3_del_delrec()
1223 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) { in igmpv3_del_delrec()
1224 if (pmc->multiaddr == multiaddr) in igmpv3_del_delrec()
1230 pmc_prev->next = pmc->next; in igmpv3_del_delrec()
1232 in_dev->mc_tomb = pmc->next; in igmpv3_del_delrec()
1234 spin_unlock_bh(&in_dev->mc_tomb_lock); in igmpv3_del_delrec()
1236 spin_lock_bh(&im->lock); in igmpv3_del_delrec()
1238 im->interface = pmc->interface; in igmpv3_del_delrec()
1239 if (im->sfmode == MCAST_INCLUDE) { in igmpv3_del_delrec()
1240 swap(im->tomb, pmc->tomb); in igmpv3_del_delrec()
1241 swap(im->sources, pmc->sources); in igmpv3_del_delrec()
1242 for (psf = im->sources; psf; psf = psf->sf_next) in igmpv3_del_delrec()
1243 psf->sf_crcount = in_dev->mr_qrv ?: in igmpv3_del_delrec()
1244 READ_ONCE(net->ipv4.sysctl_igmp_qrv); in igmpv3_del_delrec()
1246 im->crcount = in_dev->mr_qrv ?: in igmpv3_del_delrec()
1247 READ_ONCE(net->ipv4.sysctl_igmp_qrv); in igmpv3_del_delrec()
1249 in_dev_put(pmc->interface); in igmpv3_del_delrec()
1252 spin_unlock_bh(&im->lock); in igmpv3_del_delrec()
1262 spin_lock_bh(&in_dev->mc_tomb_lock); in igmpv3_clear_delrec()
1263 pmc = in_dev->mc_tomb; in igmpv3_clear_delrec()
1264 in_dev->mc_tomb = NULL; in igmpv3_clear_delrec()
1265 spin_unlock_bh(&in_dev->mc_tomb_lock); in igmpv3_clear_delrec()
1268 nextpmc = pmc->next; in igmpv3_clear_delrec()
1270 in_dev_put(pmc->interface); in igmpv3_clear_delrec()
1278 spin_lock_bh(&pmc->lock); in igmpv3_clear_delrec()
1279 psf = pmc->tomb; in igmpv3_clear_delrec()
1280 pmc->tomb = NULL; in igmpv3_clear_delrec()
1281 spin_unlock_bh(&pmc->lock); in igmpv3_clear_delrec()
1288 static void __igmp_group_dropped(struct ip_mc_list *im, gfp_t gfp) in __igmp_group_dropped() argument
1290 struct in_device *in_dev = im->interface; in __igmp_group_dropped()
1292 struct net *net = dev_net(in_dev->dev); in __igmp_group_dropped()
1296 if (im->loaded) { in __igmp_group_dropped()
1297 im->loaded = 0; in __igmp_group_dropped()
1298 ip_mc_filter_del(in_dev, im->multiaddr); in __igmp_group_dropped()
1302 if (im->multiaddr == IGMP_ALL_HOSTS) in __igmp_group_dropped()
1304 if (ipv4_is_local_multicast(im->multiaddr) && in __igmp_group_dropped()
1305 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports)) in __igmp_group_dropped()
1308 reporter = im->reporter; in __igmp_group_dropped()
1309 igmp_stop_timer(im); in __igmp_group_dropped()
1311 if (!in_dev->dead) { in __igmp_group_dropped()
1316 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE); in __igmp_group_dropped()
1320 igmpv3_add_delrec(in_dev, im, gfp); in __igmp_group_dropped()
1327 static void igmp_group_dropped(struct ip_mc_list *im) in igmp_group_dropped() argument
1329 __igmp_group_dropped(im, GFP_KERNEL); in igmp_group_dropped()
1332 static void igmp_group_added(struct ip_mc_list *im) in igmp_group_added() argument
1334 struct in_device *in_dev = im->interface; in igmp_group_added()
1336 struct net *net = dev_net(in_dev->dev); in igmp_group_added()
1339 if (im->loaded == 0) { in igmp_group_added()
1340 im->loaded = 1; in igmp_group_added()
1341 ip_mc_filter_add(in_dev, im->multiaddr); in igmp_group_added()
1345 if (im->multiaddr == IGMP_ALL_HOSTS) in igmp_group_added()
1347 if (ipv4_is_local_multicast(im->multiaddr) && in igmp_group_added()
1348 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports)) in igmp_group_added()
1351 if (in_dev->dead) in igmp_group_added()
1354 im->unsolicit_count = READ_ONCE(net->ipv4.sysctl_igmp_qrv); in igmp_group_added()
1356 spin_lock_bh(&im->lock); in igmp_group_added()
1357 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY); in igmp_group_added()
1358 spin_unlock_bh(&im->lock); in igmp_group_added()
1364 * not send filter-mode change record as the mode should be from in igmp_group_added()
1367 if (im->sfmode == MCAST_EXCLUDE) in igmp_group_added()
1368 im->crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv); in igmp_group_added()
1379 static u32 ip_mc_hash(const struct ip_mc_list *im) in ip_mc_hash() argument
1381 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG); in ip_mc_hash()
1385 struct ip_mc_list *im) in ip_mc_hash_add() argument
1390 mc_hash = rtnl_dereference(in_dev->mc_hash); in ip_mc_hash_add()
1392 hash = ip_mc_hash(im); in ip_mc_hash_add()
1393 im->next_hash = mc_hash[hash]; in ip_mc_hash_add()
1394 rcu_assign_pointer(mc_hash[hash], im); in ip_mc_hash_add()
1399 if (in_dev->mc_count < 4) in ip_mc_hash_add()
1407 for_each_pmc_rtnl(in_dev, im) { in ip_mc_hash_add()
1408 hash = ip_mc_hash(im); in ip_mc_hash_add()
1409 im->next_hash = mc_hash[hash]; in ip_mc_hash_add()
1410 RCU_INIT_POINTER(mc_hash[hash], im); in ip_mc_hash_add()
1413 rcu_assign_pointer(in_dev->mc_hash, mc_hash); in ip_mc_hash_add()
1417 struct ip_mc_list *im) in ip_mc_hash_remove() argument
1419 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash); in ip_mc_hash_remove()
1424 mc_hash += ip_mc_hash(im); in ip_mc_hash_remove()
1425 while ((aux = rtnl_dereference(*mc_hash)) != im) in ip_mc_hash_remove()
1426 mc_hash = &aux->next_hash; in ip_mc_hash_remove()
1427 *mc_hash = im->next_hash; in ip_mc_hash_remove()
1437 struct ip_mc_list *im; in ____ip_mc_inc_group() local
1441 for_each_pmc_rtnl(in_dev, im) { in ____ip_mc_inc_group()
1442 if (im->multiaddr == addr) { in ____ip_mc_inc_group()
1443 im->users++; in ____ip_mc_inc_group()
1449 im = kzalloc(sizeof(*im), gfp); in ____ip_mc_inc_group()
1450 if (!im) in ____ip_mc_inc_group()
1453 im->users = 1; in ____ip_mc_inc_group()
1454 im->interface = in_dev; in ____ip_mc_inc_group()
1456 im->multiaddr = addr; in ____ip_mc_inc_group()
1458 im->sfmode = mode; in ____ip_mc_inc_group()
1459 im->sfcount[mode] = 1; in ____ip_mc_inc_group()
1460 refcount_set(&im->refcnt, 1); in ____ip_mc_inc_group()
1461 spin_lock_init(&im->lock); in ____ip_mc_inc_group()
1463 timer_setup(&im->timer, igmp_timer_expire, 0); in ____ip_mc_inc_group()
1466 im->next_rcu = in_dev->mc_list; in ____ip_mc_inc_group()
1467 in_dev->mc_count++; in ____ip_mc_inc_group()
1468 rcu_assign_pointer(in_dev->mc_list, im); in ____ip_mc_inc_group()
1470 ip_mc_hash_add(in_dev, im); in ____ip_mc_inc_group()
1473 igmpv3_del_delrec(in_dev, im); in ____ip_mc_inc_group()
1475 igmp_group_added(im); in ____ip_mc_inc_group()
1476 if (!in_dev->dead) in ____ip_mc_inc_group()
1501 return -EINVAL; in ip_mc_check_iphdr()
1505 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph)) in ip_mc_check_iphdr()
1506 return -EINVAL; in ip_mc_check_iphdr()
1508 offset += ip_hdrlen(skb) - sizeof(*iph); in ip_mc_check_iphdr()
1511 return -EINVAL; in ip_mc_check_iphdr()
1515 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) in ip_mc_check_iphdr()
1516 return -EINVAL; in ip_mc_check_iphdr()
1518 len = skb_network_offset(skb) + ntohs(iph->tot_len); in ip_mc_check_iphdr()
1519 if (skb->len < len || len < offset) in ip_mc_check_iphdr()
1520 return -EINVAL; in ip_mc_check_iphdr()
1533 return ip_mc_may_pull(skb, len) ? 0 : -EINVAL; in ip_mc_check_igmp_reportv3()
1545 return -EINVAL; in ip_mc_check_igmp_query()
1549 return -EINVAL; in ip_mc_check_igmp_query()
1553 * all-systems destination addresses (224.0.0.1) for general queries in ip_mc_check_igmp_query()
1555 if (!igmp_hdr(skb)->group && in ip_mc_check_igmp_query()
1556 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP)) in ip_mc_check_igmp_query()
1557 return -EINVAL; in ip_mc_check_igmp_query()
1564 switch (igmp_hdr(skb)->type) { in ip_mc_check_igmp_msg()
1574 return -ENOMSG; in ip_mc_check_igmp_msg()
1590 return -EINVAL; in ip_mc_check_igmp_csum()
1595 return -EINVAL; in ip_mc_check_igmp_csum()
1604 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1610 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1612 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1613 * -ENOMEM: A memory allocation failure happened.
1625 if (ip_hdr(skb)->protocol != IPPROTO_IGMP) in ip_mc_check_igmp()
1626 return -ENOMSG; in ip_mc_check_igmp()
1642 struct ip_mc_list *im; in ip_mc_rejoin_groups() local
1644 struct net *net = dev_net(in_dev->dev); in ip_mc_rejoin_groups()
1648 for_each_pmc_rtnl(in_dev, im) { in ip_mc_rejoin_groups()
1649 if (im->multiaddr == IGMP_ALL_HOSTS) in ip_mc_rejoin_groups()
1651 if (ipv4_is_local_multicast(im->multiaddr) && in ip_mc_rejoin_groups()
1652 !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports)) in ip_mc_rejoin_groups()
1664 igmp_send_report(in_dev, im, type); in ip_mc_rejoin_groups()
1680 for (ip = &in_dev->mc_list; in __ip_mc_dec_group()
1682 ip = &i->next_rcu) { in __ip_mc_dec_group()
1683 if (i->multiaddr == addr) { in __ip_mc_dec_group()
1684 if (--i->users == 0) { in __ip_mc_dec_group()
1686 *ip = i->next_rcu; in __ip_mc_dec_group()
1687 in_dev->mc_count--; in __ip_mc_dec_group()
1691 if (!in_dev->dead) in __ip_mc_dec_group()
1741 WRITE_ONCE(in_dev->mr_ifc_count, 0); in ip_mc_down()
1742 if (del_timer(&in_dev->mr_ifc_timer)) in ip_mc_down()
1744 in_dev->mr_gq_running = 0; in ip_mc_down()
1745 if (del_timer(&in_dev->mr_gq_timer)) in ip_mc_down()
1755 struct net *net = dev_net(in_dev->dev); in ip_mc_reset()
1757 in_dev->mr_qi = IGMP_QUERY_INTERVAL; in ip_mc_reset()
1758 in_dev->mr_qri = IGMP_QUERY_RESPONSE_INTERVAL; in ip_mc_reset()
1759 in_dev->mr_qrv = READ_ONCE(net->ipv4.sysctl_igmp_qrv); in ip_mc_reset()
1772 timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0); in ip_mc_init_dev()
1773 timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0); in ip_mc_init_dev()
1777 spin_lock_init(&in_dev->mc_tomb_lock); in ip_mc_init_dev()
1815 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) { in ip_mc_destroy_dev()
1816 in_dev->mc_list = i->next_rcu; in ip_mc_destroy_dev()
1817 in_dev->mc_count--; in ip_mc_destroy_dev()
1829 if (imr->imr_ifindex) { in ip_mc_find_dev()
1830 idev = inetdev_by_index(net, imr->imr_ifindex); in ip_mc_find_dev()
1833 if (imr->imr_address.s_addr) { in ip_mc_find_dev()
1834 dev = __ip_dev_find(net, imr->imr_address.s_addr, false); in ip_mc_find_dev()
1841 imr->imr_multiaddr.s_addr, in ip_mc_find_dev()
1844 dev = rt->dst.dev; in ip_mc_find_dev()
1849 imr->imr_ifindex = dev->ifindex; in ip_mc_find_dev()
1866 for (psf = pmc->sources; psf; psf = psf->sf_next) { in ip_mc_del1_src()
1867 if (psf->sf_inaddr == *psfsrc) in ip_mc_del1_src()
1871 if (!psf || psf->sf_count[sfmode] == 0) { in ip_mc_del1_src()
1873 return -ESRCH; in ip_mc_del1_src()
1875 psf->sf_count[sfmode]--; in ip_mc_del1_src()
1876 if (psf->sf_count[sfmode] == 0) { in ip_mc_del1_src()
1877 ip_rt_multicast_event(pmc->interface); in ip_mc_del1_src()
1879 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) { in ip_mc_del1_src()
1881 struct in_device *in_dev = pmc->interface; in ip_mc_del1_src()
1882 struct net *net = dev_net(in_dev->dev); in ip_mc_del1_src()
1887 psf_prev->sf_next = psf->sf_next; in ip_mc_del1_src()
1889 pmc->sources = psf->sf_next; in ip_mc_del1_src()
1891 if (psf->sf_oldin && in ip_mc_del1_src()
1893 psf->sf_crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv); in ip_mc_del1_src()
1894 psf->sf_next = pmc->tomb; in ip_mc_del1_src()
1895 pmc->tomb = psf; in ip_mc_del1_src()
1916 return -ENODEV; in ip_mc_del_src()
1919 if (*pmca == pmc->multiaddr) in ip_mc_del_src()
1925 return -ESRCH; in ip_mc_del_src()
1927 spin_lock_bh(&pmc->lock); in ip_mc_del_src()
1933 err = -EINVAL; in ip_mc_del_src()
1934 if (!pmc->sfcount[sfmode]) in ip_mc_del_src()
1936 pmc->sfcount[sfmode]--; in ip_mc_del_src()
1946 if (pmc->sfmode == MCAST_EXCLUDE && in ip_mc_del_src()
1947 pmc->sfcount[MCAST_EXCLUDE] == 0 && in ip_mc_del_src()
1948 pmc->sfcount[MCAST_INCLUDE]) { in ip_mc_del_src()
1951 struct net *net = dev_net(in_dev->dev); in ip_mc_del_src()
1955 pmc->sfmode = MCAST_INCLUDE; in ip_mc_del_src()
1957 pmc->crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv); in ip_mc_del_src()
1958 WRITE_ONCE(in_dev->mr_ifc_count, pmc->crcount); in ip_mc_del_src()
1959 for (psf = pmc->sources; psf; psf = psf->sf_next) in ip_mc_del_src()
1960 psf->sf_crcount = 0; in ip_mc_del_src()
1961 igmp_ifc_event(pmc->interface); in ip_mc_del_src()
1963 igmp_ifc_event(pmc->interface); in ip_mc_del_src()
1967 spin_unlock_bh(&pmc->lock); in ip_mc_del_src()
1972 * Add multicast single-source filter to the interface list
1980 for (psf = pmc->sources; psf; psf = psf->sf_next) { in ip_mc_add1_src()
1981 if (psf->sf_inaddr == *psfsrc) in ip_mc_add1_src()
1988 return -ENOBUFS; in ip_mc_add1_src()
1989 psf->sf_inaddr = *psfsrc; in ip_mc_add1_src()
1991 psf_prev->sf_next = psf; in ip_mc_add1_src()
1993 pmc->sources = psf; in ip_mc_add1_src()
1995 psf->sf_count[sfmode]++; in ip_mc_add1_src()
1996 if (psf->sf_count[sfmode] == 1) { in ip_mc_add1_src()
1997 ip_rt_multicast_event(pmc->interface); in ip_mc_add1_src()
2006 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; in sf_markstate()
2008 for (psf = pmc->sources; psf; psf = psf->sf_next) in sf_markstate()
2009 if (pmc->sfcount[MCAST_EXCLUDE]) { in sf_markstate()
2010 psf->sf_oldin = mca_xcount == in sf_markstate()
2011 psf->sf_count[MCAST_EXCLUDE] && in sf_markstate()
2012 !psf->sf_count[MCAST_INCLUDE]; in sf_markstate()
2014 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0; in sf_markstate()
2020 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; in sf_setstate()
2021 int qrv = pmc->interface->mr_qrv; in sf_setstate()
2025 for (psf = pmc->sources; psf; psf = psf->sf_next) { in sf_setstate()
2026 if (pmc->sfcount[MCAST_EXCLUDE]) { in sf_setstate()
2027 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] && in sf_setstate()
2028 !psf->sf_count[MCAST_INCLUDE]; in sf_setstate()
2030 new_in = psf->sf_count[MCAST_INCLUDE] != 0; in sf_setstate()
2032 if (!psf->sf_oldin) { in sf_setstate()
2035 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) { in sf_setstate()
2036 if (dpsf->sf_inaddr == psf->sf_inaddr) in sf_setstate()
2042 prev->sf_next = dpsf->sf_next; in sf_setstate()
2044 pmc->tomb = dpsf->sf_next; in sf_setstate()
2047 psf->sf_crcount = qrv; in sf_setstate()
2050 } else if (psf->sf_oldin) { in sf_setstate()
2052 psf->sf_crcount = 0; in sf_setstate()
2057 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) in sf_setstate()
2058 if (dpsf->sf_inaddr == psf->sf_inaddr) in sf_setstate()
2065 /* pmc->lock held by callers */ in sf_setstate()
2066 dpsf->sf_next = pmc->tomb; in sf_setstate()
2067 pmc->tomb = dpsf; in sf_setstate()
2069 dpsf->sf_crcount = qrv; in sf_setstate()
2088 return -ENODEV; in ip_mc_add_src()
2091 if (*pmca == pmc->multiaddr) in ip_mc_add_src()
2097 return -ESRCH; in ip_mc_add_src()
2099 spin_lock_bh(&pmc->lock); in ip_mc_add_src()
2105 isexclude = pmc->sfmode == MCAST_EXCLUDE; in ip_mc_add_src()
2107 pmc->sfcount[sfmode]++; in ip_mc_add_src()
2118 pmc->sfcount[sfmode]--; in ip_mc_add_src()
2121 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) { in ip_mc_add_src()
2124 struct net *net = dev_net(pmc->interface->dev); in ip_mc_add_src()
2125 in_dev = pmc->interface; in ip_mc_add_src()
2129 if (pmc->sfcount[MCAST_EXCLUDE]) in ip_mc_add_src()
2130 pmc->sfmode = MCAST_EXCLUDE; in ip_mc_add_src()
2131 else if (pmc->sfcount[MCAST_INCLUDE]) in ip_mc_add_src()
2132 pmc->sfmode = MCAST_INCLUDE; in ip_mc_add_src()
2136 pmc->crcount = in_dev->mr_qrv ?: READ_ONCE(net->ipv4.sysctl_igmp_qrv); in ip_mc_add_src()
2137 WRITE_ONCE(in_dev->mr_ifc_count, pmc->crcount); in ip_mc_add_src()
2138 for (psf = pmc->sources; psf; psf = psf->sf_next) in ip_mc_add_src()
2139 psf->sf_crcount = 0; in ip_mc_add_src()
2145 spin_unlock_bh(&pmc->lock); in ip_mc_add_src()
2153 spin_lock_bh(&pmc->lock); in ip_mc_clear_src()
2154 tomb = pmc->tomb; in ip_mc_clear_src()
2155 pmc->tomb = NULL; in ip_mc_clear_src()
2156 sources = pmc->sources; in ip_mc_clear_src()
2157 pmc->sources = NULL; in ip_mc_clear_src()
2158 pmc->sfmode = MCAST_EXCLUDE; in ip_mc_clear_src()
2159 pmc->sfcount[MCAST_INCLUDE] = 0; in ip_mc_clear_src()
2160 pmc->sfcount[MCAST_EXCLUDE] = 1; in ip_mc_clear_src()
2161 spin_unlock_bh(&pmc->lock); in ip_mc_clear_src()
2172 __be32 addr = imr->imr_multiaddr.s_addr; in __ip_mc_join_group()
2184 return -EINVAL; in __ip_mc_join_group()
2189 err = -ENODEV; in __ip_mc_join_group()
2193 err = -EADDRINUSE; in __ip_mc_join_group()
2194 ifindex = imr->imr_ifindex; in __ip_mc_join_group()
2196 if (i->multi.imr_multiaddr.s_addr == addr && in __ip_mc_join_group()
2197 i->multi.imr_ifindex == ifindex) in __ip_mc_join_group()
2201 err = -ENOBUFS; in __ip_mc_join_group()
2202 if (count >= READ_ONCE(net->ipv4.sysctl_igmp_max_memberships)) in __ip_mc_join_group()
2208 memcpy(&iml->multi, imr, sizeof(*imr)); in __ip_mc_join_group()
2209 iml->next_rcu = inet->mc_list; in __ip_mc_join_group()
2210 iml->sflist = NULL; in __ip_mc_join_group()
2211 iml->sfmode = mode; in __ip_mc_join_group()
2212 rcu_assign_pointer(inet->mc_list, iml); in __ip_mc_join_group()
2219 /* Join ASM (Any-Source Multicast) group
2227 /* Join SSM (Source-Specific Multicast) group
2238 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist); in ip_mc_leave_src()
2242 /* any-source empty exclude case */ in ip_mc_leave_src()
2243 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, in ip_mc_leave_src()
2244 iml->sfmode, 0, NULL, 0); in ip_mc_leave_src()
2246 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, in ip_mc_leave_src()
2247 iml->sfmode, psf->sl_count, psf->sl_addr, 0); in ip_mc_leave_src()
2248 RCU_INIT_POINTER(iml->sflist, NULL); in ip_mc_leave_src()
2250 atomic_sub(struct_size(psf, sl_addr, psf->sl_max), &sk->sk_omem_alloc); in ip_mc_leave_src()
2262 __be32 group = imr->imr_multiaddr.s_addr; in ip_mc_leave_group()
2264 int ret = -EADDRNOTAVAIL; in ip_mc_leave_group()
2269 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) { in ip_mc_leave_group()
2270 ret = -ENODEV; in ip_mc_leave_group()
2273 ifindex = imr->imr_ifindex; in ip_mc_leave_group()
2274 for (imlp = &inet->mc_list; in ip_mc_leave_group()
2276 imlp = &iml->next_rcu) { in ip_mc_leave_group()
2277 if (iml->multi.imr_multiaddr.s_addr != group) in ip_mc_leave_group()
2280 if (iml->multi.imr_ifindex != ifindex) in ip_mc_leave_group()
2282 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr != in ip_mc_leave_group()
2283 iml->multi.imr_address.s_addr) in ip_mc_leave_group()
2288 *imlp = iml->next_rcu; in ip_mc_leave_group()
2294 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); in ip_mc_leave_group()
2308 __be32 addr = mreqs->imr_multiaddr; in ip_mc_source()
2318 return -EINVAL; in ip_mc_source()
2322 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr; in ip_mc_source()
2323 imr.imr_address.s_addr = mreqs->imr_interface; in ip_mc_source()
2328 err = -ENODEV; in ip_mc_source()
2331 err = -EADDRNOTAVAIL; in ip_mc_source()
2334 if ((pmc->multi.imr_multiaddr.s_addr == in ip_mc_source()
2336 (pmc->multi.imr_ifindex == imr.imr_ifindex)) in ip_mc_source()
2340 err = -EINVAL; in ip_mc_source()
2344 if (pmc->sflist) { in ip_mc_source()
2345 if (pmc->sfmode != omode) { in ip_mc_source()
2346 err = -EINVAL; in ip_mc_source()
2349 } else if (pmc->sfmode != omode) { in ip_mc_source()
2350 /* allow mode switches for empty-set filters */ in ip_mc_source()
2351 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0); in ip_mc_source()
2352 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, in ip_mc_source()
2354 pmc->sfmode = omode; in ip_mc_source()
2357 psl = rtnl_dereference(pmc->sflist); in ip_mc_source()
2360 goto done; /* err = -EADDRNOTAVAIL */ in ip_mc_source()
2362 for (i = 0; i < psl->sl_count; i++) { in ip_mc_source()
2363 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, in ip_mc_source()
2369 goto done; /* err = -EADDRNOTAVAIL */ in ip_mc_source()
2371 /* special case - (INCLUDE, empty) == LEAVE_GROUP */ in ip_mc_source()
2372 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) { in ip_mc_source()
2378 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1, in ip_mc_source()
2379 &mreqs->imr_sourceaddr, 1); in ip_mc_source()
2381 for (j = i+1; j < psl->sl_count; j++) in ip_mc_source()
2382 psl->sl_addr[j-1] = psl->sl_addr[j]; in ip_mc_source()
2383 psl->sl_count--; in ip_mc_source()
2389 if (psl && psl->sl_count >= READ_ONCE(net->ipv4.sysctl_igmp_max_msf)) { in ip_mc_source()
2390 err = -ENOBUFS; in ip_mc_source()
2393 if (!psl || psl->sl_count == psl->sl_max) { in ip_mc_source()
2398 count += psl->sl_max; in ip_mc_source()
2402 err = -ENOBUFS; in ip_mc_source()
2405 newpsl->sl_max = count; in ip_mc_source()
2406 newpsl->sl_count = count - IP_SFBLOCK; in ip_mc_source()
2408 for (i = 0; i < psl->sl_count; i++) in ip_mc_source()
2409 newpsl->sl_addr[i] = psl->sl_addr[i]; in ip_mc_source()
2411 atomic_sub(struct_size(psl, sl_addr, psl->sl_max), in ip_mc_source()
2412 &sk->sk_omem_alloc); in ip_mc_source()
2414 rcu_assign_pointer(pmc->sflist, newpsl); in ip_mc_source()
2420 for (i = 0; i < psl->sl_count; i++) { in ip_mc_source()
2421 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, in ip_mc_source()
2428 for (j = psl->sl_count-1; j >= i; j--) in ip_mc_source()
2429 psl->sl_addr[j+1] = psl->sl_addr[j]; in ip_mc_source()
2430 psl->sl_addr[i] = mreqs->imr_sourceaddr; in ip_mc_source()
2431 psl->sl_count++; in ip_mc_source()
2434 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, in ip_mc_source()
2435 &mreqs->imr_sourceaddr, 1); in ip_mc_source()
2446 __be32 addr = msf->imsf_multiaddr; in ip_mc_msfilter()
2455 return -EINVAL; in ip_mc_msfilter()
2456 if (msf->imsf_fmode != MCAST_INCLUDE && in ip_mc_msfilter()
2457 msf->imsf_fmode != MCAST_EXCLUDE) in ip_mc_msfilter()
2458 return -EINVAL; in ip_mc_msfilter()
2462 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; in ip_mc_msfilter()
2463 imr.imr_address.s_addr = msf->imsf_interface; in ip_mc_msfilter()
2468 err = -ENODEV; in ip_mc_msfilter()
2472 /* special case - (INCLUDE, empty) == LEAVE_GROUP */ in ip_mc_msfilter()
2473 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) { in ip_mc_msfilter()
2479 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && in ip_mc_msfilter()
2480 pmc->multi.imr_ifindex == imr.imr_ifindex) in ip_mc_msfilter()
2484 err = -EINVAL; in ip_mc_msfilter()
2487 if (msf->imsf_numsrc) { in ip_mc_msfilter()
2489 msf->imsf_numsrc), in ip_mc_msfilter()
2492 err = -ENOBUFS; in ip_mc_msfilter()
2495 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc; in ip_mc_msfilter()
2496 memcpy(newpsl->sl_addr, msf->imsf_slist_flex, in ip_mc_msfilter()
2497 flex_array_size(msf, imsf_slist_flex, msf->imsf_numsrc)); in ip_mc_msfilter()
2498 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr, in ip_mc_msfilter()
2499 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0); in ip_mc_msfilter()
2503 newpsl->sl_max)); in ip_mc_msfilter()
2508 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr, in ip_mc_msfilter()
2509 msf->imsf_fmode, 0, NULL, 0); in ip_mc_msfilter()
2511 psl = rtnl_dereference(pmc->sflist); in ip_mc_msfilter()
2513 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, in ip_mc_msfilter()
2514 psl->sl_count, psl->sl_addr, 0); in ip_mc_msfilter()
2516 atomic_sub(struct_size(psl, sl_addr, psl->sl_max), in ip_mc_msfilter()
2517 &sk->sk_omem_alloc); in ip_mc_msfilter()
2519 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, in ip_mc_msfilter()
2522 rcu_assign_pointer(pmc->sflist, newpsl); in ip_mc_msfilter()
2525 pmc->sfmode = msf->imsf_fmode; in ip_mc_msfilter()
2537 __be32 addr = msf->imsf_multiaddr; in ip_mc_msfget()
2547 return -EINVAL; in ip_mc_msfget()
2549 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; in ip_mc_msfget()
2550 imr.imr_address.s_addr = msf->imsf_interface; in ip_mc_msfget()
2555 err = -ENODEV; in ip_mc_msfget()
2558 err = -EADDRNOTAVAIL; in ip_mc_msfget()
2561 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && in ip_mc_msfget()
2562 pmc->multi.imr_ifindex == imr.imr_ifindex) in ip_mc_msfget()
2567 msf->imsf_fmode = pmc->sfmode; in ip_mc_msfget()
2568 psl = rtnl_dereference(pmc->sflist); in ip_mc_msfget()
2572 count = psl->sl_count; in ip_mc_msfget()
2574 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc; in ip_mc_msfget()
2576 msf->imsf_numsrc = count; in ip_mc_msfget()
2580 return -EFAULT; in ip_mc_msfget()
2585 psl->sl_addr, len)) in ip_mc_msfget()
2586 return -EFAULT; in ip_mc_msfget()
2604 psin = (struct sockaddr_in *)&gsf->gf_group; in ip_mc_gsfget()
2605 if (psin->sin_family != AF_INET) in ip_mc_gsfget()
2606 return -EINVAL; in ip_mc_gsfget()
2607 addr = psin->sin_addr.s_addr; in ip_mc_gsfget()
2609 return -EINVAL; in ip_mc_gsfget()
2612 if (pmc->multi.imr_multiaddr.s_addr == addr && in ip_mc_gsfget()
2613 pmc->multi.imr_ifindex == gsf->gf_interface) in ip_mc_gsfget()
2617 return -EADDRNOTAVAIL; in ip_mc_gsfget()
2618 gsf->gf_fmode = pmc->sfmode; in ip_mc_gsfget()
2619 psl = rtnl_dereference(pmc->sflist); in ip_mc_gsfget()
2620 count = psl ? psl->sl_count : 0; in ip_mc_gsfget()
2621 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc; in ip_mc_gsfget()
2622 gsf->gf_numsrc = count; in ip_mc_gsfget()
2628 psin->sin_family = AF_INET; in ip_mc_gsfget()
2629 psin->sin_addr.s_addr = psl->sl_addr[i]; in ip_mc_gsfget()
2632 return -EFAULT; in ip_mc_gsfget()
2656 if (pmc->multi.imr_multiaddr.s_addr == loc_addr && in ip_mc_sf_allow()
2657 (pmc->multi.imr_ifindex == dif || in ip_mc_sf_allow()
2658 (sdif && pmc->multi.imr_ifindex == sdif))) in ip_mc_sf_allow()
2661 ret = inet->mc_all; in ip_mc_sf_allow()
2664 psl = rcu_dereference(pmc->sflist); in ip_mc_sf_allow()
2665 ret = (pmc->sfmode == MCAST_EXCLUDE); in ip_mc_sf_allow()
2669 for (i = 0; i < psl->sl_count; i++) { in ip_mc_sf_allow()
2670 if (psl->sl_addr[i] == rmt_addr) in ip_mc_sf_allow()
2674 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count) in ip_mc_sf_allow()
2676 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count) in ip_mc_sf_allow()
2695 if (!inet->mc_list) in ip_mc_drop_socket()
2699 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) { in ip_mc_drop_socket()
2702 inet->mc_list = iml->next_rcu; in ip_mc_drop_socket()
2703 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex); in ip_mc_drop_socket()
2706 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr); in ip_mc_drop_socket()
2708 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); in ip_mc_drop_socket()
2717 struct ip_mc_list *im; in ip_check_mc_rcu() local
2722 mc_hash = rcu_dereference(in_dev->mc_hash); in ip_check_mc_rcu()
2726 for (im = rcu_dereference(mc_hash[hash]); in ip_check_mc_rcu()
2727 im != NULL; in ip_check_mc_rcu()
2728 im = rcu_dereference(im->next_hash)) { in ip_check_mc_rcu()
2729 if (im->multiaddr == mc_addr) in ip_check_mc_rcu()
2733 for_each_pmc_rcu(in_dev, im) { in ip_check_mc_rcu()
2734 if (im->multiaddr == mc_addr) in ip_check_mc_rcu()
2738 if (im && proto == IPPROTO_IGMP) { in ip_check_mc_rcu()
2740 } else if (im) { in ip_check_mc_rcu()
2742 spin_lock_bh(&im->lock); in ip_check_mc_rcu()
2743 for (psf = im->sources; psf; psf = psf->sf_next) { in ip_check_mc_rcu()
2744 if (psf->sf_inaddr == src_addr) in ip_check_mc_rcu()
2748 rv = psf->sf_count[MCAST_INCLUDE] || in ip_check_mc_rcu()
2749 psf->sf_count[MCAST_EXCLUDE] != in ip_check_mc_rcu()
2750 im->sfcount[MCAST_EXCLUDE]; in ip_check_mc_rcu()
2752 rv = im->sfcount[MCAST_EXCLUDE] != 0; in ip_check_mc_rcu()
2753 spin_unlock_bh(&im->lock); in ip_check_mc_rcu()
2767 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2772 struct ip_mc_list *im = NULL; in igmp_mc_get_first() local
2775 state->in_dev = NULL; in igmp_mc_get_first()
2776 for_each_netdev_rcu(net, state->dev) { in igmp_mc_get_first()
2779 in_dev = __in_dev_get_rcu(state->dev); in igmp_mc_get_first()
2782 im = rcu_dereference(in_dev->mc_list); in igmp_mc_get_first()
2783 if (im) { in igmp_mc_get_first()
2784 state->in_dev = in_dev; in igmp_mc_get_first()
2788 return im; in igmp_mc_get_first()
2791 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im) in igmp_mc_get_next() argument
2795 im = rcu_dereference(im->next_rcu); in igmp_mc_get_next()
2796 while (!im) { in igmp_mc_get_next()
2797 state->dev = next_net_device_rcu(state->dev); in igmp_mc_get_next()
2798 if (!state->dev) { in igmp_mc_get_next()
2799 state->in_dev = NULL; in igmp_mc_get_next()
2802 state->in_dev = __in_dev_get_rcu(state->dev); in igmp_mc_get_next()
2803 if (!state->in_dev) in igmp_mc_get_next()
2805 im = rcu_dereference(state->in_dev->mc_list); in igmp_mc_get_next()
2807 return im; in igmp_mc_get_next()
2812 struct ip_mc_list *im = igmp_mc_get_first(seq); in igmp_mc_get_idx() local
2813 if (im) in igmp_mc_get_idx()
2814 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL) in igmp_mc_get_idx()
2815 --pos; in igmp_mc_get_idx()
2816 return pos ? NULL : im; in igmp_mc_get_idx()
2823 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; in igmp_mc_seq_start()
2828 struct ip_mc_list *im; in igmp_mc_seq_next() local
2830 im = igmp_mc_get_first(seq); in igmp_mc_seq_next()
2832 im = igmp_mc_get_next(seq, v); in igmp_mc_seq_next()
2834 return im; in igmp_mc_seq_next()
2842 state->in_dev = NULL; in igmp_mc_seq_stop()
2843 state->dev = NULL; in igmp_mc_seq_stop()
2853 struct ip_mc_list *im = v; in igmp_mc_seq_show() local
2859 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" : in igmp_mc_seq_show()
2860 IGMP_V2_SEEN(state->in_dev) ? "V2" : in igmp_mc_seq_show()
2866 if (rcu_access_pointer(state->in_dev->mc_list) == im) { in igmp_mc_seq_show()
2867 seq_printf(seq, "%d\t%-10s: %5d %7s\n", in igmp_mc_seq_show()
2868 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier); in igmp_mc_seq_show()
2871 delta = im->timer.expires - jiffies; in igmp_mc_seq_show()
2874 im->multiaddr, im->users, in igmp_mc_seq_show()
2875 im->tm_running, in igmp_mc_seq_show()
2876 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0, in igmp_mc_seq_show()
2877 im->reporter); in igmp_mc_seq_show()
2893 struct ip_mc_list *im; member
2896 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2902 struct ip_mc_list *im = NULL; in igmp_mcf_get_first() local
2905 state->idev = NULL; in igmp_mcf_get_first()
2906 state->im = NULL; in igmp_mcf_get_first()
2907 for_each_netdev_rcu(net, state->dev) { in igmp_mcf_get_first()
2909 idev = __in_dev_get_rcu(state->dev); in igmp_mcf_get_first()
2912 im = rcu_dereference(idev->mc_list); in igmp_mcf_get_first()
2913 if (likely(im)) { in igmp_mcf_get_first()
2914 spin_lock_bh(&im->lock); in igmp_mcf_get_first()
2915 psf = im->sources; in igmp_mcf_get_first()
2917 state->im = im; in igmp_mcf_get_first()
2918 state->idev = idev; in igmp_mcf_get_first()
2921 spin_unlock_bh(&im->lock); in igmp_mcf_get_first()
2931 psf = psf->sf_next; in igmp_mcf_get_next()
2933 spin_unlock_bh(&state->im->lock); in igmp_mcf_get_next()
2934 state->im = state->im->next; in igmp_mcf_get_next()
2935 while (!state->im) { in igmp_mcf_get_next()
2936 state->dev = next_net_device_rcu(state->dev); in igmp_mcf_get_next()
2937 if (!state->dev) { in igmp_mcf_get_next()
2938 state->idev = NULL; in igmp_mcf_get_next()
2941 state->idev = __in_dev_get_rcu(state->dev); in igmp_mcf_get_next()
2942 if (!state->idev) in igmp_mcf_get_next()
2944 state->im = rcu_dereference(state->idev->mc_list); in igmp_mcf_get_next()
2946 if (!state->im) in igmp_mcf_get_next()
2948 spin_lock_bh(&state->im->lock); in igmp_mcf_get_next()
2949 psf = state->im->sources; in igmp_mcf_get_next()
2960 --pos; in igmp_mcf_get_idx()
2968 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; in igmp_mcf_seq_start()
2986 if (likely(state->im)) { in igmp_mcf_seq_stop()
2987 spin_unlock_bh(&state->im->lock); in igmp_mcf_seq_stop()
2988 state->im = NULL; in igmp_mcf_seq_stop()
2990 state->idev = NULL; in igmp_mcf_seq_stop()
2991 state->dev = NULL; in igmp_mcf_seq_stop()
3006 state->dev->ifindex, state->dev->name, in igmp_mcf_seq_show()
3007 ntohl(state->im->multiaddr), in igmp_mcf_seq_show()
3008 ntohl(psf->sf_inaddr), in igmp_mcf_seq_show()
3009 psf->sf_count[MCAST_INCLUDE], in igmp_mcf_seq_show()
3010 psf->sf_count[MCAST_EXCLUDE]); in igmp_mcf_seq_show()
3027 pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops, in igmp_net_init()
3031 pde = proc_create_net("mcfilter", 0444, net->proc_net, in igmp_net_init()
3035 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET, in igmp_net_init()
3046 remove_proc_entry("mcfilter", net->proc_net); in igmp_net_init()
3048 remove_proc_entry("igmp", net->proc_net); in igmp_net_init()
3050 return -ENOMEM; in igmp_net_init()
3055 remove_proc_entry("mcfilter", net->proc_net); in igmp_net_exit()
3056 remove_proc_entry("igmp", net->proc_net); in igmp_net_exit()
3057 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk); in igmp_net_exit()