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 <linux/netfilter_ipv4.h>
101 #ifdef CONFIG_IP_MROUTE
102 #include <linux/mroute.h>
104 #ifdef CONFIG_PROC_FS
105 #include <linux/proc_fs.h>
106 #include <linux/seq_file.h>
109 #define IP_MAX_MEMBERSHIPS 20
110 #define IP_MAX_MSF 10
112 #ifdef CONFIG_IP_MULTICAST
113 /* Parameter names and values are taken from igmp-v2-06 draft */
115 #define IGMP_V1_Router_Present_Timeout (400*HZ)
116 #define IGMP_V2_Router_Present_Timeout (400*HZ)
117 #define IGMP_V2_Unsolicited_Report_Interval (10*HZ)
118 #define IGMP_V3_Unsolicited_Report_Interval (1*HZ)
119 #define IGMP_Query_Response_Interval (10*HZ)
120 #define IGMP_Unsolicited_Report_Count 2
123 #define IGMP_Initial_Report_Delay (1)
125 /* IGMP_Initial_Report_Delay is not from IGMP specs!
126 * IGMP specs require to report membership immediately after
127 * joining a group, but we delay the first report by a
128 * small interval. It seems more natural and still does not
129 * contradict to specs provided this delay is small enough.
132 #define IGMP_V1_SEEN(in_dev) \
133 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
134 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
135 ((in_dev)->mr_v1_seen && \
136 time_before(jiffies, (in_dev)->mr_v1_seen)))
137 #define IGMP_V2_SEEN(in_dev) \
138 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
139 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
140 ((in_dev)->mr_v2_seen && \
141 time_before(jiffies, (in_dev)->mr_v2_seen)))
143 static int unsolicited_report_interval(struct in_device
*in_dev
)
145 int interval_ms
, interval_jiffies
;
147 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
148 interval_ms
= IN_DEV_CONF_GET(
150 IGMPV2_UNSOLICITED_REPORT_INTERVAL
);
152 interval_ms
= IN_DEV_CONF_GET(
154 IGMPV3_UNSOLICITED_REPORT_INTERVAL
);
156 interval_jiffies
= msecs_to_jiffies(interval_ms
);
158 /* _timer functions can't handle a delay of 0 jiffies so ensure
159 * we always return a positive value.
161 if (interval_jiffies
<= 0)
162 interval_jiffies
= 1;
163 return interval_jiffies
;
166 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
);
167 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
);
168 static void igmpv3_clear_delrec(struct in_device
*in_dev
);
169 static int sf_setstate(struct ip_mc_list
*pmc
);
170 static void sf_markstate(struct ip_mc_list
*pmc
);
172 static void ip_mc_clear_src(struct ip_mc_list
*pmc
);
173 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
174 int sfcount
, __be32
*psfsrc
, int delta
);
176 static void ip_ma_put(struct ip_mc_list
*im
)
178 if (atomic_dec_and_test(&im
->refcnt
)) {
179 in_dev_put(im
->interface
);
184 #define for_each_pmc_rcu(in_dev, pmc) \
185 for (pmc = rcu_dereference(in_dev->mc_list); \
187 pmc = rcu_dereference(pmc->next_rcu))
189 #define for_each_pmc_rtnl(in_dev, pmc) \
190 for (pmc = rtnl_dereference(in_dev->mc_list); \
192 pmc = rtnl_dereference(pmc->next_rcu))
194 #ifdef CONFIG_IP_MULTICAST
200 static void igmp_stop_timer(struct ip_mc_list
*im
)
202 spin_lock_bh(&im
->lock
);
203 if (del_timer(&im
->timer
))
204 atomic_dec(&im
->refcnt
);
207 im
->unsolicit_count
= 0;
208 spin_unlock_bh(&im
->lock
);
211 /* It must be called with locked im->lock */
212 static void igmp_start_timer(struct ip_mc_list
*im
, int max_delay
)
214 int tv
= net_random() % max_delay
;
217 if (!mod_timer(&im
->timer
, jiffies
+tv
+2))
218 atomic_inc(&im
->refcnt
);
221 static void igmp_gq_start_timer(struct in_device
*in_dev
)
223 int tv
= net_random() % in_dev
->mr_maxdelay
;
225 in_dev
->mr_gq_running
= 1;
226 if (!mod_timer(&in_dev
->mr_gq_timer
, jiffies
+tv
+2))
230 static void igmp_ifc_start_timer(struct in_device
*in_dev
, int delay
)
232 int tv
= net_random() % delay
;
234 if (!mod_timer(&in_dev
->mr_ifc_timer
, jiffies
+tv
+2))
238 static void igmp_mod_timer(struct ip_mc_list
*im
, int max_delay
)
240 spin_lock_bh(&im
->lock
);
241 im
->unsolicit_count
= 0;
242 if (del_timer(&im
->timer
)) {
243 if ((long)(im
->timer
.expires
-jiffies
) < max_delay
) {
244 add_timer(&im
->timer
);
246 spin_unlock_bh(&im
->lock
);
249 atomic_dec(&im
->refcnt
);
251 igmp_start_timer(im
, max_delay
);
252 spin_unlock_bh(&im
->lock
);
257 * Send an IGMP report.
260 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
263 static int is_in(struct ip_mc_list
*pmc
, struct ip_sf_list
*psf
, int type
,
264 int gdeleted
, int sdeleted
)
267 case IGMPV3_MODE_IS_INCLUDE
:
268 case IGMPV3_MODE_IS_EXCLUDE
:
269 if (gdeleted
|| sdeleted
)
271 if (!(pmc
->gsquery
&& !psf
->sf_gsresp
)) {
272 if (pmc
->sfmode
== MCAST_INCLUDE
)
274 /* don't include if this source is excluded
277 if (psf
->sf_count
[MCAST_INCLUDE
])
278 return type
== IGMPV3_MODE_IS_INCLUDE
;
279 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
280 psf
->sf_count
[MCAST_EXCLUDE
];
283 case IGMPV3_CHANGE_TO_INCLUDE
:
284 if (gdeleted
|| sdeleted
)
286 return psf
->sf_count
[MCAST_INCLUDE
] != 0;
287 case IGMPV3_CHANGE_TO_EXCLUDE
:
288 if (gdeleted
|| sdeleted
)
290 if (pmc
->sfcount
[MCAST_EXCLUDE
] == 0 ||
291 psf
->sf_count
[MCAST_INCLUDE
])
293 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
294 psf
->sf_count
[MCAST_EXCLUDE
];
295 case IGMPV3_ALLOW_NEW_SOURCES
:
296 if (gdeleted
|| !psf
->sf_crcount
)
298 return (pmc
->sfmode
== MCAST_INCLUDE
) ^ sdeleted
;
299 case IGMPV3_BLOCK_OLD_SOURCES
:
300 if (pmc
->sfmode
== MCAST_INCLUDE
)
301 return gdeleted
|| (psf
->sf_crcount
&& sdeleted
);
302 return psf
->sf_crcount
&& !gdeleted
&& !sdeleted
;
308 igmp_scount(struct ip_mc_list
*pmc
, int type
, int gdeleted
, int sdeleted
)
310 struct ip_sf_list
*psf
;
313 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
314 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
))
321 #define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb))
323 static struct sk_buff
*igmpv3_newpack(struct net_device
*dev
, int size
)
328 struct igmpv3_report
*pig
;
329 struct net
*net
= dev_net(dev
);
331 int hlen
= LL_RESERVED_SPACE(dev
);
332 int tlen
= dev
->needed_tailroom
;
335 skb
= alloc_skb(size
+ hlen
+ tlen
,
336 GFP_ATOMIC
| __GFP_NOWARN
);
343 skb
->priority
= TC_PRIO_CONTROL
;
344 igmp_skb_size(skb
) = size
;
346 rt
= ip_route_output_ports(net
, &fl4
, NULL
, IGMPV3_ALL_MCR
, 0,
348 IPPROTO_IGMP
, 0, dev
->ifindex
);
354 skb_dst_set(skb
, &rt
->dst
);
357 skb_reserve(skb
, hlen
);
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(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)->dev ? igmp_skb_size(skb) - (skb)->len : \
427 skb_tailroom(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(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
? in_dev
->mr_qrv
:
760 IGMP_Unsolicited_Report_Count
;
761 igmp_ifc_start_timer(in_dev
, 1);
765 static void igmp_timer_expire(unsigned long data
)
767 struct ip_mc_list
*im
=(struct ip_mc_list
*)data
;
768 struct in_device
*in_dev
= im
->interface
;
770 spin_lock(&im
->lock
);
773 if (im
->unsolicit_count
) {
774 im
->unsolicit_count
--;
775 igmp_start_timer(im
, unsolicited_report_interval(in_dev
));
778 spin_unlock(&im
->lock
);
780 if (IGMP_V1_SEEN(in_dev
))
781 igmp_send_report(in_dev
, im
, IGMP_HOST_MEMBERSHIP_REPORT
);
782 else if (IGMP_V2_SEEN(in_dev
))
783 igmp_send_report(in_dev
, im
, IGMPV2_HOST_MEMBERSHIP_REPORT
);
785 igmp_send_report(in_dev
, im
, IGMPV3_HOST_MEMBERSHIP_REPORT
);
790 /* mark EXCLUDE-mode sources */
791 static int igmp_xmarksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
793 struct ip_sf_list
*psf
;
797 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
800 for (i
=0; i
<nsrcs
; i
++) {
801 /* skip inactive filters */
802 if (psf
->sf_count
[MCAST_INCLUDE
] ||
803 pmc
->sfcount
[MCAST_EXCLUDE
] !=
804 psf
->sf_count
[MCAST_EXCLUDE
])
806 if (srcs
[i
] == psf
->sf_inaddr
) {
813 if (scount
== nsrcs
) /* all sources excluded */
818 static int igmp_marksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
820 struct ip_sf_list
*psf
;
823 if (pmc
->sfmode
== MCAST_EXCLUDE
)
824 return igmp_xmarksources(pmc
, nsrcs
, srcs
);
826 /* mark INCLUDE-mode sources */
828 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
831 for (i
=0; i
<nsrcs
; i
++)
832 if (srcs
[i
] == psf
->sf_inaddr
) {
846 /* return true if packet was dropped */
847 static bool igmp_heard_report(struct in_device
*in_dev
, __be32 group
)
849 struct ip_mc_list
*im
;
851 /* Timers are only set for non-local groups */
853 if (group
== IGMP_ALL_HOSTS
)
857 for_each_pmc_rcu(in_dev
, im
) {
858 if (im
->multiaddr
== group
) {
867 /* return true if packet was dropped */
868 static bool igmp_heard_query(struct in_device
*in_dev
, struct sk_buff
*skb
,
871 struct igmphdr
*ih
= igmp_hdr(skb
);
872 struct igmpv3_query
*ih3
= igmpv3_query_hdr(skb
);
873 struct ip_mc_list
*im
;
874 __be32 group
= ih
->group
;
881 /* Alas, old v1 router presents here. */
883 max_delay
= IGMP_Query_Response_Interval
;
884 in_dev
->mr_v1_seen
= jiffies
+
885 IGMP_V1_Router_Present_Timeout
;
888 /* v2 router present */
889 max_delay
= ih
->code
*(HZ
/IGMP_TIMER_SCALE
);
890 in_dev
->mr_v2_seen
= jiffies
+
891 IGMP_V2_Router_Present_Timeout
;
893 /* cancel the interface change timer */
894 in_dev
->mr_ifc_count
= 0;
895 if (del_timer(&in_dev
->mr_ifc_timer
))
896 __in_dev_put(in_dev
);
897 /* clear deleted report items */
898 igmpv3_clear_delrec(in_dev
);
899 } else if (len
< 12) {
900 return true; /* ignore bogus packet; freed by caller */
901 } else if (IGMP_V1_SEEN(in_dev
)) {
902 /* This is a v3 query with v1 queriers present */
903 max_delay
= IGMP_Query_Response_Interval
;
905 } else if (IGMP_V2_SEEN(in_dev
)) {
906 /* this is a v3 query with v2 queriers present;
907 * Interpretation of the max_delay code is problematic here.
908 * A real v2 host would use ih_code directly, while v3 has a
909 * different encoding. We use the v3 encoding as more likely
910 * to be intended in a v3 query.
912 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
914 max_delay
= 1; /* can't mod w/ 0 */
916 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)))
919 ih3
= igmpv3_query_hdr(skb
);
921 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)
922 + ntohs(ih3
->nsrcs
)*sizeof(__be32
)))
924 ih3
= igmpv3_query_hdr(skb
);
927 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
929 max_delay
= 1; /* can't mod w/ 0 */
930 in_dev
->mr_maxdelay
= max_delay
;
932 in_dev
->mr_qrv
= ih3
->qrv
;
933 if (!group
) { /* general query */
935 return false; /* no sources allowed */
936 igmp_gq_start_timer(in_dev
);
939 /* mark sources to include, if group & source-specific */
940 mark
= ih3
->nsrcs
!= 0;
944 * - Start the timers in all of our membership records
945 * that the query applies to for the interface on
946 * which the query arrived excl. those that belong
947 * to a "local" group (224.0.0.X)
948 * - For timers already running check if they need to
950 * - Use the igmp->igmp_code field as the maximum
954 for_each_pmc_rcu(in_dev
, im
) {
957 if (group
&& group
!= im
->multiaddr
)
959 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
961 spin_lock_bh(&im
->lock
);
963 im
->gsquery
= im
->gsquery
&& mark
;
966 changed
= !im
->gsquery
||
967 igmp_marksources(im
, ntohs(ih3
->nsrcs
), ih3
->srcs
);
968 spin_unlock_bh(&im
->lock
);
970 igmp_mod_timer(im
, max_delay
);
976 /* called in rcu_read_lock() section */
977 int igmp_rcv(struct sk_buff
*skb
)
979 /* This basically follows the spec line by line -- see RFC1112 */
981 struct in_device
*in_dev
= __in_dev_get_rcu(skb
->dev
);
988 if (!pskb_may_pull(skb
, sizeof(struct igmphdr
)))
991 switch (skb
->ip_summed
) {
992 case CHECKSUM_COMPLETE
:
993 if (!csum_fold(skb
->csum
))
998 if (__skb_checksum_complete(skb
))
1004 case IGMP_HOST_MEMBERSHIP_QUERY
:
1005 dropped
= igmp_heard_query(in_dev
, skb
, len
);
1007 case IGMP_HOST_MEMBERSHIP_REPORT
:
1008 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1009 /* Is it our report looped back? */
1010 if (rt_is_output_route(skb_rtable(skb
)))
1012 /* don't rely on MC router hearing unicast reports */
1013 if (skb
->pkt_type
== PACKET_MULTICAST
||
1014 skb
->pkt_type
== PACKET_BROADCAST
)
1015 dropped
= igmp_heard_report(in_dev
, ih
->group
);
1018 #ifdef CONFIG_IP_PIMSM_V1
1019 return pim_rcv_v1(skb
);
1021 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1024 case IGMP_HOST_LEAVE_MESSAGE
:
1026 case IGMP_MTRACE_RESP
:
1044 * Add a filter to a device
1047 static void ip_mc_filter_add(struct in_device
*in_dev
, __be32 addr
)
1049 char buf
[MAX_ADDR_LEN
];
1050 struct net_device
*dev
= in_dev
->dev
;
1052 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1053 We will get multicast token leakage, when IFF_MULTICAST
1054 is changed. This check should be done in ndo_set_rx_mode
1055 routine. Something sort of:
1056 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1059 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1060 dev_mc_add(dev
, buf
);
1064 * Remove a filter from a device
1067 static void ip_mc_filter_del(struct in_device
*in_dev
, __be32 addr
)
1069 char buf
[MAX_ADDR_LEN
];
1070 struct net_device
*dev
= in_dev
->dev
;
1072 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1073 dev_mc_del(dev
, buf
);
1076 #ifdef CONFIG_IP_MULTICAST
1078 * deleted ip_mc_list manipulation
1080 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
)
1082 struct ip_mc_list
*pmc
;
1084 /* this is an "ip_mc_list" for convenience; only the fields below
1085 * are actually used. In particular, the refcnt and users are not
1086 * used for management of the delete list. Using the same structure
1087 * for deleted items allows change reports to use common code with
1088 * non-deleted or query-response MCA's.
1090 pmc
= kzalloc(sizeof(*pmc
), GFP_KERNEL
);
1093 spin_lock_bh(&im
->lock
);
1094 pmc
->interface
= im
->interface
;
1095 in_dev_hold(in_dev
);
1096 pmc
->multiaddr
= im
->multiaddr
;
1097 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1098 IGMP_Unsolicited_Report_Count
;
1099 pmc
->sfmode
= im
->sfmode
;
1100 if (pmc
->sfmode
== MCAST_INCLUDE
) {
1101 struct ip_sf_list
*psf
;
1103 pmc
->tomb
= im
->tomb
;
1104 pmc
->sources
= im
->sources
;
1105 im
->tomb
= im
->sources
= NULL
;
1106 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
)
1107 psf
->sf_crcount
= pmc
->crcount
;
1109 spin_unlock_bh(&im
->lock
);
1111 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1112 pmc
->next
= in_dev
->mc_tomb
;
1113 in_dev
->mc_tomb
= pmc
;
1114 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1117 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
)
1119 struct ip_mc_list
*pmc
, *pmc_prev
;
1120 struct ip_sf_list
*psf
, *psf_next
;
1122 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1124 for (pmc
=in_dev
->mc_tomb
; pmc
; pmc
=pmc
->next
) {
1125 if (pmc
->multiaddr
== multiaddr
)
1131 pmc_prev
->next
= pmc
->next
;
1133 in_dev
->mc_tomb
= pmc
->next
;
1135 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1137 for (psf
=pmc
->tomb
; psf
; psf
=psf_next
) {
1138 psf_next
= psf
->sf_next
;
1141 in_dev_put(pmc
->interface
);
1146 static void igmpv3_clear_delrec(struct in_device
*in_dev
)
1148 struct ip_mc_list
*pmc
, *nextpmc
;
1150 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1151 pmc
= in_dev
->mc_tomb
;
1152 in_dev
->mc_tomb
= NULL
;
1153 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1155 for (; pmc
; pmc
= nextpmc
) {
1156 nextpmc
= pmc
->next
;
1157 ip_mc_clear_src(pmc
);
1158 in_dev_put(pmc
->interface
);
1161 /* clear dead sources, too */
1163 for_each_pmc_rcu(in_dev
, pmc
) {
1164 struct ip_sf_list
*psf
, *psf_next
;
1166 spin_lock_bh(&pmc
->lock
);
1169 spin_unlock_bh(&pmc
->lock
);
1170 for (; psf
; psf
=psf_next
) {
1171 psf_next
= psf
->sf_next
;
1179 static void igmp_group_dropped(struct ip_mc_list
*im
)
1181 struct in_device
*in_dev
= im
->interface
;
1182 #ifdef CONFIG_IP_MULTICAST
1188 ip_mc_filter_del(in_dev
, im
->multiaddr
);
1191 #ifdef CONFIG_IP_MULTICAST
1192 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1195 reporter
= im
->reporter
;
1196 igmp_stop_timer(im
);
1198 if (!in_dev
->dead
) {
1199 if (IGMP_V1_SEEN(in_dev
))
1201 if (IGMP_V2_SEEN(in_dev
)) {
1203 igmp_send_report(in_dev
, im
, IGMP_HOST_LEAVE_MESSAGE
);
1207 igmpv3_add_delrec(in_dev
, im
);
1209 igmp_ifc_event(in_dev
);
1214 static void igmp_group_added(struct ip_mc_list
*im
)
1216 struct in_device
*in_dev
= im
->interface
;
1218 if (im
->loaded
== 0) {
1220 ip_mc_filter_add(in_dev
, im
->multiaddr
);
1223 #ifdef CONFIG_IP_MULTICAST
1224 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1229 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
)) {
1230 spin_lock_bh(&im
->lock
);
1231 igmp_start_timer(im
, IGMP_Initial_Report_Delay
);
1232 spin_unlock_bh(&im
->lock
);
1237 im
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1238 IGMP_Unsolicited_Report_Count
;
1239 igmp_ifc_event(in_dev
);
1245 * Multicast list managers
1248 static u32
ip_mc_hash(const struct ip_mc_list
*im
)
1250 return hash_32((__force u32
)im
->multiaddr
, MC_HASH_SZ_LOG
);
1253 static void ip_mc_hash_add(struct in_device
*in_dev
,
1254 struct ip_mc_list
*im
)
1256 struct ip_mc_list __rcu
**mc_hash
;
1259 mc_hash
= rtnl_dereference(in_dev
->mc_hash
);
1261 hash
= ip_mc_hash(im
);
1262 im
->next_hash
= mc_hash
[hash
];
1263 rcu_assign_pointer(mc_hash
[hash
], im
);
1267 /* do not use a hash table for small number of items */
1268 if (in_dev
->mc_count
< 4)
1271 mc_hash
= kzalloc(sizeof(struct ip_mc_list
*) << MC_HASH_SZ_LOG
,
1276 for_each_pmc_rtnl(in_dev
, im
) {
1277 hash
= ip_mc_hash(im
);
1278 im
->next_hash
= mc_hash
[hash
];
1279 RCU_INIT_POINTER(mc_hash
[hash
], im
);
1282 rcu_assign_pointer(in_dev
->mc_hash
, mc_hash
);
1285 static void ip_mc_hash_remove(struct in_device
*in_dev
,
1286 struct ip_mc_list
*im
)
1288 struct ip_mc_list __rcu
**mc_hash
= rtnl_dereference(in_dev
->mc_hash
);
1289 struct ip_mc_list
*aux
;
1293 mc_hash
+= ip_mc_hash(im
);
1294 while ((aux
= rtnl_dereference(*mc_hash
)) != im
)
1295 mc_hash
= &aux
->next_hash
;
1296 *mc_hash
= im
->next_hash
;
1301 * A socket has joined a multicast group on device dev.
1304 void ip_mc_inc_group(struct in_device
*in_dev
, __be32 addr
)
1306 struct ip_mc_list
*im
;
1310 for_each_pmc_rtnl(in_dev
, im
) {
1311 if (im
->multiaddr
== addr
) {
1313 ip_mc_add_src(in_dev
, &addr
, MCAST_EXCLUDE
, 0, NULL
, 0);
1318 im
= kzalloc(sizeof(*im
), GFP_KERNEL
);
1323 im
->interface
= in_dev
;
1324 in_dev_hold(in_dev
);
1325 im
->multiaddr
= addr
;
1326 /* initial mode is (EX, empty) */
1327 im
->sfmode
= MCAST_EXCLUDE
;
1328 im
->sfcount
[MCAST_EXCLUDE
] = 1;
1329 atomic_set(&im
->refcnt
, 1);
1330 spin_lock_init(&im
->lock
);
1331 #ifdef CONFIG_IP_MULTICAST
1332 setup_timer(&im
->timer
, &igmp_timer_expire
, (unsigned long)im
);
1333 im
->unsolicit_count
= IGMP_Unsolicited_Report_Count
;
1336 im
->next_rcu
= in_dev
->mc_list
;
1338 rcu_assign_pointer(in_dev
->mc_list
, im
);
1340 ip_mc_hash_add(in_dev
, im
);
1342 #ifdef CONFIG_IP_MULTICAST
1343 igmpv3_del_delrec(in_dev
, im
->multiaddr
);
1345 igmp_group_added(im
);
1347 ip_rt_multicast_event(in_dev
);
1351 EXPORT_SYMBOL(ip_mc_inc_group
);
1354 * Resend IGMP JOIN report; used by netdev notifier.
1356 static void ip_mc_rejoin_groups(struct in_device
*in_dev
)
1358 #ifdef CONFIG_IP_MULTICAST
1359 struct ip_mc_list
*im
;
1364 for_each_pmc_rtnl(in_dev
, im
) {
1365 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1368 /* a failover is happening and switches
1369 * must be notified immediately
1371 if (IGMP_V1_SEEN(in_dev
))
1372 type
= IGMP_HOST_MEMBERSHIP_REPORT
;
1373 else if (IGMP_V2_SEEN(in_dev
))
1374 type
= IGMPV2_HOST_MEMBERSHIP_REPORT
;
1376 type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
1377 igmp_send_report(in_dev
, im
, type
);
1383 * A socket has left a multicast group on device dev
1386 void ip_mc_dec_group(struct in_device
*in_dev
, __be32 addr
)
1388 struct ip_mc_list
*i
;
1389 struct ip_mc_list __rcu
**ip
;
1393 for (ip
= &in_dev
->mc_list
;
1394 (i
= rtnl_dereference(*ip
)) != NULL
;
1395 ip
= &i
->next_rcu
) {
1396 if (i
->multiaddr
== addr
) {
1397 if (--i
->users
== 0) {
1398 ip_mc_hash_remove(in_dev
, i
);
1401 igmp_group_dropped(i
);
1405 ip_rt_multicast_event(in_dev
);
1414 EXPORT_SYMBOL(ip_mc_dec_group
);
1416 /* Device changing type */
1418 void ip_mc_unmap(struct in_device
*in_dev
)
1420 struct ip_mc_list
*pmc
;
1424 for_each_pmc_rtnl(in_dev
, pmc
)
1425 igmp_group_dropped(pmc
);
1428 void ip_mc_remap(struct in_device
*in_dev
)
1430 struct ip_mc_list
*pmc
;
1434 for_each_pmc_rtnl(in_dev
, pmc
)
1435 igmp_group_added(pmc
);
1438 /* Device going down */
1440 void ip_mc_down(struct in_device
*in_dev
)
1442 struct ip_mc_list
*pmc
;
1446 for_each_pmc_rtnl(in_dev
, pmc
)
1447 igmp_group_dropped(pmc
);
1449 #ifdef CONFIG_IP_MULTICAST
1450 in_dev
->mr_ifc_count
= 0;
1451 if (del_timer(&in_dev
->mr_ifc_timer
))
1452 __in_dev_put(in_dev
);
1453 in_dev
->mr_gq_running
= 0;
1454 if (del_timer(&in_dev
->mr_gq_timer
))
1455 __in_dev_put(in_dev
);
1456 igmpv3_clear_delrec(in_dev
);
1459 ip_mc_dec_group(in_dev
, IGMP_ALL_HOSTS
);
1462 void ip_mc_init_dev(struct in_device
*in_dev
)
1466 #ifdef CONFIG_IP_MULTICAST
1467 setup_timer(&in_dev
->mr_gq_timer
, igmp_gq_timer_expire
,
1468 (unsigned long)in_dev
);
1469 setup_timer(&in_dev
->mr_ifc_timer
, igmp_ifc_timer_expire
,
1470 (unsigned long)in_dev
);
1471 in_dev
->mr_qrv
= IGMP_Unsolicited_Report_Count
;
1474 spin_lock_init(&in_dev
->mc_tomb_lock
);
1477 /* Device going up */
1479 void ip_mc_up(struct in_device
*in_dev
)
1481 struct ip_mc_list
*pmc
;
1485 ip_mc_inc_group(in_dev
, IGMP_ALL_HOSTS
);
1487 for_each_pmc_rtnl(in_dev
, pmc
)
1488 igmp_group_added(pmc
);
1492 * Device is about to be destroyed: clean up.
1495 void ip_mc_destroy_dev(struct in_device
*in_dev
)
1497 struct ip_mc_list
*i
;
1501 /* Deactivate timers */
1504 while ((i
= rtnl_dereference(in_dev
->mc_list
)) != NULL
) {
1505 in_dev
->mc_list
= i
->next_rcu
;
1508 /* We've dropped the groups in ip_mc_down already */
1514 /* RTNL is locked */
1515 static struct in_device
*ip_mc_find_dev(struct net
*net
, struct ip_mreqn
*imr
)
1517 struct net_device
*dev
= NULL
;
1518 struct in_device
*idev
= NULL
;
1520 if (imr
->imr_ifindex
) {
1521 idev
= inetdev_by_index(net
, imr
->imr_ifindex
);
1524 if (imr
->imr_address
.s_addr
) {
1525 dev
= __ip_dev_find(net
, imr
->imr_address
.s_addr
, false);
1531 struct rtable
*rt
= ip_route_output(net
,
1532 imr
->imr_multiaddr
.s_addr
,
1540 imr
->imr_ifindex
= dev
->ifindex
;
1541 idev
= __in_dev_get_rtnl(dev
);
1547 * Join a socket to a group
1549 int sysctl_igmp_max_memberships __read_mostly
= IP_MAX_MEMBERSHIPS
;
1550 int sysctl_igmp_max_msf __read_mostly
= IP_MAX_MSF
;
1553 static int ip_mc_del1_src(struct ip_mc_list
*pmc
, int sfmode
,
1556 struct ip_sf_list
*psf
, *psf_prev
;
1560 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1561 if (psf
->sf_inaddr
== *psfsrc
)
1565 if (!psf
|| psf
->sf_count
[sfmode
] == 0) {
1566 /* source filter not found, or count wrong => bug */
1569 psf
->sf_count
[sfmode
]--;
1570 if (psf
->sf_count
[sfmode
] == 0) {
1571 ip_rt_multicast_event(pmc
->interface
);
1573 if (!psf
->sf_count
[MCAST_INCLUDE
] && !psf
->sf_count
[MCAST_EXCLUDE
]) {
1574 #ifdef CONFIG_IP_MULTICAST
1575 struct in_device
*in_dev
= pmc
->interface
;
1578 /* no more filters for this source */
1580 psf_prev
->sf_next
= psf
->sf_next
;
1582 pmc
->sources
= psf
->sf_next
;
1583 #ifdef CONFIG_IP_MULTICAST
1584 if (psf
->sf_oldin
&&
1585 !IGMP_V1_SEEN(in_dev
) && !IGMP_V2_SEEN(in_dev
)) {
1586 psf
->sf_crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1587 IGMP_Unsolicited_Report_Count
;
1588 psf
->sf_next
= pmc
->tomb
;
1598 #ifndef CONFIG_IP_MULTICAST
1599 #define igmp_ifc_event(x) do { } while (0)
1602 static int ip_mc_del_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1603 int sfcount
, __be32
*psfsrc
, int delta
)
1605 struct ip_mc_list
*pmc
;
1612 for_each_pmc_rcu(in_dev
, pmc
) {
1613 if (*pmca
== pmc
->multiaddr
)
1617 /* MCA not found?? bug */
1621 spin_lock_bh(&pmc
->lock
);
1623 #ifdef CONFIG_IP_MULTICAST
1628 if (!pmc
->sfcount
[sfmode
])
1630 pmc
->sfcount
[sfmode
]--;
1633 for (i
=0; i
<sfcount
; i
++) {
1634 int rv
= ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1636 changerec
|= rv
> 0;
1640 if (pmc
->sfmode
== MCAST_EXCLUDE
&&
1641 pmc
->sfcount
[MCAST_EXCLUDE
] == 0 &&
1642 pmc
->sfcount
[MCAST_INCLUDE
]) {
1643 #ifdef CONFIG_IP_MULTICAST
1644 struct ip_sf_list
*psf
;
1647 /* filter mode change */
1648 pmc
->sfmode
= MCAST_INCLUDE
;
1649 #ifdef CONFIG_IP_MULTICAST
1650 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1651 IGMP_Unsolicited_Report_Count
;
1652 in_dev
->mr_ifc_count
= pmc
->crcount
;
1653 for (psf
=pmc
->sources
; psf
; psf
= psf
->sf_next
)
1654 psf
->sf_crcount
= 0;
1655 igmp_ifc_event(pmc
->interface
);
1656 } else if (sf_setstate(pmc
) || changerec
) {
1657 igmp_ifc_event(pmc
->interface
);
1661 spin_unlock_bh(&pmc
->lock
);
1666 * Add multicast single-source filter to the interface list
1668 static int ip_mc_add1_src(struct ip_mc_list
*pmc
, int sfmode
,
1671 struct ip_sf_list
*psf
, *psf_prev
;
1674 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1675 if (psf
->sf_inaddr
== *psfsrc
)
1680 psf
= kzalloc(sizeof(*psf
), GFP_ATOMIC
);
1683 psf
->sf_inaddr
= *psfsrc
;
1685 psf_prev
->sf_next
= psf
;
1689 psf
->sf_count
[sfmode
]++;
1690 if (psf
->sf_count
[sfmode
] == 1) {
1691 ip_rt_multicast_event(pmc
->interface
);
1696 #ifdef CONFIG_IP_MULTICAST
1697 static void sf_markstate(struct ip_mc_list
*pmc
)
1699 struct ip_sf_list
*psf
;
1700 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1702 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
)
1703 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1704 psf
->sf_oldin
= mca_xcount
==
1705 psf
->sf_count
[MCAST_EXCLUDE
] &&
1706 !psf
->sf_count
[MCAST_INCLUDE
];
1708 psf
->sf_oldin
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1711 static int sf_setstate(struct ip_mc_list
*pmc
)
1713 struct ip_sf_list
*psf
, *dpsf
;
1714 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1715 int qrv
= pmc
->interface
->mr_qrv
;
1719 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1720 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1721 new_in
= mca_xcount
== psf
->sf_count
[MCAST_EXCLUDE
] &&
1722 !psf
->sf_count
[MCAST_INCLUDE
];
1724 new_in
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1726 if (!psf
->sf_oldin
) {
1727 struct ip_sf_list
*prev
= NULL
;
1729 for (dpsf
=pmc
->tomb
; dpsf
; dpsf
=dpsf
->sf_next
) {
1730 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1736 prev
->sf_next
= dpsf
->sf_next
;
1738 pmc
->tomb
= dpsf
->sf_next
;
1741 psf
->sf_crcount
= qrv
;
1744 } else if (psf
->sf_oldin
) {
1746 psf
->sf_crcount
= 0;
1748 * add or update "delete" records if an active filter
1751 for (dpsf
=pmc
->tomb
; dpsf
; dpsf
=dpsf
->sf_next
)
1752 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1755 dpsf
= kmalloc(sizeof(*dpsf
), GFP_ATOMIC
);
1759 /* pmc->lock held by callers */
1760 dpsf
->sf_next
= pmc
->tomb
;
1763 dpsf
->sf_crcount
= qrv
;
1772 * Add multicast source filter list to the interface list
1774 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1775 int sfcount
, __be32
*psfsrc
, int delta
)
1777 struct ip_mc_list
*pmc
;
1784 for_each_pmc_rcu(in_dev
, pmc
) {
1785 if (*pmca
== pmc
->multiaddr
)
1789 /* MCA not found?? bug */
1793 spin_lock_bh(&pmc
->lock
);
1796 #ifdef CONFIG_IP_MULTICAST
1799 isexclude
= pmc
->sfmode
== MCAST_EXCLUDE
;
1801 pmc
->sfcount
[sfmode
]++;
1803 for (i
=0; i
<sfcount
; i
++) {
1804 err
= ip_mc_add1_src(pmc
, sfmode
, &psfsrc
[i
]);
1812 pmc
->sfcount
[sfmode
]--;
1814 (void) ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[j
]);
1815 } else if (isexclude
!= (pmc
->sfcount
[MCAST_EXCLUDE
] != 0)) {
1816 #ifdef CONFIG_IP_MULTICAST
1817 struct ip_sf_list
*psf
;
1818 in_dev
= pmc
->interface
;
1821 /* filter mode change */
1822 if (pmc
->sfcount
[MCAST_EXCLUDE
])
1823 pmc
->sfmode
= MCAST_EXCLUDE
;
1824 else if (pmc
->sfcount
[MCAST_INCLUDE
])
1825 pmc
->sfmode
= MCAST_INCLUDE
;
1826 #ifdef CONFIG_IP_MULTICAST
1827 /* else no filters; keep old mode for reports */
1829 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1830 IGMP_Unsolicited_Report_Count
;
1831 in_dev
->mr_ifc_count
= pmc
->crcount
;
1832 for (psf
=pmc
->sources
; psf
; psf
= psf
->sf_next
)
1833 psf
->sf_crcount
= 0;
1834 igmp_ifc_event(in_dev
);
1835 } else if (sf_setstate(pmc
)) {
1836 igmp_ifc_event(in_dev
);
1839 spin_unlock_bh(&pmc
->lock
);
1843 static void ip_mc_clear_src(struct ip_mc_list
*pmc
)
1845 struct ip_sf_list
*psf
, *nextpsf
;
1847 for (psf
=pmc
->tomb
; psf
; psf
=nextpsf
) {
1848 nextpsf
= psf
->sf_next
;
1852 for (psf
=pmc
->sources
; psf
; psf
=nextpsf
) {
1853 nextpsf
= psf
->sf_next
;
1856 pmc
->sources
= NULL
;
1857 pmc
->sfmode
= MCAST_EXCLUDE
;
1858 pmc
->sfcount
[MCAST_INCLUDE
] = 0;
1859 pmc
->sfcount
[MCAST_EXCLUDE
] = 1;
1864 * Join a multicast group
1866 int ip_mc_join_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1869 __be32 addr
= imr
->imr_multiaddr
.s_addr
;
1870 struct ip_mc_socklist
*iml
= NULL
, *i
;
1871 struct in_device
*in_dev
;
1872 struct inet_sock
*inet
= inet_sk(sk
);
1873 struct net
*net
= sock_net(sk
);
1877 if (!ipv4_is_multicast(addr
))
1882 in_dev
= ip_mc_find_dev(net
, imr
);
1891 ifindex
= imr
->imr_ifindex
;
1892 for_each_pmc_rtnl(inet
, i
) {
1893 if (i
->multi
.imr_multiaddr
.s_addr
== addr
&&
1894 i
->multi
.imr_ifindex
== ifindex
)
1899 if (count
>= sysctl_igmp_max_memberships
)
1901 iml
= sock_kmalloc(sk
, sizeof(*iml
), GFP_KERNEL
);
1905 memcpy(&iml
->multi
, imr
, sizeof(*imr
));
1906 iml
->next_rcu
= inet
->mc_list
;
1908 iml
->sfmode
= MCAST_EXCLUDE
;
1909 rcu_assign_pointer(inet
->mc_list
, iml
);
1910 ip_mc_inc_group(in_dev
, addr
);
1916 EXPORT_SYMBOL(ip_mc_join_group
);
1918 static int ip_mc_leave_src(struct sock
*sk
, struct ip_mc_socklist
*iml
,
1919 struct in_device
*in_dev
)
1921 struct ip_sf_socklist
*psf
= rtnl_dereference(iml
->sflist
);
1925 /* any-source empty exclude case */
1926 return ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1927 iml
->sfmode
, 0, NULL
, 0);
1929 err
= ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1930 iml
->sfmode
, psf
->sl_count
, psf
->sl_addr
, 0);
1931 RCU_INIT_POINTER(iml
->sflist
, NULL
);
1932 /* decrease mem now to avoid the memleak warning */
1933 atomic_sub(IP_SFLSIZE(psf
->sl_max
), &sk
->sk_omem_alloc
);
1934 kfree_rcu(psf
, rcu
);
1939 * Ask a socket to leave a group.
1942 int ip_mc_leave_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1944 struct inet_sock
*inet
= inet_sk(sk
);
1945 struct ip_mc_socklist
*iml
;
1946 struct ip_mc_socklist __rcu
**imlp
;
1947 struct in_device
*in_dev
;
1948 struct net
*net
= sock_net(sk
);
1949 __be32 group
= imr
->imr_multiaddr
.s_addr
;
1951 int ret
= -EADDRNOTAVAIL
;
1954 in_dev
= ip_mc_find_dev(net
, imr
);
1959 ifindex
= imr
->imr_ifindex
;
1960 for (imlp
= &inet
->mc_list
;
1961 (iml
= rtnl_dereference(*imlp
)) != NULL
;
1962 imlp
= &iml
->next_rcu
) {
1963 if (iml
->multi
.imr_multiaddr
.s_addr
!= group
)
1966 if (iml
->multi
.imr_ifindex
!= ifindex
)
1968 } else if (imr
->imr_address
.s_addr
&& imr
->imr_address
.s_addr
!=
1969 iml
->multi
.imr_address
.s_addr
)
1972 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
1974 *imlp
= iml
->next_rcu
;
1976 ip_mc_dec_group(in_dev
, group
);
1978 /* decrease mem now to avoid the memleak warning */
1979 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
1980 kfree_rcu(iml
, rcu
);
1987 EXPORT_SYMBOL(ip_mc_leave_group
);
1989 int ip_mc_source(int add
, int omode
, struct sock
*sk
, struct
1990 ip_mreq_source
*mreqs
, int ifindex
)
1993 struct ip_mreqn imr
;
1994 __be32 addr
= mreqs
->imr_multiaddr
;
1995 struct ip_mc_socklist
*pmc
;
1996 struct in_device
*in_dev
= NULL
;
1997 struct inet_sock
*inet
= inet_sk(sk
);
1998 struct ip_sf_socklist
*psl
;
1999 struct net
*net
= sock_net(sk
);
2003 if (!ipv4_is_multicast(addr
))
2008 imr
.imr_multiaddr
.s_addr
= mreqs
->imr_multiaddr
;
2009 imr
.imr_address
.s_addr
= mreqs
->imr_interface
;
2010 imr
.imr_ifindex
= ifindex
;
2011 in_dev
= ip_mc_find_dev(net
, &imr
);
2017 err
= -EADDRNOTAVAIL
;
2019 for_each_pmc_rtnl(inet
, pmc
) {
2020 if ((pmc
->multi
.imr_multiaddr
.s_addr
==
2021 imr
.imr_multiaddr
.s_addr
) &&
2022 (pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
))
2025 if (!pmc
) { /* must have a prior join */
2029 /* if a source filter was set, must be the same mode as before */
2031 if (pmc
->sfmode
!= omode
) {
2035 } else if (pmc
->sfmode
!= omode
) {
2036 /* allow mode switches for empty-set filters */
2037 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 0, NULL
, 0);
2038 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, pmc
->sfmode
, 0,
2040 pmc
->sfmode
= omode
;
2043 psl
= rtnl_dereference(pmc
->sflist
);
2046 goto done
; /* err = -EADDRNOTAVAIL */
2048 for (i
=0; i
<psl
->sl_count
; i
++) {
2049 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2054 if (rv
) /* source not found */
2055 goto done
; /* err = -EADDRNOTAVAIL */
2057 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2058 if (psl
->sl_count
== 1 && omode
== MCAST_INCLUDE
) {
2063 /* update the interface filter */
2064 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2065 &mreqs
->imr_sourceaddr
, 1);
2067 for (j
=i
+1; j
<psl
->sl_count
; j
++)
2068 psl
->sl_addr
[j
-1] = psl
->sl_addr
[j
];
2073 /* else, add a new source to the filter */
2075 if (psl
&& psl
->sl_count
>= sysctl_igmp_max_msf
) {
2079 if (!psl
|| psl
->sl_count
== psl
->sl_max
) {
2080 struct ip_sf_socklist
*newpsl
;
2081 int count
= IP_SFBLOCK
;
2084 count
+= psl
->sl_max
;
2085 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(count
), GFP_KERNEL
);
2090 newpsl
->sl_max
= count
;
2091 newpsl
->sl_count
= count
- IP_SFBLOCK
;
2093 for (i
=0; i
<psl
->sl_count
; i
++)
2094 newpsl
->sl_addr
[i
] = psl
->sl_addr
[i
];
2095 /* decrease mem now to avoid the memleak warning */
2096 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2097 kfree_rcu(psl
, rcu
);
2099 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2102 rv
= 1; /* > 0 for insert logic below if sl_count is 0 */
2103 for (i
=0; i
<psl
->sl_count
; i
++) {
2104 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2109 if (rv
== 0) /* address already there is an error */
2111 for (j
=psl
->sl_count
-1; j
>=i
; j
--)
2112 psl
->sl_addr
[j
+1] = psl
->sl_addr
[j
];
2113 psl
->sl_addr
[i
] = mreqs
->imr_sourceaddr
;
2116 /* update the interface list */
2117 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2118 &mreqs
->imr_sourceaddr
, 1);
2122 return ip_mc_leave_group(sk
, &imr
);
2126 int ip_mc_msfilter(struct sock
*sk
, struct ip_msfilter
*msf
, int ifindex
)
2129 struct ip_mreqn imr
;
2130 __be32 addr
= msf
->imsf_multiaddr
;
2131 struct ip_mc_socklist
*pmc
;
2132 struct in_device
*in_dev
;
2133 struct inet_sock
*inet
= inet_sk(sk
);
2134 struct ip_sf_socklist
*newpsl
, *psl
;
2135 struct net
*net
= sock_net(sk
);
2138 if (!ipv4_is_multicast(addr
))
2140 if (msf
->imsf_fmode
!= MCAST_INCLUDE
&&
2141 msf
->imsf_fmode
!= MCAST_EXCLUDE
)
2146 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2147 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2148 imr
.imr_ifindex
= ifindex
;
2149 in_dev
= ip_mc_find_dev(net
, &imr
);
2156 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2157 if (msf
->imsf_fmode
== MCAST_INCLUDE
&& msf
->imsf_numsrc
== 0) {
2162 for_each_pmc_rtnl(inet
, pmc
) {
2163 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2164 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2167 if (!pmc
) { /* must have a prior join */
2171 if (msf
->imsf_numsrc
) {
2172 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(msf
->imsf_numsrc
),
2178 newpsl
->sl_max
= newpsl
->sl_count
= msf
->imsf_numsrc
;
2179 memcpy(newpsl
->sl_addr
, msf
->imsf_slist
,
2180 msf
->imsf_numsrc
* sizeof(msf
->imsf_slist
[0]));
2181 err
= ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2182 msf
->imsf_fmode
, newpsl
->sl_count
, newpsl
->sl_addr
, 0);
2184 sock_kfree_s(sk
, newpsl
, IP_SFLSIZE(newpsl
->sl_max
));
2189 (void) ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2190 msf
->imsf_fmode
, 0, NULL
, 0);
2192 psl
= rtnl_dereference(pmc
->sflist
);
2194 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2195 psl
->sl_count
, psl
->sl_addr
, 0);
2196 /* decrease mem now to avoid the memleak warning */
2197 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2198 kfree_rcu(psl
, rcu
);
2200 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2202 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2203 pmc
->sfmode
= msf
->imsf_fmode
;
2208 err
= ip_mc_leave_group(sk
, &imr
);
2212 int ip_mc_msfget(struct sock
*sk
, struct ip_msfilter
*msf
,
2213 struct ip_msfilter __user
*optval
, int __user
*optlen
)
2215 int err
, len
, count
, copycount
;
2216 struct ip_mreqn imr
;
2217 __be32 addr
= msf
->imsf_multiaddr
;
2218 struct ip_mc_socklist
*pmc
;
2219 struct in_device
*in_dev
;
2220 struct inet_sock
*inet
= inet_sk(sk
);
2221 struct ip_sf_socklist
*psl
;
2222 struct net
*net
= sock_net(sk
);
2224 if (!ipv4_is_multicast(addr
))
2229 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2230 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2231 imr
.imr_ifindex
= 0;
2232 in_dev
= ip_mc_find_dev(net
, &imr
);
2238 err
= -EADDRNOTAVAIL
;
2240 for_each_pmc_rtnl(inet
, pmc
) {
2241 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2242 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2245 if (!pmc
) /* must have a prior join */
2247 msf
->imsf_fmode
= pmc
->sfmode
;
2248 psl
= rtnl_dereference(pmc
->sflist
);
2254 count
= psl
->sl_count
;
2256 copycount
= count
< msf
->imsf_numsrc
? count
: msf
->imsf_numsrc
;
2257 len
= copycount
* sizeof(psl
->sl_addr
[0]);
2258 msf
->imsf_numsrc
= count
;
2259 if (put_user(IP_MSFILTER_SIZE(copycount
), optlen
) ||
2260 copy_to_user(optval
, msf
, IP_MSFILTER_SIZE(0))) {
2264 copy_to_user(&optval
->imsf_slist
[0], psl
->sl_addr
, len
))
2272 int ip_mc_gsfget(struct sock
*sk
, struct group_filter
*gsf
,
2273 struct group_filter __user
*optval
, int __user
*optlen
)
2275 int err
, i
, count
, copycount
;
2276 struct sockaddr_in
*psin
;
2278 struct ip_mc_socklist
*pmc
;
2279 struct inet_sock
*inet
= inet_sk(sk
);
2280 struct ip_sf_socklist
*psl
;
2282 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
2283 if (psin
->sin_family
!= AF_INET
)
2285 addr
= psin
->sin_addr
.s_addr
;
2286 if (!ipv4_is_multicast(addr
))
2291 err
= -EADDRNOTAVAIL
;
2293 for_each_pmc_rtnl(inet
, pmc
) {
2294 if (pmc
->multi
.imr_multiaddr
.s_addr
== addr
&&
2295 pmc
->multi
.imr_ifindex
== gsf
->gf_interface
)
2298 if (!pmc
) /* must have a prior join */
2300 gsf
->gf_fmode
= pmc
->sfmode
;
2301 psl
= rtnl_dereference(pmc
->sflist
);
2303 count
= psl
? psl
->sl_count
: 0;
2304 copycount
= count
< gsf
->gf_numsrc
? count
: gsf
->gf_numsrc
;
2305 gsf
->gf_numsrc
= count
;
2306 if (put_user(GROUP_FILTER_SIZE(copycount
), optlen
) ||
2307 copy_to_user(optval
, gsf
, GROUP_FILTER_SIZE(0))) {
2310 for (i
=0; i
<copycount
; i
++) {
2311 struct sockaddr_storage ss
;
2313 psin
= (struct sockaddr_in
*)&ss
;
2314 memset(&ss
, 0, sizeof(ss
));
2315 psin
->sin_family
= AF_INET
;
2316 psin
->sin_addr
.s_addr
= psl
->sl_addr
[i
];
2317 if (copy_to_user(&optval
->gf_slist
[i
], &ss
, sizeof(ss
)))
2327 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2329 int ip_mc_sf_allow(struct sock
*sk
, __be32 loc_addr
, __be32 rmt_addr
, int dif
)
2331 struct inet_sock
*inet
= inet_sk(sk
);
2332 struct ip_mc_socklist
*pmc
;
2333 struct ip_sf_socklist
*psl
;
2338 if (!ipv4_is_multicast(loc_addr
))
2342 for_each_pmc_rcu(inet
, pmc
) {
2343 if (pmc
->multi
.imr_multiaddr
.s_addr
== loc_addr
&&
2344 pmc
->multi
.imr_ifindex
== dif
)
2350 psl
= rcu_dereference(pmc
->sflist
);
2351 ret
= (pmc
->sfmode
== MCAST_EXCLUDE
);
2355 for (i
=0; i
<psl
->sl_count
; i
++) {
2356 if (psl
->sl_addr
[i
] == rmt_addr
)
2360 if (pmc
->sfmode
== MCAST_INCLUDE
&& i
>= psl
->sl_count
)
2362 if (pmc
->sfmode
== MCAST_EXCLUDE
&& i
< psl
->sl_count
)
2372 * A socket is closing.
2375 void ip_mc_drop_socket(struct sock
*sk
)
2377 struct inet_sock
*inet
= inet_sk(sk
);
2378 struct ip_mc_socklist
*iml
;
2379 struct net
*net
= sock_net(sk
);
2381 if (inet
->mc_list
== NULL
)
2385 while ((iml
= rtnl_dereference(inet
->mc_list
)) != NULL
) {
2386 struct in_device
*in_dev
;
2388 inet
->mc_list
= iml
->next_rcu
;
2389 in_dev
= inetdev_by_index(net
, iml
->multi
.imr_ifindex
);
2390 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2392 ip_mc_dec_group(in_dev
, iml
->multi
.imr_multiaddr
.s_addr
);
2393 /* decrease mem now to avoid the memleak warning */
2394 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
2395 kfree_rcu(iml
, rcu
);
2400 /* called with rcu_read_lock() */
2401 int ip_check_mc_rcu(struct in_device
*in_dev
, __be32 mc_addr
, __be32 src_addr
, u16 proto
)
2403 struct ip_mc_list
*im
;
2404 struct ip_mc_list __rcu
**mc_hash
;
2405 struct ip_sf_list
*psf
;
2408 mc_hash
= rcu_dereference(in_dev
->mc_hash
);
2410 u32 hash
= hash_32((__force u32
)mc_addr
, MC_HASH_SZ_LOG
);
2412 for (im
= rcu_dereference(mc_hash
[hash
]);
2414 im
= rcu_dereference(im
->next_hash
)) {
2415 if (im
->multiaddr
== mc_addr
)
2419 for_each_pmc_rcu(in_dev
, im
) {
2420 if (im
->multiaddr
== mc_addr
)
2424 if (im
&& proto
== IPPROTO_IGMP
) {
2428 for (psf
=im
->sources
; psf
; psf
=psf
->sf_next
) {
2429 if (psf
->sf_inaddr
== src_addr
)
2433 rv
= psf
->sf_count
[MCAST_INCLUDE
] ||
2434 psf
->sf_count
[MCAST_EXCLUDE
] !=
2435 im
->sfcount
[MCAST_EXCLUDE
];
2437 rv
= im
->sfcount
[MCAST_EXCLUDE
] != 0;
2439 rv
= 1; /* unspecified source; tentatively allow */
2444 #if defined(CONFIG_PROC_FS)
2445 struct igmp_mc_iter_state
{
2446 struct seq_net_private p
;
2447 struct net_device
*dev
;
2448 struct in_device
*in_dev
;
2451 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2453 static inline struct ip_mc_list
*igmp_mc_get_first(struct seq_file
*seq
)
2455 struct net
*net
= seq_file_net(seq
);
2456 struct ip_mc_list
*im
= NULL
;
2457 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2459 state
->in_dev
= NULL
;
2460 for_each_netdev_rcu(net
, state
->dev
) {
2461 struct in_device
*in_dev
;
2463 in_dev
= __in_dev_get_rcu(state
->dev
);
2466 im
= rcu_dereference(in_dev
->mc_list
);
2468 state
->in_dev
= in_dev
;
2475 static struct ip_mc_list
*igmp_mc_get_next(struct seq_file
*seq
, struct ip_mc_list
*im
)
2477 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2479 im
= rcu_dereference(im
->next_rcu
);
2481 state
->dev
= next_net_device_rcu(state
->dev
);
2483 state
->in_dev
= NULL
;
2486 state
->in_dev
= __in_dev_get_rcu(state
->dev
);
2489 im
= rcu_dereference(state
->in_dev
->mc_list
);
2494 static struct ip_mc_list
*igmp_mc_get_idx(struct seq_file
*seq
, loff_t pos
)
2496 struct ip_mc_list
*im
= igmp_mc_get_first(seq
);
2498 while (pos
&& (im
= igmp_mc_get_next(seq
, im
)) != NULL
)
2500 return pos
? NULL
: im
;
2503 static void *igmp_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2507 return *pos
? igmp_mc_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2510 static void *igmp_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2512 struct ip_mc_list
*im
;
2513 if (v
== SEQ_START_TOKEN
)
2514 im
= igmp_mc_get_first(seq
);
2516 im
= igmp_mc_get_next(seq
, v
);
2521 static void igmp_mc_seq_stop(struct seq_file
*seq
, void *v
)
2524 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2526 state
->in_dev
= NULL
;
2531 static int igmp_mc_seq_show(struct seq_file
*seq
, void *v
)
2533 if (v
== SEQ_START_TOKEN
)
2535 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2537 struct ip_mc_list
*im
= (struct ip_mc_list
*)v
;
2538 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2542 #ifdef CONFIG_IP_MULTICAST
2543 querier
= IGMP_V1_SEEN(state
->in_dev
) ? "V1" :
2544 IGMP_V2_SEEN(state
->in_dev
) ? "V2" :
2550 if (rcu_dereference(state
->in_dev
->mc_list
) == im
) {
2551 seq_printf(seq
, "%d\t%-10s: %5d %7s\n",
2552 state
->dev
->ifindex
, state
->dev
->name
, state
->in_dev
->mc_count
, querier
);
2555 delta
= im
->timer
.expires
- jiffies
;
2557 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2558 im
->multiaddr
, im
->users
,
2560 im
->tm_running
? jiffies_delta_to_clock_t(delta
) : 0,
2566 static const struct seq_operations igmp_mc_seq_ops
= {
2567 .start
= igmp_mc_seq_start
,
2568 .next
= igmp_mc_seq_next
,
2569 .stop
= igmp_mc_seq_stop
,
2570 .show
= igmp_mc_seq_show
,
2573 static int igmp_mc_seq_open(struct inode
*inode
, struct file
*file
)
2575 return seq_open_net(inode
, file
, &igmp_mc_seq_ops
,
2576 sizeof(struct igmp_mc_iter_state
));
2579 static const struct file_operations igmp_mc_seq_fops
= {
2580 .owner
= THIS_MODULE
,
2581 .open
= igmp_mc_seq_open
,
2583 .llseek
= seq_lseek
,
2584 .release
= seq_release_net
,
2587 struct igmp_mcf_iter_state
{
2588 struct seq_net_private p
;
2589 struct net_device
*dev
;
2590 struct in_device
*idev
;
2591 struct ip_mc_list
*im
;
2594 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2596 static inline struct ip_sf_list
*igmp_mcf_get_first(struct seq_file
*seq
)
2598 struct net
*net
= seq_file_net(seq
);
2599 struct ip_sf_list
*psf
= NULL
;
2600 struct ip_mc_list
*im
= NULL
;
2601 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2605 for_each_netdev_rcu(net
, state
->dev
) {
2606 struct in_device
*idev
;
2607 idev
= __in_dev_get_rcu(state
->dev
);
2608 if (unlikely(idev
== NULL
))
2610 im
= rcu_dereference(idev
->mc_list
);
2611 if (likely(im
!= NULL
)) {
2612 spin_lock_bh(&im
->lock
);
2614 if (likely(psf
!= NULL
)) {
2619 spin_unlock_bh(&im
->lock
);
2625 static struct ip_sf_list
*igmp_mcf_get_next(struct seq_file
*seq
, struct ip_sf_list
*psf
)
2627 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2631 spin_unlock_bh(&state
->im
->lock
);
2632 state
->im
= state
->im
->next
;
2633 while (!state
->im
) {
2634 state
->dev
= next_net_device_rcu(state
->dev
);
2639 state
->idev
= __in_dev_get_rcu(state
->dev
);
2642 state
->im
= rcu_dereference(state
->idev
->mc_list
);
2646 spin_lock_bh(&state
->im
->lock
);
2647 psf
= state
->im
->sources
;
2653 static struct ip_sf_list
*igmp_mcf_get_idx(struct seq_file
*seq
, loff_t pos
)
2655 struct ip_sf_list
*psf
= igmp_mcf_get_first(seq
);
2657 while (pos
&& (psf
= igmp_mcf_get_next(seq
, psf
)) != NULL
)
2659 return pos
? NULL
: psf
;
2662 static void *igmp_mcf_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2666 return *pos
? igmp_mcf_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2669 static void *igmp_mcf_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2671 struct ip_sf_list
*psf
;
2672 if (v
== SEQ_START_TOKEN
)
2673 psf
= igmp_mcf_get_first(seq
);
2675 psf
= igmp_mcf_get_next(seq
, v
);
2680 static void igmp_mcf_seq_stop(struct seq_file
*seq
, void *v
)
2683 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2684 if (likely(state
->im
!= NULL
)) {
2685 spin_unlock_bh(&state
->im
->lock
);
2693 static int igmp_mcf_seq_show(struct seq_file
*seq
, void *v
)
2695 struct ip_sf_list
*psf
= (struct ip_sf_list
*)v
;
2696 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2698 if (v
== SEQ_START_TOKEN
) {
2701 "%10s %10s %6s %6s\n", "Idx",
2703 "SRC", "INC", "EXC");
2707 "0x%08x %6lu %6lu\n",
2708 state
->dev
->ifindex
, state
->dev
->name
,
2709 ntohl(state
->im
->multiaddr
),
2710 ntohl(psf
->sf_inaddr
),
2711 psf
->sf_count
[MCAST_INCLUDE
],
2712 psf
->sf_count
[MCAST_EXCLUDE
]);
2717 static const struct seq_operations igmp_mcf_seq_ops
= {
2718 .start
= igmp_mcf_seq_start
,
2719 .next
= igmp_mcf_seq_next
,
2720 .stop
= igmp_mcf_seq_stop
,
2721 .show
= igmp_mcf_seq_show
,
2724 static int igmp_mcf_seq_open(struct inode
*inode
, struct file
*file
)
2726 return seq_open_net(inode
, file
, &igmp_mcf_seq_ops
,
2727 sizeof(struct igmp_mcf_iter_state
));
2730 static const struct file_operations igmp_mcf_seq_fops
= {
2731 .owner
= THIS_MODULE
,
2732 .open
= igmp_mcf_seq_open
,
2734 .llseek
= seq_lseek
,
2735 .release
= seq_release_net
,
2738 static int __net_init
igmp_net_init(struct net
*net
)
2740 struct proc_dir_entry
*pde
;
2742 pde
= proc_create("igmp", S_IRUGO
, net
->proc_net
, &igmp_mc_seq_fops
);
2745 pde
= proc_create("mcfilter", S_IRUGO
, net
->proc_net
,
2746 &igmp_mcf_seq_fops
);
2752 remove_proc_entry("igmp", net
->proc_net
);
2757 static void __net_exit
igmp_net_exit(struct net
*net
)
2759 remove_proc_entry("mcfilter", net
->proc_net
);
2760 remove_proc_entry("igmp", net
->proc_net
);
2763 static struct pernet_operations igmp_net_ops
= {
2764 .init
= igmp_net_init
,
2765 .exit
= igmp_net_exit
,
2768 static int igmp_netdev_event(struct notifier_block
*this,
2769 unsigned long event
, void *ptr
)
2771 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
2772 struct in_device
*in_dev
;
2775 case NETDEV_RESEND_IGMP
:
2776 in_dev
= __in_dev_get_rtnl(dev
);
2778 ip_mc_rejoin_groups(in_dev
);
2786 static struct notifier_block igmp_notifier
= {
2787 .notifier_call
= igmp_netdev_event
,
2790 int __init
igmp_mc_proc_init(void)
2794 err
= register_pernet_subsys(&igmp_net_ops
);
2797 err
= register_netdevice_notifier(&igmp_notifier
);
2799 goto reg_notif_fail
;
2803 unregister_pernet_subsys(&igmp_net_ops
);