use of org.onlab.packet.IpPrefix in project onos by opennetworkinglab.
the class BgpUpdateMsgVer4 method parseNlri.
/**
* Parses NLRI from channel buffer.
*
* @param cb channelBuffer
* @return list of IP Prefix
* @throws BgpParseException while parsing NLRI
*/
public static LinkedList<IpPrefix> parseNlri(ChannelBuffer cb) throws BgpParseException {
LinkedList<IpPrefix> nlri = new LinkedList<>();
while (cb.readableBytes() > 0) {
int length = cb.readByte();
IpPrefix ipPrefix;
if (length == 0) {
byte[] prefix = new byte[] { 0 };
ipPrefix = Validation.bytesToPrefix(prefix, length);
nlri.add(ipPrefix);
} else {
int len = length / BYTE_IN_BITS;
int reminder = length % BYTE_IN_BITS;
if (reminder > 0) {
len = len + 1;
}
if (cb.readableBytes() < len) {
Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.MALFORMED_ATTRIBUTE_LIST, cb.readableBytes());
}
byte[] prefix = new byte[len];
cb.readBytes(prefix, 0, len);
ipPrefix = Validation.bytesToPrefix(prefix, length);
nlri.add(ipPrefix);
}
}
return nlri;
}
use of org.onlab.packet.IpPrefix in project onos by opennetworkinglab.
the class BgpFsSourcePrefix method read.
/**
* Reads the channel buffer and returns object of IPv4AddressTlv.
*
* @param cb channelBuffer
* @return object of flow spec source prefix
* @throws BgpParseException while parsing BgpFsSourcePrefix
*/
public static BgpFsSourcePrefix read(ChannelBuffer cb) throws BgpParseException {
IpPrefix ipPrefix;
int length = cb.readByte();
if (length == 0) {
byte[] prefix = new byte[] { 0 };
ipPrefix = Validation.bytesToPrefix(prefix, length);
return new BgpFsSourcePrefix((byte) ipPrefix.prefixLength(), ipPrefix);
}
int len = length / BYTE_IN_BITS;
int reminder = length % BYTE_IN_BITS;
if (reminder > 0) {
len = len + 1;
}
if (cb.readableBytes() < len) {
Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.MALFORMED_ATTRIBUTE_LIST, cb.readableBytes());
}
byte[] prefix = new byte[len];
cb.readBytes(prefix, 0, len);
ipPrefix = Validation.bytesToPrefix(prefix, length);
return new BgpFsSourcePrefix((byte) ipPrefix.prefixLength(), ipPrefix);
}
use of org.onlab.packet.IpPrefix in project onos by opennetworkinglab.
the class FpmManager method fpmMessage.
private void fpmMessage(FpmPeer peer, FpmHeader fpmMessage) {
if (fpmMessage.type() == FpmHeader.FPM_TYPE_KEEPALIVE) {
return;
}
Netlink netlink = fpmMessage.netlink();
RtNetlink rtNetlink = netlink.rtNetlink();
if (log.isTraceEnabled()) {
log.trace("Received FPM message: {}", fpmMessage);
}
if (!(rtNetlink.protocol() == RtProtocol.ZEBRA || rtNetlink.protocol() == RtProtocol.UNSPEC)) {
log.trace("Ignoring non-zebra route");
return;
}
IpAddress dstAddress = null;
IpAddress gateway = null;
for (RouteAttribute attribute : rtNetlink.attributes()) {
if (attribute.type() == RouteAttribute.RTA_DST) {
RouteAttributeDst raDst = (RouteAttributeDst) attribute;
dstAddress = raDst.dstAddress();
} else if (attribute.type() == RouteAttribute.RTA_GATEWAY) {
RouteAttributeGateway raGateway = (RouteAttributeGateway) attribute;
gateway = raGateway.gateway();
}
}
if (dstAddress == null) {
log.error("Dst address missing!");
return;
}
IpPrefix prefix = IpPrefix.valueOf(dstAddress, rtNetlink.dstLength());
// Ignore routes that we sent.
if (gateway != null && ((prefix.isIp4() && pdPushNextHopIPv4 != null && pdPushNextHopIPv4.contains(gateway.getIp4Address())) || (prefix.isIp6() && pdPushNextHopIPv6 != null && pdPushNextHopIPv6.contains(gateway.getIp6Address())))) {
if (routeInDhcpStore(prefix) || routeInRipStore(prefix)) {
return;
}
}
List<Route> updates = new LinkedList<>();
List<Route> withdraws = new LinkedList<>();
Route route;
switch(netlink.type()) {
case RTM_NEWROUTE:
if (gateway == null) {
// We ignore interface routes with no gateway for now.
return;
}
route = new Route(Route.Source.FPM, prefix, gateway, clusterService.getLocalNode().id());
Route oldRoute = fpmRoutes.get(peer).put(prefix, route);
if (oldRoute != null) {
log.trace("Swapping {} with {}", oldRoute, route);
withdraws.add(oldRoute);
}
updates.add(route);
break;
case RTM_DELROUTE:
Route existing = fpmRoutes.get(peer).remove(prefix);
if (existing == null) {
log.warn("Got delete for non-existent prefix");
return;
}
route = new Route(Route.Source.FPM, prefix, existing.nextHop(), clusterService.getLocalNode().id());
withdraws.add(route);
break;
case RTM_GETROUTE:
default:
break;
}
updateRouteStore(updates, withdraws);
}
use of org.onlab.packet.IpPrefix in project onos by opennetworkinglab.
the class ConnectivityIntentCommand method buildTrafficSelector.
/**
* Constructs a traffic selector based on the command line arguments
* presented to the command.
* @return traffic selector
*/
protected TrafficSelector buildTrafficSelector() {
IpPrefix srcIpPrefix = null;
IpPrefix dstIpPrefix = null;
TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
if (!isNullOrEmpty(srcIpString)) {
srcIpPrefix = IpPrefix.valueOf(srcIpString);
if (srcIpPrefix.isIp4()) {
selectorBuilder.matchIPSrc(srcIpPrefix);
} else {
selectorBuilder.matchIPv6Src(srcIpPrefix);
}
}
if (!isNullOrEmpty(dstIpString)) {
dstIpPrefix = IpPrefix.valueOf(dstIpString);
if (dstIpPrefix.isIp4()) {
selectorBuilder.matchIPDst(dstIpPrefix);
} else {
selectorBuilder.matchIPv6Dst(dstIpPrefix);
}
}
if ((srcIpPrefix != null) && (dstIpPrefix != null) && (srcIpPrefix.version() != dstIpPrefix.version())) {
// ERROR: IP src/dst version mismatch
throw new IllegalArgumentException("IP source and destination version mismatch");
}
//
// Set the default EthType based on the IP version if the matching
// source or destination IP prefixes.
//
Short ethType = null;
if ((srcIpPrefix != null) && srcIpPrefix.isIp6()) {
ethType = EthType.IPV6.value();
}
if ((dstIpPrefix != null) && dstIpPrefix.isIp6()) {
ethType = EthType.IPV6.value();
}
if (!isNullOrEmpty(ethTypeString)) {
ethType = EthType.parseFromString(ethTypeString);
}
if (ethType != null) {
selectorBuilder.matchEthType(ethType);
}
if (!isNullOrEmpty(vlanString)) {
selectorBuilder.matchVlanId(VlanId.vlanId(Short.parseShort(vlanString)));
}
if (!isNullOrEmpty(srcMacString)) {
selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMacString));
}
if (!isNullOrEmpty(dstMacString)) {
selectorBuilder.matchEthDst(MacAddress.valueOf(dstMacString));
}
if (!isNullOrEmpty(ipProtoString)) {
short ipProtoShort = IpProtocol.parseFromString(ipProtoString);
selectorBuilder.matchIPProtocol((byte) ipProtoShort);
}
if (!isNullOrEmpty(fLabelString)) {
selectorBuilder.matchIPv6FlowLabel(Integer.parseInt(fLabelString));
}
if (!isNullOrEmpty(icmp6TypeString)) {
byte icmp6Type = Icmp6Type.parseFromString(icmp6TypeString);
selectorBuilder.matchIcmpv6Type(icmp6Type);
}
if (!isNullOrEmpty(icmp6CodeString)) {
byte icmp6Code = Icmp6Code.parseFromString(icmp6CodeString);
selectorBuilder.matchIcmpv6Code(icmp6Code);
}
if (!isNullOrEmpty(ndTargetString)) {
selectorBuilder.matchIPv6NDTargetAddress(Ip6Address.valueOf(ndTargetString));
}
if (!isNullOrEmpty(ndSllString)) {
selectorBuilder.matchIPv6NDSourceLinkLayerAddress(MacAddress.valueOf(ndSllString));
}
if (!isNullOrEmpty(ndTllString)) {
selectorBuilder.matchIPv6NDTargetLinkLayerAddress(MacAddress.valueOf(ndTllString));
}
if (!isNullOrEmpty(srcTcpString)) {
selectorBuilder.matchTcpSrc(TpPort.tpPort(Integer.parseInt(srcTcpString)));
}
if (!isNullOrEmpty(dstTcpString)) {
selectorBuilder.matchTcpDst(TpPort.tpPort(Integer.parseInt(dstTcpString)));
}
if (extHdrStringList != null) {
short extHdr = 0;
for (String extHdrString : extHdrStringList) {
extHdr = (short) (extHdr | ExtHeader.parseFromString(extHdrString));
}
selectorBuilder.matchIPv6ExthdrFlags(extHdr);
}
return selectorBuilder.build();
}
use of org.onlab.packet.IpPrefix in project onos by opennetworkinglab.
the class LispExtensionMappingAddressInterpreter method afi2mapping.
/**
* Converts AFI address to generalized mapping address.
*
* @param afi IP typed AFI address
* @return generalized mapping address
*/
private MappingAddress afi2mapping(LispAfiAddress afi) {
switch(afi.getAfi()) {
case IP4:
IpAddress ipv4Address = ((LispIpv4Address) afi).getAddress();
IpPrefix ipv4Prefix = IpPrefix.valueOf(ipv4Address, IPV4_PREFIX_LENGTH);
return MappingAddresses.ipv4MappingAddress(ipv4Prefix);
case IP6:
IpAddress ipv6Address = ((LispIpv6Address) afi).getAddress();
IpPrefix ipv6Prefix = IpPrefix.valueOf(ipv6Address, IPV6_PREFIX_LENGTH);
return MappingAddresses.ipv6MappingAddress(ipv6Prefix);
default:
log.warn("Only support to convert IP address type");
break;
}
return null;
}
Aggregations