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