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