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