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

#include <netnatm/unimsg.h>
#include <netnatm/msg/unistruct.h>
#include <netnatm/msg/unimsglib.h>
#include <netnatm/api/unisap.h>
#include <netnatm/sig/unidef.h>
#include <netnatm/api/atmapi.h>
#include <netnatm/api/ccatm.h>
#include <netnatm/api/ccpriv.h>

/*
* This file handles messages to a USER.
*/
static const char *stab[] = {
#define DEF(N) [N] = #N,
	USER_STATES
#undef DEF
};

const char *
cc_user_state2str(u_int s)
{
	if (s >= sizeof(stab) / sizeof(stab[0]) || stab[s] == NULL)
		return ("?");
	return (stab[s]);
}

static __inline void
set_state(struct ccuser *user, enum user_state ns)
{
	if (user->state != ns) {
		if (user->cc->log & CCLOG_USER_STATE)
			cc_user_log(user, "%s -> %s",
			    stab[user->state], stab[ns]);
		user->state = ns;
	}
}

static __inline void
cc_user_send(struct ccuser *user, u_int op, void *arg, size_t len)
{
	user->cc->funcs->send_user(user, user->uarg, op, arg, len);
}

static __inline void
cc_user_ok(struct ccuser *user, u_int data, void *arg, size_t len)
{
	user->cc->funcs->respond_user(user, user->uarg,
	    ATMERR_OK, data, arg, len);
}

static __inline void
cc_user_err(struct ccuser *user, int err)
{
	user->cc->funcs->respond_user(user, user->uarg,
	    err, ATMRESP_NONE, NULL, 0);
}


/**********************************************************************
*
* INSTANCE MANAGEMENT
*/
/*
* New endpoint created
*/
struct ccuser *
cc_user_create(struct ccdata *cc, void *uarg, const char *name)
{
	struct ccuser *user;

	user = CCZALLOC(sizeof(*user));
	if (user == NULL)
		return (NULL);

	user->cc = cc;
	user->state = USER_NULL;
	user->uarg = uarg;
	strncpy(user->name, name, sizeof(user->name));
	user->name[sizeof(user->name) - 1] = '\0';
	TAILQ_INIT(&user->connq);
	LIST_INSERT_HEAD(&cc->user_list, user, node_link);

	if (user->cc->log & CCLOG_USER_INST)
		cc_user_log(user, "created with name '%s'", name);

	return (user);
}

/*
 * Reset a user instance
 */
static void
cc_user_reset(struct ccuser *user)
{

	CCASSERT(TAILQ_EMPTY(&user->connq), ("connq not empty"));

	if (user->sap != NULL) {
		CCFREE(user->sap);
		user->sap = NULL;
	}

	if (user->accepted != NULL) {
		user->accepted->acceptor = NULL;
		user->accepted = NULL;
	}
	user->config = USER_P2P;
	user->queue_act = 0;
	user->queue_max = 0;
	user->aborted = 0;

	set_state(user, USER_NULL);

	cc_user_sig_flush(user);
}

static void
cc_user_abort(struct ccuser *user, const struct uni_ie_cause *cause)
{
	struct ccconn *conn;

	/*
	 * Although the standard state that 'all connections
	 * associated with this endpoint are aborted' we only
	 * have to abort the head one, because in state A6
	 * (call present) the endpoint is only associated to the
	 * head connection - the others are 'somewhere else' and
	 * need to be redispatched.
	 *
	 * First bring user into a state that the connections
	 * are not dispatched back to it.
	 */
	set_state(user, USER_NULL);
	if (!user->aborted) {
		if ((conn = TAILQ_FIRST(&user->connq)) != NULL) {
			memset(conn->cause, 0, sizeof(conn->cause));
			if (cause != NULL)
				conn->cause[0] = *cause;
			cc_conn_reset_acceptor(conn);
			cc_disconnect_from_user(conn);
			cc_conn_sig(conn, CONN_SIG_USER_ABORT, NULL);
		}
	}

	while ((conn = TAILQ_FIRST(&user->connq)) != NULL) {
		/* these should be in C21 */
		cc_disconnect_from_user(conn);
		cc_conn_dispatch(conn);
	}

	cc_user_reset(user);
}

/*
 * Application has closed this endpoint. Clean up all user resources and
 * abort all connections. This can be called in any state.
 */
void
cc_user_destroy(struct ccuser *user)
{

	if (user->cc->log & CCLOG_USER_INST)
		cc_user_log(user, "destroy '%s'", user->name);

	cc_user_abort(user, NULL);

	if (user->sap != NULL)
		CCFREE(user->sap);

	cc_user_sig_flush(user);

	LIST_REMOVE(user, node_link);
	CCFREE(user);
}

/**********************************************************************
 *
 * OUTGOING CALLS
 */
/*
 * Return true when the calling address of the connection matches the address.
 */
static int
addr_matches(const struct ccaddr *addr, const struct ccconn *conn)
{

	if (!IE_ISPRESENT(conn->calling))
		return (0);

	return (addr->addr.type == conn->calling.addr.type &&
	    addr->addr.plan == conn->calling.addr.plan &&
	    addr->addr.len == conn->calling.addr.len &&
	    memcmp(addr->addr.addr, conn->calling.addr.addr,
	    addr->addr.len) == 0);
}

/*
 * Check if the user's SAP (given he is in the right state) and
 * the given SAP overlap
 */
static int
check_overlap(struct ccuser *user, struct uni_sap *sap)
{
	return ((user->state == USER_IN_PREPARING ||
	    user->state == USER_IN_WAITING) &&
	    unisve_overlap_sap(user->sap, sap));
}

/*
 * Send arrival notification to user
 */
static void
do_arrival(struct ccuser *user)
{
	struct ccconn *conn;

	user->aborted = 0;
	if ((conn = TAILQ_FIRST(&user->connq)) != NULL) {
		set_state(user, USER_IN_ARRIVED);
		cc_user_send(user, ATMOP_ARRIVAL_OF_INCOMING_CALL, NULL, 0);
		cc_conn_sig(conn, CONN_SIG_ARRIVAL, NULL);
	}
}

/**********************************************************************
 *
 * ATTRIBUTES
 */
/*
 * Query an attribute. This is possible only in some states: preparation
 * of an outgoing call, after an incoming call was offered to the application
 * and in the three active states (P2P, P2PLeaf, P2PRoot).
 */
static struct ccconn *
cc_query_check(struct ccuser *user)
{

	switch (user->state) {

	  case USER_OUT_PREPARING:
	  case USER_IN_ARRIVED:
	  case USER_ACTIVE:
		return (TAILQ_FIRST(&user->connq));

	  case USER_NULL:
		/* if we are waiting for the SETUP_confirm, we are in
		 * the NULL state still (we are the new endpoint), but
		 * have a connection in 'accepted' that is in the
		 * CONN_IN_WAIT_ACCEPT_OK state.
		 */
		if (user->accepted != NULL &&
		    user->accepted->state == CONN_IN_WAIT_ACCEPT_OK)
			return (user->accepted);
		/* FALLTHRU */

	  default:
		return (NULL);
	}
}

/*
 * Query attributes
 */
static void
cc_attr_query(struct ccuser *user, struct ccconn *conn,
    uint32_t *attr, u_int count)
{
	void *val, *ptr;
	size_t total, len;
	u_int i;
	uint32_t *atab;

	/* determine the length of the total attribute buffer */
	total = sizeof(uint32_t) + count * sizeof(uint32_t);
	for (i = 0; i < count; i++) {
		len = 0;
		switch ((enum atm_attribute)attr[i]) {

		  case ATM_ATTR_NONE:
			break;

		  case ATM_ATTR_BLLI_SELECTOR:
			len = sizeof(uint32_t);
			break;

		  case ATM_ATTR_BLLI:
			len = sizeof(struct uni_ie_blli);
			break;

		  case ATM_ATTR_BEARER:
			len = sizeof(struct uni_ie_bearer);
			break;

		  case ATM_ATTR_TRAFFIC:
			len = sizeof(struct uni_ie_traffic);
			break;

		  case ATM_ATTR_QOS:
			len = sizeof(struct uni_ie_qos);
			break;

		  case ATM_ATTR_EXQOS:
			len = sizeof(struct uni_ie_exqos);
			break;

		  case ATM_ATTR_CALLED:
			len = sizeof(struct uni_ie_called);
			break;

		  case ATM_ATTR_CALLEDSUB:
			len = sizeof(struct uni_ie_calledsub);
			break;

		  case ATM_ATTR_CALLING:
			len = sizeof(struct uni_ie_calling);
			break;

		  case ATM_ATTR_CALLINGSUB:
			len = sizeof(struct uni_ie_callingsub);
			break;

		  case ATM_ATTR_AAL:
			len = sizeof(struct uni_ie_aal);
			break;

		  case ATM_ATTR_EPREF:
			len = sizeof(struct uni_ie_epref);
			break;

		  case ATM_ATTR_CONNED:
			len = sizeof(struct uni_ie_conned);
			break;

		  case ATM_ATTR_CONNEDSUB:
			len = sizeof(struct uni_ie_connedsub);
			break;

		  case ATM_ATTR_EETD:
			len = sizeof(struct uni_ie_eetd);
			break;

		  case ATM_ATTR_ABRSETUP:
			len = sizeof(struct uni_ie_abrsetup);
			break;

		  case ATM_ATTR_ABRADD:
			len = sizeof(struct uni_ie_abradd);
			break;

		  case ATM_ATTR_CONNID:
			len = sizeof(struct uni_ie_connid);
			break;

		  case ATM_ATTR_MDCR:
			len = sizeof(struct uni_ie_mdcr);
			break;
		}
		if (len == 0) {
			cc_user_err(user, ATMERR_BAD_ATTR);
			return;
		}
		total += len;
	}

	/* allocate buffer */
	val = CCMALLOC(total);
	if (val == NULL)
		return;

	atab = val;
	atab[0] = count;

	/* fill */
	ptr = (u_char *)val + (sizeof(uint32_t) + count * sizeof(uint32_t));
	for (i = 0; i < count; i++) {
		len = 0;
		atab[i + 1] = attr[i];
		switch (attr[i]) {

		  case ATM_ATTR_NONE:
			break;

		  case ATM_ATTR_BLLI_SELECTOR:
			len = sizeof(uint32_t);
			memcpy(ptr, &conn->blli_selector, len);
			break;

		  case ATM_ATTR_BLLI:
			/* in A6 the blli_selector may be 0 when
			 * there was no blli in the SETUP.
			 */
			len = sizeof(struct uni_ie_blli);
			if (conn->blli_selector == 0)
				memset(ptr, 0, len);
			else
				memcpy(ptr, &conn->blli[conn->blli_selector -
				    1], len);
			break;

		  case ATM_ATTR_BEARER:
			len = sizeof(struct uni_ie_bearer);
			memcpy(ptr, &conn->bearer, len);
			break;

		  case ATM_ATTR_TRAFFIC:
			len = sizeof(struct uni_ie_traffic);
			memcpy(ptr, &conn->traffic, len);
			break;

		  case ATM_ATTR_QOS:
			len = sizeof(struct uni_ie_qos);
			memcpy(ptr, &conn->qos, len);
			break;

		  case ATM_ATTR_EXQOS:
			len = sizeof(struct uni_ie_exqos);
			memcpy(ptr, &conn->exqos, len);
			break;

		  case ATM_ATTR_CALLED:
			len = sizeof(struct uni_ie_called);
			memcpy(ptr, &conn->called, len);
			break;

		  case ATM_ATTR_CALLEDSUB:
			len = sizeof(struct uni_ie_calledsub);
			memcpy(ptr, &conn->calledsub, len);
			break;

		  case ATM_ATTR_CALLING:
			len = sizeof(struct uni_ie_calling);
			memcpy(ptr, &conn->calling, len);
			break;

		  case ATM_ATTR_CALLINGSUB:
			len = sizeof(struct uni_ie_callingsub);
			memcpy(ptr, &conn->callingsub, len);
			break;

		  case ATM_ATTR_AAL:
			len = sizeof(struct uni_ie_aal);
			memcpy(ptr, &conn->aal, len);
			break;

		  case ATM_ATTR_EPREF:
			len = sizeof(struct uni_ie_epref);
			memcpy(ptr, &conn->epref, len);
			break;

		  case ATM_ATTR_CONNED:
			len = sizeof(struct uni_ie_conned);
			memcpy(ptr, &conn->conned, len);
			break;

		  case ATM_ATTR_CONNEDSUB:
			len = sizeof(struct uni_ie_connedsub);
			memcpy(ptr, &conn->connedsub, len);
			break;

		  case ATM_ATTR_EETD:
			len = sizeof(struct uni_ie_eetd);
			memcpy(ptr, &conn->eetd, len);
			break;

		  case ATM_ATTR_ABRSETUP:
			len = sizeof(struct uni_ie_abrsetup);
			memcpy(ptr, &conn->abrsetup, len);
			break;

		  case ATM_ATTR_ABRADD:
			len = sizeof(struct uni_ie_abradd);
			memcpy(ptr, &conn->abradd, len);
			break;

		  case ATM_ATTR_CONNID:
			len = sizeof(struct uni_ie_connid);
			memcpy(ptr, &conn->connid, len);
			break;

		  case ATM_ATTR_MDCR:
			len = sizeof(struct uni_ie_mdcr);
			memcpy(ptr, &conn->mdcr, len);
			break;
		}
		ptr = (u_char *)ptr + len;
	}

	cc_user_ok(user, ATMRESP_ATTRS, val, total);

	CCFREE(val);
}

/*
 * Check whether the state is ok and return the connection
 */
static struct ccconn *
cc_set_check(struct ccuser *user)
{
	switch(user->state) {

	  case USER_OUT_PREPARING:
	  case USER_IN_ARRIVED:
		return (TAILQ_FIRST(&user->connq));

	  default:
		return (NULL);
	}
}

/*
 * Set connection attribute(s)
 */
static void
cc_attr_set(struct ccuser *user, struct ccconn *conn, uint32_t *attr,
    u_int count, u_char *val, size_t vallen)
{
	size_t total, len;
	u_int i;
	u_char *ptr;

	/* determine the length of the total attribute buffer */
	total = 0;
	ptr = val;
	for (i = 0; i < count; i++) {
		len = 0;
		switch ((enum atm_attribute)attr[i]) {

		  case ATM_ATTR_NONE:
			break;

		  case ATM_ATTR_BLLI_SELECTOR:
		    {
			uint32_t sel;

			if (conn->state != CONN_OUT_PREPARING)
				goto rdonly;
			memcpy(&sel, ptr, sizeof(sel));
			if (sel == 0 || sel > UNI_NUM_IE_BLLI)
				goto bad_val;
			len = sizeof(uint32_t);
			break;
		    }

		  case ATM_ATTR_BLLI:
			len = sizeof(struct uni_ie_blli);
			break;

		  case ATM_ATTR_BEARER:
			if (conn->state != CONN_OUT_PREPARING)
				goto rdonly;
			len = sizeof(struct uni_ie_bearer);
			break;

		  case ATM_ATTR_TRAFFIC:
			len = sizeof(struct uni_ie_traffic);
			break;

		  case ATM_ATTR_QOS:
			if (conn->state != CONN_OUT_PREPARING)
				goto rdonly;
			len = sizeof(struct uni_ie_qos);
			break;

		  case ATM_ATTR_EXQOS:
			len = sizeof(struct uni_ie_exqos);
			break;

		  case ATM_ATTR_CALLED:
			goto rdonly;

		  case ATM_ATTR_CALLEDSUB:
			if (conn->state != CONN_OUT_PREPARING)
				goto rdonly;
			len = sizeof(struct uni_ie_calledsub);
			break;

		  case ATM_ATTR_CALLING:
			if (conn->state != CONN_OUT_PREPARING)
				goto rdonly;
			len = sizeof(struct uni_ie_calling);
			break;

		  case ATM_ATTR_CALLINGSUB:
			if (conn->state != CONN_OUT_PREPARING)
				goto rdonly;
			len = sizeof(struct uni_ie_callingsub);
			break;

		  case ATM_ATTR_AAL:
			len = sizeof(struct uni_ie_aal);
			break;

		  case ATM_ATTR_EPREF:
			goto rdonly;

		  case ATM_ATTR_CONNED:
			goto rdonly;

		  case ATM_ATTR_CONNEDSUB:
			goto rdonly;

		  case ATM_ATTR_EETD:
			len = sizeof(struct uni_ie_eetd);
			break;

		  case ATM_ATTR_ABRSETUP:
			len = sizeof(struct uni_ie_abrsetup);
			break;

		  case ATM_ATTR_ABRADD:
			len = sizeof(struct uni_ie_abradd);
			break;

		  case ATM_ATTR_CONNID:
			len = sizeof(struct uni_ie_connid);
			break;

		  case ATM_ATTR_MDCR:
			if (conn->state != CONN_OUT_PREPARING)
				goto rdonly;
			len = sizeof(struct uni_ie_mdcr);
			break;
		}
		if (len == 0) {
			cc_user_err(user, ATMERR_BAD_ATTR);
			return;
		}
		total += len;
		ptr += len;
	}

	/* check the length */
	if (vallen != total) {
		cc_user_err(user, ATMERR_BAD_ARGS);
		return;
	}

	ptr = val;
	for (i = 0; i < count; i++) {
		len = 0;
		switch ((enum atm_attribute)attr[i]) {

		  case ATM_ATTR_NONE:
			break;

		  case ATM_ATTR_BLLI_SELECTOR:
		    {
			uint32_t sel;

			memcpy(&sel, ptr, sizeof(sel));
			conn->blli_selector = sel;
			len = sizeof(uint32_t);
			break;
		    }

		  case ATM_ATTR_BLLI:
			len = sizeof(struct uni_ie_blli);
			memcpy(&conn->blli[conn->blli_selector - 1], ptr, len);
			conn->dirty_attr |= CCDIRTY_BLLI;
			break;

		  case ATM_ATTR_BEARER:
			len = sizeof(struct uni_ie_bearer);
			memcpy(&conn->bearer, ptr, len);
			break;

		  case ATM_ATTR_TRAFFIC:
			len = sizeof(struct uni_ie_traffic);
			memcpy(&conn->traffic, ptr, len);
			conn->dirty_attr |= CCDIRTY_TRAFFIC;
			break;

		  case ATM_ATTR_QOS:
			len = sizeof(struct uni_ie_qos);
			memcpy(&conn->qos, ptr, len);
			break;

		  case ATM_ATTR_EXQOS:
			len = sizeof(struct uni_ie_exqos);
			memcpy(&conn->exqos, ptr, len);
			conn->dirty_attr |= CCDIRTY_EXQOS;
			break;

		  case ATM_ATTR_CALLED:
			len = sizeof(struct uni_ie_called);
			break;

		  case ATM_ATTR_CALLEDSUB:
			len = sizeof(struct uni_ie_calledsub);
			memcpy(&conn->calledsub, ptr, len);
			break;

		  case ATM_ATTR_CALLING:
			len = sizeof(struct uni_ie_calling);
			memcpy(&conn->calling, ptr, len);
			break;

		  case ATM_ATTR_CALLINGSUB:
			len = sizeof(struct uni_ie_callingsub);
			memcpy(&conn->callingsub, ptr, len);
			break;

		  case ATM_ATTR_AAL:
			len = sizeof(struct uni_ie_aal);
			memcpy(&conn->aal, ptr, len);
			conn->dirty_attr |= CCDIRTY_AAL;
			break;

		  case ATM_ATTR_EPREF:
			len = sizeof(struct uni_ie_epref);
			break;

		  case ATM_ATTR_CONNED:
			len = sizeof(struct uni_ie_conned);
			break;

		  case ATM_ATTR_CONNEDSUB:
			len = sizeof(struct uni_ie_connedsub);
			break;

		  case ATM_ATTR_EETD:
			len = sizeof(struct uni_ie_eetd);
			memcpy(&conn->eetd, ptr, len);
			conn->dirty_attr |= CCDIRTY_EETD;
			break;

		  case ATM_ATTR_ABRSETUP:
			len = sizeof(struct uni_ie_abrsetup);
			memcpy(&conn->abrsetup, ptr, len);
			conn->dirty_attr |= CCDIRTY_ABRSETUP;
			break;

		  case ATM_ATTR_ABRADD:
			len = sizeof(struct uni_ie_abradd);
			memcpy(&conn->abradd, ptr, len);
			conn->dirty_attr |= CCDIRTY_ABRADD;
			break;

		  case ATM_ATTR_CONNID:
			len = sizeof(struct uni_ie_connid);
			memcpy(&conn->connid, ptr, len);
			conn->dirty_attr |= CCDIRTY_CONNID;
			break;

		  case ATM_ATTR_MDCR:
			len = sizeof(struct uni_ie_mdcr);
			memcpy(&conn->mdcr, ptr, len);
			break;
		}
		ptr += len;
	}

	cc_user_ok(user, ATMRESP_NONE, NULL, 0);
	return;

  bad_val:
	cc_user_err(user, ATMERR_BAD_VALUE);
	return;

  rdonly:
	cc_user_err(user, ATMERR_RDONLY);
	return;
}

#ifdef CCATM_DEBUG
static const char *op_names[] = {
#define	S(OP)	[ATMOP_##OP] = #OP
	S(RESP),
	S(ABORT_CONNECTION),
	S(ACCEPT_INCOMING_CALL),
	S(ADD_PARTY),
	S(ADD_PARTY_REJECT),
	S(ADD_PARTY_SUCCESS),
	S(ARRIVAL_OF_INCOMING_CALL),
	S(CALL_RELEASE),
	S(CONNECT_OUTGOING_CALL),
	S(DROP_PARTY),
	S(GET_LOCAL_PORT_INFO),
	S(P2MP_CALL_ACTIVE),
	S(P2P_CALL_ACTIVE),
	S(PREPARE_INCOMING_CALL),
	S(PREPARE_OUTGOING_CALL),
	S(QUERY_CONNECTION_ATTRIBUTES),
	S(REJECT_INCOMING_CALL),
	S(SET_CONNECTION_ATTRIBUTES),
	S(WAIT_ON_INCOMING_CALL),
	S(SET_CONNECTION_ATTRIBUTES_X),
	S(QUERY_CONNECTION_ATTRIBUTES_X),
	S(QUERY_STATE),
#undef S
};
#endif

/*
 * Signal from user - map this to our internal signals and queue
 * the mapped signal.
 */
int
cc_user_signal(struct ccuser *user, enum atmop sig, struct uni_msg *msg)
{
	size_t len = uni_msg_len(msg);
	int err = EINVAL;

	if (user->cc->log & CCLOG_USER_SIG)
		cc_user_log(user, "signal %s to user", op_names[sig]);

	if ((u_int)sig > ATMOP_QUERY_STATE)
		goto bad_signal;

	switch (sig) {

	  case ATMOP_ABORT_CONNECTION:
		if (len != sizeof(struct atm_abort_connection))
			goto bad_len;
		err = cc_user_sig_msg(user, USER_SIG_ABORT_CONNECTION, msg);
		break;

	  case ATMOP_ACCEPT_INCOMING_CALL:
		if (len != sizeof(struct atm_accept_incoming_call))
			goto bad_len;
		err = cc_user_sig_msg(user, USER_SIG_ACCEPT_INCOMING, msg);
		break;

	  case ATMOP_ADD_PARTY:
		if (len != sizeof(struct atm_add_party))
			goto bad_len;
		err = cc_user_sig_msg(user, USER_SIG_ADD_PARTY, msg);
		break;

	  case ATMOP_CALL_RELEASE:
		if (len != sizeof(struct atm_call_release))
			goto bad_len;
		err = cc_user_sig_msg(user, USER_SIG_CALL_RELEASE, msg);
		break;

	  case ATMOP_CONNECT_OUTGOING_CALL:
		if (len != sizeof(struct atm_connect_outgoing_call))
			goto bad_len;
		err = cc_user_sig_msg(user, USER_SIG_CONNECT_OUTGOING, msg);
		break;

	  case ATMOP_DROP_PARTY:
		if (len != sizeof(struct atm_drop_party))
			goto bad_len;
		err = cc_user_sig_msg(user, USER_SIG_DROP_PARTY, msg);
		break;

	  case ATMOP_GET_LOCAL_PORT_INFO:
		if (len != sizeof(struct atm_get_local_port_info))
			goto bad_len;
		err = cc_user_sig_msg(user, USER_SIG_GET_LOCAL_PORT_INFO, msg);
		break;

	  case ATMOP_PREPARE_INCOMING_CALL:
		if (len != sizeof(struct atm_prepare_incoming_call))
			goto bad_len;
		err = cc_user_sig_msg(user, USER_SIG_PREPARE_INCOMING, msg);
		break;

	  case ATMOP_PREPARE_OUTGOING_CALL:
		if (len != 0)
			goto bad_len;
		uni_msg_destroy(msg);
		err = cc_user_sig(user, USER_SIG_PREPARE_OUTGOING, NULL, 0);
		break;

	  case ATMOP_QUERY_CONNECTION_ATTRIBUTES:
		if (len != sizeof(struct atm_query_connection_attributes))
			goto bad_len;
		err = cc_user_sig_msg(user, USER_SIG_QUERY_ATTR, msg);
		break;

	  case ATMOP_REJECT_INCOMING_CALL:
		if (len != sizeof(struct atm_reject_incoming_call))
			goto bad_len;
		err = cc_user_sig_msg(user, USER_SIG_REJECT_INCOMING, msg);
		break;

	  case ATMOP_SET_CONNECTION_ATTRIBUTES:
		if (len < sizeof(struct atm_set_connection_attributes))
			goto bad_len;
		err = cc_user_sig_msg(user, USER_SIG_SET_ATTR, msg);
		break;

	  case ATMOP_WAIT_ON_INCOMING_CALL:
		if (len != 0)
			goto bad_len;
		uni_msg_destroy(msg);
		err = cc_user_sig(user, USER_SIG_WAIT_ON_INCOMING, NULL, 0);
		break;

	  case ATMOP_QUERY_CONNECTION_ATTRIBUTES_X:
		if (len < sizeof(struct atm_set_connection_attributes_x) ||
		    len != offsetof(struct atm_set_connection_attributes_x,
		    attr) + uni_msg_rptr(msg,
		    struct atm_set_connection_attributes_x *)->count *
		    sizeof(uint32_t))
			goto bad_len;
		err = cc_user_sig_msg(user, USER_SIG_QUERY_ATTR_X, msg);
		break;

	  case ATMOP_SET_CONNECTION_ATTRIBUTES_X:
		if (len < sizeof(struct atm_set_connection_attributes_x))
			goto bad_len;
		err = cc_user_sig_msg(user, USER_SIG_SET_ATTR_X, msg);
		break;

	  case ATMOP_QUERY_STATE:
		if (len != 0)
			goto bad_len;
		uni_msg_destroy(msg);
		err = cc_user_sig(user, USER_SIG_QUERY_STATE, NULL, 0);
		break;

	  case ATMOP_RESP:
	  case ATMOP_ADD_PARTY_REJECT:
	  case ATMOP_ADD_PARTY_SUCCESS:
	  case ATMOP_ARRIVAL_OF_INCOMING_CALL:
	  case ATMOP_P2MP_CALL_ACTIVE:
	  case ATMOP_P2P_CALL_ACTIVE:
	  bad_signal:
		/* bad signal */
		if (user->cc->log & CCLOG_USER_SIG)
			cc_user_log(user, "bad signal %u", sig);
		cc_user_err(user, ATMERR_BAD_OP);
		uni_msg_destroy(msg);
		break;
	}
	return (err);

  bad_len:
	/* bad argument length */
	if (user->cc->log & CCLOG_USER_SIG)
		cc_user_log(user, "signal %s had bad len=%zu",
		    op_names[sig], len);
	cc_user_err(user, ATMERR_BAD_ARGS);
	uni_msg_destroy(msg);
	return (EINVAL);
}

/*
 * Send active signal to user
 */
static void
cc_user_active(struct ccuser *user)
{
	struct ccconn *conn = TAILQ_FIRST(&user->connq);

	set_state(user, USER_ACTIVE);
	if (conn->bearer.cfg == UNI_BEARER_P2P) {
		struct atm_p2p_call_active *act;

		user->config = USER_P2P;
		act = CCZALLOC(sizeof(*act));
		if (act == NULL)
			return;
		act->connid = conn->connid;
		cc_user_send(user, ATMOP_P2P_CALL_ACTIVE, act, sizeof(*act));
		CCFREE(act);
	} else {
		struct atm_p2mp_call_active *act;

		user->config = USER_ROOT;
		act = CCZALLOC(sizeof(*act));
		if (act == NULL)
			return;
		act->connid = conn->connid;
		cc_user_send(user, ATMOP_P2MP_CALL_ACTIVE, act, sizeof(*act));
		CCFREE(act);
	}
}

/*
* Handle a signal to this user
*/
void
cc_user_sig_handle(struct ccuser *user, enum user_sig sig,
    void *arg, u_int arg2)
{

	if (user->cc->log & CCLOG_USER_SIG)
		cc_user_log(user, "signal %s to user state %s",
		    cc_user_sigtab[sig], stab[user->state]);

	switch (sig) {


	  case USER_SIG_PREPARE_OUTGOING:
	    {
		/*
		 * Here we create a connection for the call we soon will make.
		 * We put this call on the list of orphaned connections,
		 * because we don't know yet, which port will get the
		 * connection. It is assigned, when the user issues the call
		 * to connect.
		 */
		struct ccconn *conn;

		if (user->state != USER_NULL) {
			cc_user_err(user, ATMERR_BAD_STATE);
			goto bad_state;
		}
		conn = cc_conn_create(user->cc);
		if (conn == NULL) {
			cc_user_err(user, ATMERR_NOMEM);
			return;
		}
		set_state(user, USER_OUT_PREPARING);
		cc_conn_set_state(conn, CONN_OUT_PREPARING);
		conn->blli_selector = 1;
		cc_connect_to_user(conn, user);

		cc_user_ok(user, ATMRESP_NONE, NULL, 0);
		return;
	    }


	  case USER_SIG_CONNECT_OUTGOING:
	    {
		/*
		 * Request to connect that call
		 *
		 * Here we assign the connection to a port.
		 */
		struct uni_msg *msg = arg;
		struct atm_connect_outgoing_call *req = uni_msg_rptr(msg,
		    struct atm_connect_outgoing_call *);
		struct ccdata *priv = user->cc;
		struct ccport *port;
		struct ccaddr *addr;
		struct ccconn *conn = TAILQ_FIRST(&user->connq);

		if (user->state != USER_OUT_PREPARING) {
			uni_msg_destroy(msg);
			cc_user_err(user, ATMERR_BAD_STATE);
			goto bad_state;
		}
		if (!IE_ISPRESENT(req->called)) {
			uni_msg_destroy(msg);
			cc_user_err(user, ATMERR_BAD_ARGS);
			return;
		}
		CCASSERT(conn->port == NULL, ("connection still on port"));

		if (TAILQ_EMPTY(&priv->port_list)) {
			/*
			 * We have no ports - reject
			 */
			uni_msg_destroy(msg);
			cc_user_err(user, ATMERR_BAD_PORT);
			return;
		}

		/*
		 * Find the correct port
		 * Routing of outgoing calls goes to the lowest numbered port
		 * with a matching address or, if no address match is found to
		 * the lowest numbered port.
		 */
		TAILQ_FOREACH(port, &priv->port_list, node_link)
			TAILQ_FOREACH(addr, &port->addr_list, port_link)
				if (addr_matches(addr, conn))
					break;

		if (port == NULL)
			port = TAILQ_FIRST(&priv->port_list);

		cc_conn_ins_port(conn, port);
		conn->called = req->called;
		uni_msg_destroy(msg);

		/*
		 * Now move the state
		 */
		set_state(user, USER_OUT_WAIT_OK);
		cc_conn_sig(conn, CONN_SIG_CONNECT_OUTGOING, NULL);

		return;
	    }


	  case USER_SIG_CONNECT_OUTGOING_ERR:
		switch (user->state) {

		  case USER_OUT_WAIT_OK:
			set_state(user, USER_OUT_PREPARING);
			cc_user_err(user, arg2);
			break;

		  case USER_REL_WAIT_CONN:
		    {
			struct ccconn *conn;

			conn = TAILQ_FIRST(&user->connq);
			if (conn != NULL) {
				cc_disconnect_from_user(conn);
				cc_conn_destroy(conn);
			}

			cc_user_reset(user);
			cc_user_ok(user, ATMRESP_NONE, NULL, 0);
			break;
		    }

		  default:
			goto bad_state;
		}
		return;


	  case USER_SIG_CONNECT_OUTGOING_OK:
		switch (user->state) {

		  case USER_OUT_WAIT_OK:
			set_state(user, USER_OUT_WAIT_CONF);
			cc_user_ok(user, ATMRESP_NONE, NULL, 0);
			break;

		  case USER_REL_WAIT_CONN:
			set_state(user, USER_REL_WAIT_SCONF);
			break;

		  default:
			goto bad_state;
		}
		return;


	  case USER_SIG_SETUP_CONFIRM:
		/*
		 * SETUP.confirm from UNI stack.
		 */
		switch (user->state) {

		  case USER_OUT_WAIT_CONF:
			cc_user_active(user);
			break;

		  case USER_REL_WAIT_SCONF:
			/* now try to release */
			set_state(user, USER_REL_WAIT_CONF);
			cc_conn_sig(TAILQ_FIRST(&user->connq),
			    CONN_SIG_RELEASE, NULL);
			break;

		  default:
			goto bad_state;
		}
		return;


	  case USER_SIG_PREPARE_INCOMING:
	    {
		struct uni_msg *msg = arg;
		struct ccuser *ptr;
		struct atm_prepare_incoming_call *prep = uni_msg_rptr(msg,
		    struct atm_prepare_incoming_call *);

		if (user->state != USER_NULL) {
			uni_msg_destroy(msg);
			cc_user_err(user, ATMERR_BAD_STATE);
			goto bad_state;
		}

		/*
		 * Check the SAP
		 */
		if (unisve_check_sap(&prep->sap) != UNISVE_OK) {
			uni_msg_destroy(msg);
			cc_user_err(user, ATMERR_BAD_SAP);
			return;
		}

		/*
		 * Loop through all incoming calls and check whether there
		 * is an overlap in SAP space.
		 */
		LIST_FOREACH(ptr, &user->cc->user_list, node_link) {
			if (check_overlap(ptr, &prep->sap)) {
				uni_msg_destroy(msg);
				cc_user_err(user, ATMERR_OVERLAP);
				return;
			}
		}

		/*
		 * Save info and set state
		 */
		user->sap = CCZALLOC(sizeof(struct uni_sap));
		if (user->sap == NULL) {
			uni_msg_destroy(msg);
			cc_user_err(user, ATMERR_NOMEM);
			return;
		}
		*user->sap = prep->sap;
		user->queue_max = prep->queue_size;
		user->queue_act = 0;
		uni_msg_destroy(msg);

		set_state(user, USER_IN_PREPARING);
		cc_user_ok(user, ATMRESP_NONE, NULL, 0);

		return;
	    }


	  case USER_SIG_WAIT_ON_INCOMING:
		if (user->state != USER_IN_PREPARING) {
			cc_user_err(user, ATMERR_BAD_STATE);
			goto bad_state;
		}

		set_state(user, USER_IN_WAITING);
		cc_user_ok(user, ATMRESP_NONE, NULL, 0);
		return;


	  case USER_SIG_SETUP_IND:
		/*
		 * New connection queued up in the queue. If this is the
		 * first one, inform the application of the arrival.
		 */
		switch (user->state) {

		  case USER_IN_WAITING:
			do_arrival(user);
			break;

		  case USER_IN_ARRIVED:
		  case USER_IN_WAIT_REJ:
		  case USER_IN_WAIT_ACC:
			break;

		  default:
			goto bad_state;
		}
		return;


	  case USER_SIG_REJECT_INCOMING:
	     {
		/*
		 * User rejects call. This is done on the OLD user
		 * (i.e. the one sending the arrival).
		 */
		struct uni_msg *msg = arg;
		struct atm_reject_incoming_call *rej = uni_msg_rptr(msg,
		    struct atm_reject_incoming_call *);
		struct ccconn *conn = TAILQ_FIRST(&user->connq);

		if (user->state != USER_IN_ARRIVED) {
			uni_msg_destroy(msg);
			cc_user_err(user, ATMERR_BAD_STATE);
			goto bad_state;
		}
		if (user->aborted) {
			/* connection has disappeared. Send an ok
			 * to the user and lock whether there is another
			 * connection at this endpoint */
			uni_msg_destroy(msg);
			cc_user_ok(user, ATMRESP_NONE, NULL, 0);

			set_state(user, USER_IN_WAITING);
			do_arrival(user);
			return;
		}
		conn->cause[0] = rej->cause;
		memset(&conn->cause[1], 0, sizeof(conn->cause[1]));
		uni_msg_destroy(msg);

		set_state(user, USER_IN_WAIT_REJ);
		cc_conn_sig(conn, CONN_SIG_REJECT, NULL);

		return;
	    }


	  case USER_SIG_REJECT_OK:
		if (user->state != USER_IN_WAIT_REJ)
			goto bad_state;
		cc_user_ok(user, ATMRESP_NONE, NULL, 0);

		set_state(user, USER_IN_WAITING);
		do_arrival(user);
		return;


	  case USER_SIG_REJECT_ERR:
		if (user->state != USER_IN_WAIT_REJ)
			goto bad_state;
		cc_user_err(user, arg2);

		if (arg == NULL)
			set_state(user, USER_IN_ARRIVED);
		else {
			set_state(user, USER_IN_WAITING);
			do_arrival(user);
		}
		return;


	  case USER_SIG_ACCEPT_INCOMING:
	    {
		/*
		 * User accepts call. This is done on the OLD user (i.e. the one
		 * sending the arrival), the message contains a pointer to the
		 * new endpoint.
		 */
		struct uni_msg *msg = arg;
		struct atm_accept_incoming_call *acc =
		    uni_msg_rptr(msg, struct atm_accept_incoming_call *);
		struct ccuser *newep;

		if (user->state != USER_IN_ARRIVED) {
			uni_msg_destroy(msg);
			cc_user_err(user, ATMERR_BAD_STATE);
			return;
		}
		if (user->aborted) {
			/* connection has disappeared. Send an error
			 * to the user and lock whether there is another
			 * connection at this endpoint */
			uni_msg_destroy(msg);
			cc_user_err(user, ATMERR_PREVIOUSLY_ABORTED);

			set_state(user, USER_IN_WAITING);
			do_arrival(user);
			return;
		}
		acc->newep[sizeof(acc->newep) - 1] = '\0';

		LIST_FOREACH(newep, &user->cc->user_list, node_link)
			if (strcmp(acc->newep, newep->name) == 0)
				break;
		uni_msg_destroy(msg);

		if (newep == NULL) {
			cc_user_err(user, ATMERR_BAD_ENDPOINT);
			return;
		}

		if (newep->state != USER_NULL || newep->accepted != NULL) {
			cc_user_err(user, ATMERR_BAD_STATE);
			return;
		}

		set_state(user, USER_IN_WAIT_ACC);
		cc_conn_sig(TAILQ_FIRST(&user->connq), CONN_SIG_ACCEPT, newep);

		return;
	    }


	  case USER_SIG_ACCEPT_OK:
		if (user->state != USER_IN_WAIT_ACC)
			goto bad_state;
		cc_user_ok(user, ATMRESP_NONE, NULL, 0);

		set_state(user, USER_IN_WAITING);
		do_arrival(user);
		return;


	  case USER_SIG_ACCEPT_ERR:
		if (user->state != USER_IN_WAIT_ACC)
			goto bad_state;
		cc_user_err(user, arg2);

		if (arg == NULL) {
			/* arg used as flag! */
			set_state(user, USER_IN_ARRIVED);
		} else {
			set_state(user, USER_IN_WAITING);
			do_arrival(user);
		}
		return;


	  case USER_SIG_ACCEPTING:
		if (user->state != USER_NULL)
			goto bad_state;
		set_state(user, USER_IN_ACCEPTING);
		return;


	  case USER_SIG_SETUP_COMPL:
	    {
		struct ccconn *conn = TAILQ_FIRST(&user->connq);

		if (user->state != USER_IN_ACCEPTING)
			goto bad_state;

		user->state = USER_ACTIVE;
		if (conn->bearer.cfg == UNI_BEARER_P2P) {
			struct atm_p2p_call_active *act;

			user->config = USER_P2P;
			act = CCZALLOC(sizeof(*act));
			if (act == NULL)
				return;
			act->connid = conn->connid;
			cc_user_send(user, ATMOP_P2P_CALL_ACTIVE,
			    act, sizeof(*act));
			CCFREE(act);
		} else {
			struct atm_p2mp_call_active *act;

			user->config = USER_LEAF;
			act = CCZALLOC(sizeof(*act));
			if (act == NULL)
				return;
			act->connid = conn->connid;
			cc_user_send(user, ATMOP_P2MP_CALL_ACTIVE,
			    act, sizeof(*act));
			CCFREE(act);
		}
		return;
	    }


	  case USER_SIG_CALL_RELEASE:
	    {
		struct uni_msg *msg = arg;
		struct atm_call_release *api = uni_msg_rptr(msg,
		    struct atm_call_release *);
		struct ccconn *conn;

		conn = TAILQ_FIRST(&user->connq);
		switch (user->state) {

		  case USER_OUT_WAIT_OK:	/* U2/A3 */
			/* wait for CONN_OK first */
			conn->cause[0] = api->cause[0];
			conn->cause[1] = api->cause[1];
			set_state(user, USER_REL_WAIT_CONN);
			break;

		  case USER_OUT_WAIT_CONF:	/* U3/A3 */
			/* wait for SETUP.confirm first */
			conn->cause[0] = api->cause[0];
			conn->cause[1] = api->cause[1];
			set_state(user, USER_REL_WAIT_SCONF);
			break;

		  case USER_IN_ACCEPTING:	/* U11/A7 */
			conn->cause[0] = api->cause[0];
			conn->cause[1] = api->cause[1];
			set_state(user, USER_REL_WAIT_SCOMP);
			cc_conn_sig(conn, CONN_SIG_RELEASE, NULL);
			break;

		  case USER_ACTIVE:		/* U4/A8,A9,A10 */
			conn->cause[0] = api->cause[0];
			conn->cause[1] = api->cause[1];
			set_state(user, USER_REL_WAIT);
			cc_conn_sig(conn, CONN_SIG_RELEASE, NULL);
			break;

		  default:
			uni_msg_destroy(msg);
			cc_user_err(user, ATMERR_BAD_STATE);
			goto bad_state;
		}
		uni_msg_destroy(msg);
		return;
	    }


	  case USER_SIG_RELEASE_CONFIRM:
	    {
		struct atm_call_release *ind;

		switch (user->state) {

		  case USER_OUT_WAIT_CONF:	/* U3/A3 */
		  case USER_ACTIVE:		/* U4/A8,A9,A10 */
			cc_user_reset(user);
			break;

		  case USER_REL_WAIT:		/* U5 /A8,A9,A10 */
		  case USER_REL_WAIT_SCOMP:	/* U12/A7 */
		  case USER_REL_WAIT_SCONF:	/* U13/A3 */
		  case USER_REL_WAIT_CONF:	/* U14/A3 */
			cc_user_reset(user);
			cc_user_ok(user, ATMRESP_NONE, NULL, 0);
			return;

		  case USER_IN_ACCEPTING:	/* U11/A7 */
			cc_user_reset(user);
			break;

		  default:
			goto bad_state;
		}

		ind = CCZALLOC(sizeof(*ind));
		if (ind == NULL)
			return;
		memcpy(ind->cause, user->cause, sizeof(ind->cause));
		cc_user_send(user, ATMOP_CALL_RELEASE, ind, sizeof(*ind));
		CCFREE(ind);
		return;
	    }


	  case USER_SIG_RELEASE_ERR:
		switch (user->state) {

		  case USER_REL_WAIT:		/* U5/A8,A9,A10 */
			set_state(user, USER_ACTIVE);
			cc_user_err(user, ATM_MKUNIERR(arg2));
			break;

		  case USER_REL_WAIT_CONF:	/* U14/A3 */
			cc_user_err(user, ATM_MKUNIERR(arg2));
			cc_user_active(user);
			break;

		  case USER_REL_WAIT_SCOMP:	/* U12/A7 */
			set_state(user, USER_IN_ACCEPTING);
			cc_user_err(user, ATM_MKUNIERR(arg2));
			break;

		  default:
			goto bad_state;
		}
		return;


	  case USER_SIG_ADD_PARTY:
	    {
		struct uni_msg *msg = arg;
		struct atm_add_party *add = uni_msg_rptr(msg,
		    struct atm_add_party *);
		struct ccconn *conn;

		if (user->state != USER_ACTIVE || user->config != USER_ROOT) {
			uni_msg_destroy(msg);
			cc_user_err(user, ATMERR_BAD_STATE);
			return;
		}

		if (add->leaf_ident == 0 || add->leaf_ident >= 32786) {
			uni_msg_destroy(msg);
			cc_user_err(user, ATMERR_BAD_LEAF_IDENT);
			return;
		}

		conn = TAILQ_FIRST(&user->connq);
		conn->called = add->called;

		cc_conn_sig(conn, CONN_SIG_ADD_PARTY,
		    (void *)(uintptr_t)add->leaf_ident);

		uni_msg_destroy(msg);
		return;
	    }


	  case USER_SIG_ADD_PARTY_ERR:
		if (user->state != USER_ACTIVE)
			goto bad_state;
		cc_user_err(user, arg2);
		return;


	  case USER_SIG_ADD_PARTY_OK:
		if (user->state != USER_ACTIVE)
			goto bad_state;
		cc_user_ok(user, ATMRESP_NONE, NULL, 0);
		return;


	  case USER_SIG_ADD_PARTY_ACK:
	    {
		u_int leaf_ident = arg2;
		struct atm_add_party_success *succ;

		if (user->state != USER_ACTIVE)
			goto bad_state;

		succ = CCZALLOC(sizeof(*succ));
		if (succ == NULL)
			return;

		succ->leaf_ident = leaf_ident;
		cc_user_send(user, ATMOP_ADD_PARTY_SUCCESS,
		    succ, sizeof(*succ));

		CCFREE(succ);
		return;
	    }


	  case USER_SIG_ADD_PARTY_REJ:
	    {
		u_int leaf_ident = arg2;
		struct atm_add_party_reject *reject;

		if (user->state != USER_ACTIVE)
			goto bad_state;

		reject = CCZALLOC(sizeof(*reject));
		if (reject == NULL)
			return;

		reject->leaf_ident = leaf_ident;
		reject->cause = user->cause[0];
		cc_user_send(user, ATMOP_ADD_PARTY_REJECT,
		    reject, sizeof(*reject));

		CCFREE(reject);
		return;
	    }


	  case USER_SIG_DROP_PARTY:
	    {
		struct uni_msg *msg = arg;
		struct atm_drop_party *drop = uni_msg_rptr(msg,
		    struct atm_drop_party *);
		struct ccconn *conn;

		if (user->state != USER_ACTIVE || user->config != USER_ROOT) {
			uni_msg_destroy(msg);
			cc_user_err(user, ATMERR_BAD_STATE);
			return;
		}

		if (drop->leaf_ident >= 32786) {
			uni_msg_destroy(msg);
			cc_user_err(user, ATMERR_BAD_LEAF_IDENT);
			return;
		}

		conn = TAILQ_FIRST(&user->connq);
		conn->cause[0] = drop->cause;
		memset(&conn->cause[1], 0, sizeof(conn->cause[1]));

		cc_conn_sig(conn, CONN_SIG_DROP_PARTY,
		    (void *)(uintptr_t)drop->leaf_ident);

		uni_msg_destroy(msg);
		return;
	    }


	  case USER_SIG_DROP_PARTY_ERR:
		if (user->state != USER_ACTIVE)
			goto bad_state;
		cc_user_err(user, arg2);
		return;


	  case USER_SIG_DROP_PARTY_OK:
		if (user->state != USER_ACTIVE)
			goto bad_state;
		cc_user_ok(user, ATMRESP_NONE, NULL, 0);
		return;


	  case USER_SIG_DROP_PARTY_IND:
	    {
		u_int leaf_ident = arg2;
		struct atm_drop_party *drop;

		if (user->state != USER_ACTIVE)
			goto bad_state;

		drop = CCZALLOC(sizeof(*drop));
		if (drop == NULL)
			return;

		drop->leaf_ident = leaf_ident;
		drop->cause = user->cause[0];
		cc_user_send(user, ATMOP_DROP_PARTY, drop, sizeof(*drop));

		CCFREE(drop);
		return;
	    }


	  case USER_SIG_QUERY_ATTR:
	    {
		struct uni_msg *msg = arg;
		struct atm_query_connection_attributes *req;
		struct ccconn *conn;

		if (user->aborted) {
			cc_user_err(user, ATMERR_PREVIOUSLY_ABORTED);
			uni_msg_destroy(msg);
			return;
		}
		conn = cc_query_check(user);
		if (conn == NULL) {
			cc_user_err(user, ATMERR_BAD_STATE);
			uni_msg_destroy(msg);
			return;
		}
		req = uni_msg_rptr(msg,
		    struct atm_query_connection_attributes *);
		cc_attr_query(user, conn, &req->attr, 1);
		uni_msg_destroy(msg);
		return;
	    }

	  case USER_SIG_QUERY_ATTR_X:
	    {
		struct uni_msg *msg = arg;
		struct atm_query_connection_attributes_x *req;
		struct ccconn *conn;

		conn = cc_query_check(user);
		if (conn == NULL) {
			cc_user_err(user, ATMERR_BAD_STATE);
			uni_msg_destroy(msg);
			return;
		}
		req = uni_msg_rptr(msg,
		    struct atm_query_connection_attributes_x *);
		cc_attr_query(user, conn, req->attr, req->count);
		uni_msg_destroy(msg);
		return;
	    }

	  case USER_SIG_SET_ATTR:
	    {
		struct uni_msg *msg = arg;
		struct atm_set_connection_attributes *req;
		struct ccconn *conn;

		if (user->aborted) {
			cc_user_err(user, ATMERR_PREVIOUSLY_ABORTED);
			uni_msg_destroy(msg);
			return;
		}
		conn = cc_set_check(user);
		if (conn == NULL) {
			cc_user_err(user, ATMERR_BAD_STATE);
			uni_msg_destroy(msg);
			return;
		}
		req = uni_msg_rptr(msg, struct atm_set_connection_attributes *);
		cc_attr_set(user, conn, &req->attr, 1, (u_char *)(req + 1),
		    uni_msg_len(msg) - sizeof(*req));
		uni_msg_destroy(msg);
		return;
	    }

	  case USER_SIG_SET_ATTR_X:
	    {
		struct uni_msg *msg = arg;
		struct atm_set_connection_attributes_x *req;
		struct ccconn *conn;

		conn = cc_set_check(user);
		if (conn == NULL) {
			cc_user_err(user, ATMERR_BAD_STATE);
			uni_msg_destroy(msg);
			return;
		}
		req = uni_msg_rptr(msg,
		    struct atm_set_connection_attributes_x *);
		cc_attr_set(user, conn, req->attr, req->count,
		    (u_char *)req->attr + req->count * sizeof(req->attr[0]),
		    uni_msg_len(msg) -
		    offsetof(struct atm_set_connection_attributes_x, attr) -
		    req->count * sizeof(req->attr[0]));
		uni_msg_destroy(msg);
		return;
	    }

	  case USER_SIG_QUERY_STATE:
	    {
		struct atm_epstate state;

		strcpy(state.name, user->name);
		switch (user->state) {

		  case USER_NULL:
			if (user->accepted != NULL)
				state.state = ATM_A7;
			else
				state.state = ATM_A1;
			break;

		  case USER_OUT_PREPARING:
			state.state = ATM_A2;
			break;

		  case USER_OUT_WAIT_OK:
		  case USER_OUT_WAIT_CONF:
		  case USER_REL_WAIT_SCONF:
		  case USER_REL_WAIT_CONF:
		  case USER_REL_WAIT_CONN:
			state.state = ATM_A3;
			break;

		  case USER_ACTIVE:
		  case USER_REL_WAIT:
			switch (user->config) {

			  case USER_P2P:
				state.state = ATM_A8;
				break;

			  case USER_ROOT:
				state.state = ATM_A9;
				break;

			  case USER_LEAF:
				state.state = ATM_A10;
				break;
			}
			break;

		  case USER_IN_PREPARING:
			state.state = ATM_A4;
			break;

		  case USER_IN_WAITING:
			state.state = ATM_A5;
			break;

		  case USER_IN_ARRIVED:
		  case USER_IN_WAIT_REJ:
		  case USER_IN_WAIT_ACC:
			state.state = ATM_A6;
			break;

		  case USER_IN_ACCEPTING:
		  case USER_REL_WAIT_SCOMP:
			state.state = ATM_A7;
			break;
		}
		cc_user_ok(user, ATMRESP_STATE, &state, sizeof(state));
		return;
	    }

	  case USER_SIG_GET_LOCAL_PORT_INFO:
	    {
		struct uni_msg *msg = arg;
		struct atm_port_list *list;
		size_t list_len;

		list = cc_get_local_port_info(user->cc,
		    uni_msg_rptr(msg, struct atm_get_local_port_info *)->port,
		    &list_len);
		uni_msg_destroy(msg);
		if (list == NULL) {
			cc_user_err(user, ATMERR_NOMEM);
			return;
		}
		cc_user_ok(user, ATMRESP_PORTS, list, list_len);
		CCFREE(list);
		return;
	    }

	  case USER_SIG_ABORT_CONNECTION:
	    {
		struct uni_msg *msg = arg;
		struct atm_abort_connection *abo = uni_msg_rptr(msg,
		    struct atm_abort_connection *);

		cc_user_abort(user, &abo->cause);
		uni_msg_destroy(msg);
		cc_user_ok(user, ATMRESP_NONE, NULL, 0);
		return;
	    }

	}
	if (user->cc->log & CCLOG_USER_SIG)
		cc_user_log(user, "bad signal=%u in state=%u",
		    sig, user->state);
	return;

  bad_state:
	if (user->cc->log & CCLOG_USER_SIG)
		cc_user_log(user, "bad state=%u for signal=%u",
		    user->state, sig);
	return;
}
OpenPOWER on IntegriCloud