]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/ip_mroute.c
xnu-2422.90.20.tar.gz
[apple/xnu.git] / bsd / netinet / ip_mroute.c
CommitLineData
1c79356b 1/*
39236c6e 2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
30 * support for mandatory and extensible security protections. This notice
31 * is included in support of clause 2.2 (b) of the Apple Public License,
32 * Version 2.0.
1c79356b
A
33 */
34/*
35 * IP multicast forwarding procedures
36 *
37 * Written by David Waitzman, BBN Labs, August 1988.
38 * Modified by Steve Deering, Stanford, February 1989.
39 * Modified by Mark J. Steiglitz, Stanford, May, 1991
40 * Modified by Van Jacobson, LBL, January 1993
41 * Modified by Ajit Thyagarajan, PARC, August 1993
42 * Modified by Bill Fenner, PARC, April 1995
43 *
44 * MROUTING Revision: 3.5
9bccf70c 45 * $FreeBSD: src/sys/netinet/ip_mroute.c,v 1.56.2.2 2001/07/19 06:37:26 kris Exp $
1c79356b
A
46 */
47
1c79356b
A
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/malloc.h>
52#include <sys/mbuf.h>
53#include <sys/socket.h>
54#include <sys/socketvar.h>
55#include <sys/protosw.h>
56#include <sys/time.h>
57#include <sys/kernel.h>
58#include <sys/sockio.h>
59#include <sys/syslog.h>
b0d623f7
A
60
61#include <machine/endian.h>
62
1c79356b
A
63#include <net/if.h>
64#include <net/route.h>
2d21ac55 65#include <net/kpi_protocol.h>
1c79356b
A
66#include <netinet/in.h>
67#include <netinet/in_systm.h>
1c79356b
A
68#include <netinet/ip.h>
69#include <netinet/ip_var.h>
70#include <netinet/in_var.h>
71#include <netinet/igmp.h>
72#include <netinet/ip_mroute.h>
73#include <netinet/udp.h>
74
2d21ac55
A
75#if CONFIG_MACF_NET
76#include <security/mac_framework.h>
77#endif
78
1c79356b 79
0b4c1975 80#if !MROUTING
b0d623f7 81extern u_int32_t _ip_mcast_src(int vifi);
91447636
A
82extern int _ip_mforward(struct ip *ip, struct ifnet *ifp,
83 struct mbuf *m, struct ip_moptions *imo);
84extern int _ip_mrouter_done(void);
85extern int _ip_mrouter_get(struct socket *so, struct sockopt *sopt);
86extern int _ip_mrouter_set(struct socket *so, struct sockopt *sopt);
87extern int _mrt_ioctl(int req, caddr_t data, struct proc *p);
1c79356b
A
88
89/*
90 * Dummy routines and globals used when multicast routing is not compiled in.
91 */
92
93struct socket *ip_mrouter = NULL;
94u_int rsvpdebug = 0;
95
96int
2d21ac55
A
97_ip_mrouter_set(__unused struct socket *so,
98 __unused struct sockopt *sopt)
1c79356b
A
99{
100 return(EOPNOTSUPP);
101}
102
103int (*ip_mrouter_set)(struct socket *, struct sockopt *) = _ip_mrouter_set;
104
105
106int
2d21ac55
A
107_ip_mrouter_get(__unused struct socket *so,
108 __unused sockopt *sopt)
1c79356b
A
109{
110 return(EOPNOTSUPP);
111}
112
113int (*ip_mrouter_get)(struct socket *, struct sockopt *) = _ip_mrouter_get;
114
115int
2d21ac55 116_ip_mrouter_done(void)
1c79356b
A
117{
118 return(0);
119}
120
121int (*ip_mrouter_done)(void) = _ip_mrouter_done;
122
123int
2d21ac55
A
124_ip_mforward(__unused struct ip *ip, __unused struct ifnet *ifp,
125 __unused struct mbuf *m, __unused ip_moptions *imo)
1c79356b
A
126{
127 return(0);
128}
129
130int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
131 struct ip_moptions *) = _ip_mforward;
132
133int
6d2010ae 134_mrt_ioctl(__unused u_long req, __unused caddr_t data, __unused struct proc *p)
1c79356b
A
135{
136 return EOPNOTSUPP;
137}
138
6d2010ae 139int (*mrt_ioctl)(u_long, caddr_t, struct proc *) = _mrt_ioctl;
1c79356b
A
140
141void
2d21ac55 142rsvp_input(struct mbuf *m, int iphlen) /* XXX must fixup manually */
1c79356b
A
143{
144 /* Can still get packets with rsvp_on = 0 if there is a local member
145 * of the group to which the RSVP packet is addressed. But in this
146 * case we want to throw the packet away.
147 */
148 if (!rsvp_on) {
149 m_freem(m);
150 return;
151 }
152
153 if (ip_rsvpd != NULL) {
154 if (rsvpdebug)
155 printf("rsvp_input: Sending packet up old-style socket\n");
156 rip_input(m, iphlen);
157 return;
158 }
159 /* Drop the packet */
160 m_freem(m);
161}
162
163void ipip_input(struct mbuf *m, int iphlen) { /* XXX must fixup manually */
164 rip_input(m, iphlen);
165}
166
167int (*legal_vif_num)(int) = 0;
168
169/*
170 * This should never be called, since IP_MULTICAST_VIF should fail, but
171 * just in case it does get called, the code a little lower in ip_output
172 * will assign the packet a local address.
173 */
b0d623f7 174u_int32_t
1c79356b 175_ip_mcast_src(int vifi) { return INADDR_ANY; }
b0d623f7 176u_int32_t (*ip_mcast_src)(int) = _ip_mcast_src;
1c79356b
A
177
178int
179ip_rsvp_vif_init(so, sopt)
180 struct socket *so;
181 struct sockopt *sopt;
182{
183 return(EINVAL);
184}
185
186int
187ip_rsvp_vif_done(so, sopt)
188 struct socket *so;
189 struct sockopt *sopt;
190{
191 return(EINVAL);
192}
193
194void
195ip_rsvp_force_done(so)
196 struct socket *so;
197{
198 return;
199}
200
201#else /* MROUTING */
202
203#define M_HASCL(m) ((m)->m_flags & M_EXT)
204
205#define INSIZ sizeof(struct in_addr)
206#define same(a1, a2) \
207 (bcmp((caddr_t)(a1), (caddr_t)(a2), INSIZ) == 0)
208
209
210/*
211 * Globals. All but ip_mrouter and ip_mrtproto could be static,
212 * except for netstat or debugging purposes.
213 */
9bccf70c 214#ifndef MROUTE_LKM
1c79356b
A
215struct socket *ip_mrouter = NULL;
216static struct mrtstat mrtstat;
217#else /* MROUTE_LKM */
91447636 218extern void X_ipip_input(struct mbuf *m, int iphlen);
1c79356b
A
219extern struct mrtstat mrtstat;
220static int ip_mrtproto;
221#endif
222
223#define NO_RTE_FOUND 0x1
224#define RTE_FOUND 0x2
225
2d21ac55
A
226static struct mfc *mfctable[CONFIG_MFCTBLSIZ];
227static u_char nexpire[CONFIG_MFCTBLSIZ];
228static struct vif viftable[CONFIG_MAXVIFS];
1c79356b
A
229static u_int mrtdebug = 0; /* debug level */
230#define DEBUG_MFC 0x02
231#define DEBUG_FORWARD 0x04
232#define DEBUG_EXPIRE 0x08
233#define DEBUG_XMIT 0x10
234static u_int tbfdebug = 0; /* tbf debug level */
235static u_int rsvpdebug = 0; /* rsvp debug level */
236
1c79356b
A
237#define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
238#define UPCALL_EXPIRE 6 /* number of timeouts */
239
240/*
241 * Define the token bucket filter structures
242 * tbftable -> each vif has one of these for storing info
243 */
244
2d21ac55 245static struct tbf tbftable[CONFIG_MAXVIFS];
1c79356b
A
246#define TBF_REPROCESS (hz / 100) /* 100x / second */
247
248/*
249 * 'Interfaces' associated with decapsulator (so we can tell
250 * packets that went through it from ones that get reflected
251 * by a broken gateway). These interfaces are never linked into
252 * the system ifnet list & no routes point to them. I.e., packets
253 * can't be sent this way. They only exist as a placeholder for
254 * multicast source verification.
255 */
2d21ac55 256static struct ifnet multicast_decap_if[CONFIG_MAXVIFS];
1c79356b
A
257
258#define ENCAP_TTL 64
259#define ENCAP_PROTO IPPROTO_IPIP /* 4 */
260
261/* prototype IP hdr for encapsulated packets */
262static struct ip multicast_encap_iphdr = {
263#if BYTE_ORDER == LITTLE_ENDIAN
264 sizeof(struct ip) >> 2, IPVERSION,
265#else
266 IPVERSION, sizeof(struct ip) >> 2,
267#endif
268 0, /* tos */
269 sizeof(struct ip), /* total length */
270 0, /* id */
271 0, /* frag offset */
272 ENCAP_TTL, ENCAP_PROTO,
273 0, /* checksum */
2d21ac55 274 { 0 }, { 0 }
1c79356b
A
275};
276
277/*
278 * Private variables.
279 */
280static vifi_t numvifs = 0;
281static int have_encap_tunnel = 0;
282
283/*
284 * one-back cache used by ipip_input to locate a tunnel's vif
285 * given a datagram's src ip address.
286 */
b0d623f7 287static u_int32_t last_encap_src;
1c79356b
A
288static struct vif *last_encap_vif;
289
b0d623f7 290static u_int32_t X_ip_mcast_src(int vifi);
91447636
A
291static int X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m, struct ip_moptions *imo);
292static int X_ip_mrouter_done(void);
293static int X_ip_mrouter_get(struct socket *so, struct sockopt *m);
294static int X_ip_mrouter_set(struct socket *so, struct sockopt *m);
295static int X_legal_vif_num(int vif);
6d2010ae 296static int X_mrt_ioctl(u_long cmd, caddr_t data);
1c79356b
A
297
298static int get_sg_cnt(struct sioc_sg_req *);
299static int get_vif_cnt(struct sioc_vif_req *);
300static int ip_mrouter_init(struct socket *, int);
301static int add_vif(struct vifctl *);
302static int del_vif(vifi_t);
303static int add_mfc(struct mfcctl *);
304static int del_mfc(struct mfcctl *);
305static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
306static int set_assert(int);
307static void expire_upcalls(void *);
308static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *,
309 vifi_t);
310static void phyint_send(struct ip *, struct vif *, struct mbuf *);
311static void encap_send(struct ip *, struct vif *, struct mbuf *);
b0d623f7 312static void tbf_control(struct vif *, struct mbuf *, struct ip *, u_int32_t);
1c79356b
A
313static void tbf_queue(struct vif *, struct mbuf *);
314static void tbf_process_q(struct vif *);
315static void tbf_reprocess_q(void *);
316static int tbf_dq_sel(struct vif *, struct ip *);
317static void tbf_send_packet(struct vif *, struct mbuf *);
318static void tbf_update_tokens(struct vif *);
319static int priority(struct vif *, struct ip *);
320void multiencap_decap(struct mbuf *);
321
322/*
323 * whether or not special PIM assert processing is enabled.
324 */
325static int pim_assert;
326/*
327 * Rate limit for assert notification messages, in usec
328 */
329#define ASSERT_MSG_TIME 3000000
330
331/*
332 * Hash function for a source, group entry
333 */
334#define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
335 ((g) >> 20) ^ ((g) >> 10) ^ (g))
336
337/*
338 * Find a route for a given origin IP address and Multicast group address
339 * Type of service parameter to be added in the future!!!
340 */
341
342#define MFCFIND(o, g, rt) { \
2d21ac55 343 struct mfc *_rt = mfctable[MFCHASH(o,g)]; \
1c79356b
A
344 rt = NULL; \
345 ++mrtstat.mrts_mfc_lookups; \
346 while (_rt) { \
347 if ((_rt->mfc_origin.s_addr == o) && \
348 (_rt->mfc_mcastgrp.s_addr == g) && \
349 (_rt->mfc_stall == NULL)) { \
350 rt = _rt; \
351 break; \
352 } \
353 _rt = _rt->mfc_next; \
354 } \
355 if (rt == NULL) { \
356 ++mrtstat.mrts_mfc_misses; \
357 } \
358}
359
360
361/*
362 * Macros to compute elapsed time efficiently
363 * Borrowed from Van Jacobson's scheduling code
364 */
365#define TV_DELTA(a, b, delta) { \
2d21ac55 366 int xxs; \
1c79356b
A
367 \
368 delta = (a).tv_usec - (b).tv_usec; \
369 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
370 switch (xxs) { \
371 case 2: \
372 delta += 1000000; \
373 /* fall through */ \
374 case 1: \
375 delta += 1000000; \
376 break; \
377 default: \
378 delta += (1000000 * xxs); \
379 } \
380 } \
381}
382
383#define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
384 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
385
386#if UPCALL_TIMING
b0d623f7 387u_int32_t upcall_data[51];
1c79356b
A
388static void collate(struct timeval *);
389#endif /* UPCALL_TIMING */
390
391
392/*
393 * Handle MRT setsockopt commands to modify the multicast routing tables.
394 */
395static int
2d21ac55 396X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
1c79356b
A
397{
398 int error, optval;
399 vifi_t vifi;
400 struct vifctl vifc;
401 struct mfcctl mfc;
402
403 if (so != ip_mrouter && sopt->sopt_name != MRT_INIT)
404 return (EPERM);
405
406 error = 0;
407 switch (sopt->sopt_name) {
408 case MRT_INIT:
409 error = sooptcopyin(sopt, &optval, sizeof optval,
410 sizeof optval);
411 if (error)
412 break;
413 error = ip_mrouter_init(so, optval);
414 break;
415
416 case MRT_DONE:
417 error = ip_mrouter_done();
418 break;
419
420 case MRT_ADD_VIF:
421 error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc);
422 if (error)
423 break;
424 error = add_vif(&vifc);
425 break;
426
427 case MRT_DEL_VIF:
428 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
429 if (error)
430 break;
431 error = del_vif(vifi);
432 break;
433
434 case MRT_ADD_MFC:
435 case MRT_DEL_MFC:
436 error = sooptcopyin(sopt, &mfc, sizeof mfc, sizeof mfc);
437 if (error)
438 break;
439 if (sopt->sopt_name == MRT_ADD_MFC)
440 error = add_mfc(&mfc);
441 else
442 error = del_mfc(&mfc);
443 break;
444
445 case MRT_ASSERT:
446 error = sooptcopyin(sopt, &optval, sizeof optval,
447 sizeof optval);
448 if (error)
449 break;
450 set_assert(optval);
451 break;
452
453 default:
454 error = EOPNOTSUPP;
455 break;
456 }
457 return (error);
458}
459
9bccf70c 460#if !defined(MROUTE_LKM) || !MROUTE_LKM
1c79356b
A
461int (*ip_mrouter_set)(struct socket *, struct sockopt *) = X_ip_mrouter_set;
462#endif
463
464/*
465 * Handle MRT getsockopt commands
466 */
467static int
2d21ac55 468X_ip_mrouter_get(__unused struct socket *so, struct sockopt *sopt)
1c79356b
A
469{
470 int error;
2d21ac55 471 static int vers = 0x0305; /* !!! why is this here? XXX */
1c79356b
A
472
473 switch (sopt->sopt_name) {
474 case MRT_VERSION:
2d21ac55 475 error = sooptcopyout(sopt, &vers, sizeof vers);
1c79356b
A
476 break;
477
478 case MRT_ASSERT:
479 error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert);
480 break;
481 default:
482 error = EOPNOTSUPP;
483 break;
484 }
485 return (error);
486}
487
9bccf70c 488#if !defined(MROUTE_LKM) || !MROUTE_LKM
1c79356b
A
489int (*ip_mrouter_get)(struct socket *, struct sockopt *) = X_ip_mrouter_get;
490#endif
491
492/*
493 * Handle ioctl commands to obtain information from the cache
494 */
495static int
6d2010ae 496X_mrt_ioctl(u_long cmd, caddr_t data)
1c79356b
A
497{
498 int error = 0;
499
500 switch (cmd) {
501 case (SIOCGETVIFCNT):
502 return (get_vif_cnt((struct sioc_vif_req *)data));
503 break;
504 case (SIOCGETSGCNT):
505 return (get_sg_cnt((struct sioc_sg_req *)data));
506 break;
507 default:
508 return (EINVAL);
509 break;
510 }
511 return error;
512}
513
9bccf70c 514#if !defined(MROUTE_LKM) || !MROUTE_LKM
6d2010ae 515int (*mrt_ioctl)(u_long, caddr_t) = X_mrt_ioctl;
1c79356b
A
516#endif
517
518/*
519 * returns the packet, byte, rpf-failure count for the source group provided
520 */
521static int
2d21ac55 522get_sg_cnt(struct sioc_sg_req *req)
1c79356b 523{
2d21ac55 524 struct mfc *rt;
1c79356b 525
1c79356b 526 MFCFIND(req->src.s_addr, req->grp.s_addr, rt);
1c79356b
A
527 if (rt != NULL) {
528 req->pktcnt = rt->mfc_pkt_cnt;
529 req->bytecnt = rt->mfc_byte_cnt;
530 req->wrong_if = rt->mfc_wrong_if;
531 } else
532 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
533
534 return 0;
535}
536
537/*
538 * returns the input and output packet and byte counts on the vif provided
539 */
540static int
2d21ac55 541get_vif_cnt(struct sioc_vif_req *req)
1c79356b 542{
2d21ac55 543 vifi_t vifi = req->vifi;
1c79356b
A
544
545 if (vifi >= numvifs) return EINVAL;
546
547 req->icount = viftable[vifi].v_pkt_in;
548 req->ocount = viftable[vifi].v_pkt_out;
549 req->ibytes = viftable[vifi].v_bytes_in;
550 req->obytes = viftable[vifi].v_bytes_out;
551
552 return 0;
553}
554
555/*
556 * Enable multicast routing
557 */
558static int
2d21ac55 559ip_mrouter_init(struct socket *so, int vers)
1c79356b
A
560{
561 if (mrtdebug)
562 log(LOG_DEBUG,"ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
563 so->so_type, so->so_proto->pr_protocol);
564
565 if (so->so_type != SOCK_RAW ||
566 so->so_proto->pr_protocol != IPPROTO_IGMP) return EOPNOTSUPP;
567
2d21ac55 568 if (vers != 1)
1c79356b
A
569 return ENOPROTOOPT;
570
571 if (ip_mrouter != NULL) return EADDRINUSE;
572
573 ip_mrouter = so;
574
575 bzero((caddr_t)mfctable, sizeof(mfctable));
576 bzero((caddr_t)nexpire, sizeof(nexpire));
577
578 pim_assert = 0;
579
580 timeout(expire_upcalls, (caddr_t)NULL, EXPIRE_TIMEOUT);
581
582 if (mrtdebug)
583 log(LOG_DEBUG, "ip_mrouter_init\n");
584
585 return 0;
586}
587
588/*
589 * Disable multicast routing
590 */
591static int
2d21ac55 592X_ip_mrouter_done(void)
1c79356b
A
593{
594 vifi_t vifi;
595 int i;
596 struct ifnet *ifp;
597 struct ifreq ifr;
598 struct mfc *rt;
599 struct rtdetq *rte;
1c79356b
A
600
601 /*
602 * For each phyint in use, disable promiscuous reception of all IP
603 * multicasts.
604 */
605 for (vifi = 0; vifi < numvifs; vifi++) {
606 if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
607 !(viftable[vifi].v_flags & VIFF_TUNNEL)) {
608 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET;
609 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr.s_addr
610 = INADDR_ANY;
611 ifp = viftable[vifi].v_ifp;
612 if_allmulti(ifp, 0);
613 }
614 }
615 bzero((caddr_t)tbftable, sizeof(tbftable));
616 bzero((caddr_t)viftable, sizeof(viftable));
617 numvifs = 0;
618 pim_assert = 0;
619
620 untimeout(expire_upcalls, (caddr_t)NULL);
621
622 /*
623 * Free all multicast forwarding cache entries.
624 */
2d21ac55 625 for (i = 0; i < CONFIG_MFCTBLSIZ; i++) {
1c79356b
A
626 for (rt = mfctable[i]; rt != NULL; ) {
627 struct mfc *nr = rt->mfc_next;
628
629 for (rte = rt->mfc_stall; rte != NULL; ) {
630 struct rtdetq *n = rte->next;
631
632 m_freem(rte->m);
633 FREE(rte, M_MRTABLE);
634 rte = n;
635 }
636 FREE(rt, M_MRTABLE);
637 rt = nr;
638 }
639 }
640
641 bzero((caddr_t)mfctable, sizeof(mfctable));
642
643 /*
644 * Reset de-encapsulation cache
645 */
646 last_encap_src = 0;
647 last_encap_vif = NULL;
648 have_encap_tunnel = 0;
649
650 ip_mrouter = NULL;
651
1c79356b
A
652 if (mrtdebug)
653 log(LOG_DEBUG, "ip_mrouter_done\n");
654
655 return 0;
656}
657
9bccf70c 658#if !defined(MROUTE_LKM) || !MROUTE_LKM
1c79356b
A
659int (*ip_mrouter_done)(void) = X_ip_mrouter_done;
660#endif
661
662/*
663 * Set PIM assert processing global
664 */
665static int
2d21ac55 666set_assert(int i)
1c79356b
A
667{
668 if ((i != 1) && (i != 0))
669 return EINVAL;
670
671 pim_assert = i;
672
673 return 0;
674}
675
676/*
677 * Add a vif to the vif table
678 */
679static int
2d21ac55 680add_vif(struct vifctl *vifcp)
1c79356b 681{
2d21ac55
A
682 struct vif *vifp = viftable + vifcp->vifc_vifi;
683 static struct sockaddr_in sin = { sizeof sin, AF_INET,
684 0 , {0}, {0,0,0,0,0,0,0,0,} };
1c79356b
A
685 struct ifaddr *ifa;
686 struct ifnet *ifp;
687 int error, s;
688 struct tbf *v_tbf = tbftable + vifcp->vifc_vifi;
689
2d21ac55 690 if (vifcp->vifc_vifi >= CONFIG_MAXVIFS) return EINVAL;
1c79356b
A
691 if (vifp->v_lcl_addr.s_addr != 0) return EADDRINUSE;
692
693 /* Find the interface with an address in AF_INET family */
694 sin.sin_addr = vifcp->vifc_lcl_addr;
695 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
696 if (ifa == 0) return EADDRNOTAVAIL;
697 ifp = ifa->ifa_ifp;
6d2010ae 698 IFA_REMREF(ifa);
91447636 699 ifa = NULL;
1c79356b
A
700
701 if (vifcp->vifc_flags & VIFF_TUNNEL) {
702 if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
703 /*
704 * An encapsulating tunnel is wanted. Tell ipip_input() to
705 * start paying attention to encapsulated packets.
706 */
707 if (have_encap_tunnel == 0) {
708 have_encap_tunnel = 1;
2d21ac55 709 for (s = 0; s < CONFIG_MAXVIFS; ++s) {
1c79356b
A
710 multicast_decap_if[s].if_name = "mdecap";
711 multicast_decap_if[s].if_unit = s;
712 multicast_decap_if[s].if_family = APPLE_IF_FAM_MDECAP;
713 }
714 }
715 /*
716 * Set interface to fake encapsulator interface
717 */
718 ifp = &multicast_decap_if[vifcp->vifc_vifi];
1c79356b
A
719 } else {
720 log(LOG_ERR, "source routed tunnels not supported\n");
721 return EOPNOTSUPP;
722 }
723 } else {
724 /* Make sure the interface supports multicast */
725 if ((ifp->if_flags & IFF_MULTICAST) == 0)
726 return EOPNOTSUPP;
727
728 /* Enable promiscuous reception of all IP multicasts from the if */
1c79356b 729 error = if_allmulti(ifp, 1);
1c79356b
A
730 if (error)
731 return error;
732 }
733
1c79356b
A
734 /* define parameters for the tbf structure */
735 vifp->v_tbf = v_tbf;
736 GET_TIME(vifp->v_tbf->tbf_last_pkt_t);
737 vifp->v_tbf->tbf_n_tok = 0;
738 vifp->v_tbf->tbf_q_len = 0;
739 vifp->v_tbf->tbf_max_q_len = MAXQSIZE;
740 vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL;
741
742 vifp->v_flags = vifcp->vifc_flags;
743 vifp->v_threshold = vifcp->vifc_threshold;
744 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
745 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
746 vifp->v_ifp = ifp;
747 /* scaling up here allows division by 1024 in critical code */
748 vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000;
749 vifp->v_rsvp_on = 0;
750 vifp->v_rsvpd = NULL;
751 /* initialize per vif pkt counters */
752 vifp->v_pkt_in = 0;
753 vifp->v_pkt_out = 0;
754 vifp->v_bytes_in = 0;
755 vifp->v_bytes_out = 0;
1c79356b
A
756
757 /* Adjust numvifs up if the vifi is higher than numvifs */
758 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
759
760 if (mrtdebug)
761 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
762 vifcp->vifc_vifi,
b0d623f7 763 (u_int32_t)ntohl(vifcp->vifc_lcl_addr.s_addr),
1c79356b 764 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
b0d623f7 765 (u_int32_t)ntohl(vifcp->vifc_rmt_addr.s_addr),
1c79356b
A
766 vifcp->vifc_threshold,
767 vifcp->vifc_rate_limit);
768
769 return 0;
770}
771
772/*
773 * Delete a vif from the vif table
774 */
775static int
2d21ac55 776del_vif(vifi_t vifi)
1c79356b 777{
2d21ac55
A
778 struct vif *vifp = &viftable[vifi];
779 struct mbuf *m;
1c79356b
A
780 struct ifnet *ifp;
781 struct ifreq ifr;
1c79356b
A
782
783 if (vifi >= numvifs) return EINVAL;
784 if (vifp->v_lcl_addr.s_addr == 0) return EADDRNOTAVAIL;
785
1c79356b
A
786 if (!(vifp->v_flags & VIFF_TUNNEL)) {
787 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET;
788 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr.s_addr = INADDR_ANY;
789 ifp = vifp->v_ifp;
790 if_allmulti(ifp, 0);
791 }
792
793 if (vifp == last_encap_vif) {
794 last_encap_vif = 0;
795 last_encap_src = 0;
796 }
797
798 /*
799 * Free packets queued at the interface
800 */
801 while (vifp->v_tbf->tbf_q) {
802 m = vifp->v_tbf->tbf_q;
803 vifp->v_tbf->tbf_q = m->m_act;
804 m_freem(m);
805 }
806
807 bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf)));
808 bzero((caddr_t)vifp, sizeof (*vifp));
809
810 if (mrtdebug)
811 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
812
813 /* Adjust numvifs down */
814 for (vifi = numvifs; vifi > 0; vifi--)
815 if (viftable[vifi-1].v_lcl_addr.s_addr != 0) break;
816 numvifs = vifi;
817
1c79356b
A
818 return 0;
819}
820
821/*
822 * Add an mfc entry
823 */
824static int
2d21ac55 825add_mfc(struct mfcctl *mfccp)
1c79356b
A
826{
827 struct mfc *rt;
b0d623f7 828 u_int32_t hash;
1c79356b 829 struct rtdetq *rte;
2d21ac55 830 u_short nstl;
1c79356b
A
831 int i;
832
833 MFCFIND(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr, rt);
834
835 /* If an entry already exists, just update the fields */
836 if (rt) {
837 if (mrtdebug & DEBUG_MFC)
838 log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
b0d623f7
A
839 (u_int32_t)ntohl(mfccp->mfcc_origin.s_addr),
840 (u_int32_t)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1c79356b
A
841 mfccp->mfcc_parent);
842
1c79356b
A
843 rt->mfc_parent = mfccp->mfcc_parent;
844 for (i = 0; i < numvifs; i++)
845 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1c79356b
A
846 return 0;
847 }
848
849 /*
850 * Find the entry for which the upcall was made and update
851 */
1c79356b
A
852 hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
853 for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
854
855 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
856 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
857 (rt->mfc_stall != NULL)) {
858
859 if (nstl++)
860 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
861 "multiple kernel entries",
b0d623f7
A
862 (u_int32_t)ntohl(mfccp->mfcc_origin.s_addr),
863 (u_int32_t)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1c79356b
A
864 mfccp->mfcc_parent, (void *)rt->mfc_stall);
865
866 if (mrtdebug & DEBUG_MFC)
867 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
b0d623f7
A
868 (u_int32_t)ntohl(mfccp->mfcc_origin.s_addr),
869 (u_int32_t)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1c79356b
A
870 mfccp->mfcc_parent, (void *)rt->mfc_stall);
871
872 rt->mfc_origin = mfccp->mfcc_origin;
873 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
874 rt->mfc_parent = mfccp->mfcc_parent;
875 for (i = 0; i < numvifs; i++)
876 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
877 /* initialize pkt counters per src-grp */
878 rt->mfc_pkt_cnt = 0;
879 rt->mfc_byte_cnt = 0;
880 rt->mfc_wrong_if = 0;
881 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
882
883 rt->mfc_expire = 0; /* Don't clean this guy up */
884 nexpire[hash]--;
885
886 /* free packets Qed at the end of this entry */
887 for (rte = rt->mfc_stall; rte != NULL; ) {
888 struct rtdetq *n = rte->next;
889
890 ip_mdq(rte->m, rte->ifp, rt, -1);
891 m_freem(rte->m);
892#if UPCALL_TIMING
893 collate(&(rte->t));
894#endif /* UPCALL_TIMING */
895 FREE(rte, M_MRTABLE);
896 rte = n;
897 }
898 rt->mfc_stall = NULL;
899 }
900 }
901
902 /*
903 * It is possible that an entry is being inserted without an upcall
904 */
905 if (nstl == 0) {
906 if (mrtdebug & DEBUG_MFC)
907 log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
b0d623f7
A
908 hash, (u_int32_t)ntohl(mfccp->mfcc_origin.s_addr),
909 (u_int32_t)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1c79356b
A
910 mfccp->mfcc_parent);
911
912 for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
913
914 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
915 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
916
917 rt->mfc_origin = mfccp->mfcc_origin;
918 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
919 rt->mfc_parent = mfccp->mfcc_parent;
920 for (i = 0; i < numvifs; i++)
921 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
922 /* initialize pkt counters per src-grp */
923 rt->mfc_pkt_cnt = 0;
924 rt->mfc_byte_cnt = 0;
925 rt->mfc_wrong_if = 0;
926 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
927 if (rt->mfc_expire)
928 nexpire[hash]--;
929 rt->mfc_expire = 0;
930 }
931 }
932 if (rt == NULL) {
933 /* no upcall, so make a new entry */
934 rt = (struct mfc *) _MALLOC(sizeof(*rt), M_MRTABLE, M_NOWAIT);
935 if (rt == NULL) {
1c79356b
A
936 return ENOBUFS;
937 }
938
939 /* insert new entry at head of hash chain */
940 rt->mfc_origin = mfccp->mfcc_origin;
941 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
942 rt->mfc_parent = mfccp->mfcc_parent;
943 for (i = 0; i < numvifs; i++)
944 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
945 /* initialize pkt counters per src-grp */
946 rt->mfc_pkt_cnt = 0;
947 rt->mfc_byte_cnt = 0;
948 rt->mfc_wrong_if = 0;
949 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
950 rt->mfc_expire = 0;
951 rt->mfc_stall = NULL;
952
953 /* link into table */
954 rt->mfc_next = mfctable[hash];
955 mfctable[hash] = rt;
956 }
957 }
1c79356b
A
958 return 0;
959}
960
961#if UPCALL_TIMING
962/*
963 * collect delay statistics on the upcalls
964 */
2d21ac55
A
965static void
966collate(struct timeval *t)
1c79356b 967{
b0d623f7 968 u_int32_t d;
2d21ac55 969 struct timeval tp;
b0d623f7 970 u_int32_t delta;
1c79356b
A
971
972 GET_TIME(tp);
973
974 if (TV_LT(*t, tp))
975 {
976 TV_DELTA(tp, *t, delta);
977
978 d = delta >> 10;
979 if (d > 50)
980 d = 50;
981
982 ++upcall_data[d];
983 }
984}
985#endif /* UPCALL_TIMING */
986
987/*
988 * Delete an mfc entry
989 */
990static int
2d21ac55 991del_mfc(struct mfcctl *mfccp)
1c79356b
A
992{
993 struct in_addr origin;
994 struct in_addr mcastgrp;
995 struct mfc *rt;
996 struct mfc **nptr;
b0d623f7 997 u_int32_t hash;
1c79356b
A
998
999 origin = mfccp->mfcc_origin;
1000 mcastgrp = mfccp->mfcc_mcastgrp;
1001 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1002
1003 if (mrtdebug & DEBUG_MFC)
1004 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
b0d623f7 1005 (u_int32_t)ntohl(origin.s_addr), (u_int32_t)ntohl(mcastgrp.s_addr));
1c79356b 1006
1c79356b
A
1007 nptr = &mfctable[hash];
1008 while ((rt = *nptr) != NULL) {
1009 if (origin.s_addr == rt->mfc_origin.s_addr &&
1010 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1011 rt->mfc_stall == NULL)
1012 break;
1013
1014 nptr = &rt->mfc_next;
1015 }
1016 if (rt == NULL) {
1c79356b
A
1017 return EADDRNOTAVAIL;
1018 }
1019
1020 *nptr = rt->mfc_next;
1021 FREE(rt, M_MRTABLE);
1022
1c79356b
A
1023 return 0;
1024}
1025
1026/*
1027 * Send a message to mrouted on the multicast routing socket
1028 */
1029static int
2d21ac55 1030socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
1c79356b 1031{
91447636 1032 socket_lock(s, 1);
1c79356b
A
1033 if (s) {
1034 if (sbappendaddr(&s->so_rcv,
1035 (struct sockaddr *)src,
91447636 1036 mm, (struct mbuf *)0, NULL) != 0) {
1c79356b 1037 sorwakeup(s);
91447636 1038 socket_unlock(s, 1);
1c79356b
A
1039 return 0;
1040 }
1041 }
91447636 1042 socket_unlock(s, 1);
1c79356b
A
1043 m_freem(mm);
1044 return -1;
1045}
1046
1047/*
1048 * IP multicast forwarding function. This function assumes that the packet
1049 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1050 * pointed to by "ifp", and the packet is to be relayed to other networks
1051 * that have members of the packet's destination IP multicast group.
1052 *
1053 * The packet is returned unscathed to the caller, unless it is
1054 * erroneous, in which case a non-zero return value tells the caller to
1055 * discard it.
1056 */
1057
1058#define IP_HDR_LEN 20 /* # bytes of fixed IP header (excluding options) */
1059#define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1060
1061static int
2d21ac55
A
1062X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
1063 struct ip_moptions *imo)
1c79356b 1064{
2d21ac55
A
1065 struct mfc *rt;
1066 u_char *ipoptions;
1067 static struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET,
1068 0 , {0}, {0,0,0,0,0,0,0,0,} };
1c79356b 1069 static int srctun = 0;
2d21ac55 1070 struct mbuf *mm;
1c79356b
A
1071 vifi_t vifi;
1072 struct vif *vifp;
1073
1074 if (mrtdebug & DEBUG_FORWARD)
1075 log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
b0d623f7 1076 (u_int32_t)ntohl(ip->ip_src.s_addr), (u_int32_t)ntohl(ip->ip_dst.s_addr),
1c79356b
A
1077 (void *)ifp);
1078
1079 if (ip->ip_hl < (IP_HDR_LEN + TUNNEL_LEN) >> 2 ||
1080 (ipoptions = (u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1081 /*
1082 * Packet arrived via a physical interface or
1083 * an encapsulated tunnel.
1084 */
1085 } else {
1086 /*
1087 * Packet arrived through a source-route tunnel.
1088 * Source-route tunnels are no longer supported.
1089 */
1090 if ((srctun++ % 1000) == 0)
1091 log(LOG_ERR,
1092 "ip_mforward: received source-routed packet from %lx\n",
b0d623f7 1093 (u_int32_t)ntohl(ip->ip_src.s_addr));
1c79356b
A
1094
1095 return 1;
1096 }
1097
6d2010ae
A
1098 if (imo != NULL)
1099 IMO_LOCK(imo);
1c79356b 1100 if ((imo) && ((vifi = imo->imo_multicast_vif) < numvifs)) {
6d2010ae 1101 IMO_UNLOCK(imo);
1c79356b
A
1102 if (ip->ip_ttl < 255)
1103 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1104 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1105 vifp = viftable + vifi;
39236c6e 1106 printf("Sending IPPROTO_RSVP from %x to %x on vif %d (%s%s)\n",
1c79356b
A
1107 ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr), vifi,
1108 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
39236c6e 1109 if_name(vifp->v_ifp));
1c79356b
A
1110 }
1111 return (ip_mdq(m, ifp, NULL, vifi));
6d2010ae
A
1112 } else if (imo != NULL) {
1113 IMO_UNLOCK(imo);
1c79356b
A
1114 }
1115 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
2d21ac55 1116 printf("Warning: IPPROTO_RSVP from %x to %x without vif option\n",
1c79356b
A
1117 ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr));
1118 if(!imo)
1119 printf("In fact, no options were specified at all\n");
1120 }
1121
1122 /*
1123 * Don't forward a packet with time-to-live of zero or one,
1124 * or a packet destined to a local-only group.
1125 */
1126 if (ip->ip_ttl <= 1 ||
1127 ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP)
1128 return 0;
1129
1130 /*
1131 * Determine forwarding vifs from the forwarding cache table
1132 */
1c79356b
A
1133 MFCFIND(ip->ip_src.s_addr, ip->ip_dst.s_addr, rt);
1134
1135 /* Entry exists, so forward if necessary */
1136 if (rt != NULL) {
1c79356b
A
1137 return (ip_mdq(m, ifp, rt, -1));
1138 } else {
1139 /*
1140 * If we don't have a route for packet's origin,
1141 * Make a copy of the packet &
1142 * send message to routing daemon
1143 */
1144
2d21ac55
A
1145 struct mbuf *mb0;
1146 struct rtdetq *rte;
b0d623f7 1147 u_int32_t hash;
1c79356b
A
1148 int hlen = ip->ip_hl << 2;
1149#if UPCALL_TIMING
1150 struct timeval tp;
1151
1152 GET_TIME(tp);
1153#endif
1154
1155 mrtstat.mrts_no_route++;
1156 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1157 log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
b0d623f7
A
1158 (u_int32_t)ntohl(ip->ip_src.s_addr),
1159 (u_int32_t)ntohl(ip->ip_dst.s_addr));
1c79356b
A
1160
1161 /*
1162 * Allocate mbufs early so that we don't do extra work if we are
1163 * just going to fail anyway. Make sure to pullup the header so
1164 * that other people can't step on it.
1165 */
1166 rte = (struct rtdetq *) _MALLOC((sizeof *rte), M_MRTABLE, M_NOWAIT);
1167 if (rte == NULL) {
1c79356b
A
1168 return ENOBUFS;
1169 }
1170 mb0 = m_copy(m, 0, M_COPYALL);
1171 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1172 mb0 = m_pullup(mb0, hlen);
1173 if (mb0 == NULL) {
1174 FREE(rte, M_MRTABLE);
1c79356b
A
1175 return ENOBUFS;
1176 }
1177
1178 /* is there an upcall waiting for this packet? */
1179 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1180 for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1181 if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1182 (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1183 (rt->mfc_stall != NULL))
1184 break;
1185 }
1186
1187 if (rt == NULL) {
1188 int i;
1189 struct igmpmsg *im;
1190
1191 /* no upcall, so make a new entry */
1192 rt = (struct mfc *) _MALLOC(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1193 if (rt == NULL) {
1194 FREE(rte, M_MRTABLE);
1195 m_freem(mb0);
1c79356b
A
1196 return ENOBUFS;
1197 }
1198 /* Make a copy of the header to send to the user level process */
1199 mm = m_copy(mb0, 0, hlen);
1200 if (mm == NULL) {
1201 FREE(rte, M_MRTABLE);
1202 m_freem(mb0);
1203 FREE(rt, M_MRTABLE);
1c79356b
A
1204 return ENOBUFS;
1205 }
1206
1207 /*
1208 * Send message to routing daemon to install
1209 * a route into the kernel table
1210 */
1211 k_igmpsrc.sin_addr = ip->ip_src;
1212
1213 im = mtod(mm, struct igmpmsg *);
1214 im->im_msgtype = IGMPMSG_NOCACHE;
1215 im->im_mbz = 0;
1216
1217 mrtstat.mrts_upcalls++;
1218
1219 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1220 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1221 ++mrtstat.mrts_upq_sockfull;
1222 FREE(rte, M_MRTABLE);
1223 m_freem(mb0);
1224 FREE(rt, M_MRTABLE);
1c79356b
A
1225 return ENOBUFS;
1226 }
1227
1228 /* insert new entry at head of hash chain */
1229 rt->mfc_origin.s_addr = ip->ip_src.s_addr;
1230 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr;
1231 rt->mfc_expire = UPCALL_EXPIRE;
1232 nexpire[hash]++;
1233 for (i = 0; i < numvifs; i++)
1234 rt->mfc_ttls[i] = 0;
1235 rt->mfc_parent = -1;
1236
1237 /* link into table */
1238 rt->mfc_next = mfctable[hash];
1239 mfctable[hash] = rt;
1240 rt->mfc_stall = rte;
1241
1242 } else {
1243 /* determine if q has overflowed */
1244 int npkts = 0;
1245 struct rtdetq **p;
1246
1247 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1248 npkts++;
1249
1250 if (npkts > MAX_UPQ) {
1251 mrtstat.mrts_upq_ovflw++;
1252 FREE(rte, M_MRTABLE);
1253 m_freem(mb0);
1c79356b
A
1254 return 0;
1255 }
1256
1257 /* Add this entry to the end of the queue */
1258 *p = rte;
1259 }
1260
1261 rte->m = mb0;
1262 rte->ifp = ifp;
1263#if UPCALL_TIMING
1264 rte->t = tp;
1265#endif
1266 rte->next = NULL;
1267
1c79356b
A
1268 return 0;
1269 }
1270}
1271
9bccf70c 1272#if !defined(MROUTE_LKM) || !MROUTE_LKM
1c79356b
A
1273int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
1274 struct ip_moptions *) = X_ip_mforward;
1275#endif
1276
1277/*
1278 * Clean up the cache entry if upcall is not serviced
1279 */
1280static void
2d21ac55 1281expire_upcalls(__unused void *unused)
1c79356b
A
1282{
1283 struct rtdetq *rte;
1284 struct mfc *mfc, **nptr;
1285 int i;
1c79356b 1286
2d21ac55 1287 for (i = 0; i < CONFIG_MFCTBLSIZ; i++) {
1c79356b
A
1288 if (nexpire[i] == 0)
1289 continue;
1290 nptr = &mfctable[i];
1291 for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1292 /*
1293 * Skip real cache entries
1294 * Make sure it wasn't marked to not expire (shouldn't happen)
1295 * If it expires now
1296 */
1297 if (mfc->mfc_stall != NULL &&
1298 mfc->mfc_expire != 0 &&
1299 --mfc->mfc_expire == 0) {
1300 if (mrtdebug & DEBUG_EXPIRE)
1301 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
b0d623f7
A
1302 (u_int32_t)ntohl(mfc->mfc_origin.s_addr),
1303 (u_int32_t)ntohl(mfc->mfc_mcastgrp.s_addr));
1c79356b
A
1304 /*
1305 * drop all the packets
1306 * free the mbuf with the pkt, if, timing info
1307 */
1308 for (rte = mfc->mfc_stall; rte; ) {
1309 struct rtdetq *n = rte->next;
1310
1311 m_freem(rte->m);
1312 FREE(rte, M_MRTABLE);
1313 rte = n;
1314 }
1315 ++mrtstat.mrts_cache_cleanups;
1316 nexpire[i]--;
1317
1318 *nptr = mfc->mfc_next;
1319 FREE(mfc, M_MRTABLE);
1320 } else {
1321 nptr = &mfc->mfc_next;
1322 }
1323 }
1324 }
1c79356b 1325 timeout(expire_upcalls, (caddr_t)NULL, EXPIRE_TIMEOUT);
1c79356b
A
1326}
1327
1328/*
1329 * Packet forwarding routine once entry in the cache is made
1330 */
1331static int
2d21ac55
A
1332ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt,
1333 vifi_t xmt_vif)
1c79356b 1334{
2d21ac55
A
1335 struct ip *ip = mtod(m, struct ip *);
1336 vifi_t vifi;
1337 struct vif *vifp;
1338 int plen = ip->ip_len;
1c79356b
A
1339
1340/*
1341 * Macro to send packet on vif. Since RSVP packets don't get counted on
1342 * input, they shouldn't get counted on output, so statistics keeping is
1343 * seperate.
1344 */
1345#define MC_SEND(ip,vifp,m) { \
1346 if ((vifp)->v_flags & VIFF_TUNNEL) \
1347 encap_send((ip), (vifp), (m)); \
1348 else \
1349 phyint_send((ip), (vifp), (m)); \
1350}
1351
1352 /*
1353 * If xmt_vif is not -1, send on only the requested vif.
1354 *
1355 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1356 */
1357 if (xmt_vif < numvifs) {
1358 MC_SEND(ip, viftable + xmt_vif, m);
1359 return 1;
1360 }
1361
1362 /*
1363 * Don't forward if it didn't arrive from the parent vif for its origin.
1364 */
1365 vifi = rt->mfc_parent;
1366 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1367 /* came in the wrong interface */
1368 if (mrtdebug & DEBUG_FORWARD)
1369 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1370 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1371 ++mrtstat.mrts_wrong_if;
1372 ++rt->mfc_wrong_if;
1373 /*
1374 * If we are doing PIM assert processing, and we are forwarding
1375 * packets on this interface, and it is a broadcast medium
1376 * interface (and not a tunnel), send a message to the routing daemon.
1377 */
1378 if (pim_assert && rt->mfc_ttls[vifi] &&
1379 (ifp->if_flags & IFF_BROADCAST) &&
1380 !(viftable[vifi].v_flags & VIFF_TUNNEL)) {
1381 struct sockaddr_in k_igmpsrc;
1382 struct mbuf *mm;
1383 struct igmpmsg *im;
1384 int hlen = ip->ip_hl << 2;
1385 struct timeval now;
b0d623f7 1386 u_int32_t delta;
1c79356b
A
1387
1388 GET_TIME(now);
1389
1390 TV_DELTA(rt->mfc_last_assert, now, delta);
1391
1392 if (delta > ASSERT_MSG_TIME) {
1393 mm = m_copy(m, 0, hlen);
1394 if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1395 mm = m_pullup(mm, hlen);
1396 if (mm == NULL) {
1397 return ENOBUFS;
1398 }
1399
1400 rt->mfc_last_assert = now;
1401
1402 im = mtod(mm, struct igmpmsg *);
1403 im->im_msgtype = IGMPMSG_WRONGVIF;
1404 im->im_mbz = 0;
1405 im->im_vif = vifi;
1406
1407 k_igmpsrc.sin_addr = im->im_src;
1408
1409 socket_send(ip_mrouter, mm, &k_igmpsrc);
1410 }
1411 }
1412 return 0;
1413 }
1414
1415 /* If I sourced this packet, it counts as output, else it was input. */
1416 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1417 viftable[vifi].v_pkt_out++;
1418 viftable[vifi].v_bytes_out += plen;
1419 } else {
1420 viftable[vifi].v_pkt_in++;
1421 viftable[vifi].v_bytes_in += plen;
1422 }
1423 rt->mfc_pkt_cnt++;
1424 rt->mfc_byte_cnt += plen;
1425
1426 /*
1427 * For each vif, decide if a copy of the packet should be forwarded.
1428 * Forward if:
1429 * - the ttl exceeds the vif's threshold
1430 * - there are group members downstream on interface
1431 */
1432 for (vifp = viftable, vifi = 0; vifi < numvifs; vifp++, vifi++)
1433 if ((rt->mfc_ttls[vifi] > 0) &&
1434 (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1435 vifp->v_pkt_out++;
1436 vifp->v_bytes_out += plen;
1437 MC_SEND(ip, vifp, m);
1438 }
1439
1440 return 0;
1441}
1442
1443/*
1444 * check if a vif number is legal/ok. This is used by ip_output, to export
1445 * numvifs there,
1446 */
1447static int
2d21ac55 1448X_legal_vif_num(int vif)
1c79356b
A
1449{
1450 if (vif >= 0 && vif < numvifs)
1451 return(1);
1452 else
1453 return(0);
1454}
1455
9bccf70c 1456#if !defined(MROUTE_LKM) || !MROUTE_LKM
1c79356b
A
1457int (*legal_vif_num)(int) = X_legal_vif_num;
1458#endif
1459
1460/*
1461 * Return the local address used by this vif
1462 */
b0d623f7 1463static u_int32_t
2d21ac55 1464X_ip_mcast_src(int vifi)
1c79356b
A
1465{
1466 if (vifi >= 0 && vifi < numvifs)
1467 return viftable[vifi].v_lcl_addr.s_addr;
1468 else
1469 return INADDR_ANY;
1470}
1471
9bccf70c 1472#if !defined(MROUTE_LKM) || !MROUTE_LKM
b0d623f7 1473u_int32_t (*ip_mcast_src)(int) = X_ip_mcast_src;
1c79356b
A
1474#endif
1475
1476static void
2d21ac55 1477phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1c79356b 1478{
2d21ac55
A
1479 struct mbuf *mb_copy;
1480 int hlen = ip->ip_hl << 2;
1c79356b
A
1481
1482 /*
1483 * Make a new reference to the packet; make sure that
1484 * the IP header is actually copied, not just referenced,
1485 * so that ip_output() only scribbles on the copy.
1486 */
1487 mb_copy = m_copy(m, 0, M_COPYALL);
1488 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1489 mb_copy = m_pullup(mb_copy, hlen);
1490 if (mb_copy == NULL)
1491 return;
1492
1493 if (vifp->v_rate_limit == 0)
1494 tbf_send_packet(vifp, mb_copy);
1495 else
1496 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len);
1497}
1498
1499static void
2d21ac55 1500encap_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1c79356b 1501{
2d21ac55
A
1502 struct mbuf *mb_copy;
1503 struct ip *ip_copy;
1504 int i, len = ip->ip_len;
1c79356b
A
1505
1506 /*
1507 * copy the old packet & pullup its IP header into the
1508 * new mbuf so we can modify it. Try to fill the new
1509 * mbuf since if we don't the ethernet driver will.
1510 */
1511 MGETHDR(mb_copy, M_DONTWAIT, MT_HEADER);
1512 if (mb_copy == NULL)
1513 return;
2d21ac55
A
1514#if CONFIG_MACF_NET
1515 mac_mbuf_label_associate_multicast_encap(m, vifp->v_ifp, mb_copy);
1516#endif
1c79356b
A
1517 mb_copy->m_data += max_linkhdr;
1518 mb_copy->m_len = sizeof(multicast_encap_iphdr);
1519
1520 if ((mb_copy->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1521 m_freem(mb_copy);
1522 return;
1523 }
1524 i = MHLEN - M_LEADINGSPACE(mb_copy);
1525 if (i > len)
1526 i = len;
1527 mb_copy = m_pullup(mb_copy, i);
1528 if (mb_copy == NULL)
1529 return;
1530 mb_copy->m_pkthdr.len = len + sizeof(multicast_encap_iphdr);
1531
1532 /*
1533 * fill in the encapsulating IP header.
1534 */
1535 ip_copy = mtod(mb_copy, struct ip *);
1536 *ip_copy = multicast_encap_iphdr;
9bccf70c 1537 ip_copy->ip_id = ip_randomid();
1c79356b
A
1538 ip_copy->ip_len += len;
1539 ip_copy->ip_src = vifp->v_lcl_addr;
1540 ip_copy->ip_dst = vifp->v_rmt_addr;
1541
1542 /*
1543 * turn the encapsulated IP header back into a valid one.
1544 */
1545 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
1546 --ip->ip_ttl;
b0d623f7
A
1547
1548#if BYTE_ORDER != BIG_ENDIAN
1c79356b
A
1549 HTONS(ip->ip_len);
1550 HTONS(ip->ip_off);
b0d623f7
A
1551#endif
1552
1c79356b
A
1553 ip->ip_sum = 0;
1554 mb_copy->m_data += sizeof(multicast_encap_iphdr);
1555 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
1556 mb_copy->m_data -= sizeof(multicast_encap_iphdr);
1557
1558 if (vifp->v_rate_limit == 0)
1559 tbf_send_packet(vifp, mb_copy);
1560 else
1561 tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
1562}
1563
1564/*
1565 * De-encapsulate a packet and feed it back through ip input (this
1566 * routine is called whenever IP gets a packet with proto type
1567 * ENCAP_PROTO and a local destination address).
1568 */
1569void
1570#if MROUTE_LKM
2d21ac55 1571X_ipip_input(struct mbuf *m, int iphlen)
1c79356b 1572#else
2d21ac55 1573ipip_input(struct mbuf *m, int iphlen)
1c79356b 1574#endif
1c79356b
A
1575{
1576 struct ifnet *ifp = m->m_pkthdr.rcvif;
2d21ac55
A
1577 struct ip *ip = mtod(m, struct ip *);
1578 int hlen = ip->ip_hl << 2;
1579 struct vif *vifp;
1c79356b
A
1580
1581 if (!have_encap_tunnel) {
1582 rip_input(m, iphlen);
1583 return;
1584 }
1585 /*
1586 * dump the packet if it's not to a multicast destination or if
1587 * we don't have an encapsulating tunnel with the source.
1588 * Note: This code assumes that the remote site IP address
1589 * uniquely identifies the tunnel (i.e., that this site has
1590 * at most one tunnel with the remote site).
1591 */
1592 if (! IN_MULTICAST(ntohl(((struct ip *)((char *)ip + hlen))->ip_dst.s_addr))) {
1593 ++mrtstat.mrts_bad_tunnel;
1594 m_freem(m);
1595 return;
1596 }
1597 if (ip->ip_src.s_addr != last_encap_src) {
2d21ac55 1598 struct vif *vife;
1c79356b
A
1599
1600 vifp = viftable;
1601 vife = vifp + numvifs;
1602 last_encap_src = ip->ip_src.s_addr;
1603 last_encap_vif = 0;
1604 for ( ; vifp < vife; ++vifp)
1605 if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
1606 if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT))
1607 == VIFF_TUNNEL)
1608 last_encap_vif = vifp;
1609 break;
1610 }
1611 }
1612 if ((vifp = last_encap_vif) == 0) {
1613 last_encap_src = 0;
1614 mrtstat.mrts_cant_tunnel++; /*XXX*/
1615 m_freem(m);
1616 if (mrtdebug)
1617 log(LOG_DEBUG, "ip_mforward: no tunnel with %lx\n",
b0d623f7 1618 (u_int32_t)ntohl(ip->ip_src.s_addr));
1c79356b
A
1619 return;
1620 }
1621 ifp = vifp->v_ifp;
1622
1623 if (hlen > IP_HDR_LEN)
1624 ip_stripoptions(m, (struct mbuf *) 0);
1625 m->m_data += IP_HDR_LEN;
1626 m->m_len -= IP_HDR_LEN;
1627 m->m_pkthdr.len -= IP_HDR_LEN;
1628 m->m_pkthdr.rcvif = ifp;
91447636
A
1629
1630 proto_inject(PF_INET, m);
1c79356b
A
1631}
1632
1633/*
1634 * Token bucket filter module
1635 */
1636
1637static void
2d21ac55 1638tbf_control(struct vif *vifp, struct mbuf *m, struct ip *ip,
b0d623f7 1639 u_int32_t p_len)
1c79356b 1640{
2d21ac55 1641 struct tbf *t = vifp->v_tbf;
1c79356b
A
1642
1643 if (p_len > MAX_BKT_SIZE) {
1644 /* drop if packet is too large */
1645 mrtstat.mrts_pkt2large++;
1646 m_freem(m);
1647 return;
1648 }
1649
1650 tbf_update_tokens(vifp);
1651
1652 /* if there are enough tokens,
1653 * and the queue is empty,
1654 * send this packet out
1655 */
1656
1657 if (t->tbf_q_len == 0) {
1658 /* queue empty, send packet if enough tokens */
1659 if (p_len <= t->tbf_n_tok) {
1660 t->tbf_n_tok -= p_len;
1661 tbf_send_packet(vifp, m);
1662 } else {
1663 /* queue packet and timeout till later */
1664 tbf_queue(vifp, m);
1665 timeout(tbf_reprocess_q, (caddr_t)vifp, TBF_REPROCESS);
1666 }
1667 } else if (t->tbf_q_len < t->tbf_max_q_len) {
1668 /* finite queue length, so queue pkts and process queue */
1669 tbf_queue(vifp, m);
1670 tbf_process_q(vifp);
1671 } else {
1672 /* queue length too much, try to dq and queue and process */
1673 if (!tbf_dq_sel(vifp, ip)) {
1674 mrtstat.mrts_q_overflow++;
1675 m_freem(m);
1676 return;
1677 } else {
1678 tbf_queue(vifp, m);
1679 tbf_process_q(vifp);
1680 }
1681 }
1682 return;
1683}
1684
1685/*
1686 * adds a packet to the queue at the interface
1687 */
1688static void
2d21ac55 1689tbf_queue(struct vif *vifp, struct mbuf *m)
1c79356b 1690{
2d21ac55 1691 struct tbf *t = vifp->v_tbf;
1c79356b
A
1692
1693 if (t->tbf_t == NULL) {
1694 /* Queue was empty */
1695 t->tbf_q = m;
1696 } else {
1697 /* Insert at tail */
1698 t->tbf_t->m_act = m;
1699 }
1700
1701 /* Set new tail pointer */
1702 t->tbf_t = m;
1703
1704#if DIAGNOSTIC
1705 /* Make sure we didn't get fed a bogus mbuf */
1706 if (m->m_act)
1707 panic("tbf_queue: m_act");
1708#endif
1709 m->m_act = NULL;
1710
1711 t->tbf_q_len++;
1c79356b
A
1712}
1713
1714
1715/*
1716 * processes the queue at the interface
1717 */
1718static void
2d21ac55 1719tbf_process_q(struct vif *vifp)
1c79356b 1720{
2d21ac55
A
1721 struct mbuf *m;
1722 int len;
1723 struct tbf *t = vifp->v_tbf;
1c79356b
A
1724
1725 /* loop through the queue at the interface and send as many packets
1726 * as possible
1727 */
1728 while (t->tbf_q_len > 0) {
1729 m = t->tbf_q;
1730
1731 len = mtod(m, struct ip *)->ip_len;
1732
1733 /* determine if the packet can be sent */
1734 if (len <= t->tbf_n_tok) {
1735 /* if so,
1736 * reduce no of tokens, dequeue the packet,
1737 * send the packet.
1738 */
1739 t->tbf_n_tok -= len;
1740
1741 t->tbf_q = m->m_act;
1742 if (--t->tbf_q_len == 0)
1743 t->tbf_t = NULL;
1744
1745 m->m_act = NULL;
1746 tbf_send_packet(vifp, m);
1747
1748 } else break;
1749 }
1c79356b
A
1750}
1751
1752static void
2d21ac55 1753tbf_reprocess_q(void *xvifp)
1c79356b 1754{
2d21ac55 1755 struct vif *vifp = xvifp;
1c79356b 1756
1c79356b 1757 if (ip_mrouter == NULL) {
1c79356b
A
1758 return;
1759 }
1760
1761 tbf_update_tokens(vifp);
1762
1763 tbf_process_q(vifp);
1764
1765 if (vifp->v_tbf->tbf_q_len)
1766 timeout(tbf_reprocess_q, (caddr_t)vifp, TBF_REPROCESS);
1c79356b
A
1767}
1768
1769/* function that will selectively discard a member of the queue
1770 * based on the precedence value and the priority
1771 */
1772static int
2d21ac55 1773tbf_dq_sel(struct vif *vifp, struct ip *ip)
1c79356b 1774{
2d21ac55
A
1775 u_int p;
1776 struct mbuf *m, *last;
1777 struct mbuf **np;
1778 struct tbf *t = vifp->v_tbf;
1c79356b
A
1779
1780 p = priority(vifp, ip);
1781
1782 np = &t->tbf_q;
1783 last = NULL;
1784 while ((m = *np) != NULL) {
1785 if (p > priority(vifp, mtod(m, struct ip *))) {
1786 *np = m->m_act;
1787 /* If we're removing the last packet, fix the tail pointer */
1788 if (m == t->tbf_t)
1789 t->tbf_t = last;
1790 m_freem(m);
1791 /* it's impossible for the queue to be empty, but
1792 * we check anyway. */
1793 if (--t->tbf_q_len == 0)
1794 t->tbf_t = NULL;
1c79356b
A
1795 mrtstat.mrts_drop_sel++;
1796 return(1);
1797 }
1798 np = &m->m_act;
1799 last = m;
1800 }
1c79356b
A
1801 return(0);
1802}
1803
1804static void
2d21ac55 1805tbf_send_packet(struct vif *vifp, struct mbuf *m)
1c79356b 1806{
1c79356b 1807 int error;
39236c6e 1808 struct route ro;
1c79356b 1809
39236c6e 1810 bzero(&ro, sizeof (ro));
1c79356b
A
1811 if (vifp->v_flags & VIFF_TUNNEL) {
1812 /* If tunnel options */
39236c6e 1813 ip_output(m, (struct mbuf *)0, &ro,
2d21ac55 1814 IP_FORWARDING, (struct ip_moptions *)0, NULL);
1c79356b 1815 } else {
6d2010ae
A
1816 struct ip_moptions *imo;
1817
1818 imo = ip_allocmoptions(M_DONTWAIT);
1819 if (imo == NULL) {
1820 error = ENOMEM;
1821 goto done;
1822 }
1823
1824 imo->imo_multicast_ifp = vifp->v_ifp;
1825 imo->imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
1826 imo->imo_multicast_loop = 1;
1827 imo->imo_multicast_vif = -1;
1c79356b
A
1828
1829 /*
1830 * Re-entrancy should not be a problem here, because
1831 * the packets that we send out and are looped back at us
1832 * should get rejected because they appear to come from
1833 * the loopback interface, thus preventing looping.
1834 */
1835 error = ip_output(m, (struct mbuf *)0, &ro,
6d2010ae 1836 IP_FORWARDING, imo, NULL);
1c79356b 1837
6d2010ae
A
1838 IMO_REMREF(imo);
1839done:
1c79356b
A
1840 if (mrtdebug & DEBUG_XMIT)
1841 log(LOG_DEBUG, "phyint_send on vif %d err %d\n",
1842 vifp - viftable, error);
1843 }
39236c6e 1844 ROUTE_RELEASE(&ro);
1c79356b
A
1845}
1846
1847/* determine the current time and then
1848 * the elapsed time (between the last time and time now)
1849 * in milliseconds & update the no. of tokens in the bucket
1850 */
1851static void
2d21ac55 1852tbf_update_tokens(struct vif *vifp)
1c79356b
A
1853{
1854 struct timeval tp;
b0d623f7 1855 u_int32_t tm;
2d21ac55 1856 struct tbf *t = vifp->v_tbf;
1c79356b
A
1857
1858 GET_TIME(tp);
1859
1860 TV_DELTA(tp, t->tbf_last_pkt_t, tm);
1861
1862 /*
1863 * This formula is actually
1864 * "time in seconds" * "bytes/second".
1865 *
1866 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
1867 *
1868 * The (1000/1024) was introduced in add_vif to optimize
1869 * this divide into a shift.
1870 */
1871 t->tbf_n_tok += tm * vifp->v_rate_limit / 1024 / 8;
1872 t->tbf_last_pkt_t = tp;
1873
1874 if (t->tbf_n_tok > MAX_BKT_SIZE)
1875 t->tbf_n_tok = MAX_BKT_SIZE;
1c79356b
A
1876}
1877
1878static int
2d21ac55 1879priority(__unused struct vif *vifp, struct ip *ip)
1c79356b 1880{
2d21ac55 1881 int prio;
1c79356b
A
1882
1883 /* temporary hack; may add general packet classifier some day */
1884
1885 /*
1886 * The UDP port space is divided up into four priority ranges:
1887 * [0, 16384) : unclassified - lowest priority
1888 * [16384, 32768) : audio - highest priority
1889 * [32768, 49152) : whiteboard - medium priority
1890 * [49152, 65536) : video - low priority
1891 */
1892 if (ip->ip_p == IPPROTO_UDP) {
1893 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
1894 switch (ntohs(udp->uh_dport) & 0xc000) {
1895 case 0x4000:
1896 prio = 70;
1897 break;
1898 case 0x8000:
1899 prio = 60;
1900 break;
1901 case 0xc000:
1902 prio = 55;
1903 break;
1904 default:
1905 prio = 50;
1906 break;
1907 }
1908 if (tbfdebug > 1)
1909 log(LOG_DEBUG, "port %x prio%d\n", ntohs(udp->uh_dport), prio);
1910 } else {
1911 prio = 50;
1912 }
1913 return prio;
1914}
1915
1916/*
1917 * End of token bucket filter modifications
1918 */
1919
1920int
2d21ac55 1921ip_rsvp_vif_init(struct socket *so, struct sockopt *sopt)
1c79356b 1922{
2d21ac55 1923 int error, i;
1c79356b
A
1924
1925 if (rsvpdebug)
1926 printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
1927 so->so_type, so->so_proto->pr_protocol);
1928
1929 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
1930 return EOPNOTSUPP;
1931
1932 /* Check mbuf. */
1933 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1934 if (error)
1935 return (error);
1936
1937 if (rsvpdebug)
1938 printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n", i, rsvp_on);
1939
1c79356b
A
1940 /* Check vif. */
1941 if (!legal_vif_num(i)) {
1c79356b
A
1942 return EADDRNOTAVAIL;
1943 }
1944
1945 /* Check if socket is available. */
1946 if (viftable[i].v_rsvpd != NULL) {
1c79356b
A
1947 return EADDRINUSE;
1948 }
1949
1950 viftable[i].v_rsvpd = so;
1951 /* This may seem silly, but we need to be sure we don't over-increment
1952 * the RSVP counter, in case something slips up.
1953 */
1954 if (!viftable[i].v_rsvp_on) {
1955 viftable[i].v_rsvp_on = 1;
1956 rsvp_on++;
1957 }
1958
1c79356b
A
1959 return 0;
1960}
1961
1962int
2d21ac55 1963ip_rsvp_vif_done(struct socket *so, struct sockopt *sopt)
1c79356b 1964{
2d21ac55 1965 int error, i;
1c79356b
A
1966
1967 if (rsvpdebug)
1968 printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n",
1969 so->so_type, so->so_proto->pr_protocol);
1970
1971 if (so->so_type != SOCK_RAW ||
1972 so->so_proto->pr_protocol != IPPROTO_RSVP)
1973 return EOPNOTSUPP;
1974
1975 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1976 if (error)
1977 return (error);
2d21ac55 1978
1c79356b
A
1979 /* Check vif. */
1980 if (!legal_vif_num(i)) {
1c79356b
A
1981 return EADDRNOTAVAIL;
1982 }
1983
1984 if (rsvpdebug)
1985 printf("ip_rsvp_vif_done: v_rsvpd = %p so = %p\n",
1986 viftable[i].v_rsvpd, so);
1987
1988 viftable[i].v_rsvpd = NULL;
1989 /*
1990 * This may seem silly, but we need to be sure we don't over-decrement
1991 * the RSVP counter, in case something slips up.
1992 */
1993 if (viftable[i].v_rsvp_on) {
1994 viftable[i].v_rsvp_on = 0;
1995 rsvp_on--;
1996 }
1997
1c79356b
A
1998 return 0;
1999}
2000
2001void
2d21ac55 2002ip_rsvp_force_done(struct socket *so)
1c79356b
A
2003{
2004 int vifi;
1c79356b
A
2005
2006 /* Don't bother if it is not the right type of socket. */
2007 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2008 return;
2009
1c79356b
A
2010 /* The socket may be attached to more than one vif...this
2011 * is perfectly legal.
2012 */
2013 for (vifi = 0; vifi < numvifs; vifi++) {
2014 if (viftable[vifi].v_rsvpd == so) {
2015 viftable[vifi].v_rsvpd = NULL;
2016 /* This may seem silly, but we need to be sure we don't
2017 * over-decrement the RSVP counter, in case something slips up.
2018 */
2019 if (viftable[vifi].v_rsvp_on) {
2020 viftable[vifi].v_rsvp_on = 0;
2021 rsvp_on--;
2022 }
2023 }
2024 }
2025
1c79356b
A
2026 return;
2027}
2028
2029void
2d21ac55 2030rsvp_input(struct mbuf *m, int iphlen)
1c79356b
A
2031{
2032 int vifi;
2d21ac55
A
2033 struct ip *ip = mtod(m, struct ip *);
2034 static struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET,
2035 0 , {0}, {0,0,0,0,0,0,0,0,} };
1c79356b
A
2036 struct ifnet *ifp;
2037
2038 if (rsvpdebug)
2039 printf("rsvp_input: rsvp_on %d\n",rsvp_on);
2040
2041 /* Can still get packets with rsvp_on = 0 if there is a local member
2042 * of the group to which the RSVP packet is addressed. But in this
2043 * case we want to throw the packet away.
2044 */
2045 if (!rsvp_on) {
2046 m_freem(m);
2047 return;
2048 }
2049
1c79356b
A
2050 if (rsvpdebug)
2051 printf("rsvp_input: check vifs\n");
2052
2053#if DIAGNOSTIC
2054 if (!(m->m_flags & M_PKTHDR))
2055 panic("rsvp_input no hdr");
2056#endif
2057
2058 ifp = m->m_pkthdr.rcvif;
2059 /* Find which vif the packet arrived on. */
9bccf70c 2060 for (vifi = 0; vifi < numvifs; vifi++)
1c79356b 2061 if (viftable[vifi].v_ifp == ifp)
9bccf70c 2062 break;
1c79356b 2063
9bccf70c
A
2064 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
2065 /*
2066 * If the old-style non-vif-associated socket is set,
2067 * then use it. Otherwise, drop packet since there
2068 * is no specific socket for this vif.
2069 */
2070 if (ip_rsvpd != NULL) {
1c79356b 2071 if (rsvpdebug)
9bccf70c
A
2072 printf("rsvp_input: Sending packet up old-style socket\n");
2073 rip_input(m, iphlen); /* xxx */
2074 } else {
2075 if (rsvpdebug && vifi == numvifs)
2076 printf("rsvp_input: Can't find vif for packet.\n");
2077 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
2078 printf("rsvp_input: No socket defined for vif %d\n",vifi);
1c79356b 2079 m_freem(m);
9bccf70c 2080 }
9bccf70c 2081 return;
1c79356b
A
2082 }
2083 rsvp_src.sin_addr = ip->ip_src;
2084
2085 if (rsvpdebug && m)
b0d623f7 2086 printf("rsvp_input: m->m_len = %d, sbspace() = %d\n",
1c79356b
A
2087 m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv)));
2088
9bccf70c 2089 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
1c79356b
A
2090 if (rsvpdebug)
2091 printf("rsvp_input: Failed to append to socket\n");
9bccf70c 2092 } else {
1c79356b
A
2093 if (rsvpdebug)
2094 printf("rsvp_input: send packet up\n");
9bccf70c
A
2095 }
2096
1c79356b
A
2097}
2098
2099#if MROUTE_LKM
2100#include <sys/conf.h>
2101#include <sys/exec.h>
2102#include <sys/sysent.h>
2103#include <sys/lkm.h>
2104
2105MOD_MISC("ip_mroute_mod")
2106
2107static int
2108ip_mroute_mod_handle(struct lkm_table *lkmtp, int cmd)
2109{
2110 int i;
2111 struct lkm_misc *args = lkmtp->private.lkm_misc;
2112 int err = 0;
2113
2114 switch(cmd) {
2115 static int (*old_ip_mrouter_cmd)();
2116 static int (*old_ip_mrouter_done)();
2117 static int (*old_ip_mforward)();
2118 static int (*old_mrt_ioctl)();
2119 static void (*old_proto4_input)();
2120 static int (*old_legal_vif_num)();
1c79356b
A
2121
2122 case LKM_E_LOAD:
2123 if(lkmexists(lkmtp) || ip_mrtproto)
2124 return(EEXIST);
2125 old_ip_mrouter_cmd = ip_mrouter_cmd;
2126 ip_mrouter_cmd = X_ip_mrouter_cmd;
2127 old_ip_mrouter_done = ip_mrouter_done;
2128 ip_mrouter_done = X_ip_mrouter_done;
2129 old_ip_mforward = ip_mforward;
2130 ip_mforward = X_ip_mforward;
2131 old_mrt_ioctl = mrt_ioctl;
2132 mrt_ioctl = X_mrt_ioctl;
2133 old_proto4_input = ip_protox[ENCAP_PROTO]->pr_input;
2134 ip_protox[ENCAP_PROTO]->pr_input = X_ipip_input;
2135 old_legal_vif_num = legal_vif_num;
2136 legal_vif_num = X_legal_vif_num;
2137 ip_mrtproto = IGMP_DVMRP;
2138
2139 printf("\nIP multicast routing loaded\n");
2140 break;
2141
2142 case LKM_E_UNLOAD:
2143 if (ip_mrouter)
2144 return EINVAL;
2145
2146 ip_mrouter_cmd = old_ip_mrouter_cmd;
2147 ip_mrouter_done = old_ip_mrouter_done;
2148 ip_mforward = old_ip_mforward;
2149 mrt_ioctl = old_mrt_ioctl;
2150 ip_protox[ENCAP_PROTO]->pr_input = old_proto4_input;
2151 legal_vif_num = old_legal_vif_num;
2152 ip_mrtproto = 0;
2153 break;
2154
2155 default:
2156 err = EINVAL;
2157 break;
2158 }
2159
2160 return(err);
2161}
2162
2163int
2164ip_mroute_mod(struct lkm_table *lkmtp, int cmd, int ver) {
2165 DISPATCH(lkmtp, cmd, ver, ip_mroute_mod_handle, ip_mroute_mod_handle,
2166 nosys);
2167}
2168
2169#endif /* MROUTE_LKM */
2170#endif /* MROUTING */