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