public void computeNetworks(Collection<Interface> interfaces) { for (Interface i : interfaces) { InterfaceAddress address = i.getAddress(); if (address == null) { continue; } for (IpWildcard wn : _wildcardNetworks) { // Check if the interface IP address matches the eigrp network // when the wildcard is ORed to both long wildcardLong = wn.getWildcard().asLong(); long eigrpNetworkLong = wn.getIp().asLong(); long intIpLong = address.getIp().asLong(); long wildcardedNetworkLong = eigrpNetworkLong | wildcardLong; long wildcardedIntIpLong = intIpLong | wildcardLong; if (wildcardedNetworkLong == wildcardedIntIpLong) { // since we have a match, we add the INTERFACE network, ignoring // the wildcard stuff from before _networks.add(address.getPrefix()); break; } } } }
@Override public void exitSniel3_ip(Sniel3_ipContext ctx) { InterfaceAddress address = new InterfaceAddress(ctx.address.getText()); _currentInterface.setAddress(address); _currentInterface.getAllAddresses().add(address); }
public LocalRoute(InterfaceAddress interfaceAddress, String nextHopInterface) { this( Prefix.create(interfaceAddress.getIp(), Prefix.MAX_PREFIX_LENGTH), nextHopInterface, interfaceAddress.getNetworkBits(), 0); }
@Override public void exitIfiav_virtual_address(Ifiav_virtual_addressContext ctx) { Ip virtualAddress = Ip.parse(ctx.IP_ADDRESS().getText()); int prefixLength = _currentInterfaceAddress.getNetworkBits(); _currentVrrpGroup.setVirtualAddress(new InterfaceAddress(virtualAddress, prefixLength)); }
@Override public IpSpace visitInterfaceLinkLocation(InterfaceLinkLocation interfaceLinkLocation) { String node = interfaceLinkLocation.getNodeName(); String iface = interfaceLinkLocation.getInterfaceName(); @Nullable IpSpace linkIpSpace = AclIpSpace.union( interfaceAddresses(node, iface).stream() /* * Only include addresses on networks that might have hosts. */ .filter(address -> address.getNetworkBits() <= HOST_SUBNET_MAX_PREFIX_LENGTH) .map(address -> address.getPrefix().toHostIpSpace()) .collect(Collectors.toList())); return linkIpSpace == null ? EmptyIpSpace.INSTANCE : AclIpSpace.difference(linkIpSpace, _specifierContext.getSnapshotDeviceOwnedIps()); }
/** * Computes a mapping of primary {@link Ip}s to the names of interfaces owning them. Filters out * the interfaces having no primary {@link InterfaceAddress} */ private static Map<Ip, String> computeIpToIfaceNameMap(Map<String, Interface> interfaces) { Map<Ip, String> ipToIfaceNameMap = new HashMap<>(); for (Entry<String, Interface> interfaceNameToInterface : interfaces.entrySet()) { interfaceNameToInterface .getValue() .getAllAddresses() .forEach( interfaceAddress -> { ipToIfaceNameMap.put(interfaceAddress.getIp(), interfaceNameToInterface.getKey()); }); } return ipToIfaceNameMap; }
@Override public void exitNatp_address(Natp_addressContext ctx) { if (ctx.TO() != null) { if (ctx.IP_ADDRESS().isEmpty()) { // from IP_PREFIX to IP_PREFIX // Juniper will treat IP_PREFIX as IP ADDRESS _currentNatPool.setFromAddress(new InterfaceAddress(ctx.from.getText()).getIp()); _currentNatPool.setToAddress(new InterfaceAddress(ctx.to.getText()).getIp()); } else { // from IP_ADDRESS to IP_ADDRESS _currentNatPool.setFromAddress(Ip.parse(ctx.from.getText())); _currentNatPool.setToAddress(Ip.parse(ctx.to.getText())); } } else { Prefix prefix = Prefix.parse(ctx.prefix.getText()); _currentNatPool.setFromAddress(prefix.getFirstHostIp()); _currentNatPool.setToAddress(prefix.getLastHostIp()); } }
if (address.getNetworkBits() < Prefix.MAX_PREFIX_LENGTH) { Prefix prefix = address.getPrefix(); List<Interface> interfaceBucket = prefixInterfaces.computeIfAbsent(prefix, k -> new LinkedList<>()); .flatMap(Collection::stream) .filter( iface -> iface.getAllAddresses().stream().anyMatch(ia -> p.containsIp(ia.getIp()))) .forEach(candidateInterfaces::add);
@JsonIgnore public @Nullable Prefix getPrimaryNetwork() { return _address != null ? _address.getPrefix() : null; }
@Test public void testGetPrefixes() { String nodeName = "nodeName"; String int1 = "int1"; String int2 = "int2"; String address1 = "10.1.3.7/30"; String address2 = "128.212.155.30/24"; String address3 = "124.51.32.2/20"; InterfaceAddress interfaceAddress1 = new InterfaceAddress(address1); InterfaceAddress interfaceAddress2 = new InterfaceAddress(address2); InterfaceAddress interfaceAddress3 = new InterfaceAddress(address3); Map<String, Configuration> configs = new HashMap<>(); Configuration config = createTestConfiguration(nodeName, ConfigurationFormat.HOST, int1, int2); config .getAllInterfaces() .get(int1) .setAllAddresses(ImmutableSet.of(interfaceAddress1, interfaceAddress2)); config .getAllInterfaces() .get(int2) .setAllAddresses(ImmutableSet.of(interfaceAddress2, interfaceAddress3)); configs.put(nodeName, config); assertThat( getPrefixes(configs), equalTo( ImmutableSet.of( interfaceAddress1.getPrefix().toString(), interfaceAddress2.getPrefix().toString(), interfaceAddress3.getPrefix().toString()))); }
/** * Initialize the local RIB -- a RIB containing non-forwarding /32 routes for exact addresses of * interfaces */ @VisibleForTesting void initLocalRib() { // Look at all connected interfaces for (Interface i : _vrf.getInterfaces().values()) { if (i.getActive()) { // Make sure the interface is active // Create a route for each interface prefix for (InterfaceAddress ifaceAddress : i.getAllAddresses()) { if (ifaceAddress.getNetworkBits() < Prefix.MAX_PREFIX_LENGTH) { LocalRoute lr = new LocalRoute(ifaceAddress, i.getName()); _localRib.mergeRoute(lr); } } } } }
/** * Computes a mapping of interface names to the primary {@link Ip} owned by each of the interface. * Filters out the interfaces having no primary {@link InterfaceAddress} */ private static Map<String, Ip> computeInterfaceOwnedPrimaryIp(Map<String, Interface> interfaces) { return interfaces.entrySet().stream() .filter(e -> Objects.nonNull(e.getValue().getAddress())) .collect( ImmutableMap.toImmutableMap(Entry::getKey, e -> e.getValue().getAddress().getIp())); }
@Override public void enterIfi_address(Ifi_addressContext ctx) { Set<InterfaceAddress> allAddresses = _currentInterfaceOrRange.getAllAddresses(); InterfaceAddress address; if (ctx.IP_PREFIX() != null) { address = new InterfaceAddress(ctx.IP_PREFIX().getText()); } else if (ctx.IP_ADDRESS() != null) { Ip ip = Ip.parse(ctx.IP_ADDRESS().getText()); address = new InterfaceAddress(ip, Prefix.MAX_PREFIX_LENGTH); } else { throw new BatfishException("Invalid or missing address"); } _currentInterfaceAddress = address; if (_currentInterfaceOrRange.getPrimaryAddress() == null) { _currentInterfaceOrRange.setPrimaryAddress(address); } if (_currentInterfaceOrRange.getPreferredAddress() == null) { _currentInterfaceOrRange.setPreferredAddress(address); } allAddresses.add(address); Ip ip = address.getIp(); _currentInterfaceOrRange.getAllAddressIps().add(ip); }
@VisibleForTesting public static Set<String> getPrefixes(Map<String, Configuration> configurations) { ImmutableSet.Builder<String> prefixes = ImmutableSet.builder(); configurations .values() .forEach( configuration -> configuration .getAllInterfaces() .values() .forEach( iface -> iface.getAllAddresses().stream() .map(interfaceAddress -> interfaceAddress.getPrefix().toString()) .forEach(prefixes::add))); return prefixes.build(); }
@Override public void exitIf_ip_address(If_ip_addressContext ctx) { InterfaceAddress address; if (ctx.prefix != null) { address = new InterfaceAddress(ctx.prefix.getText()); } else { Ip ip = toIp(ctx.ip); Ip mask = toIp(ctx.subnet); address = new InterfaceAddress(ip, mask); } for (Interface currentInterface : _currentInterfaces) { currentInterface.setAddress(address); } if (ctx.STANDBY() != null) { Ip standbyIp = toIp(ctx.standby_address); InterfaceAddress standbyAddress = new InterfaceAddress(standbyIp, address.getNetworkBits()); for (Interface currentInterface : _currentInterfaces) { currentInterface.setStandbyAddress(standbyAddress); } } }
Set<IpWildcard> blacklist = nonNattedInterfaceAddresses.stream() .map(address -> new IpWildcard(address.getIp(), Ip.ZERO)) .collect(ImmutableSet.toImmutableSet()); Set<IpWildcard> whitelist = nonNattedInterfaceAddresses.stream() .map(address -> new IpWildcard(address.getPrefix())) .collect(ImmutableSet.toImmutableSet()); IpWildcardSetIpSpace ipSpace =
/** Returns {@code true} if any {@link Ip IP address} is owned by both devices. */ private static boolean haveIpInCommon(Interface i1, Interface i2) { for (InterfaceAddress ia : i1.getAllAddresses()) { for (InterfaceAddress ia2 : i2.getAllAddresses()) { if (ia.getIp().equals(ia2.getIp())) { return true; } } } return false; }
public synchronized Pair<InterfaceAddress, InterfaceAddress> getNextGeneratedLinkSubnet() { assert _currentGeneratedIpAsLong % 2 == 0; InterfaceAddress val = new InterfaceAddress(Ip.create(_currentGeneratedIpAsLong), Prefix.MAX_PREFIX_LENGTH - 1); InterfaceAddress val2 = new InterfaceAddress( Ip.create(_currentGeneratedIpAsLong + 1), Prefix.MAX_PREFIX_LENGTH - 1); _currentGeneratedIpAsLong += 2L; return new Pair<>(val, val2); }
.forEach( address -> { if (ipNodeOwners.getOrDefault(address.getIp(), ImmutableSet.of()).size() > 1 || !duplicatesOnly) { .put(COL_VRFNAME, iface.getVrfName()) .put(COL_INTERFACE_NAME, iface.getName()) .put(COL_IP, address.getIp()) .put(COL_MASK, address.getNetworkBits()) .put(COL_ACTIVE, iface.getActive()) .build());
.setOwner(_c1) .setName("iface1") .setAddress(new InterfaceAddress("1.1.1.1/24")) .build(); _c1.setInterfaces(ImmutableSortedMap.of(_iface1.getName(), _iface1)); .setOwner(_c2) .setName("iface2") .setAddress(new InterfaceAddress("2.2.2.2/24")) .build(); _c2.setInterfaces(ImmutableSortedMap.of(_iface2.getName(), _iface2)); ImmutableMap.of( _c1.getHostname(), ImmutableMap.of(_iface1.getName(), _iface1.getAddress().getIp().toIpSpace()), _c2.getHostname(), ImmutableMap.of(_iface2.getName(), _iface2.getAddress().getIp().toIpSpace()))) .build();