178b74c4186ab90540d552bb5d70340ee7b5fe7d
[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.5 2003/09/15 23:38:13 hsu 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 <net/if.h>
44 #include <net/netisr.h>
45 #include <net/route.h>
46 #include <netinet/in.h>
47 #include <netinet/igmp.h>
48 #include <netinet/in_systm.h>
49 #include <netinet/in_var.h>
50 #include <netinet/ip.h>
51 #include "ip_mroute.h"
52 #include <netinet/ip_var.h>
53 #ifdef PIM
54 #include <netinet/pim.h>
55 #include <netinet/pim_var.h>
56 #endif
57 #include <netinet/udp.h>
58 #include <machine/in_cksum.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                     multicast_decap_if[s].if_name = "mdecap";
777                     multicast_decap_if[s].if_unit = s;
778                 }
779             }
780             /*
781              * Set interface to fake encapsulator interface
782              */
783             ifp = &multicast_decap_if[vifcp->vifc_vifi];
784             /*
785              * Prepare cached route entry
786              */
787             bzero(&vifp->v_route, sizeof(vifp->v_route));
788         } else {
789             log(LOG_ERR, "source routed tunnels not supported\n");
790             return EOPNOTSUPP;
791         }
792 #ifdef PIM
793     } else if (vifcp->vifc_flags & VIFF_REGISTER) {
794         ifp = &multicast_register_if;
795         if (mrtdebug)
796             log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
797                     (void *)&multicast_register_if);
798         if (reg_vif_num == VIFI_INVALID) {
799             multicast_register_if.if_name = "register_vif";
800             multicast_register_if.if_unit = 0;
801             multicast_register_if.if_flags = IFF_LOOPBACK;
802             bzero(&vifp->v_route, sizeof(vifp->v_route));
803             reg_vif_num = vifcp->vifc_vifi;
804         }
805 #endif
806     } else {            /* Make sure the interface supports multicast */
807         if ((ifp->if_flags & IFF_MULTICAST) == 0)
808             return EOPNOTSUPP;
809
810         /* Enable promiscuous reception of all IP multicasts from the if */
811         s = splnet();
812         error = if_allmulti(ifp, 1);
813         splx(s);
814         if (error)
815             return error;
816     }
817
818     s = splnet();
819     /* define parameters for the tbf structure */
820     vifp->v_tbf = v_tbf;
821     GET_TIME(vifp->v_tbf->tbf_last_pkt_t);
822     vifp->v_tbf->tbf_n_tok = 0;
823     vifp->v_tbf->tbf_q_len = 0;
824     vifp->v_tbf->tbf_max_q_len = MAXQSIZE;
825     vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL;
826
827     vifp->v_flags     = vifcp->vifc_flags;
828     vifp->v_threshold = vifcp->vifc_threshold;
829     vifp->v_lcl_addr  = vifcp->vifc_lcl_addr;
830     vifp->v_rmt_addr  = vifcp->vifc_rmt_addr;
831     vifp->v_ifp       = ifp;
832     /* scaling up here allows division by 1024 in critical code */
833     vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000;
834     vifp->v_rsvp_on   = 0;
835     vifp->v_rsvpd     = NULL;
836     /* initialize per vif pkt counters */
837     vifp->v_pkt_in    = 0;
838     vifp->v_pkt_out   = 0;
839     vifp->v_bytes_in  = 0;
840     vifp->v_bytes_out = 0;
841     splx(s);
842
843     /* Adjust numvifs up if the vifi is higher than numvifs */
844     if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
845
846     if (mrtdebug)
847         log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
848             vifcp->vifc_vifi,
849             (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
850             (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
851             (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
852             vifcp->vifc_threshold,
853             vifcp->vifc_rate_limit);
854
855     return 0;
856 }
857
858 /*
859  * Delete a vif from the vif table
860  */
861 static int
862 del_vif(vifi_t vifi)
863 {
864     struct vif *vifp;
865     int s;
866
867     if (vifi >= numvifs)
868         return EINVAL;
869     vifp = &viftable[vifi];
870     if (vifp->v_lcl_addr.s_addr == INADDR_ANY)
871         return EADDRNOTAVAIL;
872
873     s = splnet();
874
875     if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER)))
876         if_allmulti(vifp->v_ifp, 0);
877
878     if (vifp == last_encap_vif) {
879         last_encap_vif = NULL;
880         last_encap_src = INADDR_ANY;
881     }
882
883     /*
884      * Free packets queued at the interface
885      */
886     while (vifp->v_tbf->tbf_q) {
887         struct mbuf *m = vifp->v_tbf->tbf_q;
888
889         vifp->v_tbf->tbf_q = m->m_act;
890         m_freem(m);
891     }
892
893 #ifdef PIM
894     if (vifp->v_flags & VIFF_REGISTER)
895         reg_vif_num = VIFI_INVALID;
896 #endif
897
898     bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf)));
899     bzero((caddr_t)vifp, sizeof (*vifp));
900
901     if (mrtdebug)
902         log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
903
904     /* Adjust numvifs down */
905     for (vifi = numvifs; vifi > 0; vifi--)
906         if (viftable[vifi-1].v_lcl_addr.s_addr != INADDR_ANY)
907             break;
908     numvifs = vifi;
909
910     splx(s);
911
912     return 0;
913 }
914
915 /*
916  * update an mfc entry without resetting counters and S,G addresses.
917  */
918 static void
919 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
920 {
921     int i;
922
923     rt->mfc_parent = mfccp->mfcc_parent;
924     for (i = 0; i < numvifs; i++) {
925         rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
926         rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
927             MRT_MFC_FLAGS_ALL;
928     }
929     /* set the RP address */
930     if (mrt_api_config & MRT_MFC_RP)
931         rt->mfc_rp = mfccp->mfcc_rp;
932     else
933         rt->mfc_rp.s_addr = INADDR_ANY;
934 }
935
936 /*
937  * fully initialize an mfc entry from the parameter.
938  */
939 static void
940 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
941 {
942     rt->mfc_origin     = mfccp->mfcc_origin;
943     rt->mfc_mcastgrp   = mfccp->mfcc_mcastgrp;
944
945     update_mfc_params(rt, mfccp);
946
947     /* initialize pkt counters per src-grp */
948     rt->mfc_pkt_cnt    = 0;
949     rt->mfc_byte_cnt   = 0;
950     rt->mfc_wrong_if   = 0;
951     rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
952 }
953
954
955 /*
956  * Add an mfc entry
957  */
958 static int
959 add_mfc(struct mfcctl2 *mfccp)
960 {
961     struct mfc *rt;
962     u_long hash;
963     struct rtdetq *rte;
964     u_short nstl;
965     int s;
966
967     rt = mfc_find(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
968
969     /* If an entry already exists, just update the fields */
970     if (rt) {
971         if (mrtdebug & DEBUG_MFC)
972             log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
973                 (u_long)ntohl(mfccp->mfcc_origin.s_addr),
974                 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
975                 mfccp->mfcc_parent);
976
977         s = splnet();
978         update_mfc_params(rt, mfccp);
979         splx(s);
980         return 0;
981     }
982
983     /*
984      * Find the entry for which the upcall was made and update
985      */
986     s = splnet();
987     hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
988     for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) {
989
990         if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
991                 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
992                 (rt->mfc_stall != NULL)) {
993
994             if (nstl++)
995                 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
996                     "multiple kernel entries",
997                     (u_long)ntohl(mfccp->mfcc_origin.s_addr),
998                     (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
999                     mfccp->mfcc_parent, (void *)rt->mfc_stall);
1000
1001             if (mrtdebug & DEBUG_MFC)
1002                 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
1003                     (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1004                     (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1005                     mfccp->mfcc_parent, (void *)rt->mfc_stall);
1006
1007             init_mfc_params(rt, mfccp);
1008
1009             rt->mfc_expire = 0; /* Don't clean this guy up */
1010             nexpire[hash]--;
1011
1012             /* free packets Qed at the end of this entry */
1013             for (rte = rt->mfc_stall; rte != NULL; ) {
1014                 struct rtdetq *n = rte->next;
1015
1016                 ip_mdq(rte->m, rte->ifp, rt, -1);
1017                 m_freem(rte->m);
1018                 free(rte, M_MRTABLE);
1019                 rte = n;
1020             }
1021             rt->mfc_stall = NULL;
1022         }
1023     }
1024
1025     /*
1026      * It is possible that an entry is being inserted without an upcall
1027      */
1028     if (nstl == 0) {
1029         if (mrtdebug & DEBUG_MFC)
1030             log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
1031                 hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
1032                 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
1033                 mfccp->mfcc_parent);
1034
1035         for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) {
1036             if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
1037                     (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
1038                 init_mfc_params(rt, mfccp);
1039                 if (rt->mfc_expire)
1040                     nexpire[hash]--;
1041                 rt->mfc_expire = 0;
1042                 break; /* XXX */
1043             }
1044         }
1045         if (rt == NULL) {               /* no upcall, so make a new entry */
1046             rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1047             if (rt == NULL) {
1048                 splx(s);
1049                 return ENOBUFS;
1050             }
1051
1052             init_mfc_params(rt, mfccp);
1053             rt->mfc_expire     = 0;
1054             rt->mfc_stall      = NULL;
1055
1056             rt->mfc_bw_meter = NULL;
1057             /* insert new entry at head of hash chain */
1058             rt->mfc_next = mfctable[hash];
1059             mfctable[hash] = rt;
1060         }
1061     }
1062     splx(s);
1063     return 0;
1064 }
1065
1066 /*
1067  * Delete an mfc entry
1068  */
1069 static int
1070 del_mfc(struct mfcctl2 *mfccp)
1071 {
1072     struct in_addr      origin;
1073     struct in_addr      mcastgrp;
1074     struct mfc          *rt;
1075     struct mfc          **nptr;
1076     u_long              hash;
1077     int s;
1078     struct bw_meter     *list;
1079
1080     origin = mfccp->mfcc_origin;
1081     mcastgrp = mfccp->mfcc_mcastgrp;
1082
1083     if (mrtdebug & DEBUG_MFC)
1084         log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1085             (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1086
1087     s = splnet();
1088
1089     hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1090     for (nptr = &mfctable[hash]; (rt = *nptr) != NULL; nptr = &rt->mfc_next)
1091         if (origin.s_addr == rt->mfc_origin.s_addr &&
1092                 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1093                 rt->mfc_stall == NULL)
1094             break;
1095     if (rt == NULL) {
1096         splx(s);
1097         return EADDRNOTAVAIL;
1098     }
1099
1100     *nptr = rt->mfc_next;
1101
1102     /*
1103      * free the bw_meter entries
1104      */
1105     list = rt->mfc_bw_meter;
1106     rt->mfc_bw_meter = NULL;
1107
1108     free(rt, M_MRTABLE);
1109
1110     splx(s);
1111
1112     free_bw_list(list);
1113
1114     return 0;
1115 }
1116
1117 /*
1118  * Send a message to mrouted on the multicast routing socket
1119  */
1120 static int
1121 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src)
1122 {
1123     if (s) {
1124         if (sbappendaddr(&s->so_rcv, (struct sockaddr *)src, mm, NULL) != 0) {
1125             sorwakeup(s);
1126             return 0;
1127         }
1128     }
1129     m_freem(mm);
1130     return -1;
1131 }
1132
1133 /*
1134  * IP multicast forwarding function. This function assumes that the packet
1135  * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1136  * pointed to by "ifp", and the packet is to be relayed to other networks
1137  * that have members of the packet's destination IP multicast group.
1138  *
1139  * The packet is returned unscathed to the caller, unless it is
1140  * erroneous, in which case a non-zero return value tells the caller to
1141  * discard it.
1142  */
1143
1144 #define TUNNEL_LEN  12  /* # bytes of IP option for tunnel encapsulation  */
1145
1146 static int
1147 X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m,
1148     struct ip_moptions *imo)
1149 {
1150     struct mfc *rt;
1151     int s;
1152     vifi_t vifi;
1153
1154     if (mrtdebug & DEBUG_FORWARD)
1155         log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1156             (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1157             (void *)ifp);
1158
1159     if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 ||
1160                 ((u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1161         /*
1162          * Packet arrived via a physical interface or
1163          * an encapsulated tunnel or a register_vif.
1164          */
1165     } else {
1166         /*
1167          * Packet arrived through a source-route tunnel.
1168          * Source-route tunnels are no longer supported.
1169          */
1170         static int last_log;
1171         if (last_log != time_second) {
1172             last_log = time_second;
1173             log(LOG_ERR,
1174                 "ip_mforward: received source-routed packet from %lx\n",
1175                 (u_long)ntohl(ip->ip_src.s_addr));
1176         }
1177         return 1;
1178     }
1179
1180     if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1181         if (ip->ip_ttl < 255)
1182             ip->ip_ttl++;       /* compensate for -1 in *_send routines */
1183         if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1184             struct vif *vifp = viftable + vifi;
1185
1186             printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s%d)\n",
1187                 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr),
1188                 vifi,
1189                 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1190                 vifp->v_ifp->if_name, vifp->v_ifp->if_unit);
1191         }
1192         return ip_mdq(m, ifp, NULL, vifi);
1193     }
1194     if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1195         printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1196             (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr));
1197         if (!imo)
1198             printf("In fact, no options were specified at all\n");
1199     }
1200
1201     /*
1202      * Don't forward a packet with time-to-live of zero or one,
1203      * or a packet destined to a local-only group.
1204      */
1205     if (ip->ip_ttl <= 1 || ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP)
1206         return 0;
1207
1208     /*
1209      * Determine forwarding vifs from the forwarding cache table
1210      */
1211     s = splnet();
1212     ++mrtstat.mrts_mfc_lookups;
1213     rt = mfc_find(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1214
1215     /* Entry exists, so forward if necessary */
1216     if (rt != NULL) {
1217         splx(s);
1218         return ip_mdq(m, ifp, rt, -1);
1219     } else {
1220         /*
1221          * If we don't have a route for packet's origin,
1222          * Make a copy of the packet & send message to routing daemon
1223          */
1224
1225         struct mbuf *mb0;
1226         struct rtdetq *rte;
1227         u_long hash;
1228         int hlen = ip->ip_hl << 2;
1229
1230         ++mrtstat.mrts_mfc_misses;
1231
1232         mrtstat.mrts_no_route++;
1233         if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1234             log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n",
1235                 (u_long)ntohl(ip->ip_src.s_addr),
1236                 (u_long)ntohl(ip->ip_dst.s_addr));
1237
1238         /*
1239          * Allocate mbufs early so that we don't do extra work if we are
1240          * just going to fail anyway.  Make sure to pullup the header so
1241          * that other people can't step on it.
1242          */
1243         rte = (struct rtdetq *)malloc((sizeof *rte), M_MRTABLE, M_NOWAIT);
1244         if (rte == NULL) {
1245             splx(s);
1246             return ENOBUFS;
1247         }
1248         mb0 = m_copypacket(m, M_DONTWAIT);
1249         if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen))
1250             mb0 = m_pullup(mb0, hlen);
1251         if (mb0 == NULL) {
1252             free(rte, M_MRTABLE);
1253             splx(s);
1254             return ENOBUFS;
1255         }
1256
1257         /* is there an upcall waiting for this flow ? */
1258         hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr);
1259         for (rt = mfctable[hash]; rt; rt = rt->mfc_next) {
1260             if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) &&
1261                     (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) &&
1262                     (rt->mfc_stall != NULL))
1263                 break;
1264         }
1265
1266         if (rt == NULL) {
1267             int i;
1268             struct igmpmsg *im;
1269             struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
1270             struct mbuf *mm;
1271
1272             /*
1273              * Locate the vifi for the incoming interface for this packet.
1274              * If none found, drop packet.
1275              */
1276             for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++)
1277                 ;
1278             if (vifi >= numvifs)        /* vif not found, drop packet */
1279                 goto non_fatal;
1280
1281             /* no upcall, so make a new entry */
1282             rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
1283             if (rt == NULL)
1284                 goto fail;
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, NULL);
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 = (struct bw_meter *)malloc(sizeof(*x), M_BWMETER, M_NOWAIT);
2259     if (x == NULL)
2260         return ENOBUFS;
2261     
2262     /* Set the new bw_meter entry */
2263     x->bm_threshold.b_time = req->bu_threshold.b_time;
2264     GET_TIME(now);
2265     x->bm_start_time = now;
2266     x->bm_threshold.b_packets = req->bu_threshold.b_packets;
2267     x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
2268     x->bm_measured.b_packets = 0;
2269     x->bm_measured.b_bytes = 0;
2270     x->bm_flags = flags;
2271     x->bm_time_next = NULL;
2272     x->bm_time_hash = BW_METER_BUCKETS;
2273     
2274     /* Add the new bw_meter entry to the front of entries for this MFC */
2275     s = splnet();
2276     x->bm_mfc = mfc;
2277     x->bm_mfc_next = mfc->mfc_bw_meter;
2278     mfc->mfc_bw_meter = x;
2279     schedule_bw_meter(x, &now);
2280     splx(s);
2281     
2282     return 0;
2283 }
2284
2285 static void
2286 free_bw_list(struct bw_meter *list)
2287 {
2288     while (list != NULL) {
2289         struct bw_meter *x = list;
2290
2291         list = list->bm_mfc_next;
2292         unschedule_bw_meter(x);
2293         free(x, M_BWMETER);
2294     }
2295 }
2296
2297 /*
2298  * Delete one or multiple bw_meter entries
2299  */
2300 static int
2301 del_bw_upcall(struct bw_upcall *req)
2302 {
2303     struct mfc *mfc;
2304     struct bw_meter *x;
2305     int s;
2306     
2307     if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
2308         return EOPNOTSUPP;
2309     
2310     s = splnet();
2311     /* Find the corresponding MFC entry */
2312     mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr);
2313     if (mfc == NULL) {
2314         splx(s);
2315         return EADDRNOTAVAIL;
2316     } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
2317         /*
2318          * Delete all bw_meter entries for this mfc
2319          */
2320         struct bw_meter *list;
2321         
2322         list = mfc->mfc_bw_meter;
2323         mfc->mfc_bw_meter = NULL;
2324         splx(s);
2325         free_bw_list(list);
2326         return 0;
2327     } else {                    /* Delete a single bw_meter entry */
2328         struct bw_meter *prev;
2329         uint32_t flags = 0;
2330
2331         flags = compute_bw_meter_flags(req);
2332
2333         /* Find the bw_meter entry to delete */
2334         for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
2335              x = x->bm_mfc_next) {
2336             if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
2337                                &req->bu_threshold.b_time, ==)) &&
2338                 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
2339                 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
2340                 (x->bm_flags & BW_METER_USER_FLAGS) == flags)
2341                 break;
2342         }
2343         if (x != NULL) { /* Delete entry from the list for this MFC */
2344             if (prev != NULL)
2345                 prev->bm_mfc_next = x->bm_mfc_next;     /* remove from middle*/
2346             else
2347                 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
2348             splx(s);
2349
2350             unschedule_bw_meter(x);
2351             /* Free the bw_meter entry */
2352             free(x, M_BWMETER);
2353             return 0;
2354         } else {
2355             splx(s);
2356             return EINVAL;
2357         }
2358     }
2359     /* NOTREACHED */
2360 }
2361
2362 /*
2363  * Perform bandwidth measurement processing that may result in an upcall
2364  */
2365 static void
2366 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
2367 {
2368     struct timeval delta;
2369     int s;
2370     
2371     s = splnet();
2372     delta = *nowp;
2373     BW_TIMEVALDECR(&delta, &x->bm_start_time);
2374     
2375     if (x->bm_flags & BW_METER_GEQ) {
2376         /*
2377          * Processing for ">=" type of bw_meter entry
2378          */
2379         if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2380             /* Reset the bw_meter entry */
2381             x->bm_start_time = *nowp;
2382             x->bm_measured.b_packets = 0;
2383             x->bm_measured.b_bytes = 0;
2384             x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2385         }
2386         
2387         /* Record that a packet is received */
2388         x->bm_measured.b_packets++;
2389         x->bm_measured.b_bytes += plen;
2390         
2391         /*
2392          * Test if we should deliver an upcall
2393          */
2394         if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {       
2395             if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2396                  (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
2397                 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2398                  (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
2399                 /* Prepare an upcall for delivery */
2400                 bw_meter_prepare_upcall(x, nowp);
2401                 x->bm_flags |= BW_METER_UPCALL_DELIVERED;
2402             }
2403         }
2404     } else if (x->bm_flags & BW_METER_LEQ) {
2405         /*
2406          * Processing for "<=" type of bw_meter entry
2407          */
2408         if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
2409             /*
2410              * We are behind time with the multicast forwarding table
2411              * scanning for "<=" type of bw_meter entries, so test now
2412              * if we should deliver an upcall.
2413              */
2414             if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2415                  (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2416                 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2417                  (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2418                 /* Prepare an upcall for delivery */
2419                 bw_meter_prepare_upcall(x, nowp);
2420             }
2421             /* Reschedule the bw_meter entry */
2422             unschedule_bw_meter(x);
2423             schedule_bw_meter(x, nowp);
2424         }
2425         
2426         /* Record that a packet is received */
2427         x->bm_measured.b_packets++;
2428         x->bm_measured.b_bytes += plen;
2429         
2430         /*
2431          * Test if we should restart the measuring interval
2432          */
2433         if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
2434              x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
2435             (x->bm_flags & BW_METER_UNIT_BYTES &&
2436              x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
2437             /* Don't restart the measuring interval */
2438         } else {
2439             /* Do restart the measuring interval */
2440             /*
2441              * XXX: note that we don't unschedule and schedule, because this
2442              * might be too much overhead per packet. Instead, when we process
2443              * all entries for a given timer hash bin, we check whether it is
2444              * really a timeout. If not, we reschedule at that time.
2445              */
2446             x->bm_start_time = *nowp;
2447             x->bm_measured.b_packets = 0;
2448             x->bm_measured.b_bytes = 0;
2449             x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2450         }
2451     }
2452     splx(s);
2453 }
2454
2455 /*
2456  * Prepare a bandwidth-related upcall
2457  */
2458 static void
2459 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
2460 {
2461     struct timeval delta;
2462     struct bw_upcall *u;
2463     int s;
2464     
2465     s = splnet();
2466     
2467     /*
2468      * Compute the measured time interval 
2469      */
2470     delta = *nowp;
2471     BW_TIMEVALDECR(&delta, &x->bm_start_time);
2472     
2473     /*
2474      * If there are too many pending upcalls, deliver them now
2475      */
2476     if (bw_upcalls_n >= BW_UPCALLS_MAX)
2477         bw_upcalls_send();
2478     
2479     /*
2480      * Set the bw_upcall entry
2481      */
2482     u = &bw_upcalls[bw_upcalls_n++];
2483     u->bu_src = x->bm_mfc->mfc_origin;
2484     u->bu_dst = x->bm_mfc->mfc_mcastgrp;
2485     u->bu_threshold.b_time = x->bm_threshold.b_time;
2486     u->bu_threshold.b_packets = x->bm_threshold.b_packets;
2487     u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
2488     u->bu_measured.b_time = delta;
2489     u->bu_measured.b_packets = x->bm_measured.b_packets;
2490     u->bu_measured.b_bytes = x->bm_measured.b_bytes;
2491     u->bu_flags = 0;
2492     if (x->bm_flags & BW_METER_UNIT_PACKETS)
2493         u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
2494     if (x->bm_flags & BW_METER_UNIT_BYTES)
2495         u->bu_flags |= BW_UPCALL_UNIT_BYTES;
2496     if (x->bm_flags & BW_METER_GEQ)
2497         u->bu_flags |= BW_UPCALL_GEQ;
2498     if (x->bm_flags & BW_METER_LEQ)
2499         u->bu_flags |= BW_UPCALL_LEQ;
2500     
2501     splx(s);
2502 }
2503
2504 /*
2505  * Send the pending bandwidth-related upcalls
2506  */
2507 static void
2508 bw_upcalls_send(void)
2509 {
2510     struct mbuf *m;
2511     int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
2512     struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2513     static struct igmpmsg igmpmsg = { 0,                /* unused1 */
2514                                       0,                /* unused2 */
2515                                       IGMPMSG_BW_UPCALL,/* im_msgtype */
2516                                       0,                /* im_mbz  */
2517                                       0,                /* im_vif  */
2518                                       0,                /* unused3 */
2519                                       { 0 },            /* im_src  */
2520                                       { 0 } };          /* im_dst  */
2521     
2522     if (bw_upcalls_n == 0)
2523         return;                 /* No pending upcalls */
2524
2525     bw_upcalls_n = 0;
2526     
2527     /*
2528      * Allocate a new mbuf, initialize it with the header and
2529      * the payload for the pending calls.
2530      */
2531     MGETHDR(m, M_DONTWAIT, MT_HEADER);
2532     if (m == NULL) {
2533         log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
2534         return;
2535     }
2536     
2537     m->m_len = m->m_pkthdr.len = 0;
2538     m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
2539     m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
2540     
2541     /*
2542      * Send the upcalls
2543      * XXX do we need to set the address in k_igmpsrc ?
2544      */
2545     mrtstat.mrts_upcalls++;
2546     if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
2547         log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
2548         ++mrtstat.mrts_upq_sockfull;
2549     }
2550 }
2551
2552 /*
2553  * Compute the timeout hash value for the bw_meter entries
2554  */
2555 #define BW_METER_TIMEHASH(bw_meter, hash)                               \
2556     do {                                                                \
2557         struct timeval next_timeval = (bw_meter)->bm_start_time;        \
2558                                                                         \
2559         BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2560         (hash) = next_timeval.tv_sec;                                   \
2561         if (next_timeval.tv_usec)                                       \
2562             (hash)++; /* XXX: make sure we don't timeout early */       \
2563         (hash) %= BW_METER_BUCKETS;                                     \
2564     } while (0)
2565
2566 /*
2567  * Schedule a timer to process periodically bw_meter entry of type "<="
2568  * by linking the entry in the proper hash bucket.
2569  */
2570 static void
2571 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
2572 {
2573     int time_hash, s;
2574     
2575     if (!(x->bm_flags & BW_METER_LEQ))
2576         return;         /* XXX: we schedule timers only for "<=" entries */
2577     
2578     /*
2579      * Reset the bw_meter entry
2580      */
2581     s = splnet();
2582     x->bm_start_time = *nowp;
2583     x->bm_measured.b_packets = 0;
2584     x->bm_measured.b_bytes = 0;
2585     x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
2586     splx(s);
2587     
2588     /*
2589      * Compute the timeout hash value and insert the entry
2590      */
2591     BW_METER_TIMEHASH(x, time_hash);
2592     x->bm_time_next = bw_meter_timers[time_hash];
2593     bw_meter_timers[time_hash] = x;
2594     x->bm_time_hash = time_hash;
2595 }
2596
2597 /*
2598  * Unschedule the periodic timer that processes bw_meter entry of type "<="
2599  * by removing the entry from the proper hash bucket.
2600  */
2601 static void
2602 unschedule_bw_meter(struct bw_meter *x)
2603 {
2604     int time_hash;
2605     struct bw_meter *prev, *tmp;
2606     
2607     if (!(x->bm_flags & BW_METER_LEQ))
2608         return;         /* XXX: we schedule timers only for "<=" entries */
2609     
2610     /*
2611      * Compute the timeout hash value and delete the entry
2612      */
2613     time_hash = x->bm_time_hash;
2614     if (time_hash >= BW_METER_BUCKETS)
2615         return;         /* Entry was not scheduled */
2616     
2617     for (prev = NULL, tmp = bw_meter_timers[time_hash];
2618              tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
2619         if (tmp == x)
2620             break;
2621     
2622     if (tmp == NULL)
2623         panic("unschedule_bw_meter: bw_meter entry not found");
2624     
2625     if (prev != NULL)
2626         prev->bm_time_next = x->bm_time_next;
2627     else
2628         bw_meter_timers[time_hash] = x->bm_time_next;
2629     
2630     x->bm_time_next = NULL;
2631     x->bm_time_hash = BW_METER_BUCKETS;
2632 }
2633
2634
2635 /*
2636  * Process all "<=" type of bw_meter that should be processed now,
2637  * and for each entry prepare an upcall if necessary. Each processed
2638  * entry is rescheduled again for the (periodic) processing.
2639  *
2640  * This is run periodically (once per second normally). On each round,
2641  * all the potentially matching entries are in the hash slot that we are
2642  * looking at.
2643  */
2644 static void
2645 bw_meter_process()
2646 {
2647     static uint32_t last_tv_sec;        /* last time we processed this */
2648
2649     uint32_t loops;
2650     int i, s;
2651     struct timeval now, process_endtime;
2652     
2653     GET_TIME(now);
2654     if (last_tv_sec == now.tv_sec)
2655         return;         /* nothing to do */
2656
2657     s = splnet();
2658     loops = now.tv_sec - last_tv_sec;
2659     last_tv_sec = now.tv_sec;
2660     if (loops > BW_METER_BUCKETS)
2661         loops = BW_METER_BUCKETS;
2662
2663     /*
2664      * Process all bins of bw_meter entries from the one after the last
2665      * processed to the current one. On entry, i points to the last bucket
2666      * visited, so we need to increment i at the beginning of the loop.
2667      */
2668     for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
2669         struct bw_meter *x, *tmp_list;
2670         
2671         if (++i >= BW_METER_BUCKETS)
2672             i = 0;
2673         
2674         /* Disconnect the list of bw_meter entries from the bin */
2675         tmp_list = bw_meter_timers[i];
2676         bw_meter_timers[i] = NULL;
2677         
2678         /* Process the list of bw_meter entries */
2679         while (tmp_list != NULL) {
2680             x = tmp_list;
2681             tmp_list = tmp_list->bm_time_next;
2682             
2683             /* Test if the time interval is over */
2684             process_endtime = x->bm_start_time;
2685             BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
2686             if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
2687                 /* Not yet: reschedule, but don't reset */
2688                 int time_hash;
2689                 
2690                 BW_METER_TIMEHASH(x, time_hash);
2691                 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
2692                     /*
2693                      * XXX: somehow the bin processing is a bit ahead of time.
2694                      * Put the entry in the next bin.
2695                      */
2696                     if (++time_hash >= BW_METER_BUCKETS)
2697                         time_hash = 0;
2698                 }
2699                 x->bm_time_next = bw_meter_timers[time_hash];
2700                 bw_meter_timers[time_hash] = x;
2701                 x->bm_time_hash = time_hash;
2702                 
2703                 continue;
2704             }
2705             
2706             /*
2707              * Test if we should deliver an upcall
2708              */
2709             if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
2710                  (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
2711                 ((x->bm_flags & BW_METER_UNIT_BYTES) &&
2712                  (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
2713                 /* Prepare an upcall for delivery */
2714                 bw_meter_prepare_upcall(x, &now);
2715             }
2716             
2717             /*
2718              * Reschedule for next processing
2719              */
2720             schedule_bw_meter(x, &now);
2721         }
2722     }
2723     splx(s);
2724     
2725     /* Send all upcalls that are pending delivery */
2726     bw_upcalls_send();
2727 }
2728
2729 /*
2730  * A periodic function for sending all upcalls that are pending delivery
2731  */
2732 static void
2733 expire_bw_upcalls_send(void *unused)
2734 {
2735     bw_upcalls_send();
2736     
2737     bw_upcalls_ch = timeout(expire_bw_upcalls_send, NULL, BW_UPCALLS_PERIOD);
2738 }
2739
2740 /*
2741  * A periodic function for periodic scanning of the multicast forwarding
2742  * table for processing all "<=" bw_meter entries.
2743  */
2744 static void
2745 expire_bw_meter_process(void *unused)
2746 {
2747     if (mrt_api_config & MRT_MFC_BW_UPCALL)
2748         bw_meter_process();
2749     
2750     bw_meter_ch = timeout(expire_bw_meter_process, NULL, BW_METER_PERIOD);
2751 }
2752
2753 /*
2754  * End of bandwidth monitoring code
2755  */
2756
2757 #ifdef PIM
2758 /*
2759  * Send the packet up to the user daemon, or eventually do kernel encapsulation
2760  *
2761  */
2762 static int
2763 pim_register_send(struct ip *ip, struct vif *vifp,
2764         struct mbuf *m, struct mfc *rt)
2765 {
2766     struct mbuf *mb_copy, *mm;
2767     
2768     if (mrtdebug & DEBUG_PIM)
2769         log(LOG_DEBUG, "pim_register_send: ");
2770     
2771     mb_copy = pim_register_prepare(ip, m);
2772     if (mb_copy == NULL)
2773         return ENOBUFS;
2774     
2775     /*
2776      * Send all the fragments. Note that the mbuf for each fragment
2777      * is freed by the sending machinery.
2778      */
2779     for (mm = mb_copy; mm; mm = mb_copy) {
2780         mb_copy = mm->m_nextpkt;
2781         mm->m_nextpkt = 0;
2782         mm = m_pullup(mm, sizeof(struct ip));
2783         if (mm != NULL) {
2784             ip = mtod(mm, struct ip *);
2785             if ((mrt_api_config & MRT_MFC_RP) &&
2786                 (rt->mfc_rp.s_addr != INADDR_ANY)) {
2787                 pim_register_send_rp(ip, vifp, mm, rt);
2788             } else {
2789                 pim_register_send_upcall(ip, vifp, mm, rt);
2790             }
2791         }
2792     }
2793     
2794     return 0;
2795 }
2796
2797 /*
2798  * Return a copy of the data packet that is ready for PIM Register
2799  * encapsulation.
2800  * XXX: Note that in the returned copy the IP header is a valid one.
2801  */
2802 static struct mbuf *
2803 pim_register_prepare(struct ip *ip, struct mbuf *m)
2804 {
2805     struct mbuf *mb_copy = NULL;
2806     int mtu;
2807     
2808     /* Take care of delayed checksums */
2809     if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2810         in_delayed_cksum(m);
2811         m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2812     }
2813
2814     /*
2815      * Copy the old packet & pullup its IP header into the
2816      * new mbuf so we can modify it.
2817      */
2818     mb_copy = m_copypacket(m, M_DONTWAIT);
2819     if (mb_copy == NULL)
2820         return NULL;
2821     mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
2822     if (mb_copy == NULL)
2823         return NULL;
2824     
2825     /* take care of the TTL */
2826     ip = mtod(mb_copy, struct ip *);
2827     --ip->ip_ttl;
2828     
2829     /* Compute the MTU after the PIM Register encapsulation */
2830     mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
2831     
2832     if (ip->ip_len <= mtu) {
2833         /* Turn the IP header into a valid one */
2834         ip->ip_len = htons(ip->ip_len);
2835         ip->ip_off = htons(ip->ip_off);
2836         ip->ip_sum = 0;
2837         ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
2838     } else {
2839         /* Fragment the packet */
2840         if (ip_fragment(ip, &mb_copy, mtu, 0, CSUM_DELAY_IP) != 0) {
2841             m_freem(mb_copy);
2842             return NULL;
2843         }
2844     }
2845     return mb_copy;
2846 }
2847
2848 /*
2849  * Send an upcall with the data packet to the user-level process.
2850  */
2851 static int
2852 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
2853         struct mbuf *mb_copy, struct mfc *rt)
2854 {
2855     struct mbuf *mb_first;
2856     int len = ntohs(ip->ip_len);
2857     struct igmpmsg *im;
2858     struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
2859     
2860     /*
2861      * Add a new mbuf with an upcall header
2862      */
2863     MGETHDR(mb_first, M_DONTWAIT, MT_HEADER);
2864     if (mb_first == NULL) {
2865         m_freem(mb_copy);
2866         return ENOBUFS;
2867     }
2868     mb_first->m_data += max_linkhdr;
2869     mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
2870     mb_first->m_len = sizeof(struct igmpmsg);
2871     mb_first->m_next = mb_copy;
2872     
2873     /* Send message to routing daemon */
2874     im = mtod(mb_first, struct igmpmsg *);
2875     im->im_msgtype      = IGMPMSG_WHOLEPKT;
2876     im->im_mbz          = 0;
2877     im->im_vif          = vifp - viftable;
2878     im->im_src          = ip->ip_src;
2879     im->im_dst          = ip->ip_dst;
2880     
2881     k_igmpsrc.sin_addr  = ip->ip_src;
2882     
2883     mrtstat.mrts_upcalls++;
2884     
2885     if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
2886         if (mrtdebug & DEBUG_PIM)
2887             log(LOG_WARNING,
2888                 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
2889         ++mrtstat.mrts_upq_sockfull;
2890         return ENOBUFS;
2891     }
2892     
2893     /* Keep statistics */
2894     pimstat.pims_snd_registers_msgs++;
2895     pimstat.pims_snd_registers_bytes += len;
2896     
2897     return 0;
2898 }
2899
2900 /*
2901  * Encapsulate the data packet in PIM Register message and send it to the RP.
2902  */
2903 static int
2904 pim_register_send_rp(struct ip *ip, struct vif *vifp,
2905         struct mbuf *mb_copy, struct mfc *rt)
2906 {
2907     struct mbuf *mb_first;
2908     struct ip *ip_outer;
2909     struct pim_encap_pimhdr *pimhdr;
2910     int len = ntohs(ip->ip_len);
2911     vifi_t vifi = rt->mfc_parent;
2912     
2913     if ((vifi >= numvifs) || (viftable[vifi].v_lcl_addr.s_addr == 0)) {
2914         m_freem(mb_copy);
2915         return EADDRNOTAVAIL;           /* The iif vif is invalid */
2916     }
2917     
2918     /*
2919      * Add a new mbuf with the encapsulating header
2920      */
2921     MGETHDR(mb_first, M_DONTWAIT, MT_HEADER);
2922     if (mb_first == NULL) {
2923         m_freem(mb_copy);
2924         return ENOBUFS;
2925     }
2926     mb_first->m_data += max_linkhdr;
2927     mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2928     mb_first->m_next = mb_copy;
2929
2930     mb_first->m_pkthdr.len = len + mb_first->m_len;
2931     
2932     /*
2933      * Fill in the encapsulating IP and PIM header
2934      */
2935     ip_outer = mtod(mb_first, struct ip *);
2936     *ip_outer = pim_encap_iphdr;
2937 #ifdef RANDOM_IP_ID
2938     ip_outer->ip_id = ip_randomid();
2939 #else
2940     ip_outer->ip_id = htons(ip_id++);
2941 #endif
2942     ip_outer->ip_len = len + sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
2943     ip_outer->ip_src = viftable[vifi].v_lcl_addr;
2944     ip_outer->ip_dst = rt->mfc_rp;
2945     /*
2946      * Copy the inner header TOS to the outer header, and take care of the
2947      * IP_DF bit.
2948      */
2949     ip_outer->ip_tos = ip->ip_tos;
2950     if (ntohs(ip->ip_off) & IP_DF)
2951         ip_outer->ip_off |= IP_DF;
2952     pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
2953                                          + sizeof(pim_encap_iphdr));
2954     *pimhdr = pim_encap_pimhdr;
2955     /* If the iif crosses a border, set the Border-bit */
2956     if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
2957         pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
2958     
2959     mb_first->m_data += sizeof(pim_encap_iphdr);
2960     pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
2961     mb_first->m_data -= sizeof(pim_encap_iphdr);
2962     
2963     if (vifp->v_rate_limit == 0)
2964         tbf_send_packet(vifp, mb_first);
2965     else
2966         tbf_control(vifp, mb_first, ip, ip_outer->ip_len);
2967     
2968     /* Keep statistics */
2969     pimstat.pims_snd_registers_msgs++;
2970     pimstat.pims_snd_registers_bytes += len;
2971     
2972     return 0;
2973 }
2974
2975 /*
2976  * PIM-SMv2 and PIM-DM messages processing.
2977  * Receives and verifies the PIM control messages, and passes them
2978  * up to the listening socket, using rip_input().
2979  * The only message with special processing is the PIM_REGISTER message
2980  * (used by PIM-SM): the PIM header is stripped off, and the inner packet
2981  * is passed to if_simloop().
2982  */
2983 void
2984 pim_input(struct mbuf *m, int off, int proto)
2985 {
2986     struct ip *ip = mtod(m, struct ip *);
2987     struct pim *pim;
2988     int minlen;
2989     int datalen = ip->ip_len;
2990     int ip_tos;
2991     int iphlen = off;
2992     
2993     /* Keep statistics */
2994     pimstat.pims_rcv_total_msgs++;
2995     pimstat.pims_rcv_total_bytes += datalen;
2996     
2997     /*
2998      * Validate lengths
2999      */
3000     if (datalen < PIM_MINLEN) {
3001         pimstat.pims_rcv_tooshort++;
3002         log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
3003             datalen, (u_long)ip->ip_src.s_addr);
3004         m_freem(m);
3005         return;
3006     }
3007     
3008     /*
3009      * If the packet is at least as big as a REGISTER, go agead
3010      * and grab the PIM REGISTER header size, to avoid another
3011      * possible m_pullup() later.
3012      * 
3013      * PIM_MINLEN       == pimhdr + u_int32_t == 4 + 4 = 8
3014      * PIM_REG_MINLEN   == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
3015      */
3016     minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
3017     /*
3018      * Get the IP and PIM headers in contiguous memory, and
3019      * possibly the PIM REGISTER header.
3020      */
3021     if ((m->m_flags & M_EXT || m->m_len < minlen) &&
3022         (m = m_pullup(m, minlen)) == 0) {
3023         log(LOG_ERR, "pim_input: m_pullup failure\n");
3024         return;
3025     }
3026     /* m_pullup() may have given us a new mbuf so reset ip. */
3027     ip = mtod(m, struct ip *);
3028     ip_tos = ip->ip_tos;
3029     
3030     /* adjust mbuf to point to the PIM header */
3031     m->m_data += iphlen;
3032     m->m_len  -= iphlen;
3033     pim = mtod(m, struct pim *);
3034     
3035     /*
3036      * Validate checksum. If PIM REGISTER, exclude the data packet.
3037      *
3038      * XXX: some older PIMv2 implementations don't make this distinction,
3039      * so for compatibility reason perform the checksum over part of the
3040      * message, and if error, then over the whole message.
3041      */
3042     if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
3043         /* do nothing, checksum okay */
3044     } else if (in_cksum(m, datalen)) {
3045         pimstat.pims_rcv_badsum++;
3046         if (mrtdebug & DEBUG_PIM)
3047             log(LOG_DEBUG, "pim_input: invalid checksum");
3048         m_freem(m);
3049         return;
3050     }
3051
3052     /* PIM version check */
3053     if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
3054         pimstat.pims_rcv_badversion++;
3055         log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
3056             PIM_VT_V(pim->pim_vt), PIM_VERSION);
3057         m_freem(m);
3058         return;
3059     }
3060     
3061     /* restore mbuf back to the outer IP */
3062     m->m_data -= iphlen;
3063     m->m_len  += iphlen;
3064     
3065     if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
3066         /*
3067          * Since this is a REGISTER, we'll make a copy of the register
3068          * headers ip + pim + u_int32 + encap_ip, to be passed up to the
3069          * routing daemon.
3070          */
3071         struct sockaddr_in dst = { sizeof(dst), AF_INET };
3072         struct mbuf *mcp;
3073         struct ip *encap_ip;
3074         u_int32_t *reghdr;
3075         
3076         if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
3077             if (mrtdebug & DEBUG_PIM)
3078                 log(LOG_DEBUG,
3079                     "pim_input: register vif not set: %d\n", reg_vif_num);
3080             m_freem(m);
3081             return;
3082         }
3083         
3084         /*
3085          * Validate length
3086          */
3087         if (datalen < PIM_REG_MINLEN) {
3088             pimstat.pims_rcv_tooshort++;
3089             pimstat.pims_rcv_badregisters++;
3090             log(LOG_ERR,
3091                 "pim_input: register packet size too small %d from %lx\n",
3092                 datalen, (u_long)ip->ip_src.s_addr);
3093             m_freem(m);
3094             return;
3095         }
3096         
3097         reghdr = (u_int32_t *)(pim + 1);
3098         encap_ip = (struct ip *)(reghdr + 1);
3099         
3100         if (mrtdebug & DEBUG_PIM) {
3101             log(LOG_DEBUG,
3102                 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
3103                 (u_long)ntohl(encap_ip->ip_src.s_addr),
3104                 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3105                 ntohs(encap_ip->ip_len));
3106         }
3107         
3108         /* verify the version number of the inner packet */
3109         if (encap_ip->ip_v != IPVERSION) {
3110             pimstat.pims_rcv_badregisters++;
3111             if (mrtdebug & DEBUG_PIM) {
3112                 log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
3113                     "of the inner packet\n", encap_ip->ip_v);
3114             }
3115             m_freem(m);
3116             return;
3117         }
3118         
3119         /* verify the inner packet is destined to a mcast group */
3120         if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) {
3121             pimstat.pims_rcv_badregisters++;
3122             if (mrtdebug & DEBUG_PIM)
3123                 log(LOG_DEBUG,
3124                     "pim_input: inner packet of register is not "
3125                     "multicast %lx\n",
3126                     (u_long)ntohl(encap_ip->ip_dst.s_addr));
3127             m_freem(m);
3128             return;
3129         }
3130         
3131         /*
3132          * Copy the TOS from the outer IP header to the inner IP header.
3133          */
3134         if (encap_ip->ip_tos != ip_tos) {
3135             /* Outer TOS -> inner TOS */
3136             encap_ip->ip_tos = ip_tos;
3137             /* Recompute the inner header checksum. Sigh... */
3138             
3139             /* adjust mbuf to point to the inner IP header */
3140             m->m_data += (iphlen + PIM_MINLEN);
3141             m->m_len  -= (iphlen + PIM_MINLEN);
3142             
3143             encap_ip->ip_sum = 0;
3144             encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
3145             
3146             /* restore mbuf to point back to the outer IP header */
3147             m->m_data -= (iphlen + PIM_MINLEN);
3148             m->m_len  += (iphlen + PIM_MINLEN);
3149         }
3150         
3151         /* If a NULL_REGISTER, pass it to the daemon */
3152         if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
3153             goto pim_input_to_daemon;
3154         
3155         /*
3156          * Decapsulate the inner IP packet and loopback to forward it
3157          * as a normal multicast packet. Also, make a copy of the 
3158          *     outer_iphdr + pimhdr + reghdr + encap_iphdr
3159          * to pass to the daemon later, so it can take the appropriate
3160          * actions (e.g., send back PIM_REGISTER_STOP).
3161          * XXX: here m->m_data points to the outer IP header.
3162          */
3163         mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
3164         if (mcp == NULL) {
3165             log(LOG_ERR,
3166                 "pim_input: pim register: could not copy register head\n");
3167             m_freem(m);
3168             return;
3169         }
3170         
3171         /* Keep statistics */
3172         /* XXX: registers_bytes include only the encap. mcast pkt */
3173         pimstat.pims_rcv_registers_msgs++;
3174         pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
3175         
3176         /*
3177          * forward the inner ip packet; point m_data at the inner ip.
3178          */
3179         m_adj(m, iphlen + PIM_MINLEN);
3180         
3181         if (mrtdebug & DEBUG_PIM) {
3182             log(LOG_DEBUG,
3183                 "pim_input: forwarding decapsulated register: "
3184                 "src %lx, dst %lx, vif %d\n",
3185                 (u_long)ntohl(encap_ip->ip_src.s_addr),
3186                 (u_long)ntohl(encap_ip->ip_dst.s_addr),
3187                 reg_vif_num);
3188         }
3189         if_simloop(viftable[reg_vif_num].v_ifp, m, dst.sin_family, 0);
3190         
3191         /* prepare the register head to send to the mrouting daemon */
3192         m = mcp;
3193     }
3194
3195 pim_input_to_daemon:    
3196     /*
3197      * Pass the PIM message up to the daemon; if it is a Register message,
3198      * pass the 'head' only up to the daemon. This includes the
3199      * outer IP header, PIM header, PIM-Register header and the
3200      * inner IP header.
3201      * XXX: the outer IP header pkt size of a Register is not adjust to
3202      * reflect the fact that the inner multicast data is truncated.
3203      */
3204     rip_input(m, iphlen, proto);
3205
3206     return;
3207 }
3208 #endif /* PIM */
3209
3210 static int
3211 ip_mroute_modevent(module_t mod, int type, void *unused)
3212 {
3213     int s;
3214
3215     switch (type) {
3216     case MOD_LOAD:
3217         s = splnet();
3218         /* XXX Protect against multiple loading */
3219         ip_mcast_src = X_ip_mcast_src;
3220         ip_mforward = X_ip_mforward;
3221         ip_mrouter_done = X_ip_mrouter_done;
3222         ip_mrouter_get = X_ip_mrouter_get;
3223         ip_mrouter_set = X_ip_mrouter_set;
3224         ip_rsvp_force_done = X_ip_rsvp_force_done;
3225         ip_rsvp_vif = X_ip_rsvp_vif;
3226         ipip_input = X_ipip_input;
3227         legal_vif_num = X_legal_vif_num;
3228         mrt_ioctl = X_mrt_ioctl;
3229         rsvp_input_p = X_rsvp_input;
3230         splx(s);
3231         break;
3232
3233     case MOD_UNLOAD:
3234         if (ip_mrouter)
3235             return EINVAL;
3236
3237         s = splnet();
3238         ip_mcast_src = NULL;
3239         ip_mforward = NULL;
3240         ip_mrouter_done = NULL;
3241         ip_mrouter_get = NULL;
3242         ip_mrouter_set = NULL;
3243         ip_rsvp_force_done = NULL;
3244         ip_rsvp_vif = NULL;
3245         ipip_input = NULL;
3246         legal_vif_num = NULL;
3247         mrt_ioctl = NULL;
3248         rsvp_input_p = NULL;
3249         splx(s);
3250         break;
3251     }
3252     return 0;
3253 }
3254
3255 static moduledata_t ip_mroutemod = {
3256     "ip_mroute",
3257     ip_mroute_modevent,
3258     0
3259 };
3260 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY);