Search in sources :

Example 96 with Prefix

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

the class Instance method toConfigurationNode.

public Configuration toConfigurationNode(AwsConfiguration awsVpcConfig, Region region, Warnings warnings) {
    String name = _tags.getOrDefault("Name", _instanceId);
    Configuration cfgNode = Utils.newAwsConfiguration(name, "aws");
    for (String interfaceId : _networkInterfaces) {
        NetworkInterface netInterface = region.getNetworkInterfaces().get(interfaceId);
        if (netInterface == null) {
            warnings.redFlag(String.format("Network interface \"%s\" for instance \"%s\" not found", interfaceId, _instanceId));
            continue;
        }
        ImmutableSortedSet.Builder<InterfaceAddress> ifaceAddressesBuilder = new ImmutableSortedSet.Builder<>(Comparator.naturalOrder());
        Subnet subnet = region.getSubnets().get(netInterface.getSubnetId());
        Prefix ifaceSubnet = subnet.getCidrBlock();
        Ip defaultGatewayAddress = subnet.computeInstancesIfaceIp();
        StaticRoute defaultRoute = StaticRoute.builder().setAdministrativeCost(Route.DEFAULT_STATIC_ROUTE_ADMIN).setMetric(Route.DEFAULT_STATIC_ROUTE_COST).setNextHopIp(defaultGatewayAddress).setNetwork(Prefix.ZERO).build();
        cfgNode.getDefaultVrf().getStaticRoutes().add(defaultRoute);
        for (Ip ip : netInterface.getIpAddressAssociations().keySet()) {
            if (!ifaceSubnet.containsIp(ip)) {
                warnings.pedantic(String.format("Instance subnet \"%s\" does not contain private ip: \"%s\"", ifaceSubnet, ip));
                continue;
            }
            if (ip.equals(ifaceSubnet.getEndIp())) {
                warnings.pedantic(String.format("Expected end address \"%s\" to be used by generated subnet node", ip));
                continue;
            }
            InterfaceAddress address = new InterfaceAddress(ip, ifaceSubnet.getPrefixLength());
            ifaceAddressesBuilder.add(address);
        }
        SortedSet<InterfaceAddress> ifaceAddresses = ifaceAddressesBuilder.build();
        Interface iface = Utils.newInterface(interfaceId, cfgNode, ifaceAddresses.first());
        iface.setAllAddresses(ifaceAddresses);
        cfgNode.getVendorFamily().getAws().setVpcId(_vpcId);
        cfgNode.getVendorFamily().getAws().setSubnetId(_subnetId);
        cfgNode.getVendorFamily().getAws().setRegion(region.getName());
    }
    Utils.processSecurityGroups(region, cfgNode, _securityGroups, warnings);
    return cfgNode;
}
Also used : StaticRoute(org.batfish.datamodel.StaticRoute) Configuration(org.batfish.datamodel.Configuration) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) Ip(org.batfish.datamodel.Ip) Prefix(org.batfish.datamodel.Prefix) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Interface(org.batfish.datamodel.Interface)

Example 97 with Prefix

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

the class AutoAs method evaluate.

@Override
public int evaluate(Environment environment) {
    BgpProcess proc = environment.getVrf().getBgpProcess();
    if (proc == null) {
        throw new BatfishException("Expected BGP process");
    }
    Direction direction = environment.getDirection();
    int as;
    Ip peerAddress = environment.getPeerAddress();
    if (peerAddress == null) {
        throw new BatfishException("Expected a peer address");
    }
    Prefix peerPrefix = new Prefix(peerAddress, Prefix.MAX_PREFIX_LENGTH);
    BgpNeighbor neighbor = proc.getNeighbors().get(peerPrefix);
    if (neighbor == null) {
        throw new BatfishException("Expected a peer with address: " + peerAddress);
    }
    if (direction == Direction.IN) {
        as = neighbor.getRemoteAs();
    } else if (direction == Direction.OUT) {
        as = neighbor.getLocalAs();
    } else {
        throw new BatfishException("Expected to be applied in a direction");
    }
    return as;
}
Also used : BgpNeighbor(org.batfish.datamodel.BgpNeighbor) BatfishException(org.batfish.common.BatfishException) BgpProcess(org.batfish.datamodel.BgpProcess) Ip(org.batfish.datamodel.Ip) Prefix(org.batfish.datamodel.Prefix) Direction(org.batfish.datamodel.routing_policy.Environment.Direction)

Example 98 with Prefix

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

the class IpPrefix method evaluate.

@Override
public Prefix evaluate(Environment env) {
    Ip ip = _ip.evaluate(env);
    int prefixLength = _prefixLength.evaluate(env);
    return new Prefix(ip, prefixLength);
}
Also used : Ip(org.batfish.datamodel.Ip) Prefix(org.batfish.datamodel.Prefix)

Example 99 with Prefix

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

the class OspfProcess method computeNetworks.

public void computeNetworks(Collection<Interface> interfaces) {
    for (Interface i : interfaces) {
        InterfaceAddress address = i.getAddress();
        if (address == null) {
            continue;
        }
        for (OspfWildcardNetwork wn : _wildcardNetworks) {
            // first we check if the interface ip address matches the ospf
            // network when the wildcard is ORed to both
            long wildcardLong = wn.getWildcard().asLong();
            long ospfNetworkLong = wn.getNetworkAddress().asLong();
            long intIpLong = address.getIp().asLong();
            long wildcardedOspfNetworkLong = ospfNetworkLong | wildcardLong;
            long wildcardedIntIpLong = intIpLong | wildcardLong;
            if (wildcardedOspfNetworkLong == wildcardedIntIpLong) {
                // since we have a match, we add the INTERFACE network, ignoring
                // the wildcard stuff from before
                Prefix newOspfNetwork = address.getPrefix();
                _networks.add(new OspfNetwork(newOspfNetwork, wn.getArea()));
                break;
            }
        }
    }
}
Also used : InterfaceAddress(org.batfish.datamodel.InterfaceAddress) Prefix(org.batfish.datamodel.Prefix)

Example 100 with Prefix

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

the class Encoder method buildCounterExample.

/*
   * Add the relevant variables in the counterexample to
   * display to the user in a human-readable fashion
   */
private void buildCounterExample(Encoder enc, Model m, SortedMap<String, String> model, SortedMap<String, String> packetModel, SortedSet<String> fwdModel, SortedMap<String, SortedMap<String, String>> envModel, SortedSet<String> failures) {
    SortedMap<Expr, String> valuation = new TreeMap<>();
    // If user asks for the full model
    for (Entry<String, Expr> entry : _allVariables.entrySet()) {
        String name = entry.getKey();
        Expr e = entry.getValue();
        Expr val = m.evaluate(e, true);
        if (!val.equals(e)) {
            String s = val.toString();
            if (_question.getFullModel()) {
                model.put(name, s);
            }
            valuation.put(e, s);
        }
    }
    // Packet model
    SymbolicPacket p = enc.getMainSlice().getSymbolicPacket();
    String dstIp = valuation.get(p.getDstIp());
    String srcIp = valuation.get(p.getSrcIp());
    String dstPt = valuation.get(p.getDstPort());
    String srcPt = valuation.get(p.getSrcPort());
    String icmpCode = valuation.get(p.getIcmpCode());
    String icmpType = valuation.get(p.getIcmpType());
    String ipProtocol = valuation.get(p.getIpProtocol());
    String tcpAck = valuation.get(p.getTcpAck());
    String tcpCwr = valuation.get(p.getTcpCwr());
    String tcpEce = valuation.get(p.getTcpEce());
    String tcpFin = valuation.get(p.getTcpFin());
    String tcpPsh = valuation.get(p.getTcpPsh());
    String tcpRst = valuation.get(p.getTcpRst());
    String tcpSyn = valuation.get(p.getTcpSyn());
    String tcpUrg = valuation.get(p.getTcpUrg());
    Ip dip = new Ip(Long.parseLong(dstIp));
    Ip sip = new Ip(Long.parseLong(srcIp));
    packetModel.put("dstIp", dip.toString());
    if (sip.asLong() != 0) {
        packetModel.put("srcIp", sip.toString());
    }
    if (dstPt != null && !dstPt.equals("0")) {
        packetModel.put("dstPort", dstPt);
    }
    if (srcPt != null && !srcPt.equals("0")) {
        packetModel.put("srcPort", srcPt);
    }
    if (icmpCode != null && !icmpCode.equals("0")) {
        packetModel.put("icmpCode", icmpCode);
    }
    if (icmpType != null && !icmpType.equals("0")) {
        packetModel.put("icmpType", icmpType);
    }
    if (ipProtocol != null && !ipProtocol.equals("0")) {
        Integer number = Integer.parseInt(ipProtocol);
        IpProtocol proto = IpProtocol.fromNumber(number);
        packetModel.put("protocol", proto.toString());
    }
    if ("true".equals(tcpAck)) {
        packetModel.put("tcpAck", "set");
    }
    if ("true".equals(tcpCwr)) {
        packetModel.put("tcpCwr", "set");
    }
    if ("true".equals(tcpEce)) {
        packetModel.put("tcpEce", "set");
    }
    if ("true".equals(tcpFin)) {
        packetModel.put("tcpFin", "set");
    }
    if ("true".equals(tcpPsh)) {
        packetModel.put("tcpPsh", "set");
    }
    if ("true".equals(tcpRst)) {
        packetModel.put("tcpRst", "set");
    }
    if ("true".equals(tcpSyn)) {
        packetModel.put("tcpSyn", "set");
    }
    if ("true".equals(tcpUrg)) {
        packetModel.put("tcpUrg", "set");
    }
    for (EncoderSlice slice : enc.getSlices().values()) {
        for (Entry<LogicalEdge, SymbolicRoute> entry2 : slice.getLogicalGraph().getEnvironmentVars().entrySet()) {
            LogicalEdge lge = entry2.getKey();
            SymbolicRoute r = entry2.getValue();
            if ("true".equals(valuation.get(r.getPermitted()))) {
                SortedMap<String, String> recordMap = new TreeMap<>();
                GraphEdge ge = lge.getEdge();
                String nodeIface = ge.getRouter() + "," + ge.getStart().getName() + " (BGP)";
                envModel.put(nodeIface, recordMap);
                if (r.getPrefixLength() != null) {
                    String x = valuation.get(r.getPrefixLength());
                    if (x != null) {
                        int len = Integer.parseInt(x);
                        Prefix p1 = new Prefix(dip, len);
                        recordMap.put("prefix", p1.toString());
                    }
                }
                if (r.getAdminDist() != null) {
                    String x = valuation.get(r.getAdminDist());
                    if (x != null) {
                        recordMap.put("admin distance", x);
                    }
                }
                if (r.getLocalPref() != null) {
                    String x = valuation.get(r.getLocalPref());
                    if (x != null) {
                        recordMap.put("local preference", x);
                    }
                }
                if (r.getMetric() != null) {
                    String x = valuation.get(r.getMetric());
                    if (x != null) {
                        recordMap.put("protocol metric", x);
                    }
                }
                if (r.getMed() != null) {
                    String x = valuation.get(r.getMed());
                    if (x != null) {
                        recordMap.put("multi-exit disc.", valuation.get(r.getMed()));
                    }
                }
                if (r.getOspfArea() != null && r.getOspfArea().getBitVec() != null) {
                    String x = valuation.get(r.getOspfArea().getBitVec());
                    if (x != null) {
                        Integer i = Integer.parseInt(x);
                        Long area = r.getOspfArea().value(i);
                        recordMap.put("OSPF Area", area.toString());
                    }
                }
                if (r.getOspfType() != null && r.getOspfType().getBitVec() != null) {
                    String x = valuation.get(r.getOspfType().getBitVec());
                    if (x != null) {
                        Integer i = Integer.parseInt(x);
                        OspfType type = r.getOspfType().value(i);
                        recordMap.put("OSPF Type", type.toString());
                    }
                }
                for (Entry<CommunityVar, BoolExpr> entry3 : r.getCommunities().entrySet()) {
                    CommunityVar cvar = entry3.getKey();
                    BoolExpr e = entry3.getValue();
                    String c = valuation.get(e);
                    // TODO: what about OTHER type?
                    if ("true".equals(c) && displayCommunity(cvar)) {
                        String s = cvar.getValue();
                        String t = slice.getNamedCommunities().get(cvar.getValue());
                        s = (t == null ? s : t);
                        recordMap.put("community " + s, "");
                    }
                }
            }
        }
    }
    // Forwarding Model
    enc.getMainSlice().getSymbolicDecisions().getDataForwarding().forEach((router, edge, e) -> {
        String s = valuation.get(e);
        if ("true".equals(s)) {
            SymbolicRoute r = enc.getMainSlice().getSymbolicDecisions().getBestNeighbor().get(router);
            if (r.getProtocolHistory() != null) {
                Protocol proto;
                List<Protocol> allProtocols = enc.getMainSlice().getProtocols().get(router);
                if (allProtocols.size() == 1) {
                    proto = allProtocols.get(0);
                } else {
                    s = valuation.get(r.getProtocolHistory().getBitVec());
                    int i = Integer.parseInt(s);
                    proto = r.getProtocolHistory().value(i);
                }
                fwdModel.add(edge + " (" + proto.name() + ")");
            } else {
                fwdModel.add(edge.toString());
            }
        }
    });
    _symbolicFailures.getFailedInternalLinks().forEach((x, y, e) -> {
        String s = valuation.get(e);
        if ("1".equals(s)) {
            String pair = (x.compareTo(y) < 0 ? x + "," + y : y + "," + x);
            failures.add("link(" + pair + ")");
        }
    });
    _symbolicFailures.getFailedEdgeLinks().forEach((ge, e) -> {
        String s = valuation.get(e);
        if ("1".equals(s)) {
            failures.add("link(" + ge.getRouter() + "," + ge.getStart().getName() + ")");
        }
    });
}
Also used : BoolExpr(com.microsoft.z3.BoolExpr) Ip(org.batfish.datamodel.Ip) Prefix(org.batfish.datamodel.Prefix) TreeMap(java.util.TreeMap) CommunityVar(org.batfish.symbolic.CommunityVar) BoolExpr(com.microsoft.z3.BoolExpr) ArithExpr(com.microsoft.z3.ArithExpr) BitVecExpr(com.microsoft.z3.BitVecExpr) Expr(com.microsoft.z3.Expr) IpProtocol(org.batfish.datamodel.IpProtocol) OspfType(org.batfish.symbolic.OspfType) IpProtocol(org.batfish.datamodel.IpProtocol) Protocol(org.batfish.symbolic.Protocol) GraphEdge(org.batfish.symbolic.GraphEdge)

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