OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [bsd_tcpip/] [current/] [src/] [sys/] [netinet6/] [icmp6.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      src/sys/netinet6/icmp6.c
4
//
5
//==========================================================================
6
// ####BSDCOPYRIGHTBEGIN####                                    
7
// -------------------------------------------                  
8
// This file is part of eCos, the Embedded Configurable Operating System.
9
//
10
// Portions of this software may have been derived from FreeBSD 
11
// or other sources, and if so are covered by the appropriate copyright
12
// and license included herein.                                 
13
//
14
// Portions created by the Free Software Foundation are         
15
// Copyright (C) 2002 Free Software Foundation, Inc.            
16
// -------------------------------------------                  
17
// ####BSDCOPYRIGHTEND####                                      
18
//==========================================================================
19
 
20
/*      $KAME: icmp6.c,v 1.269 2001/12/18 02:19:16 jinmei Exp $ */
21
 
22
/*
23
 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
24
 * All rights reserved.
25
 *
26
 * Redistribution and use in source and binary forms, with or without
27
 * modification, are permitted provided that the following conditions
28
 * are met:
29
 * 1. Redistributions of source code must retain the above copyright
30
 *    notice, this list of conditions and the following disclaimer.
31
 * 2. Redistributions in binary form must reproduce the above copyright
32
 *    notice, this list of conditions and the following disclaimer in the
33
 *    documentation and/or other materials provided with the distribution.
34
 * 3. Neither the name of the project nor the names of its contributors
35
 *    may be used to endorse or promote products derived from this software
36
 *    without specific prior written permission.
37
 *
38
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
39
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
42
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48
 * SUCH DAMAGE.
49
 */
50
 
51
/*
52
 * Copyright (c) 1982, 1986, 1988, 1993
53
 *      The Regents of the University of California.  All rights reserved.
54
 *
55
 * Redistribution and use in source and binary forms, with or without
56
 * modification, are permitted provided that the following conditions
57
 * are met:
58
 * 1. Redistributions of source code must retain the above copyright
59
 *    notice, this list of conditions and the following disclaimer.
60
 * 2. Redistributions in binary form must reproduce the above copyright
61
 *    notice, this list of conditions and the following disclaimer in the
62
 *    documentation and/or other materials provided with the distribution.
63
 * 3. All advertising materials mentioning features or use of this software
64
 *    must display the following acknowledgement:
65
 *      This product includes software developed by the University of
66
 *      California, Berkeley and its contributors.
67
 * 4. Neither the name of the University nor the names of its contributors
68
 *    may be used to endorse or promote products derived from this software
69
 *    without specific prior written permission.
70
 *
71
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
81
 * SUCH DAMAGE.
82
 *
83
 *      @(#)ip_icmp.c   8.2 (Berkeley) 1/4/94
84
 */
85
 
86
#include <sys/param.h>
87
#include <sys/malloc.h>
88
#include <sys/mbuf.h>
89
#include <sys/protosw.h>
90
#include <sys/socket.h>
91
#include <sys/socketvar.h>
92
#include <sys/domain.h>
93
 
94
#include <net/if.h>
95
#include <net/route.h>
96
#include <net/if_dl.h>
97
#include <net/if_types.h>
98
 
99
#include <netinet/in.h>
100
#include <netinet/in_var.h>
101
#if defined(__OpenBSD__) || (defined(__bsdi__) && _BSDI_VERSION >= 199802)
102
#include <netinet/in_systm.h>
103
#include <netinet/ip.h>
104
#endif
105
#include <netinet/ip6.h>
106
#include <netinet6/ip6_var.h>
107
#include <netinet/icmp6.h>
108
#include <netinet6/mld6_var.h>
109
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
110
#include <netinet/in_pcb.h>
111
#include <netinet6/in6_pcb.h>
112
#elif defined(__OpenBSD__) || (defined(__bsdi__) && _BSDI_VERSION >= 199802)
113
#include <netinet/in_pcb.h>
114
#else
115
#include <netinet6/in6_pcb.h>
116
#endif
117
#include <netinet6/nd6.h>
118
#include <netinet6/in6_ifattach.h>
119
#include <netinet6/ip6protosw.h>
120
 
121
#ifdef __OpenBSD__ /* KAME IPSEC */
122
#undef IPSEC
123
#endif
124
 
125
#ifdef IPSEC
126
#include <netinet6/ipsec.h>
127
#include <netkey/key.h>
128
#endif
129
 
130
#ifdef MIP6
131
#include <netinet6/mip6.h>
132
#endif
133
 
134
#ifdef HAVE_NRL_INPCB
135
/* inpcb members */
136
#define in6pcb          inpcb
137
#define in6p_laddr      inp_laddr6
138
#define in6p_faddr      inp_faddr6
139
#define in6p_icmp6filt  inp_icmp6filt
140
#define in6p_route      inp_route
141
#define in6p_socket     inp_socket
142
#define in6p_flags      inp_flags
143
#define in6p_moptions   inp_moptions6
144
#define in6p_outputopts inp_outputopts6
145
#define in6p_ip6        inp_ipv6
146
#define in6p_flowinfo   inp_flowinfo
147
#define in6p_sp         inp_sp
148
#define in6p_next       inp_next
149
#define in6p_prev       inp_prev
150
 
151
/*
152
 * for KAME src sync over BSD*'s.  XXX: FreeBSD (>=3) are VERY different from
153
 * others...
154
 */
155
#define in6p_ip6_nxt    inp_ipv6.ip6_nxt
156
#endif
157
 
158
extern struct domain inet6domain;
159
extern struct ip6protosw inet6sw[];
160
extern u_char ip6_protox[];
161
 
162
struct icmp6stat icmp6stat;
163
 
164
#if defined (__OpenBSD__)
165
extern struct inpcbtable rawin6pcbtable;
166
#elif !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
167
extern struct in6pcb rawin6pcb;
168
#else
169
extern struct inpcbhead ripcb;
170
#endif
171
extern int icmp6errppslim;
172
static int icmp6errpps_count = 0;
173
static struct timeval icmp6errppslim_last;
174
extern int icmp6_nodeinfo;
175
#if defined(__NetBSD__) || defined(__OpenBSD__)
176
/*
177
 * List of callbacks to notify when Path MTU changes are made.
178
 */
179
struct icmp6_mtudisc_callback {
180
        LIST_ENTRY(icmp6_mtudisc_callback) mc_list;
181
        void (*mc_func) __P((struct in6_addr *));
182
};
183
 
184
LIST_HEAD(, icmp6_mtudisc_callback) icmp6_mtudisc_callbacks =
185
    LIST_HEAD_INITIALIZER(&icmp6_mtudisc_callbacks);
186
 
187
static struct rttimer_queue *icmp6_mtudisc_timeout_q = NULL;
188
extern int pmtu_expire;
189
 
190
/* XXX do these values make any sense? */
191
static int icmp6_mtudisc_hiwat = 1280;
192
static int icmp6_mtudisc_lowat = 256;
193
 
194
/*
195
 * keep track of # of redirect routes.
196
 */
197
static struct rttimer_queue *icmp6_redirect_timeout_q = NULL;
198
 
199
/* XXX do these values make any sense? */
200
static int icmp6_redirect_hiwat = 1280;
201
static int icmp6_redirect_lowat = 1024;
202
#endif
203
 
204
static void icmp6_errcount __P((struct icmp6errstat *, int, int));
205
static int icmp6_rip6_input __P((struct mbuf **, int));
206
static int icmp6_ratelimit __P((const struct in6_addr *, const int, const int));
207
static const char *icmp6_redirect_diag __P((struct in6_addr *,
208
        struct in6_addr *, struct in6_addr *));
209
#ifndef HAVE_PPSRATECHECK
210
static int ppsratecheck __P((struct timeval *, int *, int));
211
#endif
212
static struct mbuf *ni6_input __P((struct mbuf *, int));
213
static struct mbuf *ni6_nametodns __P((const char *, int, int));
214
static int ni6_dnsmatch __P((const char *, int, const char *, int));
215
static int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *,
216
                          struct ifnet **, char *));
217
static int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
218
                                struct ifnet *, int));
219
static int icmp6_notify_error __P((struct mbuf *, int, int, int));
220
static int icmp6_recover_src __P((struct mbuf *));
221
#if defined(__NetBSD__) || defined(__OpenBSD__)
222
static struct rtentry *icmp6_mtudisc_clone __P((struct sockaddr *));
223
static void icmp6_mtudisc_timeout __P((struct rtentry *, struct rttimer *));
224
static void icmp6_redirect_timeout __P((struct rtentry *, struct rttimer *));
225
#endif
226
 
227
void
228
icmp6_init()
229
{
230
        mld6_init();
231
#if defined(__NetBSD__) || defined(__OpenBSD__)
232
        icmp6_mtudisc_timeout_q = rt_timer_queue_create(pmtu_expire);
233
        icmp6_redirect_timeout_q = rt_timer_queue_create(icmp6_redirtimeout);
234
#endif
235
}
236
 
237
static void
238
icmp6_errcount(stat, type, code)
239
        struct icmp6errstat *stat;
240
        int type, code;
241
{
242
        switch (type) {
243
        case ICMP6_DST_UNREACH:
244
                switch (code) {
245
                case ICMP6_DST_UNREACH_NOROUTE:
246
                        stat->icp6errs_dst_unreach_noroute++;
247
                        return;
248
                case ICMP6_DST_UNREACH_ADMIN:
249
                        stat->icp6errs_dst_unreach_admin++;
250
                        return;
251
                case ICMP6_DST_UNREACH_BEYONDSCOPE:
252
                        stat->icp6errs_dst_unreach_beyondscope++;
253
                        return;
254
                case ICMP6_DST_UNREACH_ADDR:
255
                        stat->icp6errs_dst_unreach_addr++;
256
                        return;
257
                case ICMP6_DST_UNREACH_NOPORT:
258
                        stat->icp6errs_dst_unreach_noport++;
259
                        return;
260
                }
261
                break;
262
        case ICMP6_PACKET_TOO_BIG:
263
                stat->icp6errs_packet_too_big++;
264
                return;
265
        case ICMP6_TIME_EXCEEDED:
266
                switch (code) {
267
                case ICMP6_TIME_EXCEED_TRANSIT:
268
                        stat->icp6errs_time_exceed_transit++;
269
                        return;
270
                case ICMP6_TIME_EXCEED_REASSEMBLY:
271
                        stat->icp6errs_time_exceed_reassembly++;
272
                        return;
273
                }
274
                break;
275
        case ICMP6_PARAM_PROB:
276
                switch (code) {
277
                case ICMP6_PARAMPROB_HEADER:
278
                        stat->icp6errs_paramprob_header++;
279
                        return;
280
                case ICMP6_PARAMPROB_NEXTHEADER:
281
                        stat->icp6errs_paramprob_nextheader++;
282
                        return;
283
                case ICMP6_PARAMPROB_OPTION:
284
                        stat->icp6errs_paramprob_option++;
285
                        return;
286
                }
287
                break;
288
        case ND_REDIRECT:
289
                stat->icp6errs_redirect++;
290
                return;
291
        }
292
        stat->icp6errs_unknown++;
293
}
294
 
295
#if defined(__NetBSD__) || defined(__OpenBSD__)
296
/*
297
 * Register a Path MTU Discovery callback.
298
 */
299
void
300
icmp6_mtudisc_callback_register(func)
301
        void (*func) __P((struct in6_addr *));
302
{
303
        struct icmp6_mtudisc_callback *mc;
304
 
305
        for (mc = LIST_FIRST(&icmp6_mtudisc_callbacks); mc != NULL;
306
             mc = LIST_NEXT(mc, mc_list)) {
307
                if (mc->mc_func == func)
308
                        return;
309
        }
310
 
311
        mc = malloc(sizeof(*mc), M_PCB, M_NOWAIT);
312
        if (mc == NULL)
313
                panic("icmp6_mtudisc_callback_register");
314
 
315
        mc->mc_func = func;
316
        LIST_INSERT_HEAD(&icmp6_mtudisc_callbacks, mc, mc_list);
317
}
318
#endif
319
 
320
/*
321
 * Generate an error packet of type error in response to bad IP6 packet.
322
 */
323
void
324
icmp6_error(m, type, code, param)
325
        struct mbuf *m;
326
        int type, code, param;
327
{
328
        struct ip6_hdr *oip6, *nip6;
329
        struct icmp6_hdr *icmp6;
330
        struct mbuf *n;
331
        struct ip6aux *ip6a;
332
        struct in6_addr nip6_src, *nip6_srcp;
333
        u_int preplen;
334
        int off;
335
        int nxt;
336
 
337
        icmp6stat.icp6s_error++;
338
 
339
        /* count per-type-code statistics */
340
        icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
341
 
342
#ifdef M_DECRYPTED      /* not openbsd */
343
        if (m->m_flags & M_DECRYPTED) {
344
                icmp6stat.icp6s_canterror++;
345
                goto freeit;
346
        }
347
#endif
348
 
349
#ifndef PULLDOWN_TEST
350
        IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
351
#else
352
        if (m->m_len < sizeof(struct ip6_hdr)) {
353
                m = m_pullup(m, sizeof(struct ip6_hdr));
354
                if (m == NULL)
355
                        return;
356
        }
357
#endif
358
        oip6 = mtod(m, struct ip6_hdr *);
359
 
360
        /*
361
         * If the destination address of the erroneous packet is a multicast
362
         * address, or the packet was sent using link-layer multicast,
363
         * we should basically suppress sending an error (RFC 2463, Section
364
         * 2.4).
365
         * We have two exceptions (the item e.2 in that section):
366
         * - the Pakcet Too Big message can be sent for path MTU discovery.
367
         * - the Parameter Problem Message that can be allowed an icmp6 error
368
         *   in the option type field.  This check has been done in
369
         *   ip6_unknown_opt(), so we can just check the type and code.
370
         */
371
        if ((m->m_flags & (M_BCAST|M_MCAST) ||
372
             IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
373
            (type != ICMP6_PACKET_TOO_BIG &&
374
             (type != ICMP6_PARAM_PROB ||
375
              code != ICMP6_PARAMPROB_OPTION)))
376
                goto freeit;
377
 
378
        /*
379
         * RFC 2463, 2.4 (e.5): source address check.
380
         * XXX: the case of anycast source?
381
         */
382
        if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
383
            IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
384
                goto freeit;
385
 
386
        /*
387
         * If we are about to send ICMPv6 against ICMPv6 error/redirect,
388
         * don't do it.
389
         */
390
        nxt = -1;
391
        off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
392
        if (off >= 0 && nxt == IPPROTO_ICMPV6) {
393
                struct icmp6_hdr *icp;
394
 
395
#ifndef PULLDOWN_TEST
396
                IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
397
                icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
398
#else
399
                IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
400
                        sizeof(*icp));
401
                if (icp == NULL) {
402
                        icmp6stat.icp6s_tooshort++;
403
                        return;
404
                }
405
#endif
406
                if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
407
                    icp->icmp6_type == ND_REDIRECT) {
408
                        /*
409
                         * ICMPv6 error
410
                         * Special case: for redirect (which is
411
                         * informational) we must not send icmp6 error.
412
                         */
413
                        icmp6stat.icp6s_canterror++;
414
                        goto freeit;
415
                } else {
416
                        /* ICMPv6 informational - send the error */
417
                }
418
        }
419
#if 0 /* controversial */
420
        else if (off >= 0 && nxt == IPPROTO_ESP) {
421
                /*
422
                 * It could be ICMPv6 error inside ESP.  Take a safer side,
423
                 * don't respond.
424
                 */
425
                icmp6stat.icp6s_canterror++;
426
                goto freeit;
427
        }
428
#endif
429
        else {
430
                /* non-ICMPv6 - send the error */
431
        }
432
 
433
        oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
434
 
435
        /* Finally, do rate limitation check. */
436
        if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
437
                icmp6stat.icp6s_toofreq++;
438
                goto freeit;
439
        }
440
 
441
        /*
442
         * OK, ICMP6 can be generated.
443
         */
444
 
445
        /*
446
         * Recover the original ip6_src if the src and the homeaddr
447
         * have been swapped while dest6 processing (see dest6.c).  To
448
         * avoid m_pulldown, we had better to swap addresses before
449
         * m_prepend below.
450
         *
451
         * XXX: We should consider the other candidate to keep the
452
         * icmp6 error packet correct that not swapping ip6_src and
453
         * homeaddr in the dest6 processing.
454
         */
455
        nip6_srcp = &oip6->ip6_src;
456
        n = ip6_findaux(m);
457
        if (n != NULL) {
458
                ip6a = mtod(n, struct ip6aux *);
459
                if ((ip6a->ip6a_flags & IP6A_HASEEN) != 0 &&
460
                    (ip6a->ip6a_flags & IP6A_SWAP) != 0) {
461
                        nip6_src = oip6->ip6_src;
462
                        nip6_srcp = &nip6_src;
463
                        if (icmp6_recover_src(m)) {
464
                                /* mbuf is freed in icmp6_recover_src */
465
                                return;
466
                        }
467
                        oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
468
                }
469
        }
470
 
471
        if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
472
                m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
473
 
474
        preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
475
        M_PREPEND(m, preplen, M_DONTWAIT);
476
        if (m && m->m_len < preplen)
477
                m = m_pullup(m, preplen);
478
        if (m == NULL) {
479
                nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
480
                return;
481
        }
482
 
483
        nip6 = mtod(m, struct ip6_hdr *);
484
        nip6->ip6_src  = *nip6_srcp;
485
        nip6->ip6_dst  = oip6->ip6_dst;
486
 
487
        if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
488
                oip6->ip6_src.s6_addr16[1] = 0;
489
        if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
490
                oip6->ip6_dst.s6_addr16[1] = 0;
491
 
492
        icmp6 = (struct icmp6_hdr *)(nip6 + 1);
493
        icmp6->icmp6_type = type;
494
        icmp6->icmp6_code = code;
495
        icmp6->icmp6_pptr = htonl((u_int32_t)param);
496
 
497
        /*
498
         * icmp6_reflect() is designed to be in the input path.
499
         * icmp6_error() can be called from both input and outut path,
500
         * and if we are in output path rcvif could contain bogus value.
501
         * clear m->m_pkthdr.rcvif for safety, we should have enough scope
502
         * information in ip header (nip6).
503
         */
504
        m->m_pkthdr.rcvif = NULL;
505
 
506
        icmp6stat.icp6s_outhist[type]++;
507
        icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
508
 
509
        return;
510
 
511
  freeit:
512
        /*
513
         * If we can't tell wheter or not we can generate ICMP6, free it.
514
         */
515
        m_freem(m);
516
}
517
 
518
/*
519
 * Process a received ICMP6 message.
520
 */
521
int
522
icmp6_input(mp, offp, proto)
523
        struct mbuf **mp;
524
        int *offp, proto;
525
{
526
        struct mbuf *m = *mp, *n;
527
        struct ip6_hdr *ip6, *nip6;
528
        struct icmp6_hdr *icmp6, *nicmp6;
529
        int off = *offp;
530
        int icmp6len = m->m_pkthdr.len - *offp;
531
        int code, sum, noff;
532
 
533
        icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
534
 
535
#ifndef PULLDOWN_TEST
536
        IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
537
        /* m might change if M_LOOP.  So, call mtod after this */
538
#endif
539
 
540
        /*
541
         * Locate icmp6 structure in mbuf, and check
542
         * that not corrupted and of at least minimum length
543
         */
544
 
545
        ip6 = mtod(m, struct ip6_hdr *);
546
        if (icmp6len < sizeof(struct icmp6_hdr)) {
547
                icmp6stat.icp6s_tooshort++;
548
                icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
549
                goto freeit;
550
        }
551
 
552
        /*
553
         * calculate the checksum
554
         */
555
#ifndef PULLDOWN_TEST
556
        icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
557
#else
558
        IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
559
        if (icmp6 == NULL) {
560
                icmp6stat.icp6s_tooshort++;
561
                icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
562
                return IPPROTO_DONE;
563
        }
564
#endif
565
        code = icmp6->icmp6_code;
566
 
567
        if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
568
                nd6log((LOG_ERR,
569
                    "ICMP6 checksum error(%d|%x) %s\n",
570
                    icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src)));
571
                icmp6stat.icp6s_checksum++;
572
                icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
573
                goto freeit;
574
        }
575
 
576
#if defined(NFAITH) && 0 < NFAITH
577
        if (faithprefix(&ip6->ip6_dst)) {
578
                /*
579
                 * Deliver very specific ICMP6 type only.
580
                 * This is important to deilver TOOBIG.  Otherwise PMTUD
581
                 * will not work.
582
                 */
583
                switch (icmp6->icmp6_type) {
584
                case ICMP6_DST_UNREACH:
585
                case ICMP6_PACKET_TOO_BIG:
586
                case ICMP6_TIME_EXCEEDED:
587
                        break;
588
                default:
589
                        goto freeit;
590
                }
591
        }
592
#endif
593
 
594
        icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
595
 
596
#ifdef MIP6
597
        if (mip6_icmp6_tunnel_input(m, off, icmp6len)) {
598
                goto freeit;
599
        }
600
#endif /* MIP6 */
601
        switch (icmp6->icmp6_type) {
602
        case ICMP6_DST_UNREACH:
603
                icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
604
#ifdef MIP6
605
                if (mip6_icmp6_input(m, off, icmp6len))
606
                        goto freeit;
607
#endif /* MIP6 */
608
                switch (code) {
609
                case ICMP6_DST_UNREACH_NOROUTE:
610
                        code = PRC_UNREACH_NET;
611
                        break;
612
                case ICMP6_DST_UNREACH_ADMIN:
613
                        icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
614
                        code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
615
                        break;
616
                case ICMP6_DST_UNREACH_ADDR:
617
                        code = PRC_HOSTDEAD;
618
                        break;
619
#ifdef COMPAT_RFC1885
620
                case ICMP6_DST_UNREACH_NOTNEIGHBOR:
621
                        code = PRC_UNREACH_SRCFAIL;
622
                        break;
623
#else
624
                case ICMP6_DST_UNREACH_BEYONDSCOPE:
625
                        /* I mean "source address was incorrect." */
626
                        code = PRC_UNREACH_NET;
627
                        break;
628
#endif
629
                case ICMP6_DST_UNREACH_NOPORT:
630
                        code = PRC_UNREACH_PORT;
631
                        break;
632
                default:
633
                        goto badcode;
634
                }
635
                goto deliver;
636
                break;
637
 
638
        case ICMP6_PACKET_TOO_BIG:
639
                icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
640
                if (code != 0)
641
                        goto badcode;
642
 
643
                code = PRC_MSGSIZE;
644
 
645
                /*
646
                 * Updating the path MTU will be done after examining
647
                 * intermediate extension headers.
648
                 */
649
                goto deliver;
650
                break;
651
 
652
        case ICMP6_TIME_EXCEEDED:
653
                icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
654
                switch (code) {
655
                case ICMP6_TIME_EXCEED_TRANSIT:
656
                        code = PRC_TIMXCEED_INTRANS;
657
                        break;
658
                case ICMP6_TIME_EXCEED_REASSEMBLY:
659
                        code = PRC_TIMXCEED_REASS;
660
                        break;
661
                default:
662
                        goto badcode;
663
                }
664
                goto deliver;
665
                break;
666
 
667
        case ICMP6_PARAM_PROB:
668
                icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
669
                switch (code) {
670
                case ICMP6_PARAMPROB_NEXTHEADER:
671
                        code = PRC_UNREACH_PROTOCOL;
672
                        break;
673
                case ICMP6_PARAMPROB_HEADER:
674
                case ICMP6_PARAMPROB_OPTION:
675
#ifdef MIP6
676
                        if (mip6_icmp6_input(m, off, icmp6len))
677
                                goto freeit;
678
#endif /* MIP6 */
679
                        code = PRC_PARAMPROB;
680
                        break;
681
                default:
682
                        goto badcode;
683
                }
684
                goto deliver;
685
                break;
686
 
687
        case ICMP6_ECHO_REQUEST:
688
                icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
689
                if (code != 0)
690
                        goto badcode;
691
                /*
692
                 * Copy mbuf to send to two data paths: userland socket(s),
693
                 * and to the querier (echo reply).
694
                 * m: a copy for socket, n: a copy for querier
695
                 */
696
                if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
697
                        /* Give up local */
698
                        n = m;
699
                        m = NULL;
700
                        goto deliverecho;
701
                }
702
                /*
703
                 * If the first mbuf is shared, or the first mbuf is too short,
704
                 * copy the first part of the data into a fresh mbuf.
705
                 * Otherwise, we will wrongly overwrite both copies.
706
                 */
707
                if ((n->m_flags & M_EXT) != 0 ||
708
                    n->m_len < off + sizeof(struct icmp6_hdr)) {
709
                        struct mbuf *n0 = n;
710
                        const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
711
 
712
                        /*
713
                         * Prepare an internal mbuf.  m_pullup() doesn't
714
                         * always copy the length we specified.
715
                         */
716
                        if (maxlen >= MCLBYTES) {
717
                                /* Give up remote */
718
                                m_freem(n0);
719
                                break;
720
                        }
721
                        MGETHDR(n, M_DONTWAIT, n0->m_type);
722
                        if (n && maxlen >= MHLEN) {
723
                                MCLGET(n, M_DONTWAIT);
724
                                if ((n->m_flags & M_EXT) == 0) {
725
                                        m_free(n);
726
                                        n = NULL;
727
                                }
728
                        }
729
                        if (n == NULL) {
730
                                /* Give up local */
731
                                m_freem(n0);
732
                                n = m;
733
                                m = NULL;
734
                                goto deliverecho;
735
                        }
736
#ifdef __OpenBSD__
737
                        M_MOVE_PKTHDR(n, n0);
738
#else
739
                        M_COPY_PKTHDR(n, n0);
740
#endif
741
                        /*
742
                         * Copy IPv6 and ICMPv6 only.
743
                         */
744
                        nip6 = mtod(n, struct ip6_hdr *);
745
                        bcopy(ip6, nip6, sizeof(struct ip6_hdr));
746
                        nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
747
                        bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
748
                        noff = sizeof(struct ip6_hdr);
749
                        n->m_len = noff + sizeof(struct icmp6_hdr);
750
                        /*
751
                         * Adjust mbuf.  ip6_plen will be adjusted in
752
                         * ip6_output().
753
                         * n->m_pkthdr.len == n0->m_pkthdr.len at this point.
754
                         */
755
                        n->m_pkthdr.len += noff + sizeof(struct icmp6_hdr);
756
                        n->m_pkthdr.len -= (off + sizeof(struct icmp6_hdr));
757
                        m_adj(n0, off + sizeof(struct icmp6_hdr));
758
                        n->m_next = n0;
759
                        n0->m_flags &= ~M_PKTHDR;
760
                } else {
761
         deliverecho:
762
                        nip6 = mtod(n, struct ip6_hdr *);
763
                        nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off);
764
                        noff = off;
765
                }
766
                nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
767
                nicmp6->icmp6_code = 0;
768
                if (n) {
769
                        icmp6stat.icp6s_reflect++;
770
                        icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
771
                        icmp6_reflect(n, noff);
772
                }
773
                if (!m)
774
                        goto freeit;
775
                break;
776
 
777
        case ICMP6_ECHO_REPLY:
778
                icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
779
                if (code != 0)
780
                        goto badcode;
781
                break;
782
 
783
#ifdef MIP6
784
        case ICMP6_HADISCOV_REQUEST:
785
                if (icmp6len < sizeof(struct ha_discov_req))
786
                        goto badlen;
787
                if (code != 0)
788
                        goto badcode;
789
                if (mip6_icmp6_input(m, off, icmp6len))
790
                        goto freeit;
791
                break;
792
        case ICMP6_HADISCOV_REPLY:
793
                if (icmp6len < sizeof(struct ha_discov_rep))
794
                        goto badlen;
795
                if (code != 0)
796
                        goto badcode;
797
                if (mip6_icmp6_input(m, off, icmp6len))
798
                        goto freeit;
799
                break;
800
#ifndef MIP6_DRAFT13
801
        case ICMP6_MOBILEPREFIX_SOLICIT:
802
                if (icmp6len < sizeof(struct mobile_prefix_solicit))
803
                        goto badlen;
804
                if (code != 0)
805
                        goto badcode;
806
                if (mip6_icmp6_input(m, off, icmp6len))
807
                        goto freeit;
808
                break;
809
 
810
        case ICMP6_MOBILEPREFIX_ADVERT:
811
                if (icmp6len < sizeof (struct mobile_prefix_advert))
812
                        goto badlen;
813
                if (code != 0)
814
                        goto badcode;
815
                if (mip6_icmp6_input(m, off, icmp6len))
816
                        goto freeit;
817
                break;
818
#endif /* !MIP6_DRAFT13 */
819
#endif /* MIP6 */
820
 
821
        case MLD_LISTENER_QUERY:
822
        case MLD_LISTENER_REPORT:
823
                if (icmp6len < sizeof(struct mld_hdr))
824
                        goto badlen;
825
                if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
826
                        icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
827
                else
828
                        icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
829
                if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
830
                        /* give up local */
831
                        mld6_input(m, off);
832
                        m = NULL;
833
                        goto freeit;
834
                }
835
                mld6_input(n, off);
836
                /* m stays. */
837
                break;
838
 
839
        case MLD_LISTENER_DONE:
840
                icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
841
                if (icmp6len < sizeof(struct mld_hdr))  /* necessary? */
842
                        goto badlen;
843
                break;          /* nothing to be done in kernel */
844
 
845
        case MLD_MTRACE_RESP:
846
        case MLD_MTRACE:
847
                /* XXX: these two are experimental.  not officially defined. */
848
                /* XXX: per-interface statistics? */
849
                break;          /* just pass it to applications */
850
 
851
        case ICMP6_WRUREQUEST:  /* ICMP6_FQDN_QUERY */
852
            {
853
                enum { WRU, FQDN } mode;
854
 
855
                if (!icmp6_nodeinfo)
856
                        break;
857
 
858
                if (icmp6len == sizeof(struct icmp6_hdr) + 4)
859
                        mode = WRU;
860
                else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
861
                        mode = FQDN;
862
                else
863
                        goto badlen;
864
 
865
#ifdef __FreeBSD__
866
#define hostnamelen     strlen(hostname)
867
#endif
868
                if (mode == FQDN) {
869
#ifndef PULLDOWN_TEST
870
                        IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
871
                                         IPPROTO_DONE);
872
#endif
873
                        n = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
874
                        if (n)
875
                                n = ni6_input(n, off);
876
                        /* XXX meaningless if n == NULL */
877
                        noff = sizeof(struct ip6_hdr);
878
                } else {
879
                        u_char *p;
880
                        int maxlen, maxhlen;
881
 
882
                        if ((icmp6_nodeinfo & 5) != 5)
883
                                break;
884
 
885
                        if (code != 0)
886
                                goto badcode;
887
                        maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4;
888
                        if (maxlen >= MCLBYTES) {
889
                                /* Give up remote */
890
                                break;
891
                        }
892
                        MGETHDR(n, M_DONTWAIT, m->m_type);
893
                        if (n && maxlen > MHLEN) {
894
                                MCLGET(n, M_DONTWAIT);
895
                                if ((n->m_flags & M_EXT) == 0) {
896
                                        m_free(n);
897
                                        n = NULL;
898
                                }
899
                        }
900
                        if (n == NULL) {
901
                                /* Give up remote */
902
                                break;
903
                        }
904
                        n->m_pkthdr.rcvif = NULL;
905
                        n->m_len = 0;
906
                        maxhlen = M_TRAILINGSPACE(n) - maxlen;
907
                        if (maxhlen > hostnamelen)
908
                                maxhlen = hostnamelen;
909
                        /*
910
                         * Copy IPv6 and ICMPv6 only.
911
                         */
912
                        nip6 = mtod(n, struct ip6_hdr *);
913
                        bcopy(ip6, nip6, sizeof(struct ip6_hdr));
914
                        nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
915
                        bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
916
                        p = (u_char *)(nicmp6 + 1);
917
                        bzero(p, 4);
918
                        bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */
919
                        noff = sizeof(struct ip6_hdr);
920
#ifdef __OpenBSD__
921
                        M_DUP_PKTHDR(n, m); /* just for rcvif */
922
#else
923
                        M_COPY_PKTHDR(n, m); /* just for rcvif */
924
#endif
925
                        n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
926
                                sizeof(struct icmp6_hdr) + 4 + maxhlen;
927
                        nicmp6->icmp6_type = ICMP6_WRUREPLY;
928
                        nicmp6->icmp6_code = 0;
929
                }
930
#undef hostnamelen
931
                if (n) {
932
                        icmp6stat.icp6s_reflect++;
933
                        icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
934
                        icmp6_reflect(n, noff);
935
                }
936
                break;
937
            }
938
 
939
        case ICMP6_WRUREPLY:
940
                if (code != 0)
941
                        goto badcode;
942
                break;
943
 
944
        case ND_ROUTER_SOLICIT:
945
                icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
946
                if (code != 0)
947
                        goto badcode;
948
                if (icmp6len < sizeof(struct nd_router_solicit))
949
                        goto badlen;
950
                if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
951
                        /* give up local */
952
                        nd6_rs_input(m, off, icmp6len);
953
                        m = NULL;
954
                        goto freeit;
955
                }
956
                nd6_rs_input(n, off, icmp6len);
957
                /* m stays. */
958
                break;
959
 
960
        case ND_ROUTER_ADVERT:
961
                icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
962
                if (code != 0)
963
                        goto badcode;
964
                if (icmp6len < sizeof(struct nd_router_advert))
965
                        goto badlen;
966
                if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
967
                        /* give up local */
968
                        nd6_ra_input(m, off, icmp6len);
969
                        m = NULL;
970
                        goto freeit;
971
                }
972
                nd6_ra_input(n, off, icmp6len);
973
                /* m stays. */
974
                break;
975
 
976
        case ND_NEIGHBOR_SOLICIT:
977
                icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
978
                if (code != 0)
979
                        goto badcode;
980
                if (icmp6len < sizeof(struct nd_neighbor_solicit))
981
                        goto badlen;
982
                if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
983
                        /* give up local */
984
                        nd6_ns_input(m, off, icmp6len);
985
                        m = NULL;
986
                        goto freeit;
987
                }
988
                nd6_ns_input(n, off, icmp6len);
989
                /* m stays. */
990
                break;
991
 
992
        case ND_NEIGHBOR_ADVERT:
993
                icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
994
                if (code != 0)
995
                        goto badcode;
996
                if (icmp6len < sizeof(struct nd_neighbor_advert))
997
                        goto badlen;
998
                if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
999
                        /* give up local */
1000
                        nd6_na_input(m, off, icmp6len);
1001
                        m = NULL;
1002
                        goto freeit;
1003
                }
1004
                nd6_na_input(n, off, icmp6len);
1005
                /* m stays. */
1006
                break;
1007
 
1008
        case ND_REDIRECT:
1009
                icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
1010
                if (code != 0)
1011
                        goto badcode;
1012
                if (icmp6len < sizeof(struct nd_redirect))
1013
                        goto badlen;
1014
                if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
1015
                        /* give up local */
1016
                        icmp6_redirect_input(m, off);
1017
                        m = NULL;
1018
                        goto freeit;
1019
                }
1020
                icmp6_redirect_input(n, off);
1021
                /* m stays. */
1022
                break;
1023
 
1024
        case ICMP6_ROUTER_RENUMBERING:
1025
                if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
1026
                    code != ICMP6_ROUTER_RENUMBERING_RESULT)
1027
                        goto badcode;
1028
                if (icmp6len < sizeof(struct icmp6_router_renum))
1029
                        goto badlen;
1030
                break;
1031
 
1032
        default:
1033
                nd6log((LOG_DEBUG,
1034
                    "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
1035
                    icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src),
1036
                    ip6_sprintf(&ip6->ip6_dst),
1037
                    m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0));
1038
                if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
1039
                        /* ICMPv6 error: MUST deliver it by spec... */
1040
                        code = PRC_NCMDS;
1041
                        /* deliver */
1042
                } else {
1043
                        /* ICMPv6 informational: MUST not deliver */
1044
                        break;
1045
                }
1046
        deliver:
1047
                if (icmp6_notify_error(m, off, icmp6len, code)) {
1048
                        /* In this case, m should've been freed. */
1049
                        return(IPPROTO_DONE);
1050
                }
1051
                break;
1052
 
1053
        badcode:
1054
                icmp6stat.icp6s_badcode++;
1055
                break;
1056
 
1057
        badlen:
1058
                icmp6stat.icp6s_badlen++;
1059
                break;
1060
        }
1061
 
1062
        /* deliver the packet to appropriate sockets */
1063
        icmp6_rip6_input(&m, *offp);
1064
 
1065
        return IPPROTO_DONE;
1066
 
1067
 freeit:
1068
        m_freem(m);
1069
        return IPPROTO_DONE;
1070
}
1071
 
1072
static int
1073
icmp6_notify_error(m, off, icmp6len, code)
1074
        struct mbuf *m;
1075
        int off, icmp6len;
1076
{
1077
        struct icmp6_hdr *icmp6;
1078
        struct ip6_hdr *eip6;
1079
        u_int32_t notifymtu;
1080
        int64_t zoneid;
1081
        struct sockaddr_in6 icmp6src, icmp6dst;
1082
 
1083
        if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
1084
                icmp6stat.icp6s_tooshort++;
1085
                goto freeit;
1086
        }
1087
#ifndef PULLDOWN_TEST
1088
        IP6_EXTHDR_CHECK(m, off,
1089
                         sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr),
1090
                         -1);
1091
        icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1092
#else
1093
        IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1094
                       sizeof(*icmp6) + sizeof(struct ip6_hdr));
1095
        if (icmp6 == NULL) {
1096
                icmp6stat.icp6s_tooshort++;
1097
                return(-1);
1098
        }
1099
#endif
1100
        eip6 = (struct ip6_hdr *)(icmp6 + 1);
1101
 
1102
        /* Detect the upper level protocol */
1103
        {
1104
                void (*ctlfunc) __P((int, struct sockaddr *, void *));
1105
                u_int8_t nxt = eip6->ip6_nxt;
1106
                int eoff = off + sizeof(struct icmp6_hdr) +
1107
                        sizeof(struct ip6_hdr);
1108
                struct ip6ctlparam ip6cp;
1109
                struct in6_addr *finaldst = NULL;
1110
                int icmp6type = icmp6->icmp6_type;
1111
                struct ip6_frag *fh;
1112
                struct ip6_rthdr *rth;
1113
                struct ip6_rthdr0 *rth0;
1114
                int rthlen;
1115
 
1116
                while (1) { /* XXX: should avoid infinite loop explicitly? */
1117
                        struct ip6_ext *eh;
1118
 
1119
                        switch (nxt) {
1120
                        case IPPROTO_HOPOPTS:
1121
                        case IPPROTO_DSTOPTS:
1122
                        case IPPROTO_AH:
1123
#ifndef PULLDOWN_TEST
1124
                                IP6_EXTHDR_CHECK(m, 0, eoff +
1125
                                                 sizeof(struct ip6_ext),
1126
                                                 -1);
1127
                                eh = (struct ip6_ext *)(mtod(m, caddr_t)
1128
                                                        + eoff);
1129
#else
1130
                                IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
1131
                                               eoff, sizeof(*eh));
1132
                                if (eh == NULL) {
1133
                                        icmp6stat.icp6s_tooshort++;
1134
                                        return(-1);
1135
                                }
1136
#endif
1137
 
1138
                                if (nxt == IPPROTO_AH)
1139
                                        eoff += (eh->ip6e_len + 2) << 2;
1140
                                else
1141
                                        eoff += (eh->ip6e_len + 1) << 3;
1142
                                nxt = eh->ip6e_nxt;
1143
                                break;
1144
                        case IPPROTO_ROUTING:
1145
                                /*
1146
                                 * When the erroneous packet contains a
1147
                                 * routing header, we should examine the
1148
                                 * header to determine the final destination.
1149
                                 * Otherwise, we can't properly update
1150
                                 * information that depends on the final
1151
                                 * destination (e.g. path MTU).
1152
                                 */
1153
#ifndef PULLDOWN_TEST
1154
                                IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth),
1155
                                                 -1);
1156
                                rth = (struct ip6_rthdr *)(mtod(m, caddr_t)
1157
                                                           + eoff);
1158
#else
1159
                                IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
1160
                                               eoff, sizeof(*rth));
1161
                                if (rth == NULL) {
1162
                                        icmp6stat.icp6s_tooshort++;
1163
                                        return(-1);
1164
                                }
1165
#endif
1166
                                rthlen = (rth->ip6r_len + 1) << 3;
1167
                                /*
1168
                                 * XXX: currently there is no
1169
                                 * officially defined type other
1170
                                 * than type-0.
1171
                                 * Note that if the segment left field
1172
                                 * is 0, all intermediate hops must
1173
                                 * have been passed.
1174
                                 */
1175
                                if (rth->ip6r_segleft &&
1176
                                    rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
1177
                                        int hops;
1178
 
1179
#ifndef PULLDOWN_TEST
1180
                                        IP6_EXTHDR_CHECK(m, 0, eoff + rthlen,
1181
                                                         -1);
1182
                                        rth0 = (struct ip6_rthdr0 *)(mtod(m, caddr_t) + eoff);
1183
#else
1184
                                        IP6_EXTHDR_GET(rth0,
1185
                                                       struct ip6_rthdr0 *, m,
1186
                                                       eoff, rthlen);
1187
                                        if (rth0 == NULL) {
1188
                                                icmp6stat.icp6s_tooshort++;
1189
                                                return(-1);
1190
                                        }
1191
#endif
1192
                                        /* just ignore a bogus header */
1193
                                        if ((rth0->ip6r0_len % 2) == 0 &&
1194
                                            (hops = rth0->ip6r0_len/2))
1195
                                                finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
1196
                                }
1197
                                eoff += rthlen;
1198
                                nxt = rth->ip6r_nxt;
1199
                                break;
1200
                        case IPPROTO_FRAGMENT:
1201
#ifndef PULLDOWN_TEST
1202
                                IP6_EXTHDR_CHECK(m, 0, eoff +
1203
                                                 sizeof(struct ip6_frag),
1204
                                                 -1);
1205
                                fh = (struct ip6_frag *)(mtod(m, caddr_t)
1206
                                                         + eoff);
1207
#else
1208
                                IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
1209
                                               eoff, sizeof(*fh));
1210
                                if (fh == NULL) {
1211
                                        icmp6stat.icp6s_tooshort++;
1212
                                        return(-1);
1213
                                }
1214
#endif
1215
                                /*
1216
                                 * Data after a fragment header is meaningless
1217
                                 * unless it is the first fragment, but
1218
                                 * we'll go to the notify label for path MTU
1219
                                 * discovery.
1220
                                 */
1221
                                if (fh->ip6f_offlg & IP6F_OFF_MASK)
1222
                                        goto notify;
1223
 
1224
                                eoff += sizeof(struct ip6_frag);
1225
                                nxt = fh->ip6f_nxt;
1226
                                break;
1227
                        default:
1228
                                /*
1229
                                 * This case includes ESP and the No Next
1230
                                 * Header.  In such cases going to the notify
1231
                                 * label does not have any meaning
1232
                                 * (i.e. ctlfunc will be NULL), but we go
1233
                                 * anyway since we might have to update
1234
                                 * path MTU information.
1235
                                 */
1236
                                goto notify;
1237
                        }
1238
                }
1239
          notify:
1240
#ifndef PULLDOWN_TEST
1241
                icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1242
#else
1243
                IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1244
                               sizeof(*icmp6) + sizeof(struct ip6_hdr));
1245
                if (icmp6 == NULL) {
1246
                        icmp6stat.icp6s_tooshort++;
1247
                        return(-1);
1248
                }
1249
#endif
1250
 
1251
                eip6 = (struct ip6_hdr *)(icmp6 + 1);
1252
                bzero(&icmp6dst, sizeof(icmp6dst));
1253
                icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1254
                icmp6dst.sin6_family = AF_INET6;
1255
                if (finaldst == NULL)
1256
                        icmp6dst.sin6_addr = eip6->ip6_dst;
1257
                else
1258
                        icmp6dst.sin6_addr = *finaldst;
1259
                if ((zoneid = in6_addr2zoneid(m->m_pkthdr.rcvif,
1260
                                              &icmp6dst.sin6_addr)) < 0)
1261
                        goto freeit;
1262
                icmp6dst.sin6_scope_id = zoneid;
1263
                if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst)) {
1264
                        /* should be impossbile */
1265
                        nd6log((LOG_DEBUG,
1266
                            "icmp6_notify_error: in6_embedscope failed\n"));
1267
                        goto freeit;
1268
                }
1269
 
1270
                /*
1271
                 * retrieve parameters from the inner IPv6 header, and convert
1272
                 * them into sockaddr structures.
1273
                 */
1274
                bzero(&icmp6src, sizeof(icmp6src));
1275
                icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1276
                icmp6src.sin6_family = AF_INET6;
1277
                icmp6src.sin6_addr = eip6->ip6_src;
1278
                if ((zoneid = in6_addr2zoneid(m->m_pkthdr.rcvif,
1279
                                              &icmp6src.sin6_addr)) < 0)
1280
                        goto freeit;
1281
                icmp6src.sin6_scope_id = zoneid;
1282
                if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src)) {
1283
                        /* should be impossbile */
1284
                        nd6log((LOG_DEBUG,
1285
                            "icmp6_notify_error: in6_embedscope failed\n"));
1286
                        goto freeit;
1287
                }
1288
                icmp6src.sin6_flowinfo =
1289
                        (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1290
 
1291
                if (finaldst == NULL)
1292
                        finaldst = &eip6->ip6_dst;
1293
                ip6cp.ip6c_m = m;
1294
                ip6cp.ip6c_icmp6 = icmp6;
1295
                ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1296
                ip6cp.ip6c_off = eoff;
1297
                ip6cp.ip6c_finaldst = finaldst;
1298
                ip6cp.ip6c_src = &icmp6src;
1299
                ip6cp.ip6c_nxt = nxt;
1300
 
1301
                if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1302
                        notifymtu = ntohl(icmp6->icmp6_mtu);
1303
                        ip6cp.ip6c_cmdarg = (void *)&notifymtu;
1304
#if !(defined(__NetBSD__) || defined(__OpenBSD__))
1305
                        icmp6_mtudisc_update(&ip6cp, 1);        /* XXX */
1306
#endif
1307
                }
1308
 
1309
                ctlfunc = (void (*) __P((int, struct sockaddr *, void *)))
1310
                        (inet6sw[ip6_protox[nxt]].pr_ctlinput);
1311
                if (ctlfunc) {
1312
                        (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
1313
                                          &ip6cp);
1314
                }
1315
        }
1316
        return(0);
1317
 
1318
  freeit:
1319
        m_freem(m);
1320
        return(-1);
1321
}
1322
 
1323
void
1324
icmp6_mtudisc_update(ip6cp, validated)
1325
        struct ip6ctlparam *ip6cp;
1326
        int validated;
1327
{
1328
#if defined(__NetBSD__) || defined(__OpenBSD__)
1329
        unsigned long rtcount;
1330
        struct icmp6_mtudisc_callback *mc;
1331
#endif
1332
        struct in6_addr *dst = ip6cp->ip6c_finaldst;
1333
        struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1334
        struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1335
        u_int mtu = ntohl(icmp6->icmp6_mtu);
1336
        struct rtentry *rt = NULL;
1337
        struct sockaddr_in6 sin6;
1338
#ifdef __bsdi__
1339
#ifdef NEW_STRUCT_ROUTE
1340
        struct route ro6;
1341
#else
1342
        struct route_in6 ro6;
1343
#endif
1344
#endif
1345
 
1346
#if defined(__NetBSD__) || defined(__OpenBSD__)
1347
        /*
1348
         * allow non-validated cases if memory is plenty, to make traffic
1349
         * from non-connected pcb happy.
1350
         */
1351
        rtcount = rt_timer_count(icmp6_mtudisc_timeout_q);
1352
        if (validated) {
1353
                if (0 <= icmp6_mtudisc_hiwat && rtcount > icmp6_mtudisc_hiwat)
1354
                        return;
1355
                else if (0 <= icmp6_mtudisc_lowat &&
1356
                    rtcount > icmp6_mtudisc_lowat) {
1357
                        /*
1358
                         * XXX nuke a victim, install the new one.
1359
                         */
1360
                }
1361
        } else {
1362
                if (0 <= icmp6_mtudisc_lowat && rtcount > icmp6_mtudisc_lowat)
1363
                        return;
1364
        }
1365
#else
1366
        if (!validated)
1367
                return;
1368
#endif
1369
 
1370
        bzero(&sin6, sizeof(sin6));
1371
        sin6.sin6_family = PF_INET6;
1372
        sin6.sin6_len = sizeof(struct sockaddr_in6);
1373
        sin6.sin6_addr = *dst;
1374
        /* XXX normally, this won't happen */
1375
        if (IN6_IS_ADDR_LINKLOCAL(dst)) {
1376
                sin6.sin6_addr.s6_addr16[1] =
1377
                    htons(m->m_pkthdr.rcvif->if_index);
1378
        }
1379
        /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1380
#if defined(__NetBSD__) || defined(__OpenBSD__)
1381
        rt = icmp6_mtudisc_clone((struct sockaddr *)&sin6);
1382
#else
1383
#ifdef __FreeBSD__
1384
        rt = rtalloc1((struct sockaddr *)&sin6, 0,
1385
                      RTF_CLONING | RTF_PRCLONING);
1386
#else
1387
#ifdef __bsdi__
1388
        bcopy(&sin6, &ro6.ro_dst, sizeof(struct sockaddr_in6));
1389
        ro6.ro_rt = 0;
1390
        /* rtcalloc((struct route *)&ro6); */
1391
        rtalloc((struct route *)&ro6);
1392
        rt = ro6.ro_rt;
1393
#else
1394
#error no case for this particular operating system
1395
#endif
1396
#endif
1397
#endif
1398
 
1399
        if (rt && (rt->rt_flags & RTF_HOST)
1400
            && !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
1401
                if (mtu < IPV6_MMTU) {
1402
                                /* xxx */
1403
                        rt->rt_rmx.rmx_locks |= RTV_MTU;
1404
                } else if (mtu < rt->rt_ifp->if_mtu &&
1405
                           rt->rt_rmx.rmx_mtu > mtu) {
1406
                        icmp6stat.icp6s_pmtuchg++;
1407
                        rt->rt_rmx.rmx_mtu = mtu;
1408
                }
1409
        }
1410
        if (rt) { /* XXX: need braces to avoid conflict with else in RTFREE. */
1411
                RTFREE(rt);
1412
        }
1413
 
1414
#if defined(__NetBSD__) || defined(__OpenBSD__)
1415
        /*
1416
         * Notify protocols that the MTU for this destination
1417
         * has changed.
1418
         */
1419
        for (mc = LIST_FIRST(&icmp6_mtudisc_callbacks); mc != NULL;
1420
             mc = LIST_NEXT(mc, mc_list))
1421
                (*mc->mc_func)(&sin6.sin6_addr);
1422
#endif
1423
}
1424
 
1425
/*
1426
 * Process a Node Information Query packet, based on
1427
 * draft-ietf-ipngwg-icmp-name-lookups-07.
1428
 *
1429
 * Spec incompatibilities:
1430
 * - IPv6 Subject address handling
1431
 * - IPv4 Subject address handling support missing
1432
 * - Proxy reply (answer even if it's not for me)
1433
 * - joins NI group address at in6_ifattach() time only, does not cope
1434
 *   with hostname changes by sethostname(3)
1435
 */
1436
#ifdef __FreeBSD__
1437
#define hostnamelen     strlen(hostname)
1438
#endif
1439
#ifndef offsetof                /* XXX */
1440
#define offsetof(type, member)  ((size_t)(&((type *)0)->member))
1441
#endif
1442
static struct mbuf *
1443
ni6_input(m, off)
1444
        struct mbuf *m;
1445
        int off;
1446
{
1447
        struct icmp6_nodeinfo *ni6, *nni6;
1448
        struct mbuf *n = NULL;
1449
        u_int16_t qtype;
1450
        int subjlen;
1451
        int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1452
        struct ni_reply_fqdn *fqdn;
1453
        int addrs;              /* for NI_QTYPE_NODEADDR */
1454
        struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1455
        struct sockaddr_in6 sin6; /* double meaning; ip6_dst and subjectaddr */
1456
        struct sockaddr_in6 sin6_d; /* XXX: we should retrieve this from m_aux */
1457
        struct ip6_hdr *ip6;
1458
        int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1459
        char *subj = NULL;
1460
        struct in6_ifaddr *ia6 = NULL;
1461
        int64_t zoneid;
1462
 
1463
        ip6 = mtod(m, struct ip6_hdr *);
1464
#ifndef PULLDOWN_TEST
1465
        ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1466
#else
1467
        IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1468
        if (ni6 == NULL) {
1469
                /* m is already reclaimed */
1470
                return NULL;
1471
        }
1472
#endif
1473
 
1474
        /*
1475
         * Validate IPv6 destination address.
1476
         *
1477
         * The Responder must discard the Query without further processing
1478
         * unless it is one of the Responder's unicast or anycast addresses, or
1479
         * a link-local scope multicast address which the Responder has joined.
1480
         * [icmp-name-lookups-07, Section 4.]
1481
         */
1482
        bzero(&sin6, sizeof(sin6));
1483
        sin6.sin6_family = AF_INET6;
1484
        sin6.sin6_len = sizeof(struct sockaddr_in6);
1485
        bcopy(&ip6->ip6_dst, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
1486
        /* XXX scopeid */
1487
        if ((ia6 = (struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *)&sin6)) != NULL) {
1488
                /* unicast/anycast, fine */
1489
                if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1490
                    (icmp6_nodeinfo & 4) == 0) {
1491
                        nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1492
                                "a temporary address in %s:%d",
1493
                               __FILE__, __LINE__));
1494
                        goto bad;
1495
                }
1496
        } else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr))
1497
                ; /* link-local multicast, fine */
1498
        else
1499
                goto bad;
1500
 
1501
        /* validate query Subject field. */
1502
        qtype = ntohs(ni6->ni_qtype);
1503
        subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1504
        switch (qtype) {
1505
        case NI_QTYPE_NOOP:
1506
        case NI_QTYPE_SUPTYPES:
1507
                /* 07 draft */
1508
                if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1509
                        break;
1510
                /* FALLTHROUGH */
1511
        case NI_QTYPE_FQDN:
1512
        case NI_QTYPE_NODEADDR:
1513
        case NI_QTYPE_IPV4ADDR:
1514
                switch (ni6->ni_code) {
1515
                case ICMP6_NI_SUBJ_IPV6:
1516
#if ICMP6_NI_SUBJ_IPV6 != 0
1517
                case 0:
1518
#endif
1519
                        /*
1520
                         * backward compatibility - try to accept 03 draft
1521
                         * format, where no Subject is present.
1522
                         */
1523
                        if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1524
                            subjlen == 0) {
1525
                                oldfqdn++;
1526
                                break;
1527
                        }
1528
#if ICMP6_NI_SUBJ_IPV6 != 0
1529
                        if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1530
                                goto bad;
1531
#endif
1532
 
1533
                        if (subjlen != sizeof(sin6.sin6_addr))
1534
                                goto bad;
1535
 
1536
                        /*
1537
                         * Validate Subject address.
1538
                         *
1539
                         * Not sure what exactly "address belongs to the node"
1540
                         * means in the spec, is it just unicast, or what?
1541
                         *
1542
                         * At this moment we consider Subject address as
1543
                         * "belong to the node" if the Subject address equals
1544
                         * to the IPv6 destination address; validation for
1545
                         * IPv6 destination address should have done enough
1546
                         * check for us.
1547
                         *
1548
                         * We do not do proxy at this moment.
1549
                         */
1550
                        /* m_pulldown instead of copy? */
1551
                        m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1552
                            subjlen, (caddr_t)&sin6.sin6_addr);
1553
                        if ((zoneid = in6_addr2zoneid(m->m_pkthdr.rcvif,
1554
                                                      &sin6.sin6_addr)) < 0)
1555
                                goto bad;
1556
                        sin6.sin6_scope_id = zoneid;
1557
 
1558
                        if (in6_embedscope(&sin6.sin6_addr, &sin6))
1559
                                goto bad; /* XXX should not happen */
1560
                        bzero(&sin6_d, sizeof(sin6_d));
1561
                        sin6_d.sin6_family = AF_INET6; /* not used, actually */
1562
                        sin6_d.sin6_len = sizeof(sin6_d); /* ditto */
1563
                        sin6_d.sin6_addr = ip6->ip6_dst;
1564
                        if ((zoneid = in6_addr2zoneid(m->m_pkthdr.rcvif,
1565
                                                      &ip6->ip6_dst)) < 0)
1566
                                goto bad;
1567
                        sin6_d.sin6_scope_id = zoneid;
1568
                        if (in6_embedscope(&sin6_d.sin6_addr, &sin6_d))
1569
                                goto bad; /* XXX should not happen */
1570
                        subj = (char *)&sin6;
1571
                        if (SA6_ARE_ADDR_EQUAL(&sin6, &sin6_d))
1572
                                break;
1573
 
1574
                        /*
1575
                         * XXX if we are to allow other cases, we should really
1576
                         * be careful about scope here.
1577
                         * basically, we should disallow queries toward IPv6
1578
                         * destination X with subject Y, if scope(X) > scope(Y).
1579
                         * if we allow scope(X) > scope(Y), it will result in
1580
                         * information leakage across scope boundary.
1581
                         */
1582
                        goto bad;
1583
 
1584
                case ICMP6_NI_SUBJ_FQDN:
1585
                        /*
1586
                         * Validate Subject name with gethostname(3).
1587
                         *
1588
                         * The behavior may need some debate, since:
1589
                         * - we are not sure if the node has FQDN as
1590
                         *   hostname (returned by gethostname(3)).
1591
                         * - the code does wildcard match for truncated names.
1592
                         *   however, we are not sure if we want to perform
1593
                         *   wildcard match, if gethostname(3) side has
1594
                         *   truncated hostname.
1595
                         */
1596
                        n = ni6_nametodns(hostname, hostnamelen, 0);
1597
                        if (!n || n->m_next || n->m_len == 0)
1598
                                goto bad;
1599
                        IP6_EXTHDR_GET(subj, char *, m,
1600
                            off + sizeof(struct icmp6_nodeinfo), subjlen);
1601
                        if (subj == NULL)
1602
                                goto bad;
1603
                        if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1604
                                        n->m_len)) {
1605
                                goto bad;
1606
                        }
1607
                        m_freem(n);
1608
                        n = NULL;
1609
                        break;
1610
 
1611
                case ICMP6_NI_SUBJ_IPV4:        /* XXX: to be implemented? */
1612
                default:
1613
                        goto bad;
1614
                }
1615
                break;
1616
        }
1617
 
1618
        /* refuse based on configuration.  XXX ICMP6_NI_REFUSED? */
1619
        switch (qtype) {
1620
        case NI_QTYPE_FQDN:
1621
                if ((icmp6_nodeinfo & 1) == 0)
1622
                        goto bad;
1623
                break;
1624
        case NI_QTYPE_NODEADDR:
1625
        case NI_QTYPE_IPV4ADDR:
1626
                if ((icmp6_nodeinfo & 2) == 0)
1627
                        goto bad;
1628
                break;
1629
        }
1630
 
1631
        /* guess reply length */
1632
        switch (qtype) {
1633
        case NI_QTYPE_NOOP:
1634
                break;          /* no reply data */
1635
        case NI_QTYPE_SUPTYPES:
1636
                replylen += sizeof(u_int32_t);
1637
                break;
1638
        case NI_QTYPE_FQDN:
1639
                /* XXX will append an mbuf */
1640
                replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1641
                break;
1642
        case NI_QTYPE_NODEADDR:
1643
                addrs = ni6_addrs(ni6, m, &ifp, subj);
1644
                if ((replylen += addrs * (sizeof(struct in6_addr) +
1645
                                          sizeof(u_int32_t))) > MCLBYTES)
1646
                        replylen = MCLBYTES; /* XXX: will truncate pkt later */
1647
                break;
1648
        case NI_QTYPE_IPV4ADDR:
1649
                /* unsupported - should respond with unknown Qtype? */
1650
                goto bad;
1651
        default:
1652
                /*
1653
                 * XXX: We must return a reply with the ICMP6 code
1654
                 * `unknown Qtype' in this case.  However we regard the case
1655
                 * as an FQDN query for backward compatibility.
1656
                 * Older versions set a random value to this field,
1657
                 * so it rarely varies in the defined qtypes.
1658
                 * But the mechanism is not reliable...
1659
                 * maybe we should obsolete older versions.
1660
                 */
1661
                qtype = NI_QTYPE_FQDN;
1662
                /* XXX will append an mbuf */
1663
                replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1664
                oldfqdn++;
1665
                break;
1666
        }
1667
 
1668
        /* allocate an mbuf to reply. */
1669
        MGETHDR(n, M_DONTWAIT, m->m_type);
1670
        if (n == NULL) {
1671
                m_freem(m);
1672
                return(NULL);
1673
        }
1674
#ifdef __OpenBSD__
1675
        M_DUP_PKTHDR(n, m); /* just for rcvif */
1676
#else
1677
        M_COPY_PKTHDR(n, m); /* just for rcvif */
1678
#endif
1679
        if (replylen > MHLEN) {
1680
                if (replylen > MCLBYTES) {
1681
                        /*
1682
                         * XXX: should we try to allocate more? But MCLBYTES
1683
                         * is probably much larger than IPV6_MMTU...
1684
                         */
1685
                        goto bad;
1686
                }
1687
                MCLGET(n, M_DONTWAIT);
1688
                if ((n->m_flags & M_EXT) == 0) {
1689
                        goto bad;
1690
                }
1691
        }
1692
        n->m_pkthdr.len = n->m_len = replylen;
1693
 
1694
        /* copy mbuf header and IPv6 + Node Information base headers */
1695
        bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1696
        nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1697
        bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1698
 
1699
        /* qtype dependent procedure */
1700
        switch (qtype) {
1701
        case NI_QTYPE_NOOP:
1702
                nni6->ni_code = ICMP6_NI_SUCCESS;
1703
                nni6->ni_flags = 0;
1704
                break;
1705
        case NI_QTYPE_SUPTYPES:
1706
        {
1707
                u_int32_t v;
1708
                nni6->ni_code = ICMP6_NI_SUCCESS;
1709
                nni6->ni_flags = htons(0x0000); /* raw bitmap */
1710
                /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1711
                v = (u_int32_t)htonl(0x0000000f);
1712
                bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1713
                break;
1714
        }
1715
        case NI_QTYPE_FQDN:
1716
                nni6->ni_code = ICMP6_NI_SUCCESS;
1717
                fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1718
                                                sizeof(struct ip6_hdr) +
1719
                                                sizeof(struct icmp6_nodeinfo));
1720
                nni6->ni_flags = 0; /* XXX: meaningless TTL */
1721
                fqdn->ni_fqdn_ttl = 0;   /* ditto. */
1722
                /*
1723
                 * XXX do we really have FQDN in variable "hostname"?
1724
                 */
1725
                n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
1726
                if (n->m_next == NULL)
1727
                        goto bad;
1728
                /* XXX we assume that n->m_next is not a chain */
1729
                if (n->m_next->m_next != NULL)
1730
                        goto bad;
1731
                n->m_pkthdr.len += n->m_next->m_len;
1732
                break;
1733
        case NI_QTYPE_NODEADDR:
1734
        {
1735
                int lenlim, copied;
1736
 
1737
                nni6->ni_code = ICMP6_NI_SUCCESS;
1738
                n->m_pkthdr.len = n->m_len =
1739
                    sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1740
                lenlim = M_TRAILINGSPACE(n);
1741
                copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1742
                /* XXX: reset mbuf length */
1743
                n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1744
                        sizeof(struct icmp6_nodeinfo) + copied;
1745
                break;
1746
        }
1747
        default:
1748
                break;          /* XXX impossible! */
1749
        }
1750
 
1751
        nni6->ni_type = ICMP6_NI_REPLY;
1752
        m_freem(m);
1753
        return(n);
1754
 
1755
  bad:
1756
        m_freem(m);
1757
        if (n)
1758
                m_freem(n);
1759
        return(NULL);
1760
}
1761
#undef hostnamelen
1762
 
1763
/*
1764
 * make a mbuf with DNS-encoded string.  no compression support.
1765
 *
1766
 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1767
 * treated as truncated name (two \0 at the end).  this is a wild guess.
1768
 */
1769
static struct mbuf *
1770
ni6_nametodns(name, namelen, old)
1771
        const char *name;
1772
        int namelen;
1773
        int old;        /* return pascal string if non-zero */
1774
{
1775
        struct mbuf *m;
1776
        char *cp, *ep;
1777
        const char *p, *q;
1778
        int i, len, nterm;
1779
 
1780
        if (old)
1781
                len = namelen + 1;
1782
        else
1783
                len = MCLBYTES;
1784
 
1785
        /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1786
        MGET(m, M_DONTWAIT, MT_DATA);
1787
        if (m && len > MLEN) {
1788
                MCLGET(m, M_DONTWAIT);
1789
                if ((m->m_flags & M_EXT) == 0)
1790
                        goto fail;
1791
        }
1792
        if (!m)
1793
                goto fail;
1794
        m->m_next = NULL;
1795
 
1796
        if (old) {
1797
                m->m_len = len;
1798
                *mtod(m, char *) = namelen;
1799
                bcopy(name, mtod(m, char *) + 1, namelen);
1800
                return m;
1801
        } else {
1802
                m->m_len = 0;
1803
                cp = mtod(m, char *);
1804
                ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1805
 
1806
                /* if not certain about my name, return empty buffer */
1807
                if (namelen == 0)
1808
                        return m;
1809
 
1810
                /*
1811
                 * guess if it looks like shortened hostname, or FQDN.
1812
                 * shortened hostname needs two trailing "\0".
1813
                 */
1814
                i = 0;
1815
                for (p = name; p < name + namelen; p++) {
1816
                        if (*p && *p == '.')
1817
                                i++;
1818
                }
1819
                if (i < 2)
1820
                        nterm = 2;
1821
                else
1822
                        nterm = 1;
1823
 
1824
                p = name;
1825
                while (cp < ep && p < name + namelen) {
1826
                        i = 0;
1827
                        for (q = p; q < name + namelen && *q && *q != '.'; q++)
1828
                                i++;
1829
                        /* result does not fit into mbuf */
1830
                        if (cp + i + 1 >= ep)
1831
                                goto fail;
1832
                        /*
1833
                         * DNS label length restriction, RFC1035 page 8.
1834
                         * "i == 0" case is included here to avoid returning
1835
                         * 0-length label on "foo..bar".
1836
                         */
1837
                        if (i <= 0 || i >= 64)
1838
                                goto fail;
1839
                        *cp++ = i;
1840
                        bcopy(p, cp, i);
1841
                        cp += i;
1842
                        p = q;
1843
                        if (p < name + namelen && *p == '.')
1844
                                p++;
1845
                }
1846
                /* termination */
1847
                if (cp + nterm >= ep)
1848
                        goto fail;
1849
                while (nterm-- > 0)
1850
                        *cp++ = '\0';
1851
                m->m_len = cp - mtod(m, char *);
1852
                return m;
1853
        }
1854
 
1855
        panic("should not reach here");
1856
        /* NOTREACHED */
1857
 
1858
 fail:
1859
        if (m)
1860
                m_freem(m);
1861
        return NULL;
1862
}
1863
 
1864
/*
1865
 * check if two DNS-encoded string matches.  takes care of truncated
1866
 * form (with \0\0 at the end).  no compression support.
1867
 * XXX upper/lowercase match (see RFC2065)
1868
 */
1869
static int
1870
ni6_dnsmatch(a, alen, b, blen)
1871
        const char *a;
1872
        int alen;
1873
        const char *b;
1874
        int blen;
1875
{
1876
        const char *a0, *b0;
1877
        int l;
1878
 
1879
        /* simplest case - need validation? */
1880
        if (alen == blen && bcmp(a, b, alen) == 0)
1881
                return 1;
1882
 
1883
        a0 = a;
1884
        b0 = b;
1885
 
1886
        /* termination is mandatory */
1887
        if (alen < 2 || blen < 2)
1888
                return 0;
1889
        if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1890
                return 0;
1891
        alen--;
1892
        blen--;
1893
 
1894
        while (a - a0 < alen && b - b0 < blen) {
1895
                if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1896
                        return 0;
1897
 
1898
                if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1899
                        return 0;
1900
                /* we don't support compression yet */
1901
                if (a[0] >= 64 || b[0] >= 64)
1902
                        return 0;
1903
 
1904
                /* truncated case */
1905
                if (a[0] == 0 && a - a0 == alen - 1)
1906
                        return 1;
1907
                if (b[0] == 0 && b - b0 == blen - 1)
1908
                        return 1;
1909
                if (a[0] == 0 || b[0] == 0)
1910
                        return 0;
1911
 
1912
                if (a[0] != b[0])
1913
                        return 0;
1914
                l = a[0];
1915
                if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1916
                        return 0;
1917
                if (bcmp(a + 1, b + 1, l) != 0)
1918
                        return 0;
1919
 
1920
                a += 1 + l;
1921
                b += 1 + l;
1922
        }
1923
 
1924
        if (a - a0 == alen && b - b0 == blen)
1925
                return 1;
1926
        else
1927
                return 0;
1928
}
1929
 
1930
/*
1931
 * calculate the number of addresses to be returned in the node info reply.
1932
 */
1933
static int
1934
ni6_addrs(ni6, m, ifpp, subj)
1935
        struct icmp6_nodeinfo *ni6;
1936
        struct mbuf *m;
1937
        struct ifnet **ifpp;
1938
        char *subj;
1939
{
1940
        struct ifnet *ifp;
1941
        struct in6_ifaddr *ifa6;
1942
        struct ifaddr *ifa;
1943
        struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */
1944
        int addrs = 0, addrsofif, iffound = 0;
1945
        int niflags = ni6->ni_flags;
1946
 
1947
        if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1948
                switch (ni6->ni_code) {
1949
                case ICMP6_NI_SUBJ_IPV6:
1950
                        if (subj == NULL) /* must be impossible... */
1951
                                return(0);
1952
                        subj_ip6 = (struct sockaddr_in6 *)subj;
1953
                        break;
1954
                default:
1955
                        /*
1956
                         * XXX: we only support IPv6 subject address for
1957
                         * this Qtype.
1958
                         */
1959
                        return(0);
1960
                }
1961
        }
1962
 
1963
#if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
1964
        for (ifp = ifnet; ifp; ifp = ifp->if_next)
1965
#else
1966
        for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1967
#endif
1968
        {
1969
                addrsofif = 0;
1970
#if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
1971
                for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
1972
#elif defined(__FreeBSD__) && __FreeBSD__ >= 4
1973
                TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1974
#else
1975
                for (ifa = ifp->if_addrlist.tqh_first; ifa;
1976
                     ifa = ifa->ifa_list.tqe_next)
1977
#endif
1978
                {
1979
                        if (ifa->ifa_addr->sa_family != AF_INET6)
1980
                                continue;
1981
                        ifa6 = (struct in6_ifaddr *)ifa;
1982
 
1983
                        if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1984
                            IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr,
1985
                                               &ifa6->ia_addr.sin6_addr))
1986
                                iffound = 1;
1987
 
1988
                        /*
1989
                         * IPv4-mapped addresses can only be returned by a
1990
                         * Node Information proxy, since they represent
1991
                         * addresses of IPv4-only nodes, which perforce do
1992
                         * not implement this protocol.
1993
                         * [icmp-name-lookups-07, Section 5.4]
1994
                         * So we don't support NI_NODEADDR_FLAG_COMPAT in
1995
                         * this function at this moment.
1996
                         */
1997
 
1998
                        /* What do we have to do about ::1? */
1999
                        switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
2000
                        case IPV6_ADDR_SCOPE_LINKLOCAL:
2001
                                if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
2002
                                        continue;
2003
                                break;
2004
                        case IPV6_ADDR_SCOPE_SITELOCAL:
2005
                                if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
2006
                                        continue;
2007
                                break;
2008
                        case IPV6_ADDR_SCOPE_GLOBAL:
2009
                                if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
2010
                                        continue;
2011
                                break;
2012
                        default:
2013
                                continue;
2014
                        }
2015
 
2016
                        /*
2017
                         * check if anycast is okay.
2018
                         * XXX: just experimental.  not in the spec.
2019
                         */
2020
                        if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
2021
                            (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
2022
                                continue; /* we need only unicast addresses */
2023
                        if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
2024
                            (icmp6_nodeinfo & 4) == 0) {
2025
                                continue;
2026
                        }
2027
                        addrsofif++; /* count the address */
2028
                }
2029
                if (iffound) {
2030
                        *ifpp = ifp;
2031
                        return(addrsofif);
2032
                }
2033
 
2034
                addrs += addrsofif;
2035
        }
2036
 
2037
        return(addrs);
2038
}
2039
 
2040
static int
2041
ni6_store_addrs(ni6, nni6, ifp0, resid)
2042
        struct icmp6_nodeinfo *ni6, *nni6;
2043
        struct ifnet *ifp0;
2044
        int resid;
2045
{
2046
#if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
2047
        struct ifnet *ifp = ifp0 ? ifp0 : ifnet;
2048
#else
2049
        struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
2050
#endif
2051
        struct in6_ifaddr *ifa6;
2052
        struct ifaddr *ifa;
2053
        struct ifnet *ifp_dep = NULL;
2054
        int copied = 0, allow_deprecated = 0;
2055
        u_char *cp = (u_char *)(nni6 + 1);
2056
        int niflags = ni6->ni_flags;
2057
        u_int32_t ltime;
2058
#if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
2059
        long time_second = time.tv_sec;
2060
#endif
2061
 
2062
        if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
2063
                return(0);       /* needless to copy */
2064
 
2065
  again:
2066
 
2067
#if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
2068
        for (; ifp; ifp = ifp->if_next)
2069
#else
2070
        for (; ifp; ifp = TAILQ_NEXT(ifp, if_list))
2071
#endif
2072
        {
2073
#if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
2074
                for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
2075
#else
2076
                for (ifa = ifp->if_addrlist.tqh_first; ifa;
2077
                     ifa = ifa->ifa_list.tqe_next)
2078
#endif
2079
                {
2080
                        if (ifa->ifa_addr->sa_family != AF_INET6)
2081
                                continue;
2082
                        ifa6 = (struct in6_ifaddr *)ifa;
2083
 
2084
                        if (IFA6_IS_DEPRECATED(ifa6) &&
2085
                            allow_deprecated == 0) {
2086
                                /*
2087
                                 * prefererred address should be put before
2088
                                 * deprecated addresses.
2089
                                 */
2090
 
2091
                                /* record the interface for later search */
2092
                                if (ifp_dep == NULL)
2093
                                        ifp_dep = ifp;
2094
 
2095
                                continue;
2096
                        }
2097
                        else if (!IFA6_IS_DEPRECATED(ifa6) &&
2098
                                 allow_deprecated != 0)
2099
                                continue; /* we now collect deprecated addrs */
2100
 
2101
                        /* What do we have to do about ::1? */
2102
                        switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
2103
                        case IPV6_ADDR_SCOPE_LINKLOCAL:
2104
                                if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
2105
                                        continue;
2106
                                break;
2107
                        case IPV6_ADDR_SCOPE_SITELOCAL:
2108
                                if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
2109
                                        continue;
2110
                                break;
2111
                        case IPV6_ADDR_SCOPE_GLOBAL:
2112
                                if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
2113
                                        continue;
2114
                                break;
2115
                        default:
2116
                                continue;
2117
                        }
2118
 
2119
                        /*
2120
                         * check if anycast is okay.
2121
                         * XXX: just experimental.  not in the spec.
2122
                         */
2123
                        if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
2124
                            (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
2125
                                continue;
2126
                        if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
2127
                            (icmp6_nodeinfo & 4) == 0) {
2128
                                continue;
2129
                        }
2130
 
2131
                        /* now we can copy the address */
2132
                        if (resid < sizeof(struct in6_addr) +
2133
                            sizeof(u_int32_t)) {
2134
                                /*
2135
                                 * We give up much more copy.
2136
                                 * Set the truncate flag and return.
2137
                                 */
2138
                                nni6->ni_flags |=
2139
                                        NI_NODEADDR_FLAG_TRUNCATE;
2140
                                return(copied);
2141
                        }
2142
 
2143
                        /*
2144
                         * Set the TTL of the address.
2145
                         * The TTL value should be one of the following
2146
                         * according to the specification:
2147
                         *
2148
                         * 1. The remaining lifetime of a DHCP lease on the
2149
                         *    address, or
2150
                         * 2. The remaining Valid Lifetime of a prefix from
2151
                         *    which the address was derived through Stateless
2152
                         *    Autoconfiguration.
2153
                         *
2154
                         * Note that we currently do not support stateful
2155
                         * address configuration by DHCPv6, so the former
2156
                         * case can't happen.
2157
                         *
2158
                         * TTL must be 2^31 > TTL >= 0.
2159
                         */
2160
                        if (ifa6->ia6_lifetime.ia6t_expire == 0)
2161
                                ltime = ND6_INFINITE_LIFETIME;
2162
                        else {
2163
                                if (ifa6->ia6_lifetime.ia6t_expire >
2164
                                    time_second) {
2165
                                        ltime = ifa6->ia6_lifetime.ia6t_expire
2166
                                                - time_second;
2167
                                } else
2168
                                        ltime = 0;
2169
                        }
2170
                        if (ltime > 0x7fffffff)
2171
                                ltime = 0x7fffffff;
2172
                        ltime = htonl(ltime);
2173
 
2174
                        bcopy(&ltime, cp, sizeof(u_int32_t));
2175
                        cp += sizeof(u_int32_t);
2176
 
2177
                        /* copy the address itself */
2178
                        bcopy(&ifa6->ia_addr.sin6_addr, cp,
2179
                              sizeof(struct in6_addr));
2180
                        /* XXX: KAME link-local hack; remove ifindex */
2181
                        if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr))
2182
                                ((struct in6_addr *)cp)->s6_addr16[1] = 0;
2183
                        cp += sizeof(struct in6_addr);
2184
 
2185
                        resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
2186
                        copied += (sizeof(struct in6_addr) +
2187
                                   sizeof(u_int32_t));
2188
                }
2189
                if (ifp0)       /* we need search only on the specified IF */
2190
                        break;
2191
        }
2192
 
2193
        if (allow_deprecated == 0 && ifp_dep != NULL) {
2194
                ifp = ifp_dep;
2195
                allow_deprecated = 1;
2196
 
2197
                goto again;
2198
        }
2199
 
2200
        return(copied);
2201
}
2202
 
2203
/*
2204
 * XXX almost dup'ed code with rip6_input.
2205
 */
2206
static int
2207
icmp6_rip6_input(mp, off)
2208
        struct  mbuf **mp;
2209
        int     off;
2210
{
2211
        struct mbuf *m = *mp;
2212
        struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2213
        struct in6pcb *in6p;
2214
        struct in6pcb *last = NULL;
2215
        struct sockaddr_in6 rip6src;
2216
        struct icmp6_hdr *icmp6;
2217
        struct ip6_recvpktopts opts;
2218
 
2219
#ifndef PULLDOWN_TEST
2220
        /* this is assumed to be safe. */
2221
        icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
2222
#else
2223
        IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
2224
        if (icmp6 == NULL) {
2225
                /* m is already reclaimed */
2226
                return IPPROTO_DONE;
2227
        }
2228
#endif
2229
 
2230
        bzero(&opts, sizeof(opts));
2231
        bzero(&rip6src, sizeof(rip6src));
2232
        rip6src.sin6_len = sizeof(struct sockaddr_in6);
2233
        rip6src.sin6_family = AF_INET6;
2234
        /* KAME hack: recover scopeid */
2235
        (void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif);
2236
 
2237
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
2238
        LIST_FOREACH(in6p, &ripcb, inp_list)
2239
#elif defined(__OpenBSD__)
2240
        for (in6p = rawin6pcbtable.inpt_queue.cqh_first;
2241
             in6p != (struct inpcb *)&rawin6pcbtable.inpt_queue;
2242
             in6p = in6p->inp_queue.cqe_next)
2243
#else
2244
        for (in6p = rawin6pcb.in6p_next;
2245
             in6p != &rawin6pcb; in6p = in6p->in6p_next)
2246
#endif
2247
        {
2248
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
2249
                if ((in6p->inp_vflag & INP_IPV6) == 0)
2250
                        continue;
2251
#endif
2252
#ifdef HAVE_NRL_INPCB
2253
                if (!(in6p->in6p_flags & INP_IPV6))
2254
                        continue;
2255
#endif
2256
                if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
2257
                        continue;
2258
                if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
2259
                   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
2260
                        continue;
2261
                if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
2262
                   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
2263
                        continue;
2264
                if (in6p->in6p_icmp6filt
2265
                    && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
2266
                                 in6p->in6p_icmp6filt))
2267
                        continue;
2268
                if (last) {
2269
                        struct  mbuf *n;
2270
                        if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
2271
                                if (last->in6p_flags & IN6P_CONTROLOPTS)
2272
                                        ip6_savecontrol(last, ip6, n, &opts,
2273
                                                        NULL);
2274
                                /* strip intermediate headers */
2275
                                m_adj(n, off);
2276
                                if (sbappendaddr(&last->in6p_socket->so_rcv,
2277
                                                 (struct sockaddr *)&rip6src,
2278
                                                 n, opts.head) == 0) {
2279
                                        /* should notify about lost packet */
2280
                                        m_freem(n);
2281
                                        if (opts.head) {
2282
                                                m_freem(opts.head);
2283
                                        }
2284
                                } else
2285
                                        sorwakeup(last->in6p_socket);
2286
                                bzero(&opts, sizeof(opts));
2287
                        }
2288
                }
2289
                last = in6p;
2290
        }
2291
        if (last) {
2292
                if (last->in6p_flags & IN6P_CONTROLOPTS)
2293
                        ip6_savecontrol(last, ip6, m, &opts, NULL);
2294
                /* strip intermediate headers */
2295
                m_adj(m, off);
2296
                if (sbappendaddr(&last->in6p_socket->so_rcv,
2297
                                 (struct sockaddr *)&rip6src,
2298
                                 m, opts.head) == 0) {
2299
                        m_freem(m);
2300
                        if (opts.head)
2301
                                m_freem(opts.head);
2302
                } else
2303
                        sorwakeup(last->in6p_socket);
2304
        } else {
2305
                m_freem(m);
2306
                ip6stat.ip6s_delivered--;
2307
        }
2308
        return IPPROTO_DONE;
2309
}
2310
 
2311
/*
2312
 * Reflect the ip6 packet back to the source.
2313
 * OFF points to the icmp6 header, counted from the top of the mbuf.
2314
 *
2315
 * Note: RFC 1885 required that an echo reply should be truncated if it
2316
 * did not fit in with (return) path MTU, and KAME code supported the
2317
 * behavior.  However, as a clarification after the RFC, this limitation
2318
 * was removed in a revised version of the spec, RFC 2463.  We had kept the
2319
 * old behavior, with a (non-default) ifdef block, while the new version of
2320
 * the spec was an internet-draft status, and even after the new RFC was
2321
 * published.  But it would rather make sense to clean the obsoleted part
2322
 * up, and to make the code simpler at this stage.
2323
 */
2324
void
2325
icmp6_reflect(m, off)
2326
        struct  mbuf *m;
2327
        size_t off;
2328
{
2329
        struct ip6_hdr *ip6;
2330
        struct icmp6_hdr *icmp6;
2331
        struct in6_ifaddr *ia;
2332
        struct in6_addr t, *src = 0;
2333
        int plen;
2334
        int type, code;
2335
        struct ifnet *outif = NULL;
2336
        struct sockaddr_in6 sa6_src, sa6_dst;
2337
 
2338
        /* too short to reflect */
2339
        if (off < sizeof(struct ip6_hdr)) {
2340
                nd6log((LOG_DEBUG,
2341
                    "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2342
                    (u_long)off, (u_long)sizeof(struct ip6_hdr),
2343
                    __FILE__, __LINE__));
2344
                goto bad;
2345
        }
2346
 
2347
        /*
2348
         * If there are extra headers between IPv6 and ICMPv6, strip
2349
         * off that header first.
2350
         */
2351
#ifdef DIAGNOSTIC
2352
        if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
2353
                panic("assumption failed in icmp6_reflect");
2354
#endif
2355
        if (off > sizeof(struct ip6_hdr)) {
2356
                size_t l;
2357
                struct ip6_hdr nip6;
2358
 
2359
                l = off - sizeof(struct ip6_hdr);
2360
                m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2361
                m_adj(m, l);
2362
                l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2363
                if (m->m_len < l) {
2364
                        if ((m = m_pullup(m, l)) == NULL)
2365
                                return;
2366
                }
2367
                bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2368
        } else /* off == sizeof(struct ip6_hdr) */ {
2369
                size_t l;
2370
                l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2371
                if (m->m_len < l) {
2372
                        if ((m = m_pullup(m, l)) == NULL)
2373
                                return;
2374
                }
2375
        }
2376
        plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2377
        ip6 = mtod(m, struct ip6_hdr *);
2378
        ip6->ip6_nxt = IPPROTO_ICMPV6;
2379
        icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2380
        type = icmp6->icmp6_type; /* keep type for statistics */
2381
        code = icmp6->icmp6_code; /* ditto. */
2382
 
2383
        t = ip6->ip6_dst;
2384
        /*
2385
         * ip6_input() drops a packet if its src is multicast.
2386
         * So, the src is never multicast.
2387
         */
2388
        ip6->ip6_dst = ip6->ip6_src;
2389
 
2390
        /*
2391
         * XXX: make sure to embed scope zone information, using
2392
         * already embedded IDs or the received interface (if any).
2393
         * Note that rcvif may be NULL.
2394
         * TODO: scoped routing case (XXX).
2395
         */
2396
        bzero(&sa6_src, sizeof(sa6_src));
2397
        sa6_src.sin6_family = AF_INET6;
2398
        sa6_src.sin6_len = sizeof(sa6_src);
2399
        sa6_src.sin6_addr = ip6->ip6_dst;
2400
        in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif);
2401
        in6_embedscope(&sa6_src.sin6_addr, &sa6_src);
2402
        ip6->ip6_dst = sa6_src.sin6_addr;
2403
 
2404
        bzero(&sa6_dst, sizeof(sa6_dst));
2405
        sa6_dst.sin6_family = AF_INET6;
2406
        sa6_dst.sin6_len = sizeof(sa6_dst);
2407
        sa6_dst.sin6_addr = t;
2408
        in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif);
2409
        in6_embedscope(&t, &sa6_dst);
2410
 
2411
        /*
2412
         * If the incoming packet was addressed directly to us (i.e. unicast),
2413
         * use dst as the src for the reply.
2414
         * The IN6_IFF_NOTREADY case would be VERY rare, but is possible
2415
         * (for example) when we encounter an error while forwarding procedure
2416
         * destined to a duplicated address of ours.
2417
         */
2418
        for (ia = in6_ifaddr; ia; ia = ia->ia_next)
2419
                if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) &&
2420
                    (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
2421
                        src = &t;
2422
                        break;
2423
                }
2424
        if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) {
2425
                /*
2426
                 * This is the case if the dst is our link-local address
2427
                 * and the sender is also ourselves.
2428
                 */
2429
                src = &t;
2430
        }
2431
 
2432
        if (src == 0) {
2433
                int e;
2434
#ifdef NEW_STRUCT_ROUTE
2435
                struct route ro;
2436
#else
2437
                struct route_in6 ro;
2438
#endif
2439
 
2440
                /*
2441
                 * This case matches to multicasts, our anycast, or unicasts
2442
                 * that we do not own.  Select a source address based on the
2443
                 * source address of the erroneous packet.
2444
                 */
2445
                bzero(&ro, sizeof(ro));
2446
                src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, NULL, &e);
2447
                if (ro.ro_rt) { /* XXX: see comments in icmp6_mtudisc_update */
2448
                        RTFREE(ro.ro_rt); /* XXX: we could use this */
2449
                }
2450
                if (src == NULL) {
2451
                        nd6log((LOG_DEBUG,
2452
                            "icmp6_reflect: source can't be determined: "
2453
                            "dst=%s, error=%d\n",
2454
                            ip6_sprintf(&sa6_src.sin6_addr), e));
2455
                        goto bad;
2456
                }
2457
        }
2458
 
2459
        ip6->ip6_src = *src;
2460
 
2461
        ip6->ip6_flow = 0;
2462
        ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2463
        ip6->ip6_vfc |= IPV6_VERSION;
2464
        ip6->ip6_nxt = IPPROTO_ICMPV6;
2465
        if (m->m_pkthdr.rcvif) {
2466
                /* XXX: This may not be the outgoing interface */
2467
                ip6->ip6_hlim = nd_ifinfo[m->m_pkthdr.rcvif->if_index].chlim;
2468
        } else
2469
                ip6->ip6_hlim = ip6_defhlim;
2470
 
2471
        icmp6->icmp6_cksum = 0;
2472
        icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2473
                                        sizeof(struct ip6_hdr), plen);
2474
 
2475
        /*
2476
         * XXX option handling
2477
         */
2478
 
2479
        m->m_flags &= ~(M_BCAST|M_MCAST);
2480
#ifdef IPSEC
2481
        /* Don't lookup socket */
2482
        (void)ipsec_setsocket(m, NULL);
2483
#endif /* IPSEC */
2484
 
2485
        /*
2486
         * To avoid a "too big" situation at an intermediate router
2487
         * and the path MTU discovery process, specify the IPV6_MINMTU flag.
2488
         * Note that only echo and node information replies are affected,
2489
         * since the length of ICMP6 errors is limited to the minimum MTU.
2490
         */
2491
        if (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, &outif) != 0 && outif)
2492
                icmp6_ifstat_inc(outif, ifs6_out_error);
2493
 
2494
        if (outif)
2495
                icmp6_ifoutstat_inc(outif, type, code);
2496
 
2497
        return;
2498
 
2499
 bad:
2500
        m_freem(m);
2501
        return;
2502
}
2503
 
2504
void
2505
icmp6_fasttimo()
2506
{
2507
 
2508
        mld6_fasttimeo();
2509
}
2510
 
2511
static const char *
2512
icmp6_redirect_diag(src6, dst6, tgt6)
2513
        struct in6_addr *src6;
2514
        struct in6_addr *dst6;
2515
        struct in6_addr *tgt6;
2516
{
2517
        static char buf[1024];
2518
#ifndef __bsdi__
2519
        snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2520
                ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
2521
#else
2522
        sprintf(buf, "(src=%s dst=%s tgt=%s)",
2523
                ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
2524
#endif
2525
        return buf;
2526
}
2527
 
2528
void
2529
icmp6_redirect_input(m, off)
2530
        struct mbuf *m;
2531
        int off;
2532
{
2533
        struct ifnet *ifp = m->m_pkthdr.rcvif;
2534
        struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2535
        struct nd_redirect *nd_rd;
2536
        int icmp6len = ntohs(ip6->ip6_plen);
2537
        char *lladdr = NULL;
2538
        int lladdrlen = 0;
2539
        struct rtentry *rt = NULL;
2540
        int is_router;
2541
        int is_onlink;
2542
        struct in6_addr src6 = ip6->ip6_src;
2543
        struct in6_addr redtgt6;
2544
        struct in6_addr reddst6;
2545
        union nd_opts ndopts;
2546
 
2547
        if (!m || !ifp)
2548
                return;
2549
 
2550
        /* XXX if we are router, we don't update route by icmp6 redirect */
2551
        if (ip6_forwarding)
2552
                goto freeit;
2553
        if (!icmp6_rediraccept)
2554
                goto freeit;
2555
 
2556
#ifndef PULLDOWN_TEST
2557
        IP6_EXTHDR_CHECK(m, off, icmp6len,);
2558
        nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2559
#else
2560
        IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2561
        if (nd_rd == NULL) {
2562
                icmp6stat.icp6s_tooshort++;
2563
                return;
2564
        }
2565
#endif
2566
        redtgt6 = nd_rd->nd_rd_target;
2567
        reddst6 = nd_rd->nd_rd_dst;
2568
 
2569
        if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2570
                redtgt6.s6_addr16[1] = htons(ifp->if_index);
2571
        if (IN6_IS_ADDR_LINKLOCAL(&reddst6))
2572
                reddst6.s6_addr16[1] = htons(ifp->if_index);
2573
 
2574
        /* validation */
2575
        if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2576
                nd6log((LOG_ERR,
2577
                        "ICMP6 redirect sent from %s rejected; "
2578
                        "must be from linklocal\n", ip6_sprintf(&src6)));
2579
                goto bad;
2580
        }
2581
        if (ip6->ip6_hlim != 255) {
2582
                nd6log((LOG_ERR,
2583
                        "ICMP6 redirect sent from %s rejected; "
2584
                        "hlim=%d (must be 255)\n",
2585
                        ip6_sprintf(&src6), ip6->ip6_hlim));
2586
                goto bad;
2587
        }
2588
    {
2589
        /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2590
        struct sockaddr_in6 sin6;
2591
        struct in6_addr *gw6;
2592
 
2593
        bzero(&sin6, sizeof(sin6));
2594
        sin6.sin6_family = AF_INET6;
2595
        sin6.sin6_len = sizeof(struct sockaddr_in6);
2596
        bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
2597
        rt = rtalloc1((struct sockaddr *)&sin6, 0
2598
#ifdef __FreeBSD__
2599
                      , 0UL
2600
#endif
2601
                      );
2602
        if (rt) {
2603
                if (rt->rt_gateway == NULL ||
2604
                    rt->rt_gateway->sa_family != AF_INET6) {
2605
                        nd6log((LOG_ERR,
2606
                            "ICMP6 redirect rejected; no route "
2607
                            "with inet6 gateway found for redirect dst: %s\n",
2608
                            icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2609
                        RTFREE(rt);
2610
                        goto bad;
2611
                }
2612
 
2613
                gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2614
                if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2615
                        nd6log((LOG_ERR,
2616
                                "ICMP6 redirect rejected; "
2617
                                "not equal to gw-for-src=%s (must be same): "
2618
                                "%s\n",
2619
                                ip6_sprintf(gw6),
2620
                                icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2621
                        RTFREE(rt);
2622
                        goto bad;
2623
                }
2624
        } else {
2625
                nd6log((LOG_ERR,
2626
                        "ICMP6 redirect rejected; "
2627
                        "no route found for redirect dst: %s\n",
2628
                        icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2629
                goto bad;
2630
        }
2631
        RTFREE(rt);
2632
        rt = NULL;
2633
    }
2634
        if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2635
                nd6log((LOG_ERR,
2636
                        "ICMP6 redirect rejected; "
2637
                        "redirect dst must be unicast: %s\n",
2638
                        icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2639
                goto bad;
2640
        }
2641
 
2642
        is_router = is_onlink = 0;
2643
        if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2644
                is_router = 1;  /* router case */
2645
        if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2646
                is_onlink = 1;  /* on-link destination case */
2647
        if (!is_router && !is_onlink) {
2648
                nd6log((LOG_ERR,
2649
                        "ICMP6 redirect rejected; "
2650
                        "neither router case nor onlink case: %s\n",
2651
                        icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2652
                goto bad;
2653
        }
2654
        /* validation passed */
2655
 
2656
        icmp6len -= sizeof(*nd_rd);
2657
        nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2658
        if (nd6_options(&ndopts) < 0) {
2659
                nd6log((LOG_INFO, "icmp6_redirect_input: "
2660
                        "invalid ND option, rejected: %s\n",
2661
                        icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2662
                /* nd6_options have incremented stats */
2663
                goto freeit;
2664
        }
2665
 
2666
        if (ndopts.nd_opts_tgt_lladdr) {
2667
                lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2668
                lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2669
        }
2670
 
2671
        if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2672
                nd6log((LOG_INFO,
2673
                        "icmp6_redirect_input: lladdrlen mismatch for %s "
2674
                        "(if %d, icmp6 packet %d): %s\n",
2675
                        ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2676
                        icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2677
                goto bad;
2678
        }
2679
 
2680
        /* RFC 2461 8.3 */
2681
        nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2682
                         is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2683
 
2684
        if (!is_onlink) {       /* better router case.  perform rtredirect. */
2685
                /* perform rtredirect */
2686
                struct sockaddr_in6 sdst;
2687
                struct sockaddr_in6 sgw;
2688
                struct sockaddr_in6 ssrc;
2689
#ifdef __bsdi__
2690
                extern int icmp_redirtimeout;   /* XXX */
2691
#endif
2692
#if defined(__NetBSD__) || defined(__OpenBSD__)
2693
                unsigned long rtcount;
2694
                struct rtentry *newrt = NULL;
2695
#endif
2696
 
2697
#if defined(__NetBSD__) || defined(__OpenBSD__)
2698
                /*
2699
                 * do not install redirect route, if the number of entries
2700
                 * is too much (> hiwat).  note that, the node (= host) will
2701
                 * work just fine even if we do not install redirect route
2702
                 * (there will be additional hops, though).
2703
                 */
2704
                rtcount = rt_timer_count(icmp6_redirect_timeout_q);
2705
                if (0 <= icmp6_redirect_hiwat && rtcount > icmp6_redirect_hiwat)
2706
                        return;
2707
                else if (0 <= icmp6_redirect_lowat &&
2708
                    rtcount > icmp6_redirect_lowat) {
2709
                        /*
2710
                         * XXX nuke a victim, install the new one.
2711
                         */
2712
                }
2713
#endif
2714
 
2715
                bzero(&sdst, sizeof(sdst));
2716
                bzero(&sgw, sizeof(sgw));
2717
                bzero(&ssrc, sizeof(ssrc));
2718
                sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2719
                sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2720
                        sizeof(struct sockaddr_in6);
2721
                bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2722
                bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2723
                bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2724
                rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2725
                           (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2726
                           (struct sockaddr *)&ssrc,
2727
#ifdef __bsdi__
2728
                           icmp_redirtimeout
2729
#elif defined(__NetBSD__) || defined(__OpenBSD__)
2730
                           &newrt
2731
#else
2732
                           (struct rtentry **)NULL
2733
#endif
2734
                           );
2735
 
2736
#if defined(__NetBSD__) || defined(__OpenBSD__)
2737
                if (newrt) {
2738
                        (void)rt_timer_add(newrt, icmp6_redirect_timeout,
2739
                            icmp6_redirect_timeout_q);
2740
                        rtfree(newrt);
2741
                }
2742
#endif
2743
        }
2744
        /* finally update cached route in each socket via pfctlinput */
2745
        {
2746
                struct sockaddr_in6 sdst;
2747
 
2748
                bzero(&sdst, sizeof(sdst));
2749
                sdst.sin6_family = AF_INET6;
2750
                sdst.sin6_len = sizeof(struct sockaddr_in6);
2751
                bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2752
                pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2753
#ifdef IPSEC
2754
                key_sa_routechange((struct sockaddr *)&sdst);
2755
#endif
2756
        }
2757
 
2758
 freeit:
2759
        m_freem(m);
2760
        return;
2761
 
2762
 bad:
2763
        icmp6stat.icp6s_badredirect++;
2764
        m_freem(m);
2765
}
2766
 
2767
void
2768
icmp6_redirect_output(m0, rt)
2769
        struct mbuf *m0;
2770
        struct rtentry *rt;
2771
{
2772
        struct ifnet *ifp;      /* my outgoing interface */
2773
        struct in6_addr *ifp_ll6;
2774
        struct in6_addr *router_ll6;
2775
        struct ip6_hdr *sip6;   /* m0 as struct ip6_hdr */
2776
        struct mbuf *m = NULL;  /* newly allocated one */
2777
        struct ip6_hdr *ip6;    /* m as struct ip6_hdr */
2778
        struct nd_redirect *nd_rd;
2779
        size_t maxlen;
2780
        u_char *p;
2781
        struct sockaddr_in6 src_sa;
2782
        int64_t zoneid;
2783
 
2784
        icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2785
 
2786
        /* if we are not router, we don't send icmp6 redirect */
2787
        if (!ip6_forwarding || ip6_accept_rtadv)
2788
                goto fail;
2789
 
2790
        /* sanity check */
2791
        if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2792
                goto fail;
2793
 
2794
        /*
2795
         * Address check:
2796
         *  the source address must identify a neighbor, and
2797
         *  the destination address must not be a multicast address
2798
         *  [RFC 2461, sec 8.2]
2799
         */
2800
        sip6 = mtod(m0, struct ip6_hdr *);
2801
        bzero(&src_sa, sizeof(src_sa));
2802
        src_sa.sin6_family = AF_INET6;
2803
        src_sa.sin6_len = sizeof(src_sa);
2804
        src_sa.sin6_addr = sip6->ip6_src;
2805
        /* we don't currently use sin6_scope_id, but eventually use it */
2806
        if ((zoneid = in6_addr2zoneid(ifp, &sip6->ip6_src)) < 0)
2807
                goto fail;
2808
        src_sa.sin6_scope_id = zoneid;
2809
        if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2810
                goto fail;
2811
        if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2812
                goto fail;      /* what should we do here? */
2813
 
2814
        /* rate limit */
2815
        if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2816
                goto fail;
2817
 
2818
        /*
2819
         * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2820
         * we almost always ask for an mbuf cluster for simplicity.
2821
         * (MHLEN < IPV6_MMTU is almost always true)
2822
         */
2823
#if IPV6_MMTU >= MCLBYTES
2824
# error assumption failed about IPV6_MMTU and MCLBYTES
2825
#endif
2826
        MGETHDR(m, M_DONTWAIT, MT_HEADER);
2827
        if (m && IPV6_MMTU >= MHLEN)
2828
                MCLGET(m, M_DONTWAIT);
2829
        if (!m)
2830
                goto fail;
2831
        m->m_pkthdr.rcvif = NULL;
2832
        m->m_len = 0;
2833
        maxlen = M_TRAILINGSPACE(m);
2834
        maxlen = min(IPV6_MMTU, maxlen);
2835
        /* just for safety */
2836
        if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2837
            ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2838
                goto fail;
2839
        }
2840
 
2841
        {
2842
                /* get ip6 linklocal address for ifp(my outgoing interface). */
2843
                struct in6_ifaddr *ia;
2844
                if ((ia = in6ifa_ifpforlinklocal(ifp,
2845
                                                 IN6_IFF_NOTREADY|
2846
                                                 IN6_IFF_ANYCAST)) == NULL)
2847
                        goto fail;
2848
                ifp_ll6 = &ia->ia_addr.sin6_addr;
2849
        }
2850
 
2851
        /* get ip6 linklocal address for the router. */
2852
        if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2853
                struct sockaddr_in6 *sin6;
2854
                sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2855
                router_ll6 = &sin6->sin6_addr;
2856
                if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2857
                        router_ll6 = (struct in6_addr *)NULL;
2858
        } else
2859
                router_ll6 = (struct in6_addr *)NULL;
2860
 
2861
        /* ip6 */
2862
        ip6 = mtod(m, struct ip6_hdr *);
2863
        ip6->ip6_flow = 0;
2864
        ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2865
        ip6->ip6_vfc |= IPV6_VERSION;
2866
        /* ip6->ip6_plen will be set later */
2867
        ip6->ip6_nxt = IPPROTO_ICMPV6;
2868
        ip6->ip6_hlim = 255;
2869
        /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2870
        bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2871
        bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2872
 
2873
        /* ND Redirect */
2874
        nd_rd = (struct nd_redirect *)(ip6 + 1);
2875
        nd_rd->nd_rd_type = ND_REDIRECT;
2876
        nd_rd->nd_rd_code = 0;
2877
        nd_rd->nd_rd_reserved = 0;
2878
        if (rt->rt_flags & RTF_GATEWAY) {
2879
                /*
2880
                 * nd_rd->nd_rd_target must be a link-local address in
2881
                 * better router cases.
2882
                 */
2883
                if (!router_ll6)
2884
                        goto fail;
2885
                bcopy(router_ll6, &nd_rd->nd_rd_target,
2886
                      sizeof(nd_rd->nd_rd_target));
2887
                bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2888
                      sizeof(nd_rd->nd_rd_dst));
2889
        } else {
2890
                /* make sure redtgt == reddst */
2891
                bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2892
                      sizeof(nd_rd->nd_rd_target));
2893
                bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2894
                      sizeof(nd_rd->nd_rd_dst));
2895
        }
2896
 
2897
        p = (u_char *)(nd_rd + 1);
2898
 
2899
        if (!router_ll6)
2900
                goto nolladdropt;
2901
 
2902
    {
2903
        /* target lladdr option */
2904
        struct rtentry *rt_router = NULL;
2905
        int len;
2906
        struct sockaddr_dl *sdl;
2907
        struct nd_opt_hdr *nd_opt;
2908
        char *lladdr;
2909
 
2910
        rt_router = nd6_lookup(router_ll6, 0, ifp);
2911
        if (!rt_router)
2912
                goto nolladdropt;
2913
        len = sizeof(*nd_opt) + ifp->if_addrlen;
2914
        len = (len + 7) & ~7;   /* round by 8 */
2915
        /* safety check */
2916
        if (len + (p - (u_char *)ip6) > maxlen)
2917
                goto nolladdropt;
2918
        if (!(rt_router->rt_flags & RTF_GATEWAY) &&
2919
            (rt_router->rt_flags & RTF_LLINFO) &&
2920
            (rt_router->rt_gateway->sa_family == AF_LINK) &&
2921
            (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) &&
2922
            sdl->sdl_alen) {
2923
                nd_opt = (struct nd_opt_hdr *)p;
2924
                nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2925
                nd_opt->nd_opt_len = len >> 3;
2926
                lladdr = (char *)(nd_opt + 1);
2927
                bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2928
                p += len;
2929
        }
2930
    }
2931
nolladdropt:;
2932
 
2933
        m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2934
 
2935
        /* just to be safe */
2936
#ifdef M_DECRYPTED      /* not openbsd */
2937
        if (m0->m_flags & M_DECRYPTED)
2938
                goto noredhdropt;
2939
#endif
2940
        if (p - (u_char *)ip6 > maxlen)
2941
                goto noredhdropt;
2942
 
2943
    {
2944
        /* redirected header option */
2945
        int len;
2946
        struct nd_opt_rd_hdr *nd_opt_rh;
2947
 
2948
        /*
2949
         * compute the maximum size for icmp6 redirect header option.
2950
         * XXX room for auth header?
2951
         */
2952
        len = maxlen - (p - (u_char *)ip6);
2953
        len &= ~7;
2954
 
2955
        /* This is just for simplicity. */
2956
        if (m0->m_pkthdr.len != m0->m_len) {
2957
                if (m0->m_next) {
2958
                        m_freem(m0->m_next);
2959
                        m0->m_next = NULL;
2960
                }
2961
                m0->m_pkthdr.len = m0->m_len;
2962
        }
2963
 
2964
        /*
2965
         * Redirected header option spec (RFC2461 4.6.3) talks nothing
2966
         * about padding/truncate rule for the original IP packet.
2967
         * From the discussion on IPv6imp in Feb 1999, the consensus was:
2968
         * - "attach as much as possible" is the goal
2969
         * - pad if not aligned (original size can be guessed by original
2970
         *   ip6 header)
2971
         * Following code adds the padding if it is simple enough,
2972
         * and truncates if not.
2973
         */
2974
        if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2975
                panic("assumption failed in %s:%d\n", __FILE__, __LINE__);
2976
 
2977
        if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2978
                /* not enough room, truncate */
2979
                m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2980
        } else {
2981
                /* enough room, pad or truncate */
2982
                size_t extra;
2983
 
2984
                extra = m0->m_pkthdr.len % 8;
2985
                if (extra) {
2986
                        /* pad if easy enough, truncate if not */
2987
                        if (8 - extra <= M_TRAILINGSPACE(m0)) {
2988
                                /* pad */
2989
                                m0->m_len += (8 - extra);
2990
                                m0->m_pkthdr.len += (8 - extra);
2991
                        } else {
2992
                                /* truncate */
2993
                                m0->m_pkthdr.len -= extra;
2994
                                m0->m_len -= extra;
2995
                        }
2996
                }
2997
                len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2998
                m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2999
        }
3000
 
3001
        nd_opt_rh = (struct nd_opt_rd_hdr *)p;
3002
        bzero(nd_opt_rh, sizeof(*nd_opt_rh));
3003
        nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
3004
        nd_opt_rh->nd_opt_rh_len = len >> 3;
3005
        p += sizeof(*nd_opt_rh);
3006
        m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
3007
 
3008
        /* connect m0 to m */
3009
        m->m_next = m0;
3010
        m->m_pkthdr.len = m->m_len + m0->m_len;
3011
    }
3012
noredhdropt:;
3013
 
3014
        if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src))
3015
                sip6->ip6_src.s6_addr16[1] = 0;
3016
        if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst))
3017
                sip6->ip6_dst.s6_addr16[1] = 0;
3018
#if 0
3019
        if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
3020
                ip6->ip6_src.s6_addr16[1] = 0;
3021
        if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
3022
                ip6->ip6_dst.s6_addr16[1] = 0;
3023
#endif
3024
        if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target))
3025
                nd_rd->nd_rd_target.s6_addr16[1] = 0;
3026
        if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst))
3027
                nd_rd->nd_rd_dst.s6_addr16[1] = 0;
3028
 
3029
        ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
3030
 
3031
        nd_rd->nd_rd_cksum = 0;
3032
        nd_rd->nd_rd_cksum
3033
                = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
3034
 
3035
        /* send the packet to outside... */
3036
#ifdef IPSEC
3037
        /* Don't lookup socket */
3038
        (void)ipsec_setsocket(m, NULL);
3039
#endif /* IPSEC */
3040
        if (ip6_output(m, NULL, NULL, 0, NULL, NULL) != 0)
3041
                icmp6_ifstat_inc(ifp, ifs6_out_error);
3042
 
3043
        icmp6_ifstat_inc(ifp, ifs6_out_msg);
3044
        icmp6_ifstat_inc(ifp, ifs6_out_redirect);
3045
        icmp6stat.icp6s_outhist[ND_REDIRECT]++;
3046
 
3047
        return;
3048
 
3049
fail:
3050
        if (m)
3051
                m_freem(m);
3052
        if (m0)
3053
                m_freem(m0);
3054
}
3055
 
3056
#ifdef HAVE_NRL_INPCB
3057
#define sotoin6pcb      sotoinpcb
3058
#define in6pcb          inpcb
3059
#define in6p_icmp6filt  inp_icmp6filt
3060
#endif
3061
/*
3062
 * ICMPv6 socket option processing.
3063
 */
3064
int
3065
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
3066
icmp6_ctloutput(so, sopt)
3067
        struct socket *so;
3068
        struct sockopt *sopt;
3069
#else
3070
icmp6_ctloutput(op, so, level, optname, mp)
3071
        int op;
3072
        struct socket *so;
3073
        int level, optname;
3074
        struct mbuf **mp;
3075
#endif
3076
{
3077
        int error = 0;
3078
        int optlen;
3079
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
3080
        struct inpcb *inp = sotoinpcb(so);
3081
        int level, op, optname;
3082
 
3083
        if (sopt) {
3084
                level = sopt->sopt_level;
3085
                op = sopt->sopt_dir;
3086
                optname = sopt->sopt_name;
3087
                optlen = sopt->sopt_valsize;
3088
        } else
3089
                level = op = optname = optlen = 0;
3090
#else
3091
        struct in6pcb *in6p = sotoin6pcb(so);
3092
        struct mbuf *m = *mp;
3093
 
3094
        optlen = m ? m->m_len : 0;
3095
#endif
3096
 
3097
        if (level != IPPROTO_ICMPV6) {
3098
#if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
3099
                if (op == PRCO_SETOPT && m)
3100
                        (void)m_free(m);
3101
#endif
3102
                return EINVAL;
3103
        }
3104
 
3105
        switch (op) {
3106
        case PRCO_SETOPT:
3107
                switch (optname) {
3108
                case ICMP6_FILTER:
3109
                    {
3110
                        struct icmp6_filter *p;
3111
 
3112
                        if (optlen != sizeof(*p)) {
3113
                                error = EMSGSIZE;
3114
                                break;
3115
                        }
3116
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
3117
                        if (inp->in6p_icmp6filt == NULL) {
3118
                                error = EINVAL;
3119
                                break;
3120
                        }
3121
                        error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen,
3122
                                optlen);
3123
#else
3124
                        p = mtod(m, struct icmp6_filter *);
3125
                        if (!p || !in6p->in6p_icmp6filt) {
3126
                                error = EINVAL;
3127
                                break;
3128
                        }
3129
                        bcopy(p, in6p->in6p_icmp6filt,
3130
                                sizeof(struct icmp6_filter));
3131
                        error = 0;
3132
#endif
3133
                        break;
3134
                    }
3135
 
3136
                default:
3137
                        error = ENOPROTOOPT;
3138
                        break;
3139
                }
3140
#if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
3141
                if (m)
3142
                        (void)m_freem(m);
3143
#endif          
3144
                break;
3145
 
3146
        case PRCO_GETOPT:
3147
                switch (optname) {
3148
                case ICMP6_FILTER:
3149
                    {
3150
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
3151
                        if (inp->in6p_icmp6filt == NULL) {
3152
                                error = EINVAL;
3153
                                break;
3154
                        }
3155
                        error = sooptcopyout(sopt, inp->in6p_icmp6filt,
3156
                                sizeof(struct icmp6_filter));
3157
#else
3158
                        struct icmp6_filter *p;
3159
 
3160
                        if (!in6p->in6p_icmp6filt) {
3161
                                error = EINVAL;
3162
                                break;
3163
                        }
3164
                        *mp = m = m_get(M_WAIT, MT_SOOPTS);
3165
                        m->m_len = sizeof(struct icmp6_filter);
3166
                        p = mtod(m, struct icmp6_filter *);
3167
                        bcopy(in6p->in6p_icmp6filt, p,
3168
                                sizeof(struct icmp6_filter));
3169
                        error = 0;
3170
#endif
3171
                        break;
3172
                    }
3173
 
3174
                default:
3175
                        error = ENOPROTOOPT;
3176
                        break;
3177
                }
3178
                break;
3179
        }
3180
 
3181
        return(error);
3182
}
3183
#ifdef HAVE_NRL_INPCB
3184
#undef sotoin6pcb
3185
#undef in6pcb
3186
#undef in6p_icmp6filt
3187
#endif
3188
 
3189
#ifndef HAVE_PPSRATECHECK
3190
#ifndef timersub
3191
#define timersub(tvp, uvp, vvp)                                         \
3192
        do {                                                            \
3193
                (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;          \
3194
                (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;       \
3195
                if ((vvp)->tv_usec < 0) {                                \
3196
                        (vvp)->tv_sec--;                                \
3197
                        (vvp)->tv_usec += 1000000;                      \
3198
                }                                                       \
3199
        } while (0)
3200
#endif
3201
 
3202
/*
3203
 * ppsratecheck(): packets (or events) per second limitation.
3204
 */
3205
static int
3206
ppsratecheck(lasttime, curpps, maxpps)
3207
        struct timeval *lasttime;
3208
        int *curpps;
3209
        int maxpps;     /* maximum pps allowed */
3210
{
3211
        struct timeval tv, delta;
3212
        int s, rv;
3213
 
3214
        s = splclock();
3215
#ifndef __FreeBSD__
3216
        tv = mono_time;
3217
#else
3218
        microtime(&tv);
3219
#endif
3220
        splx(s);
3221
 
3222
        timersub(&tv, lasttime, &delta);
3223
 
3224
        /*
3225
         * Check for 0,0 so that the message will be seen at least once.
3226
         * If more than one second has passed since the last update of
3227
         * lasttime, reset the counter.
3228
         *
3229
         * We do increment *curpps even in *curpps < maxpps case, as some may
3230
         * try to use *curpps for stat purposes as well.
3231
         */
3232
        if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
3233
            delta.tv_sec >= 1) {
3234
                *lasttime = tv;
3235
                *curpps = 0;
3236
                rv = 1;
3237
        } else if (maxpps < 0)
3238
                rv = 1;
3239
        else if (*curpps < maxpps)
3240
                rv = 1;
3241
        else
3242
                rv = 0;
3243
 
3244
#if 1 /* DIAGNOSTIC? */
3245
        /* be careful about wrap-around */
3246
        if (*curpps + 1 > *curpps)
3247
                *curpps = *curpps + 1;
3248
#else
3249
        /*
3250
         * assume that there's not too many calls to this function.
3251
         * not sure if the assumption holds, as it depends on *caller's*
3252
         * behavior, not the behavior of this function.
3253
         * IMHO it is wrong to make assumption on the caller's behavior,
3254
         * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
3255
         */
3256
        *curpps = *curpps + 1;
3257
#endif
3258
 
3259
        return (rv);
3260
}
3261
#endif
3262
 
3263
/*
3264
 * Perform rate limit check.
3265
 * Returns 0 if it is okay to send the icmp6 packet.
3266
 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
3267
 * limitation.
3268
 *
3269
 * XXX per-destination/type check necessary?
3270
 */
3271
static int
3272
icmp6_ratelimit(dst, type, code)
3273
        const struct in6_addr *dst;     /* not used at this moment */
3274
        const int type;                 /* not used at this moment */
3275
        const int code;                 /* not used at this moment */
3276
{
3277
        int ret;
3278
 
3279
        ret = 0; /* okay to send */
3280
 
3281
        /* PPS limit */
3282
        if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
3283
            icmp6errppslim)) {
3284
                /* The packet is subject to rate limit */
3285
                ret++;
3286
        }
3287
 
3288
        return ret;
3289
}
3290
 
3291
/*
3292
 * Swap ip6_src and the address in the homeaddress destination option.
3293
 * Since the dest6 processing routine swaps ip6_src and homeaddr (if
3294
 * any), we must swap them again when we send back the ip datagram to
3295
 * the sender (ex. icmp6_error).
3296
 *
3297
 * returns 0 if succeeded.
3298
 */
3299
static int
3300
icmp6_recover_src(m)
3301
        struct mbuf *m; /* original ip6 packet */
3302
{
3303
        int off, nxt, finished = 0;
3304
        struct ip6_hdr *oip6;
3305
        struct ip6_ext *exts;
3306
        struct ip6_dest *dstopts;
3307
        int dstoptlen;
3308
        u_int8_t *opt;
3309
        int optlen;
3310
        struct ip6_opt_home_address *haopt;
3311
        struct in6_addr t;
3312
        int error = 0;
3313
 
3314
#ifndef PULLDOWN_TEST 
3315
        IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), ENOBUFS);
3316
        oip6 = mtod(m, struct ip6_hdr *);
3317
#else
3318
        IP6_EXTHDR_GET(oip6, struct ip6_hdr *, m, 0, sizeof(*oip6));
3319
        if (oip6 == NULL) {
3320
                error = ENOBUFS;
3321
                goto bad;
3322
        }
3323
#endif
3324
 
3325
        off = sizeof(struct ip6_hdr);
3326
        nxt = oip6->ip6_nxt;
3327
        while (off + 2 < m->m_pkthdr.len) {
3328
                switch (nxt) {
3329
                case IPPROTO_DSTOPTS:
3330
#ifndef PULLDOWN_TEST 
3331
                        IP6_EXTHDR_CHECK(m, off, sizeof(struct ip6_dest),
3332
                                         ENOBUFS);
3333
                        dstopts = (struct ip6_dest *)(mtod(m, caddr_t) + off);
3334
#else
3335
                        IP6_EXTHDR_GET(dstopts, struct ip6_dest *,
3336
                                       m, off, sizeof(*dstopts));
3337
                        if (dstopts == NULL) {
3338
                                error = ENOBUFS;
3339
                                goto bad;
3340
                        }
3341
#endif
3342
 
3343
                        dstoptlen = (dstopts->ip6d_len + 1) << 3;
3344
#ifndef PULLDOWN_TEST 
3345
                        IP6_EXTHDR_CHECK(m, off, dstoptlen, ENOBUFS);
3346
                        dstopts = (struct ip6_dest *)(mtod(m, caddr_t) + off);
3347
#else
3348
                        IP6_EXTHDR_GET(dstopts, struct ip6_dest *,
3349
                                       m, off, dstoptlen);
3350
                        if (dstopts == NULL) {
3351
                                error = ENOBUFS;
3352
                                goto bad;
3353
                        }
3354
#endif
3355
                        off += dstoptlen;
3356
                        nxt = dstopts->ip6d_nxt;
3357
 
3358
                        /* find homeaddress dstopt */
3359
                        dstoptlen -= sizeof(struct ip6_dest);
3360
                        opt = (u_int8_t *)dstopts + sizeof(struct ip6_dest);
3361
                        for (optlen = 0; (dstoptlen > 0 && finished == 0);
3362
                             dstoptlen -= optlen, opt += optlen) {
3363
                                if ((*opt != IP6OPT_PAD1) &&
3364
                                    (dstoptlen < IP6OPT_MINLEN || *(opt + 1) + 2 > dstoptlen)) {
3365
                                        error = EINVAL;
3366
                                        goto bad;
3367
                                }
3368
                                switch (*opt) {
3369
                                case IP6OPT_PAD1:
3370
                                        optlen = 1;
3371
                                        break;
3372
                                case IP6OPT_PADN:
3373
                                        optlen = *(opt + 1) + 2;
3374
                                        break;
3375
                                case IP6OPT_HOME_ADDRESS:
3376
                                        haopt = (struct ip6_opt_home_address *)opt;
3377
                                        optlen = haopt->ip6oh_len + 2;
3378
                                        if (optlen < sizeof(*haopt)) {
3379
                                                error = EINVAL;
3380
                                                goto bad;
3381
                                        }
3382
 
3383
                                        /* swap */
3384
                                        bcopy(haopt->ip6oh_addr, &t,
3385
                                              sizeof(haopt->ip6oh_addr));
3386
                                        bcopy(&oip6->ip6_src,
3387
                                              haopt->ip6oh_addr,
3388
                                              sizeof(oip6->ip6_src));
3389
                                        bcopy(&t, &oip6->ip6_src,
3390
                                              sizeof(t));
3391
                                        finished = 1;
3392
                                        break;
3393
                                default:
3394
                                        optlen = ip6_unknown_opt(opt, m,
3395
                                            opt - mtod(m, u_int8_t *));
3396
                                        if (optlen == -1) {
3397
                                                error = EINVAL;
3398
                                                goto bad;
3399
                                        }
3400
                                        optlen += 2;
3401
                                        break;
3402
                                }
3403
                        }
3404
                        break;
3405
 
3406
                default:
3407
#ifndef PULLDOWN_TEST 
3408
                        IP6_EXTHDR_CHECK(m, off, sizeof(struct ip6_ext),
3409
                                         ENOBUFS);
3410
                        exts = (struct ip6_ext *)(mtod(m, caddr_t) + off);
3411
#else
3412
                        IP6_EXTHDR_GET(exts, struct ip6_ext *,
3413
                                       m, off, sizeof(*exts));
3414
                        if (exts == NULL) {
3415
                                error = ENOBUFS;
3416
                                goto bad;
3417
                        }
3418
#endif
3419
                        if (nxt == IPPROTO_AH)
3420
                                off += (exts->ip6e_len + 2) << 2;
3421
                        else
3422
                                off += (exts->ip6e_len + 1) << 3;
3423
                        nxt = exts->ip6e_nxt;
3424
                        break;
3425
                }
3426
                if (finished)
3427
                        break;
3428
        }
3429
 
3430
        return (0);
3431
 bad:
3432
        ip6_delaux(m);
3433
        m_freem(m);
3434
        return (error);
3435
}
3436
 
3437
#if defined(__NetBSD__) || defined(__OpenBSD__)
3438
static struct rtentry *
3439
icmp6_mtudisc_clone(dst)
3440
        struct sockaddr *dst;
3441
{
3442
        struct rtentry *rt;
3443
        int    error;
3444
 
3445
        rt = rtalloc1(dst, 1);
3446
        if (rt == 0)
3447
                return NULL;
3448
 
3449
        /* If we didn't get a host route, allocate one */
3450
        if ((rt->rt_flags & RTF_HOST) == 0) {
3451
                struct rtentry *nrt;
3452
 
3453
                error = rtrequest((int) RTM_ADD, dst,
3454
                    (struct sockaddr *) rt->rt_gateway,
3455
                    (struct sockaddr *) 0,
3456
                    RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
3457
                if (error) {
3458
                        rtfree(rt);
3459
                        return NULL;
3460
                }
3461
                nrt->rt_rmx = rt->rt_rmx;
3462
                rtfree(rt);
3463
                rt = nrt;
3464
        }
3465
        error = rt_timer_add(rt, icmp6_mtudisc_timeout,
3466
                        icmp6_mtudisc_timeout_q);
3467
        if (error) {
3468
                rtfree(rt);
3469
                return NULL;
3470
        }
3471
 
3472
        return rt;      /* caller need to call rtfree() */
3473
}
3474
 
3475
static void
3476
icmp6_mtudisc_timeout(rt, r)
3477
        struct rtentry *rt;
3478
        struct rttimer *r;
3479
{
3480
        if (rt == NULL)
3481
                panic("icmp6_mtudisc_timeout: bad route to timeout");
3482
        if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
3483
            (RTF_DYNAMIC | RTF_HOST)) {
3484
                rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
3485
                    rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
3486
        } else {
3487
                if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
3488
                        rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
3489
        }
3490
}
3491
 
3492
static void
3493
icmp6_redirect_timeout(rt, r)
3494
        struct rtentry *rt;
3495
        struct rttimer *r;
3496
{
3497
        if (rt == NULL)
3498
                panic("icmp6_redirect_timeout: bad route to timeout");
3499
        if ((rt->rt_flags & (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) ==
3500
            (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) {
3501
                rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
3502
                    rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
3503
        }
3504
}
3505
#endif /* __NetBSD__ || __OpenBSD__ */
3506
 
3507
#ifdef __bsdi__
3508
void
3509
icmp6_mtuexpire(rt, rtt)
3510
        struct rtentry *rt;
3511
        struct rttimer *rtt;
3512
{
3513
        rt->rt_flags |= RTF_PROBEMTU;
3514
        R_Free(rtt);
3515
}
3516
 
3517
int *icmp6_sysvars[] = ICMPV6CTL_VARS;
3518
 
3519
int
3520
icmp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
3521
        int *name;
3522
        u_int namelen;
3523
        void *oldp;
3524
        size_t *oldlenp;
3525
        void *newp;
3526
        size_t newlen;
3527
{
3528
        if (name[0] >= ICMPV6CTL_MAXID)
3529
                return (EOPNOTSUPP);
3530
        switch (name[0]) {
3531
#if 0
3532
        ICMPV6CTL_ND6_PRUNE:
3533
        ICMPV6CTL_ND6_DELAY:
3534
        ICMPV6CTL_ND6_UMAXTRIES:
3535
        ICMPV6CTL_ND6_MMAXTRIES:
3536
        ICMPV6CTL_ND6_USELOOPBACK:
3537
                /* need to check the value. */
3538
#endif
3539
        case ICMPV6CTL_STATS:
3540
                return sysctl_rdtrunc(oldp, oldlenp, newp, &icmp6stat,
3541
                    sizeof(icmp6stat));
3542
        case ICMPV6CTL_ND6_DRLIST:
3543
        case ICMPV6CTL_ND6_PRLIST:
3544
                return nd6_sysctl(name[0], oldp, oldlenp, newp, newlen);
3545
        default:
3546
                return (sysctl_int_arr(icmp6_sysvars, name, namelen,
3547
                    oldp, oldlenp, newp, newlen));
3548
        }
3549
}
3550
#endif /* __bsdi__ */
3551
 
3552
#if defined(__NetBSD__) || defined(__OpenBSD__)
3553
#include <vm/vm.h>
3554
#include <sys/sysctl.h>
3555
int
3556
icmp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
3557
        int *name;
3558
        u_int namelen;
3559
        void *oldp;
3560
        size_t *oldlenp;
3561
        void *newp;
3562
        size_t newlen;
3563
{
3564
 
3565
        /* All sysctl names at this level are terminal. */
3566
        if (namelen != 1)
3567
                return ENOTDIR;
3568
 
3569
        switch (name[0]) {
3570
 
3571
        case ICMPV6CTL_REDIRACCEPT:
3572
                return sysctl_int(oldp, oldlenp, newp, newlen,
3573
                                &icmp6_rediraccept);
3574
        case ICMPV6CTL_REDIRTIMEOUT:
3575
                return sysctl_int(oldp, oldlenp, newp, newlen,
3576
                                &icmp6_redirtimeout);
3577
        case ICMPV6CTL_STATS:
3578
                return sysctl_rdstruct(oldp, oldlenp, newp,
3579
                                &icmp6stat, sizeof(icmp6stat));
3580
        case ICMPV6CTL_ND6_PRUNE:
3581
                return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_prune);
3582
        case ICMPV6CTL_ND6_DELAY:
3583
                return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_delay);
3584
        case ICMPV6CTL_ND6_UMAXTRIES:
3585
                return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_umaxtries);
3586
        case ICMPV6CTL_ND6_MMAXTRIES:
3587
                return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_mmaxtries);
3588
        case ICMPV6CTL_ND6_USELOOPBACK:
3589
                return sysctl_int(oldp, oldlenp, newp, newlen,
3590
                                &nd6_useloopback);
3591
        case ICMPV6CTL_NODEINFO:
3592
                return sysctl_int(oldp, oldlenp, newp, newlen, &icmp6_nodeinfo);
3593
        case ICMPV6CTL_ERRPPSLIMIT:
3594
                return sysctl_int(oldp, oldlenp, newp, newlen, &icmp6errppslim);
3595
        case ICMPV6CTL_ND6_MAXNUDHINT:
3596
                return sysctl_int(oldp, oldlenp, newp, newlen,
3597
                                &nd6_maxnudhint);
3598
        case ICMPV6CTL_MTUDISC_HIWAT:
3599
                return sysctl_int(oldp, oldlenp, newp, newlen,
3600
                                &icmp6_mtudisc_hiwat);
3601
        case ICMPV6CTL_MTUDISC_LOWAT:
3602
                return sysctl_int(oldp, oldlenp, newp, newlen,
3603
                                &icmp6_mtudisc_lowat);
3604
        case ICMPV6CTL_ND6_DEBUG:
3605
                return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_debug);
3606
        case ICMPV6CTL_ND6_DRLIST:
3607
        case ICMPV6CTL_ND6_PRLIST:
3608
                return nd6_sysctl(name[0], oldp, oldlenp, newp, newlen);
3609
        default:
3610
                return ENOPROTOOPT;
3611
        }
3612
        /* NOTREACHED */
3613
}
3614
#endif /* __NetBSD__ */

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.