summaryrefslogtreecommitdiff
path: root/usr/iface.c
blob: 5d5f7bf4c5e62afa98c38412d5d72e6501ff15ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
/*
 * iSCSI iface helpers
 *
 * Copyright (C) 2008 Mike Christie
 * Copyright (C) 2008 Red Hat, Inc. All rights reserved.
 * maintained by open-iscsi@@googlegroups.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * See the file COPYING included with this distribution for more details.
 */
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <arpa/inet.h>

#include "log.h"
#include "list.h"
#include "iscsi_sysfs.h"
#include "iscsi_settings.h"
#include "config.h"
#include "transport.h"
#include "idbm.h"
#include "iface.h"
#include "session_info.h"
#include "host.h"
#include "fw_context.h"
#include "sysdeps.h"
#include "iscsi_err.h"

/*
 * Default ifaces for use with transports that do not bind to hardware
 * by defaults (transports that let the interconnect layer to the routing
 * by defaults).
 */

/*
 * iSCSI over TCP/IP
 */
static struct iface_rec iface_default = {
	.name		= "default",
	.transport_name	= "tcp",
};

/*
 * iSER
 */
static struct iface_rec iface_iser = {
	.name		= "iser",
	.transport_name	= "iser",
};

static struct iface_rec *default_ifaces[] = {
	&iface_default,
	&iface_iser,
	NULL,
};

static struct iface_rec *iface_match_default(struct iface_rec *iface)
{
	struct iface_rec *def_iface;
	int i = 0;

	while ((def_iface = default_ifaces[i++])) {
		if (!strcmp(iface->name, def_iface->name))
			return def_iface;
	}
	return NULL;
}

static void iface_init(struct iface_rec *iface)
{
	if (!strlen(iface->name))
		sprintf(iface->name, DEFAULT_IFACENAME);
}

/*
 * default is to use tcp through whatever the network layer
 * selects for us with the /etc/iscsi/initiatorname.iscsi iname.
 */
void iface_setup_defaults(struct iface_rec *iface)
{
	sprintf(iface->transport_name, DEFAULT_TRANSPORT);
	iface_init(iface);
}

struct iface_rec *iface_alloc(char *ifname, int *err)
{
	struct iface_rec *iface;

	if (!strlen(ifname) || strlen(ifname) + 1 > ISCSI_MAX_IFACE_LEN) {
		*err = ISCSI_ERR_INVAL;
		return NULL;
	}

	iface = calloc(1, sizeof(*iface));
	if (!iface) {
		*err = ISCSI_ERR_NOMEM;
		return NULL;
	}

	strlcpy(iface->name, ifname, ISCSI_MAX_IFACE_LEN);
	INIT_LIST_HEAD(&iface->list);
	return iface;
}

static int __iface_conf_read(struct iface_rec *iface)
{
	char *iface_conf;
	recinfo_t *info;
	FILE *f;
	int rc = 0;

	iface_conf = calloc(1, PATH_MAX);
	if (!iface_conf)
		return ISCSI_ERR_NOMEM;

	info = idbm_recinfo_alloc(MAX_KEYS);
	if (!info) {
		rc = ISCSI_ERR_NOMEM;
		goto free_conf;
	}

	snprintf(iface_conf, PATH_MAX, "%s/%s", IFACE_CONFIG_DIR,
		 iface->name);

	log_debug(5, "looking for iface conf %s", iface_conf);
	f = fopen(iface_conf, "r");
	if (!f) {
		/*
		 * if someone passes in default but has not defined
		 * a iface with default then we do it for them
		 */
		if (!strcmp(iface->name, DEFAULT_IFACENAME)) {
			iface_setup_defaults(iface);
			rc = 0;
		} else
			rc = ISCSI_ERR_IDBM;
		goto free_info;
	}

	iface_init(iface);
	idbm_recinfo_iface(iface, info);
	idbm_recinfo_config(info, f);
	fclose(f);

free_info:
	free(info);
free_conf:
	free(iface_conf);
	return rc;
}

int iface_conf_read(struct iface_rec *iface)
{
	struct iface_rec *def_iface;
	int rc;

	def_iface = iface_match_default(iface);
	if (def_iface) {
		/*
		 * older tools allowed default to have different
		 * transport_names so we do not want to overwrite
		 * it.
		 */
		if (!strcmp(def_iface->name, DEFAULT_IFACENAME)) {
			if (!strlen(iface->name))
				strcpy(iface->name, def_iface->name);
			if (!strlen(iface->netdev))
				strcpy(iface->netdev, def_iface->netdev);
			if (!strlen(iface->hwaddress))
				strcpy(iface->hwaddress, def_iface->hwaddress);
			if (!strlen(iface->transport_name))
				strcpy(iface->transport_name,
				       def_iface->transport_name);
			if (!strlen(iface->iname))
				strcpy(iface->iname, def_iface->iname);
		} else {
			iface_init(iface);
			iface_copy(iface, def_iface);
		}
		return 0;
	}

	rc = idbm_lock();
	if (rc)
		return rc;

	rc = __iface_conf_read(iface);
	idbm_unlock();
	return rc;
}

int iface_conf_delete(struct iface_rec *iface)
{
	struct iface_rec *def_iface;
	char *iface_conf;
	int rc = 0;

	def_iface = iface_match_default(iface);
	if (def_iface) {
		log_error("iface %s is a special interface and "
			  "cannot be deleted.\n", iface->name);
		return ISCSI_ERR_INVAL;
	}

	iface_conf = calloc(1, PATH_MAX);
	if (!iface_conf)
		return ISCSI_ERR_NOMEM;

	sprintf(iface_conf, "%s/%s", IFACE_CONFIG_DIR, iface->name);
	rc = idbm_lock();
	if (rc)
		goto free_conf;

	if (unlink(iface_conf))
		rc = ISCSI_ERR_IDBM;
	idbm_unlock();

free_conf:
	free(iface_conf);
	return rc;
}

int iface_conf_write(struct iface_rec *iface)
{
	struct iface_rec *def_iface;
	char *iface_conf;
	FILE *f;
	int rc = 0;

	def_iface = iface_match_default(iface);
	if (def_iface) {
		log_error("iface %s is a special interface and "
			  "is not stored in %s.\n", iface->name,
			  IFACE_CONFIG_DIR);
		return ISCSI_ERR_INVAL;
	}

	iface_conf = calloc(1, PATH_MAX);
	if (!iface_conf)
		return ISCSI_ERR_NOMEM;

	sprintf(iface_conf, "%s/%s", IFACE_CONFIG_DIR, iface->name);
	f = fopen(iface_conf, "w");
	if (!f) {
		rc = ISCSI_ERR_IDBM;
		goto free_conf;
	}

	rc = idbm_lock();
	if (rc)
		goto close_f;

	idbm_print(IDBM_PRINT_TYPE_IFACE, iface, 1, f);
	idbm_unlock();

close_f:
	fclose(f);
free_conf:
	free(iface_conf);
	return rc;
}

int iface_conf_update(struct db_set_param *param,
		       struct iface_rec *iface)
{
	struct iface_rec *def_iface;
	recinfo_t *info;
	int rc = 0;

	def_iface = iface_match_default(iface);
	if (def_iface) {
		log_error("iface %s is a special interface and "
			  "cannot be modified.\n", iface->name);
		return ISCSI_ERR_INVAL;
	}

	info = idbm_recinfo_alloc(MAX_KEYS);
	if (!info)
		return ISCSI_ERR_NOMEM;

	idbm_recinfo_iface(iface, info);
	rc = idbm_verify_param(info, param->name);
	if (rc)
		goto free_info;

	rc = idbm_rec_update_param(info, param->name, param->value, 0);
	if (rc)
		goto free_info;

	rc = iface_conf_write(iface);
free_info:
	free(info);
	return rc;
}

#if 0 /* Unused */
static int iface_get_next_id(void)
{
	struct stat statb;
	char *iface_conf;
	int i, rc = ENOSPC;

	iface_conf = calloc(1, PATH_MAX);
	if (!iface_conf)
		return ENOMEM;

	for (i = 0; i < INT_MAX; i++) {
		memset(iface_conf, 0, PATH_MAX);
		/* check len */
		snprintf(iface_conf, PATH_MAX, "iface%d", i);
		if (strlen(iface_conf) > ISCSI_MAX_IFACE_LEN - 1) {
			log_error("iface namespace is full. Remove unused "
				  "iface definitions from %s or send mail "
				  "to open-iscsi@googlegroups.com to report "
				  "the problem", IFACE_CONFIG_DIR);
			rc = ENOSPC;
			break;
		}
		memset(iface_conf, 0, PATH_MAX);
		snprintf(iface_conf, PATH_MAX, "%s/iface%d", IFACE_CONFIG_DIR,
			i);

		if (!stat(iface_conf, &statb))
			continue;
		if (errno == ENOENT) {
			rc = i;
			break;
		}
	}

	free(iface_conf);
        return rc;
}
#endif /* Unused */

struct iface_search {
	struct iface_rec *pattern;
	struct iface_rec *found;
};

static int __iface_get_by_net_binding(void *data, struct iface_rec *iface)
{
	struct iface_search *search = data;

	if (!strcmp(search->pattern->name, iface->name)) {
		iface_copy(search->found, iface);
		return 1;
	}

	if (iface_is_bound_by_hwaddr(search->pattern)) {
		if (!strcasecmp(iface->hwaddress,
				search->pattern->hwaddress)) {
			iface_copy(search->found, iface);
			return 1;
		} else
			return 0;
	}

	if (iface_is_bound_by_netdev(search->pattern)) {
		if (!strcmp(iface->netdev, search->pattern->netdev)) {
			iface_copy(search->found, iface);
			return 1;
		} else
			return 0;
	}

/*
	if (iface_is_bound_by_ipaddr(search->pattern)) {
		if (!strcmp(iface->ipaddress, search->pattern->ipaddress)) {
			iface_copy(search->found, iface);
			return 1;
		} else
			return 0;
	}
*/
	return 0;
}

/*
 * Before 2.0.870, we only could bind by netdeivce or hwaddress,
 * so we did a simple reverse lookup to go from sysfs info to
 * the iface name. After 2.0.870 we added a lot of options to the
 * iface binding so we added the ifacename to the kernel.
 *
 * This function is for older kernels that do not export the ifacename.
 * If the user was doing iscsi_tcp session binding we will find
 * the iface by matching net info.
 */
int iface_get_by_net_binding(struct iface_rec *pattern,
			     struct iface_rec *out_rec)
{
	int num_found = 0, rc;
	struct iface_search search;

	if (!iface_is_bound_by_hwaddr(pattern) &&
	    !iface_is_bound_by_netdev(pattern)) {
		sprintf(out_rec->name, DEFAULT_IFACENAME);
		return 0;
	}

	search.pattern = pattern;
	search.found = out_rec;

	rc = iface_for_each_iface(&search, 0, &num_found,
				  __iface_get_by_net_binding);
	if (rc == 1)
		return 0;
	return ISCSI_ERR_NO_OBJS_FOUND;
}

static int __iface_setup_host_bindings(void *data, struct host_info *hinfo)
{
	struct iface_rec *def_iface;
	struct iface_rec iface;
	struct iscsi_transport *t;
	int i = 0;

	t = iscsi_sysfs_get_transport_by_hba(hinfo->host_no);
	if (!t)
		return 0;

	/* do not setup binding for hosts using non offload drivers */
	while ((def_iface = default_ifaces[i++])) {
		if (!strcmp(t->name, def_iface->transport_name))
			return 0;
	}

	if (iface_get_by_net_binding(&hinfo->iface, &iface) ==
	    ISCSI_ERR_NO_OBJS_FOUND) {
		/* Must be a new port */
		if (!strlen(hinfo->iface.hwaddress)) {
			log_error("Invalid offload iSCSI host %u. Missing "
				  "hwaddress. Try upgrading %s driver.\n",
				  hinfo->host_no, t->name);
			return 0;
		}

		memset(&iface, 0, sizeof(struct iface_rec));
		strcpy(iface.hwaddress, hinfo->iface.hwaddress);
		strcpy(iface.transport_name, hinfo->iface.transport_name);
		snprintf(iface.name, sizeof(iface.name), "%s.%s",
			 t->name, hinfo->iface.hwaddress);
		if (iface_conf_write(&iface))
			log_error("Could not create default iface conf %s.",
				  iface.name);
			/* fall through - will not be persistent */
	}
	return 0;
}

/*
 * Create a default iface for offload cards. We assume that we will
 * be able identify each host by MAC.
 */
void iface_setup_host_bindings(void)
{
	int nr_found = 0;

	if (idbm_lock())
		return;

	if (access(IFACE_CONFIG_DIR, F_OK) != 0) {
		if (mkdir(IFACE_CONFIG_DIR, 0660) != 0) {
			log_error("Could not make %s. HW/OFFLOAD iscsi "
				  "may not be supported", IFACE_CONFIG_DIR);
			idbm_unlock();
			return;
		}
	}
	idbm_unlock();

	if (iscsi_sysfs_for_each_host(NULL, &nr_found,
				      __iface_setup_host_bindings))
		log_error("Could not scan scsi hosts. HW/OFFLOAD iscsi "
			  "operations may not be supported, or please "
			  "see README for instructions on setting up ifaces.");
}

void iface_copy(struct iface_rec *dst, struct iface_rec *src)
{
	if (strlen(src->name))
		strcpy(dst->name, src->name);
	if (strlen(src->netdev))
		strcpy(dst->netdev, src->netdev);
	if (strlen(src->ipaddress))
		strcpy(dst->ipaddress, src->ipaddress);
	if (strlen(src->hwaddress))
		strcpy(dst->hwaddress, src->hwaddress);
	if (strlen(src->transport_name))
		strcpy(dst->transport_name, src->transport_name);
	if (strlen(src->iname))
		strcpy(dst->iname, src->iname);
}

int iface_is_valid(struct iface_rec *iface)
{
	if (!iface)
		return 0;

	if (!strlen(iface->name))
		return 0;

	if (!strlen(iface->transport_name))
		return 0;

	if (iface_is_bound_by_hwaddr(iface))
		return 1;

	if (iface_is_bound_by_netdev(iface))
		return 1;
//	if (iface_is_bound_by_ipaddr(iface))
//		return 1;

	/* bound by transport name */
	return 1;
}

int iface_match(struct iface_rec *pattern, struct iface_rec *iface)
{
	if (!pattern || !iface)
		return 1;

	if (!strlen(pattern->name))
		return 1;

	if (!strcmp(pattern->name, iface->name)) {
		if (strcmp(pattern->name, DEFAULT_IFACENAME))
			return 1;
		/*
		 * For default we allow the same name, but different
		 * transports.
		 */
		if (!strlen(pattern->transport_name))
			return 1;

		if (!strcmp(pattern->transport_name, iface->transport_name))
			return 1;
		/* fall through */
	}
	return 0;
}

int iface_is_bound_by_hwaddr(struct iface_rec *iface)
{
	if (iface && strlen(iface->hwaddress) &&
	    strcmp(iface->hwaddress, DEFAULT_HWADDRESS))
		return 1;
	return 0;
}

int iface_is_bound_by_netdev(struct iface_rec *iface)
{
	if (iface && strlen(iface->netdev) &&
	   strcmp(iface->netdev, DEFAULT_NETDEV))
		return 1;
	return 0;
}

int iface_is_bound_by_ipaddr(struct iface_rec *iface)
{
	if (iface && strlen(iface->ipaddress) &&
	   strcmp(iface->ipaddress, DEFAULT_IPADDRESS))
		return 1;
	return 0;
}

void iface_print(struct iface_rec *iface, char *prefix)
{
	if (strlen(iface->name))
		printf("%sIface Name: %s\n", prefix, iface->name);
	else
		printf("%sIface Name: %s\n", prefix, UNKNOWN_VALUE);

	if (strlen(iface->transport_name))
		printf("%sIface Transport: %s\n", prefix,
		      iface->transport_name);
	else
		printf("%sIface Transport: %s\n", prefix, UNKNOWN_VALUE);

	if (strlen(iface->iname))
		printf("%sIface Initiatorname: %s\n", prefix, iface->iname);
	else
		printf("%sIface Initiatorname: %s\n", prefix, UNKNOWN_VALUE);

	if (!strlen(iface->ipaddress))
		printf("%sIface IPaddress: %s\n", prefix, UNKNOWN_VALUE);
	else if (strchr(iface->ipaddress, '.'))
		printf("%sIface IPaddress: %s\n", prefix, iface->ipaddress);
	else
		printf("%sIface IPaddress: [%s]\n", prefix, iface->ipaddress);

	if (strlen(iface->hwaddress))
		printf("%sIface HWaddress: %s\n", prefix, iface->hwaddress);
	else
		printf("%sIface HWaddress: %s\n", prefix, UNKNOWN_VALUE);

	if (strlen(iface->netdev))
		printf("%sIface Netdev: %s\n", prefix, iface->netdev);
	else
		printf("%sIface Netdev: %s\n", prefix, UNKNOWN_VALUE);
}

struct iface_print_node_data {
	struct node_rec *last_rec;
	struct iface_rec *match_iface;
};

static int iface_print_nodes(void *data, node_rec_t *rec)
{
	struct iface_print_node_data *print_data = data;

	if (!iface_match(print_data->match_iface, &rec->iface))
		return -1;

	idbm_print_node_tree(print_data->last_rec, rec, "\t");
	return 0;
}

/**
 * iface_print_tree - print out binding info
 * @iface: iface to print out
 *
 * Currently this looks like the iface conf print, because we only
 * have the binding info. When we store the iface specific node settings
 * in the iface record then it will look different.
 */
int iface_print_tree(void *data, struct iface_rec *iface)
{
	struct node_rec last_rec;
	struct iface_print_node_data print_data;
	int num_found = 0;

	printf("Iface: %s\n", iface->name);

	memset(&last_rec, 0, sizeof(struct node_rec ));

	print_data.match_iface = iface;
	print_data.last_rec = &last_rec;

	idbm_for_each_rec(&num_found, &print_data, iface_print_nodes);
	return 0;
}

int iface_print_flat(void *data, struct iface_rec *iface)
{
	printf("%s %s,%s,%s,%s,%s\n",
		strlen(iface->name) ? iface->name : UNKNOWN_VALUE,
		strlen(iface->transport_name) ? iface->transport_name :
							UNKNOWN_VALUE,
		strlen(iface->hwaddress) ? iface->hwaddress : UNKNOWN_VALUE,
		strlen(iface->ipaddress) ? iface->ipaddress : UNKNOWN_VALUE,
		strlen(iface->netdev) ? iface->netdev : UNKNOWN_VALUE,
		strlen(iface->iname) ? iface->iname : UNKNOWN_VALUE);
	return 0;
}

int iface_for_each_iface(void *data, int skip_def, int *nr_found,
			 iface_op_fn *fn)
{
	DIR *iface_dirfd;
	struct dirent *iface_dent;
	struct iface_rec *iface, *def_iface;
	int err = 0, i = 0;

	if (!skip_def) {
		while ((def_iface = default_ifaces[i++])) {
			iface = iface_alloc(def_iface->name, &err);
			if (!iface) {
				log_error("Could not add iface %s.",
					  def_iface->name);
				continue;
			}
			iface_copy(iface, def_iface);
			err = fn(data, iface);
			free(iface);
			if (err)
				return err;
			(*nr_found)++;
		}
	}

	iface_dirfd = opendir(IFACE_CONFIG_DIR);
	if (!iface_dirfd)
		return errno;

	while ((iface_dent = readdir(iface_dirfd))) {
		if (!strcmp(iface_dent->d_name, ".") ||
		    !strcmp(iface_dent->d_name, ".."))
			continue;

		log_debug(5, "iface_for_each_iface found %s",
			 iface_dent->d_name);
		iface = iface_alloc(iface_dent->d_name, &err);
		if (!iface || err) {
			if (err == ISCSI_ERR_INVAL)
				log_error("Invalid iface name %s. Must be "
					  "from 1 to %d characters.",
					   iface_dent->d_name,
					   ISCSI_MAX_IFACE_LEN - 1);
			else
				log_error("Could not add iface %s.",
					  iface_dent->d_name);
			continue;
		}

		err = idbm_lock();
		if (err) {
			free(iface);
			continue;
		}

		err = __iface_conf_read(iface);
		idbm_unlock();
		if (err) {
			log_error("Could not read def iface %s (err %d)",
				  iface->name, err);
			free(iface);
			continue;
		}

		if (!iface_is_valid(iface)) {
			log_debug(5, "iface is not valid "
				  "Iface settings " iface_fmt,
				  iface_str(iface));
			free(iface);
			continue;
		}

		err = fn(data, iface);
		free(iface);
		if (err)
			break;
		(*nr_found)++;
	}

	closedir(iface_dirfd);
	return err;
}

static int iface_link(void *data, struct iface_rec *iface)
{
	struct list_head *ifaces = data;
	struct iface_rec *iface_copy;

	iface_copy = calloc(1, sizeof(*iface_copy));
	if (!iface_copy)
		return ISCSI_ERR_NOMEM;

	memcpy(iface_copy, iface, sizeof(*iface_copy));
	INIT_LIST_HEAD(&iface_copy->list);
	list_add_tail(&iface_copy->list, ifaces);
	return 0;
}

/**
 * iface_link_ifaces - link non default ifaces
 * @ifaces: list to add ifaces to
 *
 * This will return a list of the ifaces created by iscsiadm
 * or the user. It does not return the static default ones.
 */
void iface_link_ifaces(struct list_head *ifaces)
{
	int nr_found = 0;

	iface_for_each_iface(ifaces, 1, &nr_found, iface_link);
}

/**
 * iface_setup_from_boot_context - setup iface from boot context info
 * @iface: iface t setup
 * @context: boot context info
 *
 * Returns 1 if setup for offload.
 */
int iface_setup_from_boot_context(struct iface_rec *iface,
				   struct boot_context *context)
{
	struct iscsi_transport *t;
	uint32_t hostno;

	if (strlen(context->initiatorname))
		strlcpy(iface->iname, context->initiatorname,
			sizeof(iface->iname));

	if (strlen(context->scsi_host_name)) {
		if (sscanf(context->scsi_host_name, "iscsi_boot%u", &hostno) != 		    1) {
			log_error("Could not parse %s's host no.",
				  context->scsi_host_name);
			return 0;
		}
	} else if (strlen(context->iface)) {
/* this ifdef is only temp until distros and firmwares are updated */
#ifdef OFFLOAD_BOOT_SUPPORTED
		hostno = iscsi_sysfs_get_host_no_from_hwaddress(context->mac,
								&rc);
		if (rc) {
			/*
			 * If the MAC in the boot info does not match a iscsi
			 * host then the MAC must be for network card, so boot
			 * is not going to be offloaded.
			 */
			log_debug(3, "Could not match %s to host\n",
				  context->mac);
			return 0;
		}

		strlcpy(iface->netdev, context->iface, sizeof(iface->netdev));
#else
		return 0;
#endif
	} else
		return 0;

	/*
	 * set up for access through a offload card.
	 */
	t = iscsi_sysfs_get_transport_by_hba(hostno);
	if (!t) {
		log_error("Could not get transport for host%u. "
			  "Make sure the iSCSI driver is loaded.",
			  hostno);
		return 0;
	}
	strcpy(iface->transport_name, t->name);

	memset(iface->name, 0, sizeof(iface->name));
	snprintf(iface->name, sizeof(iface->name), "%s.%s",
		 iface->transport_name, context->mac);

	strlcpy(iface->hwaddress, context->mac,
		sizeof(iface->hwaddress));
	strlcpy(iface->ipaddress, context->ipaddr,
		sizeof(iface->ipaddress));
	log_debug(1, "iface " iface_fmt "\n", iface_str(iface));
	return 1;
}

/**
 * iface_create_ifaces_from_boot_contexts - create ifaces based on boot info
 * @ifaces: list to store ifaces in
 * @targets: list of targets to create ifaces from
 *
 * This function will create a iface struct based on the boot info
 * and it will create (or update if existing already) a iface rec in
 * the ifaces dir based on the info.
 */
int iface_create_ifaces_from_boot_contexts(struct list_head *ifaces,
					   struct list_head *targets)
{
	struct boot_context *context;
	struct iface_rec *iface, *tmp_iface;
	int rc = 0;

	list_for_each_entry(context, targets, list) {
		rc = 0;
		/* use dummy name. If valid it will get overwritten below */
		iface = iface_alloc(DEFAULT_IFACENAME, &rc);
		if (!iface) {
			log_error("Could not setup iface %s for boot\n",
				  context->iface);
			goto fail;
		}
		if (!iface_setup_from_boot_context(iface, context)) {
			/* no offload so forget it */
			free(iface);
			continue;
		}

		rc = iface_conf_write(iface);
		if (rc) {
			log_error("Could not setup default iface conf "
				  "for %s.", iface->name);
			free(iface);
			goto fail;
		}
		list_add_tail(&iface->list, ifaces);
	}

	return 0;
fail:
	list_for_each_entry_safe(iface, tmp_iface, ifaces, list) {
		list_del(&iface->list);
		free(iface);
	}
	return rc;
}

struct iface_param_count {
	struct iface_rec *primary;
	int count;
};

/**
 * __iface_get_param_count - Gets netconfig parameter count for given iface
 * @data: iface_param_count structure
 * @iface: iface to setup
 */
static int __iface_get_param_count(void *data, struct iface_rec *iface)
{
	struct iface_param_count *iface_params = data;
	int iptype = ISCSI_IFACE_TYPE_IPV4;
	int count = 0;

	if (strcmp(iface_params->primary->hwaddress, iface->hwaddress))
		return 0;

	if (strcmp(iface->bootproto, "dhcp") && !strstr(iface->ipaddress, "."))
		iptype = ISCSI_IFACE_TYPE_IPV6;

	if (iptype == ISCSI_IFACE_TYPE_IPV4) {

		if (strcmp(iface->state, "disable")) {
			if (strstr(iface->bootproto, "dhcp"))
				/* DHCP enabled */
				count++;
			else {
				/* DHCP disabled */
				count++;

				if (strstr(iface->ipaddress, ".")) {
					/* User configured IPv4 Address */
					count++;

					if (strstr(iface->subnet_mask, "."))
						/* User configured Subnet */
						count++;

					if (strstr(iface->gateway, "."))
						/* User configured Gateway */
						count++;
				} else
					/*
					 * IPv4 Address not valid, decrement
					 * count of DHCP
					 */
					count--;
			}

			/*
			 * If IPv4 configuration in iface file is valid,
			 * enable state and other parameters (if any)
			 */
			if (count) {
				/* iface state */
				count++;

				if (strcmp(iface->vlan_state, "disable")) {
					/* vlan_state enabled */
					count++;

					if (iface->vlan_id)
						/* For vlan value */
						count++;
				} else
					/* vlan_state disabled */
					count++;

				if (iface->mtu)
					count++;

				if (iface->port)
					count++;
			}
		} else
			/* IPv4 is disabled, iface state */
			count++;

	} else if (iptype == ISCSI_IFACE_TYPE_IPV6) {

		if (strcmp(iface->state, "disable")) {

			/* IPv6 Address */
			if (strstr(iface->ipv6_autocfg, "nd") ||
			    strstr(iface->ipv6_autocfg, "dhcpv6"))
				/* Autocfg enabled */
				count++;
			else {
				/* Autocfg disabled */
				count++;

				if (strstr(iface->ipaddress, ":"))
					/* User configured IPv6 Address */
					count++;
				else
					/*
					 * IPv6 Address not valid, decrement
					 * count of IPv6 Autocfg
					 */
					count--;
			}

			/* IPv6 LinkLocal Address */
			if (strstr(iface->linklocal_autocfg, "auto"))
				/* Autocfg enabled */
				count++;
			else {
				/* Autocfg disabled */
				count++;

				if (strstr(iface->ipv6_linklocal, ":"))
					/* User configured LinkLocal Address */
					count++;
				else
					/*
					 * LinkLocal Address not valid,
					 * decrement count of LinkLocal Autocfg
					 */
					count--;
			}

			/* IPv6 Router Address */
			if (strstr(iface->router_autocfg, "auto"))
				/* Autocfg enabled */
				count++;
			else {
				/* Autocfg disabled */
				count++;

				if (strstr(iface->ipv6_router, ":"))
					/* User configured Router Address */
					count++;
				else
					/*
					 * Router Address not valid,
					 * decrement count of Router Autocfg
					 */
					count--;
			}

			/*
			 * If IPv6 configuration in iface file is valid,
			 * enable state and other parameters (if any)
			 */
			if (count) {
				/* iface state */
				count++;

				if (strcmp(iface->vlan_state, "disable")) {
					/* vlan_state enabled */
					count++;

					if (iface->vlan_id)
						/* For vlan value */
						count++;
				} else
					/* vlan_state disabled */
					count++;

				if (iface->mtu)
					count++;

				if (iface->port)
					count++;
			}
		} else
			/* IPv6 is disabled, iface state */
			count++;
	}

	iface_params->count += count;
	return 0;
}

/**
 * iface_get_param_count - Gets netconfig parameter count from iface
 * @iface: iface to setup
 * @iface_all: Flag for number of ifaces to traverse (1 for all)
 *
 * Returns netconfig parameter count.
 */
int iface_get_param_count(struct iface_rec *iface, int iface_all)
{
	int num_found = 0, rc;
	struct iface_param_count iface_params;

	log_debug(8, "In iface_get_param_count\n");

	iface_params.primary = iface;
	iface_params.count = 0;

	if (iface_all)
		rc = iface_for_each_iface(&iface_params, 0, &num_found,
					  __iface_get_param_count);
	else
		rc = __iface_get_param_count(&iface_params, iface);

	log_debug(8, "iface_get_param_count: rc = %d, count = %d\n",
		  rc, iface_params.count);
	return iface_params.count;
}

/* IPv4/IPv6 Port: 3260 or User defined */
static int iface_fill_port(struct iovec *iov, struct iface_rec *iface,
			   uint32_t iface_type)
{
	int len;
	struct iscsi_iface_param_info *net_param;
	uint16_t port = 3260;

	len = sizeof(struct iscsi_iface_param_info) + 2;
	iov->iov_base = calloc(len, sizeof(char));
	if (!(iov->iov_base))
		return 1;

	iov->iov_len = len;
	net_param = (struct iscsi_iface_param_info *)(iov->iov_base);
	net_param->param = ISCSI_NET_PARAM_PORT;
	net_param->iface_type = iface_type;
	net_param->iface_num = iface->iface_num;
	net_param->param_type = ISCSI_NET_PARAM;
	net_param->len = 2;
	if (iface->port)
		port = iface->port;
	memcpy(net_param->value, &port, net_param->len);
	return 0;
}

static int iface_fill_mtu(struct iovec *iov, struct iface_rec *iface,
			  uint32_t iface_type)
{
	int len;
	struct iscsi_iface_param_info *net_param;
	uint16_t mtu = 0;

	len = sizeof(struct iscsi_iface_param_info) + 2;
	iov->iov_base = calloc(len, sizeof(char));
	if (!(iov->iov_base))
		return 1;

	iov->iov_len = len;
	net_param = (struct iscsi_iface_param_info *)(iov->iov_base);
	net_param->param = ISCSI_NET_PARAM_MTU;
	net_param->iface_type = iface_type;
	net_param->iface_num = iface->iface_num;
	net_param->param_type = ISCSI_NET_PARAM;
	net_param->len = 2;
	mtu = iface->mtu;
	memcpy(net_param->value, &mtu, net_param->len);
	return 0;
}

/* IPv4/IPv6 VLAN_ID: decimal value <= 4095 */
static int iface_fill_vlan_id(struct iovec *iov, struct iface_rec *iface,
			      uint32_t iface_type)
{
	int len;
	struct iscsi_iface_param_info *net_param;
	uint16_t vlan = 0;

	len = sizeof(struct iscsi_iface_param_info) + 2;
	iov->iov_base = calloc(len, sizeof(char));
	if (!(iov->iov_base))
		return 1;

	iov->iov_len = len;
	net_param = (struct iscsi_iface_param_info *)(iov->iov_base);
	net_param->param = ISCSI_NET_PARAM_VLAN_ID;
	net_param->iface_type = iface_type;
	net_param->iface_num = iface->iface_num;
	net_param->param_type = ISCSI_NET_PARAM;
	net_param->len = 2;
	if (iface->vlan_id <= ISCSI_MAX_VLAN_ID &&
	    iface->vlan_priority <= ISCSI_MAX_VLAN_PRIORITY)
		/*
		 * Bit 15-13: User Priority of VLAN
		 * Bit 11-00: VLAN ID
		 */
		vlan = (iface->vlan_priority << 13) |
		       (iface->vlan_id & ISCSI_MAX_VLAN_ID);
	memcpy(net_param->value, &vlan, net_param->len);
	return 0;
}

/* IPv4/IPv6 VLAN state: disable/enable */
static int iface_fill_vlan_state(struct iovec *iov, struct iface_rec *iface,
				 uint32_t iface_type)
{
	int len;
	struct iscsi_iface_param_info *net_param;

	len = sizeof(struct iscsi_iface_param_info) + 1;
	iov->iov_base = calloc(len, sizeof(char));
	if (!(iov->iov_base))
		return 1;

	iov->iov_len = len;
	net_param = (struct iscsi_iface_param_info *)(iov->iov_base);
	net_param->param = ISCSI_NET_PARAM_VLAN_ENABLED;
	net_param->iface_type = iface_type;
	net_param->iface_num = iface->iface_num;
	net_param->param_type = ISCSI_NET_PARAM;
	net_param->len = 1;
	if (strcmp(iface->vlan_state, "disable") && iface->vlan_id)
		net_param->value[0] = ISCSI_VLAN_ENABLE;
	else /* Assume disabled */
		net_param->value[0] = ISCSI_VLAN_DISABLE;
	return 0;
}

/* IPv4/IPv6 Network state: disable/enable */
static int iface_fill_net_state(struct iovec *iov, struct iface_rec *iface,
				uint32_t iface_type)
{
	int len;
	struct iscsi_iface_param_info *net_param;

	len = sizeof(struct iscsi_iface_param_info) + 1;
	iov->iov_base = calloc(len, sizeof(char));
	if (!(iov->iov_base))
		return 1;

	iov->iov_len = len;
	net_param = (struct iscsi_iface_param_info *)(iov->iov_base);
	net_param->param = ISCSI_NET_PARAM_IFACE_ENABLE;
	net_param->iface_type = iface_type;
	net_param->iface_num = iface->iface_num;
	net_param->param_type = ISCSI_NET_PARAM;
	net_param->len = 1;
	if (!strcmp(iface->state, "disable"))
		net_param->value[0] = ISCSI_IFACE_DISABLE;
	else /* Assume enabled */
		net_param->value[0] = ISCSI_IFACE_ENABLE;
	return 0;
}

/* IPv4 Bootproto: DHCP/static */
static int iface_fill_net_bootproto(struct iovec *iov, struct iface_rec *iface)
{
	int len;
	struct iscsi_iface_param_info *net_param;

	len = sizeof(struct iscsi_iface_param_info) + 1;
	iov->iov_base = calloc(len, sizeof(char));
	if (!(iov->iov_base))
		return 1;

	iov->iov_len = len;
	net_param = (struct iscsi_iface_param_info *)(iov->iov_base);
	net_param->param = ISCSI_NET_PARAM_IPV4_BOOTPROTO;
	net_param->iface_type = ISCSI_IFACE_TYPE_IPV4;
	net_param->iface_num = iface->iface_num;
	net_param->param_type = ISCSI_NET_PARAM;
	net_param->len = 1;
	if (!strcmp(iface->bootproto, "dhcp"))
		net_param->value[0] = ISCSI_BOOTPROTO_DHCP;
	else
		net_param->value[0] = ISCSI_BOOTPROTO_STATIC;
	return 0;
}

/* IPv6 IPAddress Autocfg: nd/dhcpv6/disable */
static int iface_fill_net_autocfg(struct iovec *iov, struct iface_rec *iface)
{
	int len;
	struct iscsi_iface_param_info *net_param;

	len = sizeof(struct iscsi_iface_param_info) + 1;
	iov->iov_base = calloc(len, sizeof(char));
	if (!(iov->iov_base))
		return 1;

	iov->iov_len = len;
	net_param = (struct iscsi_iface_param_info *)(iov->iov_base);
	net_param->param = ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG;
	net_param->iface_type = ISCSI_IFACE_TYPE_IPV6;
	net_param->param_type = ISCSI_NET_PARAM;
	net_param->len = 1;

	if (!strcmp(iface->ipv6_autocfg, "nd"))
		net_param->value[0] = ISCSI_IPV6_AUTOCFG_ND_ENABLE;
	else if (!strcmp(iface->ipv6_autocfg, "dhcpv6"))
		net_param->value[0] = ISCSI_IPV6_AUTOCFG_DHCPV6_ENABLE;
	else
		net_param->value[0] = ISCSI_IPV6_AUTOCFG_DISABLE;

	return 0;
}

/* IPv6 LinkLocal Autocfg: enable/disable */
static int iface_fill_linklocal_autocfg(struct iovec *iov,
					struct iface_rec *iface)
{
	int len;
	struct iscsi_iface_param_info *net_param;

	len = sizeof(struct iscsi_iface_param_info) + 1;
	iov->iov_base = calloc(len, sizeof(char));
	if (!(iov->iov_base))
		return 1;

	iov->iov_len = len;
	net_param = (struct iscsi_iface_param_info *)(iov->iov_base);
	net_param->param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG;
	net_param->iface_type = ISCSI_IFACE_TYPE_IPV6;
	net_param->param_type = ISCSI_NET_PARAM;
	net_param->len = 1;

	if (strstr(iface->linklocal_autocfg, "auto"))
		net_param->value[0] = ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE;
	else
		net_param->value[0] = ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE;

	return 0;
}

/* IPv6 Router Autocfg: enable/disable */
static int iface_fill_router_autocfg(struct iovec *iov, struct iface_rec *iface)
{
	int len;
	struct iscsi_iface_param_info *net_param;

	len = sizeof(struct iscsi_iface_param_info) + 1;
	iov->iov_base = calloc(len, sizeof(char));
	if (!(iov->iov_base))
		return 1;

	iov->iov_len = len;
	net_param = (struct iscsi_iface_param_info *)(iov->iov_base);
	net_param->param = ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG;
	net_param->iface_type = ISCSI_IFACE_TYPE_IPV6;
	net_param->param_type = ISCSI_NET_PARAM;
	net_param->len = 1;

	if (strstr(iface->router_autocfg, "auto"))
		net_param->value[0] = ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE;
	else
		net_param->value[0] = ISCSI_IPV6_ROUTER_AUTOCFG_DISABLE;

	return 0;
}

/* IPv4 IPAddress/Subnet Mask/Gateway: 4 bytes */
static int iface_fill_net_ipv4_addr(struct iovec *iov, struct iface_rec *iface,
				    uint32_t param)
{
	int rc = 1;
	int len;
	struct iscsi_iface_param_info *net_param;

	len = sizeof(struct iscsi_iface_param_info) + 4;
	iov->iov_base = calloc(len, sizeof(char));
	if (!(iov->iov_base))
		return 1;

	iov->iov_len = len;
	net_param = (struct iscsi_iface_param_info *)(iov->iov_base);
	net_param->param = param;
	net_param->iface_type = ISCSI_IFACE_TYPE_IPV4;
	net_param->iface_num = iface->iface_num;
	net_param->len = 4;
	net_param->param_type = ISCSI_NET_PARAM;

	switch (param) {
	case ISCSI_NET_PARAM_IPV4_ADDR:
		rc = inet_pton(AF_INET, iface->ipaddress, net_param->value);
		if (rc <= 0)
			goto free;
		break;
	case ISCSI_NET_PARAM_IPV4_SUBNET:
		rc = inet_pton(AF_INET, iface->subnet_mask, net_param->value);
		if (rc <= 0)
			goto free;
		break;
	case ISCSI_NET_PARAM_IPV4_GW:
		rc = inet_pton(AF_INET, iface->gateway, net_param->value);
		if (rc <= 0)
			goto free;
		break;
	default:
		goto free;
	}

	/* validate */
	if (!net_param->value[0] && !net_param->value[1] &&
	    !net_param->value[2] && !net_param->value[3])
		goto free;

	return 0;
free:
	free(iov->iov_base);
	iov->iov_base = NULL;
	iov->iov_len = 0;
	return 1;
}

/* IPv6 IPAddress/LinkLocal/Router: 16 bytes */
static int iface_fill_net_ipv6_addr(struct iovec *iov, struct iface_rec *iface,
				    uint32_t param)
{
	int rc;
	int len;
	struct iscsi_iface_param_info *net_param;

	len = sizeof(struct iscsi_iface_param_info) + 16;
	iov->iov_base = calloc(len, sizeof(char));
	if (!(iov->iov_base))
		return 1;

	iov->iov_len = len;
	net_param = (struct iscsi_iface_param_info *)(iov->iov_base);
	net_param->param = param;
	net_param->iface_type = ISCSI_IFACE_TYPE_IPV6;
	net_param->iface_num = iface->iface_num;
	net_param->param_type = ISCSI_NET_PARAM;
	net_param->len = 16;

	switch (param) {
	case ISCSI_NET_PARAM_IPV6_ADDR:
		rc = inet_pton(AF_INET6, iface->ipaddress, net_param->value);
		if (rc <= 0)
			goto free;
		break;
	case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
		rc = inet_pton(AF_INET6, iface->ipv6_linklocal,
			       net_param->value);
		if (rc <= 0)
			goto free;
		break;
	case ISCSI_NET_PARAM_IPV6_ROUTER:
		rc = inet_pton(AF_INET6, iface->ipv6_router, net_param->value);
		if (rc <= 0)
			goto free;
		break;
	default:
		goto free;
	}

	return 0;
free:
	free(iov->iov_base);
	iov->iov_base = NULL;
	iov->iov_len = 0;
	return 1;
}

struct iface_net_config {
	struct iface_rec *primary;
	struct iovec *iovs;
	int count;
};

static int __iface_build_net_config(void *data, struct iface_rec *iface)
{
	struct iface_net_config *net_config = data;
	struct iovec *iov;
	int iptype = ISCSI_IFACE_TYPE_IPV4;
	int count = 0;

	if (strcmp(net_config->primary->hwaddress, iface->hwaddress))
		return 0;

	if (strcmp(iface->bootproto, "dhcp") && !strstr(iface->ipaddress, "."))
		iptype = ISCSI_IFACE_TYPE_IPV6;

	/* start at 2, because 0 is for nlmsghdr and 1 for event */
	iov = net_config->iovs + 2;

	if (iptype == ISCSI_IFACE_TYPE_IPV4) {
		if (!strcmp(iface->state, "disable")) {
			if (!iface_fill_net_state(&iov[net_config->count],
						  iface,
						  ISCSI_IFACE_TYPE_IPV4)) {
				net_config->count++;
				count++;
			}

			return 0;
		}

		if (strstr(iface->bootproto, "dhcp")) {
			if (!iface_fill_net_bootproto(&iov[net_config->count],
						      iface)) {
				net_config->count++;
				count++;
			}
		} else if (strstr(iface->ipaddress, ".")) {
			if (!iface_fill_net_bootproto(&iov[net_config->count],
						      iface)) {
				net_config->count++;
				count++;
			}
			if (!iface_fill_net_ipv4_addr(&iov[net_config->count],
						iface,
						ISCSI_NET_PARAM_IPV4_ADDR)) {
				net_config->count++;
				count++;
			}
			if (strstr(iface->subnet_mask, ".")) {
				if (!iface_fill_net_ipv4_addr(
						&iov[net_config->count], iface,
						ISCSI_NET_PARAM_IPV4_SUBNET)) {
					net_config->count++;
					count++;
				}
			}
			if (strstr(iface->gateway, ".")) {
				if (!iface_fill_net_ipv4_addr(
						&iov[net_config->count], iface,
						ISCSI_NET_PARAM_IPV4_GW)) {
					net_config->count++;
					count++;
				}
			}
		}

		/*
		 * If IPv4 configuration in iface file is valid,
		 * fill state and other parameters (if any)
		 */
		if (count) {
			if (!iface_fill_net_state(&iov[net_config->count],
						  iface,
						  ISCSI_IFACE_TYPE_IPV4)) {
				net_config->count++;
				count++;
			}
			if (!iface_fill_vlan_state(&iov[net_config->count],
						iface,
						ISCSI_IFACE_TYPE_IPV4)) {
				net_config->count++;
				count++;
			}
			if (strcmp(iface->vlan_state, "disable") &&
			    iface->vlan_id) {
				if (!iface_fill_vlan_id(&iov[net_config->count],
						iface, ISCSI_IFACE_TYPE_IPV4)) {
					net_config->count++;
					count++;
				}
			}
			if (iface->mtu) {
				if (!iface_fill_mtu(&iov[net_config->count],
						    iface,
						    ISCSI_IFACE_TYPE_IPV4)) {
					net_config->count++;
					count++;
				}
			}
			if (iface->port) {
				if (!iface_fill_port(&iov[net_config->count],
						iface,
						ISCSI_IFACE_TYPE_IPV4)) {
					net_config->count++;
					count++;
				}
			}
		}
	} else if (iptype == ISCSI_IFACE_TYPE_IPV6) {
		if (!strcmp(iface->state, "disable")) {
			if (!iface_fill_net_state(&iov[net_config->count],
						  iface,
						  ISCSI_IFACE_TYPE_IPV6)) {
				net_config->count++;
				count++;
			}
			return 0;
		}

		/* For IPv6 Address */
		if (strstr(iface->ipv6_autocfg, "nd") ||
		    strstr(iface->ipv6_autocfg, "dhcpv6")) {
			if (!iface_fill_net_autocfg(&iov[net_config->count],
						    iface)) {
				net_config->count++;
				count++;
			}
		} else if (strstr(iface->ipaddress, ":")) {
			if (!iface_fill_net_autocfg(&iov[net_config->count],
						    iface)) {
				net_config->count++;
				count++;
			}
			/* User provided IPv6 Address */
			if (!iface_fill_net_ipv6_addr(&iov[net_config->count],
						iface,
						ISCSI_NET_PARAM_IPV6_ADDR)) {
				net_config->count++;
				count++;
			}
		}

		/* For LinkLocal Address */
		if (strstr(iface->linklocal_autocfg, "auto")) {
			if (!iface_fill_linklocal_autocfg(
						&iov[net_config->count],
						iface)) {
				net_config->count++;
				count++;
			}
		} else if (strstr(iface->ipv6_linklocal, ":")) {
			if (!iface_fill_linklocal_autocfg(
						&iov[net_config->count],
						iface)) {
				net_config->count++;
				count++;
			}
			/* User provided Link Local Address */
			if (!iface_fill_net_ipv6_addr(&iov[net_config->count],
					iface,
					ISCSI_NET_PARAM_IPV6_LINKLOCAL)) {
				net_config->count++;
				count++;
			}
		}

		/* For Router Address */
		if (strstr(iface->router_autocfg, "auto")) {
			if (!iface_fill_router_autocfg(&iov[net_config->count],
						       iface)) {
				net_config->count++;
				count++;
			}
		} else if (strstr(iface->ipv6_router, ":")) {
			if (!iface_fill_router_autocfg(&iov[net_config->count],
						       iface)) {
				net_config->count++;
				count++;
			}
			/* User provided Router Address */
			if (!iface_fill_net_ipv6_addr(&iov[net_config->count],
						iface,
						ISCSI_NET_PARAM_IPV6_ROUTER)) {
				net_config->count++;
				count++;
			}
		}

		/*
		 * If IPv6 configuration in iface file is valid,
		 * fill state and other parameters
		 */
		if (count) {
			if (!iface_fill_net_state(&iov[net_config->count],
						  iface,
						  ISCSI_IFACE_TYPE_IPV6)) {
				net_config->count++;
				count++;
			}
			if (!iface_fill_vlan_state(&iov[net_config->count],
						   iface,
						   ISCSI_IFACE_TYPE_IPV6)) {
				net_config->count++;
				count++;
			}
			if (strcmp(iface->vlan_state, "disable") &&
			    iface->vlan_id) {
				if (!iface_fill_vlan_id(&iov[net_config->count],
						iface,
						ISCSI_IFACE_TYPE_IPV6)) {
					net_config->count++;
					count++;
				}
			}
			if (iface->mtu) {
				if (!iface_fill_mtu(&iov[net_config->count],
						    iface,
						    ISCSI_IFACE_TYPE_IPV6)) {
					net_config->count++;
					count++;
				}
			}
			if (iface->port) {
				if (!iface_fill_port(&iov[net_config->count],
						     iface,
						     ISCSI_IFACE_TYPE_IPV6)) {
					net_config->count++;
					count++;
				}
			}
		}
	}
	return 0;
}

/**
 * iface_build_net_config - Setup neconfig parameter buffers
 * @iface: iface to setup
 * @iface_all: Flag for number of ifaces to traverse (1 for all)
 * @iovs: iovec buffer for netconfig parameters
 *
 * Returns total number of netconfig parameter buffers used.
 */
int iface_build_net_config(struct iface_rec *iface, int iface_all,
			   struct iovec *iovs)
{
	int num_found = 0, rc;
	struct iface_net_config net_config;

	log_debug(8, "In iface_build_net_config\n");

	net_config.primary = iface;
	net_config.iovs = iovs;
	net_config.count = 0;

	if (iface_all)
		rc = iface_for_each_iface(&net_config, 0, &num_found,
					  __iface_build_net_config);
	else
		rc = __iface_build_net_config(&net_config, iface);

	log_debug(8, "iface_build_net_config: rc = %d, count = %d\n",
		  rc, net_config.count);
	return net_config.count;
}