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