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
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
*/
#include <sys/strsun.h>
#include <sys/sdt.h>
#include <sys/mac.h>
#include <sys/mac_impl.h>
#include <sys/mac_client_impl.h>
#include <sys/mac_client_priv.h>
#include <sys/ethernet.h>
#include <sys/vlan.h>
#include <sys/dlpi.h>
#include <sys/avl.h>
#include <inet/ip.h>
#include <inet/ip6.h>
#include <inet/arp.h>
#include <netinet/arp.h>
#include <netinet/udp.h>
#include <netinet/dhcp.h>
#include <netinet/dhcp6.h>
/*
* Implementation overview for DHCP address detection
*
* The purpose of DHCP address detection is to relieve the user of having to
* manually configure static IP addresses when ip-nospoof protection is turned
* on. To achieve this, the mac layer needs to intercept DHCP packets to
* determine the assigned IP addresses.
*
* A DHCP handshake between client and server typically requires at least
* 4 messages:
*
* 1. DISCOVER - client attempts to locate DHCP servers via a
* broadcast message to its subnet.
* 2. OFFER - server responds to client with an IP address and
* other parameters.
* 3. REQUEST - client requests the offered address.
* 4. ACK - server verifies that the requested address matches
* the one it offered.
*
* DHCPv6 behaves pretty much the same way aside from different message names.
*
* Address information is embedded in either the OFFER or REQUEST message.
* We chose to intercept REQUEST because this is at the last part of the
* handshake and it indicates that the client intends to keep the address.
* Intercepting OFFERs is unreliable because the client may receive multiple
* offers from different servers, and we can't tell which address the client
* will keep.
*
* Each DHCP message has a transaction ID. We use this transaction ID to match
* REQUESTs with ACKs received from servers.
*
* For IPv4, the process to acquire a DHCP-assigned address is as follows:
*
* 1. Client sends REQUEST. a new dhcpv4_txn_t object is created and inserted
* in the the mci_v4_pending_txn table (keyed by xid). This object represents
* a new transaction. It contains the xid, the client ID and requested IP
* address.
*
* 2. Server responds with an ACK. The xid from this ACK is used to lookup the
* pending transaction from the mci_v4_pending_txn table. Once the object is
* found, it is removed from the pending table and inserted into the
* completed table (mci_v4_completed_txn, keyed by client ID) and the dynamic
* IP table (mci_v4_dyn_ip, keyed by IP address).
*
* 3. An outgoing packet that goes through the ip-nospoof path will be checked
* against the dynamic IP table. Packets that have the assigned DHCP address
* as the source IP address will pass the check and be admitted onto the
* network.
*
* IPv4 notes:
*
* If the server never responds with an ACK, there is a timer that is set after
* the insertion of the transaction into the pending table. When the timer
* fires, it will check whether the transaction is old (by comparing current
* time and the txn's timestamp), if so the transaction will be freed. along
* with this, any transaction in the completed/dyn-ip tables matching the client
* ID of this stale transaction will also be freed. If the client fails to
* extend a lease, we want to stop the client from using any IP addresses that
* were granted previously.
*
* A RELEASE message from the client will not cause a transaction to be created.
* The client ID in the RELEASE message will be used for finding and removing
* transactions in the completed and dyn-ip tables.
*
*
* For IPv6, the process to acquire a DHCPv6-assigned address is as follows:
*
* 1. Client sends REQUEST. The DUID is extracted and stored into a dhcpv6_cid_t
* structure. A new transaction structure (dhcpv6_txn_t) is also created and
* it will point to the dhcpv6_cid_t. If an existing transaction with a
* matching xid is not found, this dhcpv6_txn_t will be inserted into the
* mci_v6_pending_txn table (keyed by xid).
*
* 2. Server responds with a REPLY. If a pending transaction is found, the
* addresses in the reply will be placed into the dhcpv6_cid_t pointed to by
* the transaction. The dhcpv6_cid_t will then be moved to the mci_v6_cid
* table (keyed by cid). The associated addresses will be added to the
* mci_v6_dyn_ip table (while still being pointed to by the dhcpv6_cid_t).
*
* 3. IPv6 ip-nospoof will now check mci_v6_dyn_ip for matching packets.
* Packets with a source address matching one of the DHCPv6-assigned
* addresses will be allowed through.
*
* IPv6 notes:
*
* The v6 code shares the same timer as v4 for scrubbing stale transactions.
* Just like v4, as part of removing an expired transaction, a RELEASE will be
* be triggered on the cid associated with the expired transaction.
*
* The data structures used for v6 are slightly different because a v6 client
* may have multiple addresses associated with it.
*/
/*
* These are just arbitrary limits meant for preventing abuse (e.g. a user
* flooding the network with bogus transactions). They are not meant to be
* user-modifiable so they are not exposed as linkprops.
*/
static ulong_t dhcp_max_pending_txn = 512;
static ulong_t dhcp_max_completed_txn = 512;
static time_t txn_cleanup_interval = 60;
/*
* DHCPv4 transaction. It may be added to three different tables
* (keyed by different fields).
*/
typedef struct dhcpv4_txn {
uint32_t dt_xid;
time_t dt_timestamp;
uint8_t dt_cid[DHCP_MAX_OPT_SIZE];
uint8_t dt_cid_len;
ipaddr_t dt_ipaddr;
avl_node_t dt_node;
avl_node_t dt_ipnode;
struct dhcpv4_txn *dt_next;
} dhcpv4_txn_t;
/*
* DHCPv6 address. May be added to mci_v6_dyn_ip.
* It is always pointed to by its parent dhcpv6_cid_t structure.
*/
typedef struct dhcpv6_addr {
in6_addr_t da_addr;
avl_node_t da_node;
struct dhcpv6_addr *da_next;
} dhcpv6_addr_t;
/*
* DHCPv6 client ID. May be added to mci_v6_cid.
* No dhcpv6_txn_t should be pointing to it after it is added to mci_v6_cid.
*/
typedef struct dhcpv6_cid {
uchar_t *dc_cid;
uint_t dc_cid_len;
dhcpv6_addr_t *dc_addr;
uint_t dc_addrcnt;
avl_node_t dc_node;
} dhcpv6_cid_t;
/*
* DHCPv6 transaction. Unlike its v4 counterpart, this object gets freed up
* as soon as the transaction completes or expires.
*/
typedef struct dhcpv6_txn {
uint32_t dt_xid;
time_t dt_timestamp;
dhcpv6_cid_t *dt_cid;
avl_node_t dt_node;
struct dhcpv6_txn *dt_next;
} dhcpv6_txn_t;
static void start_txn_cleanup_timer(mac_client_impl_t *);
static boolean_t allowed_ips_set(mac_resource_props_t *, uint32_t);
#define BUMP_STAT(m, s) (m)->mci_misc_stat.mms_##s++
/*
* Comparison functions for the 3 AVL trees used:
* mci_v4_pending_txn, mci_v4_completed_txn, mci_v4_dyn_ip
*/
static int
compare_dhcpv4_xid(const void *arg1, const void *arg2)
{
const dhcpv4_txn_t *txn1 = arg1, *txn2 = arg2;
if (txn1->dt_xid < txn2->dt_xid)
return (-1);
else if (txn1->dt_xid > txn2->dt_xid)
return (1);
else
return (0);
}
static int
compare_dhcpv4_cid(const void *arg1, const void *arg2)
{
const dhcpv4_txn_t *txn1 = arg1, *txn2 = arg2;
int ret;
if (txn1->dt_cid_len < txn2->dt_cid_len)
return (-1);
else if (txn1->dt_cid_len > txn2->dt_cid_len)
return (1);
if (txn1->dt_cid_len == 0)
return (0);
ret = memcmp(txn1->dt_cid, txn2->dt_cid, txn1->dt_cid_len);
if (ret < 0)
return (-1);
else if (ret > 0)
return (1);
else
return (0);
}
static int
compare_dhcpv4_ip(const void *arg1, const void *arg2)
{
const dhcpv4_txn_t *txn1 = arg1, *txn2 = arg2;
if (txn1->dt_ipaddr < txn2->dt_ipaddr)
return (-1);
else if (txn1->dt_ipaddr > txn2->dt_ipaddr)
return (1);
else
return (0);
}
/*
* Find the specified DHCPv4 option.
*/
static int
get_dhcpv4_option(struct dhcp *dh4, uchar_t *end, uint8_t type,
uchar_t **opt, uint8_t *opt_len)
{
uchar_t *start = (uchar_t *)dh4->options;
uint8_t otype, olen;
while (start < end) {
if (*start == CD_PAD) {
start++;
continue;
}
if (*start == CD_END)
break;
otype = *start++;
olen = *start++;
if (otype == type && olen > 0) {
*opt = start;
*opt_len = olen;
return (0);
}
start += olen;
}
return (ENOENT);
}
/*
* Locate the start of a DHCPv4 header.
* The possible return values and associated meanings are:
* 0 - packet is DHCP and has a DHCP header.
* EINVAL - packet is not DHCP. the recommended action is to let it pass.
* ENOSPC - packet is a initial fragment that is DHCP or is unidentifiable.
* the recommended action is to drop it.
*/
static int
get_dhcpv4_info(ipha_t *ipha, uchar_t *end, struct dhcp **dh4)
{
uint16_t offset_and_flags, client, server;
boolean_t first_frag = B_FALSE;
struct udphdr *udph;
uchar_t *dh;
if (ipha->ipha_protocol != IPPROTO_UDP)
return (EINVAL);
offset_and_flags = ntohs(ipha->ipha_fragment_offset_and_flags);
if ((offset_and_flags & (IPH_MF | IPH_OFFSET)) != 0) {
/*
* All non-initial fragments may pass because we cannot
* identify their type. It's safe to let them through
* because reassembly will fail if we decide to drop the
* initial fragment.
*/
if (((offset_and_flags << 3) & 0xffff) != 0)
return (EINVAL);
first_frag = B_TRUE;
}
/* drop packets without a udp header */
udph = (struct udphdr *)((uchar_t *)ipha + IPH_HDR_LENGTH(ipha));
if ((uchar_t *)&udph[1] > end)
return (ENOSPC);
client = htons(IPPORT_BOOTPC);
server = htons(IPPORT_BOOTPS);
if (udph->uh_sport != client && udph->uh_sport != server &&
udph->uh_dport != client && udph->uh_dport != server)
return (EINVAL);
/* drop dhcp fragments */
if (first_frag)
return (ENOSPC);
dh = (uchar_t *)&udph[1];
if (dh + BASE_PKT_SIZE > end)
return (EINVAL);
*dh4 = (struct dhcp *)dh;
return (0);
}
/*
* Wrappers for accesses to avl trees to improve readability.
* Their purposes are fairly self-explanatory.
*/
static dhcpv4_txn_t *
find_dhcpv4_pending_txn(mac_client_impl_t *mcip, uint32_t xid)
{
dhcpv4_txn_t tmp_txn;
ASSERT(MUTEX_HELD(&mcip->mci_protect_lock));
tmp_txn.dt_xid = xid;
return (avl_find(&mcip->mci_v4_pending_txn, &tmp_txn, NULL));
}
static int
insert_dhcpv4_pending_txn(mac_client_impl_t *mcip, dhcpv4_txn_t *txn)
{
avl_index_t where;
ASSERT(MUTEX_HELD(&mcip->mci_protect_lock));
if (avl_find(&mcip->mci_v4_pending_txn, txn, &where) != NULL)
return (EEXIST);
if (avl_numnodes(&mcip->mci_v4_pending_txn) >= dhcp_max_pending_txn) {
BUMP_STAT(mcip, dhcpdropped);
return (EAGAIN);
}
avl_insert(&mcip->mci_v4_pending_txn, txn, where);
return (0);
}
static void
remove_dhcpv4_pending_txn(mac_client_impl_t *mcip, dhcpv4_txn_t *txn)
{
ASSERT(MUTEX_HELD(&mcip->mci_protect_lock));
avl_remove(&mcip->mci_v4_pending_txn, txn);
}
static dhcpv4_txn_t *
find_dhcpv4_completed_txn(mac_client_impl_t *mcip, uint8_t *cid,
uint8_t cid_len)
{
dhcpv4_txn_t tmp_txn;
ASSERT(MUTEX_HELD(&mcip->mci_protect_lock));
if (cid_len > 0)
bcopy(cid, tmp_txn.dt_cid, cid_len);
tmp_txn.dt_cid_len = cid_len;
return (avl_find(&mcip->mci_v4_completed_txn, &tmp_txn, NULL));
}
/*
* After a pending txn is removed from the pending table, it is inserted
* into both the completed and dyn-ip tables. These two insertions are
* done together because a client ID must have 1:1 correspondence with
* an IP address and IP addresses must be unique in the dyn-ip table.
*/
static int
insert_dhcpv4_completed_txn(mac_client_impl_t *mcip, dhcpv4_txn_t *txn)
{
avl_index_t where;
ASSERT(MUTEX_HELD(&mcip->mci_protect_lock));
if (avl_find(&mcip->mci_v4_completed_txn, txn, &where) != NULL)
return (EEXIST);
if (avl_numnodes(&mcip->mci_v4_completed_txn) >=
dhcp_max_completed_txn) {
BUMP_STAT(mcip, dhcpdropped);
return (EAGAIN);
}
avl_insert(&mcip->mci_v4_completed_txn, txn, where);
if (avl_find(&mcip->mci_v4_dyn_ip, txn, &where) != NULL) {
avl_remove(&mcip->mci_v4_completed_txn, txn);
return (EEXIST);
}
avl_insert(&mcip->mci_v4_dyn_ip, txn, where);
return (0);
}
static void
remove_dhcpv4_completed_txn(mac_client_impl_t *mcip, dhcpv4_txn_t *txn)
{
dhcpv4_txn_t *ctxn;
ASSERT(MUTEX_HELD(&mcip->mci_protect_lock));
if ((ctxn = avl_find(&mcip->mci_v4_dyn_ip, txn, NULL)) != NULL &&
ctxn == txn)
avl_remove(&mcip->mci_v4_dyn_ip, txn);
avl_remove(&mcip->mci_v4_completed_txn, txn);
}
/*
* Check whether an IP address is in the dyn-ip table.
*/
static boolean_t
check_dhcpv4_dyn_ip(mac_client_impl_t *mcip, ipaddr_t ipaddr)
{
dhcpv4_txn_t tmp_txn, *txn;
mutex_enter(&mcip->mci_protect_lock);
tmp_txn.dt_ipaddr = ipaddr;
txn = avl_find(&mcip->mci_v4_dyn_ip, &tmp_txn, NULL);
mutex_exit(&mcip->mci_protect_lock);
return (txn != NULL);
}
/*
* Create/destroy a DHCPv4 transaction.
*/
static dhcpv4_txn_t *
create_dhcpv4_txn(uint32_t xid, uint8_t *cid, uint8_t cid_len, ipaddr_t ipaddr)
{
dhcpv4_txn_t *txn;
if ((txn = kmem_zalloc(sizeof (*txn), KM_NOSLEEP)) == NULL)
return (NULL);
txn->dt_xid = xid;
txn->dt_timestamp = ddi_get_time();
if (cid_len > 0)
bcopy(cid, &txn->dt_cid, cid_len);
txn->dt_cid_len = cid_len;
txn->dt_ipaddr = ipaddr;
return (txn);
}
static void
free_dhcpv4_txn(dhcpv4_txn_t *txn)
{
kmem_free(txn, sizeof (*txn));
}
/*
* Clean up all v4 tables.
*/
static void
flush_dhcpv4(mac_client_impl_t *mcip)
{
void *cookie = NULL;
dhcpv4_txn_t *txn;
ASSERT(MUTEX_HELD(&mcip->mci_protect_lock));
while ((txn = avl_destroy_nodes(&mcip->mci_v4_dyn_ip,
&cookie)) != NULL) {
/*
* No freeing needed here because the same txn exists
* in the mci_v4_completed_txn table as well.
*/
}
cookie = NULL;
while ((txn = avl_destroy_nodes(&mcip->mci_v4_completed_txn,
&cookie)) != NULL) {
free_dhcpv4_txn(txn);
}
cookie = NULL;
while ((txn = avl_destroy_nodes(&mcip->mci_v4_pending_txn,
&cookie)) != NULL) {
free_dhcpv4_txn(txn);
}
}
/*
* Cleanup stale DHCPv4 transactions.
*/
static void
txn_cleanup_v4(mac_client_impl_t *mcip)
{
dhcpv4_txn_t *txn, *ctxn, *next, *txn_list = NULL;
/*
* Find stale pending transactions and place them on a list
* to be removed.
*/
for (txn = avl_first(&mcip->mci_v4_pending_txn); txn != NULL;
txn = avl_walk(&mcip->mci_v4_pending_txn, txn, AVL_AFTER)) {
if (ddi_get_time() - txn->dt_timestamp >
txn_cleanup_interval) {
DTRACE_PROBE2(found__expired__txn,
mac_client_impl_t *, mcip,
dhcpv4_txn_t *, txn);
txn->dt_next = txn_list;
txn_list = txn;
}
}
/*
* Remove and free stale pending transactions and completed
* transactions with the same client IDs as the stale transactions.
*/
for (txn = txn_list; txn != NULL; txn = next) {
avl_remove(&mcip->mci_v4_pending_txn, txn);
ctxn = find_dhcpv4_completed_txn(mcip, txn->dt_cid,
txn->dt_cid_len);
if (ctxn != NULL) {
DTRACE_PROBE2(removing__completed__txn,
mac_client_impl_t *, mcip,
dhcpv4_txn_t *, ctxn);
remove_dhcpv4_completed_txn(mcip, ctxn);
free_dhcpv4_txn(ctxn);
}
next = txn->dt_next;
txn->dt_next = NULL;
DTRACE_PROBE2(freeing__txn, mac_client_impl_t *, mcip,
dhcpv4_txn_t *, txn);
free_dhcpv4_txn(txn);
}
}
/*
* Core logic for intercepting outbound DHCPv4 packets.
*/
static boolean_t
intercept_dhcpv4_outbound(mac_client_impl_t *mcip, ipha_t *ipha, uchar_t *end)
{
struct dhcp *dh4;
uchar_t *opt;
dhcpv4_txn_t *txn, *ctxn;
ipaddr_t ipaddr;
uint8_t opt_len, mtype, cid[DHCP_MAX_OPT_SIZE], cid_len;
mac_resource_props_t *mrp = MCIP_RESOURCE_PROPS(mcip);
if (get_dhcpv4_info(ipha, end, &dh4) != 0)
return (B_TRUE);
/* ip_nospoof/allowed-ips and DHCP are mutually exclusive by default */
if (allowed_ips_set(mrp, IPV4_VERSION))
return (B_FALSE);
if (get_dhcpv4_option(dh4, end, CD_DHCP_TYPE, &opt, &opt_len) != 0 ||
opt_len != 1) {
DTRACE_PROBE2(mtype__not__found, mac_client_impl_t *, mcip,
struct dhcp *, dh4);
return (B_TRUE);
}
mtype = *opt;
if (mtype != REQUEST && mtype != RELEASE) {
DTRACE_PROBE3(ignored__mtype, mac_client_impl_t *, mcip,
struct dhcp *, dh4, uint8_t, mtype);
return (B_TRUE);
}
/* client ID is optional for IPv4 */
if (get_dhcpv4_option(dh4, end, CD_CLIENT_ID, &opt, &opt_len) == 0 &&
opt_len >= 2) {
bcopy(opt, cid, opt_len);
cid_len = opt_len;
} else {
bzero(cid, DHCP_MAX_OPT_SIZE);
cid_len = 0;
}
mutex_enter(&mcip->mci_protect_lock);
if (mtype == RELEASE) {
DTRACE_PROBE2(release, mac_client_impl_t *, mcip,
struct dhcp *, dh4);
/* flush any completed txn with this cid */
ctxn = find_dhcpv4_completed_txn(mcip, cid, cid_len);
if (ctxn != NULL) {
DTRACE_PROBE2(release__successful, mac_client_impl_t *,
mcip, struct dhcp *, dh4);
remove_dhcpv4_completed_txn(mcip, ctxn);
free_dhcpv4_txn(ctxn);
}
goto done;
}
/*
* If a pending txn already exists, we'll update its timestamp so
* it won't get flushed by the timer. We don't need to create new
* txns for retransmissions.
*/
if ((txn = find_dhcpv4_pending_txn(mcip, dh4->xid)) != NULL) {
DTRACE_PROBE2(update, mac_client_impl_t *, mcip,
dhcpv4_txn_t *, txn);
txn->dt_timestamp = ddi_get_time();
goto done;
}
if (get_dhcpv4_option(dh4, end, CD_REQUESTED_IP_ADDR,
&opt, &opt_len) != 0 || opt_len != sizeof (ipaddr)) {
DTRACE_PROBE2(ipaddr__not__found, mac_client_impl_t *, mcip,
struct dhcp *, dh4);
goto done;
}
bcopy(opt, &ipaddr, sizeof (ipaddr));
if ((txn = create_dhcpv4_txn(dh4->xid, cid, cid_len, ipaddr)) == NULL)
goto done;
if (insert_dhcpv4_pending_txn(mcip, txn) != 0) {
DTRACE_PROBE2(insert__failed, mac_client_impl_t *, mcip,
dhcpv4_txn_t *, txn);
free_dhcpv4_txn(txn);
goto done;
}
start_txn_cleanup_timer(mcip);
DTRACE_PROBE2(txn__pending, mac_client_impl_t *, mcip,
dhcpv4_txn_t *, txn);
done:
mutex_exit(&mcip->mci_protect_lock);
return (B_TRUE);
}
/*
* Core logic for intercepting inbound DHCPv4 packets.
*/
static void
intercept_dhcpv4_inbound(mac_client_impl_t *mcip, ipha_t *ipha, uchar_t *end)
{
uchar_t *opt;
struct dhcp *dh4;
dhcpv4_txn_t *txn, *ctxn;
uint8_t opt_len, mtype;
if (get_dhcpv4_info(ipha, end, &dh4) != 0)
return;
if (get_dhcpv4_option(dh4, end, CD_DHCP_TYPE, &opt, &opt_len) != 0 ||
opt_len != 1) {
DTRACE_PROBE2(mtype__not__found, mac_client_impl_t *, mcip,
struct dhcp *, dh4);
return;
}
mtype = *opt;
if (mtype != ACK && mtype != NAK) {
DTRACE_PROBE3(ignored__mtype, mac_client_impl_t *, mcip,
struct dhcp *, dh4, uint8_t, mtype);
return;
}
mutex_enter(&mcip->mci_protect_lock);
if ((txn = find_dhcpv4_pending_txn(mcip, dh4->xid)) == NULL) {
DTRACE_PROBE2(txn__not__found, mac_client_impl_t *, mcip,
struct dhcp *, dh4);
goto done;
}
remove_dhcpv4_pending_txn(mcip, txn);
/*
* We're about to move a txn from the pending table to the completed/
* dyn-ip tables. If there is an existing completed txn with the
* same cid as our txn, we need to remove and free it.
*/
ctxn = find_dhcpv4_completed_txn(mcip, txn->dt_cid, txn->dt_cid_len);
if (ctxn != NULL) {
DTRACE_PROBE2(replacing__old__txn, mac_client_impl_t *, mcip,
dhcpv4_txn_t *, ctxn);
remove_dhcpv4_completed_txn(mcip, ctxn);
free_dhcpv4_txn(ctxn);
}
if (mtype == NAK) {
DTRACE_PROBE2(nak__received, mac_client_impl_t *, mcip,
dhcpv4_txn_t *, txn);
free_dhcpv4_txn(txn);
goto done;
}
if (insert_dhcpv4_completed_txn(mcip, txn) != 0) {
DTRACE_PROBE2(insert__failed, mac_client_impl_t *, mcip,
dhcpv4_txn_t *, txn);
free_dhcpv4_txn(txn);
goto done;
}
DTRACE_PROBE2(txn__completed, mac_client_impl_t *, mcip,
dhcpv4_txn_t *, txn);
done:
mutex_exit(&mcip->mci_protect_lock);
}
/*
* Comparison functions for the DHCPv6 AVL trees.
*/
static int
compare_dhcpv6_xid(const void *arg1, const void *arg2)
{
const dhcpv6_txn_t *txn1 = arg1, *txn2 = arg2;
if (txn1->dt_xid < txn2->dt_xid)
return (-1);
else if (txn1->dt_xid > txn2->dt_xid)
return (1);
else
return (0);
}
static int
compare_dhcpv6_ip(const void *arg1, const void *arg2)
{
const dhcpv6_addr_t *ip1 = arg1, *ip2 = arg2;
int ret;
ret = memcmp(&ip1->da_addr, &ip2->da_addr, sizeof (in6_addr_t));
if (ret < 0)
return (-1);
else if (ret > 0)
return (1);
else
return (0);
}
static int
compare_dhcpv6_cid(const void *arg1, const void *arg2)
{
const dhcpv6_cid_t *cid1 = arg1, *cid2 = arg2;
int ret;
if (cid1->dc_cid_len < cid2->dc_cid_len)
return (-1);
else if (cid1->dc_cid_len > cid2->dc_cid_len)
return (1);
if (cid1->dc_cid_len == 0)
return (0);
ret = memcmp(cid1->dc_cid, cid2->dc_cid, cid1->dc_cid_len);
if (ret < 0)
return (-1);
else if (ret > 0)
return (1);
else
return (0);
}
/*
* Locate the start of a DHCPv6 header.
* The possible return values and associated meanings are:
* 0 - packet is DHCP and has a DHCP header.
* EINVAL - packet is not DHCP. the recommended action is to let it pass.
* ENOSPC - packet is a initial fragment that is DHCP or is unidentifiable.
* the recommended action is to drop it.
*/
static int
get_dhcpv6_info(ip6_t *ip6h, uchar_t *end, dhcpv6_message_t **dh6)
{
uint16_t hdrlen, client, server;
boolean_t first_frag = B_FALSE;
ip6_frag_t *frag = NULL;
uint8_t proto;
struct udphdr *udph;
uchar_t *dh;
if (!mac_ip_hdr_length_v6(ip6h, end, &hdrlen, &proto, &frag))
return (ENOSPC);
if (proto != IPPROTO_UDP)
return (EINVAL);
if (frag != NULL) {
/*
* All non-initial fragments may pass because we cannot
* identify their type. It's safe to let them through
* because reassembly will fail if we decide to drop the
* initial fragment.
*/
if ((ntohs(frag->ip6f_offlg) & ~7) != 0)
return (EINVAL);
first_frag = B_TRUE;
}
/* drop packets without a udp header */
udph = (struct udphdr *)((uchar_t *)ip6h + hdrlen);
if ((uchar_t *)&udph[1] > end)
return (ENOSPC);
client = htons(IPPORT_DHCPV6C);
server = htons(IPPORT_DHCPV6S);
if (udph->uh_sport != client && udph->uh_sport != server &&
udph->uh_dport != client && udph->uh_dport != server)
return (EINVAL);
/* drop dhcp fragments */
if (first_frag)
return (ENOSPC);
dh = (uchar_t *)&udph[1];
if (dh + sizeof (dhcpv6_message_t) > end)
return (EINVAL);
*dh6 = (dhcpv6_message_t *)dh;
return (0);
}
/*
* Find the specified DHCPv6 option.
*/
static dhcpv6_option_t *
get_dhcpv6_option(void *buf, size_t buflen, dhcpv6_option_t *oldopt,
uint16_t codenum, uint_t *retlenp)
{
uchar_t *bp;
dhcpv6_option_t d6o;
uint_t olen;
codenum = htons(codenum);
bp = buf;
while (buflen >= sizeof (dhcpv6_option_t)) {
bcopy(bp, &d6o, sizeof (d6o));
olen = ntohs(d6o.d6o_len) + sizeof (d6o);
if (olen > buflen)
break;
if (d6o.d6o_code != codenum || d6o.d6o_len == 0 ||
(oldopt != NULL && bp <= (uchar_t *)oldopt)) {
bp += olen;
buflen -= olen;
continue;
}
if (retlenp != NULL)
*retlenp = olen;
/* LINTED : alignment */
return ((dhcpv6_option_t *)bp);
}
return (NULL);
}
/*
* Get the status code from a reply message.
*/
static int
get_dhcpv6_status(dhcpv6_message_t *dh6, uchar_t *end, uint16_t *status)
{
dhcpv6_option_t *d6o;
uint_t olen;
uint16_t s;
d6o = get_dhcpv6_option(&dh6[1], end - (uchar_t *)&dh6[1], NULL,
DHCPV6_OPT_STATUS_CODE, &olen);
/* Success is implied if status code is missing */
if (d6o == NULL) {
*status = DHCPV6_STAT_SUCCESS;
return (0);
}
if ((uchar_t *)d6o + olen > end)
return (EINVAL);
olen -= sizeof (*d6o);
if (olen < sizeof (s))
return (EINVAL);
bcopy(&d6o[1], &s, sizeof (s));
*status = ntohs(s);
return (0);
}
/*
* Get the addresses from a reply message.
*/
static int
get_dhcpv6_addrs(dhcpv6_message_t *dh6, uchar_t *end, dhcpv6_cid_t *cid)
{
dhcpv6_option_t *d6o;
dhcpv6_addr_t *next;
uint_t olen;
d6o = NULL;
while ((d6o = get_dhcpv6_option(&dh6[1], end - (uchar_t *)&dh6[1],
d6o, DHCPV6_OPT_IA_NA, &olen)) != NULL) {
dhcpv6_option_t *d6so;
dhcpv6_iaaddr_t d6ia;
dhcpv6_addr_t **addrp;
uchar_t *obase;
uint_t solen;
if (olen < sizeof (dhcpv6_ia_na_t) ||
(uchar_t *)d6o + olen > end)
goto fail;
obase = (uchar_t *)d6o + sizeof (dhcpv6_ia_na_t);
olen -= sizeof (dhcpv6_ia_na_t);
d6so = NULL;
while ((d6so = get_dhcpv6_option(obase, olen, d6so,
DHCPV6_OPT_IAADDR, &solen)) != NULL) {
if (solen < sizeof (dhcpv6_iaaddr_t) ||
(uchar_t *)d6so + solen > end)
goto fail;
bcopy(d6so, &d6ia, sizeof (d6ia));
for (addrp = &cid->dc_addr; *addrp != NULL;
addrp = &(*addrp)->da_next) {
if (bcmp(&(*addrp)->da_addr, &d6ia.d6ia_addr,
sizeof (in6_addr_t)) == 0)
goto fail;
}
if ((*addrp = kmem_zalloc(sizeof (dhcpv6_addr_t),
KM_NOSLEEP)) == NULL)
goto fail;
bcopy(&d6ia.d6ia_addr, &(*addrp)->da_addr,
sizeof (in6_addr_t));
cid->dc_addrcnt++;
}
}
if (cid->dc_addrcnt == 0)
return (ENOENT);
return (0);
fail:
for (; cid->dc_addr != NULL; cid->dc_addr = next) {
next = cid->dc_addr->da_next;
kmem_free(cid->dc_addr, sizeof (dhcpv6_addr_t));
cid->dc_addrcnt--;
}
ASSERT(cid->dc_addrcnt == 0);
return (EINVAL);
}
/*
* Free a cid.
* Before this gets called the caller must ensure that all the
* addresses are removed from the mci_v6_dyn_ip table.
*/
static void
free_dhcpv6_cid(dhcpv6_cid_t *cid)
{
dhcpv6_addr_t *addr, *next;
uint_t cnt = 0;
kmem_free(cid->dc_cid, cid->dc_cid_len);
for (addr = cid->dc_addr; addr != NULL; addr = next) {
next = addr->da_next;
kmem_free(addr, sizeof (*addr));
cnt++;
}
ASSERT(cnt == cid->dc_addrcnt);
kmem_free(cid, sizeof (*cid));
}
/*
* Extract the DUID from a message. The associated addresses will be
* extracted later from the reply message.
*/
static dhcpv6_cid_t *
create_dhcpv6_cid(dhcpv6_message_t *dh6, uchar_t *end)
{
dhcpv6_option_t *d6o;
dhcpv6_cid_t *cid;
uchar_t *rawcid;
uint_t olen, rawcidlen;
d6o = get_dhcpv6_option(&dh6[1], end - (uchar_t *)&dh6[1], NULL,
DHCPV6_OPT_CLIENTID, &olen);
if (d6o == NULL || (uchar_t *)d6o + olen > end)
return (NULL);
rawcidlen = olen - sizeof (*d6o);
if ((rawcid = kmem_zalloc(rawcidlen, KM_NOSLEEP)) == NULL)
return (NULL);
bcopy(d6o + 1, rawcid, rawcidlen);
if ((cid = kmem_zalloc(sizeof (*cid), KM_NOSLEEP)) == NULL) {
kmem_free(rawcid, rawcidlen);
return (NULL);
}
cid->dc_cid = rawcid;
cid->dc_cid_len = rawcidlen;
return (cid);
}
/*
* Remove a cid from mci_v6_cid. The addresses owned by the cid
* are also removed from mci_v6_dyn_ip.
*/
static void
remove_dhcpv6_cid(mac_client_impl_t *mcip, dhcpv6_cid_t *cid)
{
dhcpv6_addr_t *addr, *tmp_addr;
ASSERT(MUTEX_HELD(&mcip->mci_protect_lock));
avl_remove(&mcip->mci_v6_cid, cid);
for (addr = cid->dc_addr; addr != NULL; addr = addr->da_next) {
tmp_addr = avl_find(&mcip->mci_v6_dyn_ip, addr, NULL);
if (tmp_addr == addr)
avl_remove(&mcip->mci_v6_dyn_ip, addr);
}
}
/*
* Find and remove a matching cid and associated addresses from
* their respective tables.
*/
static void
release_dhcpv6_cid(mac_client_impl_t *mcip, dhcpv6_cid_t *cid)
{
dhcpv6_cid_t *oldcid;
ASSERT(MUTEX_HELD(&mcip->mci_protect_lock));
if ((oldcid = avl_find(&mcip->mci_v6_cid, cid, NULL)) == NULL)
return;
/*
* Since cid belongs to a pending txn, it can't possibly be in
* mci_v6_cid. Anything that's found must be an existing cid.
*/
ASSERT(oldcid != cid);
remove_dhcpv6_cid(mcip, oldcid);
free_dhcpv6_cid(oldcid);
}
/*
* Insert cid into mci_v6_cid.
*/
static int
insert_dhcpv6_cid(mac_client_impl_t *mcip, dhcpv6_cid_t *cid)
{
avl_index_t where;
dhcpv6_addr_t *addr;
ASSERT(MUTEX_HELD(&mcip->mci_protect_lock));
if (avl_find(&mcip->mci_v6_cid, cid, &where) != NULL)
return (EEXIST);
if (avl_numnodes(&mcip->mci_v6_cid) >= dhcp_max_completed_txn) {
BUMP_STAT(mcip, dhcpdropped);
return (EAGAIN);
}
avl_insert(&mcip->mci_v6_cid, cid, where);
for (addr = cid->dc_addr; addr != NULL; addr = addr->da_next) {
if (avl_find(&mcip->mci_v6_dyn_ip, addr, &where) != NULL)
goto fail;
avl_insert(&mcip->mci_v6_dyn_ip, addr, where);
}
return (0);
fail:
remove_dhcpv6_cid(mcip, cid);
return (EEXIST);
}
/*
* Check whether an IP address is in the dyn-ip table.
*/
static boolean_t
check_dhcpv6_dyn_ip(mac_client_impl_t *mcip, in6_addr_t *addr)
{
dhcpv6_addr_t tmp_addr, *a;
mutex_enter(&mcip->mci_protect_lock);
bcopy(addr, &tmp_addr.da_addr, sizeof (in6_addr_t));
a = avl_find(&mcip->mci_v6_dyn_ip, &tmp_addr, NULL);
mutex_exit(&mcip->mci_protect_lock);
return (a != NULL);
}
static dhcpv6_txn_t *
find_dhcpv6_pending_txn(mac_client_impl_t *mcip, uint32_t xid)
{
dhcpv6_txn_t tmp_txn;
ASSERT(MUTEX_HELD(&mcip->mci_protect_lock));
tmp_txn.dt_xid = xid;
return (avl_find(&mcip->mci_v6_pending_txn, &tmp_txn, NULL));
}
static void
remove_dhcpv6_pending_txn(mac_client_impl_t *mcip, dhcpv6_txn_t *txn)
{
ASSERT(MUTEX_HELD(&mcip->mci_protect_lock));
avl_remove(&mcip->mci_v6_pending_txn, txn);
}
static dhcpv6_txn_t *
create_dhcpv6_txn(uint32_t xid, dhcpv6_cid_t *cid)
{
dhcpv6_txn_t *txn;
if ((txn = kmem_zalloc(sizeof (dhcpv6_txn_t), KM_NOSLEEP)) == NULL)
return (NULL);
txn->dt_xid = xid;
txn->dt_cid = cid;
txn->dt_timestamp = ddi_get_time();
return (txn);
}
static void
free_dhcpv6_txn(dhcpv6_txn_t *txn)
{
if (txn->dt_cid != NULL)
free_dhcpv6_cid(txn->dt_cid);
kmem_free(txn, sizeof (dhcpv6_txn_t));
}
static int
insert_dhcpv6_pending_txn(mac_client_impl_t *mcip, dhcpv6_txn_t *txn)
{
avl_index_t where;
ASSERT(MUTEX_HELD(&mcip->mci_protect_lock));
if (avl_find(&mcip->mci_v6_pending_txn, txn, &where) != NULL)
return (EEXIST);
if (avl_numnodes(&mcip->mci_v6_pending_txn) >= dhcp_max_pending_txn) {
BUMP_STAT(mcip, dhcpdropped);
return (EAGAIN);
}
avl_insert(&mcip->mci_v6_pending_txn, txn, where);
return (0);
}
/*
* Clean up all v6 tables.
*/
static void
flush_dhcpv6(mac_client_impl_t *mcip)
{
void *cookie = NULL;
dhcpv6_cid_t *cid;
dhcpv6_txn_t *txn;
ASSERT(MUTEX_HELD(&mcip->mci_protect_lock));
while (avl_destroy_nodes(&mcip->mci_v6_dyn_ip, &cookie) != NULL) {
}
cookie = NULL;
while ((cid = avl_destroy_nodes(&mcip->mci_v6_cid, &cookie)) != NULL) {
free_dhcpv6_cid(cid);
}
cookie = NULL;
while ((txn = avl_destroy_nodes(&mcip->mci_v6_pending_txn,
&cookie)) != NULL) {
free_dhcpv6_txn(txn);
}
}
/*
* Cleanup stale DHCPv6 transactions.
*/
static void
txn_cleanup_v6(mac_client_impl_t *mcip)
{
dhcpv6_txn_t *txn, *next, *txn_list = NULL;
/*
* Find stale pending transactions and place them on a list
* to be removed.
*/
for (txn = avl_first(&mcip->mci_v6_pending_txn); txn != NULL;
txn = avl_walk(&mcip->mci_v6_pending_txn, txn, AVL_AFTER)) {
if (ddi_get_time() - txn->dt_timestamp >
txn_cleanup_interval) {
DTRACE_PROBE2(found__expired__txn,
mac_client_impl_t *, mcip,
dhcpv6_txn_t *, txn);
txn->dt_next = txn_list;
txn_list = txn;
}
}
/*
* Remove and free stale pending transactions.
* Release any existing cids matching the stale transactions.
*/
for (txn = txn_list; txn != NULL; txn = next) {
avl_remove(&mcip->mci_v6_pending_txn, txn);
release_dhcpv6_cid(mcip, txn->dt_cid);
next = txn->dt_next;
txn->dt_next = NULL;
DTRACE_PROBE2(freeing__txn, mac_client_impl_t *, mcip,
dhcpv6_txn_t *, txn);
free_dhcpv6_txn(txn);
}
}
/*
* Core logic for intercepting outbound DHCPv6 packets.
*/
static boolean_t
intercept_dhcpv6_outbound(mac_client_impl_t *mcip, ip6_t *ip6h, uchar_t *end)
{
dhcpv6_message_t *dh6;
dhcpv6_txn_t *txn;
dhcpv6_cid_t *cid = NULL;
uint32_t xid;
uint8_t mtype;
mac_resource_props_t *mrp = MCIP_RESOURCE_PROPS(mcip);
if (get_dhcpv6_info(ip6h, end, &dh6) != 0)
return (B_TRUE);
/* ip_nospoof/allowed-ips and DHCP are mutually exclusive by default */
if (allowed_ips_set(mrp, IPV6_VERSION))
return (B_FALSE);
mtype = dh6->d6m_msg_type;
if (mtype != DHCPV6_MSG_REQUEST && mtype != DHCPV6_MSG_RENEW &&
mtype != DHCPV6_MSG_REBIND && mtype != DHCPV6_MSG_RELEASE)
return (B_TRUE);
if ((cid = create_dhcpv6_cid(dh6, end)) == NULL)
return (B_TRUE);
mutex_enter(&mcip->mci_protect_lock);
if (mtype == DHCPV6_MSG_RELEASE) {
release_dhcpv6_cid(mcip, cid);
goto done;
}
xid = DHCPV6_GET_TRANSID(dh6);
if ((txn = find_dhcpv6_pending_txn(mcip, xid)) != NULL) {
DTRACE_PROBE2(update, mac_client_impl_t *, mcip,
dhcpv6_txn_t *, txn);
txn->dt_timestamp = ddi_get_time();
goto done;
}
if ((txn = create_dhcpv6_txn(xid, cid)) == NULL)
goto done;
cid = NULL;
if (insert_dhcpv6_pending_txn(mcip, txn) != 0) {
DTRACE_PROBE2(insert__failed, mac_client_impl_t *, mcip,
dhcpv6_txn_t *, txn);
free_dhcpv6_txn(txn);
goto done;
}
start_txn_cleanup_timer(mcip);
DTRACE_PROBE2(txn__pending, mac_client_impl_t *, mcip,
dhcpv6_txn_t *, txn);
done:
if (cid != NULL)
free_dhcpv6_cid(cid);
mutex_exit(&mcip->mci_protect_lock);
return (B_TRUE);
}
/*
* Core logic for intercepting inbound DHCPv6 packets.
*/
static void
intercept_dhcpv6_inbound(mac_client_impl_t *mcip, ip6_t *ip6h, uchar_t *end)
{
dhcpv6_message_t *dh6;
dhcpv6_txn_t *txn;
uint32_t xid;
uint8_t mtype;
uint16_t status;
if (get_dhcpv6_info(ip6h, end, &dh6) != 0)
return;
mtype = dh6->d6m_msg_type;
if (mtype != DHCPV6_MSG_REPLY)
return;
mutex_enter(&mcip->mci_protect_lock);
xid = DHCPV6_GET_TRANSID(dh6);
if ((txn = find_dhcpv6_pending_txn(mcip, xid)) == NULL) {
DTRACE_PROBE2(txn__not__found, mac_client_impl_t *, mcip,
dhcpv6_message_t *, dh6);
goto done;
}
remove_dhcpv6_pending_txn(mcip, txn);
release_dhcpv6_cid(mcip, txn->dt_cid);
if (get_dhcpv6_status(dh6, end, &status) != 0 ||
status != DHCPV6_STAT_SUCCESS) {
DTRACE_PROBE2(error__status, mac_client_impl_t *, mcip,
dhcpv6_txn_t *, txn);
goto done;
}
if (get_dhcpv6_addrs(dh6, end, txn->dt_cid) != 0) {
DTRACE_PROBE2(no__addrs, mac_client_impl_t *, mcip,
dhcpv6_txn_t *, txn);
goto done;
}
if (insert_dhcpv6_cid(mcip, txn->dt_cid) != 0) {
DTRACE_PROBE2(insert__failed, mac_client_impl_t *, mcip,
dhcpv6_txn_t *, txn);
goto done;
}
DTRACE_PROBE2(txn__completed, mac_client_impl_t *, mcip,
dhcpv6_txn_t *, txn);
txn->dt_cid = NULL;
done:
if (txn != NULL)
free_dhcpv6_txn(txn);
mutex_exit(&mcip->mci_protect_lock);
}
/*
* Timer for cleaning up stale transactions.
*/
static void
txn_cleanup_timer(void *arg)
{
mac_client_impl_t *mcip = arg;
mutex_enter(&mcip->mci_protect_lock);
if (mcip->mci_txn_cleanup_tid == 0) {
/* do nothing if timer got cancelled */
mutex_exit(&mcip->mci_protect_lock);
return;
}
mcip->mci_txn_cleanup_tid = 0;
txn_cleanup_v4(mcip);
txn_cleanup_v6(mcip);
/*
* Restart timer if pending transactions still exist.
*/
if (!avl_is_empty(&mcip->mci_v4_pending_txn) ||
!avl_is_empty(&mcip->mci_v6_pending_txn)) {
DTRACE_PROBE1(restarting__timer, mac_client_impl_t *, mcip);
mcip->mci_txn_cleanup_tid = timeout(txn_cleanup_timer, mcip,
drv_usectohz(txn_cleanup_interval * 1000000));
}
mutex_exit(&mcip->mci_protect_lock);
}
static void
start_txn_cleanup_timer(mac_client_impl_t *mcip)
{
ASSERT(MUTEX_HELD(&mcip->mci_protect_lock));
if (mcip->mci_txn_cleanup_tid == 0) {
mcip->mci_txn_cleanup_tid = timeout(txn_cleanup_timer, mcip,
drv_usectohz(txn_cleanup_interval * 1000000));
}
}
static void
cancel_txn_cleanup_timer(mac_client_impl_t *mcip)
{
timeout_id_t tid;
ASSERT(MUTEX_HELD(&mcip->mci_protect_lock));
/*
* This needs to be a while loop because the timer could get
* rearmed during untimeout().
*/
while ((tid = mcip->mci_txn_cleanup_tid) != 0) {
mcip->mci_txn_cleanup_tid = 0;
mutex_exit(&mcip->mci_protect_lock);
(void) untimeout(tid);
mutex_enter(&mcip->mci_protect_lock);
}
}
/*
* Get the start/end pointers of an L3 packet and also do pullup if needed.
* pulled-up packet needs to be freed by the caller.
*/
static int
get_l3_info(mblk_t *mp, size_t hdrsize, uchar_t **start, uchar_t **end,
mblk_t **nmp)
{
uchar_t *s, *e;
mblk_t *newmp = NULL;
/*
* Pullup if necessary but reject packets that do not have
* a proper mac header.
*/
s = mp->b_rptr + hdrsize;
e = mp->b_wptr;
if (s > mp->b_wptr)
return (EINVAL);
if (!OK_32PTR(s) || mp->b_cont != NULL) {
/*
* Temporarily adjust mp->b_rptr to ensure proper
* alignment of IP header in newmp.
*/
DTRACE_PROBE1(pullup__needed, mblk_t *, mp);
mp->b_rptr += hdrsize;
newmp = msgpullup(mp, -1);
mp->b_rptr -= hdrsize;
if (newmp == NULL)
return (ENOMEM);
s = newmp->b_rptr;
e = newmp->b_wptr;
}
*start = s;
*end = e;
*nmp = newmp;
return (0);
}
void
mac_protect_intercept_dhcp_one(mac_client_impl_t *mcip, mblk_t *mp)
{
mac_impl_t *mip = mcip->mci_mip;
uchar_t *start, *end;
mblk_t *nmp = NULL;
mac_header_info_t mhi;
int err;
err = mac_vlan_header_info((mac_handle_t)mip, mp, &mhi);
if (err != 0) {
DTRACE_PROBE2(invalid__header, mac_client_impl_t *, mcip,
mblk_t *, mp);
return;
}
err = get_l3_info(mp, mhi.mhi_hdrsize, &start, &end, &nmp);
if (err != 0) {
DTRACE_PROBE2(invalid__l3, mac_client_impl_t *, mcip,
mblk_t *, mp);
return;
}
switch (mhi.mhi_bindsap) {
case ETHERTYPE_IP: {
ipha_t *ipha = (ipha_t *)start;
if (start + sizeof (ipha_t) > end)
return;
intercept_dhcpv4_inbound(mcip, ipha, end);
break;
}
case ETHERTYPE_IPV6: {
ip6_t *ip6h = (ip6_t *)start;
if (start + sizeof (ip6_t) > end)
return;
intercept_dhcpv6_inbound(mcip, ip6h, end);
break;
}
}
freemsg(nmp);
}
void
mac_protect_intercept_dhcp(mac_client_impl_t *mcip, mblk_t *mp)
{
/*
* Skip checks if we are part of an aggr.
*/
if ((mcip->mci_state_flags & MCIS_IS_AGGR_PORT) != 0)
return;
for (; mp != NULL; mp = mp->b_next)
mac_protect_intercept_dhcp_one(mcip, mp);
}
void
mac_protect_flush_dhcp(mac_client_impl_t *mcip)
{
mutex_enter(&mcip->mci_protect_lock);
flush_dhcpv4(mcip);
flush_dhcpv6(mcip);
mutex_exit(&mcip->mci_protect_lock);
}
void
mac_protect_cancel_timer(mac_client_impl_t *mcip)
{
mutex_enter(&mcip->mci_protect_lock);
cancel_txn_cleanup_timer(mcip);
mutex_exit(&mcip->mci_protect_lock);
}
/*
* Check if addr is in the 'allowed-ips' list.
*/
/* ARGSUSED */
static boolean_t
ipnospoof_check_v4(mac_client_impl_t *mcip, mac_protect_t *protect,
ipaddr_t *addr)
{
uint_t i;
/*
* The unspecified address is allowed.
*/
if (*addr == INADDR_ANY)
return (B_TRUE);
for (i = 0; i < protect->mp_ipaddrcnt; i++) {
mac_ipaddr_t *v4addr = &protect->mp_ipaddrs[i];
if (v4addr->ip_version == IPV4_VERSION &&
V4_PART_OF_V6(v4addr->ip_addr) == *addr)
return (B_TRUE);
}
return (protect->mp_ipaddrcnt == 0 ?
check_dhcpv4_dyn_ip(mcip, *addr) : B_FALSE);
}
static boolean_t
ipnospoof_check_v6(mac_client_impl_t *mcip, mac_protect_t *protect,
in6_addr_t *addr)
{
uint_t i;
/*
* The unspecified address and the v6 link local address are allowed.
*/
if (IN6_IS_ADDR_UNSPECIFIED(addr) ||
((mcip->mci_protect_flags & MPT_FLAG_V6_LOCAL_ADDR_SET) != 0 &&
IN6_ARE_ADDR_EQUAL(&mcip->mci_v6_local_addr, addr)))
return (B_TRUE);
for (i = 0; i < protect->mp_ipaddrcnt; i++) {
mac_ipaddr_t *v6addr = &protect->mp_ipaddrs[i];
if (v6addr->ip_version == IPV6_VERSION &&
IN6_ARE_ADDR_EQUAL(&v6addr->ip_addr, addr))
return (B_TRUE);
}
return (protect->mp_ipaddrcnt == 0 ?
check_dhcpv6_dyn_ip(mcip, addr) : B_FALSE);
}
/*
* Checks various fields within an IPv6 NDP packet.
*/
static boolean_t
ipnospoof_check_ndp(mac_client_impl_t *mcip, mac_protect_t *protect,
ip6_t *ip6h, uchar_t *end)
{
icmp6_t *icmp_nd = (icmp6_t *)&ip6h[1];
int hdrlen, optlen, opttype, len;
uint_t addrlen, maclen;
uint8_t type;
nd_opt_hdr_t *opt;
struct nd_opt_lla *lla = NULL;
/*
* NDP packets do not have extension headers so the ICMPv6 header
* must immediately follow the IPv6 header.
*/
if (ip6h->ip6_nxt != IPPROTO_ICMPV6)
return (B_TRUE);
/* ICMPv6 header missing */
if ((uchar_t *)&icmp_nd[1] > end)
return (B_FALSE);
len = end - (uchar_t *)icmp_nd;
type = icmp_nd->icmp6_type;
switch (type) {
case ND_ROUTER_SOLICIT:
hdrlen = sizeof (nd_router_solicit_t);
break;
case ND_ROUTER_ADVERT:
hdrlen = sizeof (nd_router_advert_t);
break;
case ND_NEIGHBOR_SOLICIT:
hdrlen = sizeof (nd_neighbor_solicit_t);
break;
case ND_NEIGHBOR_ADVERT:
hdrlen = sizeof (nd_neighbor_advert_t);
break;
case ND_REDIRECT:
hdrlen = sizeof (nd_redirect_t);
break;
default:
return (B_TRUE);
}
if (len < hdrlen)
return (B_FALSE);
/* SLLA option checking is needed for RS/RA/NS */
opttype = ND_OPT_SOURCE_LINKADDR;
switch (type) {
case ND_NEIGHBOR_ADVERT: {
nd_neighbor_advert_t *na = (nd_neighbor_advert_t *)icmp_nd;
if (!ipnospoof_check_v6(mcip, protect, &na->nd_na_target)) {
DTRACE_PROBE2(ndp__na__fail,
mac_client_impl_t *, mcip, ip6_t *, ip6h);
return (B_FALSE);
}
/* TLLA option for NA */
opttype = ND_OPT_TARGET_LINKADDR;
break;
}
case ND_REDIRECT: {
/* option checking not needed for RD */
return (B_TRUE);
}
default:
break;
}
if (len == hdrlen) {
/* no options, we're done */
return (B_TRUE);
}
opt = (nd_opt_hdr_t *)((uchar_t *)icmp_nd + hdrlen);
optlen = len - hdrlen;
/* find the option header we need */
while (optlen > sizeof (nd_opt_hdr_t)) {
if (opt->nd_opt_type == opttype) {
lla = (struct nd_opt_lla *)opt;
break;
}
optlen -= 8 * opt->nd_opt_len;
opt = (nd_opt_hdr_t *)
((uchar_t *)opt + 8 * opt->nd_opt_len);
}
if (lla == NULL)
return (B_TRUE);
addrlen = lla->nd_opt_lla_len * 8 - sizeof (nd_opt_hdr_t);
maclen = mcip->mci_mip->mi_info.mi_addr_length;
if (addrlen != maclen ||
bcmp(mcip->mci_unicast->ma_addr,
lla->nd_opt_lla_hdw_addr, maclen) != 0) {
DTRACE_PROBE2(ndp__lla__fail,
mac_client_impl_t *, mcip, ip6_t *, ip6h);
return (B_FALSE);
}
DTRACE_PROBE2(ndp__lla__ok, mac_client_impl_t *, mcip, ip6_t *, ip6h);
return (B_TRUE);
}
/*
* Enforce ip-nospoof protection.
*/
static int
ipnospoof_check(mac_client_impl_t *mcip, mac_protect_t *protect,
mblk_t *mp, mac_header_info_t *mhip)
{
size_t hdrsize = mhip->mhi_hdrsize;
uint32_t sap = mhip->mhi_bindsap;
uchar_t *start, *end;
mblk_t *nmp = NULL;
int err;
err = get_l3_info(mp, hdrsize, &start, &end, &nmp);
if (err != 0) {
DTRACE_PROBE2(invalid__l3, mac_client_impl_t *, mcip,
mblk_t *, mp);
return (err);
}
err = EINVAL;
switch (sap) {
case ETHERTYPE_IP: {
ipha_t *ipha = (ipha_t *)start;
if (start + sizeof (ipha_t) > end)
goto fail;
if (!ipnospoof_check_v4(mcip, protect, &ipha->ipha_src))
goto fail;
if (!intercept_dhcpv4_outbound(mcip, ipha, end))
goto fail;
break;
}
case ETHERTYPE_ARP: {
arh_t *arh = (arh_t *)start;
uint32_t maclen, hlen, plen, arplen;
ipaddr_t spaddr;
uchar_t *shaddr;
if (start + sizeof (arh_t) > end)
goto fail;
maclen = mcip->mci_mip->mi_info.mi_addr_length;
hlen = arh->arh_hlen;
plen = arh->arh_plen;
if ((hlen != 0 && hlen != maclen) ||
plen != sizeof (ipaddr_t))
goto fail;
arplen = sizeof (arh_t) + 2 * hlen + 2 * plen;
if (start + arplen > end)
goto fail;
shaddr = start + sizeof (arh_t);
if (hlen != 0 &&
bcmp(mcip->mci_unicast->ma_addr, shaddr, maclen) != 0)
goto fail;
bcopy(shaddr + hlen, &spaddr, sizeof (spaddr));
if (!ipnospoof_check_v4(mcip, protect, &spaddr))
goto fail;
break;
}
case ETHERTYPE_IPV6: {
ip6_t *ip6h = (ip6_t *)start;
if (start + sizeof (ip6_t) > end)
goto fail;
if (!ipnospoof_check_v6(mcip, protect, &ip6h->ip6_src))
goto fail;
if (!ipnospoof_check_ndp(mcip, protect, ip6h, end))
goto fail;
if (!intercept_dhcpv6_outbound(mcip, ip6h, end))
goto fail;
break;
}
}
freemsg(nmp);
return (0);
fail:
freemsg(nmp);
return (err);
}
static boolean_t
dhcpnospoof_check_cid(mac_protect_t *p, uchar_t *cid, uint_t cidlen)
{
int i;
for (i = 0; i < p->mp_cidcnt; i++) {
mac_dhcpcid_t *dcid = &p->mp_cids[i];
if (dcid->dc_len == cidlen &&
bcmp(dcid->dc_id, cid, cidlen) == 0)
return (B_TRUE);
}
return (B_FALSE);
}
static boolean_t
dhcpnospoof_check_v4(mac_client_impl_t *mcip, mac_protect_t *p,
ipha_t *ipha, uchar_t *end)
{
struct dhcp *dh4;
uchar_t *cid;
uint_t maclen, cidlen = 0;
uint8_t optlen;
int err;
if ((err = get_dhcpv4_info(ipha, end, &dh4)) != 0)
return (err == EINVAL);
maclen = mcip->mci_mip->mi_info.mi_addr_length;
if (dh4->hlen == maclen &&
bcmp(mcip->mci_unicast->ma_addr, dh4->chaddr, maclen) != 0) {
return (B_FALSE);
}
if (get_dhcpv4_option(dh4, end, CD_CLIENT_ID, &cid, &optlen) == 0)
cidlen = optlen;
if (cidlen == 0)
return (B_TRUE);
if (*cid == ARPHRD_ETHER && cidlen - 1 == maclen &&
bcmp(mcip->mci_unicast->ma_addr, cid + 1, maclen) == 0)
return (B_TRUE);
return (dhcpnospoof_check_cid(p, cid, cidlen));
}
static boolean_t
dhcpnospoof_check_v6(mac_client_impl_t *mcip, mac_protect_t *p,
ip6_t *ip6h, uchar_t *end)
{
dhcpv6_message_t *dh6;
dhcpv6_option_t *d6o;
uint8_t mtype;
uchar_t *cid, *lladdr = NULL;
uint_t cidlen, maclen, addrlen = 0;
uint16_t cidtype;
int err;
if ((err = get_dhcpv6_info(ip6h, end, &dh6)) != 0)
return (err == EINVAL);
/*
* We only check client-generated messages.
*/
mtype = dh6->d6m_msg_type;
if (mtype == DHCPV6_MSG_ADVERTISE || mtype == DHCPV6_MSG_REPLY ||
mtype == DHCPV6_MSG_RECONFIGURE)
return (B_TRUE);
d6o = get_dhcpv6_option(&dh6[1], end - (uchar_t *)&dh6[1], NULL,
DHCPV6_OPT_CLIENTID, &cidlen);
if (d6o == NULL || (uchar_t *)d6o + cidlen > end)
return (B_TRUE);
cid = (uchar_t *)&d6o[1];
cidlen -= sizeof (*d6o);
if (cidlen < sizeof (cidtype))
return (B_TRUE);
bcopy(cid, &cidtype, sizeof (cidtype));
cidtype = ntohs(cidtype);
if (cidtype == DHCPV6_DUID_LLT && cidlen >= sizeof (duid_llt_t)) {
lladdr = cid + sizeof (duid_llt_t);
addrlen = cidlen - sizeof (duid_llt_t);
}
if (cidtype == DHCPV6_DUID_LL && cidlen >= sizeof (duid_ll_t)) {
lladdr = cid + sizeof (duid_ll_t);
addrlen = cidlen - sizeof (duid_ll_t);
}
maclen = mcip->mci_mip->mi_info.mi_addr_length;
if (lladdr != NULL && addrlen == maclen &&
bcmp(mcip->mci_unicast->ma_addr, lladdr, maclen) == 0) {
return (B_TRUE);
}
return (dhcpnospoof_check_cid(p, cid, cidlen));
}
/*
* Enforce dhcp-nospoof protection.
*/
static int
dhcpnospoof_check(mac_client_impl_t *mcip, mac_protect_t *protect,
mblk_t *mp, mac_header_info_t *mhip)
{
size_t hdrsize = mhip->mhi_hdrsize;
uint32_t sap = mhip->mhi_bindsap;
uchar_t *start, *end;
mblk_t *nmp = NULL;
int err;
err = get_l3_info(mp, hdrsize, &start, &end, &nmp);
if (err != 0) {
DTRACE_PROBE2(invalid__l3, mac_client_impl_t *, mcip,
mblk_t *, mp);
return (err);
}
err = EINVAL;
switch (sap) {
case ETHERTYPE_IP: {
ipha_t *ipha = (ipha_t *)start;
if (start + sizeof (ipha_t) > end)
goto fail;
if (!dhcpnospoof_check_v4(mcip, protect, ipha, end))
goto fail;
break;
}
case ETHERTYPE_IPV6: {
ip6_t *ip6h = (ip6_t *)start;
if (start + sizeof (ip6_t) > end)
goto fail;
if (!dhcpnospoof_check_v6(mcip, protect, ip6h, end))
goto fail;
break;
}
}
freemsg(nmp);
return (0);
fail:
/* increment dhcpnospoof stat here */
freemsg(nmp);
return (err);
}
/*
* This needs to be called whenever the mac client's mac address changes.
*/
void
mac_protect_update_v6_local_addr(mac_client_impl_t *mcip)
{
uint8_t *p, *macaddr = mcip->mci_unicast->ma_addr;
uint_t i, media = mcip->mci_mip->mi_info.mi_media;
in6_addr_t token, *v6addr = &mcip->mci_v6_local_addr;
in6_addr_t ll_template = {(uint32_t)V6_LINKLOCAL, 0x0, 0x0, 0x0};
bzero(&token, sizeof (token));
p = (uint8_t *)&token.s6_addr32[2];
switch (media) {
case DL_ETHER:
bcopy(macaddr, p, 3);
p[0] ^= 0x2;
p[3] = 0xff;
p[4] = 0xfe;
bcopy(macaddr + 3, p + 5, 3);
break;
case DL_IB:
ASSERT(mcip->mci_mip->mi_info.mi_addr_length == 20);
bcopy(macaddr + 12, p, 8);
p[0] |= 2;
break;
default:
/*
* We do not need to generate the local address for link types
* that do not support link protection. Wifi pretends to be
* ethernet so it is covered by the DL_ETHER case (note the
* use of mi_media instead of mi_nativemedia).
*/
return;
}
for (i = 0; i < 4; i++) {
v6addr->s6_addr32[i] = token.s6_addr32[i] |
ll_template.s6_addr32[i];
}
mcip->mci_protect_flags |= MPT_FLAG_V6_LOCAL_ADDR_SET;
}
/*
* Enforce link protection on one packet.
*/
static int
mac_protect_check_one(mac_client_impl_t *mcip, mblk_t *mp)
{
mac_impl_t *mip = mcip->mci_mip;
mac_resource_props_t *mrp = MCIP_RESOURCE_PROPS(mcip);
mac_protect_t *protect;
mac_header_info_t mhi;
uint32_t types;
int err;
ASSERT(mp->b_next == NULL);
ASSERT(mrp != NULL);
err = mac_vlan_header_info((mac_handle_t)mip, mp, &mhi);
if (err != 0) {
DTRACE_PROBE2(invalid__header, mac_client_impl_t *, mcip,
mblk_t *, mp);
return (err);
}
protect = &mrp->mrp_protect;
types = protect->mp_types;
if ((types & MPT_MACNOSPOOF) != 0) {
if (mhi.mhi_saddr != NULL &&
bcmp(mcip->mci_unicast->ma_addr, mhi.mhi_saddr,
mip->mi_info.mi_addr_length) != 0) {
BUMP_STAT(mcip, macspoofed);
DTRACE_PROBE2(mac__nospoof__fail,
mac_client_impl_t *, mcip, mblk_t *, mp);
return (EINVAL);
}
}
if ((types & MPT_RESTRICTED) != 0) {
uint32_t vid = VLAN_ID(mhi.mhi_tci);
uint32_t sap = mhi.mhi_bindsap;
/*
* ETHERTYPE_VLAN packets are allowed through, provided that
* the vid is not spoofed.
*/
if (vid != 0 && !mac_client_check_flow_vid(mcip, vid)) {
BUMP_STAT(mcip, restricted);
DTRACE_PROBE2(restricted__vid__invalid,
mac_client_impl_t *, mcip, mblk_t *, mp);
return (EINVAL);
}
if (sap != ETHERTYPE_IP && sap != ETHERTYPE_IPV6 &&
sap != ETHERTYPE_ARP) {
BUMP_STAT(mcip, restricted);
DTRACE_PROBE2(restricted__fail,
mac_client_impl_t *, mcip, mblk_t *, mp);
return (EINVAL);
}
}
if ((types & MPT_IPNOSPOOF) != 0) {
if ((err = ipnospoof_check(mcip, protect, mp, &mhi)) != 0) {
BUMP_STAT(mcip, ipspoofed);
DTRACE_PROBE2(ip__nospoof__fail,
mac_client_impl_t *, mcip, mblk_t *, mp);
return (err);
}
}
if ((types & MPT_DHCPNOSPOOF) != 0) {
if ((err = dhcpnospoof_check(mcip, protect, mp, &mhi)) != 0) {
BUMP_STAT(mcip, dhcpspoofed);
DTRACE_PROBE2(dhcp__nospoof__fail,
mac_client_impl_t *, mcip, mblk_t *, mp);
return (err);
}
}
return (0);
}
/*
* Enforce link protection on a packet chain.
* Packets that pass the checks are returned back to the caller.
*/
mblk_t *
mac_protect_check(mac_client_handle_t mch, mblk_t *mp)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mblk_t *ret_mp = NULL, **tailp = &ret_mp, *next;
/*
* Skip checks if we are part of an aggr.
*/
if ((mcip->mci_state_flags & MCIS_IS_AGGR_PORT) != 0)
return (mp);
for (; mp != NULL; mp = next) {
next = mp->b_next;
mp->b_next = NULL;
if (mac_protect_check_one(mcip, mp) == 0) {
*tailp = mp;
tailp = &mp->b_next;
} else {
freemsg(mp);
}
}
return (ret_mp);
}
/*
* Check if a particular protection type is enabled.
*/
boolean_t
mac_protect_enabled(mac_client_handle_t mch, uint32_t type)
{
return (MAC_PROTECT_ENABLED((mac_client_impl_t *)mch, type));
}
static int
validate_ips(mac_protect_t *p)
{
uint_t i, j;
if (p->mp_ipaddrcnt == MPT_RESET)
return (0);
if (p->mp_ipaddrcnt > MPT_MAXIPADDR)
return (EINVAL);
for (i = 0; i < p->mp_ipaddrcnt; i++) {
mac_ipaddr_t *addr = &p->mp_ipaddrs[i];
/*
* The unspecified address is implicitly allowed
* so there's no need to add it to the list.
*/
if (addr->ip_version == IPV4_VERSION) {
if (V4_PART_OF_V6(addr->ip_addr) == INADDR_ANY)
return (EINVAL);
} else if (addr->ip_version == IPV6_VERSION) {
if (IN6_IS_ADDR_UNSPECIFIED(&addr->ip_addr))
return (EINVAL);
} else {
/* invalid ip version */
return (EINVAL);
}
for (j = 0; j < p->mp_ipaddrcnt; j++) {
mac_ipaddr_t *addr1 = &p->mp_ipaddrs[j];
if (i == j || addr->ip_version != addr1->ip_version)
continue;
/* found a duplicate */
if ((addr->ip_version == IPV4_VERSION &&
V4_PART_OF_V6(addr->ip_addr) ==
V4_PART_OF_V6(addr1->ip_addr)) ||
IN6_ARE_ADDR_EQUAL(&addr->ip_addr,
&addr1->ip_addr))
return (EINVAL);
}
}
return (0);
}
/* ARGSUSED */
static int
validate_cids(mac_protect_t *p)
{
uint_t i, j;
if (p->mp_cidcnt == MPT_RESET)
return (0);
if (p->mp_cidcnt > MPT_MAXCID)
return (EINVAL);
for (i = 0; i < p->mp_cidcnt; i++) {
mac_dhcpcid_t *cid = &p->mp_cids[i];
if (cid->dc_len > MPT_MAXCIDLEN ||
(cid->dc_form != CIDFORM_TYPED &&
cid->dc_form != CIDFORM_HEX &&
cid->dc_form != CIDFORM_STR))
return (EINVAL);
for (j = 0; j < p->mp_cidcnt; j++) {
mac_dhcpcid_t *cid1 = &p->mp_cids[j];
if (i == j || cid->dc_len != cid1->dc_len)
continue;
/* found a duplicate */
if (bcmp(cid->dc_id, cid1->dc_id, cid->dc_len) == 0)
return (EINVAL);
}
}
return (0);
}
/*
* Sanity-checks parameters given by userland.
*/
int
mac_protect_validate(mac_resource_props_t *mrp)
{
mac_protect_t *p = &mrp->mrp_protect;
int err;
/* check for invalid types */
if (p->mp_types != MPT_RESET && (p->mp_types & ~MPT_ALL) != 0)
return (EINVAL);
if ((err = validate_ips(p)) != 0)
return (err);
if ((err = validate_cids(p)) != 0)
return (err);
return (0);
}
/*
* Enable/disable link protection.
*/
int
mac_protect_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_impl_t *mip = mcip->mci_mip;
uint_t media = mip->mi_info.mi_nativemedia;
int err;
ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
/* tunnels are not supported */
if (media == DL_IPV4 || media == DL_IPV6 || media == DL_6TO4)
return (ENOTSUP);
if ((err = mac_protect_validate(mrp)) != 0)
return (err);
if (err != 0)
return (err);
mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
i_mac_notify(((mcip->mci_state_flags & MCIS_IS_VNIC) != 0 ?
mcip->mci_upper_mip : mip), MAC_NOTE_ALLOWED_IPS);
return (0);
}
void
mac_protect_update(mac_resource_props_t *new, mac_resource_props_t *curr)
{
mac_protect_t *np = &new->mrp_protect;
mac_protect_t *cp = &curr->mrp_protect;
uint32_t types = np->mp_types;
if (types == MPT_RESET) {
cp->mp_types = 0;
curr->mrp_mask &= ~MRP_PROTECT;
} else {
if (types != 0) {
cp->mp_types = types;
curr->mrp_mask |= MRP_PROTECT;
}
}
if (np->mp_ipaddrcnt != 0) {
if (np->mp_ipaddrcnt <= MPT_MAXIPADDR) {
bcopy(np->mp_ipaddrs, cp->mp_ipaddrs,
sizeof (cp->mp_ipaddrs));
cp->mp_ipaddrcnt = np->mp_ipaddrcnt;
} else if (np->mp_ipaddrcnt == MPT_RESET) {
bzero(cp->mp_ipaddrs, sizeof (cp->mp_ipaddrs));
cp->mp_ipaddrcnt = 0;
}
}
if (np->mp_cidcnt != 0) {
if (np->mp_cidcnt <= MPT_MAXCID) {
bcopy(np->mp_cids, cp->mp_cids, sizeof (cp->mp_cids));
cp->mp_cidcnt = np->mp_cidcnt;
} else if (np->mp_cidcnt == MPT_RESET) {
bzero(cp->mp_cids, sizeof (cp->mp_cids));
cp->mp_cidcnt = 0;
}
}
}
void
mac_protect_init(mac_client_impl_t *mcip)
{
mutex_init(&mcip->mci_protect_lock, NULL, MUTEX_DRIVER, NULL);
mcip->mci_protect_flags = 0;
mcip->mci_txn_cleanup_tid = 0;
avl_create(&mcip->mci_v4_pending_txn, compare_dhcpv4_xid,
sizeof (dhcpv4_txn_t), offsetof(dhcpv4_txn_t, dt_node));
avl_create(&mcip->mci_v4_completed_txn, compare_dhcpv4_cid,
sizeof (dhcpv4_txn_t), offsetof(dhcpv4_txn_t, dt_node));
avl_create(&mcip->mci_v4_dyn_ip, compare_dhcpv4_ip,
sizeof (dhcpv4_txn_t), offsetof(dhcpv4_txn_t, dt_ipnode));
avl_create(&mcip->mci_v6_pending_txn, compare_dhcpv6_xid,
sizeof (dhcpv6_txn_t), offsetof(dhcpv6_txn_t, dt_node));
avl_create(&mcip->mci_v6_cid, compare_dhcpv6_cid,
sizeof (dhcpv6_cid_t), offsetof(dhcpv6_cid_t, dc_node));
avl_create(&mcip->mci_v6_dyn_ip, compare_dhcpv6_ip,
sizeof (dhcpv6_addr_t), offsetof(dhcpv6_addr_t, da_node));
}
void
mac_protect_fini(mac_client_impl_t *mcip)
{
avl_destroy(&mcip->mci_v6_dyn_ip);
avl_destroy(&mcip->mci_v6_cid);
avl_destroy(&mcip->mci_v6_pending_txn);
avl_destroy(&mcip->mci_v4_dyn_ip);
avl_destroy(&mcip->mci_v4_completed_txn);
avl_destroy(&mcip->mci_v4_pending_txn);
mcip->mci_txn_cleanup_tid = 0;
mcip->mci_protect_flags = 0;
mutex_destroy(&mcip->mci_protect_lock);
}
static boolean_t
allowed_ips_set(mac_resource_props_t *mrp, uint32_t af)
{
int i;
for (i = 0; i < mrp->mrp_protect.mp_ipaddrcnt; i++) {
if (mrp->mrp_protect.mp_ipaddrs[i].ip_version == af)
return (B_TRUE);
}
return (B_FALSE);
}
mac_protect_t *
mac_protect_get(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
return (&mip->mi_resource_props.mrp_protect);
}
|