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