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