3 * ===================================
4 * HARP | Host ATM Research Platform
5 * ===================================
8 * This Host ATM Research Platform ("HARP") file (the "Software") is
9 * made available by Network Computing Services, Inc. ("NetworkCS")
10 * "AS IS". NetworkCS does not provide maintenance, improvements or
11 * support of any kind.
13 * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
14 * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
15 * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
16 * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
17 * In no event shall NetworkCS be responsible for any damages, including
18 * but not limited to consequential damages, arising from or relating to
19 * any use of the Software or related support.
21 * Copyright 1994-1998 Network Computing Services, Inc.
23 * Copies of this Software may be made, however, the above copyright
24 * notice must be reproduced on all copies.
31 * Server Cache Synchronization Protocol (SCSP) Support
32 * ----------------------------------------------------
34 * Input packet processing
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <net/ethernet.h>
43 #include <netinet/in.h>
44 #include <netatm/port.h>
45 #include <netatm/queue.h>
46 #include <netatm/atm.h>
47 #include <netatm/atm_if.h>
48 #include <netatm/atm_sap.h>
49 #include <netatm/atm_sys.h>
50 #include <netatm/atm_ioctl.h>
64 __RCSID("@(#) $FreeBSD$");
68 static int scsp_parse_atmarp __P((char *, int, Scsp_atmarp_csa **));
74 * This routine is provided to handle long integers that may not
75 * be word-aligned in the input buffer.
78 * cp pointer to long int in message
81 * int long int in host order
92 * Read the long out of the input buffer
95 for (i = 0; i < sizeof(u_long); i++)
99 * Return the value in host order
106 * Free an SCSP Cache Alignment message in internal format
109 * cap pointer to CA message
119 Scsp_csa *csap, *ncsap;
122 * Return if there's nothing to free
124 if (cap == (Scsp_ca *)0)
128 * Free the CSAS records
130 for (csap = cap->ca_csa_rec; csap; csap = ncsap) {
135 * Free the CA message structure
142 * Free an SCSP Cache State Update Request, Cache State Update Reply,
143 * or Cache State Update Solicit message in internal format
146 * csup pointer to CSU message
156 Scsp_csa *csap, *ncsap;
159 * Return if there's nothing to free
161 if (csup == (Scsp_csu_msg *)0)
165 * Free the CSA records
167 for (csap = csup->csu_csa_rec; csap; csap = ncsap) {
173 * Free the CSU message structure
180 * Free an SCSP Hello message in internal format
183 * hp pointer to Hello message
194 * Return if there's nothing to free
196 if (hp == (Scsp_hello *)0)
200 * Free the Hello message structure
207 * Free an SCSP message in internal format
210 * msg pointer to input packet
220 Scsp_ext *exp, *nexp;
223 * Return if there's nothing to free
225 if (msg == (Scsp_msg *)0)
229 * Free the message body
231 switch(msg->sc_msg_type) {
233 scsp_free_ca(msg->sc_ca);
235 case SCSP_CSU_REQ_MSG:
236 case SCSP_CSU_REPLY_MSG:
238 scsp_free_csu(msg->sc_csu_msg);
241 scsp_free_hello(msg->sc_hello);
246 * Free any extensions
248 for (exp = msg->sc_ext; exp; exp = nexp) {
254 * Free the message structure
261 * Parse a Sender or Receiver ID
265 * id_len length of ID
266 * idp pointer to structure to receive the ID
269 * 0 input was invalid
270 * else length of ID processed
274 scsp_parse_id(buff, id_len, idp)
283 id_len == 0 || id_len > SCSP_MAX_ID_LEN ||
291 idp->id_len = id_len;
296 UM_COPY(buff, idp->id, id_len);
299 * Return the ID length
306 * Parse the Mandatory Common Part of an SCSP input packet
309 * buff pointer to mandatory common part
310 * pdu_len length of input packet
311 * mcp pointer to location of MCP in decoded record
314 * 0 input was invalid
315 * else length of MCP in message
319 scsp_parse_mcp(buff, pdu_len, mcp)
326 struct scsp_nmcp *smp;
329 * Get the protocol ID
331 smp = (struct scsp_nmcp *)buff;
332 mcp->pid = ntohs(smp->sm_pid);
333 if (mcp->pid < SCSP_PROTO_ATMARP ||
334 mcp->pid > SCSP_PROTO_LNNI) {
335 /* Protocol ID is invalid */
340 * Get the server group ID
342 mcp->sgid = ntohs(smp->sm_sgid);
347 mcp->flags = ntohs(smp->sm_flags);
350 * Get the sender ID and length
352 idp = (u_char *) ((caddr_t)smp + sizeof(struct scsp_nmcp));
353 len = scsp_parse_id(idp, smp->sm_sid_len, &mcp->sid);
359 * Get the receiver ID and length
362 len = scsp_parse_id(idp, smp->sm_rid_len, &mcp->rid);
368 * Get the record count
370 mcp->rec_cnt = ntohs(smp->sm_rec_cnt);
373 * Return the length of data we processed
375 return(sizeof(struct scsp_nmcp) + smp->sm_sid_len +
387 * buff pointer to Extension
388 * pdu_len length of buffer
389 * expp pointer to location to receive pointer to the Extension
392 * 0 input was invalid
393 * else length of Extension processed
397 scsp_parse_ext(buff, pdu_len, expp)
403 struct scsp_next *sep;
407 * Get memory for the extension
409 sep = (struct scsp_next *)buff;
410 len = sizeof(Scsp_ext) + ntohs(sep->se_len);
411 exp = (Scsp_ext *)UM_ALLOC(len);
420 exp->type = ntohs(sep->se_type);
425 exp->len = ntohs(sep->se_len);
431 UM_COPY((caddr_t)sep + sizeof(struct scsp_next),
432 (caddr_t)exp + sizeof(Scsp_ext),
437 * Save a pointer to the extension and return the
438 * number of bytes processed
441 return(sizeof(struct scsp_next) + exp->len);
452 * Parse a Cache State Advertisement or Cache State Advertisement
456 * buff pointer to CSA or CSAS record
457 * pdu_len length of input packet
458 * csapp pointer to location to put pointer to CSA or CSAS
461 * 0 input was invalid
462 * else length of record processed
466 scsp_parse_csa(buff, pdu_len, csapp)
473 struct scsp_ncsa *scp;
474 Scsp_csa *csap = NULL;
477 * Check the record length
479 scp = (struct scsp_ncsa *)buff;
480 if (ntohs(scp->scs_len) < (sizeof(struct scsp_ncsa) +
481 scp->scs_ck_len + scp->scs_oid_len)) {
486 * Get memory for the returned structure
488 len = sizeof(Scsp_csa) + ntohs(scp->scs_len) -
489 sizeof(struct scsp_ncsa) - scp->scs_ck_len -
491 csap = (Scsp_csa *)UM_ALLOC(len);
500 csap->hops = ntohs(scp->scs_hop_cnt);
505 csap->null = (ntohs(scp->scs_nfill) & SCSP_CSAS_NULL) != 0;
508 * Get the sequence number
510 csap->seq = get_long((u_char *)&scp->scs_seq);
515 if (scp->scs_ck_len == 0 ||
516 scp->scs_ck_len > SCSP_MAX_KEY_LEN) {
519 csap->key.key_len = scp->scs_ck_len;
520 idp = (char *) ((caddr_t)scp + sizeof(struct scsp_ncsa));
521 UM_COPY(idp, csap->key.key, scp->scs_ck_len);
524 * Get the originator ID
526 idp += scp->scs_ck_len;
527 len = scsp_parse_id(idp, scp->scs_oid_len, &csap->oid);
533 * Get the protocol-specific data, if present
535 len = ntohs(scp->scs_len) - (sizeof(struct scsp_ncsa) +
536 scp->scs_ck_len + scp->scs_oid_len);
538 idp += scp->scs_oid_len;
539 len = scsp_parse_atmarp(idp, len, &csap->atmarp_data);
545 * Set a pointer to the MCP and return the length
546 * of data we processed
549 return(ntohs(scp->scs_len));
559 * Parse a Cache Alignment message
562 * buff pointer to start of CA in message
563 * pdu_len length of input packet
564 * capp pointer to location to put pointer to CA message
567 * 0 input was invalid
568 * else length of CA message processed
572 scsp_parse_ca(buff, pdu_len, capp)
577 int i, len, proc_len;
578 struct scsp_nca *scap;
583 * Get memory for the returned structure
585 scap = (struct scsp_nca *)buff;
586 cap = (Scsp_ca *)UM_ALLOC(sizeof(Scsp_ca));
590 UM_ZERO(cap, sizeof(Scsp_ca));
593 * Get the sequence number
595 cap->ca_seq = get_long((u_char *)&scap->sca_seq);
596 proc_len = sizeof(scap->sca_seq);
597 buff += sizeof(scap->sca_seq);
600 * Process the mandatory common part of the message
602 len = scsp_parse_mcp(buff,
613 cap->ca_m = (cap->ca_mcp.flags & SCSP_CA_M) != 0;
614 cap->ca_i = (cap->ca_mcp.flags & SCSP_CA_I) != 0;
615 cap->ca_o = (cap->ca_mcp.flags & SCSP_CA_O) != 0;
618 * Get the CSAS records from the message
620 for (i = 0, csapp = &cap->ca_csa_rec; i < cap->ca_mcp.rec_cnt;
621 i++, csapp = &(*csapp)->next) {
622 len = scsp_parse_csa(buff, pdu_len - proc_len, csapp);
628 * Set the address of the CA message and
629 * return the length of processed data
642 * Parse the ATMARP-specific part of a CSA record
645 * buff pointer to ATMARP part of CSU message
646 * pdu_len length of data to process
647 * acspp pointer to location to put pointer to CSU message
650 * 0 input was invalid
651 * else length of CSU Req message processed
655 scsp_parse_atmarp(buff, pdu_len, acspp)
658 Scsp_atmarp_csa **acspp;
661 struct scsp_atmarp_ncsa *sacp;
662 Scsp_atmarp_csa *acsp = NULL;
665 * Initial packet verification
667 sacp = (struct scsp_atmarp_ncsa *)buff;
668 if ((sacp->sa_hrd != ntohs(ARP_ATMFORUM)) ||
669 (sacp->sa_pro != ntohs(ETHERTYPE_IP)))
673 * Get memory for the returned structure
675 acsp = (Scsp_atmarp_csa *)UM_ALLOC(sizeof(Scsp_atmarp_csa));
679 UM_ZERO(acsp, sizeof(Scsp_atmarp_csa));
684 acsp->sa_state = sacp->sa_state;
685 proc_len = sizeof(struct scsp_atmarp_ncsa);
688 * Verify/gather source ATM address
690 acsp->sa_sha.address_format = T_ATM_ABSENT;
691 acsp->sa_sha.address_length = 0;
692 if ((len = (sacp->sa_shtl & ARP_TL_LMASK)) != 0) {
693 if (sacp->sa_shtl & ARP_TL_E164) {
694 if (len > sizeof(Atm_addr_e164))
696 acsp->sa_sha.address_format = T_ATM_E164_ADDR;
698 if (len != sizeof(Atm_addr_nsap))
700 acsp->sa_sha.address_format = T_ATM_ENDSYS_ADDR;
702 acsp->sa_sha.address_length = len;
703 if (pdu_len < proc_len + len)
705 UM_COPY(&buff[proc_len], (char *)acsp->sa_sha.address,
711 * Verify/gather source ATM subaddress
713 acsp->sa_ssa.address_format = T_ATM_ABSENT;
714 acsp->sa_ssa.address_length = 0;
715 if ((len = (sacp->sa_sstl & ARP_TL_LMASK)) != 0) {
716 if (((sacp->sa_sstl & ARP_TL_TMASK) != ARP_TL_NSAPA) ||
717 (len != sizeof(Atm_addr_nsap)))
719 acsp->sa_ssa.address_format = T_ATM_ENDSYS_ADDR;
720 acsp->sa_ssa.address_length = len;
721 if (pdu_len < proc_len + len)
723 UM_COPY(&buff[proc_len], (char *)acsp->sa_ssa.address,
729 * Verify/gather source IP address
731 if ((len = sacp->sa_spln) != 0) {
732 if (len != sizeof(struct in_addr))
734 if (pdu_len < proc_len + len)
736 UM_COPY(&buff[proc_len], (char *)&acsp->sa_spa, len);
739 acsp->sa_spa.s_addr = 0;
743 * Verify/gather target ATM address
745 acsp->sa_tha.address_format = T_ATM_ABSENT;
746 acsp->sa_tha.address_length = 0;
747 if ((len = (sacp->sa_thtl & ARP_TL_LMASK)) != 0) {
748 if (sacp->sa_thtl & ARP_TL_E164) {
749 if (len > sizeof(Atm_addr_e164))
751 acsp->sa_tha.address_format = T_ATM_E164_ADDR;
753 if (len != sizeof(Atm_addr_nsap))
755 acsp->sa_tha.address_format = T_ATM_ENDSYS_ADDR;
757 acsp->sa_tha.address_length = len;
758 if (pdu_len < proc_len + len)
760 UM_COPY(&buff[proc_len], (char *)acsp->sa_tha.address,
766 * Verify/gather target ATM subaddress
768 acsp->sa_tsa.address_format = T_ATM_ABSENT;
769 acsp->sa_tsa.address_length = 0;
770 if ((len = (sacp->sa_tstl & ARP_TL_LMASK)) != 0) {
771 if (((sacp->sa_tstl & ARP_TL_TMASK) != ARP_TL_NSAPA) ||
772 (len != sizeof(Atm_addr_nsap)))
774 acsp->sa_tsa.address_format = T_ATM_ENDSYS_ADDR;
775 acsp->sa_tsa.address_length = len;
776 if (pdu_len < proc_len + len)
778 UM_COPY(&buff[proc_len], (char *)acsp->sa_tsa.address,
784 * Verify/gather target IP address
786 if ((len = sacp->sa_tpln) != 0) {
787 if (len != sizeof(struct in_addr))
789 if (pdu_len < proc_len + len)
791 UM_COPY(&buff[proc_len], (char *)&acsp->sa_tpa, len);
794 acsp->sa_tpa.s_addr = 0;
798 * Verify packet length
800 if (proc_len != pdu_len)
814 * Parse a Cache State Update Request, Cache State Update Reply, or
815 * Cache State Update Solicit message. These all have the same format,
816 * a Mandatory Common Part followed by a number of CSA or CSAS records.
819 * buff pointer to start of CSU message
820 * pdu_len length of input packet
821 * csupp pointer to location to put pointer to CSU message
824 * 0 input was invalid
825 * else length of CSU Req message processed
829 scsp_parse_csu(buff, pdu_len, csupp)
832 Scsp_csu_msg **csupp;
834 int i, len, proc_len;
839 * Get memory for the returned structure
841 csup = (Scsp_csu_msg *)UM_ALLOC(sizeof(Scsp_csu_msg));
845 UM_ZERO(csup, sizeof(Scsp_csu_msg));
848 * Process the mandatory common part of the message
850 len = scsp_parse_mcp(buff, pdu_len, &csup->csu_mcp);
857 * Get the CSAS records from the message
859 for (i = 0, csapp = &csup->csu_csa_rec;
860 i < csup->csu_mcp.rec_cnt;
861 i++, csapp = &(*csapp)->next) {
862 len = scsp_parse_csa(buff, pdu_len - proc_len, csapp);
868 * Set the address of the CSU Req message and
869 * return the length of processed data
882 * Parse a Hello message
885 * buff pointer to start of Hello in message
886 * pdu_len length of input packet
887 * hpp pointer to location to put pointer to Hello message
890 * 0 input was invalid
891 * else length of Hello message processed
895 scsp_parse_hello(buff, pdu_len, hpp)
900 int i, len, proc_len;
901 struct scsp_nhello *shp = (struct scsp_nhello *)buff;
907 * Get memory for the returned structure
909 hp = (Scsp_hello *)UM_ALLOC(sizeof(Scsp_hello));
913 UM_ZERO(hp, sizeof(Scsp_hello));
916 * Get the hello interval
918 hp->hello_int = ntohs(shp->sch_hi);
921 * Get the dead factor
923 hp->dead_factor = ntohs(shp->sch_df);
928 hp->family_id = ntohs(shp->sch_fid);
931 * Process the mandatory common part of the message
933 proc_len = sizeof(struct scsp_nhello) -
934 sizeof(struct scsp_nmcp);
936 len = scsp_parse_mcp(buff, pdu_len - proc_len,
944 * Get additional receiver ID records from the message
946 for (i = 0, ridpp = &hp->hello_mcp.rid.next;
947 i < hp->hello_mcp.rec_cnt;
948 i++, ridpp = &idp->next) {
949 idp = (Scsp_id *)UM_ALLOC(sizeof(Scsp_id));
953 UM_ZERO(idp, sizeof(Scsp_id));
954 len = scsp_parse_id(buff,
955 hp->hello_mcp.rid.id_len,
967 * Set the address of the CA message and
968 * return the length of processed data
981 * Parse an SCSP input packet
984 * buff pointer to input packet
985 * pdu_len length of input packet
988 * NULL input packet was invalid
989 * else pointer to packet in internal format
993 scsp_parse_msg(buff, pdu_len)
997 int ext_off, len, plen;
998 struct scsp_nhdr *shp;
999 Scsp_msg *msg = (Scsp_msg *)0;
1003 * Check the message checksum
1005 if (ip_checksum(buff, pdu_len) != 0) {
1007 * Checksum was bad--discard the message
1013 * Allocate storage for the message
1015 msg = (Scsp_msg *)UM_ALLOC(sizeof(Scsp_msg));
1019 UM_ZERO(msg, sizeof(Scsp_msg));
1022 * Decode the fixed header
1026 shp = (struct scsp_nhdr *)buff;
1027 if (shp->sh_ver != SCSP_VER_1)
1031 * Get the message type
1033 msg->sc_msg_type = shp->sh_type;
1036 * Get and check the length
1038 len = ntohs(shp->sh_len);
1043 * Get the extension offset
1045 ext_off = ntohs(shp->sh_ext_off);
1048 * Decode the body of the message, depending on the type
1050 buff += sizeof(struct scsp_nhdr);
1051 len -= sizeof(struct scsp_nhdr);
1052 switch(msg->sc_msg_type) {
1054 plen = scsp_parse_ca(buff, len, &msg->sc_ca);
1056 case SCSP_CSU_REQ_MSG:
1057 case SCSP_CSU_REPLY_MSG:
1059 plen = scsp_parse_csu(buff, len, &msg->sc_csu_msg);
1061 case SCSP_HELLO_MSG:
1062 plen = scsp_parse_hello(buff, len, &msg->sc_hello);
1074 * Decode any extensions
1077 for (expp = &msg->sc_ext; len > 0;
1078 expp = &(*expp)->next) {
1079 plen = scsp_parse_ext(buff, len, expp);
1089 * Make sure we handled the whole message
1096 * Return the address of the SCSP message in internal format
1103 return(Scsp_msg *)0;