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