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