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
|
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This file contains consumer routines of the IPv4 forwarding engine
*/
#include <sys/types.h>
#include <sys/stream.h>
#include <sys/stropts.h>
#include <sys/strlog.h>
#include <sys/dlpi.h>
#include <sys/ddi.h>
#include <sys/cmn_err.h>
#include <sys/policy.h>
#include <sys/systm.h>
#include <sys/strsun.h>
#include <sys/kmem.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <net/if_dl.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <inet/common.h>
#include <inet/mi.h>
#include <inet/mib2.h>
#include <inet/ip.h>
#include <inet/ip6.h>
#include <inet/ip_ndp.h>
#include <inet/arp.h>
#include <inet/ip_if.h>
#include <inet/ip_ire.h>
#include <inet/ip_ftable.h>
#include <inet/ip_rts.h>
#include <inet/nd.h>
#include <net/pfkeyv2.h>
#include <inet/ipsec_info.h>
#include <inet/sadb.h>
#include <sys/kmem.h>
#include <inet/tcp.h>
#include <inet/ipclassifier.h>
#include <sys/zone.h>
#include <net/radix.h>
#include <sys/tsol/label.h>
#include <sys/tsol/tnet.h>
#define IS_DEFAULT_ROUTE(ire) \
(((ire)->ire_type & IRE_DEFAULT) || \
(((ire)->ire_type & IRE_INTERFACE) && ((ire)->ire_addr == 0)))
/*
* structure for passing args between ire_ftable_lookup and ire_find_best_route
*/
typedef struct ire_ftable_args_s {
ipaddr_t ift_addr;
ipaddr_t ift_mask;
ipaddr_t ift_gateway;
int ift_type;
const ipif_t *ift_ipif;
zoneid_t ift_zoneid;
uint32_t ift_ihandle;
const ts_label_t *ift_tsl;
int ift_flags;
ire_t *ift_best_ire;
} ire_ftable_args_t;
static ire_t *route_to_dst(const struct sockaddr *, zoneid_t, ip_stack_t *);
static ire_t *ire_round_robin(irb_t *, zoneid_t, ire_ftable_args_t *,
ip_stack_t *);
static void ire_del_host_redir(ire_t *, char *);
static boolean_t ire_find_best_route(struct radix_node *, void *);
/*
* Lookup a route in forwarding table. A specific lookup is indicated by
* passing the required parameters and indicating the match required in the
* flag field.
*
* Looking for default route can be done in three ways
* 1) pass mask as 0 and set MATCH_IRE_MASK in flags field
* along with other matches.
* 2) pass type as IRE_DEFAULT and set MATCH_IRE_TYPE in flags
* field along with other matches.
* 3) if the destination and mask are passed as zeros.
*
* A request to return a default route if no route
* is found, can be specified by setting MATCH_IRE_DEFAULT
* in flags.
*
* It does not support recursion more than one level. It
* will do recursive lookup only when the lookup maps to
* a prefix or default route and MATCH_IRE_RECURSIVE flag is passed.
*
* If the routing table is setup to allow more than one level
* of recursion, the cleaning up cache table will not work resulting
* in invalid routing.
*
* Supports IP_BOUND_IF by following the ipif/ill when recursing.
*
* NOTE : When this function returns NULL, pire has already been released.
* pire is valid only when this function successfully returns an
* ire.
*/
ire_t *
ire_ftable_lookup(ipaddr_t addr, ipaddr_t mask, ipaddr_t gateway,
int type, const ipif_t *ipif, ire_t **pire, zoneid_t zoneid,
uint32_t ihandle, const ts_label_t *tsl, int flags, ip_stack_t *ipst)
{
ire_t *ire = NULL;
ipaddr_t gw_addr;
struct rt_sockaddr rdst, rmask;
struct rt_entry *rt;
ire_ftable_args_t margs;
boolean_t found_incomplete = B_FALSE;
ASSERT(ipif == NULL || !ipif->ipif_isv6);
ASSERT(!(flags & MATCH_IRE_WQ));
/*
* When we return NULL from this function, we should make
* sure that *pire is NULL so that the callers will not
* wrongly REFRELE the pire.
*/
if (pire != NULL)
*pire = NULL;
/*
* ire_match_args() will dereference ipif MATCH_IRE_SRC or
* MATCH_IRE_ILL is set.
*/
if ((flags & (MATCH_IRE_SRC | MATCH_IRE_ILL | MATCH_IRE_ILL_GROUP)) &&
(ipif == NULL))
return (NULL);
(void) memset(&rdst, 0, sizeof (rdst));
rdst.rt_sin_len = sizeof (rdst);
rdst.rt_sin_family = AF_INET;
rdst.rt_sin_addr.s_addr = addr;
(void) memset(&rmask, 0, sizeof (rmask));
rmask.rt_sin_len = sizeof (rmask);
rmask.rt_sin_family = AF_INET;
rmask.rt_sin_addr.s_addr = mask;
(void) memset(&margs, 0, sizeof (margs));
margs.ift_addr = addr;
margs.ift_mask = mask;
margs.ift_gateway = gateway;
margs.ift_type = type;
margs.ift_ipif = ipif;
margs.ift_zoneid = zoneid;
margs.ift_ihandle = ihandle;
margs.ift_tsl = tsl;
margs.ift_flags = flags;
/*
* The flags argument passed to ire_ftable_lookup may cause the
* search to return, not the longest matching prefix, but the
* "best matching prefix", i.e., the longest prefix that also
* satisfies constraints imposed via the permutation of flags
* passed in. To achieve this, we invoke ire_match_args() on
* each matching leaf in the radix tree. ire_match_args is
* invoked by the callback function ire_find_best_route()
* We hold the global tree lock in read mode when calling
* rn_match_args.Before dropping the global tree lock, ensure
* that the radix node can't be deleted by incrementing ire_refcnt.
*/
RADIX_NODE_HEAD_RLOCK(ipst->ips_ip_ftable);
rt = (struct rt_entry *)ipst->ips_ip_ftable->rnh_matchaddr_args(&rdst,
ipst->ips_ip_ftable, ire_find_best_route, &margs);
ire = margs.ift_best_ire;
RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
if (rt == NULL) {
return (NULL);
} else {
ASSERT(ire != NULL);
}
DTRACE_PROBE2(ire__found, ire_ftable_args_t *, &margs, ire_t *, ire);
if (!IS_DEFAULT_ROUTE(ire))
goto found_ire_held;
/*
* If default route is found, see if default matching criteria
* are satisfied.
*/
if (flags & MATCH_IRE_MASK) {
/*
* we were asked to match a 0 mask, and came back with
* a default route. Ok to return it.
*/
goto found_default_ire;
}
if ((flags & MATCH_IRE_TYPE) &&
(type & (IRE_DEFAULT | IRE_INTERFACE))) {
/*
* we were asked to match a default ire type. Ok to return it.
*/
goto found_default_ire;
}
if (flags & MATCH_IRE_DEFAULT) {
goto found_default_ire;
}
/*
* we found a default route, but default matching criteria
* are not specified and we are not explicitly looking for
* default.
*/
IRE_REFRELE(ire);
return (NULL);
found_default_ire:
/*
* round-robin only if we have more than one route in the bucket.
*/
if ((ire->ire_bucket->irb_ire_cnt > 1) &&
IS_DEFAULT_ROUTE(ire) &&
((flags & (MATCH_IRE_DEFAULT | MATCH_IRE_MASK)) ==
MATCH_IRE_DEFAULT)) {
ire_t *next_ire;
next_ire = ire_round_robin(ire->ire_bucket, zoneid, &margs,
ipst);
IRE_REFRELE(ire);
if (next_ire != NULL) {
ire = next_ire;
} else {
/* no route */
return (NULL);
}
}
found_ire_held:
ASSERT(ire->ire_type != IRE_MIPRTUN && ire->ire_in_ill == NULL);
if ((flags & MATCH_IRE_RJ_BHOLE) &&
(ire->ire_flags & (RTF_BLACKHOLE | RTF_REJECT))) {
return (ire);
}
/*
* At this point, IRE that was found must be an IRE_FORWARDTABLE
* type. If this is a recursive lookup and an IRE_INTERFACE type was
* found, return that. If it was some other IRE_FORWARDTABLE type of
* IRE (one of the prefix types), then it is necessary to fill in the
* parent IRE pointed to by pire, and then lookup the gateway address of
* the parent. For backwards compatiblity, if this lookup returns an
* IRE other than a IRE_CACHETABLE or IRE_INTERFACE, then one more level
* of lookup is done.
*/
if (flags & MATCH_IRE_RECURSIVE) {
ipif_t *gw_ipif;
int match_flags = MATCH_IRE_DSTONLY;
ire_t *save_ire;
if (ire->ire_type & IRE_INTERFACE)
return (ire);
if (pire != NULL)
*pire = ire;
/*
* If we can't find an IRE_INTERFACE or the caller has not
* asked for pire, we need to REFRELE the save_ire.
*/
save_ire = ire;
/*
* Currently MATCH_IRE_ILL is never used with
* (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT) while
* sending out packets as MATCH_IRE_ILL is used only
* for communicating with on-link hosts. We can't assert
* that here as RTM_GET calls this function with
* MATCH_IRE_ILL | MATCH_IRE_DEFAULT | MATCH_IRE_RECURSIVE.
* We have already used the MATCH_IRE_ILL in determining
* the right prefix route at this point. To match the
* behavior of how we locate routes while sending out
* packets, we don't want to use MATCH_IRE_ILL below
* while locating the interface route.
*
* ire_ftable_lookup may end up with an incomplete IRE_CACHE
* entry for the gateway (i.e., one for which the
* ire_nce->nce_state is not yet ND_REACHABLE). If the caller
* has specified MATCH_IRE_COMPLETE, such entries will not
* be returned; instead, we return the IF_RESOLVER ire.
*/
if (ire->ire_ipif != NULL)
match_flags |= MATCH_IRE_ILL_GROUP;
ire = ire_route_lookup(ire->ire_gateway_addr, 0, 0, 0,
ire->ire_ipif, NULL, zoneid, tsl, match_flags, ipst);
DTRACE_PROBE2(ftable__route__lookup1, (ire_t *), ire,
(ire_t *), save_ire);
if (ire == NULL ||
((ire->ire_type & IRE_CACHE) && ire->ire_nce &&
ire->ire_nce->nce_state != ND_REACHABLE &&
(flags & MATCH_IRE_COMPLETE))) {
/*
* Do not release the parent ire if MATCH_IRE_PARENT
* is set. Also return it via ire.
*/
if (ire != NULL) {
ire_refrele(ire);
ire = NULL;
found_incomplete = B_TRUE;
}
if (flags & MATCH_IRE_PARENT) {
if (pire != NULL) {
/*
* Need an extra REFHOLD, if the parent
* ire is returned via both ire and
* pire.
*/
IRE_REFHOLD(save_ire);
}
ire = save_ire;
} else {
ire_refrele(save_ire);
if (pire != NULL)
*pire = NULL;
}
if (!found_incomplete)
return (ire);
}
if (ire->ire_type & (IRE_CACHETABLE | IRE_INTERFACE)) {
/*
* If the caller did not ask for pire, release
* it now.
*/
if (pire == NULL) {
ire_refrele(save_ire);
}
return (ire);
}
match_flags |= MATCH_IRE_TYPE;
gw_addr = ire->ire_gateway_addr;
gw_ipif = ire->ire_ipif;
ire_refrele(ire);
ire = ire_route_lookup(gw_addr, 0, 0,
(found_incomplete? IRE_INTERFACE :
(IRE_CACHETABLE | IRE_INTERFACE)),
gw_ipif, NULL, zoneid, tsl, match_flags, ipst);
DTRACE_PROBE2(ftable__route__lookup2, (ire_t *), ire,
(ire_t *), save_ire);
if (ire == NULL ||
((ire->ire_type & IRE_CACHE) && ire->ire_nce &&
ire->ire_nce->nce_state != ND_REACHABLE &&
(flags & MATCH_IRE_COMPLETE))) {
/*
* Do not release the parent ire if MATCH_IRE_PARENT
* is set. Also return it via ire.
*/
if (ire != NULL) {
ire_refrele(ire);
ire = NULL;
}
if (flags & MATCH_IRE_PARENT) {
if (pire != NULL) {
/*
* Need an extra REFHOLD, if the
* parent ire is returned via both
* ire and pire.
*/
IRE_REFHOLD(save_ire);
}
ire = save_ire;
} else {
ire_refrele(save_ire);
if (pire != NULL)
*pire = NULL;
}
return (ire);
} else if (pire == NULL) {
/*
* If the caller did not ask for pire, release
* it now.
*/
ire_refrele(save_ire);
}
return (ire);
}
ASSERT(pire == NULL || *pire == NULL);
return (ire);
}
/*
* Find an IRE_OFFSUBNET IRE entry for the multicast address 'group'
* that goes through 'ipif'. As a fallback, a route that goes through
* ipif->ipif_ill can be returned.
*/
ire_t *
ipif_lookup_multi_ire(ipif_t *ipif, ipaddr_t group)
{
ire_t *ire;
ire_t *save_ire = NULL;
ire_t *gw_ire;
irb_t *irb;
ipaddr_t gw_addr;
int match_flags = MATCH_IRE_TYPE | MATCH_IRE_ILL;
ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
ASSERT(CLASSD(group));
ire = ire_ftable_lookup(group, 0, 0, 0, NULL, NULL, ALL_ZONES, 0,
NULL, MATCH_IRE_DEFAULT, ipst);
if (ire == NULL)
return (NULL);
irb = ire->ire_bucket;
ASSERT(irb);
IRB_REFHOLD(irb);
ire_refrele(ire);
for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
if (ire->ire_addr != group ||
ipif->ipif_zoneid != ire->ire_zoneid &&
ire->ire_zoneid != ALL_ZONES) {
continue;
}
switch (ire->ire_type) {
case IRE_DEFAULT:
case IRE_PREFIX:
case IRE_HOST:
gw_addr = ire->ire_gateway_addr;
gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE,
ipif, NULL, ALL_ZONES, 0, NULL, match_flags, ipst);
if (gw_ire != NULL) {
if (save_ire != NULL) {
ire_refrele(save_ire);
}
IRE_REFHOLD(ire);
if (gw_ire->ire_ipif == ipif) {
ire_refrele(gw_ire);
IRB_REFRELE(irb);
return (ire);
}
ire_refrele(gw_ire);
save_ire = ire;
}
break;
case IRE_IF_NORESOLVER:
case IRE_IF_RESOLVER:
if (ire->ire_ipif == ipif) {
if (save_ire != NULL) {
ire_refrele(save_ire);
}
IRE_REFHOLD(ire);
IRB_REFRELE(irb);
return (ire);
}
break;
}
}
IRB_REFRELE(irb);
return (save_ire);
}
/*
* Find an IRE_INTERFACE for the multicast group.
* Allows different routes for multicast addresses
* in the unicast routing table (akin to 224.0.0.0 but could be more specific)
* which point at different interfaces. This is used when IP_MULTICAST_IF
* isn't specified (when sending) and when IP_ADD_MEMBERSHIP doesn't
* specify the interface to join on.
*
* Supports IP_BOUND_IF by following the ipif/ill when recursing.
*/
ire_t *
ire_lookup_multi(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst)
{
ire_t *ire;
ipif_t *ipif = NULL;
int match_flags = MATCH_IRE_TYPE;
ipaddr_t gw_addr;
ire = ire_ftable_lookup(group, 0, 0, 0, NULL, NULL, zoneid,
0, NULL, MATCH_IRE_DEFAULT, ipst);
/* We search a resolvable ire in case of multirouting. */
if ((ire != NULL) && (ire->ire_flags & RTF_MULTIRT)) {
ire_t *cire = NULL;
/*
* If the route is not resolvable, the looked up ire
* may be changed here. In that case, ire_multirt_lookup()
* IRE_REFRELE the original ire and change it.
*/
(void) ire_multirt_lookup(&cire, &ire, MULTIRT_CACHEGW,
NULL, ipst);
if (cire != NULL)
ire_refrele(cire);
}
if (ire == NULL)
return (NULL);
/*
* Make sure we follow ire_ipif.
*
* We need to determine the interface route through
* which the gateway will be reached. We don't really
* care which interface is picked if the interface is
* part of a group.
*/
if (ire->ire_ipif != NULL) {
ipif = ire->ire_ipif;
match_flags |= MATCH_IRE_ILL_GROUP;
}
switch (ire->ire_type) {
case IRE_DEFAULT:
case IRE_PREFIX:
case IRE_HOST:
gw_addr = ire->ire_gateway_addr;
ire_refrele(ire);
ire = ire_ftable_lookup(gw_addr, 0, 0,
IRE_INTERFACE, ipif, NULL, zoneid, 0,
NULL, match_flags, ipst);
return (ire);
case IRE_IF_NORESOLVER:
case IRE_IF_RESOLVER:
return (ire);
default:
ire_refrele(ire);
return (NULL);
}
}
/*
* Delete the passed in ire if the gateway addr matches
*/
void
ire_del_host_redir(ire_t *ire, char *gateway)
{
if ((ire->ire_flags & RTF_DYNAMIC) &&
(ire->ire_gateway_addr == *(ipaddr_t *)gateway))
ire_delete(ire);
}
/*
* Search for all HOST REDIRECT routes that are
* pointing at the specified gateway and
* delete them. This routine is called only
* when a default gateway is going away.
*/
void
ire_delete_host_redirects(ipaddr_t gateway, ip_stack_t *ipst)
{
struct rtfuncarg rtfarg;
(void) memset(&rtfarg, 0, sizeof (rtfarg));
rtfarg.rt_func = ire_del_host_redir;
rtfarg.rt_arg = (void *)&gateway;
(void) ipst->ips_ip_ftable->rnh_walktree_mt(ipst->ips_ip_ftable,
rtfunc, &rtfarg, irb_refhold_rn, irb_refrele_rn);
}
struct ihandle_arg {
uint32_t ihandle;
ire_t *ire;
};
static int
ire_ihandle_onlink_match(struct radix_node *rn, void *arg)
{
struct rt_entry *rt;
irb_t *irb;
ire_t *ire;
struct ihandle_arg *ih = arg;
rt = (struct rt_entry *)rn;
ASSERT(rt != NULL);
irb = &rt->rt_irb;
for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) {
if ((ire->ire_type & IRE_INTERFACE) &&
(ire->ire_ihandle == ih->ihandle)) {
ih->ire = ire;
IRE_REFHOLD(ire);
return (1);
}
}
return (0);
}
/*
* Locate the interface ire that is tied to the cache ire 'cire' via
* cire->ire_ihandle.
*
* We are trying to create the cache ire for an onlink destn. or
* gateway in 'cire'. We are called from ire_add_v4() in the IRE_IF_RESOLVER
* case, after the ire has come back from ARP.
*/
ire_t *
ire_ihandle_lookup_onlink(ire_t *cire)
{
ire_t *ire;
int match_flags;
struct ihandle_arg ih;
ip_stack_t *ipst;
ASSERT(cire != NULL);
ipst = cire->ire_ipst;
/*
* We don't need to specify the zoneid to ire_ftable_lookup() below
* because the ihandle refers to an ipif which can be in only one zone.
*/
match_flags = MATCH_IRE_TYPE | MATCH_IRE_IHANDLE | MATCH_IRE_MASK;
/*
* We know that the mask of the interface ire equals cire->ire_cmask.
* (When ip_newroute() created 'cire' for an on-link destn. it set its
* cmask from the interface ire's mask)
*/
ire = ire_ftable_lookup(cire->ire_addr, cire->ire_cmask, 0,
IRE_INTERFACE, NULL, NULL, ALL_ZONES, cire->ire_ihandle,
NULL, match_flags, ipst);
if (ire != NULL)
return (ire);
/*
* If we didn't find an interface ire above, we can't declare failure.
* For backwards compatibility, we need to support prefix routes
* pointing to next hop gateways that are not on-link.
*
* In the resolver/noresolver case, ip_newroute() thinks it is creating
* the cache ire for an onlink destination in 'cire'. But 'cire' is
* not actually onlink, because ire_ftable_lookup() cheated it, by
* doing ire_route_lookup() twice and returning an interface ire.
*
* Eg. default - gw1 (line 1)
* gw1 - gw2 (line 2)
* gw2 - hme0 (line 3)
*
* In the above example, ip_newroute() tried to create the cache ire
* 'cire' for gw1, based on the interface route in line 3. The
* ire_ftable_lookup() above fails, because there is no interface route
* to reach gw1. (it is gw2). We fall thru below.
*
* Do a brute force search based on the ihandle in a subset of the
* forwarding tables, corresponding to cire->ire_cmask. Otherwise
* things become very complex, since we don't have 'pire' in this
* case. (Also note that this method is not possible in the offlink
* case because we don't know the mask)
*/
(void) memset(&ih, 0, sizeof (ih));
ih.ihandle = cire->ire_ihandle;
(void) ipst->ips_ip_ftable->rnh_walktree_mt(ipst->ips_ip_ftable,
ire_ihandle_onlink_match, &ih, irb_refhold_rn, irb_refrele_rn);
return (ih.ire);
}
/*
* IRE iterator used by ire_ftable_lookup[_v6]() to process multiple default
* routes. Given a starting point in the hash list (ire_origin), walk the IREs
* in the bucket skipping default interface routes and deleted entries.
* Returns the next IRE (unheld), or NULL when we're back to the starting point.
* Assumes that the caller holds a reference on the IRE bucket.
*/
ire_t *
ire_get_next_default_ire(ire_t *ire, ire_t *ire_origin)
{
ASSERT(ire_origin->ire_bucket != NULL);
ASSERT(ire != NULL);
do {
ire = ire->ire_next;
if (ire == NULL)
ire = ire_origin->ire_bucket->irb_ire;
if (ire == ire_origin)
return (NULL);
} while ((ire->ire_type & IRE_INTERFACE) ||
(ire->ire_marks & IRE_MARK_CONDEMNED));
ASSERT(ire != NULL);
return (ire);
}
static ipif_t *
ire_forward_src_ipif(ipaddr_t dst, ire_t *sire, ire_t *ire, ill_t *dst_ill,
int zoneid, ushort_t *marks)
{
ipif_t *src_ipif;
ip_stack_t *ipst = dst_ill->ill_ipst;
/*
* Pick the best source address from dst_ill.
*
* 1) If it is part of a multipathing group, we would
* like to spread the inbound packets across different
* interfaces. ipif_select_source picks a random source
* across the different ills in the group.
*
* 2) If it is not part of a multipathing group, we try
* to pick the source address from the destination
* route. Clustering assumes that when we have multiple
* prefixes hosted on an interface, the prefix of the
* source address matches the prefix of the destination
* route. We do this only if the address is not
* DEPRECATED.
*
* 3) If the conn is in a different zone than the ire, we
* need to pick a source address from the right zone.
*
* NOTE : If we hit case (1) above, the prefix of the source
* address picked may not match the prefix of the
* destination routes prefix as ipif_select_source
* does not look at "dst" while picking a source
* address.
* If we want the same behavior as (2), we will need
* to change the behavior of ipif_select_source.
*/
if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) {
/*
* The RTF_SETSRC flag is set in the parent ire (sire).
* Check that the ipif matching the requested source
* address still exists.
*/
src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL,
zoneid, NULL, NULL, NULL, NULL, ipst);
return (src_ipif);
}
*marks |= IRE_MARK_USESRC_CHECK;
if ((dst_ill->ill_group != NULL) ||
(ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) ||
(dst_ill->ill_usesrc_ifindex != 0)) {
src_ipif = ipif_select_source(dst_ill, dst, zoneid);
if (src_ipif == NULL)
return (NULL);
} else {
src_ipif = ire->ire_ipif;
ASSERT(src_ipif != NULL);
/* hold src_ipif for uniformity */
ipif_refhold(src_ipif);
}
return (src_ipif);
}
/*
* This function is called by ip_rput_noire() and ip_fast_forward()
* to resolve the route of incoming packet that needs to be forwarded.
* If the ire of the nexthop is not already in the cachetable, this
* routine will insert it to the table, but won't trigger ARP resolution yet.
* Thus unlike ip_newroute, this function adds incomplete ires to
* the cachetable. ARP resolution for these ires are delayed until
* after all of the packet processing is completed and its ready to
* be sent out on the wire, Eventually, the packet transmit routine
* ip_xmit_v4() attempts to send a packet to the driver. If it finds
* that there is no link layer information, it will do the arp
* resolution and queue the packet in ire->ire_nce->nce_qd_mp and
* then send it out once the arp resolution is over
* (see ip_xmit_v4()->ire_arpresolve()). This scheme is similar to
* the model of BSD/SunOS 4
*
* In future, the insertion of incomplete ires in the cachetable should
* be implemented in hostpath as well, as doing so will greatly reduce
* the existing complexity for code paths that depend on the context of
* the sender (such as IPsec).
*
* Thus this scheme of adding incomplete ires in cachetable in forwarding
* path can be used as a template for simplifying the hostpath.
*/
ire_t *
ire_forward(ipaddr_t dst, boolean_t *check_multirt, ire_t *supplied_ire,
ire_t *supplied_sire, const struct ts_label_s *tsl, ip_stack_t *ipst)
{
ipaddr_t gw = 0;
ire_t *ire = NULL;
ire_t *sire = NULL, *save_ire;
ill_t *dst_ill = NULL;
int error;
zoneid_t zoneid;
ipif_t *src_ipif = NULL;
mblk_t *res_mp;
ushort_t ire_marks = 0;
tsol_gcgrp_t *gcgrp = NULL;
tsol_gcgrp_addr_t ga;
zoneid = GLOBAL_ZONEID;
if (supplied_ire != NULL) {
/* We have arrived here from ipfil_sendpkt */
ire = supplied_ire;
sire = supplied_sire;
goto create_irecache;
}
ire = ire_ftable_lookup(dst, 0, 0, 0, NULL, &sire, zoneid, 0,
tsl, MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT|MATCH_IRE_SECATTR, ipst);
if (ire == NULL) {
ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0, RTA_DST, ipst);
goto icmp_err_ret;
}
/*
* If we encounter CGTP, we should have the caller use
* ip_newroute to resolve multirt instead of this function.
* CGTP specs explicitly state that it can't be used with routers.
* This essentially prevents insertion of incomplete RTF_MULTIRT
* ires in cachetable.
*/
if (ip_cgtp_filter &&
((ire->ire_flags & RTF_MULTIRT) ||
((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)))) {
ip3dbg(("ire_forward: packet is to be multirouted- "
"handing it to ip_newroute\n"));
if (sire != NULL)
ire_refrele(sire);
ire_refrele(ire);
/*
* Inform caller about encountering of multirt so that
* ip_newroute() can be called.
*/
*check_multirt = B_TRUE;
return (NULL);
}
*check_multirt = B_FALSE;
/*
* Verify that the returned IRE does not have either
* the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is
* either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER.
*/
if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) ||
(ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) {
ip3dbg(("ire 0x%p is not cache/resolver/noresolver\n",
(void *)ire));
goto icmp_err_ret;
}
/*
* If we already have a fully resolved IRE CACHE of the
* nexthop router, just hand over the cache entry
* and we are done.
*/
if (ire->ire_type & IRE_CACHE) {
/*
* If we are using this ire cache entry as a
* gateway to forward packets, chances are we
* will be using it again. So turn off
* the temporary flag, thus reducing its
* chances of getting deleted frequently.
*/
if (ire->ire_marks & IRE_MARK_TEMPORARY) {
irb_t *irb = ire->ire_bucket;
rw_enter(&irb->irb_lock, RW_WRITER);
ire->ire_marks &= ~IRE_MARK_TEMPORARY;
irb->irb_tmp_ire_cnt--;
rw_exit(&irb->irb_lock);
}
if (sire != NULL) {
UPDATE_OB_PKT_COUNT(sire);
sire->ire_last_used_time = lbolt;
ire_refrele(sire);
}
return (ire);
}
create_irecache:
/*
* Increment the ire_ob_pkt_count field for ire if it is an
* INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and
* increment the same for the parent IRE, sire, if it is some
* sort of prefix IRE (which includes DEFAULT, PREFIX, HOST
* and HOST_REDIRECT).
*/
if ((ire->ire_type & IRE_INTERFACE) != 0) {
UPDATE_OB_PKT_COUNT(ire);
ire->ire_last_used_time = lbolt;
}
/*
* sire must be either IRE_CACHETABLE OR IRE_INTERFACE type
*/
if (sire != NULL) {
gw = sire->ire_gateway_addr;
ASSERT((sire->ire_type &
(IRE_CACHETABLE | IRE_INTERFACE)) == 0);
UPDATE_OB_PKT_COUNT(sire);
sire->ire_last_used_time = lbolt;
}
/* Obtain dst_ill */
dst_ill = ip_newroute_get_dst_ill(ire->ire_ipif->ipif_ill);
if (dst_ill == NULL) {
ip2dbg(("ire_forward no dst ill; ire 0x%p\n",
(void *)ire));
goto icmp_err_ret;
}
ASSERT(src_ipif == NULL);
/* Now obtain the src_ipif */
src_ipif = ire_forward_src_ipif(dst, sire, ire, dst_ill,
zoneid, &ire_marks);
if (src_ipif == NULL)
goto icmp_err_ret;
switch (ire->ire_type) {
case IRE_IF_NORESOLVER:
/* create ire_cache for ire_addr endpoint */
case IRE_IF_RESOLVER:
/*
* We have the IRE_IF_RESOLVER of the nexthop gateway
* and now need to build a IRE_CACHE for it.
* In this case, we have the following :
*
* 1) src_ipif - used for getting a source address.
*
* 2) dst_ill - from which we derive ire_stq/ire_rfq. This
* means packets using the IRE_CACHE that we will build
* here will go out on dst_ill.
*
* 3) sire may or may not be NULL. But, the IRE_CACHE that is
* to be created will only be tied to the IRE_INTERFACE
* that was derived from the ire_ihandle field.
*
* If sire is non-NULL, it means the destination is
* off-link and we will first create the IRE_CACHE for the
* gateway.
*/
res_mp = dst_ill->ill_resolver_mp;
if (ire->ire_type == IRE_IF_RESOLVER &&
(!OK_RESOLVER_MP(res_mp))) {
ire_refrele(ire);
ire = NULL;
goto out;
}
/*
* To be at this point in the code with a non-zero gw
* means that dst is reachable through a gateway that
* we have never resolved. By changing dst to the gw
* addr we resolve the gateway first.
*/
if (gw != INADDR_ANY) {
/*
* The source ipif that was determined above was
* relative to the destination address, not the
* gateway's. If src_ipif was not taken out of
* the IRE_IF_RESOLVER entry, we'll need to call
* ipif_select_source() again.
*/
if (src_ipif != ire->ire_ipif) {
ipif_refrele(src_ipif);
src_ipif = ipif_select_source(dst_ill,
gw, zoneid);
if (src_ipif == NULL)
goto icmp_err_ret;
}
dst = gw;
gw = INADDR_ANY;
}
/*
* dst has been set to the address of the nexthop.
*
* TSol note: get security attributes of the nexthop;
* Note that the nexthop may either be a gateway, or the
* packet destination itself; Detailed explanation of
* issues involved is provided in the IRE_IF_NORESOLVER
* logic in ip_newroute().
*/
ga.ga_af = AF_INET;
IN6_IPADDR_TO_V4MAPPED(dst, &ga.ga_addr);
gcgrp = gcgrp_lookup(&ga, B_FALSE);
if (ire->ire_type == IRE_IF_NORESOLVER)
dst = ire->ire_addr; /* ire_cache for tunnel endpoint */
save_ire = ire;
/*
* create an incomplete ire-cache with a null dlureq_mp.
* The dlureq_mp will be created in ire_arpresolve.
*/
ire = ire_create(
(uchar_t *)&dst, /* dest address */
(uchar_t *)&ip_g_all_ones, /* mask */
(uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
(uchar_t *)&gw, /* gateway address */
NULL,
(save_ire->ire_type == IRE_IF_RESOLVER ? NULL:
&save_ire->ire_max_frag),
NULL,
dst_ill->ill_rq, /* recv-from queue */
dst_ill->ill_wq, /* send-to queue */
IRE_CACHE, /* IRE type */
NULL,
src_ipif,
NULL,
ire->ire_mask, /* Parent mask */
0,
ire->ire_ihandle, /* Interface handle */
0,
&(ire->ire_uinfo),
NULL,
gcgrp,
ipst);
ip1dbg(("incomplete ire_cache 0x%p\n", (void *)ire));
if (ire != NULL) {
gcgrp = NULL; /* reference now held by IRE */
ire->ire_marks |= ire_marks;
/* add the incomplete ire: */
error = ire_add(&ire, NULL, NULL, NULL, B_TRUE);
if (error == 0 && ire != NULL) {
ire->ire_max_frag = save_ire->ire_max_frag;
ip1dbg(("setting max_frag to %d in ire 0x%p\n",
ire->ire_max_frag, (void *)ire));
} else {
ire_refrele(save_ire);
goto icmp_err_ret;
}
} else {
if (gcgrp != NULL) {
GCGRP_REFRELE(gcgrp);
gcgrp = NULL;
}
}
ire_refrele(save_ire);
break;
default:
break;
}
out:
if (sire != NULL)
ire_refrele(sire);
if (dst_ill != NULL)
ill_refrele(dst_ill);
if (src_ipif != NULL)
ipif_refrele(src_ipif);
return (ire);
icmp_err_ret:
if (src_ipif != NULL)
ipif_refrele(src_ipif);
if (dst_ill != NULL)
ill_refrele(dst_ill);
if (sire != NULL)
ire_refrele(sire);
if (ire != NULL) {
ire_refrele(ire);
}
/* caller needs to send icmp error message */
return (NULL);
}
/*
* Obtain the rt_entry and rt_irb for the route to be added to
* the ips_ip_ftable.
* First attempt to add a node to the radix tree via rn_addroute. If the
* route already exists, return the bucket for the existing route.
*
* Locking notes: Need to hold the global radix tree lock in write mode to
* add a radix node. To prevent the node from being deleted, ire_get_bucket()
* returns with a ref'ed irb_t. The ire itself is added in ire_add_v4()
* while holding the irb_lock, but not the radix tree lock.
*/
irb_t *
ire_get_bucket(ire_t *ire)
{
struct radix_node *rn;
struct rt_entry *rt;
struct rt_sockaddr rmask, rdst;
irb_t *irb = NULL;
ip_stack_t *ipst = ire->ire_ipst;
ASSERT(ipst->ips_ip_ftable != NULL);
/* first try to see if route exists (based on rtalloc1) */
(void) memset(&rdst, 0, sizeof (rdst));
rdst.rt_sin_len = sizeof (rdst);
rdst.rt_sin_family = AF_INET;
rdst.rt_sin_addr.s_addr = ire->ire_addr;
(void) memset(&rmask, 0, sizeof (rmask));
rmask.rt_sin_len = sizeof (rmask);
rmask.rt_sin_family = AF_INET;
rmask.rt_sin_addr.s_addr = ire->ire_mask;
/*
* add the route. based on BSD's rtrequest1(RTM_ADD)
*/
R_Malloc(rt, rt_entry_cache, sizeof (*rt));
(void) memset(rt, 0, sizeof (*rt));
rt->rt_nodes->rn_key = (char *)&rt->rt_dst;
rt->rt_dst = rdst;
irb = &rt->rt_irb;
irb->irb_marks |= IRB_MARK_FTABLE; /* dynamically allocated/freed */
irb->irb_ipst = ipst;
rw_init(&irb->irb_lock, NULL, RW_DEFAULT, NULL);
RADIX_NODE_HEAD_WLOCK(ipst->ips_ip_ftable);
rn = ipst->ips_ip_ftable->rnh_addaddr(&rt->rt_dst, &rmask,
ipst->ips_ip_ftable, (struct radix_node *)rt);
if (rn == NULL) {
RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
Free(rt, rt_entry_cache);
rt = NULL;
irb = NULL;
RADIX_NODE_HEAD_RLOCK(ipst->ips_ip_ftable);
rn = ipst->ips_ip_ftable->rnh_lookup(&rdst, &rmask,
ipst->ips_ip_ftable);
if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) {
/* found a non-root match */
rt = (struct rt_entry *)rn;
}
}
if (rt != NULL) {
irb = &rt->rt_irb;
IRB_REFHOLD(irb);
}
RADIX_NODE_HEAD_UNLOCK(ipst->ips_ip_ftable);
return (irb);
}
/*
* This function is used when the caller wants to know the outbound
* interface for a packet given only the address.
* If this is a offlink IP address and there are multiple
* routes to this destination, this routine will utilise the
* first route it finds to IP address
* Return values:
* 0 - FAILURE
* nonzero - ifindex
*/
uint_t
ifindex_lookup(const struct sockaddr *ipaddr, zoneid_t zoneid)
{
uint_t ifindex = 0;
ire_t *ire;
ill_t *ill;
netstack_t *ns;
ip_stack_t *ipst;
if (zoneid == ALL_ZONES)
ns = netstack_find_by_zoneid(GLOBAL_ZONEID);
else
ns = netstack_find_by_zoneid(zoneid);
ASSERT(ns != NULL);
/*
* For exclusive stacks we set the zoneid to zero
* since IP uses the global zoneid in the exclusive stacks.
*/
if (ns->netstack_stackid != GLOBAL_NETSTACKID)
zoneid = GLOBAL_ZONEID;
ipst = ns->netstack_ip;
ASSERT(ipaddr->sa_family == AF_INET || ipaddr->sa_family == AF_INET6);
if ((ire = route_to_dst(ipaddr, zoneid, ipst)) != NULL) {
ill = ire_to_ill(ire);
if (ill != NULL)
ifindex = ill->ill_phyint->phyint_ifindex;
ire_refrele(ire);
}
netstack_rele(ns);
return (ifindex);
}
/*
* Routine to find the route to a destination. If a ifindex is supplied
* it tries to match the the route to the corresponding ipif for the ifindex
*/
static ire_t *
route_to_dst(const struct sockaddr *dst_addr, zoneid_t zoneid, ip_stack_t *ipst)
{
ire_t *ire = NULL;
int match_flags;
match_flags = (MATCH_IRE_DSTONLY | MATCH_IRE_DEFAULT |
MATCH_IRE_RECURSIVE | MATCH_IRE_RJ_BHOLE);
/* XXX pass NULL tsl for now */
if (dst_addr->sa_family == AF_INET) {
ire = ire_route_lookup(
((struct sockaddr_in *)dst_addr)->sin_addr.s_addr,
0, 0, 0, NULL, NULL, zoneid, NULL, match_flags, ipst);
} else {
ire = ire_route_lookup_v6(
&((struct sockaddr_in6 *)dst_addr)->sin6_addr,
0, 0, 0, NULL, NULL, zoneid, NULL, match_flags, ipst);
}
return (ire);
}
/*
* This routine is called by IP Filter to send a packet out on the wire
* to a specified V4 dst (which may be onlink or offlink). The ifindex may or
* may not be 0. A non-null ifindex indicates IP Filter has stipulated
* an outgoing interface and requires the nexthop to be on that interface.
* IP WILL NOT DO the following to the data packet before sending it out:
* a. manipulate ttl
* b. checksuming
* c. ipsec work
* d. fragmentation
*
* Return values:
* 0: IP was able to send of the data pkt
* ECOMM: Could not send packet
* ENONET No route to dst. It is up to the caller
* to send icmp unreachable error message,
* EINPROGRESS The macaddr of the onlink dst or that
* of the offlink dst's nexthop needs to get
* resolved before packet can be sent to dst.
* Thus transmission is not guaranteed.
*
*/
int
ipfil_sendpkt(const struct sockaddr *dst_addr, mblk_t *mp, uint_t ifindex,
zoneid_t zoneid)
{
ire_t *ire = NULL, *sire = NULL;
ire_t *ire_cache = NULL;
boolean_t check_multirt = B_FALSE;
int value;
int match_flags;
ipaddr_t dst;
netstack_t *ns;
ip_stack_t *ipst;
ASSERT(mp != NULL);
if (zoneid == ALL_ZONES)
ns = netstack_find_by_zoneid(GLOBAL_ZONEID);
else
ns = netstack_find_by_zoneid(zoneid);
ASSERT(ns != NULL);
/*
* For exclusive stacks we set the zoneid to zero
* since IP uses the global zoneid in the exclusive stacks.
*/
if (ns->netstack_stackid != GLOBAL_NETSTACKID)
zoneid = GLOBAL_ZONEID;
ipst = ns->netstack_ip;
ASSERT(dst_addr->sa_family == AF_INET ||
dst_addr->sa_family == AF_INET6);
if (dst_addr->sa_family == AF_INET) {
dst = ((struct sockaddr_in *)dst_addr)->sin_addr.s_addr;
} else {
/*
* We dont have support for V6 yet. It will be provided
* once RFE 6399103 has been delivered.
* Until then, for V6 dsts, IP Filter will not call
* this function. Instead the netinfo framework provides
* its own code path, in ip_inject_impl(), to achieve
* what it needs to do, for the time being.
*/
ip1dbg(("ipfil_sendpkt: no V6 support \n"));
value = ECOMM;
freemsg(mp);
goto discard;
}
/*
* Lets get the ire. We might get the ire cache entry,
* or the ire,sire pair needed to create the cache entry.
* XXX pass NULL tsl for now.
*/
if (ifindex == 0) {
/* There is no supplied index. So use the FIB info */
match_flags = (MATCH_IRE_DSTONLY | MATCH_IRE_DEFAULT |
MATCH_IRE_RECURSIVE | MATCH_IRE_RJ_BHOLE);
ire = ire_route_lookup(dst,
0, 0, 0, NULL, &sire, zoneid, MBLK_GETLABEL(mp),
match_flags, ipst);
} else {
ipif_t *supplied_ipif;
ill_t *ill;
match_flags = (MATCH_IRE_DSTONLY | MATCH_IRE_DEFAULT |
MATCH_IRE_RECURSIVE| MATCH_IRE_RJ_BHOLE|
MATCH_IRE_SECATTR);
/*
* If supplied ifindex is non-null, the only valid
* nexthop is one off of the interface or group corresponding
* to the specified ifindex.
*/
ill = ill_lookup_on_ifindex(ifindex, B_FALSE,
NULL, NULL, NULL, NULL, ipst);
if (ill != NULL) {
match_flags |= MATCH_IRE_ILL;
} else {
/* Fallback to group names if hook_emulation set */
if (ipst->ips_ipmp_hook_emulation) {
ill = ill_group_lookup_on_ifindex(ifindex,
B_FALSE, ipst);
}
if (ill == NULL) {
ip1dbg(("ipfil_sendpkt: Could not find"
" route to dst\n"));
value = ECOMM;
freemsg(mp);
goto discard;
}
match_flags |= MATCH_IRE_ILL_GROUP;
}
supplied_ipif = ipif_get_next_ipif(NULL, ill);
ire = ire_route_lookup(dst, 0, 0, 0, supplied_ipif,
&sire, zoneid, MBLK_GETLABEL(mp), match_flags, ipst);
ipif_refrele(supplied_ipif);
ill_refrele(ill);
}
/*
* Verify that the returned IRE is non-null and does
* not have either the RTF_REJECT or RTF_BLACKHOLE
* flags set and that the IRE is either an IRE_CACHE,
* IRE_IF_NORESOLVER or IRE_IF_RESOLVER.
*/
if (ire == NULL ||
((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) ||
(ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0)) {
/*
* Either ire could not be found or we got
* an invalid one
*/
ip1dbg(("ipfil_sendpkt: Could not find route to dst\n"));
value = ENONET;
freemsg(mp);
goto discard;
}
/* IP Filter and CGTP dont mix. So bail out if CGTP is on */
if (ip_cgtp_filter &&
((ire->ire_flags & RTF_MULTIRT) ||
((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)))) {
ip1dbg(("ipfil_sendpkt: IPFilter does not work with CGTP\n"));
value = ECOMM;
freemsg(mp);
goto discard;
}
ASSERT(ire->ire_type != IRE_CACHE || ire->ire_nce != NULL);
/*
* If needed, we will create the ire cache entry for the
* nexthop, resolve its link-layer address and then send
* the packet out without ttl, checksumming, IPSec processing.
*/
switch (ire->ire_type) {
case IRE_IF_NORESOLVER:
case IRE_CACHE:
if (sire != NULL) {
UPDATE_OB_PKT_COUNT(sire);
sire->ire_last_used_time = lbolt;
ire_refrele(sire);
}
ire_cache = ire;
break;
case IRE_IF_RESOLVER:
/*
* Call ire_forward(). This function
* will, create the ire cache entry of the
* the nexthop and adds this incomplete ire
* to the ire cache table
*/
ire_cache = ire_forward(dst, &check_multirt, ire, sire,
MBLK_GETLABEL(mp), ipst);
if (ire_cache == NULL) {
ip1dbg(("ipfil_sendpkt: failed to create the"
" ire cache entry \n"));
value = ENONET;
freemsg(mp);
sire = NULL;
ire = NULL;
goto discard;
}
break;
}
/*
* Now that we have the ire cache entry of the nexthop, call
* ip_xmit_v4() to trigger mac addr resolution
* if necessary and send it once ready.
*/
value = ip_xmit_v4(mp, ire_cache, NULL, B_FALSE);
ire_refrele(ire_cache);
/*
* At this point, the reference for these have already been
* released within ire_forward() and/or ip_xmit_v4(). So we set
* them to NULL to make sure we dont drop the references
* again in case ip_xmit_v4() returns with either SEND_FAILED
* or LLHDR_RESLV_FAILED
*/
sire = NULL;
ire = NULL;
switch (value) {
case SEND_FAILED:
ip1dbg(("ipfil_sendpkt: Send failed\n"));
value = ECOMM;
break;
case LLHDR_RESLV_FAILED:
ip1dbg(("ipfil_sendpkt: Link-layer resolution"
" failed\n"));
value = ECOMM;
break;
case LOOKUP_IN_PROGRESS:
netstack_rele(ns);
return (EINPROGRESS);
case SEND_PASSED:
netstack_rele(ns);
return (0);
}
discard:
if (dst_addr->sa_family == AF_INET) {
BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
} else {
BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsOutDiscards);
}
if (ire != NULL)
ire_refrele(ire);
if (sire != NULL)
ire_refrele(sire);
netstack_rele(ns);
return (value);
}
/* ire_walk routine invoked for ip_ire_report for each IRE. */
void
ire_report_ftable(ire_t *ire, char *m)
{
char buf1[16];
char buf2[16];
char buf3[16];
char buf4[16];
uint_t fo_pkt_count;
uint_t ib_pkt_count;
int ref;
uint_t print_len, buf_len;
mblk_t *mp = (mblk_t *)m;
if (ire->ire_type & IRE_CACHETABLE)
return;
buf_len = mp->b_datap->db_lim - mp->b_wptr;
if (buf_len <= 0)
return;
/* Number of active references of this ire */
ref = ire->ire_refcnt;
/* "inbound" to a non local address is a forward */
ib_pkt_count = ire->ire_ib_pkt_count;
fo_pkt_count = 0;
if (!(ire->ire_type & (IRE_LOCAL|IRE_BROADCAST))) {
fo_pkt_count = ib_pkt_count;
ib_pkt_count = 0;
}
print_len = snprintf((char *)mp->b_wptr, buf_len,
MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR "%5d "
"%s %s %s %s %05d %05ld %06ld %08d %03d %06d %09d %09d %06d %08d "
"%04d %08d %08d %d/%d/%d %s\n",
(void *)ire, (void *)ire->ire_rfq, (void *)ire->ire_stq,
(int)ire->ire_zoneid,
ip_dot_addr(ire->ire_addr, buf1), ip_dot_addr(ire->ire_mask, buf2),
ip_dot_addr(ire->ire_src_addr, buf3),
ip_dot_addr(ire->ire_gateway_addr, buf4),
ire->ire_max_frag, ire->ire_uinfo.iulp_rtt,
ire->ire_uinfo.iulp_rtt_sd,
ire->ire_uinfo.iulp_ssthresh, ref,
ire->ire_uinfo.iulp_rtomax,
(ire->ire_uinfo.iulp_tstamp_ok ? 1: 0),
(ire->ire_uinfo.iulp_wscale_ok ? 1: 0),
(ire->ire_uinfo.iulp_ecn_ok ? 1: 0),
(ire->ire_uinfo.iulp_pmtud_ok ? 1: 0),
ire->ire_uinfo.iulp_sack,
ire->ire_uinfo.iulp_spipe, ire->ire_uinfo.iulp_rpipe,
ib_pkt_count, ire->ire_ob_pkt_count, fo_pkt_count,
ip_nv_lookup(ire_nv_tbl, (int)ire->ire_type));
if (print_len < buf_len) {
mp->b_wptr += print_len;
} else {
mp->b_wptr += buf_len;
}
}
/*
* callback function provided by ire_ftable_lookup when calling
* rn_match_args(). Invoke ire_match_args on each matching leaf node in
* the radix tree.
*/
boolean_t
ire_find_best_route(struct radix_node *rn, void *arg)
{
struct rt_entry *rt = (struct rt_entry *)rn;
irb_t *irb_ptr;
ire_t *ire;
ire_ftable_args_t *margs = arg;
ipaddr_t match_mask;
ASSERT(rt != NULL);
irb_ptr = &rt->rt_irb;
if (irb_ptr->irb_ire_cnt == 0)
return (B_FALSE);
rw_enter(&irb_ptr->irb_lock, RW_READER);
for (ire = irb_ptr->irb_ire; ire != NULL; ire = ire->ire_next) {
if (ire->ire_marks & IRE_MARK_CONDEMNED)
continue;
if (margs->ift_flags & MATCH_IRE_MASK)
match_mask = margs->ift_mask;
else
match_mask = ire->ire_mask;
if (ire_match_args(ire, margs->ift_addr, match_mask,
margs->ift_gateway, margs->ift_type, margs->ift_ipif,
margs->ift_zoneid, margs->ift_ihandle, margs->ift_tsl,
margs->ift_flags)) {
IRE_REFHOLD(ire);
rw_exit(&irb_ptr->irb_lock);
margs->ift_best_ire = ire;
return (B_TRUE);
}
}
rw_exit(&irb_ptr->irb_lock);
return (B_FALSE);
}
/*
* ftable irb_t structures are dynamically allocated, and we need to
* check if the irb_t (and associated ftable tree attachment) needs to
* be cleaned up when the irb_refcnt goes to 0. The conditions that need
* be verified are:
* - no other walkers of the irebucket, i.e., quiescent irb_refcnt,
* - no other threads holding references to ire's in the bucket,
* i.e., irb_nire == 0
* - no active ire's in the bucket, i.e., irb_ire_cnt == 0
* - need to hold the global tree lock and irb_lock in write mode.
*/
void
irb_refrele_ftable(irb_t *irb)
{
for (;;) {
rw_enter(&irb->irb_lock, RW_WRITER);
ASSERT(irb->irb_refcnt != 0);
if (irb->irb_refcnt != 1) {
/*
* Someone has a reference to this radix node
* or there is some bucket walker.
*/
irb->irb_refcnt--;
rw_exit(&irb->irb_lock);
return;
} else {
/*
* There is no other walker, nor is there any
* other thread that holds a direct ref to this
* radix node. Do the clean up if needed. Call
* to ire_unlink will clear the IRB_MARK_CONDEMNED flag
*/
if (irb->irb_marks & IRB_MARK_CONDEMNED) {
ire_t *ire_list;
ire_list = ire_unlink(irb);
rw_exit(&irb->irb_lock);
if (ire_list != NULL)
ire_cleanup(ire_list);
/*
* more CONDEMNED entries could have
* been added while we dropped the lock,
* so we have to re-check.
*/
continue;
}
/*
* Now check if there are still any ires
* associated with this radix node.
*/
if (irb->irb_nire != 0) {
/*
* someone is still holding on
* to ires in this bucket
*/
irb->irb_refcnt--;
rw_exit(&irb->irb_lock);
return;
} else {
/*
* Everything is clear. Zero walkers,
* Zero threads with a ref to this
* radix node, Zero ires associated with
* this radix node. Due to lock order,
* check the above conditions again
* after grabbing all locks in the right order
*/
rw_exit(&irb->irb_lock);
if (irb_inactive(irb))
return;
/*
* irb_inactive could not free the irb.
* See if there are any walkers, if not
* try to clean up again.
*/
}
}
}
}
/*
* IRE iterator used by ire_ftable_lookup() to process multiple default
* routes. Given a starting point in the hash list (ire_origin), walk the IREs
* in the bucket skipping default interface routes and deleted entries.
* Returns the next IRE (unheld), or NULL when we're back to the starting point.
* Assumes that the caller holds a reference on the IRE bucket.
*
* In the absence of good IRE_DEFAULT routes, this function will return
* the first IRE_INTERFACE route found (if any).
*/
ire_t *
ire_round_robin(irb_t *irb_ptr, zoneid_t zoneid, ire_ftable_args_t *margs,
ip_stack_t *ipst)
{
ire_t *ire_origin;
ire_t *ire, *maybe_ire = NULL;
rw_enter(&irb_ptr->irb_lock, RW_WRITER);
ire_origin = irb_ptr->irb_rr_origin;
if (ire_origin != NULL) {
ire_origin = ire_origin->ire_next;
IRE_FIND_NEXT_ORIGIN(ire_origin);
}
if (ire_origin == NULL) {
/*
* first time through routine, or we dropped off the end
* of list.
*/
ire_origin = irb_ptr->irb_ire;
IRE_FIND_NEXT_ORIGIN(ire_origin);
}
irb_ptr->irb_rr_origin = ire_origin;
IRB_REFHOLD_LOCKED(irb_ptr);
rw_exit(&irb_ptr->irb_lock);
DTRACE_PROBE2(ire__rr__origin, (irb_t *), irb_ptr,
(ire_t *), ire_origin);
/*
* Round-robin the routers list looking for a route that
* matches the passed in parameters.
* We start with the ire we found above and we walk the hash
* list until we're back where we started. It doesn't matter if
* routes are added or deleted by other threads - we know this
* ire will stay in the list because we hold a reference on the
* ire bucket.
*/
ire = ire_origin;
while (ire != NULL) {
int match_flags = 0;
ire_t *rire;
if (ire->ire_marks & IRE_MARK_CONDEMNED)
goto next_ire;
if (!ire_match_args(ire, margs->ift_addr, (ipaddr_t)0,
margs->ift_gateway, margs->ift_type, margs->ift_ipif,
margs->ift_zoneid, margs->ift_ihandle, margs->ift_tsl,
margs->ift_flags))
goto next_ire;
if (ire->ire_type & IRE_INTERFACE) {
/*
* keep looking to see if there is a non-interface
* default ire, but save this one as a last resort.
*/
if (maybe_ire == NULL)
maybe_ire = ire;
goto next_ire;
}
if (zoneid == ALL_ZONES) {
IRE_REFHOLD(ire);
IRB_REFRELE(irb_ptr);
return (ire);
}
/*
* When we're in a local zone, we're only
* interested in routers that are
* reachable through ipifs within our zone.
*/
if (ire->ire_ipif != NULL) {
match_flags |= MATCH_IRE_ILL_GROUP;
}
rire = ire_route_lookup(ire->ire_gateway_addr,
0, 0, 0, ire->ire_ipif, NULL, zoneid, margs->ift_tsl,
match_flags, ipst);
if (rire != NULL) {
ire_refrele(rire);
IRE_REFHOLD(ire);
IRB_REFRELE(irb_ptr);
return (ire);
}
next_ire:
ire = (ire->ire_next ? ire->ire_next : irb_ptr->irb_ire);
if (ire == ire_origin)
break;
}
if (maybe_ire != NULL)
IRE_REFHOLD(maybe_ire);
IRB_REFRELE(irb_ptr);
return (maybe_ire);
}
void
irb_refhold_rn(struct radix_node *rn)
{
if ((rn->rn_flags & RNF_ROOT) == 0)
IRB_REFHOLD(&((rt_t *)(rn))->rt_irb);
}
void
irb_refrele_rn(struct radix_node *rn)
{
if ((rn->rn_flags & RNF_ROOT) == 0)
irb_refrele_ftable(&((rt_t *)(rn))->rt_irb);
}
|