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