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