summaryrefslogtreecommitdiffstats
path: root/usr.sbin/atm/scspd/scsp_input.c
blob: 2e3fbb0b67685011c0010d77e200d90f6c612174 (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
/*
 *
 * ===================================
 * HARP  |  Host ATM Research Platform
 * ===================================
 *
 *
 * This Host ATM Research Platform ("HARP") file (the "Software") is
 * made available by Network Computing Services, Inc. ("NetworkCS")
 * "AS IS".  NetworkCS does not provide maintenance, improvements or
 * support of any kind.
 *
 * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
 * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
 * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
 * In no event shall NetworkCS be responsible for any damages, including
 * but not limited to consequential damages, arising from or relating to
 * any use of the Software or related support.
 *
 * Copyright 1994-1998 Network Computing Services, Inc.
 *
 * Copies of this Software may be made, however, the above copyright
 * notice must be reproduced on all copies.
 *
 *	@(#) $FreeBSD$
 *
 */

/*
 * Server Cache Synchronization Protocol (SCSP) Support
 * ----------------------------------------------------
 *
 * Input packet processing
 *
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netatm/port.h> 
#include <netatm/queue.h> 
#include <netatm/atm.h>
#include <netatm/atm_if.h>
#include <netatm/atm_sap.h>
#include <netatm/atm_sys.h>
#include <netatm/atm_ioctl.h>
  
#include <errno.h>
#include <libatm.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>

#include "scsp_msg.h"
#include "scsp_if.h"
#include "scsp_var.h"

#ifndef lint
__RCSID("@(#) $FreeBSD$");
#endif


static int scsp_parse_atmarp(char *, int, Scsp_atmarp_csa **);


/*
 * Get a long ingeter
 *
 * This routine is provided to handle long integers that may not
 * be word-aligned in the input buffer.
 *
 * Arguments:
 *	cp	pointer to long int in message
 *
 * Returns:
 *	int	long int in host order
 *
 */
static u_long
get_long(cp)
	u_char	*cp;
{
	int	i;
	u_long	l;

	/*
	 * Read the long out of the input buffer
	 */
	l = 0;
	for (i = 0; i < sizeof(u_long); i++)
		l = (l << 8) + *cp++;

	/*
	 * Return the value in host order
	 */
	return(l);
}


/*
 * Free an SCSP Cache Alignment message in internal format
 *
 * Arguments:
 *	cap	pointer to CA message
 *
 * Returns:
 *	None
 *
 */
static void
scsp_free_ca(cap)
	Scsp_ca	*cap;
{
	Scsp_csa	*csap, *ncsap;

	/*
	 * Return if there's nothing to free
	 */
	if (cap == (Scsp_ca *)0)
		return;

	/*
	 * Free the CSAS records
	 */
	for (csap = cap->ca_csa_rec; csap; csap = ncsap) {
		ncsap = csap->next;
		SCSP_FREE_CSA(csap);
	}
	/*
	 * Free the CA message structure
	 */
	free(cap);
}


/*
 * Free an SCSP Cache State Update Request, Cache State Update Reply,
 * or Cache State Update Solicit message in internal format
 *
 * Arguments:
 *	csup	pointer to CSU message
 *
 * Returns:
 *	None
 *
 */
static void
scsp_free_csu(csup)
	Scsp_csu_msg	*csup;
{
	Scsp_csa	*csap, *ncsap;

	/*
	 * Return if there's nothing to free
	 */
	if (csup == (Scsp_csu_msg *)0)
		return;

	/*
	 * Free the CSA records
	 */
	for (csap = csup->csu_csa_rec; csap; csap = ncsap) {
		ncsap = csap->next;
		SCSP_FREE_CSA(csap);
	}

	/*
	 * Free the CSU message structure
	 */
	free(csup);
}


/*
 * Free an SCSP Hello message in  internal format
 *
 * Arguments:
 *	hp	pointer to Hello message
 *
 * Returns:
 *	None
 *
 */
static void
scsp_free_hello(hp)
	Scsp_hello	*hp;
{
	/*
	 * Return if there's nothing to free
	 */
	if (hp == (Scsp_hello *)0)
		return;

	/*
	 * Free the Hello message structure
	 */
	free(hp);
}


/*
 * Free an SCSP message in  internal format
 *
 * Arguments:
 *	msg	pointer to input packet
 *
 * Returns:
 *	None
 *
 */
void
scsp_free_msg(msg)
	Scsp_msg	*msg;
{
	Scsp_ext	*exp, *nexp;

	/*
	 * Return if there's nothing to free
	 */
	if (msg == (Scsp_msg *)0)
		return;

	/*
	 * Free the message body
	 */
	switch(msg->sc_msg_type) {
	case SCSP_CA_MSG:
		scsp_free_ca(msg->sc_ca);
		break;
	case SCSP_CSU_REQ_MSG:
	case SCSP_CSU_REPLY_MSG:
	case SCSP_CSUS_MSG:
		scsp_free_csu(msg->sc_csu_msg);
		break;
	case SCSP_HELLO_MSG:
		scsp_free_hello(msg->sc_hello);
		break;
	}

	/*
	 * Free any extensions
	 */
	for (exp = msg->sc_ext; exp; exp = nexp) {
		nexp = exp->next;
		free(exp);
	}

	/*
	 * Free the message structure
	 */
	free(msg);
}


/*
 * Parse a Sender or Receiver ID
 *
 * Arguments:
 *	buff	pointer to ID
 *	id_len	length of ID
 *	idp	pointer to structure to receive the ID
 *
 * Returns:
 *	0	input was invalid
 *	else	length of ID processed
 *
 */
static int
scsp_parse_id(buff, id_len, idp)
	char	*buff;
	int	id_len;
	Scsp_id	*idp;
{
	/*
	 * Sanity check
	 */
	if (!buff ||
			id_len == 0 || id_len > SCSP_MAX_ID_LEN ||
			!idp) {
		return(0);
	}

	/*
	 * Save the ID length
	 */
	idp->id_len = id_len;

	/*
	 * Get the ID
	 */
	bcopy(buff, idp->id, id_len);

	/*
	 * Return the ID length
	 */
	return(id_len);
}


/*
 * Parse the Mandatory Common Part of an SCSP input packet
 *
 * Arguments:
 *	buff	pointer to mandatory common part
 *	pdu_len	length of input packet
 *	mcp	pointer to location of MCP in decoded record
 *
 * Returns:
 *	0	input was invalid
 *	else	length of MCP in message
 *
 */
static int
scsp_parse_mcp(buff, pdu_len, mcp)
	char		*buff;
	int		pdu_len;
	Scsp_mcp	*mcp;
{
	int			len;
	u_char			*idp;
	struct scsp_nmcp	*smp;

	/*
	 * Get the protocol ID
	 */
	smp = (struct scsp_nmcp *)buff;
	mcp->pid = ntohs(smp->sm_pid);
	if (mcp->pid < SCSP_PROTO_ATMARP ||
			mcp->pid > SCSP_PROTO_LNNI) {
		/* Protocol ID is invalid */
		goto mcp_invalid;
	}

	/*
	 * Get the server group ID
	 */
	mcp->sgid = ntohs(smp->sm_sgid);

	/*
	 * Get the flags
	 */
	mcp->flags = ntohs(smp->sm_flags);

	/*
	 * Get the sender ID and length
	 */
	idp = (u_char *) ((caddr_t)smp + sizeof(struct scsp_nmcp));
	len = scsp_parse_id(idp, smp->sm_sid_len, &mcp->sid);
	if (len == 0) {
		goto mcp_invalid;
	 }

	/*
	 * Get the receiver ID and length
	 */
	idp += len;
	len = scsp_parse_id(idp, smp->sm_rid_len, &mcp->rid);
	if (len == 0) {
		goto mcp_invalid;
	 }

	/*
	 * Get the record count
	 */
	mcp->rec_cnt = ntohs(smp->sm_rec_cnt);

	/*
	 * Return the length of data we processed
	 */
	return(sizeof(struct scsp_nmcp) + smp->sm_sid_len +
			smp->sm_rid_len);

mcp_invalid:
	return(0);
}


/*
 * Parse an Extension
 *
 * Arguments:
 *	buff	pointer to Extension
 *	pdu_len	length of buffer
 *	expp	pointer to location to receive pointer to the Extension
 *
 * Returns:
 *	0	input was invalid
 *	else	length of Extension processed
 *
 */
static int
scsp_parse_ext(buff, pdu_len, expp)
	char		*buff;
	int		pdu_len;
	Scsp_ext	**expp;
{
	int			len;
	struct scsp_next	*sep;
	Scsp_ext		*exp;

	/*
	 * Get memory for the extension
	 */
	sep = (struct scsp_next *)buff;
	len = sizeof(Scsp_ext) + ntohs(sep->se_len);
	exp = calloc(1, len);
	if (exp == NULL)
		goto ext_invalid;

	/*
	 * Get the type
	 */
	exp->type = ntohs(sep->se_type);

	/*
	 * Get the length
	 */
	exp->len = ntohs(sep->se_len);

	/*
	 * Get the value
	 */
	if (exp->len > 0) {
		bcopy((caddr_t)sep + sizeof(struct scsp_next),
				(caddr_t)exp + sizeof(Scsp_ext),
				exp->len);
	}

	/*
	 * Save a pointer to the extension and return the
	 * number of bytes processed
	 */
	*expp = exp;
	return(sizeof(struct scsp_next) + exp->len);

ext_invalid:
	if (exp) {
		free(exp);
	}
	return(0);
}


/*
 * Parse a Cache State Advertisement or Cache State Advertisement
 * Summary record
 *
 * Arguments:
 *	buff	pointer to CSA or CSAS record
 *	pdu_len	length of input packet
 *	csapp	pointer to location to put pointer to CSA or CSAS
 *
 * Returns:
 *	0	input was invalid
 *	else	length of record processed
 *
 */
static int
scsp_parse_csa(buff, pdu_len, csapp)
	char		*buff;
	int		pdu_len;
	Scsp_csa	**csapp;
{
	int			len;
	char			*idp;
	struct scsp_ncsa	*scp;
	Scsp_csa		*csap = NULL;

	/*
	 * Check the record length
	 */
	scp = (struct scsp_ncsa *)buff;
	if (ntohs(scp->scs_len) < (sizeof(struct scsp_ncsa) +
			scp->scs_ck_len + scp->scs_oid_len)) {
		goto csa_invalid;
	}

	/*
	 * Get memory for the returned structure
	 */
	len = sizeof(Scsp_csa) + ntohs(scp->scs_len) -
			sizeof(struct scsp_ncsa) - scp->scs_ck_len -
			scp->scs_oid_len;
	csap = calloc(1, len);
	if (csap == NULL)
		goto csa_invalid;

	/*
	 * Get the hop count
	 */
	csap->hops = ntohs(scp->scs_hop_cnt);

	/*
	 * Set the null flag
	 */
	csap->null = (ntohs(scp->scs_nfill) & SCSP_CSAS_NULL) != 0;

	/*
	 * Get the sequence number
	 */
	csap->seq = get_long((u_char *)&scp->scs_seq);

	/*
	 * Get the cache key
	 */
	if (scp->scs_ck_len == 0 ||
			scp->scs_ck_len > SCSP_MAX_KEY_LEN) {
		goto csa_invalid;
	}
	csap->key.key_len = scp->scs_ck_len;
	idp = (char *) ((caddr_t)scp + sizeof(struct scsp_ncsa));
	bcopy(idp, csap->key.key, scp->scs_ck_len);

	/*
	 * Get the originator ID
	 */
	idp += scp->scs_ck_len;
	len = scsp_parse_id(idp, scp->scs_oid_len, &csap->oid);
	if (len == 0) {
		goto csa_invalid;
	}

	/*
	 * Get the protocol-specific data, if present
	 */
	len = ntohs(scp->scs_len) - (sizeof(struct scsp_ncsa) +
			scp->scs_ck_len + scp->scs_oid_len);
	if (len > 0) {
		idp += scp->scs_oid_len;
		len = scsp_parse_atmarp(idp, len, &csap->atmarp_data);
		if (len == 0)
			goto csa_invalid;
	}

	/*
	 * Set a pointer to the MCP and return the length
	 * of data we processed
	 */
	*csapp = csap;
	return(ntohs(scp->scs_len));

csa_invalid:
	if (csap)
		SCSP_FREE_CSA(csap);
	return(0);
}


/*
 * Parse a Cache Alignment message
 *
 * Arguments:
 *	buff	pointer to start of CA in message
 *	pdu_len	length of input packet
 *	capp	pointer to location to put pointer to CA message
 *
 * Returns:
 *	0	input was invalid
 *	else	length of CA message processed
 *
 */
static int
scsp_parse_ca(buff, pdu_len, capp)
	char	*buff;
	int	pdu_len;
	Scsp_ca	**capp;
{
	int		i, len, proc_len;
	struct scsp_nca	*scap;
	Scsp_ca		*cap;
	Scsp_csa	**csapp;

	/*
	 * Get memory for the returned structure
	 */
	scap = (struct scsp_nca *)buff;
	cap = calloc(1, sizeof(Scsp_ca));
	if (cap == NULL)
		goto ca_invalid;

	/*
	 * Get the sequence number
	 */
	cap->ca_seq = get_long((u_char *)&scap->sca_seq);
	proc_len = sizeof(scap->sca_seq);
	buff += sizeof(scap->sca_seq);

	/*
	 * Process the mandatory common part of the message
	 */
	len = scsp_parse_mcp(buff,
			pdu_len - proc_len,
			&cap->ca_mcp);
	if (len == 0)
		goto ca_invalid;
	buff += len;
	proc_len += len;

	/*
	 * Set the flags
	 */
	cap->ca_m = (cap->ca_mcp.flags & SCSP_CA_M) != 0;
	cap->ca_i = (cap->ca_mcp.flags & SCSP_CA_I) != 0;
	cap->ca_o = (cap->ca_mcp.flags & SCSP_CA_O) != 0;

	/*
	 * Get the CSAS records from the message
	 */
	for (i = 0, csapp = &cap->ca_csa_rec; i < cap->ca_mcp.rec_cnt;
			i++, csapp = &(*csapp)->next) {
		len = scsp_parse_csa(buff, pdu_len - proc_len, csapp);
		buff += len;
		proc_len += len;
	}

	/*
	 * Set the address of the CA message and
	 * return the length of processed data
	 */
	*capp = cap;
	return(proc_len);

ca_invalid:
	if (cap)
		scsp_free_ca(cap);
	return(0);
}


/*
 * Parse the ATMARP-specific part of a CSA record
 *
 * Arguments:
 *	buff	pointer to ATMARP part of CSU message
 *	pdu_len	length of data to process
 *	acspp	pointer to location to put pointer to CSU message
 *
 * Returns:
 *	0	input was invalid
 *	else	length of CSU Req message processed
 *
 */
static int
scsp_parse_atmarp(buff, pdu_len, acspp)
	char		*buff;
	int		pdu_len;
	Scsp_atmarp_csa	**acspp;
{
	int			len, proc_len;
	struct scsp_atmarp_ncsa	*sacp;
	Scsp_atmarp_csa		*acsp = NULL;

	/*
	 * Initial packet verification
	 */
	sacp = (struct scsp_atmarp_ncsa *)buff;
	if ((sacp->sa_hrd != ntohs(ARP_ATMFORUM)) ||
			(sacp->sa_pro != ntohs(ETHERTYPE_IP)))
		goto acs_invalid;

	/*
	 * Get memory for the returned structure
	 */
	acsp = calloc(1, sizeof(Scsp_atmarp_csa));
	if (acsp == NULL)
		goto acs_invalid;

	/*
	 * Get state code
	 */
	acsp->sa_state = sacp->sa_state;
	proc_len = sizeof(struct scsp_atmarp_ncsa);

	/*
	 * Verify/gather source ATM address
	 */
	acsp->sa_sha.address_format = T_ATM_ABSENT;
	acsp->sa_sha.address_length = 0;
	if ((len = (sacp->sa_shtl & ARP_TL_LMASK)) != 0) {
		if (sacp->sa_shtl & ARP_TL_E164) {
			if (len > sizeof(Atm_addr_e164))
				goto acs_invalid;
			acsp->sa_sha.address_format = T_ATM_E164_ADDR;
		} else {
			if (len != sizeof(Atm_addr_nsap))
				goto acs_invalid;
			acsp->sa_sha.address_format = T_ATM_ENDSYS_ADDR;
		}
		acsp->sa_sha.address_length = len;
		if (pdu_len < proc_len + len)
			goto acs_invalid;
		bcopy(&buff[proc_len], (char *)acsp->sa_sha.address,
				len);
		proc_len += len;
	}

	/*
	 * Verify/gather source ATM subaddress
	 */
	acsp->sa_ssa.address_format = T_ATM_ABSENT;
	acsp->sa_ssa.address_length = 0;
	if ((len = (sacp->sa_sstl & ARP_TL_LMASK)) != 0) {
		if (((sacp->sa_sstl & ARP_TL_TMASK) != ARP_TL_NSAPA) ||
				(len != sizeof(Atm_addr_nsap)))
			goto acs_invalid;
		acsp->sa_ssa.address_format = T_ATM_ENDSYS_ADDR;
		acsp->sa_ssa.address_length = len;
		if (pdu_len < proc_len + len)
			goto acs_invalid;
		bcopy(&buff[proc_len], (char *)acsp->sa_ssa.address,
				len);
		proc_len += len;
	}

	/*
	 * Verify/gather source IP address
	 */
	if ((len = sacp->sa_spln) != 0) {
		if (len != sizeof(struct in_addr))
			goto acs_invalid;
		if (pdu_len < proc_len + len)
			goto acs_invalid;
		bcopy(&buff[proc_len], (char *)&acsp->sa_spa, len);
		proc_len += len;
	} else {
		acsp->sa_spa.s_addr = 0;
	}

	/*
	 * Verify/gather target ATM address
	 */
	acsp->sa_tha.address_format = T_ATM_ABSENT;
	acsp->sa_tha.address_length = 0;
	if ((len = (sacp->sa_thtl & ARP_TL_LMASK)) != 0) {
		if (sacp->sa_thtl & ARP_TL_E164) {
			if (len > sizeof(Atm_addr_e164))
				goto acs_invalid;
			acsp->sa_tha.address_format = T_ATM_E164_ADDR;
		} else {
			if (len != sizeof(Atm_addr_nsap))
				goto acs_invalid;
			acsp->sa_tha.address_format = T_ATM_ENDSYS_ADDR;
		}
		acsp->sa_tha.address_length = len;
		if (pdu_len < proc_len + len)
			goto acs_invalid;
		bcopy(&buff[proc_len], (char *)acsp->sa_tha.address,
				len);
		proc_len += len;
	}

	/*
	 * Verify/gather target ATM subaddress
	 */
	acsp->sa_tsa.address_format = T_ATM_ABSENT;
	acsp->sa_tsa.address_length = 0;
	if ((len = (sacp->sa_tstl & ARP_TL_LMASK)) != 0) {
		if (((sacp->sa_tstl & ARP_TL_TMASK) != ARP_TL_NSAPA) ||
				(len != sizeof(Atm_addr_nsap)))
			goto acs_invalid;
		acsp->sa_tsa.address_format = T_ATM_ENDSYS_ADDR;
		acsp->sa_tsa.address_length = len;
		if (pdu_len < proc_len + len)
			goto acs_invalid;
		bcopy(&buff[proc_len], (char *)acsp->sa_tsa.address,
				len);
		proc_len += len;
	}

	/*
	 * Verify/gather target IP address
	 */
	if ((len = sacp->sa_tpln) != 0) {
		if (len != sizeof(struct in_addr))
			goto acs_invalid;
		if (pdu_len < proc_len + len)
			goto acs_invalid;
		bcopy(&buff[proc_len], (char *)&acsp->sa_tpa, len);
		proc_len += len;
	} else {
		acsp->sa_tpa.s_addr = 0;
	}

	/*
	 * Verify packet length
	 */
	if (proc_len != pdu_len)
		goto acs_invalid;

	*acspp = acsp;
	return(proc_len);

acs_invalid:
	if (acsp)
		free(acsp);
	return(0);
}


/*
 * Parse a Cache State Update Request, Cache State Update Reply, or
 * Cache State Update Solicit message.  These all have the same format,
 * a Mandatory Common Part followed by a number of CSA or CSAS records.
 *
 * Arguments:
 *	buff	pointer to start of CSU message
 *	pdu_len	length of input packet
 *	csupp	pointer to location to put pointer to CSU message
 *
 * Returns:
 *	0	input was invalid
 *	else	length of CSU Req message processed
 *
 */
static int
scsp_parse_csu(buff, pdu_len, csupp)
	char		*buff;
	int		pdu_len;
	Scsp_csu_msg	**csupp;
{
	int			i, len, proc_len;
	Scsp_csu_msg		*csup;
	Scsp_csa		**csapp;

	/*
	 * Get memory for the returned structure
	 */
	csup = calloc(1, sizeof(Scsp_csu_msg));
	if (csup == NULL)
		goto csu_invalid;

	/*
	 * Process the mandatory common part of the message
	 */
	len = scsp_parse_mcp(buff, pdu_len, &csup->csu_mcp);
	if (len == 0)
		goto csu_invalid;
	buff += len;
	proc_len = len;

	/*
	 * Get the CSAS records from the message
	 */
	for (i = 0, csapp = &csup->csu_csa_rec;
			i < csup->csu_mcp.rec_cnt;
			i++, csapp = &(*csapp)->next) {
		len = scsp_parse_csa(buff, pdu_len - proc_len, csapp);
		buff += len;
		proc_len += len;
	}

	/*
	 * Set the address of the CSU Req message and
	 * return the length of processed data
	 */
	*csupp = csup;
	return(proc_len);

csu_invalid:
	if (csup)
		scsp_free_csu(csup);
	return(0);
}


/*
 * Parse a Hello message
 *
 * Arguments:
 *	buff	pointer to start of Hello in message
 *	pdu_len	length of input packet
 *	hpp	pointer to location to put pointer to Hello message
 *
 * Returns:
 *	0	input was invalid
 *	else	length of Hello message processed
 *
 */
static int
scsp_parse_hello(buff, pdu_len, hpp)
	char		*buff;
	int		pdu_len;
	Scsp_hello	**hpp;
{
	int			i, len, proc_len;
	struct scsp_nhello	*shp = (struct scsp_nhello *)buff;
	Scsp_hello		*hp;
	Scsp_id			*idp;
	Scsp_id			**ridpp;

	/*
	 * Get memory for the returned structure
	 */
	hp = calloc(1, sizeof(Scsp_hello));
	if (hp == NULL)
		goto hello_invalid;

	/*
	 * Get the hello interval
	 */
	hp->hello_int = ntohs(shp->sch_hi);

	/*
	 * Get the dead factor
	 */
	hp->dead_factor = ntohs(shp->sch_df);

	/*
	 * Get the family ID
	 */
	hp->family_id = ntohs(shp->sch_fid);

	/*
	 * Process the mandatory common part of the message
	 */
	proc_len = sizeof(struct scsp_nhello) -
			sizeof(struct scsp_nmcp);
	buff += proc_len;
	len = scsp_parse_mcp(buff, pdu_len - proc_len,
			&hp->hello_mcp);
	if (len == 0)
		goto hello_invalid;
	buff += len;
	proc_len += len;

	/*
	 * Get additional receiver ID records from the message
	 */
	for (i = 0, ridpp = &hp->hello_mcp.rid.next;
			i < hp->hello_mcp.rec_cnt;
			i++, ridpp = &idp->next) {
		idp = calloc(1, sizeof(Scsp_id));
		if (idp == NULL)
			goto hello_invalid;
		len = scsp_parse_id(buff,
				hp->hello_mcp.rid.id_len,
				idp);
		if (len == 0) {
			free(idp);
			goto hello_invalid;
		}
		buff += len;
		proc_len += len;
		*ridpp = idp;
	}

	/*
	 * Set the address of the CA message and
	 * return the length of processed data
	 */
	*hpp = hp;
	return(proc_len);

hello_invalid:
	if (hp)
		scsp_free_hello(hp);
	return(0);
}


/*
 * Parse an SCSP input packet
 *
 * Arguments:
 *	buff	pointer to input packet
 *	pdu_len	length of input packet
 *
 * Returns:
 *	NULL	input packet was invalid
 *	else	pointer to packet in internal format
 *
 */
Scsp_msg *
scsp_parse_msg(buff, pdu_len)
	char			*buff;
	int			pdu_len;
{
	int			ext_off, len, plen;
	struct scsp_nhdr	*shp;
	Scsp_msg		*msg = (Scsp_msg *)0;
	Scsp_ext		**expp;

	/*
	 * Check the message checksum
	 */
	if (ip_checksum(buff, pdu_len) != 0) {
		/*
		 * Checksum was bad--discard the message
		 */
		goto ignore;
	}

	/*
	 * Allocate storage for the message
	 */
	msg = calloc(1, sizeof(Scsp_msg));
	if (msg == NULL)
		goto ignore;

	/*
	 * Decode the fixed header
	 *
	 * Check the version
	 */
	shp = (struct scsp_nhdr *)buff;
	if (shp->sh_ver != SCSP_VER_1)
		goto ignore;

	/*
	 * Get the message type
	 */
	msg->sc_msg_type = shp->sh_type;

	/*
	 * Get and check the length
	 */
	len = ntohs(shp->sh_len);
	if (len != pdu_len)
		goto ignore;

	/*
	 * Get the extension offset
	 */
	ext_off = ntohs(shp->sh_ext_off);

	/*
	 * Decode the body of the message, depending on the type
	 */
	buff += sizeof(struct scsp_nhdr);
	len -= sizeof(struct scsp_nhdr);
	switch(msg->sc_msg_type) {
	case SCSP_CA_MSG:
		plen = scsp_parse_ca(buff, len, &msg->sc_ca);
		break;
	case SCSP_CSU_REQ_MSG:
	case SCSP_CSU_REPLY_MSG:
	case SCSP_CSUS_MSG:
		plen = scsp_parse_csu(buff, len, &msg->sc_csu_msg);
		break;
	case SCSP_HELLO_MSG:
		plen = scsp_parse_hello(buff, len, &msg->sc_hello);
		break;
	default:
		goto ignore;
	}
	if (plen == 0) {
		goto ignore;
	}
	buff += plen;
	len -= plen;

	/*
	 * Decode any extensions
	 */
	if (ext_off != 0) {
		for (expp = &msg->sc_ext; len > 0;
				expp = &(*expp)->next) {
			plen = scsp_parse_ext(buff, len, expp);
			if (plen == 0) {
				goto ignore;
			}
			buff += plen;
			len -= plen;
		}
	}

	/*
	 * Make sure we handled the whole message
	 */
	if (len != 0) {
		goto ignore;
	}

	/*
	 * Return the address of the SCSP message in internal format
	 */
	return(msg);

ignore:
	if (msg)
		scsp_free_msg(msg);
	return(Scsp_msg *)0;
}
OpenPOWER on IntegriCloud