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