summaryrefslogtreecommitdiff
path: root/sys/netinet6/ipv6_output.c
blob: fca449a421586f187f93d48ac45734528eba5297 (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
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
/*
%%% 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/protosw.h>
#include <sys/domain.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/systm.h>

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

#include <netinet/in.h>
#include <netinet6/in6_var.h>
#include <netinet6/ipv6.h>
#include <netinet6/ipv6_var.h>

#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/in_pcb.h>
#include <netinet/tcp.h>
#include <netinet/tcp_timer.h>
#include <netinet/ip_var.h>
#include <netinet/tcpip.h>
#include <netinet/tcp_var.h>

#if __OpenBSD__
#undef IPSEC
#ifdef NRL_IPSEC
#define IPSEC 1
#endif /* NRL_IPSEC */
#endif /* __OpenBSD__ */

#if IPSEC
#include <sys/osdep.h>
#include <net/netproc.h>
#include <net/netproc_var.h>
#include <sys/nbuf.h>
#endif /* IPSEC */

#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 function definitions.
 */

uint32_t outfragid = 0;  /* Outbound fragment groups have unique id's. */
struct mbuf *ipv6_fragment __P((struct mbuf *,int));

/*
 * External globals.
 */

extern struct ipv6stat ipv6stat;
extern struct in6_ifaddr *in6_ifaddr;
extern struct in6_ifnet *in6_ifnet;
extern struct ifnet *mcastdefault;
extern int ipv6_defhoplmt;

#if defined(_BSDI_VERSION) && (_BSDI_VERSION >= 199802)
extern struct ifnet *loifp;
#endif /* defined(_BSDI_VERSION) && (_BSDI_VERSION >= 199802) */

void ipv6_nsolicit __P((struct ifnet *, struct mbuf *, struct rtentry *));
int ipv6_tunnel_output __P((struct mbuf *, struct sockaddr_in6 *, struct rtentry *));

int ipv6_setmoptions __P((int, struct inpcb *, void *, size_t));
int ipv6_getmoptions __P((int, struct ipv6_moptions *, int *));
void ipv6_mloopback __P((struct ifnet *, struct mbuf *, struct sockaddr_in6 *));
int ipv6_finddivpoint __P((struct mbuf *, uint8_t *));

/*----------------------------------------------------------------------
 * IPv6 output routine.  The mbuf chain contains a near-complete IPv6 header,
 * and an already-inserted list of options.  (I figure it's something for
 * the code with PCB access to handle.)  The options should have their
 * fields in network order.  The header should have its fields in host order.
 * (Save the addresses, which IMHO are always in network order.  Weird.)
 ----------------------------------------------------------------------*/

int
#ifdef __OpenBSD__
ipv6_output(struct mbuf *outgoing, ...)
#else /* __OpenBSD__ */
ipv6_output(outgoing,ro,flags,i6mo, forceifp, socket)
     struct mbuf *outgoing;
     struct route6 *ro;
     int flags;
     struct ipv6_moptions *i6mo;
     struct ifnet *forceifp;
     struct socket *socket;
#endif /* __OpenBSD__ */
{
  struct ipv6 *header;
  struct route6 ipv6route;
  struct sockaddr_in6 *dst;
  struct in6_ifaddr *i6a = NULL;
  struct ifnet *ifp = NULL;
  int error=0;
  uint32_t outmtu = 0;
#if __OpenBSD__
  va_list ap;
  struct route6 *ro;
  int flags;
  struct ipv6_moptions *i6mo;
  struct ifnet *forceifp;
  struct socket *socket;

  va_start(ap, outgoing);
  ro = va_arg(ap, struct route6 *);
  flags = va_arg(ap, int);
  i6mo = va_arg(ap, struct ipv6_moptions *);
  forceifp = va_arg(ap, struct ifnet *);
  socket = va_arg(ap, struct socket *);
  va_end(ap);
#endif /* __OpenBSD__ */

#ifdef DIAGNOSTIC
  if ((outgoing->m_flags & M_PKTHDR) == 0)
    panic("ipv6_output() no HDR");
#endif

  /*
   * Assume the IPv6 header is already contiguous.
   */
  header = mtod(outgoing, struct ipv6 *);
  
  DDO(IDL_FINISHED,printf("ipv6_output:\n");dump_ipv6(header));
  DPRINTF(IDL_FINISHED,("\n"));

  /* 
   * Fill in v6 header.  Assume flow id/version field is in network order,
   * and that the high 4 bits are 0's. 
   */
  
  if ((flags & (IPV6_FORWARDING|IPV6_RAWOUTPUT)) == 0)
    {
      header->ipv6_versfl = htonl(0x60000000) | 
	(header->ipv6_versfl & htonl(0x0fffffff));
      ipv6stat.ips_localout++;
    }
  
  /*
   * Determine interface and physical destination to send datagram out
   * towards.  Do this by looking up a route, or using the route we were
   * passed.
   */

  DPRINTF(IDL_FINISHED,("route passed to ipv6_output is:\n"));
  DDO(IDL_FINISHED,if (ro) dump_rtentry(ro->ro_rt));
  if (ro == 0)
    {
      ro = &ipv6route;
      bzero((caddr_t)ro,sizeof(struct route6));
    }
  dst = &ro->ro_dst;
  
  if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
		    !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &header->ipv6_dst)))
    {
      RTFREE(ro->ro_rt);
      ro->ro_rt = NULL;
    }
  
  if (ro->ro_rt == NULL)
    {
      dst->sin6_family = AF_INET6;
      dst->sin6_len = sizeof(struct sockaddr_in6);
      dst->sin6_addr = header->ipv6_dst;
      dst->sin6_port = 0;
      dst->sin6_flowinfo = 0;
    }
  
#define ifatoi6a(ifa) ((struct in6_ifaddr *)(ifa))
#define sin6tosa(sin6) ((struct sockaddr *)(sin6))
  
  if (flags & IPV6_ROUTETOIF)
    {
      /*
       * Check for route to interface only.  (i.e. the user doesn't want
       * to rely on the routing tables, so send it out an interface).
       */
      if ((i6a = ifatoi6a(ifa_ifwithdstaddr(sin6tosa(dst)))) == 0 &&
	  (i6a = ifatoi6a(ifa_ifwithnet(sin6tosa(dst)))) == 0 )
	{
	  /*
	   * Q:  Do we want to assume that if a user specifies this option,
	   *     the user doesn't want ANYTHING to do with the routing tables?
	   */

	  ipv6stat.ips_noroute++;
	  error = ENETUNREACH;
	  goto bad;
	}
      ifp = i6a->i6a_ifp;
      header->ipv6_hoplimit = 1;
      outmtu = ifp->if_mtu;
    }
  else
    {
      /*
       * Do normal next-hop determination with the help of the routing tree.
       */
      if (ro->ro_rt == 0)
	rtalloc((struct route *)ro);  /* Initial route lookup. */

      if (ro->ro_rt == 0)
	{
	  /*
	   * No route of any kind, so spray neighbor solicits out all
	   * interfaces, unless it's a multicast address.
	   */
	  if (IN6_IS_ADDR_MULTICAST(&header->ipv6_dst))
	    goto mcast;
	  DPRINTF(IDL_FINISHED, ("v6_output doesn't have a route...calling onlink_query!\n"));
	  ipv6_onlink_query(dst);
	  rtalloc((struct route *)ro);
	}
      if (ro->ro_rt == NULL)
	{
	  /*
	   * ipv6_onlink_query() should've added a route.  Probably
	   * failed.
	   */
	  DPRINTF(IDL_GROSS_EVENT, ("v6_output: onlink_query didn't add route!\n"));
	  ipv6stat.ips_noroute++;
	  error = ENETUNREACH;
	  goto bad;
	}

      if (ro->ro_rt->rt_ifa == NULL)
	{
	  /*
	   * We have a route where we don't quite know which interface 
	   * the neighbor belongs to yet.  If I get here, I know that this
	   * route is not pre-allocated (such as done by in6_pcbconnect()),
	   * because those pre-allocators will do the same ipv6_onlink_query()
	   * and ipv6_verify_onlink() in advance.
	   *
	   * I can therefore free the route, and get it again.
	   * Multicast dgrams should NEVER be in this code path.
	   */

	  RTFREE(ro->ro_rt);
	  ro->ro_rt = NULL;
	  DPRINTF(IDL_FINISHED,("v6_output calling ipv6_verify_onlink\n"));
	  if ((error = ipv6_verify_onlink(dst)) != 0)
	    {
	      if (error == -1)
		{
		  DPRINTF(IDL_ERROR,("verify_onlink() failed in v6_out.\n"));
		  error = ENETUNREACH;
		}
	      ipv6stat.ips_noroute++;   /* Better stat needed, because
					   error might not be
					   E{NET,HOST}UNREACH. */
	      
	      goto bad;
	    }
	  rtalloc((struct route *)ro);
	  if (ro->ro_rt == NULL || ro->ro_rt->rt_ifa == NULL)
	    panic("Oops, I'm forgetting something after verify_onlink().");
	}

      /*
       * Exploit properties of route.
       */
      
      ifp = ro->ro_rt->rt_ifp;            /* Q:  Is this REALLY the ifp
					     for the route?
					     
					     A:  Maybe.  If multi-homed,
					         and we attempt to
						 intelligently figure out
						 where link-locals are
						 destined, then we're
						 in trouble. */
      /*
       * On-net route exists, but no destination as of yet.  This can
       * be snipped if an ifp is just selected.  (Depends on multihomed
       * experience.)
       *
       * Currently, this code never executes, because we guarantee rt_ifp is
       * set.  This may, however, change in later versions of this code as
       * we gain multihomed experience.
       */
      if (ifp == NULL && ro->ro_rt->rt_gateway->sa_family == AF_LINK)
	{
	  DPRINTF(IDL_EVENT,\
		  ("ipv6_output() calling ipv6_nsolicit(2)\n"));
	  ipv6_nsolicit(NULL, outgoing, ro->ro_rt);
	  DPRINTF(IDL_EVENT,\
		  ("ipv6_output() attempted to send neighbor solicit(2), returning.\n"));
	  goto done;
	}

      /*
       * Q:  What if address has expired?  Perhaps I should use ifp to
       * obtain optimal i6a value.   There's also the question of using
       * link-local source addresses for off-link communication.  (or for
       * that matter, on-link, but not link-local communication.
       *
       * Q2:  Perhaps use this time to reset the route's ifa?
       * Q3:  Perhaps it is better to use the ipv6_rtrequest()?
       * 
       * Regardless, i6a's only use in this function is to determine the
       * source address of the packet.
       *
       * Currently, ipv6_rtrequest() attempts to store a decent in6_ifaddr
       * in rt_ifa.  This also may change with experience.
       */

      i6a = ifatoi6a(ro->ro_rt->rt_ifa);
      if (i6a->i6a_addrflags & I6AF_NOTSURE) {
	if (!(outgoing->m_flags & M_DAD))
	  {
	    /*
	     * 1. Think of a better error.
	     *
	     * 2. Keep some sort of statistic.
	     */
	    DPRINTF(IDL_ERROR,("Using NOTSURE source address.\n"));
	    error = EADDRNOTAVAIL;
	    goto bad;
	  }
	else i6a = NULL;
      }

      /*
       * More source address selection goes here.
       */

      ro->ro_rt->rt_use++;
      /*
       * Path MTU comes from the route entry.
       */
      outmtu = ro->ro_rt->rt_rmx.rmx_mtu;
      
      if (ro->ro_rt->rt_flags & RTF_GATEWAY)  /* Gateway/router/whatever. */
	dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
    }

  if (forceifp) {
    DPRINTF(IDL_EVENT, ("ipv6_output: in forceifp case\n"));
    ifp = forceifp;
    if (outmtu > ifp->if_mtu)
      outmtu = ifp->if_mtu;
  };

  /*
   * Handle case of a multicast destination.
   */
 mcast:
  if (IN6_IS_ADDR_MULTICAST(&header->ipv6_dst))
    {
      struct in6_multi *in6m;

      outgoing->m_flags |= M_MCAST;
      
      dst = &ro->ro_dst;

      if (i6mo != NULL)
	{
	  /*
	   * As we gain more multicast experience, use i6mo fields to alter
	   * properties of outgoing packet.  (Including, quite possibly,
	   * the source address.)
	   */
	  if (i6mo->i6mo_multicast_ifp != NULL)
	    ifp = i6mo->i6mo_multicast_ifp;
	  header->ipv6_hoplimit = i6mo->i6mo_multicast_ttl;
	}
      else
	{
	  /*
	   * Use default values, since there are no multicast options to
	   * use.
	   */
	  if (ifp == NULL)
	    ifp = mcastdefault;
	  header->ipv6_hoplimit = IPV6_DEFAULT_MCAST_HOPS;
	}

      if (outmtu == 0)         /* But what about mcast Path MTU? */
	outmtu = ifp->if_mtu;

      if ((ifp->if_flags & IFF_MULTICAST) == 0)
	{
	  ipv6stat.ips_noroute++;
	  error = ENETUNREACH;
	  goto bad;
	}

      if ((IN6_IS_ADDR_UNSPECIFIED(&header->ipv6_src) && !(outgoing->m_flags & M_DAD)) ||
	  (IN6_IS_ADDR_LINKLOCAL(&header->ipv6_src) && 
	   IN6_MSCOPE(&header->ipv6_dst) > IN6_LINK_LOCAL))
	{
	  register struct in6_ifaddr *i6a;
	  
	  /*
	   * Source address selection for multicast datagrams.
	   * If link-local source, get in here too, because you don't want
	   * link-local addresses going on non-local multicast.
	   *
	   * Eventually should fix this to perform best source address
	   * selection.  Probably should separate this out into a function.
	   */
	  for (i6a = in6_ifaddr; i6a; i6a = i6a->i6a_next)
	    if (i6a->i6a_ifp == ifp)
	      {
		header->ipv6_src = I6A_SIN(i6a)->sin6_addr;
		break;
              }
        }

      IN6_LOOKUP_MULTI(&header->ipv6_dst, ifp, in6m);
      DPRINTF(IDL_GROSS_EVENT,("in6m == 0x%lx, i6mo == 0x%lx\n", (unsigned long)in6m, (unsigned long)i6mo));
      if (in6m != NULL &&
	  (i6mo == NULL || i6mo->i6mo_multicast_loop))
	{

	  /*
	   * See ipv6_mloopback for details, but that function will tag
	   * the packet with the actual interface the multicast is
	   * supposed to go out.  This makes duplicate address detection
	   * harder to implement, because the inbound mbuf SHOULD be tagged
	   * as coming from me for the case of solicits.  (Perhaps burning
	   * another flag...)
	   */
	  DPRINTF(IDL_GROSS_EVENT,("Calling ipv6_mloopback().\n"));
	  ipv6_mloopback(ifp, outgoing, dst);
	}

#ifdef MROUTING
      /*
       * Do m-cast routing even if I can't find it in my m-cast list.
       */
#endif

      /*
       * If intra-node scope.  I've already hit it with ipv6_mloopback above.
       */

      if (IN6_MSCOPE(&header->ipv6_dst) == IN6_NODE_LOCAL ||
	  (ifp->if_flags & IFF_LOOPBACK))
	goto bad;  /* Not really bad, y'know, just getting out of here. */
    }

  if (ro->ro_rt == NULL && outmtu == 0)
    panic("ipv6_output: How did I get here without a route or MTU?");

  /*
   * Specify source address.  Use i6a, for now.
   */

  if (IN6_IS_ADDR_UNSPECIFIED(&header->ipv6_src) && i6a != NULL && 
      !(outgoing->m_flags & M_DAD))
    header->ipv6_src = I6A_SIN(i6a)->sin6_addr;

  DPRINTF(IDL_FINISHED,("header & chain before security check are:\n"));
  DDO(IDL_FINISHED,dump_ipv6(header));
  DDO(IDL_FINISHED,dump_mchain(outgoing));

#ifdef IPSEC
  if (!(flags & IPV6_FORWARDING)) {
	  size_t preoverhead, postoverhead;
	  void *state;

	  /* NB: If there exists a configured secure tunnel, then
	     the packet being tunneled will have been encapsulated
	     inside an IP packet with (src=me, dest=tunnel-end-point)
	     PRIOR to ip_output() being called, so the above
	     check doesn't preclude secure tunnelling.  rja */
	  /*
	   * I would like to just pass in &ia->ia_addr, but there is a small
	   * chance that the source address doesn't match ia->ia_addr.
	   *
	   * Also, if you need a dest. port, fill in ro->ro_dst with it.
	   */
	  {
	    struct sockaddr_in6 srcsa, dstsa;

	    bzero(&srcsa, sizeof(struct sockaddr_in6));
	    srcsa.sin6_family = AF_INET6;
	    srcsa.sin6_len = sizeof(struct sockaddr_in6);
	    /* XXX - port */
	    srcsa.sin6_addr = header->ipv6_src;

	    bzero(&dstsa, sizeof(struct sockaddr_in6));
	    dstsa.sin6_family = AF_INET6;
	    dstsa.sin6_len = sizeof(struct sockaddr_in6);
	    /* XXX - port */
	    dstsa.sin6_addr = header->ipv6_dst;

	    /* XXX - get the ULP protocol number */
	    if (error = netproc_outputpolicy(socket, (struct sockaddr *)&srcsa,
	        (struct sockaddr *)&dstsa, header->ipv6_nexthdr, &preoverhead,
	        &postoverhead, &state)) {
	      if (error == EACCES) /* XXX - means fail silently */
		error = 0;
	      goto bad;
	    }
	  }

	  if (state) {
	    struct nbuf *nbuf;

	    DP(FINISHED, preoverhead, d);
	    DP(FINISHED, postoverhead, d);

	    header->ipv6_length = htons(outgoing->m_pkthdr.len - 
					sizeof(struct ipv6));

	    if (!(nbuf = mton(outgoing, preoverhead, postoverhead))) {
	      netproc_outputfree(state);
	      error = ENOMEM;
	      goto bad;
	    }

	    outgoing = NULL;

	    if (error = netproc_output(state, nbuf)) {
	      if (error == EACCES)
		error = 0;
	    }

	    /* If successful, netproc_output actually does the output.
	       Either way, it frees the nbuf. */
	    goto done;
	  }
    }
#endif /* defined(IPSEC) || defined(NRL_IPSEC) */

  /*
   * Assume above three return a contiguous and UPDATED IPv6 header.
   */
  header = mtod(outgoing,struct ipv6 *);

  /*
   * Determine the outbound i6a to record statistics.  
   */
  if (flags & IPV6_FORWARDING)
    i6a = NULL;
  else if (i6a == NULL ||
	!IN6_ARE_ADDR_EQUAL(&i6a->i6a_addr.sin6_addr, &header->ipv6_src))
    {
      for (i6a = in6_ifaddr; i6a; i6a = i6a->i6a_next)
	if (i6a->i6a_ifp == ifp &&
	    IN6_ARE_ADDR_EQUAL(&i6a->i6a_addr.sin6_addr, &header->ipv6_src))
	  break;
    }

  /*
   * If small enough for path MTU, send, otherwise, fragment.
   */

  DPRINTF(IDL_FINISHED,("Before output, path mtu = %d, header is:\n",\
			   (int)outmtu));
  DDO(IDL_FINISHED,dump_ipv6(header));
  DDO(IDL_FINISHED,printf("Chain is:\n");dump_mchain(outgoing));

#if 0
/* DEBUG tunnel */
  DPRINTF(IDL_EVENT,("ROUTE passed to ipv6_output is:\n"));
  DDO(IDL_EVENT,if (ro) dump_rtentry(ro->ro_rt));
      if (ro->ro_rt && (ro->ro_rt->rt_flags & RTF_TUNNEL))
	  DPRINTF(IDL_FINISHED,("HEY !!  I see the tunnel!!!\n"));
      else {
	    DPRINTF(IDL_FINISHED,("HEY !!  I can't see the tunnel!!!\n"));
            if (ro->ro_rt == NULL)
                DPRINTF(IDL_FINISHED,("ro->ro_rt is null!!\n"));
            else
                 {
                   DPRINTF(IDL_FINISHED,("ro->ro_rt is not null!!\n"));
                   if (ro->ro_rt->rt_flags & RTF_TUNNEL)
                     DPRINTF(IDL_FINISHED,("HEY, I can see RTFTUNNEL!\n"));
                   else
                     DPRINTF(IDL_FINISHED,("HEY, I can't see RTFTUNNEL!\n"));
                 }
      }
/* END OF DEBUG tunnel */
#endif /* 0 */

  if (outgoing->m_pkthdr.len <= outmtu)
    {
DPRINTF(IDL_EVENT,("IPV6_OUTPUT():    Not entering fragmenting engine.\n"));
      header->ipv6_length = htons(outgoing->m_pkthdr.len - 
				  sizeof(struct ipv6));

      /*
       * If there is a route, and its TUNNEL bit is turned on, do not send
       * out the interface, but send through a tunneling routine, which will,
       * given information from the route, encapsulate the packet accordingly.
       *
       * Keith Sklower suggested a "rt_output() method" which would save
       * the checking here.
       */
      if (ro->ro_rt && (ro->ro_rt->rt_flags & RTF_TUNNEL)) {
	DPRINTF(IDL_EVENT,("ipv6_output():-Sending out IPV6 in IPV4/6 tunnel.\n"));
	error = ipv6_tunnel_output(outgoing, dst, ro->ro_rt);
      } else {
#if defined(_BSDI_VERSION) && (_BSDI_VERSION >= 199802)
	if (i6a) {
	      i6a->i6a_ifa.ifa_opackets++;
	      i6a->i6a_ifa.ifa_obytes += outgoing->m_pkthdr.len;
	}
#endif /* defined(_BSDI_VERSION) && (_BSDI_VERSION >= 199802) */
	error = (*ifp->if_output)(ifp, outgoing, (struct sockaddr *)dst, ro->ro_rt);
      }
      DPRINTF(IDL_FINISHED,("Lone IPv6 went out if (error = %d).\n",error));
      goto done;
    }


  /*
   * If I make it here, then the packet is too big for the path MTU, and must
   * be fragmented.
   */

  DPRINTF(IDL_EVENT,("IPV6_OUTPUT():    Entering fragmenting engine.\n"));

  if (flags & IPV6_FORWARDING)
    {
      error = EMSGSIZE;
      goto bad;
    }

  if (outgoing->m_pkthdr.len > 0xffff) {
    DPRINTF(IDL_ERROR,("Jumbogram needs fragmentation, something that can't be done\n"));
    ipv6stat.ips_odropped++;  /* ?!? */
    error = EINVAL;
    goto bad;
  }

  /*
   * The following check should never really take place.
   */
#ifdef DIAGNOSTIC
  if (outmtu < IPV6_MINMTU)
    {
      DPRINTF(IDL_ERROR,("Outbound MTU is less than IPV6_MINMTU (%d).\n",\
			    IPV6_MINMTU));
      error = ENETUNREACH;  /* Can you think of a better idea? */
      goto bad;
    }
#endif

  /*
   * ipv6_fragment returns a chain of outgoing packets.  It returns NULL
   * if something went wrong.
   */
  outgoing = ipv6_fragment(outgoing,outmtu);
  if (outgoing == NULL)
    error = ENOBUFS;    /* Can you think of a better idea? */

  DPRINTF(IDL_FINISHED,\
       ("ipv6_fragment() returned, attempting to send fragments out.\n"));

  /*
   * Walk through chain of fragments, and send them out.
   */
  while (outgoing != NULL)
    {
      struct mbuf *current = outgoing;

      DPRINTF(IDL_FINISHED,("In fragment-sending loop, error == %d.\n",\
			       error));
      outgoing = current->m_nextpkt;
      current->m_nextpkt = NULL;

      DDO(IDL_FINISHED,printf("Current (0x%lx) 1st mbuf is:\n", (unsigned long)current);\
	  dump_mbuf(current));

      if (error != 0)
	m_freem(current);
      else
	if (ro->ro_rt && (ro->ro_rt->rt_flags & RTF_TUNNEL)) {
	  DPRINTF(IDL_EVENT,("Sending fragments out tunnel.\n"));
	  error = ipv6_tunnel_output(current, dst, ro->ro_rt);
	} else {
          DPRINTF(IDL_EVENT,("After if_output(), error == %d.\n",error));
#if defined(_BSDI_VERSION) && (_BSDI_VERSION >= 199802)
          if (i6a) {
            i6a->i6a_ifa.ifa_opackets++;
            i6a->i6a_ifa.ifa_obytes += current->m_pkthdr.len;
          }
#endif /* defined(_BSDI_VERSION) && (_BSDI_VERSION >= 199802) */
          error = (*ifp->if_output)(ifp, current,(struct sockaddr *)dst, ro->ro_rt);
	}
    }

  if (error == 0)
    ipv6stat.ips_fragmented++;

done:
  if (ro == &ipv6route && (flags & IPV6_ROUTETOIF) == 0 && ro->ro_rt)
    RTFREE(ro->ro_rt);
  return (error);

bad:
  if (outgoing != NULL)
    m_freem(outgoing);
  goto done;
}

#define INDEX_TO_IFP(index, ifp)\
{\
   struct in6_ifnet *i6ifp; \
   for (i6ifp = in6_ifnet; i6ifp; i6ifp = i6ifp->i6ifp_next) \
     if (i6ifp->i6ifp_ifp->if_index == index) { \
       (ifp) = i6ifp->i6ifp_ifp; \
       break; \
     }; \
} \

/*----------------------------------------------------------------------
 * Set IPv6 multicast options.
 ----------------------------------------------------------------------*/
int ipv6_setmoptions(int optname, struct inpcb *inp, void *p, size_t len)
{
  register int error = 0;
  register int i;
  register struct ipv6_mreq *mreq;
  register struct ifnet *ifp = NULL;
  register struct ipv6_moptions *imo = inp->inp_moptions6;
  struct route6 ro;

  if (imo == NULL)
    {
      imo = (struct ipv6_moptions *)malloc(sizeof(*imo), M_IPMOPTS,M_WAITOK);
      if (imo == NULL)
	return ENOBUFS;
      inp->inp_moptions6 = imo;
      inp->inp_flags |= INP_IPV6_MCAST;
      imo->i6mo_multicast_ifp = NULL;
      imo->i6mo_multicast_ttl = IPV6_DEFAULT_MCAST_HOPS;
      imo->i6mo_multicast_loop = IPV6_DEFAULT_MCAST_LOOP;
      imo->i6mo_num_memberships = 0;
    }
  else   /* Only if points to v6 moptions can I set them! */
    if (!(inp->inp_flags & INP_IPV6_MCAST))
      return EEXIST;

  switch (optname)
    {
    case IPV6_MULTICAST_IF:
      {
	unsigned int index;
	if (!p || (len != sizeof(unsigned int))) {
	  error = EINVAL;
	  break;
	}

	index = *((int *)p);

	if (!index) {
	  imo->i6mo_multicast_ifp = NULL;
	  break;
	}

	INDEX_TO_IFP(index, ifp);
	if (!ifp || !(ifp->if_flags & IFF_MULTICAST))
	  error = EADDRNOTAVAIL;
	else
	  imo->i6mo_multicast_ifp = ifp;
      };
      break;

    case IPV6_MULTICAST_HOPS:
      /*
       * Set the IPv6 hop limit for outgoing multicast packets.
       */
      if (!p || (len != sizeof(int))) {
	error = EINVAL;
	break;
      }
      if (*((int *)p) == -1)
	imo->i6mo_multicast_ttl = IPV6_DEFAULT_MCAST_HOPS;
      else
	if ((*((int *)p) > -1) && (*((int *)p) < 256))
	  imo->i6mo_multicast_ttl = *((int *)p);
	else
	  error = EINVAL;
      break;
      
    case IPV6_MULTICAST_LOOP:
      /*
       * Set the loopback flag for outgoing multicast packets.
       * Must be zero or one.
       */
      if (!p || (len != sizeof(int))) {
	error = EINVAL;
	break;
      }
      switch(*((int *)p)) {
        case 0:
        case 1:
	  imo->i6mo_multicast_loop = *((int *)p);
	  break;
        case -1:
	  imo->i6mo_multicast_loop = IPV6_DEFAULT_MCAST_LOOP;
	  break;
        default:
	  error = EINVAL;
	  break;
      };
      break;
    case IPV6_ADD_MEMBERSHIP:
      /*
       * Add a multicast group membership.
       * Group must be a valid IP multicast address.
       */
      if (!p || (len != sizeof(struct ipv6_mreq))) {
	error = EINVAL;
	break;
      }
      mreq = (struct ipv6_mreq *)p;
      if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
	error = EINVAL;
	break;
      }
      /*
       * If no interface address was provided, use the interface of
       * the route to the given multicast address.
       */
      if (!mreq->ipv6mr_interface) {
	  ro.ro_rt = NULL;
	  ro.ro_dst.sin6_family = AF_INET6;
	  ro.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
	  ro.ro_dst.sin6_addr = mreq->ipv6mr_multiaddr;
	  rtalloc((struct route *)&ro);
	  if (ro.ro_rt == NULL)
	    {
	      error = EADDRNOTAVAIL;
	      break;
	    }
	  ifp = ro.ro_rt->rt_ifp;
	  rtfree(ro.ro_rt);
	}
      else {
	INDEX_TO_IFP(mreq->ipv6mr_interface, ifp);
      }
      /*
       * See if we found an interface, and confirm that it
       * supports multicast.
       */
      if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
	error = EADDRNOTAVAIL;
	break;
      }
      /*
       * See if the membership already exists or if all the
       * membership slots are full.
       */
      for (i = 0; i < imo->i6mo_num_memberships; ++i) {
	if (imo->i6mo_membership[i]->in6m_ifp == ifp &&
	    IN6_ARE_ADDR_EQUAL(&mreq->ipv6mr_multiaddr,
			       &imo->i6mo_membership[i]->in6m_addr))
	  break;
      }
      if (i < imo->i6mo_num_memberships) {
	error = EADDRINUSE;
	break;
      }
      if (i == IN6_MAX_MEMBERSHIPS) {
	error = ETOOMANYREFS;
	break;
      }
      /*
       * Everything looks good; add a new record to the multicast
       * address list for the given interface.
       */
      if ((imo->i6mo_membership[i] = in6_addmulti(&mreq->ipv6mr_multiaddr, ifp))
	  == NULL)
	{
	  error = ENOBUFS;
	  break;
	}
      ++imo->i6mo_num_memberships;
      break;

    case IPV6_DROP_MEMBERSHIP:
      /*
       * Drop a multicast group membership.
       * Group must be a valid IP multicast address.
       */
      if (!p || (len != sizeof(struct ipv6_mreq))) {
	error = EINVAL;
	break;
      }
      mreq = (struct ipv6_mreq *)p;
      if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
	error = EINVAL;
	break;
      }

      /*
       * If an interface index was specified, get a pointer to its ifnet
       * structure.
       */
      if (!mreq->ipv6mr_interface)
	ifp = NULL;
      else {
	INDEX_TO_IFP(mreq->ipv6mr_interface, ifp);
	if (ifp == NULL) {
	  error = EADDRNOTAVAIL;
	  break;
	}
      }

      /*
       * Find the membership in the membership array.
       */
      for (i = 0; i < imo->i6mo_num_memberships; ++i) {
	if ((ifp == NULL ||
	     imo->i6mo_membership[i]->in6m_ifp == ifp) &&
	    IN6_ARE_ADDR_EQUAL(&imo->i6mo_membership[i]->in6m_addr,
			       &mreq->ipv6mr_multiaddr))
	  break;
      }

      if (i == imo->i6mo_num_memberships) {
	error = EADDRNOTAVAIL;
	break;
      }
      /*
       * Give up the multicast address record to which the
       * membership points.
       */
      in6_delmulti(imo->i6mo_membership[i]);
      /*
       * Remove the gap in the membership array.
       */
      for (++i; i < imo->i6mo_num_memberships; ++i)
	imo->i6mo_membership[i-1] = imo->i6mo_membership[i];

      --imo->i6mo_num_memberships;

      break;
    default:
      error = EOPNOTSUPP;
      break;
    }
  
  if (imo->i6mo_multicast_ifp == NULL &&
      imo->i6mo_multicast_ttl == IPV6_DEFAULT_MCAST_HOPS &&
      imo->i6mo_multicast_loop == IPV6_DEFAULT_MCAST_LOOP &&
      imo->i6mo_num_memberships == 0) {
    free(inp->inp_moptions6, M_IPMOPTS);
    inp->inp_moptions6 = NULL;
    inp->inp_flags &= ~INP_IPV6_MCAST;
  }

  return (error);
}

#define IFP_TO_INDEX(ifp, index) \
{\
   (index) = ifp->if_index; \
}

/*----------------------------------------------------------------------
 * Get IPv6 multicast options.
 ----------------------------------------------------------------------*/
/* ... now assumes all returned values are ints... */
int ipv6_getmoptions(int optname, struct ipv6_moptions *i6mo, int *mp)
{
  switch (optname)
    {
    case IPV6_MULTICAST_IF:
      if (!i6mo == NULL || !i6mo->i6mo_multicast_ifp)
	*mp = 0;
      else {
	IFP_TO_INDEX(i6mo->i6mo_multicast_ifp, *(unsigned int *)mp);
      }
      return (0);

    case IPV6_MULTICAST_HOPS:
      *mp = i6mo ? IPV6_DEFAULT_MCAST_HOPS : i6mo->i6mo_multicast_ttl;
      return (0);

    case IPV6_MULTICAST_LOOP:
      *mp = i6mo ? IPV6_DEFAULT_MCAST_LOOP : i6mo->i6mo_multicast_loop;
      return (0);

    default:
      return (EOPNOTSUPP);
    }
}

/*----------------------------------------------------------------------
 * Free IPv6 multicast options.
 ----------------------------------------------------------------------*/
void
ipv6_freemoptions(i6mo)
     register struct ipv6_moptions *i6mo;
{
  register int i;

  if (i6mo != NULL)
    {
      for (i = 0 ; i < i6mo->i6mo_num_memberships ; i++)
	in6_delmulti(i6mo->i6mo_membership[i]);
      free(i6mo, M_IPMOPTS);
    }
}

/*----------------------------------------------------------------------
 * Handler for IPV6 [gs]etsockopt() calls.  One problem arises when an
 * AF_INET6 socket actually wants to set IPv4 options.
 *
 * The decision to call this or to call ip_ctloutput() is best left in
 * the hands of TCP/UDP/etc., which have information about which IP is
 * in use.
 *
 ----------------------------------------------------------------------*/

#if __FreeBSD__
int ipv6_ctloutput(struct socket *so, struct sockopt *sopt)
{
  register struct inpcb *inp;
  int op;
  int level;
  int optname;
  int optval;
  int error = 0;

  DPRINTF(FINISHED, ("ipv6_ctloutput(so=%08x, sopt=%08x)\n", OSDEP_PCAST(so),
         OSDEP_PCAST(sopt)));

  inp = sotoinpcb(so);

  switch(sopt->sopt_dir) {
    case SOPT_GET:
      op = PRCO_GETOPT;
      break;
    case SOPT_SET:
      op = PRCO_SETOPT;
      break;
    default:
      return EINVAL;
  };

  level = sopt->sopt_level;
  optname = sopt->sopt_name;

  DS();
#else /* __FreeBSD__ */
int
ipv6_ctloutput (op, so, level, optname, mp)
     int op;
     struct socket *so;
     int level;
     int optname;
     struct mbuf **mp;
{
  register struct inpcb *inp = sotoinpcb(so);
  struct mbuf *m = *mp;
  int error = 0;

  DPRINTF(IDL_EVENT, ("ipv6_ctloutput(op=%x,so=%08lx,level=%x,optname=%x,mp)\n", op, (unsigned long)so, level, optname));
#endif /* __FreeBSD__ */

  if ((level != IPPROTO_IP) && (level != IPPROTO_IPV6) && (level != IPPROTO_ROUTING) && (level != IPPROTO_ICMPV6)) {
#if !__FreeBSD__
    if (op == PRCO_SETOPT && *mp)
      m_free(*mp);
#endif /* !__FreeBSD__ */
    return EINVAL;
  }

  DS();
  switch (op) {
    case PRCO_SETOPT:
      switch(optname) {
        case IPV6_UNICAST_HOPS:
	  DPRINTF(IDL_GROSS_EVENT, ("ipv6_ctloutput: Reached IPV6_UNICAST_HOPS\n"));
#if __FreeBSD__
	  if (sopt->sopt_valsize != sizeof(int))
#else /* __FreeBSD__ */
	  if (m->m_len != sizeof(int))
#endif /* __FreeBSD__ */
	    error = EINVAL;
	  else {
	    struct tcpcb *tp;
#if __FreeBSD__
	    int i;

	    if (error = sooptcopyin(sopt, &i, sizeof(int), sizeof(int)))
	      break;
#else /* __FreeBSD__ */
            int i = *mtod(m, int *);
#endif /* __FreeBSD__ */

            if (i == -1)
              i = ipv6_defhoplmt;

            if ((i < 0) || (i > 255)) {
              error = EINVAL;
              break;
            };

	    inp->inp_ipv6.ipv6_hoplimit = i;

	    /*
	     *  Minor optimization for TCP.  We change the hoplimit  
	     *  in the template here so we don't have to do the extra
	     *  load before the ipv6_output() call in tcp_output() for 
	     *  every single packet (as is the case for IPv4).
	     */
	    DS();
#if !defined(_BSDI_VERSION) || (_BSDI_VERSION < 199802)
	     if ((so->so_type == SOCK_STREAM) && (tp = intotcpcb(inp)) &&
		  tp->t_template)
#if __FreeBSD__
		(mtod(tp->t_template, struct ipv6 *))->ipv6_hoplimit = i;
#else /* __FreeBSD__ */
		((struct ipv6 *)(tp->t_template))->ipv6_hoplimit = i;
#endif /* __FreeBSD__ */
#else /* !defined(_BSDI_VERSION) || (_BSDI_VERSION < 199802) */
	     if ((so->so_type == SOCK_STREAM) && (tp = intotcpcb(inp)))
		((struct ipv6 *)(tp->t_tcpiphdr))->ipv6_hoplimit = i;
#endif /* !defined(_BSDI_VERSION) || (_BSDI_VERSION < 199802) */
	  }
	  break;
	case IPV6_MULTICAST_IF:
	case IPV6_MULTICAST_HOPS:
	case IPV6_MULTICAST_LOOP:
        case IPV6_DROP_MEMBERSHIP:
        case IPV6_ADD_MEMBERSHIP:
#if __FreeBSD__
	  {
	    void *val;

	    if (!(val = OSDEP_MALLOC(sopt->sopt_valsize))) {
	      error = ENOMEM;
	      break;
	    };

	    if (error = sooptcopyin(sopt, val, sopt->sopt_valsize,
				    sopt->sopt_valsize)) {
	      OSDEP_FREE(val);
	      break;
	    };

	    error = ipv6_setmoptions(optname, inp, val, sopt->sopt_valsize);
	    OSDEP_FREE(val);
	  };
#else /* __FreeBSD__ */
          error = ipv6_setmoptions(optname, inp, mtod(m, void *), m->m_len);
#endif /* __FreeBSD__ */
	  break;
	case IPV6_ADDRFORM:
	  {
	    int newpf;
	    int oldpf = sotopf(inp->inp_socket);
	    union inpaddru new_faddru;
	    union inpaddru new_laddru;
	    int new_flags;
	    struct protosw *new_proto;
            int s;

#if __FreeBSD__
	    if (sopt->sopt_valsize != sizeof(int)) {
              DPRINTF(IDL_ERROR, ("addrform: valsize = %d\n",
				  sopt->sopt_valsize));
#else /* __FreeBSD__ */
	    if (m->m_len != sizeof(int)) {
              DPRINTF(IDL_ERROR, ("addrform: m->m_len = %d\n", m->m_len));
#endif /* __FreeBSD__ */
	      error = EINVAL;
              break;
            };

#if __FreeBSD__
	    if (error = sooptcopyin(sopt, &newpf, sizeof(int), sizeof(int)))
	      break;
#else /* __FreeBSD__ */
            newpf = *(mtod(m, int *));
#endif /* __FreeBSD__ */
	    
	    DPRINTF(IDL_ERROR, ("newpf = %d, oldpf = %d", newpf, oldpf));
	    
	    if (((newpf != AF_INET) && (newpf != AF_INET6)) ||
		((oldpf != AF_INET) && (oldpf != AF_INET6)))
	      return EINVAL;
	    
            DP(ERROR, __LINE__, d);
	    
	    if (newpf == oldpf)
	      return 0;
	    
	    DP(ERROR, inp->inp_flags, 08x);

	    if (newpf == AF_INET6)
	      if (!(inp->inp_flags & INP_IPV6_UNDEC) &&
		  !(inp->inp_flags & INP_IPV6_MAPPED))
		return EINVAL;

            DP(ERROR, __LINE__, d);

	    if (!(new_proto = pffindproto(newpf,
	      so->so_proto->pr_protocol,
              so->so_proto->pr_type)))
	      return EINVAL;

            DP(ERROR, new_proto->pr_domain->dom_family, d);

	    new_flags = inp->inp_flags;
	    new_faddru = inp->inp_faddru;
	    new_laddru = inp->inp_laddru;

	    if (newpf == AF_INET) {
	      if (new_flags & INP_IPV6_UNDEC) {
		new_flags &= ~(INP_IPV6 | INP_IPV6_MAPPED | INP_IPV6_UNDEC);
		new_laddru.iau_a4u.inaddr.s_addr = INADDR_ANY;
		new_faddru.iau_a4u.inaddr.s_addr = INADDR_ANY;
	      } else {
		new_flags &= ~(INP_IPV6 | INP_IPV6_MAPPED);
	      }
	    } else {
	      new_faddru.iau_addr6.s6_addr32[0] = 0;
	      new_faddru.iau_addr6.s6_addr32[1] = 0;
	      new_laddru.iau_addr6.s6_addr32[0] = 0;
	      new_laddru.iau_addr6.s6_addr32[1] = 0;
	      
	      if (new_laddru.iau_a4u.inaddr.s_addr == INADDR_ANY) {
		new_flags |= (INP_IPV6 | INP_IPV6_MAPPED | INP_IPV6_UNDEC);
	        new_faddru.iau_addr6.s6_addr32[2] = 0;
	        new_laddru.iau_addr6.s6_addr32[2] = 0;
	      } else {
		new_flags |= (INP_IPV6 | INP_IPV6_MAPPED);
	        new_faddru.iau_addr6.s6_addr32[2] = htonl(0xffff);
	        new_faddru.iau_addr6.s6_addr32[2] = htonl(0xffff);
	      }
	    }

	    s = splnet();

	    inp->inp_flags = new_flags;
            inp->inp_faddru = new_faddru;
            inp->inp_laddru = new_laddru;
            so->so_proto = new_proto;

            splx(s);
	  }
	  break;
	case IPV6_PKTINFO:
#if __FreeBSD__
	  if (sopt->sopt_valsize != sizeof(int)) {
#else /* __FreeBSD__ */
	  if (m->m_len != sizeof(int)) {
#endif /* __FreeBSD__ */
	    error = EINVAL;
            break;
          };

#if __FreeBSD__
	  if (error = sooptcopyin(sopt, &optval, sizeof(int), sizeof(int)))
	    break;

	  if (optval)
#else /* __FreeBSD__ */
	  if (*(mtod(m, int *)))
#endif /* __FreeBSD__ */
	    inp->inp_flags |= INP_RXINFO;
	  else
	    inp->inp_flags &= ~INP_RXINFO;
	  break;
	case IPV6_HOPOPTS:
#if __FreeBSD__
	  if (sopt->sopt_valsize != sizeof(int)) {
#else /* __FreeBSD__ */
	  if (m->m_len != sizeof(int)) {
#endif /* __FreeBSD__ */
	    error = EINVAL;
            break;
          };

#if __FreeBSD__
	  if (error = sooptcopyin(sopt, &optval, sizeof(int), sizeof(int)))
	    break;

	  if (optval)
#else /* __FreeBSD__ */
	  if (*(mtod(m, int *)))
#endif /* __FreeBSD__ */
	    inp->inp_flags |= INP_RXHOPOPTS;
	  else
	    inp->inp_flags &= ~INP_RXHOPOPTS;
	  break;
	case IPV6_DSTOPTS:
#if __FreeBSD__
	  if (sopt->sopt_valsize != sizeof(int)) {
#else /* __FreeBSD__ */
	  if (m->m_len != sizeof(int)) {
#endif /* __FreeBSD__ */
	    error = EINVAL;
            break;
          };

#if __FreeBSD__
	  if (error = sooptcopyin(sopt, &optval, sizeof(int), sizeof(int)))
	    break;

	  if (optval)
#else /* __FreeBSD__ */
	  if (*(mtod(m, int *)))
#endif /* __FreeBSD__ */
	    inp->inp_flags |= INP_RXDSTOPTS;
	  else
	    inp->inp_flags &= ~INP_RXDSTOPTS;
	  break;
	case IPV6_RTHDR:
#if __FreeBSD__
	  if (sopt->sopt_valsize != sizeof(int)) {
#else /* __FreeBSD__ */
	  if (m->m_len != sizeof(int)) {
#endif /* __FreeBSD__ */
	    error = EINVAL;
            break;
          };

#if __FreeBSD__
	  if (error = sooptcopyin(sopt, &optval, sizeof(int), sizeof(int)))
	    break;

	  if (optval)
#else /* __FreeBSD__ */
	  if (*(mtod(m, int *)))
#endif /* __FreeBSD__ */
	    inp->inp_flags |= INP_RXSRCRT;
	  else
	    inp->inp_flags &= ~INP_RXSRCRT;
	  break;
        case IPV6_HOPLIMIT:
#if __FreeBSD__
	  if (sopt->sopt_valsize != sizeof(int)) {
#else /* __FreeBSD__ */
	  if (m->m_len != sizeof(int)) {
#endif /* __FreeBSD__ */
	    error = EINVAL;
            break;
          };

#if __FreeBSD__
	  if (error = sooptcopyin(sopt, &optval, sizeof(int), sizeof(int)))
	    break;

	  if (optval)
#else /* __FreeBSD__ */
	  if (*(mtod(m, int *)))
#endif /* __FreeBSD__ */
	    inp->inp_flags |= INP_HOPLIMIT;
	  else
	    inp->inp_flags &= ~INP_HOPLIMIT;
	  break;
	default:
	  error = ENOPROTOOPT;
	  break;
	}
#if !__FreeBSD__
      if (m)
	m_free(m);
#endif /* !__FreeBSD__ */
      break;
    case PRCO_GETOPT:
      switch(optname)
	{
	case IPV6_ADDRFORM:
	  {
	    int pf = sotopf(inp->inp_socket);
            DP(ERROR, pf, d);
	    if ((pf != PF_INET) && (pf != PF_INET6))
	      return EINVAL;
            DP(ERROR, __LINE__, d);
#if __FreeBSD__
	    error = sooptcopyout(sopt, &pf, sizeof(int));
#else /* __FreeBSD__ */
	    if (!(m = m_get(M_NOWAIT, MT_SOOPTS))) {
	      error = ENOBUFS;
	      break;
	    };
	    *mp = m;
	    m->m_len = sizeof(int);
	    *mtod(m, int *) = pf;
#endif /* __FreeBSD__ */
	  }
	  break;
	case IPV6_UNICAST_HOPS:
	  DPRINTF(IDL_GROSS_EVENT,("ipv6_ctloutput(): Reached IPV6_UNICAST_HOPS:\n"));
#if __FreeBSD__
	  error = sooptcopyout(sopt, &inp->inp_ipv6.ipv6_hoplimit,
			       sizeof(int));
#else /* __FreeBSD__ */
	  if (!(m = m_get(M_NOWAIT, MT_SOOPTS))) {
	    error = ENOBUFS;
	    break;
	  };
	  *mp = m;
	  m->m_len = sizeof(int);
	  *mtod(m, int *) = inp->inp_ipv6.ipv6_hoplimit;
#endif /* __FreeBSD__ */
	  break;
	case IPV6_MULTICAST_IF:
	case IPV6_MULTICAST_HOPS:
	case IPV6_MULTICAST_LOOP:
	case IPV6_DROP_MEMBERSHIP:
	case IPV6_ADD_MEMBERSHIP:
#if __FreeBSD__
  	  error = ipv6_getmoptions(optname, inp->inp_moptions6, &optval);
	  if (error)
	    break;

	  error = sooptcopyout(sopt, &optval, sizeof(int));
#else /* __FreeBSD__ */
	  if (!(*mp = m_get(M_NOWAIT, MT_SOOPTS))) {
	    error = ENOBUFS;
	    break;
	  };

  	  error = ipv6_getmoptions(optname, inp->inp_moptions6,
				   mtod(*mp, int *));
#endif /* __FreeBSD__ */
	  break;
	case IPV6_PKTINFO:
#if __FreeBSD__
	  optval = (inp->inp_flags & INP_RXINFO) ? 1 : 0;
	  error = sooptcopyout(sopt, &optval, sizeof(int));
#else /* __FreeBSD__ */
	  if (!(m = m_get(M_NOWAIT, MT_SOOPTS))) {
	    error = ENOBUFS;
	    break;
	  };
	  *mp = m;
	  m->m_len = sizeof(int);
	  *mtod(m, int *) = (inp->inp_flags & INP_RXINFO) ? 1 : 0;
#endif /* __FreeBSD__ */
	  break;
	case IPV6_HOPOPTS:
#if __FreeBSD__
	  optval = (inp->inp_flags & INP_RXHOPOPTS) ? 1 : 0;
	  error = sooptcopyout(sopt, &optval, sizeof(int));
#else /* __FreeBSD__ */
	  if (!(*mp = m = m_get(M_NOWAIT, MT_SOOPTS))) {
	    error = ENOBUFS;
	    break;
	  };
	  m->m_len = sizeof(int);
	  *mtod(m, int *) = (inp->inp_flags & INP_RXHOPOPTS) ? 1 : 0;
#endif /* __FreeBSD__ */
	  break;
	case IPV6_DSTOPTS:
#if __FreeBSD__
	  optval = (inp->inp_flags & INP_RXDSTOPTS) ? 1 : 0;
	  error = sooptcopyout(sopt, &optval, sizeof(int));
#else /* __FreeBSD__ */
	  if (!(*mp = m = m_get(M_NOWAIT, MT_SOOPTS))) {
	    error = ENOBUFS;
	    break;
	  };
	  m->m_len = sizeof(int);
	  *mtod(m, int *) = (inp->inp_flags & INP_RXDSTOPTS) ? 1 : 0;
#endif /* __FreeBSD__ */
	  break;
	case IPV6_RTHDR:
#if __FreeBSD__
	  optval = (inp->inp_flags & INP_RXSRCRT) ? 1 : 0;
	  error = sooptcopyout(sopt, &optval, sizeof(int));
#else /* __FreeBSD__ */
	  if (!(*mp = m = m_get(M_NOWAIT, MT_SOOPTS))) {
	    error = ENOBUFS;
	    break;
	  };
	  m->m_len = sizeof(int);
	  *mtod(m, int *) = (inp->inp_flags & INP_RXSRCRT) ? 1 : 0;
#endif /* __FreeBSD__ */
	  break;
	case IPV6_HOPLIMIT:
#if __FreeBSD__
	  optval = (inp->inp_flags & INP_HOPLIMIT) ? 1 : 0;
	  error = sooptcopyout(sopt, &optval, sizeof(int));
#else /* __FreeBSD__ */
	  if (!(*mp = m = m_get(M_NOWAIT, MT_SOOPTS))) {
	    error = ENOBUFS;
	    break;
	  };
	  m->m_len = sizeof(int);
	  *mtod(m, int *) = (inp->inp_flags & INP_HOPLIMIT) ? 1 : 0;
#endif /* __FreeBSD__ */
	  break;
	default:
	  error = ENOPROTOOPT;
	  break;
	}
      break;
    default:
      error = ENOPROTOOPT;
      break;
    }

  return error;
}

/*----------------------------------------------------------------------
 * Loops back multicast packets to groups of which I'm a member.
 ----------------------------------------------------------------------*/

void
ipv6_mloopback(ifp, m, dst)
     struct ifnet *ifp;
     register struct mbuf *m;
     register struct sockaddr_in6 *dst;
{
  struct mbuf *copym;
  register struct ipv6 *header;

#if defined(_BSDI_VERSION) && _BSDI_VERSION >= 199802
  if (!loifp)
    return;
#endif /* defined(_BSDI_VERSION) && _BSDI_VERSION >= 199802 */

  /*
   * Copy mbuf chain in m, and send to loopback interface.
   */

  copym=m_copym(m,0,M_COPYALL,M_DONTWAIT);
  if (copym != NULL)
    {
      header=mtod(copym,struct ipv6 *);
      /* Jumbogram? */
      header->ipv6_length = htons(header->ipv6_length);
      /*
       * Also, there's an issue about address collision.  You may want to
       * check the ipv6 destination (or the dst address) and set the ifp
       * argument to looutput to be the loopback interface itself iff
       * it is to a solicited nodes multicast.
       *
       * Then again, it may be easier for the soliciting code to burn another
       * m_flags bit, and look for it on loopback.
       */
#if defined(_BSDI_VERSION) && _BSDI_VERSION >= 199802
      (*loifp->if_output)(ifp, copym, (struct sockaddr *)dst, NULL);
#else /* defined(_BSDI_VERSION) && _BSDI_VERSION >= 199802 */
#if __FreeBSD__
      loif->if_output(ifp,copym,(struct sockaddr *)dst,NULL);
#else /* __FreeBSD__ */
      looutput(ifp,copym,(struct sockaddr *)dst,NULL);
#endif /* __FreeBSD__ */
#endif /* defined(_BSDI_VERSION) && _BSDI_VERSION >= 199802 */
    }
  else 
    DPRINTF(IDL_GROSS_EVENT,("m_copym() failed in ipv6_mloopback.\n"));
}

/*----------------------------------------------------------------------
 * Fragment IPv6 datagram.
 *
 * Given a large mbuf chain m, with only its ipv6->ipv6_length field in
 * host order, fragment into mtu sized chunks, and return a meta-chain
 * with m->m_nextpkt being the subsequent fragments.  If there's a problem,
 * m_freem all fragments, and return NULL.  Also, all ipv6->ipv6_length
 * fields are in network order, i.e. ready-to-transmit.
 *
 * Note that there is an unusually large amount of calls to m_pullup,
 * m_copym2etc. here.  This will cause performance hits.
 *
 * A redesign of this is in order, but that will have to wait.
 ----------------------------------------------------------------------*/

struct mbuf *
ipv6_fragment(m,mtu)
     struct mbuf *m;
     int mtu;
{
  uint8_t nextopt = IPPROTO_FRAGMENT;
  uint divpoint = sizeof(struct ipv6), chunksize, sofar = 0, goal;
  struct mbuf *retval = NULL, *newfrag = NULL;
  struct ipv6_fraghdr *frag = NULL;

  outfragid++;

  DPRINTF(IDL_FINISHED,\
	  ("Entering ipv6_fragment, m_pkthdr.len = %d, mtu = %d\n",\
	   m->m_pkthdr.len, mtu));

  /*
   * Find the dividing point between pre-fragment and post-fragment options.
   */
  divpoint = ipv6_finddivpoint(m, &nextopt);

  /*
   * Options being larger than MTU can happen, especially given large routing
   * headers and large options bags.
   */
  if (divpoint + sizeof(struct ipv6_fraghdr) >= mtu)
    {
      DPRINTF(IDL_ERROR, 
	      ("ipv6_fragment(): Options are larger than passed-in MTU.\n"));
      m_freem(m);
      return NULL;
    }

#ifdef DIAGNOSTIC
  if (divpoint & 0x7)
    panic("divpoint not a multiple of 8!");
#endif

  /*
   * sofar keeps track of how much I've fragmented, chunksize is how
   * much per fragment, goal is how much data to actually fragment.
   */
  chunksize = mtu - divpoint - sizeof(struct ipv6_fraghdr);
  chunksize &= ~7;
  goal = m->m_pkthdr.len - divpoint;

  DPRINTF(IDL_FINISHED, \
	  ("Found divpoint (%d), nextopt (%d), chunksize(%d) goal(%d)\n",\
	   divpoint, nextopt, chunksize,goal));

  while (sofar < goal)
    if ((newfrag = m_copym2(m, 0, divpoint, M_DONTWAIT)) != NULL)
      {
	struct mbuf *fraghdrmbuf = NULL;
	struct ipv6 *ipv6 = NULL;
	uint tocopy = (chunksize <= (goal - sofar))?chunksize:(goal - sofar);

	DPRINTF(IDL_FINISHED,("tocopy == %d\n",tocopy));

	/*
	 * Create a new IPv6 fragment, using the header that was slightly
	 * munged by ipv6_finddivpoint().
	 *
	 * The above m_copym2() creates a copy of the first
	 */

	newfrag->m_nextpkt = retval;
	retval = newfrag;

	/*
	 * Append IPv6 fragment header to pre-fragment
	 */
	for(fraghdrmbuf = retval;fraghdrmbuf->m_next != NULL;)
	  fraghdrmbuf = fraghdrmbuf->m_next;
	MGET(fraghdrmbuf->m_next,M_DONTWAIT,MT_DATA);
	if (fraghdrmbuf->m_next == NULL)
	  {
	    DPRINTF(IDL_ERROR,("couldn't get new mbuf for frag hdr\n"));
	    ipv6stat.ips_odropped++;
	    goto bail;  
	  }

	fraghdrmbuf = fraghdrmbuf->m_next;
	fraghdrmbuf->m_len = sizeof(struct ipv6_fraghdr);
	retval->m_pkthdr.len += sizeof(struct ipv6_fraghdr);
	frag = mtod(fraghdrmbuf,struct ipv6_fraghdr *);
	frag->frag_nexthdr = nextopt;
	frag->frag_reserved = 0;
	frag->frag_bitsoffset = htons(sofar | ((sofar + tocopy) < goal));
	frag->frag_id = outfragid;

	/*
	 * Copy off (rather than just m_split()) the portion of data that
	 * goes with this fragment.
	 */
	if ((fraghdrmbuf->m_next = m_copym2(m,divpoint + sofar,tocopy,
					    M_DONTWAIT)) == NULL)
	  {
	    DPRINTF(IDL_ERROR,("couldn't copy segment.\n"));
	    goto bail;  
	  }
	retval->m_pkthdr.len += tocopy;

	/*
	 * Update fragment's IPv6 header appropriately.
	 */
	ipv6 = mtod(retval,struct ipv6 *);
	ipv6->ipv6_length = htons(retval->m_pkthdr.len - sizeof(struct ipv6));
	ipv6->ipv6_nexthdr = IPPROTO_FRAGMENT;
	ipv6stat.ips_ofragments++;
	sofar += tocopy;
      }
    else
      {
	/*
	 * Creation of new fragment failed.
	 */
	DPRINTF(IDL_ERROR,("m_copym2() failed in fragmentation loop.\n"));
	ipv6stat.ips_odropped++;
      bail:   
	DPRINTF(IDL_ERROR,("Bailing out of ipv6_fragment()\n"));
	while (retval != NULL)
	  {
	    newfrag = retval;
	    retval = retval->m_nextpkt;
	    m_freem(newfrag);
	  }
	m_freem(m);
	return NULL;
      }

  m_freem(m);

  /* Dump mbuf chain list constructed for debugging purposes. */
  DDO(IDL_FINISHED,\
      for (newfrag = retval; newfrag != NULL; newfrag = newfrag->m_nextpkt)\
        dump_mbuf(newfrag) );

  return retval;
}

/*----------------------------------------------------------------------
 * Find the dividing point between pre-fragment and post-fragment options.
 * The argument nexthdr is read/write, on input, it is the next header
 * value that should be written into the previous header's "next hdr" field,
 * and what is written back is what used to be in the previous field's
 * "next hdr" field.  For example:
 *
 * IP (next hdr = routing)       becomes -->     IP (next hdr = routing)
 * Routing (next hdr = TCP)                      Routing (next hdr = fragment)
 * TCP                                           TCP
 * argument nexthdr = fragment                   argument nexthdr = TCP
 *
 * This function returns the length of the pre-fragment options, ideal for
 * calls to m_split.
 *
 * As in ipv6_fragment, too many calls to m_pullup/m_pullup2 are performed
 * here.  Another redesign is called for, but not now.
 ----------------------------------------------------------------------*/

int
ipv6_finddivpoint(m, nexthdr)
     struct mbuf *m;
     uint8_t *nexthdr;
{
  uint8_t iprevopt, *prevopt = &(iprevopt), new = *nexthdr;
  uint8_t *nextopt;
  uint divpoint,maybe = 0;

  /*
   * IPv4 authentication code calls this function too.  It is likely that
   * v4 will just return almost immedately, after determining options
   * length. (i.e. never go through the while loop.)
   */
  if (mtod(m, struct ip *)->ip_v == 4)
    {
      iprevopt = IPPROTO_IPV4;
      nextopt = &(mtod(m, struct ip *)->ip_p);
      divpoint = sizeof(struct ip);
    }
  else
    {
      iprevopt = IPPROTO_IPV6;
      nextopt = &(mtod(m, struct ipv6 *)->ipv6_nexthdr);
      divpoint = sizeof(struct ipv6);
    }

  /*
   * Scan through options finding dividing point.  Dividing point
   * for authentication and fragmentation is the same place.
   *
   * Some weirdness here is that there MIGHT be a "Destination options bag"
   * which is actually a "per source-route-hop" bag.  There is a strong
   * argument for giving this particular options bag a separate type, but
   * for now, kludge around it.
   *
   * The "maybe" variable takes into account the length of this options bag.
   */
  while (IS_PREFRAG(*nextopt) && *prevopt != IPPROTO_ROUTING)
    {
      struct ipv6_srcroute0 *i6sr;
      struct ipv6_opthdr *oh;
      
      /*
       * ASSUMES: both nextopt and length will be in the first
       *          8 bytes of ANY pre-fragment header.
       */

      if ((divpoint + maybe + 8) > MHLEN)
	{
	  /* 
	   * This becomes complicated.  Try and collect invariant part into
	   * first (now cluster) mbuf on chain.  m_pullup() doesn't work with
	   * clusters, so either write m_pullup2() or inline it here.
	   *
	   * m_pullup2(), unlike m_pullup() will only collect exactly
	   * how many bytes the user requested.  This is to avoid problems
	   * with m_copym() and altering data that is merely referenced
	   * multiple times, rather than actually copied.  (We may eliminate
	   * the Net/2 hack of adding m_copym2().)
	   */
	  if ((m = m_pullup2(m,divpoint + maybe + 8)) == NULL)
	    {
	      DPRINTF(IDL_ERROR,\
		      ("m_pullup2(%d) failed in ipv6_fragment().\n",\
		       divpoint + maybe + 8));
	      return 0;
	    }
	}
      else
	{
	  if ((m = m_pullup(m,divpoint + maybe + 8)) == NULL)
	    {
	      DPRINTF(IDL_ERROR,\
		      ("m_pullup() failed in ipv6_fragment().\n"));
	      return 0;
	    }
	}

      /*
       * Find nextopt, and advance accordingly.
       */
      switch (*nextopt)
	{
	case IPPROTO_HOPOPTS:
	  /*
	   * Hop-by-hops should be right after IPv6 hdr.  If extra is nonzero,
	   * then there was a destination options bag.  If divpoint is not
	   * only the size of the IPv6 header, then something came before
	   * hop-by-hop options.  This is not good.
	   */
	  if (maybe || divpoint != sizeof(struct ipv6))
	    {
	      DPRINTF(IDL_ERROR, 
		      ("ipv6_input():  Weird ordering in headers.\n"));
	      m_freem(m);
	      return 0;
	    }
	  oh = (struct ipv6_opthdr *)(m->m_data + divpoint);
	  prevopt = nextopt;
	  nextopt = &(oh->oh_nexthdr);
	  divpoint += 8 + (oh->oh_extlen << 3);
	  if (oh->oh_extlen) {
	    if (divpoint > MHLEN)
	      {
		if ((m = m_pullup2(m,divpoint)) == NULL)
		  {
		    DPRINTF(IDL_EVENT,\
			    ("m_pullup2(%d) failed in IPPROTO_HOPOPTS nextopt.\n",\
			     divpoint));
		    return 0;
		  }
	      }
	    else
	      {
		if ((m = m_pullup(m,divpoint)) == NULL)
		  {
		    DPRINTF(IDL_EVENT,\
			    ("m_pullup() failed in IPPROTO_HOPOPTS nextopt.\n"));
		    return 0;
		  }
	      }
          }
	  break;
	case IPPROTO_DSTOPTS:
	  oh = (struct ipv6_opthdr *)(m->m_data + divpoint);	
	  prevopt = nextopt;
	  nextopt = &(oh->oh_nexthdr);
	  maybe = 8 + (oh->oh_extlen << 3);
	  if (oh->oh_extlen) {
	    if ( divpoint + maybe > MHLEN)
	      {
		if ((m = m_pullup2(m,divpoint + maybe)) == NULL)
		  {
		    DPRINTF(IDL_EVENT,\
			    ("m_pullup2(%d) failed in IPPROTO_DSTOPTS nextopt.\n",\
			     divpoint+maybe));
		    return 0;
		  }
	      }
	    else
	      {
		if ((m = m_pullup(m,divpoint + maybe)) == NULL)
		  {
		    DPRINTF(IDL_EVENT,\
			    ("m_pullup() failed in IPPROTO_DSTOPTS nextopt.\n"));
		    return 0;
		  }
	      }
          }
	  break;
	case IPPROTO_ROUTING:
	  if (maybe)  /* i.e. if we had a destination options bag before
			 this routing header, we should advance the dividing
			 point. */
	    divpoint += maybe;
	  maybe = 0;
	  i6sr = (struct ipv6_srcroute0 *)(m->m_data + divpoint);
	  prevopt = nextopt;
	  nextopt = &(i6sr->i6sr_nexthdr);
	  switch (i6sr->i6sr_type)
	    {
	    case 0:
	      divpoint += 8 + (i6sr->i6sr_len * 8);
	      break;
	    default:
	      DPRINTF(IDL_ERROR, 
		      ("ipv6_input():  Unknown outbound routing header.\n"));
	      break;
	    }
	  if (divpoint > MHLEN)
	      {
		if ((m = m_pullup2(m,divpoint)) == NULL)
		  {
		    DPRINTF(IDL_EVENT,\
			    ("m_pullup2(%d) failed in IPPROTO_ROUTING nextopt.\n",\
			     divpoint));
		    return 0;
		  }
	      }
	    else
	      {
		if ((m = m_pullup(m,divpoint)) == NULL)
		  {
		    DPRINTF(IDL_EVENT,\
			    ("m_pullup() failed in IPPROTO_ROUTING nextopt.\n"));
		    return 0;
		  }
	      }
	  break;
	}  /* End of switch statement. */
    };  /* End of while loop. */
    *nexthdr = *nextopt;
    *nextopt = new;
    return divpoint;
}

int ipv6_controltoheader(struct mbuf **m, struct mbuf *control, struct ifnet **forceifp, int *payload)
{
  struct cmsghdr *cmsghdr;
  int error = EINVAL;
  struct mbuf *srcrtm = NULL;

  DPRINTF(IDL_EVENT, ("ipv6_controltoheader(m=%08lx, control=%08lx, forceif=%08lx, payload=%08lx)\n", (unsigned long)m, (unsigned long)control, (unsigned long)forceifp, (unsigned long)payload));
  DDO(IDL_EVENT, dump_mchain(control));

  while((control = m_pullup2(control, sizeof(struct cmsghdr))) &&
	(cmsghdr = mtod(control, struct cmsghdr *)) &&
	(control = m_pullup2(control, cmsghdr->cmsg_len))) {
    cmsghdr = mtod(control, struct cmsghdr *);
    switch(cmsghdr->cmsg_level) {
      case IPPROTO_IPV6:
	switch(cmsghdr->cmsg_type) {
	  case IPV6_PKTINFO:
	    {
	      struct in6_pktinfo in6_pktinfo;
	      struct in6_ifnet *i6ifp;
	      struct ifaddr *ifa;

	      if (cmsghdr->cmsg_len != sizeof(struct cmsghdr) + sizeof(struct in6_pktinfo))
		goto ret;

	      bcopy((caddr_t)cmsghdr + sizeof(struct cmsghdr), &in6_pktinfo, sizeof(struct in6_pktinfo));

              if (!in6_pktinfo.ipi6_ifindex) {
                if (IN6_IS_ADDR_UNSPECIFIED(&in6_pktinfo.ipi6_addr)) {
                  DPRINTF(IDL_EVENT, ("ipv6_controltoheader: in degenerate IPV6_PKTINFO case\n"));
                  break;
                } else {
                  struct in6_ifaddr *i6a;
                  DPRINTF(IDL_EVENT, ("ipv6_controltoheader: in index = unspec, addr = spec case\n"));
                  for (i6a = in6_ifaddr; i6a; i6a = i6a->i6a_next)
                    if (IN6_ARE_ADDR_EQUAL(&I6A_SIN(i6a)->sin6_addr, &in6_pktinfo.ipi6_addr))
                      goto l2;
                  goto ret;
                };
              };

              DPRINTF(FINISHED, ("ipv6_controltoheader: in index = spec case\n"));

	      for (i6ifp = in6_ifnet; i6ifp && (i6ifp->i6ifp_ifp->if_index != in6_pktinfo.ipi6_ifindex); i6ifp = i6ifp->i6ifp_next);

              if (!i6ifp)
                goto ret;

              if (IN6_IS_ADDR_UNSPECIFIED(&in6_pktinfo.ipi6_addr)) {
                DPRINTF(FINISHED, ("ipv6_controltoheader: in index = spec, addr = unspec case\n"));
                goto l1;
              };

              DPRINTF(FINISHED, ("ipv6_controltoheader: in index = spec, addr = spec case\n"));

#ifdef __FreeBSD__
	      for (ifa = i6ifp->i6ifp_ifp->if_addrhead.tqh_first; ifa; ifa = ifa->ifa_link.tqe_next) {
#else /* __FreeBSD__ */
#if __NetBSD__ || __OpenBSD__
	      for (ifa = i6ifp->i6ifp_ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) {
#else /* __NetBSD__ || __OpenBSD__ */
	      for (ifa = i6ifp->i6ifp_ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
#endif /* __NetBSD__ || __OpenBSD__ */
#endif /* __FreeBSD__ */
		if ((ifa->ifa_addr->sa_family == AF_INET6) && !bcmp(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr, &in6_pktinfo.ipi6_addr, sizeof(struct in6_addr)))
		  goto l1;
              };
	      goto ret;

l1:           *forceifp = i6ifp->i6ifp_ifp;
l2:	      bcopy(&in6_pktinfo.ipi6_addr, &(mtod(*m, struct ipv6 *)->ipv6_src), sizeof(struct in6_addr));
	    };
	    break;
          case IPV6_HOPLIMIT:
            {
            int i;

	    if (cmsghdr->cmsg_len != sizeof(struct cmsghdr) + sizeof(int))
              goto ret;

            i = *((int *)((caddr_t)cmsghdr + sizeof(struct cmsghdr)));

            if (i == -1) {
              if (IN6_IS_ADDR_MULTICAST(&mtod(*m, struct ipv6 *)->ipv6_dst))
                i = IPV6_DEFAULT_MCAST_HOPS;
              else
                i = ipv6_defhoplmt;
            }

            if ((i < 0) || (i > 255))
              goto ret;

            mtod(*m, struct ipv6 *)->ipv6_hoplimit = i;
            };
            break;
	  default:
	    goto ret;
	};
	break;
      case IPPROTO_ROUTING:
	MGET(srcrtm, M_DONTWAIT, MT_DATA);
	if (!srcrtm) {
	  error = ENOBUFS;
	  goto ret;
	};

	srcrtm->m_len = cmsghdr->cmsg_len - sizeof(struct cmsghdr) + 3;
	if (srcrtm->m_len > MLEN) {
	  DPRINTF(IDL_ERROR, ("ipv6_controltoheader: requested source route that we can't fit in an mbuf (length %d)\n", srcrtm->m_len));
	  goto ret;
	};

	if (srcrtm->m_len & 7) {
	  DPRINTF(IDL_ERROR, ("ipv6_controltoheader: requested source route has an invalid length; %d needs to be a multiple of eight bytes\n", srcrtm->m_len));
          goto ret;
        };

	*(mtod(srcrtm, uint8_t *) + 1) = (srcrtm->m_len >> 3) - 1;
        *(mtod(srcrtm, uint8_t *) + 2) = cmsghdr->cmsg_type;
	bcopy((caddr_t *)cmsghdr + sizeof(struct cmsghdr), mtod(srcrtm, uint8_t *) + 3, cmsghdr->cmsg_len - sizeof(struct cmsghdr));
        break;
      case IPPROTO_HOPOPTS:
      case IPPROTO_DSTOPTS:
	/* XXX */
	goto ret;
      default:
	goto ret;
    };
    m_adj(control, cmsghdr->cmsg_len);
    if (!control->m_len)
      goto finish;
  };

  DPRINTF(IDL_ERROR, ("ipv6_controltoheader: pullups failed\n"));
  goto ret;

finish:
  if (srcrtm) {
    struct mbuf *m2;
    DPRINTF(IDL_EVENT, ("ipv6_controltoheader: in srcrtm case\n"));
    if (!(m2 = m_split(*m, sizeof(struct ipv6), M_DONTWAIT)))
      goto ret;
    (*m)->m_next = srcrtm;
    srcrtm->m_next = m2;
    *mtod(srcrtm, uint8_t *) = mtod(*m, struct ipv6 *)->ipv6_nexthdr;
    mtod(*m, struct ipv6 *)->ipv6_nexthdr = IPPROTO_ROUTING;

    *payload += srcrtm->m_len;
    (*m)->m_pkthdr.len += srcrtm->m_len;
  };
  m_freem(control);
  DPRINTF(IDL_FINISHED, ("ipv6_controltoheader: returning\n"));
  return 0;

ret:
  DPRINTF(IDL_ERROR, ("ipv6_controltoheader: returning error %d\n", error));
  if (srcrtm)
    m_free(srcrtm);
  if (control)
    m_freem(control);
  return error;
};