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