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