igmp.c revision f97c1e0c6ebdb606c97b6cb5e837c6110ac5a961
1/*
2 *	Linux NET3:	Internet Group Management Protocol  [IGMP]
3 *
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.
6 *
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.
10 *
11 *	Version: $Id: igmp.c,v 1.47 2002/02/01 22:01:03 davem Exp $
12 *
13 *	Authors:
14 *		Alan Cox <Alan.Cox@linux.org>
15 *
16 *	This program is free software; you can redistribute it and/or
17 *	modify it under the terms of the GNU General Public License
18 *	as published by the Free Software Foundation; either version
19 *	2 of the License, or (at your option) any later version.
20 *
21 *	Fixes:
22 *
23 *		Alan Cox	:	Added lots of __inline__ to optimise
24 *					the memory usage of all the tiny little
25 *					functions.
26 *		Alan Cox	:	Dumped the header building experiment.
27 *		Alan Cox	:	Minor tweaks ready for multicast routing
28 *					and extended IGMP protocol.
29 *		Alan Cox	:	Removed a load of inline directives. Gcc 2.5.8
30 *					writes utterly bogus code otherwise (sigh)
31 *					fixed IGMP loopback to behave in the manner
32 *					desired by mrouted, fixed the fact it has been
33 *					broken since 1.3.6 and cleaned up a few minor
34 *					points.
35 *
36 *		Chih-Jen Chang	:	Tried to revise IGMP to Version 2
37 *		Tsu-Sheng Tsao		E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
38 *					The enhancements are mainly based on Steve Deering's
39 * 					ipmulti-3.5 source code.
40 *		Chih-Jen Chang	:	Added the igmp_get_mrouter_info and
41 *		Tsu-Sheng Tsao		igmp_set_mrouter_info to keep track of
42 *					the mrouted version on that device.
43 *		Chih-Jen Chang	:	Added the max_resp_time parameter to
44 *		Tsu-Sheng Tsao		igmp_heard_query(). Using this parameter
45 *					to identify the multicast router version
46 *					and do what the IGMP version 2 specified.
47 *		Chih-Jen Chang	:	Added a timer to revert to IGMP V2 router
48 *		Tsu-Sheng Tsao		if the specified time expired.
49 *		Alan Cox	:	Stop IGMP from 0.0.0.0 being accepted.
50 *		Alan Cox	:	Use GFP_ATOMIC in the right places.
51 *		Christian Daudt :	igmp timer wasn't set for local group
52 *					memberships but was being deleted,
53 *					which caused a "del_timer() called
54 *					from %p with timer not initialized\n"
55 *					message (960131).
56 *		Christian Daudt :	removed del_timer from
57 *					igmp_timer_expire function (960205).
58 *             Christian Daudt :       igmp_heard_report now only calls
59 *                                     igmp_timer_expire if tm->running is
60 *                                     true (960216).
61 *		Malcolm Beattie :	ttl comparison wrong in igmp_rcv made
62 *					igmp_heard_query never trigger. Expiry
63 *					miscalculation fixed in igmp_heard_query
64 *					and random() made to return unsigned to
65 *					prevent negative expiry times.
66 *		Alexey Kuznetsov:	Wrong group leaving behaviour, backport
67 *					fix from pending 2.1.x patches.
68 *		Alan Cox:		Forget to enable FDDI support earlier.
69 *		Alexey Kuznetsov:	Fixed leaving groups on device down.
70 *		Alexey Kuznetsov:	Accordance to igmp-v2-06 draft.
71 *		David L Stevens:	IGMPv3 support, with help from
72 *					Vinay Kulkarni
73 */
74
75#include <linux/module.h>
76#include <asm/uaccess.h>
77#include <asm/system.h>
78#include <linux/types.h>
79#include <linux/kernel.h>
80#include <linux/jiffies.h>
81#include <linux/string.h>
82#include <linux/socket.h>
83#include <linux/sockios.h>
84#include <linux/in.h>
85#include <linux/inet.h>
86#include <linux/netdevice.h>
87#include <linux/skbuff.h>
88#include <linux/inetdevice.h>
89#include <linux/igmp.h>
90#include <linux/if_arp.h>
91#include <linux/rtnetlink.h>
92#include <linux/times.h>
93
94#include <net/net_namespace.h>
95#include <net/arp.h>
96#include <net/ip.h>
97#include <net/protocol.h>
98#include <net/route.h>
99#include <net/sock.h>
100#include <net/checksum.h>
101#include <linux/netfilter_ipv4.h>
102#ifdef CONFIG_IP_MROUTE
103#include <linux/mroute.h>
104#endif
105#ifdef CONFIG_PROC_FS
106#include <linux/proc_fs.h>
107#include <linux/seq_file.h>
108#endif
109
110#define IP_MAX_MEMBERSHIPS	20
111#define IP_MAX_MSF		10
112
113#ifdef CONFIG_IP_MULTICAST
114/* Parameter names and values are taken from igmp-v2-06 draft */
115
116#define IGMP_V1_Router_Present_Timeout		(400*HZ)
117#define IGMP_V2_Router_Present_Timeout		(400*HZ)
118#define IGMP_Unsolicited_Report_Interval	(10*HZ)
119#define IGMP_Query_Response_Interval		(10*HZ)
120#define IGMP_Unsolicited_Report_Count		2
121
122
123#define IGMP_Initial_Report_Delay		(1)
124
125/* IGMP_Initial_Report_Delay is not from IGMP specs!
126 * IGMP specs require to report membership immediately after
127 * joining a group, but we delay the first report by a
128 * small interval. It seems more natural and still does not
129 * contradict to specs provided this delay is small enough.
130 */
131
132#define IGMP_V1_SEEN(in_dev) \
133	(IPV4_DEVCONF_ALL(in_dev->dev->nd_net, FORCE_IGMP_VERSION) == 1 || \
134	 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
135	 ((in_dev)->mr_v1_seen && \
136	  time_before(jiffies, (in_dev)->mr_v1_seen)))
137#define IGMP_V2_SEEN(in_dev) \
138	(IPV4_DEVCONF_ALL(in_dev->dev->nd_net, FORCE_IGMP_VERSION) == 2 || \
139	 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
140	 ((in_dev)->mr_v2_seen && \
141	  time_before(jiffies, (in_dev)->mr_v2_seen)))
142
143static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
144static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
145static void igmpv3_clear_delrec(struct in_device *in_dev);
146static int sf_setstate(struct ip_mc_list *pmc);
147static void sf_markstate(struct ip_mc_list *pmc);
148#endif
149static void ip_mc_clear_src(struct ip_mc_list *pmc);
150static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
151			 int sfcount, __be32 *psfsrc, int delta);
152
153static void ip_ma_put(struct ip_mc_list *im)
154{
155	if (atomic_dec_and_test(&im->refcnt)) {
156		in_dev_put(im->interface);
157		kfree(im);
158	}
159}
160
161#ifdef CONFIG_IP_MULTICAST
162
163/*
164 *	Timer management
165 */
166
167static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
168{
169	spin_lock_bh(&im->lock);
170	if (del_timer(&im->timer))
171		atomic_dec(&im->refcnt);
172	im->tm_running=0;
173	im->reporter = 0;
174	im->unsolicit_count = 0;
175	spin_unlock_bh(&im->lock);
176}
177
178/* It must be called with locked im->lock */
179static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
180{
181	int tv=net_random() % max_delay;
182
183	im->tm_running=1;
184	if (!mod_timer(&im->timer, jiffies+tv+2))
185		atomic_inc(&im->refcnt);
186}
187
188static void igmp_gq_start_timer(struct in_device *in_dev)
189{
190	int tv = net_random() % in_dev->mr_maxdelay;
191
192	in_dev->mr_gq_running = 1;
193	if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
194		in_dev_hold(in_dev);
195}
196
197static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
198{
199	int tv = net_random() % delay;
200
201	if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
202		in_dev_hold(in_dev);
203}
204
205static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
206{
207	spin_lock_bh(&im->lock);
208	im->unsolicit_count = 0;
209	if (del_timer(&im->timer)) {
210		if ((long)(im->timer.expires-jiffies) < max_delay) {
211			add_timer(&im->timer);
212			im->tm_running=1;
213			spin_unlock_bh(&im->lock);
214			return;
215		}
216		atomic_dec(&im->refcnt);
217	}
218	igmp_start_timer(im, max_delay);
219	spin_unlock_bh(&im->lock);
220}
221
222
223/*
224 *	Send an IGMP report.
225 */
226
227#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
228
229
230static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
231	int gdeleted, int sdeleted)
232{
233	switch (type) {
234	case IGMPV3_MODE_IS_INCLUDE:
235	case IGMPV3_MODE_IS_EXCLUDE:
236		if (gdeleted || sdeleted)
237			return 0;
238		if (!(pmc->gsquery && !psf->sf_gsresp)) {
239			if (pmc->sfmode == MCAST_INCLUDE)
240				return 1;
241			/* don't include if this source is excluded
242			 * in all filters
243			 */
244			if (psf->sf_count[MCAST_INCLUDE])
245				return type == IGMPV3_MODE_IS_INCLUDE;
246			return pmc->sfcount[MCAST_EXCLUDE] ==
247				psf->sf_count[MCAST_EXCLUDE];
248		}
249		return 0;
250	case IGMPV3_CHANGE_TO_INCLUDE:
251		if (gdeleted || sdeleted)
252			return 0;
253		return psf->sf_count[MCAST_INCLUDE] != 0;
254	case IGMPV3_CHANGE_TO_EXCLUDE:
255		if (gdeleted || sdeleted)
256			return 0;
257		if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
258		    psf->sf_count[MCAST_INCLUDE])
259			return 0;
260		return pmc->sfcount[MCAST_EXCLUDE] ==
261			psf->sf_count[MCAST_EXCLUDE];
262	case IGMPV3_ALLOW_NEW_SOURCES:
263		if (gdeleted || !psf->sf_crcount)
264			return 0;
265		return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
266	case IGMPV3_BLOCK_OLD_SOURCES:
267		if (pmc->sfmode == MCAST_INCLUDE)
268			return gdeleted || (psf->sf_crcount && sdeleted);
269		return psf->sf_crcount && !gdeleted && !sdeleted;
270	}
271	return 0;
272}
273
274static int
275igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
276{
277	struct ip_sf_list *psf;
278	int scount = 0;
279
280	for (psf=pmc->sources; psf; psf=psf->sf_next) {
281		if (!is_in(pmc, psf, type, gdeleted, sdeleted))
282			continue;
283		scount++;
284	}
285	return scount;
286}
287
288static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
289{
290	struct sk_buff *skb;
291	struct rtable *rt;
292	struct iphdr *pip;
293	struct igmpv3_report *pig;
294
295	skb = alloc_skb(size + LL_RESERVED_SPACE(dev), GFP_ATOMIC);
296	if (skb == NULL)
297		return NULL;
298
299	{
300		struct flowi fl = { .oif = dev->ifindex,
301				    .nl_u = { .ip4_u = {
302				    .daddr = IGMPV3_ALL_MCR } },
303				    .proto = IPPROTO_IGMP };
304		if (ip_route_output_key(&rt, &fl)) {
305			kfree_skb(skb);
306			return NULL;
307		}
308	}
309	if (rt->rt_src == 0) {
310		kfree_skb(skb);
311		ip_rt_put(rt);
312		return NULL;
313	}
314
315	skb->dst = &rt->u.dst;
316	skb->dev = dev;
317
318	skb_reserve(skb, LL_RESERVED_SPACE(dev));
319
320	skb_reset_network_header(skb);
321	pip = ip_hdr(skb);
322	skb_put(skb, sizeof(struct iphdr) + 4);
323
324	pip->version  = 4;
325	pip->ihl      = (sizeof(struct iphdr)+4)>>2;
326	pip->tos      = 0xc0;
327	pip->frag_off = htons(IP_DF);
328	pip->ttl      = 1;
329	pip->daddr    = rt->rt_dst;
330	pip->saddr    = rt->rt_src;
331	pip->protocol = IPPROTO_IGMP;
332	pip->tot_len  = 0;	/* filled in later */
333	ip_select_ident(pip, &rt->u.dst, NULL);
334	((u8*)&pip[1])[0] = IPOPT_RA;
335	((u8*)&pip[1])[1] = 4;
336	((u8*)&pip[1])[2] = 0;
337	((u8*)&pip[1])[3] = 0;
338
339	skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
340	skb_put(skb, sizeof(*pig));
341	pig = igmpv3_report_hdr(skb);
342	pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
343	pig->resv1 = 0;
344	pig->csum = 0;
345	pig->resv2 = 0;
346	pig->ngrec = 0;
347	return skb;
348}
349
350static int igmpv3_sendpack(struct sk_buff *skb)
351{
352	struct igmphdr *pig = igmp_hdr(skb);
353	const int igmplen = skb->tail - skb->transport_header;
354
355	pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
356
357	return ip_local_out(skb);
358}
359
360static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
361{
362	return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc,type,gdel,sdel);
363}
364
365static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
366	int type, struct igmpv3_grec **ppgr)
367{
368	struct net_device *dev = pmc->interface->dev;
369	struct igmpv3_report *pih;
370	struct igmpv3_grec *pgr;
371
372	if (!skb)
373		skb = igmpv3_newpack(dev, dev->mtu);
374	if (!skb)
375		return NULL;
376	pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
377	pgr->grec_type = type;
378	pgr->grec_auxwords = 0;
379	pgr->grec_nsrcs = 0;
380	pgr->grec_mca = pmc->multiaddr;
381	pih = igmpv3_report_hdr(skb);
382	pih->ngrec = htons(ntohs(pih->ngrec)+1);
383	*ppgr = pgr;
384	return skb;
385}
386
387#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
388	skb_tailroom(skb)) : 0)
389
390static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
391	int type, int gdeleted, int sdeleted)
392{
393	struct net_device *dev = pmc->interface->dev;
394	struct igmpv3_report *pih;
395	struct igmpv3_grec *pgr = NULL;
396	struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
397	int scount, stotal, first, isquery, truncate;
398
399	if (pmc->multiaddr == IGMP_ALL_HOSTS)
400		return skb;
401
402	isquery = type == IGMPV3_MODE_IS_INCLUDE ||
403		  type == IGMPV3_MODE_IS_EXCLUDE;
404	truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
405		    type == IGMPV3_CHANGE_TO_EXCLUDE;
406
407	stotal = scount = 0;
408
409	psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
410
411	if (!*psf_list)
412		goto empty_source;
413
414	pih = skb ? igmpv3_report_hdr(skb) : NULL;
415
416	/* EX and TO_EX get a fresh packet, if needed */
417	if (truncate) {
418		if (pih && pih->ngrec &&
419		    AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
420			if (skb)
421				igmpv3_sendpack(skb);
422			skb = igmpv3_newpack(dev, dev->mtu);
423		}
424	}
425	first = 1;
426	psf_prev = NULL;
427	for (psf=*psf_list; psf; psf=psf_next) {
428		__be32 *psrc;
429
430		psf_next = psf->sf_next;
431
432		if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
433			psf_prev = psf;
434			continue;
435		}
436
437		/* clear marks on query responses */
438		if (isquery)
439			psf->sf_gsresp = 0;
440
441		if (AVAILABLE(skb) < sizeof(__be32) +
442		    first*sizeof(struct igmpv3_grec)) {
443			if (truncate && !first)
444				break;	 /* truncate these */
445			if (pgr)
446				pgr->grec_nsrcs = htons(scount);
447			if (skb)
448				igmpv3_sendpack(skb);
449			skb = igmpv3_newpack(dev, dev->mtu);
450			first = 1;
451			scount = 0;
452		}
453		if (first) {
454			skb = add_grhead(skb, pmc, type, &pgr);
455			first = 0;
456		}
457		if (!skb)
458			return NULL;
459		psrc = (__be32 *)skb_put(skb, sizeof(__be32));
460		*psrc = psf->sf_inaddr;
461		scount++; stotal++;
462		if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
463		     type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
464			psf->sf_crcount--;
465			if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
466				if (psf_prev)
467					psf_prev->sf_next = psf->sf_next;
468				else
469					*psf_list = psf->sf_next;
470				kfree(psf);
471				continue;
472			}
473		}
474		psf_prev = psf;
475	}
476
477empty_source:
478	if (!stotal) {
479		if (type == IGMPV3_ALLOW_NEW_SOURCES ||
480		    type == IGMPV3_BLOCK_OLD_SOURCES)
481			return skb;
482		if (pmc->crcount || isquery) {
483			/* make sure we have room for group header */
484			if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
485				igmpv3_sendpack(skb);
486				skb = NULL; /* add_grhead will get a new one */
487			}
488			skb = add_grhead(skb, pmc, type, &pgr);
489		}
490	}
491	if (pgr)
492		pgr->grec_nsrcs = htons(scount);
493
494	if (isquery)
495		pmc->gsquery = 0;	/* clear query state on report */
496	return skb;
497}
498
499static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
500{
501	struct sk_buff *skb = NULL;
502	int type;
503
504	if (!pmc) {
505		read_lock(&in_dev->mc_list_lock);
506		for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
507			if (pmc->multiaddr == IGMP_ALL_HOSTS)
508				continue;
509			spin_lock_bh(&pmc->lock);
510			if (pmc->sfcount[MCAST_EXCLUDE])
511				type = IGMPV3_MODE_IS_EXCLUDE;
512			else
513				type = IGMPV3_MODE_IS_INCLUDE;
514			skb = add_grec(skb, pmc, type, 0, 0);
515			spin_unlock_bh(&pmc->lock);
516		}
517		read_unlock(&in_dev->mc_list_lock);
518	} else {
519		spin_lock_bh(&pmc->lock);
520		if (pmc->sfcount[MCAST_EXCLUDE])
521			type = IGMPV3_MODE_IS_EXCLUDE;
522		else
523			type = IGMPV3_MODE_IS_INCLUDE;
524		skb = add_grec(skb, pmc, type, 0, 0);
525		spin_unlock_bh(&pmc->lock);
526	}
527	if (!skb)
528		return 0;
529	return igmpv3_sendpack(skb);
530}
531
532/*
533 * remove zero-count source records from a source filter list
534 */
535static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
536{
537	struct ip_sf_list *psf_prev, *psf_next, *psf;
538
539	psf_prev = NULL;
540	for (psf=*ppsf; psf; psf = psf_next) {
541		psf_next = psf->sf_next;
542		if (psf->sf_crcount == 0) {
543			if (psf_prev)
544				psf_prev->sf_next = psf->sf_next;
545			else
546				*ppsf = psf->sf_next;
547			kfree(psf);
548		} else
549			psf_prev = psf;
550	}
551}
552
553static void igmpv3_send_cr(struct in_device *in_dev)
554{
555	struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
556	struct sk_buff *skb = NULL;
557	int type, dtype;
558
559	read_lock(&in_dev->mc_list_lock);
560	spin_lock_bh(&in_dev->mc_tomb_lock);
561
562	/* deleted MCA's */
563	pmc_prev = NULL;
564	for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
565		pmc_next = pmc->next;
566		if (pmc->sfmode == MCAST_INCLUDE) {
567			type = IGMPV3_BLOCK_OLD_SOURCES;
568			dtype = IGMPV3_BLOCK_OLD_SOURCES;
569			skb = add_grec(skb, pmc, type, 1, 0);
570			skb = add_grec(skb, pmc, dtype, 1, 1);
571		}
572		if (pmc->crcount) {
573			if (pmc->sfmode == MCAST_EXCLUDE) {
574				type = IGMPV3_CHANGE_TO_INCLUDE;
575				skb = add_grec(skb, pmc, type, 1, 0);
576			}
577			pmc->crcount--;
578			if (pmc->crcount == 0) {
579				igmpv3_clear_zeros(&pmc->tomb);
580				igmpv3_clear_zeros(&pmc->sources);
581			}
582		}
583		if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
584			if (pmc_prev)
585				pmc_prev->next = pmc_next;
586			else
587				in_dev->mc_tomb = pmc_next;
588			in_dev_put(pmc->interface);
589			kfree(pmc);
590		} else
591			pmc_prev = pmc;
592	}
593	spin_unlock_bh(&in_dev->mc_tomb_lock);
594
595	/* change recs */
596	for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
597		spin_lock_bh(&pmc->lock);
598		if (pmc->sfcount[MCAST_EXCLUDE]) {
599			type = IGMPV3_BLOCK_OLD_SOURCES;
600			dtype = IGMPV3_ALLOW_NEW_SOURCES;
601		} else {
602			type = IGMPV3_ALLOW_NEW_SOURCES;
603			dtype = IGMPV3_BLOCK_OLD_SOURCES;
604		}
605		skb = add_grec(skb, pmc, type, 0, 0);
606		skb = add_grec(skb, pmc, dtype, 0, 1);	/* deleted sources */
607
608		/* filter mode changes */
609		if (pmc->crcount) {
610			if (pmc->sfmode == MCAST_EXCLUDE)
611				type = IGMPV3_CHANGE_TO_EXCLUDE;
612			else
613				type = IGMPV3_CHANGE_TO_INCLUDE;
614			skb = add_grec(skb, pmc, type, 0, 0);
615			pmc->crcount--;
616		}
617		spin_unlock_bh(&pmc->lock);
618	}
619	read_unlock(&in_dev->mc_list_lock);
620
621	if (!skb)
622		return;
623	(void) igmpv3_sendpack(skb);
624}
625
626static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
627	int type)
628{
629	struct sk_buff *skb;
630	struct iphdr *iph;
631	struct igmphdr *ih;
632	struct rtable *rt;
633	struct net_device *dev = in_dev->dev;
634	__be32	group = pmc ? pmc->multiaddr : 0;
635	__be32	dst;
636
637	if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
638		return igmpv3_send_report(in_dev, pmc);
639	else if (type == IGMP_HOST_LEAVE_MESSAGE)
640		dst = IGMP_ALL_ROUTER;
641	else
642		dst = group;
643
644	{
645		struct flowi fl = { .oif = dev->ifindex,
646				    .nl_u = { .ip4_u = { .daddr = dst } },
647				    .proto = IPPROTO_IGMP };
648		if (ip_route_output_key(&rt, &fl))
649			return -1;
650	}
651	if (rt->rt_src == 0) {
652		ip_rt_put(rt);
653		return -1;
654	}
655
656	skb=alloc_skb(IGMP_SIZE+LL_RESERVED_SPACE(dev), GFP_ATOMIC);
657	if (skb == NULL) {
658		ip_rt_put(rt);
659		return -1;
660	}
661
662	skb->dst = &rt->u.dst;
663
664	skb_reserve(skb, LL_RESERVED_SPACE(dev));
665
666	skb_reset_network_header(skb);
667	iph = ip_hdr(skb);
668	skb_put(skb, sizeof(struct iphdr) + 4);
669
670	iph->version  = 4;
671	iph->ihl      = (sizeof(struct iphdr)+4)>>2;
672	iph->tos      = 0xc0;
673	iph->frag_off = htons(IP_DF);
674	iph->ttl      = 1;
675	iph->daddr    = dst;
676	iph->saddr    = rt->rt_src;
677	iph->protocol = IPPROTO_IGMP;
678	ip_select_ident(iph, &rt->u.dst, NULL);
679	((u8*)&iph[1])[0] = IPOPT_RA;
680	((u8*)&iph[1])[1] = 4;
681	((u8*)&iph[1])[2] = 0;
682	((u8*)&iph[1])[3] = 0;
683
684	ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
685	ih->type=type;
686	ih->code=0;
687	ih->csum=0;
688	ih->group=group;
689	ih->csum=ip_compute_csum((void *)ih, sizeof(struct igmphdr));
690
691	return ip_local_out(skb);
692}
693
694static void igmp_gq_timer_expire(unsigned long data)
695{
696	struct in_device *in_dev = (struct in_device *)data;
697
698	in_dev->mr_gq_running = 0;
699	igmpv3_send_report(in_dev, NULL);
700	__in_dev_put(in_dev);
701}
702
703static void igmp_ifc_timer_expire(unsigned long data)
704{
705	struct in_device *in_dev = (struct in_device *)data;
706
707	igmpv3_send_cr(in_dev);
708	if (in_dev->mr_ifc_count) {
709		in_dev->mr_ifc_count--;
710		igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
711	}
712	__in_dev_put(in_dev);
713}
714
715static void igmp_ifc_event(struct in_device *in_dev)
716{
717	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
718		return;
719	in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
720		IGMP_Unsolicited_Report_Count;
721	igmp_ifc_start_timer(in_dev, 1);
722}
723
724
725static void igmp_timer_expire(unsigned long data)
726{
727	struct ip_mc_list *im=(struct ip_mc_list *)data;
728	struct in_device *in_dev = im->interface;
729
730	spin_lock(&im->lock);
731	im->tm_running=0;
732
733	if (im->unsolicit_count) {
734		im->unsolicit_count--;
735		igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
736	}
737	im->reporter = 1;
738	spin_unlock(&im->lock);
739
740	if (IGMP_V1_SEEN(in_dev))
741		igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
742	else if (IGMP_V2_SEEN(in_dev))
743		igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
744	else
745		igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
746
747	ip_ma_put(im);
748}
749
750/* mark EXCLUDE-mode sources */
751static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
752{
753	struct ip_sf_list *psf;
754	int i, scount;
755
756	scount = 0;
757	for (psf=pmc->sources; psf; psf=psf->sf_next) {
758		if (scount == nsrcs)
759			break;
760		for (i=0; i<nsrcs; i++) {
761			/* skip inactive filters */
762			if (pmc->sfcount[MCAST_INCLUDE] ||
763			    pmc->sfcount[MCAST_EXCLUDE] !=
764			    psf->sf_count[MCAST_EXCLUDE])
765				continue;
766			if (srcs[i] == psf->sf_inaddr) {
767				scount++;
768				break;
769			}
770		}
771	}
772	pmc->gsquery = 0;
773	if (scount == nsrcs)	/* all sources excluded */
774		return 0;
775	return 1;
776}
777
778static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
779{
780	struct ip_sf_list *psf;
781	int i, scount;
782
783	if (pmc->sfmode == MCAST_EXCLUDE)
784		return igmp_xmarksources(pmc, nsrcs, srcs);
785
786	/* mark INCLUDE-mode sources */
787	scount = 0;
788	for (psf=pmc->sources; psf; psf=psf->sf_next) {
789		if (scount == nsrcs)
790			break;
791		for (i=0; i<nsrcs; i++)
792			if (srcs[i] == psf->sf_inaddr) {
793				psf->sf_gsresp = 1;
794				scount++;
795				break;
796			}
797	}
798	if (!scount) {
799		pmc->gsquery = 0;
800		return 0;
801	}
802	pmc->gsquery = 1;
803	return 1;
804}
805
806static void igmp_heard_report(struct in_device *in_dev, __be32 group)
807{
808	struct ip_mc_list *im;
809
810	/* Timers are only set for non-local groups */
811
812	if (group == IGMP_ALL_HOSTS)
813		return;
814
815	read_lock(&in_dev->mc_list_lock);
816	for (im=in_dev->mc_list; im!=NULL; im=im->next) {
817		if (im->multiaddr == group) {
818			igmp_stop_timer(im);
819			break;
820		}
821	}
822	read_unlock(&in_dev->mc_list_lock);
823}
824
825static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
826	int len)
827{
828	struct igmphdr 		*ih = igmp_hdr(skb);
829	struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
830	struct ip_mc_list	*im;
831	__be32			group = ih->group;
832	int			max_delay;
833	int			mark = 0;
834
835
836	if (len == 8) {
837		if (ih->code == 0) {
838			/* Alas, old v1 router presents here. */
839
840			max_delay = IGMP_Query_Response_Interval;
841			in_dev->mr_v1_seen = jiffies +
842				IGMP_V1_Router_Present_Timeout;
843			group = 0;
844		} else {
845			/* v2 router present */
846			max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
847			in_dev->mr_v2_seen = jiffies +
848				IGMP_V2_Router_Present_Timeout;
849		}
850		/* cancel the interface change timer */
851		in_dev->mr_ifc_count = 0;
852		if (del_timer(&in_dev->mr_ifc_timer))
853			__in_dev_put(in_dev);
854		/* clear deleted report items */
855		igmpv3_clear_delrec(in_dev);
856	} else if (len < 12) {
857		return;	/* ignore bogus packet; freed by caller */
858	} else { /* v3 */
859		if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
860			return;
861
862		ih3 = igmpv3_query_hdr(skb);
863		if (ih3->nsrcs) {
864			if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
865					   + ntohs(ih3->nsrcs)*sizeof(__be32)))
866				return;
867			ih3 = igmpv3_query_hdr(skb);
868		}
869
870		max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
871		if (!max_delay)
872			max_delay = 1;	/* can't mod w/ 0 */
873		in_dev->mr_maxdelay = max_delay;
874		if (ih3->qrv)
875			in_dev->mr_qrv = ih3->qrv;
876		if (!group) { /* general query */
877			if (ih3->nsrcs)
878				return;	/* no sources allowed */
879			igmp_gq_start_timer(in_dev);
880			return;
881		}
882		/* mark sources to include, if group & source-specific */
883		mark = ih3->nsrcs != 0;
884	}
885
886	/*
887	 * - Start the timers in all of our membership records
888	 *   that the query applies to for the interface on
889	 *   which the query arrived excl. those that belong
890	 *   to a "local" group (224.0.0.X)
891	 * - For timers already running check if they need to
892	 *   be reset.
893	 * - Use the igmp->igmp_code field as the maximum
894	 *   delay possible
895	 */
896	read_lock(&in_dev->mc_list_lock);
897	for (im=in_dev->mc_list; im!=NULL; im=im->next) {
898		int changed;
899
900		if (group && group != im->multiaddr)
901			continue;
902		if (im->multiaddr == IGMP_ALL_HOSTS)
903			continue;
904		spin_lock_bh(&im->lock);
905		if (im->tm_running)
906			im->gsquery = im->gsquery && mark;
907		else
908			im->gsquery = mark;
909		changed = !im->gsquery ||
910			igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
911		spin_unlock_bh(&im->lock);
912		if (changed)
913			igmp_mod_timer(im, max_delay);
914	}
915	read_unlock(&in_dev->mc_list_lock);
916}
917
918int igmp_rcv(struct sk_buff *skb)
919{
920	/* This basically follows the spec line by line -- see RFC1112 */
921	struct igmphdr *ih;
922	struct in_device *in_dev = in_dev_get(skb->dev);
923	int len = skb->len;
924
925	if (in_dev==NULL) {
926		kfree_skb(skb);
927		return 0;
928	}
929
930	if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
931		goto drop;
932
933	switch (skb->ip_summed) {
934	case CHECKSUM_COMPLETE:
935		if (!csum_fold(skb->csum))
936			break;
937		/* fall through */
938	case CHECKSUM_NONE:
939		skb->csum = 0;
940		if (__skb_checksum_complete(skb))
941			goto drop;
942	}
943
944	ih = igmp_hdr(skb);
945	switch (ih->type) {
946	case IGMP_HOST_MEMBERSHIP_QUERY:
947		igmp_heard_query(in_dev, skb, len);
948		break;
949	case IGMP_HOST_MEMBERSHIP_REPORT:
950	case IGMPV2_HOST_MEMBERSHIP_REPORT:
951	case IGMPV3_HOST_MEMBERSHIP_REPORT:
952		/* Is it our report looped back? */
953		if (((struct rtable*)skb->dst)->fl.iif == 0)
954			break;
955		/* don't rely on MC router hearing unicast reports */
956		if (skb->pkt_type == PACKET_MULTICAST ||
957		    skb->pkt_type == PACKET_BROADCAST)
958			igmp_heard_report(in_dev, ih->group);
959		break;
960	case IGMP_PIM:
961#ifdef CONFIG_IP_PIMSM_V1
962		in_dev_put(in_dev);
963		return pim_rcv_v1(skb);
964#endif
965	case IGMP_DVMRP:
966	case IGMP_TRACE:
967	case IGMP_HOST_LEAVE_MESSAGE:
968	case IGMP_MTRACE:
969	case IGMP_MTRACE_RESP:
970		break;
971	default:
972		break;
973	}
974
975drop:
976	in_dev_put(in_dev);
977	kfree_skb(skb);
978	return 0;
979}
980
981#endif
982
983
984/*
985 *	Add a filter to a device
986 */
987
988static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
989{
990	char buf[MAX_ADDR_LEN];
991	struct net_device *dev = in_dev->dev;
992
993	/* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
994	   We will get multicast token leakage, when IFF_MULTICAST
995	   is changed. This check should be done in dev->set_multicast_list
996	   routine. Something sort of:
997	   if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
998	   --ANK
999	   */
1000	if (arp_mc_map(addr, buf, dev, 0) == 0)
1001		dev_mc_add(dev,buf,dev->addr_len,0);
1002}
1003
1004/*
1005 *	Remove a filter from a device
1006 */
1007
1008static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1009{
1010	char buf[MAX_ADDR_LEN];
1011	struct net_device *dev = in_dev->dev;
1012
1013	if (arp_mc_map(addr, buf, dev, 0) == 0)
1014		dev_mc_delete(dev,buf,dev->addr_len,0);
1015}
1016
1017#ifdef CONFIG_IP_MULTICAST
1018/*
1019 * deleted ip_mc_list manipulation
1020 */
1021static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1022{
1023	struct ip_mc_list *pmc;
1024
1025	/* this is an "ip_mc_list" for convenience; only the fields below
1026	 * are actually used. In particular, the refcnt and users are not
1027	 * used for management of the delete list. Using the same structure
1028	 * for deleted items allows change reports to use common code with
1029	 * non-deleted or query-response MCA's.
1030	 */
1031	pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1032	if (!pmc)
1033		return;
1034	spin_lock_bh(&im->lock);
1035	pmc->interface = im->interface;
1036	in_dev_hold(in_dev);
1037	pmc->multiaddr = im->multiaddr;
1038	pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1039		IGMP_Unsolicited_Report_Count;
1040	pmc->sfmode = im->sfmode;
1041	if (pmc->sfmode == MCAST_INCLUDE) {
1042		struct ip_sf_list *psf;
1043
1044		pmc->tomb = im->tomb;
1045		pmc->sources = im->sources;
1046		im->tomb = im->sources = NULL;
1047		for (psf=pmc->sources; psf; psf=psf->sf_next)
1048			psf->sf_crcount = pmc->crcount;
1049	}
1050	spin_unlock_bh(&im->lock);
1051
1052	spin_lock_bh(&in_dev->mc_tomb_lock);
1053	pmc->next = in_dev->mc_tomb;
1054	in_dev->mc_tomb = pmc;
1055	spin_unlock_bh(&in_dev->mc_tomb_lock);
1056}
1057
1058static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1059{
1060	struct ip_mc_list *pmc, *pmc_prev;
1061	struct ip_sf_list *psf, *psf_next;
1062
1063	spin_lock_bh(&in_dev->mc_tomb_lock);
1064	pmc_prev = NULL;
1065	for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1066		if (pmc->multiaddr == multiaddr)
1067			break;
1068		pmc_prev = pmc;
1069	}
1070	if (pmc) {
1071		if (pmc_prev)
1072			pmc_prev->next = pmc->next;
1073		else
1074			in_dev->mc_tomb = pmc->next;
1075	}
1076	spin_unlock_bh(&in_dev->mc_tomb_lock);
1077	if (pmc) {
1078		for (psf=pmc->tomb; psf; psf=psf_next) {
1079			psf_next = psf->sf_next;
1080			kfree(psf);
1081		}
1082		in_dev_put(pmc->interface);
1083		kfree(pmc);
1084	}
1085}
1086
1087static void igmpv3_clear_delrec(struct in_device *in_dev)
1088{
1089	struct ip_mc_list *pmc, *nextpmc;
1090
1091	spin_lock_bh(&in_dev->mc_tomb_lock);
1092	pmc = in_dev->mc_tomb;
1093	in_dev->mc_tomb = NULL;
1094	spin_unlock_bh(&in_dev->mc_tomb_lock);
1095
1096	for (; pmc; pmc = nextpmc) {
1097		nextpmc = pmc->next;
1098		ip_mc_clear_src(pmc);
1099		in_dev_put(pmc->interface);
1100		kfree(pmc);
1101	}
1102	/* clear dead sources, too */
1103	read_lock(&in_dev->mc_list_lock);
1104	for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1105		struct ip_sf_list *psf, *psf_next;
1106
1107		spin_lock_bh(&pmc->lock);
1108		psf = pmc->tomb;
1109		pmc->tomb = NULL;
1110		spin_unlock_bh(&pmc->lock);
1111		for (; psf; psf=psf_next) {
1112			psf_next = psf->sf_next;
1113			kfree(psf);
1114		}
1115	}
1116	read_unlock(&in_dev->mc_list_lock);
1117}
1118#endif
1119
1120static void igmp_group_dropped(struct ip_mc_list *im)
1121{
1122	struct in_device *in_dev = im->interface;
1123#ifdef CONFIG_IP_MULTICAST
1124	int reporter;
1125#endif
1126
1127	if (im->loaded) {
1128		im->loaded = 0;
1129		ip_mc_filter_del(in_dev, im->multiaddr);
1130	}
1131
1132#ifdef CONFIG_IP_MULTICAST
1133	if (im->multiaddr == IGMP_ALL_HOSTS)
1134		return;
1135
1136	reporter = im->reporter;
1137	igmp_stop_timer(im);
1138
1139	if (!in_dev->dead) {
1140		if (IGMP_V1_SEEN(in_dev))
1141			goto done;
1142		if (IGMP_V2_SEEN(in_dev)) {
1143			if (reporter)
1144				igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1145			goto done;
1146		}
1147		/* IGMPv3 */
1148		igmpv3_add_delrec(in_dev, im);
1149
1150		igmp_ifc_event(in_dev);
1151	}
1152done:
1153#endif
1154	ip_mc_clear_src(im);
1155}
1156
1157static void igmp_group_added(struct ip_mc_list *im)
1158{
1159	struct in_device *in_dev = im->interface;
1160
1161	if (im->loaded == 0) {
1162		im->loaded = 1;
1163		ip_mc_filter_add(in_dev, im->multiaddr);
1164	}
1165
1166#ifdef CONFIG_IP_MULTICAST
1167	if (im->multiaddr == IGMP_ALL_HOSTS)
1168		return;
1169
1170	if (in_dev->dead)
1171		return;
1172	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1173		spin_lock_bh(&im->lock);
1174		igmp_start_timer(im, IGMP_Initial_Report_Delay);
1175		spin_unlock_bh(&im->lock);
1176		return;
1177	}
1178	/* else, v3 */
1179
1180	im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1181		IGMP_Unsolicited_Report_Count;
1182	igmp_ifc_event(in_dev);
1183#endif
1184}
1185
1186
1187/*
1188 *	Multicast list managers
1189 */
1190
1191
1192/*
1193 *	A socket has joined a multicast group on device dev.
1194 */
1195
1196void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1197{
1198	struct ip_mc_list *im;
1199
1200	ASSERT_RTNL();
1201
1202	for (im=in_dev->mc_list; im; im=im->next) {
1203		if (im->multiaddr == addr) {
1204			im->users++;
1205			ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1206			goto out;
1207		}
1208	}
1209
1210	im = kmalloc(sizeof(*im), GFP_KERNEL);
1211	if (!im)
1212		goto out;
1213
1214	im->users=1;
1215	im->interface=in_dev;
1216	in_dev_hold(in_dev);
1217	im->multiaddr=addr;
1218	/* initial mode is (EX, empty) */
1219	im->sfmode = MCAST_EXCLUDE;
1220	im->sfcount[MCAST_INCLUDE] = 0;
1221	im->sfcount[MCAST_EXCLUDE] = 1;
1222	im->sources = NULL;
1223	im->tomb = NULL;
1224	im->crcount = 0;
1225	atomic_set(&im->refcnt, 1);
1226	spin_lock_init(&im->lock);
1227#ifdef CONFIG_IP_MULTICAST
1228	im->tm_running=0;
1229	setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
1230	im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1231	im->reporter = 0;
1232	im->gsquery = 0;
1233#endif
1234	im->loaded = 0;
1235	write_lock_bh(&in_dev->mc_list_lock);
1236	im->next=in_dev->mc_list;
1237	in_dev->mc_list=im;
1238	write_unlock_bh(&in_dev->mc_list_lock);
1239#ifdef CONFIG_IP_MULTICAST
1240	igmpv3_del_delrec(in_dev, im->multiaddr);
1241#endif
1242	igmp_group_added(im);
1243	if (!in_dev->dead)
1244		ip_rt_multicast_event(in_dev);
1245out:
1246	return;
1247}
1248
1249/*
1250 *	Resend IGMP JOIN report; used for bonding.
1251 */
1252void ip_mc_rejoin_group(struct ip_mc_list *im)
1253{
1254#ifdef CONFIG_IP_MULTICAST
1255	struct in_device *in_dev = im->interface;
1256
1257	if (im->multiaddr == IGMP_ALL_HOSTS)
1258		return;
1259
1260	if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1261		igmp_mod_timer(im, IGMP_Initial_Report_Delay);
1262		return;
1263	}
1264	/* else, v3 */
1265	im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1266		IGMP_Unsolicited_Report_Count;
1267	igmp_ifc_event(in_dev);
1268#endif
1269}
1270
1271/*
1272 *	A socket has left a multicast group on device dev
1273 */
1274
1275void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1276{
1277	struct ip_mc_list *i, **ip;
1278
1279	ASSERT_RTNL();
1280
1281	for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1282		if (i->multiaddr==addr) {
1283			if (--i->users == 0) {
1284				write_lock_bh(&in_dev->mc_list_lock);
1285				*ip = i->next;
1286				write_unlock_bh(&in_dev->mc_list_lock);
1287				igmp_group_dropped(i);
1288
1289				if (!in_dev->dead)
1290					ip_rt_multicast_event(in_dev);
1291
1292				ip_ma_put(i);
1293				return;
1294			}
1295			break;
1296		}
1297	}
1298}
1299
1300/* Device going down */
1301
1302void ip_mc_down(struct in_device *in_dev)
1303{
1304	struct ip_mc_list *i;
1305
1306	ASSERT_RTNL();
1307
1308	for (i=in_dev->mc_list; i; i=i->next)
1309		igmp_group_dropped(i);
1310
1311#ifdef CONFIG_IP_MULTICAST
1312	in_dev->mr_ifc_count = 0;
1313	if (del_timer(&in_dev->mr_ifc_timer))
1314		__in_dev_put(in_dev);
1315	in_dev->mr_gq_running = 0;
1316	if (del_timer(&in_dev->mr_gq_timer))
1317		__in_dev_put(in_dev);
1318	igmpv3_clear_delrec(in_dev);
1319#endif
1320
1321	ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1322}
1323
1324void ip_mc_init_dev(struct in_device *in_dev)
1325{
1326	ASSERT_RTNL();
1327
1328	in_dev->mc_tomb = NULL;
1329#ifdef CONFIG_IP_MULTICAST
1330	in_dev->mr_gq_running = 0;
1331	setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1332			(unsigned long)in_dev);
1333	in_dev->mr_ifc_count = 0;
1334	setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1335			(unsigned long)in_dev);
1336	in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1337#endif
1338
1339	rwlock_init(&in_dev->mc_list_lock);
1340	spin_lock_init(&in_dev->mc_tomb_lock);
1341}
1342
1343/* Device going up */
1344
1345void ip_mc_up(struct in_device *in_dev)
1346{
1347	struct ip_mc_list *i;
1348
1349	ASSERT_RTNL();
1350
1351	ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1352
1353	for (i=in_dev->mc_list; i; i=i->next)
1354		igmp_group_added(i);
1355}
1356
1357/*
1358 *	Device is about to be destroyed: clean up.
1359 */
1360
1361void ip_mc_destroy_dev(struct in_device *in_dev)
1362{
1363	struct ip_mc_list *i;
1364
1365	ASSERT_RTNL();
1366
1367	/* Deactivate timers */
1368	ip_mc_down(in_dev);
1369
1370	write_lock_bh(&in_dev->mc_list_lock);
1371	while ((i = in_dev->mc_list) != NULL) {
1372		in_dev->mc_list = i->next;
1373		write_unlock_bh(&in_dev->mc_list_lock);
1374
1375		igmp_group_dropped(i);
1376		ip_ma_put(i);
1377
1378		write_lock_bh(&in_dev->mc_list_lock);
1379	}
1380	write_unlock_bh(&in_dev->mc_list_lock);
1381}
1382
1383static struct in_device * ip_mc_find_dev(struct ip_mreqn *imr)
1384{
1385	struct flowi fl = { .nl_u = { .ip4_u =
1386				      { .daddr = imr->imr_multiaddr.s_addr } } };
1387	struct rtable *rt;
1388	struct net_device *dev = NULL;
1389	struct in_device *idev = NULL;
1390
1391	if (imr->imr_ifindex) {
1392		idev = inetdev_by_index(imr->imr_ifindex);
1393		if (idev)
1394			__in_dev_put(idev);
1395		return idev;
1396	}
1397	if (imr->imr_address.s_addr) {
1398		dev = ip_dev_find(imr->imr_address.s_addr);
1399		if (!dev)
1400			return NULL;
1401		dev_put(dev);
1402	}
1403
1404	if (!dev && !ip_route_output_key(&rt, &fl)) {
1405		dev = rt->u.dst.dev;
1406		ip_rt_put(rt);
1407	}
1408	if (dev) {
1409		imr->imr_ifindex = dev->ifindex;
1410		idev = __in_dev_get_rtnl(dev);
1411	}
1412	return idev;
1413}
1414
1415/*
1416 *	Join a socket to a group
1417 */
1418int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1419int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1420
1421
1422static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1423	__be32 *psfsrc)
1424{
1425	struct ip_sf_list *psf, *psf_prev;
1426	int rv = 0;
1427
1428	psf_prev = NULL;
1429	for (psf=pmc->sources; psf; psf=psf->sf_next) {
1430		if (psf->sf_inaddr == *psfsrc)
1431			break;
1432		psf_prev = psf;
1433	}
1434	if (!psf || psf->sf_count[sfmode] == 0) {
1435		/* source filter not found, or count wrong =>  bug */
1436		return -ESRCH;
1437	}
1438	psf->sf_count[sfmode]--;
1439	if (psf->sf_count[sfmode] == 0) {
1440		ip_rt_multicast_event(pmc->interface);
1441	}
1442	if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1443#ifdef CONFIG_IP_MULTICAST
1444		struct in_device *in_dev = pmc->interface;
1445#endif
1446
1447		/* no more filters for this source */
1448		if (psf_prev)
1449			psf_prev->sf_next = psf->sf_next;
1450		else
1451			pmc->sources = psf->sf_next;
1452#ifdef CONFIG_IP_MULTICAST
1453		if (psf->sf_oldin &&
1454		    !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1455			psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1456				IGMP_Unsolicited_Report_Count;
1457			psf->sf_next = pmc->tomb;
1458			pmc->tomb = psf;
1459			rv = 1;
1460		} else
1461#endif
1462			kfree(psf);
1463	}
1464	return rv;
1465}
1466
1467#ifndef CONFIG_IP_MULTICAST
1468#define igmp_ifc_event(x)	do { } while (0)
1469#endif
1470
1471static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1472			 int sfcount, __be32 *psfsrc, int delta)
1473{
1474	struct ip_mc_list *pmc;
1475	int	changerec = 0;
1476	int	i, err;
1477
1478	if (!in_dev)
1479		return -ENODEV;
1480	read_lock(&in_dev->mc_list_lock);
1481	for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1482		if (*pmca == pmc->multiaddr)
1483			break;
1484	}
1485	if (!pmc) {
1486		/* MCA not found?? bug */
1487		read_unlock(&in_dev->mc_list_lock);
1488		return -ESRCH;
1489	}
1490	spin_lock_bh(&pmc->lock);
1491	read_unlock(&in_dev->mc_list_lock);
1492#ifdef CONFIG_IP_MULTICAST
1493	sf_markstate(pmc);
1494#endif
1495	if (!delta) {
1496		err = -EINVAL;
1497		if (!pmc->sfcount[sfmode])
1498			goto out_unlock;
1499		pmc->sfcount[sfmode]--;
1500	}
1501	err = 0;
1502	for (i=0; i<sfcount; i++) {
1503		int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1504
1505		changerec |= rv > 0;
1506		if (!err && rv < 0)
1507			err = rv;
1508	}
1509	if (pmc->sfmode == MCAST_EXCLUDE &&
1510	    pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1511	    pmc->sfcount[MCAST_INCLUDE]) {
1512#ifdef CONFIG_IP_MULTICAST
1513		struct ip_sf_list *psf;
1514#endif
1515
1516		/* filter mode change */
1517		pmc->sfmode = MCAST_INCLUDE;
1518#ifdef CONFIG_IP_MULTICAST
1519		pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1520			IGMP_Unsolicited_Report_Count;
1521		in_dev->mr_ifc_count = pmc->crcount;
1522		for (psf=pmc->sources; psf; psf = psf->sf_next)
1523			psf->sf_crcount = 0;
1524		igmp_ifc_event(pmc->interface);
1525	} else if (sf_setstate(pmc) || changerec) {
1526		igmp_ifc_event(pmc->interface);
1527#endif
1528	}
1529out_unlock:
1530	spin_unlock_bh(&pmc->lock);
1531	return err;
1532}
1533
1534/*
1535 * Add multicast single-source filter to the interface list
1536 */
1537static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1538	__be32 *psfsrc, int delta)
1539{
1540	struct ip_sf_list *psf, *psf_prev;
1541
1542	psf_prev = NULL;
1543	for (psf=pmc->sources; psf; psf=psf->sf_next) {
1544		if (psf->sf_inaddr == *psfsrc)
1545			break;
1546		psf_prev = psf;
1547	}
1548	if (!psf) {
1549		psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1550		if (!psf)
1551			return -ENOBUFS;
1552		psf->sf_inaddr = *psfsrc;
1553		if (psf_prev) {
1554			psf_prev->sf_next = psf;
1555		} else
1556			pmc->sources = psf;
1557	}
1558	psf->sf_count[sfmode]++;
1559	if (psf->sf_count[sfmode] == 1) {
1560		ip_rt_multicast_event(pmc->interface);
1561	}
1562	return 0;
1563}
1564
1565#ifdef CONFIG_IP_MULTICAST
1566static void sf_markstate(struct ip_mc_list *pmc)
1567{
1568	struct ip_sf_list *psf;
1569	int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1570
1571	for (psf=pmc->sources; psf; psf=psf->sf_next)
1572		if (pmc->sfcount[MCAST_EXCLUDE]) {
1573			psf->sf_oldin = mca_xcount ==
1574				psf->sf_count[MCAST_EXCLUDE] &&
1575				!psf->sf_count[MCAST_INCLUDE];
1576		} else
1577			psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1578}
1579
1580static int sf_setstate(struct ip_mc_list *pmc)
1581{
1582	struct ip_sf_list *psf, *dpsf;
1583	int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1584	int qrv = pmc->interface->mr_qrv;
1585	int new_in, rv;
1586
1587	rv = 0;
1588	for (psf=pmc->sources; psf; psf=psf->sf_next) {
1589		if (pmc->sfcount[MCAST_EXCLUDE]) {
1590			new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1591				!psf->sf_count[MCAST_INCLUDE];
1592		} else
1593			new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1594		if (new_in) {
1595			if (!psf->sf_oldin) {
1596				struct ip_sf_list *prev = NULL;
1597
1598				for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1599					if (dpsf->sf_inaddr == psf->sf_inaddr)
1600						break;
1601					prev = dpsf;
1602				}
1603				if (dpsf) {
1604					if (prev)
1605						prev->sf_next = dpsf->sf_next;
1606					else
1607						pmc->tomb = dpsf->sf_next;
1608					kfree(dpsf);
1609				}
1610				psf->sf_crcount = qrv;
1611				rv++;
1612			}
1613		} else if (psf->sf_oldin) {
1614
1615			psf->sf_crcount = 0;
1616			/*
1617			 * add or update "delete" records if an active filter
1618			 * is now inactive
1619			 */
1620			for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1621				if (dpsf->sf_inaddr == psf->sf_inaddr)
1622					break;
1623			if (!dpsf) {
1624				dpsf = (struct ip_sf_list *)
1625					kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1626				if (!dpsf)
1627					continue;
1628				*dpsf = *psf;
1629				/* pmc->lock held by callers */
1630				dpsf->sf_next = pmc->tomb;
1631				pmc->tomb = dpsf;
1632			}
1633			dpsf->sf_crcount = qrv;
1634			rv++;
1635		}
1636	}
1637	return rv;
1638}
1639#endif
1640
1641/*
1642 * Add multicast source filter list to the interface list
1643 */
1644static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1645			 int sfcount, __be32 *psfsrc, int delta)
1646{
1647	struct ip_mc_list *pmc;
1648	int	isexclude;
1649	int	i, err;
1650
1651	if (!in_dev)
1652		return -ENODEV;
1653	read_lock(&in_dev->mc_list_lock);
1654	for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1655		if (*pmca == pmc->multiaddr)
1656			break;
1657	}
1658	if (!pmc) {
1659		/* MCA not found?? bug */
1660		read_unlock(&in_dev->mc_list_lock);
1661		return -ESRCH;
1662	}
1663	spin_lock_bh(&pmc->lock);
1664	read_unlock(&in_dev->mc_list_lock);
1665
1666#ifdef CONFIG_IP_MULTICAST
1667	sf_markstate(pmc);
1668#endif
1669	isexclude = pmc->sfmode == MCAST_EXCLUDE;
1670	if (!delta)
1671		pmc->sfcount[sfmode]++;
1672	err = 0;
1673	for (i=0; i<sfcount; i++) {
1674		err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1675		if (err)
1676			break;
1677	}
1678	if (err) {
1679		int j;
1680
1681		pmc->sfcount[sfmode]--;
1682		for (j=0; j<i; j++)
1683			(void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1684	} else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1685#ifdef CONFIG_IP_MULTICAST
1686		struct ip_sf_list *psf;
1687		in_dev = pmc->interface;
1688#endif
1689
1690		/* filter mode change */
1691		if (pmc->sfcount[MCAST_EXCLUDE])
1692			pmc->sfmode = MCAST_EXCLUDE;
1693		else if (pmc->sfcount[MCAST_INCLUDE])
1694			pmc->sfmode = MCAST_INCLUDE;
1695#ifdef CONFIG_IP_MULTICAST
1696		/* else no filters; keep old mode for reports */
1697
1698		pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1699			IGMP_Unsolicited_Report_Count;
1700		in_dev->mr_ifc_count = pmc->crcount;
1701		for (psf=pmc->sources; psf; psf = psf->sf_next)
1702			psf->sf_crcount = 0;
1703		igmp_ifc_event(in_dev);
1704	} else if (sf_setstate(pmc)) {
1705		igmp_ifc_event(in_dev);
1706#endif
1707	}
1708	spin_unlock_bh(&pmc->lock);
1709	return err;
1710}
1711
1712static void ip_mc_clear_src(struct ip_mc_list *pmc)
1713{
1714	struct ip_sf_list *psf, *nextpsf;
1715
1716	for (psf=pmc->tomb; psf; psf=nextpsf) {
1717		nextpsf = psf->sf_next;
1718		kfree(psf);
1719	}
1720	pmc->tomb = NULL;
1721	for (psf=pmc->sources; psf; psf=nextpsf) {
1722		nextpsf = psf->sf_next;
1723		kfree(psf);
1724	}
1725	pmc->sources = NULL;
1726	pmc->sfmode = MCAST_EXCLUDE;
1727	pmc->sfcount[MCAST_INCLUDE] = 0;
1728	pmc->sfcount[MCAST_EXCLUDE] = 1;
1729}
1730
1731
1732/*
1733 * Join a multicast group
1734 */
1735int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1736{
1737	int err;
1738	__be32 addr = imr->imr_multiaddr.s_addr;
1739	struct ip_mc_socklist *iml=NULL, *i;
1740	struct in_device *in_dev;
1741	struct inet_sock *inet = inet_sk(sk);
1742	int ifindex;
1743	int count = 0;
1744
1745	if (!ipv4_is_multicast(addr))
1746		return -EINVAL;
1747
1748	rtnl_lock();
1749
1750	in_dev = ip_mc_find_dev(imr);
1751
1752	if (!in_dev) {
1753		iml = NULL;
1754		err = -ENODEV;
1755		goto done;
1756	}
1757
1758	err = -EADDRINUSE;
1759	ifindex = imr->imr_ifindex;
1760	for (i = inet->mc_list; i; i = i->next) {
1761		if (i->multi.imr_multiaddr.s_addr == addr &&
1762		    i->multi.imr_ifindex == ifindex)
1763			goto done;
1764		count++;
1765	}
1766	err = -ENOBUFS;
1767	if (count >= sysctl_igmp_max_memberships)
1768		goto done;
1769	iml = sock_kmalloc(sk,sizeof(*iml),GFP_KERNEL);
1770	if (iml == NULL)
1771		goto done;
1772
1773	memcpy(&iml->multi, imr, sizeof(*imr));
1774	iml->next = inet->mc_list;
1775	iml->sflist = NULL;
1776	iml->sfmode = MCAST_EXCLUDE;
1777	inet->mc_list = iml;
1778	ip_mc_inc_group(in_dev, addr);
1779	err = 0;
1780done:
1781	rtnl_unlock();
1782	return err;
1783}
1784
1785static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1786			   struct in_device *in_dev)
1787{
1788	int err;
1789
1790	if (iml->sflist == NULL) {
1791		/* any-source empty exclude case */
1792		return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1793			iml->sfmode, 0, NULL, 0);
1794	}
1795	err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1796			iml->sfmode, iml->sflist->sl_count,
1797			iml->sflist->sl_addr, 0);
1798	sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1799	iml->sflist = NULL;
1800	return err;
1801}
1802
1803/*
1804 *	Ask a socket to leave a group.
1805 */
1806
1807int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1808{
1809	struct inet_sock *inet = inet_sk(sk);
1810	struct ip_mc_socklist *iml, **imlp;
1811	struct in_device *in_dev;
1812	__be32 group = imr->imr_multiaddr.s_addr;
1813	u32 ifindex;
1814	int ret = -EADDRNOTAVAIL;
1815
1816	rtnl_lock();
1817	in_dev = ip_mc_find_dev(imr);
1818	ifindex = imr->imr_ifindex;
1819	for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
1820		if (iml->multi.imr_multiaddr.s_addr != group)
1821			continue;
1822		if (ifindex) {
1823			if (iml->multi.imr_ifindex != ifindex)
1824				continue;
1825		} else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1826				iml->multi.imr_address.s_addr)
1827			continue;
1828
1829		(void) ip_mc_leave_src(sk, iml, in_dev);
1830
1831		*imlp = iml->next;
1832
1833		if (in_dev)
1834			ip_mc_dec_group(in_dev, group);
1835		rtnl_unlock();
1836		sock_kfree_s(sk, iml, sizeof(*iml));
1837		return 0;
1838	}
1839	if (!in_dev)
1840		ret = -ENODEV;
1841	rtnl_unlock();
1842	return ret;
1843}
1844
1845int ip_mc_source(int add, int omode, struct sock *sk, struct
1846	ip_mreq_source *mreqs, int ifindex)
1847{
1848	int err;
1849	struct ip_mreqn imr;
1850	__be32 addr = mreqs->imr_multiaddr;
1851	struct ip_mc_socklist *pmc;
1852	struct in_device *in_dev = NULL;
1853	struct inet_sock *inet = inet_sk(sk);
1854	struct ip_sf_socklist *psl;
1855	int leavegroup = 0;
1856	int i, j, rv;
1857
1858	if (!ipv4_is_multicast(addr))
1859		return -EINVAL;
1860
1861	rtnl_lock();
1862
1863	imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1864	imr.imr_address.s_addr = mreqs->imr_interface;
1865	imr.imr_ifindex = ifindex;
1866	in_dev = ip_mc_find_dev(&imr);
1867
1868	if (!in_dev) {
1869		err = -ENODEV;
1870		goto done;
1871	}
1872	err = -EADDRNOTAVAIL;
1873
1874	for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1875		if (pmc->multi.imr_multiaddr.s_addr == imr.imr_multiaddr.s_addr
1876		    && pmc->multi.imr_ifindex == imr.imr_ifindex)
1877			break;
1878	}
1879	if (!pmc) {		/* must have a prior join */
1880		err = -EINVAL;
1881		goto done;
1882	}
1883	/* if a source filter was set, must be the same mode as before */
1884	if (pmc->sflist) {
1885		if (pmc->sfmode != omode) {
1886			err = -EINVAL;
1887			goto done;
1888		}
1889	} else if (pmc->sfmode != omode) {
1890		/* allow mode switches for empty-set filters */
1891		ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1892		ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1893			NULL, 0);
1894		pmc->sfmode = omode;
1895	}
1896
1897	psl = pmc->sflist;
1898	if (!add) {
1899		if (!psl)
1900			goto done;	/* err = -EADDRNOTAVAIL */
1901		rv = !0;
1902		for (i=0; i<psl->sl_count; i++) {
1903			rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1904				sizeof(__be32));
1905			if (rv == 0)
1906				break;
1907		}
1908		if (rv)		/* source not found */
1909			goto done;	/* err = -EADDRNOTAVAIL */
1910
1911		/* special case - (INCLUDE, empty) == LEAVE_GROUP */
1912		if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1913			leavegroup = 1;
1914			goto done;
1915		}
1916
1917		/* update the interface filter */
1918		ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1919			&mreqs->imr_sourceaddr, 1);
1920
1921		for (j=i+1; j<psl->sl_count; j++)
1922			psl->sl_addr[j-1] = psl->sl_addr[j];
1923		psl->sl_count--;
1924		err = 0;
1925		goto done;
1926	}
1927	/* else, add a new source to the filter */
1928
1929	if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1930		err = -ENOBUFS;
1931		goto done;
1932	}
1933	if (!psl || psl->sl_count == psl->sl_max) {
1934		struct ip_sf_socklist *newpsl;
1935		int count = IP_SFBLOCK;
1936
1937		if (psl)
1938			count += psl->sl_max;
1939		newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
1940		if (!newpsl) {
1941			err = -ENOBUFS;
1942			goto done;
1943		}
1944		newpsl->sl_max = count;
1945		newpsl->sl_count = count - IP_SFBLOCK;
1946		if (psl) {
1947			for (i=0; i<psl->sl_count; i++)
1948				newpsl->sl_addr[i] = psl->sl_addr[i];
1949			sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1950		}
1951		pmc->sflist = psl = newpsl;
1952	}
1953	rv = 1;	/* > 0 for insert logic below if sl_count is 0 */
1954	for (i=0; i<psl->sl_count; i++) {
1955		rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1956			sizeof(__be32));
1957		if (rv == 0)
1958			break;
1959	}
1960	if (rv == 0)		/* address already there is an error */
1961		goto done;
1962	for (j=psl->sl_count-1; j>=i; j--)
1963		psl->sl_addr[j+1] = psl->sl_addr[j];
1964	psl->sl_addr[i] = mreqs->imr_sourceaddr;
1965	psl->sl_count++;
1966	err = 0;
1967	/* update the interface list */
1968	ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1969		&mreqs->imr_sourceaddr, 1);
1970done:
1971	rtnl_unlock();
1972	if (leavegroup)
1973		return ip_mc_leave_group(sk, &imr);
1974	return err;
1975}
1976
1977int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
1978{
1979	int err = 0;
1980	struct ip_mreqn	imr;
1981	__be32 addr = msf->imsf_multiaddr;
1982	struct ip_mc_socklist *pmc;
1983	struct in_device *in_dev;
1984	struct inet_sock *inet = inet_sk(sk);
1985	struct ip_sf_socklist *newpsl, *psl;
1986	int leavegroup = 0;
1987
1988	if (!ipv4_is_multicast(addr))
1989		return -EINVAL;
1990	if (msf->imsf_fmode != MCAST_INCLUDE &&
1991	    msf->imsf_fmode != MCAST_EXCLUDE)
1992		return -EINVAL;
1993
1994	rtnl_lock();
1995
1996	imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1997	imr.imr_address.s_addr = msf->imsf_interface;
1998	imr.imr_ifindex = ifindex;
1999	in_dev = ip_mc_find_dev(&imr);
2000
2001	if (!in_dev) {
2002		err = -ENODEV;
2003		goto done;
2004	}
2005
2006	/* special case - (INCLUDE, empty) == LEAVE_GROUP */
2007	if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2008		leavegroup = 1;
2009		goto done;
2010	}
2011
2012	for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2013		if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2014		    pmc->multi.imr_ifindex == imr.imr_ifindex)
2015			break;
2016	}
2017	if (!pmc) {		/* must have a prior join */
2018		err = -EINVAL;
2019		goto done;
2020	}
2021	if (msf->imsf_numsrc) {
2022		newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2023							   GFP_KERNEL);
2024		if (!newpsl) {
2025			err = -ENOBUFS;
2026			goto done;
2027		}
2028		newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2029		memcpy(newpsl->sl_addr, msf->imsf_slist,
2030			msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2031		err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2032			msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2033		if (err) {
2034			sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2035			goto done;
2036		}
2037	} else {
2038		newpsl = NULL;
2039		(void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2040				     msf->imsf_fmode, 0, NULL, 0);
2041	}
2042	psl = pmc->sflist;
2043	if (psl) {
2044		(void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2045			psl->sl_count, psl->sl_addr, 0);
2046		sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
2047	} else
2048		(void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2049			0, NULL, 0);
2050	pmc->sflist = newpsl;
2051	pmc->sfmode = msf->imsf_fmode;
2052	err = 0;
2053done:
2054	rtnl_unlock();
2055	if (leavegroup)
2056		err = ip_mc_leave_group(sk, &imr);
2057	return err;
2058}
2059
2060int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2061	struct ip_msfilter __user *optval, int __user *optlen)
2062{
2063	int err, len, count, copycount;
2064	struct ip_mreqn	imr;
2065	__be32 addr = msf->imsf_multiaddr;
2066	struct ip_mc_socklist *pmc;
2067	struct in_device *in_dev;
2068	struct inet_sock *inet = inet_sk(sk);
2069	struct ip_sf_socklist *psl;
2070
2071	if (!ipv4_is_multicast(addr))
2072		return -EINVAL;
2073
2074	rtnl_lock();
2075
2076	imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2077	imr.imr_address.s_addr = msf->imsf_interface;
2078	imr.imr_ifindex = 0;
2079	in_dev = ip_mc_find_dev(&imr);
2080
2081	if (!in_dev) {
2082		err = -ENODEV;
2083		goto done;
2084	}
2085	err = -EADDRNOTAVAIL;
2086
2087	for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2088		if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2089		    pmc->multi.imr_ifindex == imr.imr_ifindex)
2090			break;
2091	}
2092	if (!pmc)		/* must have a prior join */
2093		goto done;
2094	msf->imsf_fmode = pmc->sfmode;
2095	psl = pmc->sflist;
2096	rtnl_unlock();
2097	if (!psl) {
2098		len = 0;
2099		count = 0;
2100	} else {
2101		count = psl->sl_count;
2102	}
2103	copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2104	len = copycount * sizeof(psl->sl_addr[0]);
2105	msf->imsf_numsrc = count;
2106	if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2107	    copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2108		return -EFAULT;
2109	}
2110	if (len &&
2111	    copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2112		return -EFAULT;
2113	return 0;
2114done:
2115	rtnl_unlock();
2116	return err;
2117}
2118
2119int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2120	struct group_filter __user *optval, int __user *optlen)
2121{
2122	int err, i, count, copycount;
2123	struct sockaddr_in *psin;
2124	__be32 addr;
2125	struct ip_mc_socklist *pmc;
2126	struct inet_sock *inet = inet_sk(sk);
2127	struct ip_sf_socklist *psl;
2128
2129	psin = (struct sockaddr_in *)&gsf->gf_group;
2130	if (psin->sin_family != AF_INET)
2131		return -EINVAL;
2132	addr = psin->sin_addr.s_addr;
2133	if (!ipv4_is_multicast(addr))
2134		return -EINVAL;
2135
2136	rtnl_lock();
2137
2138	err = -EADDRNOTAVAIL;
2139
2140	for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2141		if (pmc->multi.imr_multiaddr.s_addr == addr &&
2142		    pmc->multi.imr_ifindex == gsf->gf_interface)
2143			break;
2144	}
2145	if (!pmc)		/* must have a prior join */
2146		goto done;
2147	gsf->gf_fmode = pmc->sfmode;
2148	psl = pmc->sflist;
2149	rtnl_unlock();
2150	count = psl ? psl->sl_count : 0;
2151	copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2152	gsf->gf_numsrc = count;
2153	if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2154	    copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2155		return -EFAULT;
2156	}
2157	for (i=0; i<copycount; i++) {
2158		struct sockaddr_storage ss;
2159
2160		psin = (struct sockaddr_in *)&ss;
2161		memset(&ss, 0, sizeof(ss));
2162		psin->sin_family = AF_INET;
2163		psin->sin_addr.s_addr = psl->sl_addr[i];
2164		if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2165			return -EFAULT;
2166	}
2167	return 0;
2168done:
2169	rtnl_unlock();
2170	return err;
2171}
2172
2173/*
2174 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2175 */
2176int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2177{
2178	struct inet_sock *inet = inet_sk(sk);
2179	struct ip_mc_socklist *pmc;
2180	struct ip_sf_socklist *psl;
2181	int i;
2182
2183	if (!ipv4_is_multicast(loc_addr))
2184		return 1;
2185
2186	for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2187		if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2188		    pmc->multi.imr_ifindex == dif)
2189			break;
2190	}
2191	if (!pmc)
2192		return 1;
2193	psl = pmc->sflist;
2194	if (!psl)
2195		return pmc->sfmode == MCAST_EXCLUDE;
2196
2197	for (i=0; i<psl->sl_count; i++) {
2198		if (psl->sl_addr[i] == rmt_addr)
2199			break;
2200	}
2201	if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2202		return 0;
2203	if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2204		return 0;
2205	return 1;
2206}
2207
2208/*
2209 *	A socket is closing.
2210 */
2211
2212void ip_mc_drop_socket(struct sock *sk)
2213{
2214	struct inet_sock *inet = inet_sk(sk);
2215	struct ip_mc_socklist *iml;
2216
2217	if (inet->mc_list == NULL)
2218		return;
2219
2220	rtnl_lock();
2221	while ((iml = inet->mc_list) != NULL) {
2222		struct in_device *in_dev;
2223		inet->mc_list = iml->next;
2224
2225		in_dev = inetdev_by_index(iml->multi.imr_ifindex);
2226		(void) ip_mc_leave_src(sk, iml, in_dev);
2227		if (in_dev != NULL) {
2228			ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2229			in_dev_put(in_dev);
2230		}
2231		sock_kfree_s(sk, iml, sizeof(*iml));
2232	}
2233	rtnl_unlock();
2234}
2235
2236int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
2237{
2238	struct ip_mc_list *im;
2239	struct ip_sf_list *psf;
2240	int rv = 0;
2241
2242	read_lock(&in_dev->mc_list_lock);
2243	for (im=in_dev->mc_list; im; im=im->next) {
2244		if (im->multiaddr == mc_addr)
2245			break;
2246	}
2247	if (im && proto == IPPROTO_IGMP) {
2248		rv = 1;
2249	} else if (im) {
2250		if (src_addr) {
2251			for (psf=im->sources; psf; psf=psf->sf_next) {
2252				if (psf->sf_inaddr == src_addr)
2253					break;
2254			}
2255			if (psf)
2256				rv = psf->sf_count[MCAST_INCLUDE] ||
2257					psf->sf_count[MCAST_EXCLUDE] !=
2258					im->sfcount[MCAST_EXCLUDE];
2259			else
2260				rv = im->sfcount[MCAST_EXCLUDE] != 0;
2261		} else
2262			rv = 1; /* unspecified source; tentatively allow */
2263	}
2264	read_unlock(&in_dev->mc_list_lock);
2265	return rv;
2266}
2267
2268#if defined(CONFIG_PROC_FS)
2269struct igmp_mc_iter_state {
2270	struct net_device *dev;
2271	struct in_device *in_dev;
2272};
2273
2274#define	igmp_mc_seq_private(seq)	((struct igmp_mc_iter_state *)(seq)->private)
2275
2276static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2277{
2278	struct ip_mc_list *im = NULL;
2279	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2280
2281	state->in_dev = NULL;
2282	for_each_netdev(&init_net, state->dev) {
2283		struct in_device *in_dev;
2284		in_dev = in_dev_get(state->dev);
2285		if (!in_dev)
2286			continue;
2287		read_lock(&in_dev->mc_list_lock);
2288		im = in_dev->mc_list;
2289		if (im) {
2290			state->in_dev = in_dev;
2291			break;
2292		}
2293		read_unlock(&in_dev->mc_list_lock);
2294		in_dev_put(in_dev);
2295	}
2296	return im;
2297}
2298
2299static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2300{
2301	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2302	im = im->next;
2303	while (!im) {
2304		if (likely(state->in_dev != NULL)) {
2305			read_unlock(&state->in_dev->mc_list_lock);
2306			in_dev_put(state->in_dev);
2307		}
2308		state->dev = next_net_device(state->dev);
2309		if (!state->dev) {
2310			state->in_dev = NULL;
2311			break;
2312		}
2313		state->in_dev = in_dev_get(state->dev);
2314		if (!state->in_dev)
2315			continue;
2316		read_lock(&state->in_dev->mc_list_lock);
2317		im = state->in_dev->mc_list;
2318	}
2319	return im;
2320}
2321
2322static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2323{
2324	struct ip_mc_list *im = igmp_mc_get_first(seq);
2325	if (im)
2326		while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2327			--pos;
2328	return pos ? NULL : im;
2329}
2330
2331static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2332{
2333	read_lock(&dev_base_lock);
2334	return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2335}
2336
2337static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2338{
2339	struct ip_mc_list *im;
2340	if (v == SEQ_START_TOKEN)
2341		im = igmp_mc_get_first(seq);
2342	else
2343		im = igmp_mc_get_next(seq, v);
2344	++*pos;
2345	return im;
2346}
2347
2348static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2349{
2350	struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2351	if (likely(state->in_dev != NULL)) {
2352		read_unlock(&state->in_dev->mc_list_lock);
2353		in_dev_put(state->in_dev);
2354		state->in_dev = NULL;
2355	}
2356	state->dev = NULL;
2357	read_unlock(&dev_base_lock);
2358}
2359
2360static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2361{
2362	if (v == SEQ_START_TOKEN)
2363		seq_puts(seq,
2364			 "Idx\tDevice    : Count Querier\tGroup    Users Timer\tReporter\n");
2365	else {
2366		struct ip_mc_list *im = (struct ip_mc_list *)v;
2367		struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2368		char   *querier;
2369#ifdef CONFIG_IP_MULTICAST
2370		querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2371			  IGMP_V2_SEEN(state->in_dev) ? "V2" :
2372			  "V3";
2373#else
2374		querier = "NONE";
2375#endif
2376
2377		if (state->in_dev->mc_list == im) {
2378			seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2379				   state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
2380		}
2381
2382		seq_printf(seq,
2383			   "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2384			   im->multiaddr, im->users,
2385			   im->tm_running, im->tm_running ?
2386			   jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2387			   im->reporter);
2388	}
2389	return 0;
2390}
2391
2392static const struct seq_operations igmp_mc_seq_ops = {
2393	.start	=	igmp_mc_seq_start,
2394	.next	=	igmp_mc_seq_next,
2395	.stop	=	igmp_mc_seq_stop,
2396	.show	=	igmp_mc_seq_show,
2397};
2398
2399static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2400{
2401	return seq_open_private(file, &igmp_mc_seq_ops,
2402			sizeof(struct igmp_mc_iter_state));
2403}
2404
2405static const struct file_operations igmp_mc_seq_fops = {
2406	.owner		=	THIS_MODULE,
2407	.open		=	igmp_mc_seq_open,
2408	.read		=	seq_read,
2409	.llseek		=	seq_lseek,
2410	.release	=	seq_release_private,
2411};
2412
2413struct igmp_mcf_iter_state {
2414	struct net_device *dev;
2415	struct in_device *idev;
2416	struct ip_mc_list *im;
2417};
2418
2419#define igmp_mcf_seq_private(seq)	((struct igmp_mcf_iter_state *)(seq)->private)
2420
2421static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2422{
2423	struct ip_sf_list *psf = NULL;
2424	struct ip_mc_list *im = NULL;
2425	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2426
2427	state->idev = NULL;
2428	state->im = NULL;
2429	for_each_netdev(&init_net, state->dev) {
2430		struct in_device *idev;
2431		idev = in_dev_get(state->dev);
2432		if (unlikely(idev == NULL))
2433			continue;
2434		read_lock(&idev->mc_list_lock);
2435		im = idev->mc_list;
2436		if (likely(im != NULL)) {
2437			spin_lock_bh(&im->lock);
2438			psf = im->sources;
2439			if (likely(psf != NULL)) {
2440				state->im = im;
2441				state->idev = idev;
2442				break;
2443			}
2444			spin_unlock_bh(&im->lock);
2445		}
2446		read_unlock(&idev->mc_list_lock);
2447		in_dev_put(idev);
2448	}
2449	return psf;
2450}
2451
2452static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2453{
2454	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2455
2456	psf = psf->sf_next;
2457	while (!psf) {
2458		spin_unlock_bh(&state->im->lock);
2459		state->im = state->im->next;
2460		while (!state->im) {
2461			if (likely(state->idev != NULL)) {
2462				read_unlock(&state->idev->mc_list_lock);
2463				in_dev_put(state->idev);
2464			}
2465			state->dev = next_net_device(state->dev);
2466			if (!state->dev) {
2467				state->idev = NULL;
2468				goto out;
2469			}
2470			state->idev = in_dev_get(state->dev);
2471			if (!state->idev)
2472				continue;
2473			read_lock(&state->idev->mc_list_lock);
2474			state->im = state->idev->mc_list;
2475		}
2476		if (!state->im)
2477			break;
2478		spin_lock_bh(&state->im->lock);
2479		psf = state->im->sources;
2480	}
2481out:
2482	return psf;
2483}
2484
2485static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2486{
2487	struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2488	if (psf)
2489		while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2490			--pos;
2491	return pos ? NULL : psf;
2492}
2493
2494static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2495{
2496	read_lock(&dev_base_lock);
2497	return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2498}
2499
2500static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2501{
2502	struct ip_sf_list *psf;
2503	if (v == SEQ_START_TOKEN)
2504		psf = igmp_mcf_get_first(seq);
2505	else
2506		psf = igmp_mcf_get_next(seq, v);
2507	++*pos;
2508	return psf;
2509}
2510
2511static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2512{
2513	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2514	if (likely(state->im != NULL)) {
2515		spin_unlock_bh(&state->im->lock);
2516		state->im = NULL;
2517	}
2518	if (likely(state->idev != NULL)) {
2519		read_unlock(&state->idev->mc_list_lock);
2520		in_dev_put(state->idev);
2521		state->idev = NULL;
2522	}
2523	state->dev = NULL;
2524	read_unlock(&dev_base_lock);
2525}
2526
2527static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2528{
2529	struct ip_sf_list *psf = (struct ip_sf_list *)v;
2530	struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2531
2532	if (v == SEQ_START_TOKEN) {
2533		seq_printf(seq,
2534			   "%3s %6s "
2535			   "%10s %10s %6s %6s\n", "Idx",
2536			   "Device", "MCA",
2537			   "SRC", "INC", "EXC");
2538	} else {
2539		seq_printf(seq,
2540			   "%3d %6.6s 0x%08x "
2541			   "0x%08x %6lu %6lu\n",
2542			   state->dev->ifindex, state->dev->name,
2543			   ntohl(state->im->multiaddr),
2544			   ntohl(psf->sf_inaddr),
2545			   psf->sf_count[MCAST_INCLUDE],
2546			   psf->sf_count[MCAST_EXCLUDE]);
2547	}
2548	return 0;
2549}
2550
2551static const struct seq_operations igmp_mcf_seq_ops = {
2552	.start	=	igmp_mcf_seq_start,
2553	.next	=	igmp_mcf_seq_next,
2554	.stop	=	igmp_mcf_seq_stop,
2555	.show	=	igmp_mcf_seq_show,
2556};
2557
2558static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2559{
2560	return seq_open_private(file, &igmp_mcf_seq_ops,
2561			sizeof(struct igmp_mcf_iter_state));
2562}
2563
2564static const struct file_operations igmp_mcf_seq_fops = {
2565	.owner		=	THIS_MODULE,
2566	.open		=	igmp_mcf_seq_open,
2567	.read		=	seq_read,
2568	.llseek		=	seq_lseek,
2569	.release	=	seq_release_private,
2570};
2571
2572int __init igmp_mc_proc_init(void)
2573{
2574	proc_net_fops_create(&init_net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2575	proc_net_fops_create(&init_net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2576	return 0;
2577}
2578#endif
2579
2580EXPORT_SYMBOL(ip_mc_dec_group);
2581EXPORT_SYMBOL(ip_mc_inc_group);
2582EXPORT_SYMBOL(ip_mc_join_group);
2583EXPORT_SYMBOL(ip_mc_rejoin_group);
2584