2 * Linux NET3: Internet Group Management Protocol [IGMP]
4 * This code implements the IGMP protocol as defined in RFC1112. There has
5 * been a further revision of this protocol since which is now supported.
7 * If you have trouble with this module be careful what gcc you have used,
8 * the older version didn't come out right using gcc 2.5.8, the newer one
9 * seems to fall out with gcc 2.6.2.
12 * Alan Cox <alan@lxorguk.ukuu.org.uk>
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
21 * Alan Cox : Added lots of __inline__ to optimise
22 * the memory usage of all the tiny little
24 * Alan Cox : Dumped the header building experiment.
25 * Alan Cox : Minor tweaks ready for multicast routing
26 * and extended IGMP protocol.
27 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
28 * writes utterly bogus code otherwise (sigh)
29 * fixed IGMP loopback to behave in the manner
30 * desired by mrouted, fixed the fact it has been
31 * broken since 1.3.6 and cleaned up a few minor
34 * Chih-Jen Chang : Tried to revise IGMP to Version 2
35 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
36 * The enhancements are mainly based on Steve Deering's
37 * ipmulti-3.5 source code.
38 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
39 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
40 * the mrouted version on that device.
41 * Chih-Jen Chang : Added the max_resp_time parameter to
42 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
43 * to identify the multicast router version
44 * and do what the IGMP version 2 specified.
45 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
46 * Tsu-Sheng Tsao if the specified time expired.
47 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
48 * Alan Cox : Use GFP_ATOMIC in the right places.
49 * Christian Daudt : igmp timer wasn't set for local group
50 * memberships but was being deleted,
51 * which caused a "del_timer() called
52 * from %p with timer not initialized\n"
54 * Christian Daudt : removed del_timer from
55 * igmp_timer_expire function (960205).
56 * Christian Daudt : igmp_heard_report now only calls
57 * igmp_timer_expire if tm->running is
59 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
60 * igmp_heard_query never trigger. Expiry
61 * miscalculation fixed in igmp_heard_query
62 * and random() made to return unsigned to
63 * prevent negative expiry times.
64 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
65 * fix from pending 2.1.x patches.
66 * Alan Cox: Forget to enable FDDI support earlier.
67 * Alexey Kuznetsov: Fixed leaving groups on device down.
68 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
69 * David L Stevens: IGMPv3 support, with help from
73 #include <linux/module.h>
74 #include <linux/slab.h>
75 #include <asm/uaccess.h>
76 #include <linux/types.h>
77 #include <linux/kernel.h>
78 #include <linux/jiffies.h>
79 #include <linux/string.h>
80 #include <linux/socket.h>
81 #include <linux/sockios.h>
83 #include <linux/inet.h>
84 #include <linux/netdevice.h>
85 #include <linux/skbuff.h>
86 #include <linux/inetdevice.h>
87 #include <linux/igmp.h>
88 #include <linux/if_arp.h>
89 #include <linux/rtnetlink.h>
90 #include <linux/times.h>
91 #include <linux/pkt_sched.h>
93 #include <net/net_namespace.h>
96 #include <net/protocol.h>
97 #include <net/route.h>
99 #include <net/checksum.h>
100 #include <net/inet_common.h>
101 #include <linux/netfilter_ipv4.h>
102 #ifdef CONFIG_IP_MROUTE
103 #include <linux/mroute.h>
105 #ifdef CONFIG_PROC_FS
106 #include <linux/proc_fs.h>
107 #include <linux/seq_file.h>
110 #define IP_MAX_MEMBERSHIPS 20
111 #define IP_MAX_MSF 10
113 /* IGMP reports for link-local multicast groups are enabled by default */
114 int sysctl_igmp_llm_reports __read_mostly
= 1;
116 #ifdef CONFIG_IP_MULTICAST
117 /* Parameter names and values are taken from igmp-v2-06 draft */
119 #define IGMP_V1_ROUTER_PRESENT_TIMEOUT (400*HZ)
120 #define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ)
121 #define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
122 #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
123 #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
124 #define IGMP_QUERY_ROBUSTNESS_VARIABLE 2
127 #define IGMP_INITIAL_REPORT_DELAY (1)
129 /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
130 * IGMP specs require to report membership immediately after
131 * joining a group, but we delay the first report by a
132 * small interval. It seems more natural and still does not
133 * contradict to specs provided this delay is small enough.
136 #define IGMP_V1_SEEN(in_dev) \
137 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
138 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
139 ((in_dev)->mr_v1_seen && \
140 time_before(jiffies, (in_dev)->mr_v1_seen)))
141 #define IGMP_V2_SEEN(in_dev) \
142 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
143 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
144 ((in_dev)->mr_v2_seen && \
145 time_before(jiffies, (in_dev)->mr_v2_seen)))
147 static int unsolicited_report_interval(struct in_device
*in_dev
)
149 int interval_ms
, interval_jiffies
;
151 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
152 interval_ms
= IN_DEV_CONF_GET(
154 IGMPV2_UNSOLICITED_REPORT_INTERVAL
);
156 interval_ms
= IN_DEV_CONF_GET(
158 IGMPV3_UNSOLICITED_REPORT_INTERVAL
);
160 interval_jiffies
= msecs_to_jiffies(interval_ms
);
162 /* _timer functions can't handle a delay of 0 jiffies so ensure
163 * we always return a positive value.
165 if (interval_jiffies
<= 0)
166 interval_jiffies
= 1;
167 return interval_jiffies
;
170 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
);
171 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
);
172 static void igmpv3_clear_delrec(struct in_device
*in_dev
);
173 static int sf_setstate(struct ip_mc_list
*pmc
);
174 static void sf_markstate(struct ip_mc_list
*pmc
);
176 static void ip_mc_clear_src(struct ip_mc_list
*pmc
);
177 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
178 int sfcount
, __be32
*psfsrc
, int delta
);
180 static void ip_ma_put(struct ip_mc_list
*im
)
182 if (atomic_dec_and_test(&im
->refcnt
)) {
183 in_dev_put(im
->interface
);
188 #define for_each_pmc_rcu(in_dev, pmc) \
189 for (pmc = rcu_dereference(in_dev->mc_list); \
191 pmc = rcu_dereference(pmc->next_rcu))
193 #define for_each_pmc_rtnl(in_dev, pmc) \
194 for (pmc = rtnl_dereference(in_dev->mc_list); \
196 pmc = rtnl_dereference(pmc->next_rcu))
198 #ifdef CONFIG_IP_MULTICAST
204 static void igmp_stop_timer(struct ip_mc_list
*im
)
206 spin_lock_bh(&im
->lock
);
207 if (del_timer(&im
->timer
))
208 atomic_dec(&im
->refcnt
);
211 im
->unsolicit_count
= 0;
212 spin_unlock_bh(&im
->lock
);
215 /* It must be called with locked im->lock */
216 static void igmp_start_timer(struct ip_mc_list
*im
, int max_delay
)
218 int tv
= prandom_u32() % max_delay
;
221 if (!mod_timer(&im
->timer
, jiffies
+tv
+2))
222 atomic_inc(&im
->refcnt
);
225 static void igmp_gq_start_timer(struct in_device
*in_dev
)
227 int tv
= prandom_u32() % in_dev
->mr_maxdelay
;
229 in_dev
->mr_gq_running
= 1;
230 if (!mod_timer(&in_dev
->mr_gq_timer
, jiffies
+tv
+2))
234 static void igmp_ifc_start_timer(struct in_device
*in_dev
, int delay
)
236 int tv
= prandom_u32() % delay
;
238 if (!mod_timer(&in_dev
->mr_ifc_timer
, jiffies
+tv
+2))
242 static void igmp_mod_timer(struct ip_mc_list
*im
, int max_delay
)
244 spin_lock_bh(&im
->lock
);
245 im
->unsolicit_count
= 0;
246 if (del_timer(&im
->timer
)) {
247 if ((long)(im
->timer
.expires
-jiffies
) < max_delay
) {
248 add_timer(&im
->timer
);
250 spin_unlock_bh(&im
->lock
);
253 atomic_dec(&im
->refcnt
);
255 igmp_start_timer(im
, max_delay
);
256 spin_unlock_bh(&im
->lock
);
261 * Send an IGMP report.
264 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
267 static int is_in(struct ip_mc_list
*pmc
, struct ip_sf_list
*psf
, int type
,
268 int gdeleted
, int sdeleted
)
271 case IGMPV3_MODE_IS_INCLUDE
:
272 case IGMPV3_MODE_IS_EXCLUDE
:
273 if (gdeleted
|| sdeleted
)
275 if (!(pmc
->gsquery
&& !psf
->sf_gsresp
)) {
276 if (pmc
->sfmode
== MCAST_INCLUDE
)
278 /* don't include if this source is excluded
281 if (psf
->sf_count
[MCAST_INCLUDE
])
282 return type
== IGMPV3_MODE_IS_INCLUDE
;
283 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
284 psf
->sf_count
[MCAST_EXCLUDE
];
287 case IGMPV3_CHANGE_TO_INCLUDE
:
288 if (gdeleted
|| sdeleted
)
290 return psf
->sf_count
[MCAST_INCLUDE
] != 0;
291 case IGMPV3_CHANGE_TO_EXCLUDE
:
292 if (gdeleted
|| sdeleted
)
294 if (pmc
->sfcount
[MCAST_EXCLUDE
] == 0 ||
295 psf
->sf_count
[MCAST_INCLUDE
])
297 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
298 psf
->sf_count
[MCAST_EXCLUDE
];
299 case IGMPV3_ALLOW_NEW_SOURCES
:
300 if (gdeleted
|| !psf
->sf_crcount
)
302 return (pmc
->sfmode
== MCAST_INCLUDE
) ^ sdeleted
;
303 case IGMPV3_BLOCK_OLD_SOURCES
:
304 if (pmc
->sfmode
== MCAST_INCLUDE
)
305 return gdeleted
|| (psf
->sf_crcount
&& sdeleted
);
306 return psf
->sf_crcount
&& !gdeleted
&& !sdeleted
;
312 igmp_scount(struct ip_mc_list
*pmc
, int type
, int gdeleted
, int sdeleted
)
314 struct ip_sf_list
*psf
;
317 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
318 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
))
325 static struct sk_buff
*igmpv3_newpack(struct net_device
*dev
, unsigned int mtu
)
330 struct igmpv3_report
*pig
;
331 struct net
*net
= dev_net(dev
);
333 int hlen
= LL_RESERVED_SPACE(dev
);
334 int tlen
= dev
->needed_tailroom
;
335 unsigned int size
= mtu
;
338 skb
= alloc_skb(size
+ hlen
+ tlen
,
339 GFP_ATOMIC
| __GFP_NOWARN
);
346 skb
->priority
= TC_PRIO_CONTROL
;
348 rt
= ip_route_output_ports(net
, &fl4
, NULL
, IGMPV3_ALL_MCR
, 0,
350 IPPROTO_IGMP
, 0, dev
->ifindex
);
356 skb_dst_set(skb
, &rt
->dst
);
359 skb
->reserved_tailroom
= skb_end_offset(skb
) -
360 min(mtu
, skb_end_offset(skb
));
361 skb_reserve(skb
, hlen
);
363 skb_reset_network_header(skb
);
365 skb_put(skb
, sizeof(struct iphdr
) + 4);
368 pip
->ihl
= (sizeof(struct iphdr
)+4)>>2;
370 pip
->frag_off
= htons(IP_DF
);
372 pip
->daddr
= fl4
.daddr
;
373 pip
->saddr
= fl4
.saddr
;
374 pip
->protocol
= IPPROTO_IGMP
;
375 pip
->tot_len
= 0; /* filled in later */
376 ip_select_ident(net
, skb
, NULL
);
377 ((u8
*)&pip
[1])[0] = IPOPT_RA
;
378 ((u8
*)&pip
[1])[1] = 4;
379 ((u8
*)&pip
[1])[2] = 0;
380 ((u8
*)&pip
[1])[3] = 0;
382 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
) + 4;
383 skb_put(skb
, sizeof(*pig
));
384 pig
= igmpv3_report_hdr(skb
);
385 pig
->type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
393 static int igmpv3_sendpack(struct sk_buff
*skb
)
395 struct igmphdr
*pig
= igmp_hdr(skb
);
396 const int igmplen
= skb_tail_pointer(skb
) - skb_transport_header(skb
);
398 pig
->csum
= ip_compute_csum(igmp_hdr(skb
), igmplen
);
400 return ip_local_out(dev_net(skb_dst(skb
)->dev
), skb
->sk
, skb
);
403 static int grec_size(struct ip_mc_list
*pmc
, int type
, int gdel
, int sdel
)
405 return sizeof(struct igmpv3_grec
) + 4*igmp_scount(pmc
, type
, gdel
, sdel
);
408 static struct sk_buff
*add_grhead(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
409 int type
, struct igmpv3_grec
**ppgr
)
411 struct net_device
*dev
= pmc
->interface
->dev
;
412 struct igmpv3_report
*pih
;
413 struct igmpv3_grec
*pgr
;
416 skb
= igmpv3_newpack(dev
, dev
->mtu
);
419 pgr
= (struct igmpv3_grec
*)skb_put(skb
, sizeof(struct igmpv3_grec
));
420 pgr
->grec_type
= type
;
421 pgr
->grec_auxwords
= 0;
423 pgr
->grec_mca
= pmc
->multiaddr
;
424 pih
= igmpv3_report_hdr(skb
);
425 pih
->ngrec
= htons(ntohs(pih
->ngrec
)+1);
430 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
432 static struct sk_buff
*add_grec(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
433 int type
, int gdeleted
, int sdeleted
)
435 struct net_device
*dev
= pmc
->interface
->dev
;
436 struct igmpv3_report
*pih
;
437 struct igmpv3_grec
*pgr
= NULL
;
438 struct ip_sf_list
*psf
, *psf_next
, *psf_prev
, **psf_list
;
439 int scount
, stotal
, first
, isquery
, truncate
;
441 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
443 if (ipv4_is_local_multicast(pmc
->multiaddr
) && !sysctl_igmp_llm_reports
)
446 isquery
= type
== IGMPV3_MODE_IS_INCLUDE
||
447 type
== IGMPV3_MODE_IS_EXCLUDE
;
448 truncate
= type
== IGMPV3_MODE_IS_EXCLUDE
||
449 type
== IGMPV3_CHANGE_TO_EXCLUDE
;
453 psf_list
= sdeleted
? &pmc
->tomb
: &pmc
->sources
;
458 pih
= skb
? igmpv3_report_hdr(skb
) : NULL
;
460 /* EX and TO_EX get a fresh packet, if needed */
462 if (pih
&& pih
->ngrec
&&
463 AVAILABLE(skb
) < grec_size(pmc
, type
, gdeleted
, sdeleted
)) {
465 igmpv3_sendpack(skb
);
466 skb
= igmpv3_newpack(dev
, dev
->mtu
);
471 for (psf
= *psf_list
; psf
; psf
= psf_next
) {
474 psf_next
= psf
->sf_next
;
476 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
)) {
481 /* clear marks on query responses */
485 if (AVAILABLE(skb
) < sizeof(__be32
) +
486 first
*sizeof(struct igmpv3_grec
)) {
487 if (truncate
&& !first
)
488 break; /* truncate these */
490 pgr
->grec_nsrcs
= htons(scount
);
492 igmpv3_sendpack(skb
);
493 skb
= igmpv3_newpack(dev
, dev
->mtu
);
498 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
503 psrc
= (__be32
*)skb_put(skb
, sizeof(__be32
));
504 *psrc
= psf
->sf_inaddr
;
506 if ((type
== IGMPV3_ALLOW_NEW_SOURCES
||
507 type
== IGMPV3_BLOCK_OLD_SOURCES
) && psf
->sf_crcount
) {
509 if ((sdeleted
|| gdeleted
) && psf
->sf_crcount
== 0) {
511 psf_prev
->sf_next
= psf
->sf_next
;
513 *psf_list
= psf
->sf_next
;
523 if (type
== IGMPV3_ALLOW_NEW_SOURCES
||
524 type
== IGMPV3_BLOCK_OLD_SOURCES
)
526 if (pmc
->crcount
|| isquery
) {
527 /* make sure we have room for group header */
528 if (skb
&& AVAILABLE(skb
) < sizeof(struct igmpv3_grec
)) {
529 igmpv3_sendpack(skb
);
530 skb
= NULL
; /* add_grhead will get a new one */
532 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
536 pgr
->grec_nsrcs
= htons(scount
);
539 pmc
->gsquery
= 0; /* clear query state on report */
543 static int igmpv3_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
)
545 struct sk_buff
*skb
= NULL
;
550 for_each_pmc_rcu(in_dev
, pmc
) {
551 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
553 if (ipv4_is_local_multicast(pmc
->multiaddr
) &&
554 !sysctl_igmp_llm_reports
)
556 spin_lock_bh(&pmc
->lock
);
557 if (pmc
->sfcount
[MCAST_EXCLUDE
])
558 type
= IGMPV3_MODE_IS_EXCLUDE
;
560 type
= IGMPV3_MODE_IS_INCLUDE
;
561 skb
= add_grec(skb
, pmc
, type
, 0, 0);
562 spin_unlock_bh(&pmc
->lock
);
566 spin_lock_bh(&pmc
->lock
);
567 if (pmc
->sfcount
[MCAST_EXCLUDE
])
568 type
= IGMPV3_MODE_IS_EXCLUDE
;
570 type
= IGMPV3_MODE_IS_INCLUDE
;
571 skb
= add_grec(skb
, pmc
, type
, 0, 0);
572 spin_unlock_bh(&pmc
->lock
);
576 return igmpv3_sendpack(skb
);
580 * remove zero-count source records from a source filter list
582 static void igmpv3_clear_zeros(struct ip_sf_list
**ppsf
)
584 struct ip_sf_list
*psf_prev
, *psf_next
, *psf
;
587 for (psf
= *ppsf
; psf
; psf
= psf_next
) {
588 psf_next
= psf
->sf_next
;
589 if (psf
->sf_crcount
== 0) {
591 psf_prev
->sf_next
= psf
->sf_next
;
593 *ppsf
= psf
->sf_next
;
600 static void igmpv3_send_cr(struct in_device
*in_dev
)
602 struct ip_mc_list
*pmc
, *pmc_prev
, *pmc_next
;
603 struct sk_buff
*skb
= NULL
;
607 spin_lock_bh(&in_dev
->mc_tomb_lock
);
611 for (pmc
= in_dev
->mc_tomb
; pmc
; pmc
= pmc_next
) {
612 pmc_next
= pmc
->next
;
613 if (pmc
->sfmode
== MCAST_INCLUDE
) {
614 type
= IGMPV3_BLOCK_OLD_SOURCES
;
615 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
616 skb
= add_grec(skb
, pmc
, type
, 1, 0);
617 skb
= add_grec(skb
, pmc
, dtype
, 1, 1);
620 if (pmc
->sfmode
== MCAST_EXCLUDE
) {
621 type
= IGMPV3_CHANGE_TO_INCLUDE
;
622 skb
= add_grec(skb
, pmc
, type
, 1, 0);
625 if (pmc
->crcount
== 0) {
626 igmpv3_clear_zeros(&pmc
->tomb
);
627 igmpv3_clear_zeros(&pmc
->sources
);
630 if (pmc
->crcount
== 0 && !pmc
->tomb
&& !pmc
->sources
) {
632 pmc_prev
->next
= pmc_next
;
634 in_dev
->mc_tomb
= pmc_next
;
635 in_dev_put(pmc
->interface
);
640 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
643 for_each_pmc_rcu(in_dev
, pmc
) {
644 spin_lock_bh(&pmc
->lock
);
645 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
646 type
= IGMPV3_BLOCK_OLD_SOURCES
;
647 dtype
= IGMPV3_ALLOW_NEW_SOURCES
;
649 type
= IGMPV3_ALLOW_NEW_SOURCES
;
650 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
652 skb
= add_grec(skb
, pmc
, type
, 0, 0);
653 skb
= add_grec(skb
, pmc
, dtype
, 0, 1); /* deleted sources */
655 /* filter mode changes */
657 if (pmc
->sfmode
== MCAST_EXCLUDE
)
658 type
= IGMPV3_CHANGE_TO_EXCLUDE
;
660 type
= IGMPV3_CHANGE_TO_INCLUDE
;
661 skb
= add_grec(skb
, pmc
, type
, 0, 0);
664 spin_unlock_bh(&pmc
->lock
);
670 (void) igmpv3_sendpack(skb
);
673 static int igmp_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
,
680 struct net_device
*dev
= in_dev
->dev
;
681 struct net
*net
= dev_net(dev
);
682 __be32 group
= pmc
? pmc
->multiaddr
: 0;
687 if (type
== IGMPV3_HOST_MEMBERSHIP_REPORT
)
688 return igmpv3_send_report(in_dev
, pmc
);
690 if (ipv4_is_local_multicast(group
) && !sysctl_igmp_llm_reports
)
693 if (type
== IGMP_HOST_LEAVE_MESSAGE
)
694 dst
= IGMP_ALL_ROUTER
;
698 rt
= ip_route_output_ports(net
, &fl4
, NULL
, dst
, 0,
700 IPPROTO_IGMP
, 0, dev
->ifindex
);
704 hlen
= LL_RESERVED_SPACE(dev
);
705 tlen
= dev
->needed_tailroom
;
706 skb
= alloc_skb(IGMP_SIZE
+ hlen
+ tlen
, GFP_ATOMIC
);
711 skb
->priority
= TC_PRIO_CONTROL
;
713 skb_dst_set(skb
, &rt
->dst
);
715 skb_reserve(skb
, hlen
);
717 skb_reset_network_header(skb
);
719 skb_put(skb
, sizeof(struct iphdr
) + 4);
722 iph
->ihl
= (sizeof(struct iphdr
)+4)>>2;
724 iph
->frag_off
= htons(IP_DF
);
727 iph
->saddr
= fl4
.saddr
;
728 iph
->protocol
= IPPROTO_IGMP
;
729 ip_select_ident(net
, skb
, NULL
);
730 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
731 ((u8
*)&iph
[1])[1] = 4;
732 ((u8
*)&iph
[1])[2] = 0;
733 ((u8
*)&iph
[1])[3] = 0;
735 ih
= (struct igmphdr
*)skb_put(skb
, sizeof(struct igmphdr
));
740 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
742 return ip_local_out(net
, skb
->sk
, skb
);
745 static void igmp_gq_timer_expire(unsigned long data
)
747 struct in_device
*in_dev
= (struct in_device
*)data
;
749 in_dev
->mr_gq_running
= 0;
750 igmpv3_send_report(in_dev
, NULL
);
754 static void igmp_ifc_timer_expire(unsigned long data
)
756 struct in_device
*in_dev
= (struct in_device
*)data
;
758 igmpv3_send_cr(in_dev
);
759 if (in_dev
->mr_ifc_count
) {
760 in_dev
->mr_ifc_count
--;
761 igmp_ifc_start_timer(in_dev
,
762 unsolicited_report_interval(in_dev
));
767 static void igmp_ifc_event(struct in_device
*in_dev
)
769 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
771 in_dev
->mr_ifc_count
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
772 igmp_ifc_start_timer(in_dev
, 1);
776 static void igmp_timer_expire(unsigned long data
)
778 struct ip_mc_list
*im
= (struct ip_mc_list
*)data
;
779 struct in_device
*in_dev
= im
->interface
;
781 spin_lock(&im
->lock
);
784 if (im
->unsolicit_count
) {
785 im
->unsolicit_count
--;
786 igmp_start_timer(im
, unsolicited_report_interval(in_dev
));
789 spin_unlock(&im
->lock
);
791 if (IGMP_V1_SEEN(in_dev
))
792 igmp_send_report(in_dev
, im
, IGMP_HOST_MEMBERSHIP_REPORT
);
793 else if (IGMP_V2_SEEN(in_dev
))
794 igmp_send_report(in_dev
, im
, IGMPV2_HOST_MEMBERSHIP_REPORT
);
796 igmp_send_report(in_dev
, im
, IGMPV3_HOST_MEMBERSHIP_REPORT
);
801 /* mark EXCLUDE-mode sources */
802 static int igmp_xmarksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
804 struct ip_sf_list
*psf
;
808 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
811 for (i
= 0; i
< nsrcs
; i
++) {
812 /* skip inactive filters */
813 if (psf
->sf_count
[MCAST_INCLUDE
] ||
814 pmc
->sfcount
[MCAST_EXCLUDE
] !=
815 psf
->sf_count
[MCAST_EXCLUDE
])
817 if (srcs
[i
] == psf
->sf_inaddr
) {
824 if (scount
== nsrcs
) /* all sources excluded */
829 static int igmp_marksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
831 struct ip_sf_list
*psf
;
834 if (pmc
->sfmode
== MCAST_EXCLUDE
)
835 return igmp_xmarksources(pmc
, nsrcs
, srcs
);
837 /* mark INCLUDE-mode sources */
839 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
842 for (i
= 0; i
< nsrcs
; i
++)
843 if (srcs
[i
] == psf
->sf_inaddr
) {
857 /* return true if packet was dropped */
858 static bool igmp_heard_report(struct in_device
*in_dev
, __be32 group
)
860 struct ip_mc_list
*im
;
862 /* Timers are only set for non-local groups */
864 if (group
== IGMP_ALL_HOSTS
)
866 if (ipv4_is_local_multicast(group
) && !sysctl_igmp_llm_reports
)
870 for_each_pmc_rcu(in_dev
, im
) {
871 if (im
->multiaddr
== group
) {
880 /* return true if packet was dropped */
881 static bool igmp_heard_query(struct in_device
*in_dev
, struct sk_buff
*skb
,
884 struct igmphdr
*ih
= igmp_hdr(skb
);
885 struct igmpv3_query
*ih3
= igmpv3_query_hdr(skb
);
886 struct ip_mc_list
*im
;
887 __be32 group
= ih
->group
;
894 /* Alas, old v1 router presents here. */
896 max_delay
= IGMP_QUERY_RESPONSE_INTERVAL
;
897 in_dev
->mr_v1_seen
= jiffies
+
898 IGMP_V1_ROUTER_PRESENT_TIMEOUT
;
901 /* v2 router present */
902 max_delay
= ih
->code
*(HZ
/IGMP_TIMER_SCALE
);
903 in_dev
->mr_v2_seen
= jiffies
+
904 IGMP_V2_ROUTER_PRESENT_TIMEOUT
;
906 /* cancel the interface change timer */
907 in_dev
->mr_ifc_count
= 0;
908 if (del_timer(&in_dev
->mr_ifc_timer
))
909 __in_dev_put(in_dev
);
910 /* clear deleted report items */
911 igmpv3_clear_delrec(in_dev
);
912 } else if (len
< 12) {
913 return true; /* ignore bogus packet; freed by caller */
914 } else if (IGMP_V1_SEEN(in_dev
)) {
915 /* This is a v3 query with v1 queriers present */
916 max_delay
= IGMP_QUERY_RESPONSE_INTERVAL
;
918 } else if (IGMP_V2_SEEN(in_dev
)) {
919 /* this is a v3 query with v2 queriers present;
920 * Interpretation of the max_delay code is problematic here.
921 * A real v2 host would use ih_code directly, while v3 has a
922 * different encoding. We use the v3 encoding as more likely
923 * to be intended in a v3 query.
925 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
927 max_delay
= 1; /* can't mod w/ 0 */
929 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)))
932 ih3
= igmpv3_query_hdr(skb
);
934 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)
935 + ntohs(ih3
->nsrcs
)*sizeof(__be32
)))
937 ih3
= igmpv3_query_hdr(skb
);
940 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
942 max_delay
= 1; /* can't mod w/ 0 */
943 in_dev
->mr_maxdelay
= max_delay
;
945 in_dev
->mr_qrv
= ih3
->qrv
;
946 if (!group
) { /* general query */
948 return true; /* no sources allowed */
949 igmp_gq_start_timer(in_dev
);
952 /* mark sources to include, if group & source-specific */
953 mark
= ih3
->nsrcs
!= 0;
957 * - Start the timers in all of our membership records
958 * that the query applies to for the interface on
959 * which the query arrived excl. those that belong
960 * to a "local" group (224.0.0.X)
961 * - For timers already running check if they need to
963 * - Use the igmp->igmp_code field as the maximum
967 for_each_pmc_rcu(in_dev
, im
) {
970 if (group
&& group
!= im
->multiaddr
)
972 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
974 if (ipv4_is_local_multicast(im
->multiaddr
) &&
975 !sysctl_igmp_llm_reports
)
977 spin_lock_bh(&im
->lock
);
979 im
->gsquery
= im
->gsquery
&& mark
;
982 changed
= !im
->gsquery
||
983 igmp_marksources(im
, ntohs(ih3
->nsrcs
), ih3
->srcs
);
984 spin_unlock_bh(&im
->lock
);
986 igmp_mod_timer(im
, max_delay
);
992 /* called in rcu_read_lock() section */
993 int igmp_rcv(struct sk_buff
*skb
)
995 /* This basically follows the spec line by line -- see RFC1112 */
997 struct in_device
*in_dev
= __in_dev_get_rcu(skb
->dev
);
1004 if (!pskb_may_pull(skb
, sizeof(struct igmphdr
)))
1007 if (skb_checksum_simple_validate(skb
))
1012 case IGMP_HOST_MEMBERSHIP_QUERY
:
1013 dropped
= igmp_heard_query(in_dev
, skb
, len
);
1015 case IGMP_HOST_MEMBERSHIP_REPORT
:
1016 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1017 /* Is it our report looped back? */
1018 if (rt_is_output_route(skb_rtable(skb
)))
1020 /* don't rely on MC router hearing unicast reports */
1021 if (skb
->pkt_type
== PACKET_MULTICAST
||
1022 skb
->pkt_type
== PACKET_BROADCAST
)
1023 dropped
= igmp_heard_report(in_dev
, ih
->group
);
1026 #ifdef CONFIG_IP_PIMSM_V1
1027 return pim_rcv_v1(skb
);
1029 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1032 case IGMP_HOST_LEAVE_MESSAGE
:
1034 case IGMP_MTRACE_RESP
:
1052 * Add a filter to a device
1055 static void ip_mc_filter_add(struct in_device
*in_dev
, __be32 addr
)
1057 char buf
[MAX_ADDR_LEN
];
1058 struct net_device
*dev
= in_dev
->dev
;
1060 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1061 We will get multicast token leakage, when IFF_MULTICAST
1062 is changed. This check should be done in ndo_set_rx_mode
1063 routine. Something sort of:
1064 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1067 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1068 dev_mc_add(dev
, buf
);
1072 * Remove a filter from a device
1075 static void ip_mc_filter_del(struct in_device
*in_dev
, __be32 addr
)
1077 char buf
[MAX_ADDR_LEN
];
1078 struct net_device
*dev
= in_dev
->dev
;
1080 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1081 dev_mc_del(dev
, buf
);
1084 #ifdef CONFIG_IP_MULTICAST
1086 * deleted ip_mc_list manipulation
1088 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
)
1090 struct ip_mc_list
*pmc
;
1092 /* this is an "ip_mc_list" for convenience; only the fields below
1093 * are actually used. In particular, the refcnt and users are not
1094 * used for management of the delete list. Using the same structure
1095 * for deleted items allows change reports to use common code with
1096 * non-deleted or query-response MCA's.
1098 pmc
= kzalloc(sizeof(*pmc
), GFP_KERNEL
);
1101 spin_lock_bh(&im
->lock
);
1102 pmc
->interface
= im
->interface
;
1103 in_dev_hold(in_dev
);
1104 pmc
->multiaddr
= im
->multiaddr
;
1105 pmc
->crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
1106 pmc
->sfmode
= im
->sfmode
;
1107 if (pmc
->sfmode
== MCAST_INCLUDE
) {
1108 struct ip_sf_list
*psf
;
1110 pmc
->tomb
= im
->tomb
;
1111 pmc
->sources
= im
->sources
;
1112 im
->tomb
= im
->sources
= NULL
;
1113 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1114 psf
->sf_crcount
= pmc
->crcount
;
1116 spin_unlock_bh(&im
->lock
);
1118 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1119 pmc
->next
= in_dev
->mc_tomb
;
1120 in_dev
->mc_tomb
= pmc
;
1121 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1124 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
)
1126 struct ip_mc_list
*pmc
, *pmc_prev
;
1127 struct ip_sf_list
*psf
, *psf_next
;
1129 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1131 for (pmc
= in_dev
->mc_tomb
; pmc
; pmc
= pmc
->next
) {
1132 if (pmc
->multiaddr
== multiaddr
)
1138 pmc_prev
->next
= pmc
->next
;
1140 in_dev
->mc_tomb
= pmc
->next
;
1142 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1144 for (psf
= pmc
->tomb
; psf
; psf
= psf_next
) {
1145 psf_next
= psf
->sf_next
;
1148 in_dev_put(pmc
->interface
);
1153 static void igmpv3_clear_delrec(struct in_device
*in_dev
)
1155 struct ip_mc_list
*pmc
, *nextpmc
;
1157 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1158 pmc
= in_dev
->mc_tomb
;
1159 in_dev
->mc_tomb
= NULL
;
1160 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1162 for (; pmc
; pmc
= nextpmc
) {
1163 nextpmc
= pmc
->next
;
1164 ip_mc_clear_src(pmc
);
1165 in_dev_put(pmc
->interface
);
1168 /* clear dead sources, too */
1170 for_each_pmc_rcu(in_dev
, pmc
) {
1171 struct ip_sf_list
*psf
, *psf_next
;
1173 spin_lock_bh(&pmc
->lock
);
1176 spin_unlock_bh(&pmc
->lock
);
1177 for (; psf
; psf
= psf_next
) {
1178 psf_next
= psf
->sf_next
;
1186 static void igmp_group_dropped(struct ip_mc_list
*im
)
1188 struct in_device
*in_dev
= im
->interface
;
1189 #ifdef CONFIG_IP_MULTICAST
1195 ip_mc_filter_del(in_dev
, im
->multiaddr
);
1198 #ifdef CONFIG_IP_MULTICAST
1199 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1201 if (ipv4_is_local_multicast(im
->multiaddr
) && !sysctl_igmp_llm_reports
)
1204 reporter
= im
->reporter
;
1205 igmp_stop_timer(im
);
1207 if (!in_dev
->dead
) {
1208 if (IGMP_V1_SEEN(in_dev
))
1210 if (IGMP_V2_SEEN(in_dev
)) {
1212 igmp_send_report(in_dev
, im
, IGMP_HOST_LEAVE_MESSAGE
);
1216 igmpv3_add_delrec(in_dev
, im
);
1218 igmp_ifc_event(in_dev
);
1223 static void igmp_group_added(struct ip_mc_list
*im
)
1225 struct in_device
*in_dev
= im
->interface
;
1227 if (im
->loaded
== 0) {
1229 ip_mc_filter_add(in_dev
, im
->multiaddr
);
1232 #ifdef CONFIG_IP_MULTICAST
1233 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1235 if (ipv4_is_local_multicast(im
->multiaddr
) && !sysctl_igmp_llm_reports
)
1240 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
)) {
1241 spin_lock_bh(&im
->lock
);
1242 igmp_start_timer(im
, IGMP_INITIAL_REPORT_DELAY
);
1243 spin_unlock_bh(&im
->lock
);
1248 im
->crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
1249 igmp_ifc_event(in_dev
);
1255 * Multicast list managers
1258 static u32
ip_mc_hash(const struct ip_mc_list
*im
)
1260 return hash_32((__force u32
)im
->multiaddr
, MC_HASH_SZ_LOG
);
1263 static void ip_mc_hash_add(struct in_device
*in_dev
,
1264 struct ip_mc_list
*im
)
1266 struct ip_mc_list __rcu
**mc_hash
;
1269 mc_hash
= rtnl_dereference(in_dev
->mc_hash
);
1271 hash
= ip_mc_hash(im
);
1272 im
->next_hash
= mc_hash
[hash
];
1273 rcu_assign_pointer(mc_hash
[hash
], im
);
1277 /* do not use a hash table for small number of items */
1278 if (in_dev
->mc_count
< 4)
1281 mc_hash
= kzalloc(sizeof(struct ip_mc_list
*) << MC_HASH_SZ_LOG
,
1286 for_each_pmc_rtnl(in_dev
, im
) {
1287 hash
= ip_mc_hash(im
);
1288 im
->next_hash
= mc_hash
[hash
];
1289 RCU_INIT_POINTER(mc_hash
[hash
], im
);
1292 rcu_assign_pointer(in_dev
->mc_hash
, mc_hash
);
1295 static void ip_mc_hash_remove(struct in_device
*in_dev
,
1296 struct ip_mc_list
*im
)
1298 struct ip_mc_list __rcu
**mc_hash
= rtnl_dereference(in_dev
->mc_hash
);
1299 struct ip_mc_list
*aux
;
1303 mc_hash
+= ip_mc_hash(im
);
1304 while ((aux
= rtnl_dereference(*mc_hash
)) != im
)
1305 mc_hash
= &aux
->next_hash
;
1306 *mc_hash
= im
->next_hash
;
1311 * A socket has joined a multicast group on device dev.
1314 void ip_mc_inc_group(struct in_device
*in_dev
, __be32 addr
)
1316 struct ip_mc_list
*im
;
1320 for_each_pmc_rtnl(in_dev
, im
) {
1321 if (im
->multiaddr
== addr
) {
1323 ip_mc_add_src(in_dev
, &addr
, MCAST_EXCLUDE
, 0, NULL
, 0);
1328 im
= kzalloc(sizeof(*im
), GFP_KERNEL
);
1333 im
->interface
= in_dev
;
1334 in_dev_hold(in_dev
);
1335 im
->multiaddr
= addr
;
1336 /* initial mode is (EX, empty) */
1337 im
->sfmode
= MCAST_EXCLUDE
;
1338 im
->sfcount
[MCAST_EXCLUDE
] = 1;
1339 atomic_set(&im
->refcnt
, 1);
1340 spin_lock_init(&im
->lock
);
1341 #ifdef CONFIG_IP_MULTICAST
1342 setup_timer(&im
->timer
, igmp_timer_expire
, (unsigned long)im
);
1343 im
->unsolicit_count
= sysctl_igmp_qrv
;
1346 im
->next_rcu
= in_dev
->mc_list
;
1348 rcu_assign_pointer(in_dev
->mc_list
, im
);
1350 ip_mc_hash_add(in_dev
, im
);
1352 #ifdef CONFIG_IP_MULTICAST
1353 igmpv3_del_delrec(in_dev
, im
->multiaddr
);
1355 igmp_group_added(im
);
1357 ip_rt_multicast_event(in_dev
);
1361 EXPORT_SYMBOL(ip_mc_inc_group
);
1363 static int ip_mc_check_iphdr(struct sk_buff
*skb
)
1365 const struct iphdr
*iph
;
1367 unsigned int offset
= skb_network_offset(skb
) + sizeof(*iph
);
1369 if (!pskb_may_pull(skb
, offset
))
1374 if (iph
->version
!= 4 || ip_hdrlen(skb
) < sizeof(*iph
))
1377 offset
+= ip_hdrlen(skb
) - sizeof(*iph
);
1379 if (!pskb_may_pull(skb
, offset
))
1384 if (unlikely(ip_fast_csum((u8
*)iph
, iph
->ihl
)))
1387 len
= skb_network_offset(skb
) + ntohs(iph
->tot_len
);
1388 if (skb
->len
< len
|| len
< offset
)
1391 skb_set_transport_header(skb
, offset
);
1396 static int ip_mc_check_igmp_reportv3(struct sk_buff
*skb
)
1398 unsigned int len
= skb_transport_offset(skb
);
1400 len
+= sizeof(struct igmpv3_report
);
1402 return pskb_may_pull(skb
, len
) ? 0 : -EINVAL
;
1405 static int ip_mc_check_igmp_query(struct sk_buff
*skb
)
1407 unsigned int len
= skb_transport_offset(skb
);
1409 len
+= sizeof(struct igmphdr
);
1414 if (skb
->len
!= len
) {
1416 len
+= sizeof(struct igmpv3_query
) - sizeof(struct igmphdr
);
1417 if (skb
->len
< len
|| !pskb_may_pull(skb
, len
))
1421 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1422 * all-systems destination addresses (224.0.0.1) for general queries
1424 if (!igmp_hdr(skb
)->group
&&
1425 ip_hdr(skb
)->daddr
!= htonl(INADDR_ALLHOSTS_GROUP
))
1431 static int ip_mc_check_igmp_msg(struct sk_buff
*skb
)
1433 switch (igmp_hdr(skb
)->type
) {
1434 case IGMP_HOST_LEAVE_MESSAGE
:
1435 case IGMP_HOST_MEMBERSHIP_REPORT
:
1436 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1439 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1440 return ip_mc_check_igmp_reportv3(skb
);
1441 case IGMP_HOST_MEMBERSHIP_QUERY
:
1442 return ip_mc_check_igmp_query(skb
);
1448 static inline __sum16
ip_mc_validate_checksum(struct sk_buff
*skb
)
1450 return skb_checksum_simple_validate(skb
);
1453 static int __ip_mc_check_igmp(struct sk_buff
*skb
, struct sk_buff
**skb_trimmed
)
1456 struct sk_buff
*skb_chk
;
1457 unsigned int transport_len
;
1458 unsigned int len
= skb_transport_offset(skb
) + sizeof(struct igmphdr
);
1461 transport_len
= ntohs(ip_hdr(skb
)->tot_len
) - ip_hdrlen(skb
);
1463 skb_chk
= skb_checksum_trimmed(skb
, transport_len
,
1464 ip_mc_validate_checksum
);
1468 if (!pskb_may_pull(skb_chk
, len
))
1471 ret
= ip_mc_check_igmp_msg(skb_chk
);
1476 *skb_trimmed
= skb_chk
;
1477 /* free now unneeded clone */
1478 else if (skb_chk
!= skb
)
1484 if (ret
&& skb_chk
&& skb_chk
!= skb
)
1491 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1492 * @skb: the skb to validate
1493 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1495 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1496 * skb transport header accordingly and returns zero.
1498 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1500 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1501 * -ENOMEM: A memory allocation failure happened.
1503 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1504 * to NULL): After parsing an IGMP packet successfully it will point to
1505 * an skb which has its tail aligned to the IP packet end. This might
1506 * either be the originally provided skb or a trimmed, cloned version if
1507 * the skb frame had data beyond the IP packet. A cloned skb allows us
1508 * to leave the original skb and its full frame unchanged (which might be
1509 * desirable for layer 2 frame jugglers).
1511 * Caller needs to set the skb network header and free any returned skb if it
1512 * differs from the provided skb.
1514 int ip_mc_check_igmp(struct sk_buff
*skb
, struct sk_buff
**skb_trimmed
)
1516 int ret
= ip_mc_check_iphdr(skb
);
1521 if (ip_hdr(skb
)->protocol
!= IPPROTO_IGMP
)
1524 return __ip_mc_check_igmp(skb
, skb_trimmed
);
1526 EXPORT_SYMBOL(ip_mc_check_igmp
);
1529 * Resend IGMP JOIN report; used by netdev notifier.
1531 static void ip_mc_rejoin_groups(struct in_device
*in_dev
)
1533 #ifdef CONFIG_IP_MULTICAST
1534 struct ip_mc_list
*im
;
1539 for_each_pmc_rtnl(in_dev
, im
) {
1540 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1542 if (ipv4_is_local_multicast(im
->multiaddr
) &&
1543 !sysctl_igmp_llm_reports
)
1546 /* a failover is happening and switches
1547 * must be notified immediately
1549 if (IGMP_V1_SEEN(in_dev
))
1550 type
= IGMP_HOST_MEMBERSHIP_REPORT
;
1551 else if (IGMP_V2_SEEN(in_dev
))
1552 type
= IGMPV2_HOST_MEMBERSHIP_REPORT
;
1554 type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
1555 igmp_send_report(in_dev
, im
, type
);
1561 * A socket has left a multicast group on device dev
1564 void ip_mc_dec_group(struct in_device
*in_dev
, __be32 addr
)
1566 struct ip_mc_list
*i
;
1567 struct ip_mc_list __rcu
**ip
;
1571 for (ip
= &in_dev
->mc_list
;
1572 (i
= rtnl_dereference(*ip
)) != NULL
;
1573 ip
= &i
->next_rcu
) {
1574 if (i
->multiaddr
== addr
) {
1575 if (--i
->users
== 0) {
1576 ip_mc_hash_remove(in_dev
, i
);
1579 igmp_group_dropped(i
);
1583 ip_rt_multicast_event(in_dev
);
1592 EXPORT_SYMBOL(ip_mc_dec_group
);
1594 /* Device changing type */
1596 void ip_mc_unmap(struct in_device
*in_dev
)
1598 struct ip_mc_list
*pmc
;
1602 for_each_pmc_rtnl(in_dev
, pmc
)
1603 igmp_group_dropped(pmc
);
1606 void ip_mc_remap(struct in_device
*in_dev
)
1608 struct ip_mc_list
*pmc
;
1612 for_each_pmc_rtnl(in_dev
, pmc
)
1613 igmp_group_added(pmc
);
1616 /* Device going down */
1618 void ip_mc_down(struct in_device
*in_dev
)
1620 struct ip_mc_list
*pmc
;
1624 for_each_pmc_rtnl(in_dev
, pmc
)
1625 igmp_group_dropped(pmc
);
1627 #ifdef CONFIG_IP_MULTICAST
1628 in_dev
->mr_ifc_count
= 0;
1629 if (del_timer(&in_dev
->mr_ifc_timer
))
1630 __in_dev_put(in_dev
);
1631 in_dev
->mr_gq_running
= 0;
1632 if (del_timer(&in_dev
->mr_gq_timer
))
1633 __in_dev_put(in_dev
);
1634 igmpv3_clear_delrec(in_dev
);
1637 ip_mc_dec_group(in_dev
, IGMP_ALL_HOSTS
);
1640 void ip_mc_init_dev(struct in_device
*in_dev
)
1644 #ifdef CONFIG_IP_MULTICAST
1645 setup_timer(&in_dev
->mr_gq_timer
, igmp_gq_timer_expire
,
1646 (unsigned long)in_dev
);
1647 setup_timer(&in_dev
->mr_ifc_timer
, igmp_ifc_timer_expire
,
1648 (unsigned long)in_dev
);
1649 in_dev
->mr_qrv
= sysctl_igmp_qrv
;
1652 spin_lock_init(&in_dev
->mc_tomb_lock
);
1655 /* Device going up */
1657 void ip_mc_up(struct in_device
*in_dev
)
1659 struct ip_mc_list
*pmc
;
1663 #ifdef CONFIG_IP_MULTICAST
1664 in_dev
->mr_qrv
= sysctl_igmp_qrv
;
1666 ip_mc_inc_group(in_dev
, IGMP_ALL_HOSTS
);
1668 for_each_pmc_rtnl(in_dev
, pmc
)
1669 igmp_group_added(pmc
);
1673 * Device is about to be destroyed: clean up.
1676 void ip_mc_destroy_dev(struct in_device
*in_dev
)
1678 struct ip_mc_list
*i
;
1682 /* Deactivate timers */
1685 while ((i
= rtnl_dereference(in_dev
->mc_list
)) != NULL
) {
1686 in_dev
->mc_list
= i
->next_rcu
;
1689 /* We've dropped the groups in ip_mc_down already */
1695 /* RTNL is locked */
1696 static struct in_device
*ip_mc_find_dev(struct net
*net
, struct ip_mreqn
*imr
)
1698 struct net_device
*dev
= NULL
;
1699 struct in_device
*idev
= NULL
;
1701 if (imr
->imr_ifindex
) {
1702 idev
= inetdev_by_index(net
, imr
->imr_ifindex
);
1705 if (imr
->imr_address
.s_addr
) {
1706 dev
= __ip_dev_find(net
, imr
->imr_address
.s_addr
, false);
1712 struct rtable
*rt
= ip_route_output(net
,
1713 imr
->imr_multiaddr
.s_addr
,
1721 imr
->imr_ifindex
= dev
->ifindex
;
1722 idev
= __in_dev_get_rtnl(dev
);
1728 * Join a socket to a group
1730 int sysctl_igmp_max_memberships __read_mostly
= IP_MAX_MEMBERSHIPS
;
1731 int sysctl_igmp_max_msf __read_mostly
= IP_MAX_MSF
;
1732 #ifdef CONFIG_IP_MULTICAST
1733 int sysctl_igmp_qrv __read_mostly
= IGMP_QUERY_ROBUSTNESS_VARIABLE
;
1736 static int ip_mc_del1_src(struct ip_mc_list
*pmc
, int sfmode
,
1739 struct ip_sf_list
*psf
, *psf_prev
;
1743 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1744 if (psf
->sf_inaddr
== *psfsrc
)
1748 if (!psf
|| psf
->sf_count
[sfmode
] == 0) {
1749 /* source filter not found, or count wrong => bug */
1752 psf
->sf_count
[sfmode
]--;
1753 if (psf
->sf_count
[sfmode
] == 0) {
1754 ip_rt_multicast_event(pmc
->interface
);
1756 if (!psf
->sf_count
[MCAST_INCLUDE
] && !psf
->sf_count
[MCAST_EXCLUDE
]) {
1757 #ifdef CONFIG_IP_MULTICAST
1758 struct in_device
*in_dev
= pmc
->interface
;
1761 /* no more filters for this source */
1763 psf_prev
->sf_next
= psf
->sf_next
;
1765 pmc
->sources
= psf
->sf_next
;
1766 #ifdef CONFIG_IP_MULTICAST
1767 if (psf
->sf_oldin
&&
1768 !IGMP_V1_SEEN(in_dev
) && !IGMP_V2_SEEN(in_dev
)) {
1769 psf
->sf_crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
1770 psf
->sf_next
= pmc
->tomb
;
1780 #ifndef CONFIG_IP_MULTICAST
1781 #define igmp_ifc_event(x) do { } while (0)
1784 static int ip_mc_del_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1785 int sfcount
, __be32
*psfsrc
, int delta
)
1787 struct ip_mc_list
*pmc
;
1794 for_each_pmc_rcu(in_dev
, pmc
) {
1795 if (*pmca
== pmc
->multiaddr
)
1799 /* MCA not found?? bug */
1803 spin_lock_bh(&pmc
->lock
);
1805 #ifdef CONFIG_IP_MULTICAST
1810 if (!pmc
->sfcount
[sfmode
])
1812 pmc
->sfcount
[sfmode
]--;
1815 for (i
= 0; i
< sfcount
; i
++) {
1816 int rv
= ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1818 changerec
|= rv
> 0;
1822 if (pmc
->sfmode
== MCAST_EXCLUDE
&&
1823 pmc
->sfcount
[MCAST_EXCLUDE
] == 0 &&
1824 pmc
->sfcount
[MCAST_INCLUDE
]) {
1825 #ifdef CONFIG_IP_MULTICAST
1826 struct ip_sf_list
*psf
;
1829 /* filter mode change */
1830 pmc
->sfmode
= MCAST_INCLUDE
;
1831 #ifdef CONFIG_IP_MULTICAST
1832 pmc
->crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
1833 in_dev
->mr_ifc_count
= pmc
->crcount
;
1834 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1835 psf
->sf_crcount
= 0;
1836 igmp_ifc_event(pmc
->interface
);
1837 } else if (sf_setstate(pmc
) || changerec
) {
1838 igmp_ifc_event(pmc
->interface
);
1842 spin_unlock_bh(&pmc
->lock
);
1847 * Add multicast single-source filter to the interface list
1849 static int ip_mc_add1_src(struct ip_mc_list
*pmc
, int sfmode
,
1852 struct ip_sf_list
*psf
, *psf_prev
;
1855 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1856 if (psf
->sf_inaddr
== *psfsrc
)
1861 psf
= kzalloc(sizeof(*psf
), GFP_ATOMIC
);
1864 psf
->sf_inaddr
= *psfsrc
;
1866 psf_prev
->sf_next
= psf
;
1870 psf
->sf_count
[sfmode
]++;
1871 if (psf
->sf_count
[sfmode
] == 1) {
1872 ip_rt_multicast_event(pmc
->interface
);
1877 #ifdef CONFIG_IP_MULTICAST
1878 static void sf_markstate(struct ip_mc_list
*pmc
)
1880 struct ip_sf_list
*psf
;
1881 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1883 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1884 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1885 psf
->sf_oldin
= mca_xcount
==
1886 psf
->sf_count
[MCAST_EXCLUDE
] &&
1887 !psf
->sf_count
[MCAST_INCLUDE
];
1889 psf
->sf_oldin
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1892 static int sf_setstate(struct ip_mc_list
*pmc
)
1894 struct ip_sf_list
*psf
, *dpsf
;
1895 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1896 int qrv
= pmc
->interface
->mr_qrv
;
1900 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1901 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1902 new_in
= mca_xcount
== psf
->sf_count
[MCAST_EXCLUDE
] &&
1903 !psf
->sf_count
[MCAST_INCLUDE
];
1905 new_in
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1907 if (!psf
->sf_oldin
) {
1908 struct ip_sf_list
*prev
= NULL
;
1910 for (dpsf
= pmc
->tomb
; dpsf
; dpsf
= dpsf
->sf_next
) {
1911 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1917 prev
->sf_next
= dpsf
->sf_next
;
1919 pmc
->tomb
= dpsf
->sf_next
;
1922 psf
->sf_crcount
= qrv
;
1925 } else if (psf
->sf_oldin
) {
1927 psf
->sf_crcount
= 0;
1929 * add or update "delete" records if an active filter
1932 for (dpsf
= pmc
->tomb
; dpsf
; dpsf
= dpsf
->sf_next
)
1933 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1936 dpsf
= kmalloc(sizeof(*dpsf
), GFP_ATOMIC
);
1940 /* pmc->lock held by callers */
1941 dpsf
->sf_next
= pmc
->tomb
;
1944 dpsf
->sf_crcount
= qrv
;
1953 * Add multicast source filter list to the interface list
1955 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1956 int sfcount
, __be32
*psfsrc
, int delta
)
1958 struct ip_mc_list
*pmc
;
1965 for_each_pmc_rcu(in_dev
, pmc
) {
1966 if (*pmca
== pmc
->multiaddr
)
1970 /* MCA not found?? bug */
1974 spin_lock_bh(&pmc
->lock
);
1977 #ifdef CONFIG_IP_MULTICAST
1980 isexclude
= pmc
->sfmode
== MCAST_EXCLUDE
;
1982 pmc
->sfcount
[sfmode
]++;
1984 for (i
= 0; i
< sfcount
; i
++) {
1985 err
= ip_mc_add1_src(pmc
, sfmode
, &psfsrc
[i
]);
1993 pmc
->sfcount
[sfmode
]--;
1994 for (j
= 0; j
< i
; j
++)
1995 (void) ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[j
]);
1996 } else if (isexclude
!= (pmc
->sfcount
[MCAST_EXCLUDE
] != 0)) {
1997 #ifdef CONFIG_IP_MULTICAST
1998 struct ip_sf_list
*psf
;
1999 in_dev
= pmc
->interface
;
2002 /* filter mode change */
2003 if (pmc
->sfcount
[MCAST_EXCLUDE
])
2004 pmc
->sfmode
= MCAST_EXCLUDE
;
2005 else if (pmc
->sfcount
[MCAST_INCLUDE
])
2006 pmc
->sfmode
= MCAST_INCLUDE
;
2007 #ifdef CONFIG_IP_MULTICAST
2008 /* else no filters; keep old mode for reports */
2010 pmc
->crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
2011 in_dev
->mr_ifc_count
= pmc
->crcount
;
2012 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
2013 psf
->sf_crcount
= 0;
2014 igmp_ifc_event(in_dev
);
2015 } else if (sf_setstate(pmc
)) {
2016 igmp_ifc_event(in_dev
);
2019 spin_unlock_bh(&pmc
->lock
);
2023 static void ip_mc_clear_src(struct ip_mc_list
*pmc
)
2025 struct ip_sf_list
*psf
, *nextpsf
;
2027 for (psf
= pmc
->tomb
; psf
; psf
= nextpsf
) {
2028 nextpsf
= psf
->sf_next
;
2032 for (psf
= pmc
->sources
; psf
; psf
= nextpsf
) {
2033 nextpsf
= psf
->sf_next
;
2036 pmc
->sources
= NULL
;
2037 pmc
->sfmode
= MCAST_EXCLUDE
;
2038 pmc
->sfcount
[MCAST_INCLUDE
] = 0;
2039 pmc
->sfcount
[MCAST_EXCLUDE
] = 1;
2042 /* Join a multicast group
2045 int ip_mc_join_group(struct sock
*sk
, struct ip_mreqn
*imr
)
2047 __be32 addr
= imr
->imr_multiaddr
.s_addr
;
2048 struct ip_mc_socklist
*iml
, *i
;
2049 struct in_device
*in_dev
;
2050 struct inet_sock
*inet
= inet_sk(sk
);
2051 struct net
*net
= sock_net(sk
);
2058 if (!ipv4_is_multicast(addr
))
2061 in_dev
= ip_mc_find_dev(net
, imr
);
2069 ifindex
= imr
->imr_ifindex
;
2070 for_each_pmc_rtnl(inet
, i
) {
2071 if (i
->multi
.imr_multiaddr
.s_addr
== addr
&&
2072 i
->multi
.imr_ifindex
== ifindex
)
2077 if (count
>= sysctl_igmp_max_memberships
)
2079 iml
= sock_kmalloc(sk
, sizeof(*iml
), GFP_KERNEL
);
2083 memcpy(&iml
->multi
, imr
, sizeof(*imr
));
2084 iml
->next_rcu
= inet
->mc_list
;
2086 iml
->sfmode
= MCAST_EXCLUDE
;
2087 rcu_assign_pointer(inet
->mc_list
, iml
);
2088 ip_mc_inc_group(in_dev
, addr
);
2093 EXPORT_SYMBOL(ip_mc_join_group
);
2095 static int ip_mc_leave_src(struct sock
*sk
, struct ip_mc_socklist
*iml
,
2096 struct in_device
*in_dev
)
2098 struct ip_sf_socklist
*psf
= rtnl_dereference(iml
->sflist
);
2102 /* any-source empty exclude case */
2103 return ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
2104 iml
->sfmode
, 0, NULL
, 0);
2106 err
= ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
2107 iml
->sfmode
, psf
->sl_count
, psf
->sl_addr
, 0);
2108 RCU_INIT_POINTER(iml
->sflist
, NULL
);
2109 /* decrease mem now to avoid the memleak warning */
2110 atomic_sub(IP_SFLSIZE(psf
->sl_max
), &sk
->sk_omem_alloc
);
2111 kfree_rcu(psf
, rcu
);
2115 int ip_mc_leave_group(struct sock
*sk
, struct ip_mreqn
*imr
)
2117 struct inet_sock
*inet
= inet_sk(sk
);
2118 struct ip_mc_socklist
*iml
;
2119 struct ip_mc_socklist __rcu
**imlp
;
2120 struct in_device
*in_dev
;
2121 struct net
*net
= sock_net(sk
);
2122 __be32 group
= imr
->imr_multiaddr
.s_addr
;
2124 int ret
= -EADDRNOTAVAIL
;
2128 in_dev
= ip_mc_find_dev(net
, imr
);
2129 if (!imr
->imr_ifindex
&& !imr
->imr_address
.s_addr
&& !in_dev
) {
2133 ifindex
= imr
->imr_ifindex
;
2134 for (imlp
= &inet
->mc_list
;
2135 (iml
= rtnl_dereference(*imlp
)) != NULL
;
2136 imlp
= &iml
->next_rcu
) {
2137 if (iml
->multi
.imr_multiaddr
.s_addr
!= group
)
2140 if (iml
->multi
.imr_ifindex
!= ifindex
)
2142 } else if (imr
->imr_address
.s_addr
&& imr
->imr_address
.s_addr
!=
2143 iml
->multi
.imr_address
.s_addr
)
2146 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2148 *imlp
= iml
->next_rcu
;
2151 ip_mc_dec_group(in_dev
, group
);
2153 /* decrease mem now to avoid the memleak warning */
2154 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
2155 kfree_rcu(iml
, rcu
);
2161 EXPORT_SYMBOL(ip_mc_leave_group
);
2163 int ip_mc_source(int add
, int omode
, struct sock
*sk
, struct
2164 ip_mreq_source
*mreqs
, int ifindex
)
2167 struct ip_mreqn imr
;
2168 __be32 addr
= mreqs
->imr_multiaddr
;
2169 struct ip_mc_socklist
*pmc
;
2170 struct in_device
*in_dev
= NULL
;
2171 struct inet_sock
*inet
= inet_sk(sk
);
2172 struct ip_sf_socklist
*psl
;
2173 struct net
*net
= sock_net(sk
);
2177 if (!ipv4_is_multicast(addr
))
2182 imr
.imr_multiaddr
.s_addr
= mreqs
->imr_multiaddr
;
2183 imr
.imr_address
.s_addr
= mreqs
->imr_interface
;
2184 imr
.imr_ifindex
= ifindex
;
2185 in_dev
= ip_mc_find_dev(net
, &imr
);
2191 err
= -EADDRNOTAVAIL
;
2193 for_each_pmc_rtnl(inet
, pmc
) {
2194 if ((pmc
->multi
.imr_multiaddr
.s_addr
==
2195 imr
.imr_multiaddr
.s_addr
) &&
2196 (pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
))
2199 if (!pmc
) { /* must have a prior join */
2203 /* if a source filter was set, must be the same mode as before */
2205 if (pmc
->sfmode
!= omode
) {
2209 } else if (pmc
->sfmode
!= omode
) {
2210 /* allow mode switches for empty-set filters */
2211 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 0, NULL
, 0);
2212 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, pmc
->sfmode
, 0,
2214 pmc
->sfmode
= omode
;
2217 psl
= rtnl_dereference(pmc
->sflist
);
2220 goto done
; /* err = -EADDRNOTAVAIL */
2222 for (i
= 0; i
< psl
->sl_count
; i
++) {
2223 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2228 if (rv
) /* source not found */
2229 goto done
; /* err = -EADDRNOTAVAIL */
2231 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2232 if (psl
->sl_count
== 1 && omode
== MCAST_INCLUDE
) {
2237 /* update the interface filter */
2238 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2239 &mreqs
->imr_sourceaddr
, 1);
2241 for (j
= i
+1; j
< psl
->sl_count
; j
++)
2242 psl
->sl_addr
[j
-1] = psl
->sl_addr
[j
];
2247 /* else, add a new source to the filter */
2249 if (psl
&& psl
->sl_count
>= sysctl_igmp_max_msf
) {
2253 if (!psl
|| psl
->sl_count
== psl
->sl_max
) {
2254 struct ip_sf_socklist
*newpsl
;
2255 int count
= IP_SFBLOCK
;
2258 count
+= psl
->sl_max
;
2259 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(count
), GFP_KERNEL
);
2264 newpsl
->sl_max
= count
;
2265 newpsl
->sl_count
= count
- IP_SFBLOCK
;
2267 for (i
= 0; i
< psl
->sl_count
; i
++)
2268 newpsl
->sl_addr
[i
] = psl
->sl_addr
[i
];
2269 /* decrease mem now to avoid the memleak warning */
2270 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2271 kfree_rcu(psl
, rcu
);
2273 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2276 rv
= 1; /* > 0 for insert logic below if sl_count is 0 */
2277 for (i
= 0; i
< psl
->sl_count
; i
++) {
2278 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2283 if (rv
== 0) /* address already there is an error */
2285 for (j
= psl
->sl_count
-1; j
>= i
; j
--)
2286 psl
->sl_addr
[j
+1] = psl
->sl_addr
[j
];
2287 psl
->sl_addr
[i
] = mreqs
->imr_sourceaddr
;
2290 /* update the interface list */
2291 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2292 &mreqs
->imr_sourceaddr
, 1);
2295 err
= ip_mc_leave_group(sk
, &imr
);
2299 int ip_mc_msfilter(struct sock
*sk
, struct ip_msfilter
*msf
, int ifindex
)
2302 struct ip_mreqn imr
;
2303 __be32 addr
= msf
->imsf_multiaddr
;
2304 struct ip_mc_socklist
*pmc
;
2305 struct in_device
*in_dev
;
2306 struct inet_sock
*inet
= inet_sk(sk
);
2307 struct ip_sf_socklist
*newpsl
, *psl
;
2308 struct net
*net
= sock_net(sk
);
2311 if (!ipv4_is_multicast(addr
))
2313 if (msf
->imsf_fmode
!= MCAST_INCLUDE
&&
2314 msf
->imsf_fmode
!= MCAST_EXCLUDE
)
2319 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2320 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2321 imr
.imr_ifindex
= ifindex
;
2322 in_dev
= ip_mc_find_dev(net
, &imr
);
2329 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2330 if (msf
->imsf_fmode
== MCAST_INCLUDE
&& msf
->imsf_numsrc
== 0) {
2335 for_each_pmc_rtnl(inet
, pmc
) {
2336 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2337 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2340 if (!pmc
) { /* must have a prior join */
2344 if (msf
->imsf_numsrc
) {
2345 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(msf
->imsf_numsrc
),
2351 newpsl
->sl_max
= newpsl
->sl_count
= msf
->imsf_numsrc
;
2352 memcpy(newpsl
->sl_addr
, msf
->imsf_slist
,
2353 msf
->imsf_numsrc
* sizeof(msf
->imsf_slist
[0]));
2354 err
= ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2355 msf
->imsf_fmode
, newpsl
->sl_count
, newpsl
->sl_addr
, 0);
2357 sock_kfree_s(sk
, newpsl
, IP_SFLSIZE(newpsl
->sl_max
));
2362 (void) ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2363 msf
->imsf_fmode
, 0, NULL
, 0);
2365 psl
= rtnl_dereference(pmc
->sflist
);
2367 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2368 psl
->sl_count
, psl
->sl_addr
, 0);
2369 /* decrease mem now to avoid the memleak warning */
2370 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2371 kfree_rcu(psl
, rcu
);
2373 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2375 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2376 pmc
->sfmode
= msf
->imsf_fmode
;
2380 err
= ip_mc_leave_group(sk
, &imr
);
2384 int ip_mc_msfget(struct sock
*sk
, struct ip_msfilter
*msf
,
2385 struct ip_msfilter __user
*optval
, int __user
*optlen
)
2387 int err
, len
, count
, copycount
;
2388 struct ip_mreqn imr
;
2389 __be32 addr
= msf
->imsf_multiaddr
;
2390 struct ip_mc_socklist
*pmc
;
2391 struct in_device
*in_dev
;
2392 struct inet_sock
*inet
= inet_sk(sk
);
2393 struct ip_sf_socklist
*psl
;
2394 struct net
*net
= sock_net(sk
);
2398 if (!ipv4_is_multicast(addr
))
2401 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2402 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2403 imr
.imr_ifindex
= 0;
2404 in_dev
= ip_mc_find_dev(net
, &imr
);
2410 err
= -EADDRNOTAVAIL
;
2412 for_each_pmc_rtnl(inet
, pmc
) {
2413 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2414 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2417 if (!pmc
) /* must have a prior join */
2419 msf
->imsf_fmode
= pmc
->sfmode
;
2420 psl
= rtnl_dereference(pmc
->sflist
);
2425 count
= psl
->sl_count
;
2427 copycount
= count
< msf
->imsf_numsrc
? count
: msf
->imsf_numsrc
;
2428 len
= copycount
* sizeof(psl
->sl_addr
[0]);
2429 msf
->imsf_numsrc
= count
;
2430 if (put_user(IP_MSFILTER_SIZE(copycount
), optlen
) ||
2431 copy_to_user(optval
, msf
, IP_MSFILTER_SIZE(0))) {
2435 copy_to_user(&optval
->imsf_slist
[0], psl
->sl_addr
, len
))
2442 int ip_mc_gsfget(struct sock
*sk
, struct group_filter
*gsf
,
2443 struct group_filter __user
*optval
, int __user
*optlen
)
2445 int err
, i
, count
, copycount
;
2446 struct sockaddr_in
*psin
;
2448 struct ip_mc_socklist
*pmc
;
2449 struct inet_sock
*inet
= inet_sk(sk
);
2450 struct ip_sf_socklist
*psl
;
2454 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
2455 if (psin
->sin_family
!= AF_INET
)
2457 addr
= psin
->sin_addr
.s_addr
;
2458 if (!ipv4_is_multicast(addr
))
2461 err
= -EADDRNOTAVAIL
;
2463 for_each_pmc_rtnl(inet
, pmc
) {
2464 if (pmc
->multi
.imr_multiaddr
.s_addr
== addr
&&
2465 pmc
->multi
.imr_ifindex
== gsf
->gf_interface
)
2468 if (!pmc
) /* must have a prior join */
2470 gsf
->gf_fmode
= pmc
->sfmode
;
2471 psl
= rtnl_dereference(pmc
->sflist
);
2472 count
= psl
? psl
->sl_count
: 0;
2473 copycount
= count
< gsf
->gf_numsrc
? count
: gsf
->gf_numsrc
;
2474 gsf
->gf_numsrc
= count
;
2475 if (put_user(GROUP_FILTER_SIZE(copycount
), optlen
) ||
2476 copy_to_user(optval
, gsf
, GROUP_FILTER_SIZE(0))) {
2479 for (i
= 0; i
< copycount
; i
++) {
2480 struct sockaddr_storage ss
;
2482 psin
= (struct sockaddr_in
*)&ss
;
2483 memset(&ss
, 0, sizeof(ss
));
2484 psin
->sin_family
= AF_INET
;
2485 psin
->sin_addr
.s_addr
= psl
->sl_addr
[i
];
2486 if (copy_to_user(&optval
->gf_slist
[i
], &ss
, sizeof(ss
)))
2495 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2497 int ip_mc_sf_allow(struct sock
*sk
, __be32 loc_addr
, __be32 rmt_addr
, int dif
)
2499 struct inet_sock
*inet
= inet_sk(sk
);
2500 struct ip_mc_socklist
*pmc
;
2501 struct ip_sf_socklist
*psl
;
2506 if (!ipv4_is_multicast(loc_addr
))
2510 for_each_pmc_rcu(inet
, pmc
) {
2511 if (pmc
->multi
.imr_multiaddr
.s_addr
== loc_addr
&&
2512 pmc
->multi
.imr_ifindex
== dif
)
2518 psl
= rcu_dereference(pmc
->sflist
);
2519 ret
= (pmc
->sfmode
== MCAST_EXCLUDE
);
2523 for (i
= 0; i
< psl
->sl_count
; i
++) {
2524 if (psl
->sl_addr
[i
] == rmt_addr
)
2528 if (pmc
->sfmode
== MCAST_INCLUDE
&& i
>= psl
->sl_count
)
2530 if (pmc
->sfmode
== MCAST_EXCLUDE
&& i
< psl
->sl_count
)
2540 * A socket is closing.
2543 void ip_mc_drop_socket(struct sock
*sk
)
2545 struct inet_sock
*inet
= inet_sk(sk
);
2546 struct ip_mc_socklist
*iml
;
2547 struct net
*net
= sock_net(sk
);
2553 while ((iml
= rtnl_dereference(inet
->mc_list
)) != NULL
) {
2554 struct in_device
*in_dev
;
2556 inet
->mc_list
= iml
->next_rcu
;
2557 in_dev
= inetdev_by_index(net
, iml
->multi
.imr_ifindex
);
2558 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2560 ip_mc_dec_group(in_dev
, iml
->multi
.imr_multiaddr
.s_addr
);
2561 /* decrease mem now to avoid the memleak warning */
2562 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
2563 kfree_rcu(iml
, rcu
);
2568 /* called with rcu_read_lock() */
2569 int ip_check_mc_rcu(struct in_device
*in_dev
, __be32 mc_addr
, __be32 src_addr
, u8 proto
)
2571 struct ip_mc_list
*im
;
2572 struct ip_mc_list __rcu
**mc_hash
;
2573 struct ip_sf_list
*psf
;
2576 mc_hash
= rcu_dereference(in_dev
->mc_hash
);
2578 u32 hash
= hash_32((__force u32
)mc_addr
, MC_HASH_SZ_LOG
);
2580 for (im
= rcu_dereference(mc_hash
[hash
]);
2582 im
= rcu_dereference(im
->next_hash
)) {
2583 if (im
->multiaddr
== mc_addr
)
2587 for_each_pmc_rcu(in_dev
, im
) {
2588 if (im
->multiaddr
== mc_addr
)
2592 if (im
&& proto
== IPPROTO_IGMP
) {
2596 for (psf
= im
->sources
; psf
; psf
= psf
->sf_next
) {
2597 if (psf
->sf_inaddr
== src_addr
)
2601 rv
= psf
->sf_count
[MCAST_INCLUDE
] ||
2602 psf
->sf_count
[MCAST_EXCLUDE
] !=
2603 im
->sfcount
[MCAST_EXCLUDE
];
2605 rv
= im
->sfcount
[MCAST_EXCLUDE
] != 0;
2607 rv
= 1; /* unspecified source; tentatively allow */
2612 #if defined(CONFIG_PROC_FS)
2613 struct igmp_mc_iter_state
{
2614 struct seq_net_private p
;
2615 struct net_device
*dev
;
2616 struct in_device
*in_dev
;
2619 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2621 static inline struct ip_mc_list
*igmp_mc_get_first(struct seq_file
*seq
)
2623 struct net
*net
= seq_file_net(seq
);
2624 struct ip_mc_list
*im
= NULL
;
2625 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2627 state
->in_dev
= NULL
;
2628 for_each_netdev_rcu(net
, state
->dev
) {
2629 struct in_device
*in_dev
;
2631 in_dev
= __in_dev_get_rcu(state
->dev
);
2634 im
= rcu_dereference(in_dev
->mc_list
);
2636 state
->in_dev
= in_dev
;
2643 static struct ip_mc_list
*igmp_mc_get_next(struct seq_file
*seq
, struct ip_mc_list
*im
)
2645 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2647 im
= rcu_dereference(im
->next_rcu
);
2649 state
->dev
= next_net_device_rcu(state
->dev
);
2651 state
->in_dev
= NULL
;
2654 state
->in_dev
= __in_dev_get_rcu(state
->dev
);
2657 im
= rcu_dereference(state
->in_dev
->mc_list
);
2662 static struct ip_mc_list
*igmp_mc_get_idx(struct seq_file
*seq
, loff_t pos
)
2664 struct ip_mc_list
*im
= igmp_mc_get_first(seq
);
2666 while (pos
&& (im
= igmp_mc_get_next(seq
, im
)) != NULL
)
2668 return pos
? NULL
: im
;
2671 static void *igmp_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2675 return *pos
? igmp_mc_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2678 static void *igmp_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2680 struct ip_mc_list
*im
;
2681 if (v
== SEQ_START_TOKEN
)
2682 im
= igmp_mc_get_first(seq
);
2684 im
= igmp_mc_get_next(seq
, v
);
2689 static void igmp_mc_seq_stop(struct seq_file
*seq
, void *v
)
2692 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2694 state
->in_dev
= NULL
;
2699 static int igmp_mc_seq_show(struct seq_file
*seq
, void *v
)
2701 if (v
== SEQ_START_TOKEN
)
2703 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2705 struct ip_mc_list
*im
= (struct ip_mc_list
*)v
;
2706 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2710 #ifdef CONFIG_IP_MULTICAST
2711 querier
= IGMP_V1_SEEN(state
->in_dev
) ? "V1" :
2712 IGMP_V2_SEEN(state
->in_dev
) ? "V2" :
2718 if (rcu_access_pointer(state
->in_dev
->mc_list
) == im
) {
2719 seq_printf(seq
, "%d\t%-10s: %5d %7s\n",
2720 state
->dev
->ifindex
, state
->dev
->name
, state
->in_dev
->mc_count
, querier
);
2723 delta
= im
->timer
.expires
- jiffies
;
2725 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2726 im
->multiaddr
, im
->users
,
2728 im
->tm_running
? jiffies_delta_to_clock_t(delta
) : 0,
2734 static const struct seq_operations igmp_mc_seq_ops
= {
2735 .start
= igmp_mc_seq_start
,
2736 .next
= igmp_mc_seq_next
,
2737 .stop
= igmp_mc_seq_stop
,
2738 .show
= igmp_mc_seq_show
,
2741 static int igmp_mc_seq_open(struct inode
*inode
, struct file
*file
)
2743 return seq_open_net(inode
, file
, &igmp_mc_seq_ops
,
2744 sizeof(struct igmp_mc_iter_state
));
2747 static const struct file_operations igmp_mc_seq_fops
= {
2748 .owner
= THIS_MODULE
,
2749 .open
= igmp_mc_seq_open
,
2751 .llseek
= seq_lseek
,
2752 .release
= seq_release_net
,
2755 struct igmp_mcf_iter_state
{
2756 struct seq_net_private p
;
2757 struct net_device
*dev
;
2758 struct in_device
*idev
;
2759 struct ip_mc_list
*im
;
2762 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2764 static inline struct ip_sf_list
*igmp_mcf_get_first(struct seq_file
*seq
)
2766 struct net
*net
= seq_file_net(seq
);
2767 struct ip_sf_list
*psf
= NULL
;
2768 struct ip_mc_list
*im
= NULL
;
2769 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2773 for_each_netdev_rcu(net
, state
->dev
) {
2774 struct in_device
*idev
;
2775 idev
= __in_dev_get_rcu(state
->dev
);
2776 if (unlikely(!idev
))
2778 im
= rcu_dereference(idev
->mc_list
);
2780 spin_lock_bh(&im
->lock
);
2787 spin_unlock_bh(&im
->lock
);
2793 static struct ip_sf_list
*igmp_mcf_get_next(struct seq_file
*seq
, struct ip_sf_list
*psf
)
2795 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2799 spin_unlock_bh(&state
->im
->lock
);
2800 state
->im
= state
->im
->next
;
2801 while (!state
->im
) {
2802 state
->dev
= next_net_device_rcu(state
->dev
);
2807 state
->idev
= __in_dev_get_rcu(state
->dev
);
2810 state
->im
= rcu_dereference(state
->idev
->mc_list
);
2814 spin_lock_bh(&state
->im
->lock
);
2815 psf
= state
->im
->sources
;
2821 static struct ip_sf_list
*igmp_mcf_get_idx(struct seq_file
*seq
, loff_t pos
)
2823 struct ip_sf_list
*psf
= igmp_mcf_get_first(seq
);
2825 while (pos
&& (psf
= igmp_mcf_get_next(seq
, psf
)) != NULL
)
2827 return pos
? NULL
: psf
;
2830 static void *igmp_mcf_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2834 return *pos
? igmp_mcf_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2837 static void *igmp_mcf_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2839 struct ip_sf_list
*psf
;
2840 if (v
== SEQ_START_TOKEN
)
2841 psf
= igmp_mcf_get_first(seq
);
2843 psf
= igmp_mcf_get_next(seq
, v
);
2848 static void igmp_mcf_seq_stop(struct seq_file
*seq
, void *v
)
2851 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2852 if (likely(state
->im
)) {
2853 spin_unlock_bh(&state
->im
->lock
);
2861 static int igmp_mcf_seq_show(struct seq_file
*seq
, void *v
)
2863 struct ip_sf_list
*psf
= (struct ip_sf_list
*)v
;
2864 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2866 if (v
== SEQ_START_TOKEN
) {
2867 seq_puts(seq
, "Idx Device MCA SRC INC EXC\n");
2871 "0x%08x %6lu %6lu\n",
2872 state
->dev
->ifindex
, state
->dev
->name
,
2873 ntohl(state
->im
->multiaddr
),
2874 ntohl(psf
->sf_inaddr
),
2875 psf
->sf_count
[MCAST_INCLUDE
],
2876 psf
->sf_count
[MCAST_EXCLUDE
]);
2881 static const struct seq_operations igmp_mcf_seq_ops
= {
2882 .start
= igmp_mcf_seq_start
,
2883 .next
= igmp_mcf_seq_next
,
2884 .stop
= igmp_mcf_seq_stop
,
2885 .show
= igmp_mcf_seq_show
,
2888 static int igmp_mcf_seq_open(struct inode
*inode
, struct file
*file
)
2890 return seq_open_net(inode
, file
, &igmp_mcf_seq_ops
,
2891 sizeof(struct igmp_mcf_iter_state
));
2894 static const struct file_operations igmp_mcf_seq_fops
= {
2895 .owner
= THIS_MODULE
,
2896 .open
= igmp_mcf_seq_open
,
2898 .llseek
= seq_lseek
,
2899 .release
= seq_release_net
,
2902 static int __net_init
igmp_net_init(struct net
*net
)
2904 struct proc_dir_entry
*pde
;
2907 pde
= proc_create("igmp", S_IRUGO
, net
->proc_net
, &igmp_mc_seq_fops
);
2910 pde
= proc_create("mcfilter", S_IRUGO
, net
->proc_net
,
2911 &igmp_mcf_seq_fops
);
2914 err
= inet_ctl_sock_create(&net
->ipv4
.mc_autojoin_sk
, AF_INET
,
2915 SOCK_DGRAM
, 0, net
);
2917 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
2925 remove_proc_entry("mcfilter", net
->proc_net
);
2927 remove_proc_entry("igmp", net
->proc_net
);
2932 static void __net_exit
igmp_net_exit(struct net
*net
)
2934 remove_proc_entry("mcfilter", net
->proc_net
);
2935 remove_proc_entry("igmp", net
->proc_net
);
2936 inet_ctl_sock_destroy(net
->ipv4
.mc_autojoin_sk
);
2939 static struct pernet_operations igmp_net_ops
= {
2940 .init
= igmp_net_init
,
2941 .exit
= igmp_net_exit
,
2945 static int igmp_netdev_event(struct notifier_block
*this,
2946 unsigned long event
, void *ptr
)
2948 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
2949 struct in_device
*in_dev
;
2952 case NETDEV_RESEND_IGMP
:
2953 in_dev
= __in_dev_get_rtnl(dev
);
2955 ip_mc_rejoin_groups(in_dev
);
2963 static struct notifier_block igmp_notifier
= {
2964 .notifier_call
= igmp_netdev_event
,
2967 int __init
igmp_mc_init(void)
2969 #if defined(CONFIG_PROC_FS)
2972 err
= register_pernet_subsys(&igmp_net_ops
);
2975 err
= register_netdevice_notifier(&igmp_notifier
);
2977 goto reg_notif_fail
;
2981 unregister_pernet_subsys(&igmp_net_ops
);
2984 return register_netdevice_notifier(&igmp_notifier
);