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