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