summaryrefslogtreecommitdiff
path: root/sys/netinet6/ipv6_rtrequest.c
blob: 08c1744df98c35c671d54f23df8666deb1e4fe93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
/*
%%% copyright-nrl-95
This software is Copyright 1995-1998 by Randall Atkinson, Ronald Lee,
Daniel McDonald, Bao Phan, and Chris Winters. All Rights Reserved. All
rights under this copyright have been assigned to the US Naval Research
Laboratory (NRL). The NRL Copyright Notice and License Agreement Version
1.1 (January 17, 1995) applies to this software.
You should have received a copy of the license with this software. If you
didn't get a copy, you may request one from <license@ipv6.nrl.navy.mil>.

*/

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/errno.h>
/*#include <sys/ioctl.h>*/
#include <sys/syslog.h>

#include <net/if.h>
#include <net/if_types.h>
#include <net/if_dl.h>
#include <net/route.h>

#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>

#include <netinet6/in6.h>
#include <netinet6/in6_var.h>
#include <netinet6/ipv6.h>
#include <netinet6/ipv6_var.h>
#include <netinet6/ipv6_icmp.h>

#ifdef DEBUG_NRL
#include <sys/debug.h>
#else /* DEBUG_NRL */
#if __OpenBSD__
#include <netinet6/debug.h>
#else /* __OpenBSD__ */
#include <sys/debug.h>
#endif /* __OpenBSD__ */
#endif /* DEBUG_NRL */

/*
 * Globals (and forward function declarations).
 */

/*
 * External globals.
 */
extern struct sockaddr_in6 in6_allones;
extern struct discq    dqhead;
extern struct v6router defrtr;
extern struct v6router nondefrtr;

extern struct in6_ifaddr *in6_ifaddr;

extern int ipv6forwarding;

/*
 * External function prototypes.
 */

/*
 * General notes:
 *
 * These functions are mainly called from ipv6_discovery.c but are
 * fairly generic and generally useful and so live in their own file.
 *     danmcd rja
 */
void tunnel_parent __P((struct rtentry *));
struct v6router *ipv6_add_defrouter_rtrequest(struct rtentry *);
void tunnel_child __P((struct rtentry *));
void ipv6_nsolicit __P((struct ifnet *, struct mbuf *, struct rtentry *));
void tunnel_parent_clean __P((struct rtentry *));
int ipv6_delete_defrouter __P((struct v6router *));
void tunnel_child_clean __P((struct rtentry *));

static int add_defchild __P((struct rtentry *));
static void add_netchild __P((struct rtentry *));
void ipv6_setrtifa __P((struct rtentry *));
static void add_non_default __P((struct rtentry *));

/*
 * Functions and macros.
 */


/*----------------------------------------------------------------------
 * add_defchild():
 * 	Find the best default router out of our list and use it
 *    for this destination route.
 ----------------------------------------------------------------------*/
static int
add_defchild(rt)
     struct rtentry *rt;
{
  struct v6router *v6r = defrtr.v6r_next;
  struct sockaddr_in6 *dst;

  /*
   * What if this child turns out to be a tunneling route?  OUCH!
   *
   * One other thing:  If rt_key(rt) is link-local, but it hit from
   *                   the default router, then perhaps it should
   *                   go with the ipv6_onlink_query() route.
   */

  dst = (struct sockaddr_in6 *)rt_key(rt);

  if (!IN6_IS_ADDR_LINKLOCAL(&dst->sin6_addr)) {
    /*
     * Since there are no priorities, just pick one.  For now, I guess I'll
     * Just pick v6r_next.
     */
    if (v6r != &defrtr)
      {
	struct v6child *v6c;
	int rc = 0;

	DPRINTF(IDL_EVENT,("About to do rt_setgate. rt before is:\n"));
	DDO(IDL_EVENT,dump_rtentry(rt));
	DPRINTF(IDL_EVENT,("Args are (dst, parent->rt_gateway, v6r_rt):\n"));
	DDO(IDL_EVENT,dump_smart_sockaddr((struct sockaddr *)dst);\
	    dump_smart_sockaddr(rt->rt_parent->rt_gateway);\
	    dump_rtentry(v6r->v6r_rt));

	if (rt_key(v6r->v6r_rt)->sa_family != AF_INET6)
	  {
	    /*
	     * Hmmm, we must be a tunneling default route.  Current
	     * conventional wisdom is that we can only see one in our
	     * "default router list," and that it was added manually.  Using
	     * that assumption, get things right, including chasing the
	     * parent and using its rt_gateway sockaddr for rt_setgate.
	     */
	    rt_setgate(rt,(struct sockaddr *)dst,rt->rt_parent->rt_gateway);
	    rc = 1;
	  }
	else
	  {
	    rt_setgate(rt,(struct sockaddr *)dst,rt_key(v6r->v6r_rt));
	    rt->rt_flags &= ~RTF_TUNNEL;  /* In case cloned off an initially
					     tunnelling default route. */
	  }

	DDO(IDL_GROSS_EVENT,\
	    printf("After rt_setgate, rt is:\n");dump_rtentry(rt));

	rt->rt_llinfo = malloc(sizeof(*v6c),M_DISCQ,M_NOWAIT);
	if (rt->rt_llinfo == NULL)
	  {
	    DPRINTF(IDL_ERROR, ("add_defchild(): malloc failed.\n"));
	    /* Perhaps route should be freed here */
	  }
	else
	  {
	    v6c = (struct v6child *)rt->rt_llinfo;
	    insque(v6c,&v6r->v6r_children);
	    v6c->v6c_route = rt;
	  }
	return rc;
      }
    else
      {
	/*
	 * Default route hit, but no default router children.
	 *
	 * Perhaps delete it?
	 */
	DPRINTF(IDL_ERROR,\
		("Default route hit, but no default routers in list.\n"));
      }
  }

  /* Either  on-link or  in trouble. Needs to be coded. */
  DPRINTF(IDL_ERROR, ("On-link or in trouble in ipv6_rtrequest\n"));

  /*
   * Convert off-link entry to indeterminate on-link entry.
   * Subsequent code after a call to this function will do the right thing.
   * (ipv6_rtrequest code to handle new on-link neighbors follows this.)
   */
  rt->rt_flags = RTF_UP|RTF_LLINFO|RTF_HOST;
  rt->rt_llinfo = NULL;
  rt->rt_ifa->ifa_refcnt--;
  rt->rt_ifa = NULL;
  rt->rt_ifp = NULL;
  bzero(rt->rt_gateway,rt->rt_gateway->sa_len);
  rt->rt_gateway->sa_family = AF_LINK;
  rt->rt_gateway->sa_len = sizeof(struct sockaddr_dl);
  rt->rt_rmx.rmx_mtu = 0;

  /*
   * QUESTION:  If I do this, should I call ipv6_nsolicit() to complete
   *            this conversion from default route child to non-deterministic
   *            on-link neighbor?
   *
   * ANSWER:    Yes, but not here.  Just return 1, and let later code take
   *            care of the nsolicit.
   */
  return 1;
}

/*----------------------------------------------------------------------
 * add_netchild():
 * 	Given a network route child (i.e. non-default), 
 *    put this child in the parent's descendant list.
 ----------------------------------------------------------------------*/

static void
add_netchild(rt)
     struct rtentry *rt;
{
  struct v6router *v6r;
  struct v6child *v6c;

  if (rt->rt_parent == NULL)
    {
      DPRINTF(IDL_ERROR,
	      ("add_netchild: No parent route. (Must be manually added.)\n"));
      return;
    }
  v6r = (struct v6router *)rt->rt_parent->rt_llinfo;

  v6c = malloc(sizeof(*v6c),M_DISCQ,M_NOWAIT);
  if (v6c == NULL)
    {
      DPRINTF(IDL_ERROR, ("add_netchild(): malloc failed.\n"));
      /* Perhaps should free route here */
      return;
    }
  insque(v6c,v6r->v6r_children.v6c_next);
  v6c->v6c_route = rt;
  v6c->v6c_parent = v6r;
  rt->rt_llinfo = (caddr_t)v6c;
}

/*----------------------------------------------------------------------
 * add_non_default():
 *	add a non-default routing entry.
 *
 ----------------------------------------------------------------------*/
static void
add_non_default(rt)
     struct rtentry *rt;
{
  struct v6router *newbie;

  if (rt_key(rt)->sa_family != AF_INET6)
    {
      DPRINTF(IDL_ERROR, 
	      ("IPv6 off-net non-tunnel route w/o IPv6 gwaddr.\n"));
      return;
    }
    
  newbie = malloc(sizeof(*newbie),M_DISCQ,M_NOWAIT);
  if (newbie == NULL)
    {
      DPRINTF(IDL_ERROR, ("add_non_default(): malloc failed.\n"));
      /* Should probably free route */
      return;
    }
  bzero(newbie,sizeof(*newbie));
  newbie->v6r_children.v6c_next = newbie->v6r_children.v6c_prev = 
    &newbie->v6r_children;
  insque(newbie,&nondefrtr);

  /*
   * On creation of rt, rt_setgate() was called, therefore we take on blind
   * faith that an appropriate neighbor cache entry was created.  If not,
   * we're in deep trouble.
   */
  newbie->v6r_rt = rt->rt_gwroute;
  rt->rt_llinfo = (caddr_t)newbie;
}


/*----------------------------------------------------------------------
 * ipv6_setrtifa():
 *	Set route's interface address.  Source address selection for
 *      a route.
 *
 ----------------------------------------------------------------------*/
void
ipv6_setrtifa(rt)
     struct rtentry *rt;
{
  struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)rt_key(rt);
  struct sockaddr_in6 *src  = (struct sockaddr_in6 *)rt->rt_ifa->ifa_addr;
  struct in6_ifaddr *src_in6ifa = (struct in6_ifaddr *)rt->rt_ifa;
  struct sockaddr_in6 *mask = (struct sockaddr_in6 *)rt_mask(rt);
  struct ifaddr *ifa = NULL;

  DPRINTF(IDL_EVENT,("Entering ipv6_setrtifa.\n"));

  /*
   * If I can't find a "better" source address, stick with the one I got.
   *
   * ASSUMES:  rt_ifp is set PROPERLY.  This NEEDS to be true.
   */

  /*
   * If link-local, use link-local source.
   */

  if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
    {
      if (IN6_IS_ADDR_LINKLOCAL(&src->sin6_addr))
	return;
#if __FreeBSD__
      for (ifa = rt->rt_ifp->if_addrhead.tqh_first; ifa; ifa = ifa->ifa_link.tqe_next)
#else /* __FreeBSD__ */
#if __NetBSD__ || __OpenBSD__
      for (ifa = rt->rt_ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
#else /* __NetBSD__ || __OpenBSD__ */
      for (ifa = rt->rt_ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
#endif /* __NetBSD__ || __OpenBSD__ */
#endif /* __FreeBSD__ */
	{
	  struct sockaddr_in6 *current = (struct sockaddr_in6 *)ifa->ifa_addr;

	  if ((current->sin6_family != AF_INET6) || 
	      IS_DEPRECATED((struct in6_ifaddr *)ifa))
	    continue;  /* For loop. */
	  if (IN6_IS_ADDR_LINKLOCAL(&current->sin6_addr))
	    break;   /* For loop. */
	}
      if (ifa == NULL)
	return;         /* We're in real trouble. */
    }

  /*
   * If v4-compatible, use v4-compatible source address.
   */
  if (ifa == NULL && (sin6->sin6_addr.s6_addr32[0] == 0 && 
		      sin6->sin6_addr.s6_addr32[1] == 0 &&
		      sin6->sin6_addr.s6_addr32[2] == 0 &&
		      sin6->sin6_addr.s6_addr32[3] != htonl(1) &&
		      (mask == NULL || mask->sin6_len >= 
		            sizeof(*mask) - sizeof(struct in_addr))))
    {
      if (IN6_IS_ADDR_V4COMPAT(&src->sin6_addr) && !IS_DEPRECATED(src_in6ifa))
	return;
#if __FreeBSD__
      for (ifa = rt->rt_ifp->if_addrhead.tqh_first; ifa; ifa = ifa->ifa_link.tqe_next)
#else /* __FreeBSD__ */
#if __NetBSD__ || __OpenBSD__
      for (ifa = rt->rt_ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
#else /* __NetBSD__ || __OpenBSD__ */
      for (ifa = rt->rt_ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
#endif /* __NetBSD__ || __OpenBSD__ */
#endif /* __FreeBSD__ */
	{
	  struct sockaddr_in6 *current = (struct sockaddr_in6 *)ifa->ifa_addr;

	  if ((current->sin6_family != AF_INET6) 
	      || IS_DEPRECATED((struct in6_ifaddr *)ifa))
	    continue;  /* For loop. */
	  if (IN6_IS_ADDR_V4COMPAT(&current->sin6_addr))
	    break;   /* For loop. */
	}
      /*if (ifa == NULL)
	return;*/        /* if ifa == NULL here, pretend it's global, because
			    it is global! */
    }

  /*
   * If site-local, use a site-local source address.
   */

  if (ifa == NULL && IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
    {
      if (IN6_IS_ADDR_SITELOCAL(&src->sin6_addr) && !IS_DEPRECATED(src_in6ifa))
	return;
#ifdef __FreeBSD__
      for (ifa = rt->rt_ifp->if_addrhead.tqh_first; ifa; ifa = ifa->ifa_link.tqe_next)
#else /* __FreeBSD__ */
#if __NetBSD__ || __OpenBSD__
      for (ifa = rt->rt_ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
#else /* __NetBSD__ || __OpenBSD__ */
      for (ifa = rt->rt_ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
#endif /* __NetBSD__ || __OpenBSD__ */
#endif /* __FreeBSD__ */
	{
	  struct sockaddr_in6 *current = (struct sockaddr_in6 *)ifa->ifa_addr;

	  if ((current->sin6_family != AF_INET6) 
	      || IS_DEPRECATED((struct in6_ifaddr *)ifa))
	    continue;  /* For loop. */
	  if (IN6_IS_ADDR_SITELOCAL(&current->sin6_addr))
	    break;   /* For loop. */
	}
      if (ifa == NULL)
	return;         /* We don't want to potentially pollute the global
			   internet with site-local traffic.  If you feel
			   differently, comment out this ifa == NULL check
			   and fallthrough. */
    }

  if (ifa == NULL)
    {
      /*
       * At this point, the address _could_ be anything, but is most likely
       * a globally routable address that didn't fit into the previous
       * categories.
       *
       * Do gyrations iff rt->rt_ifa's address is link-local and the dest
       * isn't or the src address previously picked is deprecated.
       */
      DPRINTF(IDL_GROSS_EVENT,("In default case of ipv6_setrtifa().\n"));
      if ((!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
	   IN6_IS_ADDR_LINKLOCAL(&src->sin6_addr)) || IS_DEPRECATED(src_in6ifa))
	{
	  struct ifaddr *ifa_compat = 0, *ifa_site = 0;

	  /*
	   * For now, pick a non-link-local address using the following
	   * order of preference: global, compatible, site-local.
	   *
	   */
#ifdef __FreeBSD__
	  for (ifa = rt->rt_ifp->if_addrhead.tqh_first; ifa; ifa = ifa->ifa_link.tqe_next)
#else /* __FreeBSD__ */
#if __NetBSD__ || __OpenBSD__
	  for (ifa = rt->rt_ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
#else /* __NetBSD__ || __OpenBSD__ */
	  for (ifa = rt->rt_ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
#endif /* __NetBSD__ || __OpenBSD__ */
#endif /* __FreeBSD__ */
	    if (ifa->ifa_addr->sa_family == AF_INET6 &&
                !IS_DEPRECATED((struct in6_ifaddr *)ifa) &&
		!IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
	      if (IN6_IS_ADDR_V4COMPAT(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr))
		ifa_compat = ifa;
	      else if (IN6_IS_ADDR_SITELOCAL(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr))
		ifa_site = ifa;
	      else /* globally routable address */
		break;
		}
	  if (!ifa)
	    ifa = ifa_compat ? ifa_compat : ifa_site;
	}
    }

  if (ifa != NULL)
    {
      /*
       * Q: Do I call ipv6_rtrequest (through ifa_rtrequest) if I
       *    change ifa's on a route?
       */
      /*if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
	rt->rt_ifa->ifa_rtrequest(RTM_DELETE,rt,rt->rt_gateway);*/
      IFAFREE(rt->rt_ifa);
      ifa->ifa_refcnt++;
      rt->rt_ifa = ifa;
      rt->rt_ifp = ifa->ifa_ifp;   /* Is this desirable? */
      /*if (ifa->ifa_rtrequest)
	ifa->ifa_rtrequest(RTM_ADD, rt, rt->rt_gateway);*/
    }
}


/*----------------------------------------------------------------------
 * ipv6_rtrequest():
 *	IPv6-specific route ADD, RESOLVE, and DELETE function.
 * Typically called from route.c's rtrequest() function to handle
 * the IPv6 unique things.
 ----------------------------------------------------------------------*/

void
ipv6_rtrequest(req, rt, sa)
     int req;
     register struct rtentry *rt;
     struct sockaddr *sa;  /* Ignored parameter, I believe. */
{
  static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}, *sdlp;
  int spray = 0;

  DPRINTF(IDL_EVENT,("ipv6_rtrequest():0000-Starting.\n"));
  DPRINTF(IDL_EVENT,("req = %d, rt = 0x%lx, sa = 0x%lx\n",req,\
			   (unsigned long)rt, (unsigned long)sa));
  DDO(IDL_EVENT,dump_rtentry(rt));

  switch (req)
    {
    case RTM_ADD:
      DPRINTF(IDL_EVENT,("ipv6_rtrequest():0100-case RTM_ADD:.\n"));

      /*
       * Set route Path MTU if not already set on brand new route.
       */
      if (rt->rt_rmx.rmx_mtu == 0)
	rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;

      /*
       * An explicitly added "all-ones" mask or no mask at all should be
       * a host route.
       */

      if (rt_mask(rt) == NULL ||
	  (rt_mask(rt)->sa_len == sizeof(struct sockaddr_in6) &&
	   IN6_ARE_ADDR_EQUAL(&((struct sockaddr_in6 *)rt_mask(rt))->sin6_addr,
			      &in6_allones.sin6_addr))
	  )
	rt->rt_flags |= RTF_HOST;

      /*
       * Explicitly add the cloning bit to the route entry.
       * (Do we still want to do this?)
       *
       * One fallout from this is that if someone wants to disable cloning,
       * the disabling will have to be explicitly done after adding the route.
       */
      if ((rt->rt_flags & RTF_HOST) == 0)
	{
	  /*
	   * All non-host routes that aren't m-cast are cloning!
	   */
	  if (!IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr))
	    rt->rt_flags |= RTF_CLONING;

	  if (rt->rt_flags & RTF_TUNNEL)
	    {
              DPRINTF(IDL_EVENT,("ipv6_rtrequest():0150-Calling tunnel_parent().\n"));
	      tunnel_parent(rt);
	    }

	  if (rt->rt_flags & RTF_GATEWAY) {
	    if (rt_mask(rt)->sa_len == 0)
	      {
		if (rt->rt_flags & RTF_TUNNEL)
		  {
		    /*
		     * Add tunnel to default router list.
		     */
		    ipv6_add_defrouter_rtrequest(rt);
		  }
		else
		  {
		    struct v6router *v6r;
		    /*
		     * The "default" route has just been added.
		     * Do default router stuff here.
		     *
		     * Search router list to see if I'm already there.
		     * this avoids double-adding.
		     */
		    
		    for (v6r = defrtr.v6r_next; v6r != &defrtr;
			 v6r = v6r->v6r_next)
		      if (v6r->v6r_rt == rt->rt_gwroute)
			break;
		    
		    if (v6r == &defrtr)
		      {
			DPRINTF(IDL_ERROR,
				("Calling ipv6_add_defrouter from RTM_ADD.\n"));
			ipv6_add_defrouter_rtrequest(rt);
		      }
		    
		    rt->rt_gwroute->rt_flags |= RTF_ISAROUTER;
		  }
		rt->rt_flags |= RTF_DEFAULT;
	      }
	    else
	      if (rt->rt_flags & RTF_TUNNEL)
		{
		  /*
		   * Perhaps if the tunnel bit is already set here, don't
		   * do anything.
		   */
		}
	      else
		{
		  /*
		   * A non-default network route has just been added.
		   * Do non-default router stuff here.
		   */
		  add_non_default(rt);
		}
	  } else if (!(rt->rt_flags & RTF_TUNNEL))
	    {
	      /*
	       * Interface route (i.e. on-link non-host prefix).
	       */
              DPRINTF(IDL_EVENT,("Setting up i/f route.\n"));
              rt_setgate(rt, rt_key(rt), (struct sockaddr *)&null_sdl);
              sdlp = (struct sockaddr_dl *)rt->rt_gateway;
              sdlp->sdl_type = rt->rt_ifp->if_type;
              sdlp->sdl_index = rt->rt_ifp->if_index;
	    }
	}

      DPRINTF(IDL_EVENT,("ipv6_rtrequest():0199-Falling out of RTM_ADD:.\n"));
      /* FALLTHROUGH... */
    case RTM_RESOLVE:
      {
#ifdef __FreeBSD__
      extern struct ifnet loif[];
      struct ifnet *loifp = loif; 
#else /* __FreeBSD__ */
#if (!defined(_BSDI_VERSION) || (_BSDI_VERSION < 199802)) 
      extern struct ifnet loif;
#else /* (!defined(_BSDI_VERSION) || (_BSDI_VERSION < 199802)) */
      extern struct ifnet *loifp;
#endif /* (!defined(_BSDI_VERSION) || (_BSDI_VERSION < 199802)) */
#endif /* __FreeBSD__ */

      DPRINTF(IDL_EVENT,("ipv6_rtrequest():0200-Now in case RTM_RESOLVE:.\n"));

      /*
       * First, check if ifa addr is same as route addr, 
       * If yes, then wire the loopback interface as the destination.
       */
      if ((rt->rt_flags & RTF_HOST) && rt->rt_ifa &&
	  IN6_ARE_ADDR_EQUAL(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
	  &I6A_SIN(rt->rt_ifa)->sin6_addr)
#if (defined(_BSDI_VERSION) && (_BSDI_VERSION >= 199802))
	  && loifp
#endif /* (defined(_BSDI_VERSION) && (_BSDI_VERSION >= 199802)) */
         )
	{
	  /* Change ifp to loopback and gateway to itself. */
#ifdef __FreeBSD__
	  rt->rt_ifp = loifp;
	  rt->rt_gateway = rt_key(rt);
	  rt->rt_rmx.rmx_mtu = loifp->if_mtu;
#else /* __FreeBSD__ */
#if (!defined(_BSDI_VERSION) || (_BSDI_VERSION < 199802))
	  rt->rt_ifp = &loif;
	  rt->rt_gateway = rt_key(rt);
	  rt->rt_rmx.rmx_mtu = loif.if_mtu;
#else /* (!defined(_BSDI_VERSION) || (_BSDI_VERSION < 199802)) */
	  rt->rt_ifp = loifp;
	  rt->rt_gateway = rt_key(rt);
	  rt->rt_rmx.rmx_mtu = loifp->if_mtu;
#endif /* (!defined(_BSDI_VERSION) || (_BSDI_VERSION < 199802)) */
#endif /* __FreeBSD__ */

	  if (rt->rt_parent)
            rt->rt_flags &= ~(RTF_STATIC | RTF_TUNNEL);

	  break;
	  };
	}

      /*
       * Second, check the accuracy of the route's interface address (ifa).
       * If possible, update the route's ifa such that it is the "best"
       * source address for packets bound for this destination.
       * 
       * This is done to any routes and is subtle enough
       * to merit its own separate function:
       */

      ipv6_setrtifa(rt);

      if (!(rt->rt_flags & RTF_HOST))
	{
	  /*
	   * This should be the "none of the above" case.  For now,
	   * do nothing and just return.
	   */
	  DPRINTF(IDL_GROSS_EVENT,("In RESOLVE/lateADD, but not RTF_HOST.\n"));
	  break;
	}

      /*
       * At this point, I know  (1) I'm a host route which has either been
       * added manually or  (2) I have cloned off to become either:
       *
       * a tunnelling route child,
       *      RTF_TUNNEL is set on this entry.
       *      I should be okay, although tunnel state may become necessary.
       *      I'm a next-hop cache entry.
       *
       * a default router child,
       * a non-default network route child,
       *      RTF_GATEWAY is set on this entry.
       *      I have to insert this child into the router's children list.
       *      My child entry hangs off rt_llinfo.  I'm a next-hop cache
       *      entry.
       *
       * an interface (i.e. neighbor) route child,
       * a link-local (i.e. off link-local mask) route child
       *      rt->rt_gateway is AF_LINK
       *      I should set RTF_LLINFO, and set up a discq entry because I'm
       *      a neighbor cache entry.  (Neighbor caches double as next-hop
       *      cache entries.)
       */

      if (rt->rt_gateway == NULL)
	panic("ipv6_rtrequest():  No rt_gateway at the RTM_RESOLVE code.");

      /*
       * Order of bit checking is very important.  I check TUNNEL first,
       * because it's REALLY special.  Then I check GATEWAY or not.
       */

      if (rt->rt_parent != NULL && rt_mask(rt->rt_parent)->sa_len == 0)
	{
	  DPRINTF(IDL_EVENT,("Cloning off default route!\n"));
	  /*
	   * Find a default router out of the list, and assign it to this
	   * child.  Clear or add tunnel bits if necessary.
	   */

	  rt->rt_flags &= ~RTF_STATIC;
	  /*
	   * If add_defchild returns 0, then either it's a tunneling
	   * default route, or a link-local converted to a neighbor entry.
	   *
	   * Set 'spray' to the result of add_defchild, because if it is
	   * a neighbor entry (see AF_LINK case below), then spray will
	   * indicate the need to do some pre-emptive neighbor adverts.
	   */
	  if (!(spray = add_defchild(rt)))  /* If an actual off-net default
					       route... */
	    break;
DPRINTF(IDL_ERROR,("add_defchild returned 1, either on-link or tunnel.\n"));
	  /* Otherwise, either tunnel or converted into LINK address. */
	}
      else rt->rt_llinfo = NULL;  /* Is this already done? */

      if (rt->rt_flags & RTF_TUNNEL)
	{
          DPRINTF(IDL_EVENT,("ipv6_rtrequest():0250-Calling tunnel_child().\n"));
	  tunnel_child(rt);  /* Be careful if manually added RTF_TUNNEL. */
	  break;   /* We're done with this Tunnel host route. */
	}

      if (rt->rt_flags & RTF_GATEWAY)
	{
	  add_netchild(rt);
	  if (req == RTM_RESOLVE)
	    rt->rt_flags &= ~RTF_STATIC;
	  break;
	}

      if (rt->rt_gateway->sa_family == AF_LINK)
	{
	  /*
	   * I may enter here even after RTF_GATEWAY check because
	   * get_defrouter() may convert the route entry into an on-link one!
	   */
	  struct discq *dq;

	  rt->rt_llinfo = malloc(sizeof(*dq),M_DISCQ,M_NOWAIT);
	  dq = (struct discq *)rt->rt_llinfo;
	  if (dq == NULL)
	    {
	      DPRINTF(IDL_ERROR,
		      ("ipv6_rtrequest(): malloc for rt_llinfo failed.\n"));
	      /* Probably should free route */
	      break;
	    }
	  bzero(dq,sizeof(*dq));
	  dq->dq_rt = rt;
	  dq->dq_unanswered = -1;
	  rt->rt_flags |= RTF_LLINFO;
	  insque(dq,&dqhead);
	  /*
	   * State is already INCOMPLETE, because of link stuff.
	   *
	   * If this neigbor entry is caused by add_defchild (i.e. link-local)
	   * spray nsolicit out all interfaces.
	   */

	  if (spray)
	    ipv6_nsolicit(NULL,NULL,rt);

	  break;
	}
      else
#if 0
	/*
	 * support for PPP goes here.
	 * Frame Relay, and ATM will probably probably be handled by the
	 * AF_LINK case.  Other AF_* will be covered later.
	 */
	panic("ipv6_rtrequest: Not tunnel, not off-net, and not neighbor.");
#else /* 0 */
	{
	DPRINTF(IDL_ERROR, ("ipv6_rtrequest: Not tunnel, not off-net, and not neighbor. rt is:\n"));
        DDO(IDL_ERROR, dump_rtentry(rt));
	}
#endif /* 0 */

      break;
    case RTM_DELETE:
      DPRINTF(IDL_GROSS_EVENT,("ipv6_rtrequest():0300-Now in case RTM_DELETE.\n"));
      /*
       * The FLUSH call ('route flush...') checks the ifp, perhaps I should
       * fill that just in case.
       */

      if (rt->rt_ifp == NULL)
	rt->rt_ifp = rt->rt_ifa->ifa_ifp;

      if ((rt->rt_flags & RTF_HOST) == 0)
	{
	  /*
	   * Clean up after network routes.
	   */
	  if (rt->rt_flags & RTF_TUNNEL)
	    {
	      DPRINTF(IDL_EVENT,("Cleaning up tunnel route.\n"));
	      tunnel_parent_clean(rt);
	      if (rt_mask(rt)->sa_len != 0)
		return;   /* If default route is RTF_TUNNEL, then continue. */
	    }
	  if (rt->rt_flags & RTF_GATEWAY) {
	    if (rt_mask(rt)->sa_len == 0)
	      {
		struct v6router *v6r;

		DPRINTF(IDL_GROSS_EVENT,\
			("Cleaning up THE default route.\n"));
		/*
		 * Find manually added default route thing, and clean up that
		 * entry.
		 *
		 * If user deletes default route added by receiving router
		 * adverts, then the router is still on the default router
		 * list, and needs to be added back.
		 */
		for (v6r = defrtr.v6r_next; v6r != &defrtr;
		     v6r = v6r->v6r_next)
		  {
		    /*
		     * PROBLEM:  rt->rt_gwroute, which is what I REALLY
		     *           want to check against, is gone at this
		     *           point.
		     *
		     * POSSIBLE SOLUTION:  Do a check with rt_key(v6r->v6r_rt)
		     *                     and rt->rt_gateway, but there
		     *                     is the question of tunneling
		     *                     default routes, etc., which may
		     *                     mean doing a masked match.
		     */
#define	equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
		    /*
		     * PRESENTED SOLUTION:  Try checking the expiration
		     *                      (easy giveaway for manually
		     *                      added default routes), followed
		     *                      by blatant sockaddr compares
		     *                      (will nail all other normal
		     *                      cases), followed by sa_family
		     *                      check (may be unneccesary).
		     */
		    if (v6r->v6r_expire == 0 || 
			equal(rt_key(v6r->v6r_rt),rt->rt_gateway) ||
			(rt->rt_gateway->sa_family != AF_INET6 &&
			 rt->rt_gateway->sa_family == 
			 rt_key(v6r->v6r_rt)->sa_family))
		      break;
		  }
		if (v6r != &defrtr)
		  (void)ipv6_delete_defrouter(v6r);
		if (defrtr.v6r_next != &defrtr)
		  {
		    /*
		     * Somehow re-add the default route.
		     *
		     * The default route has been deleted from the radix
		     * tree already, so re-adding should be relatively
		     * straightforward.
		     */
		    DPRINTF(IDL_ERROR,
			    ("Auto-added default routers still there!\n"));
		  }
	      }
	    else
	      {
		struct v6router *v6r = (struct v6router *)rt->rt_llinfo;
		struct v6child *v6c;

		/* Non-default router. */
		DPRINTF(IDL_GROSS_EVENT,\
			("Cleaning up a non-{default,host} route.\n"));
		
		if (v6r == NULL)
		  panic("Non-default w/o v6router entry.");
		
		v6c = v6r->v6r_children.v6c_next;
		while (v6c != v6c->v6c_next)
		  {
		    DPRINTF(IDL_ERROR,("Calling RTM_DELETE of child.\n"));
		    /* rtrequest() should remove child from the linked list */
		    rtrequest(RTM_DELETE,rt_key(v6c->v6c_route),NULL,NULL,0,
			      NULL);
		    v6c = v6r->v6r_children.v6c_next;
		  }
		remque(v6r);
		free(v6r,M_DISCQ);
	      }
          }
	  /* Anything else that isn't a HOST needs no work, so return. */
	  return;
	}

      DPRINTF(IDL_EVENT,("v6_discov_rtrequest() deleting a host\n"));
      DPRINTF(IDL_EVENT,("I'm at a host-route point.\n"));

      if (rt->rt_flags & RTF_TUNNEL)
	{
	  DPRINTF(IDL_EVENT,("Tunneling child.\n"));
	  /*
	   * PROBLEM:  Following check will die if parent went bye-bye.
	   *           See if you can fix in another way.
	   */
	  /*if (rt_mask(rt->rt_parent)->sa_len == 0)*/
	  if (rt->rt_flags & RTF_DEFAULT)
	    {
	      /*
	       * Tunneling default route child.  Clean off meta-state.
	       */
	      struct v6child *v6c = (struct v6child *)rt->rt_llinfo;

	      DPRINTF(IDL_ERROR,("Cleaning tunnel-default child.\n"));
	      remque(v6c);
	      rt->rt_llinfo = NULL;
	      free(v6c,M_DISCQ);
	    }
	  tunnel_child_clean(rt);
	}
      else if (rt->rt_flags & RTF_GATEWAY)
	{
	  struct v6child *v6c = (struct v6child *) rt->rt_llinfo;

	  if (v6c == NULL)
	    {
	      if (rt->rt_flags & RTF_HOST) {
		DPRINTF(IDL_ERROR, 
			("no v6c in RTM_DELETE of RTF_GATEWAY.\n"));
              }
	    }
	  else
	    {
	      remque(v6c);
	      rt->rt_llinfo = NULL;
	      free(v6c,M_DISCQ);
	    }
	}
      else if (rt->rt_flags & RTF_LLINFO)
	{
	  struct discq *dq;

	  /* Neighbor cache entry. */
	  if (rt->rt_flags & RTF_ISAROUTER)
	    {
	      struct v6router *v6r,*head;

	      /* Clean up all children of this router. */
	      
	      DPRINTF(IDL_GROSS_EVENT,("Cleaning up router neighbor.\n"));
	      if (rt->rt_flags & RTF_DEFAULT)
		head = &defrtr;
	      else
		{
		  head = &nondefrtr;
		  /* Q:  Do I want to delete the actual network route too? */
		  /* Q2: Do I want to delete the children? */
		}

	      for (v6r = head->v6r_next; v6r != head; v6r = v6r->v6r_next)
		if (v6r->v6r_rt == rt)
		  break;
	      if (v6r == head)
		{
		  /*
		   * At many addresses per interface, this isn't a huge
		   * problem, because a router's on-link address might not
		   * be tied to a router entry.
		   */
		  DPRINTF(IDL_EVENT,
			  ("Router neighbor inconsistency.\n"));
		}
	      else
		if (head == &defrtr)
		  (void) ipv6_delete_defrouter(v6r);  /* This gets rid of
							 children, too! */
	    }

	  dq = (struct discq *)rt->rt_llinfo;
	  rt->rt_flags &= ~RTF_LLINFO;
	  if (dq == NULL)
	    panic("No discq or other rt_llinfo in RTM_DELETE");
	  remque(dq);
	  rt->rt_llinfo = NULL;
	  if (dq->dq_queue)
	    {
	      /* Send ICMP unreachable error. */
	      ipv6_icmp_error(dq->dq_queue, ICMPV6_UNREACH,
			      ICMPV6_UNREACH_ADDRESS, 0);
	      /* m_freem(dq->dq_queue);*/
	    }
	  free(dq,M_DISCQ);
	}
      else
	{
	  DPRINTF(IDL_GROSS_EVENT,\
		  ("Freeing self-wired address.  Doing nothing.\n"));
	}
      break;
    }
    DPRINTF(IDL_GROSS_EVENT,("ipv6_rtrequest():1000-Finished.\n"));
}

/* EOF */