ip_mroute.c revision 1.87 1 /* $NetBSD: ip_mroute.c,v 1.87 2005/01/15 06:50:47 manu Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Stephen Deering of Stanford University.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
35 */
36
37 /*
38 * Copyright (c) 1989 Stephen Deering
39 *
40 * This code is derived from software contributed to Berkeley by
41 * Stephen Deering of Stanford University.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
72 */
73
74 /*
75 * IP multicast forwarding procedures
76 *
77 * Written by David Waitzman, BBN Labs, August 1988.
78 * Modified by Steve Deering, Stanford, February 1989.
79 * Modified by Mark J. Steiglitz, Stanford, May, 1991
80 * Modified by Van Jacobson, LBL, January 1993
81 * Modified by Ajit Thyagarajan, PARC, August 1993
82 * Modified by Bill Fenner, PARC, April 1994
83 * Modified by Charles M. Hannum, NetBSD, May 1995.
84 * Modified by Ahmed Helmy, SGI, June 1996
85 * Modified by George Edmond Eddy (Rusty), ISI, February 1998
86 * Modified by Pavlin Radoslavov, USC/ISI, May 1998, August 1999, October 2000
87 * Modified by Hitoshi Asaeda, WIDE, August 2000
88 * Modified by Pavlin Radoslavov, ICSI, October 2002
89 *
90 * MROUTING Revision: 1.2
91 * and PIM-SMv2 and PIM-DM support, advanced API support,
92 * bandwidth metering and signaling
93 */
94
95 #include <sys/cdefs.h>
96 __KERNEL_RCSID(0, "$NetBSD: ip_mroute.c,v 1.87 2005/01/15 06:50:47 manu Exp $");
97
98 #include "opt_inet.h"
99 #include "opt_ipsec.h"
100 #include "opt_pim.h"
101
102 #ifdef PIM
103 #define _PIM_VT 1
104 #endif
105
106 #include <sys/param.h>
107 #include <sys/systm.h>
108 #include <sys/callout.h>
109 #include <sys/mbuf.h>
110 #include <sys/socket.h>
111 #include <sys/socketvar.h>
112 #include <sys/protosw.h>
113 #include <sys/errno.h>
114 #include <sys/time.h>
115 #include <sys/kernel.h>
116 #include <sys/ioctl.h>
117 #include <sys/syslog.h>
118
119 #include <net/if.h>
120 #include <net/route.h>
121 #include <net/raw_cb.h>
122
123 #include <netinet/in.h>
124 #include <netinet/in_var.h>
125 #include <netinet/in_systm.h>
126 #include <netinet/ip.h>
127 #include <netinet/ip_var.h>
128 #include <netinet/in_pcb.h>
129 #include <netinet/udp.h>
130 #include <netinet/igmp.h>
131 #include <netinet/igmp_var.h>
132 #include <netinet/ip_mroute.h>
133 #ifdef PIM
134 #include <netinet/pim.h>
135 #include <netinet/pim_var.h>
136 #endif
137 #include <netinet/ip_encap.h>
138
139 #ifdef IPSEC
140 #include <netinet6/ipsec.h>
141 #include <netkey/key.h>
142 #endif
143
144 #ifdef FAST_IPSEC
145 #include <netipsec/ipsec.h>
146 #include <netipsec/key.h>
147 #endif
148
149 #include <machine/stdarg.h>
150
151 #define IP_MULTICASTOPTS 0
152 #define M_PULLUP(m, len) \
153 do { \
154 if ((m) && ((m)->m_flags & M_EXT || (m)->m_len < (len))) \
155 (m) = m_pullup((m), (len)); \
156 } while (/*CONSTCOND*/ 0)
157
158 /*
159 * Globals. All but ip_mrouter and ip_mrtproto could be static,
160 * except for netstat or debugging purposes.
161 */
162 struct socket *ip_mrouter = NULL;
163 int ip_mrtproto = IGMP_DVMRP; /* for netstat only */
164
165 #define NO_RTE_FOUND 0x1
166 #define RTE_FOUND 0x2
167
168 #define MFCHASH(a, g) \
169 ((((a).s_addr >> 20) ^ ((a).s_addr >> 10) ^ (a).s_addr ^ \
170 ((g).s_addr >> 20) ^ ((g).s_addr >> 10) ^ (g).s_addr) & mfchash)
171 LIST_HEAD(mfchashhdr, mfc) *mfchashtbl;
172 u_long mfchash;
173
174 u_char nexpire[MFCTBLSIZ];
175 struct vif viftable[MAXVIFS];
176 struct mrtstat mrtstat;
177 u_int mrtdebug = 0; /* debug level */
178 #define DEBUG_MFC 0x02
179 #define DEBUG_FORWARD 0x04
180 #define DEBUG_EXPIRE 0x08
181 #define DEBUG_XMIT 0x10
182 #define DEBUG_PIM 0x20
183
184 #define VIFI_INVALID ((vifi_t) -1)
185
186 u_int tbfdebug = 0; /* tbf debug level */
187 #ifdef RSVP_ISI
188 u_int rsvpdebug = 0; /* rsvp debug level */
189 extern struct socket *ip_rsvpd;
190 extern int rsvp_on;
191 #endif /* RSVP_ISI */
192
193 /* vif attachment using sys/netinet/ip_encap.c */
194 static void vif_input __P((struct mbuf *, ...));
195 static int vif_encapcheck __P((const struct mbuf *, int, int, void *));
196
197 static const struct protosw vif_protosw =
198 { SOCK_RAW, &inetdomain, IPPROTO_IPV4, PR_ATOMIC|PR_ADDR,
199 vif_input, rip_output, 0, rip_ctloutput,
200 rip_usrreq,
201 0, 0, 0, 0,
202 };
203
204 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
205 #define UPCALL_EXPIRE 6 /* number of timeouts */
206
207 /*
208 * Define the token bucket filter structures
209 */
210
211 #define TBF_REPROCESS (hz / 100) /* 100x / second */
212
213 static int get_sg_cnt __P((struct sioc_sg_req *));
214 static int get_vif_cnt __P((struct sioc_vif_req *));
215 static int ip_mrouter_init __P((struct socket *, struct mbuf *));
216 static int get_version __P((struct mbuf *));
217 static int set_assert __P((struct mbuf *));
218 static int get_assert __P((struct mbuf *));
219 static int add_vif __P((struct mbuf *));
220 static int del_vif __P((struct mbuf *));
221 static void update_mfc_params __P((struct mfc *, struct mfcctl2 *));
222 static void init_mfc_params __P((struct mfc *, struct mfcctl2 *));
223 static void expire_mfc __P((struct mfc *));
224 static int add_mfc __P((struct mbuf *));
225 #ifdef UPCALL_TIMING
226 static void collate __P((struct timeval *));
227 #endif
228 static int del_mfc __P((struct mbuf *));
229 static int set_api_config __P((struct mbuf *)); /* chose API capabilities */
230 static int get_api_support __P((struct mbuf *));
231 static int get_api_config __P((struct mbuf *));
232 static int socket_send __P((struct socket *, struct mbuf *,
233 struct sockaddr_in *));
234 static void expire_upcalls __P((void *));
235 #ifdef RSVP_ISI
236 static int ip_mdq __P((struct mbuf *, struct ifnet *, struct mfc *, vifi_t));
237 #else
238 static int ip_mdq __P((struct mbuf *, struct ifnet *, struct mfc *));
239 #endif
240 static void phyint_send __P((struct ip *, struct vif *, struct mbuf *));
241 static void encap_send __P((struct ip *, struct vif *, struct mbuf *));
242 static void tbf_control __P((struct vif *, struct mbuf *, struct ip *,
243 u_int32_t));
244 static void tbf_queue __P((struct vif *, struct mbuf *));
245 static void tbf_process_q __P((struct vif *));
246 static void tbf_reprocess_q __P((void *));
247 static int tbf_dq_sel __P((struct vif *, struct ip *));
248 static void tbf_send_packet __P((struct vif *, struct mbuf *));
249 static void tbf_update_tokens __P((struct vif *));
250 static int priority __P((struct vif *, struct ip *));
251
252 /*
253 * Bandwidth monitoring
254 */
255 static void free_bw_list __P((struct bw_meter *));
256 static int add_bw_upcall __P((struct mbuf *));
257 static int del_bw_upcall __P((struct mbuf *));
258 static void bw_meter_receive_packet __P((struct bw_meter *, int , struct timeval *));
259 static void bw_meter_prepare_upcall __P((struct bw_meter *, struct timeval *));
260 static void bw_upcalls_send __P((void));
261 static void schedule_bw_meter __P((struct bw_meter *, struct timeval *));
262 static void unschedule_bw_meter __P((struct bw_meter *));
263 static void bw_meter_process __P((void));
264 static void expire_bw_upcalls_send __P((void *));
265 static void expire_bw_meter_process __P((void *));
266
267 #ifdef PIM
268 static int pim_register_send __P((struct ip *, struct vif *,
269 struct mbuf *, struct mfc *));
270 static int pim_register_send_rp __P((struct ip *, struct vif *,
271 struct mbuf *, struct mfc *));
272 static int pim_register_send_upcall __P((struct ip *, struct vif *,
273 struct mbuf *, struct mfc *));
274 static struct mbuf *pim_register_prepare __P((struct ip *, struct mbuf *));
275 #endif
276
277 /*
278 * 'Interfaces' associated with decapsulator (so we can tell
279 * packets that went through it from ones that get reflected
280 * by a broken gateway). These interfaces are never linked into
281 * the system ifnet list & no routes point to them. I.e., packets
282 * can't be sent this way. They only exist as a placeholder for
283 * multicast source verification.
284 */
285 #if 0
286 struct ifnet multicast_decap_if[MAXVIFS];
287 #endif
288
289 #define ENCAP_TTL 64
290 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
291
292 /* prototype IP hdr for encapsulated packets */
293 struct ip multicast_encap_iphdr = {
294 #if BYTE_ORDER == LITTLE_ENDIAN
295 sizeof(struct ip) >> 2, IPVERSION,
296 #else
297 IPVERSION, sizeof(struct ip) >> 2,
298 #endif
299 0, /* tos */
300 sizeof(struct ip), /* total length */
301 0, /* id */
302 0, /* frag offset */
303 ENCAP_TTL, ENCAP_PROTO,
304 0, /* checksum */
305 };
306
307 /*
308 * Bandwidth meter variables and constants
309 */
310
311 /*
312 * Pending timeouts are stored in a hash table, the key being the
313 * expiration time. Periodically, the entries are analysed and processed.
314 */
315 #define BW_METER_BUCKETS 1024
316 static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS];
317 struct callout bw_meter_ch;
318 #define BW_METER_PERIOD (hz) /* periodical handling of bw meters */
319
320 /*
321 * Pending upcalls are stored in a vector which is flushed when
322 * full, or periodically
323 */
324 static struct bw_upcall bw_upcalls[BW_UPCALLS_MAX];
325 static u_int bw_upcalls_n; /* # of pending upcalls */
326 struct callout bw_upcalls_ch;
327 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
328
329 #ifdef PIM
330 struct pimstat pimstat;
331
332 /*
333 * Note: the PIM Register encapsulation adds the following in front of a
334 * data packet:
335 *
336 * struct pim_encap_hdr {
337 * struct ip ip;
338 * struct pim_encap_pimhdr pim;
339 * }
340 *
341 */
342
343 struct pim_encap_pimhdr {
344 struct pim pim;
345 uint32_t flags;
346 };
347
348 static struct ip pim_encap_iphdr = {
349 #if BYTE_ORDER == LITTLE_ENDIAN
350 sizeof(struct ip) >> 2,
351 IPVERSION,
352 #else
353 IPVERSION,
354 sizeof(struct ip) >> 2,
355 #endif
356 0, /* tos */
357 sizeof(struct ip), /* total length */
358 0, /* id */
359 0, /* frag offset */
360 ENCAP_TTL,
361 IPPROTO_PIM,
362 0, /* checksum */
363 };
364
365 static struct pim_encap_pimhdr pim_encap_pimhdr = {
366 {
367 PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
368 0, /* reserved */
369 0, /* checksum */
370 },
371 0 /* flags */
372 };
373
374 static struct ifnet multicast_register_if;
375 static vifi_t reg_vif_num = VIFI_INVALID;
376 #endif /* PIM */
377
378
379 /*
380 * Private variables.
381 */
382 static vifi_t numvifs = 0;
383
384 static struct callout expire_upcalls_ch;
385
386 /*
387 * one-back cache used by vif_encapcheck to locate a tunnel's vif
388 * given a datagram's src ip address.
389 */
390 static struct in_addr last_encap_src;
391 static struct vif *last_encap_vif;
392
393 /*
394 * whether or not special PIM assert processing is enabled.
395 */
396 static int pim_assert;
397 /*
398 * Rate limit for assert notification messages, in usec
399 */
400 #define ASSERT_MSG_TIME 3000000
401
402 /*
403 * Kernel multicast routing API capabilities and setup.
404 * If more API capabilities are added to the kernel, they should be
405 * recorded in `mrt_api_support'.
406 */
407 static const u_int32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
408 MRT_MFC_FLAGS_BORDER_VIF |
409 MRT_MFC_RP |
410 MRT_MFC_BW_UPCALL);
411 static u_int32_t mrt_api_config = 0;
412
413 /*
414 * Find a route for a given origin IP address and Multicast group address
415 * Type of service parameter to be added in the future!!!
416 * Statistics are updated by the caller if needed
417 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
418 */
419 static struct mfc *
420 mfc_find(struct in_addr *o, struct in_addr *g)
421 {
422 struct mfc *rt;
423
424 LIST_FOREACH(rt, &mfchashtbl[MFCHASH(*o, *g)], mfc_hash) {
425 if (in_hosteq(rt->mfc_origin, *o) &&
426 in_hosteq(rt->mfc_mcastgrp, *g) &&
427 (rt->mfc_stall == NULL))
428 break;
429 }
430
431 return (rt);
432 }
433
434 /*
435 * Macros to compute elapsed time efficiently
436 * Borrowed from Van Jacobson's scheduling code
437 */
438 #define TV_DELTA(a, b, delta) do { \
439 int xxs; \
440 delta = (a).tv_usec - (b).tv_usec; \
441 xxs = (a).tv_sec - (b).tv_sec; \
442 switch (xxs) { \
443 case 2: \
444 delta += 1000000; \
445 /* fall through */ \
446 case 1: \
447 delta += 1000000; \
448 /* fall through */ \
449 case 0: \
450 break; \
451 default: \
452 delta += (1000000 * xxs); \
453 break; \
454 } \
455 } while (/*CONSTCOND*/ 0)
456
457 #ifdef UPCALL_TIMING
458 u_int32_t upcall_data[51];
459 #endif /* UPCALL_TIMING */
460
461 /*
462 * Handle MRT setsockopt commands to modify the multicast routing tables.
463 */
464 int
465 ip_mrouter_set(so, optname, m)
466 struct socket *so;
467 int optname;
468 struct mbuf **m;
469 {
470 int error;
471
472 if (optname != MRT_INIT && so != ip_mrouter)
473 error = ENOPROTOOPT;
474 else
475 switch (optname) {
476 case MRT_INIT:
477 error = ip_mrouter_init(so, *m);
478 break;
479 case MRT_DONE:
480 error = ip_mrouter_done();
481 break;
482 case MRT_ADD_VIF:
483 error = add_vif(*m);
484 break;
485 case MRT_DEL_VIF:
486 error = del_vif(*m);
487 break;
488 case MRT_ADD_MFC:
489 error = add_mfc(*m);
490 break;
491 case MRT_DEL_MFC:
492 error = del_mfc(*m);
493 break;
494 case MRT_ASSERT:
495 error = set_assert(*m);
496 break;
497 case MRT_API_CONFIG:
498 error = set_api_config(*m);
499 break;
500 case MRT_ADD_BW_UPCALL:
501 error = add_bw_upcall(*m);
502 break;
503 case MRT_DEL_BW_UPCALL:
504 error = del_bw_upcall(*m);
505 break;
506 default:
507 error = ENOPROTOOPT;
508 break;
509 }
510
511 if (*m)
512 m_free(*m);
513 return (error);
514 }
515
516 /*
517 * Handle MRT getsockopt commands
518 */
519 int
520 ip_mrouter_get(so, optname, m)
521 struct socket *so;
522 int optname;
523 struct mbuf **m;
524 {
525 int error;
526
527 if (so != ip_mrouter)
528 error = ENOPROTOOPT;
529 else {
530 *m = m_get(M_WAIT, MT_SOOPTS);
531 MCLAIM(*m, so->so_mowner);
532
533 switch (optname) {
534 case MRT_VERSION:
535 error = get_version(*m);
536 break;
537 case MRT_ASSERT:
538 error = get_assert(*m);
539 break;
540 case MRT_API_SUPPORT:
541 error = get_api_support(*m);
542 break;
543 case MRT_API_CONFIG:
544 error = get_api_config(*m);
545 break;
546 default:
547 error = ENOPROTOOPT;
548 break;
549 }
550
551 if (error)
552 m_free(*m);
553 }
554
555 return (error);
556 }
557
558 /*
559 * Handle ioctl commands to obtain information from the cache
560 */
561 int
562 mrt_ioctl(so, cmd, data)
563 struct socket *so;
564 u_long cmd;
565 caddr_t data;
566 {
567 int error;
568
569 if (so != ip_mrouter)
570 error = EINVAL;
571 else
572 switch (cmd) {
573 case SIOCGETVIFCNT:
574 error = get_vif_cnt((struct sioc_vif_req *)data);
575 break;
576 case SIOCGETSGCNT:
577 error = get_sg_cnt((struct sioc_sg_req *)data);
578 break;
579 default:
580 error = EINVAL;
581 break;
582 }
583
584 return (error);
585 }
586
587 /*
588 * returns the packet, byte, rpf-failure count for the source group provided
589 */
590 static int
591 get_sg_cnt(req)
592 struct sioc_sg_req *req;
593 {
594 int s;
595 struct mfc *rt;
596
597 s = splsoftnet();
598 rt = mfc_find(&req->src, &req->grp);
599 if (rt == NULL) {
600 splx(s);
601 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
602 return (EADDRNOTAVAIL);
603 }
604 req->pktcnt = rt->mfc_pkt_cnt;
605 req->bytecnt = rt->mfc_byte_cnt;
606 req->wrong_if = rt->mfc_wrong_if;
607 splx(s);
608
609 return (0);
610 }
611
612 /*
613 * returns the input and output packet and byte counts on the vif provided
614 */
615 static int
616 get_vif_cnt(req)
617 struct sioc_vif_req *req;
618 {
619 vifi_t vifi = req->vifi;
620
621 if (vifi >= numvifs)
622 return (EINVAL);
623
624 req->icount = viftable[vifi].v_pkt_in;
625 req->ocount = viftable[vifi].v_pkt_out;
626 req->ibytes = viftable[vifi].v_bytes_in;
627 req->obytes = viftable[vifi].v_bytes_out;
628
629 return (0);
630 }
631
632 /*
633 * Enable multicast routing
634 */
635 static int
636 ip_mrouter_init(so, m)
637 struct socket *so;
638 struct mbuf *m;
639 {
640 int *v;
641
642 if (mrtdebug)
643 log(LOG_DEBUG,
644 "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
645 so->so_type, so->so_proto->pr_protocol);
646
647 if (so->so_type != SOCK_RAW ||
648 so->so_proto->pr_protocol != IPPROTO_IGMP)
649 return (EOPNOTSUPP);
650
651 if (m == NULL || m->m_len < sizeof(int))
652 return (EINVAL);
653
654 v = mtod(m, int *);
655 if (*v != 1)
656 return (EINVAL);
657
658 if (ip_mrouter != NULL)
659 return (EADDRINUSE);
660
661 ip_mrouter = so;
662
663 mfchashtbl =
664 hashinit(MFCTBLSIZ, HASH_LIST, M_MRTABLE, M_WAITOK, &mfchash);
665 bzero((caddr_t)nexpire, sizeof(nexpire));
666
667 pim_assert = 0;
668
669 callout_init(&expire_upcalls_ch);
670 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
671 expire_upcalls, NULL);
672
673 callout_init(&bw_upcalls_ch);
674 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
675 expire_bw_upcalls_send, NULL);
676
677 callout_init(&bw_meter_ch);
678 callout_reset(&bw_meter_ch, BW_METER_PERIOD,
679 expire_bw_meter_process, NULL);
680
681 if (mrtdebug)
682 log(LOG_DEBUG, "ip_mrouter_init\n");
683
684 return (0);
685 }
686
687 /*
688 * Disable multicast routing
689 */
690 int
691 ip_mrouter_done()
692 {
693 vifi_t vifi;
694 struct vif *vifp;
695 int i;
696 int s;
697
698 s = splsoftnet();
699
700 /* Clear out all the vifs currently in use. */
701 for (vifi = 0; vifi < numvifs; vifi++) {
702 vifp = &viftable[vifi];
703 if (!in_nullhost(vifp->v_lcl_addr))
704 reset_vif(vifp);
705 }
706
707 numvifs = 0;
708 pim_assert = 0;
709 mrt_api_config = 0;
710
711 callout_stop(&expire_upcalls_ch);
712 callout_stop(&bw_upcalls_ch);
713 callout_stop(&bw_meter_ch);
714
715 /*
716 * Free all multicast forwarding cache entries.
717 */
718 for (i = 0; i < MFCTBLSIZ; i++) {
719 struct mfc *rt, *nrt;
720
721 for (rt = LIST_FIRST(&mfchashtbl[i]); rt; rt = nrt) {
722 nrt = LIST_NEXT(rt, mfc_hash);
723
724 expire_mfc(rt);
725 }
726 }
727
728 bzero((caddr_t)nexpire, sizeof(nexpire));
729 free(mfchashtbl, M_MRTABLE);
730 mfchashtbl = NULL;
731
732 bw_upcalls_n = 0;
733 bzero(bw_meter_timers, sizeof(bw_meter_timers));
734
735 /* Reset de-encapsulation cache. */
736
737 ip_mrouter = NULL;
738
739 splx(s);
740
741 if (mrtdebug)
742 log(LOG_DEBUG, "ip_mrouter_done\n");
743
744 return (0);
745 }
746
747 void
748 ip_mrouter_detach(ifp)
749 struct ifnet *ifp;
750 {
751 int vifi, i;
752 struct vif *vifp;
753 struct mfc *rt;
754 struct rtdetq *rte;
755
756 /* XXX not sure about side effect to userland routing daemon */
757 for (vifi = 0; vifi < numvifs; vifi++) {
758 vifp = &viftable[vifi];
759 if (vifp->v_ifp == ifp)
760 reset_vif(vifp);
761 }
762 for (i = 0; i < MFCTBLSIZ; i++) {
763 if (nexpire[i] == 0)
764 continue;
765 LIST_FOREACH(rt, &mfchashtbl[i], mfc_hash) {
766 for (rte = rt->mfc_stall; rte; rte = rte->next) {
767 if (rte->ifp == ifp)
768 rte->ifp = NULL;
769 }
770 }
771 }
772 }
773
774 static int
775 get_version(m)
776 struct mbuf *m;
777 {
778 int *v = mtod(m, int *);
779
780 *v = 0x0305; /* XXX !!!! */
781 m->m_len = sizeof(int);
782 return (0);
783 }
784
785 /*
786 * Set PIM assert processing global
787 */
788 static int
789 set_assert(m)
790 struct mbuf *m;
791 {
792 int *i;
793
794 if (m == NULL || m->m_len < sizeof(int))
795 return (EINVAL);
796
797 i = mtod(m, int *);
798 pim_assert = !!*i;
799 return (0);
800 }
801
802 /*
803 * Get PIM assert processing global
804 */
805 static int
806 get_assert(m)
807 struct mbuf *m;
808 {
809 int *i = mtod(m, int *);
810
811 *i = pim_assert;
812 m->m_len = sizeof(int);
813 return (0);
814 }
815
816 /*
817 * Configure API capabilities
818 */
819 static int
820 set_api_config(struct mbuf *m)
821 {
822 int i;
823 u_int32_t *apival;
824
825 if (m == NULL || m->m_len < sizeof(u_int32_t))
826 return (EINVAL);
827
828 apival = mtod(m, u_int32_t *);
829
830 /*
831 * We can set the API capabilities only if it is the first operation
832 * after MRT_INIT. I.e.:
833 * - there are no vifs installed
834 * - pim_assert is not enabled
835 * - the MFC table is empty
836 */
837 if (numvifs > 0) {
838 *apival = 0;
839 return (EPERM);
840 }
841 if (pim_assert) {
842 *apival = 0;
843 return (EPERM);
844 }
845 for (i = 0; i < MFCTBLSIZ; i++) {
846 if (LIST_FIRST(&mfchashtbl[i]) != NULL) {
847 *apival = 0;
848 return (EPERM);
849 }
850 }
851
852 mrt_api_config = *apival & mrt_api_support;
853 *apival = mrt_api_config;
854
855 return (0);
856 }
857
858 /*
859 * Get API capabilities
860 */
861 static int
862 get_api_support(struct mbuf *m)
863 {
864 u_int32_t *apival;
865
866 if (m == NULL || m->m_len < sizeof(u_int32_t))
867 return (EINVAL);
868
869 apival = mtod(m, u_int32_t *);
870
871 *apival = mrt_api_support;
872
873 return (0);
874 }
875
876 /*
877 * Get API configured capabilities
878 */
879 static int
880 get_api_config(struct mbuf *m)
881 {
882 u_int32_t *apival;
883
884 if (m == NULL || m->m_len < sizeof(u_int32_t))
885 return (EINVAL);
886
887 apival = mtod(m, u_int32_t *);
888
889 *apival = mrt_api_config;
890
891 return (0);
892 }
893
894 static struct sockaddr_in sin = { sizeof(sin), AF_INET };
895
896 /*
897 * Add a vif to the vif table
898 */
899 static int
900 add_vif(m)
901 struct mbuf *m;
902 {
903 struct vifctl *vifcp;
904 struct vif *vifp;
905 struct ifaddr *ifa;
906 struct ifnet *ifp;
907 struct ifreq ifr;
908 int error, s;
909
910 if (m == NULL || m->m_len < sizeof(struct vifctl))
911 return (EINVAL);
912
913 vifcp = mtod(m, struct vifctl *);
914 if (vifcp->vifc_vifi >= MAXVIFS)
915 return (EINVAL);
916 if (in_nullhost(vifcp->vifc_lcl_addr))
917 return (EADDRNOTAVAIL);
918
919 vifp = &viftable[vifcp->vifc_vifi];
920 if (!in_nullhost(vifp->v_lcl_addr))
921 return (EADDRINUSE);
922
923 /* Find the interface with an address in AF_INET family. */
924 #ifdef PIM
925 if (vifcp->vifc_flags & VIFF_REGISTER) {
926 /*
927 * XXX: Because VIFF_REGISTER does not really need a valid
928 * local interface (e.g. it could be 127.0.0.2), we don't
929 * check its address.
930 */
931 ifp = NULL;
932 } else
933 #endif
934 {
935 sin.sin_addr = vifcp->vifc_lcl_addr;
936 ifa = ifa_ifwithaddr(sintosa(&sin));
937 if (ifa == NULL)
938 return (EADDRNOTAVAIL);
939 ifp = ifa->ifa_ifp;
940 }
941
942 if (vifcp->vifc_flags & VIFF_TUNNEL) {
943 if (vifcp->vifc_flags & VIFF_SRCRT) {
944 log(LOG_ERR, "source routed tunnels not supported\n");
945 return (EOPNOTSUPP);
946 }
947
948 /* attach this vif to decapsulator dispatch table */
949 vifp->v_encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV4,
950 vif_encapcheck, &vif_protosw, vifp);
951 if (!vifp->v_encap_cookie)
952 return (EINVAL);
953
954 /* Create a fake encapsulation interface. */
955 ifp = (struct ifnet *)malloc(sizeof(*ifp), M_MRTABLE, M_WAITOK);
956 bzero(ifp, sizeof(*ifp));
957 snprintf(ifp->if_xname, sizeof(ifp->if_xname),
958 "mdecap%d", vifcp->vifc_vifi);
959
960 /* Prepare cached route entry. */
961 bzero(&vifp->v_route, sizeof(vifp->v_route));
962 #ifdef PIM
963 } else if (vifcp->vifc_flags & VIFF_REGISTER) {
964 ifp = &multicast_register_if;
965 if (mrtdebug)
966 log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
967 (void *)ifp);
968 if (reg_vif_num == VIFI_INVALID) {
969 bzero(ifp, sizeof(*ifp));
970 snprintf(ifp->if_xname, sizeof(ifp->if_xname),
971 "register_vif");
972 ifp->if_flags = IFF_LOOPBACK;
973 bzero(&vifp->v_route, sizeof(vifp->v_route));
974 reg_vif_num = vifcp->vifc_vifi;
975 }
976 #endif
977 } else {
978 /* Make sure the interface supports multicast. */
979 if ((ifp->if_flags & IFF_MULTICAST) == 0)
980 return (EOPNOTSUPP);
981
982 /* Enable promiscuous reception of all IP multicasts. */
983 satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
984 satosin(&ifr.ifr_addr)->sin_family = AF_INET;
985 satosin(&ifr.ifr_addr)->sin_addr = zeroin_addr;
986 error = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, (caddr_t)&ifr);
987 if (error)
988 return (error);
989 }
990
991 s = splsoftnet();
992
993 /* Define parameters for the tbf structure. */
994 vifp->tbf_q = NULL;
995 vifp->tbf_t = &vifp->tbf_q;
996 microtime(&vifp->tbf_last_pkt_t);
997 vifp->tbf_n_tok = 0;
998 vifp->tbf_q_len = 0;
999 vifp->tbf_max_q_len = MAXQSIZE;
1000
1001 vifp->v_flags = vifcp->vifc_flags;
1002 vifp->v_threshold = vifcp->vifc_threshold;
1003 /* scaling up here allows division by 1024 in critical code */
1004 vifp->v_rate_limit = vifcp->vifc_rate_limit * 1024 / 1000;
1005 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
1006 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
1007 vifp->v_ifp = ifp;
1008 /* Initialize per vif pkt counters. */
1009 vifp->v_pkt_in = 0;
1010 vifp->v_pkt_out = 0;
1011 vifp->v_bytes_in = 0;
1012 vifp->v_bytes_out = 0;
1013
1014 callout_init(&vifp->v_repq_ch);
1015
1016 #ifdef RSVP_ISI
1017 vifp->v_rsvp_on = 0;
1018 vifp->v_rsvpd = NULL;
1019 #endif /* RSVP_ISI */
1020
1021 splx(s);
1022
1023 /* Adjust numvifs up if the vifi is higher than numvifs. */
1024 if (numvifs <= vifcp->vifc_vifi)
1025 numvifs = vifcp->vifc_vifi + 1;
1026
1027 if (mrtdebug)
1028 log(LOG_DEBUG, "add_vif #%d, lcladdr %x, %s %x, thresh %x, rate %d\n",
1029 vifcp->vifc_vifi,
1030 ntohl(vifcp->vifc_lcl_addr.s_addr),
1031 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
1032 ntohl(vifcp->vifc_rmt_addr.s_addr),
1033 vifcp->vifc_threshold,
1034 vifcp->vifc_rate_limit);
1035
1036 return (0);
1037 }
1038
1039 void
1040 reset_vif(vifp)
1041 struct vif *vifp;
1042 {
1043 struct mbuf *m, *n;
1044 struct ifnet *ifp;
1045 struct ifreq ifr;
1046
1047 callout_stop(&vifp->v_repq_ch);
1048
1049 /* detach this vif from decapsulator dispatch table */
1050 encap_detach(vifp->v_encap_cookie);
1051 vifp->v_encap_cookie = NULL;
1052
1053 /*
1054 * Free packets queued at the interface
1055 */
1056 for (m = vifp->tbf_q; m != NULL; m = n) {
1057 n = m->m_nextpkt;
1058 m_freem(m);
1059 }
1060
1061 if (vifp->v_flags & VIFF_TUNNEL) {
1062 free(vifp->v_ifp, M_MRTABLE);
1063 if (vifp == last_encap_vif) {
1064 last_encap_vif = NULL;
1065 last_encap_src = zeroin_addr;
1066 }
1067 } else if (vifp->v_flags & VIFF_REGISTER) {
1068 #ifdef PIM
1069 reg_vif_num = VIFI_INVALID;
1070 #endif
1071 } else {
1072 satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
1073 satosin(&ifr.ifr_addr)->sin_family = AF_INET;
1074 satosin(&ifr.ifr_addr)->sin_addr = zeroin_addr;
1075 ifp = vifp->v_ifp;
1076 (*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)&ifr);
1077 }
1078 bzero((caddr_t)vifp, sizeof(*vifp));
1079 }
1080
1081 /*
1082 * Delete a vif from the vif table
1083 */
1084 static int
1085 del_vif(m)
1086 struct mbuf *m;
1087 {
1088 vifi_t *vifip;
1089 struct vif *vifp;
1090 vifi_t vifi;
1091 int s;
1092
1093 if (m == NULL || m->m_len < sizeof(vifi_t))
1094 return (EINVAL);
1095
1096 vifip = mtod(m, vifi_t *);
1097 if (*vifip >= numvifs)
1098 return (EINVAL);
1099
1100 vifp = &viftable[*vifip];
1101 if (in_nullhost(vifp->v_lcl_addr))
1102 return (EADDRNOTAVAIL);
1103
1104 s = splsoftnet();
1105
1106 reset_vif(vifp);
1107
1108 /* Adjust numvifs down */
1109 for (vifi = numvifs; vifi > 0; vifi--)
1110 if (!in_nullhost(viftable[vifi - 1].v_lcl_addr))
1111 break;
1112 numvifs = vifi;
1113
1114 splx(s);
1115
1116 if (mrtdebug)
1117 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", *vifip, numvifs);
1118
1119 return (0);
1120 }
1121
1122 /*
1123 * update an mfc entry without resetting counters and S,G addresses.
1124 */
1125 static void
1126 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1127 {
1128 int i;
1129
1130 rt->mfc_parent = mfccp->mfcc_parent;
1131 for (i = 0; i < numvifs; i++) {
1132 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1133 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
1134 MRT_MFC_FLAGS_ALL;
1135 }
1136 /* set the RP address */
1137 if (mrt_api_config & MRT_MFC_RP)
1138 rt->mfc_rp = mfccp->mfcc_rp;
1139 else
1140 rt->mfc_rp = zeroin_addr;
1141 }
1142
1143 /*
1144 * fully initialize an mfc entry from the parameter.
1145 */
1146 static void
1147 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1148 {
1149 rt->mfc_origin = mfccp->mfcc_origin;
1150 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
1151
1152 update_mfc_params(rt, mfccp);
1153
1154 /* initialize pkt counters per src-grp */
1155 rt->mfc_pkt_cnt = 0;
1156 rt->mfc_byte_cnt = 0;
1157 rt->mfc_wrong_if = 0;
1158 timerclear(&rt->mfc_last_assert);
1159 }
1160
1161 static void
1162 expire_mfc(rt)
1163 struct mfc *rt;
1164 {
1165 struct rtdetq *rte, *nrte;
1166
1167 free_bw_list(rt->mfc_bw_meter);
1168
1169 for (rte = rt->mfc_stall; rte != NULL; rte = nrte) {
1170 nrte = rte->next;
1171 m_freem(rte->m);
1172 free(rte, M_MRTABLE);
1173 }
1174
1175 LIST_REMOVE(rt, mfc_hash);
1176 free(rt, M_MRTABLE);
1177 }
1178
1179 /*
1180 * Add an mfc entry
1181 */
1182 static int
1183 add_mfc(m)
1184 struct mbuf *m;
1185 {
1186 struct mfcctl2 mfcctl2;
1187 struct mfcctl2 *mfccp;
1188 struct mfc *rt;
1189 u_int32_t hash = 0;
1190 struct rtdetq *rte, *nrte;
1191 u_short nstl;
1192 int s;
1193 int mfcctl_size = sizeof(struct mfcctl);
1194
1195 if (mrt_api_config & MRT_API_FLAGS_ALL)
1196 mfcctl_size = sizeof(struct mfcctl2);
1197
1198 if (m == NULL || m->m_len < mfcctl_size)
1199 return (EINVAL);
1200
1201 /*
1202 * select data size depending on API version.
1203 */
1204 if (mrt_api_config & MRT_API_FLAGS_ALL) {
1205 struct mfcctl2 *mp2 = mtod(m, struct mfcctl2 *);
1206 bcopy(mp2, (caddr_t)&mfcctl2, sizeof(*mp2));
1207 } else {
1208 struct mfcctl *mp = mtod(m, struct mfcctl *);
1209 bcopy(mp, (caddr_t)&mfcctl2, sizeof(*mp));
1210 bzero((caddr_t)&mfcctl2 + sizeof(struct mfcctl),
1211 sizeof(mfcctl2) - sizeof(struct mfcctl));
1212 }
1213 mfccp = &mfcctl2;
1214
1215 s = splsoftnet();
1216 rt = mfc_find(&mfccp->mfcc_origin, &mfccp->mfcc_mcastgrp);
1217
1218 /* If an entry already exists, just update the fields */
1219 if (rt) {
1220 if (mrtdebug & DEBUG_MFC)
1221 log(LOG_DEBUG, "add_mfc update o %x g %x p %x\n",
1222 ntohl(mfccp->mfcc_origin.s_addr),
1223 ntohl(mfccp->mfcc_mcastgrp.s_addr),
1224 mfccp->mfcc_parent);
1225
1226 update_mfc_params(rt, mfccp);
1227
1228 splx(s);
1229 return (0);
1230 }
1231
1232 /*
1233 * Find the entry for which the upcall was made and update
1234 */
1235 nstl = 0;
1236 hash = MFCHASH(mfccp->mfcc_origin, mfccp->mfcc_mcastgrp);
1237 LIST_FOREACH(rt, &mfchashtbl[hash], mfc_hash) {
1238 if (in_hosteq(rt->mfc_origin, mfccp->mfcc_origin) &&
1239 in_hosteq(rt->mfc_mcastgrp, mfccp->mfcc_mcastgrp) &&
1240 rt->mfc_stall != NULL) {
1241 if (nstl++)
1242 log(LOG_ERR, "add_mfc %s o %x g %x p %x dbx %p\n",
1243 "multiple kernel entries",
1244 ntohl(mfccp->mfcc_origin.s_addr),
1245 ntohl(mfccp->mfcc_mcastgrp.s_addr),
1246 mfccp->mfcc_parent, rt->mfc_stall);
1247
1248 if (mrtdebug & DEBUG_MFC)
1249 log(LOG_DEBUG, "add_mfc o %x g %x p %x dbg %p\n",
1250 ntohl(mfccp->mfcc_origin.s_addr),
1251 ntohl(mfccp->mfcc_mcastgrp.s_addr),
1252 mfccp->mfcc_parent, rt->mfc_stall);
1253
1254 rte = rt->mfc_stall;
1255 init_mfc_params(rt, mfccp);
1256 rt->mfc_stall = NULL;
1257
1258 rt->mfc_expire = 0; /* Don't clean this guy up */
1259 nexpire[hash]--;
1260
1261 /* free packets Qed at the end of this entry */
1262 for (; rte != NULL; rte = nrte) {
1263 nrte = rte->next;
1264 if (rte->ifp) {
1265 #ifdef RSVP_ISI
1266 ip_mdq(rte->m, rte->ifp, rt, -1);
1267 #else
1268 ip_mdq(rte->m, rte->ifp, rt);
1269 #endif /* RSVP_ISI */
1270 }
1271 m_freem(rte->m);
1272 #ifdef UPCALL_TIMING
1273 collate(&rte->t);
1274 #endif /* UPCALL_TIMING */
1275 free(rte, M_MRTABLE);
1276 }
1277 }
1278 }
1279
1280 /*
1281 * It is possible that an entry is being inserted without an upcall
1282 */
1283 if (nstl == 0) {
1284 /*
1285 * No mfc; make a new one
1286 */
1287 if (mrtdebug & DEBUG_MFC)
1288 log(LOG_DEBUG, "add_mfc no upcall o %x g %x p %x\n",
1289 ntohl(mfccp->mfcc_origin.s_addr),
1290 ntohl(mfccp->mfcc_mcastgrp.s_addr),
1291 mfccp->mfcc_parent);
1292
1293 LIST_FOREACH(rt, &mfchashtbl[hash], mfc_hash) {
1294 if (in_hosteq(rt->mfc_origin, mfccp->mfcc_origin) &&
1295 in_hosteq(rt->mfc_mcastgrp, mfccp->mfcc_mcastgrp)) {
1296 init_mfc_params(rt, mfccp);
1297 if (rt->mfc_expire)
1298 nexpire[hash]--;
1299 rt->mfc_expire = 0;
1300 break; /* XXX */
1301 }
1302 }
1303 if (rt == NULL) { /* no upcall, so make a new entry */
1304 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE,
1305 M_NOWAIT);
1306 if (rt == NULL) {
1307 splx(s);
1308 return (ENOBUFS);
1309 }
1310
1311 init_mfc_params(rt, mfccp);
1312 rt->mfc_expire = 0;
1313 rt->mfc_stall = NULL;
1314 rt->mfc_bw_meter = NULL;
1315
1316 /* insert new entry at head of hash chain */
1317 LIST_INSERT_HEAD(&mfchashtbl[hash], rt, mfc_hash);
1318 }
1319 }
1320
1321 splx(s);
1322 return (0);
1323 }
1324
1325 #ifdef UPCALL_TIMING
1326 /*
1327 * collect delay statistics on the upcalls
1328 */
1329 static void
1330 collate(t)
1331 struct timeval *t;
1332 {
1333 u_int32_t d;
1334 struct timeval tp;
1335 u_int32_t delta;
1336
1337 microtime(&tp);
1338
1339 if (timercmp(t, &tp, <)) {
1340 TV_DELTA(tp, *t, delta);
1341
1342 d = delta >> 10;
1343 if (d > 50)
1344 d = 50;
1345
1346 ++upcall_data[d];
1347 }
1348 }
1349 #endif /* UPCALL_TIMING */
1350
1351 /*
1352 * Delete an mfc entry
1353 */
1354 static int
1355 del_mfc(m)
1356 struct mbuf *m;
1357 {
1358 struct mfcctl2 mfcctl2;
1359 struct mfcctl2 *mfccp;
1360 struct mfc *rt;
1361 int s;
1362 int mfcctl_size = sizeof(struct mfcctl);
1363 struct mfcctl *mp = mtod(m, struct mfcctl *);
1364
1365 /*
1366 * XXX: for deleting MFC entries the information in entries
1367 * of size "struct mfcctl" is sufficient.
1368 */
1369
1370 if (m == NULL || m->m_len < mfcctl_size)
1371 return (EINVAL);
1372
1373 bcopy(mp, (caddr_t)&mfcctl2, sizeof(*mp));
1374 bzero((caddr_t)&mfcctl2 + sizeof(struct mfcctl),
1375 sizeof(mfcctl2) - sizeof(struct mfcctl));
1376
1377 mfccp = &mfcctl2;
1378
1379 if (mrtdebug & DEBUG_MFC)
1380 log(LOG_DEBUG, "del_mfc origin %x mcastgrp %x\n",
1381 ntohl(mfccp->mfcc_origin.s_addr),
1382 ntohl(mfccp->mfcc_mcastgrp.s_addr));
1383
1384 s = splsoftnet();
1385
1386 rt = mfc_find(&mfccp->mfcc_origin, &mfccp->mfcc_mcastgrp);
1387 if (rt == NULL) {
1388 splx(s);
1389 return (EADDRNOTAVAIL);
1390 }
1391
1392 /*
1393 * free the bw_meter entries
1394 */
1395 free_bw_list(rt->mfc_bw_meter);
1396 rt->mfc_bw_meter = NULL;
1397
1398 LIST_REMOVE(rt, mfc_hash);
1399 free(rt, M_MRTABLE);
1400
1401 splx(s);
1402 return (0);
1403 }
1404
1405 static int
1406 socket_send(s, mm, src)
1407 struct socket *s;
1408 struct mbuf *mm;
1409 struct sockaddr_in *src;
1410 {
1411 if (s) {
1412 if (sbappendaddr(&s->so_rcv, sintosa(src), mm,
1413 (struct mbuf *)NULL) != 0) {
1414 sorwakeup(s);
1415 return (0);
1416 }
1417 }
1418 m_freem(mm);
1419 return (-1);
1420 }
1421
1422 /*
1423 * IP multicast forwarding function. This function assumes that the packet
1424 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1425 * pointed to by "ifp", and the packet is to be relayed to other networks
1426 * that have members of the packet's destination IP multicast group.
1427 *
1428 * The packet is returned unscathed to the caller, unless it is
1429 * erroneous, in which case a non-zero return value tells the caller to
1430 * discard it.
1431 */
1432
1433 #define IP_HDR_LEN 20 /* # bytes of fixed IP header (excluding options) */
1434 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1435
1436 int
1437 #ifdef RSVP_ISI
1438 ip_mforward(m, ifp, imo)
1439 #else
1440 ip_mforward(m, ifp)
1441 #endif /* RSVP_ISI */
1442 struct mbuf *m;
1443 struct ifnet *ifp;
1444 #ifdef RSVP_ISI
1445 struct ip_moptions *imo;
1446 #endif /* RSVP_ISI */
1447 {
1448 struct ip *ip = mtod(m, struct ip *);
1449 struct mfc *rt;
1450 static int srctun = 0;
1451 struct mbuf *mm;
1452 int s;
1453 vifi_t vifi;
1454
1455 if (mrtdebug & DEBUG_FORWARD)
1456 log(LOG_DEBUG, "ip_mforward: src %x, dst %x, ifp %p\n",
1457 ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr), ifp);
1458
1459 if (ip->ip_hl < (IP_HDR_LEN + TUNNEL_LEN) >> 2 ||
1460 ((u_char *)(ip + 1))[1] != IPOPT_LSRR) {
1461 /*
1462 * Packet arrived via a physical interface or
1463 * an encapsulated tunnel or a register_vif.
1464 */
1465 } else {
1466 /*
1467 * Packet arrived through a source-route tunnel.
1468 * Source-route tunnels are no longer supported.
1469 */
1470 if ((srctun++ % 1000) == 0)
1471 log(LOG_ERR,
1472 "ip_mforward: received source-routed packet from %x\n",
1473 ntohl(ip->ip_src.s_addr));
1474
1475 return (1);
1476 }
1477
1478 #ifdef RSVP_ISI
1479 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1480 if (ip->ip_ttl < 255)
1481 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1482 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1483 struct vif *vifp = viftable + vifi;
1484 printf("Sending IPPROTO_RSVP from %x to %x on vif %d (%s%s)\n",
1485 ntohl(ip->ip_src), ntohl(ip->ip_dst), vifi,
1486 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1487 vifp->v_ifp->if_xname);
1488 }
1489 return (ip_mdq(m, ifp, (struct mfc *)NULL, vifi));
1490 }
1491 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1492 printf("Warning: IPPROTO_RSVP from %x to %x without vif option\n",
1493 ntohl(ip->ip_src), ntohl(ip->ip_dst));
1494 }
1495 #endif /* RSVP_ISI */
1496
1497 /*
1498 * Don't forward a packet with time-to-live of zero or one,
1499 * or a packet destined to a local-only group.
1500 */
1501 if (ip->ip_ttl <= 1 || IN_LOCAL_GROUP(ip->ip_dst.s_addr))
1502 return (0);
1503
1504 /*
1505 * Determine forwarding vifs from the forwarding cache table
1506 */
1507 s = splsoftnet();
1508 ++mrtstat.mrts_mfc_lookups;
1509 rt = mfc_find(&ip->ip_src, &ip->ip_dst);
1510
1511 /* Entry exists, so forward if necessary */
1512 if (rt != NULL) {
1513 splx(s);
1514 #ifdef RSVP_ISI
1515 return (ip_mdq(m, ifp, rt, -1));
1516 #else
1517 return (ip_mdq(m, ifp, rt));
1518 #endif /* RSVP_ISI */
1519 } else {
1520 /*
1521 * If we don't have a route for packet's origin,
1522 * Make a copy of the packet & send message to routing daemon
1523 */
1524
1525 struct mbuf *mb0;
1526 struct rtdetq *rte;
1527 u_int32_t hash;
1528 int hlen = ip->ip_hl << 2;
1529 #ifdef UPCALL_TIMING
1530 struct timeval tp;
1531
1532 microtime(&tp);
1533 #endif /* UPCALL_TIMING */
1534
1535 ++mrtstat.mrts_mfc_misses;
1536
1537 mrtstat.mrts_no_route++;
1538 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1539 log(LOG_DEBUG, "ip_mforward: no rte s %x g %x\n",
1540 ntohl(ip->ip_src.s_addr),
1541 ntohl(ip->ip_dst.s_addr));
1542
1543 /*
1544 * Allocate mbufs early so that we don't do extra work if we are
1545 * just going to fail anyway. Make sure to pullup the header so
1546 * that other people can't step on it.
1547 */
1548 rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE,
1549 M_NOWAIT);
1550 if (rte == NULL) {
1551 splx(s);
1552 return (ENOBUFS);
1553 }
1554 mb0 = m_copy(m, 0, M_COPYALL);
1555 M_PULLUP(mb0, hlen);
1556 if (mb0 == NULL) {
1557 free(rte, M_MRTABLE);
1558 splx(s);
1559 return (ENOBUFS);
1560 }
1561
1562 /* is there an upcall waiting for this flow? */
1563 hash = MFCHASH(ip->ip_src, ip->ip_dst);
1564 LIST_FOREACH(rt, &mfchashtbl[hash], mfc_hash) {
1565 if (in_hosteq(ip->ip_src, rt->mfc_origin) &&
1566 in_hosteq(ip->ip_dst, rt->mfc_mcastgrp) &&
1567 rt->mfc_stall != NULL)
1568 break;
1569 }
1570
1571 if (rt == NULL) {
1572 int i;
1573 struct igmpmsg *im;
1574
1575 /*
1576 * Locate the vifi for the incoming interface for
1577 * this packet.
1578 * If none found, drop packet.
1579 */
1580 for (vifi = 0; vifi < numvifs &&
1581 viftable[vifi].v_ifp != ifp; vifi++)
1582 ;
1583 if (vifi >= numvifs) /* vif not found, drop packet */
1584 goto non_fatal;
1585
1586 /* no upcall, so make a new entry */
1587 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE,
1588 M_NOWAIT);
1589 if (rt == NULL)
1590 goto fail;
1591
1592 /*
1593 * Make a copy of the header to send to the user level
1594 * process
1595 */
1596 mm = m_copy(m, 0, hlen);
1597 M_PULLUP(mm, hlen);
1598 if (mm == NULL)
1599 goto fail1;
1600
1601 /*
1602 * Send message to routing daemon to install
1603 * a route into the kernel table
1604 */
1605
1606 im = mtod(mm, struct igmpmsg *);
1607 im->im_msgtype = IGMPMSG_NOCACHE;
1608 im->im_mbz = 0;
1609 im->im_vif = vifi;
1610
1611 mrtstat.mrts_upcalls++;
1612
1613 sin.sin_addr = ip->ip_src;
1614 if (socket_send(ip_mrouter, mm, &sin) < 0) {
1615 log(LOG_WARNING,
1616 "ip_mforward: ip_mrouter socket queue full\n");
1617 ++mrtstat.mrts_upq_sockfull;
1618 fail1:
1619 free(rt, M_MRTABLE);
1620 fail:
1621 free(rte, M_MRTABLE);
1622 m_freem(mb0);
1623 splx(s);
1624 return (ENOBUFS);
1625 }
1626
1627 /* insert new entry at head of hash chain */
1628 rt->mfc_origin = ip->ip_src;
1629 rt->mfc_mcastgrp = ip->ip_dst;
1630 rt->mfc_pkt_cnt = 0;
1631 rt->mfc_byte_cnt = 0;
1632 rt->mfc_wrong_if = 0;
1633 rt->mfc_expire = UPCALL_EXPIRE;
1634 nexpire[hash]++;
1635 for (i = 0; i < numvifs; i++) {
1636 rt->mfc_ttls[i] = 0;
1637 rt->mfc_flags[i] = 0;
1638 }
1639 rt->mfc_parent = -1;
1640
1641 /* clear the RP address */
1642 rt->mfc_rp = zeroin_addr;
1643
1644 rt->mfc_bw_meter = NULL;
1645
1646 /* link into table */
1647 LIST_INSERT_HEAD(&mfchashtbl[hash], rt, mfc_hash);
1648 /* Add this entry to the end of the queue */
1649 rt->mfc_stall = rte;
1650 } else {
1651 /* determine if q has overflowed */
1652 struct rtdetq **p;
1653 int npkts = 0;
1654
1655 /*
1656 * XXX ouch! we need to append to the list, but we
1657 * only have a pointer to the front, so we have to
1658 * scan the entire list every time.
1659 */
1660 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1661 if (++npkts > MAX_UPQ) {
1662 mrtstat.mrts_upq_ovflw++;
1663 non_fatal:
1664 free(rte, M_MRTABLE);
1665 m_freem(mb0);
1666 splx(s);
1667 return (0);
1668 }
1669
1670 /* Add this entry to the end of the queue */
1671 *p = rte;
1672 }
1673
1674 rte->next = NULL;
1675 rte->m = mb0;
1676 rte->ifp = ifp;
1677 #ifdef UPCALL_TIMING
1678 rte->t = tp;
1679 #endif /* UPCALL_TIMING */
1680
1681 splx(s);
1682
1683 return (0);
1684 }
1685 }
1686
1687
1688 /*ARGSUSED*/
1689 static void
1690 expire_upcalls(v)
1691 void *v;
1692 {
1693 int i;
1694 int s;
1695
1696 s = splsoftnet();
1697
1698 for (i = 0; i < MFCTBLSIZ; i++) {
1699 struct mfc *rt, *nrt;
1700
1701 if (nexpire[i] == 0)
1702 continue;
1703
1704 for (rt = LIST_FIRST(&mfchashtbl[i]); rt; rt = nrt) {
1705 nrt = LIST_NEXT(rt, mfc_hash);
1706
1707 if (rt->mfc_expire == 0 || --rt->mfc_expire > 0)
1708 continue;
1709 nexpire[i]--;
1710
1711 /*
1712 * free the bw_meter entries
1713 */
1714 while (rt->mfc_bw_meter != NULL) {
1715 struct bw_meter *x = rt->mfc_bw_meter;
1716
1717 rt->mfc_bw_meter = x->bm_mfc_next;
1718 free(x, M_BWMETER);
1719 }
1720
1721 ++mrtstat.mrts_cache_cleanups;
1722 if (mrtdebug & DEBUG_EXPIRE)
1723 log(LOG_DEBUG,
1724 "expire_upcalls: expiring (%x %x)\n",
1725 ntohl(rt->mfc_origin.s_addr),
1726 ntohl(rt->mfc_mcastgrp.s_addr));
1727
1728 expire_mfc(rt);
1729 }
1730 }
1731
1732 splx(s);
1733 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
1734 expire_upcalls, NULL);
1735 }
1736
1737 /*
1738 * Packet forwarding routine once entry in the cache is made
1739 */
1740 static int
1741 #ifdef RSVP_ISI
1742 ip_mdq(m, ifp, rt, xmt_vif)
1743 #else
1744 ip_mdq(m, ifp, rt)
1745 #endif /* RSVP_ISI */
1746 struct mbuf *m;
1747 struct ifnet *ifp;
1748 struct mfc *rt;
1749 #ifdef RSVP_ISI
1750 vifi_t xmt_vif;
1751 #endif /* RSVP_ISI */
1752 {
1753 struct ip *ip = mtod(m, struct ip *);
1754 vifi_t vifi;
1755 struct vif *vifp;
1756 int plen = ntohs(ip->ip_len) - (ip->ip_hl << 2);
1757
1758 /*
1759 * Macro to send packet on vif. Since RSVP packets don't get counted on
1760 * input, they shouldn't get counted on output, so statistics keeping is
1761 * separate.
1762 */
1763 #define MC_SEND(ip, vifp, m) do { \
1764 if ((vifp)->v_flags & VIFF_TUNNEL) \
1765 encap_send((ip), (vifp), (m)); \
1766 else \
1767 phyint_send((ip), (vifp), (m)); \
1768 } while (/*CONSTCOND*/ 0)
1769
1770 #ifdef RSVP_ISI
1771 /*
1772 * If xmt_vif is not -1, send on only the requested vif.
1773 *
1774 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.
1775 */
1776 if (xmt_vif < numvifs) {
1777 #ifdef PIM
1778 if (viftable[xmt_vif].v_flags & VIFF_REGISTER)
1779 pim_register_send(ip, viftable + xmt_vif, m, rt);
1780 else
1781 #endif
1782 MC_SEND(ip, viftable + xmt_vif, m);
1783 return (1);
1784 }
1785 #endif /* RSVP_ISI */
1786
1787 /*
1788 * Don't forward if it didn't arrive from the parent vif for its origin.
1789 */
1790 vifi = rt->mfc_parent;
1791 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1792 /* came in the wrong interface */
1793 if (mrtdebug & DEBUG_FORWARD)
1794 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1795 ifp, vifi,
1796 vifi >= numvifs ? 0 : viftable[vifi].v_ifp);
1797 ++mrtstat.mrts_wrong_if;
1798 ++rt->mfc_wrong_if;
1799 /*
1800 * If we are doing PIM assert processing, send a message
1801 * to the routing daemon.
1802 *
1803 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1804 * can complete the SPT switch, regardless of the type
1805 * of the iif (broadcast media, GRE tunnel, etc).
1806 */
1807 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) {
1808 struct timeval now;
1809 u_int32_t delta;
1810
1811 #ifdef PIM
1812 if (ifp == &multicast_register_if)
1813 pimstat.pims_rcv_registers_wrongiif++;
1814 #endif
1815
1816 /* Get vifi for the incoming packet */
1817 for (vifi = 0;
1818 vifi < numvifs && viftable[vifi].v_ifp != ifp;
1819 vifi++)
1820 ;
1821 if (vifi >= numvifs) {
1822 /* The iif is not found: ignore the packet. */
1823 return (0);
1824 }
1825
1826 if (rt->mfc_flags[vifi] &
1827 MRT_MFC_FLAGS_DISABLE_WRONGVIF) {
1828 /* WRONGVIF disabled: ignore the packet */
1829 return (0);
1830 }
1831
1832 microtime(&now);
1833
1834 TV_DELTA(rt->mfc_last_assert, now, delta);
1835
1836 if (delta > ASSERT_MSG_TIME) {
1837 struct igmpmsg *im;
1838 int hlen = ip->ip_hl << 2;
1839 struct mbuf *mm = m_copy(m, 0, hlen);
1840
1841 M_PULLUP(mm, hlen);
1842 if (mm == NULL)
1843 return (ENOBUFS);
1844
1845 rt->mfc_last_assert = now;
1846
1847 im = mtod(mm, struct igmpmsg *);
1848 im->im_msgtype = IGMPMSG_WRONGVIF;
1849 im->im_mbz = 0;
1850 im->im_vif = vifi;
1851
1852 mrtstat.mrts_upcalls++;
1853
1854 sin.sin_addr = im->im_src;
1855 if (socket_send(ip_mrouter, mm, &sin) < 0) {
1856 log(LOG_WARNING,
1857 "ip_mforward: ip_mrouter socket queue full\n");
1858 ++mrtstat.mrts_upq_sockfull;
1859 return (ENOBUFS);
1860 }
1861 }
1862 }
1863 return (0);
1864 }
1865
1866 /* If I sourced this packet, it counts as output, else it was input. */
1867 if (in_hosteq(ip->ip_src, viftable[vifi].v_lcl_addr)) {
1868 viftable[vifi].v_pkt_out++;
1869 viftable[vifi].v_bytes_out += plen;
1870 } else {
1871 viftable[vifi].v_pkt_in++;
1872 viftable[vifi].v_bytes_in += plen;
1873 }
1874 rt->mfc_pkt_cnt++;
1875 rt->mfc_byte_cnt += plen;
1876
1877 /*
1878 * For each vif, decide if a copy of the packet should be forwarded.
1879 * Forward if:
1880 * - the ttl exceeds the vif's threshold
1881 * - there are group members downstream on interface
1882 */
1883 for (vifp = viftable, vifi = 0; vifi < numvifs; vifp++, vifi++)
1884 if ((rt->mfc_ttls[vifi] > 0) &&
1885 (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1886 vifp->v_pkt_out++;
1887 vifp->v_bytes_out += plen;
1888 #ifdef PIM
1889 if (vifp->v_flags & VIFF_REGISTER)
1890 pim_register_send(ip, vifp, m, rt);
1891 else
1892 #endif
1893 MC_SEND(ip, vifp, m);
1894 }
1895
1896 /*
1897 * Perform upcall-related bw measuring.
1898 */
1899 if (rt->mfc_bw_meter != NULL) {
1900 struct bw_meter *x;
1901 struct timeval now;
1902
1903 microtime(&now);
1904 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next)
1905 bw_meter_receive_packet(x, plen, &now);
1906 }
1907
1908 return (0);
1909 }
1910
1911 #ifdef RSVP_ISI
1912 /*
1913 * check if a vif number is legal/ok. This is used by ip_output.
1914 */
1915 int
1916 legal_vif_num(vif)
1917 int vif;
1918 {
1919 if (vif >= 0 && vif < numvifs)
1920 return (1);
1921 else
1922 return (0);
1923 }
1924 #endif /* RSVP_ISI */
1925
1926 static void
1927 phyint_send(ip, vifp, m)
1928 struct ip *ip;
1929 struct vif *vifp;
1930 struct mbuf *m;
1931 {
1932 struct mbuf *mb_copy;
1933 int hlen = ip->ip_hl << 2;
1934
1935 /*
1936 * Make a new reference to the packet; make sure that
1937 * the IP header is actually copied, not just referenced,
1938 * so that ip_output() only scribbles on the copy.
1939 */
1940 mb_copy = m_copy(m, 0, M_COPYALL);
1941 M_PULLUP(mb_copy, hlen);
1942 if (mb_copy == NULL)
1943 return;
1944
1945 if (vifp->v_rate_limit <= 0)
1946 tbf_send_packet(vifp, mb_copy);
1947 else
1948 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *),
1949 ntohs(ip->ip_len));
1950 }
1951
1952 static void
1953 encap_send(ip, vifp, m)
1954 struct ip *ip;
1955 struct vif *vifp;
1956 struct mbuf *m;
1957 {
1958 struct mbuf *mb_copy;
1959 struct ip *ip_copy;
1960 int i, len = ntohs(ip->ip_len) + sizeof(multicast_encap_iphdr);
1961
1962 /* Take care of delayed checksums */
1963 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
1964 in_delayed_cksum(m);
1965 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
1966 }
1967
1968 /*
1969 * copy the old packet & pullup it's IP header into the
1970 * new mbuf so we can modify it. Try to fill the new
1971 * mbuf since if we don't the ethernet driver will.
1972 */
1973 MGETHDR(mb_copy, M_DONTWAIT, MT_DATA);
1974 if (mb_copy == NULL)
1975 return;
1976 mb_copy->m_data += max_linkhdr;
1977 mb_copy->m_pkthdr.len = len;
1978 mb_copy->m_len = sizeof(multicast_encap_iphdr);
1979
1980 if ((mb_copy->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1981 m_freem(mb_copy);
1982 return;
1983 }
1984 i = MHLEN - max_linkhdr;
1985 if (i > len)
1986 i = len;
1987 mb_copy = m_pullup(mb_copy, i);
1988 if (mb_copy == NULL)
1989 return;
1990
1991 /*
1992 * fill in the encapsulating IP header.
1993 */
1994 ip_copy = mtod(mb_copy, struct ip *);
1995 *ip_copy = multicast_encap_iphdr;
1996 ip_copy->ip_id = ip_newid();
1997 ip_copy->ip_len = htons(len);
1998 ip_copy->ip_src = vifp->v_lcl_addr;
1999 ip_copy->ip_dst = vifp->v_rmt_addr;
2000
2001 /*
2002 * turn the encapsulated IP header back into a valid one.
2003 */
2004 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
2005 --ip->ip_ttl;
2006 ip->ip_sum = 0;
2007 mb_copy->m_data += sizeof(multicast_encap_iphdr);
2008 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
2009 mb_copy->m_data -= sizeof(multicast_encap_iphdr);
2010
2011 if (vifp->v_rate_limit <= 0)
2012 tbf_send_packet(vifp, mb_copy);
2013 else
2014 tbf_control(vifp, mb_copy, ip, ntohs(ip_copy->ip_len));
2015 }
2016
2017 /*
2018 * De-encapsulate a packet and feed it back through ip input.
2019 */
2020 static void
2021 vif_input(struct mbuf *m, ...)
2022 {
2023 int off, proto;
2024 va_list ap;
2025 struct vif *vifp;
2026 int s;
2027 struct ifqueue *ifq;
2028
2029 va_start(ap, m);
2030 off = va_arg(ap, int);
2031 proto = va_arg(ap, int);
2032 va_end(ap);
2033
2034 vifp = (struct vif *)encap_getarg(m);
2035 if (!vifp || proto != AF_INET) {
2036 m_freem(m);
2037 mrtstat.mrts_bad_tunnel++;
2038 return;
2039 }
2040
2041 m_adj(m, off);
2042 m->m_pkthdr.rcvif = vifp->v_ifp;
2043 ifq = &ipintrq;
2044 s = splnet();
2045 if (IF_QFULL(ifq)) {
2046 IF_DROP(ifq);
2047 m_freem(m);
2048 } else {
2049 IF_ENQUEUE(ifq, m);
2050 /*
2051 * normally we would need a "schednetisr(NETISR_IP)"
2052 * here but we were called by ip_input and it is going
2053 * to loop back & try to dequeue the packet we just
2054 * queued as soon as we return so we avoid the
2055 * unnecessary software interrrupt.
2056 */
2057 }
2058 splx(s);
2059 }
2060
2061 /*
2062 * Check if the packet should be grabbed by us.
2063 */
2064 static int
2065 vif_encapcheck(m, off, proto, arg)
2066 const struct mbuf *m;
2067 int off;
2068 int proto;
2069 void *arg;
2070 {
2071 struct vif *vifp;
2072 struct ip ip;
2073
2074 #ifdef DIAGNOSTIC
2075 if (!arg || proto != IPPROTO_IPV4)
2076 panic("unexpected arg in vif_encapcheck");
2077 #endif
2078
2079 /*
2080 * do not grab the packet if it's not to a multicast destination or if
2081 * we don't have an encapsulating tunnel with the source.
2082 * Note: This code assumes that the remote site IP address
2083 * uniquely identifies the tunnel (i.e., that this site has
2084 * at most one tunnel with the remote site).
2085 */
2086
2087 /* LINTED const cast */
2088 m_copydata((struct mbuf *)m, off, sizeof(ip), (caddr_t)&ip);
2089 if (!IN_MULTICAST(ip.ip_dst.s_addr))
2090 return 0;
2091
2092 /* LINTED const cast */
2093 m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
2094 if (!in_hosteq(ip.ip_src, last_encap_src)) {
2095 vifp = (struct vif *)arg;
2096 if (vifp->v_flags & VIFF_TUNNEL &&
2097 in_hosteq(vifp->v_rmt_addr, ip.ip_src))
2098 ;
2099 else
2100 return 0;
2101 last_encap_vif = vifp;
2102 last_encap_src = ip.ip_src;
2103 } else
2104 vifp = last_encap_vif;
2105
2106 /* 32bit match, since we have checked ip_src only */
2107 return 32;
2108 }
2109
2110 /*
2111 * Token bucket filter module
2112 */
2113 static void
2114 tbf_control(vifp, m, ip, len)
2115 struct vif *vifp;
2116 struct mbuf *m;
2117 struct ip *ip;
2118 u_int32_t len;
2119 {
2120
2121 if (len > MAX_BKT_SIZE) {
2122 /* drop if packet is too large */
2123 mrtstat.mrts_pkt2large++;
2124 m_freem(m);
2125 return;
2126 }
2127
2128 tbf_update_tokens(vifp);
2129
2130 /*
2131 * If there are enough tokens, and the queue is empty, send this packet
2132 * out immediately. Otherwise, try to insert it on this vif's queue.
2133 */
2134 if (vifp->tbf_q_len == 0) {
2135 if (len <= vifp->tbf_n_tok) {
2136 vifp->tbf_n_tok -= len;
2137 tbf_send_packet(vifp, m);
2138 } else {
2139 /* queue packet and timeout till later */
2140 tbf_queue(vifp, m);
2141 callout_reset(&vifp->v_repq_ch, TBF_REPROCESS,
2142 tbf_reprocess_q, vifp);
2143 }
2144 } else {
2145 if (vifp->tbf_q_len >= vifp->tbf_max_q_len &&
2146 !tbf_dq_sel(vifp, ip)) {
2147 /* queue full, and couldn't make room */
2148 mrtstat.mrts_q_overflow++;
2149 m_freem(m);
2150 } else {
2151 /* queue length low enough, or made room */
2152 tbf_queue(vifp, m);
2153 tbf_process_q(vifp);
2154 }
2155 }
2156 }
2157
2158 /*
2159 * adds a packet to the queue at the interface
2160 */
2161 static void
2162 tbf_queue(vifp, m)
2163 struct vif *vifp;
2164 struct mbuf *m;
2165 {
2166 int s = splsoftnet();
2167
2168 /* insert at tail */
2169 *vifp->tbf_t = m;
2170 vifp->tbf_t = &m->m_nextpkt;
2171 vifp->tbf_q_len++;
2172
2173 splx(s);
2174 }
2175
2176
2177 /*
2178 * processes the queue at the interface
2179 */
2180 static void
2181 tbf_process_q(vifp)
2182 struct vif *vifp;
2183 {
2184 struct mbuf *m;
2185 int len;
2186 int s = splsoftnet();
2187
2188 /*
2189 * Loop through the queue at the interface and send as many packets
2190 * as possible.
2191 */
2192 for (m = vifp->tbf_q; m != NULL; m = vifp->tbf_q) {
2193 len = ntohs(mtod(m, struct ip *)->ip_len);
2194
2195 /* determine if the packet can be sent */
2196 if (len <= vifp->tbf_n_tok) {
2197 /* if so,
2198 * reduce no of tokens, dequeue the packet,
2199 * send the packet.
2200 */
2201 if ((vifp->tbf_q = m->m_nextpkt) == NULL)
2202 vifp->tbf_t = &vifp->tbf_q;
2203 --vifp->tbf_q_len;
2204
2205 m->m_nextpkt = NULL;
2206 vifp->tbf_n_tok -= len;
2207 tbf_send_packet(vifp, m);
2208 } else
2209 break;
2210 }
2211 splx(s);
2212 }
2213
2214 static void
2215 tbf_reprocess_q(arg)
2216 void *arg;
2217 {
2218 struct vif *vifp = arg;
2219
2220 if (ip_mrouter == NULL)
2221 return;
2222
2223 tbf_update_tokens(vifp);
2224 tbf_process_q(vifp);
2225
2226 if (vifp->tbf_q_len != 0)
2227 callout_reset(&vifp->v_repq_ch, TBF_REPROCESS,
2228 tbf_reprocess_q, vifp);
2229 }
2230
2231 /* function that will selectively discard a member of the queue
2232 * based on the precedence value and the priority
2233 */
2234 static int
2235 tbf_dq_sel(vifp, ip)
2236 struct vif *vifp;
2237 struct ip *ip;
2238 {
2239 u_int p;
2240 struct mbuf **mp, *m;
2241 int s = splsoftnet();
2242
2243 p = priority(vifp, ip);
2244
2245 for (mp = &vifp->tbf_q, m = *mp;
2246 m != NULL;
2247 mp = &m->m_nextpkt, m = *mp) {
2248 if (p > priority(vifp, mtod(m, struct ip *))) {
2249 if ((*mp = m->m_nextpkt) == NULL)
2250 vifp->tbf_t = mp;
2251 --vifp->tbf_q_len;
2252
2253 m_freem(m);
2254 mrtstat.mrts_drop_sel++;
2255 splx(s);
2256 return (1);
2257 }
2258 }
2259 splx(s);
2260 return (0);
2261 }
2262
2263 static void
2264 tbf_send_packet(vifp, m)
2265 struct vif *vifp;
2266 struct mbuf *m;
2267 {
2268 int error;
2269 int s = splsoftnet();
2270
2271 if (vifp->v_flags & VIFF_TUNNEL) {
2272 /* If tunnel options */
2273 ip_output(m, (struct mbuf *)NULL, &vifp->v_route,
2274 IP_FORWARDING, (struct ip_moptions *)NULL,
2275 (struct socket *)NULL);
2276 } else {
2277 /* if physical interface option, extract the options and then send */
2278 struct ip_moptions imo;
2279
2280 imo.imo_multicast_ifp = vifp->v_ifp;
2281 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
2282 imo.imo_multicast_loop = 1;
2283 #ifdef RSVP_ISI
2284 imo.imo_multicast_vif = -1;
2285 #endif
2286
2287 error = ip_output(m, (struct mbuf *)NULL, (struct route *)NULL,
2288 IP_FORWARDING|IP_MULTICASTOPTS, &imo,
2289 (struct socket *)NULL);
2290
2291 if (mrtdebug & DEBUG_XMIT)
2292 log(LOG_DEBUG, "phyint_send on vif %ld err %d\n",
2293 (long)(vifp - viftable), error);
2294 }
2295 splx(s);
2296 }
2297
2298 /* determine the current time and then
2299 * the elapsed time (between the last time and time now)
2300 * in milliseconds & update the no. of tokens in the bucket
2301 */
2302 static void
2303 tbf_update_tokens(vifp)
2304 struct vif *vifp;
2305 {
2306 struct timeval tp;
2307 u_int32_t tm;
2308 int s = splsoftnet();
2309
2310 microtime(&tp);
2311
2312 TV_DELTA(tp, vifp->tbf_last_pkt_t, tm);
2313
2314 /*
2315 * This formula is actually
2316 * "time in seconds" * "bytes/second".
2317 *
2318 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
2319 *
2320 * The (1000/1024) was introduced in add_vif to optimize
2321 * this divide into a shift.
2322 */
2323 vifp->tbf_n_tok += tm * vifp->v_rate_limit / 8192;
2324 vifp->tbf_last_pkt_t = tp;
2325
2326 if (vifp->tbf_n_tok > MAX_BKT_SIZE)
2327 vifp->tbf_n_tok = MAX_BKT_SIZE;
2328
2329 splx(s);
2330 }
2331
2332 static int
2333 priority(vifp, ip)
2334 struct vif *vifp;
2335 struct ip *ip;
2336 {
2337 int prio = 50; /* the lowest priority -- default case */
2338
2339 /* temporary hack; may add general packet classifier some day */
2340
2341 /*
2342 * The UDP port space is divided up into four priority ranges:
2343 * [0, 16384) : unclassified - lowest priority
2344 * [16384, 32768) : audio - highest priority
2345 * [32768, 49152) : whiteboard - medium priority
2346 * [49152, 65536) : video - low priority
2347 */
2348 if (ip->ip_p == IPPROTO_UDP) {
2349 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
2350
2351 switch (ntohs(udp->uh_dport) & 0xc000) {
2352 case 0x4000:
2353 prio = 70;
2354 break;
2355 case 0x8000:
2356 prio = 60;
2357 break;
2358 case 0xc000:
2359 prio = 55;
2360 break;
2361 }
2362
2363 if (tbfdebug > 1)
2364 log(LOG_DEBUG, "port %x prio %d\n",
2365 ntohs(udp->uh_dport), prio);
2366 }
2367
2368 return (prio);
2369 }
2370
2371 /*
2372 * End of token bucket filter modifications
2373 */
2374 #ifdef RSVP_ISI
2375 int
2376 ip_rsvp_vif_init(so, m)
2377 struct socket *so;
2378 struct mbuf *m;
2379 {
2380 int vifi, s;
2381
2382 if (rsvpdebug)
2383 printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
2384 so->so_type, so->so_proto->pr_protocol);
2385
2386 if (so->so_type != SOCK_RAW ||
2387 so->so_proto->pr_protocol != IPPROTO_RSVP)
2388 return (EOPNOTSUPP);
2389
2390 /* Check mbuf. */
2391 if (m == NULL || m->m_len != sizeof(int)) {
2392 return (EINVAL);
2393 }
2394 vifi = *(mtod(m, int *));
2395
2396 if (rsvpdebug)
2397 printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n",
2398 vifi, rsvp_on);
2399
2400 s = splsoftnet();
2401
2402 /* Check vif. */
2403 if (!legal_vif_num(vifi)) {
2404 splx(s);
2405 return (EADDRNOTAVAIL);
2406 }
2407
2408 /* Check if socket is available. */
2409 if (viftable[vifi].v_rsvpd != NULL) {
2410 splx(s);
2411 return (EADDRINUSE);
2412 }
2413
2414 viftable[vifi].v_rsvpd = so;
2415 /*
2416 * This may seem silly, but we need to be sure we don't over-increment
2417 * the RSVP counter, in case something slips up.
2418 */
2419 if (!viftable[vifi].v_rsvp_on) {
2420 viftable[vifi].v_rsvp_on = 1;
2421 rsvp_on++;
2422 }
2423
2424 splx(s);
2425 return (0);
2426 }
2427
2428 int
2429 ip_rsvp_vif_done(so, m)
2430 struct socket *so;
2431 struct mbuf *m;
2432 {
2433 int vifi, s;
2434
2435 if (rsvpdebug)
2436 printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n",
2437 so->so_type, so->so_proto->pr_protocol);
2438
2439 if (so->so_type != SOCK_RAW ||
2440 so->so_proto->pr_protocol != IPPROTO_RSVP)
2441 return (EOPNOTSUPP);
2442
2443 /* Check mbuf. */
2444 if (m == NULL || m->m_len != sizeof(int)) {
2445 return (EINVAL);
2446 }
2447 vifi = *(mtod(m, int *));
2448
2449 s = splsoftnet();
2450
2451 /* Check vif. */
2452 if (!legal_vif_num(vifi)) {
2453 splx(s);
2454 return (EADDRNOTAVAIL);
2455 }
2456
2457 if (rsvpdebug)
2458 printf("ip_rsvp_vif_done: v_rsvpd = %x so = %x\n",
2459 viftable[vifi].v_rsvpd, so);
2460
2461 viftable[vifi].v_rsvpd = NULL;
2462 /*
2463 * This may seem silly, but we need to be sure we don't over-decrement
2464 * the RSVP counter, in case something slips up.
2465 */
2466 if (viftable[vifi].v_rsvp_on) {
2467 viftable[vifi].v_rsvp_on = 0;
2468 rsvp_on--;
2469 }
2470
2471 splx(s);
2472 return (0);
2473 }
2474
2475 void
2476 ip_rsvp_force_done(so)
2477 struct socket *so;
2478 {
2479 int vifi, s;
2480
2481 /* Don't bother if it is not the right type of socket. */
2482 if (so->so_type != SOCK_RAW ||
2483 so->so_proto->pr_protocol != IPPROTO_RSVP)
2484 return;
2485
2486 s = splsoftnet();
2487
2488 /*
2489 * The socket may be attached to more than one vif...this
2490 * is perfectly legal.
2491 */
2492 for (vifi = 0; vifi < numvifs; vifi++) {
2493 if (viftable[vifi].v_rsvpd == so) {
2494 viftable[vifi].v_rsvpd = NULL;
2495 /*
2496 * This may seem silly, but we need to be sure we don't
2497 * over-decrement the RSVP counter, in case something
2498 * slips up.
2499 */
2500 if (viftable[vifi].v_rsvp_on) {
2501 viftable[vifi].v_rsvp_on = 0;
2502 rsvp_on--;
2503 }
2504 }
2505 }
2506
2507 splx(s);
2508 return;
2509 }
2510
2511 void
2512 rsvp_input(m, ifp)
2513 struct mbuf *m;
2514 struct ifnet *ifp;
2515 {
2516 int vifi, s;
2517 struct ip *ip = mtod(m, struct ip *);
2518 static struct sockaddr_in rsvp_src = { sizeof(sin), AF_INET };
2519
2520 if (rsvpdebug)
2521 printf("rsvp_input: rsvp_on %d\n", rsvp_on);
2522
2523 /*
2524 * Can still get packets with rsvp_on = 0 if there is a local member
2525 * of the group to which the RSVP packet is addressed. But in this
2526 * case we want to throw the packet away.
2527 */
2528 if (!rsvp_on) {
2529 m_freem(m);
2530 return;
2531 }
2532
2533 /*
2534 * If the old-style non-vif-associated socket is set, then use
2535 * it and ignore the new ones.
2536 */
2537 if (ip_rsvpd != NULL) {
2538 if (rsvpdebug)
2539 printf("rsvp_input: "
2540 "Sending packet up old-style socket\n");
2541 rip_input(m); /*XXX*/
2542 return;
2543 }
2544
2545 s = splsoftnet();
2546
2547 if (rsvpdebug)
2548 printf("rsvp_input: check vifs\n");
2549
2550 /* Find which vif the packet arrived on. */
2551 for (vifi = 0; vifi < numvifs; vifi++) {
2552 if (viftable[vifi].v_ifp == ifp)
2553 break;
2554 }
2555
2556 if (vifi == numvifs) {
2557 /* Can't find vif packet arrived on. Drop packet. */
2558 if (rsvpdebug)
2559 printf("rsvp_input: "
2560 "Can't find vif for packet...dropping it.\n");
2561 m_freem(m);
2562 splx(s);
2563 return;
2564 }
2565
2566 if (rsvpdebug)
2567 printf("rsvp_input: check socket\n");
2568
2569 if (viftable[vifi].v_rsvpd == NULL) {
2570 /*
2571 * drop packet, since there is no specific socket for this
2572 * interface
2573 */
2574 if (rsvpdebug)
2575 printf("rsvp_input: No socket defined for vif %d\n",
2576 vifi);
2577 m_freem(m);
2578 splx(s);
2579 return;
2580 }
2581
2582 rsvp_src.sin_addr = ip->ip_src;
2583
2584 if (rsvpdebug && m)
2585 printf("rsvp_input: m->m_len = %d, sbspace() = %d\n",
2586 m->m_len, sbspace(&viftable[vifi].v_rsvpd->so_rcv));
2587
2588 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0)
2589 if (rsvpdebug)
2590 printf("rsvp_input: Failed to append to socket\n");
2591 else
2592 if (rsvpdebug)
2593 printf("rsvp_input: send packet up\n");
2594
2595 splx(s);
2596 }
2597 #endif /* RSVP_ISI */
2598
2599 /*
2600 * Code for bandwidth monitors
2601 */
2602
2603 /*
2604 * Define common interface for timeval-related methods
2605 */
2606 #define BW_TIMEVALCMP(tvp, uvp, cmp) timercmp((tvp), (uvp), cmp)
2607 #define BW_TIMEVALDECR(vvp, uvp) timersub((vvp), (uvp), (vvp))
2608 #define BW_TIMEVALADD(vvp, uvp) timeradd((vvp), (uvp), (vvp))
2609
2610 static uint32_t
2611 compute_bw_meter_flags(struct bw_upcall *req)
2612 {
2613 uint32_t flags = 0;
2614
2615 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
2616 flags |= BW_METER_UNIT_PACKETS;
2617 if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
2618 flags |= BW_METER_UNIT_BYTES;
2619 if (req->bu_flags & BW_UPCALL_GEQ)
2620 flags |= BW_METER_GEQ;
2621 if (req->bu_flags & BW_UPCALL_LEQ)
2622 flags |= BW_METER_LEQ;
2623
2624 return flags;
2625 }
2626
2627 /*
2628 * Add a bw_meter entry
2629 */
2630 static int
2631 add_bw_upcall(struct mbuf *m)
2632 {
2633 int s;
2634 struct mfc *mfc;
2635 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
2636 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
2637 struct timeval now;
2638 struct bw_meter *x;
2639 uint32_t flags;
2640 struct bw_upcall *req;
2641
2642 if (m == NULL || m->m_len < sizeof(struct bw_upcall))
2643 return EINVAL;
2644
2645 req = mtod(m, struct bw_upcall *);
2646
2647 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2648 return EOPNOTSUPP;
2649
2650 /* Test if the flags are valid */
2651 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
2652 return EINVAL;
2653 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
2654 return EINVAL;
2655 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2656 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2657 return EINVAL;
2658
2659 /* Test if the threshold time interval is valid */
2660 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
2661 return EINVAL;
2662
2663 flags = compute_bw_meter_flags(req);
2664
2665 /*
2666 * Find if we have already same bw_meter entry
2667 */
2668 s = splsoftnet();
2669 mfc = mfc_find(&req->bu_src, &req->bu_dst);
2670 if (mfc == NULL) {
2671 splx(s);
2672 return EADDRNOTAVAIL;
2673 }
2674 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) {
2675 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2676 &req->bu_threshold.b_time, ==)) &&
2677 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2678 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2679 (x->bm_flags & BW_METER_USER_FLAGS) == flags) {
2680 splx(s);
2681 return 0; /* XXX Already installed */
2682 }
2683 }
2684
2685 /* Allocate the new bw_meter entry */
2686 x = (struct bw_meter *)malloc(sizeof(*x), M_BWMETER, M_NOWAIT);
2687 if (x == NULL) {
2688 splx(s);
2689 return ENOBUFS;
2690 }
2691
2692 /* Set the new bw_meter entry */
2693 x->bm_threshold.b_time = req->bu_threshold.b_time;
2694 microtime(&now);
2695 x->bm_start_time = now;
2696 x->bm_threshold.b_packets = req->bu_threshold.b_packets;
2697 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
2698 x->bm_measured.b_packets = 0;
2699 x->bm_measured.b_bytes = 0;
2700 x->bm_flags = flags;
2701 x->bm_time_next = NULL;
2702 x->bm_time_hash = BW_METER_BUCKETS;
2703
2704 /* Add the new bw_meter entry to the front of entries for this MFC */
2705 x->bm_mfc = mfc;
2706 x->bm_mfc_next = mfc->mfc_bw_meter;
2707 mfc->mfc_bw_meter = x;
2708 schedule_bw_meter(x, &now);
2709 splx(s);
2710
2711 return 0;
2712 }
2713
2714 static void
2715 free_bw_list(struct bw_meter *list)
2716 {
2717 while (list != NULL) {
2718 struct bw_meter *x = list;
2719
2720 list = list->bm_mfc_next;
2721 unschedule_bw_meter(x);
2722 free(x, M_BWMETER);
2723 }
2724 }
2725
2726 /*
2727 * Delete one or multiple bw_meter entries
2728 */
2729 static int
2730 del_bw_upcall(struct mbuf *m)
2731 {
2732 int s;
2733 struct mfc *mfc;
2734 struct bw_meter *x;
2735 struct bw_upcall *req;
2736
2737 if (m == NULL || m->m_len < sizeof(struct bw_upcall))
2738 return EINVAL;
2739
2740 req = mtod(m, struct bw_upcall *);
2741
2742 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2743 return EOPNOTSUPP;
2744
2745 s = splsoftnet();
2746 /* Find the corresponding MFC entry */
2747 mfc = mfc_find(&req->bu_src, &req->bu_dst);
2748 if (mfc == NULL) {
2749 splx(s);
2750 return EADDRNOTAVAIL;
2751 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2752 /*
2753 * Delete all bw_meter entries for this mfc
2754 */
2755 struct bw_meter *list;
2756
2757 list = mfc->mfc_bw_meter;
2758 mfc->mfc_bw_meter = NULL;
2759 free_bw_list(list);
2760 splx(s);
2761 return 0;
2762 } else { /* Delete a single bw_meter entry */
2763 struct bw_meter *prev;
2764 uint32_t flags = 0;
2765
2766 flags = compute_bw_meter_flags(req);
2767
2768 /* Find the bw_meter entry to delete */
2769 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
2770 prev = x, x = x->bm_mfc_next) {
2771 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2772 &req->bu_threshold.b_time, ==)) &&
2773 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2774 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2775 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2776 break;
2777 }
2778 if (x != NULL) { /* Delete entry from the list for this MFC */
2779 if (prev != NULL)
2780 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
2781 else
2782 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
2783
2784 unschedule_bw_meter(x);
2785 splx(s);
2786 /* Free the bw_meter entry */
2787 free(x, M_BWMETER);
2788 return 0;
2789 } else {
2790 splx(s);
2791 return EINVAL;
2792 }
2793 }
2794 /* NOTREACHED */
2795 }
2796
2797 /*
2798 * Perform bandwidth measurement processing that may result in an upcall
2799 */
2800 static void
2801 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2802 {
2803 struct timeval delta;
2804
2805 delta = *nowp;
2806 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2807
2808 if (x->bm_flags & BW_METER_GEQ) {
2809 /*
2810 * Processing for ">=" type of bw_meter entry
2811 */
2812 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2813 /* Reset the bw_meter entry */
2814 x->bm_start_time = *nowp;
2815 x->bm_measured.b_packets = 0;
2816 x->bm_measured.b_bytes = 0;
2817 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2818 }
2819
2820 /* Record that a packet is received */
2821 x->bm_measured.b_packets++;
2822 x->bm_measured.b_bytes += plen;
2823
2824 /*
2825 * Test if we should deliver an upcall
2826 */
2827 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2828 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2829 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2830 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2831 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2832 /* Prepare an upcall for delivery */
2833 bw_meter_prepare_upcall(x, nowp);
2834 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2835 }
2836 }
2837 } else if (x->bm_flags & BW_METER_LEQ) {
2838 /*
2839 * Processing for "<=" type of bw_meter entry
2840 */
2841 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2842 /*
2843 * We are behind time with the multicast forwarding table
2844 * scanning for "<=" type of bw_meter entries, so test now
2845 * if we should deliver an upcall.
2846 */
2847 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2848 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2849 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2850 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2851 /* Prepare an upcall for delivery */
2852 bw_meter_prepare_upcall(x, nowp);
2853 }
2854 /* Reschedule the bw_meter entry */
2855 unschedule_bw_meter(x);
2856 schedule_bw_meter(x, nowp);
2857 }
2858
2859 /* Record that a packet is received */
2860 x->bm_measured.b_packets++;
2861 x->bm_measured.b_bytes += plen;
2862
2863 /*
2864 * Test if we should restart the measuring interval
2865 */
2866 if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
2867 x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
2868 (x->bm_flags & BW_METER_UNIT_BYTES &&
2869 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
2870 /* Don't restart the measuring interval */
2871 } else {
2872 /* Do restart the measuring interval */
2873 /*
2874 * XXX: note that we don't unschedule and schedule, because this
2875 * might be too much overhead per packet. Instead, when we process
2876 * all entries for a given timer hash bin, we check whether it is
2877 * really a timeout. If not, we reschedule at that time.
2878 */
2879 x->bm_start_time = *nowp;
2880 x->bm_measured.b_packets = 0;
2881 x->bm_measured.b_bytes = 0;
2882 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2883 }
2884 }
2885 }
2886
2887 /*
2888 * Prepare a bandwidth-related upcall
2889 */
2890 static void
2891 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2892 {
2893 struct timeval delta;
2894 struct bw_upcall *u;
2895
2896 /*
2897 * Compute the measured time interval
2898 */
2899 delta = *nowp;
2900 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2901
2902 /*
2903 * If there are too many pending upcalls, deliver them now
2904 */
2905 if (bw_upcalls_n >= BW_UPCALLS_MAX)
2906 bw_upcalls_send();
2907
2908 /*
2909 * Set the bw_upcall entry
2910 */
2911 u = &bw_upcalls[bw_upcalls_n++];
2912 u->bu_src = x->bm_mfc->mfc_origin;
2913 u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2914 u->bu_threshold.b_time = x->bm_threshold.b_time;
2915 u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2916 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2917 u->bu_measured.b_time = delta;
2918 u->bu_measured.b_packets = x->bm_measured.b_packets;
2919 u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2920 u->bu_flags = 0;
2921 if (x->bm_flags & BW_METER_UNIT_PACKETS)
2922 u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2923 if (x->bm_flags & BW_METER_UNIT_BYTES)
2924 u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2925 if (x->bm_flags & BW_METER_GEQ)
2926 u->bu_flags |= BW_UPCALL_GEQ;
2927 if (x->bm_flags & BW_METER_LEQ)
2928 u->bu_flags |= BW_UPCALL_LEQ;
2929 }
2930
2931 /*
2932 * Send the pending bandwidth-related upcalls
2933 */
2934 static void
2935 bw_upcalls_send(void)
2936 {
2937 struct mbuf *m;
2938 int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
2939 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2940 static struct igmpmsg igmpmsg = { 0, /* unused1 */
2941 0, /* unused2 */
2942 IGMPMSG_BW_UPCALL,/* im_msgtype */
2943 0, /* im_mbz */
2944 0, /* im_vif */
2945 0, /* unused3 */
2946 { 0 }, /* im_src */
2947 { 0 } }; /* im_dst */
2948
2949 if (bw_upcalls_n == 0)
2950 return; /* No pending upcalls */
2951
2952 bw_upcalls_n = 0;
2953
2954 /*
2955 * Allocate a new mbuf, initialize it with the header and
2956 * the payload for the pending calls.
2957 */
2958 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2959 if (m == NULL) {
2960 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2961 return;
2962 }
2963
2964 m->m_len = m->m_pkthdr.len = 0;
2965 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2966 m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
2967
2968 /*
2969 * Send the upcalls
2970 * XXX do we need to set the address in k_igmpsrc ?
2971 */
2972 mrtstat.mrts_upcalls++;
2973 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
2974 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2975 ++mrtstat.mrts_upq_sockfull;
2976 }
2977 }
2978
2979 /*
2980 * Compute the timeout hash value for the bw_meter entries
2981 */
2982 #define BW_METER_TIMEHASH(bw_meter, hash) \
2983 do { \
2984 struct timeval next_timeval = (bw_meter)->bm_start_time; \
2985 \
2986 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2987 (hash) = next_timeval.tv_sec; \
2988 if (next_timeval.tv_usec) \
2989 (hash)++; /* XXX: make sure we don't timeout early */ \
2990 (hash) %= BW_METER_BUCKETS; \
2991 } while (/*CONSTCOND*/ 0)
2992
2993 /*
2994 * Schedule a timer to process periodically bw_meter entry of type "<="
2995 * by linking the entry in the proper hash bucket.
2996 */
2997 static void
2998 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
2999 {
3000 int time_hash;
3001
3002 if (!(x->bm_flags & BW_METER_LEQ))
3003 return; /* XXX: we schedule timers only for "<=" entries */
3004
3005 /*
3006 * Reset the bw_meter entry
3007 */
3008 x->bm_start_time = *nowp;
3009 x->bm_measured.b_packets = 0;
3010 x->bm_measured.b_bytes = 0;
3011 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
3012
3013 /*
3014 * Compute the timeout hash value and insert the entry
3015 */
3016 BW_METER_TIMEHASH(x, time_hash);
3017 x->bm_time_next = bw_meter_timers[time_hash];
3018 bw_meter_timers[time_hash] = x;
3019 x->bm_time_hash = time_hash;
3020 }
3021
3022 /*
3023 * Unschedule the periodic timer that processes bw_meter entry of type "<="
3024 * by removing the entry from the proper hash bucket.
3025 */
3026 static void
3027 unschedule_bw_meter(struct bw_meter *x)
3028 {
3029 int time_hash;
3030 struct bw_meter *prev, *tmp;
3031
3032 if (!(x->bm_flags & BW_METER_LEQ))
3033 return; /* XXX: we schedule timers only for "<=" entries */
3034
3035 /*
3036 * Compute the timeout hash value and delete the entry
3037 */
3038 time_hash = x->bm_time_hash;
3039 if (time_hash >= BW_METER_BUCKETS)
3040 return; /* Entry was not scheduled */
3041
3042 for (prev = NULL, tmp = bw_meter_timers[time_hash];
3043 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
3044 if (tmp == x)
3045 break;
3046
3047 if (tmp == NULL)
3048 panic("unschedule_bw_meter: bw_meter entry not found");
3049
3050 if (prev != NULL)
3051 prev->bm_time_next = x->bm_time_next;
3052 else
3053 bw_meter_timers[time_hash] = x->bm_time_next;
3054
3055 x->bm_time_next = NULL;
3056 x->bm_time_hash = BW_METER_BUCKETS;
3057 }
3058
3059 /*
3060 * Process all "<=" type of bw_meter that should be processed now,
3061 * and for each entry prepare an upcall if necessary. Each processed
3062 * entry is rescheduled again for the (periodic) processing.
3063 *
3064 * This is run periodically (once per second normally). On each round,
3065 * all the potentially matching entries are in the hash slot that we are
3066 * looking at.
3067 */
3068 static void
3069 bw_meter_process()
3070 {
3071 int s;
3072 static uint32_t last_tv_sec; /* last time we processed this */
3073
3074 uint32_t loops;
3075 int i;
3076 struct timeval now, process_endtime;
3077
3078 microtime(&now);
3079 if (last_tv_sec == now.tv_sec)
3080 return; /* nothing to do */
3081
3082 loops = now.tv_sec - last_tv_sec;
3083 last_tv_sec = now.tv_sec;
3084 if (loops > BW_METER_BUCKETS)
3085 loops = BW_METER_BUCKETS;
3086
3087 s = splsoftnet();
3088 /*
3089 * Process all bins of bw_meter entries from the one after the last
3090 * processed to the current one. On entry, i points to the last bucket
3091 * visited, so we need to increment i at the beginning of the loop.
3092 */
3093 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
3094 struct bw_meter *x, *tmp_list;
3095
3096 if (++i >= BW_METER_BUCKETS)
3097 i = 0;
3098
3099 /* Disconnect the list of bw_meter entries from the bin */
3100 tmp_list = bw_meter_timers[i];
3101 bw_meter_timers[i] = NULL;
3102
3103 /* Process the list of bw_meter entries */
3104 while (tmp_list != NULL) {
3105 x = tmp_list;
3106 tmp_list = tmp_list->bm_time_next;
3107
3108 /* Test if the time interval is over */
3109 process_endtime = x->bm_start_time;
3110 BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
3111 if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
3112 /* Not yet: reschedule, but don't reset */
3113 int time_hash;
3114
3115 BW_METER_TIMEHASH(x, time_hash);
3116 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
3117 /*
3118 * XXX: somehow the bin processing is a bit ahead of time.
3119 * Put the entry in the next bin.
3120 */
3121 if (++time_hash >= BW_METER_BUCKETS)
3122 time_hash = 0;
3123 }
3124 x->bm_time_next = bw_meter_timers[time_hash];
3125 bw_meter_timers[time_hash] = x;
3126 x->bm_time_hash = time_hash;
3127
3128 continue;
3129 }
3130
3131 /*
3132 * Test if we should deliver an upcall
3133 */
3134 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
3135 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
3136 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
3137 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
3138 /* Prepare an upcall for delivery */
3139 bw_meter_prepare_upcall(x, &now);
3140 }
3141
3142 /*
3143 * Reschedule for next processing
3144 */
3145 schedule_bw_meter(x, &now);
3146 }
3147 }
3148
3149 /* Send all upcalls that are pending delivery */
3150 bw_upcalls_send();
3151
3152 splx(s);
3153 }
3154
3155 /*
3156 * A periodic function for sending all upcalls that are pending delivery
3157 */
3158 static void
3159 expire_bw_upcalls_send(void *unused)
3160 {
3161 int s;
3162
3163 s = splsoftnet();
3164 bw_upcalls_send();
3165 splx(s);
3166
3167 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
3168 expire_bw_upcalls_send, NULL);
3169 }
3170
3171 /*
3172 * A periodic function for periodic scanning of the multicast forwarding
3173 * table for processing all "<=" bw_meter entries.
3174 */
3175 static void
3176 expire_bw_meter_process(void *unused)
3177 {
3178 if (mrt_api_config & MRT_MFC_BW_UPCALL)
3179 bw_meter_process();
3180
3181 callout_reset(&bw_meter_ch, BW_METER_PERIOD,
3182 expire_bw_meter_process, NULL);
3183 }
3184
3185 /*
3186 * End of bandwidth monitoring code
3187 */
3188
3189 #ifdef PIM
3190 /*
3191 * Send the packet up to the user daemon, or eventually do kernel encapsulation
3192 */
3193 static int
3194 pim_register_send(struct ip *ip, struct vif *vifp,
3195 struct mbuf *m, struct mfc *rt)
3196 {
3197 struct mbuf *mb_copy, *mm;
3198
3199 if (mrtdebug & DEBUG_PIM)
3200 log(LOG_DEBUG, "pim_register_send: ");
3201
3202 mb_copy = pim_register_prepare(ip, m);
3203 if (mb_copy == NULL)
3204 return ENOBUFS;
3205
3206 /*
3207 * Send all the fragments. Note that the mbuf for each fragment
3208 * is freed by the sending machinery.
3209 */
3210 for (mm = mb_copy; mm; mm = mb_copy) {
3211 mb_copy = mm->m_nextpkt;
3212 mm->m_nextpkt = NULL;
3213 mm = m_pullup(mm, sizeof(struct ip));
3214 if (mm != NULL) {
3215 ip = mtod(mm, struct ip *);
3216 if ((mrt_api_config & MRT_MFC_RP) &&
3217 !in_nullhost(rt->mfc_rp)) {
3218 pim_register_send_rp(ip, vifp, mm, rt);
3219 } else {
3220 pim_register_send_upcall(ip, vifp, mm, rt);
3221 }
3222 }
3223 }
3224
3225 return 0;
3226 }
3227
3228 /*
3229 * Return a copy of the data packet that is ready for PIM Register
3230 * encapsulation.
3231 * XXX: Note that in the returned copy the IP header is a valid one.
3232 */
3233 static struct mbuf *
3234 pim_register_prepare(struct ip *ip, struct mbuf *m)
3235 {
3236 struct mbuf *mb_copy = NULL;
3237 int mtu;
3238
3239 /* Take care of delayed checksums */
3240 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
3241 in_delayed_cksum(m);
3242 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
3243 }
3244
3245 /*
3246 * Copy the old packet & pullup its IP header into the
3247 * new mbuf so we can modify it.
3248 */
3249 mb_copy = m_copy(m, 0, M_COPYALL);
3250 if (mb_copy == NULL)
3251 return NULL;
3252 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
3253 if (mb_copy == NULL)
3254 return NULL;
3255
3256 /* take care of the TTL */
3257 ip = mtod(mb_copy, struct ip *);
3258 --ip->ip_ttl;
3259
3260 /* Compute the MTU after the PIM Register encapsulation */
3261 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
3262
3263 if (ntohs(ip->ip_len) <= mtu) {
3264 /* Turn the IP header into a valid one */
3265 ip->ip_sum = 0;
3266 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
3267 } else {
3268 /* Fragment the packet */
3269 if (ip_fragment(mb_copy, NULL, mtu) != 0) {
3270 /* XXX: mb_copy was freed by ip_fragment() */
3271 return NULL;
3272 }
3273 }
3274 return mb_copy;
3275 }
3276
3277 /*
3278 * Send an upcall with the data packet to the user-level process.
3279 */
3280 static int
3281 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
3282 struct mbuf *mb_copy, struct mfc *rt)
3283 {
3284 struct mbuf *mb_first;
3285 int len = ntohs(ip->ip_len);
3286 struct igmpmsg *im;
3287 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
3288
3289 /*
3290 * Add a new mbuf with an upcall header
3291 */
3292 MGETHDR(mb_first, M_DONTWAIT, MT_HEADER);
3293 if (mb_first == NULL) {
3294 m_freem(mb_copy);
3295 return ENOBUFS;
3296 }
3297 mb_first->m_data += max_linkhdr;
3298 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
3299 mb_first->m_len = sizeof(struct igmpmsg);
3300 mb_first->m_next = mb_copy;
3301
3302 /* Send message to routing daemon */
3303 im = mtod(mb_first, struct igmpmsg *);
3304 im->im_msgtype = IGMPMSG_WHOLEPKT;
3305 im->im_mbz = 0;
3306 im->im_vif = vifp - viftable;
3307 im->im_src = ip->ip_src;
3308 im->im_dst = ip->ip_dst;
3309
3310 k_igmpsrc.sin_addr = ip->ip_src;
3311
3312 mrtstat.mrts_upcalls++;
3313
3314 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
3315 if (mrtdebug & DEBUG_PIM)
3316 log(LOG_WARNING,
3317 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
3318 ++mrtstat.mrts_upq_sockfull;
3319 return ENOBUFS;
3320 }
3321
3322 /* Keep statistics */
3323 pimstat.pims_snd_registers_msgs++;
3324 pimstat.pims_snd_registers_bytes += len;
3325
3326 return 0;
3327 }
3328
3329 /*
3330 * Encapsulate the data packet in PIM Register message and send it to the RP.
3331 */
3332 static int
3333 pim_register_send_rp(struct ip *ip, struct vif *vifp,
3334 struct mbuf *mb_copy, struct mfc *rt)
3335 {
3336 struct mbuf *mb_first;
3337 struct ip *ip_outer;
3338 struct pim_encap_pimhdr *pimhdr;
3339 int len = ntohs(ip->ip_len);
3340 vifi_t vifi = rt->mfc_parent;
3341
3342 if ((vifi >= numvifs) || in_nullhost(viftable[vifi].v_lcl_addr)) {
3343 m_freem(mb_copy);
3344 return EADDRNOTAVAIL; /* The iif vif is invalid */
3345 }
3346
3347 /*
3348 * Add a new mbuf with the encapsulating header
3349 */
3350 MGETHDR(mb_first, M_DONTWAIT, MT_HEADER);
3351 if (mb_first == NULL) {
3352 m_freem(mb_copy);
3353 return ENOBUFS;
3354 }
3355 mb_first->m_data += max_linkhdr;
3356 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
3357 mb_first->m_next = mb_copy;
3358
3359 mb_first->m_pkthdr.len = len + mb_first->m_len;
3360
3361 /*
3362 * Fill in the encapsulating IP and PIM header
3363 */
3364 ip_outer = mtod(mb_first, struct ip *);
3365 *ip_outer = pim_encap_iphdr;
3366 ip_outer->ip_id = ip_newid();
3367 ip_outer->ip_len = htons(len + sizeof(pim_encap_iphdr) +
3368 sizeof(pim_encap_pimhdr));
3369 ip_outer->ip_src = viftable[vifi].v_lcl_addr;
3370 ip_outer->ip_dst = rt->mfc_rp;
3371 /*
3372 * Copy the inner header TOS to the outer header, and take care of the
3373 * IP_DF bit.
3374 */
3375 ip_outer->ip_tos = ip->ip_tos;
3376 if (ntohs(ip->ip_off) & IP_DF)
3377 ip_outer->ip_off |= IP_DF;
3378 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
3379 + sizeof(pim_encap_iphdr));
3380 *pimhdr = pim_encap_pimhdr;
3381 /* If the iif crosses a border, set the Border-bit */
3382 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
3383 pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
3384
3385 mb_first->m_data += sizeof(pim_encap_iphdr);
3386 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
3387 mb_first->m_data -= sizeof(pim_encap_iphdr);
3388
3389 if (vifp->v_rate_limit == 0)
3390 tbf_send_packet(vifp, mb_first);
3391 else
3392 tbf_control(vifp, mb_first, ip, ntohs(ip_outer->ip_len));
3393
3394 /* Keep statistics */
3395 pimstat.pims_snd_registers_msgs++;
3396 pimstat.pims_snd_registers_bytes += len;
3397
3398 return 0;
3399 }
3400
3401 /*
3402 * PIM-SMv2 and PIM-DM messages processing.
3403 * Receives and verifies the PIM control messages, and passes them
3404 * up to the listening socket, using rip_input().
3405 * The only message with special processing is the PIM_REGISTER message
3406 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
3407 * is passed to if_simloop().
3408 */
3409 void
3410 pim_input(struct mbuf *m, ...)
3411 {
3412 struct ip *ip = mtod(m, struct ip *);
3413 struct pim *pim;
3414 int minlen;
3415 int datalen;
3416 int ip_tos;
3417 int proto;
3418 int iphlen;
3419 va_list ap;
3420
3421 va_start(ap, m);
3422 iphlen = va_arg(ap, int);
3423 proto = va_arg(ap, int);
3424 va_end(ap);
3425
3426 datalen = ntohs(ip->ip_len) - iphlen;
3427
3428 /* Keep statistics */
3429 pimstat.pims_rcv_total_msgs++;
3430 pimstat.pims_rcv_total_bytes += datalen;
3431
3432 /*
3433 * Validate lengths
3434 */
3435 if (datalen < PIM_MINLEN) {
3436 pimstat.pims_rcv_tooshort++;
3437 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
3438 datalen, (u_long)ip->ip_src.s_addr);
3439 m_freem(m);
3440 return;
3441 }
3442
3443 /*
3444 * If the packet is at least as big as a REGISTER, go agead
3445 * and grab the PIM REGISTER header size, to avoid another
3446 * possible m_pullup() later.
3447 *
3448 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
3449 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
3450 */
3451 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
3452 /*
3453 * Get the IP and PIM headers in contiguous memory, and
3454 * possibly the PIM REGISTER header.
3455 */
3456 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
3457 (m = m_pullup(m, minlen)) == NULL) {
3458 log(LOG_ERR, "pim_input: m_pullup failure\n");
3459 return;
3460 }
3461 /* m_pullup() may have given us a new mbuf so reset ip. */
3462 ip = mtod(m, struct ip *);
3463 ip_tos = ip->ip_tos;
3464
3465 /* adjust mbuf to point to the PIM header */
3466 m->m_data += iphlen;
3467 m->m_len -= iphlen;
3468 pim = mtod(m, struct pim *);
3469
3470 /*
3471 * Validate checksum. If PIM REGISTER, exclude the data packet.
3472 *
3473 * XXX: some older PIMv2 implementations don't make this distinction,
3474 * so for compatibility reason perform the checksum over part of the
3475 * message, and if error, then over the whole message.
3476 */
3477 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
3478 /* do nothing, checksum okay */
3479 } else if (in_cksum(m, datalen)) {
3480 pimstat.pims_rcv_badsum++;
3481 if (mrtdebug & DEBUG_PIM)
3482 log(LOG_DEBUG, "pim_input: invalid checksum");
3483 m_freem(m);
3484 return;
3485 }
3486
3487 /* PIM version check */
3488 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
3489 pimstat.pims_rcv_badversion++;
3490 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
3491 PIM_VT_V(pim->pim_vt), PIM_VERSION);
3492 m_freem(m);
3493 return;
3494 }
3495
3496 /* restore mbuf back to the outer IP */
3497 m->m_data -= iphlen;
3498 m->m_len += iphlen;
3499
3500 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
3501 /*
3502 * Since this is a REGISTER, we'll make a copy of the register
3503 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
3504 * routing daemon.
3505 */
3506 int s;
3507 struct sockaddr_in dst = { sizeof(dst), AF_INET };
3508 struct mbuf *mcp;
3509 struct ip *encap_ip;
3510 u_int32_t *reghdr;
3511 struct ifnet *vifp;
3512
3513 s = splsoftnet();
3514 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
3515 splx(s);
3516 if (mrtdebug & DEBUG_PIM)
3517 log(LOG_DEBUG,
3518 "pim_input: register vif not set: %d\n", reg_vif_num);
3519 m_freem(m);
3520 return;
3521 }
3522 /* XXX need refcnt? */
3523 vifp = viftable[reg_vif_num].v_ifp;
3524 splx(s);
3525
3526 /*
3527 * Validate length
3528 */
3529 if (datalen < PIM_REG_MINLEN) {
3530 pimstat.pims_rcv_tooshort++;
3531 pimstat.pims_rcv_badregisters++;
3532 log(LOG_ERR,
3533 "pim_input: register packet size too small %d from %lx\n",
3534 datalen, (u_long)ip->ip_src.s_addr);
3535 m_freem(m);
3536 return;
3537 }
3538
3539 reghdr = (u_int32_t *)(pim + 1);
3540 encap_ip = (struct ip *)(reghdr + 1);
3541
3542 if (mrtdebug & DEBUG_PIM) {
3543 log(LOG_DEBUG,
3544 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
3545 (u_long)ntohl(encap_ip->ip_src.s_addr),
3546 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3547 ntohs(encap_ip->ip_len));
3548 }
3549
3550 /* verify the version number of the inner packet */
3551 if (encap_ip->ip_v != IPVERSION) {
3552 pimstat.pims_rcv_badregisters++;
3553 if (mrtdebug & DEBUG_PIM) {
3554 log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
3555 "of the inner packet\n", encap_ip->ip_v);
3556 }
3557 m_freem(m);
3558 return;
3559 }
3560
3561 /* verify the inner packet is destined to a mcast group */
3562 if (!IN_MULTICAST(encap_ip->ip_dst.s_addr)) {
3563 pimstat.pims_rcv_badregisters++;
3564 if (mrtdebug & DEBUG_PIM)
3565 log(LOG_DEBUG,
3566 "pim_input: inner packet of register is not "
3567 "multicast %lx\n",
3568 (u_long)ntohl(encap_ip->ip_dst.s_addr));
3569 m_freem(m);
3570 return;
3571 }
3572
3573 /* If a NULL_REGISTER, pass it to the daemon */
3574 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
3575 goto pim_input_to_daemon;
3576
3577 /*
3578 * Copy the TOS from the outer IP header to the inner IP header.
3579 */
3580 if (encap_ip->ip_tos != ip_tos) {
3581 /* Outer TOS -> inner TOS */
3582 encap_ip->ip_tos = ip_tos;
3583 /* Recompute the inner header checksum. Sigh... */
3584
3585 /* adjust mbuf to point to the inner IP header */
3586 m->m_data += (iphlen + PIM_MINLEN);
3587 m->m_len -= (iphlen + PIM_MINLEN);
3588
3589 encap_ip->ip_sum = 0;
3590 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
3591
3592 /* restore mbuf to point back to the outer IP header */
3593 m->m_data -= (iphlen + PIM_MINLEN);
3594 m->m_len += (iphlen + PIM_MINLEN);
3595 }
3596
3597 /*
3598 * Decapsulate the inner IP packet and loopback to forward it
3599 * as a normal multicast packet. Also, make a copy of the
3600 * outer_iphdr + pimhdr + reghdr + encap_iphdr
3601 * to pass to the daemon later, so it can take the appropriate
3602 * actions (e.g., send back PIM_REGISTER_STOP).
3603 * XXX: here m->m_data points to the outer IP header.
3604 */
3605 mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
3606 if (mcp == NULL) {
3607 log(LOG_ERR,
3608 "pim_input: pim register: could not copy register head\n");
3609 m_freem(m);
3610 return;
3611 }
3612
3613 /* Keep statistics */
3614 /* XXX: registers_bytes include only the encap. mcast pkt */
3615 pimstat.pims_rcv_registers_msgs++;
3616 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
3617
3618 /*
3619 * forward the inner ip packet; point m_data at the inner ip.
3620 */
3621 m_adj(m, iphlen + PIM_MINLEN);
3622
3623 if (mrtdebug & DEBUG_PIM) {
3624 log(LOG_DEBUG,
3625 "pim_input: forwarding decapsulated register: "
3626 "src %lx, dst %lx, vif %d\n",
3627 (u_long)ntohl(encap_ip->ip_src.s_addr),
3628 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3629 reg_vif_num);
3630 }
3631 /* NB: vifp was collected above; can it change on us? */
3632 looutput(vifp, m, (struct sockaddr *)&dst, (struct rtentry *)NULL);
3633
3634 /* prepare the register head to send to the mrouting daemon */
3635 m = mcp;
3636 }
3637
3638 pim_input_to_daemon:
3639 /*
3640 * Pass the PIM message up to the daemon; if it is a Register message,
3641 * pass the 'head' only up to the daemon. This includes the
3642 * outer IP header, PIM header, PIM-Register header and the
3643 * inner IP header.
3644 * XXX: the outer IP header pkt size of a Register is not adjust to
3645 * reflect the fact that the inner multicast data is truncated.
3646 */
3647 rip_input(m, iphlen, proto);
3648
3649 return;
3650 }
3651 #endif /* PIM */
3652