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 $
27 * @(#) $DragonFly: src/usr.sbin/atm/scspd/scsp_subr.c,v 1.2 2003/06/17 04:29:52 dillon 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 * Hash an SCSP cache key
70 * ckp pointer to an SCSP cache key structure
83 * Turn cache key into a positive integer
86 for (i = ckp->key_len-1, j = 0;
87 i > 0 && j < sizeof(int);
89 h = (h << 8) + ckp->key[i];
93 * Return the hashed value
95 return(h % SCSP_HASHSZ);
100 * Compare two SCSP IDs
103 * id1p pointer to an SCSP ID structure
104 * id2p pointer to an SCSP ID structure
107 * < 0 id1 is less than id2
108 * 0 id1 and id2 are equal
109 * > 0 id1 is greater than id2
113 scsp_cmp_id(id1p, id2p)
120 * Compare the two IDs, byte for byte
122 for (i = 0; i < id1p->id_len && i < id2p->id_len; i++) {
123 diff = id1p->id[i] - id2p->id[i];
130 * IDs are equal. If lengths differ, the longer ID is
131 * greater than the shorter.
133 return(id1p->id_len - id2p->id_len);
138 * Compare two SCSP cache keys
141 * ck1p pointer to an SCSP cache key structure
142 * ck2p pointer to an SCSP cache key structure
145 * < 0 ck1 is less than ck2
146 * 0 ck1 and ck2 are equal
147 * > 0 ck1 is greater than ck2
151 scsp_cmp_key(ck1p, ck2p)
158 * Compare the two keys, byte for byte
160 for (i = 0; i < ck1p->key_len && i < ck2p->key_len; i++) {
161 diff = ck1p->key[i] - ck2p->key[i];
167 * Keys are equal. If lengths differ, the longer key is
168 * greater than the shorter.
170 return(ck1p->key_len - ck2p->key_len);
175 * Check whether the host system is an ATMARP server for
176 * the LIS associated with a given interface
179 * netif pointer to the network interface name
183 * 0 host is not a server
187 scsp_is_atmarp_server(netif)
191 int buf_len = sizeof(struct air_asrv_rsp);
192 struct atminfreq air;
193 struct air_asrv_rsp *asrv_info;
196 * Get interface information from the kernel
198 strcpy(air.air_int_intf, netif);
199 air.air_opcode = AIOCS_INF_ASV;
200 buf_len = do_info_ioctl(&air, buf_len);
205 * Check the interface's ATMARP server address
207 asrv_info = (struct air_asrv_rsp *) air.air_buf_addr;
208 rc = (asrv_info->asp_addr.address_format == T_ATM_ABSENT) &&
209 (asrv_info->asp_subaddr.address_format ==
217 * Make a copy of a cache summary entry
220 * csep pointer to CSE entry to copy
224 * else pointer to new CSE entry
234 * Allocate memory for the duplicate
236 dupp = (Scsp_cse *)UM_ALLOC(sizeof(Scsp_cse));
238 scsp_mem_err("scsp_dup_cse: sizeof(Scsp_cse)");
242 * Copy data to the duplicate
244 UM_COPY(csep, dupp, sizeof(Scsp_cse));
245 dupp->sc_next = (Scsp_cse *)0;
252 * Make a copy of a CSA or CSAS record
255 * csap pointer to CSE entry to copy
259 * else pointer to new CSA or CSAS record
267 Scsp_atmarp_csa *adp;
270 * Allocate memory for the duplicate
272 dupp = (Scsp_csa *)UM_ALLOC(sizeof(Scsp_csa));
274 scsp_mem_err("scsp_dup_csa: sizeof(Scsp_csa)");
278 * Copy data to the duplicate
280 UM_COPY(csap, dupp, sizeof(Scsp_csa));
281 dupp->next = (Scsp_csa *)0;
284 * Copy protocol-specific data, if it's present
286 if (csap->atmarp_data) {
287 adp = (Scsp_atmarp_csa *)UM_ALLOC(sizeof(Scsp_atmarp_csa));
289 scsp_mem_err("scsp_dup_csa: sizeof(Scsp_atmarp_csa)");
291 UM_COPY(csap->atmarp_data, adp, sizeof(Scsp_atmarp_csa));
292 dupp->atmarp_data = adp;
300 * Copy a cache summary entry into a CSAS
303 * csep pointer to CSE entry to copy
307 * else pointer to CSAS record summarizing the entry
317 * Allocate memory for the duplicate
319 csap = (Scsp_csa *)UM_ALLOC(sizeof(Scsp_csa));
321 scsp_mem_err("scsp_cse2csas: sizeof(Scsp_csa)");
323 UM_ZERO(csap, sizeof(Scsp_csa));
326 * Copy data to the CSAS entry
328 csap->seq = csep->sc_seq;
329 csap->key = csep->sc_key;
330 csap->oid = csep->sc_oid;
337 * Copy an ATMARP cache entry into a cache summary entry
340 * aap pointer to ATMARP cache entry to copy
344 * else pointer to CSE record summarizing the entry
349 Scsp_atmarp_msg *aap;
354 * Allocate memory for the duplicate
356 csep = (Scsp_cse *)UM_ALLOC(sizeof(Scsp_cse));
358 scsp_mem_err("scsp_atmarp2cse: sizeof(Scsp_cse)");
360 UM_ZERO(csep, sizeof(Scsp_cse));
363 * Copy data to the CSE entry
365 csep->sc_seq = aap->sa_seq;
366 csep->sc_key = aap->sa_key;
367 csep->sc_oid = aap->sa_oid;
374 * Clean up a DCS block. This routine is called to clear out any
375 * lingering state information when the CA FSM reverts to an 'earlier'
376 * state (Down or Master/Slave Negotiation).
379 * dcsp pointer to a DCS control block for the neighbor
386 scsp_dcs_cleanup(dcsp)
389 Scsp_cse *csep, *ncsep;
390 Scsp_csa *csap, *next_csap;
391 Scsp_csu_rexmt *rxp, *rx_next;
394 * Free any CSAS entries waiting to be sent
396 for (csep = dcsp->sd_ca_csas; csep; csep = ncsep) {
397 ncsep = csep->sc_next;
398 UNLINK(csep, Scsp_cse, dcsp->sd_ca_csas, sc_next);
403 * Free any entries on the CRL
405 for (csap = dcsp->sd_crl; csap; csap = next_csap) {
406 next_csap = csap->next;
407 UNLINK(csap, Scsp_csa, dcsp->sd_crl, next);
412 * Free any saved CA message and cancel the CA
413 * retransmission timer
415 if (dcsp->sd_ca_rexmt_msg) {
416 scsp_free_msg(dcsp->sd_ca_rexmt_msg);
417 dcsp->sd_ca_rexmt_msg = (Scsp_msg *)0;
419 HARP_CANCEL(&dcsp->sd_ca_rexmt_t);
422 * Free any saved CSU Solicit message and cancel the CSUS
423 * retransmission timer
425 if (dcsp->sd_csus_rexmt_msg) {
426 scsp_free_msg(dcsp->sd_csus_rexmt_msg);
427 dcsp->sd_csus_rexmt_msg = (Scsp_msg *)0;
429 HARP_CANCEL(&dcsp->sd_csus_rexmt_t);
432 * Free any entries on the CSU Request retransmission queue
434 for (rxp = dcsp->sd_csu_rexmt; rxp; rxp = rx_next) {
435 rx_next = rxp->sr_next;
436 HARP_CANCEL(&rxp->sr_t);
437 for (csap = rxp->sr_csa; csap; csap = next_csap) {
438 next_csap = csap->next;
441 UNLINK(rxp, Scsp_csu_rexmt, dcsp->sd_csu_rexmt,
449 * Delete an SCSP DCS block and any associated information
452 * dcsp pointer to a DCS control block to delete
459 scsp_dcs_delete(dcsp)
462 Scsp_cse *csep, *next_cse;
463 Scsp_csu_rexmt *rxp, *next_rxp;
464 Scsp_csa *csap, *next_csa;
467 * Cancel any pending DCS timers
469 HARP_CANCEL(&dcsp->sd_open_t);
470 HARP_CANCEL(&dcsp->sd_hello_h_t);
471 HARP_CANCEL(&dcsp->sd_hello_rcv_t);
472 HARP_CANCEL(&dcsp->sd_ca_rexmt_t);
473 HARP_CANCEL(&dcsp->sd_csus_rexmt_t);
476 * Unlink the DCS block from the server block
478 UNLINK(dcsp, Scsp_dcs, dcsp->sd_server->ss_dcs, sd_next);
481 * Close the VCC to the DCS, if one is open
483 if (dcsp->sd_sock != -1) {
484 (void)close(dcsp->sd_sock);
488 * Free any saved CA message
490 if (dcsp->sd_ca_rexmt_msg) {
491 scsp_free_msg(dcsp->sd_ca_rexmt_msg);
495 * Free any pending CSAs waiting for cache alignment
497 for (csep = dcsp->sd_ca_csas; csep; csep = next_cse) {
498 next_cse = csep->sc_next;
503 * Free anything on the cache request list
505 for (csap = dcsp->sd_crl; csap; csap = next_csa) {
506 next_csa = csap->next;
511 * Free any saved CSUS message
513 if (dcsp->sd_csus_rexmt_msg) {
514 scsp_free_msg(dcsp->sd_csus_rexmt_msg);
518 * Free anything on the CSU Request retransmit queue
520 for (rxp = dcsp->sd_csu_rexmt; rxp; rxp = next_rxp) {
522 * Cancel the retransmit timer
524 HARP_CANCEL(&rxp->sr_t);
527 * Free the CSAs to be retransmitted
529 for (csap = rxp->sr_csa; csap; csap = next_csa) {
530 next_csa = csap->next;
535 * Free the CSU Req retransmission control block
537 next_rxp = rxp->sr_next;
549 * Shut down a server. This routine is called when a connection to
550 * a server is lost. It will clear the server's state without deleting
554 * ssp pointer to a server control block
561 scsp_server_shutdown(ssp)
571 if (scsp_trace_mode & (SCSP_TRACE_IF_MSG | SCSP_TRACE_CFSM)) {
572 scsp_trace("Server %s being shut down\n",
577 * Terminate up all the DCS connections and clean
578 * up the control blocks
580 for (dcsp = ssp->ss_dcs; dcsp; dcsp = dcsp->sd_next) {
581 if (dcsp->sd_sock != -1) {
582 (void)close(dcsp->sd_sock);
585 HARP_CANCEL(&dcsp->sd_open_t);
586 HARP_CANCEL(&dcsp->sd_hello_h_t);
587 HARP_CANCEL(&dcsp->sd_hello_rcv_t);
588 scsp_dcs_cleanup(dcsp);
589 dcsp->sd_hello_state = SCSP_HFSM_DOWN;
590 dcsp->sd_ca_state = SCSP_CAFSM_DOWN;
591 dcsp->sd_client_state = SCSP_CIFSM_NULL;
595 * Clean up the server control block
597 if (ssp->ss_sock != -1) {
598 (void)close(ssp->ss_sock);
601 if (ssp->ss_dcs_lsock != -1) {
602 (void)close(ssp->ss_dcs_lsock);
603 ssp->ss_dcs_lsock = -1;
605 ssp->ss_state = SCSP_SS_NULL;
608 * Free the entries in the server's summary cache
610 for (i = 0; i < SCSP_HASHSZ; i++) {
611 while (ssp->ss_cache[i]) {
612 csep = ssp->ss_cache[i];
613 UNLINK(csep, Scsp_cse, ssp->ss_cache[i],
622 * Delete an SCSP server block and any associated information
625 * ssp pointer to a server control block to delete
632 scsp_server_delete(ssp)
636 Scsp_dcs *dcsp, *next_dcs;
637 Scsp_cse *csep, *next_cse;
640 * Unlink the server block from the chain
642 UNLINK(ssp, Scsp_server, scsp_server_head, ss_next);
645 * Free the DCS blocks associated with the server
647 for (dcsp = ssp->ss_dcs; dcsp; dcsp = next_dcs) {
648 next_dcs = dcsp->sd_next;
649 scsp_dcs_delete(dcsp);
653 * Free the entries in the server's summary cache
655 for (i = 0; i < SCSP_HASHSZ; i++) {
656 for (csep = ssp->ss_cache[i]; csep; csep = next_cse) {
657 next_cse = csep->sc_next;
663 * Free the server block
665 UM_FREE(ssp->ss_name);
671 * Get informtion about a server from the kernel
674 * ssp pointer to the server block
677 * 0 server info is OK
678 * errno server is not ready
682 scsp_get_server_info(ssp)
685 int i, len, mtu, rc, sel;
686 struct atminfreq air;
687 struct air_netif_rsp *netif_rsp = (struct air_netif_rsp *)0;
688 struct air_int_rsp *intf_rsp = (struct air_int_rsp *)0;
689 struct air_cfg_rsp *cfg_rsp = (struct air_cfg_rsp *)0;
690 struct sockaddr_in *ip_addr;
694 * Make sure we're the server for the interface
696 if (!scsp_is_atmarp_server(ssp->ss_intf)) {
698 goto server_info_done;
702 * Get the IP address and physical interface name
703 * associated with the network interface
705 UM_ZERO(&air, sizeof(struct atminfreq));
706 air.air_opcode = AIOCS_INF_NIF;
707 strcpy(air.air_netif_intf, ssp->ss_intf);
708 len = do_info_ioctl(&air, sizeof(struct air_netif_rsp));
711 goto server_info_done;
713 netif_rsp = (struct air_netif_rsp *)air.air_buf_addr;
715 ip_addr = (struct sockaddr_in *)&netif_rsp->anp_proto_addr;
716 if (ip_addr->sin_family != AF_INET ||
717 ip_addr->sin_addr.s_addr == 0) {
719 goto server_info_done;
723 * Get the MTU for the network interface
725 mtu = get_mtu(ssp->ss_intf);
728 goto server_info_done;
732 * Get the ATM address associated with the
735 UM_ZERO(&air, sizeof(struct atminfreq));
736 air.air_opcode = AIOCS_INF_INT;
737 strcpy(air.air_int_intf, netif_rsp->anp_phy_intf);
738 len = do_info_ioctl(&air, sizeof(struct air_int_rsp));
741 goto server_info_done;
743 intf_rsp = (struct air_int_rsp *)air.air_buf_addr;
746 * Make sure we're running UNI signalling
748 if (intf_rsp->anp_sig_proto != ATM_SIG_UNI30 &&
749 intf_rsp->anp_sig_proto != ATM_SIG_UNI31 &&
750 intf_rsp->anp_sig_proto != ATM_SIG_UNI40) {
752 goto server_info_done;
756 * Check the physical interface's state
758 if (intf_rsp->anp_sig_state != UNISIG_ACTIVE) {
760 goto server_info_done;
764 * Make sure the interface's address is valid
766 if (intf_rsp->anp_addr.address_format != T_ATM_ENDSYS_ADDR &&
767 !(intf_rsp->anp_addr.address_format ==
769 intf_rsp->anp_subaddr.address_format ==
770 T_ATM_ENDSYS_ADDR)) {
772 goto server_info_done;
776 * Find the selector byte value for the interface
778 for (i=0; i<strlen(ssp->ss_intf); i++) {
779 if (ssp->ss_intf[i] >= '0' &&
780 ssp->ss_intf[i] <= '9')
783 sel = atoi(&ssp->ss_intf[i]);
786 * Get configuration information associated with the
789 UM_ZERO(&air, sizeof(struct atminfreq));
790 air.air_opcode = AIOCS_INF_CFG;
791 strcpy(air.air_int_intf, netif_rsp->anp_phy_intf);
792 len = do_info_ioctl(&air, sizeof(struct air_cfg_rsp));
795 goto server_info_done;
797 cfg_rsp = (struct air_cfg_rsp *)air.air_buf_addr;
800 * Update the server entry
802 UM_COPY(&ip_addr->sin_addr, ssp->ss_lsid.id, ssp->ss_id_len);
803 ssp->ss_lsid.id_len = ssp->ss_id_len;
804 ssp->ss_mtu = mtu + 8;
805 ATM_ADDR_COPY(&intf_rsp->anp_addr, &ssp->ss_addr);
806 ATM_ADDR_COPY(&intf_rsp->anp_subaddr, &ssp->ss_subaddr);
807 if (ssp->ss_addr.address_format == T_ATM_ENDSYS_ADDR) {
808 anp = (Atm_addr_nsap *)ssp->ss_addr.address;
810 } else if (ssp->ss_addr.address_format == T_ATM_E164_ADDR &&
811 ssp->ss_subaddr.address_format ==
813 anp = (Atm_addr_nsap *)ssp->ss_subaddr.address;
816 ssp->ss_media = cfg_rsp->acp_cfg.ac_media;
835 * Process a CA message
838 * dcsp pointer to a DCS control block for the neighbor
839 * cap pointer to the CA part of the received message
846 scsp_process_ca(dcsp, cap)
850 Scsp_csa *csap, *next_csap;
852 Scsp_server *ssp = dcsp->sd_server;
855 * Process CSAS records from the CA message
857 for (csap = cap->ca_csa_rec; csap; csap = next_csap) {
858 next_csap = csap->next;
859 SCSP_LOOKUP(ssp, &csap->key, csep);
860 if (!csep || (scsp_cmp_id(&csap->oid,
861 &csep->sc_oid) == 0 &&
862 csap->seq > csep->sc_seq)) {
864 * CSAS entry not in cache or more
865 * up to date than cache, add it to CRL
867 UNLINK(csap, Scsp_csa, cap->ca_csa_rec, next);
868 LINK2TAIL(csap, Scsp_csa, dcsp->sd_crl, next);
875 * Process a Cache Response message from a server
878 * ssp pointer to the server block
879 * smp pointer to the message
886 scsp_process_cache_rsp(ssp, smp)
891 Scsp_atmarp_msg *aap;
895 * Loop through the message, processing each cache entry
898 len -= sizeof(Scsp_if_msg_hdr);
899 aap = &smp->si_atmarp;
901 switch(smp->si_proto) {
902 case SCSP_ATMARP_PROTO:
904 * If we already have an entry with this key,
907 SCSP_LOOKUP(ssp, &aap->sa_key, csep);
909 SCSP_DELETE(ssp, csep);
914 * Copy the data from the server to a cache
917 csep = scsp_atmarp2cse(aap);
920 * Point past this entry
922 len -= sizeof(Scsp_atmarp_msg);
925 case SCSP_NHRP_PROTO:
928 * Not implemented yet
934 * Add the new summary entry to the cache
942 * Propagate a CSA to all the DCSs in the server group except
943 * the one the CSA was received from
946 * dcsp pointer to a the DCS the CSA came from
947 * csap pointer to a the CSA
951 * errno error encountered
955 scsp_propagate_csa(dcsp, csap)
960 Scsp_server *ssp = dcsp->sd_server;
965 * Check the hop count in the CSA
971 * Pass the cache entry on to the server's other DCSs
973 for (dcsp1 = ssp->ss_dcs; dcsp1; dcsp1 = dcsp1->sd_next) {
975 * Skip this DCS if it's the one we got
984 csap1 = scsp_dup_csa(csap);
987 * Decrement the hop count
992 * Send the copy of the CSA to the CA FSM for the DCS
994 rc = scsp_cafsm(dcsp1, SCSP_CAFSM_CACHE_UPD,
1005 * Update SCSP's cache given a CSA or CSAS
1008 * dcsp pointer to a DCS
1009 * csap pointer to a CSA
1016 scsp_update_cache(dcsp, csap)
1023 * Check whether we already have this in the cache
1025 SCSP_LOOKUP(dcsp->sd_server, &csap->key, csep);
1028 * If we don't already have it and it's not being deleted,
1029 * build a new cache summary entry
1031 if (!csep && !csap->null) {
1033 * Get memory for a new entry
1035 csep = (Scsp_cse *)UM_ALLOC(sizeof(Scsp_cse));
1037 scsp_mem_err("scsp_update_cache: sizeof(Scsp_cse)");
1039 UM_ZERO(csep, sizeof(Scsp_cse));
1042 * Fill out the new cache summary entry
1044 csep->sc_seq = csap->seq;
1045 csep->sc_key = csap->key;
1046 csep->sc_oid = csap->oid;
1049 * Add the new entry to the cache
1051 SCSP_ADD(dcsp->sd_server, csep);
1055 * Update or delete the entry
1059 * The null flag is set--delete the entry
1062 SCSP_DELETE(dcsp->sd_server, csep);
1067 * Update the existing entry
1069 csep->sc_seq = csap->seq;
1070 csep->sc_oid = csap->oid;
1078 * Called as the result of a SIGHUP interrupt. Reread the
1079 * configuration file and solicit the cache from the server.
1095 * Log a message saying we're reconfiguring
1097 scsp_log(LOG_ERR, "Reconfiguring ...");
1100 * Re-read the configuration file
1102 rc = scsp_config(scsp_config_file);
1104 scsp_log(LOG_ERR, "Found %d error%s in configuration file",
1105 rc, ((rc == 1) ? "" : "s"));
1110 * If a connection to a server is open, get the cache from
1113 for (ssp = scsp_server_head; ssp; ssp = ssp->ss_next) {
1114 if (ssp->ss_sock != -1) {
1115 rc = scsp_send_cache_ind(ssp);