blob: 9cf000f8c637e7ffa5d0fdb33dc184e535822d22 [file] [log] [blame]
Jonathan Hart9bdaaec2016-08-22 13:33:45 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jonathan Hart9bdaaec2016-08-22 13:33:45 -07003 *
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 Milkeyb65d7842017-08-03 16:28:24 -070017package org.onosproject.net.neighbour.impl;
Jonathan Hart9bdaaec2016-08-22 13:33:45 -070018
Ray Milkeyb65d7842017-08-03 16:28:24 -070019import java.util.Objects;
20
Jonathan Hart9bdaaec2016-08-22 13:33:45 -070021import org.onlab.packet.ARP;
22import org.onlab.packet.Ethernet;
23import org.onlab.packet.ICMP6;
24import org.onlab.packet.IPv6;
25import org.onlab.packet.Ip4Address;
26import org.onlab.packet.Ip6Address;
27import org.onlab.packet.IpAddress;
28import org.onlab.packet.MacAddress;
29import org.onlab.packet.VlanId;
30import org.onlab.packet.ndp.NeighborSolicitation;
Jonathan Hart9bdaaec2016-08-22 13:33:45 -070031import org.onosproject.net.ConnectPoint;
Ray Milkeyb65d7842017-08-03 16:28:24 -070032import org.onosproject.net.intf.Interface;
33import org.onosproject.net.neighbour.NeighbourMessageActions;
34import org.onosproject.net.neighbour.NeighbourMessageContext;
35import org.onosproject.net.neighbour.NeighbourMessageType;
36import org.onosproject.net.neighbour.NeighbourProtocol;
Jonathan Hart584ea2d2016-10-11 10:49:16 +020037
Jonathan Hart9bdaaec2016-08-22 13:33:45 -070038import static com.google.common.base.Preconditions.checkState;
39
40/**
41 * Default implementation of a neighbour message context.
42 */
Ray Milkeyb65d7842017-08-03 16:28:24 -070043
Jonathan Hart9bdaaec2016-08-22 13:33:45 -070044public 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
Charles Chaneded6882018-06-29 14:28:39 -070057 private boolean isRouter;
58
Jonathan Hart9bdaaec2016-08-22 13:33:45 -070059 /**
60 * Creates a new neighbour message context.
61 *
62 * @param actions actions
63 * @param eth ethernet frame
64 * @param inPort incoming port
65 * @param protocol message protocol
66 * @param type message type
67 * @param target target IP address
68 * @param sender sender IP address
69 */
70 DefaultNeighbourMessageContext(NeighbourMessageActions actions,
71 Ethernet eth, ConnectPoint inPort,
72 NeighbourProtocol protocol,
73 NeighbourMessageType type,
74 IpAddress target, IpAddress sender) {
75 this.actions = actions;
76 this.eth = eth;
77 this.inPort = inPort;
78 this.protocol = protocol;
79 this.type = type;
80 this.target = target;
81 this.sender = sender;
Charles Chaneded6882018-06-29 14:28:39 -070082 this.isRouter = false;
Jonathan Hart9bdaaec2016-08-22 13:33:45 -070083 }
84
85 @Override
86 public ConnectPoint inPort() {
87 return inPort;
88 }
89
90 @Override
91 public Ethernet packet() {
92 return eth;
93 }
94
95 @Override
96 public NeighbourProtocol protocol() {
97 return protocol;
98 }
99
100 @Override
101 public NeighbourMessageType type() {
102 return type;
103 }
104
105 @Override
106 public VlanId vlan() {
107 return VlanId.vlanId(eth.getVlanID());
108 }
109
110 @Override
111 public MacAddress srcMac() {
Jonathan Hartc4f681c2016-09-09 07:14:25 -0700112 return eth.getSourceMAC();
113 }
114
115 @Override
116 public MacAddress dstMac() {
117 return eth.getDestinationMAC();
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700118 }
119
120 @Override
121 public IpAddress target() {
122 return target;
123 }
124
125 @Override
126 public IpAddress sender() {
127 return sender;
128 }
129
130 @Override
Jonathan Hart2efe0c22016-09-21 12:04:45 -0700131 public void forward(ConnectPoint outPort) {
Jonathan Hart1e393bb2016-09-14 08:51:09 -0700132 actions.forward(this, outPort);
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700133 }
134
135 @Override
Jonathan Hart2efe0c22016-09-21 12:04:45 -0700136 public void forward(Interface outIntf) {
Jonathan Hart1e393bb2016-09-14 08:51:09 -0700137 actions.forward(this, outIntf);
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700138 }
139
140 @Override
141 public void reply(MacAddress targetMac) {
142 checkState(type == NeighbourMessageType.REQUEST, "can only reply to requests");
143
144 actions.reply(this, targetMac);
145 }
146
147 @Override
148 public void flood() {
149 actions.flood(this);
150 }
151
152 @Override
153 public void drop() {
154 actions.drop(this);
155 }
156
Jonathan Hart584ea2d2016-10-11 10:49:16 +0200157 @Override
Charles Chaneded6882018-06-29 14:28:39 -0700158 public boolean isRouter() {
159 return this.isRouter;
160 }
161
162 @Override
163 public void setIsRouter(boolean isRouter) {
164 this.isRouter = isRouter;
165 }
166
167 @Override
Jonathan Hart584ea2d2016-10-11 10:49:16 +0200168 public int hashCode() {
Charles Chaneded6882018-06-29 14:28:39 -0700169 return Objects.hash(protocol, type, target, sender, eth, inPort, isRouter);
Jonathan Hart584ea2d2016-10-11 10:49:16 +0200170 }
171
172 @Override
173 public boolean equals(Object obj) {
174 if (!(obj instanceof DefaultNeighbourMessageContext)) {
175 return false;
176 }
177
178 DefaultNeighbourMessageContext that = (DefaultNeighbourMessageContext) obj;
179
180 return Objects.equals(protocol, that.protocol) &&
181 Objects.equals(type, that.type) &&
182 Objects.equals(target, that.target) &&
183 Objects.equals(sender, that.sender) &&
184 Objects.equals(eth, that.eth) &&
Charles Chaneded6882018-06-29 14:28:39 -0700185 Objects.equals(inPort, that.inPort) &&
186 Objects.equals(isRouter, that.isRouter);
Jonathan Hart584ea2d2016-10-11 10:49:16 +0200187 }
188
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700189 /**
190 * Attempts to create a MessageContext for the given Ethernet frame. If the
191 * frame is a valid ARP or NDP request or response, a context will be
192 * created.
193 *
194 * @param eth input Ethernet frame
195 * @param inPort in port
Ray Milkeyef794342016-11-09 16:20:29 -0800196 * @param actions actions to take
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700197 * @return MessageContext if the packet was ARP or NDP, otherwise null
198 */
199 public static NeighbourMessageContext createContext(Ethernet eth,
200 ConnectPoint inPort,
201 NeighbourMessageActions actions) {
202 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
203 return createArpContext(eth, inPort, actions);
204 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
205 return createNdpContext(eth, inPort, actions);
206 }
207
208 return null;
209 }
210
211 /**
212 * Extracts context information from ARP packets.
213 *
214 * @param eth input Ethernet frame that is thought to be ARP
215 * @param inPort in port
Ray Milkeyef794342016-11-09 16:20:29 -0800216 * @param actions actions to take
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700217 * @return MessageContext object if the packet was a valid ARP packet,
218 * otherwise null
219 */
220 private static NeighbourMessageContext createArpContext(Ethernet eth,
221 ConnectPoint inPort,
222 NeighbourMessageActions actions) {
223 if (eth.getEtherType() != Ethernet.TYPE_ARP) {
224 return null;
225 }
226
227 ARP arp = (ARP) eth.getPayload();
228
229 IpAddress target = Ip4Address.valueOf(arp.getTargetProtocolAddress());
230 IpAddress sender = Ip4Address.valueOf(arp.getSenderProtocolAddress());
231
232 NeighbourMessageType type;
233 if (arp.getOpCode() == ARP.OP_REQUEST) {
234 type = NeighbourMessageType.REQUEST;
235 } else if (arp.getOpCode() == ARP.OP_REPLY) {
236 type = NeighbourMessageType.REPLY;
237 } else {
238 return null;
239 }
240
241 return new DefaultNeighbourMessageContext(actions, eth, inPort,
242 NeighbourProtocol.ARP, type, target, sender);
243 }
244
245 /**
246 * Extracts context information from NDP packets.
247 *
248 * @param eth input Ethernet frame that is thought to be NDP
249 * @param inPort in port
Ray Milkeyef794342016-11-09 16:20:29 -0800250 * @param actions actions to take
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700251 * @return MessageContext object if the packet was a valid NDP packet,
252 * otherwise null
253 */
254 private static NeighbourMessageContext createNdpContext(Ethernet eth,
255 ConnectPoint inPort,
256 NeighbourMessageActions actions) {
257 if (eth.getEtherType() != Ethernet.TYPE_IPV6) {
258 return null;
259 }
260 IPv6 ipv6 = (IPv6) eth.getPayload();
261
262 if (ipv6.getNextHeader() != IPv6.PROTOCOL_ICMP6) {
263 return null;
264 }
265 ICMP6 icmpv6 = (ICMP6) ipv6.getPayload();
266
267 IpAddress sender = Ip6Address.valueOf(ipv6.getSourceAddress());
Pier Ventre48ca5192016-11-28 16:52:24 -0800268 IpAddress target;
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700269
270 NeighbourMessageType type;
271 if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION) {
272 type = NeighbourMessageType.REQUEST;
273 NeighborSolicitation nsol = (NeighborSolicitation) icmpv6.getPayload();
274 target = Ip6Address.valueOf(nsol.getTargetAddress());
275 } else if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT) {
276 type = NeighbourMessageType.REPLY;
Pier Ventre48ca5192016-11-28 16:52:24 -0800277 /*
278 * sender and target are the same in the reply.
279 * We use as target the destination ip.
280 */
281 target = Ip6Address.valueOf(ipv6.getDestinationAddress());
Jonathan Hart9bdaaec2016-08-22 13:33:45 -0700282 } else {
283 return null;
284 }
285
286 return new DefaultNeighbourMessageContext(actions, eth, inPort,
287 NeighbourProtocol.NDP, type, target, sender);
288 }
289
290}