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_subr.c,v 1.3 1999/08/28 01:15:34 peter Exp $
32 * Server Cache Synchronization Protocol (SCSP) Support
33 * ----------------------------------------------------
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/socket.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_sigmgr.h>
50 #include <netatm/atm_sys.h>
51 #include <netatm/atm_ioctl.h>
52 #include <netatm/uni/unisig_var.h>
67 __RCSID("@(#) $FreeBSD: src/usr.sbin/atm/scspd/scsp_subr.c,v 1.3 1999/08/28 01:15:34 peter Exp $");
72 * Hash an SCSP cache key
75 * ckp pointer to an SCSP cache key structure
88 * Turn cache key into a positive integer
91 for (i = ckp->key_len-1, j = 0;
92 i > 0 && j < sizeof(int);
94 h = (h << 8) + ckp->key[i];
98 * Return the hashed value
100 return(h % SCSP_HASHSZ);
105 * Compare two SCSP IDs
108 * id1p pointer to an SCSP ID structure
109 * id2p pointer to an SCSP ID structure
112 * < 0 id1 is less than id2
113 * 0 id1 and id2 are equal
114 * > 0 id1 is greater than id2
118 scsp_cmp_id(id1p, id2p)
125 * Compare the two IDs, byte for byte
127 for (i = 0; i < id1p->id_len && i < id2p->id_len; i++) {
128 diff = id1p->id[i] - id2p->id[i];
135 * IDs are equal. If lengths differ, the longer ID is
136 * greater than the shorter.
138 return(id1p->id_len - id2p->id_len);
143 * Compare two SCSP cache keys
146 * ck1p pointer to an SCSP cache key structure
147 * ck2p pointer to an SCSP cache key structure
150 * < 0 ck1 is less than ck2
151 * 0 ck1 and ck2 are equal
152 * > 0 ck1 is greater than ck2
156 scsp_cmp_key(ck1p, ck2p)
163 * Compare the two keys, byte for byte
165 for (i = 0; i < ck1p->key_len && i < ck2p->key_len; i++) {
166 diff = ck1p->key[i] - ck2p->key[i];
172 * Keys are equal. If lengths differ, the longer key is
173 * greater than the shorter.
175 return(ck1p->key_len - ck2p->key_len);
180 * Check whether the host system is an ATMARP server for
181 * the LIS associated with a given interface
184 * netif pointer to the network interface name
188 * 0 host is not a server
192 scsp_is_atmarp_server(netif)
196 int buf_len = sizeof(struct air_asrv_rsp);
197 struct atminfreq air;
198 struct air_asrv_rsp *asrv_info;
201 * Get interface information from the kernel
203 strcpy(air.air_int_intf, netif);
204 air.air_opcode = AIOCS_INF_ASV;
205 buf_len = do_info_ioctl(&air, buf_len);
210 * Check the interface's ATMARP server address
212 asrv_info = (struct air_asrv_rsp *) air.air_buf_addr;
213 rc = (asrv_info->asp_addr.address_format == T_ATM_ABSENT) &&
214 (asrv_info->asp_subaddr.address_format ==
222 * Make a copy of a cache summary entry
225 * csep pointer to CSE entry to copy
229 * else pointer to new CSE entry
239 * Allocate memory for the duplicate
241 dupp = (Scsp_cse *)UM_ALLOC(sizeof(Scsp_cse));
243 scsp_mem_err("scsp_dup_cse: sizeof(Scsp_cse)");
247 * Copy data to the duplicate
249 UM_COPY(csep, dupp, sizeof(Scsp_cse));
250 dupp->sc_next = (Scsp_cse *)0;
257 * Make a copy of a CSA or CSAS record
260 * csap pointer to CSE entry to copy
264 * else pointer to new CSA or CSAS record
272 Scsp_atmarp_csa *adp;
275 * Allocate memory for the duplicate
277 dupp = (Scsp_csa *)UM_ALLOC(sizeof(Scsp_csa));
279 scsp_mem_err("scsp_dup_csa: sizeof(Scsp_csa)");
283 * Copy data to the duplicate
285 UM_COPY(csap, dupp, sizeof(Scsp_csa));
286 dupp->next = (Scsp_csa *)0;
289 * Copy protocol-specific data, if it's present
291 if (csap->atmarp_data) {
292 adp = (Scsp_atmarp_csa *)UM_ALLOC(sizeof(Scsp_atmarp_csa));
294 scsp_mem_err("scsp_dup_csa: sizeof(Scsp_atmarp_csa)");
296 UM_COPY(csap->atmarp_data, adp, sizeof(Scsp_atmarp_csa));
297 dupp->atmarp_data = adp;
305 * Copy a cache summary entry into a CSAS
308 * csep pointer to CSE entry to copy
312 * else pointer to CSAS record summarizing the entry
322 * Allocate memory for the duplicate
324 csap = (Scsp_csa *)UM_ALLOC(sizeof(Scsp_csa));
326 scsp_mem_err("scsp_cse2csas: sizeof(Scsp_csa)");
328 UM_ZERO(csap, sizeof(Scsp_csa));
331 * Copy data to the CSAS entry
333 csap->seq = csep->sc_seq;
334 csap->key = csep->sc_key;
335 csap->oid = csep->sc_oid;
342 * Copy an ATMARP cache entry into a cache summary entry
345 * aap pointer to ATMARP cache entry to copy
349 * else pointer to CSE record summarizing the entry
354 Scsp_atmarp_msg *aap;
359 * Allocate memory for the duplicate
361 csep = (Scsp_cse *)UM_ALLOC(sizeof(Scsp_cse));
363 scsp_mem_err("scsp_atmarp2cse: sizeof(Scsp_cse)");
365 UM_ZERO(csep, sizeof(Scsp_cse));
368 * Copy data to the CSE entry
370 csep->sc_seq = aap->sa_seq;
371 csep->sc_key = aap->sa_key;
372 csep->sc_oid = aap->sa_oid;
379 * Clean up a DCS block. This routine is called to clear out any
380 * lingering state information when the CA FSM reverts to an 'earlier'
381 * state (Down or Master/Slave Negotiation).
384 * dcsp pointer to a DCS control block for the neighbor
391 scsp_dcs_cleanup(dcsp)
394 Scsp_cse *csep, *ncsep;
395 Scsp_csa *csap, *next_csap;
396 Scsp_csu_rexmt *rxp, *rx_next;
399 * Free any CSAS entries waiting to be sent
401 for (csep = dcsp->sd_ca_csas; csep; csep = ncsep) {
402 ncsep = csep->sc_next;
403 UNLINK(csep, Scsp_cse, dcsp->sd_ca_csas, sc_next);
408 * Free any entries on the CRL
410 for (csap = dcsp->sd_crl; csap; csap = next_csap) {
411 next_csap = csap->next;
412 UNLINK(csap, Scsp_csa, dcsp->sd_crl, next);
417 * Free any saved CA message and cancel the CA
418 * retransmission timer
420 if (dcsp->sd_ca_rexmt_msg) {
421 scsp_free_msg(dcsp->sd_ca_rexmt_msg);
422 dcsp->sd_ca_rexmt_msg = (Scsp_msg *)0;
424 HARP_CANCEL(&dcsp->sd_ca_rexmt_t);
427 * Free any saved CSU Solicit message and cancel the CSUS
428 * retransmission timer
430 if (dcsp->sd_csus_rexmt_msg) {
431 scsp_free_msg(dcsp->sd_csus_rexmt_msg);
432 dcsp->sd_csus_rexmt_msg = (Scsp_msg *)0;
434 HARP_CANCEL(&dcsp->sd_csus_rexmt_t);
437 * Free any entries on the CSU Request retransmission queue
439 for (rxp = dcsp->sd_csu_rexmt; rxp; rxp = rx_next) {
440 rx_next = rxp->sr_next;
441 HARP_CANCEL(&rxp->sr_t);
442 for (csap = rxp->sr_csa; csap; csap = next_csap) {
443 next_csap = csap->next;
446 UNLINK(rxp, Scsp_csu_rexmt, dcsp->sd_csu_rexmt,
454 * Delete an SCSP DCS block and any associated information
457 * dcsp pointer to a DCS control block to delete
464 scsp_dcs_delete(dcsp)
467 Scsp_cse *csep, *next_cse;
468 Scsp_csu_rexmt *rxp, *next_rxp;
469 Scsp_csa *csap, *next_csa;
472 * Cancel any pending DCS timers
474 HARP_CANCEL(&dcsp->sd_open_t);
475 HARP_CANCEL(&dcsp->sd_hello_h_t);
476 HARP_CANCEL(&dcsp->sd_hello_rcv_t);
477 HARP_CANCEL(&dcsp->sd_ca_rexmt_t);
478 HARP_CANCEL(&dcsp->sd_csus_rexmt_t);
481 * Unlink the DCS block from the server block
483 UNLINK(dcsp, Scsp_dcs, dcsp->sd_server->ss_dcs, sd_next);
486 * Close the VCC to the DCS, if one is open
488 if (dcsp->sd_sock != -1) {
489 (void)close(dcsp->sd_sock);
493 * Free any saved CA message
495 if (dcsp->sd_ca_rexmt_msg) {
496 scsp_free_msg(dcsp->sd_ca_rexmt_msg);
500 * Free any pending CSAs waiting for cache alignment
502 for (csep = dcsp->sd_ca_csas; csep; csep = next_cse) {
503 next_cse = csep->sc_next;
508 * Free anything on the cache request list
510 for (csap = dcsp->sd_crl; csap; csap = next_csa) {
511 next_csa = csap->next;
516 * Free any saved CSUS message
518 if (dcsp->sd_csus_rexmt_msg) {
519 scsp_free_msg(dcsp->sd_csus_rexmt_msg);
523 * Free anything on the CSU Request retransmit queue
525 for (rxp = dcsp->sd_csu_rexmt; rxp; rxp = next_rxp) {
527 * Cancel the retransmit timer
529 HARP_CANCEL(&rxp->sr_t);
532 * Free the CSAs to be retransmitted
534 for (csap = rxp->sr_csa; csap; csap = next_csa) {
535 next_csa = csap->next;
540 * Free the CSU Req retransmission control block
542 next_rxp = rxp->sr_next;
554 * Shut down a server. This routine is called when a connection to
555 * a server is lost. It will clear the server's state without deleting
559 * ssp pointer to a server control block
566 scsp_server_shutdown(ssp)
576 if (scsp_trace_mode & (SCSP_TRACE_IF_MSG | SCSP_TRACE_CFSM)) {
577 scsp_trace("Server %s being shut down\n",
582 * Terminate up all the DCS connections and clean
583 * up the control blocks
585 for (dcsp = ssp->ss_dcs; dcsp; dcsp = dcsp->sd_next) {
586 if (dcsp->sd_sock != -1) {
587 (void)close(dcsp->sd_sock);
590 HARP_CANCEL(&dcsp->sd_open_t);
591 HARP_CANCEL(&dcsp->sd_hello_h_t);
592 HARP_CANCEL(&dcsp->sd_hello_rcv_t);
593 scsp_dcs_cleanup(dcsp);
594 dcsp->sd_hello_state = SCSP_HFSM_DOWN;
595 dcsp->sd_ca_state = SCSP_CAFSM_DOWN;
596 dcsp->sd_client_state = SCSP_CIFSM_NULL;
600 * Clean up the server control block
602 if (ssp->ss_sock != -1) {
603 (void)close(ssp->ss_sock);
606 if (ssp->ss_dcs_lsock != -1) {
607 (void)close(ssp->ss_dcs_lsock);
608 ssp->ss_dcs_lsock = -1;
610 ssp->ss_state = SCSP_SS_NULL;
613 * Free the entries in the server's summary cache
615 for (i = 0; i < SCSP_HASHSZ; i++) {
616 while (ssp->ss_cache[i]) {
617 csep = ssp->ss_cache[i];
618 UNLINK(csep, Scsp_cse, ssp->ss_cache[i],
627 * Delete an SCSP server block and any associated information
630 * ssp pointer to a server control block to delete
637 scsp_server_delete(ssp)
641 Scsp_dcs *dcsp, *next_dcs;
642 Scsp_cse *csep, *next_cse;
645 * Unlink the server block from the chain
647 UNLINK(ssp, Scsp_server, scsp_server_head, ss_next);
650 * Free the DCS blocks associated with the server
652 for (dcsp = ssp->ss_dcs; dcsp; dcsp = next_dcs) {
653 next_dcs = dcsp->sd_next;
654 scsp_dcs_delete(dcsp);
658 * Free the entries in the server's summary cache
660 for (i = 0; i < SCSP_HASHSZ; i++) {
661 for (csep = ssp->ss_cache[i]; csep; csep = next_cse) {
662 next_cse = csep->sc_next;
668 * Free the server block
670 UM_FREE(ssp->ss_name);
676 * Get informtion about a server from the kernel
679 * ssp pointer to the server block
682 * 0 server info is OK
683 * errno server is not ready
687 scsp_get_server_info(ssp)
690 int i, len, mtu, rc, sel;
691 struct atminfreq air;
692 struct air_netif_rsp *netif_rsp = (struct air_netif_rsp *)0;
693 struct air_int_rsp *intf_rsp = (struct air_int_rsp *)0;
694 struct air_cfg_rsp *cfg_rsp = (struct air_cfg_rsp *)0;
695 struct sockaddr_in *ip_addr;
699 * Make sure we're the server for the interface
701 if (!scsp_is_atmarp_server(ssp->ss_intf)) {
703 goto server_info_done;
707 * Get the IP address and physical interface name
708 * associated with the network interface
710 UM_ZERO(&air, sizeof(struct atminfreq));
711 air.air_opcode = AIOCS_INF_NIF;
712 strcpy(air.air_netif_intf, ssp->ss_intf);
713 len = do_info_ioctl(&air, sizeof(struct air_netif_rsp));
716 goto server_info_done;
718 netif_rsp = (struct air_netif_rsp *)air.air_buf_addr;
720 ip_addr = (struct sockaddr_in *)&netif_rsp->anp_proto_addr;
721 if (ip_addr->sin_family != AF_INET ||
722 ip_addr->sin_addr.s_addr == 0) {
724 goto server_info_done;
728 * Get the MTU for the network interface
730 mtu = get_mtu(ssp->ss_intf);
733 goto server_info_done;
737 * Get the ATM address associated with the
740 UM_ZERO(&air, sizeof(struct atminfreq));
741 air.air_opcode = AIOCS_INF_INT;
742 strcpy(air.air_int_intf, netif_rsp->anp_phy_intf);
743 len = do_info_ioctl(&air, sizeof(struct air_int_rsp));
746 goto server_info_done;
748 intf_rsp = (struct air_int_rsp *)air.air_buf_addr;
751 * Make sure we're running UNI signalling
753 if (intf_rsp->anp_sig_proto != ATM_SIG_UNI30 &&
754 intf_rsp->anp_sig_proto != ATM_SIG_UNI31 &&
755 intf_rsp->anp_sig_proto != ATM_SIG_UNI40) {
757 goto server_info_done;
761 * Check the physical interface's state
763 if (intf_rsp->anp_sig_state != UNISIG_ACTIVE) {
765 goto server_info_done;
769 * Make sure the interface's address is valid
771 if (intf_rsp->anp_addr.address_format != T_ATM_ENDSYS_ADDR &&
772 !(intf_rsp->anp_addr.address_format ==
774 intf_rsp->anp_subaddr.address_format ==
775 T_ATM_ENDSYS_ADDR)) {
777 goto server_info_done;
781 * Find the selector byte value for the interface
783 for (i=0; i<strlen(ssp->ss_intf); i++) {
784 if (ssp->ss_intf[i] >= '0' &&
785 ssp->ss_intf[i] <= '9')
788 sel = atoi(&ssp->ss_intf[i]);
791 * Get configuration information associated with the
794 UM_ZERO(&air, sizeof(struct atminfreq));
795 air.air_opcode = AIOCS_INF_CFG;
796 strcpy(air.air_int_intf, netif_rsp->anp_phy_intf);
797 len = do_info_ioctl(&air, sizeof(struct air_cfg_rsp));
800 goto server_info_done;
802 cfg_rsp = (struct air_cfg_rsp *)air.air_buf_addr;
805 * Update the server entry
807 UM_COPY(&ip_addr->sin_addr, ssp->ss_lsid.id, ssp->ss_id_len);
808 ssp->ss_lsid.id_len = ssp->ss_id_len;
809 ssp->ss_mtu = mtu + 8;
810 ATM_ADDR_COPY(&intf_rsp->anp_addr, &ssp->ss_addr);
811 ATM_ADDR_COPY(&intf_rsp->anp_subaddr, &ssp->ss_subaddr);
812 if (ssp->ss_addr.address_format == T_ATM_ENDSYS_ADDR) {
813 anp = (Atm_addr_nsap *)ssp->ss_addr.address;
815 } else if (ssp->ss_addr.address_format == T_ATM_E164_ADDR &&
816 ssp->ss_subaddr.address_format ==
818 anp = (Atm_addr_nsap *)ssp->ss_subaddr.address;
821 ssp->ss_media = cfg_rsp->acp_cfg.ac_media;
840 * Process a CA message
843 * dcsp pointer to a DCS control block for the neighbor
844 * cap pointer to the CA part of the received message
851 scsp_process_ca(dcsp, cap)
855 Scsp_csa *csap, *next_csap;
857 Scsp_server *ssp = dcsp->sd_server;
860 * Process CSAS records from the CA message
862 for (csap = cap->ca_csa_rec; csap; csap = next_csap) {
863 next_csap = csap->next;
864 SCSP_LOOKUP(ssp, &csap->key, csep);
865 if (!csep || (scsp_cmp_id(&csap->oid,
866 &csep->sc_oid) == 0 &&
867 csap->seq > csep->sc_seq)) {
869 * CSAS entry not in cache or more
870 * up to date than cache, add it to CRL
872 UNLINK(csap, Scsp_csa, cap->ca_csa_rec, next);
873 LINK2TAIL(csap, Scsp_csa, dcsp->sd_crl, next);
880 * Process a Cache Response message from a server
883 * ssp pointer to the server block
884 * smp pointer to the message
891 scsp_process_cache_rsp(ssp, smp)
896 Scsp_atmarp_msg *aap;
900 * Loop through the message, processing each cache entry
903 len -= sizeof(Scsp_if_msg_hdr);
904 aap = &smp->si_atmarp;
906 switch(smp->si_proto) {
907 case SCSP_ATMARP_PROTO:
909 * If we already have an entry with this key,
912 SCSP_LOOKUP(ssp, &aap->sa_key, csep);
914 SCSP_DELETE(ssp, csep);
919 * Copy the data from the server to a cache
922 csep = scsp_atmarp2cse(aap);
925 * Point past this entry
927 len -= sizeof(Scsp_atmarp_msg);
930 case SCSP_NHRP_PROTO:
933 * Not implemented yet
939 * Add the new summary entry to the cache
947 * Propagate a CSA to all the DCSs in the server group except
948 * the one the CSA was received from
951 * dcsp pointer to a the DCS the CSA came from
952 * csap pointer to a the CSA
956 * errno error encountered
960 scsp_propagate_csa(dcsp, csap)
965 Scsp_server *ssp = dcsp->sd_server;
970 * Check the hop count in the CSA
976 * Pass the cache entry on to the server's other DCSs
978 for (dcsp1 = ssp->ss_dcs; dcsp1; dcsp1 = dcsp1->sd_next) {
980 * Skip this DCS if it's the one we got
989 csap1 = scsp_dup_csa(csap);
992 * Decrement the hop count
997 * Send the copy of the CSA to the CA FSM for the DCS
999 rc = scsp_cafsm(dcsp1, SCSP_CAFSM_CACHE_UPD,
1010 * Update SCSP's cache given a CSA or CSAS
1013 * dcsp pointer to a DCS
1014 * csap pointer to a CSA
1021 scsp_update_cache(dcsp, csap)
1028 * Check whether we already have this in the cache
1030 SCSP_LOOKUP(dcsp->sd_server, &csap->key, csep);
1033 * If we don't already have it and it's not being deleted,
1034 * build a new cache summary entry
1036 if (!csep && !csap->null) {
1038 * Get memory for a new entry
1040 csep = (Scsp_cse *)UM_ALLOC(sizeof(Scsp_cse));
1042 scsp_mem_err("scsp_update_cache: sizeof(Scsp_cse)");
1044 UM_ZERO(csep, sizeof(Scsp_cse));
1047 * Fill out the new cache summary entry
1049 csep->sc_seq = csap->seq;
1050 csep->sc_key = csap->key;
1051 csep->sc_oid = csap->oid;
1054 * Add the new entry to the cache
1056 SCSP_ADD(dcsp->sd_server, csep);
1060 * Update or delete the entry
1064 * The null flag is set--delete the entry
1067 SCSP_DELETE(dcsp->sd_server, csep);
1072 * Update the existing entry
1074 csep->sc_seq = csap->seq;
1075 csep->sc_oid = csap->oid;
1083 * Called as the result of a SIGHUP interrupt. Reread the
1084 * configuration file and solicit the cache from the server.
1100 * Log a message saying we're reconfiguring
1102 scsp_log(LOG_ERR, "Reconfiguring ...");
1105 * Re-read the configuration file
1107 rc = scsp_config(scsp_config_file);
1109 scsp_log(LOG_ERR, "Found %d error%s in configuration file",
1110 rc, ((rc == 1) ? "" : "s"));
1115 * If a connection to a server is open, get the cache from
1118 for (ssp = scsp_server_head; ssp; ssp = ssp->ss_next) {
1119 if (ssp->ss_sock != -1) {
1120 rc = scsp_send_cache_ind(ssp);