icmp6.c revision 1.1.2.1 1 1.1.2.1 itojun /*
2 1.1.2.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 1.1.2.1 itojun * All rights reserved.
4 1.1.2.1 itojun *
5 1.1.2.1 itojun * Redistribution and use in source and binary forms, with or without
6 1.1.2.1 itojun * modification, are permitted provided that the following conditions
7 1.1.2.1 itojun * are met:
8 1.1.2.1 itojun * 1. Redistributions of source code must retain the above copyright
9 1.1.2.1 itojun * notice, this list of conditions and the following disclaimer.
10 1.1.2.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
11 1.1.2.1 itojun * notice, this list of conditions and the following disclaimer in the
12 1.1.2.1 itojun * documentation and/or other materials provided with the distribution.
13 1.1.2.1 itojun * 3. Neither the name of the project nor the names of its contributors
14 1.1.2.1 itojun * may be used to endorse or promote products derived from this software
15 1.1.2.1 itojun * without specific prior written permission.
16 1.1.2.1 itojun *
17 1.1.2.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 1.1.2.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1.2.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1.2.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 1.1.2.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1.2.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1.2.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1.2.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1.2.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1.2.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1.2.1 itojun * SUCH DAMAGE.
28 1.1.2.1 itojun */
29 1.1.2.1 itojun
30 1.1.2.1 itojun /*
31 1.1.2.1 itojun * Copyright (c) 1982, 1986, 1988, 1993
32 1.1.2.1 itojun * The Regents of the University of California. All rights reserved.
33 1.1.2.1 itojun *
34 1.1.2.1 itojun * Redistribution and use in source and binary forms, with or without
35 1.1.2.1 itojun * modification, are permitted provided that the following conditions
36 1.1.2.1 itojun * are met:
37 1.1.2.1 itojun * 1. Redistributions of source code must retain the above copyright
38 1.1.2.1 itojun * notice, this list of conditions and the following disclaimer.
39 1.1.2.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
40 1.1.2.1 itojun * notice, this list of conditions and the following disclaimer in the
41 1.1.2.1 itojun * documentation and/or other materials provided with the distribution.
42 1.1.2.1 itojun * 3. All advertising materials mentioning features or use of this software
43 1.1.2.1 itojun * must display the following acknowledgement:
44 1.1.2.1 itojun * This product includes software developed by the University of
45 1.1.2.1 itojun * California, Berkeley and its contributors.
46 1.1.2.1 itojun * 4. Neither the name of the University nor the names of its contributors
47 1.1.2.1 itojun * may be used to endorse or promote products derived from this software
48 1.1.2.1 itojun * without specific prior written permission.
49 1.1.2.1 itojun *
50 1.1.2.1 itojun * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 1.1.2.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 1.1.2.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 1.1.2.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 1.1.2.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 1.1.2.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 1.1.2.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 1.1.2.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 1.1.2.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 1.1.2.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 1.1.2.1 itojun * SUCH DAMAGE.
61 1.1.2.1 itojun *
62 1.1.2.1 itojun * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
63 1.1.2.1 itojun */
64 1.1.2.1 itojun
65 1.1.2.1 itojun #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
66 1.1.2.1 itojun #include "opt_inet.h"
67 1.1.2.1 itojun #endif
68 1.1.2.1 itojun
69 1.1.2.1 itojun #include <sys/param.h>
70 1.1.2.1 itojun #include <sys/systm.h>
71 1.1.2.1 itojun #include <sys/malloc.h>
72 1.1.2.1 itojun #include <sys/mbuf.h>
73 1.1.2.1 itojun #include <sys/protosw.h>
74 1.1.2.1 itojun #include <sys/socket.h>
75 1.1.2.1 itojun #include <sys/socketvar.h>
76 1.1.2.1 itojun #include <sys/time.h>
77 1.1.2.1 itojun #include <sys/kernel.h>
78 1.1.2.1 itojun #include <sys/syslog.h>
79 1.1.2.1 itojun
80 1.1.2.1 itojun #include <net/if.h>
81 1.1.2.1 itojun #include <net/route.h>
82 1.1.2.1 itojun #include <net/if_dl.h>
83 1.1.2.1 itojun #include <net/if_types.h>
84 1.1.2.1 itojun
85 1.1.2.1 itojun #include <netinet/in.h>
86 1.1.2.1 itojun #include <netinet/in_var.h>
87 1.1.2.1 itojun #include <netinet6/in6_systm.h>
88 1.1.2.1 itojun #include <netinet6/ip6.h>
89 1.1.2.1 itojun #include <netinet6/ip6_var.h>
90 1.1.2.1 itojun #include <netinet6/icmp6.h>
91 1.1.2.1 itojun #include <netinet6/mld6_var.h>
92 1.1.2.1 itojun #if !defined(__FreeBSD__) || __FreeBSD__ < 3
93 1.1.2.1 itojun #include <netinet6/in6_pcb.h>
94 1.1.2.1 itojun #else
95 1.1.2.1 itojun #include <netinet/in_pcb.h>
96 1.1.2.1 itojun #endif
97 1.1.2.1 itojun #include <netinet6/nd6.h>
98 1.1.2.1 itojun #include <netinet6/in6_ifattach.h>
99 1.1.2.1 itojun
100 1.1.2.1 itojun #ifdef IPSEC
101 1.1.2.1 itojun #include <netkey/key.h>
102 1.1.2.1 itojun #include <netkey/key_debug.h>
103 1.1.2.1 itojun #endif
104 1.1.2.1 itojun
105 1.1.2.1 itojun #include "faith.h"
106 1.1.2.1 itojun
107 1.1.2.1 itojun extern struct protosw inet6sw[];
108 1.1.2.1 itojun extern u_char ip6_protox[];
109 1.1.2.1 itojun
110 1.1.2.1 itojun struct icmp6stat icmp6stat;
111 1.1.2.1 itojun
112 1.1.2.1 itojun #if !defined(__FreeBSD__) || __FreeBSD__ < 3
113 1.1.2.1 itojun extern struct in6pcb rawin6pcb;
114 1.1.2.1 itojun #else
115 1.1.2.1 itojun extern struct inpcbhead ripcb;
116 1.1.2.1 itojun #endif
117 1.1.2.1 itojun extern u_int icmp6errratelim;
118 1.1.2.1 itojun static int icmp6_rip6_input __P((struct mbuf **, int));
119 1.1.2.1 itojun static int icmp6_ratelimit __P((const struct in6_addr *, const int, const int));
120 1.1.2.1 itojun static struct mbuf * ni6_input __P((struct mbuf *, int));
121 1.1.2.1 itojun static int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *,
122 1.1.2.1 itojun struct ifnet **));
123 1.1.2.1 itojun static int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
124 1.1.2.1 itojun struct ifnet *, int));
125 1.1.2.1 itojun
126 1.1.2.1 itojun #ifdef COMPAT_RFC1885
127 1.1.2.1 itojun static struct route_in6 icmp6_reflect_rt;
128 1.1.2.1 itojun #endif
129 1.1.2.1 itojun static struct timeval icmp6_nextsend = {0, 0};
130 1.1.2.1 itojun
131 1.1.2.1 itojun void
132 1.1.2.1 itojun icmp6_init()
133 1.1.2.1 itojun {
134 1.1.2.1 itojun mld6_init();
135 1.1.2.1 itojun }
136 1.1.2.1 itojun
137 1.1.2.1 itojun /*
138 1.1.2.1 itojun * Generate an error packet of type error in response to bad IP6 packet.
139 1.1.2.1 itojun */
140 1.1.2.1 itojun void
141 1.1.2.1 itojun icmp6_error(m, type, code, param)
142 1.1.2.1 itojun struct mbuf *m;
143 1.1.2.1 itojun int type, code, param;
144 1.1.2.1 itojun {
145 1.1.2.1 itojun struct ip6_hdr *oip6, *nip6;
146 1.1.2.1 itojun struct icmp6_hdr *icmp6;
147 1.1.2.1 itojun u_int prep;
148 1.1.2.1 itojun int off;
149 1.1.2.1 itojun u_char nxt;
150 1.1.2.1 itojun
151 1.1.2.1 itojun icmp6stat.icp6s_error++;
152 1.1.2.1 itojun
153 1.1.2.1 itojun if (m->m_flags & M_DECRYPTED)
154 1.1.2.1 itojun goto freeit;
155 1.1.2.1 itojun
156 1.1.2.1 itojun oip6 = mtod(m, struct ip6_hdr *);
157 1.1.2.1 itojun
158 1.1.2.1 itojun /*
159 1.1.2.1 itojun * Multicast destination check. For unrecognized option errors,
160 1.1.2.1 itojun * this check has already done in ip6_unknown_opt(), so we can
161 1.1.2.1 itojun * check only for other errors.
162 1.1.2.1 itojun */
163 1.1.2.1 itojun if ((m->m_flags & (M_BCAST|M_MCAST) ||
164 1.1.2.1 itojun IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
165 1.1.2.1 itojun (type != ICMP6_PACKET_TOO_BIG &&
166 1.1.2.1 itojun (type != ICMP6_PARAM_PROB ||
167 1.1.2.1 itojun code != ICMP6_PARAMPROB_OPTION)))
168 1.1.2.1 itojun goto freeit;
169 1.1.2.1 itojun
170 1.1.2.1 itojun /* Source address check. XXX: the case of anycast source? */
171 1.1.2.1 itojun if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
172 1.1.2.1 itojun IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
173 1.1.2.1 itojun goto freeit;
174 1.1.2.1 itojun
175 1.1.2.1 itojun /*
176 1.1.2.1 itojun * If the erroneous packet is also an ICMP error, discard it.
177 1.1.2.1 itojun */
178 1.1.2.1 itojun IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
179 1.1.2.1 itojun off = sizeof(struct ip6_hdr);
180 1.1.2.1 itojun nxt = oip6->ip6_nxt;
181 1.1.2.1 itojun while(1) { /* XXX: should avoid inf. loop explicitly? */
182 1.1.2.1 itojun struct ip6_ext *ip6e;
183 1.1.2.1 itojun struct icmp6_hdr *icp;
184 1.1.2.1 itojun
185 1.1.2.1 itojun switch(nxt) {
186 1.1.2.1 itojun case IPPROTO_IPV6:
187 1.1.2.1 itojun case IPPROTO_IPV4:
188 1.1.2.1 itojun case IPPROTO_UDP:
189 1.1.2.1 itojun case IPPROTO_TCP:
190 1.1.2.1 itojun case IPPROTO_ESP:
191 1.1.2.1 itojun case IPPROTO_FRAGMENT:
192 1.1.2.1 itojun /*
193 1.1.2.1 itojun * ICMPv6 error must not be fragmented.
194 1.1.2.1 itojun * XXX: but can we trust the sender?
195 1.1.2.1 itojun */
196 1.1.2.1 itojun default:
197 1.1.2.1 itojun /* What if unknown header followed by ICMP error? */
198 1.1.2.1 itojun goto generate;
199 1.1.2.1 itojun case IPPROTO_ICMPV6:
200 1.1.2.1 itojun IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
201 1.1.2.1 itojun icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
202 1.1.2.1 itojun if (icp->icmp6_type < ICMP6_ECHO_REQUEST
203 1.1.2.1 itojun || icp->icmp6_type == ND_REDIRECT) {
204 1.1.2.1 itojun /*
205 1.1.2.1 itojun * ICMPv6 error
206 1.1.2.1 itojun * Special case: for redirect (which is
207 1.1.2.1 itojun * informational) we must not send icmp6 error.
208 1.1.2.1 itojun */
209 1.1.2.1 itojun icmp6stat.icp6s_canterror++;
210 1.1.2.1 itojun goto freeit;
211 1.1.2.1 itojun } else {
212 1.1.2.1 itojun /* ICMPv6 informational */
213 1.1.2.1 itojun goto generate;
214 1.1.2.1 itojun }
215 1.1.2.1 itojun case IPPROTO_HOPOPTS:
216 1.1.2.1 itojun case IPPROTO_DSTOPTS:
217 1.1.2.1 itojun case IPPROTO_ROUTING:
218 1.1.2.1 itojun case IPPROTO_AH:
219 1.1.2.1 itojun IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct ip6_ext), );
220 1.1.2.1 itojun ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
221 1.1.2.1 itojun if (nxt == IPPROTO_AH)
222 1.1.2.1 itojun off += (ip6e->ip6e_len + 2) << 2;
223 1.1.2.1 itojun else
224 1.1.2.1 itojun off += (ip6e->ip6e_len + 1) << 3;
225 1.1.2.1 itojun nxt = ip6e->ip6e_nxt;
226 1.1.2.1 itojun break;
227 1.1.2.1 itojun }
228 1.1.2.1 itojun }
229 1.1.2.1 itojun
230 1.1.2.1 itojun freeit:
231 1.1.2.1 itojun /*
232 1.1.2.1 itojun * If we can't tell wheter or not we can generate ICMP6, free it.
233 1.1.2.1 itojun */
234 1.1.2.1 itojun m_freem(m);
235 1.1.2.1 itojun return;
236 1.1.2.1 itojun
237 1.1.2.1 itojun generate:
238 1.1.2.1 itojun oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
239 1.1.2.1 itojun
240 1.1.2.1 itojun /* Finally, do rate limitation check. */
241 1.1.2.1 itojun if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
242 1.1.2.1 itojun icmp6stat.icp6s_toofreq++;
243 1.1.2.1 itojun goto freeit;
244 1.1.2.1 itojun }
245 1.1.2.1 itojun
246 1.1.2.1 itojun /*
247 1.1.2.1 itojun * OK, ICMP6 can be generated.
248 1.1.2.1 itojun */
249 1.1.2.1 itojun
250 1.1.2.1 itojun if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
251 1.1.2.1 itojun m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
252 1.1.2.1 itojun
253 1.1.2.1 itojun prep = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
254 1.1.2.1 itojun M_PREPEND(m, prep, M_DONTWAIT);
255 1.1.2.1 itojun if (m && m->m_len < prep)
256 1.1.2.1 itojun m = m_pullup(m, prep);
257 1.1.2.1 itojun if (m == NULL) {
258 1.1.2.1 itojun printf("ENOBUFS in icmp6_error %d\n", __LINE__);
259 1.1.2.1 itojun return;
260 1.1.2.1 itojun }
261 1.1.2.1 itojun
262 1.1.2.1 itojun nip6 = mtod(m, struct ip6_hdr *);
263 1.1.2.1 itojun nip6->ip6_src = oip6->ip6_src;
264 1.1.2.1 itojun nip6->ip6_dst = oip6->ip6_dst;
265 1.1.2.1 itojun
266 1.1.2.1 itojun if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
267 1.1.2.1 itojun oip6->ip6_src.s6_addr16[1] = 0;
268 1.1.2.1 itojun if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
269 1.1.2.1 itojun oip6->ip6_dst.s6_addr16[1] = 0;
270 1.1.2.1 itojun
271 1.1.2.1 itojun icmp6 = (struct icmp6_hdr *)(nip6 + 1);
272 1.1.2.1 itojun icmp6->icmp6_type = type;
273 1.1.2.1 itojun icmp6->icmp6_code = code;
274 1.1.2.1 itojun icmp6->icmp6_pptr = htonl((u_long)param);
275 1.1.2.1 itojun
276 1.1.2.1 itojun icmp6stat.icp6s_outhist[type]++;
277 1.1.2.1 itojun icmp6_reflect(m, sizeof(struct ip6_hdr)); /*header order: IPv6 - ICMPv6*/
278 1.1.2.1 itojun }
279 1.1.2.1 itojun
280 1.1.2.1 itojun /*
281 1.1.2.1 itojun * Process a received ICMP6 message.
282 1.1.2.1 itojun */
283 1.1.2.1 itojun int
284 1.1.2.1 itojun icmp6_input(mp, offp, proto)
285 1.1.2.1 itojun struct mbuf **mp;
286 1.1.2.1 itojun int *offp, proto;
287 1.1.2.1 itojun {
288 1.1.2.1 itojun struct mbuf *m = *mp, *n;
289 1.1.2.1 itojun struct ip6_hdr *ip6, *nip6;
290 1.1.2.1 itojun struct icmp6_hdr *icmp6, *nicmp6;
291 1.1.2.1 itojun int off = *offp;
292 1.1.2.1 itojun int icmp6len = m->m_pkthdr.len - *offp;
293 1.1.2.1 itojun int code, sum, noff;
294 1.1.2.1 itojun struct sockaddr_in6 icmp6src;
295 1.1.2.1 itojun
296 1.1.2.1 itojun IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
297 1.1.2.1 itojun /* m might change if M_LOOP. So, call mtod after this */
298 1.1.2.1 itojun
299 1.1.2.1 itojun /*
300 1.1.2.1 itojun * Locate icmp6 structure in mbuf, and check
301 1.1.2.1 itojun * that not corrupted and of at least minimum length
302 1.1.2.1 itojun */
303 1.1.2.1 itojun
304 1.1.2.1 itojun ip6 = mtod(m, struct ip6_hdr *);
305 1.1.2.1 itojun if (icmp6len < sizeof(struct icmp6_hdr)) {
306 1.1.2.1 itojun icmp6stat.icp6s_tooshort++;
307 1.1.2.1 itojun goto freeit;
308 1.1.2.1 itojun }
309 1.1.2.1 itojun
310 1.1.2.1 itojun /*
311 1.1.2.1 itojun * calculate the checksum
312 1.1.2.1 itojun */
313 1.1.2.1 itojun
314 1.1.2.1 itojun icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
315 1.1.2.1 itojun code = icmp6->icmp6_code;
316 1.1.2.1 itojun
317 1.1.2.1 itojun if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
318 1.1.2.1 itojun log(LOG_ERR,
319 1.1.2.1 itojun "ICMP6 checksum error(%d|%x) %s\n",
320 1.1.2.1 itojun icmp6->icmp6_type,
321 1.1.2.1 itojun sum,
322 1.1.2.1 itojun ip6_sprintf(&ip6->ip6_src));
323 1.1.2.1 itojun icmp6stat.icp6s_checksum++;
324 1.1.2.1 itojun goto freeit;
325 1.1.2.1 itojun }
326 1.1.2.1 itojun
327 1.1.2.1 itojun #if defined(NFAITH) && 0 < NFAITH
328 1.1.2.1 itojun if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
329 1.1.2.1 itojun /*
330 1.1.2.1 itojun * Deliver very specific ICMP6 type only.
331 1.1.2.1 itojun * This is important to deilver TOOBIG. Otherwise PMTUD
332 1.1.2.1 itojun * will not work.
333 1.1.2.1 itojun */
334 1.1.2.1 itojun switch (icmp6->icmp6_type) {
335 1.1.2.1 itojun case ICMP6_DST_UNREACH:
336 1.1.2.1 itojun case ICMP6_PACKET_TOO_BIG:
337 1.1.2.1 itojun case ICMP6_TIME_EXCEEDED:
338 1.1.2.1 itojun break;
339 1.1.2.1 itojun default:
340 1.1.2.1 itojun goto freeit;
341 1.1.2.1 itojun }
342 1.1.2.1 itojun }
343 1.1.2.1 itojun #endif
344 1.1.2.1 itojun
345 1.1.2.1 itojun #ifdef IPSEC
346 1.1.2.1 itojun /*drop it if it does not match the default policy */
347 1.1.2.1 itojun if (ipsec6_in_reject(m, NULL)) {
348 1.1.2.1 itojun ipsecstat.in_polvio++;
349 1.1.2.1 itojun goto freeit;
350 1.1.2.1 itojun }
351 1.1.2.1 itojun #endif
352 1.1.2.1 itojun
353 1.1.2.1 itojun icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
354 1.1.2.1 itojun
355 1.1.2.1 itojun switch (icmp6->icmp6_type) {
356 1.1.2.1 itojun
357 1.1.2.1 itojun case ICMP6_DST_UNREACH:
358 1.1.2.1 itojun switch (code) {
359 1.1.2.1 itojun case ICMP6_DST_UNREACH_NOROUTE:
360 1.1.2.1 itojun code = PRC_UNREACH_NET;
361 1.1.2.1 itojun break;
362 1.1.2.1 itojun case ICMP6_DST_UNREACH_ADMIN:
363 1.1.2.1 itojun case ICMP6_DST_UNREACH_ADDR:
364 1.1.2.1 itojun code = PRC_UNREACH_HOST;
365 1.1.2.1 itojun break;
366 1.1.2.1 itojun case ICMP6_DST_UNREACH_NOTNEIGHBOR:
367 1.1.2.1 itojun code = PRC_UNREACH_SRCFAIL;
368 1.1.2.1 itojun break;
369 1.1.2.1 itojun case ICMP6_DST_UNREACH_NOPORT:
370 1.1.2.1 itojun code = PRC_UNREACH_PORT;
371 1.1.2.1 itojun break;
372 1.1.2.1 itojun default:
373 1.1.2.1 itojun goto badcode;
374 1.1.2.1 itojun }
375 1.1.2.1 itojun goto deliver;
376 1.1.2.1 itojun break;
377 1.1.2.1 itojun
378 1.1.2.1 itojun case ICMP6_PACKET_TOO_BIG:
379 1.1.2.1 itojun if (code != 0)
380 1.1.2.1 itojun goto badcode;
381 1.1.2.1 itojun {
382 1.1.2.1 itojun u_int mtu = ntohl(icmp6->icmp6_mtu);
383 1.1.2.1 itojun struct rtentry *rt;
384 1.1.2.1 itojun struct sockaddr_in6 sin6;
385 1.1.2.1 itojun #ifdef __bsdi__
386 1.1.2.1 itojun struct route_in6 ro6;
387 1.1.2.1 itojun #endif
388 1.1.2.1 itojun
389 1.1.2.1 itojun code = PRC_MSGSIZE;
390 1.1.2.1 itojun bzero(&sin6, sizeof(sin6));
391 1.1.2.1 itojun sin6.sin6_family = PF_INET6;
392 1.1.2.1 itojun sin6.sin6_len = sizeof(struct sockaddr_in6);
393 1.1.2.1 itojun sin6.sin6_addr = ((struct ip6_hdr *)(icmp6 + 1))->ip6_dst;
394 1.1.2.1 itojun rt = rtalloc1((struct sockaddr *)&sin6, 0
395 1.1.2.1 itojun #ifdef __FreeBSD__
396 1.1.2.1 itojun , RTF_CLONING | RTF_PRCLONING
397 1.1.2.1 itojun #endif /*__FreeBSD__*/
398 1.1.2.1 itojun );
399 1.1.2.1 itojun #ifdef __bsdi__
400 1.1.2.1 itojun bcopy(&sin6, &ro6.ro_dst, sizeof(struct sockaddr_in6));
401 1.1.2.1 itojun ro6.ro_rt = 0;
402 1.1.2.1 itojun rtcalloc((struct route *)&ro6);
403 1.1.2.1 itojun rt = ro6.ro_rt;
404 1.1.2.1 itojun #endif /*__bsdi__*/
405 1.1.2.1 itojun if (rt && (rt->rt_flags & RTF_HOST)
406 1.1.2.1 itojun && !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
407 1.1.2.1 itojun if (mtu < IPV6_MMTU) {
408 1.1.2.1 itojun /* xxx */
409 1.1.2.1 itojun rt->rt_rmx.rmx_locks |= RTV_MTU;
410 1.1.2.1 itojun } else if (mtu < rt->rt_ifp->if_mtu &&
411 1.1.2.1 itojun rt->rt_rmx.rmx_mtu > mtu) {
412 1.1.2.1 itojun rt->rt_rmx.rmx_mtu = mtu;
413 1.1.2.1 itojun }
414 1.1.2.1 itojun }
415 1.1.2.1 itojun if (rt)
416 1.1.2.1 itojun RTFREE(rt);
417 1.1.2.1 itojun
418 1.1.2.1 itojun goto deliver;
419 1.1.2.1 itojun }
420 1.1.2.1 itojun break;
421 1.1.2.1 itojun
422 1.1.2.1 itojun case ICMP6_TIME_EXCEEDED:
423 1.1.2.1 itojun switch (code) {
424 1.1.2.1 itojun case ICMP6_TIME_EXCEED_TRANSIT:
425 1.1.2.1 itojun case ICMP6_TIME_EXCEED_REASSEMBLY:
426 1.1.2.1 itojun code += PRC_TIMXCEED_INTRANS;
427 1.1.2.1 itojun break;
428 1.1.2.1 itojun default:
429 1.1.2.1 itojun goto badcode;
430 1.1.2.1 itojun }
431 1.1.2.1 itojun goto deliver;
432 1.1.2.1 itojun break;
433 1.1.2.1 itojun
434 1.1.2.1 itojun case ICMP6_PARAM_PROB:
435 1.1.2.1 itojun switch (code) {
436 1.1.2.1 itojun case ICMP6_PARAMPROB_NEXTHEADER:
437 1.1.2.1 itojun code = PRC_UNREACH_PROTOCOL;
438 1.1.2.1 itojun break;
439 1.1.2.1 itojun case ICMP6_PARAMPROB_HEADER:
440 1.1.2.1 itojun case ICMP6_PARAMPROB_OPTION:
441 1.1.2.1 itojun code = PRC_PARAMPROB;
442 1.1.2.1 itojun break;
443 1.1.2.1 itojun default:
444 1.1.2.1 itojun goto badcode;
445 1.1.2.1 itojun }
446 1.1.2.1 itojun goto deliver;
447 1.1.2.1 itojun break;
448 1.1.2.1 itojun
449 1.1.2.1 itojun case ICMP6_ECHO_REQUEST:
450 1.1.2.1 itojun if (code != 0)
451 1.1.2.1 itojun goto badcode;
452 1.1.2.1 itojun if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
453 1.1.2.1 itojun /* Give up remote */
454 1.1.2.1 itojun break;
455 1.1.2.1 itojun }
456 1.1.2.1 itojun if (n->m_flags & M_EXT) {
457 1.1.2.1 itojun int gap, move;
458 1.1.2.1 itojun struct mbuf *n0 = n;
459 1.1.2.1 itojun
460 1.1.2.1 itojun /*
461 1.1.2.1 itojun * Prepare an internal mbuf. m_pullup() doesn't
462 1.1.2.1 itojun * always copy the length we specified.
463 1.1.2.1 itojun */
464 1.1.2.1 itojun MGETHDR(n, M_DONTWAIT, n0->m_type);
465 1.1.2.1 itojun if (n == NULL) {
466 1.1.2.1 itojun /* Give up remote */
467 1.1.2.1 itojun m_freem(n0);
468 1.1.2.1 itojun break;
469 1.1.2.1 itojun }
470 1.1.2.1 itojun M_COPY_PKTHDR(n, n0);
471 1.1.2.1 itojun n0->m_flags &= ~M_PKTHDR;
472 1.1.2.1 itojun n->m_next = n0;
473 1.1.2.1 itojun /*
474 1.1.2.1 itojun * Copy IPv6 and ICMPv6 only.
475 1.1.2.1 itojun */
476 1.1.2.1 itojun nip6 = mtod(n, struct ip6_hdr *);
477 1.1.2.1 itojun bcopy(ip6, nip6, sizeof(struct ip6_hdr));
478 1.1.2.1 itojun nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
479 1.1.2.1 itojun bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
480 1.1.2.1 itojun /*
481 1.1.2.1 itojun * Adjust mbuf. ip6_plen will be adjusted.
482 1.1.2.1 itojun */
483 1.1.2.1 itojun noff = sizeof(struct ip6_hdr);
484 1.1.2.1 itojun n->m_len = noff + sizeof(struct icmp6_hdr);
485 1.1.2.1 itojun move = off + sizeof(struct icmp6_hdr);
486 1.1.2.1 itojun n0->m_len -= move;
487 1.1.2.1 itojun n0->m_data += move;
488 1.1.2.1 itojun gap = off - noff;
489 1.1.2.1 itojun n->m_pkthdr.len -= gap;
490 1.1.2.1 itojun } else {
491 1.1.2.1 itojun nip6 = mtod(n, struct ip6_hdr *);
492 1.1.2.1 itojun nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off);
493 1.1.2.1 itojun noff = off;
494 1.1.2.1 itojun }
495 1.1.2.1 itojun nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
496 1.1.2.1 itojun nicmp6->icmp6_code = 0;
497 1.1.2.1 itojun icmp6stat.icp6s_reflect++;
498 1.1.2.1 itojun icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
499 1.1.2.1 itojun icmp6_reflect(n, noff);
500 1.1.2.1 itojun break;
501 1.1.2.1 itojun
502 1.1.2.1 itojun case ICMP6_ECHO_REPLY:
503 1.1.2.1 itojun if (code != 0)
504 1.1.2.1 itojun goto badcode;
505 1.1.2.1 itojun break;
506 1.1.2.1 itojun
507 1.1.2.1 itojun case MLD6_LISTENER_QUERY:
508 1.1.2.1 itojun case MLD6_LISTENER_REPORT:
509 1.1.2.1 itojun if (icmp6len < sizeof(struct mld6_hdr))
510 1.1.2.1 itojun goto badlen;
511 1.1.2.1 itojun IP6_EXTHDR_CHECK(m, off, icmp6len, IPPROTO_DONE);
512 1.1.2.1 itojun mld6_input(m, off);
513 1.1.2.1 itojun /* m stays. */
514 1.1.2.1 itojun break;
515 1.1.2.1 itojun
516 1.1.2.1 itojun case MLD6_LISTENER_DONE:
517 1.1.2.1 itojun if (icmp6len < sizeof(struct mld6_hdr))
518 1.1.2.1 itojun goto badlen;
519 1.1.2.1 itojun break; /* nothing to be done in kernel */
520 1.1.2.1 itojun
521 1.1.2.1 itojun case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
522 1.1.2.1 itojun {
523 1.1.2.1 itojun enum { WRU, FQDN } mode;
524 1.1.2.1 itojun
525 1.1.2.1 itojun if (code != 0)
526 1.1.2.1 itojun goto badcode;
527 1.1.2.1 itojun if (icmp6len == sizeof(struct icmp6_hdr) + 4)
528 1.1.2.1 itojun mode = WRU;
529 1.1.2.1 itojun else if (icmp6len >= sizeof(struct icmp6_hdr) + 8) /* XXX */
530 1.1.2.1 itojun mode = FQDN;
531 1.1.2.1 itojun else
532 1.1.2.1 itojun goto badlen;
533 1.1.2.1 itojun
534 1.1.2.1 itojun #ifdef __FreeBSD__
535 1.1.2.1 itojun #define hostnamelen strlen(hostname)
536 1.1.2.1 itojun #endif
537 1.1.2.1 itojun if (mode == FQDN) {
538 1.1.2.1 itojun IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
539 1.1.2.1 itojun IPPROTO_DONE);
540 1.1.2.1 itojun n = ni6_input(m, off);
541 1.1.2.1 itojun noff = sizeof(struct ip6_hdr);
542 1.1.2.1 itojun }
543 1.1.2.1 itojun else {
544 1.1.2.1 itojun u_char *p;
545 1.1.2.1 itojun
546 1.1.2.1 itojun MGETHDR(n, M_DONTWAIT, m->m_type);
547 1.1.2.1 itojun if (n == NULL) {
548 1.1.2.1 itojun /* Give up remote */
549 1.1.2.1 itojun break;
550 1.1.2.1 itojun }
551 1.1.2.1 itojun /*
552 1.1.2.1 itojun * Copy IPv6 and ICMPv6 only.
553 1.1.2.1 itojun */
554 1.1.2.1 itojun nip6 = mtod(n, struct ip6_hdr *);
555 1.1.2.1 itojun bcopy(ip6, nip6, sizeof(struct ip6_hdr));
556 1.1.2.1 itojun nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
557 1.1.2.1 itojun bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
558 1.1.2.1 itojun p = (u_char *)(nicmp6 + 1);
559 1.1.2.1 itojun bzero(p, 4);
560 1.1.2.1 itojun bcopy(hostname, p + 4, hostnamelen);
561 1.1.2.1 itojun noff = sizeof(struct ip6_hdr);
562 1.1.2.1 itojun M_COPY_PKTHDR(n, m); /* just for recvif */
563 1.1.2.1 itojun n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
564 1.1.2.1 itojun sizeof(struct icmp6_hdr) + 4 + hostnamelen;
565 1.1.2.1 itojun nicmp6->icmp6_type = ICMP6_WRUREPLY;
566 1.1.2.1 itojun nicmp6->icmp6_code = 0;
567 1.1.2.1 itojun }
568 1.1.2.1 itojun #undef hostnamelen
569 1.1.2.1 itojun if (n) {
570 1.1.2.1 itojun icmp6stat.icp6s_reflect++;
571 1.1.2.1 itojun icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
572 1.1.2.1 itojun icmp6_reflect(n, noff);
573 1.1.2.1 itojun }
574 1.1.2.1 itojun break;
575 1.1.2.1 itojun }
576 1.1.2.1 itojun
577 1.1.2.1 itojun case ICMP6_WRUREPLY:
578 1.1.2.1 itojun if (code != 0)
579 1.1.2.1 itojun goto badcode;
580 1.1.2.1 itojun break;
581 1.1.2.1 itojun
582 1.1.2.1 itojun case ND_ROUTER_SOLICIT:
583 1.1.2.1 itojun if (code != 0)
584 1.1.2.1 itojun goto badcode;
585 1.1.2.1 itojun if (icmp6len < sizeof(struct nd_router_solicit))
586 1.1.2.1 itojun goto badlen;
587 1.1.2.1 itojun IP6_EXTHDR_CHECK(m, off, icmp6len, IPPROTO_DONE);
588 1.1.2.1 itojun nd6_rs_input(m, off, icmp6len);
589 1.1.2.1 itojun /* m stays. */
590 1.1.2.1 itojun break;
591 1.1.2.1 itojun
592 1.1.2.1 itojun case ND_ROUTER_ADVERT:
593 1.1.2.1 itojun if (code != 0)
594 1.1.2.1 itojun goto badcode;
595 1.1.2.1 itojun if (icmp6len < sizeof(struct nd_router_advert))
596 1.1.2.1 itojun goto badlen;
597 1.1.2.1 itojun IP6_EXTHDR_CHECK(m, off, icmp6len, IPPROTO_DONE);
598 1.1.2.1 itojun nd6_ra_input(m, off, icmp6len);
599 1.1.2.1 itojun /* m stays. */
600 1.1.2.1 itojun break;
601 1.1.2.1 itojun
602 1.1.2.1 itojun case ND_NEIGHBOR_SOLICIT:
603 1.1.2.1 itojun if (code != 0)
604 1.1.2.1 itojun goto badcode;
605 1.1.2.1 itojun if (icmp6len < sizeof(struct nd_neighbor_solicit))
606 1.1.2.1 itojun goto badlen;
607 1.1.2.1 itojun IP6_EXTHDR_CHECK(m, off, icmp6len, IPPROTO_DONE);
608 1.1.2.1 itojun nd6_ns_input(m, off, icmp6len);
609 1.1.2.1 itojun /* m stays. */
610 1.1.2.1 itojun break;
611 1.1.2.1 itojun
612 1.1.2.1 itojun case ND_NEIGHBOR_ADVERT:
613 1.1.2.1 itojun if (code != 0)
614 1.1.2.1 itojun goto badcode;
615 1.1.2.1 itojun if (icmp6len < sizeof(struct nd_neighbor_advert))
616 1.1.2.1 itojun goto badlen;
617 1.1.2.1 itojun IP6_EXTHDR_CHECK(m, off, icmp6len, IPPROTO_DONE);
618 1.1.2.1 itojun nd6_na_input(m, off, icmp6len);
619 1.1.2.1 itojun /* m stays. */
620 1.1.2.1 itojun break;
621 1.1.2.1 itojun
622 1.1.2.1 itojun case ND_REDIRECT:
623 1.1.2.1 itojun if (code != 0)
624 1.1.2.1 itojun goto badcode;
625 1.1.2.1 itojun if (icmp6len < sizeof(struct nd_redirect))
626 1.1.2.1 itojun goto badlen;
627 1.1.2.1 itojun icmp6_redirect_input(m, off);
628 1.1.2.1 itojun /* m stays. */
629 1.1.2.1 itojun break;
630 1.1.2.1 itojun
631 1.1.2.1 itojun case ICMP6_ROUTER_RENUMBERING:
632 1.1.2.1 itojun if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
633 1.1.2.1 itojun code != ICMP6_ROUTER_RENUMBERING_RESULT)
634 1.1.2.1 itojun goto badcode;
635 1.1.2.1 itojun if (icmp6len < sizeof(struct icmp6_router_renum))
636 1.1.2.1 itojun goto badlen;
637 1.1.2.1 itojun break;
638 1.1.2.1 itojun
639 1.1.2.1 itojun default:
640 1.1.2.1 itojun printf("icmp6_input: unknown type %d\n", icmp6->icmp6_type);
641 1.1.2.1 itojun if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
642 1.1.2.1 itojun /* ICMPv6 error: MUST deliver it by spec... */
643 1.1.2.1 itojun code = PRC_NCMDS;
644 1.1.2.1 itojun /* deliver */
645 1.1.2.1 itojun } else {
646 1.1.2.1 itojun /* ICMPv6 informational: MUST not deliver */
647 1.1.2.1 itojun break;
648 1.1.2.1 itojun }
649 1.1.2.1 itojun deliver:
650 1.1.2.1 itojun if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
651 1.1.2.1 itojun icmp6stat.icp6s_tooshort++;
652 1.1.2.1 itojun goto freeit;
653 1.1.2.1 itojun }
654 1.1.2.1 itojun IP6_EXTHDR_CHECK(m, off,
655 1.1.2.1 itojun sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr),
656 1.1.2.1 itojun IPPROTO_DONE);
657 1.1.2.1 itojun icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
658 1.1.2.1 itojun bzero(&icmp6src, sizeof(icmp6src));
659 1.1.2.1 itojun icmp6src.sin6_len = sizeof(struct sockaddr_in6);
660 1.1.2.1 itojun icmp6src.sin6_family = AF_INET6;
661 1.1.2.1 itojun icmp6src.sin6_addr = ((struct ip6_hdr *)(icmp6 + 1))->ip6_dst;
662 1.1.2.1 itojun
663 1.1.2.1 itojun /* Detect the upper level protocol */
664 1.1.2.1 itojun {
665 1.1.2.1 itojun void (*ctlfunc) __P((int, struct sockaddr *,
666 1.1.2.1 itojun struct ip6_hdr *,
667 1.1.2.1 itojun struct mbuf *, int)); /* XXX */
668 1.1.2.1 itojun struct ip6_hdr *eip6 = (struct ip6_hdr *)(icmp6 + 1);
669 1.1.2.1 itojun u_int8_t nxt = eip6->ip6_nxt;
670 1.1.2.1 itojun int eoff = off + sizeof(struct icmp6_hdr) +
671 1.1.2.1 itojun sizeof(struct ip6_hdr);
672 1.1.2.1 itojun
673 1.1.2.1 itojun while (1) { /* XXX: should avoid inf. loop explicitly? */
674 1.1.2.1 itojun struct ip6_ext *eh;
675 1.1.2.1 itojun
676 1.1.2.1 itojun switch(nxt) {
677 1.1.2.1 itojun case IPPROTO_ESP:
678 1.1.2.1 itojun case IPPROTO_NONE:
679 1.1.2.1 itojun goto passit;
680 1.1.2.1 itojun case IPPROTO_HOPOPTS:
681 1.1.2.1 itojun case IPPROTO_DSTOPTS:
682 1.1.2.1 itojun case IPPROTO_ROUTING:
683 1.1.2.1 itojun case IPPROTO_AH:
684 1.1.2.1 itojun case IPPROTO_FRAGMENT:
685 1.1.2.1 itojun IP6_EXTHDR_CHECK(m, 0, eoff +
686 1.1.2.1 itojun sizeof(struct ip6_ext),
687 1.1.2.1 itojun IPPROTO_DONE);
688 1.1.2.1 itojun eh = (struct ip6_ext *)(mtod(m, caddr_t)
689 1.1.2.1 itojun + eoff);
690 1.1.2.1 itojun if (nxt == IPPROTO_AH)
691 1.1.2.1 itojun eoff += (eh->ip6e_len + 2) << 2;
692 1.1.2.1 itojun else if (nxt == IPPROTO_FRAGMENT)
693 1.1.2.1 itojun eoff += sizeof(struct ip6_frag);
694 1.1.2.1 itojun else
695 1.1.2.1 itojun eoff += (eh->ip6e_len + 1) << 3;
696 1.1.2.1 itojun nxt = eh->ip6e_nxt;
697 1.1.2.1 itojun break;
698 1.1.2.1 itojun default:
699 1.1.2.1 itojun goto notify;
700 1.1.2.1 itojun }
701 1.1.2.1 itojun }
702 1.1.2.1 itojun notify:
703 1.1.2.1 itojun icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
704 1.1.2.1 itojun ctlfunc = (void (*) __P((int, struct sockaddr *,
705 1.1.2.1 itojun struct ip6_hdr *,
706 1.1.2.1 itojun struct mbuf *, int)))
707 1.1.2.1 itojun (inet6sw[ip6_protox[nxt]].pr_ctlinput);
708 1.1.2.1 itojun if (ctlfunc)
709 1.1.2.1 itojun (*ctlfunc)(code, (struct sockaddr *)&icmp6src,
710 1.1.2.1 itojun (struct ip6_hdr *)(icmp6 + 1),
711 1.1.2.1 itojun m, eoff);
712 1.1.2.1 itojun }
713 1.1.2.1 itojun break;
714 1.1.2.1 itojun
715 1.1.2.1 itojun badcode:
716 1.1.2.1 itojun icmp6stat.icp6s_badcode++;
717 1.1.2.1 itojun break;
718 1.1.2.1 itojun
719 1.1.2.1 itojun badlen:
720 1.1.2.1 itojun icmp6stat.icp6s_badlen++;
721 1.1.2.1 itojun break;
722 1.1.2.1 itojun }
723 1.1.2.1 itojun
724 1.1.2.1 itojun passit:
725 1.1.2.1 itojun icmp6_rip6_input(&m, *offp);
726 1.1.2.1 itojun return IPPROTO_DONE;
727 1.1.2.1 itojun
728 1.1.2.1 itojun freeit:
729 1.1.2.1 itojun m_freem(m);
730 1.1.2.1 itojun return IPPROTO_DONE;
731 1.1.2.1 itojun }
732 1.1.2.1 itojun
733 1.1.2.1 itojun /*
734 1.1.2.1 itojun * Process a Node Information Query
735 1.1.2.1 itojun */
736 1.1.2.1 itojun #ifdef __FreeBSD__
737 1.1.2.1 itojun #define hostnamelen strlen(hostname)
738 1.1.2.1 itojun #endif
739 1.1.2.1 itojun #ifndef offsetof /* XXX */
740 1.1.2.1 itojun #define offsetof(type, member) ((size_t)(&((type *)0)->member))
741 1.1.2.1 itojun #endif
742 1.1.2.1 itojun
743 1.1.2.1 itojun static struct mbuf *
744 1.1.2.1 itojun ni6_input(m, off)
745 1.1.2.1 itojun struct mbuf *m;
746 1.1.2.1 itojun int off;
747 1.1.2.1 itojun {
748 1.1.2.1 itojun struct icmp6_nodeinfo *ni6 =
749 1.1.2.1 itojun (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off), *nni6;
750 1.1.2.1 itojun struct mbuf *n = NULL;
751 1.1.2.1 itojun u_int16_t qtype = ntohs(ni6->ni_qtype);
752 1.1.2.1 itojun int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
753 1.1.2.1 itojun struct ni_reply_fqdn *fqdn;
754 1.1.2.1 itojun int addrs; /* for NI_QTYPE_NODEADDR */
755 1.1.2.1 itojun struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
756 1.1.2.1 itojun
757 1.1.2.1 itojun switch(qtype) {
758 1.1.2.1 itojun case NI_QTYPE_NOOP:
759 1.1.2.1 itojun break; /* no reply data */
760 1.1.2.1 itojun case NI_QTYPE_SUPTYPES:
761 1.1.2.1 itojun goto bad; /* xxx: to be implemented */
762 1.1.2.1 itojun break;
763 1.1.2.1 itojun case NI_QTYPE_FQDN:
764 1.1.2.1 itojun replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_name) +
765 1.1.2.1 itojun hostnamelen;
766 1.1.2.1 itojun break;
767 1.1.2.1 itojun case NI_QTYPE_NODEADDR:
768 1.1.2.1 itojun addrs = ni6_addrs(ni6, m, &ifp);
769 1.1.2.1 itojun if ((replylen += addrs * sizeof(struct in6_addr)) > MCLBYTES)
770 1.1.2.1 itojun replylen = MCLBYTES; /* XXX: we'll truncate later */
771 1.1.2.1 itojun
772 1.1.2.1 itojun break;
773 1.1.2.1 itojun default:
774 1.1.2.1 itojun /*
775 1.1.2.1 itojun * XXX: We must return a reply with the ICMP6 code
776 1.1.2.1 itojun * `unknown Qtype' in this case. However we regard the case
777 1.1.2.1 itojun * as an FQDN query for backward compatibility.
778 1.1.2.1 itojun * Older versions set a random value to this field,
779 1.1.2.1 itojun * so it rarely varies in the defined qtypes.
780 1.1.2.1 itojun * But the mechanism is not reliable...
781 1.1.2.1 itojun * maybe we should obsolete older versions.
782 1.1.2.1 itojun */
783 1.1.2.1 itojun qtype = NI_QTYPE_FQDN;
784 1.1.2.1 itojun replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_name) +
785 1.1.2.1 itojun hostnamelen;
786 1.1.2.1 itojun break;
787 1.1.2.1 itojun }
788 1.1.2.1 itojun
789 1.1.2.1 itojun /* allocate a mbuf to reply. */
790 1.1.2.1 itojun MGETHDR(n, M_DONTWAIT, m->m_type);
791 1.1.2.1 itojun if (n == NULL)
792 1.1.2.1 itojun return(NULL);
793 1.1.2.1 itojun M_COPY_PKTHDR(n, m); /* just for recvif */
794 1.1.2.1 itojun if (replylen > MHLEN) {
795 1.1.2.1 itojun if (replylen > MCLBYTES)
796 1.1.2.1 itojun /*
797 1.1.2.1 itojun * XXX: should we try to allocate more? But MCLBYTES is
798 1.1.2.1 itojun * probably much larger than IPV6_MMTU...
799 1.1.2.1 itojun */
800 1.1.2.1 itojun goto bad;
801 1.1.2.1 itojun MCLGET(n, M_DONTWAIT);
802 1.1.2.1 itojun if ((n->m_flags & M_EXT) == 0) {
803 1.1.2.1 itojun goto bad;
804 1.1.2.1 itojun }
805 1.1.2.1 itojun }
806 1.1.2.1 itojun n->m_pkthdr.len = n->m_len = replylen;
807 1.1.2.1 itojun
808 1.1.2.1 itojun /* copy mbuf header and IPv6 + Node Information base headers */
809 1.1.2.1 itojun bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
810 1.1.2.1 itojun nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
811 1.1.2.1 itojun bcopy(mtod(m, caddr_t) + off, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
812 1.1.2.1 itojun
813 1.1.2.1 itojun /* qtype dependent procedure */
814 1.1.2.1 itojun switch (qtype) {
815 1.1.2.1 itojun case NI_QTYPE_NOOP:
816 1.1.2.1 itojun nni6->ni_flags = 0;
817 1.1.2.1 itojun break;
818 1.1.2.1 itojun case NI_QTYPE_SUPTYPES:
819 1.1.2.1 itojun goto bad; /* xxx: to be implemented */
820 1.1.2.1 itojun break;
821 1.1.2.1 itojun case NI_QTYPE_FQDN:
822 1.1.2.1 itojun if (hostnamelen > 255) { /* XXX: rare case, but may happen */
823 1.1.2.1 itojun printf("ni6_input: "
824 1.1.2.1 itojun "hostname length(%d) is too large for reply\n",
825 1.1.2.1 itojun hostnamelen);
826 1.1.2.1 itojun goto bad;
827 1.1.2.1 itojun }
828 1.1.2.1 itojun fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
829 1.1.2.1 itojun sizeof(struct ip6_hdr) +
830 1.1.2.1 itojun sizeof(struct icmp6_nodeinfo));
831 1.1.2.1 itojun nni6->ni_flags = 0; /* XXX: meaningless TTL */
832 1.1.2.1 itojun fqdn->ni_fqdn_ttl = 0; /* ditto. */
833 1.1.2.1 itojun fqdn->ni_fqdn_namelen = hostnamelen;
834 1.1.2.1 itojun bcopy(hostname, &fqdn->ni_fqdn_name[0], hostnamelen);
835 1.1.2.1 itojun break;
836 1.1.2.1 itojun case NI_QTYPE_NODEADDR:
837 1.1.2.1 itojun {
838 1.1.2.1 itojun int lenlim, copied;
839 1.1.2.1 itojun
840 1.1.2.1 itojun if (n->m_flags & M_EXT)
841 1.1.2.1 itojun lenlim = MCLBYTES - sizeof(struct ip6_hdr) -
842 1.1.2.1 itojun sizeof(struct icmp6_nodeinfo);
843 1.1.2.1 itojun else
844 1.1.2.1 itojun lenlim = MHLEN - sizeof(struct ip6_hdr) -
845 1.1.2.1 itojun sizeof(struct icmp6_nodeinfo);
846 1.1.2.1 itojun copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
847 1.1.2.1 itojun /* XXX: reset mbuf length */
848 1.1.2.1 itojun n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
849 1.1.2.1 itojun sizeof(struct icmp6_nodeinfo) + copied;
850 1.1.2.1 itojun break;
851 1.1.2.1 itojun }
852 1.1.2.1 itojun default:
853 1.1.2.1 itojun break; /* XXX impossible! */
854 1.1.2.1 itojun }
855 1.1.2.1 itojun
856 1.1.2.1 itojun nni6->ni_type = ICMP6_NI_REPLY;
857 1.1.2.1 itojun nni6->ni_code = ICMP6_NI_SUCESS;
858 1.1.2.1 itojun return(n);
859 1.1.2.1 itojun
860 1.1.2.1 itojun bad:
861 1.1.2.1 itojun if (n)
862 1.1.2.1 itojun m_freem(n);
863 1.1.2.1 itojun return(NULL);
864 1.1.2.1 itojun }
865 1.1.2.1 itojun #undef hostnamelen
866 1.1.2.1 itojun
867 1.1.2.1 itojun /*
868 1.1.2.1 itojun * calculate the number of addresses to be returned in the node info reply.
869 1.1.2.1 itojun */
870 1.1.2.1 itojun static int
871 1.1.2.1 itojun ni6_addrs(ni6, m, ifpp)
872 1.1.2.1 itojun struct icmp6_nodeinfo *ni6;
873 1.1.2.1 itojun struct mbuf *m;
874 1.1.2.1 itojun struct ifnet **ifpp;
875 1.1.2.1 itojun {
876 1.1.2.1 itojun register struct ifnet *ifp;
877 1.1.2.1 itojun register struct in6_ifaddr *ifa6;
878 1.1.2.1 itojun register struct ifaddr *ifa;
879 1.1.2.1 itojun struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
880 1.1.2.1 itojun int addrs = 0, addrsofif, iffound = 0;
881 1.1.2.1 itojun
882 1.1.2.1 itojun #ifdef __NetBSD__
883 1.1.2.1 itojun for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
884 1.1.2.1 itojun #else
885 1.1.2.1 itojun for (ifp = ifnet; ifp; ifp = ifp->if_next)
886 1.1.2.1 itojun #endif
887 1.1.2.1 itojun {
888 1.1.2.1 itojun addrsofif = 0;
889 1.1.2.1 itojun #ifdef __NetBSD__
890 1.1.2.1 itojun for (ifa = ifp->if_addrlist.tqh_first; ifa;
891 1.1.2.1 itojun ifa = ifa->ifa_list.tqe_next)
892 1.1.2.1 itojun #else
893 1.1.2.1 itojun for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
894 1.1.2.1 itojun #endif
895 1.1.2.1 itojun {
896 1.1.2.1 itojun if (ifa->ifa_addr->sa_family != AF_INET6)
897 1.1.2.1 itojun continue;
898 1.1.2.1 itojun ifa6 = (struct in6_ifaddr *)ifa;
899 1.1.2.1 itojun
900 1.1.2.1 itojun if (!(ni6->ni_flags & NI_NODEADDR_FLAG_ALL) &&
901 1.1.2.1 itojun IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
902 1.1.2.1 itojun &ifa6->ia_addr.sin6_addr))
903 1.1.2.1 itojun iffound = 1;
904 1.1.2.1 itojun
905 1.1.2.1 itojun if (ifa6->ia6_flags & IN6_IFF_ANYCAST)
906 1.1.2.1 itojun continue; /* we need only unicast addresses */
907 1.1.2.1 itojun
908 1.1.2.1 itojun if ((ni6->ni_flags & (NI_NODEADDR_FLAG_LINKLOCAL |
909 1.1.2.1 itojun NI_NODEADDR_FLAG_SITELOCAL |
910 1.1.2.1 itojun NI_NODEADDR_FLAG_GLOBAL)) == 0)
911 1.1.2.1 itojun continue;
912 1.1.2.1 itojun
913 1.1.2.1 itojun /* What do we have to do about ::1? */
914 1.1.2.1 itojun switch(in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
915 1.1.2.1 itojun case IPV6_ADDR_SCOPE_LINKLOCAL:
916 1.1.2.1 itojun if (ni6->ni_flags & NI_NODEADDR_FLAG_LINKLOCAL)
917 1.1.2.1 itojun addrsofif++;
918 1.1.2.1 itojun break;
919 1.1.2.1 itojun case IPV6_ADDR_SCOPE_SITELOCAL:
920 1.1.2.1 itojun if (ni6->ni_flags & NI_NODEADDR_FLAG_SITELOCAL)
921 1.1.2.1 itojun addrsofif++;
922 1.1.2.1 itojun break;
923 1.1.2.1 itojun case IPV6_ADDR_SCOPE_GLOBAL:
924 1.1.2.1 itojun if (ni6->ni_flags & NI_NODEADDR_FLAG_GLOBAL)
925 1.1.2.1 itojun addrsofif++;
926 1.1.2.1 itojun break;
927 1.1.2.1 itojun default:
928 1.1.2.1 itojun continue;
929 1.1.2.1 itojun }
930 1.1.2.1 itojun }
931 1.1.2.1 itojun if (iffound) {
932 1.1.2.1 itojun *ifpp = ifp;
933 1.1.2.1 itojun return(addrsofif);
934 1.1.2.1 itojun }
935 1.1.2.1 itojun
936 1.1.2.1 itojun addrs += addrsofif;
937 1.1.2.1 itojun }
938 1.1.2.1 itojun
939 1.1.2.1 itojun return(addrs);
940 1.1.2.1 itojun }
941 1.1.2.1 itojun
942 1.1.2.1 itojun static int
943 1.1.2.1 itojun ni6_store_addrs(ni6, nni6, ifp0, resid)
944 1.1.2.1 itojun struct icmp6_nodeinfo *ni6, *nni6;
945 1.1.2.1 itojun struct ifnet *ifp0;
946 1.1.2.1 itojun int resid;
947 1.1.2.1 itojun {
948 1.1.2.1 itojun #ifdef __NetBSD__
949 1.1.2.1 itojun register struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
950 1.1.2.1 itojun #else
951 1.1.2.1 itojun register struct ifnet *ifp = ifp0 ? ifp0 : ifnet;
952 1.1.2.1 itojun #endif
953 1.1.2.1 itojun register struct in6_ifaddr *ifa6;
954 1.1.2.1 itojun register struct ifaddr *ifa;
955 1.1.2.1 itojun int docopy, copied = 0;
956 1.1.2.1 itojun u_char *cp = (u_char *)(nni6 + 1);
957 1.1.2.1 itojun
958 1.1.2.1 itojun if (ifp0 == NULL && !(ni6->ni_flags & NI_NODEADDR_FLAG_ALL))
959 1.1.2.1 itojun return(0); /* needless to copy */
960 1.1.2.1 itojun
961 1.1.2.1 itojun #ifdef __NetBSD__
962 1.1.2.1 itojun for (; ifp; ifp = TAILQ_NEXT(ifp, if_list))
963 1.1.2.1 itojun #else
964 1.1.2.1 itojun for (; ifp; ifp = ifp->if_next)
965 1.1.2.1 itojun #endif
966 1.1.2.1 itojun {
967 1.1.2.1 itojun #ifdef __NetBSD__
968 1.1.2.1 itojun for (ifa = ifp->if_addrlist.tqh_first; ifa;
969 1.1.2.1 itojun ifa = ifa->ifa_list.tqe_next)
970 1.1.2.1 itojun #else
971 1.1.2.1 itojun for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
972 1.1.2.1 itojun #endif
973 1.1.2.1 itojun {
974 1.1.2.1 itojun docopy = 0;
975 1.1.2.1 itojun
976 1.1.2.1 itojun if (ifa->ifa_addr->sa_family != AF_INET6)
977 1.1.2.1 itojun continue;
978 1.1.2.1 itojun ifa6 = (struct in6_ifaddr *)ifa;
979 1.1.2.1 itojun
980 1.1.2.1 itojun if (ifa6->ia6_flags & IN6_IFF_ANYCAST)
981 1.1.2.1 itojun continue; /* we need only unicast addresses */
982 1.1.2.1 itojun
983 1.1.2.1 itojun /* What do we have to do about ::1? */
984 1.1.2.1 itojun switch(in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
985 1.1.2.1 itojun case IPV6_ADDR_SCOPE_LINKLOCAL:
986 1.1.2.1 itojun if (ni6->ni_flags & NI_NODEADDR_FLAG_LINKLOCAL)
987 1.1.2.1 itojun docopy = 1;
988 1.1.2.1 itojun break;
989 1.1.2.1 itojun case IPV6_ADDR_SCOPE_SITELOCAL:
990 1.1.2.1 itojun if (ni6->ni_flags & NI_NODEADDR_FLAG_SITELOCAL)
991 1.1.2.1 itojun docopy = 1;
992 1.1.2.1 itojun break;
993 1.1.2.1 itojun case IPV6_ADDR_SCOPE_GLOBAL:
994 1.1.2.1 itojun if (ni6->ni_flags & NI_NODEADDR_FLAG_GLOBAL)
995 1.1.2.1 itojun docopy = 1;
996 1.1.2.1 itojun break;
997 1.1.2.1 itojun default:
998 1.1.2.1 itojun continue;
999 1.1.2.1 itojun }
1000 1.1.2.1 itojun
1001 1.1.2.1 itojun if (docopy) {
1002 1.1.2.1 itojun if (resid < sizeof(struct in6_addr)) {
1003 1.1.2.1 itojun /*
1004 1.1.2.1 itojun * We give up much more copy.
1005 1.1.2.1 itojun * Set the truncate flag and return.
1006 1.1.2.1 itojun */
1007 1.1.2.1 itojun nni6->ni_flags |=
1008 1.1.2.1 itojun NI_NODEADDR_FLAG_TRUNCATE;
1009 1.1.2.1 itojun return(copied);
1010 1.1.2.1 itojun }
1011 1.1.2.1 itojun bcopy(&ifa6->ia_addr.sin6_addr, cp,
1012 1.1.2.1 itojun sizeof(struct in6_addr));
1013 1.1.2.1 itojun /* XXX: KAME link-local hack; remove ifindex */
1014 1.1.2.1 itojun if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr))
1015 1.1.2.1 itojun ((struct in6_addr *)cp)->s6_addr16[1] = 0;
1016 1.1.2.1 itojun cp += sizeof(struct in6_addr);
1017 1.1.2.1 itojun resid -= sizeof(struct in6_addr);
1018 1.1.2.1 itojun copied += sizeof(struct in6_addr);
1019 1.1.2.1 itojun }
1020 1.1.2.1 itojun }
1021 1.1.2.1 itojun if (ifp0) /* we need search only on the specified IF */
1022 1.1.2.1 itojun break;
1023 1.1.2.1 itojun }
1024 1.1.2.1 itojun
1025 1.1.2.1 itojun return(copied);
1026 1.1.2.1 itojun }
1027 1.1.2.1 itojun
1028 1.1.2.1 itojun /*
1029 1.1.2.1 itojun * XXX almost dup'ed code with rip6_input.
1030 1.1.2.1 itojun */
1031 1.1.2.1 itojun static int
1032 1.1.2.1 itojun icmp6_rip6_input(mp, off)
1033 1.1.2.1 itojun struct mbuf **mp;
1034 1.1.2.1 itojun int off;
1035 1.1.2.1 itojun {
1036 1.1.2.1 itojun struct mbuf *m = *mp;
1037 1.1.2.1 itojun register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1038 1.1.2.1 itojun register struct in6pcb *in6p;
1039 1.1.2.1 itojun struct in6pcb *last = NULL;
1040 1.1.2.1 itojun struct sockaddr_in6 rip6src;
1041 1.1.2.1 itojun struct icmp6_hdr *icmp6;
1042 1.1.2.1 itojun struct mbuf *opts = NULL;
1043 1.1.2.1 itojun
1044 1.1.2.1 itojun /* this is assumed to be safe. */
1045 1.1.2.1 itojun icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1046 1.1.2.1 itojun
1047 1.1.2.1 itojun bzero(&rip6src, sizeof(rip6src));
1048 1.1.2.1 itojun rip6src.sin6_len = sizeof(struct sockaddr_in6);
1049 1.1.2.1 itojun rip6src.sin6_family = AF_INET6;
1050 1.1.2.1 itojun rip6src.sin6_addr = ip6->ip6_src;
1051 1.1.2.1 itojun if (IN6_IS_SCOPE_LINKLOCAL(&rip6src.sin6_addr))
1052 1.1.2.1 itojun rip6src.sin6_addr.s6_addr16[1] = 0;
1053 1.1.2.1 itojun if (m->m_pkthdr.rcvif) {
1054 1.1.2.1 itojun if (IN6_IS_SCOPE_LINKLOCAL(&rip6src.sin6_addr))
1055 1.1.2.1 itojun rip6src.sin6_scope_id = m->m_pkthdr.rcvif->if_index;
1056 1.1.2.1 itojun else
1057 1.1.2.1 itojun rip6src.sin6_scope_id = 0;
1058 1.1.2.1 itojun } else
1059 1.1.2.1 itojun rip6src.sin6_scope_id = 0;
1060 1.1.2.1 itojun
1061 1.1.2.1 itojun #if !defined(__FreeBSD__) || __FreeBSD__ < 3
1062 1.1.2.1 itojun for (in6p = rawin6pcb.in6p_next;
1063 1.1.2.1 itojun in6p != &rawin6pcb; in6p = in6p->in6p_next)
1064 1.1.2.1 itojun #else
1065 1.1.2.1 itojun LIST_FOREACH(in6p, &ripcb, inp_list)
1066 1.1.2.1 itojun #endif
1067 1.1.2.1 itojun {
1068 1.1.2.1 itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1069 1.1.2.1 itojun if ((in6p->inp_vflag & INP_IPV6) == NULL)
1070 1.1.2.1 itojun continue;
1071 1.1.2.1 itojun #endif
1072 1.1.2.1 itojun if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
1073 1.1.2.1 itojun continue;
1074 1.1.2.1 itojun if (!IN6_IS_ADDR_ANY(&in6p->in6p_laddr) &&
1075 1.1.2.1 itojun !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1076 1.1.2.1 itojun continue;
1077 1.1.2.1 itojun if (!IN6_IS_ADDR_ANY(&in6p->in6p_faddr) &&
1078 1.1.2.1 itojun !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1079 1.1.2.1 itojun continue;
1080 1.1.2.1 itojun if (in6p->in6p_icmp6filt
1081 1.1.2.1 itojun && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1082 1.1.2.1 itojun in6p->in6p_icmp6filt))
1083 1.1.2.1 itojun continue;
1084 1.1.2.1 itojun if (last) {
1085 1.1.2.1 itojun struct mbuf *n;
1086 1.1.2.1 itojun if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1087 1.1.2.1 itojun if (last->in6p_flags & IN6P_CONTROLOPTS)
1088 1.1.2.1 itojun ip6_savecontrol(last, &opts, ip6, n);
1089 1.1.2.1 itojun /* strip intermediate headers */
1090 1.1.2.1 itojun m_adj(n, off);
1091 1.1.2.1 itojun if (sbappendaddr(&last->in6p_socket->so_rcv,
1092 1.1.2.1 itojun (struct sockaddr *)&rip6src,
1093 1.1.2.1 itojun n, opts) == 0) {
1094 1.1.2.1 itojun /* should notify about lost packet */
1095 1.1.2.1 itojun m_freem(n);
1096 1.1.2.1 itojun if (opts)
1097 1.1.2.1 itojun m_freem(opts);
1098 1.1.2.1 itojun } else
1099 1.1.2.1 itojun sorwakeup(last->in6p_socket);
1100 1.1.2.1 itojun opts = NULL;
1101 1.1.2.1 itojun }
1102 1.1.2.1 itojun }
1103 1.1.2.1 itojun last = in6p;
1104 1.1.2.1 itojun }
1105 1.1.2.1 itojun if (last) {
1106 1.1.2.1 itojun if (last->in6p_flags & IN6P_CONTROLOPTS)
1107 1.1.2.1 itojun ip6_savecontrol(last, &opts, ip6, m);
1108 1.1.2.1 itojun /* strip intermediate headers */
1109 1.1.2.1 itojun m_adj(m, off);
1110 1.1.2.1 itojun if (sbappendaddr(&last->in6p_socket->so_rcv,
1111 1.1.2.1 itojun (struct sockaddr *)&rip6src, m, opts) == 0) {
1112 1.1.2.1 itojun m_freem(m);
1113 1.1.2.1 itojun if (opts)
1114 1.1.2.1 itojun m_freem(opts);
1115 1.1.2.1 itojun } else
1116 1.1.2.1 itojun sorwakeup(last->in6p_socket);
1117 1.1.2.1 itojun } else {
1118 1.1.2.1 itojun m_freem(m);
1119 1.1.2.1 itojun ip6stat.ip6s_delivered--;
1120 1.1.2.1 itojun }
1121 1.1.2.1 itojun return IPPROTO_DONE;
1122 1.1.2.1 itojun }
1123 1.1.2.1 itojun
1124 1.1.2.1 itojun /*
1125 1.1.2.1 itojun * Reflect the ip6 packet back to the source.
1126 1.1.2.1 itojun * The caller MUST check if the destination is multicast or not.
1127 1.1.2.1 itojun * This function is usually called with a unicast destination which
1128 1.1.2.1 itojun * can be safely the source of the reply packet. But some exceptions
1129 1.1.2.1 itojun * exist(e.g. ECHOREPLY, PATCKET_TOOBIG, "10" in OPTION type).
1130 1.1.2.1 itojun * ``off'' points to the icmp6 header, counted from the top of the mbuf.
1131 1.1.2.1 itojun */
1132 1.1.2.1 itojun void
1133 1.1.2.1 itojun icmp6_reflect(m, off)
1134 1.1.2.1 itojun struct mbuf *m;
1135 1.1.2.1 itojun size_t off;
1136 1.1.2.1 itojun {
1137 1.1.2.1 itojun struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1138 1.1.2.1 itojun struct icmp6_hdr *icmp6;
1139 1.1.2.1 itojun struct in6_ifaddr *ia;
1140 1.1.2.1 itojun struct in6_addr t, *src = 0;
1141 1.1.2.1 itojun int plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
1142 1.1.2.1 itojun #ifdef COMPAT_RFC1885
1143 1.1.2.1 itojun int mtu = IPV6_MMTU;
1144 1.1.2.1 itojun struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst;
1145 1.1.2.1 itojun #endif
1146 1.1.2.1 itojun
1147 1.1.2.1 itojun /*
1148 1.1.2.1 itojun * If there are extra headers between IPv6 and ICMPv6, strip
1149 1.1.2.1 itojun * off that header first.
1150 1.1.2.1 itojun */
1151 1.1.2.1 itojun if (off != sizeof(struct ip6_hdr)) {
1152 1.1.2.1 itojun size_t siz;
1153 1.1.2.1 itojun
1154 1.1.2.1 itojun /* sanity checks */
1155 1.1.2.1 itojun if (off < sizeof(struct ip6_hdr)) {
1156 1.1.2.1 itojun printf("sanity fail: off=%x, sizeof(ip6)=%x in %s:%d\n",
1157 1.1.2.1 itojun off, sizeof(struct ip6_hdr), __FILE__, __LINE__);
1158 1.1.2.1 itojun goto bad;
1159 1.1.2.1 itojun }
1160 1.1.2.1 itojun siz = off - sizeof(struct ip6_hdr);
1161 1.1.2.1 itojun if (plen < siz) {
1162 1.1.2.1 itojun printf("sanity fail: siz=%x, payloadlen=%x in %s:%d\n",
1163 1.1.2.1 itojun siz, plen, __FILE__, __LINE__);
1164 1.1.2.1 itojun goto bad;
1165 1.1.2.1 itojun }
1166 1.1.2.1 itojun IP6_EXTHDR_CHECK(m, 0, off, /*nothing*/);
1167 1.1.2.1 itojun IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), /*nothing*/);
1168 1.1.2.1 itojun
1169 1.1.2.1 itojun bcopy((caddr_t)ip6,
1170 1.1.2.1 itojun (caddr_t)(mtod(m, u_char *) + siz),
1171 1.1.2.1 itojun sizeof(struct ip6_hdr));
1172 1.1.2.1 itojun m->m_data += siz;
1173 1.1.2.1 itojun m->m_len -= siz;
1174 1.1.2.1 itojun m->m_pkthdr.len -= siz;
1175 1.1.2.1 itojun ip6 = mtod(m, struct ip6_hdr *);
1176 1.1.2.1 itojun ip6->ip6_nxt = IPPROTO_ICMPV6;
1177 1.1.2.1 itojun plen -= siz;
1178 1.1.2.1 itojun }
1179 1.1.2.1 itojun
1180 1.1.2.1 itojun icmp6 = (struct icmp6_hdr *)(ip6 + 1);
1181 1.1.2.1 itojun
1182 1.1.2.1 itojun t = ip6->ip6_dst;
1183 1.1.2.1 itojun /*
1184 1.1.2.1 itojun * ip6_input() drops a packet if its src is multicast.
1185 1.1.2.1 itojun * So, the src is never multicast.
1186 1.1.2.1 itojun */
1187 1.1.2.1 itojun ip6->ip6_dst = ip6->ip6_src;
1188 1.1.2.1 itojun
1189 1.1.2.1 itojun /* XXX hack for link-local addresses */
1190 1.1.2.1 itojun if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
1191 1.1.2.1 itojun ip6->ip6_dst.s6_addr16[1] =
1192 1.1.2.1 itojun htons(m->m_pkthdr.rcvif->if_index);
1193 1.1.2.1 itojun if (IN6_IS_ADDR_LINKLOCAL(&t))
1194 1.1.2.1 itojun t.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
1195 1.1.2.1 itojun
1196 1.1.2.1 itojun #ifdef COMPAT_RFC1885
1197 1.1.2.1 itojun /*
1198 1.1.2.1 itojun * xxx guess MTU
1199 1.1.2.1 itojun * RFC 1885 requires that echo reply should be truncated if it
1200 1.1.2.1 itojun * does not fit in with (return) path MTU, but the description was
1201 1.1.2.1 itojun * removed in the new spec.
1202 1.1.2.1 itojun */
1203 1.1.2.1 itojun if (icmp6_reflect_rt.ro_rt == 0 ||
1204 1.1.2.1 itojun ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) {
1205 1.1.2.1 itojun if (icmp6_reflect_rt.ro_rt) {
1206 1.1.2.1 itojun #ifdef __FreeBSD__
1207 1.1.2.1 itojun RTFREE(icmp6_reflect_rt.ro_rt);
1208 1.1.2.1 itojun #endif
1209 1.1.2.1 itojun #ifdef __bsdi__
1210 1.1.2.1 itojun rtfree(icmp6_reflect_rt.ro_rt);
1211 1.1.2.1 itojun #endif
1212 1.1.2.1 itojun icmp6_reflect_rt.ro_rt = 0;
1213 1.1.2.1 itojun }
1214 1.1.2.1 itojun bzero(sin6, sizeof(*sin6));
1215 1.1.2.1 itojun sin6->sin6_family = PF_INET6;
1216 1.1.2.1 itojun sin6->sin6_len = sizeof(struct sockaddr_in6);
1217 1.1.2.1 itojun sin6->sin6_addr = ip6->ip6_dst;
1218 1.1.2.1 itojun
1219 1.1.2.1 itojun #ifdef __NetBSD__
1220 1.1.2.1 itojun rtalloc((struct route *)&icmp6_reflect_rt.ro_rt);
1221 1.1.2.1 itojun #else
1222 1.1.2.1 itojun rtalloc_ign((struct route *)&icmp6_reflect_rt.ro_rt,
1223 1.1.2.1 itojun RTF_PRCLONING);
1224 1.1.2.1 itojun #endif
1225 1.1.2.1 itojun }
1226 1.1.2.1 itojun
1227 1.1.2.1 itojun if (icmp6_reflect_rt.ro_rt == 0)
1228 1.1.2.1 itojun goto bad;
1229 1.1.2.1 itojun
1230 1.1.2.1 itojun if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST)
1231 1.1.2.1 itojun && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu)
1232 1.1.2.1 itojun mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu;
1233 1.1.2.1 itojun
1234 1.1.2.1 itojun if (mtu < m->m_pkthdr.len) {
1235 1.1.2.1 itojun plen -= (m->m_pkthdr.len - mtu);
1236 1.1.2.1 itojun m_adj(m, mtu - m->m_pkthdr.len);
1237 1.1.2.1 itojun }
1238 1.1.2.1 itojun #endif
1239 1.1.2.1 itojun /*
1240 1.1.2.1 itojun * If the incoming packet was addressed directly to us(i.e. unicast),
1241 1.1.2.1 itojun * use dst as the src for the reply.
1242 1.1.2.1 itojun */
1243 1.1.2.1 itojun for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1244 1.1.2.1 itojun if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) &&
1245 1.1.2.1 itojun (ia->ia6_flags & IN6_IFF_ANYCAST) == 0) {
1246 1.1.2.1 itojun src = &t;
1247 1.1.2.1 itojun break;
1248 1.1.2.1 itojun }
1249 1.1.2.1 itojun if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) {
1250 1.1.2.1 itojun /*
1251 1.1.2.1 itojun * This is the case if the dst is our link-local address
1252 1.1.2.1 itojun * and the sender is also ourseleves.
1253 1.1.2.1 itojun */
1254 1.1.2.1 itojun src = &t;
1255 1.1.2.1 itojun }
1256 1.1.2.1 itojun
1257 1.1.2.1 itojun if (src == 0)
1258 1.1.2.1 itojun /*
1259 1.1.2.1 itojun * We have not multicast routing yet. So this case matches
1260 1.1.2.1 itojun * to our multicast, our anycast or not to our unicast.
1261 1.1.2.1 itojun * Select a source address which has the same scope.
1262 1.1.2.1 itojun */
1263 1.1.2.1 itojun if ((ia = in6_ifawithscope(m->m_pkthdr.rcvif, &t)) != 0)
1264 1.1.2.1 itojun src = &IA6_SIN6(ia)->sin6_addr;
1265 1.1.2.1 itojun
1266 1.1.2.1 itojun if (src == 0)
1267 1.1.2.1 itojun goto bad;
1268 1.1.2.1 itojun
1269 1.1.2.1 itojun ip6->ip6_src = *src;
1270 1.1.2.1 itojun
1271 1.1.2.1 itojun ip6->ip6_flow = 0;
1272 1.1.2.1 itojun ip6->ip6_vfc = IPV6_VERSION;
1273 1.1.2.1 itojun ip6->ip6_nxt = IPPROTO_ICMPV6;
1274 1.1.2.1 itojun if (m->m_pkthdr.rcvif) {
1275 1.1.2.1 itojun /* XXX: This may not be the outgoing interface */
1276 1.1.2.1 itojun ip6->ip6_hlim = nd_ifinfo[m->m_pkthdr.rcvif->if_index].chlim;
1277 1.1.2.1 itojun }
1278 1.1.2.1 itojun
1279 1.1.2.1 itojun icmp6->icmp6_cksum = 0;
1280 1.1.2.1 itojun icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
1281 1.1.2.1 itojun sizeof(struct ip6_hdr), plen);
1282 1.1.2.1 itojun
1283 1.1.2.1 itojun /*
1284 1.1.2.1 itojun * xxx option handling
1285 1.1.2.1 itojun */
1286 1.1.2.1 itojun
1287 1.1.2.1 itojun m->m_flags &= ~(M_BCAST|M_MCAST);
1288 1.1.2.1 itojun #ifdef IPSEC
1289 1.1.2.1 itojun m->m_pkthdr.rcvif = NULL;
1290 1.1.2.1 itojun #endif /*IPSEC*/
1291 1.1.2.1 itojun
1292 1.1.2.1 itojun #ifdef COMPAT_RFC1885
1293 1.1.2.1 itojun ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL);
1294 1.1.2.1 itojun #else
1295 1.1.2.1 itojun ip6_output(m, NULL, NULL, 0, NULL);
1296 1.1.2.1 itojun #endif
1297 1.1.2.1 itojun
1298 1.1.2.1 itojun return;
1299 1.1.2.1 itojun
1300 1.1.2.1 itojun bad:
1301 1.1.2.1 itojun m_freem(m);
1302 1.1.2.1 itojun return;
1303 1.1.2.1 itojun }
1304 1.1.2.1 itojun
1305 1.1.2.1 itojun void
1306 1.1.2.1 itojun icmp6_fasttimo()
1307 1.1.2.1 itojun {
1308 1.1.2.1 itojun mld6_fasttimeo();
1309 1.1.2.1 itojun }
1310 1.1.2.1 itojun
1311 1.1.2.1 itojun void
1312 1.1.2.1 itojun icmp6_redirect_input(m, off)
1313 1.1.2.1 itojun register struct mbuf *m;
1314 1.1.2.1 itojun int off;
1315 1.1.2.1 itojun {
1316 1.1.2.1 itojun struct ifnet *ifp = m->m_pkthdr.rcvif;
1317 1.1.2.1 itojun struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1318 1.1.2.1 itojun struct nd_redirect *nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
1319 1.1.2.1 itojun int icmp6len = ntohs(ip6->ip6_plen);
1320 1.1.2.1 itojun char *lladdr = NULL;
1321 1.1.2.1 itojun int lladdrlen = 0;
1322 1.1.2.1 itojun u_char *redirhdr = NULL;
1323 1.1.2.1 itojun int redirhdrlen = 0;
1324 1.1.2.1 itojun struct rtentry *rt = NULL;
1325 1.1.2.1 itojun int is_router;
1326 1.1.2.1 itojun int is_onlink;
1327 1.1.2.1 itojun struct in6_addr src6 = ip6->ip6_src;
1328 1.1.2.1 itojun struct in6_addr redtgt6 = nd_rd->nd_rd_target;
1329 1.1.2.1 itojun struct in6_addr reddst6 = nd_rd->nd_rd_dst;
1330 1.1.2.1 itojun union nd_opts ndopts;
1331 1.1.2.1 itojun
1332 1.1.2.1 itojun if (!m || !ifp)
1333 1.1.2.1 itojun return;
1334 1.1.2.1 itojun
1335 1.1.2.1 itojun /* XXX if we are router, we don't update route by icmp6 redirect */
1336 1.1.2.1 itojun if (ip6_forwarding)
1337 1.1.2.1 itojun return;
1338 1.1.2.1 itojun if (!icmp6_rediraccept)
1339 1.1.2.1 itojun return;
1340 1.1.2.1 itojun
1341 1.1.2.1 itojun if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
1342 1.1.2.1 itojun redtgt6.s6_addr16[1] = htons(ifp->if_index);
1343 1.1.2.1 itojun if (IN6_IS_ADDR_LINKLOCAL(&reddst6))
1344 1.1.2.1 itojun reddst6.s6_addr16[1] = htons(ifp->if_index);
1345 1.1.2.1 itojun
1346 1.1.2.1 itojun /* validation */
1347 1.1.2.1 itojun if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
1348 1.1.2.1 itojun log(LOG_ERR,
1349 1.1.2.1 itojun "ICMP6 redirect sent from %s rejected; "
1350 1.1.2.1 itojun "must be from linklocal\n", ip6_sprintf(&src6));
1351 1.1.2.1 itojun return;
1352 1.1.2.1 itojun }
1353 1.1.2.1 itojun if (ip6->ip6_hlim != 255) {
1354 1.1.2.1 itojun log(LOG_ERR,
1355 1.1.2.1 itojun "ICMP6 redirect sent from %s rejected; "
1356 1.1.2.1 itojun "hlim=%d (must be 255)\n",
1357 1.1.2.1 itojun ip6_sprintf(&src6), ip6->ip6_hlim);
1358 1.1.2.1 itojun return;
1359 1.1.2.1 itojun }
1360 1.1.2.1 itojun {
1361 1.1.2.1 itojun /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
1362 1.1.2.1 itojun struct sockaddr_in6 sin6;
1363 1.1.2.1 itojun struct in6_addr *gw6;
1364 1.1.2.1 itojun
1365 1.1.2.1 itojun bzero(&sin6, sizeof(sin6));
1366 1.1.2.1 itojun sin6.sin6_family = AF_INET6;
1367 1.1.2.1 itojun sin6.sin6_len = sizeof(struct sockaddr_in6);
1368 1.1.2.1 itojun bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
1369 1.1.2.1 itojun rt = rtalloc1((struct sockaddr *)&sin6, 0
1370 1.1.2.1 itojun #ifdef __FreeBSD__
1371 1.1.2.1 itojun , 0UL
1372 1.1.2.1 itojun #endif
1373 1.1.2.1 itojun );
1374 1.1.2.1 itojun if (rt) {
1375 1.1.2.1 itojun gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
1376 1.1.2.1 itojun if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
1377 1.1.2.1 itojun log(LOG_ERR,
1378 1.1.2.1 itojun "ICMP6 redirect sent from %s rejected; "
1379 1.1.2.1 itojun "ip src=%s, gw for %s=%s (must be same)\n",
1380 1.1.2.1 itojun ip6_sprintf(&src6), ip6_sprintf(&src6),
1381 1.1.2.1 itojun ip6_sprintf(&reddst6), ip6_sprintf(gw6));
1382 1.1.2.1 itojun RTFREE(rt);
1383 1.1.2.1 itojun return;
1384 1.1.2.1 itojun }
1385 1.1.2.1 itojun } else {
1386 1.1.2.1 itojun log(LOG_ERR,
1387 1.1.2.1 itojun "ICMP6 redirect sent from %s rejected; "
1388 1.1.2.1 itojun "no route found for redirect destination %s\n",
1389 1.1.2.1 itojun ip6_sprintf(&src6), ip6_sprintf(&reddst6));
1390 1.1.2.1 itojun return;
1391 1.1.2.1 itojun }
1392 1.1.2.1 itojun RTFREE(rt);
1393 1.1.2.1 itojun rt = NULL;
1394 1.1.2.1 itojun }
1395 1.1.2.1 itojun if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
1396 1.1.2.1 itojun log(LOG_ERR,
1397 1.1.2.1 itojun "ICMP6 redirect sent from %s rejected; "
1398 1.1.2.1 itojun "redirect destination=%s (must be unicast)\n",
1399 1.1.2.1 itojun ip6_sprintf(&src6), ip6_sprintf(&reddst6));
1400 1.1.2.1 itojun return;
1401 1.1.2.1 itojun }
1402 1.1.2.1 itojun
1403 1.1.2.1 itojun is_router = is_onlink = 0;
1404 1.1.2.1 itojun if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
1405 1.1.2.1 itojun is_router = 1; /* router case */
1406 1.1.2.1 itojun if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
1407 1.1.2.1 itojun is_onlink = 1; /* on-link destination case */
1408 1.1.2.1 itojun if (!is_router && !is_onlink) {
1409 1.1.2.1 itojun log(LOG_ERR,
1410 1.1.2.1 itojun "ICMP6 redirect sent from %s rejected; "
1411 1.1.2.1 itojun "redirect target=%s, destination=%s\n",
1412 1.1.2.1 itojun ip6_sprintf(&src6), ip6_sprintf(&redtgt6),
1413 1.1.2.1 itojun ip6_sprintf(&reddst6));
1414 1.1.2.1 itojun return;
1415 1.1.2.1 itojun }
1416 1.1.2.1 itojun /* validation passed */
1417 1.1.2.1 itojun
1418 1.1.2.1 itojun icmp6len -= sizeof(*nd_rd);
1419 1.1.2.1 itojun nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
1420 1.1.2.1 itojun if (nd6_options(&ndopts) < 0) {
1421 1.1.2.1 itojun log(LOG_INFO, "icmp6_redirect_input: "
1422 1.1.2.1 itojun "invalid ND option, rejected\n");
1423 1.1.2.1 itojun return;
1424 1.1.2.1 itojun }
1425 1.1.2.1 itojun
1426 1.1.2.1 itojun if (ndopts.nd_opts_tgt_lladdr) {
1427 1.1.2.1 itojun lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
1428 1.1.2.1 itojun lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
1429 1.1.2.1 itojun }
1430 1.1.2.1 itojun
1431 1.1.2.1 itojun if (ndopts.nd_opts_rh) {
1432 1.1.2.1 itojun redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
1433 1.1.2.1 itojun redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
1434 1.1.2.1 itojun }
1435 1.1.2.1 itojun
1436 1.1.2.1 itojun if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
1437 1.1.2.1 itojun log(LOG_INFO,
1438 1.1.2.1 itojun "icmp6_redirect_input: lladdrlen mismatch for %s "
1439 1.1.2.1 itojun "(if %d, icmp6 packet %d)\n",
1440 1.1.2.1 itojun ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2);
1441 1.1.2.1 itojun }
1442 1.1.2.1 itojun
1443 1.1.2.1 itojun /* RFC 2461 8.3 */
1444 1.1.2.1 itojun nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT);
1445 1.1.2.1 itojun
1446 1.1.2.1 itojun if (!is_onlink) { /* better router case. perform rtredirect. */
1447 1.1.2.1 itojun /* perform rtredirect */
1448 1.1.2.1 itojun struct sockaddr_in6 sdst;
1449 1.1.2.1 itojun struct sockaddr_in6 sgw;
1450 1.1.2.1 itojun struct sockaddr_in6 ssrc;
1451 1.1.2.1 itojun #ifdef __bsdi__
1452 1.1.2.1 itojun extern int icmp_redirtimeout; /*XXX*/
1453 1.1.2.1 itojun #endif
1454 1.1.2.1 itojun
1455 1.1.2.1 itojun bzero(&sdst, sizeof(sdst));
1456 1.1.2.1 itojun bzero(&sgw, sizeof(sgw));
1457 1.1.2.1 itojun bzero(&ssrc, sizeof(ssrc));
1458 1.1.2.1 itojun sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
1459 1.1.2.1 itojun sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
1460 1.1.2.1 itojun sizeof(struct sockaddr_in6);
1461 1.1.2.1 itojun bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
1462 1.1.2.1 itojun bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
1463 1.1.2.1 itojun bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
1464 1.1.2.1 itojun rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
1465 1.1.2.1 itojun (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
1466 1.1.2.1 itojun (struct sockaddr *)&ssrc,
1467 1.1.2.1 itojun #if defined(__FreeBSD__) || defined(__NetBSD__)
1468 1.1.2.1 itojun (struct rtentry **)NULL
1469 1.1.2.1 itojun #elif defined(__bsdi__)
1470 1.1.2.1 itojun icmp_redirtimeout /*XXX*/
1471 1.1.2.1 itojun #endif /*__FreeBSD__, __NetBSD__, __bsdi__*/
1472 1.1.2.1 itojun );
1473 1.1.2.1 itojun }
1474 1.1.2.1 itojun /* finally update cached route in each socket via pfctlinput */
1475 1.1.2.1 itojun {
1476 1.1.2.1 itojun struct sockaddr_in6 sdst;
1477 1.1.2.1 itojun
1478 1.1.2.1 itojun bzero(&sdst, sizeof(sdst));
1479 1.1.2.1 itojun sdst.sin6_family = AF_INET6;
1480 1.1.2.1 itojun sdst.sin6_len = sizeof(struct sockaddr_in6);
1481 1.1.2.1 itojun bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
1482 1.1.2.1 itojun pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
1483 1.1.2.1 itojun #ifdef IPSEC
1484 1.1.2.1 itojun key_sa_routechange((struct sockaddr *)&sdst);
1485 1.1.2.1 itojun #endif
1486 1.1.2.1 itojun }
1487 1.1.2.1 itojun }
1488 1.1.2.1 itojun
1489 1.1.2.1 itojun void
1490 1.1.2.1 itojun icmp6_redirect_output(m0, rt)
1491 1.1.2.1 itojun struct mbuf *m0;
1492 1.1.2.1 itojun struct rtentry *rt;
1493 1.1.2.1 itojun {
1494 1.1.2.1 itojun struct ifnet *ifp; /* my outgoing interface */
1495 1.1.2.1 itojun struct in6_addr *ifp_ll6;
1496 1.1.2.1 itojun struct in6_addr *router_ll6;
1497 1.1.2.1 itojun struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
1498 1.1.2.1 itojun struct mbuf *m = NULL; /* newly allocated one */
1499 1.1.2.1 itojun struct ip6_hdr *ip6; /* m as struct ip6_hdr */
1500 1.1.2.1 itojun struct nd_redirect *nd_rd;
1501 1.1.2.1 itojun size_t maxlen;
1502 1.1.2.1 itojun u_char *p;
1503 1.1.2.1 itojun
1504 1.1.2.1 itojun /* if we are not router, we don't send icmp6 redirect */
1505 1.1.2.1 itojun if (!ip6_forwarding || ip6_accept_rtadv)
1506 1.1.2.1 itojun goto fail;
1507 1.1.2.1 itojun
1508 1.1.2.1 itojun /* sanity check */
1509 1.1.2.1 itojun if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
1510 1.1.2.1 itojun goto fail;
1511 1.1.2.1 itojun sip6 = mtod(m0, struct ip6_hdr *);
1512 1.1.2.1 itojun if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
1513 1.1.2.1 itojun goto fail; /* what should we do here? */
1514 1.1.2.1 itojun
1515 1.1.2.1 itojun /* rate limit */
1516 1.1.2.1 itojun if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
1517 1.1.2.1 itojun goto fail;
1518 1.1.2.1 itojun
1519 1.1.2.1 itojun /*
1520 1.1.2.1 itojun * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
1521 1.1.2.1 itojun * we almost always ask for an mbuf cluster for simplicity.
1522 1.1.2.1 itojun * (MHLEN < IPV6_MMTU is almost always true)
1523 1.1.2.1 itojun */
1524 1.1.2.1 itojun MGETHDR(m, M_DONTWAIT, MT_HEADER);
1525 1.1.2.1 itojun if (!m)
1526 1.1.2.1 itojun goto fail;
1527 1.1.2.1 itojun if (MHLEN < IPV6_MMTU)
1528 1.1.2.1 itojun MCLGET(m, M_DONTWAIT);
1529 1.1.2.1 itojun maxlen = (m->m_flags & M_EXT) ? MCLBYTES : MHLEN;
1530 1.1.2.1 itojun maxlen = min(IPV6_MMTU, maxlen);
1531 1.1.2.1 itojun /* just for safety */
1532 1.1.2.1 itojun if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr))
1533 1.1.2.1 itojun goto fail;
1534 1.1.2.1 itojun
1535 1.1.2.1 itojun {
1536 1.1.2.1 itojun /* get ip6 linklocal address for ifp(my outgoing interface). */
1537 1.1.2.1 itojun struct in6_ifaddr *ia = in6ifa_ifpforlinklocal(ifp);
1538 1.1.2.1 itojun if (ia == NULL)
1539 1.1.2.1 itojun goto fail;
1540 1.1.2.1 itojun ifp_ll6 = &ia->ia_addr.sin6_addr;
1541 1.1.2.1 itojun }
1542 1.1.2.1 itojun
1543 1.1.2.1 itojun /* get ip6 linklocal address for the router. */
1544 1.1.2.1 itojun if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
1545 1.1.2.1 itojun struct sockaddr_in6 *sin6;
1546 1.1.2.1 itojun sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
1547 1.1.2.1 itojun router_ll6 = &sin6->sin6_addr;
1548 1.1.2.1 itojun if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
1549 1.1.2.1 itojun router_ll6 = (struct in6_addr *)NULL;
1550 1.1.2.1 itojun } else
1551 1.1.2.1 itojun router_ll6 = (struct in6_addr *)NULL;
1552 1.1.2.1 itojun
1553 1.1.2.1 itojun /* ip6 */
1554 1.1.2.1 itojun ip6 = mtod(m, struct ip6_hdr *);
1555 1.1.2.1 itojun ip6->ip6_flow = 0;
1556 1.1.2.1 itojun ip6->ip6_vfc = IPV6_VERSION;
1557 1.1.2.1 itojun /* ip6->ip6_plen will be set later */
1558 1.1.2.1 itojun ip6->ip6_nxt = IPPROTO_ICMPV6;
1559 1.1.2.1 itojun ip6->ip6_hlim = 255;
1560 1.1.2.1 itojun /* ip6->ip6_src must be linklocal addr for my outgoing if. */
1561 1.1.2.1 itojun bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
1562 1.1.2.1 itojun bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
1563 1.1.2.1 itojun
1564 1.1.2.1 itojun /* ND Redirect */
1565 1.1.2.1 itojun nd_rd = (struct nd_redirect *)(ip6 + 1);
1566 1.1.2.1 itojun nd_rd->nd_rd_type = ND_REDIRECT;
1567 1.1.2.1 itojun nd_rd->nd_rd_code = 0;
1568 1.1.2.1 itojun nd_rd->nd_rd_reserved = 0;
1569 1.1.2.1 itojun if (rt->rt_flags & RTF_GATEWAY) {
1570 1.1.2.1 itojun /*
1571 1.1.2.1 itojun * nd_rd->nd_rd_target must be a link-local address in
1572 1.1.2.1 itojun * better router cases.
1573 1.1.2.1 itojun */
1574 1.1.2.1 itojun if (!router_ll6)
1575 1.1.2.1 itojun goto fail;
1576 1.1.2.1 itojun bcopy(router_ll6, &nd_rd->nd_rd_target,
1577 1.1.2.1 itojun sizeof(nd_rd->nd_rd_target));
1578 1.1.2.1 itojun bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
1579 1.1.2.1 itojun sizeof(nd_rd->nd_rd_dst));
1580 1.1.2.1 itojun } else {
1581 1.1.2.1 itojun /* make sure redtgt == reddst */
1582 1.1.2.1 itojun bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
1583 1.1.2.1 itojun sizeof(nd_rd->nd_rd_target));
1584 1.1.2.1 itojun bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
1585 1.1.2.1 itojun sizeof(nd_rd->nd_rd_dst));
1586 1.1.2.1 itojun }
1587 1.1.2.1 itojun
1588 1.1.2.1 itojun p = (u_char *)(nd_rd + 1);
1589 1.1.2.1 itojun
1590 1.1.2.1 itojun if (!router_ll6)
1591 1.1.2.1 itojun goto nolladdropt;
1592 1.1.2.1 itojun
1593 1.1.2.1 itojun {
1594 1.1.2.1 itojun /* target lladdr option */
1595 1.1.2.1 itojun struct rtentry *rt_router = NULL;
1596 1.1.2.1 itojun int len;
1597 1.1.2.1 itojun struct sockaddr_dl *sdl;
1598 1.1.2.1 itojun struct nd_opt_hdr *nd_opt;
1599 1.1.2.1 itojun char *lladdr;
1600 1.1.2.1 itojun
1601 1.1.2.1 itojun rt_router = nd6_lookup(router_ll6, 0, ifp);
1602 1.1.2.1 itojun if (!rt_router)
1603 1.1.2.1 itojun goto nolladdropt;
1604 1.1.2.1 itojun if (!(rt_router->rt_flags & RTF_GATEWAY)
1605 1.1.2.1 itojun && (rt_router->rt_flags & RTF_LLINFO)
1606 1.1.2.1 itojun && (rt_router->rt_gateway->sa_family == AF_LINK)
1607 1.1.2.1 itojun && (sdl = (struct sockaddr_dl *)rt_router->rt_gateway)) {
1608 1.1.2.1 itojun nd_opt = (struct nd_opt_hdr *)p;
1609 1.1.2.1 itojun nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1610 1.1.2.1 itojun len = 2 + ifp->if_addrlen;
1611 1.1.2.1 itojun len = (len + 7) & ~7; /*round by 8*/
1612 1.1.2.1 itojun nd_opt->nd_opt_len = len >> 3;
1613 1.1.2.1 itojun p += len;
1614 1.1.2.1 itojun lladdr = (char *)(nd_opt + 1);
1615 1.1.2.1 itojun bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
1616 1.1.2.1 itojun }
1617 1.1.2.1 itojun }
1618 1.1.2.1 itojun nolladdropt:;
1619 1.1.2.1 itojun
1620 1.1.2.1 itojun m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
1621 1.1.2.1 itojun
1622 1.1.2.1 itojun /* just to be safe */
1623 1.1.2.1 itojun if (m0->m_flags & M_DECRYPTED)
1624 1.1.2.1 itojun goto noredhdropt;
1625 1.1.2.1 itojun
1626 1.1.2.1 itojun {
1627 1.1.2.1 itojun /* redirected header option */
1628 1.1.2.1 itojun int len;
1629 1.1.2.1 itojun struct nd_opt_rd_hdr *nd_opt_rh;
1630 1.1.2.1 itojun
1631 1.1.2.1 itojun /*
1632 1.1.2.1 itojun * compute the maximum size for icmp6 redirect header option.
1633 1.1.2.1 itojun * XXX room for auth header?
1634 1.1.2.1 itojun */
1635 1.1.2.1 itojun len = maxlen - (p - (u_char *)ip6);
1636 1.1.2.1 itojun len &= ~7;
1637 1.1.2.1 itojun
1638 1.1.2.1 itojun /* This is just for simplicity. */
1639 1.1.2.1 itojun if (m0->m_pkthdr.len != m0->m_len) {
1640 1.1.2.1 itojun if (m0->m_next) {
1641 1.1.2.1 itojun m_freem(m0->m_next);
1642 1.1.2.1 itojun m0->m_next = NULL;
1643 1.1.2.1 itojun }
1644 1.1.2.1 itojun m0->m_pkthdr.len = m0->m_len;
1645 1.1.2.1 itojun }
1646 1.1.2.1 itojun
1647 1.1.2.1 itojun /*
1648 1.1.2.1 itojun * Redirected header option spec (RFC2461 4.6.3) talks nothing
1649 1.1.2.1 itojun * about padding/trancate rule for the original IP packet.
1650 1.1.2.1 itojun * From the discussion on IPv6imp in Feb 1999, the consensus was:
1651 1.1.2.1 itojun * - "attach as much as possible" is the goal
1652 1.1.2.1 itojun * - pad if not aligned (original size can be guessed by original
1653 1.1.2.1 itojun * ip6 header)
1654 1.1.2.1 itojun * Following code adds the padding if it is simple enough,
1655 1.1.2.1 itojun * and truncates if not.
1656 1.1.2.1 itojun */
1657 1.1.2.1 itojun if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
1658 1.1.2.1 itojun panic("assumption failed in %s:%d\n", __FILE__, __LINE__);
1659 1.1.2.1 itojun
1660 1.1.2.1 itojun if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
1661 1.1.2.1 itojun /* not enough room, truncate */
1662 1.1.2.1 itojun m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
1663 1.1.2.1 itojun } else {
1664 1.1.2.1 itojun /* enough room, pad or truncate */
1665 1.1.2.1 itojun size_t extra;
1666 1.1.2.1 itojun
1667 1.1.2.1 itojun extra = m0->m_pkthdr.len % 8;
1668 1.1.2.1 itojun if (extra) {
1669 1.1.2.1 itojun /* pad if easy enough, truncate if not */
1670 1.1.2.1 itojun if (8 - extra <= M_TRAILINGSPACE(m0)) {
1671 1.1.2.1 itojun /* pad */
1672 1.1.2.1 itojun m0->m_len += (8 - extra);
1673 1.1.2.1 itojun m0->m_pkthdr.len += (8 - extra);
1674 1.1.2.1 itojun } else {
1675 1.1.2.1 itojun /* truncate */
1676 1.1.2.1 itojun m0->m_pkthdr.len -= extra;
1677 1.1.2.1 itojun m0->m_len -= extra;
1678 1.1.2.1 itojun }
1679 1.1.2.1 itojun }
1680 1.1.2.1 itojun len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
1681 1.1.2.1 itojun m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
1682 1.1.2.1 itojun }
1683 1.1.2.1 itojun
1684 1.1.2.1 itojun nd_opt_rh = (struct nd_opt_rd_hdr *)p;
1685 1.1.2.1 itojun bzero(nd_opt_rh, sizeof(*nd_opt_rh));
1686 1.1.2.1 itojun nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
1687 1.1.2.1 itojun nd_opt_rh->nd_opt_rh_len = len >> 3;
1688 1.1.2.1 itojun p += sizeof(*nd_opt_rh);
1689 1.1.2.1 itojun m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
1690 1.1.2.1 itojun
1691 1.1.2.1 itojun /* connect m0 to m */
1692 1.1.2.1 itojun m->m_next = m0;
1693 1.1.2.1 itojun m->m_pkthdr.len = m->m_len + m0->m_len;
1694 1.1.2.1 itojun }
1695 1.1.2.1 itojun noredhdropt:;
1696 1.1.2.1 itojun
1697 1.1.2.1 itojun if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src))
1698 1.1.2.1 itojun sip6->ip6_src.s6_addr16[1] = 0;
1699 1.1.2.1 itojun if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst))
1700 1.1.2.1 itojun sip6->ip6_dst.s6_addr16[1] = 0;
1701 1.1.2.1 itojun #if 0
1702 1.1.2.1 itojun if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
1703 1.1.2.1 itojun ip6->ip6_src.s6_addr16[1] = 0;
1704 1.1.2.1 itojun if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
1705 1.1.2.1 itojun ip6->ip6_dst.s6_addr16[1] = 0;
1706 1.1.2.1 itojun #endif
1707 1.1.2.1 itojun if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target))
1708 1.1.2.1 itojun nd_rd->nd_rd_target.s6_addr16[1] = 0;
1709 1.1.2.1 itojun if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst))
1710 1.1.2.1 itojun nd_rd->nd_rd_dst.s6_addr16[1] = 0;
1711 1.1.2.1 itojun
1712 1.1.2.1 itojun ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
1713 1.1.2.1 itojun
1714 1.1.2.1 itojun nd_rd->nd_rd_cksum = 0;
1715 1.1.2.1 itojun nd_rd->nd_rd_cksum
1716 1.1.2.1 itojun = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
1717 1.1.2.1 itojun
1718 1.1.2.1 itojun /* send the packet to outside... */
1719 1.1.2.1 itojun #ifdef IPSEC
1720 1.1.2.1 itojun m->m_pkthdr.rcvif = NULL;
1721 1.1.2.1 itojun #endif /*IPSEC*/
1722 1.1.2.1 itojun ip6_output(m, NULL, NULL, 0, NULL);
1723 1.1.2.1 itojun icmp6stat.icp6s_outhist[ND_REDIRECT]++;
1724 1.1.2.1 itojun
1725 1.1.2.1 itojun return;
1726 1.1.2.1 itojun
1727 1.1.2.1 itojun fail:
1728 1.1.2.1 itojun if (m)
1729 1.1.2.1 itojun m_freem(m);
1730 1.1.2.1 itojun if (m0)
1731 1.1.2.1 itojun m_freem(m0);
1732 1.1.2.1 itojun }
1733 1.1.2.1 itojun
1734 1.1.2.1 itojun /*
1735 1.1.2.1 itojun * ICMPv6 socket option processing.
1736 1.1.2.1 itojun */
1737 1.1.2.1 itojun int
1738 1.1.2.1 itojun icmp6_ctloutput(op, so, level, optname, mp)
1739 1.1.2.1 itojun int op;
1740 1.1.2.1 itojun struct socket *so;
1741 1.1.2.1 itojun int level, optname;
1742 1.1.2.1 itojun struct mbuf **mp;
1743 1.1.2.1 itojun {
1744 1.1.2.1 itojun register struct in6pcb *in6p = sotoin6pcb(so);
1745 1.1.2.1 itojun register struct mbuf *m = *mp;
1746 1.1.2.1 itojun int error = 0;
1747 1.1.2.1 itojun
1748 1.1.2.1 itojun if (level != IPPROTO_ICMPV6) {
1749 1.1.2.1 itojun error = EINVAL;
1750 1.1.2.1 itojun if (op == PRCO_SETOPT && m)
1751 1.1.2.1 itojun (void)m_free(m);
1752 1.1.2.1 itojun } else switch(op) {
1753 1.1.2.1 itojun case PRCO_SETOPT:
1754 1.1.2.1 itojun switch (optname) {
1755 1.1.2.1 itojun case ICMP6_FILTER:
1756 1.1.2.1 itojun {
1757 1.1.2.1 itojun struct icmp6_filter *p;
1758 1.1.2.1 itojun
1759 1.1.2.1 itojun p = mtod(m, struct icmp6_filter *);
1760 1.1.2.1 itojun if (!p || !in6p->in6p_icmp6filt) {
1761 1.1.2.1 itojun error = EINVAL;
1762 1.1.2.1 itojun break;
1763 1.1.2.1 itojun }
1764 1.1.2.1 itojun bcopy(p, in6p->in6p_icmp6filt,
1765 1.1.2.1 itojun sizeof(struct icmp6_filter));
1766 1.1.2.1 itojun error = 0;
1767 1.1.2.1 itojun break;
1768 1.1.2.1 itojun }
1769 1.1.2.1 itojun
1770 1.1.2.1 itojun default:
1771 1.1.2.1 itojun error = ENOPROTOOPT;
1772 1.1.2.1 itojun break;
1773 1.1.2.1 itojun }
1774 1.1.2.1 itojun if (m)
1775 1.1.2.1 itojun (void)m_free(m);
1776 1.1.2.1 itojun break;
1777 1.1.2.1 itojun
1778 1.1.2.1 itojun case PRCO_GETOPT:
1779 1.1.2.1 itojun switch (optname) {
1780 1.1.2.1 itojun case ICMP6_FILTER:
1781 1.1.2.1 itojun {
1782 1.1.2.1 itojun struct icmp6_filter *p;
1783 1.1.2.1 itojun
1784 1.1.2.1 itojun p = mtod(m, struct icmp6_filter *);
1785 1.1.2.1 itojun if (!p || !in6p->in6p_icmp6filt) {
1786 1.1.2.1 itojun error = EINVAL;
1787 1.1.2.1 itojun break;
1788 1.1.2.1 itojun }
1789 1.1.2.1 itojun bcopy(in6p->in6p_icmp6filt, p,
1790 1.1.2.1 itojun sizeof(struct icmp6_filter));
1791 1.1.2.1 itojun error = 0;
1792 1.1.2.1 itojun break;
1793 1.1.2.1 itojun }
1794 1.1.2.1 itojun
1795 1.1.2.1 itojun default:
1796 1.1.2.1 itojun error = ENOPROTOOPT;
1797 1.1.2.1 itojun break;
1798 1.1.2.1 itojun }
1799 1.1.2.1 itojun break;
1800 1.1.2.1 itojun }
1801 1.1.2.1 itojun
1802 1.1.2.1 itojun return(error);
1803 1.1.2.1 itojun }
1804 1.1.2.1 itojun
1805 1.1.2.1 itojun /*
1806 1.1.2.1 itojun * Perform rate limit check.
1807 1.1.2.1 itojun * Returns 0 if it is okay to send the icmp6 packet.
1808 1.1.2.1 itojun * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
1809 1.1.2.1 itojun * limitation.
1810 1.1.2.1 itojun *
1811 1.1.2.1 itojun * XXX per-destination/type check necessary?
1812 1.1.2.1 itojun */
1813 1.1.2.1 itojun static int
1814 1.1.2.1 itojun icmp6_ratelimit(dst, type, code)
1815 1.1.2.1 itojun const struct in6_addr *dst; /* not used at this moment */
1816 1.1.2.1 itojun const int type; /* not used at this moment */
1817 1.1.2.1 itojun const int code; /* not used at this moment */
1818 1.1.2.1 itojun {
1819 1.1.2.1 itojun struct timeval tp;
1820 1.1.2.1 itojun long sec_diff, usec_diff;
1821 1.1.2.1 itojun
1822 1.1.2.1 itojun /* If we are not doing rate limitation, it is always okay to send */
1823 1.1.2.1 itojun if (!icmp6errratelim)
1824 1.1.2.1 itojun return 0;
1825 1.1.2.1 itojun
1826 1.1.2.1 itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1827 1.1.2.1 itojun microtime(&tp);
1828 1.1.2.1 itojun tp.tv_sec = time_second;
1829 1.1.2.1 itojun #else
1830 1.1.2.1 itojun tp = time;
1831 1.1.2.1 itojun #endif
1832 1.1.2.1 itojun if (tp.tv_sec < icmp6_nextsend.tv_sec
1833 1.1.2.1 itojun || (tp.tv_sec == icmp6_nextsend.tv_sec
1834 1.1.2.1 itojun && tp.tv_usec < icmp6_nextsend.tv_usec)) {
1835 1.1.2.1 itojun /* The packet is subject to rate limit */
1836 1.1.2.1 itojun return 1;
1837 1.1.2.1 itojun }
1838 1.1.2.1 itojun sec_diff = icmp6errratelim / 1000000;
1839 1.1.2.1 itojun usec_diff = icmp6errratelim % 1000000;
1840 1.1.2.1 itojun icmp6_nextsend.tv_sec = tp.tv_sec + sec_diff;
1841 1.1.2.1 itojun if ((tp.tv_usec = tp.tv_usec + usec_diff) >= 1000000) {
1842 1.1.2.1 itojun icmp6_nextsend.tv_sec++;
1843 1.1.2.1 itojun icmp6_nextsend.tv_usec -= 1000000;
1844 1.1.2.1 itojun }
1845 1.1.2.1 itojun
1846 1.1.2.1 itojun /* it is okay to send this */
1847 1.1.2.1 itojun return 0;
1848 1.1.2.1 itojun }
1849 1.1.2.1 itojun
1850 1.1.2.1 itojun #ifdef __bsdi__
1851 1.1.2.1 itojun void
1852 1.1.2.1 itojun icmp6_mtuexpire(rt, rtt)
1853 1.1.2.1 itojun struct rtentry *rt;
1854 1.1.2.1 itojun struct rttimer *rtt;
1855 1.1.2.1 itojun {
1856 1.1.2.1 itojun rt->rt_flags |= RTF_PROBEMTU;
1857 1.1.2.1 itojun Free(rtt);
1858 1.1.2.1 itojun }
1859 1.1.2.1 itojun
1860 1.1.2.1 itojun int *icmp6_sysvars[] = ICMPV6CTL_VARS;
1861 1.1.2.1 itojun
1862 1.1.2.1 itojun int
1863 1.1.2.1 itojun icmp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1864 1.1.2.1 itojun int *name;
1865 1.1.2.1 itojun u_int namelen;
1866 1.1.2.1 itojun void *oldp;
1867 1.1.2.1 itojun size_t *oldlenp;
1868 1.1.2.1 itojun void *newp;
1869 1.1.2.1 itojun size_t newlen;
1870 1.1.2.1 itojun {
1871 1.1.2.1 itojun if (name[0] >= ICMPV6CTL_MAXID)
1872 1.1.2.1 itojun return (EOPNOTSUPP);
1873 1.1.2.1 itojun switch (name[0]) {
1874 1.1.2.1 itojun #if 0
1875 1.1.2.1 itojun ICMPV6CTL_ND6_PRUNE:
1876 1.1.2.1 itojun ICMPV6CTL_ND6_DELAY:
1877 1.1.2.1 itojun ICMPV6CTL_ND6_UMAXTRIES:
1878 1.1.2.1 itojun ICMPV6CTL_ND6_MMAXTRIES:
1879 1.1.2.1 itojun ICMPV6CTL_ND6_USELOOPBACK:
1880 1.1.2.1 itojun ICMPV6CTL_ND6_PROXYALL:
1881 1.1.2.1 itojun /* need to check the value. */
1882 1.1.2.1 itojun #endif
1883 1.1.2.1 itojun case ICMPV6CTL_STATS:
1884 1.1.2.1 itojun return sysctl_rdtrunc(oldp, oldlenp, newp, &icmp6stat,
1885 1.1.2.1 itojun sizeof(icmp6stat));
1886 1.1.2.1 itojun
1887 1.1.2.1 itojun default:
1888 1.1.2.1 itojun return (sysctl_int_arr(icmp6_sysvars, name, namelen,
1889 1.1.2.1 itojun oldp, oldlenp, newp, newlen));
1890 1.1.2.1 itojun }
1891 1.1.2.1 itojun }
1892 1.1.2.1 itojun #endif /*__bsdi__*/
1893 1.1.2.1 itojun
1894 1.1.2.1 itojun #ifdef __NetBSD__
1895 1.1.2.1 itojun #include <vm/vm.h>
1896 1.1.2.1 itojun #include <sys/sysctl.h>
1897 1.1.2.1 itojun int
1898 1.1.2.1 itojun icmp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1899 1.1.2.1 itojun int *name;
1900 1.1.2.1 itojun u_int namelen;
1901 1.1.2.1 itojun void *oldp;
1902 1.1.2.1 itojun size_t *oldlenp;
1903 1.1.2.1 itojun void *newp;
1904 1.1.2.1 itojun size_t newlen;
1905 1.1.2.1 itojun {
1906 1.1.2.1 itojun
1907 1.1.2.1 itojun /* All sysctl names at this level are terminal. */
1908 1.1.2.1 itojun if (namelen != 1)
1909 1.1.2.1 itojun return ENOTDIR;
1910 1.1.2.1 itojun
1911 1.1.2.1 itojun switch (name[0]) {
1912 1.1.2.1 itojun
1913 1.1.2.1 itojun case ICMPV6CTL_REDIRACCEPT:
1914 1.1.2.1 itojun return sysctl_int(oldp, oldlenp, newp, newlen,
1915 1.1.2.1 itojun &icmp6_rediraccept);
1916 1.1.2.1 itojun case ICMPV6CTL_REDIRTIMEOUT:
1917 1.1.2.1 itojun return sysctl_int(oldp, oldlenp, newp, newlen,
1918 1.1.2.1 itojun &icmp6_redirtimeout);
1919 1.1.2.1 itojun case ICMPV6CTL_STATS:
1920 1.1.2.1 itojun return sysctl_rdstruct(oldp, oldlenp, newp,
1921 1.1.2.1 itojun &icmp6stat, sizeof(icmp6stat));
1922 1.1.2.1 itojun case ICMPV6CTL_ERRRATELIMIT:
1923 1.1.2.1 itojun return sysctl_int(oldp, oldlenp, newp, newlen,
1924 1.1.2.1 itojun &icmp6errratelim);
1925 1.1.2.1 itojun case ICMPV6CTL_ND6_PRUNE:
1926 1.1.2.1 itojun return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_prune);
1927 1.1.2.1 itojun case ICMPV6CTL_ND6_DELAY:
1928 1.1.2.1 itojun return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_delay);
1929 1.1.2.1 itojun case ICMPV6CTL_ND6_UMAXTRIES:
1930 1.1.2.1 itojun return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_umaxtries);
1931 1.1.2.1 itojun case ICMPV6CTL_ND6_MMAXTRIES:
1932 1.1.2.1 itojun return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_mmaxtries);
1933 1.1.2.1 itojun case ICMPV6CTL_ND6_USELOOPBACK:
1934 1.1.2.1 itojun return sysctl_int(oldp, oldlenp, newp, newlen,
1935 1.1.2.1 itojun &nd6_useloopback);
1936 1.1.2.1 itojun case ICMPV6CTL_ND6_PROXYALL:
1937 1.1.2.1 itojun return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_proxyall);
1938 1.1.2.1 itojun default:
1939 1.1.2.1 itojun return ENOPROTOOPT;
1940 1.1.2.1 itojun }
1941 1.1.2.1 itojun /* NOTREACHED */
1942 1.1.2.1 itojun }
1943 1.1.2.1 itojun #endif /* __NetBSD__ */
1944