Search in sources :

Example 11 with RoutingPolicy

use of org.batfish.datamodel.routing_policy.RoutingPolicy in project batfish by batfish.

the class Graph method getOriginatedNetworks.

/*
   * Collects and returns all originated prefixes for the given
   * router as well as the protocol. Static routes and connected
   * routes are treated as originating the prefix.
   */
public static Set<Prefix> getOriginatedNetworks(Configuration conf, Protocol proto) {
    Set<Prefix> acc = new HashSet<>();
    if (proto.isOspf()) {
        OspfProcess ospf = conf.getDefaultVrf().getOspfProcess();
        for (OspfArea area : ospf.getAreas().values()) {
            for (String ifaceName : area.getInterfaces()) {
                Interface iface = conf.getInterfaces().get(ifaceName);
                if (iface.getActive() && iface.getOspfEnabled()) {
                    acc.add(iface.getAddress().getPrefix());
                }
            }
        }
        return acc;
    }
    if (proto.isBgp()) {
        RoutingPolicy defaultPol = findCommonRoutingPolicy(conf, Protocol.BGP);
        if (defaultPol != null) {
            AstVisitor v = new AstVisitor();
            v.visit(conf, defaultPol.getStatements(), stmt -> {
            }, expr -> {
                if (expr instanceof Conjunction) {
                    Conjunction c = (Conjunction) expr;
                    if (c.getConjuncts().size() >= 2) {
                        BooleanExpr be1 = c.getConjuncts().get(0);
                        BooleanExpr be2 = c.getConjuncts().get(1);
                        if (be1 instanceof MatchPrefixSet && be2 instanceof Not) {
                            MatchPrefixSet mps = (MatchPrefixSet) be1;
                            Not n = (Not) be2;
                            if (n.getExpr() instanceof MatchProtocol) {
                                MatchProtocol mp = (MatchProtocol) n.getExpr();
                                if (mp.getProtocol() == RoutingProtocol.BGP) {
                                    PrefixSetExpr e = mps.getPrefixSet();
                                    if (e instanceof ExplicitPrefixSet) {
                                        ExplicitPrefixSet eps = (ExplicitPrefixSet) e;
                                        Set<PrefixRange> ranges = eps.getPrefixSpace().getPrefixRanges();
                                        for (PrefixRange r : ranges) {
                                            acc.add(r.getPrefix());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }
        return acc;
    }
    if (proto.isConnected()) {
        for (Interface iface : conf.getInterfaces().values()) {
            InterfaceAddress address = iface.getAddress();
            if (address != null) {
                acc.add(address.getPrefix());
            }
        }
        return acc;
    }
    if (proto.isStatic()) {
        for (StaticRoute sr : conf.getDefaultVrf().getStaticRoutes()) {
            if (sr.getNetwork() != null) {
                acc.add(sr.getNetwork());
            }
        }
        return acc;
    }
    throw new BatfishException("ERROR: getOriginatedNetworks: " + proto.name());
}
Also used : BatfishException(org.batfish.common.BatfishException) PrefixRange(org.batfish.datamodel.PrefixRange) StaticRoute(org.batfish.datamodel.StaticRoute) OspfArea(org.batfish.datamodel.OspfArea) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) MatchPrefixSet(org.batfish.datamodel.routing_policy.expr.MatchPrefixSet) PrefixSetExpr(org.batfish.datamodel.routing_policy.expr.PrefixSetExpr) OspfProcess(org.batfish.datamodel.OspfProcess) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) Prefix(org.batfish.datamodel.Prefix) MatchProtocol(org.batfish.datamodel.routing_policy.expr.MatchProtocol) Not(org.batfish.datamodel.routing_policy.expr.Not) ExplicitPrefixSet(org.batfish.datamodel.routing_policy.expr.ExplicitPrefixSet) Conjunction(org.batfish.datamodel.routing_policy.expr.Conjunction) Interface(org.batfish.datamodel.Interface) BooleanExpr(org.batfish.datamodel.routing_policy.expr.BooleanExpr) HashSet(java.util.HashSet)

Example 12 with RoutingPolicy

use of org.batfish.datamodel.routing_policy.RoutingPolicy in project batfish by batfish.

the class Graph method findAllCommunities.

public Set<CommunityVar> findAllCommunities(String router) {
    Set<CommunityVar> comms = new HashSet<>();
    Configuration conf = getConfigurations().get(router);
    for (RoutingPolicy pol : conf.getRoutingPolicies().values()) {
        AstVisitor v = new AstVisitor();
        v.visit(conf, pol.getStatements(), stmt -> {
            if (stmt instanceof SetCommunity) {
                SetCommunity sc = (SetCommunity) stmt;
                comms.addAll(findAllCommunities(conf, sc.getExpr()));
            }
            if (stmt instanceof AddCommunity) {
                AddCommunity ac = (AddCommunity) stmt;
                comms.addAll(findAllCommunities(conf, ac.getExpr()));
            }
            if (stmt instanceof DeleteCommunity) {
                DeleteCommunity dc = (DeleteCommunity) stmt;
                comms.addAll(findAllCommunities(conf, dc.getExpr()));
            }
            if (stmt instanceof RetainCommunity) {
                RetainCommunity rc = (RetainCommunity) stmt;
                comms.addAll(findAllCommunities(conf, rc.getExpr()));
            }
        }, expr -> {
            if (expr instanceof MatchCommunitySet) {
                MatchCommunitySet m = (MatchCommunitySet) expr;
                CommunitySetExpr ce = m.getExpr();
                comms.addAll(findAllCommunities(conf, ce));
            }
        });
    }
    return comms;
}
Also used : AddCommunity(org.batfish.datamodel.routing_policy.statement.AddCommunity) Configuration(org.batfish.datamodel.Configuration) SetCommunity(org.batfish.datamodel.routing_policy.statement.SetCommunity) DeleteCommunity(org.batfish.datamodel.routing_policy.statement.DeleteCommunity) MatchCommunitySet(org.batfish.datamodel.routing_policy.expr.MatchCommunitySet) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) RetainCommunity(org.batfish.datamodel.routing_policy.statement.RetainCommunity) CommunitySetExpr(org.batfish.datamodel.routing_policy.expr.CommunitySetExpr) HashSet(java.util.HashSet)

Example 13 with RoutingPolicy

use of org.batfish.datamodel.routing_policy.RoutingPolicy in project batfish by batfish.

the class BatfishCompressor method applyFilters.

/**
 * Update RoutingPolicies to filter traffic according to filtersByRouter. This mutates the
 * _graph's configurations.
 *
 * @param filtersByRouter Filters for each router/graph edge.
 * @return A new network with the updated configs.
 */
private Map<String, Configuration> applyFilters(Table2<String, GraphEdge, EquivalenceClassFilter> filtersByRouter) {
    Map<String, Configuration> newConfigs = new HashMap<>();
    for (Entry<String, Configuration> entry : _graph.getConfigurations().entrySet()) {
        String router = entry.getKey();
        Map<GraphEdge, EquivalenceClassFilter> filters = filtersByRouter.get(router);
        if (filters != null) {
            Configuration config = entry.getValue();
            // Include this config in the compressed network.
            newConfigs.put(router, config);
            // Mutate the config by adding import/export filters
            for (GraphEdge ge : _graph.getEdgeMap().get(router)) {
                EquivalenceClassFilter tup = filters.get(ge);
                RoutingPolicy ipol = _graph.findImportRoutingPolicy(router, Protocol.BGP, ge);
                if (ipol != null) {
                    RoutingPolicy newIpol = new RoutingPolicy(ipol.getName(), config);
                    newIpol.setStatements(applyFilters(ipol.getStatements(), tup));
                    config.getRoutingPolicies().put(newIpol.getName(), newIpol);
                }
                RoutingPolicy epol = _graph.findExportRoutingPolicy(router, Protocol.BGP, ge);
                if (epol != null) {
                    RoutingPolicy newEpol = new RoutingPolicy(epol.getName(), config);
                    newEpol.setStatements(applyFilters(epol.getStatements(), tup));
                    config.getRoutingPolicies().put(newEpol.getName(), newEpol);
                }
            }
        }
    }
    return newConfigs;
}
Also used : Configuration(org.batfish.datamodel.Configuration) HashMap(java.util.HashMap) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) GraphEdge(org.batfish.symbolic.GraphEdge)

Example 14 with RoutingPolicy

use of org.batfish.datamodel.routing_policy.RoutingPolicy in project batfish by batfish.

the class JuniperConfiguration method toInterface.

private org.batfish.datamodel.Interface toInterface(Interface iface) {
    String name = iface.getName();
    org.batfish.datamodel.Interface newIface = new org.batfish.datamodel.Interface(name, _c);
    newIface.setDeclaredNames(ImmutableSortedSet.of(name));
    Integer mtu = iface.getMtu();
    if (mtu != null) {
        newIface.setMtu(mtu);
    }
    newIface.setVrrpGroups(iface.getVrrpGroups());
    newIface.setVrf(_c.getVrfs().get(iface.getRoutingInstance()));
    Zone zone = _interfaceZones.get(iface);
    if (zone != null) {
        String zoneName = zone.getName();
        // filter for interface in zone
        FirewallFilter zoneInboundInterfaceFilter = zone.getInboundInterfaceFilters().get(iface);
        if (zoneInboundInterfaceFilter != null) {
            String zoneInboundInterfaceFilterName = zoneInboundInterfaceFilter.getName();
            zoneInboundInterfaceFilter.getReferers().put(iface, "Interface: '" + iface.getName() + "' refers to inbound filter for interface in zone : '" + zoneName + "'");
            IpAccessList zoneInboundInterfaceFilterList = _c.getIpAccessLists().get(zoneInboundInterfaceFilterName);
            newIface.setInboundFilter(zoneInboundInterfaceFilterList);
        } else {
            // filter for zone
            FirewallFilter zoneInboundFilter = zone.getInboundFilter();
            String zoneInboundFilterName = zoneInboundFilter.getName();
            zoneInboundFilter.getReferers().put(iface, "Interface: '" + iface.getName() + "' refers to inbound filter for zone : '" + zoneName + "'");
            IpAccessList zoneInboundFilterList = _c.getIpAccessLists().get(zoneInboundFilterName);
            newIface.setInboundFilter(zoneInboundFilterList);
        }
    }
    String inAclName = iface.getIncomingFilter();
    if (inAclName != null) {
        int inAclLine = iface.getIncomingFilterLine();
        IpAccessList inAcl = _c.getIpAccessLists().get(inAclName);
        if (inAcl == null) {
            undefined(JuniperStructureType.FIREWALL_FILTER, inAclName, JuniperStructureUsage.INTERFACE_INCOMING_FILTER, inAclLine);
        } else {
            FirewallFilter inFilter = _filters.get(inAclName);
            inFilter.getReferers().put(iface, "Incoming ACL for interface: " + iface.getName());
            newIface.setIncomingFilter(inAcl);
            if (inFilter.getRoutingPolicy()) {
                RoutingPolicy routingPolicy = _c.getRoutingPolicies().get(inAclName);
                if (routingPolicy != null) {
                    newIface.setRoutingPolicy(inAclName);
                } else {
                    throw new BatfishException("Expected interface routing-policy to exist");
                }
            }
        }
    }
    String outAclName = iface.getOutgoingFilter();
    if (outAclName != null) {
        int outAclLine = iface.getOutgoingFilterLine();
        IpAccessList outAcl = _c.getIpAccessLists().get(outAclName);
        if (outAcl == null) {
            undefined(JuniperStructureType.FIREWALL_FILTER, outAclName, JuniperStructureUsage.INTERFACE_OUTGOING_FILTER, outAclLine);
        } else {
            _filters.get(outAclName).getReferers().put(iface, "Outgoing ACL for interface: " + iface.getName());
            newIface.setOutgoingFilter(outAcl);
        }
    }
    if (iface.getPrimaryAddress() != null) {
        newIface.setAddress(iface.getPrimaryAddress());
    }
    newIface.setAllAddresses(iface.getAllAddresses());
    newIface.setActive(iface.getActive());
    newIface.setAccessVlan(iface.getAccessVlan());
    newIface.setNativeVlan(iface.getNativeVlan());
    newIface.setSwitchportMode(iface.getSwitchportMode());
    SwitchportEncapsulationType swe = iface.getSwitchportTrunkEncapsulation();
    if (swe == null) {
        swe = SwitchportEncapsulationType.DOT1Q;
    }
    newIface.setSwitchportTrunkEncapsulation(swe);
    newIface.setBandwidth(iface.getBandwidth());
    // isis settings
    IsisInterfaceSettings isisSettings = iface.getIsisSettings();
    IsisInterfaceLevelSettings isisL1Settings = isisSettings.getLevel1Settings();
    newIface.setIsisL1InterfaceMode(IsisInterfaceMode.UNSET);
    if (isisL1Settings.getEnabled()) {
        if (isisSettings.getPassive()) {
            newIface.setIsisL1InterfaceMode(IsisInterfaceMode.PASSIVE);
        } else if (isisSettings.getEnabled()) {
            newIface.setIsisL1InterfaceMode(IsisInterfaceMode.ACTIVE);
        }
    }
    IsisInterfaceLevelSettings isisL2Settings = isisSettings.getLevel2Settings();
    newIface.setIsisL2InterfaceMode(IsisInterfaceMode.UNSET);
    if (isisL2Settings.getEnabled()) {
        if (isisSettings.getPassive()) {
            newIface.setIsisL2InterfaceMode(IsisInterfaceMode.PASSIVE);
        } else if (isisSettings.getEnabled()) {
            newIface.setIsisL2InterfaceMode(IsisInterfaceMode.ACTIVE);
        }
    }
    Integer l1Metric = isisSettings.getLevel1Settings().getMetric();
    Integer l2Metric = isisSettings.getLevel2Settings().getMetric();
    if (l1Metric != null && l2Metric != null && (l1Metric.intValue() != l2Metric.intValue())) {
        _w.unimplemented("distinct metrics for is-is level1 and level2 on an interface");
    } else if (l1Metric != null) {
        newIface.setIsisCost(l1Metric);
    } else if (l2Metric != null) {
        newIface.setIsisCost(l2Metric);
    }
    // TODO: enable/disable individual levels
    return newIface;
}
Also used : BatfishException(org.batfish.common.BatfishException) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) IpAccessList(org.batfish.datamodel.IpAccessList) SwitchportEncapsulationType(org.batfish.datamodel.SwitchportEncapsulationType)

Example 15 with RoutingPolicy

use of org.batfish.datamodel.routing_policy.RoutingPolicy in project batfish by batfish.

the class JuniperConfiguration method toAggregateRoute.

private org.batfish.datamodel.GeneratedRoute toAggregateRoute(AggregateRoute route) {
    Prefix prefix = route.getPrefix();
    int prefixLength = prefix.getPrefixLength();
    int administrativeCost = route.getMetric();
    String policyNameSuffix = route.getPrefix().toString().replace('/', '_').replace('.', '_');
    String policyName = "~AGGREGATE_" + policyNameSuffix + "~";
    RoutingPolicy routingPolicy = new RoutingPolicy(policyName, _c);
    If routingPolicyConditional = new If();
    routingPolicy.getStatements().add(routingPolicyConditional);
    routingPolicyConditional.getTrueStatements().add(Statements.ExitAccept.toStaticStatement());
    routingPolicyConditional.getFalseStatements().add(Statements.ExitReject.toStaticStatement());
    String rflName = "~AGGREGATE_" + policyNameSuffix + "_RF~";
    MatchPrefixSet isContributingRoute = new MatchPrefixSet(new DestinationNetwork(), new NamedPrefixSet(rflName));
    routingPolicyConditional.setGuard(isContributingRoute);
    RouteFilterList rfList = new RouteFilterList(rflName);
    rfList.addLine(new org.batfish.datamodel.RouteFilterLine(LineAction.ACCEPT, prefix, new SubRange(prefixLength + 1, Prefix.MAX_PREFIX_LENGTH)));
    org.batfish.datamodel.GeneratedRoute.Builder newRoute = new org.batfish.datamodel.GeneratedRoute.Builder();
    newRoute.setNetwork(prefix);
    newRoute.setAdmin(administrativeCost);
    newRoute.setDiscard(true);
    newRoute.setGenerationPolicy(policyName);
    _c.getRoutingPolicies().put(policyName, routingPolicy);
    _c.getRouteFilterLists().put(rflName, rfList);
    return newRoute.build();
}
Also used : NamedPrefixSet(org.batfish.datamodel.routing_policy.expr.NamedPrefixSet) MatchPrefixSet(org.batfish.datamodel.routing_policy.expr.MatchPrefixSet) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) Prefix(org.batfish.datamodel.Prefix) DestinationNetwork(org.batfish.datamodel.routing_policy.expr.DestinationNetwork) RouteFilterList(org.batfish.datamodel.RouteFilterList) SubRange(org.batfish.datamodel.SubRange) If(org.batfish.datamodel.routing_policy.statement.If)

Aggregations

RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)50 If (org.batfish.datamodel.routing_policy.statement.If)17 Prefix (org.batfish.datamodel.Prefix)16 BatfishException (org.batfish.common.BatfishException)12 Ip (org.batfish.datamodel.Ip)12 Conjunction (org.batfish.datamodel.routing_policy.expr.Conjunction)12 Statement (org.batfish.datamodel.routing_policy.statement.Statement)12 Configuration (org.batfish.datamodel.Configuration)11 BooleanExpr (org.batfish.datamodel.routing_policy.expr.BooleanExpr)11 MatchProtocol (org.batfish.datamodel.routing_policy.expr.MatchProtocol)11 InterfaceAddress (org.batfish.datamodel.InterfaceAddress)10 CallExpr (org.batfish.datamodel.routing_policy.expr.CallExpr)10 Disjunction (org.batfish.datamodel.routing_policy.expr.Disjunction)10 ArrayList (java.util.ArrayList)9 MatchPrefixSet (org.batfish.datamodel.routing_policy.expr.MatchPrefixSet)9 HashSet (java.util.HashSet)7 RouteFilterList (org.batfish.datamodel.RouteFilterList)7 SubRange (org.batfish.datamodel.SubRange)7 Not (org.batfish.datamodel.routing_policy.expr.Not)7 HashMap (java.util.HashMap)6