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