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 #ifdef CONFIG_IP_MULTICAST
111 /* Parameter names and values are taken from igmp-v2-06 draft */
113 #define IGMP_V1_ROUTER_PRESENT_TIMEOUT (400*HZ)
114 #define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ)
115 #define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
116 #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
117 #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
118 #define IGMP_QUERY_ROBUSTNESS_VARIABLE 2
121 #define IGMP_INITIAL_REPORT_DELAY (1)
123 /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
124 * IGMP specs require to report membership immediately after
125 * joining a group, but we delay the first report by a
126 * small interval. It seems more natural and still does not
127 * contradict to specs provided this delay is small enough.
130 #define IGMP_V1_SEEN(in_dev) \
131 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
132 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
133 ((in_dev)->mr_v1_seen && \
134 time_before(jiffies, (in_dev)->mr_v1_seen)))
135 #define IGMP_V2_SEEN(in_dev) \
136 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
137 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
138 ((in_dev)->mr_v2_seen && \
139 time_before(jiffies, (in_dev)->mr_v2_seen)))
141 static int unsolicited_report_interval(struct in_device
*in_dev
)
143 int interval_ms
, interval_jiffies
;
145 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
146 interval_ms
= IN_DEV_CONF_GET(
148 IGMPV2_UNSOLICITED_REPORT_INTERVAL
);
150 interval_ms
= IN_DEV_CONF_GET(
152 IGMPV3_UNSOLICITED_REPORT_INTERVAL
);
154 interval_jiffies
= msecs_to_jiffies(interval_ms
);
156 /* _timer functions can't handle a delay of 0 jiffies so ensure
157 * we always return a positive value.
159 if (interval_jiffies
<= 0)
160 interval_jiffies
= 1;
161 return interval_jiffies
;
164 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
);
165 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
);
166 static void igmpv3_clear_delrec(struct in_device
*in_dev
);
167 static int sf_setstate(struct ip_mc_list
*pmc
);
168 static void sf_markstate(struct ip_mc_list
*pmc
);
170 static void ip_mc_clear_src(struct ip_mc_list
*pmc
);
171 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
172 int sfcount
, __be32
*psfsrc
, int delta
);
174 static void ip_ma_put(struct ip_mc_list
*im
)
176 if (atomic_dec_and_test(&im
->refcnt
)) {
177 in_dev_put(im
->interface
);
182 #define for_each_pmc_rcu(in_dev, pmc) \
183 for (pmc = rcu_dereference(in_dev->mc_list); \
185 pmc = rcu_dereference(pmc->next_rcu))
187 #define for_each_pmc_rtnl(in_dev, pmc) \
188 for (pmc = rtnl_dereference(in_dev->mc_list); \
190 pmc = rtnl_dereference(pmc->next_rcu))
192 #ifdef CONFIG_IP_MULTICAST
198 static void igmp_stop_timer(struct ip_mc_list
*im
)
200 spin_lock_bh(&im
->lock
);
201 if (del_timer(&im
->timer
))
202 atomic_dec(&im
->refcnt
);
205 im
->unsolicit_count
= 0;
206 spin_unlock_bh(&im
->lock
);
209 /* It must be called with locked im->lock */
210 static void igmp_start_timer(struct ip_mc_list
*im
, int max_delay
)
212 int tv
= prandom_u32() % max_delay
;
215 if (!mod_timer(&im
->timer
, jiffies
+tv
+2))
216 atomic_inc(&im
->refcnt
);
219 static void igmp_gq_start_timer(struct in_device
*in_dev
)
221 int tv
= prandom_u32() % in_dev
->mr_maxdelay
;
223 in_dev
->mr_gq_running
= 1;
224 if (!mod_timer(&in_dev
->mr_gq_timer
, jiffies
+tv
+2))
228 static void igmp_ifc_start_timer(struct in_device
*in_dev
, int delay
)
230 int tv
= prandom_u32() % delay
;
232 if (!mod_timer(&in_dev
->mr_ifc_timer
, jiffies
+tv
+2))
236 static void igmp_mod_timer(struct ip_mc_list
*im
, int max_delay
)
238 spin_lock_bh(&im
->lock
);
239 im
->unsolicit_count
= 0;
240 if (del_timer(&im
->timer
)) {
241 if ((long)(im
->timer
.expires
-jiffies
) < max_delay
) {
242 add_timer(&im
->timer
);
244 spin_unlock_bh(&im
->lock
);
247 atomic_dec(&im
->refcnt
);
249 igmp_start_timer(im
, max_delay
);
250 spin_unlock_bh(&im
->lock
);
255 * Send an IGMP report.
258 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
261 static int is_in(struct ip_mc_list
*pmc
, struct ip_sf_list
*psf
, int type
,
262 int gdeleted
, int sdeleted
)
265 case IGMPV3_MODE_IS_INCLUDE
:
266 case IGMPV3_MODE_IS_EXCLUDE
:
267 if (gdeleted
|| sdeleted
)
269 if (!(pmc
->gsquery
&& !psf
->sf_gsresp
)) {
270 if (pmc
->sfmode
== MCAST_INCLUDE
)
272 /* don't include if this source is excluded
275 if (psf
->sf_count
[MCAST_INCLUDE
])
276 return type
== IGMPV3_MODE_IS_INCLUDE
;
277 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
278 psf
->sf_count
[MCAST_EXCLUDE
];
281 case IGMPV3_CHANGE_TO_INCLUDE
:
282 if (gdeleted
|| sdeleted
)
284 return psf
->sf_count
[MCAST_INCLUDE
] != 0;
285 case IGMPV3_CHANGE_TO_EXCLUDE
:
286 if (gdeleted
|| sdeleted
)
288 if (pmc
->sfcount
[MCAST_EXCLUDE
] == 0 ||
289 psf
->sf_count
[MCAST_INCLUDE
])
291 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
292 psf
->sf_count
[MCAST_EXCLUDE
];
293 case IGMPV3_ALLOW_NEW_SOURCES
:
294 if (gdeleted
|| !psf
->sf_crcount
)
296 return (pmc
->sfmode
== MCAST_INCLUDE
) ^ sdeleted
;
297 case IGMPV3_BLOCK_OLD_SOURCES
:
298 if (pmc
->sfmode
== MCAST_INCLUDE
)
299 return gdeleted
|| (psf
->sf_crcount
&& sdeleted
);
300 return psf
->sf_crcount
&& !gdeleted
&& !sdeleted
;
306 igmp_scount(struct ip_mc_list
*pmc
, int type
, int gdeleted
, int sdeleted
)
308 struct ip_sf_list
*psf
;
311 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
312 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
))
319 static struct sk_buff
*igmpv3_newpack(struct net_device
*dev
, unsigned int mtu
)
324 struct igmpv3_report
*pig
;
325 struct net
*net
= dev_net(dev
);
327 int hlen
= LL_RESERVED_SPACE(dev
);
328 int tlen
= dev
->needed_tailroom
;
329 unsigned int size
= mtu
;
332 skb
= alloc_skb(size
+ hlen
+ tlen
,
333 GFP_ATOMIC
| __GFP_NOWARN
);
340 skb
->priority
= TC_PRIO_CONTROL
;
342 rt
= ip_route_output_ports(net
, &fl4
, NULL
, IGMPV3_ALL_MCR
, 0,
344 IPPROTO_IGMP
, 0, dev
->ifindex
);
350 skb_dst_set(skb
, &rt
->dst
);
353 skb_reserve(skb
, hlen
);
354 skb_tailroom_reserve(skb
, mtu
, tlen
);
356 skb_reset_network_header(skb
);
358 skb_put(skb
, sizeof(struct iphdr
) + 4);
361 pip
->ihl
= (sizeof(struct iphdr
)+4)>>2;
363 pip
->frag_off
= htons(IP_DF
);
365 pip
->daddr
= fl4
.daddr
;
366 pip
->saddr
= fl4
.saddr
;
367 pip
->protocol
= IPPROTO_IGMP
;
368 pip
->tot_len
= 0; /* filled in later */
369 ip_select_ident(net
, skb
, NULL
);
370 ((u8
*)&pip
[1])[0] = IPOPT_RA
;
371 ((u8
*)&pip
[1])[1] = 4;
372 ((u8
*)&pip
[1])[2] = 0;
373 ((u8
*)&pip
[1])[3] = 0;
375 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
) + 4;
376 skb_put(skb
, sizeof(*pig
));
377 pig
= igmpv3_report_hdr(skb
);
378 pig
->type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
386 static int igmpv3_sendpack(struct sk_buff
*skb
)
388 struct igmphdr
*pig
= igmp_hdr(skb
);
389 const int igmplen
= skb_tail_pointer(skb
) - skb_transport_header(skb
);
391 pig
->csum
= ip_compute_csum(igmp_hdr(skb
), igmplen
);
393 return ip_local_out(dev_net(skb_dst(skb
)->dev
), skb
->sk
, skb
);
396 static int grec_size(struct ip_mc_list
*pmc
, int type
, int gdel
, int sdel
)
398 return sizeof(struct igmpv3_grec
) + 4*igmp_scount(pmc
, type
, gdel
, sdel
);
401 static struct sk_buff
*add_grhead(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
402 int type
, struct igmpv3_grec
**ppgr
)
404 struct net_device
*dev
= pmc
->interface
->dev
;
405 struct igmpv3_report
*pih
;
406 struct igmpv3_grec
*pgr
;
409 skb
= igmpv3_newpack(dev
, dev
->mtu
);
412 pgr
= (struct igmpv3_grec
*)skb_put(skb
, sizeof(struct igmpv3_grec
));
413 pgr
->grec_type
= type
;
414 pgr
->grec_auxwords
= 0;
416 pgr
->grec_mca
= pmc
->multiaddr
;
417 pih
= igmpv3_report_hdr(skb
);
418 pih
->ngrec
= htons(ntohs(pih
->ngrec
)+1);
423 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
425 static struct sk_buff
*add_grec(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
426 int type
, int gdeleted
, int sdeleted
)
428 struct net_device
*dev
= pmc
->interface
->dev
;
429 struct net
*net
= dev_net(dev
);
430 struct igmpv3_report
*pih
;
431 struct igmpv3_grec
*pgr
= NULL
;
432 struct ip_sf_list
*psf
, *psf_next
, *psf_prev
, **psf_list
;
433 int scount
, stotal
, first
, isquery
, truncate
;
435 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
437 if (ipv4_is_local_multicast(pmc
->multiaddr
) && !net
->ipv4
.sysctl_igmp_llm_reports
)
440 isquery
= type
== IGMPV3_MODE_IS_INCLUDE
||
441 type
== IGMPV3_MODE_IS_EXCLUDE
;
442 truncate
= type
== IGMPV3_MODE_IS_EXCLUDE
||
443 type
== IGMPV3_CHANGE_TO_EXCLUDE
;
447 psf_list
= sdeleted
? &pmc
->tomb
: &pmc
->sources
;
452 pih
= skb
? igmpv3_report_hdr(skb
) : NULL
;
454 /* EX and TO_EX get a fresh packet, if needed */
456 if (pih
&& pih
->ngrec
&&
457 AVAILABLE(skb
) < grec_size(pmc
, type
, gdeleted
, sdeleted
)) {
459 igmpv3_sendpack(skb
);
460 skb
= igmpv3_newpack(dev
, dev
->mtu
);
465 for (psf
= *psf_list
; psf
; psf
= psf_next
) {
468 psf_next
= psf
->sf_next
;
470 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
)) {
475 /* clear marks on query responses */
479 if (AVAILABLE(skb
) < sizeof(__be32
) +
480 first
*sizeof(struct igmpv3_grec
)) {
481 if (truncate
&& !first
)
482 break; /* truncate these */
484 pgr
->grec_nsrcs
= htons(scount
);
486 igmpv3_sendpack(skb
);
487 skb
= igmpv3_newpack(dev
, dev
->mtu
);
492 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
497 psrc
= (__be32
*)skb_put(skb
, sizeof(__be32
));
498 *psrc
= psf
->sf_inaddr
;
500 if ((type
== IGMPV3_ALLOW_NEW_SOURCES
||
501 type
== IGMPV3_BLOCK_OLD_SOURCES
) && psf
->sf_crcount
) {
503 if ((sdeleted
|| gdeleted
) && psf
->sf_crcount
== 0) {
505 psf_prev
->sf_next
= psf
->sf_next
;
507 *psf_list
= psf
->sf_next
;
517 if (type
== IGMPV3_ALLOW_NEW_SOURCES
||
518 type
== IGMPV3_BLOCK_OLD_SOURCES
)
520 if (pmc
->crcount
|| isquery
) {
521 /* make sure we have room for group header */
522 if (skb
&& AVAILABLE(skb
) < sizeof(struct igmpv3_grec
)) {
523 igmpv3_sendpack(skb
);
524 skb
= NULL
; /* add_grhead will get a new one */
526 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
530 pgr
->grec_nsrcs
= htons(scount
);
533 pmc
->gsquery
= 0; /* clear query state on report */
537 static int igmpv3_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
)
539 struct sk_buff
*skb
= NULL
;
540 struct net
*net
= dev_net(in_dev
->dev
);
545 for_each_pmc_rcu(in_dev
, pmc
) {
546 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
548 if (ipv4_is_local_multicast(pmc
->multiaddr
) &&
549 !net
->ipv4
.sysctl_igmp_llm_reports
)
551 spin_lock_bh(&pmc
->lock
);
552 if (pmc
->sfcount
[MCAST_EXCLUDE
])
553 type
= IGMPV3_MODE_IS_EXCLUDE
;
555 type
= IGMPV3_MODE_IS_INCLUDE
;
556 skb
= add_grec(skb
, pmc
, type
, 0, 0);
557 spin_unlock_bh(&pmc
->lock
);
561 spin_lock_bh(&pmc
->lock
);
562 if (pmc
->sfcount
[MCAST_EXCLUDE
])
563 type
= IGMPV3_MODE_IS_EXCLUDE
;
565 type
= IGMPV3_MODE_IS_INCLUDE
;
566 skb
= add_grec(skb
, pmc
, type
, 0, 0);
567 spin_unlock_bh(&pmc
->lock
);
571 return igmpv3_sendpack(skb
);
575 * remove zero-count source records from a source filter list
577 static void igmpv3_clear_zeros(struct ip_sf_list
**ppsf
)
579 struct ip_sf_list
*psf_prev
, *psf_next
, *psf
;
582 for (psf
= *ppsf
; psf
; psf
= psf_next
) {
583 psf_next
= psf
->sf_next
;
584 if (psf
->sf_crcount
== 0) {
586 psf_prev
->sf_next
= psf
->sf_next
;
588 *ppsf
= psf
->sf_next
;
595 static void igmpv3_send_cr(struct in_device
*in_dev
)
597 struct ip_mc_list
*pmc
, *pmc_prev
, *pmc_next
;
598 struct sk_buff
*skb
= NULL
;
602 spin_lock_bh(&in_dev
->mc_tomb_lock
);
606 for (pmc
= in_dev
->mc_tomb
; pmc
; pmc
= pmc_next
) {
607 pmc_next
= pmc
->next
;
608 if (pmc
->sfmode
== MCAST_INCLUDE
) {
609 type
= IGMPV3_BLOCK_OLD_SOURCES
;
610 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
611 skb
= add_grec(skb
, pmc
, type
, 1, 0);
612 skb
= add_grec(skb
, pmc
, dtype
, 1, 1);
615 if (pmc
->sfmode
== MCAST_EXCLUDE
) {
616 type
= IGMPV3_CHANGE_TO_INCLUDE
;
617 skb
= add_grec(skb
, pmc
, type
, 1, 0);
620 if (pmc
->crcount
== 0) {
621 igmpv3_clear_zeros(&pmc
->tomb
);
622 igmpv3_clear_zeros(&pmc
->sources
);
625 if (pmc
->crcount
== 0 && !pmc
->tomb
&& !pmc
->sources
) {
627 pmc_prev
->next
= pmc_next
;
629 in_dev
->mc_tomb
= pmc_next
;
630 in_dev_put(pmc
->interface
);
635 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
638 for_each_pmc_rcu(in_dev
, pmc
) {
639 spin_lock_bh(&pmc
->lock
);
640 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
641 type
= IGMPV3_BLOCK_OLD_SOURCES
;
642 dtype
= IGMPV3_ALLOW_NEW_SOURCES
;
644 type
= IGMPV3_ALLOW_NEW_SOURCES
;
645 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
647 skb
= add_grec(skb
, pmc
, type
, 0, 0);
648 skb
= add_grec(skb
, pmc
, dtype
, 0, 1); /* deleted sources */
650 /* filter mode changes */
652 if (pmc
->sfmode
== MCAST_EXCLUDE
)
653 type
= IGMPV3_CHANGE_TO_EXCLUDE
;
655 type
= IGMPV3_CHANGE_TO_INCLUDE
;
656 skb
= add_grec(skb
, pmc
, type
, 0, 0);
659 spin_unlock_bh(&pmc
->lock
);
665 (void) igmpv3_sendpack(skb
);
668 static int igmp_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
,
675 struct net_device
*dev
= in_dev
->dev
;
676 struct net
*net
= dev_net(dev
);
677 __be32 group
= pmc
? pmc
->multiaddr
: 0;
682 if (type
== IGMPV3_HOST_MEMBERSHIP_REPORT
)
683 return igmpv3_send_report(in_dev
, pmc
);
685 if (ipv4_is_local_multicast(group
) && !net
->ipv4
.sysctl_igmp_llm_reports
)
688 if (type
== IGMP_HOST_LEAVE_MESSAGE
)
689 dst
= IGMP_ALL_ROUTER
;
693 rt
= ip_route_output_ports(net
, &fl4
, NULL
, dst
, 0,
695 IPPROTO_IGMP
, 0, dev
->ifindex
);
699 hlen
= LL_RESERVED_SPACE(dev
);
700 tlen
= dev
->needed_tailroom
;
701 skb
= alloc_skb(IGMP_SIZE
+ hlen
+ tlen
, GFP_ATOMIC
);
706 skb
->priority
= TC_PRIO_CONTROL
;
708 skb_dst_set(skb
, &rt
->dst
);
710 skb_reserve(skb
, hlen
);
712 skb_reset_network_header(skb
);
714 skb_put(skb
, sizeof(struct iphdr
) + 4);
717 iph
->ihl
= (sizeof(struct iphdr
)+4)>>2;
719 iph
->frag_off
= htons(IP_DF
);
722 iph
->saddr
= fl4
.saddr
;
723 iph
->protocol
= IPPROTO_IGMP
;
724 ip_select_ident(net
, skb
, NULL
);
725 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
726 ((u8
*)&iph
[1])[1] = 4;
727 ((u8
*)&iph
[1])[2] = 0;
728 ((u8
*)&iph
[1])[3] = 0;
730 ih
= (struct igmphdr
*)skb_put(skb
, sizeof(struct igmphdr
));
735 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
737 return ip_local_out(net
, skb
->sk
, skb
);
740 static void igmp_gq_timer_expire(unsigned long data
)
742 struct in_device
*in_dev
= (struct in_device
*)data
;
744 in_dev
->mr_gq_running
= 0;
745 igmpv3_send_report(in_dev
, NULL
);
749 static void igmp_ifc_timer_expire(unsigned long data
)
751 struct in_device
*in_dev
= (struct in_device
*)data
;
753 igmpv3_send_cr(in_dev
);
754 if (in_dev
->mr_ifc_count
) {
755 in_dev
->mr_ifc_count
--;
756 igmp_ifc_start_timer(in_dev
,
757 unsolicited_report_interval(in_dev
));
762 static void igmp_ifc_event(struct in_device
*in_dev
)
764 struct net
*net
= dev_net(in_dev
->dev
);
765 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
767 in_dev
->mr_ifc_count
= in_dev
->mr_qrv
?: net
->ipv4
.sysctl_igmp_qrv
;
768 igmp_ifc_start_timer(in_dev
, 1);
772 static void igmp_timer_expire(unsigned long data
)
774 struct ip_mc_list
*im
= (struct ip_mc_list
*)data
;
775 struct in_device
*in_dev
= im
->interface
;
777 spin_lock(&im
->lock
);
780 if (im
->unsolicit_count
) {
781 im
->unsolicit_count
--;
782 igmp_start_timer(im
, unsolicited_report_interval(in_dev
));
785 spin_unlock(&im
->lock
);
787 if (IGMP_V1_SEEN(in_dev
))
788 igmp_send_report(in_dev
, im
, IGMP_HOST_MEMBERSHIP_REPORT
);
789 else if (IGMP_V2_SEEN(in_dev
))
790 igmp_send_report(in_dev
, im
, IGMPV2_HOST_MEMBERSHIP_REPORT
);
792 igmp_send_report(in_dev
, im
, IGMPV3_HOST_MEMBERSHIP_REPORT
);
797 /* mark EXCLUDE-mode sources */
798 static int igmp_xmarksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
800 struct ip_sf_list
*psf
;
804 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
807 for (i
= 0; i
< nsrcs
; i
++) {
808 /* skip inactive filters */
809 if (psf
->sf_count
[MCAST_INCLUDE
] ||
810 pmc
->sfcount
[MCAST_EXCLUDE
] !=
811 psf
->sf_count
[MCAST_EXCLUDE
])
813 if (srcs
[i
] == psf
->sf_inaddr
) {
820 if (scount
== nsrcs
) /* all sources excluded */
825 static int igmp_marksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
827 struct ip_sf_list
*psf
;
830 if (pmc
->sfmode
== MCAST_EXCLUDE
)
831 return igmp_xmarksources(pmc
, nsrcs
, srcs
);
833 /* mark INCLUDE-mode sources */
835 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
838 for (i
= 0; i
< nsrcs
; i
++)
839 if (srcs
[i
] == psf
->sf_inaddr
) {
853 /* return true if packet was dropped */
854 static bool igmp_heard_report(struct in_device
*in_dev
, __be32 group
)
856 struct ip_mc_list
*im
;
857 struct net
*net
= dev_net(in_dev
->dev
);
859 /* Timers are only set for non-local groups */
861 if (group
== IGMP_ALL_HOSTS
)
863 if (ipv4_is_local_multicast(group
) && !net
->ipv4
.sysctl_igmp_llm_reports
)
867 for_each_pmc_rcu(in_dev
, im
) {
868 if (im
->multiaddr
== group
) {
877 /* return true if packet was dropped */
878 static bool igmp_heard_query(struct in_device
*in_dev
, struct sk_buff
*skb
,
881 struct igmphdr
*ih
= igmp_hdr(skb
);
882 struct igmpv3_query
*ih3
= igmpv3_query_hdr(skb
);
883 struct ip_mc_list
*im
;
884 __be32 group
= ih
->group
;
887 struct net
*net
= dev_net(in_dev
->dev
);
892 /* Alas, old v1 router presents here. */
894 max_delay
= IGMP_QUERY_RESPONSE_INTERVAL
;
895 in_dev
->mr_v1_seen
= jiffies
+
896 IGMP_V1_ROUTER_PRESENT_TIMEOUT
;
899 /* v2 router present */
900 max_delay
= ih
->code
*(HZ
/IGMP_TIMER_SCALE
);
901 in_dev
->mr_v2_seen
= jiffies
+
902 IGMP_V2_ROUTER_PRESENT_TIMEOUT
;
904 /* cancel the interface change timer */
905 in_dev
->mr_ifc_count
= 0;
906 if (del_timer(&in_dev
->mr_ifc_timer
))
907 __in_dev_put(in_dev
);
908 /* clear deleted report items */
909 igmpv3_clear_delrec(in_dev
);
910 } else if (len
< 12) {
911 return true; /* ignore bogus packet; freed by caller */
912 } else if (IGMP_V1_SEEN(in_dev
)) {
913 /* This is a v3 query with v1 queriers present */
914 max_delay
= IGMP_QUERY_RESPONSE_INTERVAL
;
916 } else if (IGMP_V2_SEEN(in_dev
)) {
917 /* this is a v3 query with v2 queriers present;
918 * Interpretation of the max_delay code is problematic here.
919 * A real v2 host would use ih_code directly, while v3 has a
920 * different encoding. We use the v3 encoding as more likely
921 * to be intended in a v3 query.
923 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
925 max_delay
= 1; /* can't mod w/ 0 */
927 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)))
930 ih3
= igmpv3_query_hdr(skb
);
932 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)
933 + ntohs(ih3
->nsrcs
)*sizeof(__be32
)))
935 ih3
= igmpv3_query_hdr(skb
);
938 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
940 max_delay
= 1; /* can't mod w/ 0 */
941 in_dev
->mr_maxdelay
= max_delay
;
943 in_dev
->mr_qrv
= ih3
->qrv
;
944 if (!group
) { /* general query */
946 return true; /* no sources allowed */
947 igmp_gq_start_timer(in_dev
);
950 /* mark sources to include, if group & source-specific */
951 mark
= ih3
->nsrcs
!= 0;
955 * - Start the timers in all of our membership records
956 * that the query applies to for the interface on
957 * which the query arrived excl. those that belong
958 * to a "local" group (224.0.0.X)
959 * - For timers already running check if they need to
961 * - Use the igmp->igmp_code field as the maximum
965 for_each_pmc_rcu(in_dev
, im
) {
968 if (group
&& group
!= im
->multiaddr
)
970 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
972 if (ipv4_is_local_multicast(im
->multiaddr
) &&
973 !net
->ipv4
.sysctl_igmp_llm_reports
)
975 spin_lock_bh(&im
->lock
);
977 im
->gsquery
= im
->gsquery
&& mark
;
980 changed
= !im
->gsquery
||
981 igmp_marksources(im
, ntohs(ih3
->nsrcs
), ih3
->srcs
);
982 spin_unlock_bh(&im
->lock
);
984 igmp_mod_timer(im
, max_delay
);
990 /* called in rcu_read_lock() section */
991 int igmp_rcv(struct sk_buff
*skb
)
993 /* This basically follows the spec line by line -- see RFC1112 */
995 struct in_device
*in_dev
= __in_dev_get_rcu(skb
->dev
);
1002 if (!pskb_may_pull(skb
, sizeof(struct igmphdr
)))
1005 if (skb_checksum_simple_validate(skb
))
1010 case IGMP_HOST_MEMBERSHIP_QUERY
:
1011 dropped
= igmp_heard_query(in_dev
, skb
, len
);
1013 case IGMP_HOST_MEMBERSHIP_REPORT
:
1014 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1015 /* Is it our report looped back? */
1016 if (rt_is_output_route(skb_rtable(skb
)))
1018 /* don't rely on MC router hearing unicast reports */
1019 if (skb
->pkt_type
== PACKET_MULTICAST
||
1020 skb
->pkt_type
== PACKET_BROADCAST
)
1021 dropped
= igmp_heard_report(in_dev
, ih
->group
);
1024 #ifdef CONFIG_IP_PIMSM_V1
1025 return pim_rcv_v1(skb
);
1027 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1030 case IGMP_HOST_LEAVE_MESSAGE
:
1032 case IGMP_MTRACE_RESP
:
1050 * Add a filter to a device
1053 static void ip_mc_filter_add(struct in_device
*in_dev
, __be32 addr
)
1055 char buf
[MAX_ADDR_LEN
];
1056 struct net_device
*dev
= in_dev
->dev
;
1058 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1059 We will get multicast token leakage, when IFF_MULTICAST
1060 is changed. This check should be done in ndo_set_rx_mode
1061 routine. Something sort of:
1062 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1065 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1066 dev_mc_add(dev
, buf
);
1070 * Remove a filter from a device
1073 static void ip_mc_filter_del(struct in_device
*in_dev
, __be32 addr
)
1075 char buf
[MAX_ADDR_LEN
];
1076 struct net_device
*dev
= in_dev
->dev
;
1078 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1079 dev_mc_del(dev
, buf
);
1082 #ifdef CONFIG_IP_MULTICAST
1084 * deleted ip_mc_list manipulation
1086 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
)
1088 struct ip_mc_list
*pmc
;
1089 struct net
*net
= dev_net(in_dev
->dev
);
1091 /* this is an "ip_mc_list" for convenience; only the fields below
1092 * are actually used. In particular, the refcnt and users are not
1093 * used for management of the delete list. Using the same structure
1094 * for deleted items allows change reports to use common code with
1095 * non-deleted or query-response MCA's.
1097 pmc
= kzalloc(sizeof(*pmc
), GFP_KERNEL
);
1100 spin_lock_bh(&im
->lock
);
1101 pmc
->interface
= im
->interface
;
1102 in_dev_hold(in_dev
);
1103 pmc
->multiaddr
= im
->multiaddr
;
1104 pmc
->crcount
= in_dev
->mr_qrv
?: net
->ipv4
.sysctl_igmp_qrv
;
1105 pmc
->sfmode
= im
->sfmode
;
1106 if (pmc
->sfmode
== MCAST_INCLUDE
) {
1107 struct ip_sf_list
*psf
;
1109 pmc
->tomb
= im
->tomb
;
1110 pmc
->sources
= im
->sources
;
1111 im
->tomb
= im
->sources
= NULL
;
1112 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1113 psf
->sf_crcount
= pmc
->crcount
;
1115 spin_unlock_bh(&im
->lock
);
1117 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1118 pmc
->next
= in_dev
->mc_tomb
;
1119 in_dev
->mc_tomb
= pmc
;
1120 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1123 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
)
1125 struct ip_mc_list
*pmc
, *pmc_prev
;
1126 struct ip_sf_list
*psf
, *psf_next
;
1128 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1130 for (pmc
= in_dev
->mc_tomb
; pmc
; pmc
= pmc
->next
) {
1131 if (pmc
->multiaddr
== multiaddr
)
1137 pmc_prev
->next
= pmc
->next
;
1139 in_dev
->mc_tomb
= pmc
->next
;
1141 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1143 for (psf
= pmc
->tomb
; psf
; psf
= psf_next
) {
1144 psf_next
= psf
->sf_next
;
1147 in_dev_put(pmc
->interface
);
1152 static void igmpv3_clear_delrec(struct in_device
*in_dev
)
1154 struct ip_mc_list
*pmc
, *nextpmc
;
1156 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1157 pmc
= in_dev
->mc_tomb
;
1158 in_dev
->mc_tomb
= NULL
;
1159 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1161 for (; pmc
; pmc
= nextpmc
) {
1162 nextpmc
= pmc
->next
;
1163 ip_mc_clear_src(pmc
);
1164 in_dev_put(pmc
->interface
);
1167 /* clear dead sources, too */
1169 for_each_pmc_rcu(in_dev
, pmc
) {
1170 struct ip_sf_list
*psf
, *psf_next
;
1172 spin_lock_bh(&pmc
->lock
);
1175 spin_unlock_bh(&pmc
->lock
);
1176 for (; psf
; psf
= psf_next
) {
1177 psf_next
= psf
->sf_next
;
1185 static void igmp_group_dropped(struct ip_mc_list
*im
)
1187 struct in_device
*in_dev
= im
->interface
;
1188 #ifdef CONFIG_IP_MULTICAST
1189 struct net
*net
= dev_net(in_dev
->dev
);
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
) && !net
->ipv4
.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
;
1226 #ifdef CONFIG_IP_MULTICAST
1227 struct net
*net
= dev_net(in_dev
->dev
);
1230 if (im
->loaded
== 0) {
1232 ip_mc_filter_add(in_dev
, im
->multiaddr
);
1235 #ifdef CONFIG_IP_MULTICAST
1236 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1238 if (ipv4_is_local_multicast(im
->multiaddr
) && !net
->ipv4
.sysctl_igmp_llm_reports
)
1243 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
)) {
1244 spin_lock_bh(&im
->lock
);
1245 igmp_start_timer(im
, IGMP_INITIAL_REPORT_DELAY
);
1246 spin_unlock_bh(&im
->lock
);
1251 im
->crcount
= in_dev
->mr_qrv
?: net
->ipv4
.sysctl_igmp_qrv
;
1252 igmp_ifc_event(in_dev
);
1258 * Multicast list managers
1261 static u32
ip_mc_hash(const struct ip_mc_list
*im
)
1263 return hash_32((__force u32
)im
->multiaddr
, MC_HASH_SZ_LOG
);
1266 static void ip_mc_hash_add(struct in_device
*in_dev
,
1267 struct ip_mc_list
*im
)
1269 struct ip_mc_list __rcu
**mc_hash
;
1272 mc_hash
= rtnl_dereference(in_dev
->mc_hash
);
1274 hash
= ip_mc_hash(im
);
1275 im
->next_hash
= mc_hash
[hash
];
1276 rcu_assign_pointer(mc_hash
[hash
], im
);
1280 /* do not use a hash table for small number of items */
1281 if (in_dev
->mc_count
< 4)
1284 mc_hash
= kzalloc(sizeof(struct ip_mc_list
*) << MC_HASH_SZ_LOG
,
1289 for_each_pmc_rtnl(in_dev
, im
) {
1290 hash
= ip_mc_hash(im
);
1291 im
->next_hash
= mc_hash
[hash
];
1292 RCU_INIT_POINTER(mc_hash
[hash
], im
);
1295 rcu_assign_pointer(in_dev
->mc_hash
, mc_hash
);
1298 static void ip_mc_hash_remove(struct in_device
*in_dev
,
1299 struct ip_mc_list
*im
)
1301 struct ip_mc_list __rcu
**mc_hash
= rtnl_dereference(in_dev
->mc_hash
);
1302 struct ip_mc_list
*aux
;
1306 mc_hash
+= ip_mc_hash(im
);
1307 while ((aux
= rtnl_dereference(*mc_hash
)) != im
)
1308 mc_hash
= &aux
->next_hash
;
1309 *mc_hash
= im
->next_hash
;
1314 * A socket has joined a multicast group on device dev.
1317 void ip_mc_inc_group(struct in_device
*in_dev
, __be32 addr
)
1319 struct ip_mc_list
*im
;
1320 #ifdef CONFIG_IP_MULTICAST
1321 struct net
*net
= dev_net(in_dev
->dev
);
1326 for_each_pmc_rtnl(in_dev
, im
) {
1327 if (im
->multiaddr
== addr
) {
1329 ip_mc_add_src(in_dev
, &addr
, MCAST_EXCLUDE
, 0, NULL
, 0);
1334 im
= kzalloc(sizeof(*im
), GFP_KERNEL
);
1339 im
->interface
= in_dev
;
1340 in_dev_hold(in_dev
);
1341 im
->multiaddr
= addr
;
1342 /* initial mode is (EX, empty) */
1343 im
->sfmode
= MCAST_EXCLUDE
;
1344 im
->sfcount
[MCAST_EXCLUDE
] = 1;
1345 atomic_set(&im
->refcnt
, 1);
1346 spin_lock_init(&im
->lock
);
1347 #ifdef CONFIG_IP_MULTICAST
1348 setup_timer(&im
->timer
, igmp_timer_expire
, (unsigned long)im
);
1349 im
->unsolicit_count
= net
->ipv4
.sysctl_igmp_qrv
;
1352 im
->next_rcu
= in_dev
->mc_list
;
1354 rcu_assign_pointer(in_dev
->mc_list
, im
);
1356 ip_mc_hash_add(in_dev
, im
);
1358 #ifdef CONFIG_IP_MULTICAST
1359 igmpv3_del_delrec(in_dev
, im
->multiaddr
);
1361 igmp_group_added(im
);
1363 ip_rt_multicast_event(in_dev
);
1367 EXPORT_SYMBOL(ip_mc_inc_group
);
1369 static int ip_mc_check_iphdr(struct sk_buff
*skb
)
1371 const struct iphdr
*iph
;
1373 unsigned int offset
= skb_network_offset(skb
) + sizeof(*iph
);
1375 if (!pskb_may_pull(skb
, offset
))
1380 if (iph
->version
!= 4 || ip_hdrlen(skb
) < sizeof(*iph
))
1383 offset
+= ip_hdrlen(skb
) - sizeof(*iph
);
1385 if (!pskb_may_pull(skb
, offset
))
1390 if (unlikely(ip_fast_csum((u8
*)iph
, iph
->ihl
)))
1393 len
= skb_network_offset(skb
) + ntohs(iph
->tot_len
);
1394 if (skb
->len
< len
|| len
< offset
)
1397 skb_set_transport_header(skb
, offset
);
1402 static int ip_mc_check_igmp_reportv3(struct sk_buff
*skb
)
1404 unsigned int len
= skb_transport_offset(skb
);
1406 len
+= sizeof(struct igmpv3_report
);
1408 return pskb_may_pull(skb
, len
) ? 0 : -EINVAL
;
1411 static int ip_mc_check_igmp_query(struct sk_buff
*skb
)
1413 unsigned int len
= skb_transport_offset(skb
);
1415 len
+= sizeof(struct igmphdr
);
1420 if (skb
->len
!= len
) {
1422 len
+= sizeof(struct igmpv3_query
) - sizeof(struct igmphdr
);
1423 if (skb
->len
< len
|| !pskb_may_pull(skb
, len
))
1427 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1428 * all-systems destination addresses (224.0.0.1) for general queries
1430 if (!igmp_hdr(skb
)->group
&&
1431 ip_hdr(skb
)->daddr
!= htonl(INADDR_ALLHOSTS_GROUP
))
1437 static int ip_mc_check_igmp_msg(struct sk_buff
*skb
)
1439 switch (igmp_hdr(skb
)->type
) {
1440 case IGMP_HOST_LEAVE_MESSAGE
:
1441 case IGMP_HOST_MEMBERSHIP_REPORT
:
1442 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1445 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1446 return ip_mc_check_igmp_reportv3(skb
);
1447 case IGMP_HOST_MEMBERSHIP_QUERY
:
1448 return ip_mc_check_igmp_query(skb
);
1454 static inline __sum16
ip_mc_validate_checksum(struct sk_buff
*skb
)
1456 return skb_checksum_simple_validate(skb
);
1459 static int __ip_mc_check_igmp(struct sk_buff
*skb
, struct sk_buff
**skb_trimmed
)
1462 struct sk_buff
*skb_chk
;
1463 unsigned int transport_len
;
1464 unsigned int len
= skb_transport_offset(skb
) + sizeof(struct igmphdr
);
1467 transport_len
= ntohs(ip_hdr(skb
)->tot_len
) - ip_hdrlen(skb
);
1469 skb_chk
= skb_checksum_trimmed(skb
, transport_len
,
1470 ip_mc_validate_checksum
);
1474 if (!pskb_may_pull(skb_chk
, len
))
1477 ret
= ip_mc_check_igmp_msg(skb_chk
);
1482 *skb_trimmed
= skb_chk
;
1483 /* free now unneeded clone */
1484 else if (skb_chk
!= skb
)
1490 if (ret
&& skb_chk
&& skb_chk
!= skb
)
1497 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1498 * @skb: the skb to validate
1499 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1501 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1502 * skb transport header accordingly and returns zero.
1504 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1506 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1507 * -ENOMEM: A memory allocation failure happened.
1509 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1510 * to NULL): After parsing an IGMP packet successfully it will point to
1511 * an skb which has its tail aligned to the IP packet end. This might
1512 * either be the originally provided skb or a trimmed, cloned version if
1513 * the skb frame had data beyond the IP packet. A cloned skb allows us
1514 * to leave the original skb and its full frame unchanged (which might be
1515 * desirable for layer 2 frame jugglers).
1517 * Caller needs to set the skb network header and free any returned skb if it
1518 * differs from the provided skb.
1520 int ip_mc_check_igmp(struct sk_buff
*skb
, struct sk_buff
**skb_trimmed
)
1522 int ret
= ip_mc_check_iphdr(skb
);
1527 if (ip_hdr(skb
)->protocol
!= IPPROTO_IGMP
)
1530 return __ip_mc_check_igmp(skb
, skb_trimmed
);
1532 EXPORT_SYMBOL(ip_mc_check_igmp
);
1535 * Resend IGMP JOIN report; used by netdev notifier.
1537 static void ip_mc_rejoin_groups(struct in_device
*in_dev
)
1539 #ifdef CONFIG_IP_MULTICAST
1540 struct ip_mc_list
*im
;
1542 struct net
*net
= dev_net(in_dev
->dev
);
1546 for_each_pmc_rtnl(in_dev
, im
) {
1547 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1549 if (ipv4_is_local_multicast(im
->multiaddr
) &&
1550 !net
->ipv4
.sysctl_igmp_llm_reports
)
1553 /* a failover is happening and switches
1554 * must be notified immediately
1556 if (IGMP_V1_SEEN(in_dev
))
1557 type
= IGMP_HOST_MEMBERSHIP_REPORT
;
1558 else if (IGMP_V2_SEEN(in_dev
))
1559 type
= IGMPV2_HOST_MEMBERSHIP_REPORT
;
1561 type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
1562 igmp_send_report(in_dev
, im
, type
);
1568 * A socket has left a multicast group on device dev
1571 void ip_mc_dec_group(struct in_device
*in_dev
, __be32 addr
)
1573 struct ip_mc_list
*i
;
1574 struct ip_mc_list __rcu
**ip
;
1578 for (ip
= &in_dev
->mc_list
;
1579 (i
= rtnl_dereference(*ip
)) != NULL
;
1580 ip
= &i
->next_rcu
) {
1581 if (i
->multiaddr
== addr
) {
1582 if (--i
->users
== 0) {
1583 ip_mc_hash_remove(in_dev
, i
);
1586 igmp_group_dropped(i
);
1590 ip_rt_multicast_event(in_dev
);
1599 EXPORT_SYMBOL(ip_mc_dec_group
);
1601 /* Device changing type */
1603 void ip_mc_unmap(struct in_device
*in_dev
)
1605 struct ip_mc_list
*pmc
;
1609 for_each_pmc_rtnl(in_dev
, pmc
)
1610 igmp_group_dropped(pmc
);
1613 void ip_mc_remap(struct in_device
*in_dev
)
1615 struct ip_mc_list
*pmc
;
1619 for_each_pmc_rtnl(in_dev
, pmc
)
1620 igmp_group_added(pmc
);
1623 /* Device going down */
1625 void ip_mc_down(struct in_device
*in_dev
)
1627 struct ip_mc_list
*pmc
;
1631 for_each_pmc_rtnl(in_dev
, pmc
)
1632 igmp_group_dropped(pmc
);
1634 #ifdef CONFIG_IP_MULTICAST
1635 in_dev
->mr_ifc_count
= 0;
1636 if (del_timer(&in_dev
->mr_ifc_timer
))
1637 __in_dev_put(in_dev
);
1638 in_dev
->mr_gq_running
= 0;
1639 if (del_timer(&in_dev
->mr_gq_timer
))
1640 __in_dev_put(in_dev
);
1641 igmpv3_clear_delrec(in_dev
);
1644 ip_mc_dec_group(in_dev
, IGMP_ALL_HOSTS
);
1647 void ip_mc_init_dev(struct in_device
*in_dev
)
1649 #ifdef CONFIG_IP_MULTICAST
1650 struct net
*net
= dev_net(in_dev
->dev
);
1654 #ifdef CONFIG_IP_MULTICAST
1655 setup_timer(&in_dev
->mr_gq_timer
, igmp_gq_timer_expire
,
1656 (unsigned long)in_dev
);
1657 setup_timer(&in_dev
->mr_ifc_timer
, igmp_ifc_timer_expire
,
1658 (unsigned long)in_dev
);
1659 in_dev
->mr_qrv
= net
->ipv4
.sysctl_igmp_qrv
;
1662 spin_lock_init(&in_dev
->mc_tomb_lock
);
1665 /* Device going up */
1667 void ip_mc_up(struct in_device
*in_dev
)
1669 struct ip_mc_list
*pmc
;
1670 #ifdef CONFIG_IP_MULTICAST
1671 struct net
*net
= dev_net(in_dev
->dev
);
1676 #ifdef CONFIG_IP_MULTICAST
1677 in_dev
->mr_qrv
= net
->ipv4
.sysctl_igmp_qrv
;
1679 ip_mc_inc_group(in_dev
, IGMP_ALL_HOSTS
);
1681 for_each_pmc_rtnl(in_dev
, pmc
)
1682 igmp_group_added(pmc
);
1686 * Device is about to be destroyed: clean up.
1689 void ip_mc_destroy_dev(struct in_device
*in_dev
)
1691 struct ip_mc_list
*i
;
1695 /* Deactivate timers */
1698 while ((i
= rtnl_dereference(in_dev
->mc_list
)) != NULL
) {
1699 in_dev
->mc_list
= i
->next_rcu
;
1702 /* We've dropped the groups in ip_mc_down already */
1708 /* RTNL is locked */
1709 static struct in_device
*ip_mc_find_dev(struct net
*net
, struct ip_mreqn
*imr
)
1711 struct net_device
*dev
= NULL
;
1712 struct in_device
*idev
= NULL
;
1714 if (imr
->imr_ifindex
) {
1715 idev
= inetdev_by_index(net
, imr
->imr_ifindex
);
1718 if (imr
->imr_address
.s_addr
) {
1719 dev
= __ip_dev_find(net
, imr
->imr_address
.s_addr
, false);
1725 struct rtable
*rt
= ip_route_output(net
,
1726 imr
->imr_multiaddr
.s_addr
,
1734 imr
->imr_ifindex
= dev
->ifindex
;
1735 idev
= __in_dev_get_rtnl(dev
);
1741 * Join a socket to a group
1744 static int ip_mc_del1_src(struct ip_mc_list
*pmc
, int sfmode
,
1747 struct ip_sf_list
*psf
, *psf_prev
;
1751 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1752 if (psf
->sf_inaddr
== *psfsrc
)
1756 if (!psf
|| psf
->sf_count
[sfmode
] == 0) {
1757 /* source filter not found, or count wrong => bug */
1760 psf
->sf_count
[sfmode
]--;
1761 if (psf
->sf_count
[sfmode
] == 0) {
1762 ip_rt_multicast_event(pmc
->interface
);
1764 if (!psf
->sf_count
[MCAST_INCLUDE
] && !psf
->sf_count
[MCAST_EXCLUDE
]) {
1765 #ifdef CONFIG_IP_MULTICAST
1766 struct in_device
*in_dev
= pmc
->interface
;
1767 struct net
*net
= dev_net(in_dev
->dev
);
1770 /* no more filters for this source */
1772 psf_prev
->sf_next
= psf
->sf_next
;
1774 pmc
->sources
= psf
->sf_next
;
1775 #ifdef CONFIG_IP_MULTICAST
1776 if (psf
->sf_oldin
&&
1777 !IGMP_V1_SEEN(in_dev
) && !IGMP_V2_SEEN(in_dev
)) {
1778 psf
->sf_crcount
= in_dev
->mr_qrv
?: net
->ipv4
.sysctl_igmp_qrv
;
1779 psf
->sf_next
= pmc
->tomb
;
1789 #ifndef CONFIG_IP_MULTICAST
1790 #define igmp_ifc_event(x) do { } while (0)
1793 static int ip_mc_del_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1794 int sfcount
, __be32
*psfsrc
, int delta
)
1796 struct ip_mc_list
*pmc
;
1803 for_each_pmc_rcu(in_dev
, pmc
) {
1804 if (*pmca
== pmc
->multiaddr
)
1808 /* MCA not found?? bug */
1812 spin_lock_bh(&pmc
->lock
);
1814 #ifdef CONFIG_IP_MULTICAST
1819 if (!pmc
->sfcount
[sfmode
])
1821 pmc
->sfcount
[sfmode
]--;
1824 for (i
= 0; i
< sfcount
; i
++) {
1825 int rv
= ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1827 changerec
|= rv
> 0;
1831 if (pmc
->sfmode
== MCAST_EXCLUDE
&&
1832 pmc
->sfcount
[MCAST_EXCLUDE
] == 0 &&
1833 pmc
->sfcount
[MCAST_INCLUDE
]) {
1834 #ifdef CONFIG_IP_MULTICAST
1835 struct ip_sf_list
*psf
;
1836 struct net
*net
= dev_net(in_dev
->dev
);
1839 /* filter mode change */
1840 pmc
->sfmode
= MCAST_INCLUDE
;
1841 #ifdef CONFIG_IP_MULTICAST
1842 pmc
->crcount
= in_dev
->mr_qrv
?: net
->ipv4
.sysctl_igmp_qrv
;
1843 in_dev
->mr_ifc_count
= pmc
->crcount
;
1844 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1845 psf
->sf_crcount
= 0;
1846 igmp_ifc_event(pmc
->interface
);
1847 } else if (sf_setstate(pmc
) || changerec
) {
1848 igmp_ifc_event(pmc
->interface
);
1852 spin_unlock_bh(&pmc
->lock
);
1857 * Add multicast single-source filter to the interface list
1859 static int ip_mc_add1_src(struct ip_mc_list
*pmc
, int sfmode
,
1862 struct ip_sf_list
*psf
, *psf_prev
;
1865 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1866 if (psf
->sf_inaddr
== *psfsrc
)
1871 psf
= kzalloc(sizeof(*psf
), GFP_ATOMIC
);
1874 psf
->sf_inaddr
= *psfsrc
;
1876 psf_prev
->sf_next
= psf
;
1880 psf
->sf_count
[sfmode
]++;
1881 if (psf
->sf_count
[sfmode
] == 1) {
1882 ip_rt_multicast_event(pmc
->interface
);
1887 #ifdef CONFIG_IP_MULTICAST
1888 static void sf_markstate(struct ip_mc_list
*pmc
)
1890 struct ip_sf_list
*psf
;
1891 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1893 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1894 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1895 psf
->sf_oldin
= mca_xcount
==
1896 psf
->sf_count
[MCAST_EXCLUDE
] &&
1897 !psf
->sf_count
[MCAST_INCLUDE
];
1899 psf
->sf_oldin
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1902 static int sf_setstate(struct ip_mc_list
*pmc
)
1904 struct ip_sf_list
*psf
, *dpsf
;
1905 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1906 int qrv
= pmc
->interface
->mr_qrv
;
1910 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1911 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1912 new_in
= mca_xcount
== psf
->sf_count
[MCAST_EXCLUDE
] &&
1913 !psf
->sf_count
[MCAST_INCLUDE
];
1915 new_in
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1917 if (!psf
->sf_oldin
) {
1918 struct ip_sf_list
*prev
= NULL
;
1920 for (dpsf
= pmc
->tomb
; dpsf
; dpsf
= dpsf
->sf_next
) {
1921 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1927 prev
->sf_next
= dpsf
->sf_next
;
1929 pmc
->tomb
= dpsf
->sf_next
;
1932 psf
->sf_crcount
= qrv
;
1935 } else if (psf
->sf_oldin
) {
1937 psf
->sf_crcount
= 0;
1939 * add or update "delete" records if an active filter
1942 for (dpsf
= pmc
->tomb
; dpsf
; dpsf
= dpsf
->sf_next
)
1943 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1946 dpsf
= kmalloc(sizeof(*dpsf
), GFP_ATOMIC
);
1950 /* pmc->lock held by callers */
1951 dpsf
->sf_next
= pmc
->tomb
;
1954 dpsf
->sf_crcount
= qrv
;
1963 * Add multicast source filter list to the interface list
1965 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1966 int sfcount
, __be32
*psfsrc
, int delta
)
1968 struct ip_mc_list
*pmc
;
1975 for_each_pmc_rcu(in_dev
, pmc
) {
1976 if (*pmca
== pmc
->multiaddr
)
1980 /* MCA not found?? bug */
1984 spin_lock_bh(&pmc
->lock
);
1987 #ifdef CONFIG_IP_MULTICAST
1990 isexclude
= pmc
->sfmode
== MCAST_EXCLUDE
;
1992 pmc
->sfcount
[sfmode
]++;
1994 for (i
= 0; i
< sfcount
; i
++) {
1995 err
= ip_mc_add1_src(pmc
, sfmode
, &psfsrc
[i
]);
2003 pmc
->sfcount
[sfmode
]--;
2004 for (j
= 0; j
< i
; j
++)
2005 (void) ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[j
]);
2006 } else if (isexclude
!= (pmc
->sfcount
[MCAST_EXCLUDE
] != 0)) {
2007 #ifdef CONFIG_IP_MULTICAST
2008 struct ip_sf_list
*psf
;
2009 struct net
*net
= dev_net(pmc
->interface
->dev
);
2010 in_dev
= pmc
->interface
;
2013 /* filter mode change */
2014 if (pmc
->sfcount
[MCAST_EXCLUDE
])
2015 pmc
->sfmode
= MCAST_EXCLUDE
;
2016 else if (pmc
->sfcount
[MCAST_INCLUDE
])
2017 pmc
->sfmode
= MCAST_INCLUDE
;
2018 #ifdef CONFIG_IP_MULTICAST
2019 /* else no filters; keep old mode for reports */
2021 pmc
->crcount
= in_dev
->mr_qrv
?: net
->ipv4
.sysctl_igmp_qrv
;
2022 in_dev
->mr_ifc_count
= pmc
->crcount
;
2023 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
2024 psf
->sf_crcount
= 0;
2025 igmp_ifc_event(in_dev
);
2026 } else if (sf_setstate(pmc
)) {
2027 igmp_ifc_event(in_dev
);
2030 spin_unlock_bh(&pmc
->lock
);
2034 static void ip_mc_clear_src(struct ip_mc_list
*pmc
)
2036 struct ip_sf_list
*psf
, *nextpsf
;
2038 for (psf
= pmc
->tomb
; psf
; psf
= nextpsf
) {
2039 nextpsf
= psf
->sf_next
;
2043 for (psf
= pmc
->sources
; psf
; psf
= nextpsf
) {
2044 nextpsf
= psf
->sf_next
;
2047 pmc
->sources
= NULL
;
2048 pmc
->sfmode
= MCAST_EXCLUDE
;
2049 pmc
->sfcount
[MCAST_INCLUDE
] = 0;
2050 pmc
->sfcount
[MCAST_EXCLUDE
] = 1;
2053 /* Join a multicast group
2056 int ip_mc_join_group(struct sock
*sk
, struct ip_mreqn
*imr
)
2058 __be32 addr
= imr
->imr_multiaddr
.s_addr
;
2059 struct ip_mc_socklist
*iml
, *i
;
2060 struct in_device
*in_dev
;
2061 struct inet_sock
*inet
= inet_sk(sk
);
2062 struct net
*net
= sock_net(sk
);
2069 if (!ipv4_is_multicast(addr
))
2072 in_dev
= ip_mc_find_dev(net
, imr
);
2080 ifindex
= imr
->imr_ifindex
;
2081 for_each_pmc_rtnl(inet
, i
) {
2082 if (i
->multi
.imr_multiaddr
.s_addr
== addr
&&
2083 i
->multi
.imr_ifindex
== ifindex
)
2088 if (count
>= net
->ipv4
.sysctl_igmp_max_memberships
)
2090 iml
= sock_kmalloc(sk
, sizeof(*iml
), GFP_KERNEL
);
2094 memcpy(&iml
->multi
, imr
, sizeof(*imr
));
2095 iml
->next_rcu
= inet
->mc_list
;
2097 iml
->sfmode
= MCAST_EXCLUDE
;
2098 rcu_assign_pointer(inet
->mc_list
, iml
);
2099 ip_mc_inc_group(in_dev
, addr
);
2104 EXPORT_SYMBOL(ip_mc_join_group
);
2106 static int ip_mc_leave_src(struct sock
*sk
, struct ip_mc_socklist
*iml
,
2107 struct in_device
*in_dev
)
2109 struct ip_sf_socklist
*psf
= rtnl_dereference(iml
->sflist
);
2113 /* any-source empty exclude case */
2114 return ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
2115 iml
->sfmode
, 0, NULL
, 0);
2117 err
= ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
2118 iml
->sfmode
, psf
->sl_count
, psf
->sl_addr
, 0);
2119 RCU_INIT_POINTER(iml
->sflist
, NULL
);
2120 /* decrease mem now to avoid the memleak warning */
2121 atomic_sub(IP_SFLSIZE(psf
->sl_max
), &sk
->sk_omem_alloc
);
2122 kfree_rcu(psf
, rcu
);
2126 int ip_mc_leave_group(struct sock
*sk
, struct ip_mreqn
*imr
)
2128 struct inet_sock
*inet
= inet_sk(sk
);
2129 struct ip_mc_socklist
*iml
;
2130 struct ip_mc_socklist __rcu
**imlp
;
2131 struct in_device
*in_dev
;
2132 struct net
*net
= sock_net(sk
);
2133 __be32 group
= imr
->imr_multiaddr
.s_addr
;
2135 int ret
= -EADDRNOTAVAIL
;
2139 in_dev
= ip_mc_find_dev(net
, imr
);
2140 if (!imr
->imr_ifindex
&& !imr
->imr_address
.s_addr
&& !in_dev
) {
2144 ifindex
= imr
->imr_ifindex
;
2145 for (imlp
= &inet
->mc_list
;
2146 (iml
= rtnl_dereference(*imlp
)) != NULL
;
2147 imlp
= &iml
->next_rcu
) {
2148 if (iml
->multi
.imr_multiaddr
.s_addr
!= group
)
2151 if (iml
->multi
.imr_ifindex
!= ifindex
)
2153 } else if (imr
->imr_address
.s_addr
&& imr
->imr_address
.s_addr
!=
2154 iml
->multi
.imr_address
.s_addr
)
2157 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2159 *imlp
= iml
->next_rcu
;
2162 ip_mc_dec_group(in_dev
, group
);
2164 /* decrease mem now to avoid the memleak warning */
2165 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
2166 kfree_rcu(iml
, rcu
);
2172 EXPORT_SYMBOL(ip_mc_leave_group
);
2174 int ip_mc_source(int add
, int omode
, struct sock
*sk
, struct
2175 ip_mreq_source
*mreqs
, int ifindex
)
2178 struct ip_mreqn imr
;
2179 __be32 addr
= mreqs
->imr_multiaddr
;
2180 struct ip_mc_socklist
*pmc
;
2181 struct in_device
*in_dev
= NULL
;
2182 struct inet_sock
*inet
= inet_sk(sk
);
2183 struct ip_sf_socklist
*psl
;
2184 struct net
*net
= sock_net(sk
);
2188 if (!ipv4_is_multicast(addr
))
2193 imr
.imr_multiaddr
.s_addr
= mreqs
->imr_multiaddr
;
2194 imr
.imr_address
.s_addr
= mreqs
->imr_interface
;
2195 imr
.imr_ifindex
= ifindex
;
2196 in_dev
= ip_mc_find_dev(net
, &imr
);
2202 err
= -EADDRNOTAVAIL
;
2204 for_each_pmc_rtnl(inet
, pmc
) {
2205 if ((pmc
->multi
.imr_multiaddr
.s_addr
==
2206 imr
.imr_multiaddr
.s_addr
) &&
2207 (pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
))
2210 if (!pmc
) { /* must have a prior join */
2214 /* if a source filter was set, must be the same mode as before */
2216 if (pmc
->sfmode
!= omode
) {
2220 } else if (pmc
->sfmode
!= omode
) {
2221 /* allow mode switches for empty-set filters */
2222 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 0, NULL
, 0);
2223 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, pmc
->sfmode
, 0,
2225 pmc
->sfmode
= omode
;
2228 psl
= rtnl_dereference(pmc
->sflist
);
2231 goto done
; /* err = -EADDRNOTAVAIL */
2233 for (i
= 0; i
< psl
->sl_count
; i
++) {
2234 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2239 if (rv
) /* source not found */
2240 goto done
; /* err = -EADDRNOTAVAIL */
2242 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2243 if (psl
->sl_count
== 1 && omode
== MCAST_INCLUDE
) {
2248 /* update the interface filter */
2249 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2250 &mreqs
->imr_sourceaddr
, 1);
2252 for (j
= i
+1; j
< psl
->sl_count
; j
++)
2253 psl
->sl_addr
[j
-1] = psl
->sl_addr
[j
];
2258 /* else, add a new source to the filter */
2260 if (psl
&& psl
->sl_count
>= net
->ipv4
.sysctl_igmp_max_msf
) {
2264 if (!psl
|| psl
->sl_count
== psl
->sl_max
) {
2265 struct ip_sf_socklist
*newpsl
;
2266 int count
= IP_SFBLOCK
;
2269 count
+= psl
->sl_max
;
2270 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(count
), GFP_KERNEL
);
2275 newpsl
->sl_max
= count
;
2276 newpsl
->sl_count
= count
- IP_SFBLOCK
;
2278 for (i
= 0; i
< psl
->sl_count
; i
++)
2279 newpsl
->sl_addr
[i
] = psl
->sl_addr
[i
];
2280 /* decrease mem now to avoid the memleak warning */
2281 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2282 kfree_rcu(psl
, rcu
);
2284 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2287 rv
= 1; /* > 0 for insert logic below if sl_count is 0 */
2288 for (i
= 0; i
< psl
->sl_count
; i
++) {
2289 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2294 if (rv
== 0) /* address already there is an error */
2296 for (j
= psl
->sl_count
-1; j
>= i
; j
--)
2297 psl
->sl_addr
[j
+1] = psl
->sl_addr
[j
];
2298 psl
->sl_addr
[i
] = mreqs
->imr_sourceaddr
;
2301 /* update the interface list */
2302 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2303 &mreqs
->imr_sourceaddr
, 1);
2306 err
= ip_mc_leave_group(sk
, &imr
);
2310 int ip_mc_msfilter(struct sock
*sk
, struct ip_msfilter
*msf
, int ifindex
)
2313 struct ip_mreqn imr
;
2314 __be32 addr
= msf
->imsf_multiaddr
;
2315 struct ip_mc_socklist
*pmc
;
2316 struct in_device
*in_dev
;
2317 struct inet_sock
*inet
= inet_sk(sk
);
2318 struct ip_sf_socklist
*newpsl
, *psl
;
2319 struct net
*net
= sock_net(sk
);
2322 if (!ipv4_is_multicast(addr
))
2324 if (msf
->imsf_fmode
!= MCAST_INCLUDE
&&
2325 msf
->imsf_fmode
!= MCAST_EXCLUDE
)
2330 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2331 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2332 imr
.imr_ifindex
= ifindex
;
2333 in_dev
= ip_mc_find_dev(net
, &imr
);
2340 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2341 if (msf
->imsf_fmode
== MCAST_INCLUDE
&& msf
->imsf_numsrc
== 0) {
2346 for_each_pmc_rtnl(inet
, pmc
) {
2347 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2348 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2351 if (!pmc
) { /* must have a prior join */
2355 if (msf
->imsf_numsrc
) {
2356 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(msf
->imsf_numsrc
),
2362 newpsl
->sl_max
= newpsl
->sl_count
= msf
->imsf_numsrc
;
2363 memcpy(newpsl
->sl_addr
, msf
->imsf_slist
,
2364 msf
->imsf_numsrc
* sizeof(msf
->imsf_slist
[0]));
2365 err
= ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2366 msf
->imsf_fmode
, newpsl
->sl_count
, newpsl
->sl_addr
, 0);
2368 sock_kfree_s(sk
, newpsl
, IP_SFLSIZE(newpsl
->sl_max
));
2373 (void) ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2374 msf
->imsf_fmode
, 0, NULL
, 0);
2376 psl
= rtnl_dereference(pmc
->sflist
);
2378 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2379 psl
->sl_count
, psl
->sl_addr
, 0);
2380 /* decrease mem now to avoid the memleak warning */
2381 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2382 kfree_rcu(psl
, rcu
);
2384 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2386 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2387 pmc
->sfmode
= msf
->imsf_fmode
;
2391 err
= ip_mc_leave_group(sk
, &imr
);
2395 int ip_mc_msfget(struct sock
*sk
, struct ip_msfilter
*msf
,
2396 struct ip_msfilter __user
*optval
, int __user
*optlen
)
2398 int err
, len
, count
, copycount
;
2399 struct ip_mreqn imr
;
2400 __be32 addr
= msf
->imsf_multiaddr
;
2401 struct ip_mc_socklist
*pmc
;
2402 struct in_device
*in_dev
;
2403 struct inet_sock
*inet
= inet_sk(sk
);
2404 struct ip_sf_socklist
*psl
;
2405 struct net
*net
= sock_net(sk
);
2409 if (!ipv4_is_multicast(addr
))
2412 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2413 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2414 imr
.imr_ifindex
= 0;
2415 in_dev
= ip_mc_find_dev(net
, &imr
);
2421 err
= -EADDRNOTAVAIL
;
2423 for_each_pmc_rtnl(inet
, pmc
) {
2424 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2425 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2428 if (!pmc
) /* must have a prior join */
2430 msf
->imsf_fmode
= pmc
->sfmode
;
2431 psl
= rtnl_dereference(pmc
->sflist
);
2436 count
= psl
->sl_count
;
2438 copycount
= count
< msf
->imsf_numsrc
? count
: msf
->imsf_numsrc
;
2439 len
= copycount
* sizeof(psl
->sl_addr
[0]);
2440 msf
->imsf_numsrc
= count
;
2441 if (put_user(IP_MSFILTER_SIZE(copycount
), optlen
) ||
2442 copy_to_user(optval
, msf
, IP_MSFILTER_SIZE(0))) {
2446 copy_to_user(&optval
->imsf_slist
[0], psl
->sl_addr
, len
))
2453 int ip_mc_gsfget(struct sock
*sk
, struct group_filter
*gsf
,
2454 struct group_filter __user
*optval
, int __user
*optlen
)
2456 int err
, i
, count
, copycount
;
2457 struct sockaddr_in
*psin
;
2459 struct ip_mc_socklist
*pmc
;
2460 struct inet_sock
*inet
= inet_sk(sk
);
2461 struct ip_sf_socklist
*psl
;
2465 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
2466 if (psin
->sin_family
!= AF_INET
)
2468 addr
= psin
->sin_addr
.s_addr
;
2469 if (!ipv4_is_multicast(addr
))
2472 err
= -EADDRNOTAVAIL
;
2474 for_each_pmc_rtnl(inet
, pmc
) {
2475 if (pmc
->multi
.imr_multiaddr
.s_addr
== addr
&&
2476 pmc
->multi
.imr_ifindex
== gsf
->gf_interface
)
2479 if (!pmc
) /* must have a prior join */
2481 gsf
->gf_fmode
= pmc
->sfmode
;
2482 psl
= rtnl_dereference(pmc
->sflist
);
2483 count
= psl
? psl
->sl_count
: 0;
2484 copycount
= count
< gsf
->gf_numsrc
? count
: gsf
->gf_numsrc
;
2485 gsf
->gf_numsrc
= count
;
2486 if (put_user(GROUP_FILTER_SIZE(copycount
), optlen
) ||
2487 copy_to_user(optval
, gsf
, GROUP_FILTER_SIZE(0))) {
2490 for (i
= 0; i
< copycount
; i
++) {
2491 struct sockaddr_storage ss
;
2493 psin
= (struct sockaddr_in
*)&ss
;
2494 memset(&ss
, 0, sizeof(ss
));
2495 psin
->sin_family
= AF_INET
;
2496 psin
->sin_addr
.s_addr
= psl
->sl_addr
[i
];
2497 if (copy_to_user(&optval
->gf_slist
[i
], &ss
, sizeof(ss
)))
2506 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2508 int ip_mc_sf_allow(struct sock
*sk
, __be32 loc_addr
, __be32 rmt_addr
, int dif
)
2510 struct inet_sock
*inet
= inet_sk(sk
);
2511 struct ip_mc_socklist
*pmc
;
2512 struct ip_sf_socklist
*psl
;
2517 if (!ipv4_is_multicast(loc_addr
))
2521 for_each_pmc_rcu(inet
, pmc
) {
2522 if (pmc
->multi
.imr_multiaddr
.s_addr
== loc_addr
&&
2523 pmc
->multi
.imr_ifindex
== dif
)
2529 psl
= rcu_dereference(pmc
->sflist
);
2530 ret
= (pmc
->sfmode
== MCAST_EXCLUDE
);
2534 for (i
= 0; i
< psl
->sl_count
; i
++) {
2535 if (psl
->sl_addr
[i
] == rmt_addr
)
2539 if (pmc
->sfmode
== MCAST_INCLUDE
&& i
>= psl
->sl_count
)
2541 if (pmc
->sfmode
== MCAST_EXCLUDE
&& i
< psl
->sl_count
)
2551 * A socket is closing.
2554 void ip_mc_drop_socket(struct sock
*sk
)
2556 struct inet_sock
*inet
= inet_sk(sk
);
2557 struct ip_mc_socklist
*iml
;
2558 struct net
*net
= sock_net(sk
);
2564 while ((iml
= rtnl_dereference(inet
->mc_list
)) != NULL
) {
2565 struct in_device
*in_dev
;
2567 inet
->mc_list
= iml
->next_rcu
;
2568 in_dev
= inetdev_by_index(net
, iml
->multi
.imr_ifindex
);
2569 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2571 ip_mc_dec_group(in_dev
, iml
->multi
.imr_multiaddr
.s_addr
);
2572 /* decrease mem now to avoid the memleak warning */
2573 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
2574 kfree_rcu(iml
, rcu
);
2579 /* called with rcu_read_lock() */
2580 int ip_check_mc_rcu(struct in_device
*in_dev
, __be32 mc_addr
, __be32 src_addr
, u8 proto
)
2582 struct ip_mc_list
*im
;
2583 struct ip_mc_list __rcu
**mc_hash
;
2584 struct ip_sf_list
*psf
;
2587 mc_hash
= rcu_dereference(in_dev
->mc_hash
);
2589 u32 hash
= hash_32((__force u32
)mc_addr
, MC_HASH_SZ_LOG
);
2591 for (im
= rcu_dereference(mc_hash
[hash
]);
2593 im
= rcu_dereference(im
->next_hash
)) {
2594 if (im
->multiaddr
== mc_addr
)
2598 for_each_pmc_rcu(in_dev
, im
) {
2599 if (im
->multiaddr
== mc_addr
)
2603 if (im
&& proto
== IPPROTO_IGMP
) {
2607 for (psf
= im
->sources
; psf
; psf
= psf
->sf_next
) {
2608 if (psf
->sf_inaddr
== src_addr
)
2612 rv
= psf
->sf_count
[MCAST_INCLUDE
] ||
2613 psf
->sf_count
[MCAST_EXCLUDE
] !=
2614 im
->sfcount
[MCAST_EXCLUDE
];
2616 rv
= im
->sfcount
[MCAST_EXCLUDE
] != 0;
2618 rv
= 1; /* unspecified source; tentatively allow */
2623 #if defined(CONFIG_PROC_FS)
2624 struct igmp_mc_iter_state
{
2625 struct seq_net_private p
;
2626 struct net_device
*dev
;
2627 struct in_device
*in_dev
;
2630 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2632 static inline struct ip_mc_list
*igmp_mc_get_first(struct seq_file
*seq
)
2634 struct net
*net
= seq_file_net(seq
);
2635 struct ip_mc_list
*im
= NULL
;
2636 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2638 state
->in_dev
= NULL
;
2639 for_each_netdev_rcu(net
, state
->dev
) {
2640 struct in_device
*in_dev
;
2642 in_dev
= __in_dev_get_rcu(state
->dev
);
2645 im
= rcu_dereference(in_dev
->mc_list
);
2647 state
->in_dev
= in_dev
;
2654 static struct ip_mc_list
*igmp_mc_get_next(struct seq_file
*seq
, struct ip_mc_list
*im
)
2656 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2658 im
= rcu_dereference(im
->next_rcu
);
2660 state
->dev
= next_net_device_rcu(state
->dev
);
2662 state
->in_dev
= NULL
;
2665 state
->in_dev
= __in_dev_get_rcu(state
->dev
);
2668 im
= rcu_dereference(state
->in_dev
->mc_list
);
2673 static struct ip_mc_list
*igmp_mc_get_idx(struct seq_file
*seq
, loff_t pos
)
2675 struct ip_mc_list
*im
= igmp_mc_get_first(seq
);
2677 while (pos
&& (im
= igmp_mc_get_next(seq
, im
)) != NULL
)
2679 return pos
? NULL
: im
;
2682 static void *igmp_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2686 return *pos
? igmp_mc_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2689 static void *igmp_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2691 struct ip_mc_list
*im
;
2692 if (v
== SEQ_START_TOKEN
)
2693 im
= igmp_mc_get_first(seq
);
2695 im
= igmp_mc_get_next(seq
, v
);
2700 static void igmp_mc_seq_stop(struct seq_file
*seq
, void *v
)
2703 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2705 state
->in_dev
= NULL
;
2710 static int igmp_mc_seq_show(struct seq_file
*seq
, void *v
)
2712 if (v
== SEQ_START_TOKEN
)
2714 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2716 struct ip_mc_list
*im
= (struct ip_mc_list
*)v
;
2717 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2721 #ifdef CONFIG_IP_MULTICAST
2722 querier
= IGMP_V1_SEEN(state
->in_dev
) ? "V1" :
2723 IGMP_V2_SEEN(state
->in_dev
) ? "V2" :
2729 if (rcu_access_pointer(state
->in_dev
->mc_list
) == im
) {
2730 seq_printf(seq
, "%d\t%-10s: %5d %7s\n",
2731 state
->dev
->ifindex
, state
->dev
->name
, state
->in_dev
->mc_count
, querier
);
2734 delta
= im
->timer
.expires
- jiffies
;
2736 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2737 im
->multiaddr
, im
->users
,
2739 im
->tm_running
? jiffies_delta_to_clock_t(delta
) : 0,
2745 static const struct seq_operations igmp_mc_seq_ops
= {
2746 .start
= igmp_mc_seq_start
,
2747 .next
= igmp_mc_seq_next
,
2748 .stop
= igmp_mc_seq_stop
,
2749 .show
= igmp_mc_seq_show
,
2752 static int igmp_mc_seq_open(struct inode
*inode
, struct file
*file
)
2754 return seq_open_net(inode
, file
, &igmp_mc_seq_ops
,
2755 sizeof(struct igmp_mc_iter_state
));
2758 static const struct file_operations igmp_mc_seq_fops
= {
2759 .owner
= THIS_MODULE
,
2760 .open
= igmp_mc_seq_open
,
2762 .llseek
= seq_lseek
,
2763 .release
= seq_release_net
,
2766 struct igmp_mcf_iter_state
{
2767 struct seq_net_private p
;
2768 struct net_device
*dev
;
2769 struct in_device
*idev
;
2770 struct ip_mc_list
*im
;
2773 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2775 static inline struct ip_sf_list
*igmp_mcf_get_first(struct seq_file
*seq
)
2777 struct net
*net
= seq_file_net(seq
);
2778 struct ip_sf_list
*psf
= NULL
;
2779 struct ip_mc_list
*im
= NULL
;
2780 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2784 for_each_netdev_rcu(net
, state
->dev
) {
2785 struct in_device
*idev
;
2786 idev
= __in_dev_get_rcu(state
->dev
);
2787 if (unlikely(!idev
))
2789 im
= rcu_dereference(idev
->mc_list
);
2791 spin_lock_bh(&im
->lock
);
2798 spin_unlock_bh(&im
->lock
);
2804 static struct ip_sf_list
*igmp_mcf_get_next(struct seq_file
*seq
, struct ip_sf_list
*psf
)
2806 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2810 spin_unlock_bh(&state
->im
->lock
);
2811 state
->im
= state
->im
->next
;
2812 while (!state
->im
) {
2813 state
->dev
= next_net_device_rcu(state
->dev
);
2818 state
->idev
= __in_dev_get_rcu(state
->dev
);
2821 state
->im
= rcu_dereference(state
->idev
->mc_list
);
2825 spin_lock_bh(&state
->im
->lock
);
2826 psf
= state
->im
->sources
;
2832 static struct ip_sf_list
*igmp_mcf_get_idx(struct seq_file
*seq
, loff_t pos
)
2834 struct ip_sf_list
*psf
= igmp_mcf_get_first(seq
);
2836 while (pos
&& (psf
= igmp_mcf_get_next(seq
, psf
)) != NULL
)
2838 return pos
? NULL
: psf
;
2841 static void *igmp_mcf_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2845 return *pos
? igmp_mcf_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2848 static void *igmp_mcf_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2850 struct ip_sf_list
*psf
;
2851 if (v
== SEQ_START_TOKEN
)
2852 psf
= igmp_mcf_get_first(seq
);
2854 psf
= igmp_mcf_get_next(seq
, v
);
2859 static void igmp_mcf_seq_stop(struct seq_file
*seq
, void *v
)
2862 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2863 if (likely(state
->im
)) {
2864 spin_unlock_bh(&state
->im
->lock
);
2872 static int igmp_mcf_seq_show(struct seq_file
*seq
, void *v
)
2874 struct ip_sf_list
*psf
= (struct ip_sf_list
*)v
;
2875 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2877 if (v
== SEQ_START_TOKEN
) {
2878 seq_puts(seq
, "Idx Device MCA SRC INC EXC\n");
2882 "0x%08x %6lu %6lu\n",
2883 state
->dev
->ifindex
, state
->dev
->name
,
2884 ntohl(state
->im
->multiaddr
),
2885 ntohl(psf
->sf_inaddr
),
2886 psf
->sf_count
[MCAST_INCLUDE
],
2887 psf
->sf_count
[MCAST_EXCLUDE
]);
2892 static const struct seq_operations igmp_mcf_seq_ops
= {
2893 .start
= igmp_mcf_seq_start
,
2894 .next
= igmp_mcf_seq_next
,
2895 .stop
= igmp_mcf_seq_stop
,
2896 .show
= igmp_mcf_seq_show
,
2899 static int igmp_mcf_seq_open(struct inode
*inode
, struct file
*file
)
2901 return seq_open_net(inode
, file
, &igmp_mcf_seq_ops
,
2902 sizeof(struct igmp_mcf_iter_state
));
2905 static const struct file_operations igmp_mcf_seq_fops
= {
2906 .owner
= THIS_MODULE
,
2907 .open
= igmp_mcf_seq_open
,
2909 .llseek
= seq_lseek
,
2910 .release
= seq_release_net
,
2913 static int __net_init
igmp_net_init(struct net
*net
)
2915 struct proc_dir_entry
*pde
;
2918 pde
= proc_create("igmp", S_IRUGO
, net
->proc_net
, &igmp_mc_seq_fops
);
2921 pde
= proc_create("mcfilter", S_IRUGO
, net
->proc_net
,
2922 &igmp_mcf_seq_fops
);
2925 err
= inet_ctl_sock_create(&net
->ipv4
.mc_autojoin_sk
, AF_INET
,
2926 SOCK_DGRAM
, 0, net
);
2928 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
2933 /* Sysctl initialization */
2934 net
->ipv4
.sysctl_igmp_max_memberships
= 20;
2935 net
->ipv4
.sysctl_igmp_max_msf
= 10;
2936 /* IGMP reports for link-local multicast groups are enabled by default */
2937 net
->ipv4
.sysctl_igmp_llm_reports
= 1;
2938 net
->ipv4
.sysctl_igmp_qrv
= 2;
2942 remove_proc_entry("mcfilter", net
->proc_net
);
2944 remove_proc_entry("igmp", net
->proc_net
);
2949 static void __net_exit
igmp_net_exit(struct net
*net
)
2951 remove_proc_entry("mcfilter", net
->proc_net
);
2952 remove_proc_entry("igmp", net
->proc_net
);
2953 inet_ctl_sock_destroy(net
->ipv4
.mc_autojoin_sk
);
2956 static struct pernet_operations igmp_net_ops
= {
2957 .init
= igmp_net_init
,
2958 .exit
= igmp_net_exit
,
2962 static int igmp_netdev_event(struct notifier_block
*this,
2963 unsigned long event
, void *ptr
)
2965 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
2966 struct in_device
*in_dev
;
2969 case NETDEV_RESEND_IGMP
:
2970 in_dev
= __in_dev_get_rtnl(dev
);
2972 ip_mc_rejoin_groups(in_dev
);
2980 static struct notifier_block igmp_notifier
= {
2981 .notifier_call
= igmp_netdev_event
,
2984 int __init
igmp_mc_init(void)
2986 #if defined(CONFIG_PROC_FS)
2989 err
= register_pernet_subsys(&igmp_net_ops
);
2992 err
= register_netdevice_notifier(&igmp_notifier
);
2994 goto reg_notif_fail
;
2998 unregister_pernet_subsys(&igmp_net_ops
);
3001 return register_netdevice_notifier(&igmp_notifier
);