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
;
228 unsigned long exp
= jiffies
+ tv
+ 2;
230 if (in_dev
->mr_gq_running
&&
231 time_after_eq(exp
, (in_dev
->mr_gq_timer
).expires
))
234 in_dev
->mr_gq_running
= 1;
235 if (!mod_timer(&in_dev
->mr_gq_timer
, exp
))
239 static void igmp_ifc_start_timer(struct in_device
*in_dev
, int delay
)
241 int tv
= prandom_u32() % delay
;
243 if (!mod_timer(&in_dev
->mr_ifc_timer
, jiffies
+tv
+2))
247 static void igmp_mod_timer(struct ip_mc_list
*im
, int max_delay
)
249 spin_lock_bh(&im
->lock
);
250 im
->unsolicit_count
= 0;
251 if (del_timer(&im
->timer
)) {
252 if ((long)(im
->timer
.expires
-jiffies
) < max_delay
) {
253 add_timer(&im
->timer
);
255 spin_unlock_bh(&im
->lock
);
258 atomic_dec(&im
->refcnt
);
260 igmp_start_timer(im
, max_delay
);
261 spin_unlock_bh(&im
->lock
);
266 * Send an IGMP report.
269 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
272 static int is_in(struct ip_mc_list
*pmc
, struct ip_sf_list
*psf
, int type
,
273 int gdeleted
, int sdeleted
)
276 case IGMPV3_MODE_IS_INCLUDE
:
277 case IGMPV3_MODE_IS_EXCLUDE
:
278 if (gdeleted
|| sdeleted
)
280 if (!(pmc
->gsquery
&& !psf
->sf_gsresp
)) {
281 if (pmc
->sfmode
== MCAST_INCLUDE
)
283 /* don't include if this source is excluded
286 if (psf
->sf_count
[MCAST_INCLUDE
])
287 return type
== IGMPV3_MODE_IS_INCLUDE
;
288 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
289 psf
->sf_count
[MCAST_EXCLUDE
];
292 case IGMPV3_CHANGE_TO_INCLUDE
:
293 if (gdeleted
|| sdeleted
)
295 return psf
->sf_count
[MCAST_INCLUDE
] != 0;
296 case IGMPV3_CHANGE_TO_EXCLUDE
:
297 if (gdeleted
|| sdeleted
)
299 if (pmc
->sfcount
[MCAST_EXCLUDE
] == 0 ||
300 psf
->sf_count
[MCAST_INCLUDE
])
302 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
303 psf
->sf_count
[MCAST_EXCLUDE
];
304 case IGMPV3_ALLOW_NEW_SOURCES
:
305 if (gdeleted
|| !psf
->sf_crcount
)
307 return (pmc
->sfmode
== MCAST_INCLUDE
) ^ sdeleted
;
308 case IGMPV3_BLOCK_OLD_SOURCES
:
309 if (pmc
->sfmode
== MCAST_INCLUDE
)
310 return gdeleted
|| (psf
->sf_crcount
&& sdeleted
);
311 return psf
->sf_crcount
&& !gdeleted
&& !sdeleted
;
317 igmp_scount(struct ip_mc_list
*pmc
, int type
, int gdeleted
, int sdeleted
)
319 struct ip_sf_list
*psf
;
322 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
323 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
))
330 static struct sk_buff
*igmpv3_newpack(struct net_device
*dev
, unsigned int mtu
)
335 struct igmpv3_report
*pig
;
336 struct net
*net
= dev_net(dev
);
338 int hlen
= LL_RESERVED_SPACE(dev
);
339 int tlen
= dev
->needed_tailroom
;
340 unsigned int size
= mtu
;
343 skb
= alloc_skb(size
+ hlen
+ tlen
,
344 GFP_ATOMIC
| __GFP_NOWARN
);
351 skb
->priority
= TC_PRIO_CONTROL
;
353 rt
= ip_route_output_ports(net
, &fl4
, NULL
, IGMPV3_ALL_MCR
, 0,
355 IPPROTO_IGMP
, 0, dev
->ifindex
);
361 skb_dst_set(skb
, &rt
->dst
);
364 skb_reserve(skb
, hlen
);
365 skb_tailroom_reserve(skb
, mtu
, tlen
);
367 skb_reset_network_header(skb
);
369 skb_put(skb
, sizeof(struct iphdr
) + 4);
372 pip
->ihl
= (sizeof(struct iphdr
)+4)>>2;
374 pip
->frag_off
= htons(IP_DF
);
376 pip
->daddr
= fl4
.daddr
;
377 pip
->saddr
= fl4
.saddr
;
378 pip
->protocol
= IPPROTO_IGMP
;
379 pip
->tot_len
= 0; /* filled in later */
380 ip_select_ident(net
, skb
, NULL
);
381 ((u8
*)&pip
[1])[0] = IPOPT_RA
;
382 ((u8
*)&pip
[1])[1] = 4;
383 ((u8
*)&pip
[1])[2] = 0;
384 ((u8
*)&pip
[1])[3] = 0;
386 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
) + 4;
387 skb_put(skb
, sizeof(*pig
));
388 pig
= igmpv3_report_hdr(skb
);
389 pig
->type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
397 static int igmpv3_sendpack(struct sk_buff
*skb
)
399 struct igmphdr
*pig
= igmp_hdr(skb
);
400 const int igmplen
= skb_tail_pointer(skb
) - skb_transport_header(skb
);
402 pig
->csum
= ip_compute_csum(igmp_hdr(skb
), igmplen
);
404 return ip_local_out(dev_net(skb_dst(skb
)->dev
), skb
->sk
, skb
);
407 static int grec_size(struct ip_mc_list
*pmc
, int type
, int gdel
, int sdel
)
409 return sizeof(struct igmpv3_grec
) + 4*igmp_scount(pmc
, type
, gdel
, sdel
);
412 static struct sk_buff
*add_grhead(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
413 int type
, struct igmpv3_grec
**ppgr
)
415 struct net_device
*dev
= pmc
->interface
->dev
;
416 struct igmpv3_report
*pih
;
417 struct igmpv3_grec
*pgr
;
420 skb
= igmpv3_newpack(dev
, dev
->mtu
);
423 pgr
= (struct igmpv3_grec
*)skb_put(skb
, sizeof(struct igmpv3_grec
));
424 pgr
->grec_type
= type
;
425 pgr
->grec_auxwords
= 0;
427 pgr
->grec_mca
= pmc
->multiaddr
;
428 pih
= igmpv3_report_hdr(skb
);
429 pih
->ngrec
= htons(ntohs(pih
->ngrec
)+1);
434 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
436 static struct sk_buff
*add_grec(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
437 int type
, int gdeleted
, int sdeleted
)
439 struct net_device
*dev
= pmc
->interface
->dev
;
440 struct igmpv3_report
*pih
;
441 struct igmpv3_grec
*pgr
= NULL
;
442 struct ip_sf_list
*psf
, *psf_next
, *psf_prev
, **psf_list
;
443 int scount
, stotal
, first
, isquery
, truncate
;
445 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
447 if (ipv4_is_local_multicast(pmc
->multiaddr
) && !sysctl_igmp_llm_reports
)
450 isquery
= type
== IGMPV3_MODE_IS_INCLUDE
||
451 type
== IGMPV3_MODE_IS_EXCLUDE
;
452 truncate
= type
== IGMPV3_MODE_IS_EXCLUDE
||
453 type
== IGMPV3_CHANGE_TO_EXCLUDE
;
457 psf_list
= sdeleted
? &pmc
->tomb
: &pmc
->sources
;
462 pih
= skb
? igmpv3_report_hdr(skb
) : NULL
;
464 /* EX and TO_EX get a fresh packet, if needed */
466 if (pih
&& pih
->ngrec
&&
467 AVAILABLE(skb
) < grec_size(pmc
, type
, gdeleted
, sdeleted
)) {
469 igmpv3_sendpack(skb
);
470 skb
= igmpv3_newpack(dev
, dev
->mtu
);
475 for (psf
= *psf_list
; psf
; psf
= psf_next
) {
478 psf_next
= psf
->sf_next
;
480 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
)) {
485 /* clear marks on query responses */
489 if (AVAILABLE(skb
) < sizeof(__be32
) +
490 first
*sizeof(struct igmpv3_grec
)) {
491 if (truncate
&& !first
)
492 break; /* truncate these */
494 pgr
->grec_nsrcs
= htons(scount
);
496 igmpv3_sendpack(skb
);
497 skb
= igmpv3_newpack(dev
, dev
->mtu
);
502 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
507 psrc
= (__be32
*)skb_put(skb
, sizeof(__be32
));
508 *psrc
= psf
->sf_inaddr
;
510 if ((type
== IGMPV3_ALLOW_NEW_SOURCES
||
511 type
== IGMPV3_BLOCK_OLD_SOURCES
) && psf
->sf_crcount
) {
513 if ((sdeleted
|| gdeleted
) && psf
->sf_crcount
== 0) {
515 psf_prev
->sf_next
= psf
->sf_next
;
517 *psf_list
= psf
->sf_next
;
527 if (type
== IGMPV3_ALLOW_NEW_SOURCES
||
528 type
== IGMPV3_BLOCK_OLD_SOURCES
)
530 if (pmc
->crcount
|| isquery
) {
531 /* make sure we have room for group header */
532 if (skb
&& AVAILABLE(skb
) < sizeof(struct igmpv3_grec
)) {
533 igmpv3_sendpack(skb
);
534 skb
= NULL
; /* add_grhead will get a new one */
536 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
540 pgr
->grec_nsrcs
= htons(scount
);
543 pmc
->gsquery
= 0; /* clear query state on report */
547 static int igmpv3_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
)
549 struct sk_buff
*skb
= NULL
;
554 for_each_pmc_rcu(in_dev
, pmc
) {
555 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
557 if (ipv4_is_local_multicast(pmc
->multiaddr
) &&
558 !sysctl_igmp_llm_reports
)
560 spin_lock_bh(&pmc
->lock
);
561 if (pmc
->sfcount
[MCAST_EXCLUDE
])
562 type
= IGMPV3_MODE_IS_EXCLUDE
;
564 type
= IGMPV3_MODE_IS_INCLUDE
;
565 skb
= add_grec(skb
, pmc
, type
, 0, 0);
566 spin_unlock_bh(&pmc
->lock
);
570 spin_lock_bh(&pmc
->lock
);
571 if (pmc
->sfcount
[MCAST_EXCLUDE
])
572 type
= IGMPV3_MODE_IS_EXCLUDE
;
574 type
= IGMPV3_MODE_IS_INCLUDE
;
575 skb
= add_grec(skb
, pmc
, type
, 0, 0);
576 spin_unlock_bh(&pmc
->lock
);
580 return igmpv3_sendpack(skb
);
584 * remove zero-count source records from a source filter list
586 static void igmpv3_clear_zeros(struct ip_sf_list
**ppsf
)
588 struct ip_sf_list
*psf_prev
, *psf_next
, *psf
;
591 for (psf
= *ppsf
; psf
; psf
= psf_next
) {
592 psf_next
= psf
->sf_next
;
593 if (psf
->sf_crcount
== 0) {
595 psf_prev
->sf_next
= psf
->sf_next
;
597 *ppsf
= psf
->sf_next
;
604 static void igmpv3_send_cr(struct in_device
*in_dev
)
606 struct ip_mc_list
*pmc
, *pmc_prev
, *pmc_next
;
607 struct sk_buff
*skb
= NULL
;
611 spin_lock_bh(&in_dev
->mc_tomb_lock
);
615 for (pmc
= in_dev
->mc_tomb
; pmc
; pmc
= pmc_next
) {
616 pmc_next
= pmc
->next
;
617 if (pmc
->sfmode
== MCAST_INCLUDE
) {
618 type
= IGMPV3_BLOCK_OLD_SOURCES
;
619 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
620 skb
= add_grec(skb
, pmc
, type
, 1, 0);
621 skb
= add_grec(skb
, pmc
, dtype
, 1, 1);
624 if (pmc
->sfmode
== MCAST_EXCLUDE
) {
625 type
= IGMPV3_CHANGE_TO_INCLUDE
;
626 skb
= add_grec(skb
, pmc
, type
, 1, 0);
629 if (pmc
->crcount
== 0) {
630 igmpv3_clear_zeros(&pmc
->tomb
);
631 igmpv3_clear_zeros(&pmc
->sources
);
634 if (pmc
->crcount
== 0 && !pmc
->tomb
&& !pmc
->sources
) {
636 pmc_prev
->next
= pmc_next
;
638 in_dev
->mc_tomb
= pmc_next
;
639 in_dev_put(pmc
->interface
);
644 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
647 for_each_pmc_rcu(in_dev
, pmc
) {
648 spin_lock_bh(&pmc
->lock
);
649 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
650 type
= IGMPV3_BLOCK_OLD_SOURCES
;
651 dtype
= IGMPV3_ALLOW_NEW_SOURCES
;
653 type
= IGMPV3_ALLOW_NEW_SOURCES
;
654 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
656 skb
= add_grec(skb
, pmc
, type
, 0, 0);
657 skb
= add_grec(skb
, pmc
, dtype
, 0, 1); /* deleted sources */
659 /* filter mode changes */
661 if (pmc
->sfmode
== MCAST_EXCLUDE
)
662 type
= IGMPV3_CHANGE_TO_EXCLUDE
;
664 type
= IGMPV3_CHANGE_TO_INCLUDE
;
665 skb
= add_grec(skb
, pmc
, type
, 0, 0);
668 spin_unlock_bh(&pmc
->lock
);
674 (void) igmpv3_sendpack(skb
);
677 static int igmp_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
,
684 struct net_device
*dev
= in_dev
->dev
;
685 struct net
*net
= dev_net(dev
);
686 __be32 group
= pmc
? pmc
->multiaddr
: 0;
691 if (type
== IGMPV3_HOST_MEMBERSHIP_REPORT
)
692 return igmpv3_send_report(in_dev
, pmc
);
694 if (ipv4_is_local_multicast(group
) && !sysctl_igmp_llm_reports
)
697 if (type
== IGMP_HOST_LEAVE_MESSAGE
)
698 dst
= IGMP_ALL_ROUTER
;
702 rt
= ip_route_output_ports(net
, &fl4
, NULL
, dst
, 0,
704 IPPROTO_IGMP
, 0, dev
->ifindex
);
708 hlen
= LL_RESERVED_SPACE(dev
);
709 tlen
= dev
->needed_tailroom
;
710 skb
= alloc_skb(IGMP_SIZE
+ hlen
+ tlen
, GFP_ATOMIC
);
715 skb
->priority
= TC_PRIO_CONTROL
;
717 skb_dst_set(skb
, &rt
->dst
);
719 skb_reserve(skb
, hlen
);
721 skb_reset_network_header(skb
);
723 skb_put(skb
, sizeof(struct iphdr
) + 4);
726 iph
->ihl
= (sizeof(struct iphdr
)+4)>>2;
728 iph
->frag_off
= htons(IP_DF
);
731 iph
->saddr
= fl4
.saddr
;
732 iph
->protocol
= IPPROTO_IGMP
;
733 ip_select_ident(net
, skb
, NULL
);
734 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
735 ((u8
*)&iph
[1])[1] = 4;
736 ((u8
*)&iph
[1])[2] = 0;
737 ((u8
*)&iph
[1])[3] = 0;
739 ih
= (struct igmphdr
*)skb_put(skb
, sizeof(struct igmphdr
));
744 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
746 return ip_local_out(net
, skb
->sk
, skb
);
749 static void igmp_gq_timer_expire(unsigned long data
)
751 struct in_device
*in_dev
= (struct in_device
*)data
;
753 in_dev
->mr_gq_running
= 0;
754 igmpv3_send_report(in_dev
, NULL
);
758 static void igmp_ifc_timer_expire(unsigned long data
)
760 struct in_device
*in_dev
= (struct in_device
*)data
;
762 igmpv3_send_cr(in_dev
);
763 if (in_dev
->mr_ifc_count
) {
764 in_dev
->mr_ifc_count
--;
765 igmp_ifc_start_timer(in_dev
,
766 unsolicited_report_interval(in_dev
));
771 static void igmp_ifc_event(struct in_device
*in_dev
)
773 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
775 in_dev
->mr_ifc_count
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
776 igmp_ifc_start_timer(in_dev
, 1);
780 static void igmp_timer_expire(unsigned long data
)
782 struct ip_mc_list
*im
= (struct ip_mc_list
*)data
;
783 struct in_device
*in_dev
= im
->interface
;
785 spin_lock(&im
->lock
);
788 if (im
->unsolicit_count
) {
789 im
->unsolicit_count
--;
790 igmp_start_timer(im
, unsolicited_report_interval(in_dev
));
793 spin_unlock(&im
->lock
);
795 if (IGMP_V1_SEEN(in_dev
))
796 igmp_send_report(in_dev
, im
, IGMP_HOST_MEMBERSHIP_REPORT
);
797 else if (IGMP_V2_SEEN(in_dev
))
798 igmp_send_report(in_dev
, im
, IGMPV2_HOST_MEMBERSHIP_REPORT
);
800 igmp_send_report(in_dev
, im
, IGMPV3_HOST_MEMBERSHIP_REPORT
);
805 /* mark EXCLUDE-mode sources */
806 static int igmp_xmarksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
808 struct ip_sf_list
*psf
;
812 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
815 for (i
= 0; i
< nsrcs
; i
++) {
816 /* skip inactive filters */
817 if (psf
->sf_count
[MCAST_INCLUDE
] ||
818 pmc
->sfcount
[MCAST_EXCLUDE
] !=
819 psf
->sf_count
[MCAST_EXCLUDE
])
821 if (srcs
[i
] == psf
->sf_inaddr
) {
828 if (scount
== nsrcs
) /* all sources excluded */
833 static int igmp_marksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
835 struct ip_sf_list
*psf
;
838 if (pmc
->sfmode
== MCAST_EXCLUDE
)
839 return igmp_xmarksources(pmc
, nsrcs
, srcs
);
841 /* mark INCLUDE-mode sources */
843 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
846 for (i
= 0; i
< nsrcs
; i
++)
847 if (srcs
[i
] == psf
->sf_inaddr
) {
861 /* return true if packet was dropped */
862 static bool igmp_heard_report(struct in_device
*in_dev
, __be32 group
)
864 struct ip_mc_list
*im
;
866 /* Timers are only set for non-local groups */
868 if (group
== IGMP_ALL_HOSTS
)
870 if (ipv4_is_local_multicast(group
) && !sysctl_igmp_llm_reports
)
874 for_each_pmc_rcu(in_dev
, im
) {
875 if (im
->multiaddr
== group
) {
884 /* return true if packet was dropped */
885 static bool igmp_heard_query(struct in_device
*in_dev
, struct sk_buff
*skb
,
888 struct igmphdr
*ih
= igmp_hdr(skb
);
889 struct igmpv3_query
*ih3
= igmpv3_query_hdr(skb
);
890 struct ip_mc_list
*im
;
891 __be32 group
= ih
->group
;
898 /* Alas, old v1 router presents here. */
900 max_delay
= IGMP_QUERY_RESPONSE_INTERVAL
;
901 in_dev
->mr_v1_seen
= jiffies
+
902 IGMP_V1_ROUTER_PRESENT_TIMEOUT
;
905 /* v2 router present */
906 max_delay
= ih
->code
*(HZ
/IGMP_TIMER_SCALE
);
907 in_dev
->mr_v2_seen
= jiffies
+
908 IGMP_V2_ROUTER_PRESENT_TIMEOUT
;
910 /* cancel the interface change timer */
911 in_dev
->mr_ifc_count
= 0;
912 if (del_timer(&in_dev
->mr_ifc_timer
))
913 __in_dev_put(in_dev
);
914 /* clear deleted report items */
915 igmpv3_clear_delrec(in_dev
);
916 } else if (len
< 12) {
917 return true; /* ignore bogus packet; freed by caller */
918 } else if (IGMP_V1_SEEN(in_dev
)) {
919 /* This is a v3 query with v1 queriers present */
920 max_delay
= IGMP_QUERY_RESPONSE_INTERVAL
;
922 } else if (IGMP_V2_SEEN(in_dev
)) {
923 /* this is a v3 query with v2 queriers present;
924 * Interpretation of the max_delay code is problematic here.
925 * A real v2 host would use ih_code directly, while v3 has a
926 * different encoding. We use the v3 encoding as more likely
927 * to be intended in a v3 query.
929 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
931 max_delay
= 1; /* can't mod w/ 0 */
933 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)))
936 ih3
= igmpv3_query_hdr(skb
);
938 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)
939 + ntohs(ih3
->nsrcs
)*sizeof(__be32
)))
941 ih3
= igmpv3_query_hdr(skb
);
944 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
946 max_delay
= 1; /* can't mod w/ 0 */
947 in_dev
->mr_maxdelay
= max_delay
;
949 in_dev
->mr_qrv
= ih3
->qrv
;
950 if (!group
) { /* general query */
952 return true; /* no sources allowed */
953 igmp_gq_start_timer(in_dev
);
956 /* mark sources to include, if group & source-specific */
957 mark
= ih3
->nsrcs
!= 0;
961 * - Start the timers in all of our membership records
962 * that the query applies to for the interface on
963 * which the query arrived excl. those that belong
964 * to a "local" group (224.0.0.X)
965 * - For timers already running check if they need to
967 * - Use the igmp->igmp_code field as the maximum
971 for_each_pmc_rcu(in_dev
, im
) {
974 if (group
&& group
!= im
->multiaddr
)
976 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
978 if (ipv4_is_local_multicast(im
->multiaddr
) &&
979 !sysctl_igmp_llm_reports
)
981 spin_lock_bh(&im
->lock
);
983 im
->gsquery
= im
->gsquery
&& mark
;
986 changed
= !im
->gsquery
||
987 igmp_marksources(im
, ntohs(ih3
->nsrcs
), ih3
->srcs
);
988 spin_unlock_bh(&im
->lock
);
990 igmp_mod_timer(im
, max_delay
);
996 /* called in rcu_read_lock() section */
997 int igmp_rcv(struct sk_buff
*skb
)
999 /* This basically follows the spec line by line -- see RFC1112 */
1001 struct in_device
*in_dev
= __in_dev_get_rcu(skb
->dev
);
1003 bool dropped
= true;
1008 if (!pskb_may_pull(skb
, sizeof(struct igmphdr
)))
1011 if (skb_checksum_simple_validate(skb
))
1016 case IGMP_HOST_MEMBERSHIP_QUERY
:
1017 dropped
= igmp_heard_query(in_dev
, skb
, len
);
1019 case IGMP_HOST_MEMBERSHIP_REPORT
:
1020 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1021 /* Is it our report looped back? */
1022 if (rt_is_output_route(skb_rtable(skb
)))
1024 /* don't rely on MC router hearing unicast reports */
1025 if (skb
->pkt_type
== PACKET_MULTICAST
||
1026 skb
->pkt_type
== PACKET_BROADCAST
)
1027 dropped
= igmp_heard_report(in_dev
, ih
->group
);
1030 #ifdef CONFIG_IP_PIMSM_V1
1031 return pim_rcv_v1(skb
);
1033 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1036 case IGMP_HOST_LEAVE_MESSAGE
:
1038 case IGMP_MTRACE_RESP
:
1056 * Add a filter to a device
1059 static void ip_mc_filter_add(struct in_device
*in_dev
, __be32 addr
)
1061 char buf
[MAX_ADDR_LEN
];
1062 struct net_device
*dev
= in_dev
->dev
;
1064 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1065 We will get multicast token leakage, when IFF_MULTICAST
1066 is changed. This check should be done in ndo_set_rx_mode
1067 routine. Something sort of:
1068 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1071 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1072 dev_mc_add(dev
, buf
);
1076 * Remove a filter from a device
1079 static void ip_mc_filter_del(struct in_device
*in_dev
, __be32 addr
)
1081 char buf
[MAX_ADDR_LEN
];
1082 struct net_device
*dev
= in_dev
->dev
;
1084 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1085 dev_mc_del(dev
, buf
);
1088 #ifdef CONFIG_IP_MULTICAST
1090 * deleted ip_mc_list manipulation
1092 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
)
1094 struct ip_mc_list
*pmc
;
1096 /* this is an "ip_mc_list" for convenience; only the fields below
1097 * are actually used. In particular, the refcnt and users are not
1098 * used for management of the delete list. Using the same structure
1099 * for deleted items allows change reports to use common code with
1100 * non-deleted or query-response MCA's.
1102 pmc
= kzalloc(sizeof(*pmc
), GFP_KERNEL
);
1105 spin_lock_bh(&im
->lock
);
1106 pmc
->interface
= im
->interface
;
1107 in_dev_hold(in_dev
);
1108 pmc
->multiaddr
= im
->multiaddr
;
1109 pmc
->crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
1110 pmc
->sfmode
= im
->sfmode
;
1111 if (pmc
->sfmode
== MCAST_INCLUDE
) {
1112 struct ip_sf_list
*psf
;
1114 pmc
->tomb
= im
->tomb
;
1115 pmc
->sources
= im
->sources
;
1116 im
->tomb
= im
->sources
= NULL
;
1117 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1118 psf
->sf_crcount
= pmc
->crcount
;
1120 spin_unlock_bh(&im
->lock
);
1122 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1123 pmc
->next
= in_dev
->mc_tomb
;
1124 in_dev
->mc_tomb
= pmc
;
1125 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1128 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
)
1130 struct ip_mc_list
*pmc
, *pmc_prev
;
1131 struct ip_sf_list
*psf
, *psf_next
;
1133 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1135 for (pmc
= in_dev
->mc_tomb
; pmc
; pmc
= pmc
->next
) {
1136 if (pmc
->multiaddr
== multiaddr
)
1142 pmc_prev
->next
= pmc
->next
;
1144 in_dev
->mc_tomb
= pmc
->next
;
1146 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1148 for (psf
= pmc
->tomb
; psf
; psf
= psf_next
) {
1149 psf_next
= psf
->sf_next
;
1152 in_dev_put(pmc
->interface
);
1157 static void igmpv3_clear_delrec(struct in_device
*in_dev
)
1159 struct ip_mc_list
*pmc
, *nextpmc
;
1161 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1162 pmc
= in_dev
->mc_tomb
;
1163 in_dev
->mc_tomb
= NULL
;
1164 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1166 for (; pmc
; pmc
= nextpmc
) {
1167 nextpmc
= pmc
->next
;
1168 ip_mc_clear_src(pmc
);
1169 in_dev_put(pmc
->interface
);
1172 /* clear dead sources, too */
1174 for_each_pmc_rcu(in_dev
, pmc
) {
1175 struct ip_sf_list
*psf
, *psf_next
;
1177 spin_lock_bh(&pmc
->lock
);
1180 spin_unlock_bh(&pmc
->lock
);
1181 for (; psf
; psf
= psf_next
) {
1182 psf_next
= psf
->sf_next
;
1190 static void igmp_group_dropped(struct ip_mc_list
*im
)
1192 struct in_device
*in_dev
= im
->interface
;
1193 #ifdef CONFIG_IP_MULTICAST
1199 ip_mc_filter_del(in_dev
, im
->multiaddr
);
1202 #ifdef CONFIG_IP_MULTICAST
1203 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1205 if (ipv4_is_local_multicast(im
->multiaddr
) && !sysctl_igmp_llm_reports
)
1208 reporter
= im
->reporter
;
1209 igmp_stop_timer(im
);
1211 if (!in_dev
->dead
) {
1212 if (IGMP_V1_SEEN(in_dev
))
1214 if (IGMP_V2_SEEN(in_dev
)) {
1216 igmp_send_report(in_dev
, im
, IGMP_HOST_LEAVE_MESSAGE
);
1220 igmpv3_add_delrec(in_dev
, im
);
1222 igmp_ifc_event(in_dev
);
1227 static void igmp_group_added(struct ip_mc_list
*im
)
1229 struct in_device
*in_dev
= im
->interface
;
1231 if (im
->loaded
== 0) {
1233 ip_mc_filter_add(in_dev
, im
->multiaddr
);
1236 #ifdef CONFIG_IP_MULTICAST
1237 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1239 if (ipv4_is_local_multicast(im
->multiaddr
) && !sysctl_igmp_llm_reports
)
1244 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
)) {
1245 spin_lock_bh(&im
->lock
);
1246 igmp_start_timer(im
, IGMP_INITIAL_REPORT_DELAY
);
1247 spin_unlock_bh(&im
->lock
);
1252 im
->crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
1253 igmp_ifc_event(in_dev
);
1259 * Multicast list managers
1262 static u32
ip_mc_hash(const struct ip_mc_list
*im
)
1264 return hash_32((__force u32
)im
->multiaddr
, MC_HASH_SZ_LOG
);
1267 static void ip_mc_hash_add(struct in_device
*in_dev
,
1268 struct ip_mc_list
*im
)
1270 struct ip_mc_list __rcu
**mc_hash
;
1273 mc_hash
= rtnl_dereference(in_dev
->mc_hash
);
1275 hash
= ip_mc_hash(im
);
1276 im
->next_hash
= mc_hash
[hash
];
1277 rcu_assign_pointer(mc_hash
[hash
], im
);
1281 /* do not use a hash table for small number of items */
1282 if (in_dev
->mc_count
< 4)
1285 mc_hash
= kzalloc(sizeof(struct ip_mc_list
*) << MC_HASH_SZ_LOG
,
1290 for_each_pmc_rtnl(in_dev
, im
) {
1291 hash
= ip_mc_hash(im
);
1292 im
->next_hash
= mc_hash
[hash
];
1293 RCU_INIT_POINTER(mc_hash
[hash
], im
);
1296 rcu_assign_pointer(in_dev
->mc_hash
, mc_hash
);
1299 static void ip_mc_hash_remove(struct in_device
*in_dev
,
1300 struct ip_mc_list
*im
)
1302 struct ip_mc_list __rcu
**mc_hash
= rtnl_dereference(in_dev
->mc_hash
);
1303 struct ip_mc_list
*aux
;
1307 mc_hash
+= ip_mc_hash(im
);
1308 while ((aux
= rtnl_dereference(*mc_hash
)) != im
)
1309 mc_hash
= &aux
->next_hash
;
1310 *mc_hash
= im
->next_hash
;
1315 * A socket has joined a multicast group on device dev.
1318 void ip_mc_inc_group(struct in_device
*in_dev
, __be32 addr
)
1320 struct ip_mc_list
*im
;
1324 for_each_pmc_rtnl(in_dev
, im
) {
1325 if (im
->multiaddr
== addr
) {
1327 ip_mc_add_src(in_dev
, &addr
, MCAST_EXCLUDE
, 0, NULL
, 0);
1332 im
= kzalloc(sizeof(*im
), GFP_KERNEL
);
1337 im
->interface
= in_dev
;
1338 in_dev_hold(in_dev
);
1339 im
->multiaddr
= addr
;
1340 /* initial mode is (EX, empty) */
1341 im
->sfmode
= MCAST_EXCLUDE
;
1342 im
->sfcount
[MCAST_EXCLUDE
] = 1;
1343 atomic_set(&im
->refcnt
, 1);
1344 spin_lock_init(&im
->lock
);
1345 #ifdef CONFIG_IP_MULTICAST
1346 setup_timer(&im
->timer
, igmp_timer_expire
, (unsigned long)im
);
1347 im
->unsolicit_count
= sysctl_igmp_qrv
;
1350 im
->next_rcu
= in_dev
->mc_list
;
1352 rcu_assign_pointer(in_dev
->mc_list
, im
);
1354 ip_mc_hash_add(in_dev
, im
);
1356 #ifdef CONFIG_IP_MULTICAST
1357 igmpv3_del_delrec(in_dev
, im
->multiaddr
);
1359 igmp_group_added(im
);
1361 ip_rt_multicast_event(in_dev
);
1365 EXPORT_SYMBOL(ip_mc_inc_group
);
1367 static int ip_mc_check_iphdr(struct sk_buff
*skb
)
1369 const struct iphdr
*iph
;
1371 unsigned int offset
= skb_network_offset(skb
) + sizeof(*iph
);
1373 if (!pskb_may_pull(skb
, offset
))
1378 if (iph
->version
!= 4 || ip_hdrlen(skb
) < sizeof(*iph
))
1381 offset
+= ip_hdrlen(skb
) - sizeof(*iph
);
1383 if (!pskb_may_pull(skb
, offset
))
1388 if (unlikely(ip_fast_csum((u8
*)iph
, iph
->ihl
)))
1391 len
= skb_network_offset(skb
) + ntohs(iph
->tot_len
);
1392 if (skb
->len
< len
|| len
< offset
)
1395 skb_set_transport_header(skb
, offset
);
1400 static int ip_mc_check_igmp_reportv3(struct sk_buff
*skb
)
1402 unsigned int len
= skb_transport_offset(skb
);
1404 len
+= sizeof(struct igmpv3_report
);
1406 return pskb_may_pull(skb
, len
) ? 0 : -EINVAL
;
1409 static int ip_mc_check_igmp_query(struct sk_buff
*skb
)
1411 unsigned int len
= skb_transport_offset(skb
);
1413 len
+= sizeof(struct igmphdr
);
1418 if (skb
->len
!= len
) {
1420 len
+= sizeof(struct igmpv3_query
) - sizeof(struct igmphdr
);
1421 if (skb
->len
< len
|| !pskb_may_pull(skb
, len
))
1425 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1426 * all-systems destination addresses (224.0.0.1) for general queries
1428 if (!igmp_hdr(skb
)->group
&&
1429 ip_hdr(skb
)->daddr
!= htonl(INADDR_ALLHOSTS_GROUP
))
1435 static int ip_mc_check_igmp_msg(struct sk_buff
*skb
)
1437 switch (igmp_hdr(skb
)->type
) {
1438 case IGMP_HOST_LEAVE_MESSAGE
:
1439 case IGMP_HOST_MEMBERSHIP_REPORT
:
1440 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1443 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1444 return ip_mc_check_igmp_reportv3(skb
);
1445 case IGMP_HOST_MEMBERSHIP_QUERY
:
1446 return ip_mc_check_igmp_query(skb
);
1452 static inline __sum16
ip_mc_validate_checksum(struct sk_buff
*skb
)
1454 return skb_checksum_simple_validate(skb
);
1457 static int __ip_mc_check_igmp(struct sk_buff
*skb
, struct sk_buff
**skb_trimmed
)
1460 struct sk_buff
*skb_chk
;
1461 unsigned int transport_len
;
1462 unsigned int len
= skb_transport_offset(skb
) + sizeof(struct igmphdr
);
1465 transport_len
= ntohs(ip_hdr(skb
)->tot_len
) - ip_hdrlen(skb
);
1467 skb_chk
= skb_checksum_trimmed(skb
, transport_len
,
1468 ip_mc_validate_checksum
);
1472 if (!pskb_may_pull(skb_chk
, len
))
1475 ret
= ip_mc_check_igmp_msg(skb_chk
);
1480 *skb_trimmed
= skb_chk
;
1481 /* free now unneeded clone */
1482 else if (skb_chk
!= skb
)
1488 if (ret
&& skb_chk
&& skb_chk
!= skb
)
1495 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1496 * @skb: the skb to validate
1497 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1499 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1500 * skb transport header accordingly and returns zero.
1502 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1504 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1505 * -ENOMEM: A memory allocation failure happened.
1507 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1508 * to NULL): After parsing an IGMP packet successfully it will point to
1509 * an skb which has its tail aligned to the IP packet end. This might
1510 * either be the originally provided skb or a trimmed, cloned version if
1511 * the skb frame had data beyond the IP packet. A cloned skb allows us
1512 * to leave the original skb and its full frame unchanged (which might be
1513 * desirable for layer 2 frame jugglers).
1515 * Caller needs to set the skb network header and free any returned skb if it
1516 * differs from the provided skb.
1518 int ip_mc_check_igmp(struct sk_buff
*skb
, struct sk_buff
**skb_trimmed
)
1520 int ret
= ip_mc_check_iphdr(skb
);
1525 if (ip_hdr(skb
)->protocol
!= IPPROTO_IGMP
)
1528 return __ip_mc_check_igmp(skb
, skb_trimmed
);
1530 EXPORT_SYMBOL(ip_mc_check_igmp
);
1533 * Resend IGMP JOIN report; used by netdev notifier.
1535 static void ip_mc_rejoin_groups(struct in_device
*in_dev
)
1537 #ifdef CONFIG_IP_MULTICAST
1538 struct ip_mc_list
*im
;
1543 for_each_pmc_rtnl(in_dev
, im
) {
1544 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1546 if (ipv4_is_local_multicast(im
->multiaddr
) &&
1547 !sysctl_igmp_llm_reports
)
1550 /* a failover is happening and switches
1551 * must be notified immediately
1553 if (IGMP_V1_SEEN(in_dev
))
1554 type
= IGMP_HOST_MEMBERSHIP_REPORT
;
1555 else if (IGMP_V2_SEEN(in_dev
))
1556 type
= IGMPV2_HOST_MEMBERSHIP_REPORT
;
1558 type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
1559 igmp_send_report(in_dev
, im
, type
);
1565 * A socket has left a multicast group on device dev
1568 void ip_mc_dec_group(struct in_device
*in_dev
, __be32 addr
)
1570 struct ip_mc_list
*i
;
1571 struct ip_mc_list __rcu
**ip
;
1575 for (ip
= &in_dev
->mc_list
;
1576 (i
= rtnl_dereference(*ip
)) != NULL
;
1577 ip
= &i
->next_rcu
) {
1578 if (i
->multiaddr
== addr
) {
1579 if (--i
->users
== 0) {
1580 ip_mc_hash_remove(in_dev
, i
);
1583 igmp_group_dropped(i
);
1587 ip_rt_multicast_event(in_dev
);
1596 EXPORT_SYMBOL(ip_mc_dec_group
);
1598 /* Device changing type */
1600 void ip_mc_unmap(struct in_device
*in_dev
)
1602 struct ip_mc_list
*pmc
;
1606 for_each_pmc_rtnl(in_dev
, pmc
)
1607 igmp_group_dropped(pmc
);
1610 void ip_mc_remap(struct in_device
*in_dev
)
1612 struct ip_mc_list
*pmc
;
1616 for_each_pmc_rtnl(in_dev
, pmc
)
1617 igmp_group_added(pmc
);
1620 /* Device going down */
1622 void ip_mc_down(struct in_device
*in_dev
)
1624 struct ip_mc_list
*pmc
;
1628 for_each_pmc_rtnl(in_dev
, pmc
)
1629 igmp_group_dropped(pmc
);
1631 #ifdef CONFIG_IP_MULTICAST
1632 in_dev
->mr_ifc_count
= 0;
1633 if (del_timer(&in_dev
->mr_ifc_timer
))
1634 __in_dev_put(in_dev
);
1635 in_dev
->mr_gq_running
= 0;
1636 if (del_timer(&in_dev
->mr_gq_timer
))
1637 __in_dev_put(in_dev
);
1638 igmpv3_clear_delrec(in_dev
);
1641 ip_mc_dec_group(in_dev
, IGMP_ALL_HOSTS
);
1644 void ip_mc_init_dev(struct in_device
*in_dev
)
1648 #ifdef CONFIG_IP_MULTICAST
1649 setup_timer(&in_dev
->mr_gq_timer
, igmp_gq_timer_expire
,
1650 (unsigned long)in_dev
);
1651 setup_timer(&in_dev
->mr_ifc_timer
, igmp_ifc_timer_expire
,
1652 (unsigned long)in_dev
);
1653 in_dev
->mr_qrv
= sysctl_igmp_qrv
;
1656 spin_lock_init(&in_dev
->mc_tomb_lock
);
1659 /* Device going up */
1661 void ip_mc_up(struct in_device
*in_dev
)
1663 struct ip_mc_list
*pmc
;
1667 #ifdef CONFIG_IP_MULTICAST
1668 in_dev
->mr_qrv
= sysctl_igmp_qrv
;
1670 ip_mc_inc_group(in_dev
, IGMP_ALL_HOSTS
);
1672 for_each_pmc_rtnl(in_dev
, pmc
)
1673 igmp_group_added(pmc
);
1677 * Device is about to be destroyed: clean up.
1680 void ip_mc_destroy_dev(struct in_device
*in_dev
)
1682 struct ip_mc_list
*i
;
1686 /* Deactivate timers */
1689 while ((i
= rtnl_dereference(in_dev
->mc_list
)) != NULL
) {
1690 in_dev
->mc_list
= i
->next_rcu
;
1693 /* We've dropped the groups in ip_mc_down already */
1699 /* RTNL is locked */
1700 static struct in_device
*ip_mc_find_dev(struct net
*net
, struct ip_mreqn
*imr
)
1702 struct net_device
*dev
= NULL
;
1703 struct in_device
*idev
= NULL
;
1705 if (imr
->imr_ifindex
) {
1706 idev
= inetdev_by_index(net
, imr
->imr_ifindex
);
1709 if (imr
->imr_address
.s_addr
) {
1710 dev
= __ip_dev_find(net
, imr
->imr_address
.s_addr
, false);
1716 struct rtable
*rt
= ip_route_output(net
,
1717 imr
->imr_multiaddr
.s_addr
,
1725 imr
->imr_ifindex
= dev
->ifindex
;
1726 idev
= __in_dev_get_rtnl(dev
);
1732 * Join a socket to a group
1734 int sysctl_igmp_max_memberships __read_mostly
= IP_MAX_MEMBERSHIPS
;
1735 int sysctl_igmp_max_msf __read_mostly
= IP_MAX_MSF
;
1736 #ifdef CONFIG_IP_MULTICAST
1737 int sysctl_igmp_qrv __read_mostly
= IGMP_QUERY_ROBUSTNESS_VARIABLE
;
1740 static int ip_mc_del1_src(struct ip_mc_list
*pmc
, int sfmode
,
1743 struct ip_sf_list
*psf
, *psf_prev
;
1747 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1748 if (psf
->sf_inaddr
== *psfsrc
)
1752 if (!psf
|| psf
->sf_count
[sfmode
] == 0) {
1753 /* source filter not found, or count wrong => bug */
1756 psf
->sf_count
[sfmode
]--;
1757 if (psf
->sf_count
[sfmode
] == 0) {
1758 ip_rt_multicast_event(pmc
->interface
);
1760 if (!psf
->sf_count
[MCAST_INCLUDE
] && !psf
->sf_count
[MCAST_EXCLUDE
]) {
1761 #ifdef CONFIG_IP_MULTICAST
1762 struct in_device
*in_dev
= pmc
->interface
;
1765 /* no more filters for this source */
1767 psf_prev
->sf_next
= psf
->sf_next
;
1769 pmc
->sources
= psf
->sf_next
;
1770 #ifdef CONFIG_IP_MULTICAST
1771 if (psf
->sf_oldin
&&
1772 !IGMP_V1_SEEN(in_dev
) && !IGMP_V2_SEEN(in_dev
)) {
1773 psf
->sf_crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
1774 psf
->sf_next
= pmc
->tomb
;
1784 #ifndef CONFIG_IP_MULTICAST
1785 #define igmp_ifc_event(x) do { } while (0)
1788 static int ip_mc_del_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1789 int sfcount
, __be32
*psfsrc
, int delta
)
1791 struct ip_mc_list
*pmc
;
1798 for_each_pmc_rcu(in_dev
, pmc
) {
1799 if (*pmca
== pmc
->multiaddr
)
1803 /* MCA not found?? bug */
1807 spin_lock_bh(&pmc
->lock
);
1809 #ifdef CONFIG_IP_MULTICAST
1814 if (!pmc
->sfcount
[sfmode
])
1816 pmc
->sfcount
[sfmode
]--;
1819 for (i
= 0; i
< sfcount
; i
++) {
1820 int rv
= ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1822 changerec
|= rv
> 0;
1826 if (pmc
->sfmode
== MCAST_EXCLUDE
&&
1827 pmc
->sfcount
[MCAST_EXCLUDE
] == 0 &&
1828 pmc
->sfcount
[MCAST_INCLUDE
]) {
1829 #ifdef CONFIG_IP_MULTICAST
1830 struct ip_sf_list
*psf
;
1833 /* filter mode change */
1834 pmc
->sfmode
= MCAST_INCLUDE
;
1835 #ifdef CONFIG_IP_MULTICAST
1836 pmc
->crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
1837 in_dev
->mr_ifc_count
= pmc
->crcount
;
1838 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1839 psf
->sf_crcount
= 0;
1840 igmp_ifc_event(pmc
->interface
);
1841 } else if (sf_setstate(pmc
) || changerec
) {
1842 igmp_ifc_event(pmc
->interface
);
1846 spin_unlock_bh(&pmc
->lock
);
1851 * Add multicast single-source filter to the interface list
1853 static int ip_mc_add1_src(struct ip_mc_list
*pmc
, int sfmode
,
1856 struct ip_sf_list
*psf
, *psf_prev
;
1859 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1860 if (psf
->sf_inaddr
== *psfsrc
)
1865 psf
= kzalloc(sizeof(*psf
), GFP_ATOMIC
);
1868 psf
->sf_inaddr
= *psfsrc
;
1870 psf_prev
->sf_next
= psf
;
1874 psf
->sf_count
[sfmode
]++;
1875 if (psf
->sf_count
[sfmode
] == 1) {
1876 ip_rt_multicast_event(pmc
->interface
);
1881 #ifdef CONFIG_IP_MULTICAST
1882 static void sf_markstate(struct ip_mc_list
*pmc
)
1884 struct ip_sf_list
*psf
;
1885 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1887 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1888 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1889 psf
->sf_oldin
= mca_xcount
==
1890 psf
->sf_count
[MCAST_EXCLUDE
] &&
1891 !psf
->sf_count
[MCAST_INCLUDE
];
1893 psf
->sf_oldin
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1896 static int sf_setstate(struct ip_mc_list
*pmc
)
1898 struct ip_sf_list
*psf
, *dpsf
;
1899 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1900 int qrv
= pmc
->interface
->mr_qrv
;
1904 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1905 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1906 new_in
= mca_xcount
== psf
->sf_count
[MCAST_EXCLUDE
] &&
1907 !psf
->sf_count
[MCAST_INCLUDE
];
1909 new_in
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1911 if (!psf
->sf_oldin
) {
1912 struct ip_sf_list
*prev
= NULL
;
1914 for (dpsf
= pmc
->tomb
; dpsf
; dpsf
= dpsf
->sf_next
) {
1915 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1921 prev
->sf_next
= dpsf
->sf_next
;
1923 pmc
->tomb
= dpsf
->sf_next
;
1926 psf
->sf_crcount
= qrv
;
1929 } else if (psf
->sf_oldin
) {
1931 psf
->sf_crcount
= 0;
1933 * add or update "delete" records if an active filter
1936 for (dpsf
= pmc
->tomb
; dpsf
; dpsf
= dpsf
->sf_next
)
1937 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1940 dpsf
= kmalloc(sizeof(*dpsf
), GFP_ATOMIC
);
1944 /* pmc->lock held by callers */
1945 dpsf
->sf_next
= pmc
->tomb
;
1948 dpsf
->sf_crcount
= qrv
;
1957 * Add multicast source filter list to the interface list
1959 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1960 int sfcount
, __be32
*psfsrc
, int delta
)
1962 struct ip_mc_list
*pmc
;
1969 for_each_pmc_rcu(in_dev
, pmc
) {
1970 if (*pmca
== pmc
->multiaddr
)
1974 /* MCA not found?? bug */
1978 spin_lock_bh(&pmc
->lock
);
1981 #ifdef CONFIG_IP_MULTICAST
1984 isexclude
= pmc
->sfmode
== MCAST_EXCLUDE
;
1986 pmc
->sfcount
[sfmode
]++;
1988 for (i
= 0; i
< sfcount
; i
++) {
1989 err
= ip_mc_add1_src(pmc
, sfmode
, &psfsrc
[i
]);
1997 pmc
->sfcount
[sfmode
]--;
1998 for (j
= 0; j
< i
; j
++)
1999 (void) ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[j
]);
2000 } else if (isexclude
!= (pmc
->sfcount
[MCAST_EXCLUDE
] != 0)) {
2001 #ifdef CONFIG_IP_MULTICAST
2002 struct ip_sf_list
*psf
;
2003 in_dev
= pmc
->interface
;
2006 /* filter mode change */
2007 if (pmc
->sfcount
[MCAST_EXCLUDE
])
2008 pmc
->sfmode
= MCAST_EXCLUDE
;
2009 else if (pmc
->sfcount
[MCAST_INCLUDE
])
2010 pmc
->sfmode
= MCAST_INCLUDE
;
2011 #ifdef CONFIG_IP_MULTICAST
2012 /* else no filters; keep old mode for reports */
2014 pmc
->crcount
= in_dev
->mr_qrv
?: sysctl_igmp_qrv
;
2015 in_dev
->mr_ifc_count
= pmc
->crcount
;
2016 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
2017 psf
->sf_crcount
= 0;
2018 igmp_ifc_event(in_dev
);
2019 } else if (sf_setstate(pmc
)) {
2020 igmp_ifc_event(in_dev
);
2023 spin_unlock_bh(&pmc
->lock
);
2027 static void ip_mc_clear_src(struct ip_mc_list
*pmc
)
2029 struct ip_sf_list
*psf
, *nextpsf
;
2031 for (psf
= pmc
->tomb
; psf
; psf
= nextpsf
) {
2032 nextpsf
= psf
->sf_next
;
2036 for (psf
= pmc
->sources
; psf
; psf
= nextpsf
) {
2037 nextpsf
= psf
->sf_next
;
2040 pmc
->sources
= NULL
;
2041 pmc
->sfmode
= MCAST_EXCLUDE
;
2042 pmc
->sfcount
[MCAST_INCLUDE
] = 0;
2043 pmc
->sfcount
[MCAST_EXCLUDE
] = 1;
2046 /* Join a multicast group
2049 int ip_mc_join_group(struct sock
*sk
, struct ip_mreqn
*imr
)
2051 __be32 addr
= imr
->imr_multiaddr
.s_addr
;
2052 struct ip_mc_socklist
*iml
, *i
;
2053 struct in_device
*in_dev
;
2054 struct inet_sock
*inet
= inet_sk(sk
);
2055 struct net
*net
= sock_net(sk
);
2062 if (!ipv4_is_multicast(addr
))
2065 in_dev
= ip_mc_find_dev(net
, imr
);
2073 ifindex
= imr
->imr_ifindex
;
2074 for_each_pmc_rtnl(inet
, i
) {
2075 if (i
->multi
.imr_multiaddr
.s_addr
== addr
&&
2076 i
->multi
.imr_ifindex
== ifindex
)
2081 if (count
>= sysctl_igmp_max_memberships
)
2083 iml
= sock_kmalloc(sk
, sizeof(*iml
), GFP_KERNEL
);
2087 memcpy(&iml
->multi
, imr
, sizeof(*imr
));
2088 iml
->next_rcu
= inet
->mc_list
;
2090 iml
->sfmode
= MCAST_EXCLUDE
;
2091 rcu_assign_pointer(inet
->mc_list
, iml
);
2092 ip_mc_inc_group(in_dev
, addr
);
2097 EXPORT_SYMBOL(ip_mc_join_group
);
2099 static int ip_mc_leave_src(struct sock
*sk
, struct ip_mc_socklist
*iml
,
2100 struct in_device
*in_dev
)
2102 struct ip_sf_socklist
*psf
= rtnl_dereference(iml
->sflist
);
2106 /* any-source empty exclude case */
2107 return ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
2108 iml
->sfmode
, 0, NULL
, 0);
2110 err
= ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
2111 iml
->sfmode
, psf
->sl_count
, psf
->sl_addr
, 0);
2112 RCU_INIT_POINTER(iml
->sflist
, NULL
);
2113 /* decrease mem now to avoid the memleak warning */
2114 atomic_sub(IP_SFLSIZE(psf
->sl_max
), &sk
->sk_omem_alloc
);
2115 kfree_rcu(psf
, rcu
);
2119 int ip_mc_leave_group(struct sock
*sk
, struct ip_mreqn
*imr
)
2121 struct inet_sock
*inet
= inet_sk(sk
);
2122 struct ip_mc_socklist
*iml
;
2123 struct ip_mc_socklist __rcu
**imlp
;
2124 struct in_device
*in_dev
;
2125 struct net
*net
= sock_net(sk
);
2126 __be32 group
= imr
->imr_multiaddr
.s_addr
;
2128 int ret
= -EADDRNOTAVAIL
;
2132 in_dev
= ip_mc_find_dev(net
, imr
);
2133 if (!imr
->imr_ifindex
&& !imr
->imr_address
.s_addr
&& !in_dev
) {
2137 ifindex
= imr
->imr_ifindex
;
2138 for (imlp
= &inet
->mc_list
;
2139 (iml
= rtnl_dereference(*imlp
)) != NULL
;
2140 imlp
= &iml
->next_rcu
) {
2141 if (iml
->multi
.imr_multiaddr
.s_addr
!= group
)
2144 if (iml
->multi
.imr_ifindex
!= ifindex
)
2146 } else if (imr
->imr_address
.s_addr
&& imr
->imr_address
.s_addr
!=
2147 iml
->multi
.imr_address
.s_addr
)
2150 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2152 *imlp
= iml
->next_rcu
;
2155 ip_mc_dec_group(in_dev
, group
);
2157 /* decrease mem now to avoid the memleak warning */
2158 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
2159 kfree_rcu(iml
, rcu
);
2165 EXPORT_SYMBOL(ip_mc_leave_group
);
2167 int ip_mc_source(int add
, int omode
, struct sock
*sk
, struct
2168 ip_mreq_source
*mreqs
, int ifindex
)
2171 struct ip_mreqn imr
;
2172 __be32 addr
= mreqs
->imr_multiaddr
;
2173 struct ip_mc_socklist
*pmc
;
2174 struct in_device
*in_dev
= NULL
;
2175 struct inet_sock
*inet
= inet_sk(sk
);
2176 struct ip_sf_socklist
*psl
;
2177 struct net
*net
= sock_net(sk
);
2181 if (!ipv4_is_multicast(addr
))
2186 imr
.imr_multiaddr
.s_addr
= mreqs
->imr_multiaddr
;
2187 imr
.imr_address
.s_addr
= mreqs
->imr_interface
;
2188 imr
.imr_ifindex
= ifindex
;
2189 in_dev
= ip_mc_find_dev(net
, &imr
);
2195 err
= -EADDRNOTAVAIL
;
2197 for_each_pmc_rtnl(inet
, pmc
) {
2198 if ((pmc
->multi
.imr_multiaddr
.s_addr
==
2199 imr
.imr_multiaddr
.s_addr
) &&
2200 (pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
))
2203 if (!pmc
) { /* must have a prior join */
2207 /* if a source filter was set, must be the same mode as before */
2209 if (pmc
->sfmode
!= omode
) {
2213 } else if (pmc
->sfmode
!= omode
) {
2214 /* allow mode switches for empty-set filters */
2215 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 0, NULL
, 0);
2216 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, pmc
->sfmode
, 0,
2218 pmc
->sfmode
= omode
;
2221 psl
= rtnl_dereference(pmc
->sflist
);
2224 goto done
; /* err = -EADDRNOTAVAIL */
2226 for (i
= 0; i
< psl
->sl_count
; i
++) {
2227 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2232 if (rv
) /* source not found */
2233 goto done
; /* err = -EADDRNOTAVAIL */
2235 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2236 if (psl
->sl_count
== 1 && omode
== MCAST_INCLUDE
) {
2241 /* update the interface filter */
2242 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2243 &mreqs
->imr_sourceaddr
, 1);
2245 for (j
= i
+1; j
< psl
->sl_count
; j
++)
2246 psl
->sl_addr
[j
-1] = psl
->sl_addr
[j
];
2251 /* else, add a new source to the filter */
2253 if (psl
&& psl
->sl_count
>= sysctl_igmp_max_msf
) {
2257 if (!psl
|| psl
->sl_count
== psl
->sl_max
) {
2258 struct ip_sf_socklist
*newpsl
;
2259 int count
= IP_SFBLOCK
;
2262 count
+= psl
->sl_max
;
2263 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(count
), GFP_KERNEL
);
2268 newpsl
->sl_max
= count
;
2269 newpsl
->sl_count
= count
- IP_SFBLOCK
;
2271 for (i
= 0; i
< psl
->sl_count
; i
++)
2272 newpsl
->sl_addr
[i
] = psl
->sl_addr
[i
];
2273 /* decrease mem now to avoid the memleak warning */
2274 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2275 kfree_rcu(psl
, rcu
);
2277 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2280 rv
= 1; /* > 0 for insert logic below if sl_count is 0 */
2281 for (i
= 0; i
< psl
->sl_count
; i
++) {
2282 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2287 if (rv
== 0) /* address already there is an error */
2289 for (j
= psl
->sl_count
-1; j
>= i
; j
--)
2290 psl
->sl_addr
[j
+1] = psl
->sl_addr
[j
];
2291 psl
->sl_addr
[i
] = mreqs
->imr_sourceaddr
;
2294 /* update the interface list */
2295 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2296 &mreqs
->imr_sourceaddr
, 1);
2299 err
= ip_mc_leave_group(sk
, &imr
);
2303 int ip_mc_msfilter(struct sock
*sk
, struct ip_msfilter
*msf
, int ifindex
)
2306 struct ip_mreqn imr
;
2307 __be32 addr
= msf
->imsf_multiaddr
;
2308 struct ip_mc_socklist
*pmc
;
2309 struct in_device
*in_dev
;
2310 struct inet_sock
*inet
= inet_sk(sk
);
2311 struct ip_sf_socklist
*newpsl
, *psl
;
2312 struct net
*net
= sock_net(sk
);
2315 if (!ipv4_is_multicast(addr
))
2317 if (msf
->imsf_fmode
!= MCAST_INCLUDE
&&
2318 msf
->imsf_fmode
!= MCAST_EXCLUDE
)
2323 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2324 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2325 imr
.imr_ifindex
= ifindex
;
2326 in_dev
= ip_mc_find_dev(net
, &imr
);
2333 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2334 if (msf
->imsf_fmode
== MCAST_INCLUDE
&& msf
->imsf_numsrc
== 0) {
2339 for_each_pmc_rtnl(inet
, pmc
) {
2340 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2341 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2344 if (!pmc
) { /* must have a prior join */
2348 if (msf
->imsf_numsrc
) {
2349 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(msf
->imsf_numsrc
),
2355 newpsl
->sl_max
= newpsl
->sl_count
= msf
->imsf_numsrc
;
2356 memcpy(newpsl
->sl_addr
, msf
->imsf_slist
,
2357 msf
->imsf_numsrc
* sizeof(msf
->imsf_slist
[0]));
2358 err
= ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2359 msf
->imsf_fmode
, newpsl
->sl_count
, newpsl
->sl_addr
, 0);
2361 sock_kfree_s(sk
, newpsl
, IP_SFLSIZE(newpsl
->sl_max
));
2366 (void) ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2367 msf
->imsf_fmode
, 0, NULL
, 0);
2369 psl
= rtnl_dereference(pmc
->sflist
);
2371 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2372 psl
->sl_count
, psl
->sl_addr
, 0);
2373 /* decrease mem now to avoid the memleak warning */
2374 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2375 kfree_rcu(psl
, rcu
);
2377 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2379 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2380 pmc
->sfmode
= msf
->imsf_fmode
;
2384 err
= ip_mc_leave_group(sk
, &imr
);
2388 int ip_mc_msfget(struct sock
*sk
, struct ip_msfilter
*msf
,
2389 struct ip_msfilter __user
*optval
, int __user
*optlen
)
2391 int err
, len
, count
, copycount
;
2392 struct ip_mreqn imr
;
2393 __be32 addr
= msf
->imsf_multiaddr
;
2394 struct ip_mc_socklist
*pmc
;
2395 struct in_device
*in_dev
;
2396 struct inet_sock
*inet
= inet_sk(sk
);
2397 struct ip_sf_socklist
*psl
;
2398 struct net
*net
= sock_net(sk
);
2402 if (!ipv4_is_multicast(addr
))
2405 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2406 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2407 imr
.imr_ifindex
= 0;
2408 in_dev
= ip_mc_find_dev(net
, &imr
);
2414 err
= -EADDRNOTAVAIL
;
2416 for_each_pmc_rtnl(inet
, pmc
) {
2417 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2418 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2421 if (!pmc
) /* must have a prior join */
2423 msf
->imsf_fmode
= pmc
->sfmode
;
2424 psl
= rtnl_dereference(pmc
->sflist
);
2429 count
= psl
->sl_count
;
2431 copycount
= count
< msf
->imsf_numsrc
? count
: msf
->imsf_numsrc
;
2432 len
= copycount
* sizeof(psl
->sl_addr
[0]);
2433 msf
->imsf_numsrc
= count
;
2434 if (put_user(IP_MSFILTER_SIZE(copycount
), optlen
) ||
2435 copy_to_user(optval
, msf
, IP_MSFILTER_SIZE(0))) {
2439 copy_to_user(&optval
->imsf_slist
[0], psl
->sl_addr
, len
))
2446 int ip_mc_gsfget(struct sock
*sk
, struct group_filter
*gsf
,
2447 struct group_filter __user
*optval
, int __user
*optlen
)
2449 int err
, i
, count
, copycount
;
2450 struct sockaddr_in
*psin
;
2452 struct ip_mc_socklist
*pmc
;
2453 struct inet_sock
*inet
= inet_sk(sk
);
2454 struct ip_sf_socklist
*psl
;
2458 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
2459 if (psin
->sin_family
!= AF_INET
)
2461 addr
= psin
->sin_addr
.s_addr
;
2462 if (!ipv4_is_multicast(addr
))
2465 err
= -EADDRNOTAVAIL
;
2467 for_each_pmc_rtnl(inet
, pmc
) {
2468 if (pmc
->multi
.imr_multiaddr
.s_addr
== addr
&&
2469 pmc
->multi
.imr_ifindex
== gsf
->gf_interface
)
2472 if (!pmc
) /* must have a prior join */
2474 gsf
->gf_fmode
= pmc
->sfmode
;
2475 psl
= rtnl_dereference(pmc
->sflist
);
2476 count
= psl
? psl
->sl_count
: 0;
2477 copycount
= count
< gsf
->gf_numsrc
? count
: gsf
->gf_numsrc
;
2478 gsf
->gf_numsrc
= count
;
2479 if (put_user(GROUP_FILTER_SIZE(copycount
), optlen
) ||
2480 copy_to_user(optval
, gsf
, GROUP_FILTER_SIZE(0))) {
2483 for (i
= 0; i
< copycount
; i
++) {
2484 struct sockaddr_storage ss
;
2486 psin
= (struct sockaddr_in
*)&ss
;
2487 memset(&ss
, 0, sizeof(ss
));
2488 psin
->sin_family
= AF_INET
;
2489 psin
->sin_addr
.s_addr
= psl
->sl_addr
[i
];
2490 if (copy_to_user(&optval
->gf_slist
[i
], &ss
, sizeof(ss
)))
2499 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2501 int ip_mc_sf_allow(struct sock
*sk
, __be32 loc_addr
, __be32 rmt_addr
, int dif
)
2503 struct inet_sock
*inet
= inet_sk(sk
);
2504 struct ip_mc_socklist
*pmc
;
2505 struct ip_sf_socklist
*psl
;
2510 if (!ipv4_is_multicast(loc_addr
))
2514 for_each_pmc_rcu(inet
, pmc
) {
2515 if (pmc
->multi
.imr_multiaddr
.s_addr
== loc_addr
&&
2516 pmc
->multi
.imr_ifindex
== dif
)
2522 psl
= rcu_dereference(pmc
->sflist
);
2523 ret
= (pmc
->sfmode
== MCAST_EXCLUDE
);
2527 for (i
= 0; i
< psl
->sl_count
; i
++) {
2528 if (psl
->sl_addr
[i
] == rmt_addr
)
2532 if (pmc
->sfmode
== MCAST_INCLUDE
&& i
>= psl
->sl_count
)
2534 if (pmc
->sfmode
== MCAST_EXCLUDE
&& i
< psl
->sl_count
)
2544 * A socket is closing.
2547 void ip_mc_drop_socket(struct sock
*sk
)
2549 struct inet_sock
*inet
= inet_sk(sk
);
2550 struct ip_mc_socklist
*iml
;
2551 struct net
*net
= sock_net(sk
);
2557 while ((iml
= rtnl_dereference(inet
->mc_list
)) != NULL
) {
2558 struct in_device
*in_dev
;
2560 inet
->mc_list
= iml
->next_rcu
;
2561 in_dev
= inetdev_by_index(net
, iml
->multi
.imr_ifindex
);
2562 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2564 ip_mc_dec_group(in_dev
, iml
->multi
.imr_multiaddr
.s_addr
);
2565 /* decrease mem now to avoid the memleak warning */
2566 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
2567 kfree_rcu(iml
, rcu
);
2572 /* called with rcu_read_lock() */
2573 int ip_check_mc_rcu(struct in_device
*in_dev
, __be32 mc_addr
, __be32 src_addr
, u8 proto
)
2575 struct ip_mc_list
*im
;
2576 struct ip_mc_list __rcu
**mc_hash
;
2577 struct ip_sf_list
*psf
;
2580 mc_hash
= rcu_dereference(in_dev
->mc_hash
);
2582 u32 hash
= hash_32((__force u32
)mc_addr
, MC_HASH_SZ_LOG
);
2584 for (im
= rcu_dereference(mc_hash
[hash
]);
2586 im
= rcu_dereference(im
->next_hash
)) {
2587 if (im
->multiaddr
== mc_addr
)
2591 for_each_pmc_rcu(in_dev
, im
) {
2592 if (im
->multiaddr
== mc_addr
)
2596 if (im
&& proto
== IPPROTO_IGMP
) {
2600 for (psf
= im
->sources
; psf
; psf
= psf
->sf_next
) {
2601 if (psf
->sf_inaddr
== src_addr
)
2605 rv
= psf
->sf_count
[MCAST_INCLUDE
] ||
2606 psf
->sf_count
[MCAST_EXCLUDE
] !=
2607 im
->sfcount
[MCAST_EXCLUDE
];
2609 rv
= im
->sfcount
[MCAST_EXCLUDE
] != 0;
2611 rv
= 1; /* unspecified source; tentatively allow */
2616 #if defined(CONFIG_PROC_FS)
2617 struct igmp_mc_iter_state
{
2618 struct seq_net_private p
;
2619 struct net_device
*dev
;
2620 struct in_device
*in_dev
;
2623 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2625 static inline struct ip_mc_list
*igmp_mc_get_first(struct seq_file
*seq
)
2627 struct net
*net
= seq_file_net(seq
);
2628 struct ip_mc_list
*im
= NULL
;
2629 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2631 state
->in_dev
= NULL
;
2632 for_each_netdev_rcu(net
, state
->dev
) {
2633 struct in_device
*in_dev
;
2635 in_dev
= __in_dev_get_rcu(state
->dev
);
2638 im
= rcu_dereference(in_dev
->mc_list
);
2640 state
->in_dev
= in_dev
;
2647 static struct ip_mc_list
*igmp_mc_get_next(struct seq_file
*seq
, struct ip_mc_list
*im
)
2649 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2651 im
= rcu_dereference(im
->next_rcu
);
2653 state
->dev
= next_net_device_rcu(state
->dev
);
2655 state
->in_dev
= NULL
;
2658 state
->in_dev
= __in_dev_get_rcu(state
->dev
);
2661 im
= rcu_dereference(state
->in_dev
->mc_list
);
2666 static struct ip_mc_list
*igmp_mc_get_idx(struct seq_file
*seq
, loff_t pos
)
2668 struct ip_mc_list
*im
= igmp_mc_get_first(seq
);
2670 while (pos
&& (im
= igmp_mc_get_next(seq
, im
)) != NULL
)
2672 return pos
? NULL
: im
;
2675 static void *igmp_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2679 return *pos
? igmp_mc_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2682 static void *igmp_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2684 struct ip_mc_list
*im
;
2685 if (v
== SEQ_START_TOKEN
)
2686 im
= igmp_mc_get_first(seq
);
2688 im
= igmp_mc_get_next(seq
, v
);
2693 static void igmp_mc_seq_stop(struct seq_file
*seq
, void *v
)
2696 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2698 state
->in_dev
= NULL
;
2703 static int igmp_mc_seq_show(struct seq_file
*seq
, void *v
)
2705 if (v
== SEQ_START_TOKEN
)
2707 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2709 struct ip_mc_list
*im
= (struct ip_mc_list
*)v
;
2710 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2714 #ifdef CONFIG_IP_MULTICAST
2715 querier
= IGMP_V1_SEEN(state
->in_dev
) ? "V1" :
2716 IGMP_V2_SEEN(state
->in_dev
) ? "V2" :
2722 if (rcu_access_pointer(state
->in_dev
->mc_list
) == im
) {
2723 seq_printf(seq
, "%d\t%-10s: %5d %7s\n",
2724 state
->dev
->ifindex
, state
->dev
->name
, state
->in_dev
->mc_count
, querier
);
2727 delta
= im
->timer
.expires
- jiffies
;
2729 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2730 im
->multiaddr
, im
->users
,
2732 im
->tm_running
? jiffies_delta_to_clock_t(delta
) : 0,
2738 static const struct seq_operations igmp_mc_seq_ops
= {
2739 .start
= igmp_mc_seq_start
,
2740 .next
= igmp_mc_seq_next
,
2741 .stop
= igmp_mc_seq_stop
,
2742 .show
= igmp_mc_seq_show
,
2745 static int igmp_mc_seq_open(struct inode
*inode
, struct file
*file
)
2747 return seq_open_net(inode
, file
, &igmp_mc_seq_ops
,
2748 sizeof(struct igmp_mc_iter_state
));
2751 static const struct file_operations igmp_mc_seq_fops
= {
2752 .owner
= THIS_MODULE
,
2753 .open
= igmp_mc_seq_open
,
2755 .llseek
= seq_lseek
,
2756 .release
= seq_release_net
,
2759 struct igmp_mcf_iter_state
{
2760 struct seq_net_private p
;
2761 struct net_device
*dev
;
2762 struct in_device
*idev
;
2763 struct ip_mc_list
*im
;
2766 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2768 static inline struct ip_sf_list
*igmp_mcf_get_first(struct seq_file
*seq
)
2770 struct net
*net
= seq_file_net(seq
);
2771 struct ip_sf_list
*psf
= NULL
;
2772 struct ip_mc_list
*im
= NULL
;
2773 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2777 for_each_netdev_rcu(net
, state
->dev
) {
2778 struct in_device
*idev
;
2779 idev
= __in_dev_get_rcu(state
->dev
);
2780 if (unlikely(!idev
))
2782 im
= rcu_dereference(idev
->mc_list
);
2784 spin_lock_bh(&im
->lock
);
2791 spin_unlock_bh(&im
->lock
);
2797 static struct ip_sf_list
*igmp_mcf_get_next(struct seq_file
*seq
, struct ip_sf_list
*psf
)
2799 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2803 spin_unlock_bh(&state
->im
->lock
);
2804 state
->im
= state
->im
->next
;
2805 while (!state
->im
) {
2806 state
->dev
= next_net_device_rcu(state
->dev
);
2811 state
->idev
= __in_dev_get_rcu(state
->dev
);
2814 state
->im
= rcu_dereference(state
->idev
->mc_list
);
2818 spin_lock_bh(&state
->im
->lock
);
2819 psf
= state
->im
->sources
;
2825 static struct ip_sf_list
*igmp_mcf_get_idx(struct seq_file
*seq
, loff_t pos
)
2827 struct ip_sf_list
*psf
= igmp_mcf_get_first(seq
);
2829 while (pos
&& (psf
= igmp_mcf_get_next(seq
, psf
)) != NULL
)
2831 return pos
? NULL
: psf
;
2834 static void *igmp_mcf_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2838 return *pos
? igmp_mcf_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2841 static void *igmp_mcf_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2843 struct ip_sf_list
*psf
;
2844 if (v
== SEQ_START_TOKEN
)
2845 psf
= igmp_mcf_get_first(seq
);
2847 psf
= igmp_mcf_get_next(seq
, v
);
2852 static void igmp_mcf_seq_stop(struct seq_file
*seq
, void *v
)
2855 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2856 if (likely(state
->im
)) {
2857 spin_unlock_bh(&state
->im
->lock
);
2865 static int igmp_mcf_seq_show(struct seq_file
*seq
, void *v
)
2867 struct ip_sf_list
*psf
= (struct ip_sf_list
*)v
;
2868 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2870 if (v
== SEQ_START_TOKEN
) {
2871 seq_puts(seq
, "Idx Device MCA SRC INC EXC\n");
2875 "0x%08x %6lu %6lu\n",
2876 state
->dev
->ifindex
, state
->dev
->name
,
2877 ntohl(state
->im
->multiaddr
),
2878 ntohl(psf
->sf_inaddr
),
2879 psf
->sf_count
[MCAST_INCLUDE
],
2880 psf
->sf_count
[MCAST_EXCLUDE
]);
2885 static const struct seq_operations igmp_mcf_seq_ops
= {
2886 .start
= igmp_mcf_seq_start
,
2887 .next
= igmp_mcf_seq_next
,
2888 .stop
= igmp_mcf_seq_stop
,
2889 .show
= igmp_mcf_seq_show
,
2892 static int igmp_mcf_seq_open(struct inode
*inode
, struct file
*file
)
2894 return seq_open_net(inode
, file
, &igmp_mcf_seq_ops
,
2895 sizeof(struct igmp_mcf_iter_state
));
2898 static const struct file_operations igmp_mcf_seq_fops
= {
2899 .owner
= THIS_MODULE
,
2900 .open
= igmp_mcf_seq_open
,
2902 .llseek
= seq_lseek
,
2903 .release
= seq_release_net
,
2906 static int __net_init
igmp_net_init(struct net
*net
)
2908 struct proc_dir_entry
*pde
;
2911 pde
= proc_create("igmp", S_IRUGO
, net
->proc_net
, &igmp_mc_seq_fops
);
2914 pde
= proc_create("mcfilter", S_IRUGO
, net
->proc_net
,
2915 &igmp_mcf_seq_fops
);
2918 err
= inet_ctl_sock_create(&net
->ipv4
.mc_autojoin_sk
, AF_INET
,
2919 SOCK_DGRAM
, 0, net
);
2921 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
2929 remove_proc_entry("mcfilter", net
->proc_net
);
2931 remove_proc_entry("igmp", net
->proc_net
);
2936 static void __net_exit
igmp_net_exit(struct net
*net
)
2938 remove_proc_entry("mcfilter", net
->proc_net
);
2939 remove_proc_entry("igmp", net
->proc_net
);
2940 inet_ctl_sock_destroy(net
->ipv4
.mc_autojoin_sk
);
2943 static struct pernet_operations igmp_net_ops
= {
2944 .init
= igmp_net_init
,
2945 .exit
= igmp_net_exit
,
2949 static int igmp_netdev_event(struct notifier_block
*this,
2950 unsigned long event
, void *ptr
)
2952 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
2953 struct in_device
*in_dev
;
2956 case NETDEV_RESEND_IGMP
:
2957 in_dev
= __in_dev_get_rtnl(dev
);
2959 ip_mc_rejoin_groups(in_dev
);
2967 static struct notifier_block igmp_notifier
= {
2968 .notifier_call
= igmp_netdev_event
,
2971 int __init
igmp_mc_init(void)
2973 #if defined(CONFIG_PROC_FS)
2976 err
= register_pernet_subsys(&igmp_net_ops
);
2979 err
= register_netdevice_notifier(&igmp_notifier
);
2981 goto reg_notif_fail
;
2985 unregister_pernet_subsys(&igmp_net_ops
);
2988 return register_netdevice_notifier(&igmp_notifier
);