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