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.2/sys/netinet/ip_mroute.c 163072 2006-10-07 10:45:05Z 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 return (ENOMEM);
749
750 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
751
752 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
753 expire_bw_upcalls_send, NULL);
754 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
755
756 ip_mrouter = so;
757
758 mtx_unlock(&mrouter_mtx);
759
760 if (mrtdebug)
761 log(LOG_DEBUG, "ip_mrouter_init\n");
762
763 return 0;
764 }
765
766 /*
767 * Disable multicast routing
768 */
769 static int
770 X_ip_mrouter_done(void)
771 {
772 vifi_t vifi;
773 int i;
774 struct ifnet *ifp;
775 struct ifreq ifr;
776 struct mfc *rt;
777 struct rtdetq *rte;
778
779 mtx_lock(&mrouter_mtx);
780
781 if (ip_mrouter == NULL) {
782 mtx_unlock(&mrouter_mtx);
783 return EINVAL;
784 }
785
786 /*
787 * Detach/disable hooks to the reset of the system.
788 */
789 ip_mrouter = NULL;
790 mrt_api_config = 0;
791
792 VIF_LOCK();
793 if (encap_cookie) {
794 const struct encaptab *c = encap_cookie;
795 encap_cookie = NULL;
796 encap_detach(c);
797 }
798 VIF_UNLOCK();
799
800 callout_stop(&tbf_reprocess_ch);
801
802 VIF_LOCK();
803 /*
804 * For each phyint in use, disable promiscuous reception of all IP
805 * multicasts.
806 */
807 for (vifi = 0; vifi < numvifs; vifi++) {
808 if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
809 !(viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) {
810 struct sockaddr_in *so = (struct sockaddr_in *)&(ifr.ifr_addr);
811
812 so->sin_len = sizeof(struct sockaddr_in);
813 so->sin_family = AF_INET;
814 so->sin_addr.s_addr = INADDR_ANY;
815 ifp = viftable[vifi].v_ifp;
816 if_allmulti(ifp, 0);
817 }
818 }
819 bzero((caddr_t)tbftable, sizeof(tbftable));
820 bzero((caddr_t)viftable, sizeof(viftable));
821 numvifs = 0;
822 pim_assert = 0;
823 VIF_UNLOCK();
824 EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
825
826 /*
827 * Free all multicast forwarding cache entries.
828 */
829 callout_stop(&expire_upcalls_ch);
830 callout_stop(&bw_upcalls_ch);
831 callout_stop(&bw_meter_ch);
832
833 MFC_LOCK();
834 for (i = 0; i < MFCTBLSIZ; i++) {
835 for (rt = mfctable[i]; rt != NULL; ) {
836 struct mfc *nr = rt->mfc_next;
837
838 for (rte = rt->mfc_stall; rte != NULL; ) {
839 struct rtdetq *n = rte->next;
840
841 m_freem(rte->m);
842 free(rte, M_MRTABLE);
843 rte = n;
844 }
845 free_bw_list(rt->mfc_bw_meter);
846 free(rt, M_MRTABLE);
847 rt = nr;
848 }
849 }
850 bzero((caddr_t)mfctable, sizeof(mfctable));
851 bzero((caddr_t)nexpire, sizeof(nexpire));
852 bw_upcalls_n = 0;
853 bzero(bw_meter_timers, sizeof(bw_meter_timers));
854 MFC_UNLOCK();
855
856 /*
857 * Reset de-encapsulation cache
858 */
859 last_encap_src = INADDR_ANY;
860 last_encap_vif = NULL;
861 #ifdef PIM
862 reg_vif_num = VIFI_INVALID;
863 #endif
864
865 mtx_unlock(&mrouter_mtx);
866
867 if (mrtdebug)
868 log(LOG_DEBUG, "ip_mrouter_done\n");
869
870 return 0;
871 }
872
873 /*
874 * Set PIM assert processing global
875 */
876 static int
877 set_assert(int i)
878 {
879 if ((i != 1) && (i != 0))
880 return EINVAL;
881
882 pim_assert = i;
883
884 return 0;
885 }
886
887 /*
888 * Configure API capabilities
889 */
890 int
891 set_api_config(uint32_t *apival)
892 {
893 int i;
894
895 /*
896 * We can set the API capabilities only if it is the first operation
897 * after MRT_INIT. I.e.:
898 * - there are no vifs installed
899 * - pim_assert is not enabled
900 * - the MFC table is empty
901 */
902 if (numvifs > 0) {
903 *apival = 0;
904 return EPERM;
905 }
906 if (pim_assert) {
907 *apival = 0;
908 return EPERM;
909 }
910 for (i = 0; i < MFCTBLSIZ; i++) {
911 if (mfctable[i] != NULL) {
912 *apival = 0;
913 return EPERM;
914 }
915 }
916
917 mrt_api_config = *apival & mrt_api_support;
918 *apival = mrt_api_config;
919
920 return 0;
921 }
922
923 /*
924 * Decide if a packet is from a tunnelled peer.
925 * Return 0 if not, 64 if so. XXX yuck.. 64 ???
926 */
927 static int
928 mroute_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
929 {
930 struct ip *ip = mtod(m, struct ip *);
931 int hlen = ip->ip_hl << 2;
932
933 /*
934 * don't claim the packet if it's not to a multicast destination or if
935 * we don't have an encapsulating tunnel with the source.
936 * Note: This code assumes that the remote site IP address
937 * uniquely identifies the tunnel (i.e., that this site has
938 * at most one tunnel with the remote site).
939 */
940 if (!IN_MULTICAST(ntohl(((struct ip *)((char *)ip+hlen))->ip_dst.s_addr)))
941 return 0;
942 if (ip->ip_src.s_addr != last_encap_src) {
943 struct vif *vifp = viftable;
944 struct vif *vife = vifp + numvifs;
945
946 last_encap_src = ip->ip_src.s_addr;
947 last_encap_vif = NULL;
948 for ( ; vifp < vife; ++vifp)
949 if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
950 if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT)) == VIFF_TUNNEL)
951 last_encap_vif = vifp;
952 break;
953 }
954 }
955 if (last_encap_vif == NULL) {
956 last_encap_src = INADDR_ANY;
957 return 0;
958 }
959 return 64;
960 }
961
962 /*
963 * De-encapsulate a packet and feed it back through ip input (this
964 * routine is called whenever IP gets a packet that mroute_encap_func()
965 * claimed).
966 */
967 static void
968 mroute_encap_input(struct mbuf *m, int off)
969 {
970 struct ip *ip = mtod(m, struct ip *);
971 int hlen = ip->ip_hl << 2;
972
973 if (hlen > sizeof(struct ip))
974 ip_stripoptions(m, (struct mbuf *) 0);
975 m->m_data += sizeof(struct ip);
976 m->m_len -= sizeof(struct ip);
977 m->m_pkthdr.len -= sizeof(struct ip);
978
979 m->m_pkthdr.rcvif = last_encap_vif->v_ifp;
980
981 netisr_queue(NETISR_IP, m); /* mbuf is free'd on failure. */
982 /*
983 * normally we would need a "schednetisr(NETISR_IP)"
984 * here but we were called by ip_input and it is going
985 * to loop back & try to dequeue the packet we just
986 * queued as soon as we return so we avoid the
987 * unnecessary software interrrupt.
988 *
989 * XXX
990 * This no longer holds - we may have direct-dispatched the packet,
991 * or there may be a queue processing limit.
992 */
993 }
994
995 extern struct domain inetdomain;
996 static struct protosw mroute_encap_protosw =
997 {
998 .pr_type = SOCK_RAW,
999 .pr_domain = &inetdomain,
1000 .pr_protocol = IPPROTO_IPV4,
1001 .pr_flags = PR_ATOMIC|PR_ADDR,
1002 .pr_input = mroute_encap_input,
1003 .pr_ctloutput = rip_ctloutput,
1004 .pr_usrreqs = &rip_usrreqs
1005 };
1006
1007 /*
1008 * Add a vif to the vif table
1009 */
1010 static int
1011 add_vif(struct vifctl *vifcp)
1012 {
1013 struct vif *vifp = viftable + vifcp->vifc_vifi;
1014 struct sockaddr_in sin = {sizeof sin, AF_INET};
1015 struct ifaddr *ifa;
1016 struct ifnet *ifp;
1017 int error;
1018 struct tbf *v_tbf = tbftable + vifcp->vifc_vifi;
1019
1020 VIF_LOCK();
1021 if (vifcp->vifc_vifi >= MAXVIFS) {
1022 VIF_UNLOCK();
1023 return EINVAL;
1024 }
1025 if (vifp->v_lcl_addr.s_addr != INADDR_ANY) {
1026 VIF_UNLOCK();
1027 return EADDRINUSE;
1028 }
1029 if (vifcp->vifc_lcl_addr.s_addr == INADDR_ANY) {
1030 VIF_UNLOCK();
1031 return EADDRNOTAVAIL;
1032 }
1033
1034 /* Find the interface with an address in AF_INET family */
1035 #ifdef PIM
1036 if (vifcp->vifc_flags & VIFF_REGISTER) {
1037 /*
1038 * XXX: Because VIFF_REGISTER does not really need a valid
1039 * local interface (e.g. it could be 127.0.0.2), we don't
1040 * check its address.
1041 */
1042 ifp = NULL;
1043 } else
1044 #endif
1045 {
1046 sin.sin_addr = vifcp->vifc_lcl_addr;
1047 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
1048 if (ifa == NULL) {
1049 VIF_UNLOCK();
1050 return EADDRNOTAVAIL;
1051 }
1052 ifp = ifa->ifa_ifp;
1053 }
1054
1055 if (vifcp->vifc_flags & VIFF_TUNNEL) {
1056 if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
1057 /*
1058 * An encapsulating tunnel is wanted. Tell
1059 * mroute_encap_input() to start paying attention
1060 * to encapsulated packets.
1061 */
1062 if (encap_cookie == NULL) {
1063 int i;
1064
1065 encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV4,
1066 mroute_encapcheck,
1067 (struct protosw *)&mroute_encap_protosw, NULL);
1068
1069 if (encap_cookie == NULL) {
1070 printf("ip_mroute: unable to attach encap\n");
1071 VIF_UNLOCK();
1072 return EIO; /* XXX */
1073 }
1074 for (i = 0; i < MAXVIFS; ++i) {
1075 if_initname(&multicast_decap_if[i], "mdecap", i);
1076 }
1077 }
1078 /*
1079 * Set interface to fake encapsulator interface
1080 */
1081 ifp = &multicast_decap_if[vifcp->vifc_vifi];
1082 /*
1083 * Prepare cached route entry
1084 */
1085 bzero(&vifp->v_route, sizeof(vifp->v_route));
1086 } else {
1087 log(LOG_ERR, "source routed tunnels not supported\n");
1088 VIF_UNLOCK();
1089 return EOPNOTSUPP;
1090 }
1091 #ifdef PIM
1092 } else if (vifcp->vifc_flags & VIFF_REGISTER) {
1093 ifp = &multicast_register_if;
1094 if (mrtdebug)
1095 log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
1096 (void *)&multicast_register_if);
1097 if (reg_vif_num == VIFI_INVALID) {
1098 if_initname(&multicast_register_if, "register_vif", 0);
1099 multicast_register_if.if_flags = IFF_LOOPBACK;
1100 bzero(&vifp->v_route, sizeof(vifp->v_route));
1101 reg_vif_num = vifcp->vifc_vifi;
1102 }
1103 #endif
1104 } else { /* Make sure the interface supports multicast */
1105 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1106 VIF_UNLOCK();
1107 return EOPNOTSUPP;
1108 }
1109
1110 /* Enable promiscuous reception of all IP multicasts from the if */
1111 error = if_allmulti(ifp, 1);
1112 if (error) {
1113 VIF_UNLOCK();
1114 return error;
1115 }
1116 }
1117
1118 /* define parameters for the tbf structure */
1119 vifp->v_tbf = v_tbf;
1120 GET_TIME(vifp->v_tbf->tbf_last_pkt_t);
1121 vifp->v_tbf->tbf_n_tok = 0;
1122 vifp->v_tbf->tbf_q_len = 0;
1123 vifp->v_tbf->tbf_max_q_len = MAXQSIZE;
1124 vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL;
1125
1126 vifp->v_flags = vifcp->vifc_flags;
1127 vifp->v_threshold = vifcp->vifc_threshold;
1128 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
1129 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
1130 vifp->v_ifp = ifp;
1131 /* scaling up here allows division by 1024 in critical code */
1132 vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000;
1133 vifp->v_rsvp_on = 0;
1134 vifp->v_rsvpd = NULL;
1135 /* initialize per vif pkt counters */
1136 vifp->v_pkt_in = 0;
1137 vifp->v_pkt_out = 0;
1138 vifp->v_bytes_in = 0;
1139 vifp->v_bytes_out = 0;
1140
1141 /* Adjust numvifs up if the vifi is higher than numvifs */
1142 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
1143
1144 VIF_UNLOCK();
1145
1146 if (mrtdebug)
1147 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
1148 vifcp->vifc_vifi,
1149 (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
1150 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
1151 (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
1152 vifcp->vifc_threshold,
1153 vifcp->vifc_rate_limit);
1154
1155 return 0;
1156 }
1157
1158 /*
1159 * Delete a vif from the vif table
1160 */
1161 static int
1162 del_vif_locked(vifi_t vifi)
1163 {
1164 struct vif *vifp;
1165
1166 VIF_LOCK_ASSERT();
1167
1168 if (vifi >= numvifs) {
1169 return EINVAL;
1170 }
1171 vifp = &viftable[vifi];
1172 if (vifp->v_lcl_addr.s_addr == INADDR_ANY) {
1173 return EADDRNOTAVAIL;
1174 }
1175
1176 if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
1177 if_allmulti(vifp->v_ifp, 0);
1178
1179 if (vifp == last_encap_vif) {
1180 last_encap_vif = NULL;
1181 last_encap_src = INADDR_ANY;
1182 }
1183
1184 /*
1185 * Free packets queued at the interface
1186 */
1187 while (vifp->v_tbf->tbf_q) {
1188 struct mbuf *m = vifp->v_tbf->tbf_q;
1189
1190 vifp->v_tbf->tbf_q = m->m_act;
1191 m_freem(m);
1192 }
1193
1194 #ifdef PIM
1195 if (vifp->v_flags & VIFF_REGISTER)
1196 reg_vif_num = VIFI_INVALID;
1197 #endif
1198
1199 bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf)));
1200 bzero((caddr_t)vifp, sizeof (*vifp));
1201
1202 if (mrtdebug)
1203 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
1204
1205 /* Adjust numvifs down */
1206 for (vifi = numvifs; vifi > 0; vifi--)
1207 if (viftable[vifi-1].v_lcl_addr.s_addr != INADDR_ANY)
1208 break;
1209 numvifs = vifi;
1210
1211 return 0;
1212 }
1213
1214 static int
1215 del_vif(vifi_t vifi)
1216 {
1217 int cc;
1218
1219 VIF_LOCK();
1220 cc = del_vif_locked(vifi);
1221 VIF_UNLOCK();
1222
1223 return cc;
1224 }
1225
1226 /*
1227 * update an mfc entry without resetting counters and S,G addresses.
1228 */
1229 static void
1230 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1231 {
1232 int i;
1233
1234 rt->mfc_parent = mfccp->mfcc_parent;
1235 for (i = 0; i < numvifs; i++) {
1236 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
1237 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
1238 MRT_MFC_FLAGS_ALL;
1239 }
1240 /* set the RP address */
1241 if (mrt_api_config & MRT_MFC_RP)
1242 rt->mfc_rp = mfccp->mfcc_rp;
1243 else
1244 rt->mfc_rp.s_addr = INADDR_ANY;
1245 }
1246
1247 /*
1248 * fully initialize an mfc entry from the parameter.
1249 */
1250 static void
1251 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
1252 {
1253 rt->mfc_origin = mfccp->mfcc_origin;
1254 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
1255
1256 update_mfc_params(rt, mfccp);
1257
1258 /* initialize pkt counters per src-grp */
1259 rt->mfc_pkt_cnt = 0;
1260 rt->mfc_byte_cnt = 0;
1261 rt->mfc_wrong_if = 0;
1262 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
1263 }
1264
1265
1266 /*
1267 * Add an mfc entry
1268 */
1269 static int
1270 add_mfc(struct mfcctl2 *mfccp)
1271 {
1272 struct mfc *rt;
1273 u_long hash;
1274 struct rtdetq *rte;
1275 u_short nstl;
1276
1277 VIF_LOCK();
1278 MFC_LOCK();
1279
1280 rt = mfc_find(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1281
1282 /* If an entry already exists, just update the fields */
1283 if (rt) {
1284 if (mrtdebug & DEBUG_MFC)
1285 log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
1286 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1287 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1288 mfccp->mfcc_parent);
1289
1290 update_mfc_params(rt, mfccp);
1291 MFC_UNLOCK();
1292 VIF_UNLOCK();
1293 return 0;
1294 }
1295
1296 /*
1297 * Find the entry for which the upcall was made and update
1298 */
1299 hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
1300 for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
1301
1302 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1303 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
1304 (rt->mfc_stall != NULL)) {
1305
1306 if (nstl++)
1307 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
1308 "multiple kernel entries",
1309 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1310 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1311 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1312
1313 if (mrtdebug & DEBUG_MFC)
1314 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
1315 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1316 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1317 mfccp->mfcc_parent, (void *)rt->mfc_stall);
1318
1319 init_mfc_params(rt, mfccp);
1320
1321 rt->mfc_expire = 0; /* Don't clean this guy up */
1322 nexpire[hash]--;
1323
1324 /* free packets Qed at the end of this entry */
1325 for (rte = rt->mfc_stall; rte != NULL; ) {
1326 struct rtdetq *n = rte->next;
1327
1328 ip_mdq(rte->m, rte->ifp, rt, -1);
1329 m_freem(rte->m);
1330 free(rte, M_MRTABLE);
1331 rte = n;
1332 }
1333 rt->mfc_stall = NULL;
1334 }
1335 }
1336
1337 /*
1338 * It is possible that an entry is being inserted without an upcall
1339 */
1340 if (nstl == 0) {
1341 if (mrtdebug & DEBUG_MFC)
1342 log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
1343 hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1344 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1345 mfccp->mfcc_parent);
1346
1347 for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
1348 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1349 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
1350 init_mfc_params(rt, mfccp);
1351 if (rt->mfc_expire)
1352 nexpire[hash]--;
1353 rt->mfc_expire = 0;
1354 break; /* XXX */
1355 }
1356 }
1357 if (rt == NULL) { /* no upcall, so make a new entry */
1358 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1359 if (rt == NULL) {
1360 MFC_UNLOCK();
1361 VIF_UNLOCK();
1362 return ENOBUFS;
1363 }
1364
1365 init_mfc_params(rt, mfccp);
1366 rt->mfc_expire = 0;
1367 rt->mfc_stall = NULL;
1368
1369 rt->mfc_bw_meter = NULL;
1370 /* insert new entry at head of hash chain */
1371 rt->mfc_next = mfctable[hash];
1372 mfctable[hash] = rt;
1373 }
1374 }
1375 MFC_UNLOCK();
1376 VIF_UNLOCK();
1377 return 0;
1378 }
1379
1380 /*
1381 * Delete an mfc entry
1382 */
1383 static int
1384 del_mfc(struct mfcctl2 *mfccp)
1385 {
1386 struct in_addr origin;
1387 struct in_addr mcastgrp;
1388 struct mfc *rt;
1389 struct mfc **nptr;
1390 u_long hash;
1391 struct bw_meter *list;
1392
1393 origin = mfccp->mfcc_origin;
1394 mcastgrp = mfccp->mfcc_mcastgrp;
1395
1396 if (mrtdebug & DEBUG_MFC)
1397 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1398 (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1399
1400 MFC_LOCK();
1401
1402 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1403 for (nptr = &mfctable[hash]; (rt = *nptr) != NULL; nptr = &rt->mfc_next)
1404 if (origin.s_addr == rt->mfc_origin.s_addr &&
1405 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1406 rt->mfc_stall == NULL)
1407 break;
1408 if (rt == NULL) {
1409 MFC_UNLOCK();
1410 return EADDRNOTAVAIL;
1411 }
1412
1413 *nptr = rt->mfc_next;
1414
1415 /*
1416 * free the bw_meter entries
1417 */
1418 list = rt->mfc_bw_meter;
1419 rt->mfc_bw_meter = NULL;
1420
1421 free(rt, M_MRTABLE);
1422
1423 free_bw_list(list);
1424
1425 MFC_UNLOCK();
1426
1427 return 0;
1428 }
1429
1430 /*
1431 * Send a message to mrouted on the multicast routing socket
1432 */
1433 static int
1434 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
1435 {
1436 if (s) {
1437 SOCKBUF_LOCK(&s->so_rcv);
1438 if (sbappendaddr_locked(&s->so_rcv, (struct sockaddr *)src, mm,
1439 NULL) != 0) {
1440 sorwakeup_locked(s);
1441 return 0;
1442 }
1443 SOCKBUF_UNLOCK(&s->so_rcv);
1444 }
1445 m_freem(mm);
1446 return -1;
1447 }
1448
1449 /*
1450 * IP multicast forwarding function. This function assumes that the packet
1451 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1452 * pointed to by "ifp", and the packet is to be relayed to other networks
1453 * that have members of the packet's destination IP multicast group.
1454 *
1455 * The packet is returned unscathed to the caller, unless it is
1456 * erroneous, in which case a non-zero return value tells the caller to
1457 * discard it.
1458 */
1459
1460 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1461
1462 static int
1463 X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
1464 struct ip_moptions *imo)
1465 {
1466 struct mfc *rt;
1467 int error;
1468 vifi_t vifi;
1469
1470 if (mrtdebug & DEBUG_FORWARD)
1471 log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1472 (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1473 (void *)ifp);
1474
1475 if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1476 ((u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1477 /*
1478 * Packet arrived via a physical interface or
1479 * an encapsulated tunnel or a register_vif.
1480 */
1481 } else {
1482 /*
1483 * Packet arrived through a source-route tunnel.
1484 * Source-route tunnels are no longer supported.
1485 */
1486 static int last_log;
1487 if (last_log != time_second) {
1488 last_log = time_second;
1489 log(LOG_ERR,
1490 "ip_mforward: received source-routed packet from %lx\n",
1491 (u_long)ntohl(ip->ip_src.s_addr));
1492 }
1493 return 1;
1494 }
1495
1496 VIF_LOCK();
1497 MFC_LOCK();
1498 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1499 if (ip->ip_ttl < 255)
1500 ip->ip_ttl++; /* compensate for -1 in *_send routines */
1501 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1502 struct vif *vifp = viftable + vifi;
1503
1504 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s)\n",
1505 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr),
1506 vifi,
1507 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1508 vifp->v_ifp->if_xname);
1509 }
1510 error = ip_mdq(m, ifp, NULL, vifi);
1511 MFC_UNLOCK();
1512 VIF_UNLOCK();
1513 return error;
1514 }
1515 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1516 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1517 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr));
1518 if (!imo)
1519 printf("In fact, no options were specified at all\n");
1520 }
1521
1522 /*
1523 * Don't forward a packet with time-to-live of zero or one,
1524 * or a packet destined to a local-only group.
1525 */
1526 if (ip->ip_ttl <= 1 || ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP) {
1527 MFC_UNLOCK();
1528 VIF_UNLOCK();
1529 return 0;
1530 }
1531
1532 /*
1533 * Determine forwarding vifs from the forwarding cache table
1534 */
1535 ++mrtstat.mrts_mfc_lookups;
1536 rt = mfc_find(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1537
1538 /* Entry exists, so forward if necessary */
1539 if (rt != NULL) {
1540 error = ip_mdq(m, ifp, rt, -1);
1541 MFC_UNLOCK();
1542 VIF_UNLOCK();
1543 return error;
1544 } else {
1545 /*
1546 * If we don't have a route for packet's origin,
1547 * Make a copy of the packet & send message to routing daemon
1548 */
1549
1550 struct mbuf *mb0;
1551 struct rtdetq *rte;
1552 u_long hash;
1553 int hlen = ip->ip_hl << 2;
1554
1555 ++mrtstat.mrts_mfc_misses;
1556
1557 mrtstat.mrts_no_route++;
1558 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1559 log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1560 (u_long)ntohl(ip->ip_src.s_addr),
1561 (u_long)ntohl(ip->ip_dst.s_addr));
1562
1563 /*
1564 * Allocate mbufs early so that we don't do extra work if we are
1565 * just going to fail anyway. Make sure to pullup the header so
1566 * that other people can't step on it.
1567 */
1568 rte = (struct rtdetq *)malloc((sizeof *rte), M_MRTABLE, M_NOWAIT);
1569 if (rte == NULL) {
1570 MFC_UNLOCK();
1571 VIF_UNLOCK();
1572 return ENOBUFS;
1573 }
1574 mb0 = m_copypacket(m, M_DONTWAIT);
1575 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1576 mb0 = m_pullup(mb0, hlen);
1577 if (mb0 == NULL) {
1578 free(rte, M_MRTABLE);
1579 MFC_UNLOCK();
1580 VIF_UNLOCK();
1581 return ENOBUFS;
1582 }
1583
1584 /* is there an upcall waiting for this flow ? */
1585 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1586 for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1587 if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1588 (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1589 (rt->mfc_stall != NULL))
1590 break;
1591 }
1592
1593 if (rt == NULL) {
1594 int i;
1595 struct igmpmsg *im;
1596 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1597 struct mbuf *mm;
1598
1599 /*
1600 * Locate the vifi for the incoming interface for this packet.
1601 * If none found, drop packet.
1602 */
1603 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1604 ;
1605 if (vifi >= numvifs) /* vif not found, drop packet */
1606 goto non_fatal;
1607
1608 /* no upcall, so make a new entry */
1609 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1610 if (rt == NULL)
1611 goto fail;
1612 /* Make a copy of the header to send to the user level process */
1613 mm = m_copy(mb0, 0, hlen);
1614 if (mm == NULL)
1615 goto fail1;
1616
1617 /*
1618 * Send message to routing daemon to install
1619 * a route into the kernel table
1620 */
1621
1622 im = mtod(mm, struct igmpmsg *);
1623 im->im_msgtype = IGMPMSG_NOCACHE;
1624 im->im_mbz = 0;
1625 im->im_vif = vifi;
1626
1627 mrtstat.mrts_upcalls++;
1628
1629 k_igmpsrc.sin_addr = ip->ip_src;
1630 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1631 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
1632 ++mrtstat.mrts_upq_sockfull;
1633 fail1:
1634 free(rt, M_MRTABLE);
1635 fail:
1636 free(rte, M_MRTABLE);
1637 m_freem(mb0);
1638 MFC_UNLOCK();
1639 VIF_UNLOCK();
1640 return ENOBUFS;
1641 }
1642
1643 /* insert new entry at head of hash chain */
1644 rt->mfc_origin.s_addr = ip->ip_src.s_addr;
1645 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr;
1646 rt->mfc_expire = UPCALL_EXPIRE;
1647 nexpire[hash]++;
1648 for (i = 0; i < numvifs; i++) {
1649 rt->mfc_ttls[i] = 0;
1650 rt->mfc_flags[i] = 0;
1651 }
1652 rt->mfc_parent = -1;
1653
1654 rt->mfc_rp.s_addr = INADDR_ANY; /* clear the RP address */
1655
1656 rt->mfc_bw_meter = NULL;
1657
1658 /* link into table */
1659 rt->mfc_next = mfctable[hash];
1660 mfctable[hash] = rt;
1661 rt->mfc_stall = rte;
1662
1663 } else {
1664 /* determine if q has overflowed */
1665 int npkts = 0;
1666 struct rtdetq **p;
1667
1668 /*
1669 * XXX ouch! we need to append to the list, but we
1670 * only have a pointer to the front, so we have to
1671 * scan the entire list every time.
1672 */
1673 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
1674 npkts++;
1675
1676 if (npkts > MAX_UPQ) {
1677 mrtstat.mrts_upq_ovflw++;
1678 non_fatal:
1679 free(rte, M_MRTABLE);
1680 m_freem(mb0);
1681 MFC_UNLOCK();
1682 VIF_UNLOCK();
1683 return 0;
1684 }
1685
1686 /* Add this entry to the end of the queue */
1687 *p = rte;
1688 }
1689
1690 rte->m = mb0;
1691 rte->ifp = ifp;
1692 rte->next = NULL;
1693
1694 MFC_UNLOCK();
1695 VIF_UNLOCK();
1696
1697 return 0;
1698 }
1699 }
1700
1701 /*
1702 * Clean up the cache entry if upcall is not serviced
1703 */
1704 static void
1705 expire_upcalls(void *unused)
1706 {
1707 struct rtdetq *rte;
1708 struct mfc *mfc, **nptr;
1709 int i;
1710
1711 MFC_LOCK();
1712 for (i = 0; i < MFCTBLSIZ; i++) {
1713 if (nexpire[i] == 0)
1714 continue;
1715 nptr = &mfctable[i];
1716 for (mfc = *nptr; mfc != NULL; mfc = *nptr) {
1717 /*
1718 * Skip real cache entries
1719 * Make sure it wasn't marked to not expire (shouldn't happen)
1720 * If it expires now
1721 */
1722 if (mfc->mfc_stall != NULL && mfc->mfc_expire != 0 &&
1723 --mfc->mfc_expire == 0) {
1724 if (mrtdebug & DEBUG_EXPIRE)
1725 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n",
1726 (u_long)ntohl(mfc->mfc_origin.s_addr),
1727 (u_long)ntohl(mfc->mfc_mcastgrp.s_addr));
1728 /*
1729 * drop all the packets
1730 * free the mbuf with the pkt, if, timing info
1731 */
1732 for (rte = mfc->mfc_stall; rte; ) {
1733 struct rtdetq *n = rte->next;
1734
1735 m_freem(rte->m);
1736 free(rte, M_MRTABLE);
1737 rte = n;
1738 }
1739 ++mrtstat.mrts_cache_cleanups;
1740 nexpire[i]--;
1741
1742 /*
1743 * free the bw_meter entries
1744 */
1745 while (mfc->mfc_bw_meter != NULL) {
1746 struct bw_meter *x = mfc->mfc_bw_meter;
1747
1748 mfc->mfc_bw_meter = x->bm_mfc_next;
1749 free(x, M_BWMETER);
1750 }
1751
1752 *nptr = mfc->mfc_next;
1753 free(mfc, M_MRTABLE);
1754 } else {
1755 nptr = &mfc->mfc_next;
1756 }
1757 }
1758 }
1759 MFC_UNLOCK();
1760
1761 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL);
1762 }
1763
1764 /*
1765 * Packet forwarding routine once entry in the cache is made
1766 */
1767 static int
1768 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif)
1769 {
1770 struct ip *ip = mtod(m, struct ip *);
1771 vifi_t vifi;
1772 int plen = ip->ip_len;
1773
1774 VIF_LOCK_ASSERT();
1775 /*
1776 * Macro to send packet on vif. Since RSVP packets don't get counted on
1777 * input, they shouldn't get counted on output, so statistics keeping is
1778 * separate.
1779 */
1780 #define MC_SEND(ip,vifp,m) { \
1781 if ((vifp)->v_flags & VIFF_TUNNEL) \
1782 encap_send((ip), (vifp), (m)); \
1783 else \
1784 phyint_send((ip), (vifp), (m)); \
1785 }
1786
1787 /*
1788 * If xmt_vif is not -1, send on only the requested vif.
1789 *
1790 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1791 */
1792 if (xmt_vif < numvifs) {
1793 #ifdef PIM
1794 if (viftable[xmt_vif].v_flags & VIFF_REGISTER)
1795 pim_register_send(ip, viftable + xmt_vif, m, rt);
1796 else
1797 #endif
1798 MC_SEND(ip, viftable + xmt_vif, m);
1799 return 1;
1800 }
1801
1802 /*
1803 * Don't forward if it didn't arrive from the parent vif for its origin.
1804 */
1805 vifi = rt->mfc_parent;
1806 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1807 /* came in the wrong interface */
1808 if (mrtdebug & DEBUG_FORWARD)
1809 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1810 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp);
1811 ++mrtstat.mrts_wrong_if;
1812 ++rt->mfc_wrong_if;
1813 /*
1814 * If we are doing PIM assert processing, send a message
1815 * to the routing daemon.
1816 *
1817 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1818 * can complete the SPT switch, regardless of the type
1819 * of the iif (broadcast media, GRE tunnel, etc).
1820 */
1821 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) {
1822 struct timeval now;
1823 u_long delta;
1824
1825 #ifdef PIM
1826 if (ifp == &multicast_register_if)
1827 pimstat.pims_rcv_registers_wrongiif++;
1828 #endif
1829
1830 /* Get vifi for the incoming packet */
1831 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1832 ;
1833 if (vifi >= numvifs)
1834 return 0; /* The iif is not found: ignore the packet. */
1835
1836 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF)
1837 return 0; /* WRONGVIF disabled: ignore the packet */
1838
1839 GET_TIME(now);
1840
1841 TV_DELTA(now, rt->mfc_last_assert, delta);
1842
1843 if (delta > ASSERT_MSG_TIME) {
1844 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1845 struct igmpmsg *im;
1846 int hlen = ip->ip_hl << 2;
1847 struct mbuf *mm = m_copy(m, 0, hlen);
1848
1849 if (mm && (M_HASCL(mm) || mm->m_len < hlen))
1850 mm = m_pullup(mm, hlen);
1851 if (mm == NULL)
1852 return ENOBUFS;
1853
1854 rt->mfc_last_assert = now;
1855
1856 im = mtod(mm, struct igmpmsg *);
1857 im->im_msgtype = IGMPMSG_WRONGVIF;
1858 im->im_mbz = 0;
1859 im->im_vif = vifi;
1860
1861 mrtstat.mrts_upcalls++;
1862
1863 k_igmpsrc.sin_addr = im->im_src;
1864 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) {
1865 log(LOG_WARNING,
1866 "ip_mforward: ip_mrouter socket queue full\n");
1867 ++mrtstat.mrts_upq_sockfull;
1868 return ENOBUFS;
1869 }
1870 }
1871 }
1872 return 0;
1873 }
1874
1875 /* If I sourced this packet, it counts as output, else it was input. */
1876 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) {
1877 viftable[vifi].v_pkt_out++;
1878 viftable[vifi].v_bytes_out += plen;
1879 } else {
1880 viftable[vifi].v_pkt_in++;
1881 viftable[vifi].v_bytes_in += plen;
1882 }
1883 rt->mfc_pkt_cnt++;
1884 rt->mfc_byte_cnt += plen;
1885
1886 /*
1887 * For each vif, decide if a copy of the packet should be forwarded.
1888 * Forward if:
1889 * - the ttl exceeds the vif's threshold
1890 * - there are group members downstream on interface
1891 */
1892 for (vifi = 0; vifi < numvifs; vifi++)
1893 if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1894 viftable[vifi].v_pkt_out++;
1895 viftable[vifi].v_bytes_out += plen;
1896 #ifdef PIM
1897 if (viftable[vifi].v_flags & VIFF_REGISTER)
1898 pim_register_send(ip, viftable + vifi, m, rt);
1899 else
1900 #endif
1901 MC_SEND(ip, viftable+vifi, m);
1902 }
1903
1904 /*
1905 * Perform upcall-related bw measuring.
1906 */
1907 if (rt->mfc_bw_meter != NULL) {
1908 struct bw_meter *x;
1909 struct timeval now;
1910
1911 GET_TIME(now);
1912 MFC_LOCK_ASSERT();
1913 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next)
1914 bw_meter_receive_packet(x, plen, &now);
1915 }
1916
1917 return 0;
1918 }
1919
1920 /*
1921 * check if a vif number is legal/ok. This is used by ip_output.
1922 */
1923 static int
1924 X_legal_vif_num(int vif)
1925 {
1926 /* XXX unlocked, matter? */
1927 return (vif >= 0 && vif < numvifs);
1928 }
1929
1930 /*
1931 * Return the local address used by this vif
1932 */
1933 static u_long
1934 X_ip_mcast_src(int vifi)
1935 {
1936 /* XXX unlocked, matter? */
1937 if (vifi >= 0 && vifi < numvifs)
1938 return viftable[vifi].v_lcl_addr.s_addr;
1939 else
1940 return INADDR_ANY;
1941 }
1942
1943 static void
1944 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1945 {
1946 struct mbuf *mb_copy;
1947 int hlen = ip->ip_hl << 2;
1948
1949 VIF_LOCK_ASSERT();
1950
1951 /*
1952 * Make a new reference to the packet; make sure that
1953 * the IP header is actually copied, not just referenced,
1954 * so that ip_output() only scribbles on the copy.
1955 */
1956 mb_copy = m_copypacket(m, M_DONTWAIT);
1957 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen))
1958 mb_copy = m_pullup(mb_copy, hlen);
1959 if (mb_copy == NULL)
1960 return;
1961
1962 if (vifp->v_rate_limit == 0)
1963 tbf_send_packet(vifp, mb_copy);
1964 else
1965 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len);
1966 }
1967
1968 static void
1969 encap_send(struct ip *ip, struct vif *vifp, struct mbuf *m)
1970 {
1971 struct mbuf *mb_copy;
1972 struct ip *ip_copy;
1973 int i, len = ip->ip_len;
1974
1975 VIF_LOCK_ASSERT();
1976
1977 /* Take care of delayed checksums */
1978 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1979 in_delayed_cksum(m);
1980 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1981 }
1982
1983 /*
1984 * copy the old packet & pullup its IP header into the
1985 * new mbuf so we can modify it. Try to fill the new
1986 * mbuf since if we don't the ethernet driver will.
1987 */
1988 MGETHDR(mb_copy, M_DONTWAIT, MT_HEADER);
1989 if (mb_copy == NULL)
1990 return;
1991 #ifdef MAC
1992 mac_create_mbuf_multicast_encap(m, vifp->v_ifp, mb_copy);
1993 #endif
1994 mb_copy->m_data += max_linkhdr;
1995 mb_copy->m_len = sizeof(multicast_encap_iphdr);
1996
1997 if ((mb_copy->m_next = m_copypacket(m, M_DONTWAIT)) == NULL) {
1998 m_freem(mb_copy);
1999 return;
2000 }
2001 i = MHLEN - M_LEADINGSPACE(mb_copy);
2002 if (i > len)
2003 i = len;
2004 mb_copy = m_pullup(mb_copy, i);
2005 if (mb_copy == NULL)
2006 return;
2007 mb_copy->m_pkthdr.len = len + sizeof(multicast_encap_iphdr);
2008
2009 /*
2010 * fill in the encapsulating IP header.
2011 */
2012 ip_copy = mtod(mb_copy, struct ip *);
2013 *ip_copy = multicast_encap_iphdr;
2014 ip_copy->ip_id = ip_newid();
2015 ip_copy->ip_len += len;
2016 ip_copy->ip_src = vifp->v_lcl_addr;
2017 ip_copy->ip_dst = vifp->v_rmt_addr;
2018
2019 /*
2020 * turn the encapsulated IP header back into a valid one.
2021 */
2022 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
2023 --ip->ip_ttl;
2024 ip->ip_len = htons(ip->ip_len);
2025 ip->ip_off = htons(ip->ip_off);
2026 ip->ip_sum = 0;
2027 mb_copy->m_data += sizeof(multicast_encap_iphdr);
2028 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
2029 mb_copy->m_data -= sizeof(multicast_encap_iphdr);
2030
2031 if (vifp->v_rate_limit == 0)
2032 tbf_send_packet(vifp, mb_copy);
2033 else
2034 tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
2035 }
2036
2037 /*
2038 * Token bucket filter module
2039 */
2040
2041 static void
2042 tbf_control(struct vif *vifp, struct mbuf *m, struct ip *ip, u_long p_len)
2043 {
2044 struct tbf *t = vifp->v_tbf;
2045
2046 VIF_LOCK_ASSERT();
2047
2048 if (p_len > MAX_BKT_SIZE) { /* drop if packet is too large */
2049 mrtstat.mrts_pkt2large++;
2050 m_freem(m);
2051 return;
2052 }
2053
2054 tbf_update_tokens(vifp);
2055
2056 if (t->tbf_q_len == 0) { /* queue empty... */
2057 if (p_len <= t->tbf_n_tok) { /* send packet if enough tokens */
2058 t->tbf_n_tok -= p_len;
2059 tbf_send_packet(vifp, m);
2060 } else { /* no, queue packet and try later */
2061 tbf_queue(vifp, m);
2062 callout_reset(&tbf_reprocess_ch, TBF_REPROCESS,
2063 tbf_reprocess_q, vifp);
2064 }
2065 } else if (t->tbf_q_len < t->tbf_max_q_len) {
2066 /* finite queue length, so queue pkts and process queue */
2067 tbf_queue(vifp, m);
2068 tbf_process_q(vifp);
2069 } else {
2070 /* queue full, try to dq and queue and process */
2071 if (!tbf_dq_sel(vifp, ip)) {
2072 mrtstat.mrts_q_overflow++;
2073 m_freem(m);
2074 } else {
2075 tbf_queue(vifp, m);
2076 tbf_process_q(vifp);
2077 }
2078 }
2079 }
2080
2081 /*
2082 * adds a packet to the queue at the interface
2083 */
2084 static void
2085 tbf_queue(struct vif *vifp, struct mbuf *m)
2086 {
2087 struct tbf *t = vifp->v_tbf;
2088
2089 VIF_LOCK_ASSERT();
2090
2091 if (t->tbf_t == NULL) /* Queue was empty */
2092 t->tbf_q = m;
2093 else /* Insert at tail */
2094 t->tbf_t->m_act = m;
2095
2096 t->tbf_t = m; /* Set new tail pointer */
2097
2098 #ifdef DIAGNOSTIC
2099 /* Make sure we didn't get fed a bogus mbuf */
2100 if (m->m_act)
2101 panic("tbf_queue: m_act");
2102 #endif
2103 m->m_act = NULL;
2104
2105 t->tbf_q_len++;
2106 }
2107
2108 /*
2109 * processes the queue at the interface
2110 */
2111 static void
2112 tbf_process_q(struct vif *vifp)
2113 {
2114 struct tbf *t = vifp->v_tbf;
2115
2116 VIF_LOCK_ASSERT();
2117
2118 /* loop through the queue at the interface and send as many packets
2119 * as possible
2120 */
2121 while (t->tbf_q_len > 0) {
2122 struct mbuf *m = t->tbf_q;
2123 int len = mtod(m, struct ip *)->ip_len;
2124
2125 /* determine if the packet can be sent */
2126 if (len > t->tbf_n_tok) /* not enough tokens, we are done */
2127 break;
2128 /* ok, reduce no of tokens, dequeue and send the packet. */
2129 t->tbf_n_tok -= len;
2130
2131 t->tbf_q = m->m_act;
2132 if (--t->tbf_q_len == 0)
2133 t->tbf_t = NULL;
2134
2135 m->m_act = NULL;
2136 tbf_send_packet(vifp, m);
2137 }
2138 }
2139
2140 static void
2141 tbf_reprocess_q(void *xvifp)
2142 {
2143 struct vif *vifp = xvifp;
2144
2145 if (ip_mrouter == NULL)
2146 return;
2147 VIF_LOCK();
2148 tbf_update_tokens(vifp);
2149 tbf_process_q(vifp);
2150 if (vifp->v_tbf->tbf_q_len)
2151 callout_reset(&tbf_reprocess_ch, TBF_REPROCESS, tbf_reprocess_q, vifp);
2152 VIF_UNLOCK();
2153 }
2154
2155 /* function that will selectively discard a member of the queue
2156 * based on the precedence value and the priority
2157 */
2158 static int
2159 tbf_dq_sel(struct vif *vifp, struct ip *ip)
2160 {
2161 u_int p;
2162 struct mbuf *m, *last;
2163 struct mbuf **np;
2164 struct tbf *t = vifp->v_tbf;
2165
2166 VIF_LOCK_ASSERT();
2167
2168 p = priority(vifp, ip);
2169
2170 np = &t->tbf_q;
2171 last = NULL;
2172 while ((m = *np) != NULL) {
2173 if (p > priority(vifp, mtod(m, struct ip *))) {
2174 *np = m->m_act;
2175 /* If we're removing the last packet, fix the tail pointer */
2176 if (m == t->tbf_t)
2177 t->tbf_t = last;
2178 m_freem(m);
2179 /* It's impossible for the queue to be empty, but check anyways. */
2180 if (--t->tbf_q_len == 0)
2181 t->tbf_t = NULL;
2182 mrtstat.mrts_drop_sel++;
2183 return 1;
2184 }
2185 np = &m->m_act;
2186 last = m;
2187 }
2188 return 0;
2189 }
2190
2191 static void
2192 tbf_send_packet(struct vif *vifp, struct mbuf *m)
2193 {
2194 VIF_LOCK_ASSERT();
2195
2196 if (vifp->v_flags & VIFF_TUNNEL) /* If tunnel options */
2197 ip_output(m, NULL, &vifp->v_route, IP_FORWARDING, NULL, NULL);
2198 else {
2199 struct ip_moptions imo;
2200 int error;
2201 static struct route ro; /* XXX check this */
2202
2203 imo.imo_multicast_ifp = vifp->v_ifp;
2204 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
2205 imo.imo_multicast_loop = 1;
2206 imo.imo_multicast_vif = -1;
2207
2208 /*
2209 * Re-entrancy should not be a problem here, because
2210 * the packets that we send out and are looped back at us
2211 * should get rejected because they appear to come from
2212 * the loopback interface, thus preventing looping.
2213 */
2214 error = ip_output(m, NULL, &ro, IP_FORWARDING, &imo, NULL);
2215
2216 if (mrtdebug & DEBUG_XMIT)
2217 log(LOG_DEBUG, "phyint_send on vif %d err %d\n",
2218 (int)(vifp - viftable), error);
2219 }
2220 }
2221
2222 /* determine the current time and then
2223 * the elapsed time (between the last time and time now)
2224 * in milliseconds & update the no. of tokens in the bucket
2225 */
2226 static void
2227 tbf_update_tokens(struct vif *vifp)
2228 {
2229 struct timeval tp;
2230 u_long tm;
2231 struct tbf *t = vifp->v_tbf;
2232
2233 VIF_LOCK_ASSERT();
2234
2235 GET_TIME(tp);
2236
2237 TV_DELTA(tp, t->tbf_last_pkt_t, tm);
2238
2239 /*
2240 * This formula is actually
2241 * "time in seconds" * "bytes/second".
2242 *
2243 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
2244 *
2245 * The (1000/1024) was introduced in add_vif to optimize
2246 * this divide into a shift.
2247 */
2248 t->tbf_n_tok += tm * vifp->v_rate_limit / 1024 / 8;
2249 t->tbf_last_pkt_t = tp;
2250
2251 if (t->tbf_n_tok > MAX_BKT_SIZE)
2252 t->tbf_n_tok = MAX_BKT_SIZE;
2253 }
2254
2255 static int
2256 priority(struct vif *vifp, struct ip *ip)
2257 {
2258 int prio = 50; /* the lowest priority -- default case */
2259
2260 /* temporary hack; may add general packet classifier some day */
2261
2262 /*
2263 * The UDP port space is divided up into four priority ranges:
2264 * [0, 16384) : unclassified - lowest priority
2265 * [16384, 32768) : audio - highest priority
2266 * [32768, 49152) : whiteboard - medium priority
2267 * [49152, 65536) : video - low priority
2268 *
2269 * Everything else gets lowest priority.
2270 */
2271 if (ip->ip_p == IPPROTO_UDP) {
2272 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
2273 switch (ntohs(udp->uh_dport) & 0xc000) {
2274 case 0x4000:
2275 prio = 70;
2276 break;
2277 case 0x8000:
2278 prio = 60;
2279 break;
2280 case 0xc000:
2281 prio = 55;
2282 break;
2283 }
2284 }
2285 return prio;
2286 }
2287
2288 /*
2289 * End of token bucket filter modifications
2290 */
2291
2292 static int
2293 X_ip_rsvp_vif(struct socket *so, struct sockopt *sopt)
2294 {
2295 int error, vifi;
2296
2297 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2298 return EOPNOTSUPP;
2299
2300 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi);
2301 if (error)
2302 return error;
2303
2304 VIF_LOCK();
2305
2306 if (vifi < 0 || vifi >= numvifs) { /* Error if vif is invalid */
2307 VIF_UNLOCK();
2308 return EADDRNOTAVAIL;
2309 }
2310
2311 if (sopt->sopt_name == IP_RSVP_VIF_ON) {
2312 /* Check if socket is available. */
2313 if (viftable[vifi].v_rsvpd != NULL) {
2314 VIF_UNLOCK();
2315 return EADDRINUSE;
2316 }
2317
2318 viftable[vifi].v_rsvpd = so;
2319 /* This may seem silly, but we need to be sure we don't over-increment
2320 * the RSVP counter, in case something slips up.
2321 */
2322 if (!viftable[vifi].v_rsvp_on) {
2323 viftable[vifi].v_rsvp_on = 1;
2324 rsvp_on++;
2325 }
2326 } else { /* must be VIF_OFF */
2327 /*
2328 * XXX as an additional consistency check, one could make sure
2329 * that viftable[vifi].v_rsvpd == so, otherwise passing so as
2330 * first parameter is pretty useless.
2331 */
2332 viftable[vifi].v_rsvpd = NULL;
2333 /*
2334 * This may seem silly, but we need to be sure we don't over-decrement
2335 * the RSVP counter, in case something slips up.
2336 */
2337 if (viftable[vifi].v_rsvp_on) {
2338 viftable[vifi].v_rsvp_on = 0;
2339 rsvp_on--;
2340 }
2341 }
2342 VIF_UNLOCK();
2343 return 0;
2344 }
2345
2346 static void
2347 X_ip_rsvp_force_done(struct socket *so)
2348 {
2349 int vifi;
2350
2351 /* Don't bother if it is not the right type of socket. */
2352 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2353 return;
2354
2355 VIF_LOCK();
2356
2357 /* The socket may be attached to more than one vif...this
2358 * is perfectly legal.
2359 */
2360 for (vifi = 0; vifi < numvifs; vifi++) {
2361 if (viftable[vifi].v_rsvpd == so) {
2362 viftable[vifi].v_rsvpd = NULL;
2363 /* This may seem silly, but we need to be sure we don't
2364 * over-decrement the RSVP counter, in case something slips up.
2365 */
2366 if (viftable[vifi].v_rsvp_on) {
2367 viftable[vifi].v_rsvp_on = 0;
2368 rsvp_on--;
2369 }
2370 }
2371 }
2372
2373 VIF_UNLOCK();
2374 }
2375
2376 static void
2377 X_rsvp_input(struct mbuf *m, int off)
2378 {
2379 int vifi;
2380 struct ip *ip = mtod(m, struct ip *);
2381 struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET };
2382 struct ifnet *ifp;
2383
2384 if (rsvpdebug)
2385 printf("rsvp_input: rsvp_on %d\n",rsvp_on);
2386
2387 /* Can still get packets with rsvp_on = 0 if there is a local member
2388 * of the group to which the RSVP packet is addressed. But in this
2389 * case we want to throw the packet away.
2390 */
2391 if (!rsvp_on) {
2392 m_freem(m);
2393 return;
2394 }
2395
2396 if (rsvpdebug)
2397 printf("rsvp_input: check vifs\n");
2398
2399 #ifdef DIAGNOSTIC
2400 M_ASSERTPKTHDR(m);
2401 #endif
2402
2403 ifp = m->m_pkthdr.rcvif;
2404
2405 VIF_LOCK();
2406 /* Find which vif the packet arrived on. */
2407 for (vifi = 0; vifi < numvifs; vifi++)
2408 if (viftable[vifi].v_ifp == ifp)
2409 break;
2410
2411 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) {
2412 /*
2413 * Drop the lock here to avoid holding it across rip_input.
2414 * This could make rsvpdebug printfs wrong. If you care,
2415 * record the state of stuff before dropping the lock.
2416 */
2417 VIF_UNLOCK();
2418 /*
2419 * If the old-style non-vif-associated socket is set,
2420 * then use it. Otherwise, drop packet since there
2421 * is no specific socket for this vif.
2422 */
2423 if (ip_rsvpd != NULL) {
2424 if (rsvpdebug)
2425 printf("rsvp_input: Sending packet up old-style socket\n");
2426 rip_input(m, off); /* xxx */
2427 } else {
2428 if (rsvpdebug && vifi == numvifs)
2429 printf("rsvp_input: Can't find vif for packet.\n");
2430 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL)
2431 printf("rsvp_input: No socket defined for vif %d\n",vifi);
2432 m_freem(m);
2433 }
2434 return;
2435 }
2436 rsvp_src.sin_addr = ip->ip_src;
2437
2438 if (rsvpdebug && m)
2439 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
2440 m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv)));
2441
2442 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) {
2443 if (rsvpdebug)
2444 printf("rsvp_input: Failed to append to socket\n");
2445 } else {
2446 if (rsvpdebug)
2447 printf("rsvp_input: send packet up\n");
2448 }
2449 VIF_UNLOCK();
2450 }
2451
2452 /*
2453 * Code for bandwidth monitors
2454 */
2455
2456 /*
2457 * Define common interface for timeval-related methods
2458 */
2459 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
2460 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
2461 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
2462
2463 static uint32_t
2464 compute_bw_meter_flags(struct bw_upcall *req)
2465 {
2466 uint32_t flags = 0;
2467
2468 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
2469 flags |= BW_METER_UNIT_PACKETS;
2470 if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
2471 flags |= BW_METER_UNIT_BYTES;
2472 if (req->bu_flags & BW_UPCALL_GEQ)
2473 flags |= BW_METER_GEQ;
2474 if (req->bu_flags & BW_UPCALL_LEQ)
2475 flags |= BW_METER_LEQ;
2476
2477 return flags;
2478 }
2479
2480 /*
2481 * Add a bw_meter entry
2482 */
2483 static int
2484 add_bw_upcall(struct bw_upcall *req)
2485 {
2486 struct mfc *mfc;
2487 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
2488 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
2489 struct timeval now;
2490 struct bw_meter *x;
2491 uint32_t flags;
2492
2493 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2494 return EOPNOTSUPP;
2495
2496 /* Test if the flags are valid */
2497 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
2498 return EINVAL;
2499 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
2500 return EINVAL;
2501 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2502 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
2503 return EINVAL;
2504
2505 /* Test if the threshold time interval is valid */
2506 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
2507 return EINVAL;
2508
2509 flags = compute_bw_meter_flags(req);
2510
2511 /*
2512 * Find if we have already same bw_meter entry
2513 */
2514 MFC_LOCK();
2515 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2516 if (mfc == NULL) {
2517 MFC_UNLOCK();
2518 return EADDRNOTAVAIL;
2519 }
2520 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) {
2521 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2522 &req->bu_threshold.b_time, ==)) &&
2523 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2524 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2525 (x->bm_flags & BW_METER_USER_FLAGS) == flags) {
2526 MFC_UNLOCK();
2527 return 0; /* XXX Already installed */
2528 }
2529 }
2530
2531 /* Allocate the new bw_meter entry */
2532 x = (struct bw_meter *)malloc(sizeof(*x), M_BWMETER, M_NOWAIT);
2533 if (x == NULL) {
2534 MFC_UNLOCK();
2535 return ENOBUFS;
2536 }
2537
2538 /* Set the new bw_meter entry */
2539 x->bm_threshold.b_time = req->bu_threshold.b_time;
2540 GET_TIME(now);
2541 x->bm_start_time = now;
2542 x->bm_threshold.b_packets = req->bu_threshold.b_packets;
2543 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
2544 x->bm_measured.b_packets = 0;
2545 x->bm_measured.b_bytes = 0;
2546 x->bm_flags = flags;
2547 x->bm_time_next = NULL;
2548 x->bm_time_hash = BW_METER_BUCKETS;
2549
2550 /* Add the new bw_meter entry to the front of entries for this MFC */
2551 x->bm_mfc = mfc;
2552 x->bm_mfc_next = mfc->mfc_bw_meter;
2553 mfc->mfc_bw_meter = x;
2554 schedule_bw_meter(x, &now);
2555 MFC_UNLOCK();
2556
2557 return 0;
2558 }
2559
2560 static void
2561 free_bw_list(struct bw_meter *list)
2562 {
2563 while (list != NULL) {
2564 struct bw_meter *x = list;
2565
2566 list = list->bm_mfc_next;
2567 unschedule_bw_meter(x);
2568 free(x, M_BWMETER);
2569 }
2570 }
2571
2572 /*
2573 * Delete one or multiple bw_meter entries
2574 */
2575 static int
2576 del_bw_upcall(struct bw_upcall *req)
2577 {
2578 struct mfc *mfc;
2579 struct bw_meter *x;
2580
2581 if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2582 return EOPNOTSUPP;
2583
2584 MFC_LOCK();
2585 /* Find the corresponding MFC entry */
2586 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2587 if (mfc == NULL) {
2588 MFC_UNLOCK();
2589 return EADDRNOTAVAIL;
2590 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2591 /*
2592 * Delete all bw_meter entries for this mfc
2593 */
2594 struct bw_meter *list;
2595
2596 list = mfc->mfc_bw_meter;
2597 mfc->mfc_bw_meter = NULL;
2598 free_bw_list(list);
2599 MFC_UNLOCK();
2600 return 0;
2601 } else { /* Delete a single bw_meter entry */
2602 struct bw_meter *prev;
2603 uint32_t flags = 0;
2604
2605 flags = compute_bw_meter_flags(req);
2606
2607 /* Find the bw_meter entry to delete */
2608 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
2609 prev = x, x = x->bm_mfc_next) {
2610 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2611 &req->bu_threshold.b_time, ==)) &&
2612 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2613 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2614 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2615 break;
2616 }
2617 if (x != NULL) { /* Delete entry from the list for this MFC */
2618 if (prev != NULL)
2619 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/
2620 else
2621 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
2622
2623 unschedule_bw_meter(x);
2624 MFC_UNLOCK();
2625 /* Free the bw_meter entry */
2626 free(x, M_BWMETER);
2627 return 0;
2628 } else {
2629 MFC_UNLOCK();
2630 return EINVAL;
2631 }
2632 }
2633 /* NOTREACHED */
2634 }
2635
2636 /*
2637 * Perform bandwidth measurement processing that may result in an upcall
2638 */
2639 static void
2640 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2641 {
2642 struct timeval delta;
2643
2644 MFC_LOCK_ASSERT();
2645
2646 delta = *nowp;
2647 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2648
2649 if (x->bm_flags & BW_METER_GEQ) {
2650 /*
2651 * Processing for ">=" type of bw_meter entry
2652 */
2653 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2654 /* Reset the bw_meter entry */
2655 x->bm_start_time = *nowp;
2656 x->bm_measured.b_packets = 0;
2657 x->bm_measured.b_bytes = 0;
2658 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2659 }
2660
2661 /* Record that a packet is received */
2662 x->bm_measured.b_packets++;
2663 x->bm_measured.b_bytes += plen;
2664
2665 /*
2666 * Test if we should deliver an upcall
2667 */
2668 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
2669 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2670 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2671 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2672 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2673 /* Prepare an upcall for delivery */
2674 bw_meter_prepare_upcall(x, nowp);
2675 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2676 }
2677 }
2678 } else if (x->bm_flags & BW_METER_LEQ) {
2679 /*
2680 * Processing for "<=" type of bw_meter entry
2681 */
2682 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2683 /*
2684 * We are behind time with the multicast forwarding table
2685 * scanning for "<=" type of bw_meter entries, so test now
2686 * if we should deliver an upcall.
2687 */
2688 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2689 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2690 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2691 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2692 /* Prepare an upcall for delivery */
2693 bw_meter_prepare_upcall(x, nowp);
2694 }
2695 /* Reschedule the bw_meter entry */
2696 unschedule_bw_meter(x);
2697 schedule_bw_meter(x, nowp);
2698 }
2699
2700 /* Record that a packet is received */
2701 x->bm_measured.b_packets++;
2702 x->bm_measured.b_bytes += plen;
2703
2704 /*
2705 * Test if we should restart the measuring interval
2706 */
2707 if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
2708 x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
2709 (x->bm_flags & BW_METER_UNIT_BYTES &&
2710 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
2711 /* Don't restart the measuring interval */
2712 } else {
2713 /* Do restart the measuring interval */
2714 /*
2715 * XXX: note that we don't unschedule and schedule, because this
2716 * might be too much overhead per packet. Instead, when we process
2717 * all entries for a given timer hash bin, we check whether it is
2718 * really a timeout. If not, we reschedule at that time.
2719 */
2720 x->bm_start_time = *nowp;
2721 x->bm_measured.b_packets = 0;
2722 x->bm_measured.b_bytes = 0;
2723 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2724 }
2725 }
2726 }
2727
2728 /*
2729 * Prepare a bandwidth-related upcall
2730 */
2731 static void
2732 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2733 {
2734 struct timeval delta;
2735 struct bw_upcall *u;
2736
2737 MFC_LOCK_ASSERT();
2738
2739 /*
2740 * Compute the measured time interval
2741 */
2742 delta = *nowp;
2743 BW_TIMEVALDECR(&delta, &x->bm_start_time);
2744
2745 /*
2746 * If there are too many pending upcalls, deliver them now
2747 */
2748 if (bw_upcalls_n >= BW_UPCALLS_MAX)
2749 bw_upcalls_send();
2750
2751 /*
2752 * Set the bw_upcall entry
2753 */
2754 u = &bw_upcalls[bw_upcalls_n++];
2755 u->bu_src = x->bm_mfc->mfc_origin;
2756 u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2757 u->bu_threshold.b_time = x->bm_threshold.b_time;
2758 u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2759 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2760 u->bu_measured.b_time = delta;
2761 u->bu_measured.b_packets = x->bm_measured.b_packets;
2762 u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2763 u->bu_flags = 0;
2764 if (x->bm_flags & BW_METER_UNIT_PACKETS)
2765 u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2766 if (x->bm_flags & BW_METER_UNIT_BYTES)
2767 u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2768 if (x->bm_flags & BW_METER_GEQ)
2769 u->bu_flags |= BW_UPCALL_GEQ;
2770 if (x->bm_flags & BW_METER_LEQ)
2771 u->bu_flags |= BW_UPCALL_LEQ;
2772 }
2773
2774 /*
2775 * Send the pending bandwidth-related upcalls
2776 */
2777 static void
2778 bw_upcalls_send(void)
2779 {
2780 struct mbuf *m;
2781 int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
2782 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2783 static struct igmpmsg igmpmsg = { 0, /* unused1 */
2784 0, /* unused2 */
2785 IGMPMSG_BW_UPCALL,/* im_msgtype */
2786 0, /* im_mbz */
2787 0, /* im_vif */
2788 0, /* unused3 */
2789 { 0 }, /* im_src */
2790 { 0 } }; /* im_dst */
2791
2792 MFC_LOCK_ASSERT();
2793
2794 if (bw_upcalls_n == 0)
2795 return; /* No pending upcalls */
2796
2797 bw_upcalls_n = 0;
2798
2799 /*
2800 * Allocate a new mbuf, initialize it with the header and
2801 * the payload for the pending calls.
2802 */
2803 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2804 if (m == NULL) {
2805 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2806 return;
2807 }
2808
2809 m->m_len = m->m_pkthdr.len = 0;
2810 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2811 m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
2812
2813 /*
2814 * Send the upcalls
2815 * XXX do we need to set the address in k_igmpsrc ?
2816 */
2817 mrtstat.mrts_upcalls++;
2818 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
2819 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2820 ++mrtstat.mrts_upq_sockfull;
2821 }
2822 }
2823
2824 /*
2825 * Compute the timeout hash value for the bw_meter entries
2826 */
2827 #define BW_METER_TIMEHASH(bw_meter, hash) \
2828 do { \
2829 struct timeval next_timeval = (bw_meter)->bm_start_time; \
2830 \
2831 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2832 (hash) = next_timeval.tv_sec; \
2833 if (next_timeval.tv_usec) \
2834 (hash)++; /* XXX: make sure we don't timeout early */ \
2835 (hash) %= BW_METER_BUCKETS; \
2836 } while (0)
2837
2838 /*
2839 * Schedule a timer to process periodically bw_meter entry of type "<="
2840 * by linking the entry in the proper hash bucket.
2841 */
2842 static void
2843 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
2844 {
2845 int time_hash;
2846
2847 MFC_LOCK_ASSERT();
2848
2849 if (!(x->bm_flags & BW_METER_LEQ))
2850 return; /* XXX: we schedule timers only for "<=" entries */
2851
2852 /*
2853 * Reset the bw_meter entry
2854 */
2855 x->bm_start_time = *nowp;
2856 x->bm_measured.b_packets = 0;
2857 x->bm_measured.b_bytes = 0;
2858 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2859
2860 /*
2861 * Compute the timeout hash value and insert the entry
2862 */
2863 BW_METER_TIMEHASH(x, time_hash);
2864 x->bm_time_next = bw_meter_timers[time_hash];
2865 bw_meter_timers[time_hash] = x;
2866 x->bm_time_hash = time_hash;
2867 }
2868
2869 /*
2870 * Unschedule the periodic timer that processes bw_meter entry of type "<="
2871 * by removing the entry from the proper hash bucket.
2872 */
2873 static void
2874 unschedule_bw_meter(struct bw_meter *x)
2875 {
2876 int time_hash;
2877 struct bw_meter *prev, *tmp;
2878
2879 MFC_LOCK_ASSERT();
2880
2881 if (!(x->bm_flags & BW_METER_LEQ))
2882 return; /* XXX: we schedule timers only for "<=" entries */
2883
2884 /*
2885 * Compute the timeout hash value and delete the entry
2886 */
2887 time_hash = x->bm_time_hash;
2888 if (time_hash >= BW_METER_BUCKETS)
2889 return; /* Entry was not scheduled */
2890
2891 for (prev = NULL, tmp = bw_meter_timers[time_hash];
2892 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
2893 if (tmp == x)
2894 break;
2895
2896 if (tmp == NULL)
2897 panic("unschedule_bw_meter: bw_meter entry not found");
2898
2899 if (prev != NULL)
2900 prev->bm_time_next = x->bm_time_next;
2901 else
2902 bw_meter_timers[time_hash] = x->bm_time_next;
2903
2904 x->bm_time_next = NULL;
2905 x->bm_time_hash = BW_METER_BUCKETS;
2906 }
2907
2908
2909 /*
2910 * Process all "<=" type of bw_meter that should be processed now,
2911 * and for each entry prepare an upcall if necessary. Each processed
2912 * entry is rescheduled again for the (periodic) processing.
2913 *
2914 * This is run periodically (once per second normally). On each round,
2915 * all the potentially matching entries are in the hash slot that we are
2916 * looking at.
2917 */
2918 static void
2919 bw_meter_process()
2920 {
2921 static uint32_t last_tv_sec; /* last time we processed this */
2922
2923 uint32_t loops;
2924 int i;
2925 struct timeval now, process_endtime;
2926
2927 GET_TIME(now);
2928 if (last_tv_sec == now.tv_sec)
2929 return; /* nothing to do */
2930
2931 loops = now.tv_sec - last_tv_sec;
2932 last_tv_sec = now.tv_sec;
2933 if (loops > BW_METER_BUCKETS)
2934 loops = BW_METER_BUCKETS;
2935
2936 MFC_LOCK();
2937 /*
2938 * Process all bins of bw_meter entries from the one after the last
2939 * processed to the current one. On entry, i points to the last bucket
2940 * visited, so we need to increment i at the beginning of the loop.
2941 */
2942 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
2943 struct bw_meter *x, *tmp_list;
2944
2945 if (++i >= BW_METER_BUCKETS)
2946 i = 0;
2947
2948 /* Disconnect the list of bw_meter entries from the bin */
2949 tmp_list = bw_meter_timers[i];
2950 bw_meter_timers[i] = NULL;
2951
2952 /* Process the list of bw_meter entries */
2953 while (tmp_list != NULL) {
2954 x = tmp_list;
2955 tmp_list = tmp_list->bm_time_next;
2956
2957 /* Test if the time interval is over */
2958 process_endtime = x->bm_start_time;
2959 BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
2960 if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
2961 /* Not yet: reschedule, but don't reset */
2962 int time_hash;
2963
2964 BW_METER_TIMEHASH(x, time_hash);
2965 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
2966 /*
2967 * XXX: somehow the bin processing is a bit ahead of time.
2968 * Put the entry in the next bin.
2969 */
2970 if (++time_hash >= BW_METER_BUCKETS)
2971 time_hash = 0;
2972 }
2973 x->bm_time_next = bw_meter_timers[time_hash];
2974 bw_meter_timers[time_hash] = x;
2975 x->bm_time_hash = time_hash;
2976
2977 continue;
2978 }
2979
2980 /*
2981 * Test if we should deliver an upcall
2982 */
2983 if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2984 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2985 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2986 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2987 /* Prepare an upcall for delivery */
2988 bw_meter_prepare_upcall(x, &now);
2989 }
2990
2991 /*
2992 * Reschedule for next processing
2993 */
2994 schedule_bw_meter(x, &now);
2995 }
2996 }
2997
2998 /* Send all upcalls that are pending delivery */
2999 bw_upcalls_send();
3000
3001 MFC_UNLOCK();
3002 }
3003
3004 /*
3005 * A periodic function for sending all upcalls that are pending delivery
3006 */
3007 static void
3008 expire_bw_upcalls_send(void *unused)
3009 {
3010 MFC_LOCK();
3011 bw_upcalls_send();
3012 MFC_UNLOCK();
3013
3014 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
3015 expire_bw_upcalls_send, NULL);
3016 }
3017
3018 /*
3019 * A periodic function for periodic scanning of the multicast forwarding
3020 * table for processing all "<=" bw_meter entries.
3021 */
3022 static void
3023 expire_bw_meter_process(void *unused)
3024 {
3025 if (mrt_api_config & MRT_MFC_BW_UPCALL)
3026 bw_meter_process();
3027
3028 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL);
3029 }
3030
3031 /*
3032 * End of bandwidth monitoring code
3033 */
3034
3035 #ifdef PIM
3036 /*
3037 * Send the packet up to the user daemon, or eventually do kernel encapsulation
3038 *
3039 */
3040 static int
3041 pim_register_send(struct ip *ip, struct vif *vifp,
3042 struct mbuf *m, struct mfc *rt)
3043 {
3044 struct mbuf *mb_copy, *mm;
3045
3046 if (mrtdebug & DEBUG_PIM)
3047 log(LOG_DEBUG, "pim_register_send: ");
3048
3049 mb_copy = pim_register_prepare(ip, m);
3050 if (mb_copy == NULL)
3051 return ENOBUFS;
3052
3053 /*
3054 * Send all the fragments. Note that the mbuf for each fragment
3055 * is freed by the sending machinery.
3056 */
3057 for (mm = mb_copy; mm; mm = mb_copy) {
3058 mb_copy = mm->m_nextpkt;
3059 mm->m_nextpkt = 0;
3060 mm = m_pullup(mm, sizeof(struct ip));
3061 if (mm != NULL) {
3062 ip = mtod(mm, struct ip *);
3063 if ((mrt_api_config & MRT_MFC_RP) &&
3064 (rt->mfc_rp.s_addr != INADDR_ANY)) {
3065 pim_register_send_rp(ip, vifp, mm, rt);
3066 } else {
3067 pim_register_send_upcall(ip, vifp, mm, rt);
3068 }
3069 }
3070 }
3071
3072 return 0;
3073 }
3074
3075 /*
3076 * Return a copy of the data packet that is ready for PIM Register
3077 * encapsulation.
3078 * XXX: Note that in the returned copy the IP header is a valid one.
3079 */
3080 static struct mbuf *
3081 pim_register_prepare(struct ip *ip, struct mbuf *m)
3082 {
3083 struct mbuf *mb_copy = NULL;
3084 int mtu;
3085
3086 /* Take care of delayed checksums */
3087 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3088 in_delayed_cksum(m);
3089 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3090 }
3091
3092 /*
3093 * Copy the old packet & pullup its IP header into the
3094 * new mbuf so we can modify it.
3095 */
3096 mb_copy = m_copypacket(m, M_DONTWAIT);
3097 if (mb_copy == NULL)
3098 return NULL;
3099 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
3100 if (mb_copy == NULL)
3101 return NULL;
3102
3103 /* take care of the TTL */
3104 ip = mtod(mb_copy, struct ip *);
3105 --ip->ip_ttl;
3106
3107 /* Compute the MTU after the PIM Register encapsulation */
3108 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
3109
3110 if (ip->ip_len <= mtu) {
3111 /* Turn the IP header into a valid one */
3112 ip->ip_len = htons(ip->ip_len);
3113 ip->ip_off = htons(ip->ip_off);
3114 ip->ip_sum = 0;
3115 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
3116 } else {
3117 /* Fragment the packet */
3118 if (ip_fragment(ip, &mb_copy, mtu, 0, CSUM_DELAY_IP) != 0) {
3119 m_freem(mb_copy);
3120 return NULL;
3121 }
3122 }
3123 return mb_copy;
3124 }
3125
3126 /*
3127 * Send an upcall with the data packet to the user-level process.
3128 */
3129 static int
3130 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
3131 struct mbuf *mb_copy, struct mfc *rt)
3132 {
3133 struct mbuf *mb_first;
3134 int len = ntohs(ip->ip_len);
3135 struct igmpmsg *im;
3136 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
3137
3138 VIF_LOCK_ASSERT();
3139
3140 /*
3141 * Add a new mbuf with an upcall header
3142 */
3143 MGETHDR(mb_first, M_DONTWAIT, MT_HEADER);
3144 if (mb_first == NULL) {
3145 m_freem(mb_copy);
3146 return ENOBUFS;
3147 }
3148 mb_first->m_data += max_linkhdr;
3149 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
3150 mb_first->m_len = sizeof(struct igmpmsg);
3151 mb_first->m_next = mb_copy;
3152
3153 /* Send message to routing daemon */
3154 im = mtod(mb_first, struct igmpmsg *);
3155 im->im_msgtype = IGMPMSG_WHOLEPKT;
3156 im->im_mbz = 0;
3157 im->im_vif = vifp - viftable;
3158 im->im_src = ip->ip_src;
3159 im->im_dst = ip->ip_dst;
3160
3161 k_igmpsrc.sin_addr = ip->ip_src;
3162
3163 mrtstat.mrts_upcalls++;
3164
3165 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
3166 if (mrtdebug & DEBUG_PIM)
3167 log(LOG_WARNING,
3168 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
3169 ++mrtstat.mrts_upq_sockfull;
3170 return ENOBUFS;
3171 }
3172
3173 /* Keep statistics */
3174 pimstat.pims_snd_registers_msgs++;
3175 pimstat.pims_snd_registers_bytes += len;
3176
3177 return 0;
3178 }
3179
3180 /*
3181 * Encapsulate the data packet in PIM Register message and send it to the RP.
3182 */
3183 static int
3184 pim_register_send_rp(struct ip *ip, struct vif *vifp,
3185 struct mbuf *mb_copy, struct mfc *rt)
3186 {
3187 struct mbuf *mb_first;
3188 struct ip *ip_outer;
3189 struct pim_encap_pimhdr *pimhdr;
3190 int len = ntohs(ip->ip_len);
3191 vifi_t vifi = rt->mfc_parent;
3192
3193 VIF_LOCK_ASSERT();
3194
3195 if ((vifi >= numvifs) || (viftable[vifi].v_lcl_addr.s_addr == 0)) {
3196 m_freem(mb_copy);
3197 return EADDRNOTAVAIL; /* The iif vif is invalid */
3198 }
3199
3200 /*
3201 * Add a new mbuf with the encapsulating header
3202 */
3203 MGETHDR(mb_first, M_DONTWAIT, MT_HEADER);
3204 if (mb_first == NULL) {
3205 m_freem(mb_copy);
3206 return ENOBUFS;
3207 }
3208 mb_first->m_data += max_linkhdr;
3209 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
3210 mb_first->m_next = mb_copy;
3211
3212 mb_first->m_pkthdr.len = len + mb_first->m_len;
3213
3214 /*
3215 * Fill in the encapsulating IP and PIM header
3216 */
3217 ip_outer = mtod(mb_first, struct ip *);
3218 *ip_outer = pim_encap_iphdr;
3219 ip_outer->ip_id = ip_newid();
3220 ip_outer->ip_len = len + sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
3221 ip_outer->ip_src = viftable[vifi].v_lcl_addr;
3222 ip_outer->ip_dst = rt->mfc_rp;
3223 /*
3224 * Copy the inner header TOS to the outer header, and take care of the
3225 * IP_DF bit.
3226 */
3227 ip_outer->ip_tos = ip->ip_tos;
3228 if (ntohs(ip->ip_off) & IP_DF)
3229 ip_outer->ip_off |= IP_DF;
3230 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
3231 + sizeof(pim_encap_iphdr));
3232 *pimhdr = pim_encap_pimhdr;
3233 /* If the iif crosses a border, set the Border-bit */
3234 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
3235 pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
3236
3237 mb_first->m_data += sizeof(pim_encap_iphdr);
3238 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
3239 mb_first->m_data -= sizeof(pim_encap_iphdr);
3240
3241 if (vifp->v_rate_limit == 0)
3242 tbf_send_packet(vifp, mb_first);
3243 else
3244 tbf_control(vifp, mb_first, ip, ip_outer->ip_len);
3245
3246 /* Keep statistics */
3247 pimstat.pims_snd_registers_msgs++;
3248 pimstat.pims_snd_registers_bytes += len;
3249
3250 return 0;
3251 }
3252
3253 /*
3254 * PIM-SMv2 and PIM-DM messages processing.
3255 * Receives and verifies the PIM control messages, and passes them
3256 * up to the listening socket, using rip_input().
3257 * The only message with special processing is the PIM_REGISTER message
3258 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
3259 * is passed to if_simloop().
3260 */
3261 void
3262 pim_input(struct mbuf *m, int off)
3263 {
3264 struct ip *ip = mtod(m, struct ip *);
3265 struct pim *pim;
3266 int minlen;
3267 int datalen = ip->ip_len;
3268 int ip_tos;
3269 int iphlen = off;
3270
3271 /* Keep statistics */
3272 pimstat.pims_rcv_total_msgs++;
3273 pimstat.pims_rcv_total_bytes += datalen;
3274
3275 /*
3276 * Validate lengths
3277 */
3278 if (datalen < PIM_MINLEN) {
3279 pimstat.pims_rcv_tooshort++;
3280 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
3281 datalen, (u_long)ip->ip_src.s_addr);
3282 m_freem(m);
3283 return;
3284 }
3285
3286 /*
3287 * If the packet is at least as big as a REGISTER, go agead
3288 * and grab the PIM REGISTER header size, to avoid another
3289 * possible m_pullup() later.
3290 *
3291 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
3292 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
3293 */
3294 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
3295 /*
3296 * Get the IP and PIM headers in contiguous memory, and
3297 * possibly the PIM REGISTER header.
3298 */
3299 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
3300 (m = m_pullup(m, minlen)) == 0) {
3301 log(LOG_ERR, "pim_input: m_pullup failure\n");
3302 return;
3303 }
3304 /* m_pullup() may have given us a new mbuf so reset ip. */
3305 ip = mtod(m, struct ip *);
3306 ip_tos = ip->ip_tos;
3307
3308 /* adjust mbuf to point to the PIM header */
3309 m->m_data += iphlen;
3310 m->m_len -= iphlen;
3311 pim = mtod(m, struct pim *);
3312
3313 /*
3314 * Validate checksum. If PIM REGISTER, exclude the data packet.
3315 *
3316 * XXX: some older PIMv2 implementations don't make this distinction,
3317 * so for compatibility reason perform the checksum over part of the
3318 * message, and if error, then over the whole message.
3319 */
3320 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
3321 /* do nothing, checksum okay */
3322 } else if (in_cksum(m, datalen)) {
3323 pimstat.pims_rcv_badsum++;
3324 if (mrtdebug & DEBUG_PIM)
3325 log(LOG_DEBUG, "pim_input: invalid checksum");
3326 m_freem(m);
3327 return;
3328 }
3329
3330 /* PIM version check */
3331 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
3332 pimstat.pims_rcv_badversion++;
3333 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
3334 PIM_VT_V(pim->pim_vt), PIM_VERSION);
3335 m_freem(m);
3336 return;
3337 }
3338
3339 /* restore mbuf back to the outer IP */
3340 m->m_data -= iphlen;
3341 m->m_len += iphlen;
3342
3343 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
3344 /*
3345 * Since this is a REGISTER, we'll make a copy of the register
3346 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
3347 * routing daemon.
3348 */
3349 struct sockaddr_in dst = { sizeof(dst), AF_INET };
3350 struct mbuf *mcp;
3351 struct ip *encap_ip;
3352 u_int32_t *reghdr;
3353 struct ifnet *vifp;
3354
3355 VIF_LOCK();
3356 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
3357 VIF_UNLOCK();
3358 if (mrtdebug & DEBUG_PIM)
3359 log(LOG_DEBUG,
3360 "pim_input: register vif not set: %d\n", reg_vif_num);
3361 m_freem(m);
3362 return;
3363 }
3364 /* XXX need refcnt? */
3365 vifp = viftable[reg_vif_num].v_ifp;
3366 VIF_UNLOCK();
3367
3368 /*
3369 * Validate length
3370 */
3371 if (datalen < PIM_REG_MINLEN) {
3372 pimstat.pims_rcv_tooshort++;
3373 pimstat.pims_rcv_badregisters++;
3374 log(LOG_ERR,
3375 "pim_input: register packet size too small %d from %lx\n",
3376 datalen, (u_long)ip->ip_src.s_addr);
3377 m_freem(m);
3378 return;
3379 }
3380
3381 reghdr = (u_int32_t *)(pim + 1);
3382 encap_ip = (struct ip *)(reghdr + 1);
3383
3384 if (mrtdebug & DEBUG_PIM) {
3385 log(LOG_DEBUG,
3386 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
3387 (u_long)ntohl(encap_ip->ip_src.s_addr),
3388 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3389 ntohs(encap_ip->ip_len));
3390 }
3391
3392 /* verify the version number of the inner packet */
3393 if (encap_ip->ip_v != IPVERSION) {
3394 pimstat.pims_rcv_badregisters++;
3395 if (mrtdebug & DEBUG_PIM) {
3396 log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
3397 "of the inner packet\n", encap_ip->ip_v);
3398 }
3399 m_freem(m);
3400 return;
3401 }
3402
3403 /* verify the inner packet is destined to a mcast group */
3404 if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
3405 pimstat.pims_rcv_badregisters++;
3406 if (mrtdebug & DEBUG_PIM)
3407 log(LOG_DEBUG,
3408 "pim_input: inner packet of register is not "
3409 "multicast %lx\n",
3410 (u_long)ntohl(encap_ip->ip_dst.s_addr));
3411 m_freem(m);
3412 return;
3413 }
3414
3415 /* If a NULL_REGISTER, pass it to the daemon */
3416 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
3417 goto pim_input_to_daemon;
3418
3419 /*
3420 * Copy the TOS from the outer IP header to the inner IP header.
3421 */
3422 if (encap_ip->ip_tos != ip_tos) {
3423 /* Outer TOS -> inner TOS */
3424 encap_ip->ip_tos = ip_tos;
3425 /* Recompute the inner header checksum. Sigh... */
3426
3427 /* adjust mbuf to point to the inner IP header */
3428 m->m_data += (iphlen + PIM_MINLEN);
3429 m->m_len -= (iphlen + PIM_MINLEN);
3430
3431 encap_ip->ip_sum = 0;
3432 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
3433
3434 /* restore mbuf to point back to the outer IP header */
3435 m->m_data -= (iphlen + PIM_MINLEN);
3436 m->m_len += (iphlen + PIM_MINLEN);
3437 }
3438
3439 /*
3440 * Decapsulate the inner IP packet and loopback to forward it
3441 * as a normal multicast packet. Also, make a copy of the
3442 * outer_iphdr + pimhdr + reghdr + encap_iphdr
3443 * to pass to the daemon later, so it can take the appropriate
3444 * actions (e.g., send back PIM_REGISTER_STOP).
3445 * XXX: here m->m_data points to the outer IP header.
3446 */
3447 mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
3448 if (mcp == NULL) {
3449 log(LOG_ERR,
3450 "pim_input: pim register: could not copy register head\n");
3451 m_freem(m);
3452 return;
3453 }
3454
3455 /* Keep statistics */
3456 /* XXX: registers_bytes include only the encap. mcast pkt */
3457 pimstat.pims_rcv_registers_msgs++;
3458 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
3459
3460 /*
3461 * forward the inner ip packet; point m_data at the inner ip.
3462 */
3463 m_adj(m, iphlen + PIM_MINLEN);
3464
3465 if (mrtdebug & DEBUG_PIM) {
3466 log(LOG_DEBUG,
3467 "pim_input: forwarding decapsulated register: "
3468 "src %lx, dst %lx, vif %d\n",
3469 (u_long)ntohl(encap_ip->ip_src.s_addr),
3470 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3471 reg_vif_num);
3472 }
3473 /* NB: vifp was collected above; can it change on us? */
3474 if_simloop(vifp, m, dst.sin_family, 0);
3475
3476 /* prepare the register head to send to the mrouting daemon */
3477 m = mcp;
3478 }
3479
3480 pim_input_to_daemon:
3481 /*
3482 * Pass the PIM message up to the daemon; if it is a Register message,
3483 * pass the 'head' only up to the daemon. This includes the
3484 * outer IP header, PIM header, PIM-Register header and the
3485 * inner IP header.
3486 * XXX: the outer IP header pkt size of a Register is not adjust to
3487 * reflect the fact that the inner multicast data is truncated.
3488 */
3489 rip_input(m, iphlen);
3490
3491 return;
3492 }
3493 #endif /* PIM */
3494
3495 static int
3496 ip_mroute_modevent(module_t mod, int type, void *unused)
3497 {
3498 switch (type) {
3499 case MOD_LOAD:
3500 mtx_init(&mrouter_mtx, "mrouter initialization", NULL, MTX_DEF);
3501 MFC_LOCK_INIT();
3502 VIF_LOCK_INIT();
3503 ip_mrouter_reset();
3504 ip_mcast_src = X_ip_mcast_src;
3505 ip_mforward = X_ip_mforward;
3506 ip_mrouter_done = X_ip_mrouter_done;
3507 ip_mrouter_get = X_ip_mrouter_get;
3508 ip_mrouter_set = X_ip_mrouter_set;
3509 ip_rsvp_force_done = X_ip_rsvp_force_done;
3510 ip_rsvp_vif = X_ip_rsvp_vif;
3511 legal_vif_num = X_legal_vif_num;
3512 mrt_ioctl = X_mrt_ioctl;
3513 rsvp_input_p = X_rsvp_input;
3514 break;
3515
3516 case MOD_UNLOAD:
3517 /*
3518 * Typically module unload happens after the user-level
3519 * process has shutdown the kernel services (the check
3520 * below insures someone can't just yank the module out
3521 * from under a running process). But if the module is
3522 * just loaded and then unloaded w/o starting up a user
3523 * process we still need to cleanup.
3524 */
3525 if (ip_mrouter)
3526 return EINVAL;
3527
3528 X_ip_mrouter_done();
3529 ip_mcast_src = NULL;
3530 ip_mforward = NULL;
3531 ip_mrouter_done = NULL;
3532 ip_mrouter_get = NULL;
3533 ip_mrouter_set = NULL;
3534 ip_rsvp_force_done = NULL;
3535 ip_rsvp_vif = NULL;
3536 legal_vif_num = NULL;
3537 mrt_ioctl = NULL;
3538 rsvp_input_p = NULL;
3539 VIF_LOCK_DESTROY();
3540 MFC_LOCK_DESTROY();
3541 mtx_destroy(&mrouter_mtx);
3542 break;
3543 default:
3544 return EOPNOTSUPP;
3545 }
3546 return 0;
3547 }
3548
3549 static moduledata_t ip_mroutemod = {
3550 "ip_mroute",
3551 ip_mroute_modevent,
3552 0
3553 };
3554 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);
Cache object: 4704b7ca1dd98039f06d12fd6c058bd1
|