|
root / base / usr / src / lib / libvrrpadm
libvrrpadm Plain Text 1449 lines 35.5 KB
   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
#
# 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 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

include $(SRC)/lib/Makefile.lib

HDRS =		netinet/vrrp.h libvrrpadm.h
HDRDIR =	common

# Hammerhead: amd64-only
SUBDIRS = $(MACH64)

POFILE =	libvrrpadm.po
MSGFILES =      common/libvrrpadm.c

XGETFLAGS =	-a -x libvrrpadm.xcl

all :		TARGET = all
clean :	TARGET = clean
clobber :	TARGET = clobber
install :	TARGET = install

.KEEP_STATE:

all clean clobber install: $(SUBDIRS)

install_h:	$(ROOTHDRS)

$(POFILE):	pofile_MSGFILES

_msg:		$(MSGDOMAINPOFILE)

check:		$(CHECKHDRS)

$(SUBDIRS): FRC
	@cd $@; pwd; $(MAKE) $(TARGET)

FRC:

include $(SRC)/Makefile.msg.targ
include $(SRC)/lib/Makefile.targ
#
# 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 2010 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

LIBRARY = libvrrpadm.a
VERS    = .1
OBJECTS = libvrrpadm.o

include ../../Makefile.lib

# install this library in the root filesystem
#include ../../Makefile.rootfs

LIBS =		$(DYNLIB)
LDLIBS +=	-lc -lsocket -ldladm -lscf

SRCDIR =	../common

CFLAGS +=	$(CCVERBOSE)
CPPFLAGS +=	-I$(SRCDIR) -D_REENTRANT

CSTD =	$(CSTD_GNU99)

.KEEP_STATE:

all:		$(LIBS)


include $(SRC)/lib/Makefile.targ
#
# 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 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

include ../Makefile.com
include ../../Makefile.lib.64

install: all $(ROOTLIBS64) $(ROOTLINKS64) $(ROOTCOMPATLINKS64)
/*
 * 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 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/mman.h>
#include <sys/varargs.h>
#include <sys/vlan.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>	/* LIFNAMSIZ */
#include <netinet/vrrp.h>
#include <libdladm.h>
#include <libdlvnic.h>
#include <libdlvlan.h>
#include <libdllink.h>
#include <libintl.h>
#include <libscf.h>
#include <libvrrpadm.h>

#define	VRRP_SERVICE	"network/vrrp:default"

typedef vrrp_err_t vrrp_cmd_func_t(int, void *);

static boolean_t
vrrp_svc_isonline(char *svc_name)
{
	char		*s;
	boolean_t	isonline = B_FALSE;

	if ((s = smf_get_state(svc_name)) != NULL) {
		if (strcmp(s, SCF_STATE_STRING_ONLINE) == 0)
			isonline = B_TRUE;
		free(s);
	}

	return (isonline);
}

#define	MAX_WAIT_TIME	15

static vrrp_err_t
vrrp_enable_service()
{
	int	i;

	if (vrrp_svc_isonline(VRRP_SERVICE))
		return (VRRP_SUCCESS);

	if (smf_enable_instance(VRRP_SERVICE, 0) == -1) {
		if (scf_error() == SCF_ERROR_PERMISSION_DENIED)
			return (VRRP_EPERM);
		else
			return (VRRP_ENOSVC);
	}

	/*
	 * Wait up to MAX_WAIT_TIME seconds for the VRRP service being brought
	 * up online
	 */
	for (i = 0; i < MAX_WAIT_TIME; i++) {
		if (vrrp_svc_isonline(VRRP_SERVICE))
			break;
		(void) sleep(1);
	}
	if (i == MAX_WAIT_TIME)
		return (VRRP_ENOSVC);

	return (VRRP_SUCCESS);
}

/*
 * Disable the VRRP service if there is no VRRP router left.
 */
static void
vrrp_disable_service_when_no_router()
{
	uint32_t	cnt = 0;

	/*
	 * Get the number of the existing routers. If there is no routers
	 * left, disable the service.
	 */
	if (vrrp_list(NULL, VRRP_VRID_NONE, NULL, AF_UNSPEC, &cnt,
	    NULL) == VRRP_SUCCESS && cnt == 0) {
		(void) smf_disable_instance(VRRP_SERVICE, 0);
	}
}

static vrrp_err_t
vrrp_cmd_request(void *cmd, size_t csize, vrrp_cmd_func_t func, void *arg)
{
	struct sockaddr_un	to;
	int			sock, flags;
	size_t			len, cur_size = 0;
	vrrp_ret_t		ret;
	vrrp_err_t		err;

	if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
		return (VRRP_ESYS);

	/*
	 * Set it to be non-blocking.
	 */
	flags = fcntl(sock, F_GETFL, 0);
	(void) fcntl(sock, F_SETFL, (flags | O_NONBLOCK));

	(void) memset(&to, 0, sizeof (to));
	to.sun_family = AF_UNIX;
	(void) strlcpy(to.sun_path, VRRPD_SOCKET, sizeof (to.sun_path));

	/*
	 * Connect to vrrpd
	 */
	if (connect(sock, (const struct sockaddr *)&to, sizeof (to)) < 0) {
		(void) close(sock);
		return (VRRP_ENOSVC);
	}

	/*
	 * Send the request
	 */
	while (cur_size < csize) {
		len = write(sock, (char *)cmd + cur_size, csize - cur_size);
		if (len == (size_t)-1 && errno == EAGAIN) {
			continue;
		} else if (len > 0) {
			cur_size += len;
			continue;
		}
		(void) close(sock);
		return (VRRP_ENOSVC);
	}

	/*
	 * Expect the ack, first get the error code.
	 */
	cur_size = 0;
	while (cur_size < sizeof (vrrp_err_t)) {
		len = read(sock, (char *)&ret + cur_size,
		    sizeof (vrrp_err_t) - cur_size);

		if (len == (size_t)-1 && errno == EAGAIN) {
			continue;
		} else if (len > 0) {
			cur_size += len;
			continue;
		}
		(void) close(sock);
		return (VRRP_ESYS);
	}

	if ((err = ret.vr_err) != VRRP_SUCCESS)
		goto done;

	/*
	 * The specific callback gets the rest of the information.
	 */
	if (func != NULL)
		err = func(sock, arg);

done:
	(void) close(sock);
	return (err);
}

/*
 * public APIs
 */
const char *
vrrp_err2str(vrrp_err_t err)
{
	switch (err) {
	case VRRP_SUCCESS:
		return (dgettext(TEXT_DOMAIN, "success"));
	case VRRP_ENOMEM:
		return (dgettext(TEXT_DOMAIN, "not enough memory"));
	case VRRP_EINVALVRNAME:
		return (dgettext(TEXT_DOMAIN, "invalid router name"));
	case VRRP_ENOPRIM:
		return (dgettext(TEXT_DOMAIN, "no primary IP"));
	case VRRP_EEXIST:
		return (dgettext(TEXT_DOMAIN, "already exists"));
	case VRRP_ENOVIRT:
		return (dgettext(TEXT_DOMAIN, "no virtual IPs"));
	case VRRP_EIPADM:
		return (dgettext(TEXT_DOMAIN, "ip configuration failure"));
	case VRRP_EDLADM:
		return (dgettext(TEXT_DOMAIN, "data-link configuration "
		    "failure"));
	case VRRP_EDB:
		return (dgettext(TEXT_DOMAIN, "configuration update error"));
	case VRRP_EBADSTATE:
		return (dgettext(TEXT_DOMAIN, "invalid state"));
	case VRRP_EVREXIST:
		return (dgettext(TEXT_DOMAIN, "VRRP router already exists"));
	case VRRP_ETOOSMALL:
		return (dgettext(TEXT_DOMAIN, "not enough space"));
	case VRRP_EINSTEXIST:
		return (dgettext(TEXT_DOMAIN, "router name already exists"));
	case VRRP_ENOTFOUND:
		return (dgettext(TEXT_DOMAIN, "VRRP router not found"));
	case VRRP_EINVALADDR:
		return (dgettext(TEXT_DOMAIN, "invalid IP address"));
	case VRRP_EINVALAF:
		return (dgettext(TEXT_DOMAIN, "invalid IP address family"));
	case VRRP_EINVALLINK:
		return (dgettext(TEXT_DOMAIN, "invalid data-link"));
	case VRRP_EPERM:
		return (dgettext(TEXT_DOMAIN, "permission denied"));
	case VRRP_ESYS:
		return (dgettext(TEXT_DOMAIN, "system error"));
	case VRRP_EAGAIN:
		return (dgettext(TEXT_DOMAIN, "try again"));
	case VRRP_EALREADY:
		return (dgettext(TEXT_DOMAIN, "operation already in progress"));
	case VRRP_ENOVNIC:
		return (dgettext(TEXT_DOMAIN, "VRRP VNIC has not been "
		    "created"));
	case VRRP_ENOLINK:
		return (dgettext(TEXT_DOMAIN, "the data-link does not exist"));
	case VRRP_ENOSVC:
		return (dgettext(TEXT_DOMAIN, "the VRRP service cannot "
		    "be enabled"));
	case VRRP_EINVAL:
	default:
		return (dgettext(TEXT_DOMAIN, "invalid argument"));
	}
}

const char *
vrrp_state2str(vrrp_state_t state)
{
	switch (state) {
	case VRRP_STATE_NONE:
		return (dgettext(TEXT_DOMAIN, "NONE"));
	case VRRP_STATE_INIT:
		return (dgettext(TEXT_DOMAIN, "INIT"));
	case VRRP_STATE_MASTER:
		return (dgettext(TEXT_DOMAIN, "MASTER"));
	case VRRP_STATE_BACKUP:
		return (dgettext(TEXT_DOMAIN, "BACKUP"));
	default:
		return (dgettext(TEXT_DOMAIN, "INVALID"));
	}
}

vrrp_err_t
vrrp_open(vrrp_handle_t *vh)
{
	dladm_handle_t	dh;

	if (dladm_open(&dh) != DLADM_STATUS_OK)
		return (VRRP_EDLADM);

	if ((*vh = malloc(sizeof (struct vrrp_handle))) == NULL) {
		dladm_close(dh);
		return (VRRP_ENOMEM);
	}
	(*vh)->vh_dh = dh;
	return (VRRP_SUCCESS);
}

void
vrrp_close(vrrp_handle_t vh)
{
	if (vh != NULL) {
		dladm_close(vh->vh_dh);
		free(vh);
	}
}

boolean_t
vrrp_valid_name(const char *name)
{
	const char	*c;

	/*
	 * The legal characters in a valid router name are:
	 * alphanumeric (a-z,  A-Z,  0-9), underscore ('_'), and '.'.
	 */
	for (c = name; *c != '\0'; c++) {
		if ((isalnum(*c) == 0) && (*c != '_'))
			return (B_FALSE);
	}

	return (B_TRUE);
}

/*ARGSUSED*/
vrrp_err_t
vrrp_create(vrrp_handle_t vh, vrrp_vr_conf_t *conf)
{
	vrrp_cmd_create_t	cmd;
	vrrp_err_t		err;

again:
	/*
	 * Enable the VRRP service if it is not already enabled.
	 */
	if ((err = vrrp_enable_service()) != VRRP_SUCCESS)
		return (err);

	cmd.vcc_cmd = VRRP_CMD_CREATE;
	(void) memcpy(&cmd.vcc_conf, conf, sizeof (vrrp_vr_conf_t));

	err = vrrp_cmd_request(&cmd, sizeof (cmd), NULL, NULL);
	if (err == VRRP_ENOSVC) {
		/*
		 * This may be due to another process is deleting the last
		 * router and disabled the VRRP service, try again.
		 */
		goto again;
	} else if (err != VRRP_SUCCESS) {
		/*
		 * If router cannot be created, check if the VRRP service
		 * should be disabled, and disable if needed.
		 */
		vrrp_disable_service_when_no_router();
	}

	return (err);
}

/*ARGSUSED*/
vrrp_err_t
vrrp_delete(vrrp_handle_t vh, const char *vn)
{
	vrrp_cmd_delete_t	cmd;
	vrrp_err_t		err;

	/*
	 * If the VRRP service is not enabled, we assume there is no router
	 * configured.
	 */
	if (!vrrp_svc_isonline(VRRP_SERVICE))
		return (VRRP_ENOTFOUND);

	cmd.vcd_cmd = VRRP_CMD_DELETE;
	if (strlcpy(cmd.vcd_name, vn, VRRP_NAME_MAX) >= VRRP_NAME_MAX)
		return (VRRP_EINVAL);

	err = vrrp_cmd_request(&cmd, sizeof (cmd), NULL, NULL);
	if (err == VRRP_SUCCESS)
		vrrp_disable_service_when_no_router();
	return (err);
}

/*ARGSUSED*/
vrrp_err_t
vrrp_enable(vrrp_handle_t vh, const char *vn)
{
	vrrp_cmd_enable_t	cmd;
	vrrp_err_t		err;

	/*
	 * If the VRRP service is not enabled, we assume there is no router
	 * configured.
	 */
	if (!vrrp_svc_isonline(VRRP_SERVICE))
		return (VRRP_ENOTFOUND);

	cmd.vcs_cmd = VRRP_CMD_ENABLE;
	if (strlcpy(cmd.vcs_name, vn, VRRP_NAME_MAX) >= VRRP_NAME_MAX)
		return (VRRP_EINVAL);

	err = vrrp_cmd_request(&cmd, sizeof (cmd), NULL, NULL);
	return (err);
}

/*ARGSUSED*/
vrrp_err_t
vrrp_disable(vrrp_handle_t vh, const char *vn)
{
	vrrp_cmd_disable_t	cmd;
	vrrp_err_t		err;

	/*
	 * If the VRRP service is not enabled, we assume there is no router
	 * configured.
	 */
	if (!vrrp_svc_isonline(VRRP_SERVICE))
		return (VRRP_ENOTFOUND);

	cmd.vcx_cmd = VRRP_CMD_DISABLE;
	if (strlcpy(cmd.vcx_name, vn, VRRP_NAME_MAX) >= VRRP_NAME_MAX)
		return (VRRP_EINVAL);

	err = vrrp_cmd_request(&cmd, sizeof (cmd), NULL, NULL);
	return (err);
}

/*ARGSUSED*/
vrrp_err_t
vrrp_modify(vrrp_handle_t vh, vrrp_vr_conf_t *conf, uint32_t mask)
{
	vrrp_cmd_modify_t	cmd;
	vrrp_err_t		err;

	/*
	 * If the VRRP service is not enabled, we assume there is no router
	 * configured.
	 */
	if (!vrrp_svc_isonline(VRRP_SERVICE))
		return (VRRP_ENOTFOUND);

	cmd.vcm_cmd = VRRP_CMD_MODIFY;
	cmd.vcm_mask = mask;
	(void) memcpy(&cmd.vcm_conf, conf, sizeof (vrrp_vr_conf_t));

	err = vrrp_cmd_request(&cmd, sizeof (cmd), NULL, NULL);
	return (err);
}

typedef struct vrrp_cmd_list_arg {
	uint32_t	*vfl_cnt;
	char		*vfl_names;
} vrrp_cmd_list_arg_t;

static vrrp_err_t
vrrp_list_func(int sock, void *arg)
{
	vrrp_cmd_list_arg_t	*list_arg = arg;
	uint32_t		in_cnt = *(list_arg->vfl_cnt);
	uint32_t		out_cnt;
	vrrp_ret_list_t		ret;
	size_t			len, cur_size = 0;

	/*
	 * Get the rest of vrrp_ret_list_t besides the error code.
	 */
	cur_size = sizeof (vrrp_err_t);
	while (cur_size < sizeof (vrrp_ret_list_t)) {
		len = read(sock, (char *)&ret + cur_size,
		    sizeof (vrrp_ret_list_t) - cur_size);

		if (len == (size_t)-1 && errno == EAGAIN) {
			continue;
		} else if (len > 0) {
			cur_size += len;
			continue;
		}
		return (VRRP_ESYS);
	}

	*(list_arg->vfl_cnt) = out_cnt = ret.vrl_cnt;
	out_cnt = (in_cnt <= out_cnt) ? in_cnt : out_cnt;
	cur_size = 0;

	while (cur_size < VRRP_NAME_MAX * out_cnt) {
		len = read(sock, (char *)list_arg->vfl_names + cur_size,
		    VRRP_NAME_MAX * out_cnt - cur_size);

		if (len == (size_t)-1 && errno == EAGAIN) {
			continue;
		} else if (len > 0) {
			cur_size += len;
			continue;
		}
		return (VRRP_ESYS);
	}
	return (VRRP_SUCCESS);
}

/*
 * Looks up the vrrp instances that matches the given variable.
 *
 * If the given cnt is 0, names should be set to NULL. In this case, only
 * the count of the matched instances is returned.
 *
 * If the given cnt is non-zero, caller must allocate "names" whose size
 * is (cnt * VRRP_NAME_MAX).
 *
 * Return value: the current count of matched instances, and names will be
 * points to the list of the current vrrp instances names. Note that
 * only MIN(in_cnt, out_cnt) number of names will be returned.
 */
/*ARGSUSED*/
vrrp_err_t
vrrp_list(vrrp_handle_t vh, vrid_t vrid, const char *intf, int af,
    uint32_t *cnt, char *names)
{
	vrrp_cmd_list_t		cmd;
	vrrp_err_t		err;
	vrrp_cmd_list_arg_t	list_arg;

	if ((cnt == NULL) || (*cnt != 0 && names == NULL))
		return (VRRP_EINVAL);

	cmd.vcl_ifname[0] = '\0';
	if (intf != NULL && (strlcpy(cmd.vcl_ifname, intf,
	    LIFNAMSIZ) >= LIFNAMSIZ)) {
		return (VRRP_EINVAL);
	}

	/*
	 * If the service is not online, we assume there is no router
	 * configured.
	 */
	if (!vrrp_svc_isonline(VRRP_SERVICE)) {
		*cnt = 0;
		return (VRRP_SUCCESS);
	}

	cmd.vcl_cmd = VRRP_CMD_LIST;
	cmd.vcl_vrid = vrid;
	cmd.vcl_af = af;

	list_arg.vfl_cnt = cnt;
	list_arg.vfl_names = names;

	err = vrrp_cmd_request(&cmd, sizeof (cmd), vrrp_list_func, &list_arg);
	return (err);
}

static vrrp_err_t
vrrp_query_func(int sock, void *arg)
{
	vrrp_queryinfo_t	*qinfo = arg;
	size_t			len, cur_size = 0, total;
	uint32_t		in_cnt = qinfo->show_va.va_vipcnt;
	uint32_t		out_cnt;

	/*
	 * Expect the ack, first get the vrrp_ret_t.
	 */
	total = sizeof (vrrp_queryinfo_t);
	while (cur_size < total) {
		len = read(sock, (char *)qinfo + cur_size, total - cur_size);
		if (len == (size_t)-1 && errno == EAGAIN) {
			continue;
		} else if (len > 0) {
			cur_size += len;
			continue;
		}
		return (VRRP_ESYS);
	}

	out_cnt = qinfo->show_va.va_vipcnt;

	/*
	 * Even if there is no IP virtual IP address, there is always
	 * space in the vrrp_queryinfo_t structure for one virtual
	 * IP address.
	 */
	out_cnt = (out_cnt == 0) ? 1 : out_cnt;
	out_cnt = (in_cnt < out_cnt ? in_cnt : out_cnt) - 1;
	total += out_cnt * sizeof (vrrp_addr_t);

	while (cur_size < total) {
		len = read(sock, (char *)qinfo + cur_size, total - cur_size);
		if (len == (size_t)-1 && errno == EAGAIN) {
			continue;
		} else if (len > 0) {
			cur_size += len;
			continue;
		}
		return (VRRP_ESYS);
	}
	return (VRRP_SUCCESS);
}

/*
 * *vqp is allocated inside this function and must be freed by the caller.
 */
/*ARGSUSED*/
vrrp_err_t
vrrp_query(vrrp_handle_t vh, const char *vn, vrrp_queryinfo_t **vqp)
{
	vrrp_cmd_query_t	cmd;
	vrrp_queryinfo_t	*qinfo;
	vrrp_err_t		err;
	size_t			size;
	uint32_t		vipcnt = 1;

	if (strlcpy(cmd.vcq_name, vn, VRRP_NAME_MAX) >= VRRP_NAME_MAX)
		return (VRRP_EINVAL);

	/*
	 * If the service is not online, we assume there is no router
	 * configured.
	 */
	if (!vrrp_svc_isonline(VRRP_SERVICE))
		return (VRRP_ENOTFOUND);

	cmd.vcq_cmd = VRRP_CMD_QUERY;

	/*
	 * Allocate enough room for virtual IPs.
	 */
again:
	size = sizeof (vrrp_queryinfo_t);
	size += (vipcnt == 0) ? 0 : (vipcnt - 1) * sizeof (vrrp_addr_t);
	if ((qinfo = malloc(size)) == NULL) {
		err = VRRP_ENOMEM;
		goto done;
	}

	qinfo->show_va.va_vipcnt = vipcnt;
	err = vrrp_cmd_request(&cmd, sizeof (cmd), vrrp_query_func, qinfo);
	if (err != VRRP_SUCCESS) {
		free(qinfo);
		goto done;
	}

	/*
	 * If the returned number of virtual IPs is greater than we expected,
	 * allocate more room and try again.
	 */
	if (qinfo->show_va.va_vipcnt > vipcnt) {
		vipcnt = qinfo->show_va.va_vipcnt;
		free(qinfo);
		goto again;
	}

	*vqp = qinfo;

done:
	return (err);
}

struct lookup_vnic_arg {
	vrid_t		lva_vrid;
	datalink_id_t	lva_linkid;
	int		lva_af;
	uint16_t	lva_vid;
	vrrp_handle_t	lva_vh;
	char		lva_vnic[MAXLINKNAMELEN];
};

/*
 * Is this a special VNIC interface created for VRRP? If so, return
 * the linkid the VNIC was created on, the VRRP ID and address family.
 */
boolean_t
vrrp_is_vrrp_vnic(vrrp_handle_t vh, datalink_id_t vnicid,
    datalink_id_t *linkidp, uint16_t *vidp, vrid_t *vridp, int *afp)
{
	dladm_vnic_attr_t	vattr;

	if (dladm_vnic_info(vh->vh_dh, vnicid, &vattr, DLADM_OPT_ACTIVE) !=
	    DLADM_STATUS_OK) {
		return (B_FALSE);
	}

	*vridp = vattr.va_vrid;
	*vidp = vattr.va_vid;
	*afp = vattr.va_af;
	*linkidp = vattr.va_link_id;
	return (vattr.va_vrid != VRRP_VRID_NONE);
}

static int
lookup_vnic(dladm_handle_t dh, datalink_id_t vnicid, void *arg)
{
	vrid_t			vrid;
	uint16_t		vid;
	datalink_id_t		linkid;
	int			af;
	struct lookup_vnic_arg	*lva = arg;

	if (vrrp_is_vrrp_vnic(lva->lva_vh, vnicid, &linkid, &vid, &vrid,
	    &af) && lva->lva_vrid == vrid && lva->lva_linkid == linkid &&
	    (lva->lva_vid == VLAN_ID_NONE || lva->lva_vid == vid) &&
	    lva->lva_af == af) {
		if (dladm_datalink_id2info(dh, vnicid, NULL, NULL, NULL,
		    lva->lva_vnic, sizeof (lva->lva_vnic)) == DLADM_STATUS_OK) {
			return (DLADM_WALK_TERMINATE);
		}
	}
	return (DLADM_WALK_CONTINUE);
}

/*
 * Given the primary link name, find the assoicated VRRP vnic name, if
 * the vnic does not exist yet, return the linkid, vid of the primary link.
 */
vrrp_err_t
vrrp_get_vnicname(vrrp_handle_t vh, vrid_t vrid, int af, char *link,
    datalink_id_t *linkidp, uint16_t *vidp, char *vnic, size_t len)
{
	datalink_id_t		linkid;
	uint32_t		flags;
	uint16_t		vid = VLAN_ID_NONE;
	datalink_class_t	class;
	dladm_vlan_attr_t	vlan_attr;
	dladm_vnic_attr_t	vnic_attr;
	struct lookup_vnic_arg	lva;
	uint32_t		media;

	if ((strlen(link) == 0) || dladm_name2info(vh->vh_dh,
	    link, &linkid, &flags, &class, &media) !=
	    DLADM_STATUS_OK || !(flags & DLADM_OPT_ACTIVE)) {
		return (VRRP_EINVAL);
	}

	if (class == DATALINK_CLASS_VLAN) {
		if (dladm_vlan_info(vh->vh_dh, linkid, &vlan_attr,
		    DLADM_OPT_ACTIVE) != DLADM_STATUS_OK) {
			return (VRRP_EINVAL);
		}
		linkid = vlan_attr.dv_linkid;
		vid = vlan_attr.dv_vid;
		if ((dladm_datalink_id2info(vh->vh_dh, linkid, NULL,
		    &class, &media, NULL, 0)) != DLADM_STATUS_OK) {
			return (VRRP_EINVAL);
		}
	}

	if (class == DATALINK_CLASS_VNIC) {
		if (dladm_vnic_info(vh->vh_dh, linkid, &vnic_attr,
		    DLADM_OPT_ACTIVE) != DLADM_STATUS_OK) {
			return (VRRP_EINVAL);
		}
		linkid = vnic_attr.va_link_id;
		vid = vnic_attr.va_vid;
	}

	/*
	 * Only VRRP over vnics, aggrs and physical ethernet links is supported
	 */
	if ((class != DATALINK_CLASS_PHYS && class != DATALINK_CLASS_AGGR &&
	    class != DATALINK_CLASS_VNIC) || media != DL_ETHER) {
		return (VRRP_EINVAL);
	}

	if (linkidp != NULL)
		*linkidp = linkid;
	if (vidp != NULL)
		*vidp = vid;

	/*
	 * Find the assoicated vnic with the given vrid/vid/af/linkid
	 */
	lva.lva_vrid = vrid;
	lva.lva_vid = vid;
	lva.lva_af = af;
	lva.lva_linkid = linkid;
	lva.lva_vh = vh;
	lva.lva_vnic[0] = '\0';

	(void) dladm_walk_datalink_id(lookup_vnic, vh->vh_dh, &lva,
	    DATALINK_CLASS_VNIC, DATALINK_ANY_MEDIATYPE, DLADM_OPT_ACTIVE);
	if (strlen(lva.lva_vnic) != 0) {
		(void) strlcpy(vnic, lva.lva_vnic, len);
		return (VRRP_SUCCESS);
	}

	return (VRRP_ENOVNIC);
}
/*
 * 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 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_LIBVRRPADM_H
#define	_LIBVRRPADM_H

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>		/* in(6)_addr_t */
#include <arpa/inet.h>
#include <net/if.h>		/* LIFNAMSIZ */
#include <limits.h>
#include <netinet/vrrp.h>
#include <syslog.h>
#include <libdladm.h>

#ifdef __cplusplus
extern "C" {
#endif

#define	VRRP_NAME_MAX	32
#define	VRRPD_SOCKET	"/var/run/vrrpd.socket"

/*
 * to store the IP addresses
 */
typedef struct vrrp_addr {
	union {
		struct sockaddr_in	a4;
		struct sockaddr_in6	a6;
	} in;
#define	in4	in.a4
#define	in6	in.a6
} vrrp_addr_t;

/*
 * VRRP instance (configuration information).
 * Passed to vrrp_create(), returned by vrrp_query().
 */
typedef struct vrrp_vr_conf_s {
	char		vvc_name[VRRP_NAME_MAX];	/* VRRP router name */
	char		vvc_link[MAXLINKNAMELEN];	/* data-link name */
	vrid_t		vvc_vrid;			/* VRID */
	int		vvc_af;				/* IPv4/IPv6 */
	int		vvc_pri;
	uint32_t	vvc_adver_int;			/* in ms */
	boolean_t	vvc_preempt;
	boolean_t	vvc_accept;
	boolean_t	vvc_enabled;
} vrrp_vr_conf_t;

/*
 * VRRP state machine
 */
typedef enum {
	VRRP_STATE_NONE = -1,
	VRRP_STATE_INIT,
	VRRP_STATE_MASTER,
	VRRP_STATE_BACKUP
} vrrp_state_t;

/*
 * VRRP status structure
 * Returned by vrrp_query() as part of vrrp_queryinfo_t.
 */
typedef struct vrrp_statusinfo_s {
	vrrp_state_t	vs_state;
	vrrp_state_t	vs_prev_state;
	struct timeval	vs_st_time;	/* timestamp of last state trans */
} vrrp_stateinfo_t;

/*
 * The information obtained from peer's advertisements
 * Returned by vrrp_query() as part of vrrp_queryinfo_t.
 */
typedef struct vrrp_peer_s {
	vrrp_addr_t	vp_addr;	/* source IP addr of the message */
	int		vp_prio;	/* priority in adv message */
	struct timeval	vp_time;	/* timestamp of the adv message */
	int		vp_adver_int;	/* adv interval in adv message */
} vrrp_peer_t;

/*
 * Useful timer information, in ms
 */
typedef struct vrrp_timeinfo_s {
	int	vt_since_last_tran;	/* time since last state transition */
	int	vt_since_last_adv;	/* time since last advertisement */
	int	vt_master_down_intv;	/* timer interval for backup to */
					/* declare master down */
} vrrp_timerinfo_t;

/*
 * Address information
 */
typedef struct vrrp_addrinfo_s {
	char		va_vnic[MAXLINKNAMELEN];
	vrrp_addr_t	va_primary;
	uint32_t	va_vipcnt;
	vrrp_addr_t	va_vips[1];
} vrrp_addrinfo_t;

/*
 * VRRP instance configuration and run-time states information
 * Returned by vrrp_query().
 */
typedef struct vrrp_queryinfo {
	vrrp_vr_conf_t		show_vi;
	vrrp_stateinfo_t	show_vs;
	vrrp_peer_t		show_vp;
	vrrp_timerinfo_t	show_vt;
	vrrp_addrinfo_t		show_va;
} vrrp_queryinfo_t;

/*
 * flags sent with the VRRP_CMD_MODIFY command. Used in vrrp_setprop().
 */
#define	VRRP_CONF_PRIORITY	0x01
#define	VRRP_CONF_INTERVAL	0x02
#define	VRRP_CONF_PREEMPT	0x04
#define	VRRP_CONF_ACCEPT	0x08

/*
 * Errors
 */
typedef enum {
	VRRP_SUCCESS = 0,
	VRRP_EINVAL,		/* invalid parameter */
	VRRP_EINVALVRNAME,	/* invalid router name */
	VRRP_ENOMEM,		/* no memory */
	VRRP_ENOVIRT,		/* no virtual IP addresses */
	VRRP_ENOPRIM,		/* no primary IP address */
	VRRP_ENOVNIC,		/* no vnic created */
	VRRP_ENOLINK,		/* the link does not exist */
	VRRP_EINVALLINK,	/* invalid link */
	VRRP_EINVALADDR,	/* invalid IP address */
	VRRP_EINVALAF,		/* invalid IP address familty */
	VRRP_EDB,		/* configuration error */
	VRRP_EPERM,		/* permission denied */
	VRRP_EBADSTATE,		/* VRRP router in bad state */
	VRRP_EVREXIST,		/* <vrid, intf, af> three-tuple exists */
	VRRP_EINSTEXIST,	/* router name already exists */
	VRRP_EEXIST,		/* already exists */
	VRRP_ENOTFOUND,		/* vrrp router not found */
	VRRP_ETOOSMALL,		/* too small space */
	VRRP_EAGAIN,		/* Try again */
	VRRP_EALREADY,		/* already */
	VRRP_EDLADM,		/* dladm failure */
	VRRP_EIPADM,		/* ipadm failure */
	VRRP_ESYS,		/* system error */
	VRRP_ENOSVC		/* VRRP service not enabled */
} vrrp_err_t;

/*
 * Internal commands used between vrrpadm and vrrpd.
 */
typedef enum {
	VRRP_CMD_RETURN = 0,
	VRRP_CMD_CREATE,
	VRRP_CMD_DELETE,
	VRRP_CMD_ENABLE,
	VRRP_CMD_DISABLE,
	VRRP_CMD_MODIFY,
	VRRP_CMD_LIST,
	VRRP_CMD_QUERY
} vrrp_cmd_type_t;

#define	addr_len(af) ((af) == AF_INET ? sizeof (in_addr_t): sizeof (in6_addr_t))

#define	VRRPADDR_UNSPECIFIED(af, addr) 					\
	(((af) == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED(			\
	    &(addr)->in6.sin6_addr)) || ((af) == AF_INET &&		\
	    ((addr)->in4.sin_addr.s_addr == INADDR_ANY)))

#define	VRRPADDR2STR(af, addr, abuf, size, append) {			\
	char ap[INET6_ADDRSTRLEN];					\
									\
	if (VRRPADDR_UNSPECIFIED(af, addr)) {				\
		(void) strlcpy(ap, "--", INET6_ADDRSTRLEN);		\
	} else if ((af) == AF_INET) {					\
		(void) inet_ntop((af), &(addr)->in4.sin_addr, ap,	\
		    INET6_ADDRSTRLEN);					\
	} else {							\
		(void) inet_ntop((af), &(addr)->in6.sin6_addr, ap,	\
		    INET6_ADDRSTRLEN);					\
	}								\
	if (append)							\
		(void) strlcat(abuf, ap, size);				\
	else								\
		(void) strlcpy(abuf, ap, size);				\
}

typedef struct vrrp_cmd_create_s {
	uint32_t	vcc_cmd;
	vrrp_vr_conf_t	vcc_conf;
} vrrp_cmd_create_t;

typedef struct vrrp_ret_create_s {
	vrrp_err_t	vrc_err;
} vrrp_ret_create_t;

typedef struct vrrp_cmd_delete_s {
	uint32_t	vcd_cmd;
	char		vcd_name[VRRP_NAME_MAX];
} vrrp_cmd_delete_t;

typedef struct vrrp_ret_delete_s {
	vrrp_err_t	vrd_err;
} vrrp_ret_delete_t;

typedef struct vrrp_cmd_enable_s {
	uint32_t	vcs_cmd;
	char		vcs_name[VRRP_NAME_MAX];
} vrrp_cmd_enable_t;

typedef struct vrrp_ret_enable_s {
	vrrp_err_t	vrs_err;
} vrrp_ret_enable_t;

typedef struct vrrp_cmd_disable_s {
	uint32_t	vcx_cmd;
	char		vcx_name[VRRP_NAME_MAX];
} vrrp_cmd_disable_t;

typedef struct vrrp_ret_disable_s {
	vrrp_err_t	vrx_err;
} vrrp_ret_disable_t;

typedef struct vrrp_cmd_modify_s {
	uint32_t	vcm_cmd;
	uint32_t	vcm_mask;
	vrrp_vr_conf_t	vcm_conf;
} vrrp_cmd_modify_t;

typedef struct vrrp_ret_modify_s {
	vrrp_err_t	vrm_err;
} vrrp_ret_modify_t;

typedef struct vrrp_cmd_list_s {
	uint32_t	vcl_cmd;
	vrid_t		vcl_vrid;
	char		vcl_ifname[LIFNAMSIZ];
	int		vcl_af;
} vrrp_cmd_list_t;

typedef struct vrrp_ret_list_s {
	vrrp_err_t	vrl_err;
	uint32_t	vrl_cnt;
	/*
	 * When vrl_cnt is non-zero, the return structure will be followed
	 * by the list of router names, separated by '\0'. Its size will
	 * be vrl_cnt * VRRP_NAME_MAX.
	 */
} vrrp_ret_list_t;

typedef struct vrrp_cmd_query_s {
	uint32_t	vcq_cmd;
	char		vcq_name[VRRP_NAME_MAX];
} vrrp_cmd_query_t;

typedef struct vrrp_ret_query_s {
	vrrp_err_t		vrq_err;
	vrrp_queryinfo_t	vrq_qinfo;
} vrrp_ret_query_t;

/*
 * Union of all VRRP commands
 */
typedef union vrrp_cmd_s {
	uint32_t		vc_cmd;
	vrrp_cmd_create_t	vc_cmd_create;
	vrrp_cmd_delete_t	vc_cmd_delete;
	vrrp_cmd_enable_t	vc_cmd_enable;
	vrrp_cmd_disable_t	vc_cmd_disable;
	vrrp_cmd_modify_t	vc_cmd_modify;
	vrrp_cmd_list_t		vc_cmd_list;
} vrrp_cmd_t;

/*
 * Union of all VRRP replies of the VRRP commands
 */
typedef union vrrp_ret_s {
	vrrp_err_t		vr_err;
	vrrp_ret_create_t	vr_ret_create;
	vrrp_ret_delete_t	vr_ret_delete;
	vrrp_ret_enable_t	vr_ret_enable;
	vrrp_ret_disable_t	vr_ret_disable;
	vrrp_ret_modify_t	vr_ret_modify;
	vrrp_ret_list_t		vr_ret_list;
	vrrp_ret_query_t	vr_ret_query;
} vrrp_ret_t;

/*
 * Public APIs
 */
struct vrrp_handle {
	dladm_handle_t	vh_dh;
};
typedef struct vrrp_handle *vrrp_handle_t;

const char	*vrrp_err2str(vrrp_err_t);
const char	*vrrp_state2str(vrrp_state_t);

vrrp_err_t	vrrp_open(vrrp_handle_t *);
void		vrrp_close(vrrp_handle_t);

boolean_t	vrrp_valid_name(const char *);

vrrp_err_t	vrrp_create(vrrp_handle_t, vrrp_vr_conf_t *);
vrrp_err_t	vrrp_delete(vrrp_handle_t, const char *);

vrrp_err_t	vrrp_enable(vrrp_handle_t, const char *);
vrrp_err_t	vrrp_disable(vrrp_handle_t, const char *);

vrrp_err_t	vrrp_modify(vrrp_handle_t, vrrp_vr_conf_t *, uint32_t);

vrrp_err_t	vrrp_query(vrrp_handle_t, const char *, vrrp_queryinfo_t **);

vrrp_err_t	vrrp_list(vrrp_handle_t, vrid_t, const char *, int,
		    uint32_t *, char *);

boolean_t	vrrp_is_vrrp_vnic(vrrp_handle_t, datalink_id_t,
		    datalink_id_t *, uint16_t *, vrid_t *, int *);

vrrp_err_t	vrrp_get_vnicname(vrrp_handle_t, vrid_t, int, char *,
		    datalink_id_t *, uint16_t *, char *, size_t);

#ifdef __cplusplus
}
#endif

#endif	/* _LIBVRRPADM_H */
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
#
#
# MAPFILE HEADER START
#
# WARNING:  STOP NOW.  DO NOT MODIFY THIS FILE.
# Object versioning must comply with the rules detailed in
#
#	usr/src/lib/README.mapfiles
#
# You should not be making modifications here until you've read the most current
# copy of that file. If you need help, contact a gatekeeper for guidance.
#
# MAPFILE HEADER END
#

$mapfile_version 2

SYMBOL_VERSION SUNWprivate_1.1 {
    global:
	vrrp_close;
	vrrp_create;
	vrrp_delete;
	vrrp_disable;
	vrrp_enable;
	vrrp_err2str;
	vrrp_get_vnicname;
	vrrp_is_vrrp_vnic;
	vrrp_list;
	vrrp_modify;
	vrrp_open;
	vrrp_query;
	vrrp_state2str;
	vrrp_valid_name;
	
    local:
	*;
};
/*
 * 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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_VRRP_H
#define	_VRRP_H

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct vrrp_pkt_s {
	uint8_t		vp_vers_type;
	uint8_t		vp_vrid;
	uint8_t		vp_prio;
	uint8_t		vp_ipnum;
	uint16_t	vp_rsvd_adver_int;
	uint16_t	vp_chksum;
	/* then follows <vp_ipnum> IPvX addresses */
	/* then follows NO authentification data */
} vrrp_pkt_t;

#define	IPPROTO_VRRP		112	/* IP protocol number */
#define	VRRP_AUTH_LEN		0	/* XXX length of a chunk of Auth Data */

#define	VRRP_IP_TTL		255	/* IPv4 TTL, IPv6 hop limit */
#define	VRRP_VERSION		3	/* current version */
#define	VRRP_PKT_ADVERT		1	/* packet type */
#define	VRRP_VER_MASK		0xf0	/* version mask */
#define	VRRP_TYPE_MASK		0x0f	/* packet type mask */

#define	VRRP_PRI_OWNER		255	/* priority of IP address owner */
#define	VRRP_PRI_MIN		1	/* minimum priority */
#define	VRRP_PRIO_ZERO		0	/* stop participating VRRP */
#define	VRRP_PRI_DEFAULT	VRRP_PRI_OWNER	/* default priority */

#define	VRRP_VRID_NONE		0
#define	VRRP_VRID_MIN		1
#define	VRRP_VRID_MAX		255

#define	CENTISEC2MSEC(centisec)	((centisec) * 10)
#define	MSEC2CENTISEC(msec)	((msec) / 10)

/* Max advertisement interval, in msec */
#define	VRRP_MAX_ADVER_INT_MIN	CENTISEC2MSEC(1)
#define	VRRP_MAX_ADVER_INT_MAX	CENTISEC2MSEC(4095)	/* (2^12 -1) */
#define	VRRP_MAX_ADVER_INT_DFLT	CENTISEC2MSEC(100)	/* 1 sec */

#ifdef __cplusplus
}
#endif

#endif	/* _VRRP_H */
#
# 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 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
msgid "VRRP"
msgid "vrrpd"
msgid "VNIC"
msgid "NONE"
msgid "INIT"
msgid "MASTER"
msgid "BACKUP"