]>
Commit | Line | Data |
---|---|---|
2d21ac55 | 1 | /* |
b0d623f7 | 2 | * Copyright (c) 2003-2008 Apple Inc. All rights reserved. |
2d21ac55 A |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
27 | */ | |
55e303ae A |
28 | /* $FreeBSD: src/sys/netinet6/ip6_mroute.c,v 1.16.2.1 2002/12/18 21:39:40 suz Exp $ */ |
29 | /* $KAME: ip6_mroute.c,v 1.58 2001/12/18 02:36:31 itojun Exp $ */ | |
1c79356b A |
30 | |
31 | /* | |
32 | * Copyright (C) 1998 WIDE Project. | |
33 | * All rights reserved. | |
34 | * | |
35 | * Redistribution and use in source and binary forms, with or without | |
36 | * modification, are permitted provided that the following conditions | |
37 | * are met: | |
38 | * 1. Redistributions of source code must retain the above copyright | |
39 | * notice, this list of conditions and the following disclaimer. | |
40 | * 2. Redistributions in binary form must reproduce the above copyright | |
41 | * notice, this list of conditions and the following disclaimer in the | |
42 | * documentation and/or other materials provided with the distribution. | |
43 | * 3. Neither the name of the project nor the names of its contributors | |
44 | * may be used to endorse or promote products derived from this software | |
45 | * without specific prior written permission. | |
46 | * | |
47 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
48 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
49 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
50 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
51 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
52 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
53 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
54 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
55 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
56 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
57 | * SUCH DAMAGE. | |
58 | */ | |
2d21ac55 A |
59 | /* |
60 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
61 | * support for mandatory and extensible security protections. This notice | |
62 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
63 | * Version 2.0. | |
64 | */ | |
1c79356b A |
65 | |
66 | /* BSDI ip_mroute.c,v 2.10 1996/11/14 00:29:52 jch Exp */ | |
67 | ||
68 | /* | |
69 | * IP multicast forwarding procedures | |
70 | * | |
71 | * Written by David Waitzman, BBN Labs, August 1988. | |
72 | * Modified by Steve Deering, Stanford, February 1989. | |
73 | * Modified by Mark J. Steiglitz, Stanford, May, 1991 | |
74 | * Modified by Van Jacobson, LBL, January 1993 | |
75 | * Modified by Ajit Thyagarajan, PARC, August 1993 | |
76 | * Modified by Bill Fenenr, PARC, April 1994 | |
77 | * | |
78 | * MROUTING Revision: 3.5.1.2 + PIM-SMv2 (pimd) Support | |
79 | */ | |
80 | ||
1c79356b A |
81 | |
82 | #include <sys/param.h> | |
83 | #include <sys/systm.h> | |
1c79356b | 84 | #include <sys/malloc.h> |
1c79356b A |
85 | #include <sys/mbuf.h> |
86 | #include <sys/socket.h> | |
87 | #include <sys/socketvar.h> | |
88 | #include <sys/sockio.h> | |
89 | #include <sys/protosw.h> | |
90 | #include <sys/errno.h> | |
91 | #include <sys/time.h> | |
92 | #include <sys/kernel.h> | |
1c79356b | 93 | #include <sys/syslog.h> |
91447636 | 94 | #include <kern/locks.h> |
1c79356b A |
95 | |
96 | #include <net/if.h> | |
97 | #include <net/route.h> | |
98 | #include <net/raw_cb.h> | |
2d21ac55 A |
99 | #include <net/dlil.h> |
100 | #include <net/net_osdep.h> | |
1c79356b A |
101 | |
102 | #include <netinet/in.h> | |
103 | #include <netinet/in_var.h> | |
104 | ||
105 | #include <netinet/ip6.h> | |
106 | #include <netinet6/ip6_var.h> | |
107 | #include <netinet6/ip6_mroute.h> | |
108 | #include <netinet6/pim6.h> | |
109 | #include <netinet6/pim6_var.h> | |
110 | ||
2d21ac55 A |
111 | #if CONFIG_MACF_NET |
112 | #include <security/mac.h> | |
113 | #endif /* MAC_NET */ | |
114 | ||
9bccf70c | 115 | #ifndef __APPLE__ |
1c79356b A |
116 | static MALLOC_DEFINE(M_MRTABLE, "mf6c", "multicast forwarding cache entry"); |
117 | #endif | |
118 | ||
119 | #define M_HASCL(m) ((m)->m_flags & M_EXT) | |
120 | ||
91447636 A |
121 | static int ip6_mdq(struct mbuf *, struct ifnet *, struct mf6c *); |
122 | static void phyint_send(struct ip6_hdr *, struct mif6 *, struct mbuf *); | |
1c79356b | 123 | |
91447636 A |
124 | static int set_pim6(int *); |
125 | static int socket_send(struct socket *, struct mbuf *, | |
126 | struct sockaddr_in6 *); | |
127 | static int register_send(struct ip6_hdr *, struct mif6 *, | |
128 | struct mbuf *); | |
1c79356b | 129 | |
91447636 | 130 | extern lck_mtx_t *ip6_mutex; |
1c79356b A |
131 | /* |
132 | * Globals. All but ip6_mrouter, ip6_mrtproto and mrt6stat could be static, | |
133 | * except for netstat or debugging purposes. | |
134 | */ | |
135 | struct socket *ip6_mrouter = NULL; | |
9bccf70c | 136 | int ip6_mrouter_ver = 0; |
1c79356b | 137 | int ip6_mrtproto = IPPROTO_PIM; /* for netstat only */ |
b7266188 A |
138 | |
139 | #if MROUTING | |
140 | ||
1c79356b A |
141 | struct mrt6stat mrt6stat; |
142 | ||
143 | #define NO_RTE_FOUND 0x1 | |
144 | #define RTE_FOUND 0x2 | |
145 | ||
146 | struct mf6c *mf6ctable[MF6CTBLSIZ]; | |
55e303ae | 147 | u_char n6expire[MF6CTBLSIZ]; |
1c79356b A |
148 | static struct mif6 mif6table[MAXMIFS]; |
149 | #if MRT6DEBUG | |
150 | u_int mrt6debug = 0; /* debug level */ | |
151 | #define DEBUG_MFC 0x02 | |
152 | #define DEBUG_FORWARD 0x04 | |
153 | #define DEBUG_EXPIRE 0x08 | |
154 | #define DEBUG_XMIT 0x10 | |
155 | #define DEBUG_REG 0x20 | |
156 | #define DEBUG_PIM 0x40 | |
157 | #endif | |
158 | ||
91447636 | 159 | static void expire_upcalls(void *); |
9bccf70c | 160 | |
1c79356b A |
161 | #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */ |
162 | #define UPCALL_EXPIRE 6 /* number of timeouts */ | |
163 | ||
164 | #if INET | |
165 | #if MROUTING | |
166 | extern struct socket *ip_mrouter; | |
167 | #endif | |
168 | #endif | |
169 | ||
1c79356b A |
170 | /* |
171 | * 'Interfaces' associated with decapsulator (so we can tell | |
172 | * packets that went through it from ones that get reflected | |
173 | * by a broken gateway). These interfaces are never linked into | |
174 | * the system ifnet list & no routes point to them. I.e., packets | |
175 | * can't be sent this way. They only exist as a placeholder for | |
176 | * multicast source verification. | |
177 | */ | |
178 | struct ifnet multicast_register_if; | |
179 | ||
180 | #define ENCAP_HOPS 64 | |
181 | ||
182 | /* | |
183 | * Private variables. | |
184 | */ | |
185 | static mifi_t nummifs = 0; | |
186 | static mifi_t reg_mif_num = (mifi_t)-1; | |
187 | ||
188 | static struct pim6stat pim6stat; | |
1c79356b A |
189 | static int pim6; |
190 | ||
191 | /* | |
192 | * Hash function for a source, group entry | |
193 | */ | |
194 | #define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \ | |
195 | (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \ | |
196 | (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \ | |
197 | (g).s6_addr32[2] ^ (g).s6_addr32[3]) | |
198 | ||
199 | /* | |
200 | * Find a route for a given origin IPv6 address and Multicast group address. | |
201 | * Quality of service parameter to be added in the future!!! | |
202 | */ | |
203 | ||
9bccf70c A |
204 | #define MF6CFIND(o, g, rt) do { \ |
205 | struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \ | |
1c79356b A |
206 | rt = NULL; \ |
207 | mrt6stat.mrt6s_mfc_lookups++; \ | |
208 | while (_rt) { \ | |
209 | if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \ | |
210 | IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \ | |
211 | (_rt->mf6c_stall == NULL)) { \ | |
212 | rt = _rt; \ | |
213 | break; \ | |
214 | } \ | |
215 | _rt = _rt->mf6c_next; \ | |
216 | } \ | |
217 | if (rt == NULL) { \ | |
218 | mrt6stat.mrt6s_mfc_misses++; \ | |
219 | } \ | |
9bccf70c | 220 | } while (0) |
1c79356b A |
221 | |
222 | /* | |
223 | * Macros to compute elapsed time efficiently | |
224 | * Borrowed from Van Jacobson's scheduling code | |
225 | */ | |
9bccf70c A |
226 | #define TV_DELTA(a, b, delta) do { \ |
227 | int xxs; \ | |
1c79356b A |
228 | \ |
229 | delta = (a).tv_usec - (b).tv_usec; \ | |
230 | if ((xxs = (a).tv_sec - (b).tv_sec)) { \ | |
231 | switch (xxs) { \ | |
232 | case 2: \ | |
233 | delta += 1000000; \ | |
234 | /* fall through */ \ | |
235 | case 1: \ | |
236 | delta += 1000000; \ | |
237 | break; \ | |
238 | default: \ | |
239 | delta += (1000000 * xxs); \ | |
240 | } \ | |
241 | } \ | |
9bccf70c | 242 | } while (0) |
1c79356b A |
243 | |
244 | #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \ | |
245 | (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec) | |
246 | ||
247 | #if UPCALL_TIMING | |
248 | #define UPCALL_MAX 50 | |
b0d623f7 | 249 | u_int32_t upcall_data[UPCALL_MAX + 1]; |
1c79356b A |
250 | static void collate(); |
251 | #endif /* UPCALL_TIMING */ | |
252 | ||
91447636 | 253 | static int get_sg_cnt(struct sioc_sg_req6 *); |
b0d623f7 | 254 | static int get_mif6_cnt(void *, int); |
91447636 A |
255 | static int ip6_mrouter_init(struct socket *, int, int); |
256 | static int add_m6if(struct mif6ctl *); | |
257 | static int del_m6if(mifi_t *); | |
258 | static int add_m6fc(struct mf6cctl *); | |
259 | static int del_m6fc(struct mf6cctl *); | |
1c79356b | 260 | |
9bccf70c A |
261 | #ifndef __APPLE__ |
262 | static struct callout expire_upcalls_ch; | |
263 | #endif | |
1c79356b A |
264 | /* |
265 | * Handle MRT setsockopt commands to modify the multicast routing tables. | |
266 | */ | |
1c79356b A |
267 | int |
268 | ip6_mrouter_set(so, sopt) | |
269 | struct socket *so; | |
270 | struct sockopt *sopt; | |
271 | { | |
91447636 A |
272 | int error = 0; |
273 | int optval; | |
274 | struct mif6ctl mifc; | |
275 | struct mf6cctl mfcc; | |
276 | mifi_t mifi; | |
1c79356b A |
277 | |
278 | if (so != ip6_mrouter && sopt->sopt_name != MRT6_INIT) | |
279 | return (EACCES); | |
280 | ||
1c79356b | 281 | switch (sopt->sopt_name) { |
9bccf70c A |
282 | case MRT6_INIT: |
283 | #if MRT6_OINIT | |
284 | case MRT6_OINIT: | |
285 | #endif | |
91447636 A |
286 | error = sooptcopyin(sopt, &optval, sizeof(optval), |
287 | sizeof(optval)); | |
288 | if (error) | |
289 | break; | |
290 | error = ip6_mrouter_init(so, optval, sopt->sopt_name); | |
9bccf70c A |
291 | break; |
292 | case MRT6_DONE: | |
293 | error = ip6_mrouter_done(); | |
294 | break; | |
295 | case MRT6_ADD_MIF: | |
91447636 A |
296 | error = sooptcopyin(sopt, &mifc, sizeof(mifc), sizeof(mifc)); |
297 | if (error) | |
298 | break; | |
299 | error = add_m6if(&mifc); | |
9bccf70c A |
300 | break; |
301 | case MRT6_ADD_MFC: | |
91447636 A |
302 | error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc)); |
303 | if (error) | |
304 | break; | |
305 | error = add_m6fc(&mfcc); | |
9bccf70c A |
306 | break; |
307 | case MRT6_DEL_MFC: | |
91447636 A |
308 | error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc)); |
309 | if (error) | |
310 | break; | |
311 | error = del_m6fc(&mfcc); | |
312 | break; | |
313 | case MRT6_DEL_MIF: | |
314 | error = sooptcopyin(sopt, &mifi, sizeof(mifi), sizeof(mifi)); | |
315 | if (error) | |
316 | break; | |
317 | error = del_m6if(&mifi); | |
9bccf70c A |
318 | break; |
319 | case MRT6_PIM: | |
91447636 A |
320 | error = sooptcopyin(sopt, &optval, sizeof(optval), |
321 | sizeof(optval)); | |
322 | if (error) | |
323 | break; | |
324 | error = set_pim6(&optval); | |
9bccf70c A |
325 | break; |
326 | default: | |
327 | error = EOPNOTSUPP; | |
328 | break; | |
1c79356b A |
329 | } |
330 | ||
91447636 | 331 | return (error); |
1c79356b | 332 | } |
1c79356b A |
333 | |
334 | /* | |
335 | * Handle MRT getsockopt commands | |
336 | */ | |
1c79356b A |
337 | int |
338 | ip6_mrouter_get(so, sopt) | |
339 | struct socket *so; | |
340 | struct sockopt *sopt; | |
341 | { | |
342 | int error = 0; | |
343 | ||
344 | if (so != ip6_mrouter) return EACCES; | |
345 | ||
346 | switch (sopt->sopt_name) { | |
347 | case MRT6_PIM: | |
348 | error = sooptcopyout(sopt, &pim6, sizeof(pim6)); | |
349 | break; | |
350 | } | |
351 | return (error); | |
352 | } | |
1c79356b A |
353 | |
354 | /* | |
355 | * Handle ioctl commands to obtain information from the cache | |
356 | */ | |
357 | int | |
b0d623f7 | 358 | mrt6_ioctl(u_long cmd, caddr_t data) |
1c79356b | 359 | { |
9bccf70c A |
360 | int error = 0; |
361 | ||
362 | switch (cmd) { | |
363 | case SIOCGETSGCNT_IN6: | |
b0d623f7 A |
364 | return (get_sg_cnt((struct sioc_sg_req6 *)data)); |
365 | /* NOTREACHED */ | |
366 | ||
367 | case SIOCGETMIFCNT_IN6_32: | |
368 | case SIOCGETMIFCNT_IN6_64: | |
369 | return (get_mif6_cnt(data, cmd == SIOCGETMIFCNT_IN6_64)); | |
370 | /* NOTREACHED */ | |
371 | ||
9bccf70c | 372 | default: |
b0d623f7 | 373 | error = EINVAL; |
9bccf70c A |
374 | break; |
375 | } | |
b0d623f7 | 376 | return (error); |
1c79356b A |
377 | } |
378 | ||
379 | /* | |
380 | * returns the packet, byte, rpf-failure count for the source group provided | |
381 | */ | |
382 | static int | |
383 | get_sg_cnt(req) | |
9bccf70c | 384 | struct sioc_sg_req6 *req; |
1c79356b | 385 | { |
9bccf70c | 386 | struct mf6c *rt; |
1c79356b | 387 | |
1c79356b | 388 | MF6CFIND(req->src.sin6_addr, req->grp.sin6_addr, rt); |
1c79356b A |
389 | if (rt != NULL) { |
390 | req->pktcnt = rt->mf6c_pkt_cnt; | |
391 | req->bytecnt = rt->mf6c_byte_cnt; | |
392 | req->wrong_if = rt->mf6c_wrong_if; | |
393 | } else | |
394 | return(ESRCH); | |
395 | #if 0 | |
396 | req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff; | |
397 | #endif | |
398 | ||
399 | return 0; | |
400 | } | |
401 | ||
402 | /* | |
403 | * returns the input and output packet and byte counts on the mif provided | |
404 | */ | |
405 | static int | |
b0d623f7 | 406 | get_mif6_cnt(void *data, int p64) |
1c79356b | 407 | { |
b0d623f7 A |
408 | if (p64) { |
409 | struct sioc_mif_req6_64 *req = data; | |
1c79356b | 410 | |
b0d623f7 | 411 | mifi_t mifi = req->mifi; |
1c79356b | 412 | |
b0d623f7 A |
413 | if (mifi >= nummifs) |
414 | return (EINVAL); | |
1c79356b | 415 | |
b0d623f7 A |
416 | req->icount = mif6table[mifi].m6_pkt_in; |
417 | req->ocount = mif6table[mifi].m6_pkt_out; | |
418 | req->ibytes = mif6table[mifi].m6_bytes_in; | |
419 | req->obytes = mif6table[mifi].m6_bytes_out; | |
420 | } else { | |
421 | struct sioc_mif_req6_32 *req = data; | |
422 | ||
423 | mifi_t mifi = req->mifi; | |
424 | ||
425 | if (mifi >= nummifs) | |
426 | return (EINVAL); | |
427 | ||
428 | req->icount = mif6table[mifi].m6_pkt_in; | |
429 | req->ocount = mif6table[mifi].m6_pkt_out; | |
430 | req->ibytes = mif6table[mifi].m6_bytes_in; | |
431 | req->obytes = mif6table[mifi].m6_bytes_out; | |
432 | } | |
433 | return (0); | |
1c79356b A |
434 | } |
435 | ||
1c79356b A |
436 | static int |
437 | set_pim6(i) | |
438 | int *i; | |
439 | { | |
440 | if ((*i != 1) && (*i != 0)) | |
441 | return EINVAL; | |
442 | ||
443 | pim6 = *i; | |
444 | ||
445 | return 0; | |
446 | } | |
447 | ||
448 | /* | |
449 | * Enable multicast routing | |
450 | */ | |
451 | static int | |
91447636 | 452 | ip6_mrouter_init(so, v, cmd) |
1c79356b | 453 | struct socket *so; |
91447636 | 454 | int v; |
9bccf70c | 455 | int cmd; |
1c79356b | 456 | { |
1c79356b A |
457 | #if MRT6DEBUG |
458 | if (mrt6debug) | |
459 | log(LOG_DEBUG, | |
460 | "ip6_mrouter_init: so_type = %d, pr_protocol = %d\n", | |
461 | so->so_type, so->so_proto->pr_protocol); | |
462 | #endif | |
463 | ||
464 | if (so->so_type != SOCK_RAW || | |
465 | so->so_proto->pr_protocol != IPPROTO_ICMPV6) | |
466 | return EOPNOTSUPP; | |
467 | ||
91447636 A |
468 | if (v != 1) |
469 | return (ENOPROTOOPT); | |
1c79356b A |
470 | |
471 | if (ip6_mrouter != NULL) return EADDRINUSE; | |
472 | ||
473 | ip6_mrouter = so; | |
9bccf70c | 474 | ip6_mrouter_ver = cmd; |
1c79356b A |
475 | |
476 | bzero((caddr_t)mf6ctable, sizeof(mf6ctable)); | |
55e303ae | 477 | bzero((caddr_t)n6expire, sizeof(n6expire)); |
1c79356b A |
478 | |
479 | pim6 = 0;/* used for stubbing out/in pim stuff */ | |
480 | ||
9bccf70c A |
481 | #ifndef __APPLE__ |
482 | callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, | |
483 | expire_upcalls, NULL); | |
484 | #else | |
91447636 | 485 | timeout(expire_upcalls, (caddr_t)NULL, EXPIRE_TIMEOUT); |
1c79356b | 486 | #endif |
1c79356b A |
487 | |
488 | #if MRT6DEBUG | |
489 | if (mrt6debug) | |
490 | log(LOG_DEBUG, "ip6_mrouter_init\n"); | |
491 | #endif | |
492 | ||
493 | return 0; | |
494 | } | |
495 | ||
496 | /* | |
497 | * Disable multicast routing | |
498 | */ | |
499 | int | |
500 | ip6_mrouter_done() | |
501 | { | |
502 | mifi_t mifi; | |
503 | int i; | |
1c79356b A |
504 | struct mf6c *rt; |
505 | struct rtdetq *rte; | |
1c79356b | 506 | |
1c79356b A |
507 | |
508 | /* | |
509 | * For each phyint in use, disable promiscuous reception of all IPv6 | |
510 | * multicasts. | |
511 | */ | |
512 | #if INET | |
513 | #if MROUTING | |
514 | /* | |
515 | * If there is still IPv4 multicast routing daemon, | |
516 | * we remain interfaces to receive all muliticasted packets. | |
517 | * XXX: there may be an interface in which the IPv4 multicast | |
518 | * daemon is not interested... | |
519 | */ | |
520 | if (!ip_mrouter) | |
521 | #endif | |
522 | #endif | |
523 | { | |
524 | for (mifi = 0; mifi < nummifs; mifi++) { | |
525 | if (mif6table[mifi].m6_ifp && | |
526 | !(mif6table[mifi].m6_flags & MIFF_REGISTER)) { | |
1c79356b | 527 | #ifdef __APPLE__ |
2d21ac55 | 528 | if_allmulti(mif6table[mifi].m6_ifp, 0); |
1c79356b | 529 | #else |
2d21ac55 A |
530 | { |
531 | struct ifnet *ifp; | |
532 | struct in6_ifreq ifr; | |
533 | ||
534 | ifr.ifr_addr.sin6_family = AF_INET6; | |
535 | ifr.ifr_addr.sin6_addr= in6addr_any; | |
536 | ifp = mif6table[mifi].m6_ifp; | |
537 | ifnet_ioctl(ifp, 0, SIOCDELMULTI, &ifr); | |
538 | } | |
1c79356b A |
539 | #endif |
540 | } | |
541 | } | |
542 | } | |
543 | #if notyet | |
544 | bzero((caddr_t)qtable, sizeof(qtable)); | |
545 | bzero((caddr_t)tbftable, sizeof(tbftable)); | |
546 | #endif | |
547 | bzero((caddr_t)mif6table, sizeof(mif6table)); | |
548 | nummifs = 0; | |
549 | ||
550 | pim6 = 0; /* used to stub out/in pim specific code */ | |
551 | ||
9bccf70c A |
552 | #ifndef __APPLE__ |
553 | callout_stop(&expire_upcalls_ch); | |
554 | #else | |
91447636 | 555 | untimeout(expire_upcalls, (caddr_t)NULL); |
1c79356b | 556 | #endif |
1c79356b A |
557 | |
558 | /* | |
559 | * Free all multicast forwarding cache entries. | |
91447636 | 560 | *###LD 5/27 needs locking |
1c79356b A |
561 | */ |
562 | for (i = 0; i < MF6CTBLSIZ; i++) { | |
563 | rt = mf6ctable[i]; | |
564 | while (rt) { | |
565 | struct mf6c *frt; | |
566 | ||
567 | for (rte = rt->mf6c_stall; rte != NULL; ) { | |
568 | struct rtdetq *n = rte->next; | |
569 | ||
570 | m_free(rte->m); | |
9bccf70c | 571 | FREE(rte, M_MRTABLE); |
1c79356b A |
572 | rte = n; |
573 | } | |
574 | frt = rt; | |
575 | rt = rt->mf6c_next; | |
9bccf70c | 576 | FREE(frt, M_MRTABLE); |
1c79356b A |
577 | } |
578 | } | |
579 | ||
580 | bzero((caddr_t)mf6ctable, sizeof(mf6ctable)); | |
581 | ||
582 | /* | |
583 | * Reset de-encapsulation cache | |
584 | */ | |
585 | reg_mif_num = -1; | |
586 | ||
587 | ip6_mrouter = NULL; | |
9bccf70c | 588 | ip6_mrouter_ver = 0; |
1c79356b | 589 | |
1c79356b A |
590 | |
591 | #if MRT6DEBUG | |
592 | if (mrt6debug) | |
593 | log(LOG_DEBUG, "ip6_mrouter_done\n"); | |
594 | #endif | |
595 | ||
596 | return 0; | |
597 | } | |
598 | ||
2d21ac55 A |
599 | static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 , |
600 | 0, 0, IN6ADDR_ANY_INIT, 0}; | |
1c79356b A |
601 | |
602 | /* | |
603 | * Add a mif to the mif table | |
604 | */ | |
605 | static int | |
606 | add_m6if(mifcp) | |
9bccf70c | 607 | struct mif6ctl *mifcp; |
1c79356b | 608 | { |
9bccf70c | 609 | struct mif6 *mifp; |
1c79356b | 610 | struct ifnet *ifp; |
91447636 | 611 | int error; |
1c79356b A |
612 | #if notyet |
613 | struct tbf *m_tbf = tbftable + mifcp->mif6c_mifi; | |
614 | #endif | |
615 | ||
616 | if (mifcp->mif6c_mifi >= MAXMIFS) | |
617 | return EINVAL; | |
618 | mifp = mif6table + mifcp->mif6c_mifi; | |
619 | if (mifp->m6_ifp) | |
620 | return EADDRINUSE; /* XXX: is it appropriate? */ | |
b0d623f7 A |
621 | |
622 | ifnet_head_lock_shared(); | |
623 | if (mifcp->mif6c_pifi == 0 || mifcp->mif6c_pifi > if_index) { | |
624 | ifnet_head_done(); | |
1c79356b | 625 | return ENXIO; |
b0d623f7 | 626 | } |
1c79356b | 627 | ifp = ifindex2ifnet[mifcp->mif6c_pifi]; |
b0d623f7 | 628 | ifnet_head_done(); |
1c79356b | 629 | |
b0d623f7 A |
630 | if (ifp == NULL) { |
631 | return ENXIO; | |
632 | } | |
1c79356b A |
633 | if (mifcp->mif6c_flags & MIFF_REGISTER) { |
634 | if (reg_mif_num == (mifi_t)-1) { | |
1c79356b | 635 | multicast_register_if.if_name = "register_mif"; |
1c79356b A |
636 | multicast_register_if.if_flags |= IFF_LOOPBACK; |
637 | multicast_register_if.if_index = mifcp->mif6c_mifi; | |
638 | reg_mif_num = mifcp->mif6c_mifi; | |
639 | } | |
640 | ||
641 | ifp = &multicast_register_if; | |
642 | ||
643 | } /* if REGISTER */ | |
644 | else { | |
645 | /* Make sure the interface supports multicast */ | |
646 | if ((ifp->if_flags & IFF_MULTICAST) == 0) | |
647 | return EOPNOTSUPP; | |
648 | ||
1c79356b | 649 | error = if_allmulti(ifp, 1); |
1c79356b A |
650 | if (error) |
651 | return error; | |
652 | } | |
653 | ||
1c79356b A |
654 | mifp->m6_flags = mifcp->mif6c_flags; |
655 | mifp->m6_ifp = ifp; | |
656 | #if notyet | |
657 | /* scaling up here allows division by 1024 in critical code */ | |
658 | mifp->m6_rate_limit = mifcp->mif6c_rate_limit * 1024 / 1000; | |
659 | #endif | |
660 | /* initialize per mif pkt counters */ | |
661 | mifp->m6_pkt_in = 0; | |
662 | mifp->m6_pkt_out = 0; | |
663 | mifp->m6_bytes_in = 0; | |
664 | mifp->m6_bytes_out = 0; | |
1c79356b A |
665 | |
666 | /* Adjust nummifs up if the mifi is higher than nummifs */ | |
667 | if (nummifs <= mifcp->mif6c_mifi) | |
668 | nummifs = mifcp->mif6c_mifi + 1; | |
669 | ||
670 | #if MRT6DEBUG | |
671 | if (mrt6debug) | |
672 | log(LOG_DEBUG, | |
673 | "add_mif #%d, phyint %s%d\n", | |
674 | mifcp->mif6c_mifi, | |
675 | ifp->if_name, ifp->if_unit); | |
676 | #endif | |
677 | ||
678 | return 0; | |
679 | } | |
680 | ||
681 | /* | |
682 | * Delete a mif from the mif table | |
683 | */ | |
684 | static int | |
685 | del_m6if(mifip) | |
686 | mifi_t *mifip; | |
687 | { | |
9bccf70c A |
688 | struct mif6 *mifp = mif6table + *mifip; |
689 | mifi_t mifi; | |
1c79356b | 690 | struct ifnet *ifp; |
1c79356b A |
691 | |
692 | if (*mifip >= nummifs) | |
693 | return EINVAL; | |
694 | if (mifp->m6_ifp == NULL) | |
695 | return EINVAL; | |
696 | ||
1c79356b A |
697 | |
698 | if (!(mifp->m6_flags & MIFF_REGISTER)) { | |
699 | /* | |
700 | * XXX: what if there is yet IPv4 multicast daemon | |
701 | * using the interface? | |
702 | */ | |
703 | ifp = mifp->m6_ifp; | |
704 | ||
1c79356b | 705 | if_allmulti(ifp, 0); |
1c79356b A |
706 | } |
707 | ||
708 | #if notyet | |
709 | bzero((caddr_t)qtable[*mifip], sizeof(qtable[*mifip])); | |
710 | bzero((caddr_t)mifp->m6_tbf, sizeof(*(mifp->m6_tbf))); | |
711 | #endif | |
91447636 | 712 | bzero((caddr_t)mifp, sizeof(*mifp)); |
1c79356b A |
713 | |
714 | /* Adjust nummifs down */ | |
715 | for (mifi = nummifs; mifi > 0; mifi--) | |
716 | if (mif6table[mifi - 1].m6_ifp) | |
717 | break; | |
718 | nummifs = mifi; | |
719 | ||
1c79356b A |
720 | |
721 | #if MRT6DEBUG | |
722 | if (mrt6debug) | |
723 | log(LOG_DEBUG, "del_m6if %d, nummifs %d\n", *mifip, nummifs); | |
724 | #endif | |
725 | ||
726 | return 0; | |
727 | } | |
728 | ||
729 | /* | |
730 | * Add an mfc entry | |
731 | */ | |
732 | static int | |
733 | add_m6fc(mfccp) | |
734 | struct mf6cctl *mfccp; | |
735 | { | |
736 | struct mf6c *rt; | |
b0d623f7 | 737 | u_int32_t hash; |
1c79356b | 738 | struct rtdetq *rte; |
9bccf70c | 739 | u_short nstl; |
1c79356b A |
740 | |
741 | MF6CFIND(mfccp->mf6cc_origin.sin6_addr, | |
742 | mfccp->mf6cc_mcastgrp.sin6_addr, rt); | |
743 | ||
744 | /* If an entry already exists, just update the fields */ | |
745 | if (rt) { | |
746 | #if MRT6DEBUG | |
747 | if (mrt6debug & DEBUG_MFC) | |
55e303ae A |
748 | log(LOG_DEBUG, |
749 | "add_m6fc no upcall h %d o %s g %s p %x\n", | |
1c79356b A |
750 | ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr), |
751 | ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr), | |
752 | mfccp->mf6cc_parent); | |
753 | #endif | |
754 | ||
1c79356b A |
755 | rt->mf6c_parent = mfccp->mf6cc_parent; |
756 | rt->mf6c_ifset = mfccp->mf6cc_ifset; | |
1c79356b A |
757 | return 0; |
758 | } | |
759 | ||
760 | /* | |
761 | * Find the entry for which the upcall was made and update | |
762 | */ | |
1c79356b A |
763 | hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr, |
764 | mfccp->mf6cc_mcastgrp.sin6_addr); | |
765 | for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) { | |
766 | if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr, | |
767 | &mfccp->mf6cc_origin.sin6_addr) && | |
768 | IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr, | |
769 | &mfccp->mf6cc_mcastgrp.sin6_addr) && | |
770 | (rt->mf6c_stall != NULL)) { | |
771 | ||
772 | if (nstl++) | |
773 | log(LOG_ERR, | |
774 | "add_m6fc: %s o %s g %s p %x dbx %p\n", | |
775 | "multiple kernel entries", | |
776 | ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr), | |
777 | ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr), | |
778 | mfccp->mf6cc_parent, rt->mf6c_stall); | |
779 | ||
780 | #if MRT6DEBUG | |
781 | if (mrt6debug & DEBUG_MFC) | |
782 | log(LOG_DEBUG, | |
783 | "add_m6fc o %s g %s p %x dbg %x\n", | |
784 | ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr), | |
785 | ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr), | |
786 | mfccp->mf6cc_parent, rt->mf6c_stall); | |
787 | #endif | |
788 | ||
789 | rt->mf6c_origin = mfccp->mf6cc_origin; | |
790 | rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp; | |
791 | rt->mf6c_parent = mfccp->mf6cc_parent; | |
792 | rt->mf6c_ifset = mfccp->mf6cc_ifset; | |
793 | /* initialize pkt counters per src-grp */ | |
794 | rt->mf6c_pkt_cnt = 0; | |
795 | rt->mf6c_byte_cnt = 0; | |
796 | rt->mf6c_wrong_if = 0; | |
797 | ||
798 | rt->mf6c_expire = 0; /* Don't clean this guy up */ | |
55e303ae | 799 | n6expire[hash]--; |
1c79356b A |
800 | |
801 | /* free packets Qed at the end of this entry */ | |
802 | for (rte = rt->mf6c_stall; rte != NULL; ) { | |
803 | struct rtdetq *n = rte->next; | |
804 | ip6_mdq(rte->m, rte->ifp, rt); | |
805 | m_freem(rte->m); | |
806 | #if UPCALL_TIMING | |
807 | collate(&(rte->t)); | |
808 | #endif /* UPCALL_TIMING */ | |
9bccf70c | 809 | FREE(rte, M_MRTABLE); |
1c79356b A |
810 | rte = n; |
811 | } | |
812 | rt->mf6c_stall = NULL; | |
813 | } | |
814 | } | |
815 | ||
816 | /* | |
817 | * It is possible that an entry is being inserted without an upcall | |
818 | */ | |
819 | if (nstl == 0) { | |
820 | #if MRT6DEBUG | |
821 | if (mrt6debug & DEBUG_MFC) | |
822 | log(LOG_DEBUG,"add_mfc no upcall h %d o %s g %s p %x\n", | |
823 | hash, | |
824 | ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr), | |
825 | ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr), | |
826 | mfccp->mf6cc_parent); | |
827 | #endif | |
828 | ||
829 | for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) { | |
830 | ||
831 | if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr, | |
832 | &mfccp->mf6cc_origin.sin6_addr)&& | |
833 | IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr, | |
834 | &mfccp->mf6cc_mcastgrp.sin6_addr)) { | |
835 | ||
836 | rt->mf6c_origin = mfccp->mf6cc_origin; | |
837 | rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp; | |
838 | rt->mf6c_parent = mfccp->mf6cc_parent; | |
9bccf70c | 839 | rt->mf6c_ifset = mfccp->mf6cc_ifset; |
1c79356b A |
840 | /* initialize pkt counters per src-grp */ |
841 | rt->mf6c_pkt_cnt = 0; | |
842 | rt->mf6c_byte_cnt = 0; | |
843 | rt->mf6c_wrong_if = 0; | |
844 | ||
845 | if (rt->mf6c_expire) | |
55e303ae | 846 | n6expire[hash]--; |
1c79356b A |
847 | rt->mf6c_expire = 0; |
848 | } | |
849 | } | |
850 | if (rt == NULL) { | |
851 | /* no upcall, so make a new entry */ | |
852 | rt = (struct mf6c *)_MALLOC(sizeof(*rt), M_MRTABLE, | |
853 | M_NOWAIT); | |
854 | if (rt == NULL) { | |
1c79356b A |
855 | return ENOBUFS; |
856 | } | |
857 | ||
858 | /* insert new entry at head of hash chain */ | |
859 | rt->mf6c_origin = mfccp->mf6cc_origin; | |
860 | rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp; | |
861 | rt->mf6c_parent = mfccp->mf6cc_parent; | |
9bccf70c | 862 | rt->mf6c_ifset = mfccp->mf6cc_ifset; |
1c79356b A |
863 | /* initialize pkt counters per src-grp */ |
864 | rt->mf6c_pkt_cnt = 0; | |
865 | rt->mf6c_byte_cnt = 0; | |
866 | rt->mf6c_wrong_if = 0; | |
867 | rt->mf6c_expire = 0; | |
868 | rt->mf6c_stall = NULL; | |
869 | ||
870 | /* link into table */ | |
871 | rt->mf6c_next = mf6ctable[hash]; | |
872 | mf6ctable[hash] = rt; | |
873 | } | |
874 | } | |
1c79356b A |
875 | return 0; |
876 | } | |
877 | ||
878 | #if UPCALL_TIMING | |
879 | /* | |
880 | * collect delay statistics on the upcalls | |
881 | */ | |
882 | static void | |
883 | collate(t) | |
9bccf70c | 884 | struct timeval *t; |
1c79356b | 885 | { |
b0d623f7 | 886 | u_int32_t d; |
9bccf70c | 887 | struct timeval tp; |
b0d623f7 | 888 | u_int32_t delta; |
1c79356b A |
889 | |
890 | GET_TIME(tp); | |
891 | ||
892 | if (TV_LT(*t, tp)) | |
893 | { | |
894 | TV_DELTA(tp, *t, delta); | |
895 | ||
896 | d = delta >> 10; | |
897 | if (d > UPCALL_MAX) | |
898 | d = UPCALL_MAX; | |
899 | ||
900 | ++upcall_data[d]; | |
901 | } | |
902 | } | |
903 | #endif /* UPCALL_TIMING */ | |
904 | ||
905 | /* | |
906 | * Delete an mfc entry | |
907 | */ | |
908 | static int | |
909 | del_m6fc(mfccp) | |
910 | struct mf6cctl *mfccp; | |
911 | { | |
912 | struct sockaddr_in6 origin; | |
913 | struct sockaddr_in6 mcastgrp; | |
914 | struct mf6c *rt; | |
915 | struct mf6c **nptr; | |
b0d623f7 | 916 | u_int32_t hash; |
1c79356b A |
917 | |
918 | origin = mfccp->mf6cc_origin; | |
919 | mcastgrp = mfccp->mf6cc_mcastgrp; | |
920 | hash = MF6CHASH(origin.sin6_addr, mcastgrp.sin6_addr); | |
921 | ||
922 | #if MRT6DEBUG | |
923 | if (mrt6debug & DEBUG_MFC) | |
924 | log(LOG_DEBUG,"del_m6fc orig %s mcastgrp %s\n", | |
925 | ip6_sprintf(&origin.sin6_addr), | |
926 | ip6_sprintf(&mcastgrp.sin6_addr)); | |
927 | #endif | |
928 | ||
1c79356b A |
929 | |
930 | nptr = &mf6ctable[hash]; | |
931 | while ((rt = *nptr) != NULL) { | |
932 | if (IN6_ARE_ADDR_EQUAL(&origin.sin6_addr, | |
933 | &rt->mf6c_origin.sin6_addr) && | |
934 | IN6_ARE_ADDR_EQUAL(&mcastgrp.sin6_addr, | |
935 | &rt->mf6c_mcastgrp.sin6_addr) && | |
936 | rt->mf6c_stall == NULL) | |
937 | break; | |
938 | ||
939 | nptr = &rt->mf6c_next; | |
940 | } | |
941 | if (rt == NULL) { | |
1c79356b A |
942 | return EADDRNOTAVAIL; |
943 | } | |
944 | ||
945 | *nptr = rt->mf6c_next; | |
9bccf70c | 946 | FREE(rt, M_MRTABLE); |
1c79356b | 947 | |
1c79356b A |
948 | |
949 | return 0; | |
950 | } | |
951 | ||
952 | static int | |
953 | socket_send(s, mm, src) | |
954 | struct socket *s; | |
955 | struct mbuf *mm; | |
956 | struct sockaddr_in6 *src; | |
957 | { | |
91447636 A |
958 | //### LD 5/27/04 needs locking! |
959 | // | |
1c79356b A |
960 | if (s) { |
961 | if (sbappendaddr(&s->so_rcv, | |
962 | (struct sockaddr *)src, | |
91447636 | 963 | mm, (struct mbuf *)0, NULL) != 0) { |
1c79356b A |
964 | sorwakeup(s); |
965 | return 0; | |
966 | } | |
967 | } | |
1c79356b A |
968 | return -1; |
969 | } | |
970 | ||
971 | /* | |
972 | * IPv6 multicast forwarding function. This function assumes that the packet | |
973 | * pointed to by "ip6" has arrived on (or is about to be sent to) the interface | |
974 | * pointed to by "ifp", and the packet is to be relayed to other networks | |
975 | * that have members of the packet's destination IPv6 multicast group. | |
976 | * | |
977 | * The packet is returned unscathed to the caller, unless it is | |
978 | * erroneous, in which case a non-zero return value tells the caller to | |
979 | * discard it. | |
980 | */ | |
981 | ||
982 | int | |
983 | ip6_mforward(ip6, ifp, m) | |
9bccf70c | 984 | struct ip6_hdr *ip6; |
1c79356b A |
985 | struct ifnet *ifp; |
986 | struct mbuf *m; | |
987 | { | |
9bccf70c A |
988 | struct mf6c *rt; |
989 | struct mif6 *mifp; | |
990 | struct mbuf *mm; | |
1c79356b | 991 | mifi_t mifi; |
91447636 | 992 | struct timeval timenow; |
1c79356b A |
993 | |
994 | #if MRT6DEBUG | |
995 | if (mrt6debug & DEBUG_FORWARD) | |
996 | log(LOG_DEBUG, "ip6_mforward: src %s, dst %s, ifindex %d\n", | |
997 | ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst), | |
998 | ifp->if_index); | |
999 | #endif | |
1000 | ||
1001 | /* | |
1002 | * Don't forward a packet with Hop limit of zero or one, | |
1003 | * or a packet destined to a local-only group. | |
1004 | */ | |
1005 | if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst) || | |
1006 | IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) | |
1007 | return 0; | |
1008 | ip6->ip6_hlim--; | |
1009 | ||
9bccf70c A |
1010 | /* |
1011 | * Source address check: do not forward packets with unspecified | |
1012 | * source. It was discussed in July 2000, on ipngwg mailing list. | |
1013 | * This is rather more serious than unicast cases, because some | |
1014 | * MLD packets can be sent with the unspecified source address | |
1015 | * (although such packets must normally set 1 to the hop limit field). | |
1016 | */ | |
91447636 | 1017 | getmicrotime(&timenow); |
9bccf70c A |
1018 | if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { |
1019 | ip6stat.ip6s_cantforward++; | |
91447636 A |
1020 | if (ip6_log_time + ip6_log_interval < timenow.tv_sec) { |
1021 | ip6_log_time = timenow.tv_sec; | |
9bccf70c A |
1022 | log(LOG_DEBUG, |
1023 | "cannot forward " | |
1024 | "from %s to %s nxt %d received on %s\n", | |
1025 | ip6_sprintf(&ip6->ip6_src), | |
1026 | ip6_sprintf(&ip6->ip6_dst), | |
1027 | ip6->ip6_nxt, | |
1028 | if_name(m->m_pkthdr.rcvif)); | |
1029 | } | |
1030 | return 0; | |
1031 | } | |
1032 | ||
1c79356b A |
1033 | /* |
1034 | * Determine forwarding mifs from the forwarding cache table | |
1035 | */ | |
1c79356b A |
1036 | MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt); |
1037 | ||
1038 | /* Entry exists, so forward if necessary */ | |
1039 | if (rt) { | |
1c79356b A |
1040 | return (ip6_mdq(m, ifp, rt)); |
1041 | } else { | |
1042 | /* | |
1043 | * If we don't have a route for packet's origin, | |
1044 | * Make a copy of the packet & | |
1045 | * send message to routing daemon | |
1046 | */ | |
1047 | ||
9bccf70c A |
1048 | struct mbuf *mb0; |
1049 | struct rtdetq *rte; | |
b0d623f7 | 1050 | u_int32_t hash; |
9bccf70c | 1051 | /* int i, npkts;*/ |
1c79356b A |
1052 | #if UPCALL_TIMING |
1053 | struct timeval tp; | |
1054 | ||
1055 | GET_TIME(tp); | |
1056 | #endif /* UPCALL_TIMING */ | |
1057 | ||
1058 | mrt6stat.mrt6s_no_route++; | |
1059 | #if MRT6DEBUG | |
1060 | if (mrt6debug & (DEBUG_FORWARD | DEBUG_MFC)) | |
1061 | log(LOG_DEBUG, "ip6_mforward: no rte s %s g %s\n", | |
1062 | ip6_sprintf(&ip6->ip6_src), | |
1063 | ip6_sprintf(&ip6->ip6_dst)); | |
1064 | #endif | |
1065 | ||
1066 | /* | |
1067 | * Allocate mbufs early so that we don't do extra work if we | |
1068 | * are just going to fail anyway. | |
1069 | */ | |
1070 | rte = (struct rtdetq *)_MALLOC(sizeof(*rte), M_MRTABLE, | |
1071 | M_NOWAIT); | |
1072 | if (rte == NULL) { | |
1c79356b A |
1073 | return ENOBUFS; |
1074 | } | |
1075 | mb0 = m_copy(m, 0, M_COPYALL); | |
1076 | /* | |
1077 | * Pullup packet header if needed before storing it, | |
1078 | * as other references may modify it in the meantime. | |
1079 | */ | |
1080 | if (mb0 && | |
1081 | (M_HASCL(mb0) || mb0->m_len < sizeof(struct ip6_hdr))) | |
1082 | mb0 = m_pullup(mb0, sizeof(struct ip6_hdr)); | |
1083 | if (mb0 == NULL) { | |
9bccf70c | 1084 | FREE(rte, M_MRTABLE); |
1c79356b A |
1085 | return ENOBUFS; |
1086 | } | |
1087 | ||
1088 | /* is there an upcall waiting for this packet? */ | |
1089 | hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst); | |
1090 | for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) { | |
1091 | if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, | |
1092 | &rt->mf6c_origin.sin6_addr) && | |
1093 | IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, | |
1094 | &rt->mf6c_mcastgrp.sin6_addr) && | |
1095 | (rt->mf6c_stall != NULL)) | |
1096 | break; | |
1097 | } | |
1098 | ||
1099 | if (rt == NULL) { | |
1100 | struct mrt6msg *im; | |
9bccf70c A |
1101 | #if MRT6_OINIT |
1102 | struct omrt6msg *oim; | |
1103 | #endif | |
1c79356b A |
1104 | |
1105 | /* no upcall, so make a new entry */ | |
1106 | rt = (struct mf6c *)_MALLOC(sizeof(*rt), M_MRTABLE, | |
1107 | M_NOWAIT); | |
1108 | if (rt == NULL) { | |
9bccf70c | 1109 | FREE(rte, M_MRTABLE); |
1c79356b | 1110 | m_freem(mb0); |
1c79356b A |
1111 | return ENOBUFS; |
1112 | } | |
1113 | /* | |
1114 | * Make a copy of the header to send to the user | |
1115 | * level process | |
1116 | */ | |
1117 | mm = m_copy(mb0, 0, sizeof(struct ip6_hdr)); | |
1118 | ||
1119 | if (mm == NULL) { | |
9bccf70c | 1120 | FREE(rte, M_MRTABLE); |
1c79356b | 1121 | m_freem(mb0); |
9bccf70c | 1122 | FREE(rt, M_MRTABLE); |
1c79356b A |
1123 | return ENOBUFS; |
1124 | } | |
1125 | ||
1126 | /* | |
1127 | * Send message to routing daemon | |
1128 | */ | |
1129 | sin6.sin6_addr = ip6->ip6_src; | |
1130 | ||
9bccf70c A |
1131 | im = NULL; |
1132 | #if MRT6_OINIT | |
1133 | oim = NULL; | |
1134 | #endif | |
1135 | switch (ip6_mrouter_ver) { | |
1136 | #if MRT6_OINIT | |
1137 | case MRT6_OINIT: | |
1138 | oim = mtod(mm, struct omrt6msg *); | |
1139 | oim->im6_msgtype = MRT6MSG_NOCACHE; | |
1140 | oim->im6_mbz = 0; | |
1141 | break; | |
1142 | #endif | |
1143 | case MRT6_INIT: | |
1144 | im = mtod(mm, struct mrt6msg *); | |
1145 | im->im6_msgtype = MRT6MSG_NOCACHE; | |
1146 | im->im6_mbz = 0; | |
1147 | break; | |
1148 | default: | |
1149 | FREE(rte, M_MRTABLE); | |
1150 | m_freem(mb0); | |
1151 | FREE(rt, M_MRTABLE); | |
9bccf70c A |
1152 | return EINVAL; |
1153 | } | |
1c79356b A |
1154 | |
1155 | #if MRT6DEBUG | |
1156 | if (mrt6debug & DEBUG_FORWARD) | |
1157 | log(LOG_DEBUG, | |
1158 | "getting the iif info in the kernel\n"); | |
1159 | #endif | |
1160 | ||
1161 | for (mifp = mif6table, mifi = 0; | |
1162 | mifi < nummifs && mifp->m6_ifp != ifp; | |
1163 | mifp++, mifi++) | |
1164 | ; | |
1165 | ||
9bccf70c A |
1166 | switch (ip6_mrouter_ver) { |
1167 | #if MRT6_OINIT | |
1168 | case MRT6_OINIT: | |
1169 | oim->im6_mif = mifi; | |
1170 | break; | |
1171 | #endif | |
1172 | case MRT6_INIT: | |
1173 | im->im6_mif = mifi; | |
1174 | break; | |
1175 | } | |
1c79356b A |
1176 | |
1177 | if (socket_send(ip6_mrouter, mm, &sin6) < 0) { | |
1178 | log(LOG_WARNING, "ip6_mforward: ip6_mrouter " | |
1179 | "socket queue full\n"); | |
1180 | mrt6stat.mrt6s_upq_sockfull++; | |
9bccf70c | 1181 | FREE(rte, M_MRTABLE); |
1c79356b | 1182 | m_freem(mb0); |
9bccf70c | 1183 | FREE(rt, M_MRTABLE); |
1c79356b A |
1184 | return ENOBUFS; |
1185 | } | |
1186 | ||
1187 | mrt6stat.mrt6s_upcalls++; | |
1188 | ||
1189 | /* insert new entry at head of hash chain */ | |
1190 | bzero(rt, sizeof(*rt)); | |
1191 | rt->mf6c_origin.sin6_family = AF_INET6; | |
1192 | rt->mf6c_origin.sin6_len = sizeof(struct sockaddr_in6); | |
1193 | rt->mf6c_origin.sin6_addr = ip6->ip6_src; | |
1194 | rt->mf6c_mcastgrp.sin6_family = AF_INET6; | |
1195 | rt->mf6c_mcastgrp.sin6_len = sizeof(struct sockaddr_in6); | |
1196 | rt->mf6c_mcastgrp.sin6_addr = ip6->ip6_dst; | |
1197 | rt->mf6c_expire = UPCALL_EXPIRE; | |
55e303ae | 1198 | n6expire[hash]++; |
1c79356b A |
1199 | rt->mf6c_parent = MF6C_INCOMPLETE_PARENT; |
1200 | ||
1201 | /* link into table */ | |
1202 | rt->mf6c_next = mf6ctable[hash]; | |
1203 | mf6ctable[hash] = rt; | |
1204 | /* Add this entry to the end of the queue */ | |
1205 | rt->mf6c_stall = rte; | |
1206 | } else { | |
1207 | /* determine if q has overflowed */ | |
1208 | struct rtdetq **p; | |
9bccf70c | 1209 | int npkts = 0; |
1c79356b A |
1210 | |
1211 | for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next) | |
1212 | if (++npkts > MAX_UPQ6) { | |
1213 | mrt6stat.mrt6s_upq_ovflw++; | |
9bccf70c | 1214 | FREE(rte, M_MRTABLE); |
1c79356b | 1215 | m_freem(mb0); |
1c79356b A |
1216 | return 0; |
1217 | } | |
1218 | ||
1219 | /* Add this entry to the end of the queue */ | |
1220 | *p = rte; | |
1221 | } | |
1222 | ||
1223 | rte->next = NULL; | |
1224 | rte->m = mb0; | |
1225 | rte->ifp = ifp; | |
1226 | #if UPCALL_TIMING | |
1227 | rte->t = tp; | |
1228 | #endif /* UPCALL_TIMING */ | |
1229 | ||
1c79356b A |
1230 | |
1231 | return 0; | |
1232 | } | |
1233 | } | |
1234 | ||
9bccf70c A |
1235 | /* |
1236 | * Clean up cache entries if upcalls are not serviced | |
1237 | * Call from the Slow Timeout mechanism, every half second. | |
1238 | */ | |
1c79356b | 1239 | static void |
2d21ac55 A |
1240 | expire_upcalls( |
1241 | __unused void *unused) | |
1c79356b A |
1242 | { |
1243 | struct rtdetq *rte; | |
1244 | struct mf6c *mfc, **nptr; | |
1245 | int i; | |
1c79356b | 1246 | |
1c79356b | 1247 | for (i = 0; i < MF6CTBLSIZ; i++) { |
55e303ae | 1248 | if (n6expire[i] == 0) |
1c79356b A |
1249 | continue; |
1250 | nptr = &mf6ctable[i]; | |
1251 | while ((mfc = *nptr) != NULL) { | |
1252 | rte = mfc->mf6c_stall; | |
1253 | /* | |
1254 | * Skip real cache entries | |
1255 | * Make sure it wasn't marked to not expire (shouldn't happen) | |
1256 | * If it expires now | |
1257 | */ | |
1258 | if (rte != NULL && | |
1259 | mfc->mf6c_expire != 0 && | |
1260 | --mfc->mf6c_expire == 0) { | |
1261 | #if MRT6DEBUG | |
1262 | if (mrt6debug & DEBUG_EXPIRE) | |
1263 | log(LOG_DEBUG, "expire_upcalls: expiring (%s %s)\n", | |
1264 | ip6_sprintf(&mfc->mf6c_origin.sin6_addr), | |
1265 | ip6_sprintf(&mfc->mf6c_mcastgrp.sin6_addr)); | |
1266 | #endif | |
1267 | /* | |
1268 | * drop all the packets | |
1269 | * free the mbuf with the pkt, if, timing info | |
1270 | */ | |
1271 | do { | |
1272 | struct rtdetq *n = rte->next; | |
1273 | m_freem(rte->m); | |
9bccf70c | 1274 | FREE(rte, M_MRTABLE); |
1c79356b A |
1275 | rte = n; |
1276 | } while (rte != NULL); | |
1277 | mrt6stat.mrt6s_cache_cleanups++; | |
55e303ae | 1278 | n6expire[i]--; |
1c79356b A |
1279 | |
1280 | *nptr = mfc->mf6c_next; | |
9bccf70c | 1281 | FREE(mfc, M_MRTABLE); |
1c79356b A |
1282 | } else { |
1283 | nptr = &mfc->mf6c_next; | |
1284 | } | |
1285 | } | |
1286 | } | |
9bccf70c A |
1287 | |
1288 | #ifndef __APPLE__ | |
1289 | callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, | |
1290 | expire_upcalls, NULL); | |
1291 | #else | |
91447636 | 1292 | timeout(expire_upcalls, (caddr_t)NULL, EXPIRE_TIMEOUT); |
9bccf70c | 1293 | #endif |
1c79356b A |
1294 | } |
1295 | ||
1296 | /* | |
1297 | * Packet forwarding routine once entry in the cache is made | |
1298 | */ | |
1299 | static int | |
1300 | ip6_mdq(m, ifp, rt) | |
9bccf70c A |
1301 | struct mbuf *m; |
1302 | struct ifnet *ifp; | |
1303 | struct mf6c *rt; | |
1c79356b | 1304 | { |
9bccf70c A |
1305 | struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); |
1306 | mifi_t mifi, iif; | |
1307 | struct mif6 *mifp; | |
1308 | int plen = m->m_pkthdr.len; | |
1c79356b A |
1309 | |
1310 | /* | |
1311 | * Macro to send packet on mif. Since RSVP packets don't get counted on | |
1312 | * input, they shouldn't get counted on output, so statistics keeping is | |
55e303ae | 1313 | * separate. |
1c79356b A |
1314 | */ |
1315 | ||
9bccf70c A |
1316 | #define MC6_SEND(ip6, mifp, m) do { \ |
1317 | if ((mifp)->m6_flags & MIFF_REGISTER) \ | |
1318 | register_send((ip6), (mifp), (m)); \ | |
1319 | else \ | |
1320 | phyint_send((ip6), (mifp), (m)); \ | |
1321 | } while (0) | |
1c79356b A |
1322 | |
1323 | /* | |
1324 | * Don't forward if it didn't arrive from the parent mif | |
1325 | * for its origin. | |
1326 | */ | |
1327 | mifi = rt->mf6c_parent; | |
1328 | if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) { | |
1329 | /* came in the wrong interface */ | |
1330 | #if MRT6DEBUG | |
1331 | if (mrt6debug & DEBUG_FORWARD) | |
1332 | log(LOG_DEBUG, | |
1333 | "wrong if: ifid %d mifi %d mififid %x\n", | |
1334 | ifp->if_index, mifi, | |
1335 | mif6table[mifi].m6_ifp->if_index); | |
1336 | #endif | |
1337 | mrt6stat.mrt6s_wrong_if++; | |
1338 | rt->mf6c_wrong_if++; | |
1339 | /* | |
1340 | * If we are doing PIM processing, and we are forwarding | |
1341 | * packets on this interface, send a message to the | |
1342 | * routing daemon. | |
1343 | */ | |
9bccf70c A |
1344 | /* have to make sure this is a valid mif */ |
1345 | if (mifi < nummifs && mif6table[mifi].m6_ifp) | |
1346 | if (pim6 && (m->m_flags & M_LOOP) == 0) { | |
1347 | /* | |
1348 | * Check the M_LOOP flag to avoid an | |
1349 | * unnecessary PIM assert. | |
1350 | * XXX: M_LOOP is an ad-hoc hack... | |
1351 | */ | |
2d21ac55 A |
1352 | static struct sockaddr_in6 addr = |
1353 | { sizeof(addr), AF_INET6 , 0, 0, IN6ADDR_ANY_INIT, 0}; | |
9bccf70c A |
1354 | |
1355 | struct mbuf *mm; | |
1356 | struct mrt6msg *im; | |
1357 | #if MRT6_OINIT | |
1358 | struct omrt6msg *oim; | |
1359 | #endif | |
1360 | ||
1361 | mm = m_copy(m, 0, sizeof(struct ip6_hdr)); | |
1362 | if (mm && | |
1363 | (M_HASCL(mm) || | |
1364 | mm->m_len < sizeof(struct ip6_hdr))) | |
1365 | mm = m_pullup(mm, sizeof(struct ip6_hdr)); | |
1366 | if (mm == NULL) | |
1367 | return ENOBUFS; | |
1368 | ||
1369 | #if MRT6_OINIT | |
1370 | oim = NULL; | |
1371 | #endif | |
1372 | im = NULL; | |
1373 | switch (ip6_mrouter_ver) { | |
1374 | #if MRT6_OINIT | |
1375 | case MRT6_OINIT: | |
1376 | oim = mtod(mm, struct omrt6msg *); | |
1377 | oim->im6_msgtype = MRT6MSG_WRONGMIF; | |
1378 | oim->im6_mbz = 0; | |
1379 | break; | |
1380 | #endif | |
1381 | case MRT6_INIT: | |
1c79356b | 1382 | im = mtod(mm, struct mrt6msg *); |
9bccf70c A |
1383 | im->im6_msgtype = MRT6MSG_WRONGMIF; |
1384 | im->im6_mbz = 0; | |
1385 | break; | |
1386 | default: | |
1387 | m_freem(mm); | |
1388 | return EINVAL; | |
1389 | } | |
1c79356b | 1390 | |
9bccf70c A |
1391 | for (mifp = mif6table, iif = 0; |
1392 | iif < nummifs && mifp && | |
1393 | mifp->m6_ifp != ifp; | |
1394 | mifp++, iif++) | |
1395 | ; | |
1396 | ||
1397 | switch (ip6_mrouter_ver) { | |
1398 | #if MRT6_OINIT | |
1399 | case MRT6_OINIT: | |
1400 | oim->im6_mif = iif; | |
2d21ac55 | 1401 | addr.sin6_addr = oim->im6_src; |
9bccf70c A |
1402 | break; |
1403 | #endif | |
1404 | case MRT6_INIT: | |
1405 | im->im6_mif = iif; | |
2d21ac55 | 1406 | addr.sin6_addr = im->im6_src; |
9bccf70c A |
1407 | break; |
1408 | } | |
1c79356b | 1409 | |
9bccf70c | 1410 | mrt6stat.mrt6s_upcalls++; |
1c79356b | 1411 | |
2d21ac55 | 1412 | if (socket_send(ip6_mrouter, mm, &addr) < 0) { |
1c79356b | 1413 | #if MRT6DEBUG |
9bccf70c A |
1414 | if (mrt6debug) |
1415 | log(LOG_WARNING, "mdq, ip6_mrouter socket queue full\n"); | |
1c79356b | 1416 | #endif |
9bccf70c A |
1417 | ++mrt6stat.mrt6s_upq_sockfull; |
1418 | return ENOBUFS; | |
1419 | } /* if socket Q full */ | |
1420 | } /* if PIM */ | |
1c79356b A |
1421 | return 0; |
1422 | } /* if wrong iif */ | |
1423 | ||
1424 | /* If I sourced this packet, it counts as output, else it was input. */ | |
1425 | if (m->m_pkthdr.rcvif == NULL) { | |
1426 | /* XXX: is rcvif really NULL when output?? */ | |
1427 | mif6table[mifi].m6_pkt_out++; | |
1428 | mif6table[mifi].m6_bytes_out += plen; | |
1429 | } else { | |
1430 | mif6table[mifi].m6_pkt_in++; | |
1431 | mif6table[mifi].m6_bytes_in += plen; | |
1432 | } | |
1433 | rt->mf6c_pkt_cnt++; | |
1434 | rt->mf6c_byte_cnt += plen; | |
1435 | ||
1436 | /* | |
1437 | * For each mif, forward a copy of the packet if there are group | |
1438 | * members downstream on the interface. | |
1439 | */ | |
1440 | for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++) | |
1441 | if (IF_ISSET(mifi, &rt->mf6c_ifset)) { | |
9bccf70c A |
1442 | /* |
1443 | * check if the outgoing packet is going to break | |
1444 | * a scope boundary. | |
1445 | * XXX For packets through PIM register tunnel | |
1446 | * interface, we believe a routing daemon. | |
1447 | */ | |
1448 | if ((mif6table[rt->mf6c_parent].m6_flags & | |
1449 | MIFF_REGISTER) == 0 && | |
1450 | (mif6table[mifi].m6_flags & MIFF_REGISTER) == 0 && | |
1451 | (in6_addr2scopeid(ifp, &ip6->ip6_dst) != | |
1452 | in6_addr2scopeid(mif6table[mifi].m6_ifp, | |
1453 | &ip6->ip6_dst) || | |
1454 | in6_addr2scopeid(ifp, &ip6->ip6_src) != | |
1455 | in6_addr2scopeid(mif6table[mifi].m6_ifp, | |
1456 | &ip6->ip6_src))) { | |
1457 | ip6stat.ip6s_badscope++; | |
1458 | continue; | |
1459 | } | |
1460 | ||
1c79356b A |
1461 | mifp->m6_pkt_out++; |
1462 | mifp->m6_bytes_out += plen; | |
1463 | MC6_SEND(ip6, mifp, m); | |
1464 | } | |
1465 | return 0; | |
1466 | } | |
1467 | ||
1468 | static void | |
1469 | phyint_send(ip6, mifp, m) | |
1470 | struct ip6_hdr *ip6; | |
1471 | struct mif6 *mifp; | |
1472 | struct mbuf *m; | |
1473 | { | |
9bccf70c | 1474 | struct mbuf *mb_copy; |
1c79356b A |
1475 | struct ifnet *ifp = mifp->m6_ifp; |
1476 | int error = 0; | |
9bccf70c | 1477 | static struct route_in6 ro; |
1c79356b | 1478 | struct in6_multi *in6m; |
9bccf70c | 1479 | struct sockaddr_in6 *dst6; |
1c79356b A |
1480 | |
1481 | /* | |
1482 | * Make a new reference to the packet; make sure that | |
1483 | * the IPv6 header is actually copied, not just referenced, | |
1484 | * so that ip6_output() only scribbles on the copy. | |
1485 | */ | |
1486 | mb_copy = m_copy(m, 0, M_COPYALL); | |
1487 | if (mb_copy && | |
1488 | (M_HASCL(mb_copy) || mb_copy->m_len < sizeof(struct ip6_hdr))) | |
1489 | mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr)); | |
9bccf70c | 1490 | if (mb_copy == NULL) { |
1c79356b | 1491 | return; |
9bccf70c | 1492 | } |
1c79356b A |
1493 | /* set MCAST flag to the outgoing packet */ |
1494 | mb_copy->m_flags |= M_MCAST; | |
1495 | ||
1496 | /* | |
1497 | * If we sourced the packet, call ip6_output since we may devide | |
1498 | * the packet into fragments when the packet is too big for the | |
1499 | * outgoing interface. | |
1500 | * Otherwise, we can simply send the packet to the interface | |
1501 | * sending queue. | |
1502 | */ | |
1503 | if (m->m_pkthdr.rcvif == NULL) { | |
1504 | struct ip6_moptions im6o; | |
1505 | ||
1506 | im6o.im6o_multicast_ifp = ifp; | |
1507 | /* XXX: ip6_output will override ip6->ip6_hlim */ | |
1508 | im6o.im6o_multicast_hlim = ip6->ip6_hlim; | |
1509 | im6o.im6o_multicast_loop = 1; | |
9bccf70c | 1510 | error = ip6_output(mb_copy, NULL, &ro, |
91447636 A |
1511 | IPV6_FORWARDING, &im6o, NULL, 0); |
1512 | ||
1c79356b A |
1513 | |
1514 | #if MRT6DEBUG | |
1515 | if (mrt6debug & DEBUG_XMIT) | |
1516 | log(LOG_DEBUG, "phyint_send on mif %d err %d\n", | |
1517 | mifp - mif6table, error); | |
1518 | #endif | |
1c79356b A |
1519 | return; |
1520 | } | |
1521 | ||
1522 | /* | |
1523 | * If we belong to the destination multicast group | |
1524 | * on the outgoing interface, loop back a copy. | |
1525 | */ | |
9bccf70c | 1526 | dst6 = (struct sockaddr_in6 *)&ro.ro_dst; |
b0d623f7 | 1527 | ifnet_lock_shared(ifp); |
1c79356b | 1528 | IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m); |
b0d623f7 | 1529 | ifnet_lock_done(ifp); |
1c79356b | 1530 | if (in6m != NULL) { |
9bccf70c A |
1531 | dst6->sin6_len = sizeof(struct sockaddr_in6); |
1532 | dst6->sin6_family = AF_INET6; | |
1533 | dst6->sin6_addr = ip6->ip6_dst; | |
1534 | ip6_mloopback(ifp, m, (struct sockaddr_in6 *)&ro.ro_dst); | |
1c79356b A |
1535 | } |
1536 | /* | |
1537 | * Put the packet into the sending queue of the outgoing interface | |
1538 | * if it would fit in the MTU of the interface. | |
1539 | */ | |
55e303ae | 1540 | if (mb_copy->m_pkthdr.len <= ifp->if_mtu || ifp->if_mtu < IPV6_MMTU) { |
9bccf70c A |
1541 | dst6->sin6_len = sizeof(struct sockaddr_in6); |
1542 | dst6->sin6_family = AF_INET6; | |
1543 | dst6->sin6_addr = ip6->ip6_dst; | |
1c79356b A |
1544 | /* |
1545 | * We just call if_output instead of nd6_output here, since | |
1546 | * we need no ND for a multicast forwarded packet...right? | |
1547 | */ | |
1548 | #ifdef __APPLE__ | |
9bccf70c A |
1549 | /* Make sure the HW checksum flags are cleaned before sending the packet */ |
1550 | ||
91447636 | 1551 | mb_copy->m_pkthdr.rcvif = 0; |
9bccf70c A |
1552 | mb_copy->m_pkthdr.csum_data = 0; |
1553 | mb_copy->m_pkthdr.csum_flags = 0; | |
1554 | ||
91447636 A |
1555 | lck_mtx_unlock(ip6_mutex); |
1556 | error = dlil_output(ifp, PF_INET6, mb_copy, | |
9bccf70c | 1557 | NULL, (struct sockaddr *)&ro.ro_dst, 0); |
91447636 | 1558 | lck_mtx_lock(ip6_mutex); |
1c79356b A |
1559 | #else |
1560 | error = (*ifp->if_output)(ifp, mb_copy, | |
9bccf70c | 1561 | (struct sockaddr *)&ro.ro_dst, |
1c79356b A |
1562 | NULL); |
1563 | #endif | |
1564 | #if MRT6DEBUG | |
1565 | if (mrt6debug & DEBUG_XMIT) | |
1566 | log(LOG_DEBUG, "phyint_send on mif %d err %d\n", | |
1567 | mifp - mif6table, error); | |
1568 | #endif | |
9bccf70c | 1569 | } else { |
1c79356b A |
1570 | #if MULTICAST_PMTUD |
1571 | icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu); | |
1c79356b A |
1572 | #else |
1573 | #if MRT6DEBUG | |
1c79356b A |
1574 | if (mrt6debug & DEBUG_XMIT) |
1575 | log(LOG_DEBUG, | |
9bccf70c | 1576 | "phyint_send: packet too big on %s o %s g %s" |
1c79356b | 1577 | " size %d(discarded)\n", |
9bccf70c | 1578 | if_name(ifp), |
1c79356b A |
1579 | ip6_sprintf(&ip6->ip6_src), |
1580 | ip6_sprintf(&ip6->ip6_dst), | |
1581 | mb_copy->m_pkthdr.len); | |
1c79356b A |
1582 | #endif /* MRT6DEBUG */ |
1583 | m_freem(mb_copy); /* simply discard the packet */ | |
1c79356b A |
1584 | #endif |
1585 | } | |
1586 | } | |
1587 | ||
1588 | static int | |
1589 | register_send(ip6, mif, m) | |
9bccf70c | 1590 | struct ip6_hdr *ip6; |
1c79356b | 1591 | struct mif6 *mif; |
9bccf70c | 1592 | struct mbuf *m; |
1c79356b | 1593 | { |
9bccf70c A |
1594 | struct mbuf *mm; |
1595 | int i, len = m->m_pkthdr.len; | |
2d21ac55 A |
1596 | static struct sockaddr_in6 addr = { sizeof(addr), AF_INET6 , |
1597 | 0, 0, IN6ADDR_ANY_INIT, 0}; | |
1c79356b A |
1598 | struct mrt6msg *im6; |
1599 | ||
1600 | #if MRT6DEBUG | |
1601 | if (mrt6debug) | |
1602 | log(LOG_DEBUG, "** IPv6 register_send **\n src %s dst %s\n", | |
1603 | ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst)); | |
1604 | #endif | |
1605 | ++pim6stat.pim6s_snd_registers; | |
1606 | ||
1607 | /* Make a copy of the packet to send to the user level process */ | |
1608 | MGETHDR(mm, M_DONTWAIT, MT_HEADER); | |
1609 | if (mm == NULL) | |
1610 | return ENOBUFS; | |
2d21ac55 A |
1611 | #ifdef __darwin8_notyet |
1612 | #if CONFIG_MACF_NET | |
1613 | mac_create_mbuf_multicast_encap(m, mif->m6_ifp, mm); | |
1614 | #endif | |
1615 | #endif | |
9bccf70c | 1616 | mm->m_pkthdr.rcvif = NULL; |
1c79356b A |
1617 | mm->m_data += max_linkhdr; |
1618 | mm->m_len = sizeof(struct ip6_hdr); | |
1619 | ||
1620 | if ((mm->m_next = m_copy(m, 0, M_COPYALL)) == NULL) { | |
1621 | m_freem(mm); | |
1622 | return ENOBUFS; | |
1623 | } | |
1624 | i = MHLEN - M_LEADINGSPACE(mm); | |
1625 | if (i > len) | |
1626 | i = len; | |
1627 | mm = m_pullup(mm, i); | |
1628 | if (mm == NULL){ | |
1629 | m_freem(mm); | |
1630 | return ENOBUFS; | |
1631 | } | |
1632 | /* TODO: check it! */ | |
1633 | mm->m_pkthdr.len = len + sizeof(struct ip6_hdr); | |
1634 | ||
1635 | /* | |
1636 | * Send message to routing daemon | |
1637 | */ | |
2d21ac55 | 1638 | addr.sin6_addr = ip6->ip6_src; |
1c79356b A |
1639 | |
1640 | im6 = mtod(mm, struct mrt6msg *); | |
1641 | im6->im6_msgtype = MRT6MSG_WHOLEPKT; | |
1642 | im6->im6_mbz = 0; | |
1643 | ||
1644 | im6->im6_mif = mif - mif6table; | |
1645 | ||
1646 | /* iif info is not given for reg. encap.n */ | |
1647 | mrt6stat.mrt6s_upcalls++; | |
1648 | ||
2d21ac55 | 1649 | if (socket_send(ip6_mrouter, mm, &addr) < 0) { |
1c79356b A |
1650 | #if MRT6DEBUG |
1651 | if (mrt6debug) | |
1652 | log(LOG_WARNING, | |
55e303ae | 1653 | "register_send: ip6_mrouter socket queue full\n"); |
1c79356b | 1654 | #endif |
9bccf70c A |
1655 | ++mrt6stat.mrt6s_upq_sockfull; |
1656 | return ENOBUFS; | |
1c79356b A |
1657 | } |
1658 | return 0; | |
1659 | } | |
1660 | ||
1661 | /* | |
1662 | * PIM sparse mode hook | |
1663 | * Receives the pim control messages, and passes them up to the listening | |
1664 | * socket, using rip6_input. | |
1665 | * The only message processed is the REGISTER pim message; the pim header | |
1666 | * is stripped off, and the inner packet is passed to register_mforward. | |
1667 | */ | |
1668 | int | |
9bccf70c | 1669 | pim6_input(mp, offp) |
1c79356b | 1670 | struct mbuf **mp; |
9bccf70c | 1671 | int *offp; |
1c79356b | 1672 | { |
9bccf70c A |
1673 | struct pim *pim; /* pointer to a pim struct */ |
1674 | struct ip6_hdr *ip6; | |
1675 | int pimlen; | |
1c79356b | 1676 | struct mbuf *m = *mp; |
9bccf70c | 1677 | int minlen; |
1c79356b | 1678 | int off = *offp; |
9bccf70c | 1679 | int proto; |
1c79356b A |
1680 | |
1681 | ++pim6stat.pim6s_rcv_total; | |
1682 | ||
9bccf70c A |
1683 | ip6 = mtod(m, struct ip6_hdr *); |
1684 | pimlen = m->m_pkthdr.len - *offp; | |
1685 | proto = ip6->ip6_nxt; | |
1c79356b | 1686 | |
9bccf70c A |
1687 | /* |
1688 | * Validate lengths | |
1689 | */ | |
1c79356b A |
1690 | if (pimlen < PIM_MINLEN) { |
1691 | ++pim6stat.pim6s_rcv_tooshort; | |
1692 | #if MRT6DEBUG | |
1693 | if (mrt6debug & DEBUG_PIM) | |
1694 | log(LOG_DEBUG,"pim6_input: PIM packet too short\n"); | |
1695 | #endif | |
1696 | m_freem(m); | |
1697 | return(IPPROTO_DONE); | |
1698 | } | |
1699 | ||
1700 | /* | |
1701 | * if the packet is at least as big as a REGISTER, go ahead | |
1702 | * and grab the PIM REGISTER header size, to avoid another | |
1703 | * possible m_pullup() later. | |
1704 | * | |
1705 | * PIM_MINLEN == pimhdr + u_int32 == 8 | |
1706 | * PIM6_REG_MINLEN == pimhdr + reghdr + eip6hdr == 4 + 4 + 40 | |
1707 | */ | |
1708 | minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN; | |
1709 | ||
1710 | /* | |
1711 | * Make sure that the IP6 and PIM headers in contiguous memory, and | |
1712 | * possibly the PIM REGISTER header | |
1713 | */ | |
1714 | #ifndef PULLDOWN_TEST | |
91447636 | 1715 | IP6_EXTHDR_CHECK(m, off, minlen, return IPPROTO_DONE); |
1c79356b A |
1716 | /* adjust pointer */ |
1717 | ip6 = mtod(m, struct ip6_hdr *); | |
1718 | ||
1719 | /* adjust mbuf to point to the PIM header */ | |
1720 | pim = (struct pim *)((caddr_t)ip6 + off); | |
1721 | #else | |
1722 | IP6_EXTHDR_GET(pim, struct pim *, m, off, minlen); | |
1723 | if (pim == NULL) { | |
1724 | pim6stat.pim6s_rcv_tooshort++; | |
1725 | return IPPROTO_DONE; | |
1726 | } | |
1727 | #endif | |
1728 | ||
9bccf70c A |
1729 | #define PIM6_CHECKSUM |
1730 | #ifdef PIM6_CHECKSUM | |
1c79356b A |
1731 | { |
1732 | int cksumlen; | |
1733 | ||
1734 | /* | |
1735 | * Validate checksum. | |
1736 | * If PIM REGISTER, exclude the data packet | |
1737 | */ | |
1738 | if (pim->pim_type == PIM_REGISTER) | |
1739 | cksumlen = PIM_MINLEN; | |
1740 | else | |
1741 | cksumlen = pimlen; | |
1742 | ||
1743 | if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) { | |
1744 | ++pim6stat.pim6s_rcv_badsum; | |
1745 | #if MRT6DEBUG | |
1746 | if (mrt6debug & DEBUG_PIM) | |
1747 | log(LOG_DEBUG, | |
1748 | "pim6_input: invalid checksum\n"); | |
1749 | #endif | |
1750 | m_freem(m); | |
1751 | return(IPPROTO_DONE); | |
1752 | } | |
1753 | } | |
1754 | #endif /* PIM_CHECKSUM */ | |
1755 | ||
1756 | /* PIM version check */ | |
1757 | if (pim->pim_ver != PIM_VERSION) { | |
1758 | ++pim6stat.pim6s_rcv_badversion; | |
1759 | #if MRT6DEBUG | |
1760 | log(LOG_ERR, | |
1761 | "pim6_input: incorrect version %d, expecting %d\n", | |
1762 | pim->pim_ver, PIM_VERSION); | |
1763 | #endif | |
1764 | m_freem(m); | |
1765 | return(IPPROTO_DONE); | |
1766 | } | |
1767 | ||
1768 | if (pim->pim_type == PIM_REGISTER) { | |
1769 | /* | |
1770 | * since this is a REGISTER, we'll make a copy of the register | |
1771 | * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the | |
1772 | * routing daemon. | |
1773 | */ | |
2d21ac55 A |
1774 | static struct sockaddr_in6 dst = { sizeof(dst), AF_INET6 , |
1775 | 0, 0, IN6ADDR_ANY_INIT, 0 }; | |
1c79356b A |
1776 | |
1777 | struct mbuf *mcp; | |
1778 | struct ip6_hdr *eip6; | |
1779 | u_int32_t *reghdr; | |
1c79356b A |
1780 | |
1781 | ++pim6stat.pim6s_rcv_registers; | |
1782 | ||
1783 | if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) { | |
1784 | #if MRT6DEBUG | |
1785 | if (mrt6debug & DEBUG_PIM) | |
1786 | log(LOG_DEBUG, | |
1787 | "pim6_input: register mif not set: %d\n", | |
1788 | reg_mif_num); | |
1789 | #endif | |
1790 | m_freem(m); | |
1791 | return(IPPROTO_DONE); | |
1792 | } | |
1793 | ||
1794 | reghdr = (u_int32_t *)(pim + 1); | |
1795 | ||
1796 | if ((ntohl(*reghdr) & PIM_NULL_REGISTER)) | |
1797 | goto pim6_input_to_daemon; | |
1798 | ||
1799 | /* | |
1800 | * Validate length | |
1801 | */ | |
1802 | if (pimlen < PIM6_REG_MINLEN) { | |
1803 | ++pim6stat.pim6s_rcv_tooshort; | |
1804 | ++pim6stat.pim6s_rcv_badregisters; | |
1805 | #if MRT6DEBUG | |
1806 | log(LOG_ERR, | |
1807 | "pim6_input: register packet size too " | |
1808 | "small %d from %s\n", | |
1809 | pimlen, ip6_sprintf(&ip6->ip6_src)); | |
1810 | #endif | |
1811 | m_freem(m); | |
1812 | return(IPPROTO_DONE); | |
1813 | } | |
1814 | ||
1815 | eip6 = (struct ip6_hdr *) (reghdr + 1); | |
1816 | #if MRT6DEBUG | |
1817 | if (mrt6debug & DEBUG_PIM) | |
1818 | log(LOG_DEBUG, | |
1819 | "pim6_input[register], eip6: %s -> %s, " | |
1820 | "eip6 plen %d\n", | |
1821 | ip6_sprintf(&eip6->ip6_src), | |
1822 | ip6_sprintf(&eip6->ip6_dst), | |
1823 | ntohs(eip6->ip6_plen)); | |
1824 | #endif | |
9bccf70c A |
1825 | |
1826 | /* verify the version number of the inner packet */ | |
1827 | if ((eip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { | |
1828 | ++pim6stat.pim6s_rcv_badregisters; | |
1829 | #if MRT6DEBUG | |
1830 | log(LOG_DEBUG, "pim6_input: invalid IP version (%d) " | |
1831 | "of the inner packet\n", | |
1832 | (eip6->ip6_vfc & IPV6_VERSION)); | |
1833 | #endif | |
1834 | m_freem(m); | |
1835 | return(IPPROTO_NONE); | |
1836 | } | |
1c79356b A |
1837 | |
1838 | /* verify the inner packet is destined to a mcast group */ | |
1839 | if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) { | |
1840 | ++pim6stat.pim6s_rcv_badregisters; | |
1841 | #if MRT6DEBUG | |
1842 | if (mrt6debug & DEBUG_PIM) | |
1843 | log(LOG_DEBUG, | |
1844 | "pim6_input: inner packet of register " | |
1845 | "is not multicast %s\n", | |
1846 | ip6_sprintf(&eip6->ip6_dst)); | |
1847 | #endif | |
1848 | m_freem(m); | |
1849 | return(IPPROTO_DONE); | |
1850 | } | |
1851 | ||
1852 | /* | |
1853 | * make a copy of the whole header to pass to the daemon later. | |
1854 | */ | |
1855 | mcp = m_copy(m, 0, off + PIM6_REG_MINLEN); | |
1856 | if (mcp == NULL) { | |
1857 | #if MRT6DEBUG | |
1858 | log(LOG_ERR, | |
1859 | "pim6_input: pim register: " | |
1860 | "could not copy register head\n"); | |
1861 | #endif | |
1862 | m_freem(m); | |
1863 | return(IPPROTO_DONE); | |
1864 | } | |
1865 | ||
1866 | /* | |
1867 | * forward the inner ip6 packet; point m_data at the inner ip6. | |
1868 | */ | |
1869 | m_adj(m, off + PIM_MINLEN); | |
1870 | #if MRT6DEBUG | |
1871 | if (mrt6debug & DEBUG_PIM) { | |
1872 | log(LOG_DEBUG, | |
1873 | "pim6_input: forwarding decapsulated register: " | |
1874 | "src %s, dst %s, mif %d\n", | |
1875 | ip6_sprintf(&eip6->ip6_src), | |
1876 | ip6_sprintf(&eip6->ip6_dst), | |
1877 | reg_mif_num); | |
1878 | } | |
1879 | #endif | |
1880 | ||
9bccf70c A |
1881 | #ifdef __APPLE__ |
1882 | ||
91447636 A |
1883 | if (lo_ifp) { |
1884 | lck_mtx_unlock(ip6_mutex); | |
1885 | dlil_output(lo_ifp, PF_INET6, m, 0, (struct sockaddr *)&dst, 0); | |
1886 | lck_mtx_lock(ip6_mutex); | |
1887 | } | |
1c79356b A |
1888 | else { |
1889 | printf("Warning: pim6_input call to dlil_find_dltag failed!\n"); | |
1890 | m_freem(m); | |
1891 | } | |
1892 | #else | |
2d21ac55 | 1893 | (void) if_simloop(mif6table[reg_mif_num].m6_ifp, m, |
9bccf70c | 1894 | dst.sin6_family, NULL); |
1c79356b A |
1895 | #endif |
1896 | ||
1897 | /* prepare the register head to send to the mrouting daemon */ | |
1898 | m = mcp; | |
1899 | } | |
1900 | ||
1901 | /* | |
1902 | * Pass the PIM message up to the daemon; if it is a register message | |
1903 | * pass the 'head' only up to the daemon. This includes the | |
1904 | * encapsulator ip6 header, pim header, register header and the | |
1905 | * encapsulated ip6 header. | |
1906 | */ | |
1907 | pim6_input_to_daemon: | |
9bccf70c | 1908 | rip6_input(&m, offp); |
1c79356b A |
1909 | return(IPPROTO_DONE); |
1910 | } | |
b7266188 | 1911 | #endif |