Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Ray Milkey | b65d784 | 2017-08-03 16:28:24 -0700 | [diff] [blame] | 17 | package org.onosproject.net.neighbour.impl; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 18 | |
Ray Milkey | b65d784 | 2017-08-03 16:28:24 -0700 | [diff] [blame] | 19 | import java.util.Objects; |
| 20 | |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 21 | import org.onlab.packet.ARP; |
| 22 | import org.onlab.packet.Ethernet; |
| 23 | import org.onlab.packet.ICMP6; |
| 24 | import org.onlab.packet.IPv6; |
| 25 | import org.onlab.packet.Ip4Address; |
| 26 | import org.onlab.packet.Ip6Address; |
| 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onlab.packet.MacAddress; |
| 29 | import org.onlab.packet.VlanId; |
| 30 | import org.onlab.packet.ndp.NeighborSolicitation; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 31 | import org.onosproject.net.ConnectPoint; |
Ray Milkey | b65d784 | 2017-08-03 16:28:24 -0700 | [diff] [blame] | 32 | import org.onosproject.net.intf.Interface; |
| 33 | import org.onosproject.net.neighbour.NeighbourMessageActions; |
| 34 | import org.onosproject.net.neighbour.NeighbourMessageContext; |
| 35 | import org.onosproject.net.neighbour.NeighbourMessageType; |
| 36 | import org.onosproject.net.neighbour.NeighbourProtocol; |
Jonathan Hart | 584ea2d | 2016-10-11 10:49:16 +0200 | [diff] [blame] | 37 | |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 38 | import static com.google.common.base.Preconditions.checkState; |
| 39 | |
| 40 | /** |
| 41 | * Default implementation of a neighbour message context. |
| 42 | */ |
Ray Milkey | b65d784 | 2017-08-03 16:28:24 -0700 | [diff] [blame] | 43 | |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 44 | public class DefaultNeighbourMessageContext implements NeighbourMessageContext { |
| 45 | |
| 46 | private final NeighbourProtocol protocol; |
| 47 | private final NeighbourMessageType type; |
| 48 | |
| 49 | private final IpAddress target; |
| 50 | private final IpAddress sender; |
| 51 | |
| 52 | private final Ethernet eth; |
| 53 | private final ConnectPoint inPort; |
| 54 | |
| 55 | private final NeighbourMessageActions actions; |
| 56 | |
| 57 | /** |
| 58 | * Creates a new neighbour message context. |
| 59 | * |
| 60 | * @param actions actions |
| 61 | * @param eth ethernet frame |
| 62 | * @param inPort incoming port |
| 63 | * @param protocol message protocol |
| 64 | * @param type message type |
| 65 | * @param target target IP address |
| 66 | * @param sender sender IP address |
| 67 | */ |
| 68 | DefaultNeighbourMessageContext(NeighbourMessageActions actions, |
| 69 | Ethernet eth, ConnectPoint inPort, |
| 70 | NeighbourProtocol protocol, |
| 71 | NeighbourMessageType type, |
| 72 | IpAddress target, IpAddress sender) { |
| 73 | this.actions = actions; |
| 74 | this.eth = eth; |
| 75 | this.inPort = inPort; |
| 76 | this.protocol = protocol; |
| 77 | this.type = type; |
| 78 | this.target = target; |
| 79 | this.sender = sender; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public ConnectPoint inPort() { |
| 84 | return inPort; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public Ethernet packet() { |
| 89 | return eth; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public NeighbourProtocol protocol() { |
| 94 | return protocol; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public NeighbourMessageType type() { |
| 99 | return type; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public VlanId vlan() { |
| 104 | return VlanId.vlanId(eth.getVlanID()); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public MacAddress srcMac() { |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 109 | return eth.getSourceMAC(); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public MacAddress dstMac() { |
| 114 | return eth.getDestinationMAC(); |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public IpAddress target() { |
| 119 | return target; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public IpAddress sender() { |
| 124 | return sender; |
| 125 | } |
| 126 | |
| 127 | @Override |
Jonathan Hart | 2efe0c2 | 2016-09-21 12:04:45 -0700 | [diff] [blame] | 128 | public void forward(ConnectPoint outPort) { |
Jonathan Hart | 1e393bb | 2016-09-14 08:51:09 -0700 | [diff] [blame] | 129 | actions.forward(this, outPort); |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
Jonathan Hart | 2efe0c2 | 2016-09-21 12:04:45 -0700 | [diff] [blame] | 133 | public void forward(Interface outIntf) { |
Jonathan Hart | 1e393bb | 2016-09-14 08:51:09 -0700 | [diff] [blame] | 134 | actions.forward(this, outIntf); |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void reply(MacAddress targetMac) { |
| 139 | checkState(type == NeighbourMessageType.REQUEST, "can only reply to requests"); |
| 140 | |
| 141 | actions.reply(this, targetMac); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void flood() { |
| 146 | actions.flood(this); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void drop() { |
| 151 | actions.drop(this); |
| 152 | } |
| 153 | |
Jonathan Hart | 584ea2d | 2016-10-11 10:49:16 +0200 | [diff] [blame] | 154 | @Override |
| 155 | public int hashCode() { |
| 156 | return Objects.hash(protocol, type, target, sender, eth, inPort); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public boolean equals(Object obj) { |
| 161 | if (!(obj instanceof DefaultNeighbourMessageContext)) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | DefaultNeighbourMessageContext that = (DefaultNeighbourMessageContext) obj; |
| 166 | |
| 167 | return Objects.equals(protocol, that.protocol) && |
| 168 | Objects.equals(type, that.type) && |
| 169 | Objects.equals(target, that.target) && |
| 170 | Objects.equals(sender, that.sender) && |
| 171 | Objects.equals(eth, that.eth) && |
| 172 | Objects.equals(inPort, that.inPort); |
| 173 | } |
| 174 | |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 175 | /** |
| 176 | * Attempts to create a MessageContext for the given Ethernet frame. If the |
| 177 | * frame is a valid ARP or NDP request or response, a context will be |
| 178 | * created. |
| 179 | * |
| 180 | * @param eth input Ethernet frame |
| 181 | * @param inPort in port |
Ray Milkey | ef79434 | 2016-11-09 16:20:29 -0800 | [diff] [blame] | 182 | * @param actions actions to take |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 183 | * @return MessageContext if the packet was ARP or NDP, otherwise null |
| 184 | */ |
| 185 | public static NeighbourMessageContext createContext(Ethernet eth, |
| 186 | ConnectPoint inPort, |
| 187 | NeighbourMessageActions actions) { |
| 188 | if (eth.getEtherType() == Ethernet.TYPE_ARP) { |
| 189 | return createArpContext(eth, inPort, actions); |
| 190 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) { |
| 191 | return createNdpContext(eth, inPort, actions); |
| 192 | } |
| 193 | |
| 194 | return null; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Extracts context information from ARP packets. |
| 199 | * |
| 200 | * @param eth input Ethernet frame that is thought to be ARP |
| 201 | * @param inPort in port |
Ray Milkey | ef79434 | 2016-11-09 16:20:29 -0800 | [diff] [blame] | 202 | * @param actions actions to take |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 203 | * @return MessageContext object if the packet was a valid ARP packet, |
| 204 | * otherwise null |
| 205 | */ |
| 206 | private static NeighbourMessageContext createArpContext(Ethernet eth, |
| 207 | ConnectPoint inPort, |
| 208 | NeighbourMessageActions actions) { |
| 209 | if (eth.getEtherType() != Ethernet.TYPE_ARP) { |
| 210 | return null; |
| 211 | } |
| 212 | |
| 213 | ARP arp = (ARP) eth.getPayload(); |
| 214 | |
| 215 | IpAddress target = Ip4Address.valueOf(arp.getTargetProtocolAddress()); |
| 216 | IpAddress sender = Ip4Address.valueOf(arp.getSenderProtocolAddress()); |
| 217 | |
| 218 | NeighbourMessageType type; |
| 219 | if (arp.getOpCode() == ARP.OP_REQUEST) { |
| 220 | type = NeighbourMessageType.REQUEST; |
| 221 | } else if (arp.getOpCode() == ARP.OP_REPLY) { |
| 222 | type = NeighbourMessageType.REPLY; |
| 223 | } else { |
| 224 | return null; |
| 225 | } |
| 226 | |
| 227 | return new DefaultNeighbourMessageContext(actions, eth, inPort, |
| 228 | NeighbourProtocol.ARP, type, target, sender); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Extracts context information from NDP packets. |
| 233 | * |
| 234 | * @param eth input Ethernet frame that is thought to be NDP |
| 235 | * @param inPort in port |
Ray Milkey | ef79434 | 2016-11-09 16:20:29 -0800 | [diff] [blame] | 236 | * @param actions actions to take |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 237 | * @return MessageContext object if the packet was a valid NDP packet, |
| 238 | * otherwise null |
| 239 | */ |
| 240 | private static NeighbourMessageContext createNdpContext(Ethernet eth, |
| 241 | ConnectPoint inPort, |
| 242 | NeighbourMessageActions actions) { |
| 243 | if (eth.getEtherType() != Ethernet.TYPE_IPV6) { |
| 244 | return null; |
| 245 | } |
| 246 | IPv6 ipv6 = (IPv6) eth.getPayload(); |
| 247 | |
| 248 | if (ipv6.getNextHeader() != IPv6.PROTOCOL_ICMP6) { |
| 249 | return null; |
| 250 | } |
| 251 | ICMP6 icmpv6 = (ICMP6) ipv6.getPayload(); |
| 252 | |
| 253 | IpAddress sender = Ip6Address.valueOf(ipv6.getSourceAddress()); |
Pier Ventre | 48ca519 | 2016-11-28 16:52:24 -0800 | [diff] [blame] | 254 | IpAddress target; |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 255 | |
| 256 | NeighbourMessageType type; |
| 257 | if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION) { |
| 258 | type = NeighbourMessageType.REQUEST; |
| 259 | NeighborSolicitation nsol = (NeighborSolicitation) icmpv6.getPayload(); |
| 260 | target = Ip6Address.valueOf(nsol.getTargetAddress()); |
| 261 | } else if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT) { |
| 262 | type = NeighbourMessageType.REPLY; |
Pier Ventre | 48ca519 | 2016-11-28 16:52:24 -0800 | [diff] [blame] | 263 | /* |
| 264 | * sender and target are the same in the reply. |
| 265 | * We use as target the destination ip. |
| 266 | */ |
| 267 | target = Ip6Address.valueOf(ipv6.getDestinationAddress()); |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 268 | } else { |
| 269 | return null; |
| 270 | } |
| 271 | |
| 272 | return new DefaultNeighbourMessageContext(actions, eth, inPort, |
| 273 | NeighbourProtocol.NDP, type, target, sender); |
| 274 | } |
| 275 | |
| 276 | } |