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