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