ip_mroute.c revision 1.86 1 /* $NetBSD: ip_mroute.c,v 1.86 2004/09/04 23:30:07 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.86 2004/09/04 23:30:07 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 if (vifp->v_flags & VIFF_REGISTER)
1070 reg_vif_num = VIFI_INVALID;
1071 #endif
1072 } else {
1073 satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
1074 satosin(&ifr.ifr_addr)->sin_family = AF_INET;
1075 satosin(&ifr.ifr_addr)->sin_addr = zeroin_addr;
1076 ifp = vifp->v_ifp;
1077 (*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)&ifr);
1078 }
1079 bzero((caddr_t)vifp, sizeof(*vifp));
1080 }
1081
1082 /*
1083 * Delete a vif from the vif table
1084 */
1085 static int
1086 del_vif(m)
1087 struct mbuf *m;
1088 {
1089 vifi_t *vifip;
1090 struct vif *vifp;
1091 vifi_t vifi;
1092 int s;
1093
1094 if (m == NULL || m->m_len < sizeof(vifi_t))
1095 return (EINVAL);
1096
1097 vifip = mtod(m, vifi_t *);
1098 if (*vifip >= numvifs)
1099 return (EINVAL);
1100
1101 vifp = &viftable[*vifip];
1102 if (in_nullhost(vifp->v_lcl_addr))
1103 return (EADDRNOTAVAIL);
1104
1105 s = splsoftnet();
1106
1107 reset_vif(vifp);
1108
1109 /* Adjust numvifs down */
1110 for (vifi = numvifs; vifi > 0; vifi--)
1111 if (!in_nullhost(viftable[vifi - 1].v_lcl_addr))
1112 break;
1113 numvifs = vifi;
1114
1115 splx(s);
1116
1117 if (mrtdebug)
1118 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", *vifip, numvifs);
1119
1120 return (0);
1121 }
1122
1123 /*
1124 * update an mfc entry without resetting counters and S,G addresses.
1125 */
1126 static void
1127 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1128 {
1129 int i;
1130
1131 rt->mfc_parent = mfccp->mfcc_parent;
1132 for (i = 0; i < numvifs; i++) {
1133 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1134 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
1135 MRT_MFC_FLAGS_ALL;
1136 }
1137 /* set the RP address */
1138 if (mrt_api_config & MRT_MFC_RP)
1139 rt->mfc_rp = mfccp->mfcc_rp;
1140 else
1141 rt->mfc_rp = zeroin_addr;
1142 }
1143
1144 /*
1145 * fully initialize an mfc entry from the parameter.
1146 */
1147 static void
1148 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1149 {
1150 rt->mfc_origin = mfccp->mfcc_origin;
1151 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
1152
1153 update_mfc_params(rt, mfccp);
1154
1155 /* initialize pkt counters per src-grp */
1156 rt->mfc_pkt_cnt = 0;
1157 rt->mfc_byte_cnt = 0;
1158 rt->mfc_wrong_if = 0;
1159 timerclear(&rt->mfc_last_assert);
1160 }
1161
1162 static void
1163 expire_mfc(rt)
1164 struct mfc *rt;
1165 {
1166 struct rtdetq *rte, *nrte;
1167
1168 free_bw_list(rt->mfc_bw_meter);
1169
1170 for (rte = rt->mfc_stall; rte != NULL; rte = nrte) {
1171 nrte = rte->next;
1172 m_freem(rte->m);
1173 free(rte, M_MRTABLE);
1174 }
1175
1176 LIST_REMOVE(rt, mfc_hash);
1177 free(rt, M_MRTABLE);
1178 }
1179
1180 /*
1181 * Add an mfc entry
1182 */
1183 static int
1184 add_mfc(m)
1185 struct mbuf *m;
1186 {
1187 struct mfcctl2 mfcctl2;
1188 struct mfcctl2 *mfccp;
1189 struct mfc *rt;
1190 u_int32_t hash = 0;
1191 struct rtdetq *rte, *nrte;
1192 u_short nstl;
1193 int s;
1194 int mfcctl_size = sizeof(struct mfcctl);
1195
1196 if (mrt_api_config & MRT_API_FLAGS_ALL)
1197 mfcctl_size = sizeof(struct mfcctl2);
1198
1199 if (m == NULL || m->m_len < mfcctl_size)
1200 return (EINVAL);
1201
1202 /*
1203 * select data size depending on API version.
1204 */
1205 if (mrt_api_config & MRT_API_FLAGS_ALL) {
1206 struct mfcctl2 *mp2 = mtod(m, struct mfcctl2 *);
1207 bcopy(mp2, (caddr_t)&mfcctl2, sizeof(*mp2));
1208 } else {
1209 struct mfcctl *mp = mtod(m, struct mfcctl *);
1210 bcopy(mp, (caddr_t)&mfcctl2, sizeof(*mp));
1211 bzero((caddr_t)&mfcctl2 + sizeof(struct mfcctl),
1212 sizeof(mfcctl2) - sizeof(struct mfcctl));
1213 }
1214 mfccp = &mfcctl2;
1215
1216 s = splsoftnet();
1217 rt = mfc_find(&mfccp->mfcc_origin, &mfccp->mfcc_mcastgrp);
1218
1219 /* If an entry already exists, just update the fields */
1220 if (rt) {
1221 if (mrtdebug & DEBUG_MFC)
1222 log(LOG_DEBUG, "add_mfc update o %x g %x p %x\n",
1223 ntohl(mfccp->mfcc_origin.s_addr),
1224 ntohl(mfccp->mfcc_mcastgrp.s_addr),
1225 mfccp->mfcc_parent);
1226
1227 update_mfc_params(rt, mfccp);
1228
1229 splx(s);
1230 return (0);
1231 }
1232
1233 /*
1234 * Find the entry for which the upcall was made and update
1235 */
1236 nstl = 0;
1237 hash = MFCHASH(mfccp->mfcc_origin, mfccp->mfcc_mcastgrp);
1238 LIST_FOREACH(rt, &mfchashtbl[hash], mfc_hash) {
1239 if (in_hosteq(rt->mfc_origin, mfccp->mfcc_origin) &&
1240 in_hosteq(rt->mfc_mcastgrp, mfccp->mfcc_mcastgrp) &&
1241 rt->mfc_stall != NULL) {
1242 if (nstl++)
1243 log(LOG_ERR, "add_mfc %s o %x g %x p %x dbx %p\n",
1244 "multiple kernel entries",
1245 ntohl(mfccp->mfcc_origin.s_addr),
1246 ntohl(mfccp->mfcc_mcastgrp.s_addr),
1247 mfccp->mfcc_parent, rt->mfc_stall);
1248
1249 if (mrtdebug & DEBUG_MFC)
1250 log(LOG_DEBUG, "add_mfc o %x g %x p %x dbg %p\n",
1251 ntohl(mfccp->mfcc_origin.s_addr),
1252 ntohl(mfccp->mfcc_mcastgrp.s_addr),
1253 mfccp->mfcc_parent, rt->mfc_stall);
1254
1255 rte = rt->mfc_stall;
1256 init_mfc_params(rt, mfccp);
1257 rt->mfc_stall = NULL;
1258
1259 rt->mfc_expire = 0; /* Don't clean this guy up */
1260 nexpire[hash]--;
1261
1262 /* free packets Qed at the end of this entry */
1263 for (; rte != NULL; rte = nrte) {
1264 nrte = rte->next;
1265 if (rte->ifp) {
1266 #ifdef RSVP_ISI
1267 ip_mdq(rte->m, rte->ifp, rt, -1);
1268 #else
1269 ip_mdq(rte->m, rte->ifp, rt);
1270 #endif /* RSVP_ISI */
1271 }
1272 m_freem(rte->m);
1273 #ifdef UPCALL_TIMING
1274 collate(&rte->t);
1275 #endif /* UPCALL_TIMING */
1276 free(rte, M_MRTABLE);
1277 }
1278 }
1279 }
1280
1281 /*
1282 * It is possible that an entry is being inserted without an upcall
1283 */
1284 if (nstl == 0) {
1285 /*
1286 * No mfc; make a new one
1287 */
1288 if (mrtdebug & DEBUG_MFC)
1289 log(LOG_DEBUG, "add_mfc no upcall o %x g %x p %x\n",
1290 ntohl(mfccp->mfcc_origin.s_addr),
1291 ntohl(mfccp->mfcc_mcastgrp.s_addr),
1292 mfccp->mfcc_parent);
1293
1294 LIST_FOREACH(rt, &mfchashtbl[hash], mfc_hash) {
1295 if (in_hosteq(rt->mfc_origin, mfccp->mfcc_origin) &&
1296 in_hosteq(rt->mfc_mcastgrp, mfccp->mfcc_mcastgrp)) {
1297 init_mfc_params(rt, mfccp);
1298 if (rt->mfc_expire)
1299 nexpire[hash]--;
1300 rt->mfc_expire = 0;
1301 break; /* XXX */
1302 }
1303 }
1304 if (rt == NULL) { /* no upcall, so make a new entry */
1305 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE,
1306 M_NOWAIT);
1307 if (rt == NULL) {
1308 splx(s);
1309 return (ENOBUFS);
1310 }
1311
1312 init_mfc_params(rt, mfccp);
1313 rt->mfc_expire = 0;
1314 rt->mfc_stall = NULL;
1315 rt->mfc_bw_meter = NULL;
1316
1317 /* insert new entry at head of hash chain */
1318 LIST_INSERT_HEAD(&mfchashtbl[hash], rt, mfc_hash);
1319 }
1320 }
1321
1322 splx(s);
1323 return (0);
1324 }
1325
1326 #ifdef UPCALL_TIMING
1327 /*
1328 * collect delay statistics on the upcalls
1329 */
1330 static void
1331 collate(t)
1332 struct timeval *t;
1333 {
1334 u_int32_t d;
1335 struct timeval tp;
1336 u_int32_t delta;
1337
1338 microtime(&tp);
1339
1340 if (timercmp(t, &tp, <)) {
1341 TV_DELTA(tp, *t, delta);
1342
1343 d = delta >> 10;
1344 if (d > 50)
1345 d = 50;
1346
1347 ++upcall_data[d];
1348 }
1349 }
1350 #endif /* UPCALL_TIMING */
1351
1352 /*
1353 * Delete an mfc entry
1354 */
1355 static int
1356 del_mfc(m)
1357 struct mbuf *m;
1358 {
1359 struct mfcctl2 mfcctl2;
1360 struct mfcctl2 *mfccp;
1361 struct mfc *rt;
1362 int s;
1363 int mfcctl_size = sizeof(struct mfcctl);
1364 struct mfcctl *mp = mtod(m, struct mfcctl *);
1365
1366 /*
1367 * XXX: for deleting MFC entries the information in entries
1368 * of size "struct mfcctl" is sufficient.
1369 */
1370
1371 if (m == NULL || m->m_len < mfcctl_size)
1372 return (EINVAL);
1373
1374 bcopy(mp, (caddr_t)&mfcctl2, sizeof(*mp));
1375 bzero((caddr_t)&mfcctl2 + sizeof(struct mfcctl),
1376 sizeof(mfcctl2) - sizeof(struct mfcctl));
1377
1378 mfccp = &mfcctl2;
1379
1380 if (mrtdebug & DEBUG_MFC)
1381 log(LOG_DEBUG, "del_mfc origin %x mcastgrp %x\n",
1382 ntohl(mfccp->mfcc_origin.s_addr),
1383 ntohl(mfccp->mfcc_mcastgrp.s_addr));
1384
1385 s = splsoftnet();
1386
1387 rt = mfc_find(&mfccp->mfcc_origin, &mfccp->mfcc_mcastgrp);
1388 if (rt == NULL) {
1389 splx(s);
1390 return (EADDRNOTAVAIL);
1391 }
1392
1393 /*
1394 * free the bw_meter entries
1395 */
1396 free_bw_list(rt->mfc_bw_meter);
1397 rt->mfc_bw_meter = NULL;
1398
1399 LIST_REMOVE(rt, mfc_hash);
1400 free(rt, M_MRTABLE);
1401
1402 splx(s);
1403 return (0);
1404 }
1405
1406 static int
1407 socket_send(s, mm, src)
1408 struct socket *s;
1409 struct mbuf *mm;
1410 struct sockaddr_in *src;
1411 {
1412 if (s) {
1413 if (sbappendaddr(&s->so_rcv, sintosa(src), mm,
1414 (struct mbuf *)NULL) != 0) {
1415 sorwakeup(s);
1416 return (0);
1417 }
1418 }
1419 m_freem(mm);
1420 return (-1);
1421 }
1422
1423 /*
1424 * IP multicast forwarding function. This function assumes that the packet
1425 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1426 * pointed to by "ifp", and the packet is to be relayed to other networks
1427 * that have members of the packet's destination IP multicast group.
1428 *
1429 * The packet is returned unscathed to the caller, unless it is
1430 * erroneous, in which case a non-zero return value tells the caller to
1431 * discard it.
1432 */
1433
1434 #define IP_HDR_LEN 20 /* # bytes of fixed IP header (excluding options) */
1435 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1436
1437 int
1438 #ifdef RSVP_ISI
1439 ip_mforward(m, ifp, imo)
1440 #else
1441 ip_mforward(m, ifp)
1442 #endif /* RSVP_ISI */
1443 struct mbuf *m;
1444 struct ifnet *ifp;
1445 #ifdef RSVP_ISI
1446 struct ip_moptions *imo;
1447 #endif /* RSVP_ISI */
1448 {
1449 struct ip *ip = mtod(m, struct ip *);
1450 struct mfc *rt;
1451 static int srctun = 0;
1452 struct mbuf *mm;
1453 int s;
1454 vifi_t vifi;
1455
1456 if (mrtdebug & DEBUG_FORWARD)
1457 log(LOG_DEBUG, "ip_mforward: src %x, dst %x, ifp %p\n",
1458 ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr), ifp);
1459
1460 if (ip->ip_hl < (IP_HDR_LEN + TUNNEL_LEN) >> 2 ||
1461 ((u_char *)(ip + 1))[1] != IPOPT_LSRR) {
1462 /*
1463 * Packet arrived via a physical interface or
1464 * an encapsulated tunnel or a register_vif.
1465 */
1466 } else {
1467 /*
1468 * Packet arrived through a source-route tunnel.
1469 * Source-route tunnels are no longer supported.
1470 */
1471 if ((srctun++ % 1000) == 0)
1472 log(LOG_ERR,
1473 "ip_mforward: received source-routed packet from %x\n",
1474 ntohl(ip->ip_src.s_addr));
1475
1476 return (1);
1477 }
1478
1479 #ifdef RSVP_ISI
1480 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1481 if (ip->ip_ttl < 255)
1482 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1483 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1484 struct vif *vifp = viftable + vifi;
1485 printf("Sending IPPROTO_RSVP from %x to %x on vif %d (%s%s)\n",
1486 ntohl(ip->ip_src), ntohl(ip->ip_dst), vifi,
1487 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1488 vifp->v_ifp->if_xname);
1489 }
1490 return (ip_mdq(m, ifp, (struct mfc *)NULL, vifi));
1491 }
1492 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1493 printf("Warning: IPPROTO_RSVP from %x to %x without vif option\n",
1494 ntohl(ip->ip_src), ntohl(ip->ip_dst));
1495 }
1496 #endif /* RSVP_ISI */
1497
1498 /*
1499 * Don't forward a packet with time-to-live of zero or one,
1500 * or a packet destined to a local-only group.
1501 */
1502 if (ip->ip_ttl <= 1 || IN_LOCAL_GROUP(ip->ip_dst.s_addr))
1503 return (0);
1504
1505 /*
1506 * Determine forwarding vifs from the forwarding cache table
1507 */
1508 s = splsoftnet();
1509 ++mrtstat.mrts_mfc_lookups;
1510 rt = mfc_find(&ip->ip_src, &ip->ip_dst);
1511
1512 /* Entry exists, so forward if necessary */
1513 if (rt != NULL) {
1514 splx(s);
1515 #ifdef RSVP_ISI
1516 return (ip_mdq(m, ifp, rt, -1));
1517 #else
1518 return (ip_mdq(m, ifp, rt));
1519 #endif /* RSVP_ISI */
1520 } else {
1521 /*
1522 * If we don't have a route for packet's origin,
1523 * Make a copy of the packet & send message to routing daemon
1524 */
1525
1526 struct mbuf *mb0;
1527 struct rtdetq *rte;
1528 u_int32_t hash;
1529 int hlen = ip->ip_hl << 2;
1530 #ifdef UPCALL_TIMING
1531 struct timeval tp;
1532
1533 microtime(&tp);
1534 #endif /* UPCALL_TIMING */
1535
1536 ++mrtstat.mrts_mfc_misses;
1537
1538 mrtstat.mrts_no_route++;
1539 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1540 log(LOG_DEBUG, "ip_mforward: no rte s %x g %x\n",
1541 ntohl(ip->ip_src.s_addr),
1542 ntohl(ip->ip_dst.s_addr));
1543
1544 /*
1545 * Allocate mbufs early so that we don't do extra work if we are
1546 * just going to fail anyway. Make sure to pullup the header so
1547 * that other people can't step on it.
1548 */
1549 rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE,
1550 M_NOWAIT);
1551 if (rte == NULL) {
1552 splx(s);
1553 return (ENOBUFS);
1554 }
1555 mb0 = m_copy(m, 0, M_COPYALL);
1556 M_PULLUP(mb0, hlen);
1557 if (mb0 == NULL) {
1558 free(rte, M_MRTABLE);
1559 splx(s);
1560 return (ENOBUFS);
1561 }
1562
1563 /* is there an upcall waiting for this flow? */
1564 hash = MFCHASH(ip->ip_src, ip->ip_dst);
1565 LIST_FOREACH(rt, &mfchashtbl[hash], mfc_hash) {
1566 if (in_hosteq(ip->ip_src, rt->mfc_origin) &&
1567 in_hosteq(ip->ip_dst, rt->mfc_mcastgrp) &&
1568 rt->mfc_stall != NULL)
1569 break;
1570 }
1571
1572 if (rt == NULL) {
1573 int i;
1574 struct igmpmsg *im;
1575
1576 /*
1577 * Locate the vifi for the incoming interface for
1578 * this packet.
1579 * If none found, drop packet.
1580 */
1581 for (vifi = 0; vifi < numvifs &&
1582 viftable[vifi].v_ifp != ifp; vifi++)
1583 ;
1584 if (vifi >= numvifs) /* vif not found, drop packet */
1585 goto non_fatal;
1586
1587 /* no upcall, so make a new entry */
1588 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE,
1589 M_NOWAIT);
1590 if (rt == NULL)
1591 goto fail;
1592
1593 /*
1594 * Make a copy of the header to send to the user level
1595 * process
1596 */
1597 mm = m_copy(m, 0, hlen);
1598 M_PULLUP(mm, hlen);
1599 if (mm == NULL)
1600 goto fail1;
1601
1602 /*
1603 * Send message to routing daemon to install
1604 * a route into the kernel table
1605 */
1606
1607 im = mtod(mm, struct igmpmsg *);
1608 im->im_msgtype = IGMPMSG_NOCACHE;
1609 im->im_mbz = 0;
1610 im->im_vif = vifi;
1611
1612 mrtstat.mrts_upcalls++;
1613
1614 sin.sin_addr = ip->ip_src;
1615 if (socket_send(ip_mrouter, mm, &sin) < 0) {
1616 log(LOG_WARNING,
1617 "ip_mforward: ip_mrouter socket queue full\n");
1618 ++mrtstat.mrts_upq_sockfull;
1619 fail1:
1620 free(rt, M_MRTABLE);
1621 fail:
1622 free(rte, M_MRTABLE);
1623 m_freem(mb0);
1624 splx(s);
1625 return (ENOBUFS);
1626 }
1627
1628 /* insert new entry at head of hash chain */
1629 rt->mfc_origin = ip->ip_src;
1630 rt->mfc_mcastgrp = ip->ip_dst;
1631 rt->mfc_pkt_cnt = 0;
1632 rt->mfc_byte_cnt = 0;
1633 rt->mfc_wrong_if = 0;
1634 rt->mfc_expire = UPCALL_EXPIRE;
1635 nexpire[hash]++;
1636 for (i = 0; i < numvifs; i++) {
1637 rt->mfc_ttls[i] = 0;
1638 rt->mfc_flags[i] = 0;
1639 }
1640 rt->mfc_parent = -1;
1641
1642 /* clear the RP address */
1643 rt->mfc_rp = zeroin_addr;
1644
1645 rt->mfc_bw_meter = NULL;
1646
1647 /* link into table */
1648 LIST_INSERT_HEAD(&mfchashtbl[hash], rt, mfc_hash);
1649 /* Add this entry to the end of the queue */
1650 rt->mfc_stall = rte;
1651 } else {
1652 /* determine if q has overflowed */
1653 struct rtdetq **p;
1654 int npkts = 0;
1655
1656 /*
1657 * XXX ouch! we need to append to the list, but we
1658 * only have a pointer to the front, so we have to
1659 * scan the entire list every time.
1660 */
1661 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1662 if (++npkts > MAX_UPQ) {
1663 mrtstat.mrts_upq_ovflw++;
1664 non_fatal:
1665 free(rte, M_MRTABLE);
1666 m_freem(mb0);
1667 splx(s);
1668 return (0);
1669 }
1670
1671 /* Add this entry to the end of the queue */
1672 *p = rte;
1673 }
1674
1675 rte->next = NULL;
1676 rte->m = mb0;
1677 rte->ifp = ifp;
1678 #ifdef UPCALL_TIMING
1679 rte->t = tp;
1680 #endif /* UPCALL_TIMING */
1681
1682 splx(s);
1683
1684 return (0);
1685 }
1686 }
1687
1688
1689 /*ARGSUSED*/
1690 static void
1691 expire_upcalls(v)
1692 void *v;
1693 {
1694 int i;
1695 int s;
1696
1697 s = splsoftnet();
1698
1699 for (i = 0; i < MFCTBLSIZ; i++) {
1700 struct mfc *rt, *nrt;
1701
1702 if (nexpire[i] == 0)
1703 continue;
1704
1705 for (rt = LIST_FIRST(&mfchashtbl[i]); rt; rt = nrt) {
1706 nrt = LIST_NEXT(rt, mfc_hash);
1707
1708 if (rt->mfc_expire == 0 || --rt->mfc_expire > 0)
1709 continue;
1710 nexpire[i]--;
1711
1712 /*
1713 * free the bw_meter entries
1714 */
1715 while (rt->mfc_bw_meter != NULL) {
1716 struct bw_meter *x = rt->mfc_bw_meter;
1717
1718 rt->mfc_bw_meter = x->bm_mfc_next;
1719 free(x, M_BWMETER);
1720 }
1721
1722 ++mrtstat.mrts_cache_cleanups;
1723 if (mrtdebug & DEBUG_EXPIRE)
1724 log(LOG_DEBUG,
1725 "expire_upcalls: expiring (%x %x)\n",
1726 ntohl(rt->mfc_origin.s_addr),
1727 ntohl(rt->mfc_mcastgrp.s_addr));
1728
1729 expire_mfc(rt);
1730 }
1731 }
1732
1733 splx(s);
1734 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
1735 expire_upcalls, NULL);
1736 }
1737
1738 /*
1739 * Packet forwarding routine once entry in the cache is made
1740 */
1741 static int
1742 #ifdef RSVP_ISI
1743 ip_mdq(m, ifp, rt, xmt_vif)
1744 #else
1745 ip_mdq(m, ifp, rt)
1746 #endif /* RSVP_ISI */
1747 struct mbuf *m;
1748 struct ifnet *ifp;
1749 struct mfc *rt;
1750 #ifdef RSVP_ISI
1751 vifi_t xmt_vif;
1752 #endif /* RSVP_ISI */
1753 {
1754 struct ip *ip = mtod(m, struct ip *);
1755 vifi_t vifi;
1756 struct vif *vifp;
1757 int plen = ntohs(ip->ip_len) - (ip->ip_hl << 2);
1758
1759 /*
1760 * Macro to send packet on vif. Since RSVP packets don't get counted on
1761 * input, they shouldn't get counted on output, so statistics keeping is
1762 * separate.
1763 */
1764 #define MC_SEND(ip, vifp, m) do { \
1765 if ((vifp)->v_flags & VIFF_TUNNEL) \
1766 encap_send((ip), (vifp), (m)); \
1767 else \
1768 phyint_send((ip), (vifp), (m)); \
1769 } while (/*CONSTCOND*/ 0)
1770
1771 #ifdef RSVP_ISI
1772 /*
1773 * If xmt_vif is not -1, send on only the requested vif.
1774 *
1775 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.
1776 */
1777 if (xmt_vif < numvifs) {
1778 #ifdef PIM
1779 if (viftable[xmt_vif].v_flags & VIFF_REGISTER)
1780 pim_register_send(ip, viftable + xmt_vif, m, rt);
1781 else
1782 #endif
1783 MC_SEND(ip, viftable + xmt_vif, m);
1784 return (1);
1785 }
1786 #endif /* RSVP_ISI */
1787
1788 /*
1789 * Don't forward if it didn't arrive from the parent vif for its origin.
1790 */
1791 vifi = rt->mfc_parent;
1792 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1793 /* came in the wrong interface */
1794 if (mrtdebug & DEBUG_FORWARD)
1795 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1796 ifp, vifi,
1797 vifi >= numvifs ? 0 : viftable[vifi].v_ifp);
1798 ++mrtstat.mrts_wrong_if;
1799 ++rt->mfc_wrong_if;
1800 /*
1801 * If we are doing PIM assert processing, send a message
1802 * to the routing daemon.
1803 *
1804 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1805 * can complete the SPT switch, regardless of the type
1806 * of the iif (broadcast media, GRE tunnel, etc).
1807 */
1808 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) {
1809 struct timeval now;
1810 u_int32_t delta;
1811
1812 #ifdef PIM
1813 if (ifp == &multicast_register_if)
1814 pimstat.pims_rcv_registers_wrongiif++;
1815 #endif
1816
1817 /* Get vifi for the incoming packet */
1818 for (vifi = 0;
1819 vifi < numvifs && viftable[vifi].v_ifp != ifp;
1820 vifi++)
1821 ;
1822 if (vifi >= numvifs) {
1823 /* The iif is not found: ignore the packet. */
1824 return (0);
1825 }
1826
1827 if (rt->mfc_flags[vifi] &
1828 MRT_MFC_FLAGS_DISABLE_WRONGVIF) {
1829 /* WRONGVIF disabled: ignore the packet */
1830 return (0);
1831 }
1832
1833 microtime(&now);
1834
1835 TV_DELTA(rt->mfc_last_assert, now, delta);
1836
1837 if (delta > ASSERT_MSG_TIME) {
1838 struct igmpmsg *im;
1839 int hlen = ip->ip_hl << 2;
1840 struct mbuf *mm = m_copy(m, 0, hlen);
1841
1842 M_PULLUP(mm, hlen);
1843 if (mm == NULL)
1844 return (ENOBUFS);
1845
1846 rt->mfc_last_assert = now;
1847
1848 im = mtod(mm, struct igmpmsg *);
1849 im->im_msgtype = IGMPMSG_WRONGVIF;
1850 im->im_mbz = 0;
1851 im->im_vif = vifi;
1852
1853 mrtstat.mrts_upcalls++;
1854
1855 sin.sin_addr = im->im_src;
1856 if (socket_send(ip_mrouter, mm, &sin) < 0) {
1857 log(LOG_WARNING,
1858 "ip_mforward: ip_mrouter socket queue full\n");
1859 ++mrtstat.mrts_upq_sockfull;
1860 return (ENOBUFS);
1861 }
1862 }
1863 }
1864 return (0);
1865 }
1866
1867 /* If I sourced this packet, it counts as output, else it was input. */
1868 if (in_hosteq(ip->ip_src, viftable[vifi].v_lcl_addr)) {
1869 viftable[vifi].v_pkt_out++;
1870 viftable[vifi].v_bytes_out += plen;
1871 } else {
1872 viftable[vifi].v_pkt_in++;
1873 viftable[vifi].v_bytes_in += plen;
1874 }
1875 rt->mfc_pkt_cnt++;
1876 rt->mfc_byte_cnt += plen;
1877
1878 /*
1879 * For each vif, decide if a copy of the packet should be forwarded.
1880 * Forward if:
1881 * - the ttl exceeds the vif's threshold
1882 * - there are group members downstream on interface
1883 */
1884 for (vifp = viftable, vifi = 0; vifi < numvifs; vifp++, vifi++)
1885 if ((rt->mfc_ttls[vifi] > 0) &&
1886 (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1887 vifp->v_pkt_out++;
1888 vifp->v_bytes_out += plen;
1889 #ifdef PIM
1890 if (vifp->v_flags & VIFF_REGISTER)
1891 pim_register_send(ip, vifp, m, rt);
1892 else
1893 #endif
1894 MC_SEND(ip, vifp, m);
1895 }
1896
1897 /*
1898 * Perform upcall-related bw measuring.
1899 */
1900 if (rt->mfc_bw_meter != NULL) {
1901 struct bw_meter *x;
1902 struct timeval now;
1903
1904 microtime(&now);
1905 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next)
1906 bw_meter_receive_packet(x, plen, &now);
1907 }
1908
1909 return (0);
1910 }
1911
1912 #ifdef RSVP_ISI
1913 /*
1914 * check if a vif number is legal/ok. This is used by ip_output.
1915 */
1916 int
1917 legal_vif_num(vif)
1918 int vif;
1919 {
1920 if (vif >= 0 && vif < numvifs)
1921 return (1);
1922 else
1923 return (0);
1924 }
1925 #endif /* RSVP_ISI */
1926
1927 static void
1928 phyint_send(ip, vifp, m)
1929 struct ip *ip;
1930 struct vif *vifp;
1931 struct mbuf *m;
1932 {
1933 struct mbuf *mb_copy;
1934 int hlen = ip->ip_hl << 2;
1935
1936 /*
1937 * Make a new reference to the packet; make sure that
1938 * the IP header is actually copied, not just referenced,
1939 * so that ip_output() only scribbles on the copy.
1940 */
1941 mb_copy = m_copy(m, 0, M_COPYALL);
1942 M_PULLUP(mb_copy, hlen);
1943 if (mb_copy == NULL)
1944 return;
1945
1946 if (vifp->v_rate_limit <= 0)
1947 tbf_send_packet(vifp, mb_copy);
1948 else
1949 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *),
1950 ntohs(ip->ip_len));
1951 }
1952
1953 static void
1954 encap_send(ip, vifp, m)
1955 struct ip *ip;
1956 struct vif *vifp;
1957 struct mbuf *m;
1958 {
1959 struct mbuf *mb_copy;
1960 struct ip *ip_copy;
1961 int i, len = ntohs(ip->ip_len) + sizeof(multicast_encap_iphdr);
1962
1963 /* Take care of delayed checksums */
1964 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
1965 in_delayed_cksum(m);
1966 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
1967 }
1968
1969 /*
1970 * copy the old packet & pullup it's IP header into the
1971 * new mbuf so we can modify it. Try to fill the new
1972 * mbuf since if we don't the ethernet driver will.
1973 */
1974 MGETHDR(mb_copy, M_DONTWAIT, MT_DATA);
1975 if (mb_copy == NULL)
1976 return;
1977 mb_copy->m_data += max_linkhdr;
1978 mb_copy->m_pkthdr.len = len;
1979 mb_copy->m_len = sizeof(multicast_encap_iphdr);
1980
1981 if ((mb_copy->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
1982 m_freem(mb_copy);
1983 return;
1984 }
1985 i = MHLEN - max_linkhdr;
1986 if (i > len)
1987 i = len;
1988 mb_copy = m_pullup(mb_copy, i);
1989 if (mb_copy == NULL)
1990 return;
1991
1992 /*
1993 * fill in the encapsulating IP header.
1994 */
1995 ip_copy = mtod(mb_copy, struct ip *);
1996 *ip_copy = multicast_encap_iphdr;
1997 ip_copy->ip_id = ip_newid();
1998 ip_copy->ip_len = htons(len);
1999 ip_copy->ip_src = vifp->v_lcl_addr;
2000 ip_copy->ip_dst = vifp->v_rmt_addr;
2001
2002 /*
2003 * turn the encapsulated IP header back into a valid one.
2004 */
2005 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
2006 --ip->ip_ttl;
2007 ip->ip_sum = 0;
2008 mb_copy->m_data += sizeof(multicast_encap_iphdr);
2009 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
2010 mb_copy->m_data -= sizeof(multicast_encap_iphdr);
2011
2012 if (vifp->v_rate_limit <= 0)
2013 tbf_send_packet(vifp, mb_copy);
2014 else
2015 tbf_control(vifp, mb_copy, ip, ntohs(ip_copy->ip_len));
2016 }
2017
2018 /*
2019 * De-encapsulate a packet and feed it back through ip input.
2020 */
2021 static void
2022 vif_input(struct mbuf *m, ...)
2023 {
2024 int off, proto;
2025 va_list ap;
2026 struct vif *vifp;
2027 int s;
2028 struct ifqueue *ifq;
2029
2030 va_start(ap, m);
2031 off = va_arg(ap, int);
2032 proto = va_arg(ap, int);
2033 va_end(ap);
2034
2035 vifp = (struct vif *)encap_getarg(m);
2036 if (!vifp || proto != AF_INET) {
2037 m_freem(m);
2038 mrtstat.mrts_bad_tunnel++;
2039 return;
2040 }
2041
2042 m_adj(m, off);
2043 m->m_pkthdr.rcvif = vifp->v_ifp;
2044 ifq = &ipintrq;
2045 s = splnet();
2046 if (IF_QFULL(ifq)) {
2047 IF_DROP(ifq);
2048 m_freem(m);
2049 } else {
2050 IF_ENQUEUE(ifq, m);
2051 /*
2052 * normally we would need a "schednetisr(NETISR_IP)"
2053 * here but we were called by ip_input and it is going
2054 * to loop back & try to dequeue the packet we just
2055 * queued as soon as we return so we avoid the
2056 * unnecessary software interrrupt.
2057 */
2058 }
2059 splx(s);
2060 }
2061
2062 /*
2063 * Check if the packet should be grabbed by us.
2064 */
2065 static int
2066 vif_encapcheck(m, off, proto, arg)
2067 const struct mbuf *m;
2068 int off;
2069 int proto;
2070 void *arg;
2071 {
2072 struct vif *vifp;
2073 struct ip ip;
2074
2075 #ifdef DIAGNOSTIC
2076 if (!arg || proto != IPPROTO_IPV4)
2077 panic("unexpected arg in vif_encapcheck");
2078 #endif
2079
2080 /*
2081 * do not grab the packet if it's not to a multicast destination or if
2082 * we don't have an encapsulating tunnel with the source.
2083 * Note: This code assumes that the remote site IP address
2084 * uniquely identifies the tunnel (i.e., that this site has
2085 * at most one tunnel with the remote site).
2086 */
2087
2088 /* LINTED const cast */
2089 m_copydata((struct mbuf *)m, off, sizeof(ip), (caddr_t)&ip);
2090 if (!IN_MULTICAST(ip.ip_dst.s_addr))
2091 return 0;
2092
2093 /* LINTED const cast */
2094 m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
2095 if (!in_hosteq(ip.ip_src, last_encap_src)) {
2096 vifp = (struct vif *)arg;
2097 if (vifp->v_flags & VIFF_TUNNEL &&
2098 in_hosteq(vifp->v_rmt_addr, ip.ip_src))
2099 ;
2100 else
2101 return 0;
2102 last_encap_vif = vifp;
2103 last_encap_src = ip.ip_src;
2104 } else
2105 vifp = last_encap_vif;
2106
2107 /* 32bit match, since we have checked ip_src only */
2108 return 32;
2109 }
2110
2111 /*
2112 * Token bucket filter module
2113 */
2114 static void
2115 tbf_control(vifp, m, ip, len)
2116 struct vif *vifp;
2117 struct mbuf *m;
2118 struct ip *ip;
2119 u_int32_t len;
2120 {
2121
2122 if (len > MAX_BKT_SIZE) {
2123 /* drop if packet is too large */
2124 mrtstat.mrts_pkt2large++;
2125 m_freem(m);
2126 return;
2127 }
2128
2129 tbf_update_tokens(vifp);
2130
2131 /*
2132 * If there are enough tokens, and the queue is empty, send this packet
2133 * out immediately. Otherwise, try to insert it on this vif's queue.
2134 */
2135 if (vifp->tbf_q_len == 0) {
2136 if (len <= vifp->tbf_n_tok) {
2137 vifp->tbf_n_tok -= len;
2138 tbf_send_packet(vifp, m);
2139 } else {
2140 /* queue packet and timeout till later */
2141 tbf_queue(vifp, m);
2142 callout_reset(&vifp->v_repq_ch, TBF_REPROCESS,
2143 tbf_reprocess_q, vifp);
2144 }
2145 } else {
2146 if (vifp->tbf_q_len >= vifp->tbf_max_q_len &&
2147 !tbf_dq_sel(vifp, ip)) {
2148 /* queue full, and couldn't make room */
2149 mrtstat.mrts_q_overflow++;
2150 m_freem(m);
2151 } else {
2152 /* queue length low enough, or made room */
2153 tbf_queue(vifp, m);
2154 tbf_process_q(vifp);
2155 }
2156 }
2157 }
2158
2159 /*
2160 * adds a packet to the queue at the interface
2161 */
2162 static void
2163 tbf_queue(vifp, m)
2164 struct vif *vifp;
2165 struct mbuf *m;
2166 {
2167 int s = splsoftnet();
2168
2169 /* insert at tail */
2170 *vifp->tbf_t = m;
2171 vifp->tbf_t = &m->m_nextpkt;
2172 vifp->tbf_q_len++;
2173
2174 splx(s);
2175 }
2176
2177
2178 /*
2179 * processes the queue at the interface
2180 */
2181 static void
2182 tbf_process_q(vifp)
2183 struct vif *vifp;
2184 {
2185 struct mbuf *m;
2186 int len;
2187 int s = splsoftnet();
2188
2189 /*
2190 * Loop through the queue at the interface and send as many packets
2191 * as possible.
2192 */
2193 for (m = vifp->tbf_q; m != NULL; m = vifp->tbf_q) {
2194 len = ntohs(mtod(m, struct ip *)->ip_len);
2195
2196 /* determine if the packet can be sent */
2197 if (len <= vifp->tbf_n_tok) {
2198 /* if so,
2199 * reduce no of tokens, dequeue the packet,
2200 * send the packet.
2201 */
2202 if ((vifp->tbf_q = m->m_nextpkt) == NULL)
2203 vifp->tbf_t = &vifp->tbf_q;
2204 --vifp->tbf_q_len;
2205
2206 m->m_nextpkt = NULL;
2207 vifp->tbf_n_tok -= len;
2208 tbf_send_packet(vifp, m);
2209 } else
2210 break;
2211 }
2212 splx(s);
2213 }
2214
2215 static void
2216 tbf_reprocess_q(arg)
2217 void *arg;
2218 {
2219 struct vif *vifp = arg;
2220
2221 if (ip_mrouter == NULL)
2222 return;
2223
2224 tbf_update_tokens(vifp);
2225 tbf_process_q(vifp);
2226
2227 if (vifp->tbf_q_len != 0)
2228 callout_reset(&vifp->v_repq_ch, TBF_REPROCESS,
2229 tbf_reprocess_q, vifp);
2230 }
2231
2232 /* function that will selectively discard a member of the queue
2233 * based on the precedence value and the priority
2234 */
2235 static int
2236 tbf_dq_sel(vifp, ip)
2237 struct vif *vifp;
2238 struct ip *ip;
2239 {
2240 u_int p;
2241 struct mbuf **mp, *m;
2242 int s = splsoftnet();
2243
2244 p = priority(vifp, ip);
2245
2246 for (mp = &vifp->tbf_q, m = *mp;
2247 m != NULL;
2248 mp = &m->m_nextpkt, m = *mp) {
2249 if (p > priority(vifp, mtod(m, struct ip *))) {
2250 if ((*mp = m->m_nextpkt) == NULL)
2251 vifp->tbf_t = mp;
2252 --vifp->tbf_q_len;
2253
2254 m_freem(m);
2255 mrtstat.mrts_drop_sel++;
2256 splx(s);
2257 return (1);
2258 }
2259 }
2260 splx(s);
2261 return (0);
2262 }
2263
2264 static void
2265 tbf_send_packet(vifp, m)
2266 struct vif *vifp;
2267 struct mbuf *m;
2268 {
2269 int error;
2270 int s = splsoftnet();
2271
2272 if (vifp->v_flags & VIFF_TUNNEL) {
2273 /* If tunnel options */
2274 ip_output(m, (struct mbuf *)NULL, &vifp->v_route,
2275 IP_FORWARDING, (struct ip_moptions *)NULL,
2276 (struct socket *)NULL);
2277 } else {
2278 /* if physical interface option, extract the options and then send */
2279 struct ip_moptions imo;
2280
2281 imo.imo_multicast_ifp = vifp->v_ifp;
2282 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
2283 imo.imo_multicast_loop = 1;
2284 #ifdef RSVP_ISI
2285 imo.imo_multicast_vif = -1;
2286 #endif
2287
2288 error = ip_output(m, (struct mbuf *)NULL, (struct route *)NULL,
2289 IP_FORWARDING|IP_MULTICASTOPTS, &imo,
2290 (struct socket *)NULL);
2291
2292 if (mrtdebug & DEBUG_XMIT)
2293 log(LOG_DEBUG, "phyint_send on vif %ld err %d\n",
2294 (long)(vifp - viftable), error);
2295 }
2296 splx(s);
2297 }
2298
2299 /* determine the current time and then
2300 * the elapsed time (between the last time and time now)
2301 * in milliseconds & update the no. of tokens in the bucket
2302 */
2303 static void
2304 tbf_update_tokens(vifp)
2305 struct vif *vifp;
2306 {
2307 struct timeval tp;
2308 u_int32_t tm;
2309 int s = splsoftnet();
2310
2311 microtime(&tp);
2312
2313 TV_DELTA(tp, vifp->tbf_last_pkt_t, tm);
2314
2315 /*
2316 * This formula is actually
2317 * "time in seconds" * "bytes/second".
2318 *
2319 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
2320 *
2321 * The (1000/1024) was introduced in add_vif to optimize
2322 * this divide into a shift.
2323 */
2324 vifp->tbf_n_tok += tm * vifp->v_rate_limit / 8192;
2325 vifp->tbf_last_pkt_t = tp;
2326
2327 if (vifp->tbf_n_tok > MAX_BKT_SIZE)
2328 vifp->tbf_n_tok = MAX_BKT_SIZE;
2329
2330 splx(s);
2331 }
2332
2333 static int
2334 priority(vifp, ip)
2335 struct vif *vifp;
2336 struct ip *ip;
2337 {
2338 int prio = 50; /* the lowest priority -- default case */
2339
2340 /* temporary hack; may add general packet classifier some day */
2341
2342 /*
2343 * The UDP port space is divided up into four priority ranges:
2344 * [0, 16384) : unclassified - lowest priority
2345 * [16384, 32768) : audio - highest priority
2346 * [32768, 49152) : whiteboard - medium priority
2347 * [49152, 65536) : video - low priority
2348 */
2349 if (ip->ip_p == IPPROTO_UDP) {
2350 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
2351
2352 switch (ntohs(udp->uh_dport) & 0xc000) {
2353 case 0x4000:
2354 prio = 70;
2355 break;
2356 case 0x8000:
2357 prio = 60;
2358 break;
2359 case 0xc000:
2360 prio = 55;
2361 break;
2362 }
2363
2364 if (tbfdebug > 1)
2365 log(LOG_DEBUG, "port %x prio %d\n",
2366 ntohs(udp->uh_dport), prio);
2367 }
2368
2369 return (prio);
2370 }
2371
2372 /*
2373 * End of token bucket filter modifications
2374 */
2375 #ifdef RSVP_ISI
2376 int
2377 ip_rsvp_vif_init(so, m)
2378 struct socket *so;
2379 struct mbuf *m;
2380 {
2381 int vifi, s;
2382
2383 if (rsvpdebug)
2384 printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
2385 so->so_type, so->so_proto->pr_protocol);
2386
2387 if (so->so_type != SOCK_RAW ||
2388 so->so_proto->pr_protocol != IPPROTO_RSVP)
2389 return (EOPNOTSUPP);
2390
2391 /* Check mbuf. */
2392 if (m == NULL || m->m_len != sizeof(int)) {
2393 return (EINVAL);
2394 }
2395 vifi = *(mtod(m, int *));
2396
2397 if (rsvpdebug)
2398 printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n",
2399 vifi, rsvp_on);
2400
2401 s = splsoftnet();
2402
2403 /* Check vif. */
2404 if (!legal_vif_num(vifi)) {
2405 splx(s);
2406 return (EADDRNOTAVAIL);
2407 }
2408
2409 /* Check if socket is available. */
2410 if (viftable[vifi].v_rsvpd != NULL) {
2411 splx(s);
2412 return (EADDRINUSE);
2413 }
2414
2415 viftable[vifi].v_rsvpd = so;
2416 /*
2417 * This may seem silly, but we need to be sure we don't over-increment
2418 * the RSVP counter, in case something slips up.
2419 */
2420 if (!viftable[vifi].v_rsvp_on) {
2421 viftable[vifi].v_rsvp_on = 1;
2422 rsvp_on++;
2423 }
2424
2425 splx(s);
2426 return (0);
2427 }
2428
2429 int
2430 ip_rsvp_vif_done(so, m)
2431 struct socket *so;
2432 struct mbuf *m;
2433 {
2434 int vifi, s;
2435
2436 if (rsvpdebug)
2437 printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n",
2438 so->so_type, so->so_proto->pr_protocol);
2439
2440 if (so->so_type != SOCK_RAW ||
2441 so->so_proto->pr_protocol != IPPROTO_RSVP)
2442 return (EOPNOTSUPP);
2443
2444 /* Check mbuf. */
2445 if (m == NULL || m->m_len != sizeof(int)) {
2446 return (EINVAL);
2447 }
2448 vifi = *(mtod(m, int *));
2449
2450 s = splsoftnet();
2451
2452 /* Check vif. */
2453 if (!legal_vif_num(vifi)) {
2454 splx(s);
2455 return (EADDRNOTAVAIL);
2456 }
2457
2458 if (rsvpdebug)
2459 printf("ip_rsvp_vif_done: v_rsvpd = %x so = %x\n",
2460 viftable[vifi].v_rsvpd, so);
2461
2462 viftable[vifi].v_rsvpd = NULL;
2463 /*
2464 * This may seem silly, but we need to be sure we don't over-decrement
2465 * the RSVP counter, in case something slips up.
2466 */
2467 if (viftable[vifi].v_rsvp_on) {
2468 viftable[vifi].v_rsvp_on = 0;
2469 rsvp_on--;
2470 }
2471
2472 splx(s);
2473 return (0);
2474 }
2475
2476 void
2477 ip_rsvp_force_done(so)
2478 struct socket *so;
2479 {
2480 int vifi, s;
2481
2482 /* Don't bother if it is not the right type of socket. */
2483 if (so->so_type != SOCK_RAW ||
2484 so->so_proto->pr_protocol != IPPROTO_RSVP)
2485 return;
2486
2487 s = splsoftnet();
2488
2489 /*
2490 * The socket may be attached to more than one vif...this
2491 * is perfectly legal.
2492 */
2493 for (vifi = 0; vifi < numvifs; vifi++) {
2494 if (viftable[vifi].v_rsvpd == so) {
2495 viftable[vifi].v_rsvpd = NULL;
2496 /*
2497 * This may seem silly, but we need to be sure we don't
2498 * over-decrement the RSVP counter, in case something
2499 * slips up.
2500 */
2501 if (viftable[vifi].v_rsvp_on) {
2502 viftable[vifi].v_rsvp_on = 0;
2503 rsvp_on--;
2504 }
2505 }
2506 }
2507
2508 splx(s);
2509 return;
2510 }
2511
2512 void
2513 rsvp_input(m, ifp)
2514 struct mbuf *m;
2515 struct ifnet *ifp;
2516 {
2517 int vifi, s;
2518 struct ip *ip = mtod(m, struct ip *);
2519 static struct sockaddr_in rsvp_src = { sizeof(sin), AF_INET };
2520
2521 if (rsvpdebug)
2522 printf("rsvp_input: rsvp_on %d\n", rsvp_on);
2523
2524 /*
2525 * Can still get packets with rsvp_on = 0 if there is a local member
2526 * of the group to which the RSVP packet is addressed. But in this
2527 * case we want to throw the packet away.
2528 */
2529 if (!rsvp_on) {
2530 m_freem(m);
2531 return;
2532 }
2533
2534 /*
2535 * If the old-style non-vif-associated socket is set, then use
2536 * it and ignore the new ones.
2537 */
2538 if (ip_rsvpd != NULL) {
2539 if (rsvpdebug)
2540 printf("rsvp_input: "
2541 "Sending packet up old-style socket\n");
2542 rip_input(m); /*XXX*/
2543 return;
2544 }
2545
2546 s = splsoftnet();
2547
2548 if (rsvpdebug)
2549 printf("rsvp_input: check vifs\n");
2550
2551 /* Find which vif the packet arrived on. */
2552 for (vifi = 0; vifi < numvifs; vifi++) {
2553 if (viftable[vifi].v_ifp == ifp)
2554 break;
2555 }
2556
2557 if (vifi == numvifs) {
2558 /* Can't find vif packet arrived on. Drop packet. */
2559 if (rsvpdebug)
2560 printf("rsvp_input: "
2561 "Can't find vif for packet...dropping it.\n");
2562 m_freem(m);
2563 splx(s);
2564 return;
2565 }
2566
2567 if (rsvpdebug)
2568 printf("rsvp_input: check socket\n");
2569
2570 if (viftable[vifi].v_rsvpd == NULL) {
2571 /*
2572 * drop packet, since there is no specific socket for this
2573 * interface
2574 */
2575 if (rsvpdebug)
2576 printf("rsvp_input: No socket defined for vif %d\n",
2577 vifi);
2578 m_freem(m);
2579 splx(s);
2580 return;
2581 }
2582
2583 rsvp_src.sin_addr = ip->ip_src;
2584
2585 if (rsvpdebug && m)
2586 printf("rsvp_input: m->m_len = %d, sbspace() = %d\n",
2587 m->m_len, sbspace(&viftable[vifi].v_rsvpd->so_rcv));
2588
2589 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0)
2590 if (rsvpdebug)
2591 printf("rsvp_input: Failed to append to socket\n");
2592 else
2593 if (rsvpdebug)
2594 printf("rsvp_input: send packet up\n");
2595
2596 splx(s);
2597 }
2598 #endif /* RSVP_ISI */
2599
2600 /*
2601 * Code for bandwidth monitors
2602 */
2603
2604 /*
2605 * Define common interface for timeval-related methods
2606 */
2607 #define BW_TIMEVALCMP(tvp, uvp, cmp) timercmp((tvp), (uvp), cmp)
2608 #define BW_TIMEVALDECR(vvp, uvp) timersub((vvp), (uvp), (vvp))
2609 #define BW_TIMEVALADD(vvp, uvp) timeradd((vvp), (uvp), (vvp))
2610
2611 static uint32_t
2612 compute_bw_meter_flags(struct bw_upcall *req)
2613 {
2614 uint32_t flags = 0;
2615
2616 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
2617 flags |= BW_METER_UNIT_PACKETS;
2618 if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
2619 flags |= BW_METER_UNIT_BYTES;
2620 if (req->bu_flags & BW_UPCALL_GEQ)
2621 flags |= BW_METER_GEQ;
2622 if (req->bu_flags & BW_UPCALL_LEQ)
2623 flags |= BW_METER_LEQ;
2624
2625 return flags;
2626 }
2627
2628 /*
2629 * Add a bw_meter entry
2630 */
2631 static int
2632 add_bw_upcall(struct mbuf *m)
2633 {
2634 int s;
2635 struct mfc *mfc;
2636 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
2637 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
2638 struct timeval now;
2639 struct bw_meter *x;
2640 uint32_t flags;
2641 struct bw_upcall *req;
2642
2643 if (m == NULL || m->m_len < sizeof(struct bw_upcall))
2644 return EINVAL;
2645
2646 req = mtod(m, struct bw_upcall *);
2647
2648 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2649 return EOPNOTSUPP;
2650
2651 /* Test if the flags are valid */
2652 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
2653 return EINVAL;
2654 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
2655 return EINVAL;
2656 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2657 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2658 return EINVAL;
2659
2660 /* Test if the threshold time interval is valid */
2661 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
2662 return EINVAL;
2663
2664 flags = compute_bw_meter_flags(req);
2665
2666 /*
2667 * Find if we have already same bw_meter entry
2668 */
2669 s = splsoftnet();
2670 mfc = mfc_find(&req->bu_src, &req->bu_dst);
2671 if (mfc == NULL) {
2672 splx(s);
2673 return EADDRNOTAVAIL;
2674 }
2675 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) {
2676 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2677 &req->bu_threshold.b_time, ==)) &&
2678 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2679 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2680 (x->bm_flags & BW_METER_USER_FLAGS) == flags) {
2681 splx(s);
2682 return 0; /* XXX Already installed */
2683 }
2684 }
2685
2686 /* Allocate the new bw_meter entry */
2687 x = (struct bw_meter *)malloc(sizeof(*x), M_BWMETER, M_NOWAIT);
2688 if (x == NULL) {
2689 splx(s);
2690 return ENOBUFS;
2691 }
2692
2693 /* Set the new bw_meter entry */
2694 x->bm_threshold.b_time = req->bu_threshold.b_time;
2695 microtime(&now);
2696 x->bm_start_time = now;
2697 x->bm_threshold.b_packets = req->bu_threshold.b_packets;
2698 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
2699 x->bm_measured.b_packets = 0;
2700 x->bm_measured.b_bytes = 0;
2701 x->bm_flags = flags;
2702 x->bm_time_next = NULL;
2703 x->bm_time_hash = BW_METER_BUCKETS;
2704
2705 /* Add the new bw_meter entry to the front of entries for this MFC */
2706 x->bm_mfc = mfc;
2707 x->bm_mfc_next = mfc->mfc_bw_meter;
2708 mfc->mfc_bw_meter = x;
2709 schedule_bw_meter(x, &now);
2710 splx(s);
2711
2712 return 0;
2713 }
2714
2715 static void
2716 free_bw_list(struct bw_meter *list)
2717 {
2718 while (list != NULL) {
2719 struct bw_meter *x = list;
2720
2721 list = list->bm_mfc_next;
2722 unschedule_bw_meter(x);
2723 free(x, M_BWMETER);
2724 }
2725 }
2726
2727 /*
2728 * Delete one or multiple bw_meter entries
2729 */
2730 static int
2731 del_bw_upcall(struct mbuf *m)
2732 {
2733 int s;
2734 struct mfc *mfc;
2735 struct bw_meter *x;
2736 struct bw_upcall *req;
2737
2738 if (m == NULL || m->m_len < sizeof(struct bw_upcall))
2739 return EINVAL;
2740
2741 req = mtod(m, struct bw_upcall *);
2742
2743 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2744 return EOPNOTSUPP;
2745
2746 s = splsoftnet();
2747 /* Find the corresponding MFC entry */
2748 mfc = mfc_find(&req->bu_src, &req->bu_dst);
2749 if (mfc == NULL) {
2750 splx(s);
2751 return EADDRNOTAVAIL;
2752 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2753 /*
2754 * Delete all bw_meter entries for this mfc
2755 */
2756 struct bw_meter *list;
2757
2758 list = mfc->mfc_bw_meter;
2759 mfc->mfc_bw_meter = NULL;
2760 free_bw_list(list);
2761 splx(s);
2762 return 0;
2763 } else { /* Delete a single bw_meter entry */
2764 struct bw_meter *prev;
2765 uint32_t flags = 0;
2766
2767 flags = compute_bw_meter_flags(req);
2768
2769 /* Find the bw_meter entry to delete */
2770 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
2771 prev = x, x = x->bm_mfc_next) {
2772 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2773 &req->bu_threshold.b_time, ==)) &&
2774 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2775 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2776 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2777 break;
2778 }
2779 if (x != NULL) { /* Delete entry from the list for this MFC */
2780 if (prev != NULL)
2781 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
2782 else
2783 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
2784
2785 unschedule_bw_meter(x);
2786 splx(s);
2787 /* Free the bw_meter entry */
2788 free(x, M_BWMETER);
2789 return 0;
2790 } else {
2791 splx(s);
2792 return EINVAL;
2793 }
2794 }
2795 /* NOTREACHED */
2796 }
2797
2798 /*
2799 * Perform bandwidth measurement processing that may result in an upcall
2800 */
2801 static void
2802 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2803 {
2804 struct timeval delta;
2805
2806 delta = *nowp;
2807 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2808
2809 if (x->bm_flags & BW_METER_GEQ) {
2810 /*
2811 * Processing for ">=" type of bw_meter entry
2812 */
2813 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2814 /* Reset the bw_meter entry */
2815 x->bm_start_time = *nowp;
2816 x->bm_measured.b_packets = 0;
2817 x->bm_measured.b_bytes = 0;
2818 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2819 }
2820
2821 /* Record that a packet is received */
2822 x->bm_measured.b_packets++;
2823 x->bm_measured.b_bytes += plen;
2824
2825 /*
2826 * Test if we should deliver an upcall
2827 */
2828 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2829 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2830 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2831 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2832 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2833 /* Prepare an upcall for delivery */
2834 bw_meter_prepare_upcall(x, nowp);
2835 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2836 }
2837 }
2838 } else if (x->bm_flags & BW_METER_LEQ) {
2839 /*
2840 * Processing for "<=" type of bw_meter entry
2841 */
2842 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2843 /*
2844 * We are behind time with the multicast forwarding table
2845 * scanning for "<=" type of bw_meter entries, so test now
2846 * if we should deliver an upcall.
2847 */
2848 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2849 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2850 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2851 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2852 /* Prepare an upcall for delivery */
2853 bw_meter_prepare_upcall(x, nowp);
2854 }
2855 /* Reschedule the bw_meter entry */
2856 unschedule_bw_meter(x);
2857 schedule_bw_meter(x, nowp);
2858 }
2859
2860 /* Record that a packet is received */
2861 x->bm_measured.b_packets++;
2862 x->bm_measured.b_bytes += plen;
2863
2864 /*
2865 * Test if we should restart the measuring interval
2866 */
2867 if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
2868 x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
2869 (x->bm_flags & BW_METER_UNIT_BYTES &&
2870 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
2871 /* Don't restart the measuring interval */
2872 } else {
2873 /* Do restart the measuring interval */
2874 /*
2875 * XXX: note that we don't unschedule and schedule, because this
2876 * might be too much overhead per packet. Instead, when we process
2877 * all entries for a given timer hash bin, we check whether it is
2878 * really a timeout. If not, we reschedule at that time.
2879 */
2880 x->bm_start_time = *nowp;
2881 x->bm_measured.b_packets = 0;
2882 x->bm_measured.b_bytes = 0;
2883 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2884 }
2885 }
2886 }
2887
2888 /*
2889 * Prepare a bandwidth-related upcall
2890 */
2891 static void
2892 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2893 {
2894 struct timeval delta;
2895 struct bw_upcall *u;
2896
2897 /*
2898 * Compute the measured time interval
2899 */
2900 delta = *nowp;
2901 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2902
2903 /*
2904 * If there are too many pending upcalls, deliver them now
2905 */
2906 if (bw_upcalls_n >= BW_UPCALLS_MAX)
2907 bw_upcalls_send();
2908
2909 /*
2910 * Set the bw_upcall entry
2911 */
2912 u = &bw_upcalls[bw_upcalls_n++];
2913 u->bu_src = x->bm_mfc->mfc_origin;
2914 u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2915 u->bu_threshold.b_time = x->bm_threshold.b_time;
2916 u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2917 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2918 u->bu_measured.b_time = delta;
2919 u->bu_measured.b_packets = x->bm_measured.b_packets;
2920 u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2921 u->bu_flags = 0;
2922 if (x->bm_flags & BW_METER_UNIT_PACKETS)
2923 u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2924 if (x->bm_flags & BW_METER_UNIT_BYTES)
2925 u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2926 if (x->bm_flags & BW_METER_GEQ)
2927 u->bu_flags |= BW_UPCALL_GEQ;
2928 if (x->bm_flags & BW_METER_LEQ)
2929 u->bu_flags |= BW_UPCALL_LEQ;
2930 }
2931
2932 /*
2933 * Send the pending bandwidth-related upcalls
2934 */
2935 static void
2936 bw_upcalls_send(void)
2937 {
2938 struct mbuf *m;
2939 int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
2940 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2941 static struct igmpmsg igmpmsg = { 0, /* unused1 */
2942 0, /* unused2 */
2943 IGMPMSG_BW_UPCALL,/* im_msgtype */
2944 0, /* im_mbz */
2945 0, /* im_vif */
2946 0, /* unused3 */
2947 { 0 }, /* im_src */
2948 { 0 } }; /* im_dst */
2949
2950 if (bw_upcalls_n == 0)
2951 return; /* No pending upcalls */
2952
2953 bw_upcalls_n = 0;
2954
2955 /*
2956 * Allocate a new mbuf, initialize it with the header and
2957 * the payload for the pending calls.
2958 */
2959 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2960 if (m == NULL) {
2961 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2962 return;
2963 }
2964
2965 m->m_len = m->m_pkthdr.len = 0;
2966 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2967 m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
2968
2969 /*
2970 * Send the upcalls
2971 * XXX do we need to set the address in k_igmpsrc ?
2972 */
2973 mrtstat.mrts_upcalls++;
2974 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
2975 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2976 ++mrtstat.mrts_upq_sockfull;
2977 }
2978 }
2979
2980 /*
2981 * Compute the timeout hash value for the bw_meter entries
2982 */
2983 #define BW_METER_TIMEHASH(bw_meter, hash) \
2984 do { \
2985 struct timeval next_timeval = (bw_meter)->bm_start_time; \
2986 \
2987 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2988 (hash) = next_timeval.tv_sec; \
2989 if (next_timeval.tv_usec) \
2990 (hash)++; /* XXX: make sure we don't timeout early */ \
2991 (hash) %= BW_METER_BUCKETS; \
2992 } while (/*CONSTCOND*/ 0)
2993
2994 /*
2995 * Schedule a timer to process periodically bw_meter entry of type "<="
2996 * by linking the entry in the proper hash bucket.
2997 */
2998 static void
2999 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
3000 {
3001 int time_hash;
3002
3003 if (!(x->bm_flags & BW_METER_LEQ))
3004 return; /* XXX: we schedule timers only for "<=" entries */
3005
3006 /*
3007 * Reset the bw_meter entry
3008 */
3009 x->bm_start_time = *nowp;
3010 x->bm_measured.b_packets = 0;
3011 x->bm_measured.b_bytes = 0;
3012 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
3013
3014 /*
3015 * Compute the timeout hash value and insert the entry
3016 */
3017 BW_METER_TIMEHASH(x, time_hash);
3018 x->bm_time_next = bw_meter_timers[time_hash];
3019 bw_meter_timers[time_hash] = x;
3020 x->bm_time_hash = time_hash;
3021 }
3022
3023 /*
3024 * Unschedule the periodic timer that processes bw_meter entry of type "<="
3025 * by removing the entry from the proper hash bucket.
3026 */
3027 static void
3028 unschedule_bw_meter(struct bw_meter *x)
3029 {
3030 int time_hash;
3031 struct bw_meter *prev, *tmp;
3032
3033 if (!(x->bm_flags & BW_METER_LEQ))
3034 return; /* XXX: we schedule timers only for "<=" entries */
3035
3036 /*
3037 * Compute the timeout hash value and delete the entry
3038 */
3039 time_hash = x->bm_time_hash;
3040 if (time_hash >= BW_METER_BUCKETS)
3041 return; /* Entry was not scheduled */
3042
3043 for (prev = NULL, tmp = bw_meter_timers[time_hash];
3044 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
3045 if (tmp == x)
3046 break;
3047
3048 if (tmp == NULL)
3049 panic("unschedule_bw_meter: bw_meter entry not found");
3050
3051 if (prev != NULL)
3052 prev->bm_time_next = x->bm_time_next;
3053 else
3054 bw_meter_timers[time_hash] = x->bm_time_next;
3055
3056 x->bm_time_next = NULL;
3057 x->bm_time_hash = BW_METER_BUCKETS;
3058 }
3059
3060 /*
3061 * Process all "<=" type of bw_meter that should be processed now,
3062 * and for each entry prepare an upcall if necessary. Each processed
3063 * entry is rescheduled again for the (periodic) processing.
3064 *
3065 * This is run periodically (once per second normally). On each round,
3066 * all the potentially matching entries are in the hash slot that we are
3067 * looking at.
3068 */
3069 static void
3070 bw_meter_process()
3071 {
3072 int s;
3073 static uint32_t last_tv_sec; /* last time we processed this */
3074
3075 uint32_t loops;
3076 int i;
3077 struct timeval now, process_endtime;
3078
3079 microtime(&now);
3080 if (last_tv_sec == now.tv_sec)
3081 return; /* nothing to do */
3082
3083 loops = now.tv_sec - last_tv_sec;
3084 last_tv_sec = now.tv_sec;
3085 if (loops > BW_METER_BUCKETS)
3086 loops = BW_METER_BUCKETS;
3087
3088 s = splsoftnet();
3089 /*
3090 * Process all bins of bw_meter entries from the one after the last
3091 * processed to the current one. On entry, i points to the last bucket
3092 * visited, so we need to increment i at the beginning of the loop.
3093 */
3094 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
3095 struct bw_meter *x, *tmp_list;
3096
3097 if (++i >= BW_METER_BUCKETS)
3098 i = 0;
3099
3100 /* Disconnect the list of bw_meter entries from the bin */
3101 tmp_list = bw_meter_timers[i];
3102 bw_meter_timers[i] = NULL;
3103
3104 /* Process the list of bw_meter entries */
3105 while (tmp_list != NULL) {
3106 x = tmp_list;
3107 tmp_list = tmp_list->bm_time_next;
3108
3109 /* Test if the time interval is over */
3110 process_endtime = x->bm_start_time;
3111 BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
3112 if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
3113 /* Not yet: reschedule, but don't reset */
3114 int time_hash;
3115
3116 BW_METER_TIMEHASH(x, time_hash);
3117 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
3118 /*
3119 * XXX: somehow the bin processing is a bit ahead of time.
3120 * Put the entry in the next bin.
3121 */
3122 if (++time_hash >= BW_METER_BUCKETS)
3123 time_hash = 0;
3124 }
3125 x->bm_time_next = bw_meter_timers[time_hash];
3126 bw_meter_timers[time_hash] = x;
3127 x->bm_time_hash = time_hash;
3128
3129 continue;
3130 }
3131
3132 /*
3133 * Test if we should deliver an upcall
3134 */
3135 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
3136 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
3137 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
3138 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
3139 /* Prepare an upcall for delivery */
3140 bw_meter_prepare_upcall(x, &now);
3141 }
3142
3143 /*
3144 * Reschedule for next processing
3145 */
3146 schedule_bw_meter(x, &now);
3147 }
3148 }
3149
3150 /* Send all upcalls that are pending delivery */
3151 bw_upcalls_send();
3152
3153 splx(s);
3154 }
3155
3156 /*
3157 * A periodic function for sending all upcalls that are pending delivery
3158 */
3159 static void
3160 expire_bw_upcalls_send(void *unused)
3161 {
3162 int s;
3163
3164 s = splsoftnet();
3165 bw_upcalls_send();
3166 splx(s);
3167
3168 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
3169 expire_bw_upcalls_send, NULL);
3170 }
3171
3172 /*
3173 * A periodic function for periodic scanning of the multicast forwarding
3174 * table for processing all "<=" bw_meter entries.
3175 */
3176 static void
3177 expire_bw_meter_process(void *unused)
3178 {
3179 if (mrt_api_config & MRT_MFC_BW_UPCALL)
3180 bw_meter_process();
3181
3182 callout_reset(&bw_meter_ch, BW_METER_PERIOD,
3183 expire_bw_meter_process, NULL);
3184 }
3185
3186 /*
3187 * End of bandwidth monitoring code
3188 */
3189
3190 #ifdef PIM
3191 /*
3192 * Send the packet up to the user daemon, or eventually do kernel encapsulation
3193 */
3194 static int
3195 pim_register_send(struct ip *ip, struct vif *vifp,
3196 struct mbuf *m, struct mfc *rt)
3197 {
3198 struct mbuf *mb_copy, *mm;
3199
3200 if (mrtdebug & DEBUG_PIM)
3201 log(LOG_DEBUG, "pim_register_send: ");
3202
3203 mb_copy = pim_register_prepare(ip, m);
3204 if (mb_copy == NULL)
3205 return ENOBUFS;
3206
3207 /*
3208 * Send all the fragments. Note that the mbuf for each fragment
3209 * is freed by the sending machinery.
3210 */
3211 for (mm = mb_copy; mm; mm = mb_copy) {
3212 mb_copy = mm->m_nextpkt;
3213 mm->m_nextpkt = NULL;
3214 mm = m_pullup(mm, sizeof(struct ip));
3215 if (mm != NULL) {
3216 ip = mtod(mm, struct ip *);
3217 if ((mrt_api_config & MRT_MFC_RP) &&
3218 !in_nullhost(rt->mfc_rp)) {
3219 pim_register_send_rp(ip, vifp, mm, rt);
3220 } else {
3221 pim_register_send_upcall(ip, vifp, mm, rt);
3222 }
3223 }
3224 }
3225
3226 return 0;
3227 }
3228
3229 /*
3230 * Return a copy of the data packet that is ready for PIM Register
3231 * encapsulation.
3232 * XXX: Note that in the returned copy the IP header is a valid one.
3233 */
3234 static struct mbuf *
3235 pim_register_prepare(struct ip *ip, struct mbuf *m)
3236 {
3237 struct mbuf *mb_copy = NULL;
3238 int mtu;
3239
3240 /* Take care of delayed checksums */
3241 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
3242 in_delayed_cksum(m);
3243 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
3244 }
3245
3246 /*
3247 * Copy the old packet & pullup its IP header into the
3248 * new mbuf so we can modify it.
3249 */
3250 mb_copy = m_copy(m, 0, M_COPYALL);
3251 if (mb_copy == NULL)
3252 return NULL;
3253 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
3254 if (mb_copy == NULL)
3255 return NULL;
3256
3257 /* take care of the TTL */
3258 ip = mtod(mb_copy, struct ip *);
3259 --ip->ip_ttl;
3260
3261 /* Compute the MTU after the PIM Register encapsulation */
3262 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
3263
3264 if (ntohs(ip->ip_len) <= mtu) {
3265 /* Turn the IP header into a valid one */
3266 ip->ip_sum = 0;
3267 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
3268 } else {
3269 /* Fragment the packet */
3270 if (ip_fragment(mb_copy, NULL, mtu) != 0) {
3271 /* XXX: mb_copy was freed by ip_fragment() */
3272 return NULL;
3273 }
3274 }
3275 return mb_copy;
3276 }
3277
3278 /*
3279 * Send an upcall with the data packet to the user-level process.
3280 */
3281 static int
3282 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
3283 struct mbuf *mb_copy, struct mfc *rt)
3284 {
3285 struct mbuf *mb_first;
3286 int len = ntohs(ip->ip_len);
3287 struct igmpmsg *im;
3288 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
3289
3290 /*
3291 * Add a new mbuf with an upcall header
3292 */
3293 MGETHDR(mb_first, M_DONTWAIT, MT_HEADER);
3294 if (mb_first == NULL) {
3295 m_freem(mb_copy);
3296 return ENOBUFS;
3297 }
3298 mb_first->m_data += max_linkhdr;
3299 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
3300 mb_first->m_len = sizeof(struct igmpmsg);
3301 mb_first->m_next = mb_copy;
3302
3303 /* Send message to routing daemon */
3304 im = mtod(mb_first, struct igmpmsg *);
3305 im->im_msgtype = IGMPMSG_WHOLEPKT;
3306 im->im_mbz = 0;
3307 im->im_vif = vifp - viftable;
3308 im->im_src = ip->ip_src;
3309 im->im_dst = ip->ip_dst;
3310
3311 k_igmpsrc.sin_addr = ip->ip_src;
3312
3313 mrtstat.mrts_upcalls++;
3314
3315 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
3316 if (mrtdebug & DEBUG_PIM)
3317 log(LOG_WARNING,
3318 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
3319 ++mrtstat.mrts_upq_sockfull;
3320 return ENOBUFS;
3321 }
3322
3323 /* Keep statistics */
3324 pimstat.pims_snd_registers_msgs++;
3325 pimstat.pims_snd_registers_bytes += len;
3326
3327 return 0;
3328 }
3329
3330 /*
3331 * Encapsulate the data packet in PIM Register message and send it to the RP.
3332 */
3333 static int
3334 pim_register_send_rp(struct ip *ip, struct vif *vifp,
3335 struct mbuf *mb_copy, struct mfc *rt)
3336 {
3337 struct mbuf *mb_first;
3338 struct ip *ip_outer;
3339 struct pim_encap_pimhdr *pimhdr;
3340 int len = ntohs(ip->ip_len);
3341 vifi_t vifi = rt->mfc_parent;
3342
3343 if ((vifi >= numvifs) || in_nullhost(viftable[vifi].v_lcl_addr)) {
3344 m_freem(mb_copy);
3345 return EADDRNOTAVAIL; /* The iif vif is invalid */
3346 }
3347
3348 /*
3349 * Add a new mbuf with the encapsulating header
3350 */
3351 MGETHDR(mb_first, M_DONTWAIT, MT_HEADER);
3352 if (mb_first == NULL) {
3353 m_freem(mb_copy);
3354 return ENOBUFS;
3355 }
3356 mb_first->m_data += max_linkhdr;
3357 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
3358 mb_first->m_next = mb_copy;
3359
3360 mb_first->m_pkthdr.len = len + mb_first->m_len;
3361
3362 /*
3363 * Fill in the encapsulating IP and PIM header
3364 */
3365 ip_outer = mtod(mb_first, struct ip *);
3366 *ip_outer = pim_encap_iphdr;
3367 ip_outer->ip_id = ip_newid();
3368 ip_outer->ip_len = htons(len + sizeof(pim_encap_iphdr) +
3369 sizeof(pim_encap_pimhdr));
3370 ip_outer->ip_src = viftable[vifi].v_lcl_addr;
3371 ip_outer->ip_dst = rt->mfc_rp;
3372 /*
3373 * Copy the inner header TOS to the outer header, and take care of the
3374 * IP_DF bit.
3375 */
3376 ip_outer->ip_tos = ip->ip_tos;
3377 if (ntohs(ip->ip_off) & IP_DF)
3378 ip_outer->ip_off |= IP_DF;
3379 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
3380 + sizeof(pim_encap_iphdr));
3381 *pimhdr = pim_encap_pimhdr;
3382 /* If the iif crosses a border, set the Border-bit */
3383 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
3384 pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
3385
3386 mb_first->m_data += sizeof(pim_encap_iphdr);
3387 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
3388 mb_first->m_data -= sizeof(pim_encap_iphdr);
3389
3390 if (vifp->v_rate_limit == 0)
3391 tbf_send_packet(vifp, mb_first);
3392 else
3393 tbf_control(vifp, mb_first, ip, ntohs(ip_outer->ip_len));
3394
3395 /* Keep statistics */
3396 pimstat.pims_snd_registers_msgs++;
3397 pimstat.pims_snd_registers_bytes += len;
3398
3399 return 0;
3400 }
3401
3402 /*
3403 * PIM-SMv2 and PIM-DM messages processing.
3404 * Receives and verifies the PIM control messages, and passes them
3405 * up to the listening socket, using rip_input().
3406 * The only message with special processing is the PIM_REGISTER message
3407 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
3408 * is passed to if_simloop().
3409 */
3410 void
3411 pim_input(struct mbuf *m, ...)
3412 {
3413 struct ip *ip = mtod(m, struct ip *);
3414 struct pim *pim;
3415 int minlen;
3416 int datalen;
3417 int ip_tos;
3418 int proto;
3419 int iphlen;
3420 va_list ap;
3421
3422 va_start(ap, m);
3423 iphlen = va_arg(ap, int);
3424 proto = va_arg(ap, int);
3425 va_end(ap);
3426
3427 datalen = ntohs(ip->ip_len) - iphlen;
3428
3429 /* Keep statistics */
3430 pimstat.pims_rcv_total_msgs++;
3431 pimstat.pims_rcv_total_bytes += datalen;
3432
3433 /*
3434 * Validate lengths
3435 */
3436 if (datalen < PIM_MINLEN) {
3437 pimstat.pims_rcv_tooshort++;
3438 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
3439 datalen, (u_long)ip->ip_src.s_addr);
3440 m_freem(m);
3441 return;
3442 }
3443
3444 /*
3445 * If the packet is at least as big as a REGISTER, go agead
3446 * and grab the PIM REGISTER header size, to avoid another
3447 * possible m_pullup() later.
3448 *
3449 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
3450 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
3451 */
3452 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
3453 /*
3454 * Get the IP and PIM headers in contiguous memory, and
3455 * possibly the PIM REGISTER header.
3456 */
3457 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
3458 (m = m_pullup(m, minlen)) == NULL) {
3459 log(LOG_ERR, "pim_input: m_pullup failure\n");
3460 return;
3461 }
3462 /* m_pullup() may have given us a new mbuf so reset ip. */
3463 ip = mtod(m, struct ip *);
3464 ip_tos = ip->ip_tos;
3465
3466 /* adjust mbuf to point to the PIM header */
3467 m->m_data += iphlen;
3468 m->m_len -= iphlen;
3469 pim = mtod(m, struct pim *);
3470
3471 /*
3472 * Validate checksum. If PIM REGISTER, exclude the data packet.
3473 *
3474 * XXX: some older PIMv2 implementations don't make this distinction,
3475 * so for compatibility reason perform the checksum over part of the
3476 * message, and if error, then over the whole message.
3477 */
3478 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
3479 /* do nothing, checksum okay */
3480 } else if (in_cksum(m, datalen)) {
3481 pimstat.pims_rcv_badsum++;
3482 if (mrtdebug & DEBUG_PIM)
3483 log(LOG_DEBUG, "pim_input: invalid checksum");
3484 m_freem(m);
3485 return;
3486 }
3487
3488 /* PIM version check */
3489 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
3490 pimstat.pims_rcv_badversion++;
3491 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
3492 PIM_VT_V(pim->pim_vt), PIM_VERSION);
3493 m_freem(m);
3494 return;
3495 }
3496
3497 /* restore mbuf back to the outer IP */
3498 m->m_data -= iphlen;
3499 m->m_len += iphlen;
3500
3501 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
3502 /*
3503 * Since this is a REGISTER, we'll make a copy of the register
3504 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
3505 * routing daemon.
3506 */
3507 int s;
3508 struct sockaddr_in dst = { sizeof(dst), AF_INET };
3509 struct mbuf *mcp;
3510 struct ip *encap_ip;
3511 u_int32_t *reghdr;
3512 struct ifnet *vifp;
3513
3514 s = splsoftnet();
3515 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
3516 splx(s);
3517 if (mrtdebug & DEBUG_PIM)
3518 log(LOG_DEBUG,
3519 "pim_input: register vif not set: %d\n", reg_vif_num);
3520 m_freem(m);
3521 return;
3522 }
3523 /* XXX need refcnt? */
3524 vifp = viftable[reg_vif_num].v_ifp;
3525 splx(s);
3526
3527 /*
3528 * Validate length
3529 */
3530 if (datalen < PIM_REG_MINLEN) {
3531 pimstat.pims_rcv_tooshort++;
3532 pimstat.pims_rcv_badregisters++;
3533 log(LOG_ERR,
3534 "pim_input: register packet size too small %d from %lx\n",
3535 datalen, (u_long)ip->ip_src.s_addr);
3536 m_freem(m);
3537 return;
3538 }
3539
3540 reghdr = (u_int32_t *)(pim + 1);
3541 encap_ip = (struct ip *)(reghdr + 1);
3542
3543 if (mrtdebug & DEBUG_PIM) {
3544 log(LOG_DEBUG,
3545 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
3546 (u_long)ntohl(encap_ip->ip_src.s_addr),
3547 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3548 ntohs(encap_ip->ip_len));
3549 }
3550
3551 /* verify the version number of the inner packet */
3552 if (encap_ip->ip_v != IPVERSION) {
3553 pimstat.pims_rcv_badregisters++;
3554 if (mrtdebug & DEBUG_PIM) {
3555 log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
3556 "of the inner packet\n", encap_ip->ip_v);
3557 }
3558 m_freem(m);
3559 return;
3560 }
3561
3562 /* verify the inner packet is destined to a mcast group */
3563 if (!IN_MULTICAST(encap_ip->ip_dst.s_addr)) {
3564 pimstat.pims_rcv_badregisters++;
3565 if (mrtdebug & DEBUG_PIM)
3566 log(LOG_DEBUG,
3567 "pim_input: inner packet of register is not "
3568 "multicast %lx\n",
3569 (u_long)ntohl(encap_ip->ip_dst.s_addr));
3570 m_freem(m);
3571 return;
3572 }
3573
3574 /* If a NULL_REGISTER, pass it to the daemon */
3575 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
3576 goto pim_input_to_daemon;
3577
3578 /*
3579 * Copy the TOS from the outer IP header to the inner IP header.
3580 */
3581 if (encap_ip->ip_tos != ip_tos) {
3582 /* Outer TOS -> inner TOS */
3583 encap_ip->ip_tos = ip_tos;
3584 /* Recompute the inner header checksum. Sigh... */
3585
3586 /* adjust mbuf to point to the inner IP header */
3587 m->m_data += (iphlen + PIM_MINLEN);
3588 m->m_len -= (iphlen + PIM_MINLEN);
3589
3590 encap_ip->ip_sum = 0;
3591 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
3592
3593 /* restore mbuf to point back to the outer IP header */
3594 m->m_data -= (iphlen + PIM_MINLEN);
3595 m->m_len += (iphlen + PIM_MINLEN);
3596 }
3597
3598 /*
3599 * Decapsulate the inner IP packet and loopback to forward it
3600 * as a normal multicast packet. Also, make a copy of the
3601 * outer_iphdr + pimhdr + reghdr + encap_iphdr
3602 * to pass to the daemon later, so it can take the appropriate
3603 * actions (e.g., send back PIM_REGISTER_STOP).
3604 * XXX: here m->m_data points to the outer IP header.
3605 */
3606 mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
3607 if (mcp == NULL) {
3608 log(LOG_ERR,
3609 "pim_input: pim register: could not copy register head\n");
3610 m_freem(m);
3611 return;
3612 }
3613
3614 /* Keep statistics */
3615 /* XXX: registers_bytes include only the encap. mcast pkt */
3616 pimstat.pims_rcv_registers_msgs++;
3617 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
3618
3619 /*
3620 * forward the inner ip packet; point m_data at the inner ip.
3621 */
3622 m_adj(m, iphlen + PIM_MINLEN);
3623
3624 if (mrtdebug & DEBUG_PIM) {
3625 log(LOG_DEBUG,
3626 "pim_input: forwarding decapsulated register: "
3627 "src %lx, dst %lx, vif %d\n",
3628 (u_long)ntohl(encap_ip->ip_src.s_addr),
3629 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3630 reg_vif_num);
3631 }
3632 /* NB: vifp was collected above; can it change on us? */
3633 looutput(vifp, m, (struct sockaddr *)&dst, (struct rtentry *)NULL);
3634
3635 /* prepare the register head to send to the mrouting daemon */
3636 m = mcp;
3637 }
3638
3639 pim_input_to_daemon:
3640 /*
3641 * Pass the PIM message up to the daemon; if it is a Register message,
3642 * pass the 'head' only up to the daemon. This includes the
3643 * outer IP header, PIM header, PIM-Register header and the
3644 * inner IP header.
3645 * XXX: the outer IP header pkt size of a Register is not adjust to
3646 * reflect the fact that the inner multicast data is truncated.
3647 */
3648 rip_input(m, iphlen, proto);
3649
3650 return;
3651 }
3652 #endif /* PIM */
3653