use of org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT in project onos by opennetworkinglab.
the class DefaultNeighbourMessageActions method buildNdpReply.
/**
* Builds an NDP reply based on a request.
*
* @param srcIp the IP address to use as the reply source
* @param srcMac the MAC address to use as the reply source
* @param request the Neighbor Solicitation request we got
* @param isRouter true if this reply is sent on behalf of a router
* @return an Ethernet frame containing the Neighbor Advertisement reply
*/
private Ethernet buildNdpReply(Ip6Address srcIp, MacAddress srcMac, Ethernet request, boolean isRouter) {
Ethernet eth = new Ethernet();
eth.setDestinationMACAddress(request.getSourceMAC());
eth.setSourceMACAddress(srcMac);
eth.setEtherType(Ethernet.TYPE_IPV6);
eth.setVlanID(request.getVlanID());
IPv6 requestIp = (IPv6) request.getPayload();
IPv6 ipv6 = new IPv6();
ipv6.setSourceAddress(srcIp.toOctets());
ipv6.setDestinationAddress(requestIp.getSourceAddress());
ipv6.setHopLimit((byte) 255);
ICMP6 icmp6 = new ICMP6();
icmp6.setIcmpType(ICMP6.NEIGHBOR_ADVERTISEMENT);
icmp6.setIcmpCode((byte) 0);
NeighborAdvertisement nadv = new NeighborAdvertisement();
nadv.setTargetAddress(srcIp.toOctets());
nadv.setSolicitedFlag((byte) 1);
nadv.setOverrideFlag((byte) 1);
if (isRouter) {
nadv.setRouterFlag((byte) 1);
}
nadv.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, srcMac.toBytes());
icmp6.setPayload(nadv);
ipv6.setPayload(icmp6);
eth.setPayload(ipv6);
return eth;
}
use of org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT in project onos by opennetworkinglab.
the class DefaultNeighbourMessageContext method createNdpContext.
/**
* Extracts context information from NDP packets.
*
* @param eth input Ethernet frame that is thought to be NDP
* @param inPort in port
* @param actions actions to take
* @return MessageContext object if the packet was a valid NDP packet,
* otherwise null
*/
private static NeighbourMessageContext createNdpContext(Ethernet eth, ConnectPoint inPort, NeighbourMessageActions actions) {
if (eth.getEtherType() != Ethernet.TYPE_IPV6) {
return null;
}
IPv6 ipv6 = (IPv6) eth.getPayload();
if (ipv6.getNextHeader() != IPv6.PROTOCOL_ICMP6) {
return null;
}
ICMP6 icmpv6 = (ICMP6) ipv6.getPayload();
IpAddress sender = Ip6Address.valueOf(ipv6.getSourceAddress());
IpAddress target;
NeighbourMessageType type;
if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION) {
type = NeighbourMessageType.REQUEST;
NeighborSolicitation nsol = (NeighborSolicitation) icmpv6.getPayload();
target = Ip6Address.valueOf(nsol.getTargetAddress());
} else if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT) {
type = NeighbourMessageType.REPLY;
/*
* sender and target are the same in the reply.
* We use as target the destination ip.
*/
target = Ip6Address.valueOf(ipv6.getDestinationAddress());
} else {
return null;
}
return new DefaultNeighbourMessageContext(actions, eth, inPort, NeighbourProtocol.NDP, type, target, sender);
}
use of org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT in project onos by opennetworkinglab.
the class NeighborAdvertisement method buildNdpAdv.
/**
* Builds an NDP reply based on a request.
*
* @param srcIp the IP address to use as the reply source
* @param srcMac the MAC address to use as the reply source
* @param request the Neighbor Solicitation request we got
* @return an Ethernet frame containing the Neighbor Advertisement reply
*/
public static Ethernet buildNdpAdv(Ip6Address srcIp, MacAddress srcMac, Ethernet request) {
checkNotNull(srcIp, "IP address cannot be null");
checkNotNull(srcMac, "MAC address cannot be null");
checkNotNull(request, "Request cannot be null");
checkArgument(request.getEtherType() == Ethernet.TYPE_IPV6, "EtherType must be IPv6");
final IPv6 ipv6Request = (IPv6) request.getPayload();
checkArgument(ipv6Request.getNextHeader() == IPv6.PROTOCOL_ICMP6, "Protocol must be ICMP6");
final ICMP6 icmpv6 = (ICMP6) ipv6Request.getPayload();
checkArgument(icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION, "ICMP6 type must be NEIGHBOR_SOLICITATION");
Ethernet eth = new Ethernet();
eth.setDestinationMACAddress(request.getSourceMAC());
eth.setSourceMACAddress(srcMac);
eth.setEtherType(Ethernet.TYPE_IPV6);
eth.setVlanID(request.getVlanID());
IPv6 ipv6 = new IPv6();
ipv6.setSourceAddress(srcIp.toOctets());
ipv6.setDestinationAddress(ipv6Request.getSourceAddress());
ipv6.setHopLimit(NDP_HOP_LIMIT);
ipv6.setNextHeader(IPv6.PROTOCOL_ICMP6);
ICMP6 icmp6 = new ICMP6();
icmp6.setIcmpType(ICMP6.NEIGHBOR_ADVERTISEMENT);
icmp6.setIcmpCode(RESERVED_CODE);
NeighborAdvertisement nadv = new NeighborAdvertisement();
nadv.setTargetAddress(srcIp.toOctets());
nadv.setSolicitedFlag(NDP_SOLICITED_FLAG);
nadv.setOverrideFlag(NDP_OVERRIDE_FLAG);
nadv.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, srcMac.toBytes());
icmp6.setPayload(nadv);
ipv6.setPayload(icmp6);
eth.setPayload(ipv6);
return eth;
}
use of org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT in project onos by opennetworkinglab.
the class NeighborAdvertisementTest method testBuildNdpAdv.
/**
* Test Neighbor Advertisement reply build.
*/
@Test
public void testBuildNdpAdv() {
Ethernet eth = new Ethernet();
eth.setSourceMACAddress(MAC_ADDRESS);
eth.setDestinationMACAddress(MAC_ADDRESS2);
IPv6 ipv6 = new IPv6();
ipv6.setSourceAddress(IPV6_SOURCE_ADDRESS);
ipv6.setDestinationAddress(IPV6_DESTINATION_ADDRESS);
ipv6.setNextHeader(IPv6.PROTOCOL_ICMP6);
eth.setEtherType(Ethernet.TYPE_IPV6);
eth.setPayload(ipv6);
ICMP6 icmp6 = new ICMP6();
icmp6.setIcmpType(ICMP6.NEIGHBOR_SOLICITATION);
icmp6.setIcmpCode(NeighborAdvertisement.RESERVED_CODE);
ipv6.setPayload(icmp6);
final Ethernet ethResponse = NeighborAdvertisement.buildNdpAdv(IP_6_ADDRESS, MAC_ADDRESS2, eth);
assertTrue(ethResponse.getDestinationMAC().equals(MAC_ADDRESS));
assertTrue(ethResponse.getSourceMAC().equals(MAC_ADDRESS2));
assertTrue(ethResponse.getEtherType() == Ethernet.TYPE_IPV6);
final IPv6 responseIpv6 = (IPv6) ethResponse.getPayload();
assertArrayEquals(responseIpv6.getSourceAddress(), ipv6.getDestinationAddress());
assertArrayEquals(responseIpv6.getDestinationAddress(), ipv6.getSourceAddress());
assertTrue(responseIpv6.getNextHeader() == IPv6.PROTOCOL_ICMP6);
final ICMP6 responseIcmp6 = (ICMP6) responseIpv6.getPayload();
assertTrue(responseIcmp6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT);
assertTrue(responseIcmp6.getIcmpCode() == NeighborAdvertisement.RESERVED_CODE);
final NeighborAdvertisement responseNadv = (NeighborAdvertisement) responseIcmp6.getPayload();
assertArrayEquals(responseNadv.getTargetAddress(), IPV6_DESTINATION_ADDRESS);
assertTrue(responseNadv.getSolicitedFlag() == NeighborAdvertisement.NDP_SOLICITED_FLAG);
assertTrue(responseNadv.getOverrideFlag() == NeighborAdvertisement.NDP_OVERRIDE_FLAG);
assertThat(responseNadv.getOptions(), hasItem(hasOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, MAC_ADDRESS2.toBytes())));
}
Aggregations