Search in sources :

Example 6 with Prefix

use of org.batfish.datamodel.Prefix in project batfish by batfish.

the class VirtualRouter method computeBgpAdvertisementsToOutside.

int computeBgpAdvertisementsToOutside(Map<Ip, Set<String>> ipOwners) {
    int numAdvertisements = 0;
    // If we have no BGP process, nothing to do
    if (_vrf.getBgpProcess() == null) {
        return numAdvertisements;
    }
    for (BgpNeighbor neighbor : _vrf.getBgpProcess().getNeighbors().values()) {
        Ip localIp = neighbor.getLocalIp();
        Set<String> localIpOwners = ipOwners.get(localIp);
        String hostname = _c.getHostname();
        if (localIpOwners == null || !localIpOwners.contains(hostname)) {
            continue;
        }
        Prefix remotePrefix = neighbor.getPrefix();
        if (remotePrefix.getPrefixLength() != Prefix.MAX_PREFIX_LENGTH) {
            // Do not support dynamic outside neighbors
            continue;
        }
        Ip remoteIp = remotePrefix.getStartIp();
        if (ipOwners.get(remoteIp) != null) {
            // Skip if neighbor is not outside the network
            continue;
        }
        int localAs = neighbor.getLocalAs();
        int remoteAs = neighbor.getRemoteAs();
        String remoteHostname = remoteIp.toString();
        String remoteVrfName = Configuration.DEFAULT_VRF_NAME;
        RoutingPolicy exportPolicy = _c.getRoutingPolicies().get(neighbor.getExportPolicy());
        boolean ebgpSession = localAs != remoteAs;
        RoutingProtocol targetProtocol = ebgpSession ? RoutingProtocol.BGP : RoutingProtocol.IBGP;
        Set<AbstractRoute> candidateRoutes = Collections.newSetFromMap(new IdentityHashMap<>());
        // Add IGP routes
        Set<AbstractRoute> activeRoutes = Collections.newSetFromMap(new IdentityHashMap<>());
        activeRoutes.addAll(_mainRib.getRoutes());
        for (AbstractRoute candidateRoute : activeRoutes) {
            if (candidateRoute.getProtocol() != RoutingProtocol.BGP && candidateRoute.getProtocol() != RoutingProtocol.IBGP) {
                candidateRoutes.add(candidateRoute);
            }
        }
        /*
       * bgp advertise-external
       *
       * When this is set, add best eBGP path independently of whether
       * it is preempted by an iBGP or IGP route. Only applicable to
       * iBGP sessions.
       */
        boolean advertiseExternal = !ebgpSession && neighbor.getAdvertiseExternal();
        if (advertiseExternal) {
            candidateRoutes.addAll(_ebgpBestPathRib.getRoutes());
        }
        /*
       * bgp advertise-inactive
       *
       * When this is set, add best BGP path independently of whether
       * it is preempted by an IGP route. Only applicable to eBGP
       * sessions.
       */
        boolean advertiseInactive = ebgpSession && neighbor.getAdvertiseInactive();
        /* Add best bgp paths if they are active, or if advertise-inactive */
        for (AbstractRoute candidateRoute : _bgpBestPathRib.getRoutes()) {
            if (advertiseInactive || activeRoutes.contains(candidateRoute)) {
                candidateRoutes.add(candidateRoute);
            }
        }
        /* Add all bgp paths if additional-paths active for this session */
        boolean additionalPaths = !ebgpSession && neighbor.getAdditionalPathsSend() && neighbor.getAdditionalPathsSelectAll();
        if (additionalPaths) {
            candidateRoutes.addAll(_bgpMultipathRib.getRoutes());
        }
        for (AbstractRoute route : candidateRoutes) {
            BgpRoute.Builder transformedOutgoingRouteBuilder = new BgpRoute.Builder();
            RoutingProtocol routeProtocol = route.getProtocol();
            boolean routeIsBgp = routeProtocol == RoutingProtocol.IBGP || routeProtocol == RoutingProtocol.BGP;
            // originatorIP
            Ip originatorIp;
            if (!ebgpSession && routeProtocol.equals(RoutingProtocol.IBGP)) {
                BgpRoute bgpRoute = (BgpRoute) route;
                originatorIp = bgpRoute.getOriginatorIp();
            } else {
                originatorIp = _vrf.getBgpProcess().getRouterId();
            }
            transformedOutgoingRouteBuilder.setOriginatorIp(originatorIp);
            transformedOutgoingRouteBuilder.setReceivedFromIp(neighbor.getLocalIp());
            // for bgp remote route)
            if (routeIsBgp) {
                BgpRoute bgpRoute = (BgpRoute) route;
                transformedOutgoingRouteBuilder.setOriginType(bgpRoute.getOriginType());
                if (ebgpSession && bgpRoute.getAsPath().containsAs(neighbor.getRemoteAs()) && !neighbor.getAllowRemoteAsOut()) {
                    // disable-peer-as-check (getAllowRemoteAsOut) is set
                    continue;
                }
                /*
           * route reflection: reflect everything received from
           * clients to clients and non-clients. reflect everything
           * received from non-clients to clients. Do not reflect to
           * originator
           */
                Ip routeOriginatorIp = bgpRoute.getOriginatorIp();
                /*
           *  iBGP speaker should not send out routes to iBGP neighbor whose router-id is
           *  same as originator id of advertisement
           */
                if (!ebgpSession && routeOriginatorIp != null && remoteIp.equals(routeOriginatorIp)) {
                    continue;
                }
                if (routeProtocol.equals(RoutingProtocol.IBGP) && !ebgpSession) {
                    boolean routeReceivedFromRouteReflectorClient = bgpRoute.getReceivedFromRouteReflectorClient();
                    boolean sendingToRouteReflectorClient = neighbor.getRouteReflectorClient();
                    transformedOutgoingRouteBuilder.getClusterList().addAll(bgpRoute.getClusterList());
                    if (!routeReceivedFromRouteReflectorClient && !sendingToRouteReflectorClient) {
                        continue;
                    }
                    if (sendingToRouteReflectorClient) {
                        // sender adds its local cluster id to clusterlist of
                        // new route
                        transformedOutgoingRouteBuilder.getClusterList().add(neighbor.getClusterId());
                    }
                }
            }
            // Outgoing communities
            if (routeIsBgp) {
                BgpRoute bgpRoute = (BgpRoute) route;
                transformedOutgoingRouteBuilder.setAsPath(bgpRoute.getAsPath().getAsSets());
                if (neighbor.getSendCommunity()) {
                    transformedOutgoingRouteBuilder.getCommunities().addAll(bgpRoute.getCommunities());
                }
            }
            if (ebgpSession) {
                SortedSet<Integer> newAsPathElement = new TreeSet<>();
                newAsPathElement.add(localAs);
                transformedOutgoingRouteBuilder.getAsPath().add(0, newAsPathElement);
            }
            // Outgoing protocol
            transformedOutgoingRouteBuilder.setProtocol(targetProtocol);
            transformedOutgoingRouteBuilder.setNetwork(route.getNetwork());
            // Outgoing metric
            if (routeIsBgp) {
                transformedOutgoingRouteBuilder.setMetric(route.getMetric());
            }
            // Outgoing nextHopIp
            // Outgoing localPreference
            Ip nextHopIp;
            int localPreference;
            if (ebgpSession || !routeIsBgp) {
                nextHopIp = neighbor.getLocalIp();
                localPreference = BgpRoute.DEFAULT_LOCAL_PREFERENCE;
            } else {
                nextHopIp = route.getNextHopIp();
                BgpRoute ibgpRoute = (BgpRoute) route;
                localPreference = ibgpRoute.getLocalPreference();
            }
            if (nextHopIp.equals(Route.UNSET_ROUTE_NEXT_HOP_IP)) {
                // should only happen for ibgp
                String nextHopInterface = route.getNextHopInterface();
                InterfaceAddress nextHopAddress = _c.getInterfaces().get(nextHopInterface).getAddress();
                if (nextHopAddress == null) {
                    throw new BatfishException("route's nextHopInterface has no address");
                }
                nextHopIp = nextHopAddress.getIp();
            }
            transformedOutgoingRouteBuilder.setNextHopIp(nextHopIp);
            transformedOutgoingRouteBuilder.setLocalPreference(localPreference);
            // Outgoing srcProtocol
            transformedOutgoingRouteBuilder.setSrcProtocol(route.getProtocol());
            /*
         * CREATE OUTGOING ROUTE
         */
            boolean acceptOutgoing = exportPolicy.process(route, transformedOutgoingRouteBuilder, remoteIp, remoteVrfName, Direction.OUT);
            if (acceptOutgoing) {
                BgpRoute transformedOutgoingRoute = transformedOutgoingRouteBuilder.build();
                // Record sent advertisement
                BgpAdvertisementType sentType = ebgpSession ? BgpAdvertisementType.EBGP_SENT : BgpAdvertisementType.IBGP_SENT;
                Ip sentOriginatorIp = transformedOutgoingRoute.getOriginatorIp();
                SortedSet<Long> sentClusterList = transformedOutgoingRoute.getClusterList();
                AsPath sentAsPath = transformedOutgoingRoute.getAsPath();
                SortedSet<Long> sentCommunities = transformedOutgoingRoute.getCommunities();
                Prefix sentNetwork = route.getNetwork();
                Ip sentNextHopIp;
                String sentSrcNode = hostname;
                String sentSrcVrf = _vrf.getName();
                Ip sentSrcIp = neighbor.getLocalIp();
                String sentDstNode = remoteHostname;
                String sentDstVrf = remoteVrfName;
                Ip sentDstIp = remoteIp;
                int sentWeight = -1;
                if (ebgpSession) {
                    sentNextHopIp = nextHopIp;
                } else {
                    sentNextHopIp = transformedOutgoingRoute.getNextHopIp();
                }
                int sentLocalPreference = transformedOutgoingRoute.getLocalPreference();
                long sentMed = transformedOutgoingRoute.getMetric();
                OriginType sentOriginType = transformedOutgoingRoute.getOriginType();
                RoutingProtocol sentSrcProtocol = targetProtocol;
                BgpAdvertisement sentAdvert = new BgpAdvertisement(sentType, sentNetwork, sentNextHopIp, sentSrcNode, sentSrcVrf, sentSrcIp, sentDstNode, sentDstVrf, sentDstIp, sentSrcProtocol, sentOriginType, sentLocalPreference, sentMed, sentOriginatorIp, sentAsPath, sentCommunities, sentClusterList, sentWeight);
                _sentBgpAdvertisements.add(sentAdvert);
                numAdvertisements++;
            }
        }
    }
    return numAdvertisements;
}
Also used : RoutingProtocol(org.batfish.datamodel.RoutingProtocol) BgpAdvertisementType(org.batfish.datamodel.BgpAdvertisement.BgpAdvertisementType) Ip(org.batfish.datamodel.Ip) Prefix(org.batfish.datamodel.Prefix) BgpNeighbor(org.batfish.datamodel.BgpNeighbor) TreeSet(java.util.TreeSet) BgpRoute(org.batfish.datamodel.BgpRoute) AbstractRoute(org.batfish.datamodel.AbstractRoute) BatfishException(org.batfish.common.BatfishException) OriginType(org.batfish.datamodel.OriginType) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) AsPath(org.batfish.datamodel.AsPath) BgpAdvertisement(org.batfish.datamodel.BgpAdvertisement)

Example 7 with Prefix

use of org.batfish.datamodel.Prefix in project batfish by batfish.

the class VirtualRouter method initIntraAreaOspfRoutes.

/**
 * Initialize Intra-area OSPF routes from the interface prefixes
 */
private void initIntraAreaOspfRoutes() {
    OspfProcess proc = _vrf.getOspfProcess();
    if (proc == null) {
        // nothing to do
        return;
    }
    // init intra-area routes from connected routes
    // For each interface within an OSPF area and each interface prefix,
    // construct a new OSPF-IA route. Put it in the IA RIB.
    proc.getAreas().forEach((areaNum, area) -> {
        for (String ifaceName : area.getInterfaces()) {
            Interface iface = _c.getInterfaces().get(ifaceName);
            if (iface.getActive()) {
                Set<Prefix> allNetworkPrefixes = iface.getAllAddresses().stream().map(InterfaceAddress::getPrefix).collect(Collectors.toSet());
                int interfaceOspfCost = iface.getOspfCost();
                for (Prefix prefix : allNetworkPrefixes) {
                    long cost = interfaceOspfCost;
                    boolean stubNetwork = iface.getOspfPassive() || iface.getOspfPointToPoint();
                    if (stubNetwork) {
                        if (proc.getMaxMetricStubNetworks() != null) {
                            cost = proc.getMaxMetricStubNetworks();
                        }
                    } else if (proc.getMaxMetricTransitLinks() != null) {
                        cost = proc.getMaxMetricTransitLinks();
                    }
                    OspfIntraAreaRoute route = new OspfIntraAreaRoute(prefix, null, RoutingProtocol.OSPF.getDefaultAdministrativeCost(_c.getConfigurationFormat()), cost, areaNum);
                    _ospfIntraAreaRib.mergeRoute(route);
                }
            }
        }
    });
}
Also used : OspfIntraAreaRoute(org.batfish.datamodel.OspfIntraAreaRoute) OspfProcess(org.batfish.datamodel.OspfProcess) Prefix(org.batfish.datamodel.Prefix) Interface(org.batfish.datamodel.Interface)

Example 8 with Prefix

use of org.batfish.datamodel.Prefix in project batfish by batfish.

the class CiscoControlPlaneExtractor method exitRr_network.

@Override
public void exitRr_network(Rr_networkContext ctx) {
    Ip networkAddress = toIp(ctx.network);
    Ip mask = networkAddress.getClassMask();
    Prefix network = new Prefix(networkAddress, mask);
    _currentRipProcess.getNetworks().add(network);
}
Also used : Ip(org.batfish.datamodel.Ip) RoutePolicyNextHopIp(org.batfish.representation.cisco.RoutePolicyNextHopIp) Prefix(org.batfish.datamodel.Prefix)

Example 9 with Prefix

use of org.batfish.datamodel.Prefix in project batfish by batfish.

the class CiscoControlPlaneExtractor method exitRo_network.

@Override
public void exitRo_network(Ro_networkContext ctx) {
    Ip address;
    Ip wildcard;
    if (ctx.prefix != null) {
        Prefix prefix = Prefix.parse(ctx.prefix.getText());
        address = prefix.getStartIp();
        wildcard = prefix.getPrefixWildcard();
    } else {
        address = toIp(ctx.ip);
        wildcard = toIp(ctx.wildcard);
    }
    if (_configuration.getVendor() == ConfigurationFormat.CISCO_ASA) {
        wildcard = wildcard.inverted();
    }
    long area;
    if (ctx.area_int != null) {
        area = toLong(ctx.area_int);
    } else if (ctx.area_ip != null) {
        area = toIp(ctx.area_ip).asLong();
    } else {
        throw new BatfishException("bad area");
    }
    OspfWildcardNetwork network = new OspfWildcardNetwork(address, wildcard, area);
    _currentOspfProcess.getWildcardNetworks().add(network);
}
Also used : BatfishException(org.batfish.common.BatfishException) RedFlagBatfishException(org.batfish.common.RedFlagBatfishException) OspfWildcardNetwork(org.batfish.representation.cisco.OspfWildcardNetwork) Ip(org.batfish.datamodel.Ip) RoutePolicyNextHopIp(org.batfish.representation.cisco.RoutePolicyNextHopIp) Prefix(org.batfish.datamodel.Prefix)

Example 10 with Prefix

use of org.batfish.datamodel.Prefix in project batfish by batfish.

the class CiscoControlPlaneExtractor method exitNetwork_bgp_tail.

@Override
public void exitNetwork_bgp_tail(Network_bgp_tailContext ctx) {
    Prefix prefix;
    if (ctx.prefix != null) {
        prefix = Prefix.parse(ctx.prefix.getText());
    } else {
        Ip address = toIp(ctx.ip);
        Ip mask = (ctx.mask != null) ? toIp(ctx.mask) : address.getClassMask();
        int prefixLength = mask.numSubnetBits();
        prefix = new Prefix(address, prefixLength);
    }
    String map = null;
    Integer mapLine = null;
    if (ctx.mapname != null) {
        map = ctx.mapname.getText();
        mapLine = ctx.mapname.getStart().getLine();
    }
    BgpNetwork bgpNetwork = new BgpNetwork(prefix, map, mapLine);
    BgpProcess proc = currentVrf().getBgpProcess();
    proc.getIpNetworks().put(prefix, bgpNetwork);
}
Also used : BgpProcess(org.batfish.representation.cisco.BgpProcess) BgpNetwork(org.batfish.representation.cisco.BgpNetwork) Ip(org.batfish.datamodel.Ip) RoutePolicyNextHopIp(org.batfish.representation.cisco.RoutePolicyNextHopIp) Prefix(org.batfish.datamodel.Prefix)

Aggregations

Prefix (org.batfish.datamodel.Prefix)133 Ip (org.batfish.datamodel.Ip)53 Configuration (org.batfish.datamodel.Configuration)33 InterfaceAddress (org.batfish.datamodel.InterfaceAddress)29 Interface (org.batfish.datamodel.Interface)28 BatfishException (org.batfish.common.BatfishException)22 RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)20 SubRange (org.batfish.datamodel.SubRange)19 HashMap (java.util.HashMap)18 StaticRoute (org.batfish.datamodel.StaticRoute)18 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)17 BgpNeighbor (org.batfish.datamodel.BgpNeighbor)17 BgpProcess (org.batfish.datamodel.BgpProcess)17 SortedSet (java.util.SortedSet)16 TreeSet (java.util.TreeSet)16 AbstractRoute (org.batfish.datamodel.AbstractRoute)16 RoutingProtocol (org.batfish.datamodel.RoutingProtocol)16 TreeMap (java.util.TreeMap)14 HashSet (java.util.HashSet)13