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