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