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.
26 * @(#) $FreeBSD: src/usr.sbin/atm/scspd/scsp_output.c,v 1.3 1999/08/28 01:15:33 peter Exp $
27 * @(#) $DragonFly: src/usr.sbin/atm/scspd/scsp_output.c,v 1.2 2003/06/17 04:29:52 dillon Exp $
31 * Server Cache Synchronization Protocol (SCSP) Support
32 * ----------------------------------------------------
34 * Output 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>
65 * Put a long integer into the output buffer
67 * This routine is provided for cases where long ints may not be
68 * word-aligned in the output buffer.
72 * cp pointer to output buffer
86 * Convert to network order and copy to output buffer
89 UM_COPY(&nl, cp, sizeof(u_long));
94 * Format a Sender or Receiver ID
97 * idp ponter to ID structure
101 * 0 input was invalid
102 * else length of ID processed
106 scsp_format_id(idp, buff)
113 UM_COPY(idp->id, buff, idp->id_len);
116 * Return the ID length
123 * Format the Mandatory Common Part of an SCSP input packet
127 * buff pointer to mandatory common part
130 * 0 input was invalid
131 * else length of MCP in message
135 scsp_format_mcp(mcp, buff)
141 struct scsp_nmcp *smp;
144 * Set the protocol ID
146 smp = (struct scsp_nmcp *)buff;
147 smp->sm_pid = htons(mcp->pid);
150 * Set the server group ID
152 smp->sm_sgid = htons(mcp->sgid);
157 smp->sm_flags = htons(mcp->flags);
160 * Set the sender ID and length
162 smp->sm_sid_len = mcp->sid.id_len;
163 odp = buff + sizeof(struct scsp_nmcp);
164 len = scsp_format_id(&mcp->sid, odp);
170 * Set the receiver ID and length
172 smp->sm_rid_len = mcp->rid.id_len;
173 odp += mcp->sid.id_len;
174 len = scsp_format_id(&mcp->rid, odp);
180 * Set the record count
182 smp->sm_rec_cnt = htons(mcp->rec_cnt);
185 * Return the length of data we processed
187 return(sizeof(struct scsp_nmcp) + mcp->sid.id_len +
196 * Format an Extension
199 * exp pointer to extension in internal format
200 * buff pointer to output buffer
201 * blen space available in buffer
204 * 0 input was invalid
205 * else length of extension processed
209 scsp_format_ext(exp, buff, blen)
214 struct scsp_next *sep;
217 * Make sure there's room in the buffer
219 if (blen < (sizeof(struct scsp_next) + exp->len))
225 sep = (struct scsp_next *)buff;
226 sep->se_type = htons(exp->type);
231 sep->se_len = htons(exp->len);
237 buff += sizeof(struct scsp_next);
238 UM_COPY((caddr_t)exp + sizeof(Scsp_ext),
244 * Return the number of bytes processed
246 return(sizeof(struct scsp_next) + exp->len);
251 * Format the ATMARP part of a CSA record
254 * acsp pointer to ATMARP protocol-specific CSA record
255 * buff pointer to output buffer
258 * 0 input was invalid
259 * else length of record processed
263 scsp_format_atmarp(acsp, buff)
264 Scsp_atmarp_csa *acsp;
269 struct scsp_atmarp_ncsa *sanp;
272 * Figure out how long PDU is going to be
274 pkt_len = sizeof(struct scsp_atmarp_ncsa);
275 switch (acsp->sa_sha.address_format) {
276 case T_ATM_ENDSYS_ADDR:
277 pkt_len += acsp->sa_sha.address_length;
280 case T_ATM_E164_ADDR:
281 pkt_len += acsp->sa_sha.address_length;
282 if (acsp->sa_ssa.address_format == T_ATM_ENDSYS_ADDR)
283 pkt_len += acsp->sa_ssa.address_length;
287 switch (acsp->sa_tha.address_format) {
288 case T_ATM_ENDSYS_ADDR:
289 pkt_len += acsp->sa_tha.address_length;
292 case T_ATM_E164_ADDR:
293 pkt_len += acsp->sa_tha.address_length;
294 if (acsp->sa_tha.address_format == T_ATM_ENDSYS_ADDR)
295 pkt_len += acsp->sa_tha.address_length;
299 if (acsp->sa_spa.s_addr != 0)
300 pkt_len += sizeof(struct in_addr);
302 if (acsp->sa_tpa.s_addr != 0)
303 pkt_len += sizeof(struct in_addr);
308 sanp = (struct scsp_atmarp_ncsa *)buff;
309 cp = (char *)sanp + sizeof(struct scsp_atmarp_ncsa);
314 sanp->sa_hrd = htons(ARP_ATMFORUM);
315 sanp->sa_pro = htons(ETHERTYPE_IP);
318 len = acsp->sa_sha.address_length;
319 switch (acsp->sa_sha.address_format) {
320 case T_ATM_ENDSYS_ADDR:
321 sanp->sa_shtl = ARP_TL_NSAPA | (len & ARP_TL_LMASK);
324 UM_COPY(acsp->sa_sha.address, cp, len);
330 case T_ATM_E164_ADDR:
331 sanp->sa_shtl = ARP_TL_E164 | (len & ARP_TL_LMASK);
334 UM_COPY(acsp->sa_sha.address, cp, len);
337 if (acsp->sa_ssa.address_format == T_ATM_ENDSYS_ADDR) {
338 len = acsp->sa_ssa.address_length;
339 sanp->sa_sstl = ARP_TL_NSAPA |
340 (len & ARP_TL_LMASK);
343 UM_COPY(acsp->sa_ssa.address, cp, len);
355 sanp->sa_state = acsp->sa_state;
359 if (acsp->sa_spa.s_addr != 0) {
360 sanp->sa_spln = sizeof(struct in_addr);
361 UM_COPY(&acsp->sa_spa, cp, sizeof(struct in_addr));
362 cp += sizeof(struct in_addr);
366 len = acsp->sa_tha.address_length;
367 switch (acsp->sa_tha.address_format) {
368 case T_ATM_ENDSYS_ADDR:
369 sanp->sa_thtl = ARP_TL_NSAPA | (len & ARP_TL_LMASK);
372 UM_COPY(acsp->sa_tha.address, cp, len);
378 case T_ATM_E164_ADDR:
379 sanp->sa_thtl = ARP_TL_E164 | (len & ARP_TL_LMASK);
382 UM_COPY(acsp->sa_tha.address, cp, len);
385 if (acsp->sa_tsa.address_format == T_ATM_ENDSYS_ADDR) {
386 len = acsp->sa_tha.address_length;
387 sanp->sa_tstl = ARP_TL_NSAPA |
388 (len & ARP_TL_LMASK);
391 UM_COPY(acsp->sa_tsa.address, cp, len);
403 if (acsp->sa_tpa.s_addr != 0) {
404 sanp->sa_tpln = sizeof(struct in_addr);
405 UM_COPY(&acsp->sa_tpa, cp, sizeof(struct in_addr));
413 * Format a Cache State Advertisement or Cache State Advertisement
417 * csapp pointer to CSA or CSAS
418 * buff pointer to output buffer
421 * 0 input was invalid
422 * else length of record processed
426 scsp_format_csa(csap, buff)
432 struct scsp_ncsa *scp;
437 scp = (struct scsp_ncsa *)buff;
438 scp->scs_hop_cnt = htons(csap->hops);
444 scp->scs_nfill = htons(SCSP_CSAS_NULL);
448 * Set the sequence number
450 put_long(csap->seq, (u_char *)&scp->scs_seq);
455 scp->scs_ck_len = csap->key.key_len;
456 odp = buff + sizeof(struct scsp_ncsa);
457 UM_COPY(csap->key.key, odp, scp->scs_ck_len);
460 * Set the originator ID
462 odp += scp->scs_ck_len;
463 scp->scs_oid_len = scsp_format_id(&csap->oid, odp);
466 * Set the protocol-specific data, if present. At the
467 * moment, we only handle data for ATMARP.
469 if (csap->atmarp_data) {
470 odp += scp->scs_oid_len;
471 len = scsp_format_atmarp(csap->atmarp_data, odp);
475 * Set the record length
477 scp->scs_len = htons(sizeof(struct scsp_ncsa) +
478 scp->scs_ck_len + scp->scs_oid_len +
482 * Return the length of data we processed
484 return(ntohs(scp->scs_len));
489 * Format a Cache Alignment message
492 * cap pointer to CA message
493 * buff pointer to output buffer
494 * blen space available in buffer
497 * 0 input was invalid
498 * else length of CA message processed
502 scsp_format_ca(cap, buff, blen)
507 int i, len, proc_len;
508 struct scsp_nca *scap;
512 * Set the sequence number
514 scap = (struct scsp_nca *)buff;
515 put_long(cap->ca_seq, (u_char *)&scap->sca_seq);
516 proc_len = sizeof(scap->sca_seq);
517 buff += sizeof(scap->sca_seq);
522 cap->ca_mcp.flags = 0;
524 cap->ca_mcp.flags |= SCSP_CA_M;
526 cap->ca_mcp.flags |= SCSP_CA_I;
528 cap->ca_mcp.flags |= SCSP_CA_O;
531 * Format the mandatory common part of the message
533 len = scsp_format_mcp(&cap->ca_mcp, buff);
540 * Put any CSAS records into the message
542 for (i = 0, csap = cap->ca_csa_rec; i < cap->ca_mcp.rec_cnt;
543 i++, csap = csap->next) {
544 len = scsp_format_csa(csap, buff);
547 if (proc_len > blen) {
548 scsp_log(LOG_CRIT, "scsp_format_ca: buffer overflow");
554 * Return the length of processed data
564 * Format a Cache State Update Request, Cache State Update Reply, or
565 * Cache State Update Solicit message. These all have the same format,
566 * a Mandatory Common Part followed by a number of CSA or CSAS records.
569 * csup pointer to location to put pointer to CSU Req message
570 * buff pointer to output buffer
571 * blen space available in buffer
574 * 0 input was invalid
575 * else length of CSU Req message processed
579 scsp_format_csu(csup, buff, blen)
584 int i, len, proc_len;
585 struct scsp_ncsu_msg *scsup;
589 * Format the mandatory common part of the message
591 scsup = (struct scsp_ncsu_msg *)buff;
592 len = scsp_format_mcp(&csup->csu_mcp, buff);
599 * Put the CSAS records into the message
601 for (i = 0, csap = csup->csu_csa_rec;
602 i < csup->csu_mcp.rec_cnt && csap;
603 i++, csap = csap->next) {
604 len = scsp_format_csa(csap, buff);
607 if (proc_len > blen) {
608 scsp_log(LOG_CRIT, "scsp_format_csu: buffer overflow");
614 * Return the length of processed data
624 * Format a Hello message
627 * hpp pointer to Hello message
628 * buff pointer to output buffer
629 * blen space available in buffer
632 * 0 input was invalid
633 * else length of Hello message processed
637 scsp_format_hello(hp, buff, blen)
643 struct scsp_nhello *shp;
647 * Set the hello interval
649 shp = (struct scsp_nhello *)buff;
650 shp->sch_hi = htons(hp->hello_int);
653 * Set the dead factor
655 shp->sch_df = htons(hp->dead_factor);
660 shp->sch_fid = htons(hp->family_id);
663 * Process the mandatory common part of the message
665 proc_len = sizeof(struct scsp_nhello) -
666 sizeof(struct scsp_nmcp);
668 len = scsp_format_mcp(&hp->hello_mcp, buff);
675 * Add any additional receiver ID records to the message
677 for (ridp = hp->hello_mcp.rid.next; ridp;
679 len = scsp_format_id(ridp, buff);
688 * Return the length of the Hello message body
690 if (proc_len > blen) {
691 scsp_log(LOG_CRIT, "scsp_format_hello: buffer overflow");
702 * Format an SCSP output packet
705 * dcsp pointer to DCS for which message is being prepared
706 * msg pointer to input packet
707 * bpp pointer to location to put pointer to formatted packet
710 * 0 input packet was invalid
711 * else length of formatted packet
715 scsp_format_msg(dcsp, msg, bpp)
720 char *buff = (char *)0, *e_buff = (char *)0;
721 int buff_len, e_buff_len;
722 int e_len, len, plen;
723 struct scsp_nhdr *shp;
727 * Allocate a buffer for the message
729 buff_len = dcsp->sd_server->ss_mtu;
730 buff = (char *)UM_ALLOC(buff_len);
732 scsp_mem_err("scsp_format_msg: dcsp->sd_server->ss_mtu");
734 UM_ZERO(buff, buff_len);
738 * Encode the fixed header
742 shp = (struct scsp_nhdr *)buff;
743 shp->sh_ver = SCSP_VER_1;
746 * Set the message type
748 shp->sh_type = msg->sc_msg_type;
751 * Point past the fixed header
753 len = sizeof(struct scsp_nhdr);
757 * Encode any extensions into a temporary buffer
762 * Get a buffer for the extensions
765 e_buff = (char *)UM_ALLOC(e_buff_len);
767 scsp_mem_err("scsp_format_msg: e_buff_len");
769 UM_ZERO(e_buff, e_buff_len);
772 * Encode the extensions
774 for (exp = msg->sc_ext = 0; exp; exp = exp->next) {
775 plen = scsp_format_ext(exp, e_buff + e_len,
784 * Free the buffer if we didn't use it
794 * Encode the body of the message, depending on the type
796 switch(msg->sc_msg_type) {
798 plen = scsp_format_ca(msg->sc_ca, buff + len, buff_len);
800 case SCSP_CSU_REQ_MSG:
801 case SCSP_CSU_REPLY_MSG:
803 plen = scsp_format_csu(msg->sc_csu_msg, buff + len,
807 plen = scsp_format_hello(msg->sc_hello, buff + len,
819 * Copy the extensions to the end of the message
822 shp->sh_ext_off = htons(len);
823 UM_COPY(e_buff, buff + len, e_len);
830 shp->sh_len = htons(len);
833 * Compute the message checksum
835 shp->sh_checksum = htons(ip_checksum(buff, len));
838 * Return the length of the buffer
853 * Send an SCSP message
856 * dcsp pointer to DCS control block
857 * msg pointer to message to send
861 * errno error encountered
865 scsp_send_msg(dcsp, msg)
873 * Make sure we have a socket open
875 if (dcsp->sd_sock == -1) {
882 if (((scsp_trace_mode & SCSP_TRACE_HELLO_MSG) &&
883 msg->sc_msg_type == SCSP_HELLO_MSG) ||
884 ((scsp_trace_mode & SCSP_TRACE_CA_MSG) &&
885 msg->sc_msg_type != SCSP_HELLO_MSG)) {
886 scsp_trace_msg(dcsp, msg, 0);
891 * Put the message into network format
893 len = scsp_format_msg(dcsp, msg, &buff);
895 scsp_log(LOG_ERR, "scsp_send_msg: message conversion failed\n");
900 * Write the message to the DCS
902 rc = write(dcsp->sd_sock, (void *)buff, len);
904 if (rc == len || (rc == -1 && errno == EINPROGRESS)) {
908 * There was an error on the write--close the VCC
910 (void)close(dcsp->sd_sock);
914 * Inform the Hello FSM
916 (void)scsp_hfsm(dcsp, SCSP_HFSM_VC_CLOSED,
920 * Set the return code