ip_mroute.c revision 1.78 1 1.78 itojun /* $NetBSD: ip_mroute.c,v 1.78 2003/08/22 21:53:04 itojun 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.1 hpeyerl *
85 1.15 mycroft * MROUTING Revision: 1.2
86 1.1 hpeyerl */
87 1.58 lukem
88 1.58 lukem #include <sys/cdefs.h>
89 1.78 itojun __KERNEL_RCSID(0, "$NetBSD: ip_mroute.c,v 1.78 2003/08/22 21:53:04 itojun Exp $");
90 1.44 thorpej
91 1.44 thorpej #include "opt_ipsec.h"
92 1.1 hpeyerl
93 1.1 hpeyerl #include <sys/param.h>
94 1.15 mycroft #include <sys/systm.h>
95 1.47 thorpej #include <sys/callout.h>
96 1.1 hpeyerl #include <sys/mbuf.h>
97 1.1 hpeyerl #include <sys/socket.h>
98 1.1 hpeyerl #include <sys/socketvar.h>
99 1.15 mycroft #include <sys/protosw.h>
100 1.15 mycroft #include <sys/errno.h>
101 1.1 hpeyerl #include <sys/time.h>
102 1.15 mycroft #include <sys/kernel.h>
103 1.15 mycroft #include <sys/ioctl.h>
104 1.15 mycroft #include <sys/syslog.h>
105 1.1 hpeyerl #include <net/if.h>
106 1.1 hpeyerl #include <net/route.h>
107 1.1 hpeyerl #include <net/raw_cb.h>
108 1.1 hpeyerl #include <netinet/in.h>
109 1.15 mycroft #include <netinet/in_var.h>
110 1.1 hpeyerl #include <netinet/in_systm.h>
111 1.1 hpeyerl #include <netinet/ip.h>
112 1.15 mycroft #include <netinet/ip_var.h>
113 1.1 hpeyerl #include <netinet/in_pcb.h>
114 1.15 mycroft #include <netinet/udp.h>
115 1.1 hpeyerl #include <netinet/igmp.h>
116 1.1 hpeyerl #include <netinet/igmp_var.h>
117 1.1 hpeyerl #include <netinet/ip_mroute.h>
118 1.54 itojun #include <netinet/ip_encap.h>
119 1.64 fair
120 1.64 fair #ifdef IPSEC
121 1.64 fair #include <netinet6/ipsec.h>
122 1.64 fair #include <netkey/key.h>
123 1.64 fair #endif
124 1.37 hwr
125 1.77 jonathan #ifdef FAST_IPSEC
126 1.77 jonathan #include <netipsec/ipsec.h>
127 1.77 jonathan #include <netipsec/key.h>
128 1.77 jonathan #endif
129 1.77 jonathan
130 1.25 christos #include <machine/stdarg.h>
131 1.25 christos
132 1.15 mycroft #define IP_MULTICASTOPTS 0
133 1.15 mycroft #define M_PULLUP(m, len) \
134 1.15 mycroft do { \
135 1.15 mycroft if ((m) && ((m)->m_flags & M_EXT || (m)->m_len < (len))) \
136 1.15 mycroft (m) = m_pullup((m), (len)); \
137 1.63 perry } while (/*CONSTCOND*/ 0)
138 1.1 hpeyerl
139 1.1 hpeyerl /*
140 1.1 hpeyerl * Globals. All but ip_mrouter and ip_mrtproto could be static,
141 1.1 hpeyerl * except for netstat or debugging purposes.
142 1.1 hpeyerl */
143 1.30 mycroft struct socket *ip_mrouter = 0;
144 1.15 mycroft int ip_mrtproto = IGMP_DVMRP; /* for netstat only */
145 1.15 mycroft
146 1.15 mycroft #define NO_RTE_FOUND 0x1
147 1.15 mycroft #define RTE_FOUND 0x2
148 1.1 hpeyerl
149 1.15 mycroft #define MFCHASH(a, g) \
150 1.29 mycroft ((((a).s_addr >> 20) ^ ((a).s_addr >> 10) ^ (a).s_addr ^ \
151 1.29 mycroft ((g).s_addr >> 20) ^ ((g).s_addr >> 10) ^ (g).s_addr) & mfchash)
152 1.15 mycroft LIST_HEAD(mfchashhdr, mfc) *mfchashtbl;
153 1.15 mycroft u_long mfchash;
154 1.15 mycroft
155 1.15 mycroft u_char nexpire[MFCTBLSIZ];
156 1.15 mycroft struct vif viftable[MAXVIFS];
157 1.15 mycroft struct mrtstat mrtstat;
158 1.15 mycroft u_int mrtdebug = 0; /* debug level */
159 1.15 mycroft #define DEBUG_MFC 0x02
160 1.15 mycroft #define DEBUG_FORWARD 0x04
161 1.15 mycroft #define DEBUG_EXPIRE 0x08
162 1.15 mycroft #define DEBUG_XMIT 0x10
163 1.15 mycroft u_int tbfdebug = 0; /* tbf debug level */
164 1.15 mycroft #ifdef RSVP_ISI
165 1.15 mycroft u_int rsvpdebug = 0; /* rsvp debug level */
166 1.15 mycroft extern struct socket *ip_rsvpd;
167 1.15 mycroft extern int rsvp_on;
168 1.15 mycroft #endif /* RSVP_ISI */
169 1.15 mycroft
170 1.54 itojun /* vif attachment using sys/netinet/ip_encap.c */
171 1.54 itojun extern struct domain inetdomain;
172 1.54 itojun static void vif_input __P((struct mbuf *, ...));
173 1.54 itojun static int vif_encapcheck __P((const struct mbuf *, int, int, void *));
174 1.54 itojun static struct protosw vif_protosw =
175 1.54 itojun { SOCK_RAW, &inetdomain, IPPROTO_IPV4, PR_ATOMIC|PR_ADDR,
176 1.54 itojun vif_input, rip_output, 0, rip_ctloutput,
177 1.54 itojun rip_usrreq,
178 1.54 itojun 0, 0, 0, 0,
179 1.54 itojun };
180 1.54 itojun
181 1.15 mycroft #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
182 1.15 mycroft #define UPCALL_EXPIRE 6 /* number of timeouts */
183 1.15 mycroft
184 1.15 mycroft /*
185 1.15 mycroft * Define the token bucket filter structures
186 1.15 mycroft */
187 1.15 mycroft
188 1.31 mycroft #define TBF_REPROCESS (hz / 100) /* 100x / second */
189 1.15 mycroft
190 1.25 christos static int get_sg_cnt __P((struct sioc_sg_req *));
191 1.25 christos static int get_vif_cnt __P((struct sioc_vif_req *));
192 1.25 christos static int ip_mrouter_init __P((struct socket *, struct mbuf *));
193 1.25 christos static int get_version __P((struct mbuf *));
194 1.25 christos static int set_assert __P((struct mbuf *));
195 1.25 christos static int get_assert __P((struct mbuf *));
196 1.25 christos static int add_vif __P((struct mbuf *));
197 1.25 christos static int del_vif __P((struct mbuf *));
198 1.25 christos static void update_mfc __P((struct mfcctl *, struct mfc *));
199 1.25 christos static void expire_mfc __P((struct mfc *));
200 1.25 christos static int add_mfc __P((struct mbuf *));
201 1.25 christos #ifdef UPCALL_TIMING
202 1.25 christos static void collate __P((struct timeval *));
203 1.25 christos #endif
204 1.25 christos static int del_mfc __P((struct mbuf *));
205 1.25 christos static int socket_send __P((struct socket *, struct mbuf *,
206 1.25 christos struct sockaddr_in *));
207 1.25 christos static void expire_upcalls __P((void *));
208 1.25 christos #ifdef RSVP_ISI
209 1.25 christos static int ip_mdq __P((struct mbuf *, struct ifnet *, struct mfc *, vifi_t));
210 1.25 christos #else
211 1.25 christos static int ip_mdq __P((struct mbuf *, struct ifnet *, struct mfc *));
212 1.25 christos #endif
213 1.25 christos static void phyint_send __P((struct ip *, struct vif *, struct mbuf *));
214 1.25 christos static void encap_send __P((struct ip *, struct vif *, struct mbuf *));
215 1.25 christos static void tbf_control __P((struct vif *, struct mbuf *, struct ip *,
216 1.25 christos u_int32_t));
217 1.31 mycroft static void tbf_queue __P((struct vif *, struct mbuf *));
218 1.25 christos static void tbf_process_q __P((struct vif *));
219 1.25 christos static void tbf_reprocess_q __P((void *));
220 1.25 christos static int tbf_dq_sel __P((struct vif *, struct ip *));
221 1.25 christos static void tbf_send_packet __P((struct vif *, struct mbuf *));
222 1.25 christos static void tbf_update_tokens __P((struct vif *));
223 1.25 christos static int priority __P((struct vif *, struct ip *));
224 1.1 hpeyerl
225 1.1 hpeyerl /*
226 1.12 brezak * 'Interfaces' associated with decapsulator (so we can tell
227 1.12 brezak * packets that went through it from ones that get reflected
228 1.12 brezak * by a broken gateway). These interfaces are never linked into
229 1.12 brezak * the system ifnet list & no routes point to them. I.e., packets
230 1.12 brezak * can't be sent this way. They only exist as a placeholder for
231 1.12 brezak * multicast source verification.
232 1.12 brezak */
233 1.17 mycroft #if 0
234 1.12 brezak struct ifnet multicast_decap_if[MAXVIFS];
235 1.17 mycroft #endif
236 1.12 brezak
237 1.17 mycroft #define ENCAP_TTL 64
238 1.17 mycroft #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
239 1.12 brezak
240 1.12 brezak /* prototype IP hdr for encapsulated packets */
241 1.12 brezak struct ip multicast_encap_iphdr = {
242 1.15 mycroft #if BYTE_ORDER == LITTLE_ENDIAN
243 1.12 brezak sizeof(struct ip) >> 2, IPVERSION,
244 1.12 brezak #else
245 1.12 brezak IPVERSION, sizeof(struct ip) >> 2,
246 1.12 brezak #endif
247 1.12 brezak 0, /* tos */
248 1.12 brezak sizeof(struct ip), /* total length */
249 1.12 brezak 0, /* id */
250 1.12 brezak 0, /* frag offset */
251 1.60 itojun ENCAP_TTL, ENCAP_PROTO,
252 1.12 brezak 0, /* checksum */
253 1.12 brezak };
254 1.12 brezak
255 1.12 brezak /*
256 1.1 hpeyerl * Private variables.
257 1.1 hpeyerl */
258 1.15 mycroft static vifi_t numvifs = 0;
259 1.12 brezak
260 1.47 thorpej static struct callout expire_upcalls_ch;
261 1.47 thorpej
262 1.12 brezak /*
263 1.59 sommerfe * one-back cache used by vif_encapcheck to locate a tunnel's vif
264 1.12 brezak * given a datagram's src ip address.
265 1.12 brezak */
266 1.29 mycroft static struct in_addr last_encap_src;
267 1.12 brezak static struct vif *last_encap_vif;
268 1.12 brezak
269 1.12 brezak /*
270 1.15 mycroft * whether or not special PIM assert processing is enabled.
271 1.15 mycroft */
272 1.15 mycroft static int pim_assert;
273 1.15 mycroft /*
274 1.15 mycroft * Rate limit for assert notification messages, in usec
275 1.12 brezak */
276 1.15 mycroft #define ASSERT_MSG_TIME 3000000
277 1.12 brezak
278 1.15 mycroft /*
279 1.15 mycroft * Find a route for a given origin IP address and Multicast group address
280 1.15 mycroft * Type of service parameter to be added in the future!!!
281 1.15 mycroft */
282 1.15 mycroft
283 1.69 itojun #define MFCFIND(o, g, rt) do { \
284 1.48 augustss struct mfc *_rt; \
285 1.30 mycroft (rt) = 0; \
286 1.15 mycroft ++mrtstat.mrts_mfc_lookups; \
287 1.57 matt LIST_FOREACH(_rt, &mfchashtbl[MFCHASH(o, g)], mfc_hash) { \
288 1.29 mycroft if (in_hosteq(_rt->mfc_origin, (o)) && \
289 1.29 mycroft in_hosteq(_rt->mfc_mcastgrp, (g)) && \
290 1.30 mycroft _rt->mfc_stall == 0) { \
291 1.15 mycroft (rt) = _rt; \
292 1.15 mycroft break; \
293 1.15 mycroft } \
294 1.15 mycroft } \
295 1.30 mycroft if ((rt) == 0) \
296 1.15 mycroft ++mrtstat.mrts_mfc_misses; \
297 1.69 itojun } while (/*CONSTCOND*/ 0)
298 1.12 brezak
299 1.12 brezak /*
300 1.15 mycroft * Macros to compute elapsed time efficiently
301 1.15 mycroft * Borrowed from Van Jacobson's scheduling code
302 1.12 brezak */
303 1.69 itojun #define TV_DELTA(a, b, delta) do { \
304 1.48 augustss int xxs; \
305 1.15 mycroft delta = (a).tv_usec - (b).tv_usec; \
306 1.15 mycroft xxs = (a).tv_sec - (b).tv_sec; \
307 1.15 mycroft switch (xxs) { \
308 1.15 mycroft case 2: \
309 1.15 mycroft delta += 1000000; \
310 1.15 mycroft /* fall through */ \
311 1.15 mycroft case 1: \
312 1.15 mycroft delta += 1000000; \
313 1.15 mycroft /* fall through */ \
314 1.15 mycroft case 0: \
315 1.15 mycroft break; \
316 1.15 mycroft default: \
317 1.15 mycroft delta += (1000000 * xxs); \
318 1.15 mycroft break; \
319 1.15 mycroft } \
320 1.69 itojun } while (/*CONSTCOND*/ 0)
321 1.15 mycroft
322 1.15 mycroft #ifdef UPCALL_TIMING
323 1.15 mycroft u_int32_t upcall_data[51];
324 1.15 mycroft #endif /* UPCALL_TIMING */
325 1.15 mycroft
326 1.12 brezak /*
327 1.15 mycroft * Handle MRT setsockopt commands to modify the multicast routing tables.
328 1.12 brezak */
329 1.15 mycroft int
330 1.28 mycroft ip_mrouter_set(so, optname, m)
331 1.15 mycroft struct socket *so;
332 1.28 mycroft int optname;
333 1.15 mycroft struct mbuf **m;
334 1.15 mycroft {
335 1.15 mycroft int error;
336 1.15 mycroft
337 1.28 mycroft if (optname != MRT_INIT && so != ip_mrouter)
338 1.28 mycroft error = ENOPROTOOPT;
339 1.15 mycroft else
340 1.28 mycroft switch (optname) {
341 1.15 mycroft case MRT_INIT:
342 1.15 mycroft error = ip_mrouter_init(so, *m);
343 1.15 mycroft break;
344 1.15 mycroft case MRT_DONE:
345 1.15 mycroft error = ip_mrouter_done();
346 1.15 mycroft break;
347 1.15 mycroft case MRT_ADD_VIF:
348 1.15 mycroft error = add_vif(*m);
349 1.15 mycroft break;
350 1.15 mycroft case MRT_DEL_VIF:
351 1.15 mycroft error = del_vif(*m);
352 1.15 mycroft break;
353 1.15 mycroft case MRT_ADD_MFC:
354 1.15 mycroft error = add_mfc(*m);
355 1.15 mycroft break;
356 1.15 mycroft case MRT_DEL_MFC:
357 1.15 mycroft error = del_mfc(*m);
358 1.15 mycroft break;
359 1.15 mycroft case MRT_ASSERT:
360 1.15 mycroft error = set_assert(*m);
361 1.15 mycroft break;
362 1.15 mycroft default:
363 1.28 mycroft error = ENOPROTOOPT;
364 1.15 mycroft break;
365 1.15 mycroft }
366 1.15 mycroft
367 1.15 mycroft if (*m)
368 1.15 mycroft m_free(*m);
369 1.15 mycroft return (error);
370 1.12 brezak }
371 1.12 brezak
372 1.15 mycroft /*
373 1.15 mycroft * Handle MRT getsockopt commands
374 1.15 mycroft */
375 1.15 mycroft int
376 1.28 mycroft ip_mrouter_get(so, optname, m)
377 1.15 mycroft struct socket *so;
378 1.28 mycroft int optname;
379 1.15 mycroft struct mbuf **m;
380 1.12 brezak {
381 1.15 mycroft int error;
382 1.12 brezak
383 1.15 mycroft if (so != ip_mrouter)
384 1.28 mycroft error = ENOPROTOOPT;
385 1.15 mycroft else {
386 1.28 mycroft *m = m_get(M_WAIT, MT_SOOPTS);
387 1.66 matt MCLAIM(*m, so->so_mowner);
388 1.12 brezak
389 1.28 mycroft switch (optname) {
390 1.15 mycroft case MRT_VERSION:
391 1.28 mycroft error = get_version(*m);
392 1.15 mycroft break;
393 1.15 mycroft case MRT_ASSERT:
394 1.28 mycroft error = get_assert(*m);
395 1.15 mycroft break;
396 1.15 mycroft default:
397 1.28 mycroft error = ENOPROTOOPT;
398 1.15 mycroft break;
399 1.15 mycroft }
400 1.15 mycroft
401 1.15 mycroft if (error)
402 1.28 mycroft m_free(*m);
403 1.12 brezak }
404 1.15 mycroft
405 1.15 mycroft return (error);
406 1.12 brezak }
407 1.12 brezak
408 1.1 hpeyerl /*
409 1.15 mycroft * Handle ioctl commands to obtain information from the cache
410 1.1 hpeyerl */
411 1.1 hpeyerl int
412 1.28 mycroft mrt_ioctl(so, cmd, data)
413 1.28 mycroft struct socket *so;
414 1.15 mycroft u_long cmd;
415 1.15 mycroft caddr_t data;
416 1.1 hpeyerl {
417 1.15 mycroft int error;
418 1.1 hpeyerl
419 1.28 mycroft if (so != ip_mrouter)
420 1.15 mycroft error = EINVAL;
421 1.28 mycroft else
422 1.28 mycroft switch (cmd) {
423 1.28 mycroft case SIOCGETVIFCNT:
424 1.28 mycroft error = get_vif_cnt((struct sioc_vif_req *)data);
425 1.28 mycroft break;
426 1.28 mycroft case SIOCGETSGCNT:
427 1.28 mycroft error = get_sg_cnt((struct sioc_sg_req *)data);
428 1.28 mycroft break;
429 1.28 mycroft default:
430 1.28 mycroft error = EINVAL;
431 1.28 mycroft break;
432 1.28 mycroft }
433 1.1 hpeyerl
434 1.15 mycroft return (error);
435 1.15 mycroft }
436 1.1 hpeyerl
437 1.15 mycroft /*
438 1.15 mycroft * returns the packet, byte, rpf-failure count for the source group provided
439 1.15 mycroft */
440 1.15 mycroft static int
441 1.15 mycroft get_sg_cnt(req)
442 1.48 augustss struct sioc_sg_req *req;
443 1.15 mycroft {
444 1.48 augustss struct mfc *rt;
445 1.15 mycroft int s;
446 1.1 hpeyerl
447 1.24 mycroft s = splsoftnet();
448 1.29 mycroft MFCFIND(req->src, req->grp, rt);
449 1.15 mycroft splx(s);
450 1.30 mycroft if (rt != 0) {
451 1.15 mycroft req->pktcnt = rt->mfc_pkt_cnt;
452 1.15 mycroft req->bytecnt = rt->mfc_byte_cnt;
453 1.15 mycroft req->wrong_if = rt->mfc_wrong_if;
454 1.15 mycroft } else
455 1.15 mycroft req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
456 1.1 hpeyerl
457 1.15 mycroft return (0);
458 1.15 mycroft }
459 1.1 hpeyerl
460 1.15 mycroft /*
461 1.15 mycroft * returns the input and output packet and byte counts on the vif provided
462 1.15 mycroft */
463 1.15 mycroft static int
464 1.15 mycroft get_vif_cnt(req)
465 1.48 augustss struct sioc_vif_req *req;
466 1.15 mycroft {
467 1.48 augustss vifi_t vifi = req->vifi;
468 1.1 hpeyerl
469 1.15 mycroft if (vifi >= numvifs)
470 1.15 mycroft return (EINVAL);
471 1.1 hpeyerl
472 1.15 mycroft req->icount = viftable[vifi].v_pkt_in;
473 1.15 mycroft req->ocount = viftable[vifi].v_pkt_out;
474 1.15 mycroft req->ibytes = viftable[vifi].v_bytes_in;
475 1.15 mycroft req->obytes = viftable[vifi].v_bytes_out;
476 1.1 hpeyerl
477 1.15 mycroft return (0);
478 1.1 hpeyerl }
479 1.1 hpeyerl
480 1.1 hpeyerl /*
481 1.1 hpeyerl * Enable multicast routing
482 1.1 hpeyerl */
483 1.1 hpeyerl static int
484 1.15 mycroft ip_mrouter_init(so, m)
485 1.15 mycroft struct socket *so;
486 1.15 mycroft struct mbuf *m;
487 1.1 hpeyerl {
488 1.15 mycroft int *v;
489 1.15 mycroft
490 1.15 mycroft if (mrtdebug)
491 1.15 mycroft log(LOG_DEBUG,
492 1.30 mycroft "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
493 1.15 mycroft so->so_type, so->so_proto->pr_protocol);
494 1.15 mycroft
495 1.1 hpeyerl if (so->so_type != SOCK_RAW ||
496 1.1 hpeyerl so->so_proto->pr_protocol != IPPROTO_IGMP)
497 1.1 hpeyerl return (EOPNOTSUPP);
498 1.1 hpeyerl
499 1.15 mycroft if (m == 0 || m->m_len < sizeof(int))
500 1.15 mycroft return (EINVAL);
501 1.15 mycroft
502 1.15 mycroft v = mtod(m, int *);
503 1.15 mycroft if (*v != 1)
504 1.15 mycroft return (EINVAL);
505 1.15 mycroft
506 1.30 mycroft if (ip_mrouter != 0)
507 1.1 hpeyerl return (EADDRINUSE);
508 1.1 hpeyerl
509 1.1 hpeyerl ip_mrouter = so;
510 1.1 hpeyerl
511 1.51 ad mfchashtbl =
512 1.51 ad hashinit(MFCTBLSIZ, HASH_LIST, M_MRTABLE, M_WAITOK, &mfchash);
513 1.15 mycroft bzero((caddr_t)nexpire, sizeof(nexpire));
514 1.15 mycroft
515 1.15 mycroft pim_assert = 0;
516 1.15 mycroft
517 1.47 thorpej callout_init(&expire_upcalls_ch);
518 1.47 thorpej callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
519 1.47 thorpej expire_upcalls, NULL);
520 1.15 mycroft
521 1.15 mycroft if (mrtdebug)
522 1.30 mycroft log(LOG_DEBUG, "ip_mrouter_init\n");
523 1.15 mycroft
524 1.1 hpeyerl return (0);
525 1.1 hpeyerl }
526 1.1 hpeyerl
527 1.1 hpeyerl /*
528 1.1 hpeyerl * Disable multicast routing
529 1.1 hpeyerl */
530 1.1 hpeyerl int
531 1.1 hpeyerl ip_mrouter_done()
532 1.1 hpeyerl {
533 1.15 mycroft vifi_t vifi;
534 1.48 augustss struct vif *vifp;
535 1.15 mycroft int i;
536 1.15 mycroft int s;
537 1.60 itojun
538 1.24 mycroft s = splsoftnet();
539 1.1 hpeyerl
540 1.17 mycroft /* Clear out all the vifs currently in use. */
541 1.1 hpeyerl for (vifi = 0; vifi < numvifs; vifi++) {
542 1.15 mycroft vifp = &viftable[vifi];
543 1.29 mycroft if (!in_nullhost(vifp->v_lcl_addr))
544 1.17 mycroft reset_vif(vifp);
545 1.1 hpeyerl }
546 1.17 mycroft
547 1.1 hpeyerl numvifs = 0;
548 1.15 mycroft pim_assert = 0;
549 1.60 itojun
550 1.47 thorpej callout_stop(&expire_upcalls_ch);
551 1.60 itojun
552 1.15 mycroft /*
553 1.15 mycroft * Free all multicast forwarding cache entries.
554 1.15 mycroft */
555 1.15 mycroft for (i = 0; i < MFCTBLSIZ; i++) {
556 1.48 augustss struct mfc *rt, *nrt;
557 1.1 hpeyerl
558 1.57 matt for (rt = LIST_FIRST(&mfchashtbl[i]); rt; rt = nrt) {
559 1.57 matt nrt = LIST_NEXT(rt, mfc_hash);
560 1.60 itojun
561 1.15 mycroft expire_mfc(rt);
562 1.15 mycroft }
563 1.15 mycroft }
564 1.40 mycroft
565 1.15 mycroft free(mfchashtbl, M_MRTABLE);
566 1.40 mycroft mfchashtbl = 0;
567 1.60 itojun
568 1.17 mycroft /* Reset de-encapsulation cache. */
569 1.60 itojun
570 1.30 mycroft ip_mrouter = 0;
571 1.60 itojun
572 1.15 mycroft splx(s);
573 1.60 itojun
574 1.15 mycroft if (mrtdebug)
575 1.30 mycroft log(LOG_DEBUG, "ip_mrouter_done\n");
576 1.60 itojun
577 1.15 mycroft return (0);
578 1.72 itojun }
579 1.72 itojun
580 1.72 itojun void
581 1.72 itojun ip_mrouter_detach(ifp)
582 1.72 itojun struct ifnet *ifp;
583 1.72 itojun {
584 1.72 itojun int vifi, i;
585 1.72 itojun struct vif *vifp;
586 1.75 itojun struct mfc *rt;
587 1.75 itojun struct rtdetq *rte;
588 1.72 itojun
589 1.72 itojun /* XXX not sure about sideeffect to userland routing daemon */
590 1.72 itojun for (vifi = 0; vifi < numvifs; vifi++) {
591 1.72 itojun vifp = &viftable[vifi];
592 1.72 itojun if (vifp->v_ifp == ifp)
593 1.72 itojun reset_vif(vifp);
594 1.72 itojun }
595 1.72 itojun for (i = 0; i < MFCTBLSIZ; i++) {
596 1.75 itojun if (nexpire[i] == 0)
597 1.75 itojun continue;
598 1.75 itojun LIST_FOREACH(rt, &mfchashtbl[i], mfc_hash) {
599 1.75 itojun for (rte = rt->mfc_stall; rte; rte = rte->next) {
600 1.75 itojun if (rte->ifp == ifp)
601 1.75 itojun rte->ifp = NULL;
602 1.72 itojun }
603 1.72 itojun }
604 1.72 itojun }
605 1.15 mycroft }
606 1.15 mycroft
607 1.15 mycroft static int
608 1.15 mycroft get_version(m)
609 1.15 mycroft struct mbuf *m;
610 1.15 mycroft {
611 1.15 mycroft int *v = mtod(m, int *);
612 1.15 mycroft
613 1.15 mycroft *v = 0x0305; /* XXX !!!! */
614 1.15 mycroft m->m_len = sizeof(int);
615 1.15 mycroft return (0);
616 1.15 mycroft }
617 1.15 mycroft
618 1.15 mycroft /*
619 1.15 mycroft * Set PIM assert processing global
620 1.15 mycroft */
621 1.15 mycroft static int
622 1.15 mycroft set_assert(m)
623 1.15 mycroft struct mbuf *m;
624 1.15 mycroft {
625 1.15 mycroft int *i;
626 1.15 mycroft
627 1.15 mycroft if (m == 0 || m->m_len < sizeof(int))
628 1.15 mycroft return (EINVAL);
629 1.1 hpeyerl
630 1.15 mycroft i = mtod(m, int *);
631 1.15 mycroft pim_assert = !!*i;
632 1.15 mycroft return (0);
633 1.15 mycroft }
634 1.15 mycroft
635 1.15 mycroft /*
636 1.15 mycroft * Get PIM assert processing global
637 1.15 mycroft */
638 1.15 mycroft static int
639 1.15 mycroft get_assert(m)
640 1.15 mycroft struct mbuf *m;
641 1.15 mycroft {
642 1.15 mycroft int *i = mtod(m, int *);
643 1.1 hpeyerl
644 1.15 mycroft *i = pim_assert;
645 1.15 mycroft m->m_len = sizeof(int);
646 1.1 hpeyerl return (0);
647 1.1 hpeyerl }
648 1.1 hpeyerl
649 1.15 mycroft static struct sockaddr_in sin = { sizeof(sin), AF_INET };
650 1.15 mycroft
651 1.1 hpeyerl /*
652 1.1 hpeyerl * Add a vif to the vif table
653 1.1 hpeyerl */
654 1.1 hpeyerl static int
655 1.15 mycroft add_vif(m)
656 1.15 mycroft struct mbuf *m;
657 1.15 mycroft {
658 1.48 augustss struct vifctl *vifcp;
659 1.48 augustss struct vif *vifp;
660 1.15 mycroft struct ifaddr *ifa;
661 1.15 mycroft struct ifnet *ifp;
662 1.1 hpeyerl struct ifreq ifr;
663 1.15 mycroft int error, s;
664 1.60 itojun
665 1.15 mycroft if (m == 0 || m->m_len < sizeof(struct vifctl))
666 1.15 mycroft return (EINVAL);
667 1.1 hpeyerl
668 1.15 mycroft vifcp = mtod(m, struct vifctl *);
669 1.1 hpeyerl if (vifcp->vifc_vifi >= MAXVIFS)
670 1.1 hpeyerl return (EINVAL);
671 1.15 mycroft
672 1.15 mycroft vifp = &viftable[vifcp->vifc_vifi];
673 1.29 mycroft if (!in_nullhost(vifp->v_lcl_addr))
674 1.1 hpeyerl return (EADDRINUSE);
675 1.60 itojun
676 1.15 mycroft /* Find the interface with an address in AF_INET family. */
677 1.1 hpeyerl sin.sin_addr = vifcp->vifc_lcl_addr;
678 1.18 mycroft ifa = ifa_ifwithaddr(sintosa(&sin));
679 1.1 hpeyerl if (ifa == 0)
680 1.1 hpeyerl return (EADDRNOTAVAIL);
681 1.60 itojun
682 1.12 brezak if (vifcp->vifc_flags & VIFF_TUNNEL) {
683 1.17 mycroft if (vifcp->vifc_flags & VIFF_SRCRT) {
684 1.30 mycroft log(LOG_ERR, "Source routed tunnels not supported\n");
685 1.15 mycroft return (EOPNOTSUPP);
686 1.12 brezak }
687 1.17 mycroft
688 1.54 itojun /* attach this vif to decapsulator dispatch table */
689 1.54 itojun vifp->v_encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV4,
690 1.54 itojun vif_encapcheck, &vif_protosw, vifp);
691 1.54 itojun if (!vifp->v_encap_cookie)
692 1.54 itojun return (EINVAL);
693 1.54 itojun
694 1.17 mycroft /* Create a fake encapsulation interface. */
695 1.17 mycroft ifp = (struct ifnet *)malloc(sizeof(*ifp), M_MRTABLE, M_WAITOK);
696 1.17 mycroft bzero(ifp, sizeof(*ifp));
697 1.34 christos sprintf(ifp->if_xname, "mdecap%d", vifcp->vifc_vifi);
698 1.17 mycroft
699 1.17 mycroft /* Prepare cached route entry. */
700 1.17 mycroft bzero(&vifp->v_route, sizeof(vifp->v_route));
701 1.12 brezak } else {
702 1.17 mycroft /* Use the physical interface associated with the address. */
703 1.17 mycroft ifp = ifa->ifa_ifp;
704 1.17 mycroft
705 1.15 mycroft /* Make sure the interface supports multicast. */
706 1.12 brezak if ((ifp->if_flags & IFF_MULTICAST) == 0)
707 1.15 mycroft return (EOPNOTSUPP);
708 1.45 thorpej
709 1.15 mycroft /* Enable promiscuous reception of all IP multicasts. */
710 1.19 mycroft satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
711 1.15 mycroft satosin(&ifr.ifr_addr)->sin_family = AF_INET;
712 1.29 mycroft satosin(&ifr.ifr_addr)->sin_addr = zeroin_addr;
713 1.1 hpeyerl error = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, (caddr_t)&ifr);
714 1.12 brezak if (error)
715 1.15 mycroft return (error);
716 1.1 hpeyerl }
717 1.45 thorpej
718 1.24 mycroft s = splsoftnet();
719 1.31 mycroft
720 1.15 mycroft /* Define parameters for the tbf structure. */
721 1.31 mycroft vifp->tbf_q = 0;
722 1.31 mycroft vifp->tbf_t = &vifp->tbf_q;
723 1.31 mycroft microtime(&vifp->tbf_last_pkt_t);
724 1.31 mycroft vifp->tbf_n_tok = 0;
725 1.31 mycroft vifp->tbf_q_len = 0;
726 1.31 mycroft vifp->tbf_max_q_len = MAXQSIZE;
727 1.60 itojun
728 1.1 hpeyerl vifp->v_flags = vifcp->vifc_flags;
729 1.1 hpeyerl vifp->v_threshold = vifcp->vifc_threshold;
730 1.31 mycroft /* scaling up here allows division by 1024 in critical code */
731 1.31 mycroft vifp->v_rate_limit = vifcp->vifc_rate_limit * 1024 / 1000;
732 1.1 hpeyerl vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
733 1.15 mycroft vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
734 1.12 brezak vifp->v_ifp = ifp;
735 1.15 mycroft /* Initialize per vif pkt counters. */
736 1.15 mycroft vifp->v_pkt_in = 0;
737 1.15 mycroft vifp->v_pkt_out = 0;
738 1.15 mycroft vifp->v_bytes_in = 0;
739 1.15 mycroft vifp->v_bytes_out = 0;
740 1.47 thorpej
741 1.47 thorpej callout_init(&vifp->v_repq_ch);
742 1.47 thorpej
743 1.31 mycroft #ifdef RSVP_ISI
744 1.31 mycroft vifp->v_rsvp_on = 0;
745 1.31 mycroft vifp->v_rsvpd = 0;
746 1.31 mycroft #endif /* RSVP_ISI */
747 1.31 mycroft
748 1.12 brezak splx(s);
749 1.60 itojun
750 1.15 mycroft /* Adjust numvifs up if the vifi is higher than numvifs. */
751 1.1 hpeyerl if (numvifs <= vifcp->vifc_vifi)
752 1.1 hpeyerl numvifs = vifcp->vifc_vifi + 1;
753 1.60 itojun
754 1.15 mycroft if (mrtdebug)
755 1.30 mycroft log(LOG_DEBUG, "add_vif #%d, lcladdr %x, %s %x, thresh %x, rate %d\n",
756 1.60 itojun vifcp->vifc_vifi,
757 1.15 mycroft ntohl(vifcp->vifc_lcl_addr.s_addr),
758 1.15 mycroft (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
759 1.15 mycroft ntohl(vifcp->vifc_rmt_addr.s_addr),
760 1.15 mycroft vifcp->vifc_threshold,
761 1.60 itojun vifcp->vifc_rate_limit);
762 1.60 itojun
763 1.1 hpeyerl return (0);
764 1.1 hpeyerl }
765 1.1 hpeyerl
766 1.17 mycroft void
767 1.17 mycroft reset_vif(vifp)
768 1.48 augustss struct vif *vifp;
769 1.17 mycroft {
770 1.48 augustss struct mbuf *m, *n;
771 1.17 mycroft struct ifnet *ifp;
772 1.17 mycroft struct ifreq ifr;
773 1.17 mycroft
774 1.47 thorpej callout_stop(&vifp->v_repq_ch);
775 1.47 thorpej
776 1.54 itojun /* detach this vif from decapsulator dispatch table */
777 1.54 itojun encap_detach(vifp->v_encap_cookie);
778 1.54 itojun vifp->v_encap_cookie = NULL;
779 1.54 itojun
780 1.31 mycroft for (m = vifp->tbf_q; m != 0; m = n) {
781 1.31 mycroft n = m->m_nextpkt;
782 1.31 mycroft m_freem(m);
783 1.31 mycroft }
784 1.31 mycroft
785 1.17 mycroft if (vifp->v_flags & VIFF_TUNNEL) {
786 1.17 mycroft free(vifp->v_ifp, M_MRTABLE);
787 1.17 mycroft if (vifp == last_encap_vif) {
788 1.17 mycroft last_encap_vif = 0;
789 1.29 mycroft last_encap_src = zeroin_addr;
790 1.17 mycroft }
791 1.17 mycroft } else {
792 1.19 mycroft satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
793 1.17 mycroft satosin(&ifr.ifr_addr)->sin_family = AF_INET;
794 1.29 mycroft satosin(&ifr.ifr_addr)->sin_addr = zeroin_addr;
795 1.17 mycroft ifp = vifp->v_ifp;
796 1.17 mycroft (*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)&ifr);
797 1.17 mycroft }
798 1.17 mycroft bzero((caddr_t)vifp, sizeof(*vifp));
799 1.17 mycroft }
800 1.17 mycroft
801 1.1 hpeyerl /*
802 1.1 hpeyerl * Delete a vif from the vif table
803 1.1 hpeyerl */
804 1.1 hpeyerl static int
805 1.15 mycroft del_vif(m)
806 1.15 mycroft struct mbuf *m;
807 1.1 hpeyerl {
808 1.15 mycroft vifi_t *vifip;
809 1.48 augustss struct vif *vifp;
810 1.48 augustss vifi_t vifi;
811 1.15 mycroft int s;
812 1.60 itojun
813 1.15 mycroft if (m == 0 || m->m_len < sizeof(vifi_t))
814 1.15 mycroft return (EINVAL);
815 1.1 hpeyerl
816 1.15 mycroft vifip = mtod(m, vifi_t *);
817 1.1 hpeyerl if (*vifip >= numvifs)
818 1.1 hpeyerl return (EINVAL);
819 1.15 mycroft
820 1.15 mycroft vifp = &viftable[*vifip];
821 1.29 mycroft if (in_nullhost(vifp->v_lcl_addr))
822 1.1 hpeyerl return (EADDRNOTAVAIL);
823 1.60 itojun
824 1.24 mycroft s = splsoftnet();
825 1.60 itojun
826 1.17 mycroft reset_vif(vifp);
827 1.60 itojun
828 1.1 hpeyerl /* Adjust numvifs down */
829 1.15 mycroft for (vifi = numvifs; vifi > 0; vifi--)
830 1.29 mycroft if (!in_nullhost(viftable[vifi-1].v_lcl_addr))
831 1.1 hpeyerl break;
832 1.15 mycroft numvifs = vifi;
833 1.60 itojun
834 1.1 hpeyerl splx(s);
835 1.60 itojun
836 1.15 mycroft if (mrtdebug)
837 1.30 mycroft log(LOG_DEBUG, "del_vif %d, numvifs %d\n", *vifip, numvifs);
838 1.60 itojun
839 1.1 hpeyerl return (0);
840 1.1 hpeyerl }
841 1.1 hpeyerl
842 1.15 mycroft static void
843 1.15 mycroft update_mfc(mfccp, rt)
844 1.15 mycroft struct mfcctl *mfccp;
845 1.15 mycroft struct mfc *rt;
846 1.1 hpeyerl {
847 1.15 mycroft vifi_t vifi;
848 1.1 hpeyerl
849 1.15 mycroft rt->mfc_parent = mfccp->mfcc_parent;
850 1.15 mycroft for (vifi = 0; vifi < numvifs; vifi++)
851 1.15 mycroft rt->mfc_ttls[vifi] = mfccp->mfcc_ttls[vifi];
852 1.15 mycroft rt->mfc_expire = 0;
853 1.15 mycroft rt->mfc_stall = 0;
854 1.15 mycroft }
855 1.1 hpeyerl
856 1.15 mycroft static void
857 1.15 mycroft expire_mfc(rt)
858 1.15 mycroft struct mfc *rt;
859 1.15 mycroft {
860 1.15 mycroft struct rtdetq *rte, *nrte;
861 1.1 hpeyerl
862 1.30 mycroft for (rte = rt->mfc_stall; rte != 0; rte = nrte) {
863 1.15 mycroft nrte = rte->next;
864 1.15 mycroft m_freem(rte->m);
865 1.15 mycroft free(rte, M_MRTABLE);
866 1.1 hpeyerl }
867 1.1 hpeyerl
868 1.15 mycroft LIST_REMOVE(rt, mfc_hash);
869 1.15 mycroft free(rt, M_MRTABLE);
870 1.1 hpeyerl }
871 1.1 hpeyerl
872 1.1 hpeyerl /*
873 1.15 mycroft * Add an mfc entry
874 1.1 hpeyerl */
875 1.1 hpeyerl static int
876 1.15 mycroft add_mfc(m)
877 1.15 mycroft struct mbuf *m;
878 1.1 hpeyerl {
879 1.15 mycroft struct mfcctl *mfccp;
880 1.25 christos struct mfc *rt;
881 1.25 christos u_int32_t hash = 0;
882 1.15 mycroft struct rtdetq *rte, *nrte;
883 1.48 augustss u_short nstl;
884 1.15 mycroft int s;
885 1.1 hpeyerl
886 1.15 mycroft if (m == 0 || m->m_len < sizeof(struct mfcctl))
887 1.1 hpeyerl return (EINVAL);
888 1.15 mycroft
889 1.15 mycroft mfccp = mtod(m, struct mfcctl *);
890 1.1 hpeyerl
891 1.24 mycroft s = splsoftnet();
892 1.29 mycroft MFCFIND(mfccp->mfcc_origin, mfccp->mfcc_mcastgrp, rt);
893 1.1 hpeyerl
894 1.15 mycroft /* If an entry already exists, just update the fields */
895 1.15 mycroft if (rt) {
896 1.15 mycroft if (mrtdebug & DEBUG_MFC)
897 1.70 itojun log(LOG_DEBUG, "add_mfc update o %x g %x p %x\n",
898 1.15 mycroft ntohl(mfccp->mfcc_origin.s_addr),
899 1.15 mycroft ntohl(mfccp->mfcc_mcastgrp.s_addr),
900 1.15 mycroft mfccp->mfcc_parent);
901 1.1 hpeyerl
902 1.15 mycroft if (rt->mfc_expire)
903 1.15 mycroft nexpire[hash]--;
904 1.1 hpeyerl
905 1.15 mycroft update_mfc(mfccp, rt);
906 1.1 hpeyerl
907 1.15 mycroft splx(s);
908 1.15 mycroft return (0);
909 1.15 mycroft }
910 1.1 hpeyerl
911 1.60 itojun /*
912 1.15 mycroft * Find the entry for which the upcall was made and update
913 1.15 mycroft */
914 1.15 mycroft nstl = 0;
915 1.29 mycroft hash = MFCHASH(mfccp->mfcc_origin, mfccp->mfcc_mcastgrp);
916 1.57 matt LIST_FOREACH(rt, &mfchashtbl[hash], mfc_hash) {
917 1.29 mycroft if (in_hosteq(rt->mfc_origin, mfccp->mfcc_origin) &&
918 1.29 mycroft in_hosteq(rt->mfc_mcastgrp, mfccp->mfcc_mcastgrp) &&
919 1.30 mycroft rt->mfc_stall != 0) {
920 1.15 mycroft if (nstl++)
921 1.30 mycroft log(LOG_ERR, "add_mfc %s o %x g %x p %x dbx %p\n",
922 1.15 mycroft "multiple kernel entries",
923 1.15 mycroft ntohl(mfccp->mfcc_origin.s_addr),
924 1.15 mycroft ntohl(mfccp->mfcc_mcastgrp.s_addr),
925 1.15 mycroft mfccp->mfcc_parent, rt->mfc_stall);
926 1.15 mycroft
927 1.15 mycroft if (mrtdebug & DEBUG_MFC)
928 1.70 itojun log(LOG_DEBUG, "add_mfc o %x g %x p %x dbg %p\n",
929 1.15 mycroft ntohl(mfccp->mfcc_origin.s_addr),
930 1.15 mycroft ntohl(mfccp->mfcc_mcastgrp.s_addr),
931 1.15 mycroft mfccp->mfcc_parent, rt->mfc_stall);
932 1.15 mycroft
933 1.15 mycroft if (rt->mfc_expire)
934 1.15 mycroft nexpire[hash]--;
935 1.15 mycroft
936 1.35 mycroft rte = rt->mfc_stall;
937 1.35 mycroft update_mfc(mfccp, rt);
938 1.35 mycroft
939 1.15 mycroft /* free packets Qed at the end of this entry */
940 1.35 mycroft for (; rte != 0; rte = nrte) {
941 1.15 mycroft nrte = rte->next;
942 1.75 itojun if (rte->ifp) {
943 1.15 mycroft #ifdef RSVP_ISI
944 1.75 itojun ip_mdq(rte->m, rte->ifp, rt, -1);
945 1.15 mycroft #else
946 1.75 itojun ip_mdq(rte->m, rte->ifp, rt);
947 1.15 mycroft #endif /* RSVP_ISI */
948 1.75 itojun }
949 1.15 mycroft m_freem(rte->m);
950 1.15 mycroft #ifdef UPCALL_TIMING
951 1.15 mycroft collate(&rte->t);
952 1.15 mycroft #endif /* UPCALL_TIMING */
953 1.15 mycroft free(rte, M_MRTABLE);
954 1.15 mycroft }
955 1.15 mycroft }
956 1.15 mycroft }
957 1.1 hpeyerl
958 1.15 mycroft if (nstl == 0) {
959 1.15 mycroft /*
960 1.15 mycroft * No mfc; make a new one
961 1.15 mycroft */
962 1.15 mycroft if (mrtdebug & DEBUG_MFC)
963 1.70 itojun log(LOG_DEBUG, "add_mfc no upcall o %x g %x p %x\n",
964 1.15 mycroft ntohl(mfccp->mfcc_origin.s_addr),
965 1.15 mycroft ntohl(mfccp->mfcc_mcastgrp.s_addr),
966 1.15 mycroft mfccp->mfcc_parent);
967 1.60 itojun
968 1.15 mycroft rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
969 1.30 mycroft if (rt == 0) {
970 1.1 hpeyerl splx(s);
971 1.15 mycroft return (ENOBUFS);
972 1.1 hpeyerl }
973 1.15 mycroft
974 1.15 mycroft rt->mfc_origin = mfccp->mfcc_origin;
975 1.15 mycroft rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
976 1.15 mycroft /* initialize pkt counters per src-grp */
977 1.15 mycroft rt->mfc_pkt_cnt = 0;
978 1.15 mycroft rt->mfc_byte_cnt = 0;
979 1.15 mycroft rt->mfc_wrong_if = 0;
980 1.15 mycroft timerclear(&rt->mfc_last_assert);
981 1.15 mycroft update_mfc(mfccp, rt);
982 1.60 itojun
983 1.15 mycroft /* insert new entry at head of hash chain */
984 1.15 mycroft LIST_INSERT_HEAD(&mfchashtbl[hash], rt, mfc_hash);
985 1.15 mycroft }
986 1.15 mycroft
987 1.1 hpeyerl splx(s);
988 1.1 hpeyerl return (0);
989 1.1 hpeyerl }
990 1.1 hpeyerl
991 1.15 mycroft #ifdef UPCALL_TIMING
992 1.15 mycroft /*
993 1.60 itojun * collect delay statistics on the upcalls
994 1.15 mycroft */
995 1.15 mycroft static void collate(t)
996 1.67 itojun struct timeval *t;
997 1.15 mycroft {
998 1.67 itojun u_int32_t d;
999 1.67 itojun struct timeval tp;
1000 1.67 itojun u_int32_t delta;
1001 1.60 itojun
1002 1.67 itojun microtime(&tp);
1003 1.60 itojun
1004 1.67 itojun if (timercmp(t, &tp, <)) {
1005 1.67 itojun TV_DELTA(tp, *t, delta);
1006 1.60 itojun
1007 1.67 itojun d = delta >> 10;
1008 1.67 itojun if (d > 50)
1009 1.67 itojun d = 50;
1010 1.60 itojun
1011 1.67 itojun ++upcall_data[d];
1012 1.67 itojun }
1013 1.15 mycroft }
1014 1.15 mycroft #endif /* UPCALL_TIMING */
1015 1.15 mycroft
1016 1.1 hpeyerl /*
1017 1.15 mycroft * Delete an mfc entry
1018 1.1 hpeyerl */
1019 1.1 hpeyerl static int
1020 1.15 mycroft del_mfc(m)
1021 1.15 mycroft struct mbuf *m;
1022 1.1 hpeyerl {
1023 1.15 mycroft struct mfcctl *mfccp;
1024 1.15 mycroft struct mfc *rt;
1025 1.1 hpeyerl int s;
1026 1.1 hpeyerl
1027 1.15 mycroft if (m == 0 || m->m_len < sizeof(struct mfcctl))
1028 1.15 mycroft return (EINVAL);
1029 1.15 mycroft
1030 1.15 mycroft mfccp = mtod(m, struct mfcctl *);
1031 1.15 mycroft
1032 1.15 mycroft if (mrtdebug & DEBUG_MFC)
1033 1.30 mycroft log(LOG_DEBUG, "del_mfc origin %x mcastgrp %x\n",
1034 1.29 mycroft ntohl(mfccp->mfcc_origin.s_addr),
1035 1.29 mycroft ntohl(mfccp->mfcc_mcastgrp.s_addr));
1036 1.1 hpeyerl
1037 1.24 mycroft s = splsoftnet();
1038 1.1 hpeyerl
1039 1.29 mycroft MFCFIND(mfccp->mfcc_origin, mfccp->mfcc_mcastgrp, rt);
1040 1.30 mycroft if (rt == 0) {
1041 1.1 hpeyerl splx(s);
1042 1.15 mycroft return (EADDRNOTAVAIL);
1043 1.1 hpeyerl }
1044 1.1 hpeyerl
1045 1.15 mycroft LIST_REMOVE(rt, mfc_hash);
1046 1.15 mycroft free(rt, M_MRTABLE);
1047 1.1 hpeyerl
1048 1.1 hpeyerl splx(s);
1049 1.1 hpeyerl return (0);
1050 1.1 hpeyerl }
1051 1.1 hpeyerl
1052 1.1 hpeyerl static int
1053 1.15 mycroft socket_send(s, mm, src)
1054 1.67 itojun struct socket *s;
1055 1.67 itojun struct mbuf *mm;
1056 1.67 itojun struct sockaddr_in *src;
1057 1.67 itojun {
1058 1.67 itojun if (s) {
1059 1.67 itojun if (sbappendaddr(&s->so_rcv, sintosa(src), mm,
1060 1.67 itojun (struct mbuf *)0) != 0) {
1061 1.67 itojun sorwakeup(s);
1062 1.67 itojun return (0);
1063 1.67 itojun }
1064 1.67 itojun }
1065 1.67 itojun m_freem(mm);
1066 1.67 itojun return (-1);
1067 1.1 hpeyerl }
1068 1.1 hpeyerl
1069 1.1 hpeyerl /*
1070 1.1 hpeyerl * IP multicast forwarding function. This function assumes that the packet
1071 1.1 hpeyerl * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1072 1.1 hpeyerl * pointed to by "ifp", and the packet is to be relayed to other networks
1073 1.1 hpeyerl * that have members of the packet's destination IP multicast group.
1074 1.1 hpeyerl *
1075 1.15 mycroft * The packet is returned unscathed to the caller, unless it is
1076 1.15 mycroft * erroneous, in which case a non-zero return value tells the caller to
1077 1.1 hpeyerl * discard it.
1078 1.1 hpeyerl */
1079 1.1 hpeyerl
1080 1.15 mycroft #define IP_HDR_LEN 20 /* # bytes of fixed IP header (excluding options) */
1081 1.15 mycroft #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1082 1.1 hpeyerl
1083 1.1 hpeyerl int
1084 1.15 mycroft #ifdef RSVP_ISI
1085 1.15 mycroft ip_mforward(m, ifp, imo)
1086 1.15 mycroft #else
1087 1.10 mycroft ip_mforward(m, ifp)
1088 1.15 mycroft #endif /* RSVP_ISI */
1089 1.67 itojun struct mbuf *m;
1090 1.67 itojun struct ifnet *ifp;
1091 1.15 mycroft #ifdef RSVP_ISI
1092 1.67 itojun struct ip_moptions *imo;
1093 1.15 mycroft #endif /* RSVP_ISI */
1094 1.1 hpeyerl {
1095 1.67 itojun struct ip *ip = mtod(m, struct ip *);
1096 1.67 itojun struct mfc *rt;
1097 1.67 itojun static int srctun = 0;
1098 1.67 itojun struct mbuf *mm;
1099 1.67 itojun int s;
1100 1.15 mycroft #ifdef RSVP_ISI
1101 1.67 itojun struct vif *vifp;
1102 1.67 itojun vifi_t vifi;
1103 1.15 mycroft #endif /* RSVP_ISI */
1104 1.55 thorpej
1105 1.67 itojun /*
1106 1.67 itojun * Clear any in-bound checksum flags for this packet.
1107 1.67 itojun */
1108 1.67 itojun m->m_pkthdr.csum_flags = 0;
1109 1.15 mycroft
1110 1.67 itojun if (mrtdebug & DEBUG_FORWARD)
1111 1.71 itojun log(LOG_DEBUG, "ip_mforward: src %x, dst %x, ifp %p\n",
1112 1.71 itojun ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr), ifp);
1113 1.1 hpeyerl
1114 1.67 itojun if (ip->ip_hl < (IP_HDR_LEN + TUNNEL_LEN) >> 2 ||
1115 1.67 itojun ((u_char *)(ip + 1))[1] != IPOPT_LSRR) {
1116 1.67 itojun /*
1117 1.67 itojun * Packet arrived via a physical interface or
1118 1.67 itojun * an encapuslated tunnel.
1119 1.67 itojun */
1120 1.67 itojun } else {
1121 1.67 itojun /*
1122 1.67 itojun * Packet arrived through a source-route tunnel.
1123 1.67 itojun * Source-route tunnels are no longer supported.
1124 1.67 itojun */
1125 1.67 itojun if ((srctun++ % 1000) == 0)
1126 1.67 itojun log(LOG_ERR,
1127 1.67 itojun "ip_mforward: received source-routed packet from %x\n",
1128 1.67 itojun ntohl(ip->ip_src.s_addr));
1129 1.15 mycroft
1130 1.67 itojun return (1);
1131 1.67 itojun }
1132 1.15 mycroft
1133 1.15 mycroft #ifdef RSVP_ISI
1134 1.67 itojun if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
1135 1.67 itojun if (ip->ip_ttl < 255)
1136 1.67 itojun ip->ip_ttl++; /* compensate for -1 in *_send routines */
1137 1.67 itojun if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1138 1.67 itojun vifp = viftable + vifi;
1139 1.67 itojun printf("Sending IPPROTO_RSVP from %x to %x on vif %d (%s%s)\n",
1140 1.67 itojun ntohl(ip->ip_src), ntohl(ip->ip_dst), vifi,
1141 1.67 itojun (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
1142 1.67 itojun vifp->v_ifp->if_xname);
1143 1.67 itojun }
1144 1.67 itojun return (ip_mdq(m, ifp, (struct mfc *)0, vifi));
1145 1.67 itojun }
1146 1.15 mycroft if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
1147 1.67 itojun printf("Warning: IPPROTO_RSVP from %x to %x without vif option\n",
1148 1.67 itojun ntohl(ip->ip_src), ntohl(ip->ip_dst));
1149 1.67 itojun }
1150 1.15 mycroft #endif /* RSVP_ISI */
1151 1.15 mycroft
1152 1.67 itojun /*
1153 1.67 itojun * Don't forward a packet with time-to-live of zero or one,
1154 1.67 itojun * or a packet destined to a local-only group.
1155 1.67 itojun */
1156 1.67 itojun if (ip->ip_ttl <= 1 || IN_LOCAL_GROUP(ip->ip_dst.s_addr))
1157 1.67 itojun return (0);
1158 1.15 mycroft
1159 1.67 itojun /*
1160 1.67 itojun * Determine forwarding vifs from the forwarding cache table
1161 1.67 itojun */
1162 1.67 itojun s = splsoftnet();
1163 1.67 itojun MFCFIND(ip->ip_src, ip->ip_dst, rt);
1164 1.1 hpeyerl
1165 1.67 itojun /* Entry exists, so forward if necessary */
1166 1.67 itojun if (rt != 0) {
1167 1.67 itojun splx(s);
1168 1.15 mycroft #ifdef RSVP_ISI
1169 1.67 itojun return (ip_mdq(m, ifp, rt, -1));
1170 1.15 mycroft #else
1171 1.67 itojun return (ip_mdq(m, ifp, rt));
1172 1.15 mycroft #endif /* RSVP_ISI */
1173 1.67 itojun } else {
1174 1.67 itojun /*
1175 1.67 itojun * If we don't have a route for packet's origin,
1176 1.67 itojun * Make a copy of the packet &
1177 1.67 itojun * send message to routing daemon
1178 1.67 itojun */
1179 1.15 mycroft
1180 1.67 itojun struct mbuf *mb0;
1181 1.67 itojun struct rtdetq *rte;
1182 1.67 itojun u_int32_t hash;
1183 1.67 itojun int hlen = ip->ip_hl << 2;
1184 1.15 mycroft #ifdef UPCALL_TIMING
1185 1.67 itojun struct timeval tp;
1186 1.15 mycroft
1187 1.67 itojun microtime(&tp);
1188 1.15 mycroft #endif /* UPCALL_TIMING */
1189 1.15 mycroft
1190 1.67 itojun mrtstat.mrts_no_route++;
1191 1.67 itojun if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
1192 1.67 itojun log(LOG_DEBUG, "ip_mforward: no rte s %x g %x\n",
1193 1.67 itojun ntohl(ip->ip_src.s_addr),
1194 1.67 itojun ntohl(ip->ip_dst.s_addr));
1195 1.67 itojun
1196 1.67 itojun /*
1197 1.67 itojun * Allocate mbufs early so that we don't do extra work if we are
1198 1.67 itojun * just going to fail anyway. Make sure to pullup the header so
1199 1.67 itojun * that other people can't step on it.
1200 1.67 itojun */
1201 1.67 itojun rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE,
1202 1.67 itojun M_NOWAIT);
1203 1.67 itojun if (rte == 0) {
1204 1.67 itojun splx(s);
1205 1.67 itojun return (ENOBUFS);
1206 1.67 itojun }
1207 1.67 itojun mb0 = m_copy(m, 0, M_COPYALL);
1208 1.67 itojun M_PULLUP(mb0, hlen);
1209 1.67 itojun if (mb0 == 0) {
1210 1.67 itojun free(rte, M_MRTABLE);
1211 1.67 itojun splx(s);
1212 1.67 itojun return (ENOBUFS);
1213 1.67 itojun }
1214 1.67 itojun
1215 1.67 itojun /* is there an upcall waiting for this packet? */
1216 1.67 itojun hash = MFCHASH(ip->ip_src, ip->ip_dst);
1217 1.67 itojun LIST_FOREACH(rt, &mfchashtbl[hash], mfc_hash) {
1218 1.67 itojun if (in_hosteq(ip->ip_src, rt->mfc_origin) &&
1219 1.67 itojun in_hosteq(ip->ip_dst, rt->mfc_mcastgrp) &&
1220 1.67 itojun rt->mfc_stall != 0)
1221 1.67 itojun break;
1222 1.67 itojun }
1223 1.67 itojun
1224 1.67 itojun if (rt == 0) {
1225 1.67 itojun int i;
1226 1.67 itojun struct igmpmsg *im;
1227 1.67 itojun
1228 1.67 itojun /* no upcall, so make a new entry */
1229 1.67 itojun rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE,
1230 1.67 itojun M_NOWAIT);
1231 1.67 itojun if (rt == 0) {
1232 1.67 itojun free(rte, M_MRTABLE);
1233 1.67 itojun m_freem(mb0);
1234 1.67 itojun splx(s);
1235 1.67 itojun return (ENOBUFS);
1236 1.67 itojun }
1237 1.67 itojun /*
1238 1.67 itojun * Make a copy of the header to send to the user level
1239 1.67 itojun * process
1240 1.67 itojun */
1241 1.67 itojun mm = m_copy(m, 0, hlen);
1242 1.67 itojun M_PULLUP(mm, hlen);
1243 1.67 itojun if (mm == 0) {
1244 1.67 itojun free(rte, M_MRTABLE);
1245 1.67 itojun m_freem(mb0);
1246 1.67 itojun free(rt, M_MRTABLE);
1247 1.67 itojun splx(s);
1248 1.67 itojun return (ENOBUFS);
1249 1.67 itojun }
1250 1.60 itojun
1251 1.67 itojun /*
1252 1.67 itojun * Send message to routing daemon to install
1253 1.67 itojun * a route into the kernel table
1254 1.67 itojun */
1255 1.67 itojun sin.sin_addr = ip->ip_src;
1256 1.15 mycroft
1257 1.67 itojun im = mtod(mm, struct igmpmsg *);
1258 1.67 itojun im->im_msgtype = IGMPMSG_NOCACHE;
1259 1.67 itojun im->im_mbz = 0;
1260 1.67 itojun
1261 1.67 itojun mrtstat.mrts_upcalls++;
1262 1.67 itojun
1263 1.67 itojun if (socket_send(ip_mrouter, mm, &sin) < 0) {
1264 1.67 itojun log(LOG_WARNING,
1265 1.67 itojun "ip_mforward: ip_mrouter socket queue full\n");
1266 1.67 itojun ++mrtstat.mrts_upq_sockfull;
1267 1.67 itojun free(rte, M_MRTABLE);
1268 1.67 itojun m_freem(mb0);
1269 1.67 itojun free(rt, M_MRTABLE);
1270 1.67 itojun splx(s);
1271 1.67 itojun return (ENOBUFS);
1272 1.67 itojun }
1273 1.15 mycroft
1274 1.67 itojun /* insert new entry at head of hash chain */
1275 1.67 itojun rt->mfc_origin = ip->ip_src;
1276 1.67 itojun rt->mfc_mcastgrp = ip->ip_dst;
1277 1.67 itojun rt->mfc_pkt_cnt = 0;
1278 1.67 itojun rt->mfc_byte_cnt = 0;
1279 1.67 itojun rt->mfc_wrong_if = 0;
1280 1.67 itojun rt->mfc_expire = UPCALL_EXPIRE;
1281 1.67 itojun nexpire[hash]++;
1282 1.67 itojun for (i = 0; i < numvifs; i++)
1283 1.67 itojun rt->mfc_ttls[i] = 0;
1284 1.67 itojun rt->mfc_parent = -1;
1285 1.67 itojun
1286 1.67 itojun /* link into table */
1287 1.67 itojun LIST_INSERT_HEAD(&mfchashtbl[hash], rt, mfc_hash);
1288 1.67 itojun /* Add this entry to the end of the queue */
1289 1.67 itojun rt->mfc_stall = rte;
1290 1.67 itojun } else {
1291 1.67 itojun /* determine if q has overflowed */
1292 1.67 itojun struct rtdetq **p;
1293 1.67 itojun int npkts = 0;
1294 1.67 itojun
1295 1.67 itojun for (p = &rt->mfc_stall; *p != 0; p = &(*p)->next)
1296 1.67 itojun if (++npkts > MAX_UPQ) {
1297 1.67 itojun mrtstat.mrts_upq_ovflw++;
1298 1.67 itojun free(rte, M_MRTABLE);
1299 1.67 itojun m_freem(mb0);
1300 1.67 itojun splx(s);
1301 1.67 itojun return (0);
1302 1.67 itojun }
1303 1.15 mycroft
1304 1.67 itojun /* Add this entry to the end of the queue */
1305 1.67 itojun *p = rte;
1306 1.67 itojun }
1307 1.15 mycroft
1308 1.67 itojun rte->next = 0;
1309 1.67 itojun rte->m = mb0;
1310 1.67 itojun rte->ifp = ifp;
1311 1.15 mycroft #ifdef UPCALL_TIMING
1312 1.67 itojun rte->t = tp;
1313 1.15 mycroft #endif /* UPCALL_TIMING */
1314 1.15 mycroft
1315 1.67 itojun splx(s);
1316 1.15 mycroft
1317 1.67 itojun return (0);
1318 1.67 itojun }
1319 1.1 hpeyerl }
1320 1.1 hpeyerl
1321 1.15 mycroft
1322 1.25 christos /*ARGSUSED*/
1323 1.1 hpeyerl static void
1324 1.25 christos expire_upcalls(v)
1325 1.25 christos void *v;
1326 1.1 hpeyerl {
1327 1.15 mycroft int i;
1328 1.15 mycroft int s;
1329 1.15 mycroft
1330 1.24 mycroft s = splsoftnet();
1331 1.15 mycroft
1332 1.15 mycroft for (i = 0; i < MFCTBLSIZ; i++) {
1333 1.48 augustss struct mfc *rt, *nrt;
1334 1.15 mycroft
1335 1.15 mycroft if (nexpire[i] == 0)
1336 1.15 mycroft continue;
1337 1.15 mycroft
1338 1.57 matt for (rt = LIST_FIRST(&mfchashtbl[i]); rt; rt = nrt) {
1339 1.57 matt nrt = LIST_NEXT(rt, mfc_hash);
1340 1.1 hpeyerl
1341 1.67 itojun if (rt->mfc_expire == 0 || --rt->mfc_expire > 0)
1342 1.15 mycroft continue;
1343 1.15 mycroft nexpire[i]--;
1344 1.15 mycroft
1345 1.15 mycroft ++mrtstat.mrts_cache_cleanups;
1346 1.15 mycroft if (mrtdebug & DEBUG_EXPIRE)
1347 1.15 mycroft log(LOG_DEBUG,
1348 1.30 mycroft "expire_upcalls: expiring (%x %x)\n",
1349 1.15 mycroft ntohl(rt->mfc_origin.s_addr),
1350 1.15 mycroft ntohl(rt->mfc_mcastgrp.s_addr));
1351 1.1 hpeyerl
1352 1.15 mycroft expire_mfc(rt);
1353 1.15 mycroft }
1354 1.15 mycroft }
1355 1.1 hpeyerl
1356 1.15 mycroft splx(s);
1357 1.47 thorpej callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
1358 1.47 thorpej expire_upcalls, NULL);
1359 1.1 hpeyerl }
1360 1.1 hpeyerl
1361 1.15 mycroft /*
1362 1.15 mycroft * Packet forwarding routine once entry in the cache is made
1363 1.15 mycroft */
1364 1.15 mycroft static int
1365 1.15 mycroft #ifdef RSVP_ISI
1366 1.15 mycroft ip_mdq(m, ifp, rt, xmt_vif)
1367 1.15 mycroft #else
1368 1.15 mycroft ip_mdq(m, ifp, rt)
1369 1.15 mycroft #endif /* RSVP_ISI */
1370 1.67 itojun struct mbuf *m;
1371 1.67 itojun struct ifnet *ifp;
1372 1.67 itojun struct mfc *rt;
1373 1.15 mycroft #ifdef RSVP_ISI
1374 1.67 itojun vifi_t xmt_vif;
1375 1.15 mycroft #endif /* RSVP_ISI */
1376 1.1 hpeyerl {
1377 1.67 itojun struct ip *ip = mtod(m, struct ip *);
1378 1.67 itojun vifi_t vifi;
1379 1.67 itojun struct vif *vifp;
1380 1.74 itojun int plen = ntohs(ip->ip_len) - (ip->ip_hl << 2);
1381 1.15 mycroft
1382 1.15 mycroft /*
1383 1.15 mycroft * Macro to send packet on vif. Since RSVP packets don't get counted on
1384 1.15 mycroft * input, they shouldn't get counted on output, so statistics keeping is
1385 1.56 wiz * separate.
1386 1.15 mycroft */
1387 1.70 itojun #define MC_SEND(ip, vifp, m) do { \
1388 1.67 itojun if ((vifp)->v_flags & VIFF_TUNNEL) \
1389 1.67 itojun encap_send((ip), (vifp), (m)); \
1390 1.67 itojun else \
1391 1.67 itojun phyint_send((ip), (vifp), (m)); \
1392 1.68 itojun } while (/*CONSTCOND*/ 0)
1393 1.1 hpeyerl
1394 1.15 mycroft #ifdef RSVP_ISI
1395 1.67 itojun /*
1396 1.67 itojun * If xmt_vif is not -1, send on only the requested vif.
1397 1.67 itojun *
1398 1.67 itojun * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.
1399 1.67 itojun */
1400 1.67 itojun if (xmt_vif < numvifs) {
1401 1.67 itojun MC_SEND(ip, viftable + xmt_vif, m);
1402 1.67 itojun return (1);
1403 1.67 itojun }
1404 1.15 mycroft #endif /* RSVP_ISI */
1405 1.15 mycroft
1406 1.67 itojun /*
1407 1.67 itojun * Don't forward if it didn't arrive from the parent vif for its origin.
1408 1.67 itojun */
1409 1.67 itojun vifi = rt->mfc_parent;
1410 1.67 itojun if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
1411 1.67 itojun /* came in the wrong interface */
1412 1.67 itojun if (mrtdebug & DEBUG_FORWARD)
1413 1.67 itojun log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
1414 1.70 itojun ifp, vifi,
1415 1.70 itojun vifi >= numvifs ? 0 : viftable[vifi].v_ifp);
1416 1.67 itojun ++mrtstat.mrts_wrong_if;
1417 1.67 itojun ++rt->mfc_wrong_if;
1418 1.67 itojun /*
1419 1.67 itojun * If we are doing PIM assert processing, and we are forwarding
1420 1.67 itojun * packets on this interface, and it is a broadcast medium
1421 1.67 itojun * interface (and not a tunnel), send a message to the routing
1422 1.67 itojun * daemon.
1423 1.67 itojun */
1424 1.67 itojun if (pim_assert && rt->mfc_ttls[vifi] &&
1425 1.67 itojun (ifp->if_flags & IFF_BROADCAST) &&
1426 1.67 itojun !(viftable[vifi].v_flags & VIFF_TUNNEL)) {
1427 1.67 itojun struct mbuf *mm;
1428 1.67 itojun struct igmpmsg *im;
1429 1.67 itojun int hlen = ip->ip_hl << 2;
1430 1.67 itojun struct timeval now;
1431 1.67 itojun u_int32_t delta;
1432 1.67 itojun
1433 1.67 itojun microtime(&now);
1434 1.67 itojun
1435 1.67 itojun TV_DELTA(rt->mfc_last_assert, now, delta);
1436 1.67 itojun
1437 1.67 itojun if (delta > ASSERT_MSG_TIME) {
1438 1.67 itojun mm = m_copy(m, 0, hlen);
1439 1.67 itojun M_PULLUP(mm, hlen);
1440 1.67 itojun if (mm == 0) {
1441 1.67 itojun return (ENOBUFS);
1442 1.67 itojun }
1443 1.67 itojun
1444 1.67 itojun rt->mfc_last_assert = now;
1445 1.67 itojun
1446 1.67 itojun im = mtod(mm, struct igmpmsg *);
1447 1.67 itojun im->im_msgtype = IGMPMSG_WRONGVIF;
1448 1.67 itojun im->im_mbz = 0;
1449 1.67 itojun im->im_vif = vifi;
1450 1.15 mycroft
1451 1.67 itojun sin.sin_addr = im->im_src;
1452 1.15 mycroft
1453 1.67 itojun socket_send(ip_mrouter, mm, &sin);
1454 1.67 itojun }
1455 1.67 itojun }
1456 1.67 itojun return (0);
1457 1.15 mycroft }
1458 1.15 mycroft
1459 1.67 itojun /* If I sourced this packet, it counts as output, else it was input. */
1460 1.67 itojun if (in_hosteq(ip->ip_src, viftable[vifi].v_lcl_addr)) {
1461 1.67 itojun viftable[vifi].v_pkt_out++;
1462 1.67 itojun viftable[vifi].v_bytes_out += plen;
1463 1.67 itojun } else {
1464 1.67 itojun viftable[vifi].v_pkt_in++;
1465 1.67 itojun viftable[vifi].v_bytes_in += plen;
1466 1.1 hpeyerl }
1467 1.67 itojun rt->mfc_pkt_cnt++;
1468 1.67 itojun rt->mfc_byte_cnt += plen;
1469 1.67 itojun
1470 1.67 itojun /*
1471 1.67 itojun * For each vif, decide if a copy of the packet should be forwarded.
1472 1.67 itojun * Forward if:
1473 1.67 itojun * - the ttl exceeds the vif's threshold
1474 1.67 itojun * - there are group members downstream on interface
1475 1.67 itojun */
1476 1.67 itojun for (vifp = viftable, vifi = 0; vifi < numvifs; vifp++, vifi++)
1477 1.67 itojun if ((rt->mfc_ttls[vifi] > 0) &&
1478 1.67 itojun (ip->ip_ttl > rt->mfc_ttls[vifi])) {
1479 1.67 itojun vifp->v_pkt_out++;
1480 1.67 itojun vifp->v_bytes_out += plen;
1481 1.67 itojun MC_SEND(ip, vifp, m);
1482 1.67 itojun }
1483 1.1 hpeyerl
1484 1.67 itojun return (0);
1485 1.15 mycroft }
1486 1.15 mycroft
1487 1.15 mycroft #ifdef RSVP_ISI
1488 1.15 mycroft /*
1489 1.15 mycroft * check if a vif number is legal/ok. This is used by ip_output, to export
1490 1.60 itojun * numvifs there,
1491 1.15 mycroft */
1492 1.15 mycroft int
1493 1.15 mycroft legal_vif_num(vif)
1494 1.67 itojun int vif;
1495 1.15 mycroft {
1496 1.67 itojun if (vif >= 0 && vif < numvifs)
1497 1.67 itojun return (1);
1498 1.67 itojun else
1499 1.67 itojun return (0);
1500 1.15 mycroft }
1501 1.15 mycroft #endif /* RSVP_ISI */
1502 1.15 mycroft
1503 1.15 mycroft static void
1504 1.15 mycroft phyint_send(ip, vifp, m)
1505 1.15 mycroft struct ip *ip;
1506 1.15 mycroft struct vif *vifp;
1507 1.15 mycroft struct mbuf *m;
1508 1.15 mycroft {
1509 1.48 augustss struct mbuf *mb_copy;
1510 1.48 augustss int hlen = ip->ip_hl << 2;
1511 1.15 mycroft
1512 1.15 mycroft /*
1513 1.15 mycroft * Make a new reference to the packet; make sure that
1514 1.15 mycroft * the IP header is actually copied, not just referenced,
1515 1.15 mycroft * so that ip_output() only scribbles on the copy.
1516 1.15 mycroft */
1517 1.12 brezak mb_copy = m_copy(m, 0, M_COPYALL);
1518 1.15 mycroft M_PULLUP(mb_copy, hlen);
1519 1.30 mycroft if (mb_copy == 0)
1520 1.1 hpeyerl return;
1521 1.1 hpeyerl
1522 1.15 mycroft if (vifp->v_rate_limit <= 0)
1523 1.15 mycroft tbf_send_packet(vifp, mb_copy);
1524 1.15 mycroft else
1525 1.62 itojun tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *),
1526 1.62 itojun ntohs(ip->ip_len));
1527 1.12 brezak }
1528 1.12 brezak
1529 1.12 brezak static void
1530 1.12 brezak encap_send(ip, vifp, m)
1531 1.48 augustss struct ip *ip;
1532 1.48 augustss struct vif *vifp;
1533 1.48 augustss struct mbuf *m;
1534 1.48 augustss {
1535 1.48 augustss struct mbuf *mb_copy;
1536 1.48 augustss struct ip *ip_copy;
1537 1.62 itojun int i, len = ntohs(ip->ip_len) + sizeof(multicast_encap_iphdr);
1538 1.12 brezak
1539 1.12 brezak /*
1540 1.12 brezak * copy the old packet & pullup it's IP header into the
1541 1.12 brezak * new mbuf so we can modify it. Try to fill the new
1542 1.12 brezak * mbuf since if we don't the ethernet driver will.
1543 1.12 brezak */
1544 1.15 mycroft MGETHDR(mb_copy, M_DONTWAIT, MT_DATA);
1545 1.30 mycroft if (mb_copy == 0)
1546 1.12 brezak return;
1547 1.15 mycroft mb_copy->m_data += max_linkhdr;
1548 1.15 mycroft mb_copy->m_pkthdr.len = len;
1549 1.12 brezak mb_copy->m_len = sizeof(multicast_encap_iphdr);
1550 1.60 itojun
1551 1.30 mycroft if ((mb_copy->m_next = m_copy(m, 0, M_COPYALL)) == 0) {
1552 1.12 brezak m_freem(mb_copy);
1553 1.12 brezak return;
1554 1.12 brezak }
1555 1.15 mycroft i = MHLEN - max_linkhdr;
1556 1.12 brezak if (i > len)
1557 1.12 brezak i = len;
1558 1.12 brezak mb_copy = m_pullup(mb_copy, i);
1559 1.30 mycroft if (mb_copy == 0)
1560 1.12 brezak return;
1561 1.60 itojun
1562 1.12 brezak /*
1563 1.12 brezak * fill in the encapsulating IP header.
1564 1.12 brezak */
1565 1.12 brezak ip_copy = mtod(mb_copy, struct ip *);
1566 1.12 brezak *ip_copy = multicast_encap_iphdr;
1567 1.12 brezak ip_copy->ip_id = htons(ip_id++);
1568 1.62 itojun ip_copy->ip_len = htons(len);
1569 1.12 brezak ip_copy->ip_src = vifp->v_lcl_addr;
1570 1.12 brezak ip_copy->ip_dst = vifp->v_rmt_addr;
1571 1.60 itojun
1572 1.12 brezak /*
1573 1.12 brezak * turn the encapsulated IP header back into a valid one.
1574 1.12 brezak */
1575 1.12 brezak ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
1576 1.12 brezak --ip->ip_ttl;
1577 1.12 brezak ip->ip_sum = 0;
1578 1.12 brezak mb_copy->m_data += sizeof(multicast_encap_iphdr);
1579 1.12 brezak ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
1580 1.12 brezak mb_copy->m_data -= sizeof(multicast_encap_iphdr);
1581 1.60 itojun
1582 1.15 mycroft if (vifp->v_rate_limit <= 0)
1583 1.15 mycroft tbf_send_packet(vifp, mb_copy);
1584 1.15 mycroft else
1585 1.62 itojun tbf_control(vifp, mb_copy, ip, ntohs(ip_copy->ip_len));
1586 1.12 brezak }
1587 1.12 brezak
1588 1.12 brezak /*
1589 1.54 itojun * De-encapsulate a packet and feed it back through ip input.
1590 1.12 brezak */
1591 1.54 itojun static void
1592 1.54 itojun #if __STDC__
1593 1.54 itojun vif_input(struct mbuf *m, ...)
1594 1.54 itojun #else
1595 1.54 itojun vif_input(m, va_alist)
1596 1.25 christos struct mbuf *m;
1597 1.54 itojun va_dcl
1598 1.54 itojun #endif
1599 1.25 christos {
1600 1.54 itojun int off, proto;
1601 1.54 itojun va_list ap;
1602 1.54 itojun struct vif *vifp;
1603 1.48 augustss int s;
1604 1.48 augustss struct ifqueue *ifq;
1605 1.25 christos
1606 1.54 itojun va_start(ap, m);
1607 1.54 itojun off = va_arg(ap, int);
1608 1.54 itojun proto = va_arg(ap, int);
1609 1.54 itojun va_end(ap);
1610 1.22 mycroft
1611 1.54 itojun vifp = (struct vif *)encap_getarg(m);
1612 1.54 itojun if (!vifp || proto != AF_INET) {
1613 1.54 itojun m_freem(m);
1614 1.54 itojun mrtstat.mrts_bad_tunnel++;
1615 1.54 itojun return;
1616 1.12 brezak }
1617 1.22 mycroft
1618 1.54 itojun m_adj(m, off);
1619 1.15 mycroft m->m_pkthdr.rcvif = vifp->v_ifp;
1620 1.12 brezak ifq = &ipintrq;
1621 1.53 thorpej s = splnet();
1622 1.12 brezak if (IF_QFULL(ifq)) {
1623 1.12 brezak IF_DROP(ifq);
1624 1.12 brezak m_freem(m);
1625 1.12 brezak } else {
1626 1.12 brezak IF_ENQUEUE(ifq, m);
1627 1.12 brezak /*
1628 1.12 brezak * normally we would need a "schednetisr(NETISR_IP)"
1629 1.12 brezak * here but we were called by ip_input and it is going
1630 1.12 brezak * to loop back & try to dequeue the packet we just
1631 1.12 brezak * queued as soon as we return so we avoid the
1632 1.12 brezak * unnecessary software interrrupt.
1633 1.12 brezak */
1634 1.12 brezak }
1635 1.12 brezak splx(s);
1636 1.54 itojun }
1637 1.54 itojun
1638 1.54 itojun /*
1639 1.54 itojun * Check if the packet should be grabbed by us.
1640 1.54 itojun */
1641 1.54 itojun static int
1642 1.54 itojun vif_encapcheck(m, off, proto, arg)
1643 1.54 itojun const struct mbuf *m;
1644 1.54 itojun int off;
1645 1.54 itojun int proto;
1646 1.54 itojun void *arg;
1647 1.54 itojun {
1648 1.54 itojun struct vif *vifp;
1649 1.54 itojun struct ip ip;
1650 1.54 itojun
1651 1.54 itojun #ifdef DIAGNOSTIC
1652 1.54 itojun if (!arg || proto != IPPROTO_IPV4)
1653 1.54 itojun panic("unexpected arg in vif_encapcheck");
1654 1.54 itojun #endif
1655 1.54 itojun
1656 1.54 itojun /*
1657 1.54 itojun * do not grab the packet if it's not to a multicast destination or if
1658 1.54 itojun * we don't have an encapsulating tunnel with the source.
1659 1.54 itojun * Note: This code assumes that the remote site IP address
1660 1.54 itojun * uniquely identifies the tunnel (i.e., that this site has
1661 1.54 itojun * at most one tunnel with the remote site).
1662 1.54 itojun */
1663 1.54 itojun
1664 1.54 itojun /* LINTED const cast */
1665 1.54 itojun m_copydata((struct mbuf *)m, off, sizeof(ip), (caddr_t)&ip);
1666 1.54 itojun if (!IN_MULTICAST(ip.ip_dst.s_addr))
1667 1.54 itojun return 0;
1668 1.54 itojun
1669 1.54 itojun /* LINTED const cast */
1670 1.54 itojun m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
1671 1.54 itojun if (!in_hosteq(ip.ip_src, last_encap_src)) {
1672 1.54 itojun vifp = (struct vif *)arg;
1673 1.54 itojun if (vifp->v_flags & VIFF_TUNNEL &&
1674 1.54 itojun in_hosteq(vifp->v_rmt_addr, ip.ip_src))
1675 1.54 itojun ;
1676 1.54 itojun else
1677 1.54 itojun return 0;
1678 1.54 itojun last_encap_vif = vifp;
1679 1.54 itojun last_encap_src = ip.ip_src;
1680 1.54 itojun } else
1681 1.54 itojun vifp = last_encap_vif;
1682 1.54 itojun
1683 1.54 itojun /* 32bit match, since we have checked ip_src only */
1684 1.54 itojun return 32;
1685 1.1 hpeyerl }
1686 1.15 mycroft
1687 1.15 mycroft /*
1688 1.15 mycroft * Token bucket filter module
1689 1.15 mycroft */
1690 1.15 mycroft static void
1691 1.31 mycroft tbf_control(vifp, m, ip, len)
1692 1.48 augustss struct vif *vifp;
1693 1.48 augustss struct mbuf *m;
1694 1.48 augustss struct ip *ip;
1695 1.48 augustss u_int32_t len;
1696 1.15 mycroft {
1697 1.15 mycroft
1698 1.31 mycroft if (len > MAX_BKT_SIZE) {
1699 1.31 mycroft /* drop if packet is too large */
1700 1.31 mycroft mrtstat.mrts_pkt2large++;
1701 1.31 mycroft m_freem(m);
1702 1.31 mycroft return;
1703 1.31 mycroft }
1704 1.31 mycroft
1705 1.21 mycroft tbf_update_tokens(vifp);
1706 1.15 mycroft
1707 1.21 mycroft /*
1708 1.21 mycroft * If there are enough tokens, and the queue is empty, send this packet
1709 1.21 mycroft * out immediately. Otherwise, try to insert it on this vif's queue.
1710 1.21 mycroft */
1711 1.31 mycroft if (vifp->tbf_q_len == 0) {
1712 1.31 mycroft if (len <= vifp->tbf_n_tok) {
1713 1.31 mycroft vifp->tbf_n_tok -= len;
1714 1.21 mycroft tbf_send_packet(vifp, m);
1715 1.21 mycroft } else {
1716 1.21 mycroft /* queue packet and timeout till later */
1717 1.31 mycroft tbf_queue(vifp, m);
1718 1.47 thorpej callout_reset(&vifp->v_repq_ch, TBF_REPROCESS,
1719 1.47 thorpej tbf_reprocess_q, vifp);
1720 1.21 mycroft }
1721 1.15 mycroft } else {
1722 1.31 mycroft if (vifp->tbf_q_len >= vifp->tbf_max_q_len &&
1723 1.21 mycroft !tbf_dq_sel(vifp, ip)) {
1724 1.21 mycroft /* queue length too much, and couldn't make room */
1725 1.21 mycroft mrtstat.mrts_q_overflow++;
1726 1.21 mycroft m_freem(m);
1727 1.21 mycroft } else {
1728 1.21 mycroft /* queue length low enough, or made room */
1729 1.31 mycroft tbf_queue(vifp, m);
1730 1.21 mycroft tbf_process_q(vifp);
1731 1.21 mycroft }
1732 1.15 mycroft }
1733 1.15 mycroft }
1734 1.15 mycroft
1735 1.60 itojun /*
1736 1.15 mycroft * adds a packet to the queue at the interface
1737 1.15 mycroft */
1738 1.15 mycroft static void
1739 1.60 itojun tbf_queue(vifp, m)
1740 1.48 augustss struct vif *vifp;
1741 1.48 augustss struct mbuf *m;
1742 1.15 mycroft {
1743 1.48 augustss int s = splsoftnet();
1744 1.15 mycroft
1745 1.31 mycroft /* insert at tail */
1746 1.31 mycroft *vifp->tbf_t = m;
1747 1.31 mycroft vifp->tbf_t = &m->m_nextpkt;
1748 1.31 mycroft vifp->tbf_q_len++;
1749 1.15 mycroft
1750 1.31 mycroft splx(s);
1751 1.15 mycroft }
1752 1.15 mycroft
1753 1.15 mycroft
1754 1.60 itojun /*
1755 1.15 mycroft * processes the queue at the interface
1756 1.15 mycroft */
1757 1.15 mycroft static void
1758 1.15 mycroft tbf_process_q(vifp)
1759 1.48 augustss struct vif *vifp;
1760 1.15 mycroft {
1761 1.48 augustss struct mbuf *m;
1762 1.48 augustss int len;
1763 1.48 augustss int s = splsoftnet();
1764 1.15 mycroft
1765 1.31 mycroft /*
1766 1.31 mycroft * Loop through the queue at the interface and send as many packets
1767 1.31 mycroft * as possible.
1768 1.31 mycroft */
1769 1.67 itojun for (m = vifp->tbf_q; m != 0; m = vifp->tbf_q) {
1770 1.62 itojun len = ntohs(mtod(m, struct ip *)->ip_len);
1771 1.31 mycroft
1772 1.31 mycroft /* determine if the packet can be sent */
1773 1.31 mycroft if (len <= vifp->tbf_n_tok) {
1774 1.31 mycroft /* if so,
1775 1.31 mycroft * reduce no of tokens, dequeue the packet,
1776 1.31 mycroft * send the packet.
1777 1.31 mycroft */
1778 1.31 mycroft if ((vifp->tbf_q = m->m_nextpkt) == 0)
1779 1.31 mycroft vifp->tbf_t = &vifp->tbf_q;
1780 1.31 mycroft --vifp->tbf_q_len;
1781 1.15 mycroft
1782 1.31 mycroft m->m_nextpkt = 0;
1783 1.31 mycroft vifp->tbf_n_tok -= len;
1784 1.31 mycroft tbf_send_packet(vifp, m);
1785 1.31 mycroft } else
1786 1.31 mycroft break;
1787 1.31 mycroft }
1788 1.31 mycroft splx(s);
1789 1.15 mycroft }
1790 1.15 mycroft
1791 1.15 mycroft static void
1792 1.20 mycroft tbf_reprocess_q(arg)
1793 1.20 mycroft void *arg;
1794 1.15 mycroft {
1795 1.48 augustss struct vif *vifp = arg;
1796 1.15 mycroft
1797 1.60 itojun if (ip_mrouter == 0)
1798 1.20 mycroft return;
1799 1.15 mycroft
1800 1.20 mycroft tbf_update_tokens(vifp);
1801 1.20 mycroft tbf_process_q(vifp);
1802 1.15 mycroft
1803 1.31 mycroft if (vifp->tbf_q_len != 0)
1804 1.47 thorpej callout_reset(&vifp->v_repq_ch, TBF_REPROCESS,
1805 1.47 thorpej tbf_reprocess_q, vifp);
1806 1.15 mycroft }
1807 1.15 mycroft
1808 1.15 mycroft /* function that will selectively discard a member of the queue
1809 1.31 mycroft * based on the precedence value and the priority
1810 1.15 mycroft */
1811 1.15 mycroft static int
1812 1.15 mycroft tbf_dq_sel(vifp, ip)
1813 1.48 augustss struct vif *vifp;
1814 1.48 augustss struct ip *ip;
1815 1.15 mycroft {
1816 1.48 augustss u_int p;
1817 1.48 augustss struct mbuf **mp, *m;
1818 1.48 augustss int s = splsoftnet();
1819 1.31 mycroft
1820 1.31 mycroft p = priority(vifp, ip);
1821 1.31 mycroft
1822 1.31 mycroft for (mp = &vifp->tbf_q, m = *mp;
1823 1.31 mycroft m != 0;
1824 1.31 mycroft mp = &m->m_nextpkt, m = *mp) {
1825 1.31 mycroft if (p > priority(vifp, mtod(m, struct ip *))) {
1826 1.31 mycroft if ((*mp = m->m_nextpkt) == 0)
1827 1.31 mycroft vifp->tbf_t = mp;
1828 1.31 mycroft --vifp->tbf_q_len;
1829 1.31 mycroft
1830 1.31 mycroft m_freem(m);
1831 1.31 mycroft mrtstat.mrts_drop_sel++;
1832 1.31 mycroft splx(s);
1833 1.31 mycroft return (1);
1834 1.31 mycroft }
1835 1.15 mycroft }
1836 1.31 mycroft splx(s);
1837 1.31 mycroft return (0);
1838 1.15 mycroft }
1839 1.15 mycroft
1840 1.15 mycroft static void
1841 1.31 mycroft tbf_send_packet(vifp, m)
1842 1.48 augustss struct vif *vifp;
1843 1.48 augustss struct mbuf *m;
1844 1.15 mycroft {
1845 1.31 mycroft int error;
1846 1.31 mycroft int s = splsoftnet();
1847 1.31 mycroft
1848 1.31 mycroft if (vifp->v_flags & VIFF_TUNNEL) {
1849 1.31 mycroft /* If tunnel options */
1850 1.43 itojun #ifdef IPSEC
1851 1.46 itojun /* Don't lookup socket in forwading case */
1852 1.52 itojun (void)ipsec_setsocket(m, NULL);
1853 1.43 itojun #endif
1854 1.31 mycroft ip_output(m, (struct mbuf *)0, &vifp->v_route,
1855 1.78 itojun IP_FORWARDING, (struct ip_moptions *)NULL,
1856 1.78 itojun (struct socket *)NULL);
1857 1.31 mycroft } else {
1858 1.31 mycroft /* if physical interface option, extract the options and then send */
1859 1.31 mycroft struct ip_moptions imo;
1860 1.15 mycroft
1861 1.31 mycroft imo.imo_multicast_ifp = vifp->v_ifp;
1862 1.31 mycroft imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
1863 1.31 mycroft imo.imo_multicast_loop = 1;
1864 1.15 mycroft #ifdef RSVP_ISI
1865 1.31 mycroft imo.imo_multicast_vif = -1;
1866 1.1 hpeyerl #endif
1867 1.15 mycroft
1868 1.43 itojun #ifdef IPSEC
1869 1.46 itojun /* Don't lookup socket in forwading case */
1870 1.52 itojun (void)ipsec_setsocket(m, NULL);
1871 1.43 itojun #endif
1872 1.31 mycroft error = ip_output(m, (struct mbuf *)0, (struct route *)0,
1873 1.78 itojun IP_FORWARDING|IP_MULTICASTOPTS, &imo,
1874 1.78 itojun (struct socket *)NULL);
1875 1.31 mycroft
1876 1.31 mycroft if (mrtdebug & DEBUG_XMIT)
1877 1.42 nathanw log(LOG_DEBUG, "phyint_send on vif %ld err %d\n",
1878 1.67 itojun (long)(vifp - viftable), error);
1879 1.31 mycroft }
1880 1.31 mycroft splx(s);
1881 1.15 mycroft }
1882 1.15 mycroft
1883 1.15 mycroft /* determine the current time and then
1884 1.15 mycroft * the elapsed time (between the last time and time now)
1885 1.15 mycroft * in milliseconds & update the no. of tokens in the bucket
1886 1.15 mycroft */
1887 1.15 mycroft static void
1888 1.15 mycroft tbf_update_tokens(vifp)
1889 1.48 augustss struct vif *vifp;
1890 1.15 mycroft {
1891 1.31 mycroft struct timeval tp;
1892 1.48 augustss u_int32_t tm;
1893 1.48 augustss int s = splsoftnet();
1894 1.15 mycroft
1895 1.31 mycroft microtime(&tp);
1896 1.15 mycroft
1897 1.31 mycroft TV_DELTA(tp, vifp->tbf_last_pkt_t, tm);
1898 1.15 mycroft
1899 1.31 mycroft /*
1900 1.31 mycroft * This formula is actually
1901 1.31 mycroft * "time in seconds" * "bytes/second".
1902 1.31 mycroft *
1903 1.31 mycroft * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
1904 1.31 mycroft *
1905 1.31 mycroft * The (1000/1024) was introduced in add_vif to optimize
1906 1.31 mycroft * this divide into a shift.
1907 1.31 mycroft */
1908 1.31 mycroft vifp->tbf_n_tok += tm * vifp->v_rate_limit / 8192;
1909 1.31 mycroft vifp->tbf_last_pkt_t = tp;
1910 1.15 mycroft
1911 1.31 mycroft if (vifp->tbf_n_tok > MAX_BKT_SIZE)
1912 1.31 mycroft vifp->tbf_n_tok = MAX_BKT_SIZE;
1913 1.15 mycroft
1914 1.31 mycroft splx(s);
1915 1.15 mycroft }
1916 1.15 mycroft
1917 1.15 mycroft static int
1918 1.15 mycroft priority(vifp, ip)
1919 1.67 itojun struct vif *vifp;
1920 1.67 itojun struct ip *ip;
1921 1.15 mycroft {
1922 1.67 itojun int prio;
1923 1.15 mycroft
1924 1.67 itojun /* temporary hack; may add general packet classifier some day */
1925 1.60 itojun
1926 1.67 itojun /*
1927 1.67 itojun * The UDP port space is divided up into four priority ranges:
1928 1.67 itojun * [0, 16384) : unclassified - lowest priority
1929 1.67 itojun * [16384, 32768) : audio - highest priority
1930 1.67 itojun * [32768, 49152) : whiteboard - medium priority
1931 1.67 itojun * [49152, 65536) : video - low priority
1932 1.67 itojun */
1933 1.67 itojun if (ip->ip_p == IPPROTO_UDP) {
1934 1.67 itojun struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
1935 1.15 mycroft
1936 1.67 itojun switch (ntohs(udp->uh_dport) & 0xc000) {
1937 1.67 itojun case 0x4000:
1938 1.67 itojun prio = 70;
1939 1.67 itojun break;
1940 1.67 itojun case 0x8000:
1941 1.67 itojun prio = 60;
1942 1.67 itojun break;
1943 1.67 itojun case 0xc000:
1944 1.67 itojun prio = 55;
1945 1.67 itojun break;
1946 1.67 itojun default:
1947 1.67 itojun prio = 50;
1948 1.67 itojun break;
1949 1.67 itojun }
1950 1.15 mycroft
1951 1.67 itojun if (tbfdebug > 1)
1952 1.67 itojun log(LOG_DEBUG, "port %x prio %d\n",
1953 1.67 itojun ntohs(udp->uh_dport), prio);
1954 1.67 itojun } else
1955 1.67 itojun prio = 50;
1956 1.15 mycroft
1957 1.67 itojun return (prio);
1958 1.15 mycroft }
1959 1.15 mycroft
1960 1.15 mycroft /*
1961 1.60 itojun * End of token bucket filter modifications
1962 1.15 mycroft */
1963 1.15 mycroft #ifdef RSVP_ISI
1964 1.15 mycroft int
1965 1.15 mycroft ip_rsvp_vif_init(so, m)
1966 1.67 itojun struct socket *so;
1967 1.67 itojun struct mbuf *m;
1968 1.15 mycroft {
1969 1.67 itojun int i;
1970 1.67 itojun int s;
1971 1.15 mycroft
1972 1.67 itojun if (rsvpdebug)
1973 1.67 itojun printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
1974 1.67 itojun so->so_type, so->so_proto->pr_protocol);
1975 1.15 mycroft
1976 1.67 itojun if (so->so_type != SOCK_RAW ||
1977 1.67 itojun so->so_proto->pr_protocol != IPPROTO_RSVP)
1978 1.67 itojun return (EOPNOTSUPP);
1979 1.15 mycroft
1980 1.67 itojun /* Check mbuf. */
1981 1.67 itojun if (m == 0 || m->m_len != sizeof(int)) {
1982 1.67 itojun return (EINVAL);
1983 1.67 itojun }
1984 1.67 itojun i = *(mtod(m, int *));
1985 1.15 mycroft
1986 1.67 itojun if (rsvpdebug)
1987 1.70 itojun printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n", i, rsvp_on);
1988 1.15 mycroft
1989 1.67 itojun s = splsoftnet();
1990 1.15 mycroft
1991 1.67 itojun /* Check vif. */
1992 1.67 itojun if (!legal_vif_num(i)) {
1993 1.67 itojun splx(s);
1994 1.67 itojun return (EADDRNOTAVAIL);
1995 1.67 itojun }
1996 1.15 mycroft
1997 1.67 itojun /* Check if socket is available. */
1998 1.67 itojun if (viftable[i].v_rsvpd != 0) {
1999 1.67 itojun splx(s);
2000 1.67 itojun return (EADDRINUSE);
2001 1.67 itojun }
2002 1.15 mycroft
2003 1.67 itojun viftable[i].v_rsvpd = so;
2004 1.67 itojun /*
2005 1.67 itojun * This may seem silly, but we need to be sure we don't over-increment
2006 1.67 itojun * the RSVP counter, in case something slips up.
2007 1.67 itojun */
2008 1.67 itojun if (!viftable[i].v_rsvp_on) {
2009 1.67 itojun viftable[i].v_rsvp_on = 1;
2010 1.67 itojun rsvp_on++;
2011 1.67 itojun }
2012 1.15 mycroft
2013 1.67 itojun splx(s);
2014 1.67 itojun return (0);
2015 1.15 mycroft }
2016 1.15 mycroft
2017 1.15 mycroft int
2018 1.15 mycroft ip_rsvp_vif_done(so, m)
2019 1.67 itojun struct socket *so;
2020 1.67 itojun struct mbuf *m;
2021 1.15 mycroft {
2022 1.67 itojun int i;
2023 1.67 itojun int s;
2024 1.15 mycroft
2025 1.67 itojun if (rsvpdebug)
2026 1.67 itojun printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n",
2027 1.67 itojun so->so_type, so->so_proto->pr_protocol);
2028 1.15 mycroft
2029 1.67 itojun if (so->so_type != SOCK_RAW ||
2030 1.67 itojun so->so_proto->pr_protocol != IPPROTO_RSVP)
2031 1.67 itojun return (EOPNOTSUPP);
2032 1.15 mycroft
2033 1.67 itojun /* Check mbuf. */
2034 1.67 itojun if (m == 0 || m->m_len != sizeof(int)) {
2035 1.67 itojun return (EINVAL);
2036 1.67 itojun }
2037 1.67 itojun i = *(mtod(m, int *));
2038 1.15 mycroft
2039 1.67 itojun s = splsoftnet();
2040 1.15 mycroft
2041 1.67 itojun /* Check vif. */
2042 1.67 itojun if (!legal_vif_num(i)) {
2043 1.67 itojun splx(s);
2044 1.67 itojun return (EADDRNOTAVAIL);
2045 1.67 itojun }
2046 1.15 mycroft
2047 1.67 itojun if (rsvpdebug)
2048 1.67 itojun printf("ip_rsvp_vif_done: v_rsvpd = %x so = %x\n",
2049 1.67 itojun viftable[i].v_rsvpd, so);
2050 1.15 mycroft
2051 1.67 itojun viftable[i].v_rsvpd = 0;
2052 1.67 itojun /*
2053 1.67 itojun * This may seem silly, but we need to be sure we don't over-decrement
2054 1.67 itojun * the RSVP counter, in case something slips up.
2055 1.67 itojun */
2056 1.67 itojun if (viftable[i].v_rsvp_on) {
2057 1.67 itojun viftable[i].v_rsvp_on = 0;
2058 1.67 itojun rsvp_on--;
2059 1.67 itojun }
2060 1.67 itojun
2061 1.67 itojun splx(s);
2062 1.67 itojun return (0);
2063 1.15 mycroft }
2064 1.15 mycroft
2065 1.25 christos void
2066 1.15 mycroft ip_rsvp_force_done(so)
2067 1.67 itojun struct socket *so;
2068 1.15 mycroft {
2069 1.67 itojun int vifi;
2070 1.67 itojun int s;
2071 1.15 mycroft
2072 1.67 itojun /* Don't bother if it is not the right type of socket. */
2073 1.67 itojun if (so->so_type != SOCK_RAW ||
2074 1.67 itojun so->so_proto->pr_protocol != IPPROTO_RSVP)
2075 1.67 itojun return;
2076 1.15 mycroft
2077 1.67 itojun s = splsoftnet();
2078 1.15 mycroft
2079 1.67 itojun /*
2080 1.67 itojun * The socket may be attached to more than one vif...this
2081 1.67 itojun * is perfectly legal.
2082 1.67 itojun */
2083 1.67 itojun for (vifi = 0; vifi < numvifs; vifi++) {
2084 1.67 itojun if (viftable[vifi].v_rsvpd == so) {
2085 1.67 itojun viftable[vifi].v_rsvpd = 0;
2086 1.67 itojun /*
2087 1.67 itojun * This may seem silly, but we need to be sure we don't
2088 1.67 itojun * over-decrement the RSVP counter, in case something
2089 1.67 itojun * slips up.
2090 1.67 itojun */
2091 1.67 itojun if (viftable[vifi].v_rsvp_on) {
2092 1.67 itojun viftable[vifi].v_rsvp_on = 0;
2093 1.67 itojun rsvp_on--;
2094 1.67 itojun }
2095 1.67 itojun }
2096 1.15 mycroft }
2097 1.15 mycroft
2098 1.67 itojun splx(s);
2099 1.67 itojun return;
2100 1.15 mycroft }
2101 1.15 mycroft
2102 1.25 christos void
2103 1.15 mycroft rsvp_input(m, ifp)
2104 1.67 itojun struct mbuf *m;
2105 1.67 itojun struct ifnet *ifp;
2106 1.15 mycroft {
2107 1.67 itojun int vifi;
2108 1.67 itojun struct ip *ip = mtod(m, struct ip *);
2109 1.67 itojun static struct sockaddr_in rsvp_src = { sizeof(sin), AF_INET };
2110 1.67 itojun int s;
2111 1.15 mycroft
2112 1.15 mycroft if (rsvpdebug)
2113 1.70 itojun printf("rsvp_input: rsvp_on %d\n", rsvp_on);
2114 1.15 mycroft
2115 1.67 itojun /*
2116 1.67 itojun * Can still get packets with rsvp_on = 0 if there is a local member
2117 1.67 itojun * of the group to which the RSVP packet is addressed. But in this
2118 1.67 itojun * case we want to throw the packet away.
2119 1.67 itojun */
2120 1.67 itojun if (!rsvp_on) {
2121 1.67 itojun m_freem(m);
2122 1.67 itojun return;
2123 1.67 itojun }
2124 1.15 mycroft
2125 1.67 itojun /*
2126 1.67 itojun * If the old-style non-vif-associated socket is set, then use
2127 1.67 itojun * it and ignore the new ones.
2128 1.67 itojun */
2129 1.67 itojun if (ip_rsvpd != 0) {
2130 1.67 itojun if (rsvpdebug)
2131 1.67 itojun printf("rsvp_input: "
2132 1.67 itojun "Sending packet up old-style socket\n");
2133 1.67 itojun rip_input(m); /*XXX*/
2134 1.67 itojun return;
2135 1.67 itojun }
2136 1.15 mycroft
2137 1.67 itojun s = splsoftnet();
2138 1.15 mycroft
2139 1.15 mycroft if (rsvpdebug)
2140 1.67 itojun printf("rsvp_input: check vifs\n");
2141 1.67 itojun
2142 1.67 itojun /* Find which vif the packet arrived on. */
2143 1.67 itojun for (vifi = 0; vifi < numvifs; vifi++) {
2144 1.67 itojun if (viftable[vifi].v_ifp == ifp)
2145 1.67 itojun break;
2146 1.67 itojun }
2147 1.15 mycroft
2148 1.67 itojun if (vifi == numvifs) {
2149 1.67 itojun /* Can't find vif packet arrived on. Drop packet. */
2150 1.67 itojun if (rsvpdebug)
2151 1.67 itojun printf("rsvp_input: "
2152 1.67 itojun "Can't find vif for packet...dropping it.\n");
2153 1.67 itojun m_freem(m);
2154 1.67 itojun splx(s);
2155 1.67 itojun return;
2156 1.67 itojun }
2157 1.15 mycroft
2158 1.15 mycroft if (rsvpdebug)
2159 1.67 itojun printf("rsvp_input: check socket\n");
2160 1.15 mycroft
2161 1.67 itojun if (viftable[vifi].v_rsvpd == 0) {
2162 1.67 itojun /*
2163 1.67 itojun * drop packet, since there is no specific socket for this
2164 1.67 itojun * interface
2165 1.67 itojun */
2166 1.67 itojun if (rsvpdebug)
2167 1.67 itojun printf("rsvp_input: No socket defined for vif %d\n",
2168 1.67 itojun vifi);
2169 1.67 itojun m_freem(m);
2170 1.67 itojun splx(s);
2171 1.67 itojun return;
2172 1.67 itojun }
2173 1.15 mycroft
2174 1.67 itojun rsvp_src.sin_addr = ip->ip_src;
2175 1.15 mycroft
2176 1.67 itojun if (rsvpdebug && m)
2177 1.67 itojun printf("rsvp_input: m->m_len = %d, sbspace() = %d\n",
2178 1.70 itojun m->m_len, sbspace(&viftable[vifi].v_rsvpd->so_rcv));
2179 1.67 itojun
2180 1.67 itojun if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0)
2181 1.67 itojun if (rsvpdebug)
2182 1.67 itojun printf("rsvp_input: Failed to append to socket\n");
2183 1.67 itojun else
2184 1.67 itojun if (rsvpdebug)
2185 1.67 itojun printf("rsvp_input: send packet up\n");
2186 1.60 itojun
2187 1.67 itojun splx(s);
2188 1.15 mycroft }
2189 1.15 mycroft #endif /* RSVP_ISI */
2190