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