// 2019 Rackspace, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"): you may // not use this file except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. package tsip import ( "fmt" "github.com/sirupsen/logrus" "github.rackspace.com/gorax/asaapi/pkg/asa" ) func (c *Client) lookupASA(r *RealIP) error { // Initialize the ASA found as the next-hop device device, err := asa.NewASA(*c.core.Token, r.Gateway.ID) if err != nil { logrus.Errorf("%s:lookupASA:NewASA:%s", r.MappedAddress, err) return errConnect } // Retrieve Interface configurations and populate the Config.Interfaces type if err := device.GetXlate(); err != nil { logrus.Errorf("%s:lookupASA:GetXlate:%s", r.MappedAddress, err) return errGather } // Range through the list of ManualNat configurations looking for the IP as the // translated address. for _, nat := range device.Status.Xlate.NAT { // Bypass any NAT statements that are identity NAT if nat.Flag.Identity && nat.Flag.Twice { continue } for _, ipNet := range nat.ToNetwork { if ipNet.Contains(r.Device.addrObj) { r.XlateType = "Static NAT" for _, srcNet := range nat.FromNetwork { r.RealAddress = append(r.RealAddress, srcNet.String()) } return nil } } } // Range through the list of AfterAutoNat configurations looking for the IP as the // translated address. for _, nat := range device.Status.Xlate.PAT { // If the translated network does not contain the // mapped IP passed in, continue. if nat.ToNetwork.String() != r.Device.addrObj.String() { continue } r.XlateType = fmt.Sprintf("%s PAT", nat.Type) // If we get here, we know that the NAT network contains the mapped IP address. // We need to now range through the origin networks to get a list of active // connections that are part of a dynamic NAT statement. if r.port == "" || r.port == nat.FromPort { if !existInSlice(nat.FromNetwork.String(), r.RealAddress) { r.RealAddress = append(r.RealAddress, nat.FromNetwork.String()) } } } // If, throughout our discovery, we found some real IPs, exit successfully if len(r.RealAddress) == 0 { r.XlateType = "" r.Details = errNoXlate.Error() r.RealAddress = []string{fmt.Sprintf("%s/32", r.MappedAddress)} } return nil }