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