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