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