ip_mroute.c revision 1.23 1 /* $NetBSD: ip_mroute.c,v 1.23 1995/06/12 03:05:12 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((void *));
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_len = sizeof(struct sockaddr_in);
571 satosin(&ifr.ifr_addr)->sin_family = AF_INET;
572 satosin(&ifr.ifr_addr)->sin_addr.s_addr = INADDR_ANY;
573 error = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, (caddr_t)&ifr);
574 if (error)
575 return (error);
576 }
577
578 s = splnet();
579 /* Define parameters for the tbf structure. */
580 vifp->v_tbf.q_len = 0;
581 vifp->v_tbf.n_tok = 0;
582 vifp->v_tbf.last_pkt_t = 0;
583
584 vifp->v_flags = vifcp->vifc_flags;
585 vifp->v_threshold = vifcp->vifc_threshold;
586 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
587 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
588 vifp->v_ifp = ifp;
589 vifp->v_rate_limit = vifcp->vifc_rate_limit;
590 #ifdef RSVP_ISI
591 vifp->v_rsvp_on = 0;
592 vifp->v_rsvpd = NULL;
593 #endif /* RSVP_ISI */
594 /* Initialize per vif pkt counters. */
595 vifp->v_pkt_in = 0;
596 vifp->v_pkt_out = 0;
597 vifp->v_bytes_in = 0;
598 vifp->v_bytes_out = 0;
599 splx(s);
600
601 /* Adjust numvifs up if the vifi is higher than numvifs. */
602 if (numvifs <= vifcp->vifc_vifi)
603 numvifs = vifcp->vifc_vifi + 1;
604
605 if (mrtdebug)
606 log(LOG_DEBUG, "add_vif #%d, lcladdr %x, %s %x, thresh %x, rate %d",
607 vifcp->vifc_vifi,
608 ntohl(vifcp->vifc_lcl_addr.s_addr),
609 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
610 ntohl(vifcp->vifc_rmt_addr.s_addr),
611 vifcp->vifc_threshold,
612 vifcp->vifc_rate_limit);
613
614 return (0);
615 }
616
617 void
618 reset_vif(vifp)
619 register struct vif *vifp;
620 {
621 struct ifnet *ifp;
622 struct ifreq ifr;
623
624 if (vifp->v_flags & VIFF_TUNNEL) {
625 free(vifp->v_ifp, M_MRTABLE);
626 if (vifp == last_encap_vif) {
627 last_encap_vif = 0;
628 last_encap_src = 0;
629 }
630 } else {
631 satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
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, sintosa(src), mm, (struct mbuf *)0) != 0) {
898 sorwakeup(s);
899 return (0);
900 }
901 }
902 m_freem(mm);
903 return (-1);
904 }
905
906 /*
907 * IP multicast forwarding function. This function assumes that the packet
908 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
909 * pointed to by "ifp", and the packet is to be relayed to other networks
910 * that have members of the packet's destination IP multicast group.
911 *
912 * The packet is returned unscathed to the caller, unless it is
913 * erroneous, in which case a non-zero return value tells the caller to
914 * discard it.
915 */
916
917 #define IP_HDR_LEN 20 /* # bytes of fixed IP header (excluding options) */
918 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
919
920 int
921 #ifdef RSVP_ISI
922 ip_mforward(m, ifp, imo)
923 #else
924 ip_mforward(m, ifp)
925 #endif /* RSVP_ISI */
926 struct mbuf *m;
927 struct ifnet *ifp;
928 #ifdef RSVP_ISI
929 struct ip_moptions *imo;
930 #endif /* RSVP_ISI */
931 {
932 register struct ip *ip = mtod(m, struct ip *);
933 register struct mfc *rt;
934 register struct vif *vifp;
935 register u_char *ipoptions;
936 static struct sockproto k_igmpproto = { AF_INET, IPPROTO_IGMP };
937 static int srctun = 0;
938 register struct mbuf *mm, *mn;
939 int s;
940 #ifdef RSVP_ISI
941 vifi_t vifi;
942 #endif /* RSVP_ISI */
943
944 if (mrtdebug & DEBUG_FORWARD)
945 log(LOG_DEBUG, "ip_mforward: src %x, dst %x, ifp %x",
946 ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr), ifp);
947
948 if (ip->ip_hl < (IP_HDR_LEN + TUNNEL_LEN) >> 2 ||
949 (ipoptions = (u_char *)(ip + 1))[1] != IPOPT_LSRR) {
950 /*
951 * Packet arrived via a physical interface or
952 * an encapuslated tunnel.
953 */
954 } else {
955 /*
956 * Packet arrived through a source-route tunnel.
957 * Source-route tunnels are no longer supported.
958 */
959 if ((srctun++ % 1000) == 0)
960 log(LOG_ERR, "ip_mforward: received source-routed packet from %x",
961 ntohl(ip->ip_src.s_addr));
962
963 return (1);
964 }
965
966 #ifdef RSVP_ISI
967 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
968 if (ip->ip_ttl < 255)
969 ip->ip_ttl++; /* compensate for -1 in *_send routines */
970 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
971 vifp = viftable + vifi;
972 printf("Sending IPPROTO_RSVP from %x to %x on vif %d (%s%s%d)\n",
973 ntohl(ip->ip_src), ntohl(ip->ip_dst), vifi,
974 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
975 vifp->v_ifp->if_name, vifp->v_ifp->if_unit);
976 }
977 return (ip_mdq(m, ifp, rt, vifi));
978 }
979 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
980 printf("Warning: IPPROTO_RSVP from %x to %x without vif option\n",
981 ntohl(ip->ip_src), ntohl(ip->ip_dst));
982 }
983 #endif /* RSVP_ISI */
984
985 /*
986 * Don't forward a packet with time-to-live of zero or one,
987 * or a packet destined to a local-only group.
988 */
989 if (ip->ip_ttl <= 1 ||
990 IN_LOCAL_GROUP(ip->ip_dst.s_addr))
991 return (0);
992
993 /*
994 * Determine forwarding vifs from the forwarding cache table
995 */
996 s = splnet();
997 MFCFIND(ip->ip_src.s_addr, ip->ip_dst.s_addr, rt);
998
999 /* Entry exists, so forward if necessary */
1000 if (rt != NULL) {
1001 splx(s);
1002 #ifdef RSVP_ISI
1003 return (ip_mdq(m, ifp, rt, -1));
1004 #else
1005 return (ip_mdq(m, ifp, rt));
1006 #endif /* RSVP_ISI */
1007 } else {
1008 /*
1009 * If we don't have a route for packet's origin,
1010 * Make a copy of the packet &
1011 * send message to routing daemon
1012 */
1013
1014 register struct mbuf *mb0;
1015 register struct rtdetq *rte;
1016 register u_int32_t hash;
1017 #ifdef UPCALL_TIMING
1018 struct timeval tp;
1019
1020 microtime(&tp);
1021 #endif /* UPCALL_TIMING */
1022
1023 mrtstat.mrts_no_route++;
1024 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1025 log(LOG_DEBUG, "ip_mforward: no rte s %x g %x",
1026 ntohl(ip->ip_src.s_addr),
1027 ntohl(ip->ip_dst.s_addr));
1028
1029 /*
1030 * Allocate mbufs early so that we don't do extra work if we are
1031 * just going to fail anyway.
1032 */
1033 rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE, M_NOWAIT);
1034 if (rte == NULL) {
1035 splx(s);
1036 return (ENOBUFS);
1037 }
1038 mb0 = m_copy(m, 0, M_COPYALL);
1039 if (mb0 == NULL) {
1040 free(rte, M_MRTABLE);
1041 splx(s);
1042 return (ENOBUFS);
1043 }
1044
1045 /* is there an upcall waiting for this packet? */
1046 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1047 for (rt = mfchashtbl[hash].lh_first; rt; rt = rt->mfc_hash.le_next) {
1048 if (ip->ip_src.s_addr == rt->mfc_origin.s_addr &&
1049 ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr &&
1050 rt->mfc_stall != NULL)
1051 break;
1052 }
1053
1054 if (rt == NULL) {
1055 int hlen = ip->ip_hl << 2;
1056 int i;
1057 struct igmpmsg *im;
1058
1059 /* no upcall, so make a new entry */
1060 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1061 if (rt == NULL) {
1062 free(rte, M_MRTABLE);
1063 m_free(mb0);
1064 splx(s);
1065 return (ENOBUFS);
1066 }
1067 /* Make a copy of the header to send to the user level process */
1068 mm = m_copy(m, 0, hlen);
1069 M_PULLUP(mm, hlen);
1070 if (mm == NULL) {
1071 free(rte, M_MRTABLE);
1072 m_free(mb0);
1073 free(rt, M_MRTABLE);
1074 splx(s);
1075 return (ENOBUFS);
1076 }
1077
1078 /*
1079 * Send message to routing daemon to install
1080 * a route into the kernel table
1081 */
1082 sin.sin_addr = ip->ip_src;
1083
1084 im = mtod(mm, struct igmpmsg *);
1085 im->im_msgtype = IGMPMSG_NOCACHE;
1086 im->im_mbz = 0;
1087
1088 mrtstat.mrts_upcalls++;
1089
1090 if (socket_send(ip_mrouter, mm, &sin) < 0) {
1091 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full");
1092 ++mrtstat.mrts_upq_sockfull;
1093 free(rte, M_MRTABLE);
1094 m_free(mb0);
1095 free(rt, M_MRTABLE);
1096 splx(s);
1097 return (ENOBUFS);
1098 }
1099
1100 /* insert new entry at head of hash chain */
1101 rt->mfc_origin = ip->ip_src;
1102 rt->mfc_mcastgrp = ip->ip_dst;
1103 rt->mfc_pkt_cnt = 0;
1104 rt->mfc_byte_cnt = 0;
1105 rt->mfc_wrong_if = 0;
1106 rt->mfc_expire = UPCALL_EXPIRE;
1107 nexpire[hash]++;
1108 for (i = 0; i < numvifs; i++)
1109 rt->mfc_ttls[i] = 0;
1110 rt->mfc_parent = -1;
1111
1112 /* link into table */
1113 LIST_INSERT_HEAD(&mfchashtbl[hash], rt, mfc_hash);
1114 /* Add this entry to the end of the queue */
1115 rt->mfc_stall = rte;
1116 } else {
1117 /* determine if q has overflowed */
1118 struct rtdetq **p;
1119 register int npkts = 0;
1120
1121 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1122 if (++npkts > MAX_UPQ) {
1123 mrtstat.mrts_upq_ovflw++;
1124 free(rte, M_MRTABLE);
1125 m_free(mb0);
1126 splx(s);
1127 return (0);
1128 }
1129
1130 /* Add this entry to the end of the queue */
1131 *p = rte;
1132 }
1133
1134 rte->next = NULL;
1135 rte->m = mb0;
1136 rte->ifp = ifp;
1137 #ifdef UPCALL_TIMING
1138 rte->t = tp;
1139 #endif /* UPCALL_TIMING */
1140
1141
1142 splx(s);
1143
1144 return (0);
1145 }
1146 }
1147
1148
1149 static void
1150 expire_upcalls()
1151 {
1152 int i;
1153 int s;
1154
1155 s = splnet();
1156
1157 for (i = 0; i < MFCTBLSIZ; i++) {
1158 register struct mfc *rt, *nrt;
1159
1160 if (nexpire[i] == 0)
1161 continue;
1162
1163 for (rt = mfchashtbl[i].lh_first; rt; rt = nrt) {
1164 nrt = rt->mfc_hash.le_next;
1165
1166 if (rt->mfc_expire == 0 ||
1167 --rt->mfc_expire > 0)
1168 continue;
1169 nexpire[i]--;
1170
1171 ++mrtstat.mrts_cache_cleanups;
1172 if (mrtdebug & DEBUG_EXPIRE)
1173 log(LOG_DEBUG,
1174 "expire_upcalls: expiring (%x %x)",
1175 ntohl(rt->mfc_origin.s_addr),
1176 ntohl(rt->mfc_mcastgrp.s_addr));
1177
1178 expire_mfc(rt);
1179 }
1180 }
1181
1182 splx(s);
1183 timeout(expire_upcalls, (caddr_t)0, EXPIRE_TIMEOUT);
1184 }
1185
1186 /*
1187 * Packet forwarding routine once entry in the cache is made
1188 */
1189 static int
1190 #ifdef RSVP_ISI
1191 ip_mdq(m, ifp, rt, xmt_vif)
1192 #else
1193 ip_mdq(m, ifp, rt)
1194 #endif /* RSVP_ISI */
1195 register struct mbuf *m;
1196 register struct ifnet *ifp;
1197 register struct mfc *rt;
1198 #ifdef RSVP_ISI
1199 register vifi_t xmt_vif;
1200 #endif /* RSVP_ISI */
1201 {
1202 register struct ip *ip = mtod(m, struct ip *);
1203 register vifi_t vifi;
1204 register struct vif *vifp;
1205 register struct mbuf *tmp;
1206 register int plen = ntohs(ip->ip_len);
1207
1208 /*
1209 * Macro to send packet on vif. Since RSVP packets don't get counted on
1210 * input, they shouldn't get counted on output, so statistics keeping is
1211 * seperate.
1212 */
1213 #define MC_SEND(ip,vifp,m) { \
1214 if ((vifp)->v_flags & VIFF_TUNNEL) \
1215 encap_send((ip), (vifp), (m)); \
1216 else \
1217 phyint_send((ip), (vifp), (m)); \
1218 }
1219
1220 #ifdef RSVP_ISI
1221 /*
1222 * If xmt_vif is not -1, send on only the requested vif.
1223 *
1224 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.
1225 */
1226 if (xmt_vif < numvifs) {
1227 MC_SEND(ip, viftable + xmt_vif, m);
1228 return (1);
1229 }
1230 #endif /* RSVP_ISI */
1231
1232 /*
1233 * Don't forward if it didn't arrive from the parent vif for its origin.
1234 */
1235 vifi = rt->mfc_parent;
1236 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1237 /* came in the wrong interface */
1238 if (mrtdebug & DEBUG_FORWARD)
1239 log(LOG_DEBUG, "wrong if: ifp %x vifi %d vififp %x",
1240 ifp, vifi, viftable[vifi].v_ifp);
1241 ++mrtstat.mrts_wrong_if;
1242 ++rt->mfc_wrong_if;
1243 /*
1244 * If we are doing PIM assert processing, and we are forwarding
1245 * packets on this interface, and it is a broadcast medium
1246 * interface (and not a tunnel), send a message to the routing daemon.
1247 */
1248 if (pim_assert && rt->mfc_ttls[vifi] &&
1249 (ifp->if_flags & IFF_BROADCAST) &&
1250 !(viftable[vifi].v_flags & VIFF_TUNNEL)) {
1251 struct mbuf *mm;
1252 struct igmpmsg *im;
1253 int hlen = ip->ip_hl << 2;
1254 struct timeval now;
1255 register u_int32_t delta;
1256
1257 microtime(&now);
1258
1259 TV_DELTA(rt->mfc_last_assert, now, delta);
1260
1261 if (delta > ASSERT_MSG_TIME) {
1262 mm = m_copy(m, 0, hlen);
1263 M_PULLUP(mm, hlen);
1264 if (mm == NULL) {
1265 return (ENOBUFS);
1266 }
1267
1268 rt->mfc_last_assert = now;
1269
1270 im = mtod(mm, struct igmpmsg *);
1271 im->im_msgtype = IGMPMSG_WRONGVIF;
1272 im->im_mbz = 0;
1273 im->im_vif = vifi;
1274
1275 sin.sin_addr = im->im_src;
1276
1277 socket_send(ip_mrouter, m, &sin);
1278 }
1279 }
1280 return (0);
1281 }
1282
1283 /* If I sourced this packet, it counts as output, else it was input. */
1284 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1285 viftable[vifi].v_pkt_out++;
1286 viftable[vifi].v_bytes_out += plen;
1287 } else {
1288 viftable[vifi].v_pkt_in++;
1289 viftable[vifi].v_bytes_in += plen;
1290 }
1291 rt->mfc_pkt_cnt++;
1292 rt->mfc_byte_cnt += plen;
1293
1294 /*
1295 * For each vif, decide if a copy of the packet should be forwarded.
1296 * Forward if:
1297 * - the ttl exceeds the vif's threshold
1298 * - there are group members downstream on interface
1299 */
1300 for (vifp = viftable, vifi = 0; vifi < numvifs; vifp++, vifi++)
1301 if ((rt->mfc_ttls[vifi] > 0) &&
1302 (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1303 vifp->v_pkt_out++;
1304 vifp->v_bytes_out += plen;
1305 MC_SEND(ip, vifp, m);
1306 }
1307
1308 return (0);
1309 }
1310
1311 #ifdef RSVP_ISI
1312 /*
1313 * check if a vif number is legal/ok. This is used by ip_output, to export
1314 * numvifs there,
1315 */
1316 int
1317 legal_vif_num(vif)
1318 int vif;
1319 {
1320 if (vif >= 0 && vif < numvifs)
1321 return (1);
1322 else
1323 return (0);
1324 }
1325 #endif /* RSVP_ISI */
1326
1327 static void
1328 phyint_send(ip, vifp, m)
1329 struct ip *ip;
1330 struct vif *vifp;
1331 struct mbuf *m;
1332 {
1333 register struct mbuf *mb_copy;
1334 register int hlen = ip->ip_hl << 2;
1335
1336 /*
1337 * Make a new reference to the packet; make sure that
1338 * the IP header is actually copied, not just referenced,
1339 * so that ip_output() only scribbles on the copy.
1340 */
1341 mb_copy = m_copy(m, 0, M_COPYALL);
1342 M_PULLUP(mb_copy, hlen);
1343 if (mb_copy == NULL)
1344 return;
1345
1346 if (vifp->v_rate_limit <= 0)
1347 tbf_send_packet(vifp, mb_copy);
1348 else
1349 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len);
1350 }
1351
1352 static void
1353 encap_send(ip, vifp, m)
1354 register struct ip *ip;
1355 register struct vif *vifp;
1356 register struct mbuf *m;
1357 {
1358 register struct mbuf *mb_copy;
1359 register struct ip *ip_copy;
1360 register int i, len = ip->ip_len + sizeof(multicast_encap_iphdr);
1361
1362 /*
1363 * copy the old packet & pullup it's IP header into the
1364 * new mbuf so we can modify it. Try to fill the new
1365 * mbuf since if we don't the ethernet driver will.
1366 */
1367 MGETHDR(mb_copy, M_DONTWAIT, MT_DATA);
1368 if (mb_copy == NULL)
1369 return;
1370 mb_copy->m_data += max_linkhdr;
1371 mb_copy->m_pkthdr.len = len;
1372 mb_copy->m_len = sizeof(multicast_encap_iphdr);
1373
1374 if ((mb_copy->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1375 m_freem(mb_copy);
1376 return;
1377 }
1378 i = MHLEN - max_linkhdr;
1379 if (i > len)
1380 i = len;
1381 mb_copy = m_pullup(mb_copy, i);
1382 if (mb_copy == NULL)
1383 return;
1384
1385 /*
1386 * fill in the encapsulating IP header.
1387 */
1388 ip_copy = mtod(mb_copy, struct ip *);
1389 *ip_copy = multicast_encap_iphdr;
1390 ip_copy->ip_id = htons(ip_id++);
1391 ip_copy->ip_len = len;
1392 ip_copy->ip_src = vifp->v_lcl_addr;
1393 ip_copy->ip_dst = vifp->v_rmt_addr;
1394
1395 /*
1396 * turn the encapsulated IP header back into a valid one.
1397 */
1398 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
1399 --ip->ip_ttl;
1400 HTONS(ip->ip_len);
1401 HTONS(ip->ip_off);
1402 ip->ip_sum = 0;
1403 #if defined(LBL) && !defined(ultrix) && !defined(i386)
1404 ip->ip_sum = ~oc_cksum((caddr_t)ip, ip->ip_hl << 2, 0);
1405 #else
1406 mb_copy->m_data += sizeof(multicast_encap_iphdr);
1407 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
1408 mb_copy->m_data -= sizeof(multicast_encap_iphdr);
1409 #endif
1410
1411 if (vifp->v_rate_limit <= 0)
1412 tbf_send_packet(vifp, mb_copy);
1413 else
1414 tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
1415 }
1416
1417 /*
1418 * De-encapsulate a packet and feed it back through ip input (this
1419 * routine is called whenever IP gets a packet with proto type
1420 * ENCAP_PROTO and a local destination address).
1421 */
1422 void
1423 ipip_input(m, hlen)
1424 register struct mbuf *m;
1425 register int hlen;
1426 {
1427 register struct ip *ip = mtod(m, struct ip *);
1428 register int s;
1429 register struct ifqueue *ifq;
1430 register struct vif *vifp;
1431
1432 if (!have_encap_tunnel) {
1433 rip_input(m);
1434 return;
1435 }
1436
1437 /*
1438 * dump the packet if it's not to a multicast destination or if
1439 * we don't have an encapsulating tunnel with the source.
1440 * Note: This code assumes that the remote site IP address
1441 * uniquely identifies the tunnel (i.e., that this site has
1442 * at most one tunnel with the remote site).
1443 */
1444 if (!IN_MULTICAST(((struct ip *)((char *)ip + hlen))->ip_dst.s_addr)) {
1445 ++mrtstat.mrts_bad_tunnel;
1446 m_freem(m);
1447 return;
1448 }
1449
1450 if (ip->ip_src.s_addr != last_encap_src) {
1451 register struct vif *vife;
1452
1453 vifp = viftable;
1454 vife = vifp + numvifs;
1455 for (; vifp < vife; vifp++)
1456 if (vifp->v_flags & VIFF_TUNNEL &&
1457 vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr)
1458 break;
1459 if (vifp == vife) {
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 last_encap_vif = vifp;
1468 last_encap_src = ip->ip_src.s_addr;
1469 } else
1470 vifp = last_encap_vif;
1471
1472 m->m_data += hlen;
1473 m->m_len -= hlen;
1474 m->m_pkthdr.len -= hlen;
1475 m->m_pkthdr.rcvif = vifp->v_ifp;
1476 ifq = &ipintrq;
1477 s = splimp();
1478 if (IF_QFULL(ifq)) {
1479 IF_DROP(ifq);
1480 m_freem(m);
1481 } else {
1482 IF_ENQUEUE(ifq, m);
1483 /*
1484 * normally we would need a "schednetisr(NETISR_IP)"
1485 * here but we were called by ip_input and it is going
1486 * to loop back & try to dequeue the packet we just
1487 * queued as soon as we return so we avoid the
1488 * unnecessary software interrrupt.
1489 */
1490 }
1491 splx(s);
1492 }
1493
1494 /*
1495 * Token bucket filter module
1496 */
1497 static void
1498 tbf_control(vifp, m, ip, p_len)
1499 register struct vif *vifp;
1500 register struct mbuf *m;
1501 register struct ip *ip;
1502 register u_int32_t p_len;
1503 {
1504
1505 tbf_update_tokens(vifp);
1506
1507 /*
1508 * If there are enough tokens, and the queue is empty, send this packet
1509 * out immediately. Otherwise, try to insert it on this vif's queue.
1510 */
1511 if (vifp->v_tbf.q_len == 0) {
1512 if (p_len <= vifp->v_tbf.n_tok) {
1513 vifp->v_tbf.n_tok -= p_len;
1514 tbf_send_packet(vifp, m);
1515 } else if (p_len > MAX_BKT_SIZE) {
1516 /* drop if packet is too large */
1517 mrtstat.mrts_pkt2large++;
1518 m_freem(m);
1519 } else {
1520 /* queue packet and timeout till later */
1521 tbf_queue(vifp, m, ip);
1522 timeout(tbf_reprocess_q, vifp, 1);
1523 }
1524 } else {
1525 if (vifp->v_tbf.q_len >= MAXQSIZE &&
1526 !tbf_dq_sel(vifp, ip)) {
1527 /* queue length too much, and couldn't make room */
1528 mrtstat.mrts_q_overflow++;
1529 m_freem(m);
1530 } else {
1531 /* queue length low enough, or made room */
1532 tbf_queue(vifp, m, ip);
1533 tbf_process_q(vifp);
1534 }
1535 }
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(arg)
1623 void *arg;
1624 {
1625 register struct vif *vifp = arg;
1626
1627 if (ip_mrouter == NULL)
1628 return;
1629
1630 tbf_update_tokens(vifp);
1631 tbf_process_q(vifp);
1632
1633 if (vifp->v_tbf.q_len)
1634 timeout(tbf_reprocess_q, vifp, 1);
1635 }
1636
1637 /* function that will selectively discard a member of the queue
1638 * based on the precedence value and the priority obtained through
1639 * a lookup table - not yet implemented accurately!
1640 */
1641 static int
1642 tbf_dq_sel(vifp, ip)
1643 register struct vif *vifp;
1644 register struct ip *ip;
1645 {
1646 register int i;
1647 register int s = splnet();
1648 register u_int p;
1649
1650 p = priority(vifp, ip);
1651
1652 for(i=vifp->v_tbf.q_len-1;i >= 0;i--) {
1653 if (p > priority(vifp, qtable[vifp-viftable][i].pkt_ip)) {
1654 m_freem(qtable[vifp-viftable][i].pkt_m);
1655 tbf_dequeue(vifp, i);
1656 splx(s);
1657 mrtstat.mrts_drop_sel++;
1658 return (1);
1659 }
1660 }
1661 splx(s);
1662 return (0);
1663 }
1664
1665 static void
1666 tbf_send_packet(vifp,m)
1667 register struct vif *vifp;
1668 register struct mbuf *m;
1669 {
1670 register struct mbuf *mcp;
1671 int error;
1672 int s = splnet();
1673
1674 if (vifp->v_flags & VIFF_TUNNEL) {
1675 /* If tunnel options */
1676 ip_output(m, (struct mbuf *)0, &vifp->v_route,
1677 IP_FORWARDING, NULL);
1678 } else {
1679 /* if physical interface option, extract the options and then send */
1680 struct ip *ip = mtod(m, struct ip *);
1681 struct ip_moptions imo;
1682 imo.imo_multicast_ifp = vifp->v_ifp;
1683 imo.imo_multicast_ttl = ip->ip_ttl - 1;
1684 imo.imo_multicast_loop = 1;
1685 #ifdef RSVP_ISI
1686 imo.imo_multicast_vif = -1;
1687 #endif
1688
1689 error = ip_output(m, (struct mbuf *)0, (struct route *)0,
1690 IP_FORWARDING|IP_MULTICASTOPTS, &imo);
1691 if (mrtdebug & DEBUG_XMIT)
1692 log(LOG_DEBUG, "phyint_send on vif %d err %d", vifp-viftable, error);
1693 }
1694 splx(s);
1695 }
1696
1697 /* determine the current time and then
1698 * the elapsed time (between the last time and time now)
1699 * in milliseconds & update the no. of tokens in the bucket
1700 */
1701 static void
1702 tbf_update_tokens(vifp)
1703 register struct vif *vifp;
1704 {
1705 struct timeval tp;
1706 register u_int32_t t;
1707 register u_int32_t elapsed;
1708 register int s = splnet();
1709
1710 microtime(&tp);
1711
1712 t = tp.tv_sec*1000 + tp.tv_usec/1000;
1713
1714 elapsed = (t - vifp->v_tbf.last_pkt_t) * vifp->v_rate_limit /8;
1715 vifp->v_tbf.n_tok += elapsed;
1716 vifp->v_tbf.last_pkt_t = t;
1717
1718 if (vifp->v_tbf.n_tok > MAX_BKT_SIZE)
1719 vifp->v_tbf.n_tok = MAX_BKT_SIZE;
1720
1721 splx(s);
1722 }
1723
1724 static int
1725 priority(vifp, ip)
1726 register struct vif *vifp;
1727 register struct ip *ip;
1728 {
1729 register u_short port;
1730 register int prio;
1731
1732 /* temporary hack; may add general packet classifier some day */
1733
1734 /*
1735 * The UDP port space is divided up into four priority ranges:
1736 * [0, 16384) : unclassified - lowest priority
1737 * [16384, 32768) : audio - highest priority
1738 * [32768, 49152) : whiteboard - medium priority
1739 * [49152, 65536) : video - low priority
1740 */
1741 if (ip->ip_p == IPPROTO_UDP) {
1742 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
1743
1744 switch (ntohs(udp->uh_dport) & 0xc000) {
1745 case 0x4000:
1746 prio = 70;
1747 break;
1748 case 0x8000:
1749 prio = 60;
1750 break;
1751 case 0xc000:
1752 prio = 55;
1753 break;
1754 default:
1755 prio = 50;
1756 break;
1757 }
1758
1759 if (tbfdebug > 1) log(LOG_DEBUG, "port %x prio %d", ntohs(udp->uh_dport), prio);
1760 } else
1761 prio = 50;
1762
1763
1764 return (prio);
1765 }
1766
1767 /*
1768 * End of token bucket filter modifications
1769 */
1770
1771 #ifdef RSVP_ISI
1772
1773 int
1774 ip_rsvp_vif_init(so, m)
1775 struct socket *so;
1776 struct mbuf *m;
1777 {
1778 int i;
1779 register int s;
1780
1781 if (rsvpdebug)
1782 printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
1783 so->so_type, so->so_proto->pr_protocol);
1784
1785 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
1786 return (EOPNOTSUPP);
1787
1788 /* Check mbuf. */
1789 if (m == NULL || m->m_len != sizeof(int)) {
1790 return (EINVAL);
1791 }
1792 i = *(mtod(m, int *));
1793
1794 if (rsvpdebug)
1795 printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n",i,rsvp_on);
1796
1797 s = splnet();
1798
1799 /* Check vif. */
1800 if (!legal_vif_num(i)) {
1801 splx(s);
1802 return (EADDRNOTAVAIL);
1803 }
1804
1805 /* Check if socket is available. */
1806 if (viftable[i].v_rsvpd != NULL) {
1807 splx(s);
1808 return (EADDRINUSE);
1809 }
1810
1811 viftable[i].v_rsvpd = so;
1812 /* This may seem silly, but we need to be sure we don't over-increment
1813 * the RSVP counter, in case something slips up.
1814 */
1815 if (!viftable[i].v_rsvp_on) {
1816 viftable[i].v_rsvp_on = 1;
1817 rsvp_on++;
1818 }
1819
1820 splx(s);
1821 return (0);
1822 }
1823
1824 int
1825 ip_rsvp_vif_done(so, m)
1826 struct socket *so;
1827 struct mbuf *m;
1828 {
1829 int i;
1830 register int s;
1831
1832 if (rsvpdebug)
1833 printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n",
1834 so->so_type, so->so_proto->pr_protocol);
1835
1836 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
1837 return (EOPNOTSUPP);
1838
1839 /* Check mbuf. */
1840 if (m == NULL || m->m_len != sizeof(int)) {
1841 return (EINVAL);
1842 }
1843 i = *(mtod(m, int *));
1844
1845 s = splnet();
1846
1847 /* Check vif. */
1848 if (!legal_vif_num(i)) {
1849 splx(s);
1850 return (EADDRNOTAVAIL);
1851 }
1852
1853 if (rsvpdebug)
1854 printf("ip_rsvp_vif_done: v_rsvpd = %x so = %x\n",
1855 viftable[i].v_rsvpd, so);
1856
1857 viftable[i].v_rsvpd = NULL;
1858 /* This may seem silly, but we need to be sure we don't over-decrement
1859 * the RSVP counter, in case something slips up.
1860 */
1861 if (viftable[i].v_rsvp_on) {
1862 viftable[i].v_rsvp_on = 0;
1863 rsvp_on--;
1864 }
1865
1866 splx(s);
1867 return (0);
1868 }
1869
1870 ip_rsvp_force_done(so)
1871 struct socket *so;
1872 {
1873 int vifi;
1874 register int s;
1875
1876 /* Don't bother if it is not the right type of socket. */
1877 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
1878 return;
1879
1880 s = splnet();
1881
1882 /* The socket may be attached to more than one vif...this
1883 * is perfectly legal.
1884 */
1885 for (vifi = 0; vifi < numvifs; vifi++) {
1886 if (viftable[vifi].v_rsvpd == so) {
1887 viftable[vifi].v_rsvpd = NULL;
1888 /* This may seem silly, but we need to be sure we don't
1889 * over-decrement the RSVP counter, in case something slips up.
1890 */
1891 if (viftable[vifi].v_rsvp_on) {
1892 viftable[vifi].v_rsvp_on = 0;
1893 rsvp_on--;
1894 }
1895 }
1896 }
1897
1898 splx(s);
1899 return;
1900 }
1901
1902 rsvp_input(m, ifp)
1903 struct mbuf *m;
1904 struct ifnet *ifp;
1905 {
1906 int vifi;
1907 register struct ip *ip = mtod(m, struct ip *);
1908 static struct sockaddr_in rsvp_src = { sizeof(sin), AF_INET };
1909 register int s;
1910
1911 if (rsvpdebug)
1912 printf("rsvp_input: rsvp_on %d\n",rsvp_on);
1913
1914 /* Can still get packets with rsvp_on = 0 if there is a local member
1915 * of the group to which the RSVP packet is addressed. But in this
1916 * case we want to throw the packet away.
1917 */
1918 if (!rsvp_on) {
1919 m_freem(m);
1920 return;
1921 }
1922
1923 /* If the old-style non-vif-associated socket is set, then use
1924 * it and ignore the new ones.
1925 */
1926 if (ip_rsvpd != NULL) {
1927 if (rsvpdebug)
1928 printf("rsvp_input: Sending packet up old-style socket\n");
1929 rip_input(m);
1930 return;
1931 }
1932
1933 s = splnet();
1934
1935 if (rsvpdebug)
1936 printf("rsvp_input: check vifs\n");
1937
1938 /* Find which vif the packet arrived on. */
1939 for (vifi = 0; vifi < numvifs; vifi++) {
1940 if (viftable[vifi].v_ifp == ifp)
1941 break;
1942 }
1943
1944 if (vifi == numvifs) {
1945 /* Can't find vif packet arrived on. Drop packet. */
1946 if (rsvpdebug)
1947 printf("rsvp_input: Can't find vif for packet...dropping it.\n");
1948 m_freem(m);
1949 splx(s);
1950 return;
1951 }
1952
1953 if (rsvpdebug)
1954 printf("rsvp_input: check socket\n");
1955
1956 if (viftable[vifi].v_rsvpd == NULL) {
1957 /* drop packet, since there is no specific socket for this
1958 * interface */
1959 if (rsvpdebug)
1960 printf("rsvp_input: No socket defined for vif %d\n",vifi);
1961 m_freem(m);
1962 splx(s);
1963 return;
1964 }
1965
1966 rsvp_src.sin_addr = ip->ip_src;
1967
1968 if (rsvpdebug && m)
1969 printf("rsvp_input: m->m_len = %d, sbspace() = %d\n",
1970 m->m_len,sbspace(&viftable[vifi].v_rsvpd->so_rcv));
1971
1972 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0)
1973 if (rsvpdebug)
1974 printf("rsvp_input: Failed to append to socket\n");
1975 else
1976 if (rsvpdebug)
1977 printf("rsvp_input: send packet up\n");
1978
1979 splx(s);
1980 }
1981 #endif /* RSVP_ISI */
1982