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/sys/netatm/ipatm/ipatm_vcm.c,v 1.4 1999/08/28 00:48:45 peter Exp $
27 * @(#) $DragonFly: src/sys/netproto/atm/ipatm/ipatm_vcm.c,v 1.6 2005/02/12 01:28:07 joerg Exp $
34 * Virtual Channel Manager
38 #include <netproto/atm/kern_include.h>
41 #include "ipatm_var.h"
42 #include "ipatm_serv.h"
44 Atm_attributes ipatm_aal5llc = {
120 T_ATM_NETWORK_CODING,
137 Atm_attributes ipatm_aal5null = {
139 CMAPI_CPCS, /* api */
141 sizeof(struct ifnet *), /* headin */
200 T_ATM_NETWORK_CODING,
217 Atm_attributes ipatm_aal4null = {
219 CMAPI_CPCS, /* api */
221 sizeof(struct ifnet *), /* headin */
280 T_ATM_NETWORK_CODING,
297 static struct t_atm_cause ipatm_cause = {
308 * This function will perform all actions necessary to activate a
309 * PVC for IP usage. In particular, it will allocate control blocks,
310 * open the PVC, initialize PVC stack, and initiate whatever ARP
311 * procedures are required.
314 * pvp pointer to PVC parameter structure
315 * sivp address to return pointer to IP PVC control block
318 * 0 PVC was successfully opened
319 * errno open failed - reason indicated
323 ipatm_openpvc(pvp, sivp)
324 struct ipatmpvc *pvp;
334 inp = pvp->ipp_ipnif;
338 * Make sure interface is ready to go
340 if (inp->inf_state != IPNIF_ACTIVE) {
346 * Validate fixed destination IP address
348 if (pvp->ipp_dst.sin_addr.s_addr != INADDR_ANY) {
349 if (in_broadcast(pvp->ipp_dst.sin_addr, &nip->nif_if) ||
350 IN_MULTICAST(ntohl(pvp->ipp_dst.sin_addr.s_addr)) ||
351 ipatm_chknif(pvp->ipp_dst.sin_addr, inp)) {
358 * Allocate IP VCC block
360 ivp = (struct ipvcc *)atm_allocate(&ipatm_vcpool);
369 ivp->iv_flags = IVF_PVC;
370 if (pvp->ipp_encaps == ATM_ENC_LLC)
371 ivp->iv_flags |= IVF_LLC;
374 * Fill out connection attributes
376 if (pvp->ipp_aal == ATM_AAL5) {
377 if (pvp->ipp_encaps == ATM_ENC_LLC)
380 ap = &ipatm_aal5null;
382 ap = &ipatm_aal4null;
386 ap->traffic.v.forward.PCR_all_traffic = nip->nif_pif->pif_pcr;
387 ap->traffic.v.backward.PCR_all_traffic = nip->nif_pif->pif_pcr;
388 ap->called.addr.address_format = T_ATM_PVC_ADDR;
389 ap->called.addr.address_length = sizeof(Atm_addr_pvc);
390 pvcp = (Atm_addr_pvc *)ap->called.addr.address;
391 ATM_PVC_SET_VPI(pvcp, pvp->ipp_vpi);
392 ATM_PVC_SET_VCI(pvcp, pvp->ipp_vci);
393 ap->called.subaddr.address_format = T_ATM_ABSENT;
394 ap->called.subaddr.address_length = 0;
399 err = atm_cm_connect(&ipatm_endpt, ivp, ap, &ivp->iv_conn);
401 atm_free((caddr_t)ivp);
406 * Save PVC information and link in VCC
408 /* ivp->iv_ = ap->headout; */
411 * Queue VCC onto its network interface
415 ENQUEUE(ivp, struct ipvcc, iv_elem, inp->inf_vcq);
420 * Set destination IP address and IPVCC state
422 if (pvp->ipp_dst.sin_addr.s_addr == INADDR_ANY) {
424 * Initiate ARP processing
426 switch ((*inp->inf_serv->is_arp_pvcopen)(ivp)) {
432 ivp->iv_state = IPVCC_ACTIVE;
437 * We've got our answer already
439 ivp->iv_state = IPVCC_ACTIVE;
440 ivp->iv_flags |= IVF_MAPOK;
441 ivp->iv_dst.s_addr = ivp->iv_arpent->am_dstip.s_addr;
448 ivp->iv_state = IPVCC_ACTPENT;
449 IPVCC_TIMER(ivp, 1 * ATM_HZ);
453 panic("ipatm_openpvc: invalid arp_pvcopen return");
458 * Use configured IP destination
460 ivp->iv_dst.s_addr = pvp->ipp_dst.sin_addr.s_addr;
461 ivp->iv_state = IPVCC_ACTIVE;
462 ivp->iv_flags |= IVF_MAPOK;
476 * This function will initiate the creation of an IP SVC. The IP VCC
477 * control block will be initialized and, if required, we will initiate
478 * ARP processing in order to resolve the destination's ATM address. Once
479 * the destination ATM address is known, ipatm_opensvc() will be called.
482 * ifp pointer to destination ifnet structure
483 * daf destination address family type
484 * dst pointer to destination address
485 * sivp address to return pointer to IP SVC control block
488 * 0 SVC creation was successfully initiated
489 * errno creation failed - reason indicated
493 ipatm_createsvc(ifp, daf, dst, sivp)
499 struct atm_nif *nip = (struct atm_nif *)ifp;
507 * Get IP interface and make sure its ready
509 for (inp = ipatm_nif_head; inp; inp = inp->inf_next) {
510 if (inp->inf_nif == nip)
517 if (inp->inf_state != IPNIF_ACTIVE) {
523 * Validate destination address
525 if (daf == AF_INET) {
527 * Destination is IP address
529 ip = (struct in_addr *)dst;
531 if (ip->s_addr == INADDR_ANY) {
535 } else if (daf == AF_ATM) {
537 * Destination is ATM address
539 atm = (Atm_addr *)dst;
541 if (atm->address_format == T_ATM_ABSENT) {
551 * Make sure we have services provider and ARP support
553 if ((inp->inf_serv == NULL) ||
554 (inp->inf_serv->is_arp_svcout == NULL)) {
562 ivp = (struct ipvcc *)atm_allocate(&ipatm_vcpool);
571 ivp->iv_flags = IVF_SVC;
575 * Get destination ATM address
577 if (daf == AF_INET) {
581 ivp->iv_dst.s_addr = ip->s_addr;
583 switch ((*inp->inf_serv->is_arp_svcout)(ivp, ip)) {
589 ivp->iv_state = IPVCC_PMAP;
590 IPVCC_TIMER(ivp, IPATM_ARP_TIME);
595 * We've got our answer already, so open SVC
597 ivp->iv_flags |= IVF_MAPOK;
598 err = ipatm_opensvc(ivp);
600 (*inp->inf_serv->is_arp_close)(ivp);
601 atm_free((caddr_t)ivp);
608 * So sorry...come again
610 atm_free((caddr_t)ivp);
615 panic("ipatm_createsvc: invalid arp_svcout return");
619 * We were given the ATM address, so open the SVC
621 * Create temporary arp map entry so that opensvc() works.
622 * Caller must set up a permanent entry immediately! (yuk)
626 ATM_ADDR_COPY(atm, &map.am_dstatm);
627 map.am_dstatmsub.address_format = T_ATM_ABSENT;
628 map.am_dstatmsub.address_length = 0;
629 ivp->iv_arpent = ↦
630 err = ipatm_opensvc(ivp);
632 atm_free((caddr_t)ivp);
635 ivp->iv_arpent = NULL;
639 * Queue VCC onto its network interface
643 ENQUEUE(ivp, struct ipvcc, iv_elem, inp->inf_vcq);
657 * This function will continue the IP SVC creation process. Here, we
658 * will issue an SVC open to the signalling manager and then wait for
659 * the final SVC setup results.
662 * ivp pointer to IP SVC to open
665 * 0 SVC open was successfully initiated
666 * errno open failed - reason indicated
673 struct ip_nif *inp = ivp->iv_ipnif;
678 * Cancel possible arp timeout
683 * Fill out connection attributes
686 if (inp->inf_serv->is_vccparm[i].ivc_aal == ATM_AAL5) {
687 if (inp->inf_serv->is_vccparm[i].ivc_encaps == ATM_ENC_LLC) {
689 ivp->iv_flags |= IVF_LLC;
691 ap = &ipatm_aal5null;
692 ivp->iv_flags &= ~IVF_LLC;
695 ap = &ipatm_aal4null;
696 ivp->iv_flags &= ~IVF_LLC;
699 ap->nif = inp->inf_nif;
700 ap->traffic.v.forward.PCR_all_traffic = inp->inf_nif->nif_pif->pif_pcr;
701 ap->traffic.v.backward.PCR_all_traffic = inp->inf_nif->nif_pif->pif_pcr;
703 ATM_ADDR_COPY(&ivp->iv_arpent->am_dstatm, &ap->called.addr);
704 ATM_ADDR_COPY(&ivp->iv_arpent->am_dstatmsub, &ap->called.subaddr);
709 err = atm_cm_connect(&ipatm_endpt, ivp, ap, &ivp->iv_conn);
714 * Call is progressing
716 /* ivp->iv_ = ap->headout; */
719 * Now we just wait for a CALL_CONNECTED event
721 ivp->iv_state = IPVCC_POPEN;
722 IPVCC_TIMER(ivp, IPATM_SVC_TIME);
728 * We've been hooked up with a shared VCC
730 /* ivp->iv_ = ap->headout; */
740 * Retry an IP SVC Open
742 * This function will attempt to retry a failed SVC open request. The IP
743 * interface service provider specifies a list of possible VCC parameters
744 * for IP to use. We will try each set of parameters in turn until either
745 * an open succeeds or we reach the end of the list.
748 * ivp pointer to IP SVC
751 * 0 SVC (re)open was successfully initiated
759 struct ip_nif *inp = ivp->iv_ipnif;
762 * If there isn't another set of vcc parameters to try, return
764 if ((++ivp->iv_parmx >= IPATM_VCCPARMS) ||
765 (inp->inf_serv->is_vccparm[ivp->iv_parmx].ivc_aal == 0))
769 * Okay, now initiate open with a new set of parameters
771 return (ipatm_opensvc(ivp));
776 * Finish IP SVC Activation
779 * ivp pointer to IP SVC
791 * Connection is now active
793 ivp->iv_state = IPVCC_ACTIVE;
797 * Tell ARP module that connection is active
799 if ((*ivp->iv_ipnif->inf_serv->is_arp_svcact)(ivp)) {
800 (void) ipatm_closevc(ivp, T_ATM_CAUSE_TEMPORARY_FAILURE);
805 * Send any queued packet
807 if ((ivp->iv_flags & IVF_MAPOK) && ivp->iv_queue) {
808 struct sockaddr_in sin;
811 sin.sin_family = AF_INET;
812 sin.sin_addr.s_addr = ivp->iv_dst.s_addr;
813 ifp = (struct ifnet *)ivp->iv_ipnif->inf_nif;
814 (void) ipatm_ifoutput(ifp, ivp->iv_queue,
815 (struct sockaddr *)&sin);
816 ivp->iv_queue = NULL;
822 * Process Incoming Calls
824 * This function will receive control when an incoming call has been matched
825 * to one of our registered listen parameter blocks. Assuming the call passes
826 * acceptance criteria and all required resources are available, we will
827 * create an IP SVC and notify the connection manager of our decision. We
828 * will then await notification of the final SVC setup results. If any
829 * problems are encountered, we will just tell the connection manager to
835 * tok owner's matched listening token
836 * cop pointer to incoming call's connection block
837 * ap pointer to incoming call's attributes
838 * tokp pointer to location to store our connection token
842 * errno call rejected - reason indicated
846 ipatm_incoming(tok, cop, ap, tokp)
852 struct atm_nif *nip = ap->nif;
854 struct ipvcc *ivp = NULL;
856 int usellc = 0, mtu = ATM_NIF_MTU;
859 * Get IP interface and make sure its ready
861 for (inp = ipatm_nif_head; inp; inp = inp->inf_next) {
862 if (inp->inf_nif == nip)
865 if ((inp == NULL) || (inp->inf_state != IPNIF_ACTIVE)) {
867 cause = T_ATM_CAUSE_SERVICE_OR_OPTION_UNAVAILABLE;
872 * Make sure we have services provider and ARP support
874 if ((inp->inf_serv == NULL) ||
875 (inp->inf_serv->is_arp_svcin == NULL)) {
877 cause = T_ATM_CAUSE_SERVICE_OR_OPTION_UNAVAILABLE;
882 * Check for LLC encapsulation
884 if ((ap->blli.tag_l2 == T_ATM_PRESENT) &&
885 (ap->blli.v.layer_2_protocol.ID_type == T_ATM_SIMPLE_ID) &&
886 (ap->blli.v.layer_2_protocol.ID.simple_ID == T_ATM_BLLI2_I8802)) {
888 mtu += IPATM_LLC_LEN;
892 * Verify requested MTU
894 if (ap->aal.type == ATM_AAL5) {
895 if ((ap->aal.v.aal5.forward_max_SDU_size > mtu) ||
896 (ap->aal.v.aal5.backward_max_SDU_size < mtu)) {
898 cause = T_ATM_CAUSE_AAL_PARAMETERS_NOT_SUPPORTED;
902 if ((ap->aal.v.aal4.forward_max_SDU_size > mtu) ||
903 (ap->aal.v.aal4.backward_max_SDU_size < mtu)) {
905 cause = T_ATM_CAUSE_AAL_PARAMETERS_NOT_SUPPORTED;
913 ivp = (struct ipvcc *)atm_allocate(&ipatm_vcpool);
916 cause = T_ATM_CAUSE_UNSPECIFIED_RESOURCE_UNAVAILABLE;
923 ivp->iv_flags = IVF_SVC;
926 ivp->iv_flags |= IVF_LLC;
929 * Lookup ARP entry for destination
931 switch ((*inp->inf_serv->is_arp_svcin)
932 (ivp, &ap->calling.addr, &ap->calling.subaddr)) {
936 * We'll be (hopefully) notified later
942 * We've got our answer already
944 ivp->iv_flags |= IVF_MAPOK;
945 ivp->iv_dst.s_addr = ivp->iv_arpent->am_dstip.s_addr;
950 * So sorry...come again
953 cause = T_ATM_CAUSE_SERVICE_OR_OPTION_UNAVAILABLE;
957 panic("ipatm_incoming: invalid arp_svcin return");
961 * Accept SVC connection
963 ivp->iv_state = IPVCC_PACCEPT;
966 * Save VCC information
970 /* ivp->iv_ = ap->headout; */
973 * Queue VCC onto its network interface
976 ENQUEUE(ivp, struct ipvcc, iv_elem, inp->inf_vcq);
979 * Wait for a CALL_CONNECTED event
981 IPVCC_TIMER(ivp, IPATM_SVC_TIME);
987 * Clean up after call failure
990 (*inp->inf_serv->is_arp_close)(ivp);
991 atm_free((caddr_t)ivp);
993 ap->cause.tag = T_ATM_PRESENT;
994 ap->cause.v = ipatm_cause;
995 ap->cause.v.cause_value = cause;
1003 * This function will close an IP VCC (PVC or SVC), including notifying
1004 * the signalling and ARP subsystems of the VCC's demise and cleaning
1005 * up memory after ourselves.
1008 * ivp pointer to VCC
1012 * 0 VCC successfully closed
1013 * errno close failed - reason indicated
1017 ipatm_closevc(ivp, code)
1021 struct ip_nif *inp = ivp->iv_ipnif;
1025 * Make sure VCC hasn't been through here already
1027 switch (ivp->iv_state) {
1034 * Reset lookup cache
1036 if (last_map_ipvcc == ivp) {
1037 last_map_ipvcc = NULL;
1042 * Tell ARP about SVCs and dynamic PVCs
1044 if (inp->inf_serv &&
1045 ((ivp->iv_flags & IVF_SVC) || inp->inf_serv->is_arp_pvcopen)) {
1046 (*inp->inf_serv->is_arp_close)(ivp);
1050 * Free queued packets
1053 KB_FREEALL(ivp->iv_queue);
1063 switch (ivp->iv_state) {
1072 ipatm_cause.cause_value = code;
1073 err = atm_cm_release(ivp->iv_conn, &ipatm_cause);
1076 "ipatm_closevc: release fail: err=%d\n", err);
1085 "ipatm_closevc: unknown state: ivp=%p, state=%d\n",
1086 ivp, ivp->iv_state);
1090 * Remove VCC from network i/f
1093 DEQUEUE(ivp, struct ipvcc, iv_elem, inp->inf_vcq);
1096 * Reset state just to be sure
1098 ivp->iv_state = IPVCC_FREE;
1101 * If ARP module is done with VCC too, then free it
1103 if (ivp->iv_arpconn == NULL)
1104 atm_free((caddr_t)ivp);
1113 * Check if IP address is valid on a Network Interface
1115 * Checks whether the supplied IP address is allowed to be assigned to
1116 * the supplied IP network interface.
1120 * inp pointer to IP network interface
1124 * 1 - not valid to assign
1128 ipatm_chknif(in, inp)
1132 struct in_ifaddr *ia;
1136 * Make sure there's an interface requested
1142 * Make sure we have an IP address
1144 i = ntohl(in.s_addr);
1149 * Make sure an interface address is set
1156 * Make sure we're on the right subnet
1158 if ((i & ia->ia_subnetmask) != ia->ia_subnet)
1166 * Map an IP Address to an IP VCC
1168 * Given a destination IP address, this function will return a pointer
1169 * to the appropriate output IP VCC to which to send the packet.
1170 * This is currently implemented using a one-behind cache containing the
1171 * last successful mapping result. If the cache lookup fails, then a
1172 * simple linear search of all IP VCCs on the destination network interface
1173 * is performed. This is obviously an area to look at for performance
1177 * dst pointer to destination IP address
1178 * nip pointer to destination network interface
1181 * addr pointer to located IP VCC
1182 * 0 no such mapping exists
1186 ipatm_iptovc(dst, nip)
1187 struct sockaddr_in *dst;
1188 struct atm_nif *nip;
1192 u_long dstip = dst->sin_addr.s_addr;
1196 * Look in cache first
1198 if (last_map_ipdst == dstip)
1199 return (last_map_ipvcc);
1202 * Oh well, we've got to search for it...first find the interface
1205 for (inp = ipatm_nif_head; inp; inp = inp->inf_next) {
1206 if (inp->inf_nif == nip)
1215 * Now home in on the VCC
1217 for (ivp = Q_HEAD(inp->inf_vcq, struct ipvcc); ivp;
1218 ivp = Q_NEXT(ivp, struct ipvcc, iv_elem)) {
1219 if (ivp->iv_dst.s_addr == dstip)
1224 * Update lookup cache
1227 last_map_ipdst = dstip;
1228 last_map_ipvcc = ivp;