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