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 <asm/system.h>
77 #include <linux/types.h>
78 #include <linux/kernel.h>
79 #include <linux/jiffies.h>
80 #include <linux/string.h>
81 #include <linux/socket.h>
82 #include <linux/sockios.h>
84 #include <linux/inet.h>
85 #include <linux/netdevice.h>
86 #include <linux/skbuff.h>
87 #include <linux/inetdevice.h>
88 #include <linux/igmp.h>
89 #include <linux/if_arp.h>
90 #include <linux/rtnetlink.h>
91 #include <linux/times.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_Unsolicited_Report_Interval (10*HZ)
118 #define IGMP_Query_Response_Interval (10*HZ)
119 #define IGMP_Unsolicited_Report_Count 2
122 #define IGMP_Initial_Report_Delay (1)
124 /* IGMP_Initial_Report_Delay is not from IGMP specs!
125 * IGMP specs require to report membership immediately after
126 * joining a group, but we delay the first report by a
127 * small interval. It seems more natural and still does not
128 * contradict to specs provided this delay is small enough.
131 #define IGMP_V1_SEEN(in_dev) \
132 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
133 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
134 ((in_dev)->mr_v1_seen && \
135 time_before(jiffies, (in_dev)->mr_v1_seen)))
136 #define IGMP_V2_SEEN(in_dev) \
137 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
138 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
139 ((in_dev)->mr_v2_seen && \
140 time_before(jiffies, (in_dev)->mr_v2_seen)))
142 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
);
143 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
);
144 static void igmpv3_clear_delrec(struct in_device
*in_dev
);
145 static int sf_setstate(struct ip_mc_list
*pmc
);
146 static void sf_markstate(struct ip_mc_list
*pmc
);
148 static void ip_mc_clear_src(struct ip_mc_list
*pmc
);
149 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
150 int sfcount
, __be32
*psfsrc
, int delta
);
152 static void ip_ma_put(struct ip_mc_list
*im
)
154 if (atomic_dec_and_test(&im
->refcnt
)) {
155 in_dev_put(im
->interface
);
160 #ifdef CONFIG_IP_MULTICAST
166 static __inline__
void igmp_stop_timer(struct ip_mc_list
*im
)
168 spin_lock_bh(&im
->lock
);
169 if (del_timer(&im
->timer
))
170 atomic_dec(&im
->refcnt
);
173 im
->unsolicit_count
= 0;
174 spin_unlock_bh(&im
->lock
);
177 /* It must be called with locked im->lock */
178 static void igmp_start_timer(struct ip_mc_list
*im
, int max_delay
)
180 int tv
= net_random() % max_delay
;
183 if (!mod_timer(&im
->timer
, jiffies
+tv
+2))
184 atomic_inc(&im
->refcnt
);
187 static void igmp_gq_start_timer(struct in_device
*in_dev
)
189 int tv
= net_random() % in_dev
->mr_maxdelay
;
191 in_dev
->mr_gq_running
= 1;
192 if (!mod_timer(&in_dev
->mr_gq_timer
, jiffies
+tv
+2))
196 static void igmp_ifc_start_timer(struct in_device
*in_dev
, int delay
)
198 int tv
= net_random() % delay
;
200 if (!mod_timer(&in_dev
->mr_ifc_timer
, jiffies
+tv
+2))
204 static void igmp_mod_timer(struct ip_mc_list
*im
, int max_delay
)
206 spin_lock_bh(&im
->lock
);
207 im
->unsolicit_count
= 0;
208 if (del_timer(&im
->timer
)) {
209 if ((long)(im
->timer
.expires
-jiffies
) < max_delay
) {
210 add_timer(&im
->timer
);
212 spin_unlock_bh(&im
->lock
);
215 atomic_dec(&im
->refcnt
);
217 igmp_start_timer(im
, max_delay
);
218 spin_unlock_bh(&im
->lock
);
223 * Send an IGMP report.
226 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
229 static int is_in(struct ip_mc_list
*pmc
, struct ip_sf_list
*psf
, int type
,
230 int gdeleted
, int sdeleted
)
233 case IGMPV3_MODE_IS_INCLUDE
:
234 case IGMPV3_MODE_IS_EXCLUDE
:
235 if (gdeleted
|| sdeleted
)
237 if (!(pmc
->gsquery
&& !psf
->sf_gsresp
)) {
238 if (pmc
->sfmode
== MCAST_INCLUDE
)
240 /* don't include if this source is excluded
243 if (psf
->sf_count
[MCAST_INCLUDE
])
244 return type
== IGMPV3_MODE_IS_INCLUDE
;
245 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
246 psf
->sf_count
[MCAST_EXCLUDE
];
249 case IGMPV3_CHANGE_TO_INCLUDE
:
250 if (gdeleted
|| sdeleted
)
252 return psf
->sf_count
[MCAST_INCLUDE
] != 0;
253 case IGMPV3_CHANGE_TO_EXCLUDE
:
254 if (gdeleted
|| sdeleted
)
256 if (pmc
->sfcount
[MCAST_EXCLUDE
] == 0 ||
257 psf
->sf_count
[MCAST_INCLUDE
])
259 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
260 psf
->sf_count
[MCAST_EXCLUDE
];
261 case IGMPV3_ALLOW_NEW_SOURCES
:
262 if (gdeleted
|| !psf
->sf_crcount
)
264 return (pmc
->sfmode
== MCAST_INCLUDE
) ^ sdeleted
;
265 case IGMPV3_BLOCK_OLD_SOURCES
:
266 if (pmc
->sfmode
== MCAST_INCLUDE
)
267 return gdeleted
|| (psf
->sf_crcount
&& sdeleted
);
268 return psf
->sf_crcount
&& !gdeleted
&& !sdeleted
;
274 igmp_scount(struct ip_mc_list
*pmc
, int type
, int gdeleted
, int sdeleted
)
276 struct ip_sf_list
*psf
;
279 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
280 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
))
287 static struct sk_buff
*igmpv3_newpack(struct net_device
*dev
, int size
)
292 struct igmpv3_report
*pig
;
293 struct net
*net
= dev_net(dev
);
295 skb
= alloc_skb(size
+ LL_ALLOCATED_SPACE(dev
), GFP_ATOMIC
);
300 struct flowi fl
= { .oif
= dev
->ifindex
,
302 .daddr
= IGMPV3_ALL_MCR
} },
303 .proto
= IPPROTO_IGMP
};
304 if (ip_route_output_key(net
, &rt
, &fl
)) {
309 if (rt
->rt_src
== 0) {
315 skb_dst_set(skb
, &rt
->dst
);
318 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
320 skb_reset_network_header(skb
);
322 skb_put(skb
, sizeof(struct iphdr
) + 4);
325 pip
->ihl
= (sizeof(struct iphdr
)+4)>>2;
327 pip
->frag_off
= htons(IP_DF
);
329 pip
->daddr
= rt
->rt_dst
;
330 pip
->saddr
= rt
->rt_src
;
331 pip
->protocol
= IPPROTO_IGMP
;
332 pip
->tot_len
= 0; /* filled in later */
333 ip_select_ident(pip
, &rt
->dst
, NULL
);
334 ((u8
*)&pip
[1])[0] = IPOPT_RA
;
335 ((u8
*)&pip
[1])[1] = 4;
336 ((u8
*)&pip
[1])[2] = 0;
337 ((u8
*)&pip
[1])[3] = 0;
339 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
) + 4;
340 skb_put(skb
, sizeof(*pig
));
341 pig
= igmpv3_report_hdr(skb
);
342 pig
->type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
350 static int igmpv3_sendpack(struct sk_buff
*skb
)
352 struct igmphdr
*pig
= igmp_hdr(skb
);
353 const int igmplen
= skb
->tail
- skb
->transport_header
;
355 pig
->csum
= ip_compute_csum(igmp_hdr(skb
), igmplen
);
357 return ip_local_out(skb
);
360 static int grec_size(struct ip_mc_list
*pmc
, int type
, int gdel
, int sdel
)
362 return sizeof(struct igmpv3_grec
) + 4*igmp_scount(pmc
, type
, gdel
, sdel
);
365 static struct sk_buff
*add_grhead(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
366 int type
, struct igmpv3_grec
**ppgr
)
368 struct net_device
*dev
= pmc
->interface
->dev
;
369 struct igmpv3_report
*pih
;
370 struct igmpv3_grec
*pgr
;
373 skb
= igmpv3_newpack(dev
, dev
->mtu
);
376 pgr
= (struct igmpv3_grec
*)skb_put(skb
, sizeof(struct igmpv3_grec
));
377 pgr
->grec_type
= type
;
378 pgr
->grec_auxwords
= 0;
380 pgr
->grec_mca
= pmc
->multiaddr
;
381 pih
= igmpv3_report_hdr(skb
);
382 pih
->ngrec
= htons(ntohs(pih
->ngrec
)+1);
387 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
388 skb_tailroom(skb)) : 0)
390 static struct sk_buff
*add_grec(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
391 int type
, int gdeleted
, int sdeleted
)
393 struct net_device
*dev
= pmc
->interface
->dev
;
394 struct igmpv3_report
*pih
;
395 struct igmpv3_grec
*pgr
= NULL
;
396 struct ip_sf_list
*psf
, *psf_next
, *psf_prev
, **psf_list
;
397 int scount
, stotal
, first
, isquery
, truncate
;
399 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
402 isquery
= type
== IGMPV3_MODE_IS_INCLUDE
||
403 type
== IGMPV3_MODE_IS_EXCLUDE
;
404 truncate
= type
== IGMPV3_MODE_IS_EXCLUDE
||
405 type
== IGMPV3_CHANGE_TO_EXCLUDE
;
409 psf_list
= sdeleted
? &pmc
->tomb
: &pmc
->sources
;
414 pih
= skb
? igmpv3_report_hdr(skb
) : NULL
;
416 /* EX and TO_EX get a fresh packet, if needed */
418 if (pih
&& pih
->ngrec
&&
419 AVAILABLE(skb
) < grec_size(pmc
, type
, gdeleted
, sdeleted
)) {
421 igmpv3_sendpack(skb
);
422 skb
= igmpv3_newpack(dev
, dev
->mtu
);
427 for (psf
=*psf_list
; psf
; psf
=psf_next
) {
430 psf_next
= psf
->sf_next
;
432 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
)) {
437 /* clear marks on query responses */
441 if (AVAILABLE(skb
) < sizeof(__be32
) +
442 first
*sizeof(struct igmpv3_grec
)) {
443 if (truncate
&& !first
)
444 break; /* truncate these */
446 pgr
->grec_nsrcs
= htons(scount
);
448 igmpv3_sendpack(skb
);
449 skb
= igmpv3_newpack(dev
, dev
->mtu
);
454 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
459 psrc
= (__be32
*)skb_put(skb
, sizeof(__be32
));
460 *psrc
= psf
->sf_inaddr
;
462 if ((type
== IGMPV3_ALLOW_NEW_SOURCES
||
463 type
== IGMPV3_BLOCK_OLD_SOURCES
) && psf
->sf_crcount
) {
465 if ((sdeleted
|| gdeleted
) && psf
->sf_crcount
== 0) {
467 psf_prev
->sf_next
= psf
->sf_next
;
469 *psf_list
= psf
->sf_next
;
479 if (type
== IGMPV3_ALLOW_NEW_SOURCES
||
480 type
== IGMPV3_BLOCK_OLD_SOURCES
)
482 if (pmc
->crcount
|| isquery
) {
483 /* make sure we have room for group header */
484 if (skb
&& AVAILABLE(skb
)<sizeof(struct igmpv3_grec
)) {
485 igmpv3_sendpack(skb
);
486 skb
= NULL
; /* add_grhead will get a new one */
488 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
492 pgr
->grec_nsrcs
= htons(scount
);
495 pmc
->gsquery
= 0; /* clear query state on report */
499 static int igmpv3_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
)
501 struct sk_buff
*skb
= NULL
;
505 read_lock(&in_dev
->mc_list_lock
);
506 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
507 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
509 spin_lock_bh(&pmc
->lock
);
510 if (pmc
->sfcount
[MCAST_EXCLUDE
])
511 type
= IGMPV3_MODE_IS_EXCLUDE
;
513 type
= IGMPV3_MODE_IS_INCLUDE
;
514 skb
= add_grec(skb
, pmc
, type
, 0, 0);
515 spin_unlock_bh(&pmc
->lock
);
517 read_unlock(&in_dev
->mc_list_lock
);
519 spin_lock_bh(&pmc
->lock
);
520 if (pmc
->sfcount
[MCAST_EXCLUDE
])
521 type
= IGMPV3_MODE_IS_EXCLUDE
;
523 type
= IGMPV3_MODE_IS_INCLUDE
;
524 skb
= add_grec(skb
, pmc
, type
, 0, 0);
525 spin_unlock_bh(&pmc
->lock
);
529 return igmpv3_sendpack(skb
);
533 * remove zero-count source records from a source filter list
535 static void igmpv3_clear_zeros(struct ip_sf_list
**ppsf
)
537 struct ip_sf_list
*psf_prev
, *psf_next
, *psf
;
540 for (psf
=*ppsf
; psf
; psf
= psf_next
) {
541 psf_next
= psf
->sf_next
;
542 if (psf
->sf_crcount
== 0) {
544 psf_prev
->sf_next
= psf
->sf_next
;
546 *ppsf
= psf
->sf_next
;
553 static void igmpv3_send_cr(struct in_device
*in_dev
)
555 struct ip_mc_list
*pmc
, *pmc_prev
, *pmc_next
;
556 struct sk_buff
*skb
= NULL
;
559 read_lock(&in_dev
->mc_list_lock
);
560 spin_lock_bh(&in_dev
->mc_tomb_lock
);
564 for (pmc
=in_dev
->mc_tomb
; pmc
; pmc
=pmc_next
) {
565 pmc_next
= pmc
->next
;
566 if (pmc
->sfmode
== MCAST_INCLUDE
) {
567 type
= IGMPV3_BLOCK_OLD_SOURCES
;
568 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
569 skb
= add_grec(skb
, pmc
, type
, 1, 0);
570 skb
= add_grec(skb
, pmc
, dtype
, 1, 1);
573 if (pmc
->sfmode
== MCAST_EXCLUDE
) {
574 type
= IGMPV3_CHANGE_TO_INCLUDE
;
575 skb
= add_grec(skb
, pmc
, type
, 1, 0);
578 if (pmc
->crcount
== 0) {
579 igmpv3_clear_zeros(&pmc
->tomb
);
580 igmpv3_clear_zeros(&pmc
->sources
);
583 if (pmc
->crcount
== 0 && !pmc
->tomb
&& !pmc
->sources
) {
585 pmc_prev
->next
= pmc_next
;
587 in_dev
->mc_tomb
= pmc_next
;
588 in_dev_put(pmc
->interface
);
593 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
596 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
597 spin_lock_bh(&pmc
->lock
);
598 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
599 type
= IGMPV3_BLOCK_OLD_SOURCES
;
600 dtype
= IGMPV3_ALLOW_NEW_SOURCES
;
602 type
= IGMPV3_ALLOW_NEW_SOURCES
;
603 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
605 skb
= add_grec(skb
, pmc
, type
, 0, 0);
606 skb
= add_grec(skb
, pmc
, dtype
, 0, 1); /* deleted sources */
608 /* filter mode changes */
610 if (pmc
->sfmode
== MCAST_EXCLUDE
)
611 type
= IGMPV3_CHANGE_TO_EXCLUDE
;
613 type
= IGMPV3_CHANGE_TO_INCLUDE
;
614 skb
= add_grec(skb
, pmc
, type
, 0, 0);
617 spin_unlock_bh(&pmc
->lock
);
619 read_unlock(&in_dev
->mc_list_lock
);
623 (void) igmpv3_sendpack(skb
);
626 static int igmp_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
,
633 struct net_device
*dev
= in_dev
->dev
;
634 struct net
*net
= dev_net(dev
);
635 __be32 group
= pmc
? pmc
->multiaddr
: 0;
638 if (type
== IGMPV3_HOST_MEMBERSHIP_REPORT
)
639 return igmpv3_send_report(in_dev
, pmc
);
640 else if (type
== IGMP_HOST_LEAVE_MESSAGE
)
641 dst
= IGMP_ALL_ROUTER
;
646 struct flowi fl
= { .oif
= dev
->ifindex
,
647 .nl_u
= { .ip4_u
= { .daddr
= dst
} },
648 .proto
= IPPROTO_IGMP
};
649 if (ip_route_output_key(net
, &rt
, &fl
))
652 if (rt
->rt_src
== 0) {
657 skb
= alloc_skb(IGMP_SIZE
+LL_ALLOCATED_SPACE(dev
), GFP_ATOMIC
);
663 skb_dst_set(skb
, &rt
->dst
);
665 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
667 skb_reset_network_header(skb
);
669 skb_put(skb
, sizeof(struct iphdr
) + 4);
672 iph
->ihl
= (sizeof(struct iphdr
)+4)>>2;
674 iph
->frag_off
= htons(IP_DF
);
677 iph
->saddr
= rt
->rt_src
;
678 iph
->protocol
= IPPROTO_IGMP
;
679 ip_select_ident(iph
, &rt
->dst
, NULL
);
680 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
681 ((u8
*)&iph
[1])[1] = 4;
682 ((u8
*)&iph
[1])[2] = 0;
683 ((u8
*)&iph
[1])[3] = 0;
685 ih
= (struct igmphdr
*)skb_put(skb
, sizeof(struct igmphdr
));
690 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
692 return ip_local_out(skb
);
695 static void igmp_gq_timer_expire(unsigned long data
)
697 struct in_device
*in_dev
= (struct in_device
*)data
;
699 in_dev
->mr_gq_running
= 0;
700 igmpv3_send_report(in_dev
, NULL
);
701 __in_dev_put(in_dev
);
704 static void igmp_ifc_timer_expire(unsigned long data
)
706 struct in_device
*in_dev
= (struct in_device
*)data
;
708 igmpv3_send_cr(in_dev
);
709 if (in_dev
->mr_ifc_count
) {
710 in_dev
->mr_ifc_count
--;
711 igmp_ifc_start_timer(in_dev
, IGMP_Unsolicited_Report_Interval
);
713 __in_dev_put(in_dev
);
716 static void igmp_ifc_event(struct in_device
*in_dev
)
718 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
720 in_dev
->mr_ifc_count
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
721 IGMP_Unsolicited_Report_Count
;
722 igmp_ifc_start_timer(in_dev
, 1);
726 static void igmp_timer_expire(unsigned long data
)
728 struct ip_mc_list
*im
=(struct ip_mc_list
*)data
;
729 struct in_device
*in_dev
= im
->interface
;
731 spin_lock(&im
->lock
);
734 if (im
->unsolicit_count
) {
735 im
->unsolicit_count
--;
736 igmp_start_timer(im
, IGMP_Unsolicited_Report_Interval
);
739 spin_unlock(&im
->lock
);
741 if (IGMP_V1_SEEN(in_dev
))
742 igmp_send_report(in_dev
, im
, IGMP_HOST_MEMBERSHIP_REPORT
);
743 else if (IGMP_V2_SEEN(in_dev
))
744 igmp_send_report(in_dev
, im
, IGMPV2_HOST_MEMBERSHIP_REPORT
);
746 igmp_send_report(in_dev
, im
, IGMPV3_HOST_MEMBERSHIP_REPORT
);
751 /* mark EXCLUDE-mode sources */
752 static int igmp_xmarksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
754 struct ip_sf_list
*psf
;
758 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
761 for (i
=0; i
<nsrcs
; i
++) {
762 /* skip inactive filters */
763 if (pmc
->sfcount
[MCAST_INCLUDE
] ||
764 pmc
->sfcount
[MCAST_EXCLUDE
] !=
765 psf
->sf_count
[MCAST_EXCLUDE
])
767 if (srcs
[i
] == psf
->sf_inaddr
) {
774 if (scount
== nsrcs
) /* all sources excluded */
779 static int igmp_marksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
781 struct ip_sf_list
*psf
;
784 if (pmc
->sfmode
== MCAST_EXCLUDE
)
785 return igmp_xmarksources(pmc
, nsrcs
, srcs
);
787 /* mark INCLUDE-mode sources */
789 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
792 for (i
=0; i
<nsrcs
; i
++)
793 if (srcs
[i
] == psf
->sf_inaddr
) {
807 static void igmp_heard_report(struct in_device
*in_dev
, __be32 group
)
809 struct ip_mc_list
*im
;
811 /* Timers are only set for non-local groups */
813 if (group
== IGMP_ALL_HOSTS
)
816 read_lock(&in_dev
->mc_list_lock
);
817 for (im
=in_dev
->mc_list
; im
!=NULL
; im
=im
->next
) {
818 if (im
->multiaddr
== group
) {
823 read_unlock(&in_dev
->mc_list_lock
);
826 static void igmp_heard_query(struct in_device
*in_dev
, struct sk_buff
*skb
,
829 struct igmphdr
*ih
= igmp_hdr(skb
);
830 struct igmpv3_query
*ih3
= igmpv3_query_hdr(skb
);
831 struct ip_mc_list
*im
;
832 __be32 group
= ih
->group
;
839 /* Alas, old v1 router presents here. */
841 max_delay
= IGMP_Query_Response_Interval
;
842 in_dev
->mr_v1_seen
= jiffies
+
843 IGMP_V1_Router_Present_Timeout
;
846 /* v2 router present */
847 max_delay
= ih
->code
*(HZ
/IGMP_TIMER_SCALE
);
848 in_dev
->mr_v2_seen
= jiffies
+
849 IGMP_V2_Router_Present_Timeout
;
851 /* cancel the interface change timer */
852 in_dev
->mr_ifc_count
= 0;
853 if (del_timer(&in_dev
->mr_ifc_timer
))
854 __in_dev_put(in_dev
);
855 /* clear deleted report items */
856 igmpv3_clear_delrec(in_dev
);
857 } else if (len
< 12) {
858 return; /* ignore bogus packet; freed by caller */
859 } else if (IGMP_V1_SEEN(in_dev
)) {
860 /* This is a v3 query with v1 queriers present */
861 max_delay
= IGMP_Query_Response_Interval
;
863 } else if (IGMP_V2_SEEN(in_dev
)) {
864 /* this is a v3 query with v2 queriers present;
865 * Interpretation of the max_delay code is problematic here.
866 * A real v2 host would use ih_code directly, while v3 has a
867 * different encoding. We use the v3 encoding as more likely
868 * to be intended in a v3 query.
870 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
872 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)))
875 ih3
= igmpv3_query_hdr(skb
);
877 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)
878 + ntohs(ih3
->nsrcs
)*sizeof(__be32
)))
880 ih3
= igmpv3_query_hdr(skb
);
883 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
885 max_delay
= 1; /* can't mod w/ 0 */
886 in_dev
->mr_maxdelay
= max_delay
;
888 in_dev
->mr_qrv
= ih3
->qrv
;
889 if (!group
) { /* general query */
891 return; /* no sources allowed */
892 igmp_gq_start_timer(in_dev
);
895 /* mark sources to include, if group & source-specific */
896 mark
= ih3
->nsrcs
!= 0;
900 * - Start the timers in all of our membership records
901 * that the query applies to for the interface on
902 * which the query arrived excl. those that belong
903 * to a "local" group (224.0.0.X)
904 * - For timers already running check if they need to
906 * - Use the igmp->igmp_code field as the maximum
909 read_lock(&in_dev
->mc_list_lock
);
910 for (im
=in_dev
->mc_list
; im
!=NULL
; im
=im
->next
) {
913 if (group
&& group
!= im
->multiaddr
)
915 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
917 spin_lock_bh(&im
->lock
);
919 im
->gsquery
= im
->gsquery
&& mark
;
922 changed
= !im
->gsquery
||
923 igmp_marksources(im
, ntohs(ih3
->nsrcs
), ih3
->srcs
);
924 spin_unlock_bh(&im
->lock
);
926 igmp_mod_timer(im
, max_delay
);
928 read_unlock(&in_dev
->mc_list_lock
);
931 /* called in rcu_read_lock() section */
932 int igmp_rcv(struct sk_buff
*skb
)
934 /* This basically follows the spec line by line -- see RFC1112 */
936 struct in_device
*in_dev
= __in_dev_get_rcu(skb
->dev
);
942 if (!pskb_may_pull(skb
, sizeof(struct igmphdr
)))
945 switch (skb
->ip_summed
) {
946 case CHECKSUM_COMPLETE
:
947 if (!csum_fold(skb
->csum
))
952 if (__skb_checksum_complete(skb
))
958 case IGMP_HOST_MEMBERSHIP_QUERY
:
959 igmp_heard_query(in_dev
, skb
, len
);
961 case IGMP_HOST_MEMBERSHIP_REPORT
:
962 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
963 /* Is it our report looped back? */
964 if (skb_rtable(skb
)->fl
.iif
== 0)
966 /* don't rely on MC router hearing unicast reports */
967 if (skb
->pkt_type
== PACKET_MULTICAST
||
968 skb
->pkt_type
== PACKET_BROADCAST
)
969 igmp_heard_report(in_dev
, ih
->group
);
972 #ifdef CONFIG_IP_PIMSM_V1
973 return pim_rcv_v1(skb
);
975 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
978 case IGMP_HOST_LEAVE_MESSAGE
:
980 case IGMP_MTRACE_RESP
:
995 * Add a filter to a device
998 static void ip_mc_filter_add(struct in_device
*in_dev
, __be32 addr
)
1000 char buf
[MAX_ADDR_LEN
];
1001 struct net_device
*dev
= in_dev
->dev
;
1003 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1004 We will get multicast token leakage, when IFF_MULTICAST
1005 is changed. This check should be done in dev->set_multicast_list
1006 routine. Something sort of:
1007 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1010 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1011 dev_mc_add(dev
, buf
);
1015 * Remove a filter from a device
1018 static void ip_mc_filter_del(struct in_device
*in_dev
, __be32 addr
)
1020 char buf
[MAX_ADDR_LEN
];
1021 struct net_device
*dev
= in_dev
->dev
;
1023 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1024 dev_mc_del(dev
, buf
);
1027 #ifdef CONFIG_IP_MULTICAST
1029 * deleted ip_mc_list manipulation
1031 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
)
1033 struct ip_mc_list
*pmc
;
1035 /* this is an "ip_mc_list" for convenience; only the fields below
1036 * are actually used. In particular, the refcnt and users are not
1037 * used for management of the delete list. Using the same structure
1038 * for deleted items allows change reports to use common code with
1039 * non-deleted or query-response MCA's.
1041 pmc
= kzalloc(sizeof(*pmc
), GFP_KERNEL
);
1044 spin_lock_bh(&im
->lock
);
1045 pmc
->interface
= im
->interface
;
1046 in_dev_hold(in_dev
);
1047 pmc
->multiaddr
= im
->multiaddr
;
1048 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1049 IGMP_Unsolicited_Report_Count
;
1050 pmc
->sfmode
= im
->sfmode
;
1051 if (pmc
->sfmode
== MCAST_INCLUDE
) {
1052 struct ip_sf_list
*psf
;
1054 pmc
->tomb
= im
->tomb
;
1055 pmc
->sources
= im
->sources
;
1056 im
->tomb
= im
->sources
= NULL
;
1057 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
)
1058 psf
->sf_crcount
= pmc
->crcount
;
1060 spin_unlock_bh(&im
->lock
);
1062 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1063 pmc
->next
= in_dev
->mc_tomb
;
1064 in_dev
->mc_tomb
= pmc
;
1065 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1068 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
)
1070 struct ip_mc_list
*pmc
, *pmc_prev
;
1071 struct ip_sf_list
*psf
, *psf_next
;
1073 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1075 for (pmc
=in_dev
->mc_tomb
; pmc
; pmc
=pmc
->next
) {
1076 if (pmc
->multiaddr
== multiaddr
)
1082 pmc_prev
->next
= pmc
->next
;
1084 in_dev
->mc_tomb
= pmc
->next
;
1086 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1088 for (psf
=pmc
->tomb
; psf
; psf
=psf_next
) {
1089 psf_next
= psf
->sf_next
;
1092 in_dev_put(pmc
->interface
);
1097 static void igmpv3_clear_delrec(struct in_device
*in_dev
)
1099 struct ip_mc_list
*pmc
, *nextpmc
;
1101 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1102 pmc
= in_dev
->mc_tomb
;
1103 in_dev
->mc_tomb
= NULL
;
1104 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1106 for (; pmc
; pmc
= nextpmc
) {
1107 nextpmc
= pmc
->next
;
1108 ip_mc_clear_src(pmc
);
1109 in_dev_put(pmc
->interface
);
1112 /* clear dead sources, too */
1113 read_lock(&in_dev
->mc_list_lock
);
1114 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1115 struct ip_sf_list
*psf
, *psf_next
;
1117 spin_lock_bh(&pmc
->lock
);
1120 spin_unlock_bh(&pmc
->lock
);
1121 for (; psf
; psf
=psf_next
) {
1122 psf_next
= psf
->sf_next
;
1126 read_unlock(&in_dev
->mc_list_lock
);
1130 static void igmp_group_dropped(struct ip_mc_list
*im
)
1132 struct in_device
*in_dev
= im
->interface
;
1133 #ifdef CONFIG_IP_MULTICAST
1139 ip_mc_filter_del(in_dev
, im
->multiaddr
);
1142 #ifdef CONFIG_IP_MULTICAST
1143 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1146 reporter
= im
->reporter
;
1147 igmp_stop_timer(im
);
1149 if (!in_dev
->dead
) {
1150 if (IGMP_V1_SEEN(in_dev
))
1152 if (IGMP_V2_SEEN(in_dev
)) {
1154 igmp_send_report(in_dev
, im
, IGMP_HOST_LEAVE_MESSAGE
);
1158 igmpv3_add_delrec(in_dev
, im
);
1160 igmp_ifc_event(in_dev
);
1164 ip_mc_clear_src(im
);
1167 static void igmp_group_added(struct ip_mc_list
*im
)
1169 struct in_device
*in_dev
= im
->interface
;
1171 if (im
->loaded
== 0) {
1173 ip_mc_filter_add(in_dev
, im
->multiaddr
);
1176 #ifdef CONFIG_IP_MULTICAST
1177 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1182 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
)) {
1183 spin_lock_bh(&im
->lock
);
1184 igmp_start_timer(im
, IGMP_Initial_Report_Delay
);
1185 spin_unlock_bh(&im
->lock
);
1190 im
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1191 IGMP_Unsolicited_Report_Count
;
1192 igmp_ifc_event(in_dev
);
1198 * Multicast list managers
1203 * A socket has joined a multicast group on device dev.
1206 void ip_mc_inc_group(struct in_device
*in_dev
, __be32 addr
)
1208 struct ip_mc_list
*im
;
1212 for (im
=in_dev
->mc_list
; im
; im
=im
->next
) {
1213 if (im
->multiaddr
== addr
) {
1215 ip_mc_add_src(in_dev
, &addr
, MCAST_EXCLUDE
, 0, NULL
, 0);
1220 im
= kmalloc(sizeof(*im
), GFP_KERNEL
);
1225 im
->interface
= in_dev
;
1226 in_dev_hold(in_dev
);
1227 im
->multiaddr
= addr
;
1228 /* initial mode is (EX, empty) */
1229 im
->sfmode
= MCAST_EXCLUDE
;
1230 im
->sfcount
[MCAST_INCLUDE
] = 0;
1231 im
->sfcount
[MCAST_EXCLUDE
] = 1;
1235 atomic_set(&im
->refcnt
, 1);
1236 spin_lock_init(&im
->lock
);
1237 #ifdef CONFIG_IP_MULTICAST
1239 setup_timer(&im
->timer
, &igmp_timer_expire
, (unsigned long)im
);
1240 im
->unsolicit_count
= IGMP_Unsolicited_Report_Count
;
1245 write_lock_bh(&in_dev
->mc_list_lock
);
1246 im
->next
= in_dev
->mc_list
;
1247 in_dev
->mc_list
= im
;
1249 write_unlock_bh(&in_dev
->mc_list_lock
);
1250 #ifdef CONFIG_IP_MULTICAST
1251 igmpv3_del_delrec(in_dev
, im
->multiaddr
);
1253 igmp_group_added(im
);
1255 ip_rt_multicast_event(in_dev
);
1259 EXPORT_SYMBOL(ip_mc_inc_group
);
1262 * Resend IGMP JOIN report; used for bonding.
1264 void ip_mc_rejoin_group(struct ip_mc_list
*im
)
1266 #ifdef CONFIG_IP_MULTICAST
1267 struct in_device
*in_dev
= im
->interface
;
1269 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1272 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
)) {
1273 igmp_mod_timer(im
, IGMP_Initial_Report_Delay
);
1277 im
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1278 IGMP_Unsolicited_Report_Count
;
1279 igmp_ifc_event(in_dev
);
1282 EXPORT_SYMBOL(ip_mc_rejoin_group
);
1285 * A socket has left a multicast group on device dev
1288 void ip_mc_dec_group(struct in_device
*in_dev
, __be32 addr
)
1290 struct ip_mc_list
*i
, **ip
;
1294 for (ip
=&in_dev
->mc_list
; (i
=*ip
)!=NULL
; ip
=&i
->next
) {
1295 if (i
->multiaddr
== addr
) {
1296 if (--i
->users
== 0) {
1297 write_lock_bh(&in_dev
->mc_list_lock
);
1300 write_unlock_bh(&in_dev
->mc_list_lock
);
1301 igmp_group_dropped(i
);
1304 ip_rt_multicast_event(in_dev
);
1313 EXPORT_SYMBOL(ip_mc_dec_group
);
1315 /* Device changing type */
1317 void ip_mc_unmap(struct in_device
*in_dev
)
1319 struct ip_mc_list
*i
;
1323 for (i
= in_dev
->mc_list
; i
; i
= i
->next
)
1324 igmp_group_dropped(i
);
1327 void ip_mc_remap(struct in_device
*in_dev
)
1329 struct ip_mc_list
*i
;
1333 for (i
= in_dev
->mc_list
; i
; i
= i
->next
)
1334 igmp_group_added(i
);
1337 /* Device going down */
1339 void ip_mc_down(struct in_device
*in_dev
)
1341 struct ip_mc_list
*i
;
1345 for (i
=in_dev
->mc_list
; i
; i
=i
->next
)
1346 igmp_group_dropped(i
);
1348 #ifdef CONFIG_IP_MULTICAST
1349 in_dev
->mr_ifc_count
= 0;
1350 if (del_timer(&in_dev
->mr_ifc_timer
))
1351 __in_dev_put(in_dev
);
1352 in_dev
->mr_gq_running
= 0;
1353 if (del_timer(&in_dev
->mr_gq_timer
))
1354 __in_dev_put(in_dev
);
1355 igmpv3_clear_delrec(in_dev
);
1358 ip_mc_dec_group(in_dev
, IGMP_ALL_HOSTS
);
1361 void ip_mc_init_dev(struct in_device
*in_dev
)
1365 in_dev
->mc_tomb
= NULL
;
1366 #ifdef CONFIG_IP_MULTICAST
1367 in_dev
->mr_gq_running
= 0;
1368 setup_timer(&in_dev
->mr_gq_timer
, igmp_gq_timer_expire
,
1369 (unsigned long)in_dev
);
1370 in_dev
->mr_ifc_count
= 0;
1371 in_dev
->mc_count
= 0;
1372 setup_timer(&in_dev
->mr_ifc_timer
, igmp_ifc_timer_expire
,
1373 (unsigned long)in_dev
);
1374 in_dev
->mr_qrv
= IGMP_Unsolicited_Report_Count
;
1377 rwlock_init(&in_dev
->mc_list_lock
);
1378 spin_lock_init(&in_dev
->mc_tomb_lock
);
1381 /* Device going up */
1383 void ip_mc_up(struct in_device
*in_dev
)
1385 struct ip_mc_list
*i
;
1389 ip_mc_inc_group(in_dev
, IGMP_ALL_HOSTS
);
1391 for (i
=in_dev
->mc_list
; i
; i
=i
->next
)
1392 igmp_group_added(i
);
1396 * Device is about to be destroyed: clean up.
1399 void ip_mc_destroy_dev(struct in_device
*in_dev
)
1401 struct ip_mc_list
*i
;
1405 /* Deactivate timers */
1408 write_lock_bh(&in_dev
->mc_list_lock
);
1409 while ((i
= in_dev
->mc_list
) != NULL
) {
1410 in_dev
->mc_list
= i
->next
;
1412 write_unlock_bh(&in_dev
->mc_list_lock
);
1413 igmp_group_dropped(i
);
1416 write_lock_bh(&in_dev
->mc_list_lock
);
1418 write_unlock_bh(&in_dev
->mc_list_lock
);
1421 static struct in_device
*ip_mc_find_dev(struct net
*net
, struct ip_mreqn
*imr
)
1423 struct flowi fl
= { .nl_u
= { .ip4_u
=
1424 { .daddr
= imr
->imr_multiaddr
.s_addr
} } };
1426 struct net_device
*dev
= NULL
;
1427 struct in_device
*idev
= NULL
;
1429 if (imr
->imr_ifindex
) {
1430 idev
= inetdev_by_index(net
, imr
->imr_ifindex
);
1435 if (imr
->imr_address
.s_addr
) {
1436 dev
= ip_dev_find(net
, imr
->imr_address
.s_addr
);
1442 if (!dev
&& !ip_route_output_key(net
, &rt
, &fl
)) {
1447 imr
->imr_ifindex
= dev
->ifindex
;
1448 idev
= __in_dev_get_rtnl(dev
);
1454 * Join a socket to a group
1456 int sysctl_igmp_max_memberships __read_mostly
= IP_MAX_MEMBERSHIPS
;
1457 int sysctl_igmp_max_msf __read_mostly
= IP_MAX_MSF
;
1460 static int ip_mc_del1_src(struct ip_mc_list
*pmc
, int sfmode
,
1463 struct ip_sf_list
*psf
, *psf_prev
;
1467 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1468 if (psf
->sf_inaddr
== *psfsrc
)
1472 if (!psf
|| psf
->sf_count
[sfmode
] == 0) {
1473 /* source filter not found, or count wrong => bug */
1476 psf
->sf_count
[sfmode
]--;
1477 if (psf
->sf_count
[sfmode
] == 0) {
1478 ip_rt_multicast_event(pmc
->interface
);
1480 if (!psf
->sf_count
[MCAST_INCLUDE
] && !psf
->sf_count
[MCAST_EXCLUDE
]) {
1481 #ifdef CONFIG_IP_MULTICAST
1482 struct in_device
*in_dev
= pmc
->interface
;
1485 /* no more filters for this source */
1487 psf_prev
->sf_next
= psf
->sf_next
;
1489 pmc
->sources
= psf
->sf_next
;
1490 #ifdef CONFIG_IP_MULTICAST
1491 if (psf
->sf_oldin
&&
1492 !IGMP_V1_SEEN(in_dev
) && !IGMP_V2_SEEN(in_dev
)) {
1493 psf
->sf_crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1494 IGMP_Unsolicited_Report_Count
;
1495 psf
->sf_next
= pmc
->tomb
;
1505 #ifndef CONFIG_IP_MULTICAST
1506 #define igmp_ifc_event(x) do { } while (0)
1509 static int ip_mc_del_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1510 int sfcount
, __be32
*psfsrc
, int delta
)
1512 struct ip_mc_list
*pmc
;
1518 read_lock(&in_dev
->mc_list_lock
);
1519 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1520 if (*pmca
== pmc
->multiaddr
)
1524 /* MCA not found?? bug */
1525 read_unlock(&in_dev
->mc_list_lock
);
1528 spin_lock_bh(&pmc
->lock
);
1529 read_unlock(&in_dev
->mc_list_lock
);
1530 #ifdef CONFIG_IP_MULTICAST
1535 if (!pmc
->sfcount
[sfmode
])
1537 pmc
->sfcount
[sfmode
]--;
1540 for (i
=0; i
<sfcount
; i
++) {
1541 int rv
= ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1543 changerec
|= rv
> 0;
1547 if (pmc
->sfmode
== MCAST_EXCLUDE
&&
1548 pmc
->sfcount
[MCAST_EXCLUDE
] == 0 &&
1549 pmc
->sfcount
[MCAST_INCLUDE
]) {
1550 #ifdef CONFIG_IP_MULTICAST
1551 struct ip_sf_list
*psf
;
1554 /* filter mode change */
1555 pmc
->sfmode
= MCAST_INCLUDE
;
1556 #ifdef CONFIG_IP_MULTICAST
1557 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1558 IGMP_Unsolicited_Report_Count
;
1559 in_dev
->mr_ifc_count
= pmc
->crcount
;
1560 for (psf
=pmc
->sources
; psf
; psf
= psf
->sf_next
)
1561 psf
->sf_crcount
= 0;
1562 igmp_ifc_event(pmc
->interface
);
1563 } else if (sf_setstate(pmc
) || changerec
) {
1564 igmp_ifc_event(pmc
->interface
);
1568 spin_unlock_bh(&pmc
->lock
);
1573 * Add multicast single-source filter to the interface list
1575 static int ip_mc_add1_src(struct ip_mc_list
*pmc
, int sfmode
,
1576 __be32
*psfsrc
, int delta
)
1578 struct ip_sf_list
*psf
, *psf_prev
;
1581 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1582 if (psf
->sf_inaddr
== *psfsrc
)
1587 psf
= kzalloc(sizeof(*psf
), GFP_ATOMIC
);
1590 psf
->sf_inaddr
= *psfsrc
;
1592 psf_prev
->sf_next
= psf
;
1596 psf
->sf_count
[sfmode
]++;
1597 if (psf
->sf_count
[sfmode
] == 1) {
1598 ip_rt_multicast_event(pmc
->interface
);
1603 #ifdef CONFIG_IP_MULTICAST
1604 static void sf_markstate(struct ip_mc_list
*pmc
)
1606 struct ip_sf_list
*psf
;
1607 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1609 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
)
1610 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1611 psf
->sf_oldin
= mca_xcount
==
1612 psf
->sf_count
[MCAST_EXCLUDE
] &&
1613 !psf
->sf_count
[MCAST_INCLUDE
];
1615 psf
->sf_oldin
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1618 static int sf_setstate(struct ip_mc_list
*pmc
)
1620 struct ip_sf_list
*psf
, *dpsf
;
1621 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1622 int qrv
= pmc
->interface
->mr_qrv
;
1626 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1627 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1628 new_in
= mca_xcount
== psf
->sf_count
[MCAST_EXCLUDE
] &&
1629 !psf
->sf_count
[MCAST_INCLUDE
];
1631 new_in
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1633 if (!psf
->sf_oldin
) {
1634 struct ip_sf_list
*prev
= NULL
;
1636 for (dpsf
=pmc
->tomb
; dpsf
; dpsf
=dpsf
->sf_next
) {
1637 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1643 prev
->sf_next
= dpsf
->sf_next
;
1645 pmc
->tomb
= dpsf
->sf_next
;
1648 psf
->sf_crcount
= qrv
;
1651 } else if (psf
->sf_oldin
) {
1653 psf
->sf_crcount
= 0;
1655 * add or update "delete" records if an active filter
1658 for (dpsf
=pmc
->tomb
; dpsf
; dpsf
=dpsf
->sf_next
)
1659 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1662 dpsf
= kmalloc(sizeof(*dpsf
), GFP_ATOMIC
);
1666 /* pmc->lock held by callers */
1667 dpsf
->sf_next
= pmc
->tomb
;
1670 dpsf
->sf_crcount
= qrv
;
1679 * Add multicast source filter list to the interface list
1681 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1682 int sfcount
, __be32
*psfsrc
, int delta
)
1684 struct ip_mc_list
*pmc
;
1690 read_lock(&in_dev
->mc_list_lock
);
1691 for (pmc
=in_dev
->mc_list
; pmc
; pmc
=pmc
->next
) {
1692 if (*pmca
== pmc
->multiaddr
)
1696 /* MCA not found?? bug */
1697 read_unlock(&in_dev
->mc_list_lock
);
1700 spin_lock_bh(&pmc
->lock
);
1701 read_unlock(&in_dev
->mc_list_lock
);
1703 #ifdef CONFIG_IP_MULTICAST
1706 isexclude
= pmc
->sfmode
== MCAST_EXCLUDE
;
1708 pmc
->sfcount
[sfmode
]++;
1710 for (i
=0; i
<sfcount
; i
++) {
1711 err
= ip_mc_add1_src(pmc
, sfmode
, &psfsrc
[i
], delta
);
1718 pmc
->sfcount
[sfmode
]--;
1720 (void) ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1721 } else if (isexclude
!= (pmc
->sfcount
[MCAST_EXCLUDE
] != 0)) {
1722 #ifdef CONFIG_IP_MULTICAST
1723 struct ip_sf_list
*psf
;
1724 in_dev
= pmc
->interface
;
1727 /* filter mode change */
1728 if (pmc
->sfcount
[MCAST_EXCLUDE
])
1729 pmc
->sfmode
= MCAST_EXCLUDE
;
1730 else if (pmc
->sfcount
[MCAST_INCLUDE
])
1731 pmc
->sfmode
= MCAST_INCLUDE
;
1732 #ifdef CONFIG_IP_MULTICAST
1733 /* else no filters; keep old mode for reports */
1735 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1736 IGMP_Unsolicited_Report_Count
;
1737 in_dev
->mr_ifc_count
= pmc
->crcount
;
1738 for (psf
=pmc
->sources
; psf
; psf
= psf
->sf_next
)
1739 psf
->sf_crcount
= 0;
1740 igmp_ifc_event(in_dev
);
1741 } else if (sf_setstate(pmc
)) {
1742 igmp_ifc_event(in_dev
);
1745 spin_unlock_bh(&pmc
->lock
);
1749 static void ip_mc_clear_src(struct ip_mc_list
*pmc
)
1751 struct ip_sf_list
*psf
, *nextpsf
;
1753 for (psf
=pmc
->tomb
; psf
; psf
=nextpsf
) {
1754 nextpsf
= psf
->sf_next
;
1758 for (psf
=pmc
->sources
; psf
; psf
=nextpsf
) {
1759 nextpsf
= psf
->sf_next
;
1762 pmc
->sources
= NULL
;
1763 pmc
->sfmode
= MCAST_EXCLUDE
;
1764 pmc
->sfcount
[MCAST_INCLUDE
] = 0;
1765 pmc
->sfcount
[MCAST_EXCLUDE
] = 1;
1770 * Join a multicast group
1772 int ip_mc_join_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1775 __be32 addr
= imr
->imr_multiaddr
.s_addr
;
1776 struct ip_mc_socklist
*iml
= NULL
, *i
;
1777 struct in_device
*in_dev
;
1778 struct inet_sock
*inet
= inet_sk(sk
);
1779 struct net
*net
= sock_net(sk
);
1783 if (!ipv4_is_multicast(addr
))
1788 in_dev
= ip_mc_find_dev(net
, imr
);
1797 ifindex
= imr
->imr_ifindex
;
1798 for (i
= inet
->mc_list
; i
; i
= i
->next
) {
1799 if (i
->multi
.imr_multiaddr
.s_addr
== addr
&&
1800 i
->multi
.imr_ifindex
== ifindex
)
1805 if (count
>= sysctl_igmp_max_memberships
)
1807 iml
= sock_kmalloc(sk
, sizeof(*iml
), GFP_KERNEL
);
1811 memcpy(&iml
->multi
, imr
, sizeof(*imr
));
1812 iml
->next
= inet
->mc_list
;
1814 iml
->sfmode
= MCAST_EXCLUDE
;
1815 rcu_assign_pointer(inet
->mc_list
, iml
);
1816 ip_mc_inc_group(in_dev
, addr
);
1822 EXPORT_SYMBOL(ip_mc_join_group
);
1824 static void ip_sf_socklist_reclaim(struct rcu_head
*rp
)
1826 struct ip_sf_socklist
*psf
;
1828 psf
= container_of(rp
, struct ip_sf_socklist
, rcu
);
1829 /* sk_omem_alloc should have been decreased by the caller*/
1833 static int ip_mc_leave_src(struct sock
*sk
, struct ip_mc_socklist
*iml
,
1834 struct in_device
*in_dev
)
1836 struct ip_sf_socklist
*psf
= iml
->sflist
;
1840 /* any-source empty exclude case */
1841 return ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1842 iml
->sfmode
, 0, NULL
, 0);
1844 err
= ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1845 iml
->sfmode
, psf
->sl_count
, psf
->sl_addr
, 0);
1846 rcu_assign_pointer(iml
->sflist
, NULL
);
1847 /* decrease mem now to avoid the memleak warning */
1848 atomic_sub(IP_SFLSIZE(psf
->sl_max
), &sk
->sk_omem_alloc
);
1849 call_rcu(&psf
->rcu
, ip_sf_socklist_reclaim
);
1854 static void ip_mc_socklist_reclaim(struct rcu_head
*rp
)
1856 struct ip_mc_socklist
*iml
;
1858 iml
= container_of(rp
, struct ip_mc_socklist
, rcu
);
1859 /* sk_omem_alloc should have been decreased by the caller*/
1865 * Ask a socket to leave a group.
1868 int ip_mc_leave_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1870 struct inet_sock
*inet
= inet_sk(sk
);
1871 struct ip_mc_socklist
*iml
, **imlp
;
1872 struct in_device
*in_dev
;
1873 struct net
*net
= sock_net(sk
);
1874 __be32 group
= imr
->imr_multiaddr
.s_addr
;
1876 int ret
= -EADDRNOTAVAIL
;
1879 in_dev
= ip_mc_find_dev(net
, imr
);
1880 ifindex
= imr
->imr_ifindex
;
1881 for (imlp
= &inet
->mc_list
; (iml
= *imlp
) != NULL
; imlp
= &iml
->next
) {
1882 if (iml
->multi
.imr_multiaddr
.s_addr
!= group
)
1885 if (iml
->multi
.imr_ifindex
!= ifindex
)
1887 } else if (imr
->imr_address
.s_addr
&& imr
->imr_address
.s_addr
!=
1888 iml
->multi
.imr_address
.s_addr
)
1891 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
1893 rcu_assign_pointer(*imlp
, iml
->next
);
1896 ip_mc_dec_group(in_dev
, group
);
1898 /* decrease mem now to avoid the memleak warning */
1899 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
1900 call_rcu(&iml
->rcu
, ip_mc_socklist_reclaim
);
1909 int ip_mc_source(int add
, int omode
, struct sock
*sk
, struct
1910 ip_mreq_source
*mreqs
, int ifindex
)
1913 struct ip_mreqn imr
;
1914 __be32 addr
= mreqs
->imr_multiaddr
;
1915 struct ip_mc_socklist
*pmc
;
1916 struct in_device
*in_dev
= NULL
;
1917 struct inet_sock
*inet
= inet_sk(sk
);
1918 struct ip_sf_socklist
*psl
;
1919 struct net
*net
= sock_net(sk
);
1923 if (!ipv4_is_multicast(addr
))
1928 imr
.imr_multiaddr
.s_addr
= mreqs
->imr_multiaddr
;
1929 imr
.imr_address
.s_addr
= mreqs
->imr_interface
;
1930 imr
.imr_ifindex
= ifindex
;
1931 in_dev
= ip_mc_find_dev(net
, &imr
);
1937 err
= -EADDRNOTAVAIL
;
1939 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
1940 if ((pmc
->multi
.imr_multiaddr
.s_addr
==
1941 imr
.imr_multiaddr
.s_addr
) &&
1942 (pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
))
1945 if (!pmc
) { /* must have a prior join */
1949 /* if a source filter was set, must be the same mode as before */
1951 if (pmc
->sfmode
!= omode
) {
1955 } else if (pmc
->sfmode
!= omode
) {
1956 /* allow mode switches for empty-set filters */
1957 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 0, NULL
, 0);
1958 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, pmc
->sfmode
, 0,
1960 pmc
->sfmode
= omode
;
1966 goto done
; /* err = -EADDRNOTAVAIL */
1968 for (i
=0; i
<psl
->sl_count
; i
++) {
1969 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
1974 if (rv
) /* source not found */
1975 goto done
; /* err = -EADDRNOTAVAIL */
1977 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1978 if (psl
->sl_count
== 1 && omode
== MCAST_INCLUDE
) {
1983 /* update the interface filter */
1984 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
1985 &mreqs
->imr_sourceaddr
, 1);
1987 for (j
=i
+1; j
<psl
->sl_count
; j
++)
1988 psl
->sl_addr
[j
-1] = psl
->sl_addr
[j
];
1993 /* else, add a new source to the filter */
1995 if (psl
&& psl
->sl_count
>= sysctl_igmp_max_msf
) {
1999 if (!psl
|| psl
->sl_count
== psl
->sl_max
) {
2000 struct ip_sf_socklist
*newpsl
;
2001 int count
= IP_SFBLOCK
;
2004 count
+= psl
->sl_max
;
2005 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(count
), GFP_KERNEL
);
2010 newpsl
->sl_max
= count
;
2011 newpsl
->sl_count
= count
- IP_SFBLOCK
;
2013 for (i
=0; i
<psl
->sl_count
; i
++)
2014 newpsl
->sl_addr
[i
] = psl
->sl_addr
[i
];
2015 /* decrease mem now to avoid the memleak warning */
2016 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2017 call_rcu(&psl
->rcu
, ip_sf_socklist_reclaim
);
2019 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2022 rv
= 1; /* > 0 for insert logic below if sl_count is 0 */
2023 for (i
=0; i
<psl
->sl_count
; i
++) {
2024 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2029 if (rv
== 0) /* address already there is an error */
2031 for (j
=psl
->sl_count
-1; j
>=i
; j
--)
2032 psl
->sl_addr
[j
+1] = psl
->sl_addr
[j
];
2033 psl
->sl_addr
[i
] = mreqs
->imr_sourceaddr
;
2036 /* update the interface list */
2037 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2038 &mreqs
->imr_sourceaddr
, 1);
2042 return ip_mc_leave_group(sk
, &imr
);
2046 int ip_mc_msfilter(struct sock
*sk
, struct ip_msfilter
*msf
, int ifindex
)
2049 struct ip_mreqn imr
;
2050 __be32 addr
= msf
->imsf_multiaddr
;
2051 struct ip_mc_socklist
*pmc
;
2052 struct in_device
*in_dev
;
2053 struct inet_sock
*inet
= inet_sk(sk
);
2054 struct ip_sf_socklist
*newpsl
, *psl
;
2055 struct net
*net
= sock_net(sk
);
2058 if (!ipv4_is_multicast(addr
))
2060 if (msf
->imsf_fmode
!= MCAST_INCLUDE
&&
2061 msf
->imsf_fmode
!= MCAST_EXCLUDE
)
2066 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2067 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2068 imr
.imr_ifindex
= ifindex
;
2069 in_dev
= ip_mc_find_dev(net
, &imr
);
2076 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2077 if (msf
->imsf_fmode
== MCAST_INCLUDE
&& msf
->imsf_numsrc
== 0) {
2082 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
2083 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2084 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2087 if (!pmc
) { /* must have a prior join */
2091 if (msf
->imsf_numsrc
) {
2092 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(msf
->imsf_numsrc
),
2098 newpsl
->sl_max
= newpsl
->sl_count
= msf
->imsf_numsrc
;
2099 memcpy(newpsl
->sl_addr
, msf
->imsf_slist
,
2100 msf
->imsf_numsrc
* sizeof(msf
->imsf_slist
[0]));
2101 err
= ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2102 msf
->imsf_fmode
, newpsl
->sl_count
, newpsl
->sl_addr
, 0);
2104 sock_kfree_s(sk
, newpsl
, IP_SFLSIZE(newpsl
->sl_max
));
2109 (void) ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2110 msf
->imsf_fmode
, 0, NULL
, 0);
2114 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2115 psl
->sl_count
, psl
->sl_addr
, 0);
2116 /* decrease mem now to avoid the memleak warning */
2117 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2118 call_rcu(&psl
->rcu
, ip_sf_socklist_reclaim
);
2120 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2122 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2123 pmc
->sfmode
= msf
->imsf_fmode
;
2128 err
= ip_mc_leave_group(sk
, &imr
);
2132 int ip_mc_msfget(struct sock
*sk
, struct ip_msfilter
*msf
,
2133 struct ip_msfilter __user
*optval
, int __user
*optlen
)
2135 int err
, len
, count
, copycount
;
2136 struct ip_mreqn imr
;
2137 __be32 addr
= msf
->imsf_multiaddr
;
2138 struct ip_mc_socklist
*pmc
;
2139 struct in_device
*in_dev
;
2140 struct inet_sock
*inet
= inet_sk(sk
);
2141 struct ip_sf_socklist
*psl
;
2142 struct net
*net
= sock_net(sk
);
2144 if (!ipv4_is_multicast(addr
))
2149 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2150 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2151 imr
.imr_ifindex
= 0;
2152 in_dev
= ip_mc_find_dev(net
, &imr
);
2158 err
= -EADDRNOTAVAIL
;
2160 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
2161 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2162 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2165 if (!pmc
) /* must have a prior join */
2167 msf
->imsf_fmode
= pmc
->sfmode
;
2174 count
= psl
->sl_count
;
2176 copycount
= count
< msf
->imsf_numsrc
? count
: msf
->imsf_numsrc
;
2177 len
= copycount
* sizeof(psl
->sl_addr
[0]);
2178 msf
->imsf_numsrc
= count
;
2179 if (put_user(IP_MSFILTER_SIZE(copycount
), optlen
) ||
2180 copy_to_user(optval
, msf
, IP_MSFILTER_SIZE(0))) {
2184 copy_to_user(&optval
->imsf_slist
[0], psl
->sl_addr
, len
))
2192 int ip_mc_gsfget(struct sock
*sk
, struct group_filter
*gsf
,
2193 struct group_filter __user
*optval
, int __user
*optlen
)
2195 int err
, i
, count
, copycount
;
2196 struct sockaddr_in
*psin
;
2198 struct ip_mc_socklist
*pmc
;
2199 struct inet_sock
*inet
= inet_sk(sk
);
2200 struct ip_sf_socklist
*psl
;
2202 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
2203 if (psin
->sin_family
!= AF_INET
)
2205 addr
= psin
->sin_addr
.s_addr
;
2206 if (!ipv4_is_multicast(addr
))
2211 err
= -EADDRNOTAVAIL
;
2213 for (pmc
=inet
->mc_list
; pmc
; pmc
=pmc
->next
) {
2214 if (pmc
->multi
.imr_multiaddr
.s_addr
== addr
&&
2215 pmc
->multi
.imr_ifindex
== gsf
->gf_interface
)
2218 if (!pmc
) /* must have a prior join */
2220 gsf
->gf_fmode
= pmc
->sfmode
;
2223 count
= psl
? psl
->sl_count
: 0;
2224 copycount
= count
< gsf
->gf_numsrc
? count
: gsf
->gf_numsrc
;
2225 gsf
->gf_numsrc
= count
;
2226 if (put_user(GROUP_FILTER_SIZE(copycount
), optlen
) ||
2227 copy_to_user(optval
, gsf
, GROUP_FILTER_SIZE(0))) {
2230 for (i
=0; i
<copycount
; i
++) {
2231 struct sockaddr_storage ss
;
2233 psin
= (struct sockaddr_in
*)&ss
;
2234 memset(&ss
, 0, sizeof(ss
));
2235 psin
->sin_family
= AF_INET
;
2236 psin
->sin_addr
.s_addr
= psl
->sl_addr
[i
];
2237 if (copy_to_user(&optval
->gf_slist
[i
], &ss
, sizeof(ss
)))
2247 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2249 int ip_mc_sf_allow(struct sock
*sk
, __be32 loc_addr
, __be32 rmt_addr
, int dif
)
2251 struct inet_sock
*inet
= inet_sk(sk
);
2252 struct ip_mc_socklist
*pmc
;
2253 struct ip_sf_socklist
*psl
;
2258 if (!ipv4_is_multicast(loc_addr
))
2262 for (pmc
=rcu_dereference(inet
->mc_list
); pmc
; pmc
=rcu_dereference(pmc
->next
)) {
2263 if (pmc
->multi
.imr_multiaddr
.s_addr
== loc_addr
&&
2264 pmc
->multi
.imr_ifindex
== dif
)
2271 ret
= (pmc
->sfmode
== MCAST_EXCLUDE
);
2275 for (i
=0; i
<psl
->sl_count
; i
++) {
2276 if (psl
->sl_addr
[i
] == rmt_addr
)
2280 if (pmc
->sfmode
== MCAST_INCLUDE
&& i
>= psl
->sl_count
)
2282 if (pmc
->sfmode
== MCAST_EXCLUDE
&& i
< psl
->sl_count
)
2292 * A socket is closing.
2295 void ip_mc_drop_socket(struct sock
*sk
)
2297 struct inet_sock
*inet
= inet_sk(sk
);
2298 struct ip_mc_socklist
*iml
;
2299 struct net
*net
= sock_net(sk
);
2301 if (inet
->mc_list
== NULL
)
2305 while ((iml
= inet
->mc_list
) != NULL
) {
2306 struct in_device
*in_dev
;
2307 rcu_assign_pointer(inet
->mc_list
, iml
->next
);
2309 in_dev
= inetdev_by_index(net
, iml
->multi
.imr_ifindex
);
2310 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2311 if (in_dev
!= NULL
) {
2312 ip_mc_dec_group(in_dev
, iml
->multi
.imr_multiaddr
.s_addr
);
2315 /* decrease mem now to avoid the memleak warning */
2316 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
2317 call_rcu(&iml
->rcu
, ip_mc_socklist_reclaim
);
2322 int ip_check_mc(struct in_device
*in_dev
, __be32 mc_addr
, __be32 src_addr
, u16 proto
)
2324 struct ip_mc_list
*im
;
2325 struct ip_sf_list
*psf
;
2328 read_lock(&in_dev
->mc_list_lock
);
2329 for (im
=in_dev
->mc_list
; im
; im
=im
->next
) {
2330 if (im
->multiaddr
== mc_addr
)
2333 if (im
&& proto
== IPPROTO_IGMP
) {
2337 for (psf
=im
->sources
; psf
; psf
=psf
->sf_next
) {
2338 if (psf
->sf_inaddr
== src_addr
)
2342 rv
= psf
->sf_count
[MCAST_INCLUDE
] ||
2343 psf
->sf_count
[MCAST_EXCLUDE
] !=
2344 im
->sfcount
[MCAST_EXCLUDE
];
2346 rv
= im
->sfcount
[MCAST_EXCLUDE
] != 0;
2348 rv
= 1; /* unspecified source; tentatively allow */
2350 read_unlock(&in_dev
->mc_list_lock
);
2354 #if defined(CONFIG_PROC_FS)
2355 struct igmp_mc_iter_state
{
2356 struct seq_net_private p
;
2357 struct net_device
*dev
;
2358 struct in_device
*in_dev
;
2361 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2363 static inline struct ip_mc_list
*igmp_mc_get_first(struct seq_file
*seq
)
2365 struct net
*net
= seq_file_net(seq
);
2366 struct ip_mc_list
*im
= NULL
;
2367 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2369 state
->in_dev
= NULL
;
2370 for_each_netdev_rcu(net
, state
->dev
) {
2371 struct in_device
*in_dev
;
2373 in_dev
= __in_dev_get_rcu(state
->dev
);
2376 read_lock(&in_dev
->mc_list_lock
);
2377 im
= in_dev
->mc_list
;
2379 state
->in_dev
= in_dev
;
2382 read_unlock(&in_dev
->mc_list_lock
);
2387 static struct ip_mc_list
*igmp_mc_get_next(struct seq_file
*seq
, struct ip_mc_list
*im
)
2389 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2392 if (likely(state
->in_dev
!= NULL
))
2393 read_unlock(&state
->in_dev
->mc_list_lock
);
2395 state
->dev
= next_net_device_rcu(state
->dev
);
2397 state
->in_dev
= NULL
;
2400 state
->in_dev
= __in_dev_get_rcu(state
->dev
);
2403 read_lock(&state
->in_dev
->mc_list_lock
);
2404 im
= state
->in_dev
->mc_list
;
2409 static struct ip_mc_list
*igmp_mc_get_idx(struct seq_file
*seq
, loff_t pos
)
2411 struct ip_mc_list
*im
= igmp_mc_get_first(seq
);
2413 while (pos
&& (im
= igmp_mc_get_next(seq
, im
)) != NULL
)
2415 return pos
? NULL
: im
;
2418 static void *igmp_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2422 return *pos
? igmp_mc_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2425 static void *igmp_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2427 struct ip_mc_list
*im
;
2428 if (v
== SEQ_START_TOKEN
)
2429 im
= igmp_mc_get_first(seq
);
2431 im
= igmp_mc_get_next(seq
, v
);
2436 static void igmp_mc_seq_stop(struct seq_file
*seq
, void *v
)
2439 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2440 if (likely(state
->in_dev
!= NULL
)) {
2441 read_unlock(&state
->in_dev
->mc_list_lock
);
2442 state
->in_dev
= NULL
;
2448 static int igmp_mc_seq_show(struct seq_file
*seq
, void *v
)
2450 if (v
== SEQ_START_TOKEN
)
2452 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2454 struct ip_mc_list
*im
= (struct ip_mc_list
*)v
;
2455 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2457 #ifdef CONFIG_IP_MULTICAST
2458 querier
= IGMP_V1_SEEN(state
->in_dev
) ? "V1" :
2459 IGMP_V2_SEEN(state
->in_dev
) ? "V2" :
2465 if (state
->in_dev
->mc_list
== im
) {
2466 seq_printf(seq
, "%d\t%-10s: %5d %7s\n",
2467 state
->dev
->ifindex
, state
->dev
->name
, state
->in_dev
->mc_count
, querier
);
2471 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2472 im
->multiaddr
, im
->users
,
2473 im
->tm_running
, im
->tm_running
?
2474 jiffies_to_clock_t(im
->timer
.expires
-jiffies
) : 0,
2480 static const struct seq_operations igmp_mc_seq_ops
= {
2481 .start
= igmp_mc_seq_start
,
2482 .next
= igmp_mc_seq_next
,
2483 .stop
= igmp_mc_seq_stop
,
2484 .show
= igmp_mc_seq_show
,
2487 static int igmp_mc_seq_open(struct inode
*inode
, struct file
*file
)
2489 return seq_open_net(inode
, file
, &igmp_mc_seq_ops
,
2490 sizeof(struct igmp_mc_iter_state
));
2493 static const struct file_operations igmp_mc_seq_fops
= {
2494 .owner
= THIS_MODULE
,
2495 .open
= igmp_mc_seq_open
,
2497 .llseek
= seq_lseek
,
2498 .release
= seq_release_net
,
2501 struct igmp_mcf_iter_state
{
2502 struct seq_net_private p
;
2503 struct net_device
*dev
;
2504 struct in_device
*idev
;
2505 struct ip_mc_list
*im
;
2508 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2510 static inline struct ip_sf_list
*igmp_mcf_get_first(struct seq_file
*seq
)
2512 struct net
*net
= seq_file_net(seq
);
2513 struct ip_sf_list
*psf
= NULL
;
2514 struct ip_mc_list
*im
= NULL
;
2515 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2519 for_each_netdev_rcu(net
, state
->dev
) {
2520 struct in_device
*idev
;
2521 idev
= __in_dev_get_rcu(state
->dev
);
2522 if (unlikely(idev
== NULL
))
2524 read_lock(&idev
->mc_list_lock
);
2526 if (likely(im
!= NULL
)) {
2527 spin_lock_bh(&im
->lock
);
2529 if (likely(psf
!= NULL
)) {
2534 spin_unlock_bh(&im
->lock
);
2536 read_unlock(&idev
->mc_list_lock
);
2541 static struct ip_sf_list
*igmp_mcf_get_next(struct seq_file
*seq
, struct ip_sf_list
*psf
)
2543 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2547 spin_unlock_bh(&state
->im
->lock
);
2548 state
->im
= state
->im
->next
;
2549 while (!state
->im
) {
2550 if (likely(state
->idev
!= NULL
))
2551 read_unlock(&state
->idev
->mc_list_lock
);
2553 state
->dev
= next_net_device_rcu(state
->dev
);
2558 state
->idev
= __in_dev_get_rcu(state
->dev
);
2561 read_lock(&state
->idev
->mc_list_lock
);
2562 state
->im
= state
->idev
->mc_list
;
2566 spin_lock_bh(&state
->im
->lock
);
2567 psf
= state
->im
->sources
;
2573 static struct ip_sf_list
*igmp_mcf_get_idx(struct seq_file
*seq
, loff_t pos
)
2575 struct ip_sf_list
*psf
= igmp_mcf_get_first(seq
);
2577 while (pos
&& (psf
= igmp_mcf_get_next(seq
, psf
)) != NULL
)
2579 return pos
? NULL
: psf
;
2582 static void *igmp_mcf_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2586 return *pos
? igmp_mcf_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2589 static void *igmp_mcf_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2591 struct ip_sf_list
*psf
;
2592 if (v
== SEQ_START_TOKEN
)
2593 psf
= igmp_mcf_get_first(seq
);
2595 psf
= igmp_mcf_get_next(seq
, v
);
2600 static void igmp_mcf_seq_stop(struct seq_file
*seq
, void *v
)
2603 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2604 if (likely(state
->im
!= NULL
)) {
2605 spin_unlock_bh(&state
->im
->lock
);
2608 if (likely(state
->idev
!= NULL
)) {
2609 read_unlock(&state
->idev
->mc_list_lock
);
2616 static int igmp_mcf_seq_show(struct seq_file
*seq
, void *v
)
2618 struct ip_sf_list
*psf
= (struct ip_sf_list
*)v
;
2619 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2621 if (v
== SEQ_START_TOKEN
) {
2624 "%10s %10s %6s %6s\n", "Idx",
2626 "SRC", "INC", "EXC");
2630 "0x%08x %6lu %6lu\n",
2631 state
->dev
->ifindex
, state
->dev
->name
,
2632 ntohl(state
->im
->multiaddr
),
2633 ntohl(psf
->sf_inaddr
),
2634 psf
->sf_count
[MCAST_INCLUDE
],
2635 psf
->sf_count
[MCAST_EXCLUDE
]);
2640 static const struct seq_operations igmp_mcf_seq_ops
= {
2641 .start
= igmp_mcf_seq_start
,
2642 .next
= igmp_mcf_seq_next
,
2643 .stop
= igmp_mcf_seq_stop
,
2644 .show
= igmp_mcf_seq_show
,
2647 static int igmp_mcf_seq_open(struct inode
*inode
, struct file
*file
)
2649 return seq_open_net(inode
, file
, &igmp_mcf_seq_ops
,
2650 sizeof(struct igmp_mcf_iter_state
));
2653 static const struct file_operations igmp_mcf_seq_fops
= {
2654 .owner
= THIS_MODULE
,
2655 .open
= igmp_mcf_seq_open
,
2657 .llseek
= seq_lseek
,
2658 .release
= seq_release_net
,
2661 static int __net_init
igmp_net_init(struct net
*net
)
2663 struct proc_dir_entry
*pde
;
2665 pde
= proc_net_fops_create(net
, "igmp", S_IRUGO
, &igmp_mc_seq_fops
);
2668 pde
= proc_net_fops_create(net
, "mcfilter", S_IRUGO
, &igmp_mcf_seq_fops
);
2674 proc_net_remove(net
, "igmp");
2679 static void __net_exit
igmp_net_exit(struct net
*net
)
2681 proc_net_remove(net
, "mcfilter");
2682 proc_net_remove(net
, "igmp");
2685 static struct pernet_operations igmp_net_ops
= {
2686 .init
= igmp_net_init
,
2687 .exit
= igmp_net_exit
,
2690 int __init
igmp_mc_proc_init(void)
2692 return register_pernet_subsys(&igmp_net_ops
);