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_reserve(skb
, hlen
);
357 skb_tailroom_reserve(skb
, mtu
, tlen
);
359 skb_reset_network_header(skb
);
361 skb_put(skb
, sizeof(struct iphdr
) + 4);
364 pip
->ihl
= (sizeof(struct iphdr
)+4)>>2;
366 pip
->frag_off
= htons(IP_DF
);
368 pip
->daddr
= fl4
.daddr
;
369 pip
->saddr
= fl4
.saddr
;
370 pip
->protocol
= IPPROTO_IGMP
;
371 pip
->tot_len
= 0; /* filled in later */
372 ip_select_ident(net
, skb
, NULL
);
373 ((u8
*)&pip
[1])[0] = IPOPT_RA
;
374 ((u8
*)&pip
[1])[1] = 4;
375 ((u8
*)&pip
[1])[2] = 0;
376 ((u8
*)&pip
[1])[3] = 0;
378 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
) + 4;
379 skb_put(skb
, sizeof(*pig
));
380 pig
= igmpv3_report_hdr(skb
);
381 pig
->type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
389 static int igmpv3_sendpack(struct sk_buff
*skb
)
391 struct igmphdr
*pig
= igmp_hdr(skb
);
392 const int igmplen
= skb_tail_pointer(skb
) - skb_transport_header(skb
);
394 pig
->csum
= ip_compute_csum(igmp_hdr(skb
), igmplen
);
396 return ip_local_out(skb
);
399 static int grec_size(struct ip_mc_list
*pmc
, int type
, int gdel
, int sdel
)
401 return sizeof(struct igmpv3_grec
) + 4*igmp_scount(pmc
, type
, gdel
, sdel
);
404 static struct sk_buff
*add_grhead(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
405 int type
, struct igmpv3_grec
**ppgr
)
407 struct net_device
*dev
= pmc
->interface
->dev
;
408 struct igmpv3_report
*pih
;
409 struct igmpv3_grec
*pgr
;
412 skb
= igmpv3_newpack(dev
, dev
->mtu
);
415 pgr
= (struct igmpv3_grec
*)skb_put(skb
, sizeof(struct igmpv3_grec
));
416 pgr
->grec_type
= type
;
417 pgr
->grec_auxwords
= 0;
419 pgr
->grec_mca
= pmc
->multiaddr
;
420 pih
= igmpv3_report_hdr(skb
);
421 pih
->ngrec
= htons(ntohs(pih
->ngrec
)+1);
426 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
428 static struct sk_buff
*add_grec(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
429 int type
, int gdeleted
, int sdeleted
)
431 struct net_device
*dev
= pmc
->interface
->dev
;
432 struct igmpv3_report
*pih
;
433 struct igmpv3_grec
*pgr
= NULL
;
434 struct ip_sf_list
*psf
, *psf_next
, *psf_prev
, **psf_list
;
435 int scount
, stotal
, first
, isquery
, truncate
;
437 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
440 isquery
= type
== IGMPV3_MODE_IS_INCLUDE
||
441 type
== IGMPV3_MODE_IS_EXCLUDE
;
442 truncate
= type
== IGMPV3_MODE_IS_EXCLUDE
||
443 type
== IGMPV3_CHANGE_TO_EXCLUDE
;
447 psf_list
= sdeleted
? &pmc
->tomb
: &pmc
->sources
;
452 pih
= skb
? igmpv3_report_hdr(skb
) : NULL
;
454 /* EX and TO_EX get a fresh packet, if needed */
456 if (pih
&& pih
->ngrec
&&
457 AVAILABLE(skb
) < grec_size(pmc
, type
, gdeleted
, sdeleted
)) {
459 igmpv3_sendpack(skb
);
460 skb
= igmpv3_newpack(dev
, dev
->mtu
);
465 for (psf
= *psf_list
; psf
; psf
= psf_next
) {
468 psf_next
= psf
->sf_next
;
470 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
)) {
475 /* clear marks on query responses */
479 if (AVAILABLE(skb
) < sizeof(__be32
) +
480 first
*sizeof(struct igmpv3_grec
)) {
481 if (truncate
&& !first
)
482 break; /* truncate these */
484 pgr
->grec_nsrcs
= htons(scount
);
486 igmpv3_sendpack(skb
);
487 skb
= igmpv3_newpack(dev
, dev
->mtu
);
492 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
497 psrc
= (__be32
*)skb_put(skb
, sizeof(__be32
));
498 *psrc
= psf
->sf_inaddr
;
500 if ((type
== IGMPV3_ALLOW_NEW_SOURCES
||
501 type
== IGMPV3_BLOCK_OLD_SOURCES
) && psf
->sf_crcount
) {
503 if ((sdeleted
|| gdeleted
) && psf
->sf_crcount
== 0) {
505 psf_prev
->sf_next
= psf
->sf_next
;
507 *psf_list
= psf
->sf_next
;
517 if (type
== IGMPV3_ALLOW_NEW_SOURCES
||
518 type
== IGMPV3_BLOCK_OLD_SOURCES
)
520 if (pmc
->crcount
|| isquery
) {
521 /* make sure we have room for group header */
522 if (skb
&& AVAILABLE(skb
) < sizeof(struct igmpv3_grec
)) {
523 igmpv3_sendpack(skb
);
524 skb
= NULL
; /* add_grhead will get a new one */
526 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
530 pgr
->grec_nsrcs
= htons(scount
);
533 pmc
->gsquery
= 0; /* clear query state on report */
537 static int igmpv3_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
)
539 struct sk_buff
*skb
= NULL
;
544 for_each_pmc_rcu(in_dev
, pmc
) {
545 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
547 spin_lock_bh(&pmc
->lock
);
548 if (pmc
->sfcount
[MCAST_EXCLUDE
])
549 type
= IGMPV3_MODE_IS_EXCLUDE
;
551 type
= IGMPV3_MODE_IS_INCLUDE
;
552 skb
= add_grec(skb
, pmc
, type
, 0, 0);
553 spin_unlock_bh(&pmc
->lock
);
557 spin_lock_bh(&pmc
->lock
);
558 if (pmc
->sfcount
[MCAST_EXCLUDE
])
559 type
= IGMPV3_MODE_IS_EXCLUDE
;
561 type
= IGMPV3_MODE_IS_INCLUDE
;
562 skb
= add_grec(skb
, pmc
, type
, 0, 0);
563 spin_unlock_bh(&pmc
->lock
);
567 return igmpv3_sendpack(skb
);
571 * remove zero-count source records from a source filter list
573 static void igmpv3_clear_zeros(struct ip_sf_list
**ppsf
)
575 struct ip_sf_list
*psf_prev
, *psf_next
, *psf
;
578 for (psf
= *ppsf
; psf
; psf
= psf_next
) {
579 psf_next
= psf
->sf_next
;
580 if (psf
->sf_crcount
== 0) {
582 psf_prev
->sf_next
= psf
->sf_next
;
584 *ppsf
= psf
->sf_next
;
591 static void igmpv3_send_cr(struct in_device
*in_dev
)
593 struct ip_mc_list
*pmc
, *pmc_prev
, *pmc_next
;
594 struct sk_buff
*skb
= NULL
;
598 spin_lock_bh(&in_dev
->mc_tomb_lock
);
602 for (pmc
= in_dev
->mc_tomb
; pmc
; pmc
= pmc_next
) {
603 pmc_next
= pmc
->next
;
604 if (pmc
->sfmode
== MCAST_INCLUDE
) {
605 type
= IGMPV3_BLOCK_OLD_SOURCES
;
606 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
607 skb
= add_grec(skb
, pmc
, type
, 1, 0);
608 skb
= add_grec(skb
, pmc
, dtype
, 1, 1);
611 if (pmc
->sfmode
== MCAST_EXCLUDE
) {
612 type
= IGMPV3_CHANGE_TO_INCLUDE
;
613 skb
= add_grec(skb
, pmc
, type
, 1, 0);
616 if (pmc
->crcount
== 0) {
617 igmpv3_clear_zeros(&pmc
->tomb
);
618 igmpv3_clear_zeros(&pmc
->sources
);
621 if (pmc
->crcount
== 0 && !pmc
->tomb
&& !pmc
->sources
) {
623 pmc_prev
->next
= pmc_next
;
625 in_dev
->mc_tomb
= pmc_next
;
626 in_dev_put(pmc
->interface
);
631 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
634 for_each_pmc_rcu(in_dev
, pmc
) {
635 spin_lock_bh(&pmc
->lock
);
636 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
637 type
= IGMPV3_BLOCK_OLD_SOURCES
;
638 dtype
= IGMPV3_ALLOW_NEW_SOURCES
;
640 type
= IGMPV3_ALLOW_NEW_SOURCES
;
641 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
643 skb
= add_grec(skb
, pmc
, type
, 0, 0);
644 skb
= add_grec(skb
, pmc
, dtype
, 0, 1); /* deleted sources */
646 /* filter mode changes */
648 if (pmc
->sfmode
== MCAST_EXCLUDE
)
649 type
= IGMPV3_CHANGE_TO_EXCLUDE
;
651 type
= IGMPV3_CHANGE_TO_INCLUDE
;
652 skb
= add_grec(skb
, pmc
, type
, 0, 0);
655 spin_unlock_bh(&pmc
->lock
);
661 (void) igmpv3_sendpack(skb
);
664 static int igmp_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
,
671 struct net_device
*dev
= in_dev
->dev
;
672 struct net
*net
= dev_net(dev
);
673 __be32 group
= pmc
? pmc
->multiaddr
: 0;
678 if (type
== IGMPV3_HOST_MEMBERSHIP_REPORT
)
679 return igmpv3_send_report(in_dev
, pmc
);
680 else if (type
== IGMP_HOST_LEAVE_MESSAGE
)
681 dst
= IGMP_ALL_ROUTER
;
685 rt
= ip_route_output_ports(net
, &fl4
, NULL
, dst
, 0,
687 IPPROTO_IGMP
, 0, dev
->ifindex
);
691 hlen
= LL_RESERVED_SPACE(dev
);
692 tlen
= dev
->needed_tailroom
;
693 skb
= alloc_skb(IGMP_SIZE
+ hlen
+ tlen
, GFP_ATOMIC
);
698 skb
->priority
= TC_PRIO_CONTROL
;
700 skb_dst_set(skb
, &rt
->dst
);
702 skb_reserve(skb
, hlen
);
704 skb_reset_network_header(skb
);
706 skb_put(skb
, sizeof(struct iphdr
) + 4);
709 iph
->ihl
= (sizeof(struct iphdr
)+4)>>2;
711 iph
->frag_off
= htons(IP_DF
);
714 iph
->saddr
= fl4
.saddr
;
715 iph
->protocol
= IPPROTO_IGMP
;
716 ip_select_ident(net
, skb
, NULL
);
717 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
718 ((u8
*)&iph
[1])[1] = 4;
719 ((u8
*)&iph
[1])[2] = 0;
720 ((u8
*)&iph
[1])[3] = 0;
722 ih
= (struct igmphdr
*)skb_put(skb
, sizeof(struct igmphdr
));
727 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
729 return ip_local_out(skb
);
732 static void igmp_gq_timer_expire(unsigned long data
)
734 struct in_device
*in_dev
= (struct in_device
*)data
;
736 in_dev
->mr_gq_running
= 0;
737 igmpv3_send_report(in_dev
, NULL
);
741 static void igmp_ifc_timer_expire(unsigned long data
)
743 struct in_device
*in_dev
= (struct in_device
*)data
;
745 igmpv3_send_cr(in_dev
);
746 if (in_dev
->mr_ifc_count
) {
747 in_dev
->mr_ifc_count
--;
748 igmp_ifc_start_timer(in_dev
,
749 unsolicited_report_interval(in_dev
));
754 static void igmp_ifc_event(struct in_device
*in_dev
)
756 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
758 in_dev
->mr_ifc_count
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
759 igmp_ifc_start_timer(in_dev
, 1);
763 static void igmp_timer_expire(unsigned long data
)
765 struct ip_mc_list
*im
= (struct ip_mc_list
*)data
;
766 struct in_device
*in_dev
= im
->interface
;
768 spin_lock(&im
->lock
);
771 if (im
->unsolicit_count
) {
772 im
->unsolicit_count
--;
773 igmp_start_timer(im
, unsolicited_report_interval(in_dev
));
776 spin_unlock(&im
->lock
);
778 if (IGMP_V1_SEEN(in_dev
))
779 igmp_send_report(in_dev
, im
, IGMP_HOST_MEMBERSHIP_REPORT
);
780 else if (IGMP_V2_SEEN(in_dev
))
781 igmp_send_report(in_dev
, im
, IGMPV2_HOST_MEMBERSHIP_REPORT
);
783 igmp_send_report(in_dev
, im
, IGMPV3_HOST_MEMBERSHIP_REPORT
);
788 /* mark EXCLUDE-mode sources */
789 static int igmp_xmarksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
791 struct ip_sf_list
*psf
;
795 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
798 for (i
= 0; i
< nsrcs
; i
++) {
799 /* skip inactive filters */
800 if (psf
->sf_count
[MCAST_INCLUDE
] ||
801 pmc
->sfcount
[MCAST_EXCLUDE
] !=
802 psf
->sf_count
[MCAST_EXCLUDE
])
804 if (srcs
[i
] == psf
->sf_inaddr
) {
811 if (scount
== nsrcs
) /* all sources excluded */
816 static int igmp_marksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
818 struct ip_sf_list
*psf
;
821 if (pmc
->sfmode
== MCAST_EXCLUDE
)
822 return igmp_xmarksources(pmc
, nsrcs
, srcs
);
824 /* mark INCLUDE-mode sources */
826 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
829 for (i
= 0; i
< nsrcs
; i
++)
830 if (srcs
[i
] == psf
->sf_inaddr
) {
844 /* return true if packet was dropped */
845 static bool igmp_heard_report(struct in_device
*in_dev
, __be32 group
)
847 struct ip_mc_list
*im
;
849 /* Timers are only set for non-local groups */
851 if (group
== IGMP_ALL_HOSTS
)
855 for_each_pmc_rcu(in_dev
, im
) {
856 if (im
->multiaddr
== group
) {
865 /* return true if packet was dropped */
866 static bool igmp_heard_query(struct in_device
*in_dev
, struct sk_buff
*skb
,
869 struct igmphdr
*ih
= igmp_hdr(skb
);
870 struct igmpv3_query
*ih3
= igmpv3_query_hdr(skb
);
871 struct ip_mc_list
*im
;
872 __be32 group
= ih
->group
;
879 /* Alas, old v1 router presents here. */
881 max_delay
= IGMP_QUERY_RESPONSE_INTERVAL
;
882 in_dev
->mr_v1_seen
= jiffies
+
883 IGMP_V1_ROUTER_PRESENT_TIMEOUT
;
886 /* v2 router present */
887 max_delay
= ih
->code
*(HZ
/IGMP_TIMER_SCALE
);
888 in_dev
->mr_v2_seen
= jiffies
+
889 IGMP_V2_ROUTER_PRESENT_TIMEOUT
;
891 /* cancel the interface change timer */
892 in_dev
->mr_ifc_count
= 0;
893 if (del_timer(&in_dev
->mr_ifc_timer
))
894 __in_dev_put(in_dev
);
895 /* clear deleted report items */
896 igmpv3_clear_delrec(in_dev
);
897 } else if (len
< 12) {
898 return true; /* ignore bogus packet; freed by caller */
899 } else if (IGMP_V1_SEEN(in_dev
)) {
900 /* This is a v3 query with v1 queriers present */
901 max_delay
= IGMP_QUERY_RESPONSE_INTERVAL
;
903 } else if (IGMP_V2_SEEN(in_dev
)) {
904 /* this is a v3 query with v2 queriers present;
905 * Interpretation of the max_delay code is problematic here.
906 * A real v2 host would use ih_code directly, while v3 has a
907 * different encoding. We use the v3 encoding as more likely
908 * to be intended in a v3 query.
910 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
912 max_delay
= 1; /* can't mod w/ 0 */
914 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)))
917 ih3
= igmpv3_query_hdr(skb
);
919 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)
920 + ntohs(ih3
->nsrcs
)*sizeof(__be32
)))
922 ih3
= igmpv3_query_hdr(skb
);
925 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
927 max_delay
= 1; /* can't mod w/ 0 */
928 in_dev
->mr_maxdelay
= max_delay
;
930 in_dev
->mr_qrv
= ih3
->qrv
;
931 if (!group
) { /* general query */
933 return true; /* no sources allowed */
934 igmp_gq_start_timer(in_dev
);
937 /* mark sources to include, if group & source-specific */
938 mark
= ih3
->nsrcs
!= 0;
942 * - Start the timers in all of our membership records
943 * that the query applies to for the interface on
944 * which the query arrived excl. those that belong
945 * to a "local" group (224.0.0.X)
946 * - For timers already running check if they need to
948 * - Use the igmp->igmp_code field as the maximum
952 for_each_pmc_rcu(in_dev
, im
) {
955 if (group
&& group
!= im
->multiaddr
)
957 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
959 spin_lock_bh(&im
->lock
);
961 im
->gsquery
= im
->gsquery
&& mark
;
964 changed
= !im
->gsquery
||
965 igmp_marksources(im
, ntohs(ih3
->nsrcs
), ih3
->srcs
);
966 spin_unlock_bh(&im
->lock
);
968 igmp_mod_timer(im
, max_delay
);
974 /* called in rcu_read_lock() section */
975 int igmp_rcv(struct sk_buff
*skb
)
977 /* This basically follows the spec line by line -- see RFC1112 */
979 struct in_device
*in_dev
= __in_dev_get_rcu(skb
->dev
);
986 if (!pskb_may_pull(skb
, sizeof(struct igmphdr
)))
989 if (skb_checksum_simple_validate(skb
))
994 case IGMP_HOST_MEMBERSHIP_QUERY
:
995 dropped
= igmp_heard_query(in_dev
, skb
, len
);
997 case IGMP_HOST_MEMBERSHIP_REPORT
:
998 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
999 /* Is it our report looped back? */
1000 if (rt_is_output_route(skb_rtable(skb
)))
1002 /* don't rely on MC router hearing unicast reports */
1003 if (skb
->pkt_type
== PACKET_MULTICAST
||
1004 skb
->pkt_type
== PACKET_BROADCAST
)
1005 dropped
= igmp_heard_report(in_dev
, ih
->group
);
1008 #ifdef CONFIG_IP_PIMSM_V1
1009 return pim_rcv_v1(skb
);
1011 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1014 case IGMP_HOST_LEAVE_MESSAGE
:
1016 case IGMP_MTRACE_RESP
:
1034 * Add a filter to a device
1037 static void ip_mc_filter_add(struct in_device
*in_dev
, __be32 addr
)
1039 char buf
[MAX_ADDR_LEN
];
1040 struct net_device
*dev
= in_dev
->dev
;
1042 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1043 We will get multicast token leakage, when IFF_MULTICAST
1044 is changed. This check should be done in ndo_set_rx_mode
1045 routine. Something sort of:
1046 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1049 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1050 dev_mc_add(dev
, buf
);
1054 * Remove a filter from a device
1057 static void ip_mc_filter_del(struct in_device
*in_dev
, __be32 addr
)
1059 char buf
[MAX_ADDR_LEN
];
1060 struct net_device
*dev
= in_dev
->dev
;
1062 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1063 dev_mc_del(dev
, buf
);
1066 #ifdef CONFIG_IP_MULTICAST
1068 * deleted ip_mc_list manipulation
1070 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
)
1072 struct ip_mc_list
*pmc
;
1074 /* this is an "ip_mc_list" for convenience; only the fields below
1075 * are actually used. In particular, the refcnt and users are not
1076 * used for management of the delete list. Using the same structure
1077 * for deleted items allows change reports to use common code with
1078 * non-deleted or query-response MCA's.
1080 pmc
= kzalloc(sizeof(*pmc
), GFP_KERNEL
);
1083 spin_lock_bh(&im
->lock
);
1084 pmc
->interface
= im
->interface
;
1085 in_dev_hold(in_dev
);
1086 pmc
->multiaddr
= im
->multiaddr
;
1087 pmc
->crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
1088 pmc
->sfmode
= im
->sfmode
;
1089 if (pmc
->sfmode
== MCAST_INCLUDE
) {
1090 struct ip_sf_list
*psf
;
1092 pmc
->tomb
= im
->tomb
;
1093 pmc
->sources
= im
->sources
;
1094 im
->tomb
= im
->sources
= NULL
;
1095 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1096 psf
->sf_crcount
= pmc
->crcount
;
1098 spin_unlock_bh(&im
->lock
);
1100 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1101 pmc
->next
= in_dev
->mc_tomb
;
1102 in_dev
->mc_tomb
= pmc
;
1103 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1106 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
)
1108 struct ip_mc_list
*pmc
, *pmc_prev
;
1109 struct ip_sf_list
*psf
, *psf_next
;
1111 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1113 for (pmc
= in_dev
->mc_tomb
; pmc
; pmc
= pmc
->next
) {
1114 if (pmc
->multiaddr
== multiaddr
)
1120 pmc_prev
->next
= pmc
->next
;
1122 in_dev
->mc_tomb
= pmc
->next
;
1124 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1126 for (psf
= pmc
->tomb
; psf
; psf
= psf_next
) {
1127 psf_next
= psf
->sf_next
;
1130 in_dev_put(pmc
->interface
);
1135 static void igmpv3_clear_delrec(struct in_device
*in_dev
)
1137 struct ip_mc_list
*pmc
, *nextpmc
;
1139 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1140 pmc
= in_dev
->mc_tomb
;
1141 in_dev
->mc_tomb
= NULL
;
1142 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1144 for (; pmc
; pmc
= nextpmc
) {
1145 nextpmc
= pmc
->next
;
1146 ip_mc_clear_src(pmc
);
1147 in_dev_put(pmc
->interface
);
1150 /* clear dead sources, too */
1152 for_each_pmc_rcu(in_dev
, pmc
) {
1153 struct ip_sf_list
*psf
, *psf_next
;
1155 spin_lock_bh(&pmc
->lock
);
1158 spin_unlock_bh(&pmc
->lock
);
1159 for (; psf
; psf
= psf_next
) {
1160 psf_next
= psf
->sf_next
;
1168 static void igmp_group_dropped(struct ip_mc_list
*im
)
1170 struct in_device
*in_dev
= im
->interface
;
1171 #ifdef CONFIG_IP_MULTICAST
1177 ip_mc_filter_del(in_dev
, im
->multiaddr
);
1180 #ifdef CONFIG_IP_MULTICAST
1181 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1184 reporter
= im
->reporter
;
1185 igmp_stop_timer(im
);
1187 if (!in_dev
->dead
) {
1188 if (IGMP_V1_SEEN(in_dev
))
1190 if (IGMP_V2_SEEN(in_dev
)) {
1192 igmp_send_report(in_dev
, im
, IGMP_HOST_LEAVE_MESSAGE
);
1196 igmpv3_add_delrec(in_dev
, im
);
1198 igmp_ifc_event(in_dev
);
1203 static void igmp_group_added(struct ip_mc_list
*im
)
1205 struct in_device
*in_dev
= im
->interface
;
1207 if (im
->loaded
== 0) {
1209 ip_mc_filter_add(in_dev
, im
->multiaddr
);
1212 #ifdef CONFIG_IP_MULTICAST
1213 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1218 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
)) {
1219 spin_lock_bh(&im
->lock
);
1220 igmp_start_timer(im
, IGMP_INITIAL_REPORT_DELAY
);
1221 spin_unlock_bh(&im
->lock
);
1226 im
->crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
1227 igmp_ifc_event(in_dev
);
1233 * Multicast list managers
1236 static u32
ip_mc_hash(const struct ip_mc_list
*im
)
1238 return hash_32((__force u32
)im
->multiaddr
, MC_HASH_SZ_LOG
);
1241 static void ip_mc_hash_add(struct in_device
*in_dev
,
1242 struct ip_mc_list
*im
)
1244 struct ip_mc_list __rcu
**mc_hash
;
1247 mc_hash
= rtnl_dereference(in_dev
->mc_hash
);
1249 hash
= ip_mc_hash(im
);
1250 im
->next_hash
= mc_hash
[hash
];
1251 rcu_assign_pointer(mc_hash
[hash
], im
);
1255 /* do not use a hash table for small number of items */
1256 if (in_dev
->mc_count
< 4)
1259 mc_hash
= kzalloc(sizeof(struct ip_mc_list
*) << MC_HASH_SZ_LOG
,
1264 for_each_pmc_rtnl(in_dev
, im
) {
1265 hash
= ip_mc_hash(im
);
1266 im
->next_hash
= mc_hash
[hash
];
1267 RCU_INIT_POINTER(mc_hash
[hash
], im
);
1270 rcu_assign_pointer(in_dev
->mc_hash
, mc_hash
);
1273 static void ip_mc_hash_remove(struct in_device
*in_dev
,
1274 struct ip_mc_list
*im
)
1276 struct ip_mc_list __rcu
**mc_hash
= rtnl_dereference(in_dev
->mc_hash
);
1277 struct ip_mc_list
*aux
;
1281 mc_hash
+= ip_mc_hash(im
);
1282 while ((aux
= rtnl_dereference(*mc_hash
)) != im
)
1283 mc_hash
= &aux
->next_hash
;
1284 *mc_hash
= im
->next_hash
;
1289 * A socket has joined a multicast group on device dev.
1292 void ip_mc_inc_group(struct in_device
*in_dev
, __be32 addr
)
1294 struct ip_mc_list
*im
;
1298 for_each_pmc_rtnl(in_dev
, im
) {
1299 if (im
->multiaddr
== addr
) {
1301 ip_mc_add_src(in_dev
, &addr
, MCAST_EXCLUDE
, 0, NULL
, 0);
1306 im
= kzalloc(sizeof(*im
), GFP_KERNEL
);
1311 im
->interface
= in_dev
;
1312 in_dev_hold(in_dev
);
1313 im
->multiaddr
= addr
;
1314 /* initial mode is (EX, empty) */
1315 im
->sfmode
= MCAST_EXCLUDE
;
1316 im
->sfcount
[MCAST_EXCLUDE
] = 1;
1317 atomic_set(&im
->refcnt
, 1);
1318 spin_lock_init(&im
->lock
);
1319 #ifdef CONFIG_IP_MULTICAST
1320 setup_timer(&im
->timer
, igmp_timer_expire
, (unsigned long)im
);
1321 im
->unsolicit_count
= sysctl_igmp_qrv
;
1324 im
->next_rcu
= in_dev
->mc_list
;
1326 rcu_assign_pointer(in_dev
->mc_list
, im
);
1328 ip_mc_hash_add(in_dev
, im
);
1330 #ifdef CONFIG_IP_MULTICAST
1331 igmpv3_del_delrec(in_dev
, im
->multiaddr
);
1333 igmp_group_added(im
);
1335 ip_rt_multicast_event(in_dev
);
1339 EXPORT_SYMBOL(ip_mc_inc_group
);
1342 * Resend IGMP JOIN report; used by netdev notifier.
1344 static void ip_mc_rejoin_groups(struct in_device
*in_dev
)
1346 #ifdef CONFIG_IP_MULTICAST
1347 struct ip_mc_list
*im
;
1352 for_each_pmc_rtnl(in_dev
, im
) {
1353 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1356 /* a failover is happening and switches
1357 * must be notified immediately
1359 if (IGMP_V1_SEEN(in_dev
))
1360 type
= IGMP_HOST_MEMBERSHIP_REPORT
;
1361 else if (IGMP_V2_SEEN(in_dev
))
1362 type
= IGMPV2_HOST_MEMBERSHIP_REPORT
;
1364 type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
1365 igmp_send_report(in_dev
, im
, type
);
1371 * A socket has left a multicast group on device dev
1374 void ip_mc_dec_group(struct in_device
*in_dev
, __be32 addr
)
1376 struct ip_mc_list
*i
;
1377 struct ip_mc_list __rcu
**ip
;
1381 for (ip
= &in_dev
->mc_list
;
1382 (i
= rtnl_dereference(*ip
)) != NULL
;
1383 ip
= &i
->next_rcu
) {
1384 if (i
->multiaddr
== addr
) {
1385 if (--i
->users
== 0) {
1386 ip_mc_hash_remove(in_dev
, i
);
1389 igmp_group_dropped(i
);
1393 ip_rt_multicast_event(in_dev
);
1402 EXPORT_SYMBOL(ip_mc_dec_group
);
1404 /* Device changing type */
1406 void ip_mc_unmap(struct in_device
*in_dev
)
1408 struct ip_mc_list
*pmc
;
1412 for_each_pmc_rtnl(in_dev
, pmc
)
1413 igmp_group_dropped(pmc
);
1416 void ip_mc_remap(struct in_device
*in_dev
)
1418 struct ip_mc_list
*pmc
;
1422 for_each_pmc_rtnl(in_dev
, pmc
)
1423 igmp_group_added(pmc
);
1426 /* Device going down */
1428 void ip_mc_down(struct in_device
*in_dev
)
1430 struct ip_mc_list
*pmc
;
1434 for_each_pmc_rtnl(in_dev
, pmc
)
1435 igmp_group_dropped(pmc
);
1437 #ifdef CONFIG_IP_MULTICAST
1438 in_dev
->mr_ifc_count
= 0;
1439 if (del_timer(&in_dev
->mr_ifc_timer
))
1440 __in_dev_put(in_dev
);
1441 in_dev
->mr_gq_running
= 0;
1442 if (del_timer(&in_dev
->mr_gq_timer
))
1443 __in_dev_put(in_dev
);
1444 igmpv3_clear_delrec(in_dev
);
1447 ip_mc_dec_group(in_dev
, IGMP_ALL_HOSTS
);
1450 void ip_mc_init_dev(struct in_device
*in_dev
)
1454 #ifdef CONFIG_IP_MULTICAST
1455 setup_timer(&in_dev
->mr_gq_timer
, igmp_gq_timer_expire
,
1456 (unsigned long)in_dev
);
1457 setup_timer(&in_dev
->mr_ifc_timer
, igmp_ifc_timer_expire
,
1458 (unsigned long)in_dev
);
1459 in_dev
->mr_qrv
= sysctl_igmp_qrv
;
1462 spin_lock_init(&in_dev
->mc_tomb_lock
);
1465 /* Device going up */
1467 void ip_mc_up(struct in_device
*in_dev
)
1469 struct ip_mc_list
*pmc
;
1473 #ifdef CONFIG_IP_MULTICAST
1474 in_dev
->mr_qrv
= sysctl_igmp_qrv
;
1476 ip_mc_inc_group(in_dev
, IGMP_ALL_HOSTS
);
1478 for_each_pmc_rtnl(in_dev
, pmc
)
1479 igmp_group_added(pmc
);
1483 * Device is about to be destroyed: clean up.
1486 void ip_mc_destroy_dev(struct in_device
*in_dev
)
1488 struct ip_mc_list
*i
;
1492 /* Deactivate timers */
1495 while ((i
= rtnl_dereference(in_dev
->mc_list
)) != NULL
) {
1496 in_dev
->mc_list
= i
->next_rcu
;
1499 /* We've dropped the groups in ip_mc_down already */
1505 /* RTNL is locked */
1506 static struct in_device
*ip_mc_find_dev(struct net
*net
, struct ip_mreqn
*imr
)
1508 struct net_device
*dev
= NULL
;
1509 struct in_device
*idev
= NULL
;
1511 if (imr
->imr_ifindex
) {
1512 idev
= inetdev_by_index(net
, imr
->imr_ifindex
);
1515 if (imr
->imr_address
.s_addr
) {
1516 dev
= __ip_dev_find(net
, imr
->imr_address
.s_addr
, false);
1522 struct rtable
*rt
= ip_route_output(net
,
1523 imr
->imr_multiaddr
.s_addr
,
1531 imr
->imr_ifindex
= dev
->ifindex
;
1532 idev
= __in_dev_get_rtnl(dev
);
1538 * Join a socket to a group
1540 int sysctl_igmp_max_memberships __read_mostly
= IP_MAX_MEMBERSHIPS
;
1541 int sysctl_igmp_max_msf __read_mostly
= IP_MAX_MSF
;
1542 #ifdef CONFIG_IP_MULTICAST
1543 int sysctl_igmp_qrv __read_mostly
= IGMP_QUERY_ROBUSTNESS_VARIABLE
;
1546 static int ip_mc_del1_src(struct ip_mc_list
*pmc
, int sfmode
,
1549 struct ip_sf_list
*psf
, *psf_prev
;
1553 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1554 if (psf
->sf_inaddr
== *psfsrc
)
1558 if (!psf
|| psf
->sf_count
[sfmode
] == 0) {
1559 /* source filter not found, or count wrong => bug */
1562 psf
->sf_count
[sfmode
]--;
1563 if (psf
->sf_count
[sfmode
] == 0) {
1564 ip_rt_multicast_event(pmc
->interface
);
1566 if (!psf
->sf_count
[MCAST_INCLUDE
] && !psf
->sf_count
[MCAST_EXCLUDE
]) {
1567 #ifdef CONFIG_IP_MULTICAST
1568 struct in_device
*in_dev
= pmc
->interface
;
1571 /* no more filters for this source */
1573 psf_prev
->sf_next
= psf
->sf_next
;
1575 pmc
->sources
= psf
->sf_next
;
1576 #ifdef CONFIG_IP_MULTICAST
1577 if (psf
->sf_oldin
&&
1578 !IGMP_V1_SEEN(in_dev
) && !IGMP_V2_SEEN(in_dev
)) {
1579 psf
->sf_crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
1580 psf
->sf_next
= pmc
->tomb
;
1590 #ifndef CONFIG_IP_MULTICAST
1591 #define igmp_ifc_event(x) do { } while (0)
1594 static int ip_mc_del_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1595 int sfcount
, __be32
*psfsrc
, int delta
)
1597 struct ip_mc_list
*pmc
;
1604 for_each_pmc_rcu(in_dev
, pmc
) {
1605 if (*pmca
== pmc
->multiaddr
)
1609 /* MCA not found?? bug */
1613 spin_lock_bh(&pmc
->lock
);
1615 #ifdef CONFIG_IP_MULTICAST
1620 if (!pmc
->sfcount
[sfmode
])
1622 pmc
->sfcount
[sfmode
]--;
1625 for (i
= 0; i
< sfcount
; i
++) {
1626 int rv
= ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1628 changerec
|= rv
> 0;
1632 if (pmc
->sfmode
== MCAST_EXCLUDE
&&
1633 pmc
->sfcount
[MCAST_EXCLUDE
] == 0 &&
1634 pmc
->sfcount
[MCAST_INCLUDE
]) {
1635 #ifdef CONFIG_IP_MULTICAST
1636 struct ip_sf_list
*psf
;
1639 /* filter mode change */
1640 pmc
->sfmode
= MCAST_INCLUDE
;
1641 #ifdef CONFIG_IP_MULTICAST
1642 pmc
->crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
1643 in_dev
->mr_ifc_count
= pmc
->crcount
;
1644 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1645 psf
->sf_crcount
= 0;
1646 igmp_ifc_event(pmc
->interface
);
1647 } else if (sf_setstate(pmc
) || changerec
) {
1648 igmp_ifc_event(pmc
->interface
);
1652 spin_unlock_bh(&pmc
->lock
);
1657 * Add multicast single-source filter to the interface list
1659 static int ip_mc_add1_src(struct ip_mc_list
*pmc
, int sfmode
,
1662 struct ip_sf_list
*psf
, *psf_prev
;
1665 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1666 if (psf
->sf_inaddr
== *psfsrc
)
1671 psf
= kzalloc(sizeof(*psf
), GFP_ATOMIC
);
1674 psf
->sf_inaddr
= *psfsrc
;
1676 psf_prev
->sf_next
= psf
;
1680 psf
->sf_count
[sfmode
]++;
1681 if (psf
->sf_count
[sfmode
] == 1) {
1682 ip_rt_multicast_event(pmc
->interface
);
1687 #ifdef CONFIG_IP_MULTICAST
1688 static void sf_markstate(struct ip_mc_list
*pmc
)
1690 struct ip_sf_list
*psf
;
1691 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1693 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1694 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1695 psf
->sf_oldin
= mca_xcount
==
1696 psf
->sf_count
[MCAST_EXCLUDE
] &&
1697 !psf
->sf_count
[MCAST_INCLUDE
];
1699 psf
->sf_oldin
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1702 static int sf_setstate(struct ip_mc_list
*pmc
)
1704 struct ip_sf_list
*psf
, *dpsf
;
1705 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1706 int qrv
= pmc
->interface
->mr_qrv
;
1710 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1711 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1712 new_in
= mca_xcount
== psf
->sf_count
[MCAST_EXCLUDE
] &&
1713 !psf
->sf_count
[MCAST_INCLUDE
];
1715 new_in
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1717 if (!psf
->sf_oldin
) {
1718 struct ip_sf_list
*prev
= NULL
;
1720 for (dpsf
= pmc
->tomb
; dpsf
; dpsf
= dpsf
->sf_next
) {
1721 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1727 prev
->sf_next
= dpsf
->sf_next
;
1729 pmc
->tomb
= dpsf
->sf_next
;
1732 psf
->sf_crcount
= qrv
;
1735 } else if (psf
->sf_oldin
) {
1737 psf
->sf_crcount
= 0;
1739 * add or update "delete" records if an active filter
1742 for (dpsf
= pmc
->tomb
; dpsf
; dpsf
= dpsf
->sf_next
)
1743 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1746 dpsf
= kmalloc(sizeof(*dpsf
), GFP_ATOMIC
);
1750 /* pmc->lock held by callers */
1751 dpsf
->sf_next
= pmc
->tomb
;
1754 dpsf
->sf_crcount
= qrv
;
1763 * Add multicast source filter list to the interface list
1765 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1766 int sfcount
, __be32
*psfsrc
, int delta
)
1768 struct ip_mc_list
*pmc
;
1775 for_each_pmc_rcu(in_dev
, pmc
) {
1776 if (*pmca
== pmc
->multiaddr
)
1780 /* MCA not found?? bug */
1784 spin_lock_bh(&pmc
->lock
);
1787 #ifdef CONFIG_IP_MULTICAST
1790 isexclude
= pmc
->sfmode
== MCAST_EXCLUDE
;
1792 pmc
->sfcount
[sfmode
]++;
1794 for (i
= 0; i
< sfcount
; i
++) {
1795 err
= ip_mc_add1_src(pmc
, sfmode
, &psfsrc
[i
]);
1803 pmc
->sfcount
[sfmode
]--;
1804 for (j
= 0; j
< i
; j
++)
1805 (void) ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[j
]);
1806 } else if (isexclude
!= (pmc
->sfcount
[MCAST_EXCLUDE
] != 0)) {
1807 #ifdef CONFIG_IP_MULTICAST
1808 struct ip_sf_list
*psf
;
1809 in_dev
= pmc
->interface
;
1812 /* filter mode change */
1813 if (pmc
->sfcount
[MCAST_EXCLUDE
])
1814 pmc
->sfmode
= MCAST_EXCLUDE
;
1815 else if (pmc
->sfcount
[MCAST_INCLUDE
])
1816 pmc
->sfmode
= MCAST_INCLUDE
;
1817 #ifdef CONFIG_IP_MULTICAST
1818 /* else no filters; keep old mode for reports */
1820 pmc
->crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
1821 in_dev
->mr_ifc_count
= pmc
->crcount
;
1822 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1823 psf
->sf_crcount
= 0;
1824 igmp_ifc_event(in_dev
);
1825 } else if (sf_setstate(pmc
)) {
1826 igmp_ifc_event(in_dev
);
1829 spin_unlock_bh(&pmc
->lock
);
1833 static void ip_mc_clear_src(struct ip_mc_list
*pmc
)
1835 struct ip_sf_list
*psf
, *nextpsf
;
1837 for (psf
= pmc
->tomb
; psf
; psf
= nextpsf
) {
1838 nextpsf
= psf
->sf_next
;
1842 for (psf
= pmc
->sources
; psf
; psf
= nextpsf
) {
1843 nextpsf
= psf
->sf_next
;
1846 pmc
->sources
= NULL
;
1847 pmc
->sfmode
= MCAST_EXCLUDE
;
1848 pmc
->sfcount
[MCAST_INCLUDE
] = 0;
1849 pmc
->sfcount
[MCAST_EXCLUDE
] = 1;
1852 /* Join a multicast group
1855 int ip_mc_join_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1857 __be32 addr
= imr
->imr_multiaddr
.s_addr
;
1858 struct ip_mc_socklist
*iml
, *i
;
1859 struct in_device
*in_dev
;
1860 struct inet_sock
*inet
= inet_sk(sk
);
1861 struct net
*net
= sock_net(sk
);
1868 if (!ipv4_is_multicast(addr
))
1871 in_dev
= ip_mc_find_dev(net
, imr
);
1879 ifindex
= imr
->imr_ifindex
;
1880 for_each_pmc_rtnl(inet
, i
) {
1881 if (i
->multi
.imr_multiaddr
.s_addr
== addr
&&
1882 i
->multi
.imr_ifindex
== ifindex
)
1887 if (count
>= sysctl_igmp_max_memberships
)
1889 iml
= sock_kmalloc(sk
, sizeof(*iml
), GFP_KERNEL
);
1893 memcpy(&iml
->multi
, imr
, sizeof(*imr
));
1894 iml
->next_rcu
= inet
->mc_list
;
1896 iml
->sfmode
= MCAST_EXCLUDE
;
1897 rcu_assign_pointer(inet
->mc_list
, iml
);
1898 ip_mc_inc_group(in_dev
, addr
);
1903 EXPORT_SYMBOL(ip_mc_join_group
);
1905 static int ip_mc_leave_src(struct sock
*sk
, struct ip_mc_socklist
*iml
,
1906 struct in_device
*in_dev
)
1908 struct ip_sf_socklist
*psf
= rtnl_dereference(iml
->sflist
);
1912 /* any-source empty exclude case */
1913 return ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1914 iml
->sfmode
, 0, NULL
, 0);
1916 err
= ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1917 iml
->sfmode
, psf
->sl_count
, psf
->sl_addr
, 0);
1918 RCU_INIT_POINTER(iml
->sflist
, NULL
);
1919 /* decrease mem now to avoid the memleak warning */
1920 atomic_sub(IP_SFLSIZE(psf
->sl_max
), &sk
->sk_omem_alloc
);
1921 kfree_rcu(psf
, rcu
);
1925 int ip_mc_leave_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1927 struct inet_sock
*inet
= inet_sk(sk
);
1928 struct ip_mc_socklist
*iml
;
1929 struct ip_mc_socklist __rcu
**imlp
;
1930 struct in_device
*in_dev
;
1931 struct net
*net
= sock_net(sk
);
1932 __be32 group
= imr
->imr_multiaddr
.s_addr
;
1934 int ret
= -EADDRNOTAVAIL
;
1938 in_dev
= ip_mc_find_dev(net
, imr
);
1943 ifindex
= imr
->imr_ifindex
;
1944 for (imlp
= &inet
->mc_list
;
1945 (iml
= rtnl_dereference(*imlp
)) != NULL
;
1946 imlp
= &iml
->next_rcu
) {
1947 if (iml
->multi
.imr_multiaddr
.s_addr
!= group
)
1950 if (iml
->multi
.imr_ifindex
!= ifindex
)
1952 } else if (imr
->imr_address
.s_addr
&& imr
->imr_address
.s_addr
!=
1953 iml
->multi
.imr_address
.s_addr
)
1956 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
1958 *imlp
= iml
->next_rcu
;
1960 ip_mc_dec_group(in_dev
, group
);
1962 /* decrease mem now to avoid the memleak warning */
1963 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
1964 kfree_rcu(iml
, rcu
);
1970 EXPORT_SYMBOL(ip_mc_leave_group
);
1972 int ip_mc_source(int add
, int omode
, struct sock
*sk
, struct
1973 ip_mreq_source
*mreqs
, int ifindex
)
1976 struct ip_mreqn imr
;
1977 __be32 addr
= mreqs
->imr_multiaddr
;
1978 struct ip_mc_socklist
*pmc
;
1979 struct in_device
*in_dev
= NULL
;
1980 struct inet_sock
*inet
= inet_sk(sk
);
1981 struct ip_sf_socklist
*psl
;
1982 struct net
*net
= sock_net(sk
);
1986 if (!ipv4_is_multicast(addr
))
1991 imr
.imr_multiaddr
.s_addr
= mreqs
->imr_multiaddr
;
1992 imr
.imr_address
.s_addr
= mreqs
->imr_interface
;
1993 imr
.imr_ifindex
= ifindex
;
1994 in_dev
= ip_mc_find_dev(net
, &imr
);
2000 err
= -EADDRNOTAVAIL
;
2002 for_each_pmc_rtnl(inet
, pmc
) {
2003 if ((pmc
->multi
.imr_multiaddr
.s_addr
==
2004 imr
.imr_multiaddr
.s_addr
) &&
2005 (pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
))
2008 if (!pmc
) { /* must have a prior join */
2012 /* if a source filter was set, must be the same mode as before */
2014 if (pmc
->sfmode
!= omode
) {
2018 } else if (pmc
->sfmode
!= omode
) {
2019 /* allow mode switches for empty-set filters */
2020 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 0, NULL
, 0);
2021 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, pmc
->sfmode
, 0,
2023 pmc
->sfmode
= omode
;
2026 psl
= rtnl_dereference(pmc
->sflist
);
2029 goto done
; /* err = -EADDRNOTAVAIL */
2031 for (i
= 0; i
< psl
->sl_count
; i
++) {
2032 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2037 if (rv
) /* source not found */
2038 goto done
; /* err = -EADDRNOTAVAIL */
2040 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2041 if (psl
->sl_count
== 1 && omode
== MCAST_INCLUDE
) {
2046 /* update the interface filter */
2047 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2048 &mreqs
->imr_sourceaddr
, 1);
2050 for (j
= i
+1; j
< psl
->sl_count
; j
++)
2051 psl
->sl_addr
[j
-1] = psl
->sl_addr
[j
];
2056 /* else, add a new source to the filter */
2058 if (psl
&& psl
->sl_count
>= sysctl_igmp_max_msf
) {
2062 if (!psl
|| psl
->sl_count
== psl
->sl_max
) {
2063 struct ip_sf_socklist
*newpsl
;
2064 int count
= IP_SFBLOCK
;
2067 count
+= psl
->sl_max
;
2068 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(count
), GFP_KERNEL
);
2073 newpsl
->sl_max
= count
;
2074 newpsl
->sl_count
= count
- IP_SFBLOCK
;
2076 for (i
= 0; i
< psl
->sl_count
; i
++)
2077 newpsl
->sl_addr
[i
] = psl
->sl_addr
[i
];
2078 /* decrease mem now to avoid the memleak warning */
2079 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2080 kfree_rcu(psl
, rcu
);
2082 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2085 rv
= 1; /* > 0 for insert logic below if sl_count is 0 */
2086 for (i
= 0; i
< psl
->sl_count
; i
++) {
2087 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2092 if (rv
== 0) /* address already there is an error */
2094 for (j
= psl
->sl_count
-1; j
>= i
; j
--)
2095 psl
->sl_addr
[j
+1] = psl
->sl_addr
[j
];
2096 psl
->sl_addr
[i
] = mreqs
->imr_sourceaddr
;
2099 /* update the interface list */
2100 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2101 &mreqs
->imr_sourceaddr
, 1);
2104 err
= ip_mc_leave_group(sk
, &imr
);
2108 int ip_mc_msfilter(struct sock
*sk
, struct ip_msfilter
*msf
, int ifindex
)
2111 struct ip_mreqn imr
;
2112 __be32 addr
= msf
->imsf_multiaddr
;
2113 struct ip_mc_socklist
*pmc
;
2114 struct in_device
*in_dev
;
2115 struct inet_sock
*inet
= inet_sk(sk
);
2116 struct ip_sf_socklist
*newpsl
, *psl
;
2117 struct net
*net
= sock_net(sk
);
2120 if (!ipv4_is_multicast(addr
))
2122 if (msf
->imsf_fmode
!= MCAST_INCLUDE
&&
2123 msf
->imsf_fmode
!= MCAST_EXCLUDE
)
2128 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2129 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2130 imr
.imr_ifindex
= ifindex
;
2131 in_dev
= ip_mc_find_dev(net
, &imr
);
2138 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2139 if (msf
->imsf_fmode
== MCAST_INCLUDE
&& msf
->imsf_numsrc
== 0) {
2144 for_each_pmc_rtnl(inet
, pmc
) {
2145 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2146 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2149 if (!pmc
) { /* must have a prior join */
2153 if (msf
->imsf_numsrc
) {
2154 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(msf
->imsf_numsrc
),
2160 newpsl
->sl_max
= newpsl
->sl_count
= msf
->imsf_numsrc
;
2161 memcpy(newpsl
->sl_addr
, msf
->imsf_slist
,
2162 msf
->imsf_numsrc
* sizeof(msf
->imsf_slist
[0]));
2163 err
= ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2164 msf
->imsf_fmode
, newpsl
->sl_count
, newpsl
->sl_addr
, 0);
2166 sock_kfree_s(sk
, newpsl
, IP_SFLSIZE(newpsl
->sl_max
));
2171 (void) ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2172 msf
->imsf_fmode
, 0, NULL
, 0);
2174 psl
= rtnl_dereference(pmc
->sflist
);
2176 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2177 psl
->sl_count
, psl
->sl_addr
, 0);
2178 /* decrease mem now to avoid the memleak warning */
2179 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2180 kfree_rcu(psl
, rcu
);
2182 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2184 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2185 pmc
->sfmode
= msf
->imsf_fmode
;
2189 err
= ip_mc_leave_group(sk
, &imr
);
2193 int ip_mc_msfget(struct sock
*sk
, struct ip_msfilter
*msf
,
2194 struct ip_msfilter __user
*optval
, int __user
*optlen
)
2196 int err
, len
, count
, copycount
;
2197 struct ip_mreqn imr
;
2198 __be32 addr
= msf
->imsf_multiaddr
;
2199 struct ip_mc_socklist
*pmc
;
2200 struct in_device
*in_dev
;
2201 struct inet_sock
*inet
= inet_sk(sk
);
2202 struct ip_sf_socklist
*psl
;
2203 struct net
*net
= sock_net(sk
);
2205 if (!ipv4_is_multicast(addr
))
2210 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2211 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2212 imr
.imr_ifindex
= 0;
2213 in_dev
= ip_mc_find_dev(net
, &imr
);
2219 err
= -EADDRNOTAVAIL
;
2221 for_each_pmc_rtnl(inet
, pmc
) {
2222 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2223 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2226 if (!pmc
) /* must have a prior join */
2228 msf
->imsf_fmode
= pmc
->sfmode
;
2229 psl
= rtnl_dereference(pmc
->sflist
);
2235 count
= psl
->sl_count
;
2237 copycount
= count
< msf
->imsf_numsrc
? count
: msf
->imsf_numsrc
;
2238 len
= copycount
* sizeof(psl
->sl_addr
[0]);
2239 msf
->imsf_numsrc
= count
;
2240 if (put_user(IP_MSFILTER_SIZE(copycount
), optlen
) ||
2241 copy_to_user(optval
, msf
, IP_MSFILTER_SIZE(0))) {
2245 copy_to_user(&optval
->imsf_slist
[0], psl
->sl_addr
, len
))
2253 int ip_mc_gsfget(struct sock
*sk
, struct group_filter
*gsf
,
2254 struct group_filter __user
*optval
, int __user
*optlen
)
2256 int err
, i
, count
, copycount
;
2257 struct sockaddr_in
*psin
;
2259 struct ip_mc_socklist
*pmc
;
2260 struct inet_sock
*inet
= inet_sk(sk
);
2261 struct ip_sf_socklist
*psl
;
2263 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
2264 if (psin
->sin_family
!= AF_INET
)
2266 addr
= psin
->sin_addr
.s_addr
;
2267 if (!ipv4_is_multicast(addr
))
2272 err
= -EADDRNOTAVAIL
;
2274 for_each_pmc_rtnl(inet
, pmc
) {
2275 if (pmc
->multi
.imr_multiaddr
.s_addr
== addr
&&
2276 pmc
->multi
.imr_ifindex
== gsf
->gf_interface
)
2279 if (!pmc
) /* must have a prior join */
2281 gsf
->gf_fmode
= pmc
->sfmode
;
2282 psl
= rtnl_dereference(pmc
->sflist
);
2284 count
= psl
? psl
->sl_count
: 0;
2285 copycount
= count
< gsf
->gf_numsrc
? count
: gsf
->gf_numsrc
;
2286 gsf
->gf_numsrc
= count
;
2287 if (put_user(GROUP_FILTER_SIZE(copycount
), optlen
) ||
2288 copy_to_user(optval
, gsf
, GROUP_FILTER_SIZE(0))) {
2291 for (i
= 0; i
< copycount
; i
++) {
2292 struct sockaddr_storage ss
;
2294 psin
= (struct sockaddr_in
*)&ss
;
2295 memset(&ss
, 0, sizeof(ss
));
2296 psin
->sin_family
= AF_INET
;
2297 psin
->sin_addr
.s_addr
= psl
->sl_addr
[i
];
2298 if (copy_to_user(&optval
->gf_slist
[i
], &ss
, sizeof(ss
)))
2308 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2310 int ip_mc_sf_allow(struct sock
*sk
, __be32 loc_addr
, __be32 rmt_addr
, int dif
)
2312 struct inet_sock
*inet
= inet_sk(sk
);
2313 struct ip_mc_socklist
*pmc
;
2314 struct ip_sf_socklist
*psl
;
2319 if (!ipv4_is_multicast(loc_addr
))
2323 for_each_pmc_rcu(inet
, pmc
) {
2324 if (pmc
->multi
.imr_multiaddr
.s_addr
== loc_addr
&&
2325 pmc
->multi
.imr_ifindex
== dif
)
2331 psl
= rcu_dereference(pmc
->sflist
);
2332 ret
= (pmc
->sfmode
== MCAST_EXCLUDE
);
2336 for (i
= 0; i
< psl
->sl_count
; i
++) {
2337 if (psl
->sl_addr
[i
] == rmt_addr
)
2341 if (pmc
->sfmode
== MCAST_INCLUDE
&& i
>= psl
->sl_count
)
2343 if (pmc
->sfmode
== MCAST_EXCLUDE
&& i
< psl
->sl_count
)
2353 * A socket is closing.
2356 void ip_mc_drop_socket(struct sock
*sk
)
2358 struct inet_sock
*inet
= inet_sk(sk
);
2359 struct ip_mc_socklist
*iml
;
2360 struct net
*net
= sock_net(sk
);
2366 while ((iml
= rtnl_dereference(inet
->mc_list
)) != NULL
) {
2367 struct in_device
*in_dev
;
2369 inet
->mc_list
= iml
->next_rcu
;
2370 in_dev
= inetdev_by_index(net
, iml
->multi
.imr_ifindex
);
2371 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2373 ip_mc_dec_group(in_dev
, iml
->multi
.imr_multiaddr
.s_addr
);
2374 /* decrease mem now to avoid the memleak warning */
2375 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
2376 kfree_rcu(iml
, rcu
);
2381 /* called with rcu_read_lock() */
2382 int ip_check_mc_rcu(struct in_device
*in_dev
, __be32 mc_addr
, __be32 src_addr
, u16 proto
)
2384 struct ip_mc_list
*im
;
2385 struct ip_mc_list __rcu
**mc_hash
;
2386 struct ip_sf_list
*psf
;
2389 mc_hash
= rcu_dereference(in_dev
->mc_hash
);
2391 u32 hash
= hash_32((__force u32
)mc_addr
, MC_HASH_SZ_LOG
);
2393 for (im
= rcu_dereference(mc_hash
[hash
]);
2395 im
= rcu_dereference(im
->next_hash
)) {
2396 if (im
->multiaddr
== mc_addr
)
2400 for_each_pmc_rcu(in_dev
, im
) {
2401 if (im
->multiaddr
== mc_addr
)
2405 if (im
&& proto
== IPPROTO_IGMP
) {
2409 for (psf
= im
->sources
; psf
; psf
= psf
->sf_next
) {
2410 if (psf
->sf_inaddr
== src_addr
)
2414 rv
= psf
->sf_count
[MCAST_INCLUDE
] ||
2415 psf
->sf_count
[MCAST_EXCLUDE
] !=
2416 im
->sfcount
[MCAST_EXCLUDE
];
2418 rv
= im
->sfcount
[MCAST_EXCLUDE
] != 0;
2420 rv
= 1; /* unspecified source; tentatively allow */
2425 #if defined(CONFIG_PROC_FS)
2426 struct igmp_mc_iter_state
{
2427 struct seq_net_private p
;
2428 struct net_device
*dev
;
2429 struct in_device
*in_dev
;
2432 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2434 static inline struct ip_mc_list
*igmp_mc_get_first(struct seq_file
*seq
)
2436 struct net
*net
= seq_file_net(seq
);
2437 struct ip_mc_list
*im
= NULL
;
2438 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2440 state
->in_dev
= NULL
;
2441 for_each_netdev_rcu(net
, state
->dev
) {
2442 struct in_device
*in_dev
;
2444 in_dev
= __in_dev_get_rcu(state
->dev
);
2447 im
= rcu_dereference(in_dev
->mc_list
);
2449 state
->in_dev
= in_dev
;
2456 static struct ip_mc_list
*igmp_mc_get_next(struct seq_file
*seq
, struct ip_mc_list
*im
)
2458 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2460 im
= rcu_dereference(im
->next_rcu
);
2462 state
->dev
= next_net_device_rcu(state
->dev
);
2464 state
->in_dev
= NULL
;
2467 state
->in_dev
= __in_dev_get_rcu(state
->dev
);
2470 im
= rcu_dereference(state
->in_dev
->mc_list
);
2475 static struct ip_mc_list
*igmp_mc_get_idx(struct seq_file
*seq
, loff_t pos
)
2477 struct ip_mc_list
*im
= igmp_mc_get_first(seq
);
2479 while (pos
&& (im
= igmp_mc_get_next(seq
, im
)) != NULL
)
2481 return pos
? NULL
: im
;
2484 static void *igmp_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2488 return *pos
? igmp_mc_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2491 static void *igmp_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2493 struct ip_mc_list
*im
;
2494 if (v
== SEQ_START_TOKEN
)
2495 im
= igmp_mc_get_first(seq
);
2497 im
= igmp_mc_get_next(seq
, v
);
2502 static void igmp_mc_seq_stop(struct seq_file
*seq
, void *v
)
2505 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2507 state
->in_dev
= NULL
;
2512 static int igmp_mc_seq_show(struct seq_file
*seq
, void *v
)
2514 if (v
== SEQ_START_TOKEN
)
2516 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2518 struct ip_mc_list
*im
= (struct ip_mc_list
*)v
;
2519 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2523 #ifdef CONFIG_IP_MULTICAST
2524 querier
= IGMP_V1_SEEN(state
->in_dev
) ? "V1" :
2525 IGMP_V2_SEEN(state
->in_dev
) ? "V2" :
2531 if (rcu_access_pointer(state
->in_dev
->mc_list
) == im
) {
2532 seq_printf(seq
, "%d\t%-10s: %5d %7s\n",
2533 state
->dev
->ifindex
, state
->dev
->name
, state
->in_dev
->mc_count
, querier
);
2536 delta
= im
->timer
.expires
- jiffies
;
2538 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2539 im
->multiaddr
, im
->users
,
2541 im
->tm_running
? jiffies_delta_to_clock_t(delta
) : 0,
2547 static const struct seq_operations igmp_mc_seq_ops
= {
2548 .start
= igmp_mc_seq_start
,
2549 .next
= igmp_mc_seq_next
,
2550 .stop
= igmp_mc_seq_stop
,
2551 .show
= igmp_mc_seq_show
,
2554 static int igmp_mc_seq_open(struct inode
*inode
, struct file
*file
)
2556 return seq_open_net(inode
, file
, &igmp_mc_seq_ops
,
2557 sizeof(struct igmp_mc_iter_state
));
2560 static const struct file_operations igmp_mc_seq_fops
= {
2561 .owner
= THIS_MODULE
,
2562 .open
= igmp_mc_seq_open
,
2564 .llseek
= seq_lseek
,
2565 .release
= seq_release_net
,
2568 struct igmp_mcf_iter_state
{
2569 struct seq_net_private p
;
2570 struct net_device
*dev
;
2571 struct in_device
*idev
;
2572 struct ip_mc_list
*im
;
2575 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2577 static inline struct ip_sf_list
*igmp_mcf_get_first(struct seq_file
*seq
)
2579 struct net
*net
= seq_file_net(seq
);
2580 struct ip_sf_list
*psf
= NULL
;
2581 struct ip_mc_list
*im
= NULL
;
2582 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2586 for_each_netdev_rcu(net
, state
->dev
) {
2587 struct in_device
*idev
;
2588 idev
= __in_dev_get_rcu(state
->dev
);
2589 if (unlikely(!idev
))
2591 im
= rcu_dereference(idev
->mc_list
);
2593 spin_lock_bh(&im
->lock
);
2600 spin_unlock_bh(&im
->lock
);
2606 static struct ip_sf_list
*igmp_mcf_get_next(struct seq_file
*seq
, struct ip_sf_list
*psf
)
2608 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2612 spin_unlock_bh(&state
->im
->lock
);
2613 state
->im
= state
->im
->next
;
2614 while (!state
->im
) {
2615 state
->dev
= next_net_device_rcu(state
->dev
);
2620 state
->idev
= __in_dev_get_rcu(state
->dev
);
2623 state
->im
= rcu_dereference(state
->idev
->mc_list
);
2627 spin_lock_bh(&state
->im
->lock
);
2628 psf
= state
->im
->sources
;
2634 static struct ip_sf_list
*igmp_mcf_get_idx(struct seq_file
*seq
, loff_t pos
)
2636 struct ip_sf_list
*psf
= igmp_mcf_get_first(seq
);
2638 while (pos
&& (psf
= igmp_mcf_get_next(seq
, psf
)) != NULL
)
2640 return pos
? NULL
: psf
;
2643 static void *igmp_mcf_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2647 return *pos
? igmp_mcf_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2650 static void *igmp_mcf_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2652 struct ip_sf_list
*psf
;
2653 if (v
== SEQ_START_TOKEN
)
2654 psf
= igmp_mcf_get_first(seq
);
2656 psf
= igmp_mcf_get_next(seq
, v
);
2661 static void igmp_mcf_seq_stop(struct seq_file
*seq
, void *v
)
2664 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2665 if (likely(state
->im
)) {
2666 spin_unlock_bh(&state
->im
->lock
);
2674 static int igmp_mcf_seq_show(struct seq_file
*seq
, void *v
)
2676 struct ip_sf_list
*psf
= (struct ip_sf_list
*)v
;
2677 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2679 if (v
== SEQ_START_TOKEN
) {
2680 seq_puts(seq
, "Idx Device MCA SRC INC EXC\n");
2684 "0x%08x %6lu %6lu\n",
2685 state
->dev
->ifindex
, state
->dev
->name
,
2686 ntohl(state
->im
->multiaddr
),
2687 ntohl(psf
->sf_inaddr
),
2688 psf
->sf_count
[MCAST_INCLUDE
],
2689 psf
->sf_count
[MCAST_EXCLUDE
]);
2694 static const struct seq_operations igmp_mcf_seq_ops
= {
2695 .start
= igmp_mcf_seq_start
,
2696 .next
= igmp_mcf_seq_next
,
2697 .stop
= igmp_mcf_seq_stop
,
2698 .show
= igmp_mcf_seq_show
,
2701 static int igmp_mcf_seq_open(struct inode
*inode
, struct file
*file
)
2703 return seq_open_net(inode
, file
, &igmp_mcf_seq_ops
,
2704 sizeof(struct igmp_mcf_iter_state
));
2707 static const struct file_operations igmp_mcf_seq_fops
= {
2708 .owner
= THIS_MODULE
,
2709 .open
= igmp_mcf_seq_open
,
2711 .llseek
= seq_lseek
,
2712 .release
= seq_release_net
,
2715 static int __net_init
igmp_net_init(struct net
*net
)
2717 struct proc_dir_entry
*pde
;
2720 pde
= proc_create("igmp", S_IRUGO
, net
->proc_net
, &igmp_mc_seq_fops
);
2723 pde
= proc_create("mcfilter", S_IRUGO
, net
->proc_net
,
2724 &igmp_mcf_seq_fops
);
2727 err
= inet_ctl_sock_create(&net
->ipv4
.mc_autojoin_sk
, AF_INET
,
2728 SOCK_DGRAM
, 0, net
);
2730 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
2738 remove_proc_entry("mcfilter", net
->proc_net
);
2740 remove_proc_entry("igmp", net
->proc_net
);
2745 static void __net_exit
igmp_net_exit(struct net
*net
)
2747 remove_proc_entry("mcfilter", net
->proc_net
);
2748 remove_proc_entry("igmp", net
->proc_net
);
2749 inet_ctl_sock_destroy(net
->ipv4
.mc_autojoin_sk
);
2752 static struct pernet_operations igmp_net_ops
= {
2753 .init
= igmp_net_init
,
2754 .exit
= igmp_net_exit
,
2758 static int igmp_netdev_event(struct notifier_block
*this,
2759 unsigned long event
, void *ptr
)
2761 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
2762 struct in_device
*in_dev
;
2765 case NETDEV_RESEND_IGMP
:
2766 in_dev
= __in_dev_get_rtnl(dev
);
2768 ip_mc_rejoin_groups(in_dev
);
2776 static struct notifier_block igmp_notifier
= {
2777 .notifier_call
= igmp_netdev_event
,
2780 int __init
igmp_mc_init(void)
2782 #if defined(CONFIG_PROC_FS)
2785 err
= register_pernet_subsys(&igmp_net_ops
);
2788 err
= register_netdevice_notifier(&igmp_notifier
);
2790 goto reg_notif_fail
;
2794 unregister_pernet_subsys(&igmp_net_ops
);
2797 return register_netdevice_notifier(&igmp_notifier
);