2 * Linux NET3: Internet Group Management Protocol [IGMP]
4 * This code implements the IGMP protocol as defined in RFC1112. There has
5 * been a further revision of this protocol since which is now supported.
7 * If you have trouble with this module be careful what gcc you have used,
8 * the older version didn't come out right using gcc 2.5.8, the newer one
9 * seems to fall out with gcc 2.6.2.
12 * Alan Cox <alan@lxorguk.ukuu.org.uk>
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
21 * Alan Cox : Added lots of __inline__ to optimise
22 * the memory usage of all the tiny little
24 * Alan Cox : Dumped the header building experiment.
25 * Alan Cox : Minor tweaks ready for multicast routing
26 * and extended IGMP protocol.
27 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
28 * writes utterly bogus code otherwise (sigh)
29 * fixed IGMP loopback to behave in the manner
30 * desired by mrouted, fixed the fact it has been
31 * broken since 1.3.6 and cleaned up a few minor
34 * Chih-Jen Chang : Tried to revise IGMP to Version 2
35 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
36 * The enhancements are mainly based on Steve Deering's
37 * ipmulti-3.5 source code.
38 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
39 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
40 * the mrouted version on that device.
41 * Chih-Jen Chang : Added the max_resp_time parameter to
42 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
43 * to identify the multicast router version
44 * and do what the IGMP version 2 specified.
45 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
46 * Tsu-Sheng Tsao if the specified time expired.
47 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
48 * Alan Cox : Use GFP_ATOMIC in the right places.
49 * Christian Daudt : igmp timer wasn't set for local group
50 * memberships but was being deleted,
51 * which caused a "del_timer() called
52 * from %p with timer not initialized\n"
54 * Christian Daudt : removed del_timer from
55 * igmp_timer_expire function (960205).
56 * Christian Daudt : igmp_heard_report now only calls
57 * igmp_timer_expire if tm->running is
59 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
60 * igmp_heard_query never trigger. Expiry
61 * miscalculation fixed in igmp_heard_query
62 * and random() made to return unsigned to
63 * prevent negative expiry times.
64 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
65 * fix from pending 2.1.x patches.
66 * Alan Cox: Forget to enable FDDI support earlier.
67 * Alexey Kuznetsov: Fixed leaving groups on device down.
68 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
69 * David L Stevens: IGMPv3 support, with help from
73 #include <linux/module.h>
74 #include <linux/slab.h>
75 #include <asm/uaccess.h>
76 #include <linux/types.h>
77 #include <linux/kernel.h>
78 #include <linux/jiffies.h>
79 #include <linux/string.h>
80 #include <linux/socket.h>
81 #include <linux/sockios.h>
83 #include <linux/inet.h>
84 #include <linux/netdevice.h>
85 #include <linux/skbuff.h>
86 #include <linux/inetdevice.h>
87 #include <linux/igmp.h>
88 #include <linux/if_arp.h>
89 #include <linux/rtnetlink.h>
90 #include <linux/times.h>
91 #include <linux/pkt_sched.h>
93 #include <net/net_namespace.h>
96 #include <net/protocol.h>
97 #include <net/route.h>
99 #include <net/checksum.h>
100 #include <net/inet_common.h>
101 #include <linux/netfilter_ipv4.h>
102 #ifdef CONFIG_IP_MROUTE
103 #include <linux/mroute.h>
105 #ifdef CONFIG_PROC_FS
106 #include <linux/proc_fs.h>
107 #include <linux/seq_file.h>
110 #define IP_MAX_MEMBERSHIPS 20
111 #define IP_MAX_MSF 10
113 #ifdef CONFIG_IP_MULTICAST
114 /* Parameter names and values are taken from igmp-v2-06 draft */
116 #define IGMP_V1_ROUTER_PRESENT_TIMEOUT (400*HZ)
117 #define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ)
118 #define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
119 #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
120 #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
121 #define IGMP_QUERY_ROBUSTNESS_VARIABLE 2
124 #define IGMP_INITIAL_REPORT_DELAY (1)
126 /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
127 * IGMP specs require to report membership immediately after
128 * joining a group, but we delay the first report by a
129 * small interval. It seems more natural and still does not
130 * contradict to specs provided this delay is small enough.
133 #define IGMP_V1_SEEN(in_dev) \
134 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
135 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
136 ((in_dev)->mr_v1_seen && \
137 time_before(jiffies, (in_dev)->mr_v1_seen)))
138 #define IGMP_V2_SEEN(in_dev) \
139 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
140 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
141 ((in_dev)->mr_v2_seen && \
142 time_before(jiffies, (in_dev)->mr_v2_seen)))
144 static int unsolicited_report_interval(struct in_device
*in_dev
)
146 int interval_ms
, interval_jiffies
;
148 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
149 interval_ms
= IN_DEV_CONF_GET(
151 IGMPV2_UNSOLICITED_REPORT_INTERVAL
);
153 interval_ms
= IN_DEV_CONF_GET(
155 IGMPV3_UNSOLICITED_REPORT_INTERVAL
);
157 interval_jiffies
= msecs_to_jiffies(interval_ms
);
159 /* _timer functions can't handle a delay of 0 jiffies so ensure
160 * we always return a positive value.
162 if (interval_jiffies
<= 0)
163 interval_jiffies
= 1;
164 return interval_jiffies
;
167 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
);
168 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
);
169 static void igmpv3_clear_delrec(struct in_device
*in_dev
);
170 static int sf_setstate(struct ip_mc_list
*pmc
);
171 static void sf_markstate(struct ip_mc_list
*pmc
);
173 static void ip_mc_clear_src(struct ip_mc_list
*pmc
);
174 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
175 int sfcount
, __be32
*psfsrc
, int delta
);
177 static void ip_ma_put(struct ip_mc_list
*im
)
179 if (atomic_dec_and_test(&im
->refcnt
)) {
180 in_dev_put(im
->interface
);
185 #define for_each_pmc_rcu(in_dev, pmc) \
186 for (pmc = rcu_dereference(in_dev->mc_list); \
188 pmc = rcu_dereference(pmc->next_rcu))
190 #define for_each_pmc_rtnl(in_dev, pmc) \
191 for (pmc = rtnl_dereference(in_dev->mc_list); \
193 pmc = rtnl_dereference(pmc->next_rcu))
195 #ifdef CONFIG_IP_MULTICAST
201 static void igmp_stop_timer(struct ip_mc_list
*im
)
203 spin_lock_bh(&im
->lock
);
204 if (del_timer(&im
->timer
))
205 atomic_dec(&im
->refcnt
);
208 im
->unsolicit_count
= 0;
209 spin_unlock_bh(&im
->lock
);
212 /* It must be called with locked im->lock */
213 static void igmp_start_timer(struct ip_mc_list
*im
, int max_delay
)
215 int tv
= prandom_u32() % max_delay
;
218 if (!mod_timer(&im
->timer
, jiffies
+tv
+2))
219 atomic_inc(&im
->refcnt
);
222 static void igmp_gq_start_timer(struct in_device
*in_dev
)
224 int tv
= prandom_u32() % in_dev
->mr_maxdelay
;
226 in_dev
->mr_gq_running
= 1;
227 if (!mod_timer(&in_dev
->mr_gq_timer
, jiffies
+tv
+2))
231 static void igmp_ifc_start_timer(struct in_device
*in_dev
, int delay
)
233 int tv
= prandom_u32() % delay
;
235 if (!mod_timer(&in_dev
->mr_ifc_timer
, jiffies
+tv
+2))
239 static void igmp_mod_timer(struct ip_mc_list
*im
, int max_delay
)
241 spin_lock_bh(&im
->lock
);
242 im
->unsolicit_count
= 0;
243 if (del_timer(&im
->timer
)) {
244 if ((long)(im
->timer
.expires
-jiffies
) < max_delay
) {
245 add_timer(&im
->timer
);
247 spin_unlock_bh(&im
->lock
);
250 atomic_dec(&im
->refcnt
);
252 igmp_start_timer(im
, max_delay
);
253 spin_unlock_bh(&im
->lock
);
258 * Send an IGMP report.
261 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
264 static int is_in(struct ip_mc_list
*pmc
, struct ip_sf_list
*psf
, int type
,
265 int gdeleted
, int sdeleted
)
268 case IGMPV3_MODE_IS_INCLUDE
:
269 case IGMPV3_MODE_IS_EXCLUDE
:
270 if (gdeleted
|| sdeleted
)
272 if (!(pmc
->gsquery
&& !psf
->sf_gsresp
)) {
273 if (pmc
->sfmode
== MCAST_INCLUDE
)
275 /* don't include if this source is excluded
278 if (psf
->sf_count
[MCAST_INCLUDE
])
279 return type
== IGMPV3_MODE_IS_INCLUDE
;
280 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
281 psf
->sf_count
[MCAST_EXCLUDE
];
284 case IGMPV3_CHANGE_TO_INCLUDE
:
285 if (gdeleted
|| sdeleted
)
287 return psf
->sf_count
[MCAST_INCLUDE
] != 0;
288 case IGMPV3_CHANGE_TO_EXCLUDE
:
289 if (gdeleted
|| sdeleted
)
291 if (pmc
->sfcount
[MCAST_EXCLUDE
] == 0 ||
292 psf
->sf_count
[MCAST_INCLUDE
])
294 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
295 psf
->sf_count
[MCAST_EXCLUDE
];
296 case IGMPV3_ALLOW_NEW_SOURCES
:
297 if (gdeleted
|| !psf
->sf_crcount
)
299 return (pmc
->sfmode
== MCAST_INCLUDE
) ^ sdeleted
;
300 case IGMPV3_BLOCK_OLD_SOURCES
:
301 if (pmc
->sfmode
== MCAST_INCLUDE
)
302 return gdeleted
|| (psf
->sf_crcount
&& sdeleted
);
303 return psf
->sf_crcount
&& !gdeleted
&& !sdeleted
;
309 igmp_scount(struct ip_mc_list
*pmc
, int type
, int gdeleted
, int sdeleted
)
311 struct ip_sf_list
*psf
;
314 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
315 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
))
322 static struct sk_buff
*igmpv3_newpack(struct net_device
*dev
, unsigned int mtu
)
327 struct igmpv3_report
*pig
;
328 struct net
*net
= dev_net(dev
);
330 int hlen
= LL_RESERVED_SPACE(dev
);
331 int tlen
= dev
->needed_tailroom
;
332 unsigned int size
= mtu
;
335 skb
= alloc_skb(size
+ hlen
+ tlen
,
336 GFP_ATOMIC
| __GFP_NOWARN
);
343 skb
->priority
= TC_PRIO_CONTROL
;
345 rt
= ip_route_output_ports(net
, &fl4
, NULL
, IGMPV3_ALL_MCR
, 0,
347 IPPROTO_IGMP
, 0, dev
->ifindex
);
353 skb_dst_set(skb
, &rt
->dst
);
356 skb
->reserved_tailroom
= skb_end_offset(skb
) -
357 min(mtu
, skb_end_offset(skb
));
358 skb_reserve(skb
, hlen
);
360 skb_reset_network_header(skb
);
362 skb_put(skb
, sizeof(struct iphdr
) + 4);
365 pip
->ihl
= (sizeof(struct iphdr
)+4)>>2;
367 pip
->frag_off
= htons(IP_DF
);
369 pip
->daddr
= fl4
.daddr
;
370 pip
->saddr
= fl4
.saddr
;
371 pip
->protocol
= IPPROTO_IGMP
;
372 pip
->tot_len
= 0; /* filled in later */
373 ip_select_ident(net
, skb
, NULL
);
374 ((u8
*)&pip
[1])[0] = IPOPT_RA
;
375 ((u8
*)&pip
[1])[1] = 4;
376 ((u8
*)&pip
[1])[2] = 0;
377 ((u8
*)&pip
[1])[3] = 0;
379 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
) + 4;
380 skb_put(skb
, sizeof(*pig
));
381 pig
= igmpv3_report_hdr(skb
);
382 pig
->type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
390 static int igmpv3_sendpack(struct sk_buff
*skb
)
392 struct igmphdr
*pig
= igmp_hdr(skb
);
393 const int igmplen
= skb_tail_pointer(skb
) - skb_transport_header(skb
);
395 pig
->csum
= ip_compute_csum(igmp_hdr(skb
), igmplen
);
397 return ip_local_out(skb
);
400 static int grec_size(struct ip_mc_list
*pmc
, int type
, int gdel
, int sdel
)
402 return sizeof(struct igmpv3_grec
) + 4*igmp_scount(pmc
, type
, gdel
, sdel
);
405 static struct sk_buff
*add_grhead(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
406 int type
, struct igmpv3_grec
**ppgr
)
408 struct net_device
*dev
= pmc
->interface
->dev
;
409 struct igmpv3_report
*pih
;
410 struct igmpv3_grec
*pgr
;
413 skb
= igmpv3_newpack(dev
, dev
->mtu
);
416 pgr
= (struct igmpv3_grec
*)skb_put(skb
, sizeof(struct igmpv3_grec
));
417 pgr
->grec_type
= type
;
418 pgr
->grec_auxwords
= 0;
420 pgr
->grec_mca
= pmc
->multiaddr
;
421 pih
= igmpv3_report_hdr(skb
);
422 pih
->ngrec
= htons(ntohs(pih
->ngrec
)+1);
427 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
429 static struct sk_buff
*add_grec(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
430 int type
, int gdeleted
, int sdeleted
)
432 struct net_device
*dev
= pmc
->interface
->dev
;
433 struct igmpv3_report
*pih
;
434 struct igmpv3_grec
*pgr
= NULL
;
435 struct ip_sf_list
*psf
, *psf_next
, *psf_prev
, **psf_list
;
436 int scount
, stotal
, first
, isquery
, truncate
;
438 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
441 isquery
= type
== IGMPV3_MODE_IS_INCLUDE
||
442 type
== IGMPV3_MODE_IS_EXCLUDE
;
443 truncate
= type
== IGMPV3_MODE_IS_EXCLUDE
||
444 type
== IGMPV3_CHANGE_TO_EXCLUDE
;
448 psf_list
= sdeleted
? &pmc
->tomb
: &pmc
->sources
;
453 pih
= skb
? igmpv3_report_hdr(skb
) : NULL
;
455 /* EX and TO_EX get a fresh packet, if needed */
457 if (pih
&& pih
->ngrec
&&
458 AVAILABLE(skb
) < grec_size(pmc
, type
, gdeleted
, sdeleted
)) {
460 igmpv3_sendpack(skb
);
461 skb
= igmpv3_newpack(dev
, dev
->mtu
);
466 for (psf
= *psf_list
; psf
; psf
= psf_next
) {
469 psf_next
= psf
->sf_next
;
471 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
)) {
476 /* clear marks on query responses */
480 if (AVAILABLE(skb
) < sizeof(__be32
) +
481 first
*sizeof(struct igmpv3_grec
)) {
482 if (truncate
&& !first
)
483 break; /* truncate these */
485 pgr
->grec_nsrcs
= htons(scount
);
487 igmpv3_sendpack(skb
);
488 skb
= igmpv3_newpack(dev
, dev
->mtu
);
493 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
498 psrc
= (__be32
*)skb_put(skb
, sizeof(__be32
));
499 *psrc
= psf
->sf_inaddr
;
501 if ((type
== IGMPV3_ALLOW_NEW_SOURCES
||
502 type
== IGMPV3_BLOCK_OLD_SOURCES
) && psf
->sf_crcount
) {
504 if ((sdeleted
|| gdeleted
) && psf
->sf_crcount
== 0) {
506 psf_prev
->sf_next
= psf
->sf_next
;
508 *psf_list
= psf
->sf_next
;
518 if (type
== IGMPV3_ALLOW_NEW_SOURCES
||
519 type
== IGMPV3_BLOCK_OLD_SOURCES
)
521 if (pmc
->crcount
|| isquery
) {
522 /* make sure we have room for group header */
523 if (skb
&& AVAILABLE(skb
) < sizeof(struct igmpv3_grec
)) {
524 igmpv3_sendpack(skb
);
525 skb
= NULL
; /* add_grhead will get a new one */
527 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
531 pgr
->grec_nsrcs
= htons(scount
);
534 pmc
->gsquery
= 0; /* clear query state on report */
538 static int igmpv3_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
)
540 struct sk_buff
*skb
= NULL
;
545 for_each_pmc_rcu(in_dev
, pmc
) {
546 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
548 spin_lock_bh(&pmc
->lock
);
549 if (pmc
->sfcount
[MCAST_EXCLUDE
])
550 type
= IGMPV3_MODE_IS_EXCLUDE
;
552 type
= IGMPV3_MODE_IS_INCLUDE
;
553 skb
= add_grec(skb
, pmc
, type
, 0, 0);
554 spin_unlock_bh(&pmc
->lock
);
558 spin_lock_bh(&pmc
->lock
);
559 if (pmc
->sfcount
[MCAST_EXCLUDE
])
560 type
= IGMPV3_MODE_IS_EXCLUDE
;
562 type
= IGMPV3_MODE_IS_INCLUDE
;
563 skb
= add_grec(skb
, pmc
, type
, 0, 0);
564 spin_unlock_bh(&pmc
->lock
);
568 return igmpv3_sendpack(skb
);
572 * remove zero-count source records from a source filter list
574 static void igmpv3_clear_zeros(struct ip_sf_list
**ppsf
)
576 struct ip_sf_list
*psf_prev
, *psf_next
, *psf
;
579 for (psf
= *ppsf
; psf
; psf
= psf_next
) {
580 psf_next
= psf
->sf_next
;
581 if (psf
->sf_crcount
== 0) {
583 psf_prev
->sf_next
= psf
->sf_next
;
585 *ppsf
= psf
->sf_next
;
592 static void igmpv3_send_cr(struct in_device
*in_dev
)
594 struct ip_mc_list
*pmc
, *pmc_prev
, *pmc_next
;
595 struct sk_buff
*skb
= NULL
;
599 spin_lock_bh(&in_dev
->mc_tomb_lock
);
603 for (pmc
= in_dev
->mc_tomb
; pmc
; pmc
= pmc_next
) {
604 pmc_next
= pmc
->next
;
605 if (pmc
->sfmode
== MCAST_INCLUDE
) {
606 type
= IGMPV3_BLOCK_OLD_SOURCES
;
607 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
608 skb
= add_grec(skb
, pmc
, type
, 1, 0);
609 skb
= add_grec(skb
, pmc
, dtype
, 1, 1);
612 if (pmc
->sfmode
== MCAST_EXCLUDE
) {
613 type
= IGMPV3_CHANGE_TO_INCLUDE
;
614 skb
= add_grec(skb
, pmc
, type
, 1, 0);
617 if (pmc
->crcount
== 0) {
618 igmpv3_clear_zeros(&pmc
->tomb
);
619 igmpv3_clear_zeros(&pmc
->sources
);
622 if (pmc
->crcount
== 0 && !pmc
->tomb
&& !pmc
->sources
) {
624 pmc_prev
->next
= pmc_next
;
626 in_dev
->mc_tomb
= pmc_next
;
627 in_dev_put(pmc
->interface
);
632 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
635 for_each_pmc_rcu(in_dev
, pmc
) {
636 spin_lock_bh(&pmc
->lock
);
637 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
638 type
= IGMPV3_BLOCK_OLD_SOURCES
;
639 dtype
= IGMPV3_ALLOW_NEW_SOURCES
;
641 type
= IGMPV3_ALLOW_NEW_SOURCES
;
642 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
644 skb
= add_grec(skb
, pmc
, type
, 0, 0);
645 skb
= add_grec(skb
, pmc
, dtype
, 0, 1); /* deleted sources */
647 /* filter mode changes */
649 if (pmc
->sfmode
== MCAST_EXCLUDE
)
650 type
= IGMPV3_CHANGE_TO_EXCLUDE
;
652 type
= IGMPV3_CHANGE_TO_INCLUDE
;
653 skb
= add_grec(skb
, pmc
, type
, 0, 0);
656 spin_unlock_bh(&pmc
->lock
);
662 (void) igmpv3_sendpack(skb
);
665 static int igmp_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
,
672 struct net_device
*dev
= in_dev
->dev
;
673 struct net
*net
= dev_net(dev
);
674 __be32 group
= pmc
? pmc
->multiaddr
: 0;
679 if (type
== IGMPV3_HOST_MEMBERSHIP_REPORT
)
680 return igmpv3_send_report(in_dev
, pmc
);
681 else if (type
== IGMP_HOST_LEAVE_MESSAGE
)
682 dst
= IGMP_ALL_ROUTER
;
686 rt
= ip_route_output_ports(net
, &fl4
, NULL
, dst
, 0,
688 IPPROTO_IGMP
, 0, dev
->ifindex
);
692 hlen
= LL_RESERVED_SPACE(dev
);
693 tlen
= dev
->needed_tailroom
;
694 skb
= alloc_skb(IGMP_SIZE
+ hlen
+ tlen
, GFP_ATOMIC
);
699 skb
->priority
= TC_PRIO_CONTROL
;
701 skb_dst_set(skb
, &rt
->dst
);
703 skb_reserve(skb
, hlen
);
705 skb_reset_network_header(skb
);
707 skb_put(skb
, sizeof(struct iphdr
) + 4);
710 iph
->ihl
= (sizeof(struct iphdr
)+4)>>2;
712 iph
->frag_off
= htons(IP_DF
);
715 iph
->saddr
= fl4
.saddr
;
716 iph
->protocol
= IPPROTO_IGMP
;
717 ip_select_ident(net
, skb
, NULL
);
718 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
719 ((u8
*)&iph
[1])[1] = 4;
720 ((u8
*)&iph
[1])[2] = 0;
721 ((u8
*)&iph
[1])[3] = 0;
723 ih
= (struct igmphdr
*)skb_put(skb
, sizeof(struct igmphdr
));
728 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
730 return ip_local_out(skb
);
733 static void igmp_gq_timer_expire(unsigned long data
)
735 struct in_device
*in_dev
= (struct in_device
*)data
;
737 in_dev
->mr_gq_running
= 0;
738 igmpv3_send_report(in_dev
, NULL
);
742 static void igmp_ifc_timer_expire(unsigned long data
)
744 struct in_device
*in_dev
= (struct in_device
*)data
;
746 igmpv3_send_cr(in_dev
);
747 if (in_dev
->mr_ifc_count
) {
748 in_dev
->mr_ifc_count
--;
749 igmp_ifc_start_timer(in_dev
,
750 unsolicited_report_interval(in_dev
));
755 static void igmp_ifc_event(struct in_device
*in_dev
)
757 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
759 in_dev
->mr_ifc_count
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
760 igmp_ifc_start_timer(in_dev
, 1);
764 static void igmp_timer_expire(unsigned long data
)
766 struct ip_mc_list
*im
= (struct ip_mc_list
*)data
;
767 struct in_device
*in_dev
= im
->interface
;
769 spin_lock(&im
->lock
);
772 if (im
->unsolicit_count
) {
773 im
->unsolicit_count
--;
774 igmp_start_timer(im
, unsolicited_report_interval(in_dev
));
777 spin_unlock(&im
->lock
);
779 if (IGMP_V1_SEEN(in_dev
))
780 igmp_send_report(in_dev
, im
, IGMP_HOST_MEMBERSHIP_REPORT
);
781 else if (IGMP_V2_SEEN(in_dev
))
782 igmp_send_report(in_dev
, im
, IGMPV2_HOST_MEMBERSHIP_REPORT
);
784 igmp_send_report(in_dev
, im
, IGMPV3_HOST_MEMBERSHIP_REPORT
);
789 /* mark EXCLUDE-mode sources */
790 static int igmp_xmarksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
792 struct ip_sf_list
*psf
;
796 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
799 for (i
= 0; i
< nsrcs
; i
++) {
800 /* skip inactive filters */
801 if (psf
->sf_count
[MCAST_INCLUDE
] ||
802 pmc
->sfcount
[MCAST_EXCLUDE
] !=
803 psf
->sf_count
[MCAST_EXCLUDE
])
805 if (srcs
[i
] == psf
->sf_inaddr
) {
812 if (scount
== nsrcs
) /* all sources excluded */
817 static int igmp_marksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
819 struct ip_sf_list
*psf
;
822 if (pmc
->sfmode
== MCAST_EXCLUDE
)
823 return igmp_xmarksources(pmc
, nsrcs
, srcs
);
825 /* mark INCLUDE-mode sources */
827 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
830 for (i
= 0; i
< nsrcs
; i
++)
831 if (srcs
[i
] == psf
->sf_inaddr
) {
845 /* return true if packet was dropped */
846 static bool igmp_heard_report(struct in_device
*in_dev
, __be32 group
)
848 struct ip_mc_list
*im
;
850 /* Timers are only set for non-local groups */
852 if (group
== IGMP_ALL_HOSTS
)
856 for_each_pmc_rcu(in_dev
, im
) {
857 if (im
->multiaddr
== group
) {
866 /* return true if packet was dropped */
867 static bool igmp_heard_query(struct in_device
*in_dev
, struct sk_buff
*skb
,
870 struct igmphdr
*ih
= igmp_hdr(skb
);
871 struct igmpv3_query
*ih3
= igmpv3_query_hdr(skb
);
872 struct ip_mc_list
*im
;
873 __be32 group
= ih
->group
;
880 /* Alas, old v1 router presents here. */
882 max_delay
= IGMP_QUERY_RESPONSE_INTERVAL
;
883 in_dev
->mr_v1_seen
= jiffies
+
884 IGMP_V1_ROUTER_PRESENT_TIMEOUT
;
887 /* v2 router present */
888 max_delay
= ih
->code
*(HZ
/IGMP_TIMER_SCALE
);
889 in_dev
->mr_v2_seen
= jiffies
+
890 IGMP_V2_ROUTER_PRESENT_TIMEOUT
;
892 /* cancel the interface change timer */
893 in_dev
->mr_ifc_count
= 0;
894 if (del_timer(&in_dev
->mr_ifc_timer
))
895 __in_dev_put(in_dev
);
896 /* clear deleted report items */
897 igmpv3_clear_delrec(in_dev
);
898 } else if (len
< 12) {
899 return true; /* ignore bogus packet; freed by caller */
900 } else if (IGMP_V1_SEEN(in_dev
)) {
901 /* This is a v3 query with v1 queriers present */
902 max_delay
= IGMP_QUERY_RESPONSE_INTERVAL
;
904 } else if (IGMP_V2_SEEN(in_dev
)) {
905 /* this is a v3 query with v2 queriers present;
906 * Interpretation of the max_delay code is problematic here.
907 * A real v2 host would use ih_code directly, while v3 has a
908 * different encoding. We use the v3 encoding as more likely
909 * to be intended in a v3 query.
911 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
913 max_delay
= 1; /* can't mod w/ 0 */
915 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)))
918 ih3
= igmpv3_query_hdr(skb
);
920 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)
921 + ntohs(ih3
->nsrcs
)*sizeof(__be32
)))
923 ih3
= igmpv3_query_hdr(skb
);
926 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
928 max_delay
= 1; /* can't mod w/ 0 */
929 in_dev
->mr_maxdelay
= max_delay
;
931 in_dev
->mr_qrv
= ih3
->qrv
;
932 if (!group
) { /* general query */
934 return true; /* no sources allowed */
935 igmp_gq_start_timer(in_dev
);
938 /* mark sources to include, if group & source-specific */
939 mark
= ih3
->nsrcs
!= 0;
943 * - Start the timers in all of our membership records
944 * that the query applies to for the interface on
945 * which the query arrived excl. those that belong
946 * to a "local" group (224.0.0.X)
947 * - For timers already running check if they need to
949 * - Use the igmp->igmp_code field as the maximum
953 for_each_pmc_rcu(in_dev
, im
) {
956 if (group
&& group
!= im
->multiaddr
)
958 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
960 spin_lock_bh(&im
->lock
);
962 im
->gsquery
= im
->gsquery
&& mark
;
965 changed
= !im
->gsquery
||
966 igmp_marksources(im
, ntohs(ih3
->nsrcs
), ih3
->srcs
);
967 spin_unlock_bh(&im
->lock
);
969 igmp_mod_timer(im
, max_delay
);
975 /* called in rcu_read_lock() section */
976 int igmp_rcv(struct sk_buff
*skb
)
978 /* This basically follows the spec line by line -- see RFC1112 */
980 struct in_device
*in_dev
= __in_dev_get_rcu(skb
->dev
);
987 if (!pskb_may_pull(skb
, sizeof(struct igmphdr
)))
990 if (skb_checksum_simple_validate(skb
))
995 case IGMP_HOST_MEMBERSHIP_QUERY
:
996 dropped
= igmp_heard_query(in_dev
, skb
, len
);
998 case IGMP_HOST_MEMBERSHIP_REPORT
:
999 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1000 /* Is it our report looped back? */
1001 if (rt_is_output_route(skb_rtable(skb
)))
1003 /* don't rely on MC router hearing unicast reports */
1004 if (skb
->pkt_type
== PACKET_MULTICAST
||
1005 skb
->pkt_type
== PACKET_BROADCAST
)
1006 dropped
= igmp_heard_report(in_dev
, ih
->group
);
1009 #ifdef CONFIG_IP_PIMSM_V1
1010 return pim_rcv_v1(skb
);
1012 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1015 case IGMP_HOST_LEAVE_MESSAGE
:
1017 case IGMP_MTRACE_RESP
:
1035 * Add a filter to a device
1038 static void ip_mc_filter_add(struct in_device
*in_dev
, __be32 addr
)
1040 char buf
[MAX_ADDR_LEN
];
1041 struct net_device
*dev
= in_dev
->dev
;
1043 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1044 We will get multicast token leakage, when IFF_MULTICAST
1045 is changed. This check should be done in ndo_set_rx_mode
1046 routine. Something sort of:
1047 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1050 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1051 dev_mc_add(dev
, buf
);
1055 * Remove a filter from a device
1058 static void ip_mc_filter_del(struct in_device
*in_dev
, __be32 addr
)
1060 char buf
[MAX_ADDR_LEN
];
1061 struct net_device
*dev
= in_dev
->dev
;
1063 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1064 dev_mc_del(dev
, buf
);
1067 #ifdef CONFIG_IP_MULTICAST
1069 * deleted ip_mc_list manipulation
1071 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
)
1073 struct ip_mc_list
*pmc
;
1075 /* this is an "ip_mc_list" for convenience; only the fields below
1076 * are actually used. In particular, the refcnt and users are not
1077 * used for management of the delete list. Using the same structure
1078 * for deleted items allows change reports to use common code with
1079 * non-deleted or query-response MCA's.
1081 pmc
= kzalloc(sizeof(*pmc
), GFP_KERNEL
);
1084 spin_lock_bh(&im
->lock
);
1085 pmc
->interface
= im
->interface
;
1086 in_dev_hold(in_dev
);
1087 pmc
->multiaddr
= im
->multiaddr
;
1088 pmc
->crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
1089 pmc
->sfmode
= im
->sfmode
;
1090 if (pmc
->sfmode
== MCAST_INCLUDE
) {
1091 struct ip_sf_list
*psf
;
1093 pmc
->tomb
= im
->tomb
;
1094 pmc
->sources
= im
->sources
;
1095 im
->tomb
= im
->sources
= NULL
;
1096 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1097 psf
->sf_crcount
= pmc
->crcount
;
1099 spin_unlock_bh(&im
->lock
);
1101 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1102 pmc
->next
= in_dev
->mc_tomb
;
1103 in_dev
->mc_tomb
= pmc
;
1104 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1107 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
)
1109 struct ip_mc_list
*pmc
, *pmc_prev
;
1110 struct ip_sf_list
*psf
, *psf_next
;
1112 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1114 for (pmc
= in_dev
->mc_tomb
; pmc
; pmc
= pmc
->next
) {
1115 if (pmc
->multiaddr
== multiaddr
)
1121 pmc_prev
->next
= pmc
->next
;
1123 in_dev
->mc_tomb
= pmc
->next
;
1125 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1127 for (psf
= pmc
->tomb
; psf
; psf
= psf_next
) {
1128 psf_next
= psf
->sf_next
;
1131 in_dev_put(pmc
->interface
);
1136 static void igmpv3_clear_delrec(struct in_device
*in_dev
)
1138 struct ip_mc_list
*pmc
, *nextpmc
;
1140 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1141 pmc
= in_dev
->mc_tomb
;
1142 in_dev
->mc_tomb
= NULL
;
1143 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1145 for (; pmc
; pmc
= nextpmc
) {
1146 nextpmc
= pmc
->next
;
1147 ip_mc_clear_src(pmc
);
1148 in_dev_put(pmc
->interface
);
1151 /* clear dead sources, too */
1153 for_each_pmc_rcu(in_dev
, pmc
) {
1154 struct ip_sf_list
*psf
, *psf_next
;
1156 spin_lock_bh(&pmc
->lock
);
1159 spin_unlock_bh(&pmc
->lock
);
1160 for (; psf
; psf
= psf_next
) {
1161 psf_next
= psf
->sf_next
;
1169 static void igmp_group_dropped(struct ip_mc_list
*im
)
1171 struct in_device
*in_dev
= im
->interface
;
1172 #ifdef CONFIG_IP_MULTICAST
1178 ip_mc_filter_del(in_dev
, im
->multiaddr
);
1181 #ifdef CONFIG_IP_MULTICAST
1182 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1185 reporter
= im
->reporter
;
1186 igmp_stop_timer(im
);
1188 if (!in_dev
->dead
) {
1189 if (IGMP_V1_SEEN(in_dev
))
1191 if (IGMP_V2_SEEN(in_dev
)) {
1193 igmp_send_report(in_dev
, im
, IGMP_HOST_LEAVE_MESSAGE
);
1197 igmpv3_add_delrec(in_dev
, im
);
1199 igmp_ifc_event(in_dev
);
1204 static void igmp_group_added(struct ip_mc_list
*im
)
1206 struct in_device
*in_dev
= im
->interface
;
1208 if (im
->loaded
== 0) {
1210 ip_mc_filter_add(in_dev
, im
->multiaddr
);
1213 #ifdef CONFIG_IP_MULTICAST
1214 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1219 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
)) {
1220 spin_lock_bh(&im
->lock
);
1221 igmp_start_timer(im
, IGMP_INITIAL_REPORT_DELAY
);
1222 spin_unlock_bh(&im
->lock
);
1227 im
->crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
1228 igmp_ifc_event(in_dev
);
1234 * Multicast list managers
1237 static u32
ip_mc_hash(const struct ip_mc_list
*im
)
1239 return hash_32((__force u32
)im
->multiaddr
, MC_HASH_SZ_LOG
);
1242 static void ip_mc_hash_add(struct in_device
*in_dev
,
1243 struct ip_mc_list
*im
)
1245 struct ip_mc_list __rcu
**mc_hash
;
1248 mc_hash
= rtnl_dereference(in_dev
->mc_hash
);
1250 hash
= ip_mc_hash(im
);
1251 im
->next_hash
= mc_hash
[hash
];
1252 rcu_assign_pointer(mc_hash
[hash
], im
);
1256 /* do not use a hash table for small number of items */
1257 if (in_dev
->mc_count
< 4)
1260 mc_hash
= kzalloc(sizeof(struct ip_mc_list
*) << MC_HASH_SZ_LOG
,
1265 for_each_pmc_rtnl(in_dev
, im
) {
1266 hash
= ip_mc_hash(im
);
1267 im
->next_hash
= mc_hash
[hash
];
1268 RCU_INIT_POINTER(mc_hash
[hash
], im
);
1271 rcu_assign_pointer(in_dev
->mc_hash
, mc_hash
);
1274 static void ip_mc_hash_remove(struct in_device
*in_dev
,
1275 struct ip_mc_list
*im
)
1277 struct ip_mc_list __rcu
**mc_hash
= rtnl_dereference(in_dev
->mc_hash
);
1278 struct ip_mc_list
*aux
;
1282 mc_hash
+= ip_mc_hash(im
);
1283 while ((aux
= rtnl_dereference(*mc_hash
)) != im
)
1284 mc_hash
= &aux
->next_hash
;
1285 *mc_hash
= im
->next_hash
;
1290 * A socket has joined a multicast group on device dev.
1293 void ip_mc_inc_group(struct in_device
*in_dev
, __be32 addr
)
1295 struct ip_mc_list
*im
;
1299 for_each_pmc_rtnl(in_dev
, im
) {
1300 if (im
->multiaddr
== addr
) {
1302 ip_mc_add_src(in_dev
, &addr
, MCAST_EXCLUDE
, 0, NULL
, 0);
1307 im
= kzalloc(sizeof(*im
), GFP_KERNEL
);
1312 im
->interface
= in_dev
;
1313 in_dev_hold(in_dev
);
1314 im
->multiaddr
= addr
;
1315 /* initial mode is (EX, empty) */
1316 im
->sfmode
= MCAST_EXCLUDE
;
1317 im
->sfcount
[MCAST_EXCLUDE
] = 1;
1318 atomic_set(&im
->refcnt
, 1);
1319 spin_lock_init(&im
->lock
);
1320 #ifdef CONFIG_IP_MULTICAST
1321 setup_timer(&im
->timer
, igmp_timer_expire
, (unsigned long)im
);
1322 im
->unsolicit_count
= sysctl_igmp_qrv
;
1325 im
->next_rcu
= in_dev
->mc_list
;
1327 rcu_assign_pointer(in_dev
->mc_list
, im
);
1329 ip_mc_hash_add(in_dev
, im
);
1331 #ifdef CONFIG_IP_MULTICAST
1332 igmpv3_del_delrec(in_dev
, im
->multiaddr
);
1334 igmp_group_added(im
);
1336 ip_rt_multicast_event(in_dev
);
1340 EXPORT_SYMBOL(ip_mc_inc_group
);
1343 * Resend IGMP JOIN report; used by netdev notifier.
1345 static void ip_mc_rejoin_groups(struct in_device
*in_dev
)
1347 #ifdef CONFIG_IP_MULTICAST
1348 struct ip_mc_list
*im
;
1353 for_each_pmc_rtnl(in_dev
, im
) {
1354 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1357 /* a failover is happening and switches
1358 * must be notified immediately
1360 if (IGMP_V1_SEEN(in_dev
))
1361 type
= IGMP_HOST_MEMBERSHIP_REPORT
;
1362 else if (IGMP_V2_SEEN(in_dev
))
1363 type
= IGMPV2_HOST_MEMBERSHIP_REPORT
;
1365 type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
1366 igmp_send_report(in_dev
, im
, type
);
1372 * A socket has left a multicast group on device dev
1375 void ip_mc_dec_group(struct in_device
*in_dev
, __be32 addr
)
1377 struct ip_mc_list
*i
;
1378 struct ip_mc_list __rcu
**ip
;
1382 for (ip
= &in_dev
->mc_list
;
1383 (i
= rtnl_dereference(*ip
)) != NULL
;
1384 ip
= &i
->next_rcu
) {
1385 if (i
->multiaddr
== addr
) {
1386 if (--i
->users
== 0) {
1387 ip_mc_hash_remove(in_dev
, i
);
1390 igmp_group_dropped(i
);
1394 ip_rt_multicast_event(in_dev
);
1403 EXPORT_SYMBOL(ip_mc_dec_group
);
1405 /* Device changing type */
1407 void ip_mc_unmap(struct in_device
*in_dev
)
1409 struct ip_mc_list
*pmc
;
1413 for_each_pmc_rtnl(in_dev
, pmc
)
1414 igmp_group_dropped(pmc
);
1417 void ip_mc_remap(struct in_device
*in_dev
)
1419 struct ip_mc_list
*pmc
;
1423 for_each_pmc_rtnl(in_dev
, pmc
)
1424 igmp_group_added(pmc
);
1427 /* Device going down */
1429 void ip_mc_down(struct in_device
*in_dev
)
1431 struct ip_mc_list
*pmc
;
1435 for_each_pmc_rtnl(in_dev
, pmc
)
1436 igmp_group_dropped(pmc
);
1438 #ifdef CONFIG_IP_MULTICAST
1439 in_dev
->mr_ifc_count
= 0;
1440 if (del_timer(&in_dev
->mr_ifc_timer
))
1441 __in_dev_put(in_dev
);
1442 in_dev
->mr_gq_running
= 0;
1443 if (del_timer(&in_dev
->mr_gq_timer
))
1444 __in_dev_put(in_dev
);
1445 igmpv3_clear_delrec(in_dev
);
1448 ip_mc_dec_group(in_dev
, IGMP_ALL_HOSTS
);
1451 void ip_mc_init_dev(struct in_device
*in_dev
)
1455 #ifdef CONFIG_IP_MULTICAST
1456 setup_timer(&in_dev
->mr_gq_timer
, igmp_gq_timer_expire
,
1457 (unsigned long)in_dev
);
1458 setup_timer(&in_dev
->mr_ifc_timer
, igmp_ifc_timer_expire
,
1459 (unsigned long)in_dev
);
1460 in_dev
->mr_qrv
= sysctl_igmp_qrv
;
1463 spin_lock_init(&in_dev
->mc_tomb_lock
);
1466 /* Device going up */
1468 void ip_mc_up(struct in_device
*in_dev
)
1470 struct ip_mc_list
*pmc
;
1474 #ifdef CONFIG_IP_MULTICAST
1475 in_dev
->mr_qrv
= sysctl_igmp_qrv
;
1477 ip_mc_inc_group(in_dev
, IGMP_ALL_HOSTS
);
1479 for_each_pmc_rtnl(in_dev
, pmc
)
1480 igmp_group_added(pmc
);
1484 * Device is about to be destroyed: clean up.
1487 void ip_mc_destroy_dev(struct in_device
*in_dev
)
1489 struct ip_mc_list
*i
;
1493 /* Deactivate timers */
1496 while ((i
= rtnl_dereference(in_dev
->mc_list
)) != NULL
) {
1497 in_dev
->mc_list
= i
->next_rcu
;
1500 /* We've dropped the groups in ip_mc_down already */
1506 /* RTNL is locked */
1507 static struct in_device
*ip_mc_find_dev(struct net
*net
, struct ip_mreqn
*imr
)
1509 struct net_device
*dev
= NULL
;
1510 struct in_device
*idev
= NULL
;
1512 if (imr
->imr_ifindex
) {
1513 idev
= inetdev_by_index(net
, imr
->imr_ifindex
);
1516 if (imr
->imr_address
.s_addr
) {
1517 dev
= __ip_dev_find(net
, imr
->imr_address
.s_addr
, false);
1523 struct rtable
*rt
= ip_route_output(net
,
1524 imr
->imr_multiaddr
.s_addr
,
1532 imr
->imr_ifindex
= dev
->ifindex
;
1533 idev
= __in_dev_get_rtnl(dev
);
1539 * Join a socket to a group
1541 int sysctl_igmp_max_memberships __read_mostly
= IP_MAX_MEMBERSHIPS
;
1542 int sysctl_igmp_max_msf __read_mostly
= IP_MAX_MSF
;
1543 #ifdef CONFIG_IP_MULTICAST
1544 int sysctl_igmp_qrv __read_mostly
= IGMP_QUERY_ROBUSTNESS_VARIABLE
;
1547 static int ip_mc_del1_src(struct ip_mc_list
*pmc
, int sfmode
,
1550 struct ip_sf_list
*psf
, *psf_prev
;
1554 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1555 if (psf
->sf_inaddr
== *psfsrc
)
1559 if (!psf
|| psf
->sf_count
[sfmode
] == 0) {
1560 /* source filter not found, or count wrong => bug */
1563 psf
->sf_count
[sfmode
]--;
1564 if (psf
->sf_count
[sfmode
] == 0) {
1565 ip_rt_multicast_event(pmc
->interface
);
1567 if (!psf
->sf_count
[MCAST_INCLUDE
] && !psf
->sf_count
[MCAST_EXCLUDE
]) {
1568 #ifdef CONFIG_IP_MULTICAST
1569 struct in_device
*in_dev
= pmc
->interface
;
1572 /* no more filters for this source */
1574 psf_prev
->sf_next
= psf
->sf_next
;
1576 pmc
->sources
= psf
->sf_next
;
1577 #ifdef CONFIG_IP_MULTICAST
1578 if (psf
->sf_oldin
&&
1579 !IGMP_V1_SEEN(in_dev
) && !IGMP_V2_SEEN(in_dev
)) {
1580 psf
->sf_crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
1581 psf
->sf_next
= pmc
->tomb
;
1591 #ifndef CONFIG_IP_MULTICAST
1592 #define igmp_ifc_event(x) do { } while (0)
1595 static int ip_mc_del_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1596 int sfcount
, __be32
*psfsrc
, int delta
)
1598 struct ip_mc_list
*pmc
;
1605 for_each_pmc_rcu(in_dev
, pmc
) {
1606 if (*pmca
== pmc
->multiaddr
)
1610 /* MCA not found?? bug */
1614 spin_lock_bh(&pmc
->lock
);
1616 #ifdef CONFIG_IP_MULTICAST
1621 if (!pmc
->sfcount
[sfmode
])
1623 pmc
->sfcount
[sfmode
]--;
1626 for (i
= 0; i
< sfcount
; i
++) {
1627 int rv
= ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1629 changerec
|= rv
> 0;
1633 if (pmc
->sfmode
== MCAST_EXCLUDE
&&
1634 pmc
->sfcount
[MCAST_EXCLUDE
] == 0 &&
1635 pmc
->sfcount
[MCAST_INCLUDE
]) {
1636 #ifdef CONFIG_IP_MULTICAST
1637 struct ip_sf_list
*psf
;
1640 /* filter mode change */
1641 pmc
->sfmode
= MCAST_INCLUDE
;
1642 #ifdef CONFIG_IP_MULTICAST
1643 pmc
->crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
1644 in_dev
->mr_ifc_count
= pmc
->crcount
;
1645 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1646 psf
->sf_crcount
= 0;
1647 igmp_ifc_event(pmc
->interface
);
1648 } else if (sf_setstate(pmc
) || changerec
) {
1649 igmp_ifc_event(pmc
->interface
);
1653 spin_unlock_bh(&pmc
->lock
);
1658 * Add multicast single-source filter to the interface list
1660 static int ip_mc_add1_src(struct ip_mc_list
*pmc
, int sfmode
,
1663 struct ip_sf_list
*psf
, *psf_prev
;
1666 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1667 if (psf
->sf_inaddr
== *psfsrc
)
1672 psf
= kzalloc(sizeof(*psf
), GFP_ATOMIC
);
1675 psf
->sf_inaddr
= *psfsrc
;
1677 psf_prev
->sf_next
= psf
;
1681 psf
->sf_count
[sfmode
]++;
1682 if (psf
->sf_count
[sfmode
] == 1) {
1683 ip_rt_multicast_event(pmc
->interface
);
1688 #ifdef CONFIG_IP_MULTICAST
1689 static void sf_markstate(struct ip_mc_list
*pmc
)
1691 struct ip_sf_list
*psf
;
1692 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1694 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1695 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1696 psf
->sf_oldin
= mca_xcount
==
1697 psf
->sf_count
[MCAST_EXCLUDE
] &&
1698 !psf
->sf_count
[MCAST_INCLUDE
];
1700 psf
->sf_oldin
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1703 static int sf_setstate(struct ip_mc_list
*pmc
)
1705 struct ip_sf_list
*psf
, *dpsf
;
1706 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1707 int qrv
= pmc
->interface
->mr_qrv
;
1711 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1712 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1713 new_in
= mca_xcount
== psf
->sf_count
[MCAST_EXCLUDE
] &&
1714 !psf
->sf_count
[MCAST_INCLUDE
];
1716 new_in
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1718 if (!psf
->sf_oldin
) {
1719 struct ip_sf_list
*prev
= NULL
;
1721 for (dpsf
= pmc
->tomb
; dpsf
; dpsf
= dpsf
->sf_next
) {
1722 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1728 prev
->sf_next
= dpsf
->sf_next
;
1730 pmc
->tomb
= dpsf
->sf_next
;
1733 psf
->sf_crcount
= qrv
;
1736 } else if (psf
->sf_oldin
) {
1738 psf
->sf_crcount
= 0;
1740 * add or update "delete" records if an active filter
1743 for (dpsf
= pmc
->tomb
; dpsf
; dpsf
= dpsf
->sf_next
)
1744 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1747 dpsf
= kmalloc(sizeof(*dpsf
), GFP_ATOMIC
);
1751 /* pmc->lock held by callers */
1752 dpsf
->sf_next
= pmc
->tomb
;
1755 dpsf
->sf_crcount
= qrv
;
1764 * Add multicast source filter list to the interface list
1766 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1767 int sfcount
, __be32
*psfsrc
, int delta
)
1769 struct ip_mc_list
*pmc
;
1776 for_each_pmc_rcu(in_dev
, pmc
) {
1777 if (*pmca
== pmc
->multiaddr
)
1781 /* MCA not found?? bug */
1785 spin_lock_bh(&pmc
->lock
);
1788 #ifdef CONFIG_IP_MULTICAST
1791 isexclude
= pmc
->sfmode
== MCAST_EXCLUDE
;
1793 pmc
->sfcount
[sfmode
]++;
1795 for (i
= 0; i
< sfcount
; i
++) {
1796 err
= ip_mc_add1_src(pmc
, sfmode
, &psfsrc
[i
]);
1804 pmc
->sfcount
[sfmode
]--;
1805 for (j
= 0; j
< i
; j
++)
1806 (void) ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[j
]);
1807 } else if (isexclude
!= (pmc
->sfcount
[MCAST_EXCLUDE
] != 0)) {
1808 #ifdef CONFIG_IP_MULTICAST
1809 struct ip_sf_list
*psf
;
1810 in_dev
= pmc
->interface
;
1813 /* filter mode change */
1814 if (pmc
->sfcount
[MCAST_EXCLUDE
])
1815 pmc
->sfmode
= MCAST_EXCLUDE
;
1816 else if (pmc
->sfcount
[MCAST_INCLUDE
])
1817 pmc
->sfmode
= MCAST_INCLUDE
;
1818 #ifdef CONFIG_IP_MULTICAST
1819 /* else no filters; keep old mode for reports */
1821 pmc
->crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
1822 in_dev
->mr_ifc_count
= pmc
->crcount
;
1823 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1824 psf
->sf_crcount
= 0;
1825 igmp_ifc_event(in_dev
);
1826 } else if (sf_setstate(pmc
)) {
1827 igmp_ifc_event(in_dev
);
1830 spin_unlock_bh(&pmc
->lock
);
1834 static void ip_mc_clear_src(struct ip_mc_list
*pmc
)
1836 struct ip_sf_list
*psf
, *nextpsf
;
1838 for (psf
= pmc
->tomb
; psf
; psf
= nextpsf
) {
1839 nextpsf
= psf
->sf_next
;
1843 for (psf
= pmc
->sources
; psf
; psf
= nextpsf
) {
1844 nextpsf
= psf
->sf_next
;
1847 pmc
->sources
= NULL
;
1848 pmc
->sfmode
= MCAST_EXCLUDE
;
1849 pmc
->sfcount
[MCAST_INCLUDE
] = 0;
1850 pmc
->sfcount
[MCAST_EXCLUDE
] = 1;
1853 /* Join a multicast group
1856 int ip_mc_join_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1858 __be32 addr
= imr
->imr_multiaddr
.s_addr
;
1859 struct ip_mc_socklist
*iml
, *i
;
1860 struct in_device
*in_dev
;
1861 struct inet_sock
*inet
= inet_sk(sk
);
1862 struct net
*net
= sock_net(sk
);
1869 if (!ipv4_is_multicast(addr
))
1872 in_dev
= ip_mc_find_dev(net
, imr
);
1880 ifindex
= imr
->imr_ifindex
;
1881 for_each_pmc_rtnl(inet
, i
) {
1882 if (i
->multi
.imr_multiaddr
.s_addr
== addr
&&
1883 i
->multi
.imr_ifindex
== ifindex
)
1888 if (count
>= sysctl_igmp_max_memberships
)
1890 iml
= sock_kmalloc(sk
, sizeof(*iml
), GFP_KERNEL
);
1894 memcpy(&iml
->multi
, imr
, sizeof(*imr
));
1895 iml
->next_rcu
= inet
->mc_list
;
1897 iml
->sfmode
= MCAST_EXCLUDE
;
1898 rcu_assign_pointer(inet
->mc_list
, iml
);
1899 ip_mc_inc_group(in_dev
, addr
);
1904 EXPORT_SYMBOL(ip_mc_join_group
);
1906 static int ip_mc_leave_src(struct sock
*sk
, struct ip_mc_socklist
*iml
,
1907 struct in_device
*in_dev
)
1909 struct ip_sf_socklist
*psf
= rtnl_dereference(iml
->sflist
);
1913 /* any-source empty exclude case */
1914 return ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1915 iml
->sfmode
, 0, NULL
, 0);
1917 err
= ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1918 iml
->sfmode
, psf
->sl_count
, psf
->sl_addr
, 0);
1919 RCU_INIT_POINTER(iml
->sflist
, NULL
);
1920 /* decrease mem now to avoid the memleak warning */
1921 atomic_sub(IP_SFLSIZE(psf
->sl_max
), &sk
->sk_omem_alloc
);
1922 kfree_rcu(psf
, rcu
);
1926 int ip_mc_leave_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1928 struct inet_sock
*inet
= inet_sk(sk
);
1929 struct ip_mc_socklist
*iml
;
1930 struct ip_mc_socklist __rcu
**imlp
;
1931 struct in_device
*in_dev
;
1932 struct net
*net
= sock_net(sk
);
1933 __be32 group
= imr
->imr_multiaddr
.s_addr
;
1935 int ret
= -EADDRNOTAVAIL
;
1939 in_dev
= ip_mc_find_dev(net
, imr
);
1944 ifindex
= imr
->imr_ifindex
;
1945 for (imlp
= &inet
->mc_list
;
1946 (iml
= rtnl_dereference(*imlp
)) != NULL
;
1947 imlp
= &iml
->next_rcu
) {
1948 if (iml
->multi
.imr_multiaddr
.s_addr
!= group
)
1951 if (iml
->multi
.imr_ifindex
!= ifindex
)
1953 } else if (imr
->imr_address
.s_addr
&& imr
->imr_address
.s_addr
!=
1954 iml
->multi
.imr_address
.s_addr
)
1957 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
1959 *imlp
= iml
->next_rcu
;
1961 ip_mc_dec_group(in_dev
, group
);
1963 /* decrease mem now to avoid the memleak warning */
1964 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
1965 kfree_rcu(iml
, rcu
);
1971 EXPORT_SYMBOL(ip_mc_leave_group
);
1973 int ip_mc_source(int add
, int omode
, struct sock
*sk
, struct
1974 ip_mreq_source
*mreqs
, int ifindex
)
1977 struct ip_mreqn imr
;
1978 __be32 addr
= mreqs
->imr_multiaddr
;
1979 struct ip_mc_socklist
*pmc
;
1980 struct in_device
*in_dev
= NULL
;
1981 struct inet_sock
*inet
= inet_sk(sk
);
1982 struct ip_sf_socklist
*psl
;
1983 struct net
*net
= sock_net(sk
);
1987 if (!ipv4_is_multicast(addr
))
1992 imr
.imr_multiaddr
.s_addr
= mreqs
->imr_multiaddr
;
1993 imr
.imr_address
.s_addr
= mreqs
->imr_interface
;
1994 imr
.imr_ifindex
= ifindex
;
1995 in_dev
= ip_mc_find_dev(net
, &imr
);
2001 err
= -EADDRNOTAVAIL
;
2003 for_each_pmc_rtnl(inet
, pmc
) {
2004 if ((pmc
->multi
.imr_multiaddr
.s_addr
==
2005 imr
.imr_multiaddr
.s_addr
) &&
2006 (pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
))
2009 if (!pmc
) { /* must have a prior join */
2013 /* if a source filter was set, must be the same mode as before */
2015 if (pmc
->sfmode
!= omode
) {
2019 } else if (pmc
->sfmode
!= omode
) {
2020 /* allow mode switches for empty-set filters */
2021 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 0, NULL
, 0);
2022 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, pmc
->sfmode
, 0,
2024 pmc
->sfmode
= omode
;
2027 psl
= rtnl_dereference(pmc
->sflist
);
2030 goto done
; /* err = -EADDRNOTAVAIL */
2032 for (i
= 0; i
< psl
->sl_count
; i
++) {
2033 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2038 if (rv
) /* source not found */
2039 goto done
; /* err = -EADDRNOTAVAIL */
2041 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2042 if (psl
->sl_count
== 1 && omode
== MCAST_INCLUDE
) {
2047 /* update the interface filter */
2048 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2049 &mreqs
->imr_sourceaddr
, 1);
2051 for (j
= i
+1; j
< psl
->sl_count
; j
++)
2052 psl
->sl_addr
[j
-1] = psl
->sl_addr
[j
];
2057 /* else, add a new source to the filter */
2059 if (psl
&& psl
->sl_count
>= sysctl_igmp_max_msf
) {
2063 if (!psl
|| psl
->sl_count
== psl
->sl_max
) {
2064 struct ip_sf_socklist
*newpsl
;
2065 int count
= IP_SFBLOCK
;
2068 count
+= psl
->sl_max
;
2069 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(count
), GFP_KERNEL
);
2074 newpsl
->sl_max
= count
;
2075 newpsl
->sl_count
= count
- IP_SFBLOCK
;
2077 for (i
= 0; i
< psl
->sl_count
; i
++)
2078 newpsl
->sl_addr
[i
] = psl
->sl_addr
[i
];
2079 /* decrease mem now to avoid the memleak warning */
2080 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2081 kfree_rcu(psl
, rcu
);
2083 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2086 rv
= 1; /* > 0 for insert logic below if sl_count is 0 */
2087 for (i
= 0; i
< psl
->sl_count
; i
++) {
2088 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2093 if (rv
== 0) /* address already there is an error */
2095 for (j
= psl
->sl_count
-1; j
>= i
; j
--)
2096 psl
->sl_addr
[j
+1] = psl
->sl_addr
[j
];
2097 psl
->sl_addr
[i
] = mreqs
->imr_sourceaddr
;
2100 /* update the interface list */
2101 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2102 &mreqs
->imr_sourceaddr
, 1);
2105 err
= ip_mc_leave_group(sk
, &imr
);
2109 int ip_mc_msfilter(struct sock
*sk
, struct ip_msfilter
*msf
, int ifindex
)
2112 struct ip_mreqn imr
;
2113 __be32 addr
= msf
->imsf_multiaddr
;
2114 struct ip_mc_socklist
*pmc
;
2115 struct in_device
*in_dev
;
2116 struct inet_sock
*inet
= inet_sk(sk
);
2117 struct ip_sf_socklist
*newpsl
, *psl
;
2118 struct net
*net
= sock_net(sk
);
2121 if (!ipv4_is_multicast(addr
))
2123 if (msf
->imsf_fmode
!= MCAST_INCLUDE
&&
2124 msf
->imsf_fmode
!= MCAST_EXCLUDE
)
2129 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2130 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2131 imr
.imr_ifindex
= ifindex
;
2132 in_dev
= ip_mc_find_dev(net
, &imr
);
2139 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2140 if (msf
->imsf_fmode
== MCAST_INCLUDE
&& msf
->imsf_numsrc
== 0) {
2145 for_each_pmc_rtnl(inet
, pmc
) {
2146 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2147 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2150 if (!pmc
) { /* must have a prior join */
2154 if (msf
->imsf_numsrc
) {
2155 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(msf
->imsf_numsrc
),
2161 newpsl
->sl_max
= newpsl
->sl_count
= msf
->imsf_numsrc
;
2162 memcpy(newpsl
->sl_addr
, msf
->imsf_slist
,
2163 msf
->imsf_numsrc
* sizeof(msf
->imsf_slist
[0]));
2164 err
= ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2165 msf
->imsf_fmode
, newpsl
->sl_count
, newpsl
->sl_addr
, 0);
2167 sock_kfree_s(sk
, newpsl
, IP_SFLSIZE(newpsl
->sl_max
));
2172 (void) ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2173 msf
->imsf_fmode
, 0, NULL
, 0);
2175 psl
= rtnl_dereference(pmc
->sflist
);
2177 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2178 psl
->sl_count
, psl
->sl_addr
, 0);
2179 /* decrease mem now to avoid the memleak warning */
2180 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2181 kfree_rcu(psl
, rcu
);
2183 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2185 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2186 pmc
->sfmode
= msf
->imsf_fmode
;
2190 err
= ip_mc_leave_group(sk
, &imr
);
2194 int ip_mc_msfget(struct sock
*sk
, struct ip_msfilter
*msf
,
2195 struct ip_msfilter __user
*optval
, int __user
*optlen
)
2197 int err
, len
, count
, copycount
;
2198 struct ip_mreqn imr
;
2199 __be32 addr
= msf
->imsf_multiaddr
;
2200 struct ip_mc_socklist
*pmc
;
2201 struct in_device
*in_dev
;
2202 struct inet_sock
*inet
= inet_sk(sk
);
2203 struct ip_sf_socklist
*psl
;
2204 struct net
*net
= sock_net(sk
);
2206 if (!ipv4_is_multicast(addr
))
2211 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2212 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2213 imr
.imr_ifindex
= 0;
2214 in_dev
= ip_mc_find_dev(net
, &imr
);
2220 err
= -EADDRNOTAVAIL
;
2222 for_each_pmc_rtnl(inet
, pmc
) {
2223 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2224 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2227 if (!pmc
) /* must have a prior join */
2229 msf
->imsf_fmode
= pmc
->sfmode
;
2230 psl
= rtnl_dereference(pmc
->sflist
);
2236 count
= psl
->sl_count
;
2238 copycount
= count
< msf
->imsf_numsrc
? count
: msf
->imsf_numsrc
;
2239 len
= copycount
* sizeof(psl
->sl_addr
[0]);
2240 msf
->imsf_numsrc
= count
;
2241 if (put_user(IP_MSFILTER_SIZE(copycount
), optlen
) ||
2242 copy_to_user(optval
, msf
, IP_MSFILTER_SIZE(0))) {
2246 copy_to_user(&optval
->imsf_slist
[0], psl
->sl_addr
, len
))
2254 int ip_mc_gsfget(struct sock
*sk
, struct group_filter
*gsf
,
2255 struct group_filter __user
*optval
, int __user
*optlen
)
2257 int err
, i
, count
, copycount
;
2258 struct sockaddr_in
*psin
;
2260 struct ip_mc_socklist
*pmc
;
2261 struct inet_sock
*inet
= inet_sk(sk
);
2262 struct ip_sf_socklist
*psl
;
2264 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
2265 if (psin
->sin_family
!= AF_INET
)
2267 addr
= psin
->sin_addr
.s_addr
;
2268 if (!ipv4_is_multicast(addr
))
2273 err
= -EADDRNOTAVAIL
;
2275 for_each_pmc_rtnl(inet
, pmc
) {
2276 if (pmc
->multi
.imr_multiaddr
.s_addr
== addr
&&
2277 pmc
->multi
.imr_ifindex
== gsf
->gf_interface
)
2280 if (!pmc
) /* must have a prior join */
2282 gsf
->gf_fmode
= pmc
->sfmode
;
2283 psl
= rtnl_dereference(pmc
->sflist
);
2285 count
= psl
? psl
->sl_count
: 0;
2286 copycount
= count
< gsf
->gf_numsrc
? count
: gsf
->gf_numsrc
;
2287 gsf
->gf_numsrc
= count
;
2288 if (put_user(GROUP_FILTER_SIZE(copycount
), optlen
) ||
2289 copy_to_user(optval
, gsf
, GROUP_FILTER_SIZE(0))) {
2292 for (i
= 0; i
< copycount
; i
++) {
2293 struct sockaddr_storage ss
;
2295 psin
= (struct sockaddr_in
*)&ss
;
2296 memset(&ss
, 0, sizeof(ss
));
2297 psin
->sin_family
= AF_INET
;
2298 psin
->sin_addr
.s_addr
= psl
->sl_addr
[i
];
2299 if (copy_to_user(&optval
->gf_slist
[i
], &ss
, sizeof(ss
)))
2309 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2311 int ip_mc_sf_allow(struct sock
*sk
, __be32 loc_addr
, __be32 rmt_addr
, int dif
)
2313 struct inet_sock
*inet
= inet_sk(sk
);
2314 struct ip_mc_socklist
*pmc
;
2315 struct ip_sf_socklist
*psl
;
2320 if (!ipv4_is_multicast(loc_addr
))
2324 for_each_pmc_rcu(inet
, pmc
) {
2325 if (pmc
->multi
.imr_multiaddr
.s_addr
== loc_addr
&&
2326 pmc
->multi
.imr_ifindex
== dif
)
2332 psl
= rcu_dereference(pmc
->sflist
);
2333 ret
= (pmc
->sfmode
== MCAST_EXCLUDE
);
2337 for (i
= 0; i
< psl
->sl_count
; i
++) {
2338 if (psl
->sl_addr
[i
] == rmt_addr
)
2342 if (pmc
->sfmode
== MCAST_INCLUDE
&& i
>= psl
->sl_count
)
2344 if (pmc
->sfmode
== MCAST_EXCLUDE
&& i
< psl
->sl_count
)
2354 * A socket is closing.
2357 void ip_mc_drop_socket(struct sock
*sk
)
2359 struct inet_sock
*inet
= inet_sk(sk
);
2360 struct ip_mc_socklist
*iml
;
2361 struct net
*net
= sock_net(sk
);
2367 while ((iml
= rtnl_dereference(inet
->mc_list
)) != NULL
) {
2368 struct in_device
*in_dev
;
2370 inet
->mc_list
= iml
->next_rcu
;
2371 in_dev
= inetdev_by_index(net
, iml
->multi
.imr_ifindex
);
2372 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2374 ip_mc_dec_group(in_dev
, iml
->multi
.imr_multiaddr
.s_addr
);
2375 /* decrease mem now to avoid the memleak warning */
2376 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
2377 kfree_rcu(iml
, rcu
);
2382 /* called with rcu_read_lock() */
2383 int ip_check_mc_rcu(struct in_device
*in_dev
, __be32 mc_addr
, __be32 src_addr
, u16 proto
)
2385 struct ip_mc_list
*im
;
2386 struct ip_mc_list __rcu
**mc_hash
;
2387 struct ip_sf_list
*psf
;
2390 mc_hash
= rcu_dereference(in_dev
->mc_hash
);
2392 u32 hash
= hash_32((__force u32
)mc_addr
, MC_HASH_SZ_LOG
);
2394 for (im
= rcu_dereference(mc_hash
[hash
]);
2396 im
= rcu_dereference(im
->next_hash
)) {
2397 if (im
->multiaddr
== mc_addr
)
2401 for_each_pmc_rcu(in_dev
, im
) {
2402 if (im
->multiaddr
== mc_addr
)
2406 if (im
&& proto
== IPPROTO_IGMP
) {
2410 for (psf
= im
->sources
; psf
; psf
= psf
->sf_next
) {
2411 if (psf
->sf_inaddr
== src_addr
)
2415 rv
= psf
->sf_count
[MCAST_INCLUDE
] ||
2416 psf
->sf_count
[MCAST_EXCLUDE
] !=
2417 im
->sfcount
[MCAST_EXCLUDE
];
2419 rv
= im
->sfcount
[MCAST_EXCLUDE
] != 0;
2421 rv
= 1; /* unspecified source; tentatively allow */
2426 #if defined(CONFIG_PROC_FS)
2427 struct igmp_mc_iter_state
{
2428 struct seq_net_private p
;
2429 struct net_device
*dev
;
2430 struct in_device
*in_dev
;
2433 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2435 static inline struct ip_mc_list
*igmp_mc_get_first(struct seq_file
*seq
)
2437 struct net
*net
= seq_file_net(seq
);
2438 struct ip_mc_list
*im
= NULL
;
2439 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2441 state
->in_dev
= NULL
;
2442 for_each_netdev_rcu(net
, state
->dev
) {
2443 struct in_device
*in_dev
;
2445 in_dev
= __in_dev_get_rcu(state
->dev
);
2448 im
= rcu_dereference(in_dev
->mc_list
);
2450 state
->in_dev
= in_dev
;
2457 static struct ip_mc_list
*igmp_mc_get_next(struct seq_file
*seq
, struct ip_mc_list
*im
)
2459 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2461 im
= rcu_dereference(im
->next_rcu
);
2463 state
->dev
= next_net_device_rcu(state
->dev
);
2465 state
->in_dev
= NULL
;
2468 state
->in_dev
= __in_dev_get_rcu(state
->dev
);
2471 im
= rcu_dereference(state
->in_dev
->mc_list
);
2476 static struct ip_mc_list
*igmp_mc_get_idx(struct seq_file
*seq
, loff_t pos
)
2478 struct ip_mc_list
*im
= igmp_mc_get_first(seq
);
2480 while (pos
&& (im
= igmp_mc_get_next(seq
, im
)) != NULL
)
2482 return pos
? NULL
: im
;
2485 static void *igmp_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2489 return *pos
? igmp_mc_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2492 static void *igmp_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2494 struct ip_mc_list
*im
;
2495 if (v
== SEQ_START_TOKEN
)
2496 im
= igmp_mc_get_first(seq
);
2498 im
= igmp_mc_get_next(seq
, v
);
2503 static void igmp_mc_seq_stop(struct seq_file
*seq
, void *v
)
2506 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2508 state
->in_dev
= NULL
;
2513 static int igmp_mc_seq_show(struct seq_file
*seq
, void *v
)
2515 if (v
== SEQ_START_TOKEN
)
2517 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2519 struct ip_mc_list
*im
= (struct ip_mc_list
*)v
;
2520 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2524 #ifdef CONFIG_IP_MULTICAST
2525 querier
= IGMP_V1_SEEN(state
->in_dev
) ? "V1" :
2526 IGMP_V2_SEEN(state
->in_dev
) ? "V2" :
2532 if (rcu_access_pointer(state
->in_dev
->mc_list
) == im
) {
2533 seq_printf(seq
, "%d\t%-10s: %5d %7s\n",
2534 state
->dev
->ifindex
, state
->dev
->name
, state
->in_dev
->mc_count
, querier
);
2537 delta
= im
->timer
.expires
- jiffies
;
2539 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2540 im
->multiaddr
, im
->users
,
2542 im
->tm_running
? jiffies_delta_to_clock_t(delta
) : 0,
2548 static const struct seq_operations igmp_mc_seq_ops
= {
2549 .start
= igmp_mc_seq_start
,
2550 .next
= igmp_mc_seq_next
,
2551 .stop
= igmp_mc_seq_stop
,
2552 .show
= igmp_mc_seq_show
,
2555 static int igmp_mc_seq_open(struct inode
*inode
, struct file
*file
)
2557 return seq_open_net(inode
, file
, &igmp_mc_seq_ops
,
2558 sizeof(struct igmp_mc_iter_state
));
2561 static const struct file_operations igmp_mc_seq_fops
= {
2562 .owner
= THIS_MODULE
,
2563 .open
= igmp_mc_seq_open
,
2565 .llseek
= seq_lseek
,
2566 .release
= seq_release_net
,
2569 struct igmp_mcf_iter_state
{
2570 struct seq_net_private p
;
2571 struct net_device
*dev
;
2572 struct in_device
*idev
;
2573 struct ip_mc_list
*im
;
2576 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2578 static inline struct ip_sf_list
*igmp_mcf_get_first(struct seq_file
*seq
)
2580 struct net
*net
= seq_file_net(seq
);
2581 struct ip_sf_list
*psf
= NULL
;
2582 struct ip_mc_list
*im
= NULL
;
2583 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2587 for_each_netdev_rcu(net
, state
->dev
) {
2588 struct in_device
*idev
;
2589 idev
= __in_dev_get_rcu(state
->dev
);
2590 if (unlikely(!idev
))
2592 im
= rcu_dereference(idev
->mc_list
);
2594 spin_lock_bh(&im
->lock
);
2601 spin_unlock_bh(&im
->lock
);
2607 static struct ip_sf_list
*igmp_mcf_get_next(struct seq_file
*seq
, struct ip_sf_list
*psf
)
2609 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2613 spin_unlock_bh(&state
->im
->lock
);
2614 state
->im
= state
->im
->next
;
2615 while (!state
->im
) {
2616 state
->dev
= next_net_device_rcu(state
->dev
);
2621 state
->idev
= __in_dev_get_rcu(state
->dev
);
2624 state
->im
= rcu_dereference(state
->idev
->mc_list
);
2628 spin_lock_bh(&state
->im
->lock
);
2629 psf
= state
->im
->sources
;
2635 static struct ip_sf_list
*igmp_mcf_get_idx(struct seq_file
*seq
, loff_t pos
)
2637 struct ip_sf_list
*psf
= igmp_mcf_get_first(seq
);
2639 while (pos
&& (psf
= igmp_mcf_get_next(seq
, psf
)) != NULL
)
2641 return pos
? NULL
: psf
;
2644 static void *igmp_mcf_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2648 return *pos
? igmp_mcf_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2651 static void *igmp_mcf_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2653 struct ip_sf_list
*psf
;
2654 if (v
== SEQ_START_TOKEN
)
2655 psf
= igmp_mcf_get_first(seq
);
2657 psf
= igmp_mcf_get_next(seq
, v
);
2662 static void igmp_mcf_seq_stop(struct seq_file
*seq
, void *v
)
2665 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2666 if (likely(state
->im
)) {
2667 spin_unlock_bh(&state
->im
->lock
);
2675 static int igmp_mcf_seq_show(struct seq_file
*seq
, void *v
)
2677 struct ip_sf_list
*psf
= (struct ip_sf_list
*)v
;
2678 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2680 if (v
== SEQ_START_TOKEN
) {
2681 seq_puts(seq
, "Idx Device MCA SRC INC EXC\n");
2685 "0x%08x %6lu %6lu\n",
2686 state
->dev
->ifindex
, state
->dev
->name
,
2687 ntohl(state
->im
->multiaddr
),
2688 ntohl(psf
->sf_inaddr
),
2689 psf
->sf_count
[MCAST_INCLUDE
],
2690 psf
->sf_count
[MCAST_EXCLUDE
]);
2695 static const struct seq_operations igmp_mcf_seq_ops
= {
2696 .start
= igmp_mcf_seq_start
,
2697 .next
= igmp_mcf_seq_next
,
2698 .stop
= igmp_mcf_seq_stop
,
2699 .show
= igmp_mcf_seq_show
,
2702 static int igmp_mcf_seq_open(struct inode
*inode
, struct file
*file
)
2704 return seq_open_net(inode
, file
, &igmp_mcf_seq_ops
,
2705 sizeof(struct igmp_mcf_iter_state
));
2708 static const struct file_operations igmp_mcf_seq_fops
= {
2709 .owner
= THIS_MODULE
,
2710 .open
= igmp_mcf_seq_open
,
2712 .llseek
= seq_lseek
,
2713 .release
= seq_release_net
,
2716 static int __net_init
igmp_net_init(struct net
*net
)
2718 struct proc_dir_entry
*pde
;
2721 pde
= proc_create("igmp", S_IRUGO
, net
->proc_net
, &igmp_mc_seq_fops
);
2724 pde
= proc_create("mcfilter", S_IRUGO
, net
->proc_net
,
2725 &igmp_mcf_seq_fops
);
2728 err
= inet_ctl_sock_create(&net
->ipv4
.mc_autojoin_sk
, AF_INET
,
2729 SOCK_DGRAM
, 0, net
);
2731 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
2739 remove_proc_entry("mcfilter", net
->proc_net
);
2741 remove_proc_entry("igmp", net
->proc_net
);
2746 static void __net_exit
igmp_net_exit(struct net
*net
)
2748 remove_proc_entry("mcfilter", net
->proc_net
);
2749 remove_proc_entry("igmp", net
->proc_net
);
2750 inet_ctl_sock_destroy(net
->ipv4
.mc_autojoin_sk
);
2753 static struct pernet_operations igmp_net_ops
= {
2754 .init
= igmp_net_init
,
2755 .exit
= igmp_net_exit
,
2759 static int igmp_netdev_event(struct notifier_block
*this,
2760 unsigned long event
, void *ptr
)
2762 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
2763 struct in_device
*in_dev
;
2766 case NETDEV_RESEND_IGMP
:
2767 in_dev
= __in_dev_get_rtnl(dev
);
2769 ip_mc_rejoin_groups(in_dev
);
2777 static struct notifier_block igmp_notifier
= {
2778 .notifier_call
= igmp_netdev_event
,
2781 int __init
igmp_mc_init(void)
2783 #if defined(CONFIG_PROC_FS)
2786 err
= register_pernet_subsys(&igmp_net_ops
);
2789 err
= register_netdevice_notifier(&igmp_notifier
);
2791 goto reg_notif_fail
;
2795 unregister_pernet_subsys(&igmp_net_ops
);
2798 return register_netdevice_notifier(&igmp_notifier
);