ip_mroute.c revision 41591
1163953Srrs/*
2166675Srrs * IP multicast forwarding procedures
3163953Srrs *
4163953Srrs * Written by David Waitzman, BBN Labs, August 1988.
5163953Srrs * Modified by Steve Deering, Stanford, February 1989.
6163953Srrs * Modified by Mark J. Steiglitz, Stanford, May, 1991
7163953Srrs * Modified by Van Jacobson, LBL, January 1993
8163953Srrs * Modified by Ajit Thyagarajan, PARC, August 1993
9163953Srrs * Modified by Bill Fenner, PARC, April 1995
10163953Srrs *
11163953Srrs * MROUTING Revision: 3.5
12163953Srrs * $Id: ip_mroute.c,v 1.49 1998/08/23 03:07:14 wollman Exp $
13163953Srrs */
14163953Srrs
15163953Srrs#include "opt_mrouting.h"
16163953Srrs
17163953Srrs#include <sys/param.h>
18163953Srrs#include <sys/systm.h>
19163953Srrs#include <sys/mbuf.h>
20163953Srrs#include <sys/socket.h>
21163953Srrs#include <sys/socketvar.h>
22163953Srrs#include <sys/protosw.h>
23163953Srrs#include <sys/time.h>
24163953Srrs#include <sys/kernel.h>
25163953Srrs#include <sys/sockio.h>
26163953Srrs#include <sys/syslog.h>
27163953Srrs#include <net/if.h>
28163953Srrs#include <net/route.h>
29163953Srrs#include <netinet/in.h>
30163953Srrs#include <netinet/in_systm.h>
31163953Srrs#include <netinet/ip.h>
32163953Srrs#include <netinet/ip_var.h>
33163953Srrs#include <netinet/in_var.h>
34163953Srrs#include <netinet/igmp.h>
35163953Srrs#include <netinet/ip_mroute.h>
36163953Srrs#include <netinet/udp.h>
37163953Srrs
38167598Srrs#ifndef NTOHL
39163953Srrs#if BYTE_ORDER != BIG_ENDIAN
40163953Srrs#define NTOHL(d) ((d) = ntohl((d)))
41163953Srrs#define NTOHS(d) ((d) = ntohs((u_short)(d)))
42163953Srrs#define HTONL(d) ((d) = htonl((d)))
43163953Srrs#define HTONS(d) ((d) = htons((u_short)(d)))
44163953Srrs#else
45163953Srrs#define NTOHL(d)
46163953Srrs#define NTOHS(d)
47163953Srrs#define HTONL(d)
48163953Srrs#define HTONS(d)
49163953Srrs#endif
50163953Srrs#endif
51163953Srrs
52163953Srrs#ifndef MROUTING
53163953Srrsextern u_long	_ip_mcast_src __P((int vifi));
54163953Srrsextern int	_ip_mforward __P((struct ip *ip, struct ifnet *ifp,
55163953Srrs				  struct mbuf *m, struct ip_moptions *imo));
56165220Srrsextern int	_ip_mrouter_done __P((void));
57165220Srrsextern int	_ip_mrouter_get __P((struct socket *so, struct sockopt *sopt));
58165220Srrsextern int	_ip_mrouter_set __P((struct socket *so, struct sockopt *sopt));
59165220Srrsextern int	_mrt_ioctl __P((int req, caddr_t data, struct proc *p));
60165220Srrs
61163953Srrs/*
62163953Srrs * Dummy routines and globals used when multicast routing is not compiled in.
63165220Srrs */
64163953Srrs
65163953Srrsstruct socket  *ip_mrouter  = NULL;
66163953Srrsstatic u_int		ip_mrtproto = 0;
67165220Srrsstatic struct mrtstat	mrtstat;
68165220Srrsu_int		rsvpdebug = 0;
69165220Srrs
70165220Srrsint
71165220Srrs_ip_mrouter_set(so, sopt)
72165220Srrs	struct socket *so;
73163953Srrs	struct sockopt *sopt;
74163953Srrs{
75163953Srrs	return(EOPNOTSUPP);
76163953Srrs}
77163953Srrs
78163953Srrsint (*ip_mrouter_set)(struct socket *, struct sockopt *) = _ip_mrouter_set;
79163953Srrs
80163953Srrs
81168299Srrsint
82163953Srrs_ip_mrouter_get(so, sopt)
83163953Srrs	struct socket *so;
84163953Srrs	struct sockopt *sopt;
85163953Srrs{
86163953Srrs	return(EOPNOTSUPP);
87163953Srrs}
88163953Srrs
89163953Srrsint (*ip_mrouter_get)(struct socket *, struct sockopt *) = _ip_mrouter_get;
90163953Srrs
91163953Srrsint
92163953Srrs_ip_mrouter_done()
93163953Srrs{
94163953Srrs	return(0);
95163953Srrs}
96163953Srrs
97163953Srrsint (*ip_mrouter_done)(void) = _ip_mrouter_done;
98163953Srrs
99163953Srrsint
100163953Srrs_ip_mforward(ip, ifp, m, imo)
101163953Srrs	struct ip *ip;
102163953Srrs	struct ifnet *ifp;
103163953Srrs	struct mbuf *m;
104163953Srrs	struct ip_moptions *imo;
105163953Srrs{
106168299Srrs	return(0);
107168299Srrs}
108163953Srrs
109163953Srrsint (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
110163953Srrs		   struct ip_moptions *) = _ip_mforward;
111163953Srrs
112163953Srrsint
113163953Srrs_mrt_ioctl(int req, caddr_t data, struct proc *p)
114168299Srrs{
115168299Srrs	return EOPNOTSUPP;
116163953Srrs}
117163953Srrs
118163953Srrsint (*mrt_ioctl)(int, caddr_t, struct proc *) = _mrt_ioctl;
119163953Srrs
120163953Srrsvoid
121163953Srrsrsvp_input(m, iphlen)		/* XXX must fixup manually */
122163953Srrs	struct mbuf *m;
123168299Srrs	int iphlen;
124168299Srrs{
125163953Srrs    /* Can still get packets with rsvp_on = 0 if there is a local member
126163953Srrs     * of the group to which the RSVP packet is addressed.  But in this
127163953Srrs     * case we want to throw the packet away.
128163953Srrs     */
129163953Srrs    if (!rsvp_on) {
130163953Srrs	m_freem(m);
131163953Srrs	return;
132163953Srrs    }
133163953Srrs
134163953Srrs    if (ip_rsvpd != NULL) {
135163953Srrs	if (rsvpdebug)
136163953Srrs	    printf("rsvp_input: Sending packet up old-style socket\n");
137168299Srrs	rip_input(m, iphlen);
138168299Srrs	return;
139163953Srrs    }
140163953Srrs    /* Drop the packet */
141163953Srrs    m_freem(m);
142163953Srrs}
143163953Srrs
144163953Srrsvoid ipip_input(struct mbuf *m, int iphlen) { /* XXX must fixup manually */
145168299Srrs	rip_input(m, iphlen);
146168299Srrs}
147163953Srrs
148163953Srrsint (*legal_vif_num)(int) = 0;
149163953Srrs
150163953Srrs/*
151163953Srrs * This should never be called, since IP_MULTICAST_VIF should fail, but
152163953Srrs * just in case it does get called, the code a little lower in ip_output
153163953Srrs * will assign the packet a local address.
154168299Srrs */
155168299Srrsu_long
156163953Srrs_ip_mcast_src(int vifi) { return INADDR_ANY; }
157163953Srrsu_long (*ip_mcast_src)(int) = _ip_mcast_src;
158163953Srrs
159163953Srrsint
160163953Srrsip_rsvp_vif_init(so, sopt)
161163953Srrs    struct socket *so;
162163953Srrs    struct sockopt *sopt;
163163953Srrs{
164163953Srrs    return(EINVAL);
165163953Srrs}
166163953Srrs
167163953Srrsint
168163953Srrsip_rsvp_vif_done(so, sopt)
169163953Srrs    struct socket *so;
170163953Srrs    struct sockopt *sopt;
171163953Srrs{
172163953Srrs    return(EINVAL);
173163953Srrs}
174163953Srrs
175163953Srrsvoid
176163953Srrsip_rsvp_force_done(so)
177163953Srrs    struct socket *so;
178163953Srrs{
179163953Srrs    return;
180163953Srrs}
181163953Srrs
182163953Srrs#else /* MROUTING */
183163953Srrs
184163953Srrs#define M_HASCL(m)	((m)->m_flags & M_EXT)
185163953Srrs
186163953Srrs#define INSIZ		sizeof(struct in_addr)
187163953Srrs#define	same(a1, a2) \
188163953Srrs	(bcmp((caddr_t)(a1), (caddr_t)(a2), INSIZ) == 0)
189163953Srrs
190163953Srrs#define MT_MRTABLE MT_RTABLE	/* since nothing else uses it */
191163953Srrs
192163953Srrs/*
193163953Srrs * Globals.  All but ip_mrouter and ip_mrtproto could be static,
194163953Srrs * except for netstat or debugging purposes.
195164181Srrs */
196163953Srrs#ifndef MROUTE_LKM
197163953Srrsstruct socket  *ip_mrouter  = NULL;
198163953Srrsstatic struct mrtstat	mrtstat;
199163953Srrs
200163953Srrsstatic int		ip_mrtproto = IGMP_DVMRP;    /* for netstat only */
201163953Srrs#else /* MROUTE_LKM */
202163953Srrsextern void	X_ipip_input __P((struct mbuf *m, int iphlen));
203163953Srrsextern struct mrtstat mrtstat;
204163953Srrsstatic int ip_mrtproto;
205163953Srrs#endif
206163953Srrs
207163953Srrs#define NO_RTE_FOUND 	0x1
208163953Srrs#define RTE_FOUND	0x2
209163953Srrs
210163953Srrsstatic struct mbuf    *mfctable[MFCTBLSIZ];
211163953Srrsstatic u_char		nexpire[MFCTBLSIZ];
212163953Srrsstatic struct vif	viftable[MAXVIFS];
213163953Srrsstatic u_int	mrtdebug = 0;	  /* debug level 	*/
214163953Srrs#define		DEBUG_MFC	0x02
215163953Srrs#define		DEBUG_FORWARD	0x04
216163953Srrs#define		DEBUG_EXPIRE	0x08
217163953Srrs#define		DEBUG_XMIT	0x10
218163953Srrsstatic u_int  	tbfdebug = 0;     /* tbf debug level 	*/
219163953Srrsstatic u_int	rsvpdebug = 0;	  /* rsvp debug level   */
220163953Srrs
221163953Srrsstatic struct callout_handle expire_upcalls_ch;
222163953Srrs
223163964Srrs#define		EXPIRE_TIMEOUT	(hz / 4)	/* 4x / second		*/
224163979Sru#define		UPCALL_EXPIRE	6		/* number of timeouts	*/
225163953Srrs
226163953Srrs/*
227163953Srrs * Define the token bucket filter structures
228163953Srrs * tbftable -> each vif has one of these for storing info
229163953Srrs */
230163953Srrs
231163953Srrsstatic struct tbf tbftable[MAXVIFS];
232163953Srrs#define		TBF_REPROCESS	(hz / 100)	/* 100x / second */
233163953Srrs
234164181Srrs/*
235163953Srrs * 'Interfaces' associated with decapsulator (so we can tell
236163953Srrs * packets that went through it from ones that get reflected
237163953Srrs * by a broken gateway).  These interfaces are never linked into
238163953Srrs * the system ifnet list & no routes point to them.  I.e., packets
239163953Srrs * can't be sent this way.  They only exist as a placeholder for
240163953Srrs * multicast source verification.
241163953Srrs */
242163953Srrsstatic struct ifnet multicast_decap_if[MAXVIFS];
243163953Srrs
244163953Srrs#define ENCAP_TTL 64
245163953Srrs#define ENCAP_PROTO IPPROTO_IPIP	/* 4 */
246163953Srrs
247163953Srrs/* prototype IP hdr for encapsulated packets */
248163953Srrsstatic struct ip multicast_encap_iphdr = {
249163953Srrs#if BYTE_ORDER == LITTLE_ENDIAN
250163953Srrs	sizeof(struct ip) >> 2, IPVERSION,
251164181Srrs#else
252164181Srrs	IPVERSION, sizeof(struct ip) >> 2,
253164181Srrs#endif
254164181Srrs	0,				/* tos */
255164181Srrs	sizeof(struct ip),		/* total length */
256164181Srrs	0,				/* id */
257164181Srrs	0,				/* frag offset */
258164181Srrs	ENCAP_TTL, ENCAP_PROTO,
259164181Srrs	0,				/* checksum */
260164181Srrs};
261164181Srrs
262164181Srrs/*
263164181Srrs * Private variables.
264163953Srrs */
265163953Srrsstatic vifi_t	   numvifs = 0;
266164181Srrsstatic int have_encap_tunnel = 0;
267164181Srrs
268164181Srrs/*
269164181Srrs * one-back cache used by ipip_input to locate a tunnel's vif
270163953Srrs * given a datagram's src ip address.
271163953Srrs */
272163953Srrsstatic u_long last_encap_src;
273163953Srrsstatic struct vif *last_encap_vif;
274163953Srrs
275163953Srrsstatic u_long	X_ip_mcast_src __P((int vifi));
276163953Srrsstatic int	X_ip_mforward __P((struct ip *ip, struct ifnet *ifp, struct mbuf *m, struct ip_moptions *imo));
277163953Srrsstatic int	X_ip_mrouter_done __P((void));
278163953Srrsstatic int	X_ip_mrouter_get __P((struct socket *so, struct sockopt *m));
279163953Srrsstatic int	X_ip_mrouter_set __P((struct socket *so, struct sockopt *m));
280163953Srrsstatic int	X_legal_vif_num __P((int vif));
281163953Srrsstatic int	X_mrt_ioctl __P((int cmd, caddr_t data));
282163953Srrs
283163953Srrsstatic int get_sg_cnt(struct sioc_sg_req *);
284163953Srrsstatic int get_vif_cnt(struct sioc_vif_req *);
285163953Srrsstatic int ip_mrouter_init(struct socket *, int);
286163953Srrsstatic int add_vif(struct vifctl *);
287163953Srrsstatic int del_vif(vifi_t);
288163953Srrsstatic int add_mfc(struct mfcctl *);
289163953Srrsstatic int del_mfc(struct mfcctl *);
290163953Srrsstatic int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
291163953Srrsstatic int set_assert(int);
292163953Srrsstatic void expire_upcalls(void *);
293163953Srrsstatic int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *,
294163953Srrs		  vifi_t);
295163953Srrsstatic void phyint_send(struct ip *, struct vif *, struct mbuf *);
296163953Srrsstatic void encap_send(struct ip *, struct vif *, struct mbuf *);
297163953Srrsstatic void tbf_control(struct vif *, struct mbuf *, struct ip *, u_long);
298163953Srrsstatic void tbf_queue(struct vif *, struct mbuf *);
299163953Srrsstatic void tbf_process_q(struct vif *);
300163953Srrsstatic void tbf_reprocess_q(void *);
301163953Srrsstatic int tbf_dq_sel(struct vif *, struct ip *);
302163953Srrsstatic void tbf_send_packet(struct vif *, struct mbuf *);
303163953Srrsstatic void tbf_update_tokens(struct vif *);
304163953Srrsstatic int priority(struct vif *, struct ip *);
305163953Srrsvoid multiencap_decap(struct mbuf *);
306163953Srrs
307163953Srrs/*
308163953Srrs * whether or not special PIM assert processing is enabled.
309163953Srrs */
310163953Srrsstatic int pim_assert;
311163953Srrs/*
312163953Srrs * Rate limit for assert notification messages, in usec
313163953Srrs */
314163953Srrs#define ASSERT_MSG_TIME		3000000
315163953Srrs
316168299Srrs/*
317163953Srrs * Hash function for a source, group entry
318163953Srrs */
319163953Srrs#define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
320163953Srrs			((g) >> 20) ^ ((g) >> 10) ^ (g))
321163953Srrs
322163953Srrs/*
323163953Srrs * Find a route for a given origin IP address and Multicast group address
324163953Srrs * Type of service parameter to be added in the future!!!
325163953Srrs */
326163953Srrs
327163953Srrs#define MFCFIND(o, g, rt) { \
328163953Srrs	register struct mbuf *_mb_rt = mfctable[MFCHASH(o,g)]; \
329163953Srrs	register struct mfc *_rt = NULL; \
330163953Srrs	rt = NULL; \
331163953Srrs	++mrtstat.mrts_mfc_lookups; \
332163953Srrs	while (_mb_rt) { \
333163953Srrs		_rt = mtod(_mb_rt, struct mfc *); \
334163953Srrs		if ((_rt->mfc_origin.s_addr == o) && \
335163953Srrs		    (_rt->mfc_mcastgrp.s_addr == g) && \
336163953Srrs		    (_mb_rt->m_act == NULL)) { \
337163953Srrs			rt = _rt; \
338163953Srrs			break; \
339163953Srrs		} \
340163953Srrs		_mb_rt = _mb_rt->m_next; \
341163953Srrs	} \
342163953Srrs	if (rt == NULL) { \
343163953Srrs		++mrtstat.mrts_mfc_misses; \
344165220Srrs	} \
345168299Srrs}
346163953Srrs
347163953Srrs
348163953Srrs/*
349163953Srrs * Macros to compute elapsed time efficiently
350163953Srrs * Borrowed from Van Jacobson's scheduling code
351163953Srrs */
352163953Srrs#define TV_DELTA(a, b, delta) { \
353163953Srrs	    register int xxs; \
354163953Srrs		\
355163953Srrs	    delta = (a).tv_usec - (b).tv_usec; \
356163953Srrs	    if ((xxs = (a).tv_sec - (b).tv_sec)) { \
357163953Srrs	       switch (xxs) { \
358163953Srrs		      case 2: \
359163953Srrs			  delta += 1000000; \
360163953Srrs			      /* fall through */ \
361163953Srrs		      case 1: \
362163953Srrs			  delta += 1000000; \
363163953Srrs			  break; \
364163953Srrs		      default: \
365163953Srrs			  delta += (1000000 * xxs); \
366163953Srrs	       } \
367163953Srrs	    } \
368168299Srrs}
369163953Srrs
370163953Srrs#define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
371163953Srrs	      (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
372163953Srrs
373163953Srrs#ifdef UPCALL_TIMING
374163953Srrsu_long upcall_data[51];
375163953Srrsstatic void collate(struct timeval *);
376163953Srrs#endif /* UPCALL_TIMING */
377163953Srrs
378163953Srrs
379163953Srrs/*
380163953Srrs * Handle MRT setsockopt commands to modify the multicast routing tables.
381163953Srrs */
382163953Srrsstatic int
383163953SrrsX_ip_mrouter_set(so, sopt)
384163953Srrs	struct socket *so;
385163953Srrs	struct sockopt *sopt;
386163953Srrs{
387163953Srrs	int	error, optval;
388163953Srrs	vifi_t	vifi;
389165220Srrs	struct	vifctl vifc;
390163953Srrs	struct	mfcctl mfc;
391165220Srrs
392165220Srrs	if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
393165220Srrs		return (EPERM);
394163953Srrs
395163953Srrs	error = 0;
396163953Srrs	switch (sopt->sopt_name) {
397163953Srrs	case MRT_INIT:
398163953Srrs		error = sooptcopyin(sopt, &optval, sizeof optval,
399163953Srrs				    sizeof optval);
400163953Srrs		if (error)
401163953Srrs			break;
402163953Srrs		error = ip_mrouter_init(so, optval);
403163953Srrs		break;
404163953Srrs
405163953Srrs	case MRT_DONE:
406163953Srrs		error = ip_mrouter_done();
407163953Srrs		break;
408163953Srrs
409163953Srrs	case MRT_ADD_VIF:
410163953Srrs		error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
411163953Srrs		if (error)
412163953Srrs			break;
413163953Srrs		error = add_vif(&vifc);
414165647Srrs		break;
415163953Srrs
416163953Srrs	case MRT_DEL_VIF:
417163953Srrs		error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
418163953Srrs		if (error)
419163953Srrs			break;
420163953Srrs		error = del_vif(vifi);
421163953Srrs		break;
422163953Srrs
423163953Srrs	case MRT_ADD_MFC:
424163953Srrs	case MRT_DEL_MFC:
425163953Srrs		error = sooptcopyin(sopt, &mfc, sizeof mfc, sizeof mfc);
426163953Srrs		if (error)
427168299Srrs			break;
428163953Srrs		if (sopt->sopt_name == MRT_ADD_MFC)
429163953Srrs			error = add_mfc(&mfc);
430163953Srrs		else
431163953Srrs			error = del_mfc(&mfc);
432163953Srrs
433163953Srrs	case MRT_ASSERT:
434163953Srrs		error = sooptcopyin(sopt, &optval, sizeof optval,
435163953Srrs				    sizeof optval);
436163953Srrs		if (error)
437163953Srrs			break;
438163953Srrs		set_assert(optval);
439163953Srrs
440163953Srrs	default:
441163953Srrs		error = EOPNOTSUPP;
442163953Srrs		break;
443163953Srrs	}
444163953Srrs	return (error);
445163953Srrs}
446163953Srrs
447163953Srrs#ifndef MROUTE_LKM
448163953Srrsint (*ip_mrouter_set)(struct socket *, struct sockopt *) = X_ip_mrouter_set;
449163953Srrs#endif
450163953Srrs
451163953Srrs/*
452163953Srrs * Handle MRT getsockopt commands
453163953Srrs */
454163953Srrsstatic int
455163953SrrsX_ip_mrouter_get(so, sopt)
456163953Srrs	struct socket *so;
457163953Srrs	struct sockopt *sopt;
458163953Srrs{
459163953Srrs	int error;
460163953Srrs	static int version = 0x0305; /* !!! why is this here? XXX */
461163953Srrs
462163953Srrs	switch (sopt->sopt_name) {
463163953Srrs	case MRT_VERSION:
464163953Srrs		error = sooptcopyout(sopt, &version, sizeof version);
465163953Srrs		break;
466163953Srrs
467163953Srrs	case MRT_ASSERT:
468163953Srrs		error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert);
469163953Srrs		break;
470163953Srrs	default:
471163953Srrs		error = EOPNOTSUPP;
472163953Srrs		break;
473163953Srrs	}
474163953Srrs	return (error);
475163953Srrs}
476163953Srrs
477163953Srrs#ifndef MROUTE_LKM
478163953Srrsint (*ip_mrouter_get)(struct socket *, struct sockopt *) = X_ip_mrouter_get;
479163953Srrs#endif
480163953Srrs
481163953Srrs/*
482163953Srrs * Handle ioctl commands to obtain information from the cache
483163953Srrs */
484163953Srrsstatic int
485163953SrrsX_mrt_ioctl(cmd, data)
486167598Srrs    int cmd;
487167598Srrs    caddr_t data;
488167598Srrs{
489167598Srrs    int error = 0;
490167598Srrs
491167598Srrs    switch (cmd) {
492167598Srrs	case (SIOCGETVIFCNT):
493167598Srrs	    return (get_vif_cnt((struct sioc_vif_req *)data));
494167598Srrs	    break;
495167598Srrs	case (SIOCGETSGCNT):
496167598Srrs	    return (get_sg_cnt((struct sioc_sg_req *)data));
497167598Srrs	    break;
498167598Srrs	default:
499167598Srrs	    return (EINVAL);
500167598Srrs	    break;
501167598Srrs    }
502163953Srrs    return error;
503163953Srrs}
504163953Srrs
505163953Srrs#ifndef MROUTE_LKM
506163953Srrsint (*mrt_ioctl)(int, caddr_t) = X_mrt_ioctl;
507163953Srrs#endif
508163953Srrs
509163953Srrs/*
510163953Srrs * returns the packet, byte, rpf-failure count for the source group provided
511163953Srrs */
512163953Srrsstatic int
513163953Srrsget_sg_cnt(req)
514163953Srrs    register struct sioc_sg_req *req;
515163953Srrs{
516163953Srrs    register struct mfc *rt;
517163953Srrs    int s;
518163953Srrs
519163953Srrs    s = splnet();
520163953Srrs    MFCFIND(req->src.s_addr, req->grp.s_addr, rt);
521163953Srrs    splx(s);
522163953Srrs    if (rt != NULL) {
523163953Srrs	req->pktcnt = rt->mfc_pkt_cnt;
524163953Srrs	req->bytecnt = rt->mfc_byte_cnt;
525163953Srrs	req->wrong_if = rt->mfc_wrong_if;
526163953Srrs    } else
527163953Srrs	req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
528163953Srrs
529163953Srrs    return 0;
530163953Srrs}
531163953Srrs
532163953Srrs/*
533163953Srrs * returns the input and output packet and byte counts on the vif provided
534163953Srrs */
535163953Srrsstatic int
536163953Srrsget_vif_cnt(req)
537163953Srrs    register struct sioc_vif_req *req;
538163953Srrs{
539165220Srrs    register vifi_t vifi = req->vifi;
540163953Srrs
541163953Srrs    if (vifi >= numvifs) return EINVAL;
542163953Srrs
543163953Srrs    req->icount = viftable[vifi].v_pkt_in;
544163953Srrs    req->ocount = viftable[vifi].v_pkt_out;
545163953Srrs    req->ibytes = viftable[vifi].v_bytes_in;
546163953Srrs    req->obytes = viftable[vifi].v_bytes_out;
547163953Srrs
548165220Srrs    return 0;
549163953Srrs}
550163953Srrs
551163953Srrs/*
552163953Srrs * Enable multicast routing
553163953Srrs */
554163953Srrsstatic int
555163953Srrsip_mrouter_init(so, version)
556163953Srrs	struct socket *so;
557163953Srrs	int version;
558163953Srrs{
559163953Srrs    if (mrtdebug)
560163953Srrs	log(LOG_DEBUG,"ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
561163953Srrs		so->so_type, so->so_proto->pr_protocol);
562163953Srrs
563163953Srrs    if (so->so_type != SOCK_RAW ||
564163953Srrs	so->so_proto->pr_protocol != IPPROTO_IGMP) return EOPNOTSUPP;
565163953Srrs
566163953Srrs    if (version != 1)
567163953Srrs	return ENOPROTOOPT;
568163953Srrs
569163953Srrs    if (ip_mrouter != NULL) return EADDRINUSE;
570165220Srrs
571165220Srrs    ip_mrouter = so;
572165220Srrs
573163953Srrs    bzero((caddr_t)mfctable, sizeof(mfctable));
574163953Srrs    bzero((caddr_t)nexpire, sizeof(nexpire));
575163953Srrs
576163953Srrs    pim_assert = 0;
577163953Srrs
578163953Srrs    expire_upcalls_ch = timeout(expire_upcalls, (caddr_t)NULL, EXPIRE_TIMEOUT);
579163953Srrs
580163953Srrs    if (mrtdebug)
581165220Srrs	log(LOG_DEBUG, "ip_mrouter_init\n");
582163953Srrs
583163953Srrs    return 0;
584163953Srrs}
585163953Srrs
586165220Srrs/*
587165220Srrs * Disable multicast routing
588165220Srrs */
589163953Srrsstatic int
590163996SrrsX_ip_mrouter_done()
591163953Srrs{
592163953Srrs    vifi_t vifi;
593163953Srrs    int i;
594163953Srrs    struct ifnet *ifp;
595163953Srrs    struct ifreq ifr;
596163953Srrs    struct mbuf *mb_rt;
597163953Srrs    struct mbuf *m;
598163953Srrs    struct rtdetq *rte;
599163953Srrs    int s;
600163953Srrs
601163953Srrs    s = splnet();
602163953Srrs
603163953Srrs    /*
604163953Srrs     * For each phyint in use, disable promiscuous reception of all IP
605163953Srrs     * multicasts.
606163953Srrs     */
607163953Srrs    for (vifi = 0; vifi < numvifs; vifi++) {
608163953Srrs	if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
609163953Srrs	    !(viftable[vifi].v_flags & VIFF_TUNNEL)) {
610163953Srrs	    ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET;
611163953Srrs	    ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr.s_addr
612165220Srrs								= INADDR_ANY;
613163953Srrs	    ifp = viftable[vifi].v_ifp;
614163953Srrs	    if_allmulti(ifp, 0);
615163953Srrs	}
616163953Srrs    }
617163953Srrs    bzero((caddr_t)tbftable, sizeof(tbftable));
618163953Srrs    bzero((caddr_t)viftable, sizeof(viftable));
619163953Srrs    numvifs = 0;
620163953Srrs    pim_assert = 0;
621163953Srrs
622163953Srrs    untimeout(expire_upcalls, (caddr_t)NULL, expire_upcalls_ch);
623163953Srrs
624163953Srrs    /*
625163953Srrs     * Free all multicast forwarding cache entries.
626163953Srrs     */
627163953Srrs    for (i = 0; i < MFCTBLSIZ; i++) {
628163953Srrs	mb_rt = mfctable[i];
629163953Srrs	while (mb_rt) {
630163953Srrs	    if (mb_rt->m_act != NULL) {
631163953Srrs		while (mb_rt->m_act) {
632163953Srrs		    m = mb_rt->m_act;
633163953Srrs		    mb_rt->m_act = m->m_act;
634163953Srrs		    rte = mtod(m, struct rtdetq *);
635163953Srrs		    m_freem(rte->m);
636163953Srrs		    m_free(m);
637166675Srrs		}
638166675Srrs	    }
639163953Srrs	    mb_rt = m_free(mb_rt);
640163953Srrs	}
641163953Srrs    }
642163953Srrs
643163953Srrs    bzero((caddr_t)mfctable, sizeof(mfctable));
644163953Srrs
645163953Srrs    /*
646163953Srrs     * Reset de-encapsulation cache
647163953Srrs     */
648163953Srrs    last_encap_src = 0;
649163953Srrs    last_encap_vif = NULL;
650163953Srrs    have_encap_tunnel = 0;
651163953Srrs
652163953Srrs    ip_mrouter = NULL;
653163953Srrs
654163953Srrs    splx(s);
655163953Srrs
656163953Srrs    if (mrtdebug)
657163953Srrs	log(LOG_DEBUG, "ip_mrouter_done\n");
658163953Srrs
659163953Srrs    return 0;
660163953Srrs}
661163953Srrs
662163953Srrs#ifndef MROUTE_LKM
663163953Srrsint (*ip_mrouter_done)(void) = X_ip_mrouter_done;
664163953Srrs#endif
665163953Srrs
666163953Srrs/*
667163953Srrs * Set PIM assert processing global
668163953Srrs */
669163953Srrsstatic int
670163953Srrsset_assert(i)
671165220Srrs	int i;
672163953Srrs{
673163953Srrs    if ((i != 1) && (i != 0))
674163953Srrs	return EINVAL;
675163953Srrs
676165220Srrs    pim_assert = i;
677165220Srrs
678165220Srrs    return 0;
679163953Srrs}
680163996Srrs
681163953Srrs/*
682163953Srrs * Add a vif to the vif table
683163953Srrs */
684163953Srrsstatic int
685163953Srrsadd_vif(vifcp)
686164205Srrs    register struct vifctl *vifcp;
687163953Srrs{
688163953Srrs    register struct vif *vifp = viftable + vifcp->vifc_vifi;
689165220Srrs    static struct sockaddr_in sin = {sizeof sin, AF_INET};
690163953Srrs    struct ifaddr *ifa;
691163953Srrs    struct ifnet *ifp;
692163953Srrs    int error, s;
693163953Srrs    struct tbf *v_tbf = tbftable + vifcp->vifc_vifi;
694163953Srrs
695163953Srrs    if (vifcp->vifc_vifi >= MAXVIFS)  return EINVAL;
696163953Srrs    if (vifp->v_lcl_addr.s_addr != 0) return EADDRINUSE;
697163953Srrs
698163953Srrs    /* Find the interface with an address in AF_INET family */
699163953Srrs    sin.sin_addr = vifcp->vifc_lcl_addr;
700163953Srrs    ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
701163953Srrs    if (ifa == 0) return EADDRNOTAVAIL;
702163953Srrs    ifp = ifa->ifa_ifp;
703165220Srrs
704165220Srrs    if (vifcp->vifc_flags & VIFF_TUNNEL) {
705163953Srrs	if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
706163953Srrs		/*
707163953Srrs		 * An encapsulating tunnel is wanted.  Tell ipip_input() to
708163953Srrs		 * start paying attention to encapsulated packets.
709163953Srrs		 */
710163953Srrs		if (have_encap_tunnel == 0) {
711163953Srrs			have_encap_tunnel = 1;
712163953Srrs			for (s = 0; s < MAXVIFS; ++s) {
713163953Srrs				multicast_decap_if[s].if_name = "mdecap";
714163953Srrs				multicast_decap_if[s].if_unit = s;
715163953Srrs			}
716163953Srrs		}
717163953Srrs		/*
718163953Srrs		 * Set interface to fake encapsulator interface
719163953Srrs		 */
720163953Srrs		ifp = &multicast_decap_if[vifcp->vifc_vifi];
721163953Srrs		/*
722163953Srrs		 * Prepare cached route entry
723163953Srrs		 */
724163953Srrs		bzero(&vifp->v_route, sizeof(vifp->v_route));
725163953Srrs	} else {
726163953Srrs	    log(LOG_ERR, "source routed tunnels not supported\n");
727163953Srrs	    return EOPNOTSUPP;
728163953Srrs	}
729163953Srrs    } else {
730163953Srrs	/* Make sure the interface supports multicast */
731163953Srrs	if ((ifp->if_flags & IFF_MULTICAST) == 0)
732163953Srrs	    return EOPNOTSUPP;
733163953Srrs
734163953Srrs	/* Enable promiscuous reception of all IP multicasts from the if */
735163953Srrs	s = splnet();
736163953Srrs	error = if_allmulti(ifp, 1);
737163953Srrs	splx(s);
738163953Srrs	if (error)
739163953Srrs	    return error;
740163953Srrs    }
741163953Srrs
742163953Srrs    s = splnet();
743163953Srrs    /* define parameters for the tbf structure */
744163953Srrs    vifp->v_tbf = v_tbf;
745163953Srrs    GET_TIME(vifp->v_tbf->tbf_last_pkt_t);
746163953Srrs    vifp->v_tbf->tbf_n_tok = 0;
747163953Srrs    vifp->v_tbf->tbf_q_len = 0;
748163953Srrs    vifp->v_tbf->tbf_max_q_len = MAXQSIZE;
749163953Srrs    vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL;
750163953Srrs
751163953Srrs    vifp->v_flags     = vifcp->vifc_flags;
752163953Srrs    vifp->v_threshold = vifcp->vifc_threshold;
753163953Srrs    vifp->v_lcl_addr  = vifcp->vifc_lcl_addr;
754163953Srrs    vifp->v_rmt_addr  = vifcp->vifc_rmt_addr;
755163953Srrs    vifp->v_ifp       = ifp;
756163953Srrs    /* scaling up here allows division by 1024 in critical code */
757163953Srrs    vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000;
758163953Srrs    vifp->v_rsvp_on   = 0;
759163953Srrs    vifp->v_rsvpd     = NULL;
760163953Srrs    /* initialize per vif pkt counters */
761163953Srrs    vifp->v_pkt_in    = 0;
762163953Srrs    vifp->v_pkt_out   = 0;
763163953Srrs    vifp->v_bytes_in  = 0;
764163953Srrs    vifp->v_bytes_out = 0;
765163953Srrs    splx(s);
766163953Srrs
767163953Srrs    /* Adjust numvifs up if the vifi is higher than numvifs */
768163953Srrs    if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
769163953Srrs
770163953Srrs    if (mrtdebug)
771163953Srrs	log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
772163953Srrs	    vifcp->vifc_vifi,
773163953Srrs	    (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
774163953Srrs	    (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
775163953Srrs	    (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
776163953Srrs	    vifcp->vifc_threshold,
777163953Srrs	    vifcp->vifc_rate_limit);
778163953Srrs
779163953Srrs    return 0;
780163953Srrs}
781163953Srrs
782163953Srrs/*
783163953Srrs * Delete a vif from the vif table
784163953Srrs */
785163953Srrsstatic int
786163953Srrsdel_vif(vifi)
787163953Srrs	vifi_t vifi;
788163953Srrs{
789163953Srrs    register struct vif *vifp = &viftable[vifi];
790163953Srrs    register struct mbuf *m;
791163953Srrs    struct ifnet *ifp;
792163953Srrs    struct ifreq ifr;
793163953Srrs    int s;
794163953Srrs
795163953Srrs    if (vifi >= numvifs) return EINVAL;
796163953Srrs    if (vifp->v_lcl_addr.s_addr == 0) return EADDRNOTAVAIL;
797163953Srrs
798163953Srrs    s = splnet();
799163953Srrs
800163953Srrs    if (!(vifp->v_flags & VIFF_TUNNEL)) {
801163953Srrs	((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET;
802163953Srrs	((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr.s_addr = INADDR_ANY;
803163953Srrs	ifp = vifp->v_ifp;
804163953Srrs	if_allmulti(ifp, 0);
805163953Srrs    }
806163953Srrs
807163953Srrs    if (vifp == last_encap_vif) {
808163953Srrs	last_encap_vif = 0;
809163953Srrs	last_encap_src = 0;
810163953Srrs    }
811163953Srrs
812163953Srrs    /*
813163953Srrs     * Free packets queued at the interface
814163953Srrs     */
815163953Srrs    while (vifp->v_tbf->tbf_q) {
816163953Srrs	m = vifp->v_tbf->tbf_q;
817163953Srrs	vifp->v_tbf->tbf_q = m->m_act;
818163953Srrs	m_freem(m);
819163953Srrs    }
820163953Srrs
821163953Srrs    bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf)));
822163953Srrs    bzero((caddr_t)vifp, sizeof (*vifp));
823163953Srrs
824163953Srrs    if (mrtdebug)
825163953Srrs      log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
826163953Srrs
827163953Srrs    /* Adjust numvifs down */
828163953Srrs    for (vifi = numvifs; vifi > 0; vifi--)
829163953Srrs	if (viftable[vifi-1].v_lcl_addr.s_addr != 0) break;
830163953Srrs    numvifs = vifi;
831163953Srrs
832163953Srrs    splx(s);
833163953Srrs
834163953Srrs    return 0;
835163953Srrs}
836163953Srrs
837163953Srrs/*
838163953Srrs * Add an mfc entry
839163953Srrs */
840163953Srrsstatic int
841163953Srrsadd_mfc(mfccp)
842163953Srrs    struct mfcctl *mfccp;
843163953Srrs{
844163953Srrs    struct mfc *rt;
845163953Srrs    register struct mbuf *mb_rt;
846163953Srrs    u_long hash;
847165220Srrs    struct mbuf *mb_ntry;
848163953Srrs    struct rtdetq *rte;
849163953Srrs    register u_short nstl;
850163953Srrs    int s;
851163953Srrs    int i;
852163953Srrs
853163953Srrs    MFCFIND(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr, rt);
854163953Srrs
855163953Srrs    /* If an entry already exists, just update the fields */
856163953Srrs    if (rt) {
857163953Srrs	if (mrtdebug & DEBUG_MFC)
858163953Srrs	    log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
859163953Srrs		(u_long)ntohl(mfccp->mfcc_origin.s_addr),
860163953Srrs		(u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
861163953Srrs		mfccp->mfcc_parent);
862163953Srrs
863163953Srrs	s = splnet();
864163953Srrs	rt->mfc_parent = mfccp->mfcc_parent;
865163953Srrs	for (i = 0; i < numvifs; i++)
866163953Srrs	    rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
867163953Srrs	splx(s);
868163953Srrs	return 0;
869163953Srrs    }
870163953Srrs
871163953Srrs    /*
872163953Srrs     * Find the entry for which the upcall was made and update
873163953Srrs     */
874163953Srrs    s = splnet();
875163953Srrs    hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
876163953Srrs    for (mb_rt = mfctable[hash], nstl = 0; mb_rt; mb_rt = mb_rt->m_next) {
877163953Srrs
878163953Srrs	rt = mtod(mb_rt, struct mfc *);
879163953Srrs	if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
880163953Srrs	    (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
881163953Srrs	    (mb_rt->m_act != NULL)) {
882163953Srrs
883163953Srrs	    if (nstl++)
884163953Srrs		log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
885163953Srrs		    "multiple kernel entries",
886163953Srrs		    (u_long)ntohl(mfccp->mfcc_origin.s_addr),
887163953Srrs		    (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
888163953Srrs		    mfccp->mfcc_parent, (void *)mb_rt->m_act);
889163953Srrs
890166086Srrs	    if (mrtdebug & DEBUG_MFC)
891163953Srrs		log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
892163953Srrs		    (u_long)ntohl(mfccp->mfcc_origin.s_addr),
893163953Srrs		    (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
894163953Srrs		    mfccp->mfcc_parent, (void *)mb_rt->m_act);
895163953Srrs
896163953Srrs	    rt->mfc_origin     = mfccp->mfcc_origin;
897163953Srrs	    rt->mfc_mcastgrp   = mfccp->mfcc_mcastgrp;
898163953Srrs	    rt->mfc_parent     = mfccp->mfcc_parent;
899163953Srrs	    for (i = 0; i < numvifs; i++)
900163953Srrs		rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
901163953Srrs	    /* initialize pkt counters per src-grp */
902163953Srrs	    rt->mfc_pkt_cnt    = 0;
903166086Srrs	    rt->mfc_byte_cnt   = 0;
904163953Srrs	    rt->mfc_wrong_if   = 0;
905163953Srrs	    rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
906163953Srrs
907163953Srrs	    rt->mfc_expire = 0;	/* Don't clean this guy up */
908163953Srrs	    nexpire[hash]--;
909163953Srrs
910163953Srrs	    /* free packets Qed at the end of this entry */
911163953Srrs	    while (mb_rt->m_act) {
912163953Srrs		mb_ntry = mb_rt->m_act;
913163953Srrs		rte = mtod(mb_ntry, struct rtdetq *);
914163953Srrs/* #ifdef RSVP_ISI */
915163953Srrs		ip_mdq(rte->m, rte->ifp, rt, -1);
916163953Srrs/* #endif */
917163953Srrs		mb_rt->m_act = mb_ntry->m_act;
918163953Srrs		m_freem(rte->m);
919163953Srrs#ifdef UPCALL_TIMING
920163953Srrs		collate(&(rte->t));
921163953Srrs#endif /* UPCALL_TIMING */
922163953Srrs		m_free(mb_ntry);
923163953Srrs	    }
924163953Srrs	}
925163953Srrs    }
926163953Srrs
927163953Srrs    /*
928168299Srrs     * It is possible that an entry is being inserted without an upcall
929163953Srrs     */
930163953Srrs    if (nstl == 0) {
931163953Srrs	if (mrtdebug & DEBUG_MFC)
932163953Srrs	    log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
933163953Srrs		hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
934163953Srrs		(u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
935163953Srrs		mfccp->mfcc_parent);
936163953Srrs
937163953Srrs	for (mb_rt = mfctable[hash]; mb_rt; mb_rt = mb_rt->m_next) {
938163953Srrs
939163953Srrs	    rt = mtod(mb_rt, struct mfc *);
940163953Srrs	    if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
941163953Srrs		(rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
942163953Srrs
943163953Srrs		rt->mfc_origin     = mfccp->mfcc_origin;
944163953Srrs		rt->mfc_mcastgrp   = mfccp->mfcc_mcastgrp;
945163953Srrs		rt->mfc_parent     = mfccp->mfcc_parent;
946163953Srrs		for (i = 0; i < numvifs; i++)
947163953Srrs		    rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
948163953Srrs		/* initialize pkt counters per src-grp */
949163953Srrs		rt->mfc_pkt_cnt    = 0;
950163953Srrs		rt->mfc_byte_cnt   = 0;
951163953Srrs		rt->mfc_wrong_if   = 0;
952168299Srrs		rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
953163953Srrs		if (rt->mfc_expire)
954163953Srrs		    nexpire[hash]--;
955163953Srrs		rt->mfc_expire	   = 0;
956163953Srrs	    }
957163953Srrs	}
958163953Srrs	if (mb_rt == NULL) {
959163953Srrs	    /* no upcall, so make a new entry */
960163953Srrs	    MGET(mb_rt, M_DONTWAIT, MT_MRTABLE);
961163953Srrs	    if (mb_rt == NULL) {
962168299Srrs		splx(s);
963163953Srrs		return ENOBUFS;
964163953Srrs	    }
965163953Srrs
966163953Srrs	    rt = mtod(mb_rt, struct mfc *);
967163953Srrs
968163953Srrs	    /* insert new entry at head of hash chain */
969163953Srrs	    rt->mfc_origin     = mfccp->mfcc_origin;
970168299Srrs	    rt->mfc_mcastgrp   = mfccp->mfcc_mcastgrp;
971163953Srrs	    rt->mfc_parent     = mfccp->mfcc_parent;
972163953Srrs	    for (i = 0; i < numvifs; i++)
973163953Srrs		    rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
974163953Srrs	    /* initialize pkt counters per src-grp */
975163953Srrs	    rt->mfc_pkt_cnt    = 0;
976163953Srrs	    rt->mfc_byte_cnt   = 0;
977163953Srrs	    rt->mfc_wrong_if   = 0;
978168299Srrs	    rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
979163953Srrs	    rt->mfc_expire     = 0;
980163953Srrs
981163953Srrs	    /* link into table */
982163953Srrs	    mb_rt->m_next  = mfctable[hash];
983163953Srrs	    mfctable[hash] = mb_rt;
984163953Srrs	    mb_rt->m_act = NULL;
985163953Srrs	}
986168299Srrs    }
987163953Srrs    splx(s);
988163953Srrs    return 0;
989163953Srrs}
990163953Srrs
991163953Srrs#ifdef UPCALL_TIMING
992163953Srrs/*
993163953Srrs * collect delay statistics on the upcalls
994163953Srrs */
995163953Srrsstatic void collate(t)
996163953Srrsregister struct timeval *t;
997163953Srrs{
998163953Srrs    register u_long d;
999163953Srrs    register struct timeval tp;
1000163953Srrs    register u_long delta;
1001163953Srrs
1002163953Srrs    GET_TIME(tp);
1003163953Srrs
1004163953Srrs    if (TV_LT(*t, tp))
1005163953Srrs    {
1006163953Srrs	TV_DELTA(tp, *t, delta);
1007163953Srrs
1008168299Srrs	d = delta >> 10;
1009168299Srrs	if (d > 50)
1010163953Srrs	    d = 50;
1011163953Srrs
1012163953Srrs	++upcall_data[d];
1013163953Srrs    }
1014163953Srrs}
1015163953Srrs#endif /* UPCALL_TIMING */
1016163953Srrs
1017163953Srrs/*
1018163953Srrs * Delete an mfc entry
1019163953Srrs */
1020163953Srrsstatic int
1021163953Srrsdel_mfc(mfccp)
1022163953Srrs    struct mfcctl *mfccp;
1023163953Srrs{
1024163953Srrs    struct in_addr 	origin;
1025163953Srrs    struct in_addr 	mcastgrp;
1026163953Srrs    struct mfc 		*rt;
1027163953Srrs    struct mbuf 	*mb_rt;
1028163953Srrs    struct mbuf 	**nptr;
1029163953Srrs    u_long 		hash;
1030163953Srrs    int s;
1031165220Srrs
1032163953Srrs    origin = mfccp->mfcc_origin;
1033163953Srrs    mcastgrp = mfccp->mfcc_mcastgrp;
1034163953Srrs    hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1035163953Srrs
1036163953Srrs    if (mrtdebug & DEBUG_MFC)
1037163953Srrs	log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1038163953Srrs	    (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1039163953Srrs
1040163953Srrs    s = splnet();
1041163953Srrs
1042163953Srrs    nptr = &mfctable[hash];
1043163953Srrs    while ((mb_rt = *nptr) != NULL) {
1044163953Srrs        rt = mtod(mb_rt, struct mfc *);
1045163953Srrs	if (origin.s_addr == rt->mfc_origin.s_addr &&
1046163953Srrs	    mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1047163953Srrs	    mb_rt->m_act == NULL)
1048163953Srrs	    break;
1049163953Srrs
1050163953Srrs	nptr = &mb_rt->m_next;
1051163953Srrs    }
1052163953Srrs    if (mb_rt == NULL) {
1053163953Srrs	splx(s);
1054163953Srrs	return EADDRNOTAVAIL;
1055163953Srrs    }
1056163953Srrs
1057163953Srrs    MFREE(mb_rt, *nptr);
1058163953Srrs
1059163953Srrs    splx(s);
1060163953Srrs
1061163953Srrs    return 0;
1062163953Srrs}
1063163953Srrs
1064163953Srrs/*
1065163953Srrs * Send a message to mrouted on the multicast routing socket
1066163953Srrs */
1067163953Srrsstatic int
1068163953Srrssocket_send(s, mm, src)
1069163953Srrs	struct socket *s;
1070163953Srrs	struct mbuf *mm;
1071163953Srrs	struct sockaddr_in *src;
1072163953Srrs{
1073163953Srrs	if (s) {
1074163953Srrs		if (sbappendaddr(&s->so_rcv,
1075163953Srrs				 (struct sockaddr *)src,
1076163953Srrs				 mm, (struct mbuf *)0) != 0) {
1077163953Srrs			sorwakeup(s);
1078163953Srrs			return 0;
1079163953Srrs		}
1080163953Srrs	}
1081165220Srrs	m_freem(mm);
1082163953Srrs	return -1;
1083164205Srrs}
1084166023Srrs
1085163953Srrs/*
1086163953Srrs * IP multicast forwarding function. This function assumes that the packet
1087163953Srrs * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1088166675Srrs * pointed to by "ifp", and the packet is to be relayed to other networks
1089166023Srrs * that have members of the packet's destination IP multicast group.
1090166023Srrs *
1091166023Srrs * The packet is returned unscathed to the caller, unless it is
1092166023Srrs * erroneous, in which case a non-zero return value tells the caller to
1093166023Srrs * discard it.
1094166023Srrs */
1095163953Srrs
1096163953Srrs#define IP_HDR_LEN  20	/* # bytes of fixed IP header (excluding options) */
1097163953Srrs#define TUNNEL_LEN  12  /* # bytes of IP option for tunnel encapsulation  */
1098163953Srrs
1099163953Srrsstatic int
1100163953SrrsX_ip_mforward(ip, ifp, m, imo)
1101163953Srrs    register struct ip *ip;
1102165647Srrs    struct ifnet *ifp;
1103163953Srrs    struct mbuf *m;
1104163953Srrs    struct ip_moptions *imo;
1105163953Srrs{
1106163953Srrs    register struct mfc *rt;
1107163953Srrs    register u_char *ipoptions;
1108165647Srrs    static struct sockaddr_in 	k_igmpsrc	= { sizeof k_igmpsrc, AF_INET };
1109165647Srrs    static int srctun = 0;
1110165647Srrs    register struct mbuf *mm;
1111163953Srrs    int s;
1112165647Srrs    vifi_t vifi;
1113163953Srrs    struct vif *vifp;
1114163953Srrs
1115163953Srrs    if (mrtdebug & DEBUG_FORWARD)
1116163953Srrs	log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1117166023Srrs	    (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1118166023Srrs	    (void *)ifp);
1119163953Srrs
1120163953Srrs    if (ip->ip_hl < (IP_HDR_LEN + TUNNEL_LEN) >> 2 ||
1121163953Srrs	(ipoptions = (u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1122163953Srrs	/*
1123163953Srrs	 * Packet arrived via a physical interface or
1124163953Srrs	 * an encapsulated tunnel.
1125163953Srrs	 */
1126163953Srrs    } else {
1127163953Srrs	/*
1128163953Srrs	 * Packet arrived through a source-route tunnel.
1129163953Srrs	 * Source-route tunnels are no longer supported.
1130163953Srrs	 */
1131163953Srrs	if ((srctun++ % 1000) == 0)
1132163953Srrs	    log(LOG_ERR,
1133163953Srrs		"ip_mforward: received source-routed packet from %lx\n",
1134163953Srrs		(u_long)ntohl(ip->ip_src.s_addr));
1135163953Srrs
1136163953Srrs	return 1;
1137163953Srrs    }
1138163953Srrs
1139163953Srrs    if ((imo) && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1140163953Srrs	if (ip->ip_ttl < 255)
1141163953Srrs		ip->ip_ttl++;	/* compensate for -1 in *_send routines */
1142163953Srrs	if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1143163953Srrs	    vifp = viftable + vifi;
1144163953Srrs	    printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s%d)\n",
1145163953Srrs		ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr), vifi,
1146163953Srrs		(vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1147163953Srrs		vifp->v_ifp->if_name, vifp->v_ifp->if_unit);
1148163953Srrs	}
1149163953Srrs	return (ip_mdq(m, ifp, NULL, vifi));
1150163953Srrs    }
1151163953Srrs    if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1152163953Srrs	printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1153163953Srrs	    ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr));
1154163953Srrs	if(!imo)
1155163953Srrs		printf("In fact, no options were specified at all\n");
1156163953Srrs    }
1157163953Srrs
1158163953Srrs    /*
1159163953Srrs     * Don't forward a packet with time-to-live of zero or one,
1160163953Srrs     * or a packet destined to a local-only group.
1161166023Srrs     */
1162166086Srrs    if (ip->ip_ttl <= 1 ||
1163166023Srrs	ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP)
1164166086Srrs	return 0;
1165166086Srrs
1166166086Srrs    /*
1167166086Srrs     * Determine forwarding vifs from the forwarding cache table
1168166023Srrs     */
1169163953Srrs    s = splnet();
1170163953Srrs    MFCFIND(ip->ip_src.s_addr, ip->ip_dst.s_addr, rt);
1171163953Srrs
1172166023Srrs    /* Entry exists, so forward if necessary */
1173163953Srrs    if (rt != NULL) {
1174163953Srrs	splx(s);
1175166023Srrs	return (ip_mdq(m, ifp, rt, -1));
1176166023Srrs    } else {
1177166023Srrs	/*
1178163953Srrs	 * If we don't have a route for packet's origin,
1179163953Srrs	 * Make a copy of the packet &
1180163953Srrs	 * send message to routing daemon
1181163953Srrs	 */
1182166023Srrs
1183166023Srrs	register struct mbuf *mb_rt;
1184163953Srrs	register struct mbuf *mb_ntry;
1185163953Srrs	register struct mbuf *mb0;
1186163953Srrs	register struct rtdetq *rte;
1187163953Srrs	register struct mbuf *rte_m;
1188165220Srrs	register u_long hash;
1189165220Srrs	register int npkts;
1190163953Srrs	int hlen = ip->ip_hl << 2;
1191166675Srrs#ifdef UPCALL_TIMING
1192166675Srrs	struct timeval tp;
1193166675Srrs
1194166675Srrs	GET_TIME(tp);
1195163953Srrs#endif
1196166675Srrs
1197163953Srrs	mrtstat.mrts_no_route++;
1198163953Srrs	if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1199163953Srrs	    log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1200166675Srrs		(u_long)ntohl(ip->ip_src.s_addr),
1201163953Srrs		(u_long)ntohl(ip->ip_dst.s_addr));
1202163953Srrs
1203163953Srrs	/*
1204166675Srrs	 * Allocate mbufs early so that we don't do extra work if we are
1205165220Srrs	 * just going to fail anyway.  Make sure to pullup the header so
1206163953Srrs	 * that other people can't step on it.
1207163953Srrs	 */
1208163953Srrs	MGET(mb_ntry, M_DONTWAIT, MT_DATA);
1209163953Srrs	if (mb_ntry == NULL) {
1210163953Srrs	    splx(s);
1211163953Srrs	    return ENOBUFS;
1212163953Srrs	}
1213163953Srrs	mb0 = m_copy(m, 0, M_COPYALL);
1214163953Srrs	if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1215163953Srrs	    mb0 = m_pullup(mb0, hlen);
1216163953Srrs	if (mb0 == NULL) {
1217163953Srrs	    m_free(mb_ntry);
1218163953Srrs	    splx(s);
1219163953Srrs	    return ENOBUFS;
1220163953Srrs	}
1221163953Srrs
1222163953Srrs	/* is there an upcall waiting for this packet? */
1223163953Srrs	hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1224163953Srrs	for (mb_rt = mfctable[hash]; mb_rt; mb_rt = mb_rt->m_next) {
1225163953Srrs	    rt = mtod(mb_rt, struct mfc *);
1226163953Srrs	    if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1227163953Srrs		(ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1228163953Srrs		(mb_rt->m_act != NULL))
1229163953Srrs		break;
1230163953Srrs	}
1231163953Srrs
1232163953Srrs	if (mb_rt == NULL) {
1233163953Srrs	    int i;
1234163953Srrs	    struct igmpmsg *im;
1235163953Srrs
1236163953Srrs	    /* no upcall, so make a new entry */
1237163953Srrs	    MGET(mb_rt, M_DONTWAIT, MT_MRTABLE);
1238163953Srrs	    if (mb_rt == NULL) {
1239166675Srrs		m_free(mb_ntry);
1240163953Srrs		m_freem(mb0);
1241163953Srrs		splx(s);
1242165220Srrs		return ENOBUFS;
1243163953Srrs	    }
1244163953Srrs	    /* Make a copy of the header to send to the user level process */
1245163953Srrs	    mm = m_copy(mb0, 0, hlen);
1246163953Srrs	    if (mm == NULL) {
1247163953Srrs		m_free(mb_ntry);
1248163953Srrs		m_freem(mb0);
1249163953Srrs		m_free(mb_rt);
1250163953Srrs		splx(s);
1251166023Srrs		return ENOBUFS;
1252166023Srrs	    }
1253163953Srrs
1254163953Srrs	    /*
1255163953Srrs	     * Send message to routing daemon to install
1256165220Srrs	     * a route into the kernel table
1257163953Srrs	     */
1258166023Srrs	    k_igmpsrc.sin_addr = ip->ip_src;
1259166023Srrs
1260163953Srrs	    im = mtod(mm, struct igmpmsg *);
1261163953Srrs	    im->im_msgtype	= IGMPMSG_NOCACHE;
1262163953Srrs	    im->im_mbz		= 0;
1263163953Srrs
1264163953Srrs	    mrtstat.mrts_upcalls++;
1265163953Srrs
1266163953Srrs	    if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1267163953Srrs		log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1268163953Srrs		++mrtstat.mrts_upq_sockfull;
1269166023Srrs		m_free(mb_ntry);
1270166023Srrs		m_freem(mb0);
1271163953Srrs		m_free(mb_rt);
1272163953Srrs		splx(s);
1273163953Srrs		return ENOBUFS;
1274163953Srrs	    }
1275163953Srrs
1276163953Srrs	    rt = mtod(mb_rt, struct mfc *);
1277163953Srrs
1278163953Srrs	    /* insert new entry at head of hash chain */
1279163953Srrs	    rt->mfc_origin.s_addr     = ip->ip_src.s_addr;
1280166023Srrs	    rt->mfc_mcastgrp.s_addr   = ip->ip_dst.s_addr;
1281166023Srrs	    rt->mfc_expire	      = UPCALL_EXPIRE;
1282166023Srrs	    nexpire[hash]++;
1283166023Srrs	    for (i = 0; i < numvifs; i++)
1284166023Srrs		rt->mfc_ttls[i] = 0;
1285166023Srrs	    rt->mfc_parent = -1;
1286166023Srrs
1287166023Srrs	    /* link into table */
1288166023Srrs	    mb_rt->m_next  = mfctable[hash];
1289166023Srrs	    mfctable[hash] = mb_rt;
1290166023Srrs	    mb_rt->m_act = NULL;
1291166023Srrs
1292166023Srrs	    rte_m = mb_rt;
1293166023Srrs	} else {
1294166023Srrs	    /* determine if q has overflowed */
1295166023Srrs	    for (rte_m = mb_rt, npkts = 0; rte_m->m_act; rte_m = rte_m->m_act)
1296166023Srrs		npkts++;
1297166023Srrs
1298166023Srrs	    if (npkts > MAX_UPQ) {
1299166023Srrs		mrtstat.mrts_upq_ovflw++;
1300166023Srrs		m_free(mb_ntry);
1301166023Srrs		m_freem(mb0);
1302166023Srrs		splx(s);
1303166023Srrs		return 0;
1304166023Srrs	    }
1305165220Srrs	}
1306163953Srrs
1307163953Srrs	mb_ntry->m_act = NULL;
1308163953Srrs	rte = mtod(mb_ntry, struct rtdetq *);
1309163953Srrs
1310163953Srrs	rte->m 			= mb0;
1311163953Srrs	rte->ifp 		= ifp;
1312163953Srrs#ifdef UPCALL_TIMING
1313163953Srrs	rte->t			= tp;
1314163953Srrs#endif
1315163953Srrs
1316163953Srrs	/* Add this entry to the end of the queue */
1317163953Srrs	rte_m->m_act		= mb_ntry;
1318164205Srrs
1319164205Srrs	splx(s);
1320164205Srrs
1321163953Srrs	return 0;
1322164205Srrs    }
1323164205Srrs}
1324164205Srrs
1325164205Srrs#ifndef MROUTE_LKM
1326164205Srrsint (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
1327164205Srrs		   struct ip_moptions *) = X_ip_mforward;
1328164205Srrs#endif
1329164205Srrs
1330164205Srrs/*
1331164205Srrs * Clean up the cache entry if upcall is not serviced
1332163953Srrs */
1333164205Srrsstatic void
1334164205Srrsexpire_upcalls(void *unused)
1335164205Srrs{
1336168124Srrs    struct mbuf *mb_rt, *m, **nptr;
1337164205Srrs    struct rtdetq *rte;
1338164205Srrs    struct mfc *mfc;
1339164205Srrs    int i;
1340164205Srrs    int s;
1341164205Srrs
1342163953Srrs    s = splnet();
1343163953Srrs    for (i = 0; i < MFCTBLSIZ; i++) {
1344163953Srrs	if (nexpire[i] == 0)
1345166023Srrs	    continue;
1346166023Srrs	nptr = &mfctable[i];
1347163953Srrs	for (mb_rt = *nptr; mb_rt != NULL; mb_rt = *nptr) {
1348163953Srrs	    mfc = mtod(mb_rt, struct mfc *);
1349163953Srrs
1350163953Srrs	    /*
1351163953Srrs	     * Skip real cache entries
1352166023Srrs	     * Make sure it wasn't marked to not expire (shouldn't happen)
1353166023Srrs	     * If it expires now
1354163953Srrs	     */
1355163953Srrs	    if (mb_rt->m_act != NULL &&
1356163953Srrs	        mfc->mfc_expire != 0 &&
1357163953Srrs		--mfc->mfc_expire == 0) {
1358163953Srrs		if (mrtdebug & DEBUG_EXPIRE)
1359163953Srrs		    log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1360163953Srrs			(u_long)ntohl(mfc->mfc_origin.s_addr),
1361163953Srrs			(u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1362163953Srrs		/*
1363163953Srrs		 * drop all the packets
1364163953Srrs		 * free the mbuf with the pkt, if, timing info
1365163953Srrs		 */
1366163953Srrs		while (mb_rt->m_act) {
1367166675Srrs		    m = mb_rt->m_act;
1368166675Srrs		    mb_rt->m_act = m->m_act;
1369166675Srrs
1370166675Srrs		    rte = mtod(m, struct rtdetq *);
1371166675Srrs		    m_freem(rte->m);
1372166675Srrs		    m_free(m);
1373166675Srrs		}
1374166675Srrs		++mrtstat.mrts_cache_cleanups;
1375166675Srrs		nexpire[i]--;
1376166675Srrs
1377163953Srrs		MFREE(mb_rt, *nptr);
1378163953Srrs	    } else {
1379166675Srrs		nptr = &mb_rt->m_next;
1380163953Srrs	    }
1381163953Srrs	}
1382163953Srrs    }
1383163953Srrs    splx(s);
1384163953Srrs    expire_upcalls_ch = timeout(expire_upcalls, (caddr_t)NULL, EXPIRE_TIMEOUT);
1385163953Srrs}
1386163953Srrs
1387165220Srrs/*
1388163953Srrs * Packet forwarding routine once entry in the cache is made
1389164205Srrs */
1390164205Srrsstatic int
1391164205Srrsip_mdq(m, ifp, rt, xmt_vif)
1392164205Srrs    register struct mbuf *m;
1393164205Srrs    register struct ifnet *ifp;
1394164205Srrs    register struct mfc *rt;
1395164205Srrs    register vifi_t xmt_vif;
1396164205Srrs{
1397164205Srrs    register struct ip  *ip = mtod(m, struct ip *);
1398164205Srrs    register vifi_t vifi;
1399166023Srrs    register struct vif *vifp;
1400166023Srrs    register int plen = ip->ip_len;
1401166023Srrs
1402163953Srrs/*
1403163953Srrs * Macro to send packet on vif.  Since RSVP packets don't get counted on
1404163953Srrs * input, they shouldn't get counted on output, so statistics keeping is
1405163953Srrs * seperate.
1406163953Srrs */
1407163953Srrs#define MC_SEND(ip,vifp,m) {                             \
1408163953Srrs                if ((vifp)->v_flags & VIFF_TUNNEL)  	 \
1409163953Srrs                    encap_send((ip), (vifp), (m));       \
1410163953Srrs                else                                     \
1411163953Srrs                    phyint_send((ip), (vifp), (m));      \
1412163953Srrs}
1413163953Srrs
1414165220Srrs    /*
1415166023Srrs     * If xmt_vif is not -1, send on only the requested vif.
1416166023Srrs     *
1417165220Srrs     * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1418165220Srrs     */
1419165647Srrs    if (xmt_vif < numvifs) {
1420163953Srrs	MC_SEND(ip, viftable + xmt_vif, m);
1421163953Srrs	return 1;
1422163953Srrs    }
1423165220Srrs
1424166675Srrs    /*
1425166675Srrs     * Don't forward if it didn't arrive from the parent vif for its origin.
1426166675Srrs     */
1427166675Srrs    vifi = rt->mfc_parent;
1428166675Srrs    if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1429166675Srrs	/* came in the wrong interface */
1430166675Srrs	if (mrtdebug & DEBUG_FORWARD)
1431166675Srrs	    log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1432166675Srrs		(void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1433166675Srrs	++mrtstat.mrts_wrong_if;
1434165647Srrs	++rt->mfc_wrong_if;
1435165647Srrs	/*
1436165647Srrs	 * If we are doing PIM assert processing, and we are forwarding
1437165647Srrs	 * packets on this interface, and it is a broadcast medium
1438165647Srrs	 * interface (and not a tunnel), send a message to the routing daemon.
1439165647Srrs	 */
1440165647Srrs	if (pim_assert && rt->mfc_ttls[vifi] &&
1441165647Srrs		(ifp->if_flags & IFF_BROADCAST) &&
1442165647Srrs		!(viftable[vifi].v_flags & VIFF_TUNNEL)) {
1443165647Srrs	    struct sockaddr_in k_igmpsrc;
1444165647Srrs	    struct mbuf *mm;
1445165647Srrs	    struct igmpmsg *im;
1446165647Srrs	    int hlen = ip->ip_hl << 2;
1447165647Srrs	    struct timeval now;
1448165647Srrs	    register u_long delta;
1449165647Srrs
1450165647Srrs	    GET_TIME(now);
1451165647Srrs
1452165647Srrs	    TV_DELTA(rt->mfc_last_assert, now, delta);
1453165647Srrs
1454165647Srrs	    if (delta > ASSERT_MSG_TIME) {
1455165647Srrs		mm = m_copy(m, 0, hlen);
1456165647Srrs		if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1457165647Srrs		    mm = m_pullup(mm, hlen);
1458165220Srrs		if (mm == NULL) {
1459165220Srrs		    return ENOBUFS;
1460165220Srrs		}
1461165220Srrs
1462165220Srrs		rt->mfc_last_assert = now;
1463163953Srrs
1464164205Srrs		im = mtod(mm, struct igmpmsg *);
1465165220Srrs		im->im_msgtype	= IGMPMSG_WRONGVIF;
1466164205Srrs		im->im_mbz		= 0;
1467165220Srrs		im->im_vif		= vifi;
1468165220Srrs
1469165220Srrs		k_igmpsrc.sin_addr = im->im_src;
1470165220Srrs
1471165220Srrs		socket_send(ip_mrouter, mm, &k_igmpsrc);
1472163953Srrs	    }
1473163953Srrs	}
1474163953Srrs	return 0;
1475163953Srrs    }
1476163953Srrs
1477163953Srrs    /* If I sourced this packet, it counts as output, else it was input. */
1478163953Srrs    if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1479163953Srrs	viftable[vifi].v_pkt_out++;
1480163953Srrs	viftable[vifi].v_bytes_out += plen;
1481163953Srrs    } else {
1482163953Srrs	viftable[vifi].v_pkt_in++;
1483163953Srrs	viftable[vifi].v_bytes_in += plen;
1484163953Srrs    }
1485163953Srrs    rt->mfc_pkt_cnt++;
1486163953Srrs    rt->mfc_byte_cnt += plen;
1487163953Srrs
1488163953Srrs    /*
1489163953Srrs     * For each vif, decide if a copy of the packet should be forwarded.
1490163953Srrs     * Forward if:
1491163953Srrs     *		- the ttl exceeds the vif's threshold
1492163953Srrs     *		- there are group members downstream on interface
1493163953Srrs     */
1494163953Srrs    for (vifp = viftable, vifi = 0; vifi < numvifs; vifp++, vifi++)
1495163953Srrs	if ((rt->mfc_ttls[vifi] > 0) &&
1496164205Srrs	    (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1497165220Srrs	    vifp->v_pkt_out++;
1498165220Srrs	    vifp->v_bytes_out += plen;
1499165220Srrs	    MC_SEND(ip, vifp, m);
1500163953Srrs	}
1501163953Srrs
1502166023Srrs    return 0;
1503166023Srrs}
1504166023Srrs
1505163953Srrs/*
1506163953Srrs * check if a vif number is legal/ok. This is used by ip_output, to export
1507163953Srrs * numvifs there,
1508163953Srrs */
1509163953Srrsstatic int
1510163953SrrsX_legal_vif_num(vif)
1511163953Srrs    int vif;
1512163953Srrs{
1513163953Srrs    if (vif >= 0 && vif < numvifs)
1514163953Srrs       return(1);
1515163953Srrs    else
1516166023Srrs       return(0);
1517166023Srrs}
1518166023Srrs
1519163953Srrs#ifndef MROUTE_LKM
1520163953Srrsint (*legal_vif_num)(int) = X_legal_vif_num;
1521163953Srrs#endif
1522163953Srrs
1523165220Srrs/*
1524163953Srrs * Return the local address used by this vif
1525166023Srrs */
1526166023Srrsstatic u_long
1527163953SrrsX_ip_mcast_src(vifi)
1528163953Srrs    int vifi;
1529163953Srrs{
1530166023Srrs    if (vifi >= 0 && vifi < numvifs)
1531166023Srrs	return viftable[vifi].v_lcl_addr.s_addr;
1532163953Srrs    else
1533163953Srrs	return INADDR_ANY;
1534163953Srrs}
1535163953Srrs
1536166086Srrs#ifndef MROUTE_LKM
1537163953Srrsu_long (*ip_mcast_src)(int) = X_ip_mcast_src;
1538163953Srrs#endif
1539163953Srrs
1540163953Srrsstatic void
1541163953Srrsphyint_send(ip, vifp, m)
1542163953Srrs    struct ip *ip;
1543163953Srrs    struct vif *vifp;
1544163953Srrs    struct mbuf *m;
1545163953Srrs{
1546163953Srrs    register struct mbuf *mb_copy;
1547163953Srrs    register int hlen = ip->ip_hl << 2;
1548163953Srrs
1549163953Srrs    /*
1550163953Srrs     * Make a new reference to the packet; make sure that
1551163953Srrs     * the IP header is actually copied, not just referenced,
1552163953Srrs     * so that ip_output() only scribbles on the copy.
1553163953Srrs     */
1554163953Srrs    mb_copy = m_copy(m, 0, M_COPYALL);
1555163953Srrs    if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1556163953Srrs	mb_copy = m_pullup(mb_copy, hlen);
1557163953Srrs    if (mb_copy == NULL)
1558163953Srrs	return;
1559168299Srrs
1560163953Srrs    if (vifp->v_rate_limit <= 0)
1561163953Srrs	tbf_send_packet(vifp, mb_copy);
1562163953Srrs    else
1563163953Srrs	tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len);
1564163953Srrs}
1565166675Srrs
1566163953Srrsstatic void
1567168299Srrsencap_send(ip, vifp, m)
1568167598Srrs    register struct ip *ip;
1569163953Srrs    register struct vif *vifp;
1570163953Srrs    register struct mbuf *m;
1571163953Srrs{
1572163953Srrs    register struct mbuf *mb_copy;
1573163953Srrs    register struct ip *ip_copy;
1574163953Srrs    register int i, len = ip->ip_len;
1575163953Srrs
1576163953Srrs    /*
1577163953Srrs     * copy the old packet & pullup its IP header into the
1578163953Srrs     * new mbuf so we can modify it.  Try to fill the new
1579163953Srrs     * mbuf since if we don't the ethernet driver will.
1580163953Srrs     */
1581163953Srrs    MGETHDR(mb_copy, M_DONTWAIT, MT_HEADER);
1582163953Srrs    if (mb_copy == NULL)
1583163953Srrs	return;
1584163953Srrs    mb_copy->m_data += max_linkhdr;
1585163953Srrs    mb_copy->m_len = sizeof(multicast_encap_iphdr);
1586163953Srrs
1587163953Srrs    if ((mb_copy->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1588163953Srrs	m_freem(mb_copy);
1589163953Srrs	return;
1590163953Srrs    }
1591163953Srrs    i = MHLEN - M_LEADINGSPACE(mb_copy);
1592163953Srrs    if (i > len)
1593163953Srrs	i = len;
1594163953Srrs    mb_copy = m_pullup(mb_copy, i);
1595163953Srrs    if (mb_copy == NULL)
1596163953Srrs	return;
1597163953Srrs    mb_copy->m_pkthdr.len = len + sizeof(multicast_encap_iphdr);
1598163953Srrs
1599163953Srrs    /*
1600163953Srrs     * fill in the encapsulating IP header.
1601163953Srrs     */
1602163953Srrs    ip_copy = mtod(mb_copy, struct ip *);
1603163953Srrs    *ip_copy = multicast_encap_iphdr;
1604163953Srrs    ip_copy->ip_id = htons(ip_id++);
1605163953Srrs    ip_copy->ip_len += len;
1606163953Srrs    ip_copy->ip_src = vifp->v_lcl_addr;
1607163953Srrs    ip_copy->ip_dst = vifp->v_rmt_addr;
1608163953Srrs
1609163953Srrs    /*
1610163953Srrs     * turn the encapsulated IP header back into a valid one.
1611163953Srrs     */
1612163953Srrs    ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
1613163953Srrs    --ip->ip_ttl;
1614163953Srrs    HTONS(ip->ip_len);
1615163953Srrs    HTONS(ip->ip_off);
1616163953Srrs    ip->ip_sum = 0;
1617163953Srrs    mb_copy->m_data += sizeof(multicast_encap_iphdr);
1618163953Srrs    ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
1619163953Srrs    mb_copy->m_data -= sizeof(multicast_encap_iphdr);
1620163953Srrs
1621163953Srrs    if (vifp->v_rate_limit <= 0)
1622163953Srrs	tbf_send_packet(vifp, mb_copy);
1623163953Srrs    else
1624163953Srrs	tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
1625163953Srrs}
1626163953Srrs
1627163953Srrs/*
1628163953Srrs * De-encapsulate a packet and feed it back through ip input (this
1629163953Srrs * routine is called whenever IP gets a packet with proto type
1630168299Srrs * ENCAP_PROTO and a local destination address).
1631163953Srrs */
1632163953Srrsvoid
1633163953Srrs#ifdef MROUTE_LKM
1634163953SrrsX_ipip_input(m, iphlen)
1635163953Srrs#else
1636163953Srrsipip_input(m, iphlen)
1637163953Srrs#endif
1638163953Srrs	register struct mbuf *m;
1639163953Srrs	int iphlen;
1640163953Srrs{
1641163953Srrs    struct ifnet *ifp = m->m_pkthdr.rcvif;
1642163953Srrs    register struct ip *ip = mtod(m, struct ip *);
1643163953Srrs    register int hlen = ip->ip_hl << 2;
1644163953Srrs    register int s;
1645163953Srrs    register struct ifqueue *ifq;
1646163953Srrs    register struct vif *vifp;
1647163953Srrs
1648163953Srrs    if (!have_encap_tunnel) {
1649163953Srrs	    rip_input(m, iphlen);
1650163953Srrs	    return;
1651163953Srrs    }
1652163953Srrs    /*
1653163953Srrs     * dump the packet if it's not to a multicast destination or if
1654163953Srrs     * we don't have an encapsulating tunnel with the source.
1655163953Srrs     * Note:  This code assumes that the remote site IP address
1656163953Srrs     * uniquely identifies the tunnel (i.e., that this site has
1657163953Srrs     * at most one tunnel with the remote site).
1658163953Srrs     */
1659163953Srrs    if (! IN_MULTICAST(ntohl(((struct ip *)((char *)ip + hlen))->ip_dst.s_addr))) {
1660163953Srrs	++mrtstat.mrts_bad_tunnel;
1661163953Srrs	m_freem(m);
1662163953Srrs	return;
1663163953Srrs    }
1664163953Srrs    if (ip->ip_src.s_addr != last_encap_src) {
1665163953Srrs	register struct vif *vife;
1666163953Srrs
1667163953Srrs	vifp = viftable;
1668163953Srrs	vife = vifp + numvifs;
1669163953Srrs	last_encap_src = ip->ip_src.s_addr;
1670163953Srrs	last_encap_vif = 0;
1671163953Srrs	for ( ; vifp < vife; ++vifp)
1672163953Srrs	    if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
1673163953Srrs		if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT))
1674163953Srrs		    == VIFF_TUNNEL)
1675163953Srrs		    last_encap_vif = vifp;
1676163953Srrs		break;
1677163953Srrs	    }
1678163953Srrs    }
1679163953Srrs    if ((vifp = last_encap_vif) == 0) {
1680163953Srrs	last_encap_src = 0;
1681163953Srrs	mrtstat.mrts_cant_tunnel++; /*XXX*/
1682163953Srrs	m_freem(m);
1683163953Srrs	if (mrtdebug)
1684165220Srrs	  log(LOG_DEBUG, "ip_mforward: no tunnel with %lx\n",
1685163953Srrs		(u_long)ntohl(ip->ip_src.s_addr));
1686163953Srrs	return;
1687163953Srrs    }
1688163953Srrs    ifp = vifp->v_ifp;
1689163953Srrs
1690163953Srrs    if (hlen > IP_HDR_LEN)
1691165220Srrs      ip_stripoptions(m, (struct mbuf *) 0);
1692163953Srrs    m->m_data += IP_HDR_LEN;
1693163953Srrs    m->m_len -= IP_HDR_LEN;
1694163953Srrs    m->m_pkthdr.len -= IP_HDR_LEN;
1695163953Srrs    m->m_pkthdr.rcvif = ifp;
1696163953Srrs
1697163953Srrs    ifq = &ipintrq;
1698163953Srrs    s = splimp();
1699163953Srrs    if (IF_QFULL(ifq)) {
1700163953Srrs	IF_DROP(ifq);
1701163953Srrs	m_freem(m);
1702163953Srrs    } else {
1703163953Srrs	IF_ENQUEUE(ifq, m);
1704163953Srrs	/*
1705166675Srrs	 * normally we would need a "schednetisr(NETISR_IP)"
1706163953Srrs	 * here but we were called by ip_input and it is going
1707163953Srrs	 * to loop back & try to dequeue the packet we just
1708163953Srrs	 * queued as soon as we return so we avoid the
1709163953Srrs	 * unnecessary software interrrupt.
1710163953Srrs	 */
1711163953Srrs    }
1712165220Srrs    splx(s);
1713163953Srrs}
1714163953Srrs
1715163953Srrs/*
1716163953Srrs * Token bucket filter module
1717163953Srrs */
1718163953Srrs
1719163953Srrsstatic void
1720163953Srrstbf_control(vifp, m, ip, p_len)
1721163953Srrs	register struct vif *vifp;
1722163953Srrs	register struct mbuf *m;
1723163953Srrs	register struct ip *ip;
1724163953Srrs	register u_long p_len;
1725163953Srrs{
1726163953Srrs    register struct tbf *t = vifp->v_tbf;
1727163953Srrs
1728163953Srrs    if (p_len > MAX_BKT_SIZE) {
1729163953Srrs	/* drop if packet is too large */
1730163953Srrs	mrtstat.mrts_pkt2large++;
1731163953Srrs	m_freem(m);
1732165220Srrs	return;
1733163953Srrs    }
1734163953Srrs
1735163953Srrs    tbf_update_tokens(vifp);
1736163953Srrs
1737163953Srrs    /* if there are enough tokens,
1738163953Srrs     * and the queue is empty,
1739163953Srrs     * send this packet out
1740163953Srrs     */
1741163953Srrs
1742163953Srrs    if (t->tbf_q_len == 0) {
1743163953Srrs	/* queue empty, send packet if enough tokens */
1744163953Srrs	if (p_len <= t->tbf_n_tok) {
1745163953Srrs	    t->tbf_n_tok -= p_len;
1746163953Srrs	    tbf_send_packet(vifp, m);
1747163953Srrs	} else {
1748163953Srrs	    /* queue packet and timeout till later */
1749163953Srrs	    tbf_queue(vifp, m);
1750163953Srrs	    timeout(tbf_reprocess_q, (caddr_t)vifp, TBF_REPROCESS);
1751163953Srrs	}
1752163953Srrs    } else if (t->tbf_q_len < t->tbf_max_q_len) {
1753163953Srrs	/* finite queue length, so queue pkts and process queue */
1754163953Srrs	tbf_queue(vifp, m);
1755163953Srrs	tbf_process_q(vifp);
1756163953Srrs    } else {
1757163953Srrs	/* queue length too much, try to dq and queue and process */
1758163953Srrs	if (!tbf_dq_sel(vifp, ip)) {
1759163953Srrs	    mrtstat.mrts_q_overflow++;
1760163953Srrs	    m_freem(m);
1761163953Srrs	    return;
1762163953Srrs	} else {
1763163953Srrs	    tbf_queue(vifp, m);
1764165220Srrs	    tbf_process_q(vifp);
1765163953Srrs	}
1766163953Srrs    }
1767163953Srrs    return;
1768163953Srrs}
1769163953Srrs
1770163953Srrs/*
1771163953Srrs * adds a packet to the queue at the interface
1772163953Srrs */
1773163953Srrsstatic void
1774163953Srrstbf_queue(vifp, m)
1775163953Srrs	register struct vif *vifp;
1776163953Srrs	register struct mbuf *m;
1777163953Srrs{
1778163953Srrs    register int s = splnet();
1779163953Srrs    register struct tbf *t = vifp->v_tbf;
1780163953Srrs
1781163953Srrs    if (t->tbf_t == NULL) {
1782163953Srrs	/* Queue was empty */
1783163953Srrs	t->tbf_q = m;
1784163953Srrs    } else {
1785163953Srrs	/* Insert at tail */
1786163953Srrs	t->tbf_t->m_act = m;
1787163953Srrs    }
1788163953Srrs
1789163953Srrs    /* Set new tail pointer */
1790163953Srrs    t->tbf_t = m;
1791163953Srrs
1792163953Srrs#ifdef DIAGNOSTIC
1793163953Srrs    /* Make sure we didn't get fed a bogus mbuf */
1794163953Srrs    if (m->m_act)
1795163953Srrs	panic("tbf_queue: m_act");
1796163953Srrs#endif
1797163953Srrs    m->m_act = NULL;
1798163953Srrs
1799163953Srrs    t->tbf_q_len++;
1800163953Srrs
1801163953Srrs    splx(s);
1802163953Srrs}
1803163953Srrs
1804163953Srrs
1805163953Srrs/*
1806163953Srrs * processes the queue at the interface
1807165220Srrs */
1808165220Srrsstatic void
1809165220Srrstbf_process_q(vifp)
1810163953Srrs    register struct vif *vifp;
1811163953Srrs{
1812163953Srrs    register struct mbuf *m;
1813163953Srrs    register int len;
1814163953Srrs    register int s = splnet();
1815163953Srrs    register struct tbf *t = vifp->v_tbf;
1816163953Srrs
1817163953Srrs    /* loop through the queue at the interface and send as many packets
1818163953Srrs     * as possible
1819163953Srrs     */
1820163953Srrs    while (t->tbf_q_len > 0) {
1821163953Srrs	m = t->tbf_q;
1822163953Srrs
1823165220Srrs	len = mtod(m, struct ip *)->ip_len;
1824163953Srrs
1825163953Srrs	/* determine if the packet can be sent */
1826163953Srrs	if (len <= t->tbf_n_tok) {
1827163953Srrs	    /* if so,
1828163953Srrs	     * reduce no of tokens, dequeue the packet,
1829163953Srrs	     * send the packet.
1830163953Srrs	     */
1831163953Srrs	    t->tbf_n_tok -= len;
1832163953Srrs
1833163953Srrs	    t->tbf_q = m->m_act;
1834163953Srrs	    if (--t->tbf_q_len == 0)
1835163953Srrs		t->tbf_t = NULL;
1836163953Srrs
1837163953Srrs	    m->m_act = NULL;
1838163953Srrs	    tbf_send_packet(vifp, m);
1839163953Srrs
1840163953Srrs	} else break;
1841163953Srrs    }
1842163953Srrs    splx(s);
1843163953Srrs}
1844163953Srrs
1845163953Srrsstatic void
1846163953Srrstbf_reprocess_q(xvifp)
1847163953Srrs	void *xvifp;
1848163953Srrs{
1849163953Srrs    register struct vif *vifp = xvifp;
1850163953Srrs    if (ip_mrouter == NULL)
1851163953Srrs	return;
1852163953Srrs
1853163953Srrs    tbf_update_tokens(vifp);
1854163953Srrs
1855163953Srrs    tbf_process_q(vifp);
1856163953Srrs
1857163953Srrs    if (vifp->v_tbf->tbf_q_len)
1858163953Srrs	timeout(tbf_reprocess_q, (caddr_t)vifp, TBF_REPROCESS);
1859163953Srrs}
1860163953Srrs
1861163953Srrs/* function that will selectively discard a member of the queue
1862163953Srrs * based on the precedence value and the priority
1863163953Srrs */
1864163953Srrsstatic int
1865163953Srrstbf_dq_sel(vifp, ip)
1866163953Srrs    register struct vif *vifp;
1867163953Srrs    register struct ip *ip;
1868163953Srrs{
1869165647Srrs    register int s = splnet();
1870163953Srrs    register u_int p;
1871163953Srrs    register struct mbuf *m, *last;
1872163953Srrs    register struct mbuf **np;
1873163953Srrs    register struct tbf *t = vifp->v_tbf;
1874163953Srrs
1875163953Srrs    p = priority(vifp, ip);
1876163953Srrs
1877163953Srrs    np = &t->tbf_q;
1878163953Srrs    last = NULL;
1879163953Srrs    while ((m = *np) != NULL) {
1880163953Srrs	if (p > priority(vifp, mtod(m, struct ip *))) {
1881163953Srrs	    *np = m->m_act;
1882166675Srrs	    /* If we're removing the last packet, fix the tail pointer */
1883163953Srrs	    if (m == t->tbf_t)
1884163953Srrs		t->tbf_t = last;
1885163953Srrs	    m_freem(m);
1886163953Srrs	    /* it's impossible for the queue to be empty, but
1887163953Srrs	     * we check anyway. */
1888163953Srrs	    if (--t->tbf_q_len == 0)
1889163953Srrs		t->tbf_t = NULL;
1890163953Srrs	    splx(s);
1891163953Srrs	    mrtstat.mrts_drop_sel++;
1892163953Srrs	    return(1);
1893163953Srrs	}
1894163953Srrs	np = &m->m_act;
1895163953Srrs	last = m;
1896163953Srrs    }
1897163953Srrs    splx(s);
1898163953Srrs    return(0);
1899163953Srrs}
1900163953Srrs
1901163953Srrsstatic void
1902163953Srrstbf_send_packet(vifp, m)
1903163953Srrs    register struct vif *vifp;
1904163953Srrs    register struct mbuf *m;
1905163953Srrs{
1906163953Srrs    struct ip_moptions imo;
1907163953Srrs    int error;
1908163953Srrs    static struct route ro;
1909163953Srrs    int s = splnet();
1910163953Srrs
1911163953Srrs    if (vifp->v_flags & VIFF_TUNNEL) {
1912163953Srrs	/* If tunnel options */
1913163953Srrs	ip_output(m, (struct mbuf *)0, &vifp->v_route,
1914163953Srrs		  IP_FORWARDING, (struct ip_moptions *)0);
1915163953Srrs    } else {
1916163953Srrs	imo.imo_multicast_ifp  = vifp->v_ifp;
1917163953Srrs	imo.imo_multicast_ttl  = mtod(m, struct ip *)->ip_ttl - 1;
1918163953Srrs	imo.imo_multicast_loop = 1;
1919163953Srrs	imo.imo_multicast_vif  = -1;
1920163953Srrs
1921163953Srrs	/*
1922163953Srrs	 * Re-entrancy should not be a problem here, because
1923163953Srrs	 * the packets that we send out and are looped back at us
1924163953Srrs	 * should get rejected because they appear to come from
1925163953Srrs	 * the loopback interface, thus preventing looping.
1926163953Srrs	 */
1927163953Srrs	error = ip_output(m, (struct mbuf *)0, &ro,
1928163953Srrs			  IP_FORWARDING, &imo);
1929163953Srrs
1930163953Srrs	if (mrtdebug & DEBUG_XMIT)
1931163953Srrs	    log(LOG_DEBUG, "phyint_send on vif %d err %d\n",
1932163953Srrs		vifp - viftable, error);
1933163953Srrs    }
1934163953Srrs    splx(s);
1935163953Srrs}
1936163953Srrs
1937163953Srrs/* determine the current time and then
1938163953Srrs * the elapsed time (between the last time and time now)
1939163953Srrs * in milliseconds & update the no. of tokens in the bucket
1940163953Srrs */
1941163953Srrsstatic void
1942163953Srrstbf_update_tokens(vifp)
1943163953Srrs    register struct vif *vifp;
1944163953Srrs{
1945163953Srrs    struct timeval tp;
1946163953Srrs    register u_long tm;
1947163953Srrs    register int s = splnet();
1948163953Srrs    register struct tbf *t = vifp->v_tbf;
1949163953Srrs
1950163953Srrs    GET_TIME(tp);
1951163953Srrs
1952163953Srrs    TV_DELTA(tp, t->tbf_last_pkt_t, tm);
1953163953Srrs
1954163953Srrs    /*
1955164085Srrs     * This formula is actually
1956163953Srrs     * "time in seconds" * "bytes/second".
1957163953Srrs     *
1958163953Srrs     * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
1959163953Srrs     *
1960163953Srrs     * The (1000/1024) was introduced in add_vif to optimize
1961163953Srrs     * this divide into a shift.
1962163953Srrs     */
1963163953Srrs    t->tbf_n_tok += tm * vifp->v_rate_limit / 1024 / 8;
1964163953Srrs    t->tbf_last_pkt_t = tp;
1965163953Srrs
1966163953Srrs    if (t->tbf_n_tok > MAX_BKT_SIZE)
1967163953Srrs	t->tbf_n_tok = MAX_BKT_SIZE;
1968163953Srrs
1969163953Srrs    splx(s);
1970163953Srrs}
1971163953Srrs
1972163953Srrsstatic int
1973163953Srrspriority(vifp, ip)
1974163953Srrs    register struct vif *vifp;
1975163953Srrs    register struct ip *ip;
1976163953Srrs{
1977163953Srrs    register int prio;
1978163953Srrs
1979163953Srrs    /* temporary hack; may add general packet classifier some day */
1980163953Srrs
1981163953Srrs    /*
1982163953Srrs     * The UDP port space is divided up into four priority ranges:
1983165647Srrs     * [0, 16384)     : unclassified - lowest priority
1984165647Srrs     * [16384, 32768) : audio - highest priority
1985163953Srrs     * [32768, 49152) : whiteboard - medium priority
1986165647Srrs     * [49152, 65536) : video - low priority
1987163953Srrs     */
1988163953Srrs    if (ip->ip_p == IPPROTO_UDP) {
1989163953Srrs	struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
1990163953Srrs	switch (ntohs(udp->uh_dport) & 0xc000) {
1991163953Srrs	    case 0x4000:
1992163953Srrs		prio = 70;
1993163953Srrs		break;
1994163953Srrs	    case 0x8000:
1995163953Srrs		prio = 60;
1996163953Srrs		break;
1997163953Srrs	    case 0xc000:
1998163953Srrs		prio = 55;
1999163953Srrs		break;
2000163953Srrs	    default:
2001163953Srrs		prio = 50;
2002163953Srrs		break;
2003163953Srrs	}
2004163953Srrs	if (tbfdebug > 1)
2005163953Srrs		log(LOG_DEBUG, "port %x prio%d\n", ntohs(udp->uh_dport), prio);
2006163953Srrs    } else {
2007163953Srrs	    prio = 50;
2008163953Srrs    }
2009163953Srrs    return prio;
2010163953Srrs}
2011163953Srrs
2012163953Srrs/*
2013165647Srrs * End of token bucket filter modifications
2014163953Srrs */
2015163953Srrs
2016163953Srrsint
2017163953Srrsip_rsvp_vif_init(so, sopt)
2018163953Srrs	struct socket *so;
2019165647Srrs	struct sockopt *sopt;
2020165647Srrs{
2021165647Srrs    int error, i, s;
2022163953Srrs
2023163953Srrs    if (rsvpdebug)
2024165647Srrs	printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
2025163953Srrs	       so->so_type, so->so_proto->pr_protocol);
2026163953Srrs
2027163953Srrs    if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2028163953Srrs	return EOPNOTSUPP;
2029163953Srrs
2030163953Srrs    /* Check mbuf. */
2031163953Srrs    error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
2032163953Srrs    if (error)
2033163953Srrs	    return (error);
2034163953Srrs
2035163953Srrs    if (rsvpdebug)
2036163953Srrs	printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n", i, rsvp_on);
2037163953Srrs
2038163953Srrs    s = splnet();
2039163953Srrs
2040163953Srrs    /* Check vif. */
2041163953Srrs    if (!legal_vif_num(i)) {
2042163953Srrs	splx(s);
2043163953Srrs	return EADDRNOTAVAIL;
2044163953Srrs    }
2045163953Srrs
2046163953Srrs    /* Check if socket is available. */
2047163953Srrs    if (viftable[i].v_rsvpd != NULL) {
2048163953Srrs	splx(s);
2049163953Srrs	return EADDRINUSE;
2050163953Srrs    }
2051163953Srrs
2052163953Srrs    viftable[i].v_rsvpd = so;
2053163953Srrs    /* This may seem silly, but we need to be sure we don't over-increment
2054163953Srrs     * the RSVP counter, in case something slips up.
2055163953Srrs     */
2056163953Srrs    if (!viftable[i].v_rsvp_on) {
2057163953Srrs	viftable[i].v_rsvp_on = 1;
2058163953Srrs	rsvp_on++;
2059163953Srrs    }
2060163953Srrs
2061163953Srrs    splx(s);
2062163953Srrs    return 0;
2063163953Srrs}
2064163953Srrs
2065163953Srrsint
2066163953Srrsip_rsvp_vif_done(so, sopt)
2067163953Srrs	struct socket *so;
2068163953Srrs	struct sockopt *sopt;
2069163953Srrs{
2070163953Srrs	int error, i, s;
2071163953Srrs
2072163953Srrs	if (rsvpdebug)
2073163953Srrs		printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n",
2074163953Srrs		       so->so_type, so->so_proto->pr_protocol);
2075163953Srrs
2076163953Srrs	if (so->so_type != SOCK_RAW ||
2077165220Srrs	    so->so_proto->pr_protocol != IPPROTO_RSVP)
2078165220Srrs		return EOPNOTSUPP;
2079165220Srrs
2080165220Srrs	error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
2081165220Srrs	if (error)
2082165220Srrs		return (error);
2083165220Srrs
2084165220Srrs	s = splnet();
2085165220Srrs
2086165220Srrs	/* Check vif. */
2087165220Srrs	if (!legal_vif_num(i)) {
2088165220Srrs		splx(s);
2089165220Srrs		return EADDRNOTAVAIL;
2090165220Srrs	}
2091165220Srrs
2092165220Srrs	if (rsvpdebug)
2093165220Srrs		printf("ip_rsvp_vif_done: v_rsvpd = %p so = %p\n",
2094165220Srrs		       viftable[i].v_rsvpd, so);
2095165220Srrs
2096165220Srrs	viftable[i].v_rsvpd = NULL;
2097165220Srrs	/*
2098165220Srrs	 * This may seem silly, but we need to be sure we don't over-decrement
2099165220Srrs	 * the RSVP counter, in case something slips up.
2100165220Srrs	 */
2101165220Srrs	if (viftable[i].v_rsvp_on) {
2102163953Srrs		viftable[i].v_rsvp_on = 0;
2103163953Srrs		rsvp_on--;
2104163953Srrs	}
2105163953Srrs
2106163953Srrs	splx(s);
2107163953Srrs	return 0;
2108163953Srrs}
2109163953Srrs
2110163953Srrsvoid
2111163953Srrsip_rsvp_force_done(so)
2112163953Srrs    struct socket *so;
2113163953Srrs{
2114163953Srrs    int vifi;
2115163953Srrs    register int s;
2116163953Srrs
2117163953Srrs    /* Don't bother if it is not the right type of socket. */
2118163953Srrs    if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2119163953Srrs	return;
2120163953Srrs
2121163953Srrs    s = splnet();
2122163953Srrs
2123163953Srrs    /* The socket may be attached to more than one vif...this
2124163953Srrs     * is perfectly legal.
2125163953Srrs     */
2126163953Srrs    for (vifi = 0; vifi < numvifs; vifi++) {
2127163953Srrs	if (viftable[vifi].v_rsvpd == so) {
2128163953Srrs	    viftable[vifi].v_rsvpd = NULL;
2129163953Srrs	    /* This may seem silly, but we need to be sure we don't
2130163953Srrs	     * over-decrement the RSVP counter, in case something slips up.
2131163953Srrs	     */
2132163953Srrs	    if (viftable[vifi].v_rsvp_on) {
2133163953Srrs		viftable[vifi].v_rsvp_on = 0;
2134165220Srrs		rsvp_on--;
2135165220Srrs	    }
2136163953Srrs	}
2137163953Srrs    }
2138163953Srrs
2139163953Srrs    splx(s);
2140163953Srrs    return;
2141163953Srrs}
2142163953Srrs
2143163953Srrsvoid
2144163953Srrsrsvp_input(m, iphlen)
2145163953Srrs	struct mbuf *m;
2146163953Srrs	int iphlen;
2147163953Srrs{
2148163953Srrs    int vifi;
2149163953Srrs    register struct ip *ip = mtod(m, struct ip *);
2150163953Srrs    static struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
2151163953Srrs    register int s;
2152163953Srrs    struct ifnet *ifp;
2153163953Srrs
2154163953Srrs    if (rsvpdebug)
2155163953Srrs	printf("rsvp_input: rsvp_on %d\n",rsvp_on);
2156163953Srrs
2157163953Srrs    /* Can still get packets with rsvp_on = 0 if there is a local member
2158163953Srrs     * of the group to which the RSVP packet is addressed.  But in this
2159163953Srrs     * case we want to throw the packet away.
2160163953Srrs     */
2161163953Srrs    if (!rsvp_on) {
2162163953Srrs	m_freem(m);
2163163953Srrs	return;
2164163953Srrs    }
2165163953Srrs
2166163953Srrs    /* If the old-style non-vif-associated socket is set, then use
2167163953Srrs     * it and ignore the new ones.
2168163953Srrs     */
2169163953Srrs    if (ip_rsvpd != NULL) {
2170163953Srrs	if (rsvpdebug)
2171163953Srrs	    printf("rsvp_input: Sending packet up old-style socket\n");
2172163953Srrs	rip_input(m, iphlen);
2173163953Srrs	return;
2174163953Srrs    }
2175163953Srrs
2176163953Srrs    s = splnet();
2177166086Srrs
2178163953Srrs    if (rsvpdebug)
2179163953Srrs	printf("rsvp_input: check vifs\n");
2180163953Srrs
2181163953Srrs#ifdef DIAGNOSTIC
2182163953Srrs    if (!(m->m_flags & M_PKTHDR))
2183163953Srrs	    panic("rsvp_input no hdr");
2184163953Srrs#endif
2185163953Srrs
2186163953Srrs    ifp = m->m_pkthdr.rcvif;
2187163953Srrs    /* Find which vif the packet arrived on. */
2188163953Srrs    for (vifi = 0; vifi < numvifs; vifi++) {
2189163953Srrs	if (viftable[vifi].v_ifp == ifp)
2190163953Srrs 		break;
2191163953Srrs 	}
2192163953Srrs
2193163953Srrs    if (vifi == numvifs) {
2194163953Srrs	/* Can't find vif packet arrived on. Drop packet. */
2195165220Srrs	if (rsvpdebug)
2196163953Srrs	    printf("rsvp_input: Can't find vif for packet...dropping it.\n");
2197163953Srrs	m_freem(m);
2198163953Srrs	splx(s);
2199166086Srrs	return;
2200166086Srrs    }
2201166086Srrs
2202166086Srrs    if (rsvpdebug)
2203166086Srrs	printf("rsvp_input: check socket\n");
2204166086Srrs
2205163953Srrs    if (viftable[vifi].v_rsvpd == NULL) {
2206163953Srrs	/* drop packet, since there is no specific socket for this
2207163953Srrs	 * interface */
2208166086Srrs	    if (rsvpdebug)
2209163953Srrs		    printf("rsvp_input: No socket defined for vif %d\n",vifi);
2210163953Srrs	    m_freem(m);
2211163953Srrs	    splx(s);
2212163953Srrs	    return;
2213163953Srrs    }
2214163953Srrs    rsvp_src.sin_addr = ip->ip_src;
2215163953Srrs
2216163953Srrs    if (rsvpdebug && m)
2217163953Srrs	printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
2218163953Srrs	       m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv)));
2219163953Srrs
2220163953Srrs    if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0)
2221163953Srrs	if (rsvpdebug)
2222163953Srrs	    printf("rsvp_input: Failed to append to socket\n");
2223163953Srrs    else
2224163953Srrs	if (rsvpdebug)
2225163953Srrs	    printf("rsvp_input: send packet up\n");
2226163953Srrs
2227163953Srrs    splx(s);
2228163953Srrs}
2229163953Srrs
2230163953Srrs#ifdef MROUTE_LKM
2231163953Srrs#include <sys/conf.h>
2232163953Srrs#include <sys/exec.h>
2233163953Srrs#include <sys/sysent.h>
2234163953Srrs#include <sys/lkm.h>
2235163953Srrs
2236163953SrrsMOD_MISC("ip_mroute_mod")
2237163953Srrs
2238163953Srrsstatic int
2239166086Srrsip_mroute_mod_handle(struct lkm_table *lkmtp, int cmd)
2240166086Srrs{
2241166086Srrs	int i;
2242166086Srrs	struct lkm_misc	*args = lkmtp->private.lkm_misc;
2243166086Srrs	int err = 0;
2244166086Srrs
2245166086Srrs	switch(cmd) {
2246166086Srrs		static int (*old_ip_mrouter_cmd)();
2247166086Srrs		static int (*old_ip_mrouter_done)();
2248166086Srrs		static int (*old_ip_mforward)();
2249166086Srrs		static int (*old_mrt_ioctl)();
2250166086Srrs		static void (*old_proto4_input)();
2251166086Srrs		static int (*old_legal_vif_num)();
2252166086Srrs		extern struct protosw inetsw[];
2253166086Srrs
2254163953Srrs	case LKM_E_LOAD:
2255163953Srrs		if(lkmexists(lkmtp) || ip_mrtproto)
2256166086Srrs		  return(EEXIST);
2257163953Srrs		old_ip_mrouter_cmd = ip_mrouter_cmd;
2258166086Srrs		ip_mrouter_cmd = X_ip_mrouter_cmd;
2259166086Srrs		old_ip_mrouter_done = ip_mrouter_done;
2260166086Srrs		ip_mrouter_done = X_ip_mrouter_done;
2261166086Srrs		old_ip_mforward = ip_mforward;
2262166086Srrs		ip_mforward = X_ip_mforward;
2263163953Srrs		old_mrt_ioctl = mrt_ioctl;
2264163953Srrs		mrt_ioctl = X_mrt_ioctl;
2265163953Srrs              old_proto4_input = inetsw[ip_protox[ENCAP_PROTO]].pr_input;
2266163953Srrs              inetsw[ip_protox[ENCAP_PROTO]].pr_input = X_ipip_input;
2267163953Srrs		old_legal_vif_num = legal_vif_num;
2268163953Srrs		legal_vif_num = X_legal_vif_num;
2269163953Srrs		ip_mrtproto = IGMP_DVMRP;
2270163953Srrs
2271163953Srrs		printf("\nIP multicast routing loaded\n");
2272163953Srrs		break;
2273163953Srrs
2274163953Srrs	case LKM_E_UNLOAD:
2275163953Srrs		if (ip_mrouter)
2276163953Srrs		  return EINVAL;
2277163953Srrs
2278163953Srrs		ip_mrouter_cmd = old_ip_mrouter_cmd;
2279163953Srrs		ip_mrouter_done = old_ip_mrouter_done;
2280163953Srrs		ip_mforward = old_ip_mforward;
2281163953Srrs		mrt_ioctl = old_mrt_ioctl;
2282163953Srrs              inetsw[ip_protox[ENCAP_PROTO]].pr_input = old_proto4_input;
2283163953Srrs		legal_vif_num = old_legal_vif_num;
2284163953Srrs		ip_mrtproto = 0;
2285163953Srrs		break;
2286163953Srrs
2287163953Srrs	default:
2288163953Srrs		err = EINVAL;
2289163953Srrs		break;
2290163953Srrs	}
2291163953Srrs
2292163953Srrs	return(err);
2293163953Srrs}
2294163953Srrs
2295163953Srrsint
2296163953Srrsip_mroute_mod(struct lkm_table *lkmtp, int cmd, int ver) {
2297163953Srrs	DISPATCH(lkmtp, cmd, ver, ip_mroute_mod_handle, ip_mroute_mod_handle,
2298163953Srrs		 nosys);
2299163953Srrs}
2300163953Srrs
2301163953Srrs#endif /* MROUTE_LKM */
2302163953Srrs#endif /* MROUTING */
2303163953Srrs