OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [bsd_tcpip/] [v2_0/] [src/] [sys/] [netinet6/] [nd6.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      src/sys/netinet6/nd6.c
4
//
5
//==========================================================================
6
//####BSDCOPYRIGHTBEGIN####
7
//
8
// -------------------------------------------
9
//
10
// Portions of this software may have been derived from OpenBSD, 
11
// FreeBSD or other sources, and are covered by the appropriate
12
// copyright disclaimers included herein.
13
//
14
// Portions created by Red Hat are
15
// Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
16
//
17
// -------------------------------------------
18
//
19
//####BSDCOPYRIGHTEND####
20
//==========================================================================
21
 
22
/*      $KAME: nd6.c,v 1.221 2001/12/18 02:23:45 itojun Exp $   */
23
 
24
/*
25
 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
26
 * All rights reserved.
27
 *
28
 * Redistribution and use in source and binary forms, with or without
29
 * modification, are permitted provided that the following conditions
30
 * are met:
31
 * 1. Redistributions of source code must retain the above copyright
32
 *    notice, this list of conditions and the following disclaimer.
33
 * 2. Redistributions in binary form must reproduce the above copyright
34
 *    notice, this list of conditions and the following disclaimer in the
35
 *    documentation and/or other materials provided with the distribution.
36
 * 3. Neither the name of the project nor the names of its contributors
37
 *    may be used to endorse or promote products derived from this software
38
 *    without specific prior written permission.
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
41
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
44
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
 * SUCH DAMAGE.
51
 */
52
 
53
/*
54
 * XXX
55
 * KAME 970409 note:
56
 * BSD/OS version heavily modifies this code, related to llinfo.
57
 * Since we don't have BSD/OS version of net/route.c in our hand,
58
 * I left the code mostly as it was in 970310.  -- itojun
59
 */
60
 
61
#include <sys/param.h>
62
#include <sys/malloc.h>
63
#include <sys/mbuf.h>
64
#include <sys/socket.h>
65
#include <sys/sockio.h>
66
#include <sys/protosw.h>
67
#include <sys/errno.h>
68
#include <sys/queue.h>
69
 
70
#include <net/if.h>
71
#include <net/if_dl.h>
72
#include <net/if_types.h>
73
#include <net/route.h>
74
 
75
#include <netinet/in.h>
76
#ifndef __NetBSD__
77
#include <netinet/if_ether.h>
78
#ifdef __bsdi__
79
#include <net/if_fddi.h>
80
#endif
81
#ifdef __OpenBSD__
82
#include <netinet/ip_ipsp.h>
83
#endif
84
#else /* __NetBSD__ */
85
#include <net/if_ether.h>
86
#include <netinet/if_inarp.h>
87
#include <net/if_fddi.h>
88
#endif /* __NetBSD__ */
89
#include <netinet6/in6_var.h>
90
#include <netinet/ip6.h>
91
#include <netinet6/ip6_var.h>
92
#include <netinet6/nd6.h>
93
#include <netinet/icmp6.h>
94
 
95
#if defined(__NetBSD__)
96
extern struct ifnet loif[NLOOP];
97
#endif
98
 
99
#define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
100
#define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
101
 
102
#define SIN6(s) ((struct sockaddr_in6 *)s)
103
#define SDL(s) ((struct sockaddr_dl *)s)
104
 
105
/* timer values */
106
int     nd6_prune       = 1;    /* walk list every 1 seconds */
107
int     nd6_delay       = 5;    /* delay first probe time 5 second */
108
int     nd6_umaxtries   = 3;    /* maximum unicast query */
109
int     nd6_mmaxtries   = 3;    /* maximum multicast query */
110
int     nd6_useloopback = 1;    /* use loopback interface for local traffic */
111
int     nd6_gctimer     = (60 * 60 * 24); /* 1 day: garbage collection timer */
112
 
113
/* preventing too many loops in ND option parsing */
114
int nd6_maxndopt = 10;  /* max # of ND options allowed */
115
 
116
int nd6_maxnudhint = 0;  /* max # of subsequent upper layer hints */
117
 
118
int nd6_debug = 1;
119
 
120
/* for debugging? */
121
static int nd6_inuse, nd6_allocated;
122
 
123
struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
124
static size_t nd_ifinfo_indexlim = 8;
125
struct nd_ifinfo *nd_ifinfo = NULL;
126
struct nd_drhead nd_defrouter;
127
struct nd_prhead nd_prefix = { 0 };
128
 
129
int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
130
static struct sockaddr_in6 all1_sa;
131
 
132
static void nd6_slowtimo __P((void *));
133
static int regen_tmpaddr __P((struct in6_ifaddr *));
134
static struct llinfo_nd6 *nd6_free __P((struct rtentry *, int));
135
 
136
#ifdef __NetBSD__
137
struct callout nd6_slowtimo_ch = CALLOUT_INITIALIZER;
138
struct callout nd6_timer_ch = CALLOUT_INITIALIZER;
139
extern struct callout in6_tmpaddrtimer_ch;
140
#elif (defined(__FreeBSD__) && __FreeBSD__ >= 3)
141
struct callout nd6_slowtimo_ch;
142
struct callout nd6_timer_ch;
143
extern struct callout in6_tmpaddrtimer_ch;
144
#elif defined(__OpenBSD__)
145
struct timeout nd6_slowtimo_ch;
146
struct timeout nd6_timer_ch;
147
extern struct timeout in6_tmpaddrtimer_ch;
148
#endif
149
 
150
void
151
nd6_init()
152
{
153
        static int nd6_init_done = 0;
154
        int i;
155
 
156
        if (nd6_init_done) {
157
                log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
158
                return;
159
        }
160
 
161
        all1_sa.sin6_family = AF_INET6;
162
        all1_sa.sin6_len = sizeof(struct sockaddr_in6);
163
        for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
164
                all1_sa.sin6_addr.s6_addr[i] = 0xff;
165
 
166
        /* initialization of the default router list */
167
        TAILQ_INIT(&nd_defrouter);
168
 
169
        nd6_init_done = 1;
170
 
171
        /* start timer */
172
#if defined(__NetBSD__) || (defined(__FreeBSD__) && __FreeBSD__ >= 3)
173
        callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
174
            nd6_slowtimo, NULL);
175
#elif defined(__OpenBSD__)
176
        timeout_set(&nd6_slowtimo_ch, nd6_slowtimo, NULL);
177
        timeout_add(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz);
178
#else
179
        timeout(nd6_slowtimo, (caddr_t)0, ND6_SLOWTIMER_INTERVAL * hz);
180
#endif
181
}
182
 
183
void
184
nd6_ifattach(ifp)
185
        struct ifnet *ifp;
186
{
187
 
188
        /*
189
         * We have some arrays that should be indexed by if_index.
190
         * since if_index will grow dynamically, they should grow too.
191
         */
192
        if (nd_ifinfo == NULL || if_index >= nd_ifinfo_indexlim) {
193
                size_t n;
194
                caddr_t q;
195
 
196
                while (if_index >= nd_ifinfo_indexlim)
197
                        nd_ifinfo_indexlim <<= 1;
198
 
199
                /* grow nd_ifinfo */
200
                n = nd_ifinfo_indexlim * sizeof(struct nd_ifinfo);
201
                q = (caddr_t)malloc(n, M_IP6NDP, M_WAITOK);
202
                bzero(q, n);
203
                if (nd_ifinfo) {
204
                        bcopy((caddr_t)nd_ifinfo, q, n/2);
205
                        free((caddr_t)nd_ifinfo, M_IP6NDP);
206
                }
207
                nd_ifinfo = (struct nd_ifinfo *)q;
208
        }
209
 
210
#define ND nd_ifinfo[ifp->if_index]
211
 
212
        /*
213
         * Don't initialize if called twice.
214
         * XXX: to detect this, we should choose a member that is never set
215
         * before initialization of the ND structure itself.  We formaly used
216
         * the linkmtu member, which was not suitable because it could be
217
         * initialized via "ifconfig mtu".
218
         */
219
        if (ND.basereachable)
220
                return;
221
 
222
#ifdef DIAGNOSTIC
223
#if defined(__FreeBSD__) && __FreeBSD__ >= 5
224
        if (!ifnet_byindex(ifp->if_index))
225
                panic("nd6_ifattach: ifnet_byindex is NULL");
226
#else
227
        if (!ifindex2ifnet[ifp->if_index])
228
                panic("nd6_ifattach: ifindex2ifnet is NULL");
229
#endif
230
#endif
231
#if defined(__FreeBSD__) && __FreeBSD__ >= 5
232
        ND.linkmtu = ifnet_byindex(ifp->if_index)->if_mtu;
233
#else
234
        ND.linkmtu = ifindex2ifnet[ifp->if_index]->if_mtu;
235
#endif
236
        ND.chlim = IPV6_DEFHLIM;
237
        ND.basereachable = REACHABLE_TIME;
238
        ND.reachable = ND_COMPUTE_RTIME(ND.basereachable);
239
        ND.retrans = RETRANS_TIMER;
240
        ND.receivedra = 0;
241
        /*
242
         * Note that the default value of ip6_accept_rtadv is 0, which means
243
         * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
244
         * here.
245
         */
246
        ND.flags = ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV;
247
        nd6_setmtu(ifp);
248
#undef ND
249
}
250
 
251
/*
252
 * Reset ND level link MTU. This function is called when the physical MTU
253
 * changes, which means we might have to adjust the ND level MTU.
254
 */
255
void
256
nd6_setmtu(ifp)
257
        struct ifnet *ifp;
258
{
259
#ifndef MIN
260
#define MIN(a,b) ((a) < (b) ? (a) : (b))
261
#endif
262
        struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index];
263
        u_long oldmaxmtu = ndi->maxmtu;
264
        u_long oldlinkmtu = ndi->linkmtu;
265
 
266
        switch (ifp->if_type) {
267
        case IFT_ARCNET:        /* XXX MTU handling needs more work */
268
                ndi->maxmtu = MIN(60480, ifp->if_mtu);
269
                break;
270
        case IFT_ETHER:
271
                ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
272
                break;
273
#if defined(__FreeBSD__) || defined(__bsdi__)
274
        case IFT_FDDI:
275
#if 0 // FIXME
276
#if defined(__bsdi__) && _BSDI_VERSION >= 199802
277
                ndi->maxmtu = MIN(FDDIMTU, ifp->if_mtu);
278
#else
279
                ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu);
280
#endif
281
#endif
282
                break;
283
#endif
284
#if !(defined(__bsdi__) && _BSDI_VERSION >= 199802)
285
        case IFT_ATM:
286
#if 0 // FIXME
287
                ndi->maxmtu = MIN(ATMMTU, ifp->if_mtu);
288
#endif
289
                break;
290
#endif
291
        case IFT_IEEE1394:      /* XXX should be IEEE1394MTU(1500) */
292
                ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
293
                break;
294
#ifdef IFT_IEEE80211
295
        case IFT_IEEE80211:     /* XXX should be IEEE80211MTU(1500) */
296
                ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
297
                break;
298
#endif
299
        default:
300
                ndi->maxmtu = ifp->if_mtu;
301
                break;
302
        }
303
 
304
        if (oldmaxmtu != ndi->maxmtu) {
305
                /*
306
                 * If the ND level MTU is not set yet, or if the maxmtu
307
                 * is reset to a smaller value than the ND level MTU,
308
                 * also reset the ND level MTU.
309
                 */
310
                if (ndi->linkmtu == 0 ||
311
                    ndi->maxmtu < ndi->linkmtu) {
312
                        ndi->linkmtu = ndi->maxmtu;
313
                        /* also adjust in6_maxmtu if necessary. */
314
                        if (oldlinkmtu == 0) {
315
                                /*
316
                                 * XXX: the case analysis is grotty, but
317
                                 * it is not efficient to call in6_setmaxmtu()
318
                                 * here when we are during the initialization
319
                                 * procedure.
320
                                 */
321
                                if (in6_maxmtu < ndi->linkmtu)
322
                                        in6_maxmtu = ndi->linkmtu;
323
                        } else
324
                                in6_setmaxmtu();
325
                }
326
        }
327
#undef MIN
328
}
329
 
330
void
331
nd6_option_init(opt, icmp6len, ndopts)
332
        void *opt;
333
        int icmp6len;
334
        union nd_opts *ndopts;
335
{
336
        bzero(ndopts, sizeof(*ndopts));
337
        ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
338
        ndopts->nd_opts_last
339
                = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
340
 
341
        if (icmp6len == 0) {
342
                ndopts->nd_opts_done = 1;
343
                ndopts->nd_opts_search = NULL;
344
        }
345
}
346
 
347
/*
348
 * Take one ND option.
349
 */
350
struct nd_opt_hdr *
351
nd6_option(ndopts)
352
        union nd_opts *ndopts;
353
{
354
        struct nd_opt_hdr *nd_opt;
355
        int olen;
356
 
357
        if (!ndopts)
358
                panic("ndopts == NULL in nd6_option\n");
359
        if (!ndopts->nd_opts_last)
360
                panic("uninitialized ndopts in nd6_option\n");
361
        if (!ndopts->nd_opts_search)
362
                return NULL;
363
        if (ndopts->nd_opts_done)
364
                return NULL;
365
 
366
        nd_opt = ndopts->nd_opts_search;
367
 
368
        /* make sure nd_opt_len is inside the buffer */
369
        if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
370
                bzero(ndopts, sizeof(*ndopts));
371
                return NULL;
372
        }
373
 
374
        olen = nd_opt->nd_opt_len << 3;
375
        if (olen == 0) {
376
                /*
377
                 * Message validation requires that all included
378
                 * options have a length that is greater than zero.
379
                 */
380
                bzero(ndopts, sizeof(*ndopts));
381
                return NULL;
382
        }
383
 
384
        ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
385
        if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
386
                /* option overruns the end of buffer, invalid */
387
                bzero(ndopts, sizeof(*ndopts));
388
                return NULL;
389
        } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
390
                /* reached the end of options chain */
391
                ndopts->nd_opts_done = 1;
392
                ndopts->nd_opts_search = NULL;
393
        }
394
        return nd_opt;
395
}
396
 
397
/*
398
 * Parse multiple ND options.
399
 * This function is much easier to use, for ND routines that do not need
400
 * multiple options of the same type.
401
 */
402
int
403
nd6_options(ndopts)
404
        union nd_opts *ndopts;
405
{
406
        struct nd_opt_hdr *nd_opt;
407
        int i = 0;
408
 
409
        if (!ndopts)
410
                panic("ndopts == NULL in nd6_options\n");
411
        if (!ndopts->nd_opts_last)
412
                panic("uninitialized ndopts in nd6_options\n");
413
        if (!ndopts->nd_opts_search)
414
                return 0;
415
 
416
        while (1) {
417
                nd_opt = nd6_option(ndopts);
418
                if (!nd_opt && !ndopts->nd_opts_last) {
419
                        /*
420
                         * Message validation requires that all included
421
                         * options have a length that is greater than zero.
422
                         */
423
                        icmp6stat.icp6s_nd_badopt++;
424
                        bzero(ndopts, sizeof(*ndopts));
425
                        return -1;
426
                }
427
 
428
                if (!nd_opt)
429
                        goto skip1;
430
 
431
                switch (nd_opt->nd_opt_type) {
432
                case ND_OPT_SOURCE_LINKADDR:
433
                case ND_OPT_TARGET_LINKADDR:
434
                case ND_OPT_MTU:
435
                case ND_OPT_REDIRECTED_HEADER:
436
                case ND_OPT_ADVINTERVAL:
437
                case ND_OPT_SOURCE_ADDRLIST:
438
                case ND_OPT_TARGET_ADDRLIST:
439
                        if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
440
                                nd6log((LOG_INFO,
441
                                    "duplicated ND6 option found (type=%d)\n",
442
                                    nd_opt->nd_opt_type));
443
                                /* XXX bark? */
444
                        } else {
445
                                ndopts->nd_opt_array[nd_opt->nd_opt_type]
446
                                        = nd_opt;
447
                        }
448
                        break;
449
                case ND_OPT_PREFIX_INFORMATION:
450
                        if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
451
                                ndopts->nd_opt_array[nd_opt->nd_opt_type]
452
                                        = nd_opt;
453
                        }
454
                        ndopts->nd_opts_pi_end =
455
                                (struct nd_opt_prefix_info *)nd_opt;
456
                        break;
457
                case ND_OPT_HOMEAGENT_INFO:
458
                        break;
459
                default:
460
                        /*
461
                         * Unknown options must be silently ignored,
462
                         * to accomodate future extension to the protocol.
463
                         */
464
                        nd6log((LOG_DEBUG,
465
                            "nd6_options: unsupported option %d - "
466
                            "option ignored\n", nd_opt->nd_opt_type));
467
                }
468
 
469
skip1:
470
                i++;
471
                if (i > nd6_maxndopt) {
472
                        icmp6stat.icp6s_nd_toomanyopt++;
473
                        nd6log((LOG_INFO, "too many loop in nd opt\n"));
474
                        break;
475
                }
476
 
477
                if (ndopts->nd_opts_done)
478
                        break;
479
        }
480
 
481
        return 0;
482
}
483
 
484
/*
485
 * ND6 timer routine to expire default route list and prefix list
486
 */
487
void
488
nd6_timer(ignored_arg)
489
        void    *ignored_arg;
490
{
491
        int s;
492
        struct llinfo_nd6 *ln;
493
        struct nd_defrouter *dr;
494
        struct nd_prefix *pr;
495
#if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
496
        long time_second = time.tv_sec;
497
#endif
498
        struct ifnet *ifp;
499
        struct in6_ifaddr *ia6, *nia6;
500
        struct in6_addrlifetime *lt6;
501
 
502
#ifdef __NetBSD__
503
        s = splsoftnet();
504
#else
505
        s = splnet();
506
#endif
507
#if defined(__NetBSD__) || (defined(__FreeBSD__) && __FreeBSD__ >= 3)
508
        callout_reset(&nd6_timer_ch, nd6_prune * hz,
509
            nd6_timer, NULL);
510
#elif defined(__OpenBSD__)
511
        timeout_set(&nd6_timer_ch, nd6_timer, NULL);
512
        timeout_add(&nd6_timer_ch, nd6_prune * hz);
513
#else
514
        timeout(nd6_timer, (caddr_t)0, nd6_prune * hz);
515
#endif
516
 
517
        ln = llinfo_nd6.ln_next;
518
        while (ln && ln != &llinfo_nd6) {
519
                struct rtentry *rt;
520
                struct sockaddr_in6 *dst;
521
                struct llinfo_nd6 *next = ln->ln_next;
522
                /* XXX: used for the DELAY case only: */
523
                struct nd_ifinfo *ndi = NULL;
524
 
525
                if ((rt = ln->ln_rt) == NULL) {
526
                        ln = next;
527
                        continue;
528
                }
529
                if ((ifp = rt->rt_ifp) == NULL) {
530
                        ln = next;
531
                        continue;
532
                }
533
                ndi = &nd_ifinfo[ifp->if_index];
534
                dst = (struct sockaddr_in6 *)rt_key(rt);
535
 
536
                if (ln->ln_expire > time_second) {
537
                        ln = next;
538
                        continue;
539
                }
540
 
541
                /* sanity check */
542
                if (!rt)
543
                        panic("rt=0 in nd6_timer(ln=%p)\n", ln);
544
                if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
545
                        panic("rt_llinfo(%p) is not equal to ln(%p)\n",
546
                              rt->rt_llinfo, ln);
547
                if (!dst)
548
                        panic("dst=0 in nd6_timer(ln=%p)\n", ln);
549
 
550
                switch (ln->ln_state) {
551
                case ND6_LLINFO_INCOMPLETE:
552
                        if (ln->ln_asked < nd6_mmaxtries) {
553
                                ln->ln_asked++;
554
                                ln->ln_expire = time_second +
555
                                        nd_ifinfo[ifp->if_index].retrans / 1000;
556
                                nd6_ns_output(ifp, NULL, &dst->sin6_addr,
557
                                        ln, 0);
558
                        } else {
559
                                struct mbuf *m = ln->ln_hold;
560
                                if (m) {
561
                                        struct ip6_hdr *ip6_in;
562
                                        struct sockaddr_in6 sin6_in;
563
                                        int64_t szoneid, dzoneid;
564
 
565
                                        /*
566
                                         * Fake rcvif to make the ICMP error
567
                                         * more helpful in diagnosing for the
568
                                         * receiver.
569
                                         * XXX: should we consider
570
                                         * older rcvif?
571
                                         */
572
                                        m->m_pkthdr.rcvif = rt->rt_ifp;
573
 
574
                                        /*
575
                                         * XXX: for scoped addresses, we should
576
                                         * disambiguate the zone.  We should
577
                                         * perhaps hang sockaddr_in6 as aux
578
                                         * data in the mbuf.
579
                                         */
580
                                        ip6_in = mtod(m, struct ip6_hdr *);
581
                                        szoneid = in6_addr2zoneid(rt->rt_ifp,
582
                                                                  &ip6_in->ip6_src);
583
                                        dzoneid = in6_addr2zoneid(rt->rt_ifp,
584
                                                                  &ip6_in->ip6_dst);
585
                                        if (szoneid < 0 || dzoneid < 0) {
586
                                                /* impossible... */
587
                                                m_freem(m);
588
                                        } else {
589
                                                bzero(&sin6_in,
590
                                                      sizeof(sin6_in));
591
                                                sin6_in.sin6_addr = ip6_in->ip6_src;
592
                                                sin6_in.sin6_scope_id = szoneid;
593
                                                in6_embedscope(&ip6_in->ip6_src,
594
                                                               &sin6_in);
595
                                                bzero(&sin6_in,
596
                                                      sizeof(sin6_in));
597
                                                sin6_in.sin6_addr = ip6_in->ip6_dst;
598
                                                sin6_in.sin6_scope_id = dzoneid;
599
                                                in6_embedscope(&ip6_in->ip6_dst,
600
                                                               &sin6_in);
601
                                                icmp6_error(m,
602
                                                            ICMP6_DST_UNREACH,
603
                                                            ICMP6_DST_UNREACH_ADDR, 0);
604
                                        }
605
                                        ln->ln_hold = NULL;
606
                                }
607
                                next = nd6_free(rt, 0);
608
                        }
609
                        break;
610
                case ND6_LLINFO_REACHABLE:
611
                        if (ln->ln_expire) {
612
                                ln->ln_state = ND6_LLINFO_STALE;
613
                                ln->ln_expire = time_second + nd6_gctimer;
614
                        }
615
                        break;
616
 
617
                case ND6_LLINFO_STALE:
618
                        /* Garbage Collection(RFC 2461 5.3) */
619
                        if (ln->ln_expire)
620
                                next = nd6_free(rt, 1);
621
                        break;
622
 
623
                case ND6_LLINFO_DELAY:
624
                        if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
625
                                /* We need NUD */
626
                                ln->ln_asked = 1;
627
                                ln->ln_state = ND6_LLINFO_PROBE;
628
                                ln->ln_expire = time_second +
629
                                        ndi->retrans / 1000;
630
                                nd6_ns_output(ifp, &dst->sin6_addr,
631
                                              &dst->sin6_addr,
632
                                              ln, 0);
633
                        } else {
634
                                ln->ln_state = ND6_LLINFO_STALE; /* XXX */
635
                                ln->ln_expire = time_second + nd6_gctimer;
636
                        }
637
                        break;
638
                case ND6_LLINFO_PROBE:
639
                        if (ln->ln_asked < nd6_umaxtries) {
640
                                ln->ln_asked++;
641
                                ln->ln_expire = time_second +
642
                                        nd_ifinfo[ifp->if_index].retrans / 1000;
643
                                nd6_ns_output(ifp, &dst->sin6_addr,
644
                                               &dst->sin6_addr, ln, 0);
645
                        } else {
646
                                next = nd6_free(rt, 0);
647
                        }
648
                        break;
649
                }
650
                ln = next;
651
        }
652
 
653
        /* expire default router list */
654
        dr = TAILQ_FIRST(&nd_defrouter);
655
        while (dr) {
656
                if (dr->expire && dr->expire < time_second) {
657
                        struct nd_defrouter *t;
658
                        t = TAILQ_NEXT(dr, dr_entry);
659
                        defrtrlist_del(dr);
660
                        dr = t;
661
                } else {
662
                        dr = TAILQ_NEXT(dr, dr_entry);
663
                }
664
        }
665
 
666
        /*
667
         * expire interface addresses.
668
         * in the past the loop was inside prefix expiry processing.
669
         * However, from a stricter speci-confrmance standpoint, we should
670
         * rather separate address lifetimes and prefix lifetimes.
671
         */
672
  addrloop:
673
        for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
674
                nia6 = ia6->ia_next;
675
                /* check address lifetime */
676
                lt6 = &ia6->ia6_lifetime;
677
                if (IFA6_IS_INVALID(ia6)) {
678
                        int regen = 0;
679
 
680
                        /*
681
                         * If the expiring address is temporary, try
682
                         * regenerating a new one.  This would be useful when
683
                         * we suspended a laptop PC, then turned it on after a
684
                         * period that could invalidate all temporary
685
                         * addresses.  Although we may have to restart the
686
                         * loop (see below), it must be after purging the
687
                         * address.  Otherwise, we'd see an infinite loop of
688
                         * regeneration.
689
                         */
690
                        if (ip6_use_tempaddr &&
691
                            (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
692
                                if (regen_tmpaddr(ia6) == 0)
693
                                        regen = 1;
694
                        }
695
 
696
                        in6_purgeaddr(&ia6->ia_ifa);
697
 
698
                        if (regen)
699
                                goto addrloop; /* XXX: see below */
700
                }
701
                if (IFA6_IS_DEPRECATED(ia6)) {
702
                        int oldflags = ia6->ia6_flags;
703
 
704
                        ia6->ia6_flags |= IN6_IFF_DEPRECATED;
705
 
706
                        /*
707
                         * If a temporary address has just become deprecated,
708
                         * regenerate a new one if possible.
709
                         */
710
                        if (ip6_use_tempaddr &&
711
                            (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
712
                            (oldflags & IN6_IFF_DEPRECATED) == 0) {
713
 
714
                                if (regen_tmpaddr(ia6) == 0) {
715
                                        /*
716
                                         * A new temporary address is
717
                                         * generated.
718
                                         * XXX: this means the address chain
719
                                         * has changed while we are still in
720
                                         * the loop.  Although the change
721
                                         * would not cause disaster (because
722
                                         * it's not a deletion, but an
723
                                         * addition,) we'd rather restart the
724
                                         * loop just for safety.  Or does this
725
                                         * significantly reduce performance??
726
                                         */
727
                                        goto addrloop;
728
                                }
729
                        }
730
                } else {
731
                        /*
732
                         * A new RA might have made a deprecated address
733
                         * preferred.
734
                         */
735
                        ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
736
                }
737
        }
738
 
739
        /* expire prefix list */
740
        pr = nd_prefix.lh_first;
741
        while (pr) {
742
                /*
743
                 * check prefix lifetime.
744
                 * since pltime is just for autoconf, pltime processing for
745
                 * prefix is not necessary.
746
                 */
747
                if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
748
                    time_second - pr->ndpr_lastupdate > pr->ndpr_vltime) {
749
                        struct nd_prefix *t;
750
                        t = pr->ndpr_next;
751
 
752
                        /*
753
                         * address expiration and prefix expiration are
754
                         * separate.  NEVER perform in6_purgeaddr here.
755
                         */
756
 
757
                        prelist_remove(pr);
758
                        pr = t;
759
                } else
760
                        pr = pr->ndpr_next;
761
        }
762
        splx(s);
763
}
764
 
765
static int
766
regen_tmpaddr(ia6)
767
        struct in6_ifaddr *ia6; /* deprecated/invalidated temporary address */
768
{
769
        struct ifaddr *ifa;
770
        struct ifnet *ifp;
771
        struct in6_ifaddr *public_ifa6 = NULL;
772
 
773
        ifp = ia6->ia_ifa.ifa_ifp;
774
#if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
775
        for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
776
#else
777
        for (ifa = ifp->if_addrlist.tqh_first; ifa;
778
             ifa = ifa->ifa_list.tqe_next)
779
#endif
780
        {
781
                struct in6_ifaddr *it6;
782
 
783
                if (ifa->ifa_addr->sa_family != AF_INET6)
784
                        continue;
785
 
786
                it6 = (struct in6_ifaddr *)ifa;
787
 
788
                /* ignore no autoconf addresses. */
789
                if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
790
                        continue;
791
 
792
                /* ignore autoconf addresses with different prefixes. */
793
                if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
794
                        continue;
795
 
796
                /*
797
                 * Now we are looking at an autoconf address with the same
798
                 * prefix as ours.  If the address is temporary and is still
799
                 * preferred, do not create another one.  It would be rare, but
800
                 * could happen, for example, when we resume a laptop PC after
801
                 * a long period.
802
                 */
803
                if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
804
                    !IFA6_IS_DEPRECATED(it6)) {
805
                        public_ifa6 = NULL;
806
                        break;
807
                }
808
 
809
                /*
810
                 * This is a public autoconf address that has the same prefix
811
                 * as ours.  If it is preferred, keep it.  We can't break the
812
                 * loop here, because there may be a still-preferred temporary
813
                 * address with the prefix.
814
                 */
815
                if (!IFA6_IS_DEPRECATED(it6))
816
                    public_ifa6 = it6;
817
        }
818
 
819
        if (public_ifa6 != NULL) {
820
                int e;
821
 
822
                if ((e = in6_tmpifadd(public_ifa6, 0)) != 0) {
823
                        log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
824
                            " tmp addr,errno=%d\n", e);
825
                        return(-1);
826
                }
827
                return(0);
828
        }
829
 
830
        return(-1);
831
}
832
 
833
/*
834
 * Nuke neighbor cache/prefix/default router management table, right before
835
 * ifp goes away.
836
 */
837
void
838
nd6_purge(ifp)
839
        struct ifnet *ifp;
840
{
841
        struct llinfo_nd6 *ln, *nln;
842
        struct nd_defrouter *dr, *ndr;
843
        struct nd_prefix *pr, *npr;
844
 
845
        /*
846
         * Nuke default router list entries toward ifp.
847
         * We defer removal of default router list entries that is installed
848
         * in the routing table, in order to keep additional side effects as
849
         * small as possible.
850
         */
851
        for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = ndr) {
852
                ndr = TAILQ_NEXT(dr, dr_entry);
853
                if (dr->installed)
854
                        continue;
855
 
856
                if (dr->ifp == ifp)
857
                        defrtrlist_del(dr);
858
        }
859
        for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = ndr) {
860
                ndr = TAILQ_NEXT(dr, dr_entry);
861
                if (!dr->installed)
862
                        continue;
863
 
864
                if (dr->ifp == ifp)
865
                        defrtrlist_del(dr);
866
        }
867
 
868
        /* Nuke prefix list entries toward ifp */
869
        for (pr = nd_prefix.lh_first; pr; pr = npr) {
870
                npr = pr->ndpr_next;
871
                if (pr->ndpr_ifp == ifp) {
872
                        /*
873
                         * Previously, pr->ndpr_addr is removed as well,
874
                         * but I strongly believe we don't have to do it.
875
                         * nd6_purge() is only called from in6_ifdetach(),
876
                         * which removes all the associated interface addresses
877
                         * by itself.
878
                         * (jinmei@kame.net 20010129)
879
                         */
880
                        prelist_remove(pr);
881
                }
882
        }
883
 
884
        /* cancel default outgoing interface setting */
885
        if (nd6_defifindex == ifp->if_index)
886
                nd6_setdefaultiface(0);
887
 
888
        if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
889
                /* refresh default router list */
890
                defrouter_select();
891
        }
892
 
893
        /*
894
         * Nuke neighbor cache entries for the ifp.
895
         * Note that rt->rt_ifp may not be the same as ifp,
896
         * due to KAME goto ours hack.  See RTM_RESOLVE case in
897
         * nd6_rtrequest(), and ip6_input().
898
         */
899
        ln = llinfo_nd6.ln_next;
900
        while (ln && ln != &llinfo_nd6) {
901
                struct rtentry *rt;
902
                struct sockaddr_dl *sdl;
903
 
904
                nln = ln->ln_next;
905
                rt = ln->ln_rt;
906
                if (rt && rt->rt_gateway &&
907
                    rt->rt_gateway->sa_family == AF_LINK) {
908
                        sdl = (struct sockaddr_dl *)rt->rt_gateway;
909
                        if (sdl->sdl_index == ifp->if_index)
910
                                nln = nd6_free(rt, 0);
911
                }
912
                ln = nln;
913
        }
914
}
915
 
916
struct rtentry *
917
nd6_lookup(addr6, create, ifp)
918
        struct in6_addr *addr6;
919
        int create;
920
        struct ifnet *ifp;
921
{
922
        struct rtentry *rt;
923
        struct sockaddr_in6 sin6;
924
#ifdef SCOPEDROUTING
925
        int64_t zoneid;
926
#endif
927
 
928
        bzero(&sin6, sizeof(sin6));
929
        sin6.sin6_len = sizeof(struct sockaddr_in6);
930
        sin6.sin6_family = AF_INET6;
931
        sin6.sin6_addr = *addr6;
932
#ifdef SCOPEDROUTING
933
        if ((zoneid = in6_addr2zoneid(ifp, addr6)) < 0)
934
                return(NULL);
935
        sin6.sin6_scope_id = zoneid;
936
#endif
937
        rt = rtalloc1((struct sockaddr *)&sin6, create
938
#ifdef __FreeBSD__
939
                      , 0UL
940
#endif /* __FreeBSD__ */
941
                      );
942
        if (rt && (rt->rt_flags & RTF_LLINFO) == 0) {
943
                /*
944
                 * This is the case for the default route.
945
                 * If we want to create a neighbor cache for the address, we
946
                 * should free the route for the destination and allocate an
947
                 * interface route.
948
                 */
949
                if (create) {
950
                        RTFREE(rt);
951
                        rt = 0;
952
                }
953
        }
954
        if (!rt) {
955
                if (create && ifp) {
956
                        int e;
957
 
958
                        /*
959
                         * If no route is available and create is set,
960
                         * we allocate a host route for the destination
961
                         * and treat it like an interface route.
962
                         * This hack is necessary for a neighbor which can't
963
                         * be covered by our own prefix.
964
                         */
965
                        struct ifaddr *ifa =
966
                                ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
967
                        if (ifa == NULL)
968
                                return(NULL);
969
 
970
                        /*
971
                         * Create a new route.  RTF_LLINFO is necessary
972
                         * to create a Neighbor Cache entry for the
973
                         * destination in nd6_rtrequest which will be
974
                         * called in rtrequest via ifa->ifa_rtrequest.
975
                         */
976
                        if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
977
                                           ifa->ifa_addr,
978
                                           (struct sockaddr *)&all1_sa,
979
                                           (ifa->ifa_flags |
980
                                            RTF_HOST | RTF_LLINFO) &
981
                                           ~RTF_CLONING,
982
                                           &rt)) != 0) {
983
#if 0
984
                                log(LOG_ERR,
985
                                    "nd6_lookup: failed to add route for a "
986
                                    "neighbor(%s), errno=%d\n",
987
                                    ip6_sprintf(addr6), e);
988
#endif
989
                                return(NULL);
990
                        }
991
                        if (rt == NULL)
992
                                return(NULL);
993
                        if (rt->rt_llinfo) {
994
                                struct llinfo_nd6 *ln =
995
                                        (struct llinfo_nd6 *)rt->rt_llinfo;
996
                                ln->ln_state = ND6_LLINFO_NOSTATE;
997
                        }
998
                } else
999
                        return(NULL);
1000
        }
1001
        rt->rt_refcnt--;
1002
        /*
1003
         * Validation for the entry.
1004
         * XXX: we can't use rt->rt_ifp to check for the interface, since
1005
         *      it might be the loopback interface if the entry is for our
1006
         *      own address on a non-loopback interface. Instead, we should
1007
         *      use rt->rt_ifa->ifa_ifp, which would specify the REAL interface.
1008
         */
1009
        if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
1010
            rt->rt_gateway->sa_family != AF_LINK ||
1011
            (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
1012
                if (create) {
1013
                        log(LOG_DEBUG, "nd6_lookup: failed to lookup %s (if = %s)\n",
1014
                            ip6_sprintf(addr6), ifp ? if_name(ifp) : "unspec");
1015
                }
1016
                return(0);
1017
        }
1018
        return(rt);
1019
}
1020
 
1021
/*
1022
 * Detect if a given IPv6 address identifies a neighbor on a given link.
1023
 * XXX: should take care of the destination of a p2p link?
1024
 */
1025
int
1026
nd6_is_addr_neighbor(addr, ifp)
1027
        struct sockaddr_in6 *addr;
1028
        struct ifnet *ifp;
1029
{
1030
        struct nd_prefix *pr;
1031
        struct rtentry *rt;
1032
        int i;
1033
 
1034
#define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
1035
#define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
1036
 
1037
        /*
1038
         * A link-local address is always a neighbor.
1039
         * XXX: we should use the sin6_scope_id field rather than the embedded
1040
         * interface index.
1041
         * XXX: a link does not necessarily specify a single interface.
1042
         */
1043
        if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
1044
            ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
1045
                return(1);
1046
 
1047
        /*
1048
         * If the address matches one of our on-link prefixes, it should be a
1049
         * neighbor.
1050
         */
1051
        for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1052
 
1053
                if (pr->ndpr_ifp != ifp)
1054
                        continue;
1055
 
1056
                if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
1057
                        continue;
1058
 
1059
                for (i = 0; i < 4; i++) {
1060
                        if ((pr->ndpr_mask.s6_addr32[i] &
1061
                             addr->sin6_addr.s6_addr32[i]) !=
1062
                            pr->ndpr_prefix.sin6_addr.s6_addr32[i])
1063
                                break;
1064
                }
1065
                if (i == 4)     /* full match */
1066
                        return(1);
1067
        }
1068
 
1069
        /*
1070
         * If the default router list is empty, all addresses are regarded
1071
         * as on-link, and thus, as a neighbor.
1072
         * XXX: we restrict the condition to hosts, because routers usually do
1073
         * not have the "default router list".
1074
         */
1075
        if (!ip6_forwarding && TAILQ_FIRST(&nd_defrouter) == NULL &&
1076
            nd6_defifindex == ifp->if_index) {
1077
                return(1);
1078
        }
1079
 
1080
        /*
1081
         * Even if the address matches none of our addresses, it might be
1082
         * in the neighbor cache.
1083
         */
1084
        if ((rt = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL &&
1085
            rt->rt_llinfo != NULL)
1086
                return(1);
1087
 
1088
        return(0);
1089
#undef IFADDR6
1090
#undef IFMASK6
1091
}
1092
 
1093
/*
1094
 * Free an nd6 llinfo entry.
1095
 * Since the function would cause significant changes in the kernel, DO NOT
1096
 * make it global, unless you have a strong reason for the change, and are sure
1097
 * that the change is safe.
1098
 */
1099
static struct llinfo_nd6 *
1100
nd6_free(rt, gc)
1101
        struct rtentry *rt;
1102
        int gc;
1103
{
1104
        struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
1105
        struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
1106
        struct nd_defrouter *dr;
1107
 
1108
        /*
1109
         * we used to have pfctlinput(PRC_HOSTDEAD) here.
1110
         * even though it is not harmful, it was not really necessary.
1111
         */
1112
 
1113
        if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
1114
                int s;
1115
#ifdef __NetBSD__
1116
                s = splsoftnet();
1117
#else
1118
                s = splnet();
1119
#endif
1120
                dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1121
                                      rt->rt_ifp);
1122
 
1123
                if (dr != NULL && dr->expire &&
1124
                    ln->ln_state == ND6_LLINFO_STALE && gc) {
1125
                        /*
1126
                         * If the reason for the deletion is just garbage
1127
                         * collection, and the neighbor is an active default
1128
                         * router, do not delete it.  Instead, reset the GC
1129
                         * timer using the router's lifetime.
1130
                         * Simply deleting the entry would affect default
1131
                         * router selection, which is not necessarily a good
1132
                         * thing, especially when we're using router preference
1133
                         * values.
1134
                         * XXX: the check for ln_state would be redundant,
1135
                         *      but we intentionally keep it just in case.
1136
                         */
1137
                        ln->ln_expire = dr->expire;
1138
                        splx(s);
1139
                        return(ln->ln_next);
1140
                }
1141
 
1142
                if (ln->ln_router || dr) {
1143
                        /*
1144
                         * rt6_flush must be called whether or not the neighbor
1145
                         * is in the Default Router List.
1146
                         * See a corresponding comment in nd6_na_input().
1147
                         */
1148
                        rt6_flush(&in6, rt->rt_ifp);
1149
                }
1150
 
1151
                if (dr) {
1152
                        /*
1153
                         * Unreachablity of a router might affect the default
1154
                         * router selection and on-link detection of advertised
1155
                         * prefixes.
1156
                         */
1157
 
1158
                        /*
1159
                         * Temporarily fake the state to choose a new default
1160
                         * router and to perform on-link determination of
1161
                         * prefixes correctly.
1162
                         * Below the state will be set correctly,
1163
                         * or the entry itself will be deleted.
1164
                         */
1165
                        ln->ln_state = ND6_LLINFO_INCOMPLETE;
1166
 
1167
                        /*
1168
                         * Since defrouter_select() does not affect the
1169
                         * on-link determination and MIP6 needs the check
1170
                         * before the default router selection, we perform
1171
                         * the check now.
1172
                         */
1173
                        pfxlist_onlink_check();
1174
 
1175
                        /*
1176
                         * refresh default router list
1177
                         */
1178
                        defrouter_select();
1179
                }
1180
                splx(s);
1181
        }
1182
 
1183
        /*
1184
         * Before deleting the entry, remember the next entry as the
1185
         * return value.  We need this because pfxlist_onlink_check() above
1186
         * might have freed other entries (particularly the old next entry) as
1187
         * a side effect (XXX).
1188
         */
1189
        next = ln->ln_next;
1190
 
1191
        /*
1192
         * Detach the route from the routing tree and the list of neighbor
1193
         * caches, and disable the route entry not to be used in already
1194
         * cached routes.
1195
         */
1196
        rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
1197
                  rt_mask(rt), 0, (struct rtentry **)0);
1198
 
1199
        return(next);
1200
}
1201
 
1202
/*
1203
 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1204
 *
1205
 * XXX cost-effective metods?
1206
 */
1207
void
1208
nd6_nud_hint(rt, dst6, force)
1209
        struct rtentry *rt;
1210
        struct in6_addr *dst6;
1211
        int force;
1212
{
1213
        struct llinfo_nd6 *ln;
1214
#if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
1215
        long time_second = time.tv_sec;
1216
#endif
1217
 
1218
        /*
1219
         * If the caller specified "rt", use that.  Otherwise, resolve the
1220
         * routing table by supplied "dst6".
1221
         */
1222
        if (!rt) {
1223
                if (!dst6)
1224
                        return;
1225
                if (!(rt = nd6_lookup(dst6, 0, NULL)))
1226
                        return;
1227
        }
1228
 
1229
        if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
1230
            (rt->rt_flags & RTF_LLINFO) == 0 ||
1231
            !rt->rt_llinfo || !rt->rt_gateway ||
1232
            rt->rt_gateway->sa_family != AF_LINK) {
1233
                /* This is not a host route. */
1234
                return;
1235
        }
1236
 
1237
        ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1238
        if (ln->ln_state < ND6_LLINFO_REACHABLE)
1239
                return;
1240
 
1241
        /*
1242
         * if we get upper-layer reachability confirmation many times,
1243
         * it is possible we have false information.
1244
         */
1245
        if (!force) {
1246
                ln->ln_byhint++;
1247
                if (ln->ln_byhint > nd6_maxnudhint)
1248
                        return;
1249
        }
1250
 
1251
        ln->ln_state = ND6_LLINFO_REACHABLE;
1252
        if (ln->ln_expire)
1253
                ln->ln_expire = time_second +
1254
                        nd_ifinfo[rt->rt_ifp->if_index].reachable;
1255
}
1256
 
1257
void
1258
#if (defined(__bsdi__) && _BSDI_VERSION >= 199802) || defined(__NetBSD__) || defined(__OpenBSD__)
1259
nd6_rtrequest(req, rt, info)
1260
        int     req;
1261
        struct rtentry *rt;
1262
        struct rt_addrinfo *info; /* xxx unused */
1263
#else
1264
nd6_rtrequest(req, rt, sa)
1265
        int     req;
1266
        struct rtentry *rt;
1267
        struct sockaddr *sa; /* xxx unused */
1268
#endif
1269
{
1270
        struct sockaddr *gate = rt->rt_gateway;
1271
        struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1272
        static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1273
        struct ifnet *ifp = rt->rt_ifp;
1274
        struct ifaddr *ifa;
1275
#if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
1276
        long time_second = time.tv_sec;
1277
#endif
1278
 
1279
        if ((rt->rt_flags & RTF_GATEWAY))
1280
                return;
1281
 
1282
        if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
1283
                /*
1284
                 * This is probably an interface direct route for a link
1285
                 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1286
                 * We do not need special treatment below for such a route.
1287
                 * Moreover, the RTF_LLINFO flag which would be set below
1288
                 * would annoy the ndp(8) command.
1289
                 */
1290
                return;
1291
        }
1292
 
1293
        if (req == RTM_RESOLVE &&
1294
            !nd6_is_addr_neighbor((struct sockaddr_in6 *)rt_key(rt), ifp)) {
1295
                /*
1296
                 * FreeBSD and BSD/OS often make a cloned host route based
1297
                 * on a less-specific route (e.g. the default route).
1298
                 * If the less specific route does not have a "gateway"
1299
                 * (this is the case when the route just goes to a p2p
1300
                 * interface), we'll mistakenly make a neighbor cache for
1301
                 * the host route, and will see strange neighbor solicitation
1302
                 * for the corresponding destination.  In order to avoid the
1303
                 * confusion, we check if the destination of the route is
1304
                 * a neighbor in terms of neighbor discovery, and stop the
1305
                 * process if not.  Additionally, we remove the LLINFO flag
1306
                 * so that ndp(8) will not try to get the neighbor information
1307
                 * of the destination.
1308
                 */
1309
                rt->rt_flags &= ~RTF_LLINFO;
1310
                return;
1311
        }
1312
 
1313
        switch (req) {
1314
        case RTM_ADD:
1315
                /*
1316
                 * There is no backward compatibility :)
1317
                 *
1318
                 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1319
                 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1320
                 *         rt->rt_flags |= RTF_CLONING;
1321
                 */
1322
                if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
1323
                        /*
1324
                         * Case 1: This route should come from
1325
                         * a route to interface.  RTF_LLINFO flag is set
1326
                         * for a host route whose destination should be
1327
                         * treated as on-link.
1328
                         */
1329
                        rt_setgate(rt, rt_key(rt),
1330
                                   (struct sockaddr *)&null_sdl);
1331
                        gate = rt->rt_gateway;
1332
                        SDL(gate)->sdl_type = ifp->if_type;
1333
                        SDL(gate)->sdl_index = ifp->if_index;
1334
                        if (ln)
1335
                                ln->ln_expire = time_second;
1336
#if 1
1337
                        if (ln && ln->ln_expire == 0) {
1338
                                /* kludge for desktops */
1339
#if 0
1340
                                printf("nd6_rtequest: time.tv_sec is zero; "
1341
                                       "treat it as 1\n");
1342
#endif
1343
                                ln->ln_expire = 1;
1344
                        }
1345
#endif
1346
                        if ((rt->rt_flags & RTF_CLONING))
1347
                                break;
1348
                }
1349
                /*
1350
                 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1351
                 * We don't do that here since llinfo is not ready yet.
1352
                 *
1353
                 * There are also couple of other things to be discussed:
1354
                 * - unsolicited NA code needs improvement beforehand
1355
                 * - RFC2461 says we MAY send multicast unsolicited NA
1356
                 *   (7.2.6 paragraph 4), however, it also says that we
1357
                 *   SHOULD provide a mechanism to prevent multicast NA storm.
1358
                 *   we don't have anything like it right now.
1359
                 *   note that the mechanism needs a mutual agreement
1360
                 *   between proxies, which means that we need to implement
1361
                 *   a new protocol, or a new kludge.
1362
                 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1363
                 *   we need to check ip6forwarding before sending it.
1364
                 *   (or should we allow proxy ND configuration only for
1365
                 *   routers?  there's no mention about proxy ND from hosts)
1366
                 */
1367
#if 0
1368
                /* XXX it does not work */
1369
                if (rt->rt_flags & RTF_ANNOUNCE)
1370
                        nd6_na_output(ifp,
1371
                              &SIN6(rt_key(rt))->sin6_addr,
1372
                              &SIN6(rt_key(rt))->sin6_addr,
1373
                              ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1374
                              1, NULL);
1375
#endif
1376
                /* FALLTHROUGH */
1377
        case RTM_RESOLVE:
1378
                if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1379
                        /*
1380
                         * Address resolution isn't necessary for a point to
1381
                         * point link, so we can skip this test for a p2p link.
1382
                         */
1383
                        if (gate->sa_family != AF_LINK ||
1384
                            gate->sa_len < sizeof(null_sdl)) {
1385
                                log(LOG_DEBUG,
1386
                                    "nd6_rtrequest: bad gateway value: %s\n",
1387
                                    if_name(ifp));
1388
                                break;
1389
                        }
1390
                        SDL(gate)->sdl_type = ifp->if_type;
1391
                        SDL(gate)->sdl_index = ifp->if_index;
1392
                }
1393
                if (ln != NULL)
1394
                        break;  /* This happens on a route change */
1395
                /*
1396
                 * Case 2: This route may come from cloning, or a manual route
1397
                 * add with a LL address.
1398
                 */
1399
                R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1400
                rt->rt_llinfo = (caddr_t)ln;
1401
                if (!ln) {
1402
                        log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1403
                        break;
1404
                }
1405
                nd6_inuse++;
1406
                nd6_allocated++;
1407
                Bzero(ln, sizeof(*ln));
1408
                ln->ln_rt = rt;
1409
                /* this is required for "ndp" command. - shin */
1410
                if (req == RTM_ADD) {
1411
                        /*
1412
                         * gate should have some valid AF_LINK entry,
1413
                         * and ln->ln_expire should have some lifetime
1414
                         * which is specified by ndp command.
1415
                         */
1416
                        ln->ln_state = ND6_LLINFO_REACHABLE;
1417
                        ln->ln_byhint = 0;
1418
                } else {
1419
                        /*
1420
                         * When req == RTM_RESOLVE, rt is created and
1421
                         * initialized in rtrequest(), so rt_expire is 0.
1422
                         */
1423
                        ln->ln_state = ND6_LLINFO_NOSTATE;
1424
                        ln->ln_expire = time_second;
1425
                }
1426
                rt->rt_flags |= RTF_LLINFO;
1427
                ln->ln_next = llinfo_nd6.ln_next;
1428
                llinfo_nd6.ln_next = ln;
1429
                ln->ln_prev = &llinfo_nd6;
1430
                ln->ln_next->ln_prev = ln;
1431
 
1432
                /*
1433
                 * check if rt_key(rt) is one of my address assigned
1434
                 * to the interface.
1435
                 */
1436
                ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1437
                                          &SIN6(rt_key(rt))->sin6_addr);
1438
                if (ifa) {
1439
                        caddr_t macp = nd6_ifptomac(ifp);
1440
                        ln->ln_expire = 0;
1441
                        ln->ln_state = ND6_LLINFO_REACHABLE;
1442
                        ln->ln_byhint = 0;
1443
                        if (macp) {
1444
                                Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1445
                                SDL(gate)->sdl_alen = ifp->if_addrlen;
1446
                        }
1447
                        if (nd6_useloopback) {
1448
#ifdef __bsdi__
1449
#if _BSDI_VERSION >= 199802
1450
                                extern struct ifnet *loifp;
1451
                                rt->rt_ifp = loifp;     /* XXX */
1452
#else
1453
                                extern struct ifnet loif;
1454
                                rt->rt_ifp = &loif;     /* XXX */
1455
#endif
1456
#elif defined(__OpenBSD__)
1457
                                rt->rt_ifp = lo0ifp;    /* XXX */
1458
#else
1459
                                rt->rt_ifp = &loif[0];   /* XXX */
1460
#endif
1461
                                /*
1462
                                 * Make sure rt_ifa be equal to the ifaddr
1463
                                 * corresponding to the address.
1464
                                 * We need this because when we refer
1465
                                 * rt_ifa->ia6_flags in ip6_input, we assume
1466
                                 * that the rt_ifa points to the address instead
1467
                                 * of the loopback address.
1468
                                 */
1469
                                if (ifa != rt->rt_ifa) {
1470
                                        IFAFREE(rt->rt_ifa);
1471
                                        IFAREF(ifa);
1472
                                        rt->rt_ifa = ifa;
1473
                                }
1474
                        }
1475
                } else if (rt->rt_flags & RTF_ANNOUNCE) {
1476
                        ln->ln_expire = 0;
1477
                        ln->ln_state = ND6_LLINFO_REACHABLE;
1478
                        ln->ln_byhint = 0;
1479
 
1480
                        /* join solicited node multicast for proxy ND */
1481
                        if (ifp->if_flags & IFF_MULTICAST) {
1482
                                struct in6_addr llsol;
1483
                                int error;
1484
 
1485
                                llsol = SIN6(rt_key(rt))->sin6_addr;
1486
                                llsol.s6_addr16[0] = htons(0xff02);
1487
                                llsol.s6_addr16[1] = htons(ifp->if_index);
1488
                                llsol.s6_addr32[1] = 0;
1489
                                llsol.s6_addr32[2] = htonl(1);
1490
                                llsol.s6_addr8[12] = 0xff;
1491
 
1492
                                if (!in6_addmulti(&llsol, ifp, &error)) {
1493
                                        nd6log((LOG_ERR, "%s: failed to join "
1494
                                            "%s (errno=%d)\n", if_name(ifp),
1495
                                            ip6_sprintf(&llsol), error));
1496
                                }
1497
                        }
1498
                }
1499
                break;
1500
 
1501
        case RTM_DELETE:
1502
                if (!ln)
1503
                        break;
1504
                /* leave from solicited node multicast for proxy ND */
1505
                if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1506
                    (ifp->if_flags & IFF_MULTICAST) != 0) {
1507
                        struct in6_addr llsol;
1508
                        struct in6_multi *in6m;
1509
 
1510
                        llsol = SIN6(rt_key(rt))->sin6_addr;
1511
                        llsol.s6_addr16[0] = htons(0xff02);
1512
                        llsol.s6_addr16[1] = htons(ifp->if_index);
1513
                        llsol.s6_addr32[1] = 0;
1514
                        llsol.s6_addr32[2] = htonl(1);
1515
                        llsol.s6_addr8[12] = 0xff;
1516
 
1517
                        IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1518
                        if (in6m)
1519
                                in6_delmulti(in6m);
1520
                }
1521
                nd6_inuse--;
1522
                ln->ln_next->ln_prev = ln->ln_prev;
1523
                ln->ln_prev->ln_next = ln->ln_next;
1524
                ln->ln_prev = NULL;
1525
                rt->rt_llinfo = 0;
1526
                rt->rt_flags &= ~RTF_LLINFO;
1527
                if (ln->ln_hold)
1528
                        m_freem(ln->ln_hold);
1529
                Free((caddr_t)ln);
1530
        }
1531
}
1532
 
1533
int
1534
nd6_ioctl(cmd, data, ifp)
1535
        u_long cmd;
1536
        caddr_t data;
1537
        struct ifnet *ifp;
1538
{
1539
        struct in6_drlist *drl = (struct in6_drlist *)data;
1540
        struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1541
        struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1542
        struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1543
        struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1544
        struct nd_defrouter *dr;
1545
        struct nd_prefix *pr;
1546
        struct rtentry *rt;
1547
        int i = 0, error = 0;
1548
        int s;
1549
 
1550
        switch (cmd) {
1551
        case SIOCGDRLST_IN6:
1552
                /*
1553
                 * obsolete API, use sysctl under net.inet6.icmp6
1554
                 */
1555
                bzero(drl, sizeof(*drl));
1556
#ifdef __NetBSD__
1557
                s = splsoftnet();
1558
#else
1559
                s = splnet();
1560
#endif
1561
                dr = TAILQ_FIRST(&nd_defrouter);
1562
                while (dr && i < DRLSTSIZ) {
1563
                        drl->defrouter[i].rtaddr = dr->rtaddr;
1564
                        if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1565
                                /* XXX: need to this hack for KAME stack */
1566
                                drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1567
                        } else
1568
                                log(LOG_ERR,
1569
                                    "default router list contains a "
1570
                                    "non-linklocal address(%s)\n",
1571
                                    ip6_sprintf(&drl->defrouter[i].rtaddr));
1572
 
1573
                        drl->defrouter[i].flags = dr->flags;
1574
                        drl->defrouter[i].rtlifetime = dr->rtlifetime;
1575
                        drl->defrouter[i].expire = dr->expire;
1576
                        drl->defrouter[i].if_index = dr->ifp->if_index;
1577
                        i++;
1578
                        dr = TAILQ_NEXT(dr, dr_entry);
1579
                }
1580
                splx(s);
1581
                break;
1582
        case SIOCGPRLST_IN6:
1583
                /*
1584
                 * obsolete API, use sysctl under net.inet6.icmp6
1585
                 *
1586
                 * XXX the structure in6_prlist was changed in backward-
1587
                 * incompatible manner.  in6_oprlist is used for SIOCGPRLST_IN6,
1588
                 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1589
                 */
1590
                /*
1591
                 * XXX meaning of fields, especialy "raflags", is very
1592
                 * differnet between RA prefix list and RR/static prefix list.
1593
                 * how about separating ioctls into two?
1594
                 */
1595
                bzero(oprl, sizeof(*oprl));
1596
#ifdef __NetBSD__
1597
                s = splsoftnet();
1598
#else
1599
                s = splnet();
1600
#endif
1601
                pr = nd_prefix.lh_first;
1602
                while (pr && i < PRLSTSIZ) {
1603
                        struct nd_pfxrouter *pfr;
1604
                        int j;
1605
 
1606
                        (void)in6_embedscope(&oprl->prefix[i].prefix,
1607
                                             &pr->ndpr_prefix);
1608
                        oprl->prefix[i].raflags = pr->ndpr_raf;
1609
                        oprl->prefix[i].prefixlen = pr->ndpr_plen;
1610
                        oprl->prefix[i].vltime = pr->ndpr_vltime;
1611
                        oprl->prefix[i].pltime = pr->ndpr_pltime;
1612
                        oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1613
                        if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1614
                                oprl->prefix[i].expire = 0;
1615
                        else {
1616
                                time_t maxexpire;
1617
 
1618
                                /* XXX: we assume time_t is signed. */
1619
                                maxexpire = (-1) &
1620
                                        ~(1 << ((sizeof(maxexpire) * 8) - 1));
1621
                                if (pr->ndpr_vltime <
1622
                                    maxexpire - pr->ndpr_lastupdate) {
1623
                                        oprl->prefix[i].expire =
1624
                                                 pr->ndpr_lastupdate +
1625
                                                pr->ndpr_vltime;
1626
                                } else
1627
                                        oprl->prefix[i].expire = maxexpire;
1628
                        }
1629
                        pfr = pr->ndpr_advrtrs.lh_first;
1630
                        j = 0;
1631
                        while (pfr) {
1632
                                if (j < DRLSTSIZ) {
1633
#define RTRADDR oprl->prefix[i].advrtr[j]
1634
                                        RTRADDR = pfr->router->rtaddr;
1635
                                        if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1636
                                                /* XXX: hack for KAME */
1637
                                                RTRADDR.s6_addr16[1] = 0;
1638
                                        } else
1639
                                                log(LOG_ERR,
1640
                                                    "a router(%s) advertises "
1641
                                                    "a prefix with "
1642
                                                    "non-link local address\n",
1643
                                                    ip6_sprintf(&RTRADDR));
1644
#undef RTRADDR
1645
                                }
1646
                                j++;
1647
                                pfr = pfr->pfr_next;
1648
                        }
1649
                        oprl->prefix[i].advrtrs = j;
1650
                        oprl->prefix[i].origin = PR_ORIG_RA;
1651
 
1652
                        i++;
1653
                        pr = pr->ndpr_next;
1654
                }
1655
                splx(s);
1656
 
1657
                break;
1658
        case OSIOCGIFINFO_IN6:
1659
                if (!nd_ifinfo || i >= nd_ifinfo_indexlim) {
1660
                        error = EINVAL;
1661
                        break;
1662
                }
1663
                ndi->ndi.linkmtu = nd_ifinfo[ifp->if_index].linkmtu;
1664
                ndi->ndi.maxmtu = nd_ifinfo[ifp->if_index].maxmtu;
1665
                ndi->ndi.basereachable =
1666
                    nd_ifinfo[ifp->if_index].basereachable;
1667
                ndi->ndi.reachable = nd_ifinfo[ifp->if_index].reachable;
1668
                ndi->ndi.retrans = nd_ifinfo[ifp->if_index].retrans;
1669
                ndi->ndi.flags = nd_ifinfo[ifp->if_index].flags;
1670
                ndi->ndi.recalctm = nd_ifinfo[ifp->if_index].recalctm;
1671
                ndi->ndi.chlim = nd_ifinfo[ifp->if_index].chlim;
1672
                ndi->ndi.receivedra = nd_ifinfo[ifp->if_index].receivedra;
1673
                break;
1674
        case SIOCGIFINFO_IN6:
1675
                if (!nd_ifinfo || i >= nd_ifinfo_indexlim) {
1676
                        error = EINVAL;
1677
                        break;
1678
                }
1679
                ndi->ndi = nd_ifinfo[ifp->if_index];
1680
                break;
1681
        case SIOCSIFINFO_FLAGS:
1682
                /* XXX: almost all other fields of ndi->ndi is unused */
1683
                if (!nd_ifinfo || i >= nd_ifinfo_indexlim) {
1684
                        error = EINVAL;
1685
                        break;
1686
                }
1687
                nd_ifinfo[ifp->if_index].flags = ndi->ndi.flags;
1688
                break;
1689
        case SIOCSNDFLUSH_IN6:  /* XXX: the ioctl name is confusing... */
1690
                /* sync kernel routing table with the default router list */
1691
                defrouter_reset();
1692
                defrouter_select();
1693
                break;
1694
        case SIOCSPFXFLUSH_IN6:
1695
            {
1696
                /* flush all the prefix advertised by routers */
1697
                struct nd_prefix *pr, *next;
1698
 
1699
#ifdef __NetBSD__
1700
                s = splsoftnet();
1701
#else
1702
                s = splnet();
1703
#endif
1704
                for (pr = nd_prefix.lh_first; pr; pr = next) {
1705
                        struct in6_ifaddr *ia, *ia_next;
1706
 
1707
                        next = pr->ndpr_next;
1708
 
1709
                        if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1710
                                continue; /* XXX */
1711
 
1712
                        /* do we really have to remove addresses as well? */
1713
                        for (ia = in6_ifaddr; ia; ia = ia_next) {
1714
                                /* ia might be removed.  keep the next ptr. */
1715
                                ia_next = ia->ia_next;
1716
 
1717
                                if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1718
                                        continue;
1719
 
1720
                                if (ia->ia6_ndpr == pr)
1721
                                        in6_purgeaddr(&ia->ia_ifa);
1722
                        }
1723
                        prelist_remove(pr);
1724
                }
1725
                splx(s);
1726
                break;
1727
            }
1728
        case SIOCSRTRFLUSH_IN6:
1729
            {
1730
                /* flush all the default routers */
1731
                struct nd_defrouter *dr, *next;
1732
 
1733
#ifdef __NetBSD__
1734
                s = splsoftnet();
1735
#else
1736
                s = splnet();
1737
#endif
1738
                defrouter_reset();
1739
                for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = next) {
1740
                        next = TAILQ_NEXT(dr, dr_entry);
1741
                        defrtrlist_del(dr);
1742
                }
1743
                defrouter_select();
1744
                splx(s);
1745
                break;
1746
            }
1747
        case SIOCGNBRINFO_IN6:
1748
            {
1749
                struct llinfo_nd6 *ln;
1750
                struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1751
 
1752
                /*
1753
                 * XXX: KAME specific hack for scoped addresses
1754
                 *      XXXX: for other scopes than link-local?
1755
                 */
1756
                if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1757
                    IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1758
                        u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1759
 
1760
                        if (*idp == 0)
1761
                                *idp = htons(ifp->if_index);
1762
                }
1763
 
1764
#ifdef __NetBSD__
1765
                s = splsoftnet();
1766
#else
1767
                s = splnet();
1768
#endif
1769
                if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL ||
1770
                    (ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) {
1771
                        error = EINVAL;
1772
                        splx(s);
1773
                        break;
1774
                }
1775
                nbi->state = ln->ln_state;
1776
                nbi->asked = ln->ln_asked;
1777
                nbi->isrouter = ln->ln_router;
1778
                nbi->expire = ln->ln_expire;
1779
                splx(s);
1780
 
1781
                break;
1782
            }
1783
        case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1784
                ndif->ifindex = nd6_defifindex;
1785
                break;
1786
        case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1787
                return(nd6_setdefaultiface(ndif->ifindex));
1788
                break;
1789
        }
1790
        return(error);
1791
}
1792
 
1793
/*
1794
 * Create neighbor cache entry and cache link-layer address,
1795
 * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
1796
 */
1797
struct rtentry *
1798
nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code)
1799
        struct ifnet *ifp;
1800
        struct in6_addr *from;
1801
        char *lladdr;
1802
        int lladdrlen;
1803
        int type;       /* ICMP6 type */
1804
        int code;       /* type dependent information */
1805
{
1806
        struct rtentry *rt = NULL;
1807
        struct llinfo_nd6 *ln = NULL;
1808
        int is_newentry;
1809
        struct sockaddr_dl *sdl = NULL;
1810
        int do_update;
1811
        int olladdr;
1812
        int llchange;
1813
        int newstate = 0;
1814
#if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
1815
        long time_second = time.tv_sec;
1816
#endif
1817
 
1818
        if (!ifp)
1819
                panic("ifp == NULL in nd6_cache_lladdr");
1820
        if (!from)
1821
                panic("from == NULL in nd6_cache_lladdr");
1822
 
1823
        /* nothing must be updated for unspecified address */
1824
        if (IN6_IS_ADDR_UNSPECIFIED(from))
1825
                return NULL;
1826
 
1827
        /*
1828
         * Validation about ifp->if_addrlen and lladdrlen must be done in
1829
         * the caller.
1830
         *
1831
         * XXX If the link does not have link-layer adderss, what should
1832
         * we do? (ifp->if_addrlen == 0)
1833
         * Spec says nothing in sections for RA, RS and NA.  There's small
1834
         * description on it in NS section (RFC 2461 7.2.3).
1835
         */
1836
 
1837
        rt = nd6_lookup(from, 0, ifp);
1838
        if (!rt) {
1839
#if 0
1840
                /* nothing must be done if there's no lladdr */
1841
                if (!lladdr || !lladdrlen)
1842
                        return NULL;
1843
#endif
1844
 
1845
                rt = nd6_lookup(from, 1, ifp);
1846
                is_newentry = 1;
1847
        } else {
1848
                /* do nothing if static ndp is set */
1849
                if (rt->rt_flags & RTF_STATIC)
1850
                        return NULL;
1851
                is_newentry = 0;
1852
        }
1853
 
1854
        if (!rt)
1855
                return NULL;
1856
        if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1857
fail:
1858
                (void)nd6_free(rt, 0);
1859
                return NULL;
1860
        }
1861
        ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1862
        if (!ln)
1863
                goto fail;
1864
        if (!rt->rt_gateway)
1865
                goto fail;
1866
        if (rt->rt_gateway->sa_family != AF_LINK)
1867
                goto fail;
1868
        sdl = SDL(rt->rt_gateway);
1869
 
1870
        olladdr = (sdl->sdl_alen) ? 1 : 0;
1871
        if (olladdr && lladdr) {
1872
                if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1873
                        llchange = 1;
1874
                else
1875
                        llchange = 0;
1876
        } else
1877
                llchange = 0;
1878
 
1879
        /*
1880
         * newentry olladdr  lladdr  llchange   (*=record)
1881
         *      0        n       n       --      (1)
1882
         *      0        y       n       --      (2)
1883
         *      0        n       y       --      (3) * STALE
1884
         *      0        y       y       n       (4) *
1885
         *      0        y       y       y       (5) * STALE
1886
         *      1       --      n       --      (6)   NOSTATE(= PASSIVE)
1887
         *      1       --      y       --      (7) * STALE
1888
         */
1889
 
1890
        if (lladdr) {           /* (3-5) and (7) */
1891
                /*
1892
                 * Record source link-layer address
1893
                 * XXX is it dependent to ifp->if_type?
1894
                 */
1895
                sdl->sdl_alen = ifp->if_addrlen;
1896
                bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1897
        }
1898
 
1899
        if (!is_newentry) {
1900
                if ((!olladdr && lladdr)                /* (3) */
1901
                 || (olladdr && lladdr && llchange)) {  /* (5) */
1902
                        do_update = 1;
1903
                        newstate = ND6_LLINFO_STALE;
1904
                } else                                  /* (1-2,4) */
1905
                        do_update = 0;
1906
        } else {
1907
                do_update = 1;
1908
                if (!lladdr)                            /* (6) */
1909
                        newstate = ND6_LLINFO_NOSTATE;
1910
                else                                    /* (7) */
1911
                        newstate = ND6_LLINFO_STALE;
1912
        }
1913
 
1914
        if (do_update) {
1915
                /*
1916
                 * Update the state of the neighbor cache.
1917
                 */
1918
                ln->ln_state = newstate;
1919
 
1920
                if (ln->ln_state == ND6_LLINFO_STALE) {
1921
                        /*
1922
                         * XXX: since nd6_output() below will cause
1923
                         * state tansition to DELAY and reset the timer,
1924
                         * we must set the timer now, although it is actually
1925
                         * meaningless.
1926
                         */
1927
                        ln->ln_expire = time_second + nd6_gctimer;
1928
 
1929
                        if (ln->ln_hold) {
1930
                                /*
1931
                                 * we assume ifp is not a p2p here, so just
1932
                                 * set the 2nd argument as the 1st one.
1933
                                 */
1934
                                nd6_output(ifp, ifp, ln->ln_hold,
1935
                                           (struct sockaddr_in6 *)rt_key(rt),
1936
                                           rt);
1937
                                ln->ln_hold = NULL;
1938
                        }
1939
                } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1940
                        /* probe right away */
1941
                        ln->ln_expire = time_second;
1942
                }
1943
        }
1944
 
1945
        /*
1946
         * ICMP6 type dependent behavior.
1947
         *
1948
         * NS: clear IsRouter if new entry
1949
         * RS: clear IsRouter
1950
         * RA: set IsRouter if there's lladdr
1951
         * redir: clear IsRouter if new entry
1952
         *
1953
         * RA case, (1):
1954
         * The spec says that we must set IsRouter in the following cases:
1955
         * - If lladdr exist, set IsRouter.  This means (1-5).
1956
         * - If it is old entry (!newentry), set IsRouter.  This means (7).
1957
         * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1958
         * A quetion arises for (1) case.  (1) case has no lladdr in the
1959
         * neighbor cache, this is similar to (6).
1960
         * This case is rare but we figured that we MUST NOT set IsRouter.
1961
         *
1962
         * newentry olladdr  lladdr  llchange       NS  RS  RA  redir
1963
         *                                                      D R
1964
         *      0        n       n       --      (1)     c   ?     s
1965
         *      0        y       n       --      (2)     c   s     s
1966
         *      0        n       y       --      (3)     c   s     s
1967
         *      0        y       y       n       (4)     c   s     s
1968
         *      0        y       y       y       (5)     c   s     s
1969
         *      1       --      n       --      (6) c   c       c s
1970
         *      1       --      y       --      (7) c   c   s   c s
1971
         *
1972
         *                                      (c=clear s=set)
1973
         */
1974
        switch (type & 0xff) {
1975
        case ND_NEIGHBOR_SOLICIT:
1976
                /*
1977
                 * New entry must have is_router flag cleared.
1978
                 */
1979
                if (is_newentry)        /* (6-7) */
1980
                        ln->ln_router = 0;
1981
                break;
1982
        case ND_REDIRECT:
1983
                /*
1984
                 * If the icmp is a redirect to a better router, always set the
1985
                 * is_router flag.  Otherwise, if the entry is newly created,
1986
                 * clear the flag.  [RFC 2461, sec 8.3]
1987
                 */
1988
                if (code == ND_REDIRECT_ROUTER)
1989
                        ln->ln_router = 1;
1990
                else if (is_newentry) /* (6-7) */
1991
                        ln->ln_router = 0;
1992
                break;
1993
        case ND_ROUTER_SOLICIT:
1994
                /*
1995
                 * is_router flag must always be cleared.
1996
                 */
1997
                ln->ln_router = 0;
1998
                break;
1999
        case ND_ROUTER_ADVERT:
2000
                /*
2001
                 * Mark an entry with lladdr as a router.
2002
                 */
2003
                if ((!is_newentry && (olladdr || lladdr))       /* (2-5) */
2004
                 || (is_newentry && lladdr)) {                  /* (7) */
2005
                        ln->ln_router = 1;
2006
                }
2007
                break;
2008
        }
2009
 
2010
        /*
2011
         * When the link-layer address of a router changes, select the
2012
         * best router again.  In particular, when the neighbor entry is newly
2013
         * created, it might affect the selection policy.
2014
         * Question: can we restrict the first condition to the "is_newentry"
2015
         * case?
2016
         * XXX: when we hear an RA from a new router with the link-layer
2017
         * address option, defrouter_select() is called twice, since
2018
         * defrtrlist_update called the function as well.  However, I believe
2019
         * we can compromise the overhead, since it only happens the first
2020
         * time.
2021
         * XXX: although defrouter_select() should not have a bad effect
2022
         * for those are not autoconfigured hosts, we explicitly avoid such
2023
         * cases for safety.
2024
         */
2025
        if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
2026
                defrouter_select();
2027
 
2028
        return rt;
2029
}
2030
 
2031
static void
2032
nd6_slowtimo(ignored_arg)
2033
    void *ignored_arg;
2034
{
2035
#ifdef __NetBSD__
2036
        int s = splsoftnet();
2037
#else
2038
        int s = splnet();
2039
#endif
2040
        int i;
2041
        struct nd_ifinfo *nd6if;
2042
 
2043
#if defined(__NetBSD__) || (defined(__FreeBSD__) && __FreeBSD__ >= 3)
2044
        callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
2045
            nd6_slowtimo, NULL);
2046
#elif defined(__OpenBSD__)
2047
        timeout_set(&nd6_slowtimo_ch, nd6_slowtimo, NULL);
2048
        timeout_add(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz);
2049
#else
2050
        timeout(nd6_slowtimo, (caddr_t)0, ND6_SLOWTIMER_INTERVAL * hz);
2051
#endif
2052
        for (i = 1; i < if_index + 1; i++) {
2053
                if (!nd_ifinfo || i >= nd_ifinfo_indexlim)
2054
                        continue;
2055
                nd6if = &nd_ifinfo[i];
2056
                if (nd6if->basereachable && /* already initialized */
2057
                    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
2058
                        /*
2059
                         * Since reachable time rarely changes by router
2060
                         * advertisements, we SHOULD insure that a new random
2061
                         * value gets recomputed at least once every few hours.
2062
                         * (RFC 2461, 6.3.4)
2063
                         */
2064
                        nd6if->recalctm = nd6_recalc_reachtm_interval;
2065
                        nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
2066
                }
2067
        }
2068
        splx(s);
2069
}
2070
 
2071
#define senderr(e) { error = (e); goto bad;}
2072
int
2073
nd6_output(ifp, origifp, m0, dst, rt0)
2074
        struct ifnet *ifp;
2075
        struct ifnet *origifp;
2076
        struct mbuf *m0;
2077
        struct sockaddr_in6 *dst;
2078
        struct rtentry *rt0;
2079
{
2080
        struct mbuf *m = m0;
2081
        struct rtentry *rt = rt0;
2082
        struct sockaddr_in6 *gw6 = NULL;
2083
        struct llinfo_nd6 *ln = NULL;
2084
        int error = 0;
2085
#if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
2086
        long time_second = time.tv_sec;
2087
#endif
2088
#if defined(__OpenBSD__) && defined(IPSEC)
2089
        struct m_tag *mtag;
2090
#endif /* IPSEC */
2091
 
2092
        if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
2093
                goto sendpkt;
2094
 
2095
        if (nd6_need_cache(ifp) == 0)
2096
                goto sendpkt;
2097
 
2098
        /*
2099
         * next hop determination.  This routine is derived from ether_outpout.
2100
         */
2101
        if (rt) {
2102
                if ((rt->rt_flags & RTF_UP) == 0) {
2103
#ifdef __FreeBSD__
2104
                        if ((rt0 = rt = rtalloc1((struct sockaddr *)dst, 1, 0UL)) !=
2105
                                NULL)
2106
#else
2107
                        if ((rt0 = rt = rtalloc1((struct sockaddr *)dst, 1)) !=
2108
                                NULL)
2109
#endif
2110
                        {
2111
                                rt->rt_refcnt--;
2112
                                if (rt->rt_ifp != ifp) {
2113
                                        /* XXX: loop care? */
2114
                                        return nd6_output(ifp, origifp, m0,
2115
                                                          dst, rt);
2116
                                }
2117
                        } else
2118
                                senderr(EHOSTUNREACH);
2119
                }
2120
 
2121
                if (rt->rt_flags & RTF_GATEWAY) {
2122
                        gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
2123
 
2124
                        /*
2125
                         * We skip link-layer address resolution and NUD
2126
                         * if the gateway is not a neighbor from ND point
2127
                         * of view, regardless of the value of nd_ifinfo.flags.
2128
                         * The second condition is a bit tricky; we skip
2129
                         * if the gateway is our own address, which is
2130
                         * sometimes used to install a route to a p2p link.
2131
                         */
2132
                        if (!nd6_is_addr_neighbor(gw6, ifp) ||
2133
                            in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
2134
                                /*
2135
                                 * We allow this kind of tricky route only
2136
                                 * when the outgoing interface is p2p.
2137
                                 * XXX: we may need a more generic rule here.
2138
                                 */
2139
                                if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
2140
                                        senderr(EHOSTUNREACH);
2141
 
2142
                                goto sendpkt;
2143
                        }
2144
 
2145
                        if (rt->rt_gwroute == 0)
2146
                                goto lookup;
2147
                        if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
2148
                                rtfree(rt); rt = rt0;
2149
                        lookup:
2150
#ifdef __FreeBSD__
2151
                                rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL);
2152
#else
2153
                                rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
2154
#endif
2155
                                if ((rt = rt->rt_gwroute) == 0)
2156
                                        senderr(EHOSTUNREACH);
2157
#if defined(__bsdi__) || defined(__NetBSD__)
2158
                                /* the "G" test below also prevents rt == rt0 */
2159
                                if ((rt->rt_flags & RTF_GATEWAY) ||
2160
                                    (rt->rt_ifp != ifp)) {
2161
                                        rt->rt_refcnt--;
2162
                                        rt0->rt_gwroute = 0;
2163
                                        senderr(EHOSTUNREACH);
2164
                                }
2165
#endif
2166
                        }
2167
                }
2168
        }
2169
 
2170
        /*
2171
         * Address resolution or Neighbor Unreachability Detection
2172
         * for the next hop.
2173
         * At this point, the destination of the packet must be a unicast
2174
         * or an anycast address(i.e. not a multicast).
2175
         */
2176
 
2177
        /* Look up the neighbor cache for the nexthop */
2178
        if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
2179
                ln = (struct llinfo_nd6 *)rt->rt_llinfo;
2180
        else {
2181
                /*
2182
                 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2183
                 * the condition below is not very efficient.  But we believe
2184
                 * it is tolerable, because this should be a rare case.
2185
                 */
2186
                if (nd6_is_addr_neighbor(dst, ifp) &&
2187
                    (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
2188
                        ln = (struct llinfo_nd6 *)rt->rt_llinfo;
2189
        }
2190
        if (!ln || !rt) {
2191
                if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
2192
                    !(nd_ifinfo[ifp->if_index].flags & ND6_IFF_PERFORMNUD)) {
2193
                        log(LOG_DEBUG,
2194
                            "nd6_output: can't allocate llinfo for %s "
2195
                            "(ln=%p, rt=%p)\n",
2196
                            ip6_sprintf(&dst->sin6_addr), ln, rt);
2197
                        senderr(EIO);   /* XXX: good error? */
2198
                }
2199
 
2200
                goto sendpkt;   /* send anyway */
2201
        }
2202
 
2203
        /* We don't have to do link-layer address resolution on a p2p link. */
2204
        if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
2205
            ln->ln_state < ND6_LLINFO_REACHABLE) {
2206
                ln->ln_state = ND6_LLINFO_STALE;
2207
                ln->ln_expire = time_second + nd6_gctimer;
2208
        }
2209
 
2210
        /*
2211
         * The first time we send a packet to a neighbor whose entry is
2212
         * STALE, we have to change the state to DELAY and a sets a timer to
2213
         * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2214
         * neighbor unreachability detection on expiration.
2215
         * (RFC 2461 7.3.3)
2216
         */
2217
        if (ln->ln_state == ND6_LLINFO_STALE) {
2218
                ln->ln_asked = 0;
2219
                ln->ln_state = ND6_LLINFO_DELAY;
2220
                ln->ln_expire = time_second + nd6_delay;
2221
        }
2222
 
2223
        /*
2224
         * If the neighbor cache entry has a state other than INCOMPLETE
2225
         * (i.e. its link-layer address is already resolved), just
2226
         * send the packet.
2227
         */
2228
        if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
2229
                goto sendpkt;
2230
 
2231
        /*
2232
         * There is a neighbor cache entry, but no ethernet address
2233
         * response yet.  Replace the held mbuf (if any) with this
2234
         * latest one.
2235
         * This code conforms to the rate-limiting rule described in Section
2236
         * 7.2.2 of RFC 2461, because the timer is set correctly after sending
2237
         * an NS below.
2238
         */
2239
        if (ln->ln_state == ND6_LLINFO_NOSTATE)
2240
                ln->ln_state = ND6_LLINFO_INCOMPLETE;
2241
        if (ln->ln_hold)
2242
                m_freem(ln->ln_hold);
2243
        ln->ln_hold = m;
2244
        if (ln->ln_expire) {
2245
                if (ln->ln_asked < nd6_mmaxtries &&
2246
                    ln->ln_expire < time_second) {
2247
                        ln->ln_asked++;
2248
                        ln->ln_expire = time_second +
2249
                                nd_ifinfo[ifp->if_index].retrans / 1000;
2250
                        nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
2251
                }
2252
        }
2253
        return(0);
2254
 
2255
  sendpkt:
2256
#if defined(__OpenBSD__) && defined(IPSEC)
2257
        /*
2258
         * If the packet needs outgoing IPsec crypto processing and the
2259
         * interface doesn't support it, drop it.
2260
         */
2261
        mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
2262
#endif /* IPSEC */
2263
 
2264
        if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
2265
#if defined(__OpenBSD__) && defined(IPSEC)
2266
                if (mtag != NULL &&
2267
                    (origifp->if_capabilities & IFCAP_IPSEC) == 0) {
2268
                        /* Tell IPsec to do its own crypto. */
2269
                        ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
2270
                        error = EACCES;
2271
                        goto bad;
2272
                }
2273
#endif /* IPSEC */
2274
                return((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
2275
                                         rt));
2276
        }
2277
#if defined(__OpenBSD__) && defined(IPSEC)
2278
        if (mtag != NULL &&
2279
            (ifp->if_capabilities & IFCAP_IPSEC) == 0) {
2280
                /* Tell IPsec to do its own crypto. */
2281
                ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
2282
                error = EACCES;
2283
                goto bad;
2284
        }
2285
#endif /* IPSEC */
2286
        return((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
2287
 
2288
  bad:
2289
        if (m)
2290
                m_freem(m);
2291
        return (error);
2292
}
2293
#undef senderr
2294
 
2295
int
2296
nd6_need_cache(ifp)
2297
        struct ifnet *ifp;
2298
{
2299
        /*
2300
         * XXX: we currently do not make neighbor cache on any interface
2301
         * other than ARCnet, Ethernet, FDDI and GIF.
2302
         *
2303
         * RFC2893 says:
2304
         * - unidirectional tunnels needs no ND
2305
         */
2306
        switch (ifp->if_type) {
2307
        case IFT_ARCNET:
2308
        case IFT_ETHER:
2309
        case IFT_FDDI:
2310
        case IFT_IEEE1394:
2311
#ifdef IFT_PROPVIRTUAL
2312
        case IFT_PROPVIRTUAL:
2313
#endif
2314
#ifdef IFT_L2VLAN
2315
        case IFT_L2VLAN:
2316
#endif
2317
#ifdef IFT_IEEE80211
2318
        case IFT_IEEE80211:
2319
#endif
2320
        case IFT_GIF:           /* XXX need more cases? */
2321
                return(1);
2322
        default:
2323
                return(0);
2324
        }
2325
}
2326
 
2327
int
2328
nd6_storelladdr(ifp, rt, m, dst, desten)
2329
        struct ifnet *ifp;
2330
        struct rtentry *rt;
2331
        struct mbuf *m;
2332
        struct sockaddr *dst;
2333
        u_char *desten;
2334
{
2335
        int i;
2336
        struct sockaddr_dl *sdl;
2337
 
2338
        if (m->m_flags & M_MCAST) {
2339
                switch (ifp->if_type) {
2340
                case IFT_ETHER:
2341
                case IFT_FDDI:
2342
#ifdef IFT_PROPVIRTUAL
2343
                case IFT_PROPVIRTUAL:
2344
#endif
2345
#ifdef IFT_L2VLAN
2346
                case IFT_L2VLAN:
2347
#endif
2348
#ifdef IFT_IEEE80211
2349
                case IFT_IEEE80211:
2350
#endif
2351
                        ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2352
                                                 desten);
2353
                        return(1);
2354
                case IFT_IEEE1394:
2355
                        /*
2356
                         * netbsd can use if_broadcastaddr, but we don't do so
2357
                         * to reduce # of ifdef.
2358
                         */
2359
                        for (i = 0; i < ifp->if_addrlen; i++)
2360
                                desten[i] = ~0;
2361
                        return(1);
2362
                case IFT_ARCNET:
2363
                        *desten = 0;
2364
                        return(1);
2365
                default:
2366
                        m_freem(m);
2367
                        return(0);
2368
                }
2369
        }
2370
 
2371
        if (rt == NULL) {
2372
                /* this could happen, if we could not allocate memory */
2373
                m_freem(m);
2374
                return(0);
2375
        }
2376
        if (rt->rt_gateway->sa_family != AF_LINK) {
2377
                printf("nd6_storelladdr: something odd happens\n");
2378
                m_freem(m);
2379
                return(0);
2380
        }
2381
        sdl = SDL(rt->rt_gateway);
2382
        if (sdl->sdl_alen == 0) {
2383
                /* this should be impossible, but we bark here for debugging */
2384
                printf("nd6_storelladdr: sdl_alen == 0, dst=%s, if=%s\n",
2385
                       ip6_sprintf(&SIN6(dst)->sin6_addr), if_name(ifp));
2386
                m_freem(m);
2387
                return(0);
2388
        }
2389
 
2390
        bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2391
        return(1);
2392
}

powered by: WebSVN 2.1.0

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