1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux NET3: Internet Group Management Protocol [IGMP]
5 * This code implements the IGMP protocol as defined in RFC1112. There has
6 * been a further revision of this protocol since which is now supported.
8 * If you have trouble with this module be careful what gcc you have used,
9 * the older version didn't come out right using gcc 2.5.8, the newer one
10 * seems to fall out with gcc 2.6.2.
13 * Alan Cox <alan@lxorguk.ukuu.org.uk>
17 * Alan Cox : Added lots of __inline__ to optimise
18 * the memory usage of all the tiny little
20 * Alan Cox : Dumped the header building experiment.
21 * Alan Cox : Minor tweaks ready for multicast routing
22 * and extended IGMP protocol.
23 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
24 * writes utterly bogus code otherwise (sigh)
25 * fixed IGMP loopback to behave in the manner
26 * desired by mrouted, fixed the fact it has been
27 * broken since 1.3.6 and cleaned up a few minor
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
32 * The enhancements are mainly based on Steve Deering's
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
36 * the mrouted version on that device.
37 * Chih-Jen Chang : Added the max_resp_time parameter to
38 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
39 * to identify the multicast router version
40 * and do what the IGMP version 2 specified.
41 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
42 * Tsu-Sheng Tsao if the specified time expired.
43 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
44 * Alan Cox : Use GFP_ATOMIC in the right places.
45 * Christian Daudt : igmp timer wasn't set for local group
46 * memberships but was being deleted,
47 * which caused a "del_timer() called
48 * from %p with timer not initialized\n"
50 * Christian Daudt : removed del_timer from
51 * igmp_timer_expire function (960205).
52 * Christian Daudt : igmp_heard_report now only calls
53 * igmp_timer_expire if tm->running is
55 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
56 * igmp_heard_query never trigger. Expiry
57 * miscalculation fixed in igmp_heard_query
58 * and random() made to return unsigned to
59 * prevent negative expiry times.
60 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
61 * fix from pending 2.1.x patches.
62 * Alan Cox: Forget to enable FDDI support earlier.
63 * Alexey Kuznetsov: Fixed leaving groups on device down.
64 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
65 * David L Stevens: IGMPv3 support, with help from
69 #include <linux/module.h>
70 #include <linux/slab.h>
71 #include <linux/uaccess.h>
72 #include <linux/types.h>
73 #include <linux/kernel.h>
74 #include <linux/jiffies.h>
75 #include <linux/string.h>
76 #include <linux/socket.h>
77 #include <linux/sockios.h>
79 #include <linux/inet.h>
80 #include <linux/netdevice.h>
81 #include <linux/skbuff.h>
82 #include <linux/inetdevice.h>
83 #include <linux/igmp.h>
84 #include <linux/if_arp.h>
85 #include <linux/rtnetlink.h>
86 #include <linux/times.h>
87 #include <linux/pkt_sched.h>
88 #include <linux/byteorder/generic.h>
90 #include <net/net_namespace.h>
93 #include <net/protocol.h>
94 #include <net/route.h>
96 #include <net/checksum.h>
97 #include <net/inet_common.h>
98 #include <linux/netfilter_ipv4.h>
99 #ifdef CONFIG_IP_MROUTE
100 #include <linux/mroute.h>
102 #ifdef CONFIG_PROC_FS
103 #include <linux/proc_fs.h>
104 #include <linux/seq_file.h>
107 #ifdef CONFIG_IP_MULTICAST
108 /* Parameter names and values are taken from igmp-v2-06 draft */
110 #define IGMP_QUERY_INTERVAL (125*HZ)
111 #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
113 #define IGMP_INITIAL_REPORT_DELAY (1)
115 /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
116 * IGMP specs require to report membership immediately after
117 * joining a group, but we delay the first report by a
118 * small interval. It seems more natural and still does not
119 * contradict to specs provided this delay is small enough.
122 #define IGMP_V1_SEEN(in_dev) \
123 (IPV4_DEVCONF_ALL_RO(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
124 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
125 ((in_dev)->mr_v1_seen && \
126 time_before(jiffies, (in_dev)->mr_v1_seen)))
127 #define IGMP_V2_SEEN(in_dev) \
128 (IPV4_DEVCONF_ALL_RO(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
129 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
130 ((in_dev)->mr_v2_seen && \
131 time_before(jiffies, (in_dev)->mr_v2_seen)))
133 static int unsolicited_report_interval(struct in_device
*in_dev
)
135 int interval_ms
, interval_jiffies
;
137 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
138 interval_ms
= IN_DEV_CONF_GET(
140 IGMPV2_UNSOLICITED_REPORT_INTERVAL
);
142 interval_ms
= IN_DEV_CONF_GET(
144 IGMPV3_UNSOLICITED_REPORT_INTERVAL
);
146 interval_jiffies
= msecs_to_jiffies(interval_ms
);
148 /* _timer functions can't handle a delay of 0 jiffies so ensure
149 * we always return a positive value.
151 if (interval_jiffies
<= 0)
152 interval_jiffies
= 1;
153 return interval_jiffies
;
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
);
159 static void igmpv3_clear_delrec(struct in_device
*in_dev
);
160 static int sf_setstate(struct ip_mc_list
*pmc
);
161 static void sf_markstate(struct ip_mc_list
*pmc
);
163 static void ip_mc_clear_src(struct ip_mc_list
*pmc
);
164 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
165 int sfcount
, __be32
*psfsrc
, int delta
);
167 static void ip_ma_put(struct ip_mc_list
*im
)
169 if (refcount_dec_and_test(&im
->refcnt
)) {
170 in_dev_put(im
->interface
);
175 #define for_each_pmc_rcu(in_dev, pmc) \
176 for (pmc = rcu_dereference(in_dev->mc_list); \
178 pmc = rcu_dereference(pmc->next_rcu))
180 #define for_each_pmc_rtnl(in_dev, pmc) \
181 for (pmc = rtnl_dereference(in_dev->mc_list); \
183 pmc = rtnl_dereference(pmc->next_rcu))
185 static void ip_sf_list_clear_all(struct ip_sf_list
*psf
)
187 struct ip_sf_list
*next
;
196 #ifdef CONFIG_IP_MULTICAST
202 static void igmp_stop_timer(struct ip_mc_list
*im
)
204 spin_lock_bh(&im
->lock
);
205 if (del_timer(&im
->timer
))
206 refcount_dec(&im
->refcnt
);
209 im
->unsolicit_count
= 0;
210 spin_unlock_bh(&im
->lock
);
213 /* It must be called with locked im->lock */
214 static void igmp_start_timer(struct ip_mc_list
*im
, int max_delay
)
216 int tv
= get_random_u32_below(max_delay
);
219 if (refcount_inc_not_zero(&im
->refcnt
)) {
220 if (mod_timer(&im
->timer
, jiffies
+ tv
+ 2))
225 static void igmp_gq_start_timer(struct in_device
*in_dev
)
227 int tv
= get_random_u32_below(in_dev
->mr_maxdelay
);
228 unsigned long exp
= jiffies
+ tv
+ 2;
230 if (in_dev
->mr_gq_running
&&
231 time_after_eq(exp
, (in_dev
->mr_gq_timer
).expires
))
234 in_dev
->mr_gq_running
= 1;
235 if (!mod_timer(&in_dev
->mr_gq_timer
, exp
))
239 static void igmp_ifc_start_timer(struct in_device
*in_dev
, int delay
)
241 int tv
= get_random_u32_below(delay
);
243 if (!mod_timer(&in_dev
->mr_ifc_timer
, jiffies
+tv
+2))
247 static void igmp_mod_timer(struct ip_mc_list
*im
, int max_delay
)
249 spin_lock_bh(&im
->lock
);
250 im
->unsolicit_count
= 0;
251 if (del_timer(&im
->timer
)) {
252 if ((long)(im
->timer
.expires
-jiffies
) < max_delay
) {
253 add_timer(&im
->timer
);
255 spin_unlock_bh(&im
->lock
);
258 refcount_dec(&im
->refcnt
);
260 igmp_start_timer(im
, max_delay
);
261 spin_unlock_bh(&im
->lock
);
266 * Send an IGMP report.
269 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
272 static int is_in(struct ip_mc_list
*pmc
, struct ip_sf_list
*psf
, int type
,
273 int gdeleted
, int sdeleted
)
276 case IGMPV3_MODE_IS_INCLUDE
:
277 case IGMPV3_MODE_IS_EXCLUDE
:
278 if (gdeleted
|| sdeleted
)
280 if (!(pmc
->gsquery
&& !psf
->sf_gsresp
)) {
281 if (pmc
->sfmode
== MCAST_INCLUDE
)
283 /* don't include if this source is excluded
286 if (psf
->sf_count
[MCAST_INCLUDE
])
287 return type
== IGMPV3_MODE_IS_INCLUDE
;
288 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
289 psf
->sf_count
[MCAST_EXCLUDE
];
292 case IGMPV3_CHANGE_TO_INCLUDE
:
293 if (gdeleted
|| sdeleted
)
295 return psf
->sf_count
[MCAST_INCLUDE
] != 0;
296 case IGMPV3_CHANGE_TO_EXCLUDE
:
297 if (gdeleted
|| sdeleted
)
299 if (pmc
->sfcount
[MCAST_EXCLUDE
] == 0 ||
300 psf
->sf_count
[MCAST_INCLUDE
])
302 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
303 psf
->sf_count
[MCAST_EXCLUDE
];
304 case IGMPV3_ALLOW_NEW_SOURCES
:
305 if (gdeleted
|| !psf
->sf_crcount
)
307 return (pmc
->sfmode
== MCAST_INCLUDE
) ^ sdeleted
;
308 case IGMPV3_BLOCK_OLD_SOURCES
:
309 if (pmc
->sfmode
== MCAST_INCLUDE
)
310 return gdeleted
|| (psf
->sf_crcount
&& sdeleted
);
311 return psf
->sf_crcount
&& !gdeleted
&& !sdeleted
;
317 igmp_scount(struct ip_mc_list
*pmc
, int type
, int gdeleted
, int sdeleted
)
319 struct ip_sf_list
*psf
;
322 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
323 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
))
330 /* source address selection per RFC 3376 section 4.2.13 */
331 static __be32
igmpv3_get_srcaddr(struct net_device
*dev
,
332 const struct flowi4
*fl4
)
334 struct in_device
*in_dev
= __in_dev_get_rcu(dev
);
335 const struct in_ifaddr
*ifa
;
338 return htonl(INADDR_ANY
);
340 in_dev_for_each_ifa_rcu(ifa
, in_dev
) {
341 if (fl4
->saddr
== ifa
->ifa_local
)
345 return htonl(INADDR_ANY
);
348 static struct sk_buff
*igmpv3_newpack(struct net_device
*dev
, unsigned int mtu
)
353 struct igmpv3_report
*pig
;
354 struct net
*net
= dev_net(dev
);
356 int hlen
= LL_RESERVED_SPACE(dev
);
357 int tlen
= dev
->needed_tailroom
;
360 size
= min(mtu
, IP_MAX_MTU
);
362 skb
= alloc_skb(size
+ hlen
+ tlen
,
363 GFP_ATOMIC
| __GFP_NOWARN
);
370 skb
->priority
= TC_PRIO_CONTROL
;
372 rt
= ip_route_output_ports(net
, &fl4
, NULL
, IGMPV3_ALL_MCR
, 0,
374 IPPROTO_IGMP
, 0, dev
->ifindex
);
380 skb_dst_set(skb
, &rt
->dst
);
383 skb_reserve(skb
, hlen
);
384 skb_tailroom_reserve(skb
, mtu
, tlen
);
386 skb_reset_network_header(skb
);
388 skb_put(skb
, sizeof(struct iphdr
) + 4);
391 pip
->ihl
= (sizeof(struct iphdr
)+4)>>2;
393 pip
->frag_off
= htons(IP_DF
);
395 pip
->daddr
= fl4
.daddr
;
398 pip
->saddr
= igmpv3_get_srcaddr(dev
, &fl4
);
401 pip
->protocol
= IPPROTO_IGMP
;
402 pip
->tot_len
= 0; /* filled in later */
403 ip_select_ident(net
, skb
, NULL
);
404 ((u8
*)&pip
[1])[0] = IPOPT_RA
;
405 ((u8
*)&pip
[1])[1] = 4;
406 ((u8
*)&pip
[1])[2] = 0;
407 ((u8
*)&pip
[1])[3] = 0;
409 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
) + 4;
410 skb_put(skb
, sizeof(*pig
));
411 pig
= igmpv3_report_hdr(skb
);
412 pig
->type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
420 static int igmpv3_sendpack(struct sk_buff
*skb
)
422 struct igmphdr
*pig
= igmp_hdr(skb
);
423 const int igmplen
= skb_tail_pointer(skb
) - skb_transport_header(skb
);
425 pig
->csum
= ip_compute_csum(igmp_hdr(skb
), igmplen
);
427 return ip_local_out(dev_net(skb_dst(skb
)->dev
), skb
->sk
, skb
);
430 static int grec_size(struct ip_mc_list
*pmc
, int type
, int gdel
, int sdel
)
432 return sizeof(struct igmpv3_grec
) + 4*igmp_scount(pmc
, type
, gdel
, sdel
);
435 static struct sk_buff
*add_grhead(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
436 int type
, struct igmpv3_grec
**ppgr
, unsigned int mtu
)
438 struct net_device
*dev
= pmc
->interface
->dev
;
439 struct igmpv3_report
*pih
;
440 struct igmpv3_grec
*pgr
;
443 skb
= igmpv3_newpack(dev
, mtu
);
447 pgr
= skb_put(skb
, sizeof(struct igmpv3_grec
));
448 pgr
->grec_type
= type
;
449 pgr
->grec_auxwords
= 0;
451 pgr
->grec_mca
= pmc
->multiaddr
;
452 pih
= igmpv3_report_hdr(skb
);
453 pih
->ngrec
= htons(ntohs(pih
->ngrec
)+1);
458 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
460 static struct sk_buff
*add_grec(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
461 int type
, int gdeleted
, int sdeleted
)
463 struct net_device
*dev
= pmc
->interface
->dev
;
464 struct net
*net
= dev_net(dev
);
465 struct igmpv3_report
*pih
;
466 struct igmpv3_grec
*pgr
= NULL
;
467 struct ip_sf_list
*psf
, *psf_next
, *psf_prev
, **psf_list
;
468 int scount
, stotal
, first
, isquery
, truncate
;
471 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
473 if (ipv4_is_local_multicast(pmc
->multiaddr
) &&
474 !READ_ONCE(net
->ipv4
.sysctl_igmp_llm_reports
))
477 mtu
= READ_ONCE(dev
->mtu
);
478 if (mtu
< IPV4_MIN_MTU
)
481 isquery
= type
== IGMPV3_MODE_IS_INCLUDE
||
482 type
== IGMPV3_MODE_IS_EXCLUDE
;
483 truncate
= type
== IGMPV3_MODE_IS_EXCLUDE
||
484 type
== IGMPV3_CHANGE_TO_EXCLUDE
;
488 psf_list
= sdeleted
? &pmc
->tomb
: &pmc
->sources
;
493 pih
= skb
? igmpv3_report_hdr(skb
) : NULL
;
495 /* EX and TO_EX get a fresh packet, if needed */
497 if (pih
&& pih
->ngrec
&&
498 AVAILABLE(skb
) < grec_size(pmc
, type
, gdeleted
, sdeleted
)) {
500 igmpv3_sendpack(skb
);
501 skb
= igmpv3_newpack(dev
, mtu
);
506 for (psf
= *psf_list
; psf
; psf
= psf_next
) {
509 psf_next
= psf
->sf_next
;
511 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
)) {
516 /* Based on RFC3376 5.1. Should not send source-list change
517 * records when there is a filter mode change.
519 if (((gdeleted
&& pmc
->sfmode
== MCAST_EXCLUDE
) ||
520 (!gdeleted
&& pmc
->crcount
)) &&
521 (type
== IGMPV3_ALLOW_NEW_SOURCES
||
522 type
== IGMPV3_BLOCK_OLD_SOURCES
) && psf
->sf_crcount
)
523 goto decrease_sf_crcount
;
525 /* clear marks on query responses */
529 if (AVAILABLE(skb
) < sizeof(__be32
) +
530 first
*sizeof(struct igmpv3_grec
)) {
531 if (truncate
&& !first
)
532 break; /* truncate these */
534 pgr
->grec_nsrcs
= htons(scount
);
536 igmpv3_sendpack(skb
);
537 skb
= igmpv3_newpack(dev
, mtu
);
542 skb
= add_grhead(skb
, pmc
, type
, &pgr
, mtu
);
547 psrc
= skb_put(skb
, sizeof(__be32
));
548 *psrc
= psf
->sf_inaddr
;
550 if ((type
== IGMPV3_ALLOW_NEW_SOURCES
||
551 type
== IGMPV3_BLOCK_OLD_SOURCES
) && psf
->sf_crcount
) {
554 if ((sdeleted
|| gdeleted
) && psf
->sf_crcount
== 0) {
556 psf_prev
->sf_next
= psf
->sf_next
;
558 *psf_list
= psf
->sf_next
;
568 if (type
== IGMPV3_ALLOW_NEW_SOURCES
||
569 type
== IGMPV3_BLOCK_OLD_SOURCES
)
571 if (pmc
->crcount
|| isquery
) {
572 /* make sure we have room for group header */
573 if (skb
&& AVAILABLE(skb
) < sizeof(struct igmpv3_grec
)) {
574 igmpv3_sendpack(skb
);
575 skb
= NULL
; /* add_grhead will get a new one */
577 skb
= add_grhead(skb
, pmc
, type
, &pgr
, mtu
);
581 pgr
->grec_nsrcs
= htons(scount
);
584 pmc
->gsquery
= 0; /* clear query state on report */
588 static int igmpv3_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
)
590 struct sk_buff
*skb
= NULL
;
591 struct net
*net
= dev_net(in_dev
->dev
);
596 for_each_pmc_rcu(in_dev
, pmc
) {
597 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
599 if (ipv4_is_local_multicast(pmc
->multiaddr
) &&
600 !READ_ONCE(net
->ipv4
.sysctl_igmp_llm_reports
))
602 spin_lock_bh(&pmc
->lock
);
603 if (pmc
->sfcount
[MCAST_EXCLUDE
])
604 type
= IGMPV3_MODE_IS_EXCLUDE
;
606 type
= IGMPV3_MODE_IS_INCLUDE
;
607 skb
= add_grec(skb
, pmc
, type
, 0, 0);
608 spin_unlock_bh(&pmc
->lock
);
612 spin_lock_bh(&pmc
->lock
);
613 if (pmc
->sfcount
[MCAST_EXCLUDE
])
614 type
= IGMPV3_MODE_IS_EXCLUDE
;
616 type
= IGMPV3_MODE_IS_INCLUDE
;
617 skb
= add_grec(skb
, pmc
, type
, 0, 0);
618 spin_unlock_bh(&pmc
->lock
);
622 return igmpv3_sendpack(skb
);
626 * remove zero-count source records from a source filter list
628 static void igmpv3_clear_zeros(struct ip_sf_list
**ppsf
)
630 struct ip_sf_list
*psf_prev
, *psf_next
, *psf
;
633 for (psf
= *ppsf
; psf
; psf
= psf_next
) {
634 psf_next
= psf
->sf_next
;
635 if (psf
->sf_crcount
== 0) {
637 psf_prev
->sf_next
= psf
->sf_next
;
639 *ppsf
= psf
->sf_next
;
646 static void kfree_pmc(struct ip_mc_list
*pmc
)
648 ip_sf_list_clear_all(pmc
->sources
);
649 ip_sf_list_clear_all(pmc
->tomb
);
653 static void igmpv3_send_cr(struct in_device
*in_dev
)
655 struct ip_mc_list
*pmc
, *pmc_prev
, *pmc_next
;
656 struct sk_buff
*skb
= NULL
;
660 spin_lock_bh(&in_dev
->mc_tomb_lock
);
664 for (pmc
= in_dev
->mc_tomb
; pmc
; pmc
= pmc_next
) {
665 pmc_next
= pmc
->next
;
666 if (pmc
->sfmode
== MCAST_INCLUDE
) {
667 type
= IGMPV3_BLOCK_OLD_SOURCES
;
668 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
669 skb
= add_grec(skb
, pmc
, type
, 1, 0);
670 skb
= add_grec(skb
, pmc
, dtype
, 1, 1);
673 if (pmc
->sfmode
== MCAST_EXCLUDE
) {
674 type
= IGMPV3_CHANGE_TO_INCLUDE
;
675 skb
= add_grec(skb
, pmc
, type
, 1, 0);
678 if (pmc
->crcount
== 0) {
679 igmpv3_clear_zeros(&pmc
->tomb
);
680 igmpv3_clear_zeros(&pmc
->sources
);
683 if (pmc
->crcount
== 0 && !pmc
->tomb
&& !pmc
->sources
) {
685 pmc_prev
->next
= pmc_next
;
687 in_dev
->mc_tomb
= pmc_next
;
688 in_dev_put(pmc
->interface
);
693 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
696 for_each_pmc_rcu(in_dev
, pmc
) {
697 spin_lock_bh(&pmc
->lock
);
698 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
699 type
= IGMPV3_BLOCK_OLD_SOURCES
;
700 dtype
= IGMPV3_ALLOW_NEW_SOURCES
;
702 type
= IGMPV3_ALLOW_NEW_SOURCES
;
703 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
705 skb
= add_grec(skb
, pmc
, type
, 0, 0);
706 skb
= add_grec(skb
, pmc
, dtype
, 0, 1); /* deleted sources */
708 /* filter mode changes */
710 if (pmc
->sfmode
== MCAST_EXCLUDE
)
711 type
= IGMPV3_CHANGE_TO_EXCLUDE
;
713 type
= IGMPV3_CHANGE_TO_INCLUDE
;
714 skb
= add_grec(skb
, pmc
, type
, 0, 0);
717 spin_unlock_bh(&pmc
->lock
);
723 (void) igmpv3_sendpack(skb
);
726 static int igmp_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
,
733 struct net_device
*dev
= in_dev
->dev
;
734 struct net
*net
= dev_net(dev
);
735 __be32 group
= pmc
? pmc
->multiaddr
: 0;
740 if (type
== IGMPV3_HOST_MEMBERSHIP_REPORT
)
741 return igmpv3_send_report(in_dev
, pmc
);
743 if (ipv4_is_local_multicast(group
) &&
744 !READ_ONCE(net
->ipv4
.sysctl_igmp_llm_reports
))
747 if (type
== IGMP_HOST_LEAVE_MESSAGE
)
748 dst
= IGMP_ALL_ROUTER
;
752 rt
= ip_route_output_ports(net
, &fl4
, NULL
, dst
, 0,
754 IPPROTO_IGMP
, 0, dev
->ifindex
);
758 hlen
= LL_RESERVED_SPACE(dev
);
759 tlen
= dev
->needed_tailroom
;
760 skb
= alloc_skb(IGMP_SIZE
+ hlen
+ tlen
, GFP_ATOMIC
);
765 skb
->priority
= TC_PRIO_CONTROL
;
767 skb_dst_set(skb
, &rt
->dst
);
769 skb_reserve(skb
, hlen
);
771 skb_reset_network_header(skb
);
773 skb_put(skb
, sizeof(struct iphdr
) + 4);
776 iph
->ihl
= (sizeof(struct iphdr
)+4)>>2;
778 iph
->frag_off
= htons(IP_DF
);
781 iph
->saddr
= fl4
.saddr
;
782 iph
->protocol
= IPPROTO_IGMP
;
783 ip_select_ident(net
, skb
, NULL
);
784 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
785 ((u8
*)&iph
[1])[1] = 4;
786 ((u8
*)&iph
[1])[2] = 0;
787 ((u8
*)&iph
[1])[3] = 0;
789 ih
= skb_put(skb
, sizeof(struct igmphdr
));
794 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
796 return ip_local_out(net
, skb
->sk
, skb
);
799 static void igmp_gq_timer_expire(struct timer_list
*t
)
801 struct in_device
*in_dev
= from_timer(in_dev
, t
, mr_gq_timer
);
803 in_dev
->mr_gq_running
= 0;
804 igmpv3_send_report(in_dev
, NULL
);
808 static void igmp_ifc_timer_expire(struct timer_list
*t
)
810 struct in_device
*in_dev
= from_timer(in_dev
, t
, mr_ifc_timer
);
813 igmpv3_send_cr(in_dev
);
815 mr_ifc_count
= READ_ONCE(in_dev
->mr_ifc_count
);
818 if (cmpxchg(&in_dev
->mr_ifc_count
,
820 mr_ifc_count
- 1) != mr_ifc_count
)
822 igmp_ifc_start_timer(in_dev
,
823 unsolicited_report_interval(in_dev
));
828 static void igmp_ifc_event(struct in_device
*in_dev
)
830 struct net
*net
= dev_net(in_dev
->dev
);
831 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
833 WRITE_ONCE(in_dev
->mr_ifc_count
, in_dev
->mr_qrv
?: READ_ONCE(net
->ipv4
.sysctl_igmp_qrv
));
834 igmp_ifc_start_timer(in_dev
, 1);
838 static void igmp_timer_expire(struct timer_list
*t
)
840 struct ip_mc_list
*im
= from_timer(im
, t
, timer
);
841 struct in_device
*in_dev
= im
->interface
;
843 spin_lock(&im
->lock
);
846 if (im
->unsolicit_count
&& --im
->unsolicit_count
)
847 igmp_start_timer(im
, unsolicited_report_interval(in_dev
));
850 spin_unlock(&im
->lock
);
852 if (IGMP_V1_SEEN(in_dev
))
853 igmp_send_report(in_dev
, im
, IGMP_HOST_MEMBERSHIP_REPORT
);
854 else if (IGMP_V2_SEEN(in_dev
))
855 igmp_send_report(in_dev
, im
, IGMPV2_HOST_MEMBERSHIP_REPORT
);
857 igmp_send_report(in_dev
, im
, IGMPV3_HOST_MEMBERSHIP_REPORT
);
862 /* mark EXCLUDE-mode sources */
863 static int igmp_xmarksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
865 struct ip_sf_list
*psf
;
869 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
872 for (i
= 0; i
< nsrcs
; i
++) {
873 /* skip inactive filters */
874 if (psf
->sf_count
[MCAST_INCLUDE
] ||
875 pmc
->sfcount
[MCAST_EXCLUDE
] !=
876 psf
->sf_count
[MCAST_EXCLUDE
])
878 if (srcs
[i
] == psf
->sf_inaddr
) {
885 if (scount
== nsrcs
) /* all sources excluded */
890 static int igmp_marksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
892 struct ip_sf_list
*psf
;
895 if (pmc
->sfmode
== MCAST_EXCLUDE
)
896 return igmp_xmarksources(pmc
, nsrcs
, srcs
);
898 /* mark INCLUDE-mode sources */
900 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
903 for (i
= 0; i
< nsrcs
; i
++)
904 if (srcs
[i
] == psf
->sf_inaddr
) {
918 /* return true if packet was dropped */
919 static bool igmp_heard_report(struct in_device
*in_dev
, __be32 group
)
921 struct ip_mc_list
*im
;
922 struct net
*net
= dev_net(in_dev
->dev
);
924 /* Timers are only set for non-local groups */
926 if (group
== IGMP_ALL_HOSTS
)
928 if (ipv4_is_local_multicast(group
) &&
929 !READ_ONCE(net
->ipv4
.sysctl_igmp_llm_reports
))
933 for_each_pmc_rcu(in_dev
, im
) {
934 if (im
->multiaddr
== group
) {
943 /* return true if packet was dropped */
944 static bool igmp_heard_query(struct in_device
*in_dev
, struct sk_buff
*skb
,
947 struct igmphdr
*ih
= igmp_hdr(skb
);
948 struct igmpv3_query
*ih3
= igmpv3_query_hdr(skb
);
949 struct ip_mc_list
*im
;
950 __be32 group
= ih
->group
;
953 struct net
*net
= dev_net(in_dev
->dev
);
958 /* Alas, old v1 router presents here. */
960 max_delay
= IGMP_QUERY_RESPONSE_INTERVAL
;
961 in_dev
->mr_v1_seen
= jiffies
+
962 (in_dev
->mr_qrv
* in_dev
->mr_qi
) +
966 /* v2 router present */
967 max_delay
= ih
->code
*(HZ
/IGMP_TIMER_SCALE
);
968 in_dev
->mr_v2_seen
= jiffies
+
969 (in_dev
->mr_qrv
* in_dev
->mr_qi
) +
972 /* cancel the interface change timer */
973 WRITE_ONCE(in_dev
->mr_ifc_count
, 0);
974 if (del_timer(&in_dev
->mr_ifc_timer
))
975 __in_dev_put(in_dev
);
976 /* clear deleted report items */
977 igmpv3_clear_delrec(in_dev
);
978 } else if (len
< 12) {
979 return true; /* ignore bogus packet; freed by caller */
980 } else if (IGMP_V1_SEEN(in_dev
)) {
981 /* This is a v3 query with v1 queriers present */
982 max_delay
= IGMP_QUERY_RESPONSE_INTERVAL
;
984 } else if (IGMP_V2_SEEN(in_dev
)) {
985 /* this is a v3 query with v2 queriers present;
986 * Interpretation of the max_delay code is problematic here.
987 * A real v2 host would use ih_code directly, while v3 has a
988 * different encoding. We use the v3 encoding as more likely
989 * to be intended in a v3 query.
991 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
993 max_delay
= 1; /* can't mod w/ 0 */
995 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)))
998 ih3
= igmpv3_query_hdr(skb
);
1000 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)
1001 + ntohs(ih3
->nsrcs
)*sizeof(__be32
)))
1003 ih3
= igmpv3_query_hdr(skb
);
1006 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
1008 max_delay
= 1; /* can't mod w/ 0 */
1009 in_dev
->mr_maxdelay
= max_delay
;
1011 /* RFC3376, 4.1.6. QRV and 4.1.7. QQIC, when the most recently
1012 * received value was zero, use the default or statically
1015 in_dev
->mr_qrv
= ih3
->qrv
?: READ_ONCE(net
->ipv4
.sysctl_igmp_qrv
);
1016 in_dev
->mr_qi
= IGMPV3_QQIC(ih3
->qqic
)*HZ
?: IGMP_QUERY_INTERVAL
;
1018 /* RFC3376, 8.3. Query Response Interval:
1019 * The number of seconds represented by the [Query Response
1020 * Interval] must be less than the [Query Interval].
1022 if (in_dev
->mr_qri
>= in_dev
->mr_qi
)
1023 in_dev
->mr_qri
= (in_dev
->mr_qi
/HZ
- 1)*HZ
;
1025 if (!group
) { /* general query */
1027 return true; /* no sources allowed */
1028 igmp_gq_start_timer(in_dev
);
1031 /* mark sources to include, if group & source-specific */
1032 mark
= ih3
->nsrcs
!= 0;
1036 * - Start the timers in all of our membership records
1037 * that the query applies to for the interface on
1038 * which the query arrived excl. those that belong
1039 * to a "local" group (224.0.0.X)
1040 * - For timers already running check if they need to
1042 * - Use the igmp->igmp_code field as the maximum
1046 for_each_pmc_rcu(in_dev
, im
) {
1049 if (group
&& group
!= im
->multiaddr
)
1051 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1053 if (ipv4_is_local_multicast(im
->multiaddr
) &&
1054 !READ_ONCE(net
->ipv4
.sysctl_igmp_llm_reports
))
1056 spin_lock_bh(&im
->lock
);
1058 im
->gsquery
= im
->gsquery
&& mark
;
1061 changed
= !im
->gsquery
||
1062 igmp_marksources(im
, ntohs(ih3
->nsrcs
), ih3
->srcs
);
1063 spin_unlock_bh(&im
->lock
);
1065 igmp_mod_timer(im
, max_delay
);
1071 /* called in rcu_read_lock() section */
1072 int igmp_rcv(struct sk_buff
*skb
)
1074 /* This basically follows the spec line by line -- see RFC1112 */
1076 struct net_device
*dev
= skb
->dev
;
1077 struct in_device
*in_dev
;
1079 bool dropped
= true;
1081 if (netif_is_l3_master(dev
)) {
1082 dev
= dev_get_by_index_rcu(dev_net(dev
), IPCB(skb
)->iif
);
1087 in_dev
= __in_dev_get_rcu(dev
);
1091 if (!pskb_may_pull(skb
, sizeof(struct igmphdr
)))
1094 if (skb_checksum_simple_validate(skb
))
1099 case IGMP_HOST_MEMBERSHIP_QUERY
:
1100 dropped
= igmp_heard_query(in_dev
, skb
, len
);
1102 case IGMP_HOST_MEMBERSHIP_REPORT
:
1103 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1104 /* Is it our report looped back? */
1105 if (rt_is_output_route(skb_rtable(skb
)))
1107 /* don't rely on MC router hearing unicast reports */
1108 if (skb
->pkt_type
== PACKET_MULTICAST
||
1109 skb
->pkt_type
== PACKET_BROADCAST
)
1110 dropped
= igmp_heard_report(in_dev
, ih
->group
);
1113 #ifdef CONFIG_IP_PIMSM_V1
1114 return pim_rcv_v1(skb
);
1116 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1119 case IGMP_HOST_LEAVE_MESSAGE
:
1121 case IGMP_MTRACE_RESP
:
1139 * Add a filter to a device
1142 static void ip_mc_filter_add(struct in_device
*in_dev
, __be32 addr
)
1144 char buf
[MAX_ADDR_LEN
];
1145 struct net_device
*dev
= in_dev
->dev
;
1147 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1148 We will get multicast token leakage, when IFF_MULTICAST
1149 is changed. This check should be done in ndo_set_rx_mode
1150 routine. Something sort of:
1151 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1154 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1155 dev_mc_add(dev
, buf
);
1159 * Remove a filter from a device
1162 static void ip_mc_filter_del(struct in_device
*in_dev
, __be32 addr
)
1164 char buf
[MAX_ADDR_LEN
];
1165 struct net_device
*dev
= in_dev
->dev
;
1167 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1168 dev_mc_del(dev
, buf
);
1171 #ifdef CONFIG_IP_MULTICAST
1173 * deleted ip_mc_list manipulation
1175 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
,
1178 struct ip_mc_list
*pmc
;
1179 struct net
*net
= dev_net(in_dev
->dev
);
1181 /* this is an "ip_mc_list" for convenience; only the fields below
1182 * are actually used. In particular, the refcnt and users are not
1183 * used for management of the delete list. Using the same structure
1184 * for deleted items allows change reports to use common code with
1185 * non-deleted or query-response MCA's.
1187 pmc
= kzalloc(sizeof(*pmc
), gfp
);
1190 spin_lock_init(&pmc
->lock
);
1191 spin_lock_bh(&im
->lock
);
1192 pmc
->interface
= im
->interface
;
1193 in_dev_hold(in_dev
);
1194 pmc
->multiaddr
= im
->multiaddr
;
1195 pmc
->crcount
= in_dev
->mr_qrv
?: READ_ONCE(net
->ipv4
.sysctl_igmp_qrv
);
1196 pmc
->sfmode
= im
->sfmode
;
1197 if (pmc
->sfmode
== MCAST_INCLUDE
) {
1198 struct ip_sf_list
*psf
;
1200 pmc
->tomb
= im
->tomb
;
1201 pmc
->sources
= im
->sources
;
1202 im
->tomb
= im
->sources
= NULL
;
1203 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1204 psf
->sf_crcount
= pmc
->crcount
;
1206 spin_unlock_bh(&im
->lock
);
1208 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1209 pmc
->next
= in_dev
->mc_tomb
;
1210 in_dev
->mc_tomb
= pmc
;
1211 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1215 * restore ip_mc_list deleted records
1217 static void igmpv3_del_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
)
1219 struct ip_mc_list
*pmc
, *pmc_prev
;
1220 struct ip_sf_list
*psf
;
1221 struct net
*net
= dev_net(in_dev
->dev
);
1222 __be32 multiaddr
= im
->multiaddr
;
1224 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1226 for (pmc
= in_dev
->mc_tomb
; pmc
; pmc
= pmc
->next
) {
1227 if (pmc
->multiaddr
== multiaddr
)
1233 pmc_prev
->next
= pmc
->next
;
1235 in_dev
->mc_tomb
= pmc
->next
;
1237 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1239 spin_lock_bh(&im
->lock
);
1241 im
->interface
= pmc
->interface
;
1242 if (im
->sfmode
== MCAST_INCLUDE
) {
1243 swap(im
->tomb
, pmc
->tomb
);
1244 swap(im
->sources
, pmc
->sources
);
1245 for (psf
= im
->sources
; psf
; psf
= psf
->sf_next
)
1246 psf
->sf_crcount
= in_dev
->mr_qrv
?:
1247 READ_ONCE(net
->ipv4
.sysctl_igmp_qrv
);
1249 im
->crcount
= in_dev
->mr_qrv
?:
1250 READ_ONCE(net
->ipv4
.sysctl_igmp_qrv
);
1252 in_dev_put(pmc
->interface
);
1255 spin_unlock_bh(&im
->lock
);
1259 * flush ip_mc_list deleted records
1261 static void igmpv3_clear_delrec(struct in_device
*in_dev
)
1263 struct ip_mc_list
*pmc
, *nextpmc
;
1265 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1266 pmc
= in_dev
->mc_tomb
;
1267 in_dev
->mc_tomb
= NULL
;
1268 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1270 for (; pmc
; pmc
= nextpmc
) {
1271 nextpmc
= pmc
->next
;
1272 ip_mc_clear_src(pmc
);
1273 in_dev_put(pmc
->interface
);
1276 /* clear dead sources, too */
1278 for_each_pmc_rcu(in_dev
, pmc
) {
1279 struct ip_sf_list
*psf
;
1281 spin_lock_bh(&pmc
->lock
);
1284 spin_unlock_bh(&pmc
->lock
);
1285 ip_sf_list_clear_all(psf
);
1291 static void __igmp_group_dropped(struct ip_mc_list
*im
, gfp_t gfp
)
1293 struct in_device
*in_dev
= im
->interface
;
1294 #ifdef CONFIG_IP_MULTICAST
1295 struct net
*net
= dev_net(in_dev
->dev
);
1301 ip_mc_filter_del(in_dev
, im
->multiaddr
);
1304 #ifdef CONFIG_IP_MULTICAST
1305 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1307 if (ipv4_is_local_multicast(im
->multiaddr
) &&
1308 !READ_ONCE(net
->ipv4
.sysctl_igmp_llm_reports
))
1311 reporter
= im
->reporter
;
1312 igmp_stop_timer(im
);
1314 if (!in_dev
->dead
) {
1315 if (IGMP_V1_SEEN(in_dev
))
1317 if (IGMP_V2_SEEN(in_dev
)) {
1319 igmp_send_report(in_dev
, im
, IGMP_HOST_LEAVE_MESSAGE
);
1323 igmpv3_add_delrec(in_dev
, im
, gfp
);
1325 igmp_ifc_event(in_dev
);
1330 static void igmp_group_dropped(struct ip_mc_list
*im
)
1332 __igmp_group_dropped(im
, GFP_KERNEL
);
1335 static void igmp_group_added(struct ip_mc_list
*im
)
1337 struct in_device
*in_dev
= im
->interface
;
1338 #ifdef CONFIG_IP_MULTICAST
1339 struct net
*net
= dev_net(in_dev
->dev
);
1342 if (im
->loaded
== 0) {
1344 ip_mc_filter_add(in_dev
, im
->multiaddr
);
1347 #ifdef CONFIG_IP_MULTICAST
1348 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1350 if (ipv4_is_local_multicast(im
->multiaddr
) &&
1351 !READ_ONCE(net
->ipv4
.sysctl_igmp_llm_reports
))
1357 im
->unsolicit_count
= READ_ONCE(net
->ipv4
.sysctl_igmp_qrv
);
1358 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
)) {
1359 spin_lock_bh(&im
->lock
);
1360 igmp_start_timer(im
, IGMP_INITIAL_REPORT_DELAY
);
1361 spin_unlock_bh(&im
->lock
);
1366 /* Based on RFC3376 5.1, for newly added INCLUDE SSM, we should
1367 * not send filter-mode change record as the mode should be from
1370 if (im
->sfmode
== MCAST_EXCLUDE
)
1371 im
->crcount
= in_dev
->mr_qrv
?: READ_ONCE(net
->ipv4
.sysctl_igmp_qrv
);
1373 igmp_ifc_event(in_dev
);
1379 * Multicast list managers
1382 static u32
ip_mc_hash(const struct ip_mc_list
*im
)
1384 return hash_32((__force u32
)im
->multiaddr
, MC_HASH_SZ_LOG
);
1387 static void ip_mc_hash_add(struct in_device
*in_dev
,
1388 struct ip_mc_list
*im
)
1390 struct ip_mc_list __rcu
**mc_hash
;
1393 mc_hash
= rtnl_dereference(in_dev
->mc_hash
);
1395 hash
= ip_mc_hash(im
);
1396 im
->next_hash
= mc_hash
[hash
];
1397 rcu_assign_pointer(mc_hash
[hash
], im
);
1401 /* do not use a hash table for small number of items */
1402 if (in_dev
->mc_count
< 4)
1405 mc_hash
= kzalloc(sizeof(struct ip_mc_list
*) << MC_HASH_SZ_LOG
,
1410 for_each_pmc_rtnl(in_dev
, im
) {
1411 hash
= ip_mc_hash(im
);
1412 im
->next_hash
= mc_hash
[hash
];
1413 RCU_INIT_POINTER(mc_hash
[hash
], im
);
1416 rcu_assign_pointer(in_dev
->mc_hash
, mc_hash
);
1419 static void ip_mc_hash_remove(struct in_device
*in_dev
,
1420 struct ip_mc_list
*im
)
1422 struct ip_mc_list __rcu
**mc_hash
= rtnl_dereference(in_dev
->mc_hash
);
1423 struct ip_mc_list
*aux
;
1427 mc_hash
+= ip_mc_hash(im
);
1428 while ((aux
= rtnl_dereference(*mc_hash
)) != im
)
1429 mc_hash
= &aux
->next_hash
;
1430 *mc_hash
= im
->next_hash
;
1435 * A socket has joined a multicast group on device dev.
1437 static void ____ip_mc_inc_group(struct in_device
*in_dev
, __be32 addr
,
1438 unsigned int mode
, gfp_t gfp
)
1440 struct ip_mc_list __rcu
**mc_hash
;
1441 struct ip_mc_list
*im
;
1445 mc_hash
= rtnl_dereference(in_dev
->mc_hash
);
1447 u32 hash
= hash_32((__force u32
)addr
, MC_HASH_SZ_LOG
);
1449 for (im
= rtnl_dereference(mc_hash
[hash
]);
1451 im
= rtnl_dereference(im
->next_hash
)) {
1452 if (im
->multiaddr
== addr
)
1456 for_each_pmc_rtnl(in_dev
, im
) {
1457 if (im
->multiaddr
== addr
)
1464 ip_mc_add_src(in_dev
, &addr
, mode
, 0, NULL
, 0);
1468 im
= kzalloc(sizeof(*im
), gfp
);
1473 im
->interface
= in_dev
;
1474 in_dev_hold(in_dev
);
1475 im
->multiaddr
= addr
;
1476 /* initial mode is (EX, empty) */
1478 im
->sfcount
[mode
] = 1;
1479 refcount_set(&im
->refcnt
, 1);
1480 spin_lock_init(&im
->lock
);
1481 #ifdef CONFIG_IP_MULTICAST
1482 timer_setup(&im
->timer
, igmp_timer_expire
, 0);
1485 im
->next_rcu
= in_dev
->mc_list
;
1487 rcu_assign_pointer(in_dev
->mc_list
, im
);
1489 ip_mc_hash_add(in_dev
, im
);
1491 #ifdef CONFIG_IP_MULTICAST
1492 igmpv3_del_delrec(in_dev
, im
);
1494 igmp_group_added(im
);
1496 ip_rt_multicast_event(in_dev
);
1501 void __ip_mc_inc_group(struct in_device
*in_dev
, __be32 addr
, gfp_t gfp
)
1503 ____ip_mc_inc_group(in_dev
, addr
, MCAST_EXCLUDE
, gfp
);
1505 EXPORT_SYMBOL(__ip_mc_inc_group
);
1507 void ip_mc_inc_group(struct in_device
*in_dev
, __be32 addr
)
1509 __ip_mc_inc_group(in_dev
, addr
, GFP_KERNEL
);
1511 EXPORT_SYMBOL(ip_mc_inc_group
);
1513 static int ip_mc_check_iphdr(struct sk_buff
*skb
)
1515 const struct iphdr
*iph
;
1517 unsigned int offset
= skb_network_offset(skb
) + sizeof(*iph
);
1519 if (!pskb_may_pull(skb
, offset
))
1524 if (iph
->version
!= 4 || ip_hdrlen(skb
) < sizeof(*iph
))
1527 offset
+= ip_hdrlen(skb
) - sizeof(*iph
);
1529 if (!pskb_may_pull(skb
, offset
))
1534 if (unlikely(ip_fast_csum((u8
*)iph
, iph
->ihl
)))
1537 len
= skb_network_offset(skb
) + ntohs(iph
->tot_len
);
1538 if (skb
->len
< len
|| len
< offset
)
1541 skb_set_transport_header(skb
, offset
);
1546 static int ip_mc_check_igmp_reportv3(struct sk_buff
*skb
)
1548 unsigned int len
= skb_transport_offset(skb
);
1550 len
+= sizeof(struct igmpv3_report
);
1552 return ip_mc_may_pull(skb
, len
) ? 0 : -EINVAL
;
1555 static int ip_mc_check_igmp_query(struct sk_buff
*skb
)
1557 unsigned int transport_len
= ip_transport_len(skb
);
1561 if (transport_len
!= sizeof(struct igmphdr
)) {
1563 if (transport_len
< sizeof(struct igmpv3_query
))
1566 len
= skb_transport_offset(skb
) + sizeof(struct igmpv3_query
);
1567 if (!ip_mc_may_pull(skb
, len
))
1571 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1572 * all-systems destination addresses (224.0.0.1) for general queries
1574 if (!igmp_hdr(skb
)->group
&&
1575 ip_hdr(skb
)->daddr
!= htonl(INADDR_ALLHOSTS_GROUP
))
1581 static int ip_mc_check_igmp_msg(struct sk_buff
*skb
)
1583 switch (igmp_hdr(skb
)->type
) {
1584 case IGMP_HOST_LEAVE_MESSAGE
:
1585 case IGMP_HOST_MEMBERSHIP_REPORT
:
1586 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1588 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1589 return ip_mc_check_igmp_reportv3(skb
);
1590 case IGMP_HOST_MEMBERSHIP_QUERY
:
1591 return ip_mc_check_igmp_query(skb
);
1597 static __sum16
ip_mc_validate_checksum(struct sk_buff
*skb
)
1599 return skb_checksum_simple_validate(skb
);
1602 static int ip_mc_check_igmp_csum(struct sk_buff
*skb
)
1604 unsigned int len
= skb_transport_offset(skb
) + sizeof(struct igmphdr
);
1605 unsigned int transport_len
= ip_transport_len(skb
);
1606 struct sk_buff
*skb_chk
;
1608 if (!ip_mc_may_pull(skb
, len
))
1611 skb_chk
= skb_checksum_trimmed(skb
, transport_len
,
1612 ip_mc_validate_checksum
);
1623 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1624 * @skb: the skb to validate
1626 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1627 * skb transport header accordingly and returns zero.
1629 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1631 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1632 * -ENOMEM: A memory allocation failure happened.
1634 * Caller needs to set the skb network header and free any returned skb if it
1635 * differs from the provided skb.
1637 int ip_mc_check_igmp(struct sk_buff
*skb
)
1639 int ret
= ip_mc_check_iphdr(skb
);
1644 if (ip_hdr(skb
)->protocol
!= IPPROTO_IGMP
)
1647 ret
= ip_mc_check_igmp_csum(skb
);
1651 return ip_mc_check_igmp_msg(skb
);
1653 EXPORT_SYMBOL(ip_mc_check_igmp
);
1656 * Resend IGMP JOIN report; used by netdev notifier.
1658 static void ip_mc_rejoin_groups(struct in_device
*in_dev
)
1660 #ifdef CONFIG_IP_MULTICAST
1661 struct ip_mc_list
*im
;
1663 struct net
*net
= dev_net(in_dev
->dev
);
1667 for_each_pmc_rtnl(in_dev
, im
) {
1668 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1670 if (ipv4_is_local_multicast(im
->multiaddr
) &&
1671 !READ_ONCE(net
->ipv4
.sysctl_igmp_llm_reports
))
1674 /* a failover is happening and switches
1675 * must be notified immediately
1677 if (IGMP_V1_SEEN(in_dev
))
1678 type
= IGMP_HOST_MEMBERSHIP_REPORT
;
1679 else if (IGMP_V2_SEEN(in_dev
))
1680 type
= IGMPV2_HOST_MEMBERSHIP_REPORT
;
1682 type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
1683 igmp_send_report(in_dev
, im
, type
);
1689 * A socket has left a multicast group on device dev
1692 void __ip_mc_dec_group(struct in_device
*in_dev
, __be32 addr
, gfp_t gfp
)
1694 struct ip_mc_list
*i
;
1695 struct ip_mc_list __rcu
**ip
;
1699 for (ip
= &in_dev
->mc_list
;
1700 (i
= rtnl_dereference(*ip
)) != NULL
;
1701 ip
= &i
->next_rcu
) {
1702 if (i
->multiaddr
== addr
) {
1703 if (--i
->users
== 0) {
1704 ip_mc_hash_remove(in_dev
, i
);
1707 __igmp_group_dropped(i
, gfp
);
1711 ip_rt_multicast_event(in_dev
);
1720 EXPORT_SYMBOL(__ip_mc_dec_group
);
1722 /* Device changing type */
1724 void ip_mc_unmap(struct in_device
*in_dev
)
1726 struct ip_mc_list
*pmc
;
1730 for_each_pmc_rtnl(in_dev
, pmc
)
1731 igmp_group_dropped(pmc
);
1734 void ip_mc_remap(struct in_device
*in_dev
)
1736 struct ip_mc_list
*pmc
;
1740 for_each_pmc_rtnl(in_dev
, pmc
) {
1741 #ifdef CONFIG_IP_MULTICAST
1742 igmpv3_del_delrec(in_dev
, pmc
);
1744 igmp_group_added(pmc
);
1748 /* Device going down */
1750 void ip_mc_down(struct in_device
*in_dev
)
1752 struct ip_mc_list
*pmc
;
1756 for_each_pmc_rtnl(in_dev
, pmc
)
1757 igmp_group_dropped(pmc
);
1759 #ifdef CONFIG_IP_MULTICAST
1760 WRITE_ONCE(in_dev
->mr_ifc_count
, 0);
1761 if (del_timer(&in_dev
->mr_ifc_timer
))
1762 __in_dev_put(in_dev
);
1763 in_dev
->mr_gq_running
= 0;
1764 if (del_timer(&in_dev
->mr_gq_timer
))
1765 __in_dev_put(in_dev
);
1768 ip_mc_dec_group(in_dev
, IGMP_ALL_HOSTS
);
1771 #ifdef CONFIG_IP_MULTICAST
1772 static void ip_mc_reset(struct in_device
*in_dev
)
1774 struct net
*net
= dev_net(in_dev
->dev
);
1776 in_dev
->mr_qi
= IGMP_QUERY_INTERVAL
;
1777 in_dev
->mr_qri
= IGMP_QUERY_RESPONSE_INTERVAL
;
1778 in_dev
->mr_qrv
= READ_ONCE(net
->ipv4
.sysctl_igmp_qrv
);
1781 static void ip_mc_reset(struct in_device
*in_dev
)
1786 void ip_mc_init_dev(struct in_device
*in_dev
)
1790 #ifdef CONFIG_IP_MULTICAST
1791 timer_setup(&in_dev
->mr_gq_timer
, igmp_gq_timer_expire
, 0);
1792 timer_setup(&in_dev
->mr_ifc_timer
, igmp_ifc_timer_expire
, 0);
1794 ip_mc_reset(in_dev
);
1796 spin_lock_init(&in_dev
->mc_tomb_lock
);
1799 /* Device going up */
1801 void ip_mc_up(struct in_device
*in_dev
)
1803 struct ip_mc_list
*pmc
;
1807 ip_mc_reset(in_dev
);
1808 ip_mc_inc_group(in_dev
, IGMP_ALL_HOSTS
);
1810 for_each_pmc_rtnl(in_dev
, pmc
) {
1811 #ifdef CONFIG_IP_MULTICAST
1812 igmpv3_del_delrec(in_dev
, pmc
);
1814 igmp_group_added(pmc
);
1819 * Device is about to be destroyed: clean up.
1822 void ip_mc_destroy_dev(struct in_device
*in_dev
)
1824 struct ip_mc_list
*i
;
1828 /* Deactivate timers */
1830 #ifdef CONFIG_IP_MULTICAST
1831 igmpv3_clear_delrec(in_dev
);
1834 while ((i
= rtnl_dereference(in_dev
->mc_list
)) != NULL
) {
1835 in_dev
->mc_list
= i
->next_rcu
;
1842 /* RTNL is locked */
1843 static struct in_device
*ip_mc_find_dev(struct net
*net
, struct ip_mreqn
*imr
)
1845 struct net_device
*dev
= NULL
;
1846 struct in_device
*idev
= NULL
;
1848 if (imr
->imr_ifindex
) {
1849 idev
= inetdev_by_index(net
, imr
->imr_ifindex
);
1852 if (imr
->imr_address
.s_addr
) {
1853 dev
= __ip_dev_find(net
, imr
->imr_address
.s_addr
, false);
1859 struct rtable
*rt
= ip_route_output(net
,
1860 imr
->imr_multiaddr
.s_addr
,
1869 imr
->imr_ifindex
= dev
->ifindex
;
1870 idev
= __in_dev_get_rtnl(dev
);
1876 * Join a socket to a group
1879 static int ip_mc_del1_src(struct ip_mc_list
*pmc
, int sfmode
,
1882 struct ip_sf_list
*psf
, *psf_prev
;
1886 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1887 if (psf
->sf_inaddr
== *psfsrc
)
1891 if (!psf
|| psf
->sf_count
[sfmode
] == 0) {
1892 /* source filter not found, or count wrong => bug */
1895 psf
->sf_count
[sfmode
]--;
1896 if (psf
->sf_count
[sfmode
] == 0) {
1897 ip_rt_multicast_event(pmc
->interface
);
1899 if (!psf
->sf_count
[MCAST_INCLUDE
] && !psf
->sf_count
[MCAST_EXCLUDE
]) {
1900 #ifdef CONFIG_IP_MULTICAST
1901 struct in_device
*in_dev
= pmc
->interface
;
1902 struct net
*net
= dev_net(in_dev
->dev
);
1905 /* no more filters for this source */
1907 psf_prev
->sf_next
= psf
->sf_next
;
1909 pmc
->sources
= psf
->sf_next
;
1910 #ifdef CONFIG_IP_MULTICAST
1911 if (psf
->sf_oldin
&&
1912 !IGMP_V1_SEEN(in_dev
) && !IGMP_V2_SEEN(in_dev
)) {
1913 psf
->sf_crcount
= in_dev
->mr_qrv
?: READ_ONCE(net
->ipv4
.sysctl_igmp_qrv
);
1914 psf
->sf_next
= pmc
->tomb
;
1924 #ifndef CONFIG_IP_MULTICAST
1925 #define igmp_ifc_event(x) do { } while (0)
1928 static int ip_mc_del_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1929 int sfcount
, __be32
*psfsrc
, int delta
)
1931 struct ip_mc_list
*pmc
;
1938 for_each_pmc_rcu(in_dev
, pmc
) {
1939 if (*pmca
== pmc
->multiaddr
)
1943 /* MCA not found?? bug */
1947 spin_lock_bh(&pmc
->lock
);
1949 #ifdef CONFIG_IP_MULTICAST
1954 if (!pmc
->sfcount
[sfmode
])
1956 pmc
->sfcount
[sfmode
]--;
1959 for (i
= 0; i
< sfcount
; i
++) {
1960 int rv
= ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1962 changerec
|= rv
> 0;
1966 if (pmc
->sfmode
== MCAST_EXCLUDE
&&
1967 pmc
->sfcount
[MCAST_EXCLUDE
] == 0 &&
1968 pmc
->sfcount
[MCAST_INCLUDE
]) {
1969 #ifdef CONFIG_IP_MULTICAST
1970 struct ip_sf_list
*psf
;
1971 struct net
*net
= dev_net(in_dev
->dev
);
1974 /* filter mode change */
1975 pmc
->sfmode
= MCAST_INCLUDE
;
1976 #ifdef CONFIG_IP_MULTICAST
1977 pmc
->crcount
= in_dev
->mr_qrv
?: READ_ONCE(net
->ipv4
.sysctl_igmp_qrv
);
1978 WRITE_ONCE(in_dev
->mr_ifc_count
, pmc
->crcount
);
1979 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1980 psf
->sf_crcount
= 0;
1981 igmp_ifc_event(pmc
->interface
);
1982 } else if (sf_setstate(pmc
) || changerec
) {
1983 igmp_ifc_event(pmc
->interface
);
1987 spin_unlock_bh(&pmc
->lock
);
1992 * Add multicast single-source filter to the interface list
1994 static int ip_mc_add1_src(struct ip_mc_list
*pmc
, int sfmode
,
1997 struct ip_sf_list
*psf
, *psf_prev
;
2000 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
2001 if (psf
->sf_inaddr
== *psfsrc
)
2006 psf
= kzalloc(sizeof(*psf
), GFP_ATOMIC
);
2009 psf
->sf_inaddr
= *psfsrc
;
2011 psf_prev
->sf_next
= psf
;
2015 psf
->sf_count
[sfmode
]++;
2016 if (psf
->sf_count
[sfmode
] == 1) {
2017 ip_rt_multicast_event(pmc
->interface
);
2022 #ifdef CONFIG_IP_MULTICAST
2023 static void sf_markstate(struct ip_mc_list
*pmc
)
2025 struct ip_sf_list
*psf
;
2026 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
2028 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
2029 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
2030 psf
->sf_oldin
= mca_xcount
==
2031 psf
->sf_count
[MCAST_EXCLUDE
] &&
2032 !psf
->sf_count
[MCAST_INCLUDE
];
2034 psf
->sf_oldin
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
2037 static int sf_setstate(struct ip_mc_list
*pmc
)
2039 struct ip_sf_list
*psf
, *dpsf
;
2040 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
2041 int qrv
= pmc
->interface
->mr_qrv
;
2045 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
2046 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
2047 new_in
= mca_xcount
== psf
->sf_count
[MCAST_EXCLUDE
] &&
2048 !psf
->sf_count
[MCAST_INCLUDE
];
2050 new_in
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
2052 if (!psf
->sf_oldin
) {
2053 struct ip_sf_list
*prev
= NULL
;
2055 for (dpsf
= pmc
->tomb
; dpsf
; dpsf
= dpsf
->sf_next
) {
2056 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
2062 prev
->sf_next
= dpsf
->sf_next
;
2064 pmc
->tomb
= dpsf
->sf_next
;
2067 psf
->sf_crcount
= qrv
;
2070 } else if (psf
->sf_oldin
) {
2072 psf
->sf_crcount
= 0;
2074 * add or update "delete" records if an active filter
2077 for (dpsf
= pmc
->tomb
; dpsf
; dpsf
= dpsf
->sf_next
)
2078 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
2081 dpsf
= kmalloc(sizeof(*dpsf
), GFP_ATOMIC
);
2085 /* pmc->lock held by callers */
2086 dpsf
->sf_next
= pmc
->tomb
;
2089 dpsf
->sf_crcount
= qrv
;
2098 * Add multicast source filter list to the interface list
2100 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
2101 int sfcount
, __be32
*psfsrc
, int delta
)
2103 struct ip_mc_list
*pmc
;
2110 for_each_pmc_rcu(in_dev
, pmc
) {
2111 if (*pmca
== pmc
->multiaddr
)
2115 /* MCA not found?? bug */
2119 spin_lock_bh(&pmc
->lock
);
2122 #ifdef CONFIG_IP_MULTICAST
2125 isexclude
= pmc
->sfmode
== MCAST_EXCLUDE
;
2127 pmc
->sfcount
[sfmode
]++;
2129 for (i
= 0; i
< sfcount
; i
++) {
2130 err
= ip_mc_add1_src(pmc
, sfmode
, &psfsrc
[i
]);
2138 pmc
->sfcount
[sfmode
]--;
2139 for (j
= 0; j
< i
; j
++)
2140 (void) ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[j
]);
2141 } else if (isexclude
!= (pmc
->sfcount
[MCAST_EXCLUDE
] != 0)) {
2142 #ifdef CONFIG_IP_MULTICAST
2143 struct ip_sf_list
*psf
;
2144 struct net
*net
= dev_net(pmc
->interface
->dev
);
2145 in_dev
= pmc
->interface
;
2148 /* filter mode change */
2149 if (pmc
->sfcount
[MCAST_EXCLUDE
])
2150 pmc
->sfmode
= MCAST_EXCLUDE
;
2151 else if (pmc
->sfcount
[MCAST_INCLUDE
])
2152 pmc
->sfmode
= MCAST_INCLUDE
;
2153 #ifdef CONFIG_IP_MULTICAST
2154 /* else no filters; keep old mode for reports */
2156 pmc
->crcount
= in_dev
->mr_qrv
?: READ_ONCE(net
->ipv4
.sysctl_igmp_qrv
);
2157 WRITE_ONCE(in_dev
->mr_ifc_count
, pmc
->crcount
);
2158 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
2159 psf
->sf_crcount
= 0;
2160 igmp_ifc_event(in_dev
);
2161 } else if (sf_setstate(pmc
)) {
2162 igmp_ifc_event(in_dev
);
2165 spin_unlock_bh(&pmc
->lock
);
2169 static void ip_mc_clear_src(struct ip_mc_list
*pmc
)
2171 struct ip_sf_list
*tomb
, *sources
;
2173 spin_lock_bh(&pmc
->lock
);
2176 sources
= pmc
->sources
;
2177 pmc
->sources
= NULL
;
2178 pmc
->sfmode
= MCAST_EXCLUDE
;
2179 pmc
->sfcount
[MCAST_INCLUDE
] = 0;
2180 pmc
->sfcount
[MCAST_EXCLUDE
] = 1;
2181 spin_unlock_bh(&pmc
->lock
);
2183 ip_sf_list_clear_all(tomb
);
2184 ip_sf_list_clear_all(sources
);
2187 /* Join a multicast group
2189 static int __ip_mc_join_group(struct sock
*sk
, struct ip_mreqn
*imr
,
2192 __be32 addr
= imr
->imr_multiaddr
.s_addr
;
2193 struct ip_mc_socklist
*iml
, *i
;
2194 struct in_device
*in_dev
;
2195 struct inet_sock
*inet
= inet_sk(sk
);
2196 struct net
*net
= sock_net(sk
);
2203 if (!ipv4_is_multicast(addr
))
2206 in_dev
= ip_mc_find_dev(net
, imr
);
2214 ifindex
= imr
->imr_ifindex
;
2215 for_each_pmc_rtnl(inet
, i
) {
2216 if (i
->multi
.imr_multiaddr
.s_addr
== addr
&&
2217 i
->multi
.imr_ifindex
== ifindex
)
2222 if (count
>= READ_ONCE(net
->ipv4
.sysctl_igmp_max_memberships
))
2224 iml
= sock_kmalloc(sk
, sizeof(*iml
), GFP_KERNEL
);
2228 memcpy(&iml
->multi
, imr
, sizeof(*imr
));
2229 iml
->next_rcu
= inet
->mc_list
;
2232 rcu_assign_pointer(inet
->mc_list
, iml
);
2233 ____ip_mc_inc_group(in_dev
, addr
, mode
, GFP_KERNEL
);
2239 /* Join ASM (Any-Source Multicast) group
2241 int ip_mc_join_group(struct sock
*sk
, struct ip_mreqn
*imr
)
2243 return __ip_mc_join_group(sk
, imr
, MCAST_EXCLUDE
);
2245 EXPORT_SYMBOL(ip_mc_join_group
);
2247 /* Join SSM (Source-Specific Multicast) group
2249 int ip_mc_join_group_ssm(struct sock
*sk
, struct ip_mreqn
*imr
,
2252 return __ip_mc_join_group(sk
, imr
, mode
);
2255 static int ip_mc_leave_src(struct sock
*sk
, struct ip_mc_socklist
*iml
,
2256 struct in_device
*in_dev
)
2258 struct ip_sf_socklist
*psf
= rtnl_dereference(iml
->sflist
);
2262 /* any-source empty exclude case */
2263 return ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
2264 iml
->sfmode
, 0, NULL
, 0);
2266 err
= ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
2267 iml
->sfmode
, psf
->sl_count
, psf
->sl_addr
, 0);
2268 RCU_INIT_POINTER(iml
->sflist
, NULL
);
2269 /* decrease mem now to avoid the memleak warning */
2270 atomic_sub(struct_size(psf
, sl_addr
, psf
->sl_max
), &sk
->sk_omem_alloc
);
2271 kfree_rcu(psf
, rcu
);
2275 int ip_mc_leave_group(struct sock
*sk
, struct ip_mreqn
*imr
)
2277 struct inet_sock
*inet
= inet_sk(sk
);
2278 struct ip_mc_socklist
*iml
;
2279 struct ip_mc_socklist __rcu
**imlp
;
2280 struct in_device
*in_dev
;
2281 struct net
*net
= sock_net(sk
);
2282 __be32 group
= imr
->imr_multiaddr
.s_addr
;
2284 int ret
= -EADDRNOTAVAIL
;
2288 in_dev
= ip_mc_find_dev(net
, imr
);
2289 if (!imr
->imr_ifindex
&& !imr
->imr_address
.s_addr
&& !in_dev
) {
2293 ifindex
= imr
->imr_ifindex
;
2294 for (imlp
= &inet
->mc_list
;
2295 (iml
= rtnl_dereference(*imlp
)) != NULL
;
2296 imlp
= &iml
->next_rcu
) {
2297 if (iml
->multi
.imr_multiaddr
.s_addr
!= group
)
2300 if (iml
->multi
.imr_ifindex
!= ifindex
)
2302 } else if (imr
->imr_address
.s_addr
&& imr
->imr_address
.s_addr
!=
2303 iml
->multi
.imr_address
.s_addr
)
2306 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2308 *imlp
= iml
->next_rcu
;
2311 ip_mc_dec_group(in_dev
, group
);
2313 /* decrease mem now to avoid the memleak warning */
2314 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
2315 kfree_rcu(iml
, rcu
);
2321 EXPORT_SYMBOL(ip_mc_leave_group
);
2323 int ip_mc_source(int add
, int omode
, struct sock
*sk
, struct
2324 ip_mreq_source
*mreqs
, int ifindex
)
2327 struct ip_mreqn imr
;
2328 __be32 addr
= mreqs
->imr_multiaddr
;
2329 struct ip_mc_socklist
*pmc
;
2330 struct in_device
*in_dev
= NULL
;
2331 struct inet_sock
*inet
= inet_sk(sk
);
2332 struct ip_sf_socklist
*psl
;
2333 struct net
*net
= sock_net(sk
);
2337 if (!ipv4_is_multicast(addr
))
2342 imr
.imr_multiaddr
.s_addr
= mreqs
->imr_multiaddr
;
2343 imr
.imr_address
.s_addr
= mreqs
->imr_interface
;
2344 imr
.imr_ifindex
= ifindex
;
2345 in_dev
= ip_mc_find_dev(net
, &imr
);
2351 err
= -EADDRNOTAVAIL
;
2353 for_each_pmc_rtnl(inet
, pmc
) {
2354 if ((pmc
->multi
.imr_multiaddr
.s_addr
==
2355 imr
.imr_multiaddr
.s_addr
) &&
2356 (pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
))
2359 if (!pmc
) { /* must have a prior join */
2363 /* if a source filter was set, must be the same mode as before */
2365 if (pmc
->sfmode
!= omode
) {
2369 } else if (pmc
->sfmode
!= omode
) {
2370 /* allow mode switches for empty-set filters */
2371 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 0, NULL
, 0);
2372 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, pmc
->sfmode
, 0,
2374 pmc
->sfmode
= omode
;
2377 psl
= rtnl_dereference(pmc
->sflist
);
2380 goto done
; /* err = -EADDRNOTAVAIL */
2382 for (i
= 0; i
< psl
->sl_count
; i
++) {
2383 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2388 if (rv
) /* source not found */
2389 goto done
; /* err = -EADDRNOTAVAIL */
2391 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2392 if (psl
->sl_count
== 1 && omode
== MCAST_INCLUDE
) {
2397 /* update the interface filter */
2398 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2399 &mreqs
->imr_sourceaddr
, 1);
2401 for (j
= i
+1; j
< psl
->sl_count
; j
++)
2402 psl
->sl_addr
[j
-1] = psl
->sl_addr
[j
];
2407 /* else, add a new source to the filter */
2409 if (psl
&& psl
->sl_count
>= READ_ONCE(net
->ipv4
.sysctl_igmp_max_msf
)) {
2413 if (!psl
|| psl
->sl_count
== psl
->sl_max
) {
2414 struct ip_sf_socklist
*newpsl
;
2415 int count
= IP_SFBLOCK
;
2418 count
+= psl
->sl_max
;
2419 newpsl
= sock_kmalloc(sk
, struct_size(newpsl
, sl_addr
, count
),
2425 newpsl
->sl_max
= count
;
2426 newpsl
->sl_count
= count
- IP_SFBLOCK
;
2428 for (i
= 0; i
< psl
->sl_count
; i
++)
2429 newpsl
->sl_addr
[i
] = psl
->sl_addr
[i
];
2430 /* decrease mem now to avoid the memleak warning */
2431 atomic_sub(struct_size(psl
, sl_addr
, psl
->sl_max
),
2432 &sk
->sk_omem_alloc
);
2434 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2436 kfree_rcu(psl
, rcu
);
2439 rv
= 1; /* > 0 for insert logic below if sl_count is 0 */
2440 for (i
= 0; i
< psl
->sl_count
; i
++) {
2441 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2446 if (rv
== 0) /* address already there is an error */
2448 for (j
= psl
->sl_count
-1; j
>= i
; j
--)
2449 psl
->sl_addr
[j
+1] = psl
->sl_addr
[j
];
2450 psl
->sl_addr
[i
] = mreqs
->imr_sourceaddr
;
2453 /* update the interface list */
2454 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2455 &mreqs
->imr_sourceaddr
, 1);
2458 err
= ip_mc_leave_group(sk
, &imr
);
2462 int ip_mc_msfilter(struct sock
*sk
, struct ip_msfilter
*msf
, int ifindex
)
2465 struct ip_mreqn imr
;
2466 __be32 addr
= msf
->imsf_multiaddr
;
2467 struct ip_mc_socklist
*pmc
;
2468 struct in_device
*in_dev
;
2469 struct inet_sock
*inet
= inet_sk(sk
);
2470 struct ip_sf_socklist
*newpsl
, *psl
;
2471 struct net
*net
= sock_net(sk
);
2474 if (!ipv4_is_multicast(addr
))
2476 if (msf
->imsf_fmode
!= MCAST_INCLUDE
&&
2477 msf
->imsf_fmode
!= MCAST_EXCLUDE
)
2482 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2483 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2484 imr
.imr_ifindex
= ifindex
;
2485 in_dev
= ip_mc_find_dev(net
, &imr
);
2492 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2493 if (msf
->imsf_fmode
== MCAST_INCLUDE
&& msf
->imsf_numsrc
== 0) {
2498 for_each_pmc_rtnl(inet
, pmc
) {
2499 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2500 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2503 if (!pmc
) { /* must have a prior join */
2507 if (msf
->imsf_numsrc
) {
2508 newpsl
= sock_kmalloc(sk
, struct_size(newpsl
, sl_addr
,
2515 newpsl
->sl_max
= newpsl
->sl_count
= msf
->imsf_numsrc
;
2516 memcpy(newpsl
->sl_addr
, msf
->imsf_slist_flex
,
2517 flex_array_size(msf
, imsf_slist_flex
, msf
->imsf_numsrc
));
2518 err
= ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2519 msf
->imsf_fmode
, newpsl
->sl_count
, newpsl
->sl_addr
, 0);
2521 sock_kfree_s(sk
, newpsl
,
2522 struct_size(newpsl
, sl_addr
,
2528 (void) ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2529 msf
->imsf_fmode
, 0, NULL
, 0);
2531 psl
= rtnl_dereference(pmc
->sflist
);
2533 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2534 psl
->sl_count
, psl
->sl_addr
, 0);
2535 /* decrease mem now to avoid the memleak warning */
2536 atomic_sub(struct_size(psl
, sl_addr
, psl
->sl_max
),
2537 &sk
->sk_omem_alloc
);
2539 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2542 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2544 kfree_rcu(psl
, rcu
);
2545 pmc
->sfmode
= msf
->imsf_fmode
;
2549 err
= ip_mc_leave_group(sk
, &imr
);
2552 int ip_mc_msfget(struct sock
*sk
, struct ip_msfilter
*msf
,
2553 sockptr_t optval
, sockptr_t optlen
)
2555 int err
, len
, count
, copycount
, msf_size
;
2556 struct ip_mreqn imr
;
2557 __be32 addr
= msf
->imsf_multiaddr
;
2558 struct ip_mc_socklist
*pmc
;
2559 struct in_device
*in_dev
;
2560 struct inet_sock
*inet
= inet_sk(sk
);
2561 struct ip_sf_socklist
*psl
;
2562 struct net
*net
= sock_net(sk
);
2566 if (!ipv4_is_multicast(addr
))
2569 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2570 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2571 imr
.imr_ifindex
= 0;
2572 in_dev
= ip_mc_find_dev(net
, &imr
);
2578 err
= -EADDRNOTAVAIL
;
2580 for_each_pmc_rtnl(inet
, pmc
) {
2581 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2582 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2585 if (!pmc
) /* must have a prior join */
2587 msf
->imsf_fmode
= pmc
->sfmode
;
2588 psl
= rtnl_dereference(pmc
->sflist
);
2592 count
= psl
->sl_count
;
2594 copycount
= count
< msf
->imsf_numsrc
? count
: msf
->imsf_numsrc
;
2595 len
= flex_array_size(psl
, sl_addr
, copycount
);
2596 msf
->imsf_numsrc
= count
;
2597 msf_size
= IP_MSFILTER_SIZE(copycount
);
2598 if (copy_to_sockptr(optlen
, &msf_size
, sizeof(int)) ||
2599 copy_to_sockptr(optval
, msf
, IP_MSFILTER_SIZE(0))) {
2603 copy_to_sockptr_offset(optval
,
2604 offsetof(struct ip_msfilter
, imsf_slist_flex
),
2612 int ip_mc_gsfget(struct sock
*sk
, struct group_filter
*gsf
,
2613 sockptr_t optval
, size_t ss_offset
)
2615 int i
, count
, copycount
;
2616 struct sockaddr_in
*psin
;
2618 struct ip_mc_socklist
*pmc
;
2619 struct inet_sock
*inet
= inet_sk(sk
);
2620 struct ip_sf_socklist
*psl
;
2624 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
2625 if (psin
->sin_family
!= AF_INET
)
2627 addr
= psin
->sin_addr
.s_addr
;
2628 if (!ipv4_is_multicast(addr
))
2631 for_each_pmc_rtnl(inet
, pmc
) {
2632 if (pmc
->multi
.imr_multiaddr
.s_addr
== addr
&&
2633 pmc
->multi
.imr_ifindex
== gsf
->gf_interface
)
2636 if (!pmc
) /* must have a prior join */
2637 return -EADDRNOTAVAIL
;
2638 gsf
->gf_fmode
= pmc
->sfmode
;
2639 psl
= rtnl_dereference(pmc
->sflist
);
2640 count
= psl
? psl
->sl_count
: 0;
2641 copycount
= count
< gsf
->gf_numsrc
? count
: gsf
->gf_numsrc
;
2642 gsf
->gf_numsrc
= count
;
2643 for (i
= 0; i
< copycount
; i
++) {
2644 struct sockaddr_storage ss
;
2646 psin
= (struct sockaddr_in
*)&ss
;
2647 memset(&ss
, 0, sizeof(ss
));
2648 psin
->sin_family
= AF_INET
;
2649 psin
->sin_addr
.s_addr
= psl
->sl_addr
[i
];
2650 if (copy_to_sockptr_offset(optval
, ss_offset
,
2653 ss_offset
+= sizeof(ss
);
2659 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2661 int ip_mc_sf_allow(const struct sock
*sk
, __be32 loc_addr
, __be32 rmt_addr
,
2664 const struct inet_sock
*inet
= inet_sk(sk
);
2665 struct ip_mc_socklist
*pmc
;
2666 struct ip_sf_socklist
*psl
;
2671 if (!ipv4_is_multicast(loc_addr
))
2675 for_each_pmc_rcu(inet
, pmc
) {
2676 if (pmc
->multi
.imr_multiaddr
.s_addr
== loc_addr
&&
2677 (pmc
->multi
.imr_ifindex
== dif
||
2678 (sdif
&& pmc
->multi
.imr_ifindex
== sdif
)))
2681 ret
= inet_test_bit(MC_ALL
, sk
);
2684 psl
= rcu_dereference(pmc
->sflist
);
2685 ret
= (pmc
->sfmode
== MCAST_EXCLUDE
);
2689 for (i
= 0; i
< psl
->sl_count
; i
++) {
2690 if (psl
->sl_addr
[i
] == rmt_addr
)
2694 if (pmc
->sfmode
== MCAST_INCLUDE
&& i
>= psl
->sl_count
)
2696 if (pmc
->sfmode
== MCAST_EXCLUDE
&& i
< psl
->sl_count
)
2706 * A socket is closing.
2709 void ip_mc_drop_socket(struct sock
*sk
)
2711 struct inet_sock
*inet
= inet_sk(sk
);
2712 struct ip_mc_socklist
*iml
;
2713 struct net
*net
= sock_net(sk
);
2719 while ((iml
= rtnl_dereference(inet
->mc_list
)) != NULL
) {
2720 struct in_device
*in_dev
;
2722 inet
->mc_list
= iml
->next_rcu
;
2723 in_dev
= inetdev_by_index(net
, iml
->multi
.imr_ifindex
);
2724 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2726 ip_mc_dec_group(in_dev
, iml
->multi
.imr_multiaddr
.s_addr
);
2727 /* decrease mem now to avoid the memleak warning */
2728 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
2729 kfree_rcu(iml
, rcu
);
2734 /* called with rcu_read_lock() */
2735 int ip_check_mc_rcu(struct in_device
*in_dev
, __be32 mc_addr
, __be32 src_addr
, u8 proto
)
2737 struct ip_mc_list
*im
;
2738 struct ip_mc_list __rcu
**mc_hash
;
2739 struct ip_sf_list
*psf
;
2742 mc_hash
= rcu_dereference(in_dev
->mc_hash
);
2744 u32 hash
= hash_32((__force u32
)mc_addr
, MC_HASH_SZ_LOG
);
2746 for (im
= rcu_dereference(mc_hash
[hash
]);
2748 im
= rcu_dereference(im
->next_hash
)) {
2749 if (im
->multiaddr
== mc_addr
)
2753 for_each_pmc_rcu(in_dev
, im
) {
2754 if (im
->multiaddr
== mc_addr
)
2758 if (im
&& proto
== IPPROTO_IGMP
) {
2762 spin_lock_bh(&im
->lock
);
2763 for (psf
= im
->sources
; psf
; psf
= psf
->sf_next
) {
2764 if (psf
->sf_inaddr
== src_addr
)
2768 rv
= psf
->sf_count
[MCAST_INCLUDE
] ||
2769 psf
->sf_count
[MCAST_EXCLUDE
] !=
2770 im
->sfcount
[MCAST_EXCLUDE
];
2772 rv
= im
->sfcount
[MCAST_EXCLUDE
] != 0;
2773 spin_unlock_bh(&im
->lock
);
2775 rv
= 1; /* unspecified source; tentatively allow */
2780 #if defined(CONFIG_PROC_FS)
2781 struct igmp_mc_iter_state
{
2782 struct seq_net_private p
;
2783 struct net_device
*dev
;
2784 struct in_device
*in_dev
;
2787 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2789 static inline struct ip_mc_list
*igmp_mc_get_first(struct seq_file
*seq
)
2791 struct net
*net
= seq_file_net(seq
);
2792 struct ip_mc_list
*im
= NULL
;
2793 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2795 state
->in_dev
= NULL
;
2796 for_each_netdev_rcu(net
, state
->dev
) {
2797 struct in_device
*in_dev
;
2799 in_dev
= __in_dev_get_rcu(state
->dev
);
2802 im
= rcu_dereference(in_dev
->mc_list
);
2804 state
->in_dev
= in_dev
;
2811 static struct ip_mc_list
*igmp_mc_get_next(struct seq_file
*seq
, struct ip_mc_list
*im
)
2813 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2815 im
= rcu_dereference(im
->next_rcu
);
2817 state
->dev
= next_net_device_rcu(state
->dev
);
2819 state
->in_dev
= NULL
;
2822 state
->in_dev
= __in_dev_get_rcu(state
->dev
);
2825 im
= rcu_dereference(state
->in_dev
->mc_list
);
2830 static struct ip_mc_list
*igmp_mc_get_idx(struct seq_file
*seq
, loff_t pos
)
2832 struct ip_mc_list
*im
= igmp_mc_get_first(seq
);
2834 while (pos
&& (im
= igmp_mc_get_next(seq
, im
)) != NULL
)
2836 return pos
? NULL
: im
;
2839 static void *igmp_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2843 return *pos
? igmp_mc_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2846 static void *igmp_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2848 struct ip_mc_list
*im
;
2849 if (v
== SEQ_START_TOKEN
)
2850 im
= igmp_mc_get_first(seq
);
2852 im
= igmp_mc_get_next(seq
, v
);
2857 static void igmp_mc_seq_stop(struct seq_file
*seq
, void *v
)
2860 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2862 state
->in_dev
= NULL
;
2867 static int igmp_mc_seq_show(struct seq_file
*seq
, void *v
)
2869 if (v
== SEQ_START_TOKEN
)
2871 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2873 struct ip_mc_list
*im
= v
;
2874 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2878 #ifdef CONFIG_IP_MULTICAST
2879 querier
= IGMP_V1_SEEN(state
->in_dev
) ? "V1" :
2880 IGMP_V2_SEEN(state
->in_dev
) ? "V2" :
2886 if (rcu_access_pointer(state
->in_dev
->mc_list
) == im
) {
2887 seq_printf(seq
, "%d\t%-10s: %5d %7s\n",
2888 state
->dev
->ifindex
, state
->dev
->name
, state
->in_dev
->mc_count
, querier
);
2891 delta
= im
->timer
.expires
- jiffies
;
2893 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2894 im
->multiaddr
, im
->users
,
2896 im
->tm_running
? jiffies_delta_to_clock_t(delta
) : 0,
2902 static const struct seq_operations igmp_mc_seq_ops
= {
2903 .start
= igmp_mc_seq_start
,
2904 .next
= igmp_mc_seq_next
,
2905 .stop
= igmp_mc_seq_stop
,
2906 .show
= igmp_mc_seq_show
,
2909 struct igmp_mcf_iter_state
{
2910 struct seq_net_private p
;
2911 struct net_device
*dev
;
2912 struct in_device
*idev
;
2913 struct ip_mc_list
*im
;
2916 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2918 static inline struct ip_sf_list
*igmp_mcf_get_first(struct seq_file
*seq
)
2920 struct net
*net
= seq_file_net(seq
);
2921 struct ip_sf_list
*psf
= NULL
;
2922 struct ip_mc_list
*im
= NULL
;
2923 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2927 for_each_netdev_rcu(net
, state
->dev
) {
2928 struct in_device
*idev
;
2929 idev
= __in_dev_get_rcu(state
->dev
);
2930 if (unlikely(!idev
))
2932 im
= rcu_dereference(idev
->mc_list
);
2934 spin_lock_bh(&im
->lock
);
2941 spin_unlock_bh(&im
->lock
);
2947 static struct ip_sf_list
*igmp_mcf_get_next(struct seq_file
*seq
, struct ip_sf_list
*psf
)
2949 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2953 spin_unlock_bh(&state
->im
->lock
);
2954 state
->im
= state
->im
->next
;
2955 while (!state
->im
) {
2956 state
->dev
= next_net_device_rcu(state
->dev
);
2961 state
->idev
= __in_dev_get_rcu(state
->dev
);
2964 state
->im
= rcu_dereference(state
->idev
->mc_list
);
2966 spin_lock_bh(&state
->im
->lock
);
2967 psf
= state
->im
->sources
;
2973 static struct ip_sf_list
*igmp_mcf_get_idx(struct seq_file
*seq
, loff_t pos
)
2975 struct ip_sf_list
*psf
= igmp_mcf_get_first(seq
);
2977 while (pos
&& (psf
= igmp_mcf_get_next(seq
, psf
)) != NULL
)
2979 return pos
? NULL
: psf
;
2982 static void *igmp_mcf_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2986 return *pos
? igmp_mcf_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2989 static void *igmp_mcf_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2991 struct ip_sf_list
*psf
;
2992 if (v
== SEQ_START_TOKEN
)
2993 psf
= igmp_mcf_get_first(seq
);
2995 psf
= igmp_mcf_get_next(seq
, v
);
3000 static void igmp_mcf_seq_stop(struct seq_file
*seq
, void *v
)
3003 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
3004 if (likely(state
->im
)) {
3005 spin_unlock_bh(&state
->im
->lock
);
3013 static int igmp_mcf_seq_show(struct seq_file
*seq
, void *v
)
3015 struct ip_sf_list
*psf
= v
;
3016 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
3018 if (v
== SEQ_START_TOKEN
) {
3019 seq_puts(seq
, "Idx Device MCA SRC INC EXC\n");
3023 "0x%08x %6lu %6lu\n",
3024 state
->dev
->ifindex
, state
->dev
->name
,
3025 ntohl(state
->im
->multiaddr
),
3026 ntohl(psf
->sf_inaddr
),
3027 psf
->sf_count
[MCAST_INCLUDE
],
3028 psf
->sf_count
[MCAST_EXCLUDE
]);
3033 static const struct seq_operations igmp_mcf_seq_ops
= {
3034 .start
= igmp_mcf_seq_start
,
3035 .next
= igmp_mcf_seq_next
,
3036 .stop
= igmp_mcf_seq_stop
,
3037 .show
= igmp_mcf_seq_show
,
3040 static int __net_init
igmp_net_init(struct net
*net
)
3042 struct proc_dir_entry
*pde
;
3045 pde
= proc_create_net("igmp", 0444, net
->proc_net
, &igmp_mc_seq_ops
,
3046 sizeof(struct igmp_mc_iter_state
));
3049 pde
= proc_create_net("mcfilter", 0444, net
->proc_net
,
3050 &igmp_mcf_seq_ops
, sizeof(struct igmp_mcf_iter_state
));
3053 err
= inet_ctl_sock_create(&net
->ipv4
.mc_autojoin_sk
, AF_INET
,
3054 SOCK_DGRAM
, 0, net
);
3056 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3064 remove_proc_entry("mcfilter", net
->proc_net
);
3066 remove_proc_entry("igmp", net
->proc_net
);
3071 static void __net_exit
igmp_net_exit(struct net
*net
)
3073 remove_proc_entry("mcfilter", net
->proc_net
);
3074 remove_proc_entry("igmp", net
->proc_net
);
3075 inet_ctl_sock_destroy(net
->ipv4
.mc_autojoin_sk
);
3078 static struct pernet_operations igmp_net_ops
= {
3079 .init
= igmp_net_init
,
3080 .exit
= igmp_net_exit
,
3084 static int igmp_netdev_event(struct notifier_block
*this,
3085 unsigned long event
, void *ptr
)
3087 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
3088 struct in_device
*in_dev
;
3091 case NETDEV_RESEND_IGMP
:
3092 in_dev
= __in_dev_get_rtnl(dev
);
3094 ip_mc_rejoin_groups(in_dev
);
3102 static struct notifier_block igmp_notifier
= {
3103 .notifier_call
= igmp_netdev_event
,
3106 int __init
igmp_mc_init(void)
3108 #if defined(CONFIG_PROC_FS)
3111 err
= register_pernet_subsys(&igmp_net_ops
);
3114 err
= register_netdevice_notifier(&igmp_notifier
);
3116 goto reg_notif_fail
;
3120 unregister_pernet_subsys(&igmp_net_ops
);
3123 return register_netdevice_notifier(&igmp_notifier
);